MySemaphoreWait

decompiled_code:
void __fastcall MySemaphoreWait(__int64 a1)
{
int v1; // eax
_QWORD *v2; // rax
__int64 v3; // rbx
const ucontext_t *v4; // rdi
__int64 v5; // [rsp+8h] [rbp-10h]

if ( a1 && !*(_DWORD *)(a1 + 4) )
{
v5 = currThread;
getcontext(*(ucontext_t **)(currThread + 16LL));
v1 = *(_DWORD *)a1;
if ( *(int *)a1 <= 0 )
{
++*(_DWORD *)(v5 + 8);
v2 = malloc(0x10uLL);
if ( v2 )
{
*v2 = v5;
if ( *(_QWORD *)(a1 + 16) )
{
*(_QWORD *)(*(_QWORD *)(a1 + 24) + 8LL) = v2;
*(_QWORD *)(a1 + 24) = v2;
}
else
{
*(_QWORD *)(a1 + 24) = v2;
*(_QWORD *)(a1 + 16) = v2;
}
v2[1] = 0LL;
}
else
{
perror("ERROR:Not enough memory\nCan't add the thread to semaphore wait queue\n");
}
v3 = remFromQueue(&readyQueue);
if ( !v3 )
setcontext(mainPrg);
v4 = *(const ucontext_t **)(v3 + 16);
currThread = v3;
setcontext(v4);
}
else
{
++*(_DWORD *)(a1 + 8);
*(_DWORD *)a1 = v1 - 1;
}
}
}

canonical_decompiled_code:
void __fastcall func1(__int64 a1)
{
int v1; // eax
_QWORD *v2; // rax
__int64 v3; // rbx
const ucontext_t *v4; // rdi
__int64 v5; // [rsp+8h] [rbp-10h]

if ( a1 && !*(_DWORD *)(a1 + 4) )
{
v5 = currThread;
getcontext(*(ucontext_t **)(currThread + 16LL));
v1 = *(_DWORD *)a1;
if ( *(int *)a1 <= 0 )
{
++*(_DWORD *)(v5 + 8);
v2 = malloc(0x10uLL);
if ( v2 )
{
*v2 = v5;
if ( *(_QWORD *)(a1 + 16) )
{
*(_QWORD *)(*(_QWORD *)(a1 + 24) + 8LL) = v2;
*(_QWORD *)(a1 + 24) = v2;
}
else
{
*(_QWORD *)(a1 + 24) = v2;
*(_QWORD *)(a1 + 16) = v2;
}
v2[1] = 0LL;
}
else
{
perror("ERROR:Not enough memory\nCan't add the thread to semaphore wait queue\n");
}
v3 = remFromQueue(&readyQueue);
if ( !v3 )
setcontext(mainPrg);
v4 = *(const ucontext_t **)(v3 + 16);
currThread = v3;
setcontext(v4);
}
else
{
++*(_DWORD *)(a1 + 8);
*(_DWORD *)a1 = v1 - 1;
}
}
}

original_code:
void MySemaphoreWait(MySemaphore sem)
{
 if(sem!=((void *)0))
 {
  Mythread *t;
  Mysemaphore *semptr = (Mysemaphore*)sem;
  if(semptr -> destroyed == 0)
  {
   t = currThread;
   getcontext(t -> thread);
   if(semptr -> value > 0)
   {
    semptr -> value--;
    semptr -> in_use++;
   }
   else
   {
    t -> blocked = t -> blocked+1;
    MysemNode *sn;
    sn = malloc(sizeof(MysemNode));
    if(sn == ((void *)0)) perror("ERROR:Not enough memory\nCan't add the thread to semaphore wait queue\n");
    else
    {
     sn -> thread = t;
     if(semptr->head == ((void *)0)) semptr->head = semptr-> tail = sn;
     else
     {
      semptr->tail->next = sn;
      semptr->tail = sn;
     }
     sn -> next = ((void *)0);
    }
    t = remFromQueue(&readyQueue);
    if(t == ((void *)0))
    {
     setcontext(mainPrg);
    }
    currThread = t;
    setcontext(t->thread);
   }
  }
 }
}

canonical_original_code:
void MySemaphoreWait(void *sem)
{
 if(sem!=((void *)0))
 {
  struct Mythread_ *t;
  struct Mysemaphore_ *semptr = (Mysemaphore*)sem;
  if(semptr -> destroyed == 0)
  {
   t = currThread;
   getcontext(t -> thread);
   if(semptr -> value > 0)
   {
    semptr -> value--;
    semptr -> in_use++;
   }
   else
   {
    t -> blocked = t -> blocked+1;
    struct MysemNode_ *sn;
    sn = malloc(sizeof(MysemNode));
    if(sn == ((void *)0)) perror("ERROR:Not enough memory\nCan't add the thread to semaphore wait queue\n");
    else
    {
     sn -> thread = t;
     if(semptr->head == ((void *)0)) semptr->head = semptr-> tail = sn;
     else
     {
      semptr->tail->next = sn;
      semptr->tail = sn;
     }
     sn -> next = ((void *)0);
    }
    t = remFromQueue(&readyQueue);
    if(t == ((void *)0))
    {
     setcontext(mainPrg);
    }
    currThread = t;
    setcontext(t->thread);
   }
  }
 }
}

variable_types: struct MysemNode_ { struct Mythread_ { int threadId; int yieldFlag; int blocked; int parentBlocked; struct ucontext_t { unsigned long uc_flags; struct ucontext_t *uc_link; struct stack_t { void *ss_sp; int ss_flags; unsigned long ss_size; } uc_stack; struct mcontext_t { long long gregs[23]; struct _libc_fpstate { unsigned short cwd; unsigned short swd; unsigned short ftw; unsigned short fop; unsigned long rip; unsigned long rdp; unsigned int mxcsr; unsigned int mxcr_mask; struct _libc_fpxreg { unsigned short significand[4]; unsigned short exponent; unsigned short __glibc_reserved1[3]; } _st[8]; struct _libc_xmmreg { unsigned int element[4]; } _xmm[16]; unsigned int __glibc_reserved1[24]; } *fpregs; unsigned long long __reserved1[8]; } uc_mcontext; struct __sigset_t { unsigned long __val[-1]; } uc_sigmask; struct _libc_fpstate __fpregs_mem; unsigned long long __ssp[4]; } *thread; struct Mythread_ *next; struct Mythread_ *childList; struct Mythread_ *sibling; struct Mythread_ *parentptr; } *thread; struct MysemNode_ *next; } *


MySemaphoreDestroy

decompiled_code:
__int64 __fastcall MySemaphoreDestroy(__int64 a1)
{
__int64 result; // rax

if ( !a1 )
return 0xFFFFFFFFLL;
if ( *(_QWORD *)(a1 + 16) )
return 0xFFFFFFFFLL;
result = *(unsigned int *)(a1 + 8);
if ( (_DWORD)result )
return 0xFFFFFFFFLL;
*(_DWORD *)(a1 + 4) = 1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
__int64 result; // rax

if ( !a1 )
return 0xFFFFFFFFLL;
if ( *(_QWORD *)(a1 + 16) )
return 0xFFFFFFFFLL;
result = *(unsigned int *)(a1 + 8);
if ( (_DWORD)result )
return 0xFFFFFFFFLL;
*(_DWORD *)(a1 + 4) = 1;
return result;
}

original_code:
int MySemaphoreDestroy(MySemaphore sem)
{
 Mysemaphore *semptr = (Mysemaphore*)sem;
 if(semptr == ((void *)0)) return -1;
 else if (semptr -> head != ((void *)0) || semptr -> in_use != 0) return -1;
 else
 {
  semptr -> destroyed = 1;
  return 0;
 }
}

canonical_original_code:
int MySemaphoreDestroy(void *sem)
{
 struct Mysemaphore_ *semptr = (Mysemaphore*)sem;
 if(semptr == ((void *)0)) return -1;
 else if (semptr -> head != ((void *)0) || semptr -> in_use != 0) return -1;
 else
 {
  semptr -> destroyed = 1;
  return 0;
 }
}

variable_types:
sem
void *

variable_types: struct Mysemaphore_ { int value; int destroyed; int in_use; struct MysemNode_ { struct Mythread_ { int threadId; int yieldFlag; int blocked; int parentBlocked; struct ucontext_t { unsigned long uc_flags; struct ucontext_t *uc_link; struct stack_t { void *ss_sp; int ss_flags; unsigned long ss_size; } uc_stack; struct mcontext_t { long long gregs[23]; struct _libc_fpstate { unsigned short cwd; unsigned short swd; unsigned short ftw; unsigned short fop; unsigned long rip; unsigned long rdp; unsigned int mxcsr; unsigned int mxcr_mask; struct _libc_fpxreg { unsigned short significand[4]; unsigned short exponent; unsigned short __glibc_reserved1[3]; } _st[8]; struct _libc_xmmreg { unsigned int element[4]; } _xmm[16]; unsigned int __glibc_reserved1[24]; } *fpregs; unsigned long long __reserved1[8]; } uc_mcontext; struct __sigset_t { unsigned long __val[-1]; } uc_sigmask; struct _libc_fpstate __fpregs_mem; unsigned long long __ssp[4]; } *thread; struct Mythread_ *next; struct Mythread_ *childList; struct Mythread_ *sibling; struct Mythread_ *parentptr; } *thread; struct MysemNode_ *next; } *head; } *

return_type: int

user_defined_types: struct Mysemaphore_ { int value; int destroyed; int in_use; struct MysemNode_ *head; }

user_defined_types: struct MysemNode_ { struct Mythread_ *thread; struct MysemNode_ *next; }

user_defined_types: struct Mythread_ { int threadId; int yieldFlag; int blocked; int parentBlocked; struct ucontext_t *thread; struct Mythread_ *next; struct Mythread_ *childList; struct Mythread_ *sibling; struct Mythread_ *parentptr; }

user_defined_types: struct ucontext_t { unsigned long uc_flags; struct ucontext_t *uc_link; struct stack_t uc_stack; struct mcontext_t uc_mcontext; struct __sigset_t uc_sigmask; struct _libc_fpstate __fpregs_mem; unsigned long long __ssp[4]; }

user_defined_types: struct mcontext_t { long long gregs[23]; struct _libc_fpstate *fpregs; unsigned long long __reserved1[8]; }

user_defined_types: struct _libc_fpstate { unsigned short cwd; unsigned short swd; unsigned short ftw; unsigned short fop; unsigned long rip; unsigned long rdp; unsigned int mxcsr; unsigned int mxcr_mask; struct _libc_fpxreg _st[8]; struct _libc_xmmreg _xmm[16]; unsigned int __glibc_reserved1[24]; }


MyThreadRun

decompiled_code:
void MyThreadRun(void)
{
free(mainPrg->uc_stack.ss_sp);
free(mainPrg);
}

canonical_decompiled_code:
void func0(void)
{
free(mainPrg->uc_stack.ss_sp);
free(mainPrg);
}

original_code:
void MyThreadRun(void)
{
 free(mainPrg -> uc_stack.ss_sp);
 free(mainPrg);
}

canonical_original_code:
void MyThreadRun()
{
 free(mainPrg -> uc_stack.ss_sp);
 free(mainPrg);
}

return_type: void


MyThreadInit

decompiled_code:
int __fastcall MyThreadInit(void (*a1)(void), __int64 a2)
{
_QWORD *v2; // rax
_QWORD *v3; // rbx
ucontext_t *v4; // rax
ucontext_t *v5; // rbp
void *v6; // rax
ucontext_t *v7; // rbp
void *v8; // rax
const ucontext_t *v9; // rdi

id = 0;
*(&readyQueue + 1) = 0LL;
readyQueue = 0LL;
v2 = malloc(0x38uLL);
mainThread = (__int64)v2;
if ( !v2 )
goto LABEL_7;
v3 = v2;
v4 = (ucontext_t *)malloc(0x3C8uLL);
v3[2] = v4;
v5 = v4;
if ( !v4 )
goto LABEL_7;
v6 = malloc(0x2000uLL);
sizeArr = (__int64)v6;
if ( !v6 )
goto LABEL_7;
*v3 = 0LL;
v3[1] = 0LL;
v3[6] = 0LL;
v3[5] = 0LL;
v3[4] = 0LL;
v5->uc_link = 0LL;
v5->uc_stack.ss_sp = v6;
v5->uc_stack.ss_size = 0x2000LL;
getcontext(v5);
makecontext(*(ucontext_t **)(mainThread + 16), a1, 1, a2);
mainPrg = (ucontext_t *)malloc(0x3C8uLL);
v7 = mainPrg;
if ( !mainPrg || (v8 = malloc(0x2000uLL), (sizeArr = (__int64)v8) == 0) )
{
LABEL_7:
perror("ERROR:Not enough memory to create main Thread.. exiting..\n");
exit(-1);
}
v7->uc_link = 0LL;
v7->uc_stack.ss_sp = v8;
v7->uc_stack.ss_size = 0x2000LL;
getcontext(v7);
makecontext(mainPrg, MyThreadRun, 1, a2);
++id;
v9 = *(const ucontext_t **)(mainThread + 16);
currThread = mainThread;
return setcontext(v9);
}

canonical_decompiled_code:
int __fastcall func1(void (*a1)(void), __int64 a2)
{
_QWORD *v2; // rax
_QWORD *v3; // rbx
ucontext_t *v4; // rax
ucontext_t *v5; // rbp
void *v6; // rax
ucontext_t *v7; // rbp
void *v8; // rax
const ucontext_t *v9; // rdi

id = 0;
*(&readyQueue + 1) = 0LL;
readyQueue = 0LL;
v2 = malloc(0x38uLL);
mainThread = (__int64)v2;
if ( !v2 )
goto LABEL_7;
v3 = v2;
v4 = (ucontext_t *)malloc(0x3C8uLL);
v3[2] = v4;
v5 = v4;
if ( !v4 )
goto LABEL_7;
v6 = malloc(0x2000uLL);
sizeArr = (__int64)v6;
if ( !v6 )
goto LABEL_7;
*v3 = 0LL;
v3[1] = 0LL;
v3[6] = 0LL;
v3[5] = 0LL;
v3[4] = 0LL;
v5->uc_link = 0LL;
v5->uc_stack.ss_sp = v6;
v5->uc_stack.ss_size = 0x2000LL;
getcontext(v5);
makecontext(*(ucontext_t **)(mainThread + 16), a1, 1, a2);
mainPrg = (ucontext_t *)malloc(0x3C8uLL);
v7 = mainPrg;
if ( !mainPrg || (v8 = malloc(0x2000uLL), (sizeArr = (__int64)v8) == 0) )
{
LABEL_7:
perror("ERROR:Not enough memory to create main Thread.. exiting..\n");
exit(-1);
}
v7->uc_link = 0LL;
v7->uc_stack.ss_sp = v8;
v7->uc_stack.ss_size = 0x2000LL;
getcontext(v7);
makecontext(mainPrg, MyThreadRun, 1, a2);
++id;
v9 = *(const ucontext_t **)(mainThread + 16);
currThread = mainThread;
return setcontext(v9);
}

original_code:
void MyThreadInit(void(*startTh_funct)(void *), void *args)
{
 id = 0;
 readyQueue.head = readyQueue.tail = ((void *)0);
 mainThread = (Mythread*) malloc(sizeof(Mythread));
 if(mainThread == ((void *)0))
 {
  perror("ERROR:Not enough memory to create main Thread.. exiting..\n");
  exit(-1);
 }
 mainThread -> thread = (ucontext_t*) malloc(sizeof(ucontext_t));
 if(mainThread -> thread == ((void *)0))
 {
  perror("ERROR:Not enough memory to create main Thread.. exiting..\n");
  exit(-1);
 }
 sizeArr = malloc(8<<10);
 if(sizeArr == ((void *)0))
 {
  perror("ERROR:Not enough memory to create main Thread.. exiting..\n");
  exit(-1);
 }
 mainThread -> yieldFlag = 0;
 mainThread -> threadId = id;
 mainThread -> blocked = 0;
 mainThread -> parentBlocked = 0;
 mainThread -> childList = mainThread -> sibling = mainThread -> parentptr = ((void *)0);
 mainThread -> thread -> uc_link = ((void *)0);
 mainThread -> thread -> uc_stack.ss_sp = sizeArr;
 mainThread -> thread -> uc_stack.ss_size = 8<<10;
 getcontext(mainThread -> thread);
 makecontext(mainThread -> thread,(void(*)(void)) startTh_funct,1,args);
 mainPrg = malloc(sizeof(ucontext_t));
 if(mainPrg == ((void *)0))
 {
  perror("ERROR:Not enough memory to create main Thread.. exiting..\n");
  exit(-1);
 }
 sizeArr = malloc(8<<10);
 if(sizeArr == ((void *)0))
 {
  perror("ERROR:Not enough memory to create main Thread.. exiting..\n");
  exit(-1);
 }
 mainPrg -> uc_link = ((void *)0);
 mainPrg -> uc_stack.ss_sp = sizeArr;
 mainPrg -> uc_stack.ss_size = 8<<10;
 getcontext(mainPrg);
 makecontext(mainPrg,(void(*)(void)) MyThreadRun,1,args);
 currThread = mainThread;
 id++;
 setcontext(mainThread->thread);
}

canonical_original_code:
void MyThreadInit(void (*startTh_funct)(void *), void *args)
{
 id = 0;
 readyQueue.head = readyQueue.tail = ((void *)0);
 mainThread = (struct Mythread_ *) malloc(sizeof(Mythread));
 if(mainThread == ((void *)0))
 {
  perror("ERROR:Not enough memory to create main Thread.. exiting..\n");
  exit(-1);
 }
 mainThread -> thread = (struct ucontext_t *) malloc(sizeof(ucontext_t));
 if(mainThread -> thread == ((void *)0))
 {
  perror("ERROR:Not enough memory to create main Thread.. exiting..\n");
  exit(-1);
 }
 sizeArr = malloc(8<<10);
 if(sizeArr == ((void *)0))
 {
  perror("ERROR:Not enough memory to create main Thread.. exiting..\n");
  exit(-1);
 }
 mainThread -> yieldFlag = 0;
 mainThread -> threadId = id;
 mainThread -> blocked = 0;
 mainThread -> parentBlocked = 0;
 mainThread -> childList = mainThread -> sibling = mainThread -> parentptr = ((void *)0);
 mainThread -> thread -> uc_link = ((void *)0);
 mainThread -> thread -> uc_stack.ss_sp = sizeArr;
 mainThread -> thread -> uc_stack.ss_size = 8<<10;
 getcontext(mainThread -> thread);
 makecontext(mainThread -> thread,(void (*)()) startTh_funct,1,args);
 mainPrg = malloc(sizeof(ucontext_t));
 if(mainPrg == ((void *)0))
 {
  perror("ERROR:Not enough memory to create main Thread.. exiting..\n");
  exit(-1);
 }
 sizeArr = malloc(8<<10);
 if(sizeArr == ((void *)0))
 {
  perror("ERROR:Not enough memory to create main Thread.. exiting..\n");
  exit(-1);
 }
 mainPrg -> uc_link = ((void *)0);
 mainPrg -> uc_stack.ss_sp = sizeArr;
 mainPrg -> uc_stack.ss_size = 8<<10;
 getcontext(mainPrg);
 makecontext(mainPrg,(void (*)()) MyThreadRun,1,args);
 currThread = mainThread;
 id++;
 setcontext(mainThread->thread);
}

variable_types:
args
void *

user_defined_types: struct stack_t { void *ss_sp; int ss_flags; unsigned long ss_size; }

user_defined_types: struct __sigset_t { unsigned long __val[-1]; }

user_defined_types: struct _libc_fpxreg { unsigned short significand[4]; unsigned short exponent; unsigned short __glibc_reserved1[3]; }

user_defined_types: struct _libc_xmmreg { unsigned int element[4]; }


decompiled_code:
int __fastcall execute_command(__int64 a1)
{
const char **v2; // r12
const char *v3; // r12
int v4; // eax
char *i; // rsi
char *v6; // rax
char v7[512]; // [rsp+0h] [rbp-418h] BYREF
char dest[536]; // [rsp+200h] [rbp-218h] BYREF

setupRedirect();
if ( (unsigned int)isBuiltinCmd(**(_QWORD **)(a1 + 40)) != -1 )
return execCmd(a1);
v2 = *(const char ***)(a1 + 40);
strcpy(v7, *v2);
v3 = *v2;
if ( !strchr(v3, 47) )
{
strcpy(dest, path);
for ( i = strtok(dest, ":"); i; i = strtok(0LL, ":") )
{
v6 = stpcpy(v7, i);
*(_WORD *)v6 = 47;
strcpy(v6 + 1, **(const char ***)(a1 + 40));
if ( !(unsigned int)iscmd(v7) )
break;
}
strcpy(**(char ***)(a1 + 40), v7);
v3 = **(const char ***)(a1 + 40);
}
v4 = iscmd(v3);
if ( v4 )
return fprintf(stderr, "%s\n", (const char *)*(&invalid_cmd + v4));
pid = fork();
if ( !pid )
{
execv(**(const char ***)(a1 + 40), *(char *const **)(a1 + 40));
exit(0);
}
return waitpid(pid, &status, 0);
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1)
{
const char **v2; // r12
const char *v3; // r12
int v4; // eax
char *i; // rsi
char *v6; // rax
char v7[512]; // [rsp+0h] [rbp-418h] BYREF
char dest[536]; // [rsp+200h] [rbp-218h] BYREF

setupRedirect();
if ( (unsigned int)isBuiltinCmd(**(_QWORD **)(a1 + 40)) != -1 )
return execCmd(a1);
v2 = *(const char ***)(a1 + 40);
strcpy(v7, *v2);
v3 = *v2;
if ( !strchr(v3, 47) )
{
strcpy(dest, path);
for ( i = strtok(dest, ":"); i; i = strtok(0LL, ":") )
{
v6 = stpcpy(v7, i);
*(_WORD *)v6 = 47;
strcpy(v6 + 1, **(const char ***)(a1 + 40));
if ( !(unsigned int)iscmd(v7) )
break;
}
strcpy(**(char ***)(a1 + 40), v7);
v3 = **(const char ***)(a1 + 40);
}
v4 = iscmd(v3);
if ( v4 )
return fprintf(stderr, "%s\n", (const char *)*(&invalid_cmd + v4));
pid = fork();
if ( !pid )
{
execv(**(const char ***)(a1 + 40), *(char *const **)(a1 + 40));
exit(0);
}
return waitpid(pid, &status, 0);
}

original_code:
void execute_command(Cmd c)
{
 setupRedirect(c);
 if(isBuiltinCmd(c->args[0]) != -1)
  execCmd(c);
 else
 {
  char cmd_path[500],path1[500];
  strcpy(cmd_path,c->args[0]);
  if(strstr(c->args[0],"/")==((void *)0))
  {
   char *needle;
   strcpy(path1,path);
   needle = strtok(path1,":");
   while(needle != ((void *)0))
   {
    strcpy(cmd_path,needle);
    strcat(cmd_path,"/");
    strcat(cmd_path,c->args[0]);
    if(!iscmd(cmd_path))
     break;
    needle = strtok(((void *)0),":");
   }
   strcpy(c->args[0],cmd_path);
  }
  int r = iscmd(c->args[0]);
  if(r)
  {
   fprintf(stderr,"%s\n",invalid_cmd[r]);
   return;
  }
  pid = fork();
  if(!pid)
                {
   execv(c->args[0],c->args);
   exit(0);
  }
  else
  {
   if(pid)
    waitpid(pid,&status,0);
  }
 }
}

canonical_original_code:
void execute_command(struct cmd_t *c)
{
 setupRedirect(c);
 if(isBuiltinCmd(c->args[0]) != -1)
  execCmd(c);
 else
 {
  char cmd_path[500]; char path1[500];
  strcpy(cmd_path,c->args[0]);
  if(strstr(c->args[0],"/")==((void *)0))
  {
   char *needle;
   strcpy(path1,path);
   needle = strtok(path1,":");
   while(needle != ((void *)0))
   {
    strcpy(cmd_path,needle);
    strcat(cmd_path,"/");
    strcat(cmd_path,c->args[0]);
    if(!iscmd(cmd_path))
     break;
    needle = strtok(((void *)0),":");
   }
   strcpy(c->args[0],cmd_path);
  }
  int r = iscmd(c->args[0]);
  if(r)
  {
   fprintf(stderr,"%s\n",invalid_cmd[r]);
   return;
  }
  pid = fork();
  if(!pid)
                {
   execv(c->args[0],c->args);
   exit(0);
  }
  else
  {
   if(pid)
    waitpid(pid,&status,0);
  }
 }
}

variable_types: struct cmd_t { enum Token { Terror=0, Tword=1, Tamp=2, Tpipe=3, Tsemi=4, Tin=5, Tout=6, Tapp=7, TpipeErr=8, ToutErr=9, TappErr=10, Tnl=11, Tnil=12, Tend=13 } exec; enum Token { Terror=0, Tword=1, Tamp=2, Tpipe=3, Tsemi=4, Tin=5, Tout=6, Tapp=7, TpipeErr=8, ToutErr=9, TappErr=10, Tnl=11, Tnil=12, Tend=13 } in; char *infile; int nargs; char **args; struct cmd_t *next; } *

variable_types:
needle
char *

variable_types: r int

user_defined_types: struct cmd_t { enum Token exec; enum Token in; char *infile; int nargs; char **args; struct cmd_t *next; }


runShell

decompiled_code:
__int64 __fastcall runShell()
{
__int64 v0; // rbx
__int64 result; // rax
bool v2; // cf
bool v3; // zf
__int64 v4; // rcx
const char *v5; // rdi
int *v6; // rsi
__int64 v7; // r12
__int64 v8; // rdx
__int64 v9; // rax
__int64 v10; // rbx
unsigned int v11; // ebp
unsigned int v12; // eax
unsigned int v13; // eax

while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
printPrompt();
result = parse();
v2 = 0;
v3 = result == 0;
if ( result )
break;
prompt_flag = 0;
}
v4 = 4LL;
v5 = "end";
v6 = **(int ***)(*(_QWORD *)(result + 8) + 40LL);
do
{
if ( !v4 )
break;
v2 = *(_BYTE *)v6 < *v5;
v3 = *(_BYTE *)v6 == *v5;
v6 = (int *)((char *)v6 + 1);
++v5;
--v4;
}
while ( v3 );
if ( (!v2 && !v3) == v2 )
break;
prompt_flag = 0;
pipe_line = result;
do
{
saveStreams(v5, v6);
pid = 0;
exit_flag = 0;
v7 = *(_QWORD *)(pipe_line + 8);
if ( v7 )
{
v8 = 0LL;
while ( 1 )
{
v9 = v7;
do
{
v10 = v9;
v9 = *(_QWORD *)(v9 + 48);
}
while ( v9 != v8 );
if ( v10 == v7 )
{
v11 = 0;
goto LABEL_17;
}
pipe_ref = pipe_ref == 0;
createPipe();
v11 = fork();
if ( v11 )
break;
v12 = fileno(stdout);
setPipeRedirect((unsigned int)pipe_ref, v12);
v8 = v10;
exit_flag = 1;
}
v7 = v10;
v13 = fileno(stdin);
setPipeRedirect((unsigned int)pipe_ref, v13);
}
else
{
v7 = v0;
v11 = 0;
}
LABEL_17:
execute_command(v7);
v6 = &status;
v5 = (const char *)v11;
waitpid(v11, &status, 0);
if ( exit_flag )
exit(0);
if ( pipe_line )
pipe_line = *(_QWORD *)(pipe_line + 16);
v0 = v7;
resetStreams();
}
while ( pipe_line );
}
if ( !rc_processing )
return result;
setupPrompt(v5, v6);
}
}

canonical_decompiled_code:
__int64 __fastcall func1()
{
__int64 v0; // rbx
__int64 result; // rax
bool v2; // cf
bool v3; // zf
__int64 v4; // rcx
const char *v5; // rdi
int *v6; // rsi
__int64 v7; // r12
__int64 v8; // rdx
__int64 v9; // rax
__int64 v10; // rbx
unsigned int v11; // ebp
unsigned int v12; // eax
unsigned int v13; // eax

while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
printPrompt();
result = parse();
v2 = 0;
v3 = result == 0;
if ( result )
break;
prompt_flag = 0;
}
v4 = 4LL;
v5 = "end";
v6 = **(int ***)(*(_QWORD *)(result + 8) + 40LL);
do
{
if ( !v4 )
break;
v2 = *(_BYTE *)v6 < *v5;
v3 = *(_BYTE *)v6 == *v5;
v6 = (int *)((char *)v6 + 1);
++v5;
--v4;
}
while ( v3 );
if ( (!v2 && !v3) == v2 )
break;
prompt_flag = 0;
pipe_line = result;
do
{
saveStreams(v5, v6);
pid = 0;
exit_flag = 0;
v7 = *(_QWORD *)(pipe_line + 8);
if ( v7 )
{
v8 = 0LL;
while ( 1 )
{
v9 = v7;
do
{
v10 = v9;
v9 = *(_QWORD *)(v9 + 48);
}
while ( v9 != v8 );
if ( v10 == v7 )
{
v11 = 0;
goto LABEL_17;
}
pipe_ref = pipe_ref == 0;
createPipe();
v11 = fork();
if ( v11 )
break;
v12 = fileno(stdout);
setPipeRedirect((unsigned int)pipe_ref, v12);
v8 = v10;
exit_flag = 1;
}
v7 = v10;
v13 = fileno(stdin);
setPipeRedirect((unsigned int)pipe_ref, v13);
}
else
{
v7 = v0;
v11 = 0;
}
LABEL_17:
func0(v7);
v6 = &status;
v5 = (const char *)v11;
waitpid(v11, &status, 0);
if ( exit_flag )
exit(0);
if ( pipe_line )
pipe_line = *(_QWORD *)(pipe_line + 16);
v0 = v7;
resetStreams();
}
while ( pipe_line );
}
if ( !rc_processing )
return result;
setupPrompt(v5, v6);
}
}

original_code:
void runShell()
{
 Pipe cmd_line;
 int subshell_pid;
 while(1)
 {
  printPrompt();
  cmd_line = parse();
  if(cmd_line == ((void *)0))
  {
   prompt_flag = 0;
   continue;
  }
  if(strcmp(cmd_line -> head -> args[0],"end")==0)
  {
   if(rc_processing)
   {
    setupPrompt();
    continue;
   }
   break;
  }
  prompt_flag = 0;
  pipe_line = cmd_line;
  while(pipe_line)
  {
   saveStreams();
   subshell_pid = pid = 0;
   exit_flag = 0;
   Cmd c = pipe_line -> head;
   Cmd d = ((void *)0);
   Cmd e;
   while(c!=d)
   {
    e = c;
    while(e->next != d)
     e=e->next;
    d = e;
    if(c != d)
    {
     pipe_ref = !pipe_ref;
     createPipe(pipe_ref);
     subshell_pid = fork();
     if(subshell_pid)
     {
      setPipeRedirect(pipe_ref,fileno(stdin));
      d = c;
      break;
     }
     else
     {
      setPipeRedirect(pipe_ref,fileno(stdout));
      exit_flag = 1;
     }
    }
   }
   execute_command(e);
   waitpid(subshell_pid,&status,0);
   if(exit_flag)
    exit(0);
   if(pipe_line)
    pipe_line = pipe_line -> next;
   resetStreams();
  }
 }
}

canonical_original_code:
void runShell()
{
 struct pipe_t *cmd_line;
 int subshell_pid;
 while(1)
 {
  printPrompt();
  cmd_line = parse();
  if(cmd_line == ((void *)0))
  {
   prompt_flag = 0;
   continue;
  }
  if(strcmp(cmd_line -> head -> args[0],"end")==0)
  {
   if(rc_processing)
   {
    setupPrompt();
    continue;
   }
   break;
  }
  prompt_flag = 0;
  pipe_line = cmd_line;
  while(pipe_line)
  {
   saveStreams();
   subshell_pid = pid = 0;
   exit_flag = 0;
   struct cmd_t *c = pipe_line -> head;
   struct cmd_t *d = ((void *)0);
   struct cmd_t *e;
   while(c!=d)
   {
    e = c;
    while(e->next != d)
     e=e->next;
    d = e;
    if(c != d)
    {
     pipe_ref = !pipe_ref;
     createPipe(pipe_ref);
     subshell_pid = fork();
     if(subshell_pid)
     {
      setPipeRedirect(pipe_ref,fileno(stdin));
      d = c;
      break;
     }
     else
     {
      setPipeRedirect(pipe_ref,fileno(stdout));
      exit_flag = 1;
     }
    }
   }
   execute_command(e);
   waitpid(subshell_pid,&status,0);
   if(exit_flag)
    exit(0);
   if(pipe_line)
    pipe_line = pipe_line -> next;
   resetStreams();
  }
 }
}

variable_types: struct pipe_t { enum Ptype { Pout=0, PoutErr=1 } type; struct cmd_t { enum Token { Terror=0, Tword=1, Tamp=2, Tpipe=3, Tsemi=4, Tin=5, Tout=6, Tapp=7, TpipeErr=8, ToutErr=9, TappErr=10, Tnl=11, Tnil=12, Tend=13 } exec; enum Token { Terror=0, Tword=1, Tamp=2, Tpipe=3, Tsemi=4, Tin=5, Tout=6, Tapp=7, TpipeErr=8, ToutErr=9, TappErr=10, Tnl=11, Tnil=12, Tend=13 } in; char *infile; int nargs; char **args; struct cmd_t *next; } *head; struct pipe_t *next; } *

user_defined_types: struct pipe_t { enum Ptype type; struct cmd_t *head; struct pipe_t *next; }


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
initShell(argc, argv, envp);
pipe_ref = 1;
runShell();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
initShell(argc, argv, envp);
pipe_ref = 1;
func1();
return 0;
}

original_code:
int main()
{
 initShell();
 pipe_ref = 1;
 runShell();
}


prCmd

original_code:
static void prCmd(Cmd c)
{
  int i;
  if ( c ) {
    printf("%s%s ", c->exec == Tamp ? "BG " : "", c->args[0]);
    if ( c->in == Tin )
      printf("<(%s) ", c->infile);
    if ( c->out != Tnil )
      switch ( c->out ) {
      case Tout:
        printf(">(%s) ", c->outfile);
        break;
      case Tapp:
        printf(">>(%s) ", c->outfile);
        break;
      case ToutErr:
        printf(">&(%s) ", c->outfile);
        break;
      case TappErr:
        printf(">>&(%s) ", c->outfile);
        break;
      default:
        fprintf(stderr, "Shouldn't get here\n");
        exit(-1);
      }
    if ( c->nargs > 1 ) {
      printf("[");
      for ( i = 1; c->args[i] != ((void *)0); i++ )
        printf("%d:%s,", i, c->args[i]);
      printf("\b]");
    }
    putchar('\n');
    if ( !strcmp(c->args[0], "end") )
      exit(0);
  }
}

canonical_original_code:
static void prCmd(struct cmd_t *c)
{
  int i;
  if ( c ) {
    printf("%s%s ", c->exec == Tamp ? "BG " : "", c->args[0]);
    if ( c->in == Tin )
      printf("<(%s) ", c->infile);
    if ( c->out != Tnil )
      switch ( c->out ) {
      case Tout:
        printf(">(%s) ", c->outfile);
        break;
      case Tapp:
        printf(">>(%s) ", c->outfile);
        break;
      case ToutErr:
        printf(">&(%s) ", c->outfile);
        break;
      case TappErr:
        printf(">>&(%s) ", c->outfile);
        break;
      default:
        fprintf(stderr, "Shouldn't get here\n");
        exit(-1);
      }
    if ( c->nargs > 1 ) {
      printf("[");
      for ( i = 1; c->args[i] != ((void *)0); i++ )
        printf("%d:%s,", i, c->args[i]);
      printf("\b]");
    }
    putchar('\n');
    if ( !strcmp(c->args[0], "end") )
      exit(0);
  }
}

variable_types: i int

user_defined_types: enum Token { Terror=0, Tword=1, Tamp=2, Tpipe=3, Tsemi=4, Tin=5, Tout=6, Tapp=7, TpipeErr=8, ToutErr=9, TappErr=10, Tnl=11, Tnil=12, Tend=13 }


saveStreams

decompiled_code:
int saveStreams()
{
int v0; // eax
int v1; // eax
int v2; // eax
int result; // eax

v0 = fileno(stdin);
old_stdin = dup(v0);
if ( old_stdin == -1 )
{
fwrite("dup savestream stdin failed\n", 1uLL, 0x1CuLL, stderr);
exit(0);
}
v1 = fileno(stdout);
old_stdout = dup(v1);
if ( old_stdout == -1 )
{
fwrite("dup savestream stdout failed\n", 1uLL, 0x1DuLL, stderr);
exit(0);
}
v2 = fileno(stderr);
result = dup(v2);
old_stderr = result;
if ( result == -1 )
{
fwrite("dup savestream stderr failed\n", 1uLL, 0x1DuLL, stderr);
exit(0);
}
return result;
}

canonical_decompiled_code:
int func1()
{
int v0; // eax
int v1; // eax
int v2; // eax
int result; // eax

v0 = fileno(stdin);
old_stdin = dup(v0);
if ( old_stdin == -1 )
{
fwrite("dup savestream stdin failed\n", 1uLL, 0x1CuLL, stderr);
exit(0);
}
v1 = fileno(stdout);
old_stdout = dup(v1);
if ( old_stdout == -1 )
{
fwrite("dup savestream stdout failed\n", 1uLL, 0x1DuLL, stderr);
exit(0);
}
v2 = fileno(stderr);
result = dup(v2);
old_stderr = result;
if ( result == -1 )
{
fwrite("dup savestream stderr failed\n", 1uLL, 0x1DuLL, stderr);
exit(0);
}
return result;
}

original_code:
void saveStreams()
{
 if((old_stdin = dup(fileno(stdin))) == -1)
 {
  fprintf(stderr,"dup savestream stdin failed\n");
  exit(0);
 }
 if((old_stdout = dup(fileno(stdout))) == -1)
 {
  fprintf(stderr,"dup savestream stdout failed\n");
  exit(0);
 }
 if((old_stderr = dup(fileno(stderr))) == -1)
 {
  fprintf(stderr,"dup savestream stderr failed\n");
  exit(0);
 }
}


resetStreams

decompiled_code:
int resetStreams()
{
int v0; // eax
int v1; // eax
int v2; // eax

v0 = fileno(stdin);
if ( dup2(old_stdin, v0) == -1 )
{
fwrite("dup reset stdin failed\n", 1uLL, 0x17uLL, stderr);
exit(-1);
}
close(old_stdin);
v1 = fileno(stdout);
if ( dup2(old_stdout, v1) == -1 )
{
fwrite("dup reset stdout failed\n", 1uLL, 0x18uLL, stderr);
exit(-1);
}
close(old_stdout);
v2 = fileno(stderr);
if ( dup2(old_stderr, v2) == -1 )
{
fwrite("dup reset stderr failed\n", 1uLL, 0x18uLL, stderr);
exit(-1);
}
return close(old_stderr);
}

canonical_decompiled_code:
int func2()
{
int v0; // eax
int v1; // eax
int v2; // eax

v0 = fileno(stdin);
if ( dup2(old_stdin, v0) == -1 )
{
fwrite("dup reset stdin failed\n", 1uLL, 0x17uLL, stderr);
exit(-1);
}
close(old_stdin);
v1 = fileno(stdout);
if ( dup2(old_stdout, v1) == -1 )
{
fwrite("dup reset stdout failed\n", 1uLL, 0x18uLL, stderr);
exit(-1);
}
close(old_stdout);
v2 = fileno(stderr);
if ( dup2(old_stderr, v2) == -1 )
{
fwrite("dup reset stderr failed\n", 1uLL, 0x18uLL, stderr);
exit(-1);
}
return close(old_stderr);
}

original_code:
void resetStreams()
{
 if(dup2(old_stdin,fileno(stdin)) == -1)
 {
  fprintf(stderr,"dup reset stdin failed\n");
  exit(-1);
 }
 close(old_stdin);
 if(dup2(old_stdout,fileno(stdout)) == -1)
 {
  fprintf(stderr,"dup reset stdout failed\n");
  exit(-1);
 }
 close(old_stdout);
 if(dup2(old_stderr,fileno(stderr)) == -1)
 {
  fprintf(stderr,"dup reset stderr failed\n");
  exit(-1);
 }
 close(old_stderr);
}


printPrompt

decompiled_code:
int printPrompt()
{
int result; // eax

result = prompt_flag;
if ( !prompt_flag )
{
printf("%s", prompt);
result = fflush(stdout);
prompt_flag = 1;
}
return result;
}

canonical_decompiled_code:
int func3()
{
int result; // eax

result = prompt_flag;
if ( !prompt_flag )
{
printf("%s", prompt);
result = fflush(stdout);
prompt_flag = 1;
}
return result;
}

original_code:
void printPrompt()
{
 if(prompt_flag == 0)
 {
  printf("%s",prompt);
  fflush(stdout);
  prompt_flag = 1;
 }
}


decompiled_code:
void handle_sigint()
{
if ( exit_flag )
exit(0);
putchar(10);
prompt_flag = 0;
pipe_line = 0LL;
printPrompt();
}

canonical_decompiled_code:
void func4()
{
if ( exit_flag )
exit(0);
putchar(10);
prompt_flag = 0;
pipe_line = 0LL;
func3();
}

original_code:
void handle_sigint()
{
 if(exit_flag)
  exit(0);
 printf("\n");
 prompt_flag = 0;
 pipe_line = ((void *)0);
 printPrompt();
}


createPipe

decompiled_code:
int __fastcall createPipe(int a1)
{
int result; // eax

result = pipe(&pipefd[2 * a1]);
if ( result == -1 )
{
puts("Pipe creation failed");
exit(-1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func5(int a1)
{
int result; // eax

result = pipe(&pipefd[2 * a1]);
if ( result == -1 )
{
puts("Pipe creation failed");
exit(-1);
}
return result;
}

original_code:
void createPipe(int pipe_ref)
{
 if (pipe(pipefd[pipe_ref]) == -1)
 {
  printf("Pipe creation failed\n");
  exit(-1);
 }
}


setPipeRedirect

decompiled_code:
int __fastcall setPipeRedirect(int a1, int a2)
{
__int64 v2; // rbx
int result; // eax

v2 = 2LL * a1;
close(pipefd[v2 + (a2 == 0)]);
result = dup2(pipefd[a2 + v2], a2);
if ( result == -1 )
{
fwrite("Pipe redirect failed\n", 1uLL, 0x15uLL, stderr);
exit(-1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func6(int a1, int a2)
{
__int64 v2; // rbx
int result; // eax

v2 = 2LL * a1;
close(pipefd[v2 + (a2 == 0)]);
result = dup2(pipefd[a2 + v2], a2);
if ( result == -1 )
{
fwrite("Pipe redirect failed\n", 1uLL, 0x15uLL, stderr);
exit(-1);
}
return result;
}

original_code:
void setPipeRedirect(int pipe_ref,int stream)
{
 close(pipefd[pipe_ref][!stream]);
 if(dup2(pipefd[pipe_ref][stream],stream)==-1)
 {
  fprintf(stderr,"Pipe redirect failed\n");
  exit(-1);
 }
}

canonical_original_code:
void setPipeRedirect(int pipe_ref, int stream)
{
 close(pipefd[pipe_ref][!stream]);
 if(dup2(pipefd[pipe_ref][stream],stream)==-1)
 {
  fprintf(stderr,"Pipe redirect failed\n");
  exit(-1);
 }
}

variable_types: stream int


isBuiltinCmd

decompiled_code:
__int64 __fastcall isBuiltinCmd(char *s1)
{
__int64 v1; // rbx
unsigned int v2; // er12

v1 = 0LL;
while ( 1 )
{
v2 = v1;
if ( !strcmp(s1, (&builtin_cmds)[v1]) )
break;
if ( ++v1 == 8 )
return (unsigned int)-1;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func7(char *s1)
{
__int64 v1; // rbx
unsigned int v2; // er12

v1 = 0LL;
while ( 1 )
{
v2 = v1;
if ( !strcmp(s1, (&builtin_cmds)[v1]) )
break;
if ( ++v1 == 8 )
return (unsigned int)-1;
}
return v2;
}

original_code:
int isBuiltinCmd(char *cmd)
{
 int i;
 for(i=0;i<8;i++)
  if(!strcmp(cmd,builtin_cmds[i]))
   return i;
 return -1;
}

variable_types:
cmd
char *


initShell

decompiled_code:
int initShell()
{
char *v0; // rax
int v1; // eax
int v2; // eax
size_t v3; // rax
int v4; // eax
char v6[512]; // [rsp+0h] [rbp-408h] BYREF
char name[520]; // [rsp+200h] [rbp-208h] BYREF

v0 = getenv("PATH");
prompt_flag = 0;
path = v0;
home = getenv("HOME");
*(_QWORD *)stpcpy(v6, home) = 0x63726873752E2FLL;
prompt[0] = 0;
signal(3, (__sighandler_t)((char *)prCmd + 1));
signal(20, (__sighandler_t)((char *)prCmd + 1));
signal(2, (__sighandler_t)handle_sigint);
rc_file = open(v6, 0);
if ( rc_file == -1 )
{
rc_processing = 0;
v4 = fileno(stdout);
LODWORD(v3) = isatty(v4);
if ( (_DWORD)v3 )
{
gethostname(name, 0LL);
v3 = strlen(prompt);
*(_WORD *)&prompt[v3] = 8229;
prompt[v3 + 2] = 0;
}
}
else
{
v1 = fileno(stdin);
old_rc_stdin = dup(v1);
if ( old_rc_stdin == -1 || (v2 = fileno(stdin), dup2(rc_file, v2) == -1) )
{
fwrite("dup rcfile failed", 1uLL, 0x11uLL, stderr);
exit(-1);
}
LODWORD(v3) = close(rc_file);
rc_processing = 1;
}
return v3;
}

canonical_decompiled_code:
int func8()
{
char *v0; // rax
int v1; // eax
int v2; // eax
size_t v3; // rax
int v4; // eax
char v6[512]; // [rsp+0h] [rbp-408h] BYREF
char name[520]; // [rsp+200h] [rbp-208h] BYREF

v0 = getenv("PATH");
prompt_flag = 0;
path = v0;
home = getenv("HOME");
*(_QWORD *)stpcpy(v6, home) = 0x63726873752E2FLL;
prompt[0] = 0;
signal(3, (__sighandler_t)((char *)prCmd + 1));
signal(20, (__sighandler_t)((char *)prCmd + 1));
signal(2, (__sighandler_t)handle_sigint);
rc_file = open(v6, 0);
if ( rc_file == -1 )
{
rc_processing = 0;
v4 = fileno(stdout);
LODWORD(v3) = isatty(v4);
if ( (_DWORD)v3 )
{
gethostname(name, 0LL);
v3 = strlen(prompt);
*(_WORD *)&prompt[v3] = 8229;
prompt[v3 + 2] = 0;
}
}
else
{
v1 = fileno(stdin);
old_rc_stdin = dup(v1);
if ( old_rc_stdin == -1 || (v2 = fileno(stdin), dup2(rc_file, v2) == -1) )
{
fwrite("dup rcfile failed", 1uLL, 0x11uLL, stderr);
exit(-1);
}
LODWORD(v3) = close(rc_file);
rc_processing = 1;
}
return v3;
}

original_code:
int initShell()
{
 char rcpath[500];
 path = getenv("PATH");
 prompt_flag = 0;
 home = getenv("HOME");
 strcpy(rcpath,home);
 strcat(rcpath,"/.ushrc");
 strcpy(prompt,"");
 signal(3,((__sighandler_t) 1));
 signal(20,((__sighandler_t) 1));
 signal(2,handle_sigint);
 if((rc_file = open(rcpath,00)) != -1)
 {
  old_rc_stdin = dup(fileno(stdin));
  if(old_rc_stdin == -1)
  {
   fprintf(stderr,"dup rcfile failed");
   exit(-1);
  }
  if(dup2(rc_file,fileno(stdin))==-1)
  {
   fprintf(stderr,"dup rcfile failed");
   exit(-1);
  }
  close(rc_file);
  rc_processing = 1;
 }
 else
 {
  char hostname[500];
  int len;
  rc_processing = 0;
  if(isatty(fileno(stdout)))
  {
   gethostname(hostname,len);
   if(len)
    strcpy(prompt,hostname);
   strcat(prompt,"% ");
  }
 }
}

canonical_original_code:
int initShell()
{
 char rcpath[500];
 path = getenv("PATH");
 prompt_flag = 0;
 home = getenv("HOME");
 strcpy(rcpath,home);
 strcat(rcpath,"/.ushrc");
 strcpy(prompt,"");
 signal(3,((void (*)(int)) 1));
 signal(20,((void (*)(int)) 1));
 signal(2,handle_sigint);
 if((rc_file = open(rcpath,00)) != -1)
 {
  old_rc_stdin = dup(fileno(stdin));
  if(old_rc_stdin == -1)
  {
   fprintf(stderr,"dup rcfile failed");
   exit(-1);
  }
  if(dup2(rc_file,fileno(stdin))==-1)
  {
   fprintf(stderr,"dup rcfile failed");
   exit(-1);
  }
  close(rc_file);
  rc_processing = 1;
 }
 else
 {
  char hostname[500];
  int len;
  rc_processing = 0;
  if(isatty(fileno(stdout)))
  {
   gethostname(hostname,len);
   if(len)
    strcpy(prompt,hostname);
   strcat(prompt,"% ");
  }
 }
}

variable_types:
rcpath
char [500]

variable_types:
hostname
char [500]

variable_types: len int


setupPrompt

decompiled_code:
int setupPrompt()
{
int v0; // eax
int v1; // eax
size_t v2; // rax
char v4[520]; // [rsp+0h] [rbp-208h] BYREF

v0 = fileno(stdin);
dup2(old_rc_stdin, v0);
close(old_rc_stdin);
rc_processing = 0;
v1 = fileno(stdout);
LODWORD(v2) = isatty(v1);
if ( (_DWORD)v2 )
{
gethostname(v4, 0LL);
v2 = strlen(prompt);
*(_WORD *)&prompt[v2] = 8229;
prompt[v2 + 2] = 0;
}
prompt_flag = 0;
return v2;
}

canonical_decompiled_code:
int func9()
{
int v0; // eax
int v1; // eax
size_t v2; // rax
char v4[520]; // [rsp+0h] [rbp-208h] BYREF

v0 = fileno(stdin);
dup2(old_rc_stdin, v0);
close(old_rc_stdin);
rc_processing = 0;
v1 = fileno(stdout);
LODWORD(v2) = isatty(v1);
if ( (_DWORD)v2 )
{
gethostname(v4, 0LL);
v2 = strlen(prompt);
*(_WORD *)&prompt[v2] = 8229;
prompt[v2 + 2] = 0;
}
prompt_flag = 0;
return v2;
}

original_code:
int setupPrompt()
{
 char hostname[500];
 int len;
 dup2(old_rc_stdin,fileno(stdin));
 close(old_rc_stdin);
 rc_processing = 0;
 if(isatty(fileno(stdout)))
 {
  gethostname(hostname,len);
  if(len)
   strcpy(prompt,hostname);
  strcat(prompt,"% ");
 }
 prompt_flag = 0;
}


decompiled_code:
int __fastcall exec_echo(__int64 a1)
{
__int64 v1; // rax
const char *v2; // rsi
__int64 v3; // rbx

v1 = *(_QWORD *)(a1 + 40);
v2 = *(const char **)(v1 + 8);
if ( v2 )
{
v3 = 16LL;
do
{
printf("%s ", v2);
v2 = *(const char **)(*(_QWORD *)(a1 + 40) + v3);
v3 += 8LL;
}
while ( v2 );
LODWORD(v1) = putchar(10);
}
return v1;
}

canonical_decompiled_code:
int __fastcall func10(__int64 a1)
{
__int64 v1; // rax
const char *v2; // rsi
__int64 v3; // rbx

v1 = *(_QWORD *)(a1 + 40);
v2 = *(const char **)(v1 + 8);
if ( v2 )
{
v3 = 16LL;
do
{
printf("%s ", v2);
v2 = *(const char **)(*(_QWORD *)(a1 + 40) + v3);
v3 += 8LL;
}
while ( v2 );
LODWORD(v1) = putchar(10);
}
return v1;
}

original_code:
void exec_echo(Cmd c)
{
 int i=1;
 char buf[500];
 while(c->args[i]!=((void *)0))
 {
  printf("%s ",c->args[i]);
  i++;
 }
 if(i!=1)
  printf("\n");
}

canonical_original_code:
void exec_echo(struct cmd_t *c)
{
 int i = 1;
 char buf[500];
 while(c->args[i]!=((void *)0))
 {
  printf("%s ",c->args[i]);
  i++;
 }
 if(i!=1)
  printf("\n");
}

variable_types:
buf
char [500]


isdir

decompiled_code:
__int64 __fastcall isdir(char *file)
{
struct stat *v1; // rbp
__mode_t v2; // er12

v1 = (struct stat *)malloc(0x90uLL);
stat(file, v1);
v2 = v1->st_mode;
free(v1);
return v2 & 0x4000;
}

canonical_decompiled_code:
__int64 __fastcall func11(char *file)
{
struct stat *v1; // rbp
__mode_t v2; // er12

v1 = (struct stat *)malloc(0x90uLL);
stat(file, v1);
v2 = v1->st_mode;
free(v1);
return v2 & 0x4000;
}

original_code:
int isdir(char *cmd_path)
{
 int r;
 struct stat *buf;
 buf = malloc(sizeof(struct stat));
 stat(cmd_path,buf);
 r = buf->st_mode & 0040000;
 free(buf);
 return r;
}

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec { long tv_sec; long tv_nsec; } st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; } *

user_defined_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }


decompiled_code:
char *__fastcall exec_cd(__int64 a1)
{
__int64 v1; // rbp
char *v3; // rdi
char *v4; // rdi
char *result; // rax
const char *i; // rdi
char *v7; // rax
const char *v8; // r12
int v9; // eax
const char *v10; // r14
__int64 v11; // rbp
size_t v12; // rax
__int64 v13; // r13
char *v14; // rax
const char *v15; // r12
size_t v16; // rax
__int64 v17; // rbp
char *v18; // rax
const char *v19; // r12
size_t v20; // rax
char *v21; // rax

v1 = *(_QWORD *)(a1 + 40);
v3 = *(char **)(v1 + 8);
if ( !v3 )
{
v19 = home;
v20 = strlen(home);
v21 = (char *)malloc(v20);
*(_QWORD *)(v1 + 8) = v21;
strcpy(v21, v19);
v3 = *(char **)(v1 + 8);
}
if ( *v3 == 126 )
{
v7 = strtok(v3, "~");
v8 = v7;
if ( v7 )
{
v9 = strlen(v7);
v10 = home;
v11 = v9;
v12 = strlen(home);
v13 = *(_QWORD *)(a1 + 40);
v14 = (char *)malloc(v11 + v12);
*(_QWORD *)(v13 + 8) = v14;
strcpy(v14, v10);
strcat(*(char **)(v13 + 8), v8);
v3 = *(char **)(*(_QWORD *)(a1 + 40) + 8LL);
}
else
{
v15 = home;
v16 = strlen(home);
v17 = *(_QWORD *)(a1 + 40);
v18 = (char *)malloc(v16);
*(_QWORD *)(v17 + 8) = v18;
strcpy(v18, v15);
v3 = *(char **)(v17 + 8);
}
}
if ( !(unsigned int)isdir(v3) )
return (char *)fwrite("Not a directory\n", 1uLL, 0x10uLL, stderr);
v4 = *(char **)(*(_QWORD *)(a1 + 40) + 8LL);
if ( *v4 == 47 )
{
chdir("/");
v4 = *(char **)(*(_QWORD *)(a1 + 40) + 8LL);
}
result = strtok(v4, "/");
for ( i = result; result; i = result )
{
if ( *i != 126 )
chdir(i);
result = strtok(0LL, "/");
}
return result;
}

canonical_decompiled_code:
char *__fastcall func12(__int64 a1)
{
__int64 v1; // rbp
char *v3; // rdi
char *v4; // rdi
char *result; // rax
const char *i; // rdi
char *v7; // rax
const char *v8; // r12
int v9; // eax
const char *v10; // r14
__int64 v11; // rbp
size_t v12; // rax
__int64 v13; // r13
char *v14; // rax
const char *v15; // r12
size_t v16; // rax
__int64 v17; // rbp
char *v18; // rax
const char *v19; // r12
size_t v20; // rax
char *v21; // rax

v1 = *(_QWORD *)(a1 + 40);
v3 = *(char **)(v1 + 8);
if ( !v3 )
{
v19 = home;
v20 = strlen(home);
v21 = (char *)malloc(v20);
*(_QWORD *)(v1 + 8) = v21;
strcpy(v21, v19);
v3 = *(char **)(v1 + 8);
}
if ( *v3 == 126 )
{
v7 = strtok(v3, "~");
v8 = v7;
if ( v7 )
{
v9 = strlen(v7);
v10 = home;
v11 = v9;
v12 = strlen(home);
v13 = *(_QWORD *)(a1 + 40);
v14 = (char *)malloc(v11 + v12);
*(_QWORD *)(v13 + 8) = v14;
strcpy(v14, v10);
strcat(*(char **)(v13 + 8), v8);
v3 = *(char **)(*(_QWORD *)(a1 + 40) + 8LL);
}
else
{
v15 = home;
v16 = strlen(home);
v17 = *(_QWORD *)(a1 + 40);
v18 = (char *)malloc(v16);
*(_QWORD *)(v17 + 8) = v18;
strcpy(v18, v15);
v3 = *(char **)(v17 + 8);
}
}
if ( !(unsigned int)func11(v3) )
return (char *)fwrite("Not a directory\n", 1uLL, 0x10uLL, stderr);
v4 = *(char **)(*(_QWORD *)(a1 + 40) + 8LL);
if ( *v4 == 47 )
{
chdir("/");
v4 = *(char **)(*(_QWORD *)(a1 + 40) + 8LL);
}
result = strtok(v4, "/");
for ( i = result; result; i = result )
{
if ( *i != 126 )
chdir(i);
result = strtok(0LL, "/");
}
return result;
}

original_code:
void exec_cd(Cmd c)
{
 char *cmd_path,*path1;
 if(c->args[1] == ((void *)0))
 {
  c->args[1] = malloc(strlen(home));
  strcpy(c->args[1],home);
 }
 if(c->args[1][0]=='~')
 {
  char *temp,temp1[500];
  int tlen = 0;
  temp = strtok(c->args[1],"~");
  if(temp)
   tlen = strlen(temp);
  c->args[1] = malloc(strlen(home)+tlen);
  strcpy(c->args[1],home);
  if(temp)
   strcat(c->args[1],temp);
 }
        if(isdir(c->args[1]))
 {
  if(c->args[1][0] == '/')
  {
   chdir("/");
  }
  cmd_path=strtok(c->args[1],"/");
  while(cmd_path != ((void *)0))
  {
   if(cmd_path[0]!='~')
   {
    chdir(cmd_path);
   }
   cmd_path = strtok(((void *)0),"/");
  }
 }
 else
  fprintf(stderr,"Not a directory\n");
}

canonical_original_code:
void exec_cd(struct cmd_t *c)
{
 char *cmd_path; char *path1;
 if(c->args[1] == ((void *)0))
 {
  c->args[1] = malloc(strlen(home));
  strcpy(c->args[1],home);
 }
 if(c->args[1][0]=='~')
 {
  char *temp; char temp1[500];
  int tlen = 0;
  temp = strtok(c->args[1],"~");
  if(temp)
   tlen = strlen(temp);
  c->args[1] = malloc(strlen(home)+tlen);
  strcpy(c->args[1],home);
  if(temp)
   strcat(c->args[1],temp);
 }
        if(isdir(c->args[1]))
 {
  if(c->args[1][0] == '/')
  {
   chdir("/");
  }
  cmd_path=strtok(c->args[1],"/");
  while(cmd_path != ((void *)0))
  {
   if(cmd_path[0]!='~')
   {
    chdir(cmd_path);
   }
   cmd_path = strtok(((void *)0),"/");
  }
 }
 else
  fprintf(stderr,"Not a directory\n");
}

variable_types:
temp
char *

variable_types: tlen int


iscmd

decompiled_code:
__int64 __fastcall iscmd(char *file)
{
int v1; // er8
__int64 result; // rax

v1 = access(file, 0);
result = 1LL;
if ( !v1 )
{
if ( access(file, 5) || (result = isdir(file), (_DWORD)result) )
result = 2LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func13(char *file)
{
int v1; // er8
__int64 result; // rax

v1 = access(file, 0);
result = 1LL;
if ( !v1 )
{
if ( access(file, 5) || (result = func11(file), (_DWORD)result) )
result = 2LL;
}
return result;
}

original_code:
int iscmd(char *cmd_path)
{
 if(access(cmd_path,0) !=0) return 1;
 if(access(cmd_path,4|1) !=0 || isdir(cmd_path)) return 2;
 return 0;
}


decompiled_code:
void __fastcall exec_nice(__int64 a1)
{
int v1; // er12
const char *v3; // rdi
int v4; // eax
__int64 v5; // rbx
const char *v6; // r13
char *v7; // r12
char **v8; // rax
char **v9; // rbp
size_t v10; // rax
char *v11; // rax

v1 = 4;
v3 = *(const char **)(*(_QWORD *)(a1 + 40) + 8LL);
if ( v3 )
{
v4 = atoi(v3);
v1 = 20;
if ( v4 <= 20 )
v1 = v4;
if ( v1 < -19 )
v1 = -19;
}
getpriority(PRIO_PROCESS, 0);
setpriority(PRIO_PROCESS, 0, v1);
v5 = *(_QWORD *)(a1 + 40);
if ( *(_QWORD *)(v5 + 8) )
{
v6 = *(const char **)(v5 + 16);
if ( v6 )
{
v7 = (char *)malloc(0x38uLL);
v8 = (char **)malloc(8uLL);
*((_QWORD *)v7 + 5) = v8;
v9 = v8;
*(_QWORD *)(v7 + 4) = 0xC0000000CLL;
v10 = strlen(v6);
v11 = (char *)malloc(v10);
*v9 = v11;
strcpy(v11, *(const char **)(v5 + 16));
execute_command(v7);
free(**((void ***)v7 + 5));
free(*((void **)v7 + 5));
free(v7);
}
}
}

canonical_decompiled_code:
void __fastcall func14(__int64 a1)
{
int v1; // er12
const char *v3; // rdi
int v4; // eax
__int64 v5; // rbx
const char *v6; // r13
char *v7; // r12
char **v8; // rax
char **v9; // rbp
size_t v10; // rax
char *v11; // rax

v1 = 4;
v3 = *(const char **)(*(_QWORD *)(a1 + 40) + 8LL);
if ( v3 )
{
v4 = atoi(v3);
v1 = 20;
if ( v4 <= 20 )
v1 = v4;
if ( v1 < -19 )
v1 = -19;
}
getpriority(PRIO_PROCESS, 0);
setpriority(PRIO_PROCESS, 0, v1);
v5 = *(_QWORD *)(a1 + 40);
if ( *(_QWORD *)(v5 + 8) )
{
v6 = *(const char **)(v5 + 16);
if ( v6 )
{
v7 = (char *)malloc(0x38uLL);
v8 = (char **)malloc(8uLL);
*((_QWORD *)v7 + 5) = v8;
v9 = v8;
*(_QWORD *)(v7 + 4) = 0xC0000000CLL;
v10 = strlen(v6);
v11 = (char *)malloc(v10);
*v9 = v11;
strcpy(v11, *(const char **)(v5 + 16));
execute_command(v7);
free(**((void ***)v7 + 5));
free(*((void **)v7 + 5));
free(v7);
}
}
}

original_code:
void exec_nice(Cmd c)
{
 int which, who, prio;
 which = PRIO_PROCESS;
 who = 0;
 if(c->args[1]!=((void *)0))
 {
  prio = atoi(c->args[1]);
  if(prio<-19)
   prio = -19;
  else if(prio>20)
   prio = 20;
 }
 else prio = 4;
 getpriority(which, who);
 setpriority(which, who, prio);
 if(c->args[1]!=((void *)0))
  if(c->args[2]!=((void *)0))
  {
   Cmd temp;
   temp = malloc(sizeof(struct cmd_t));
    temp->args = malloc(sizeof(char*));
   temp->in = Tnil;
   temp->out = Tnil;
   temp->args[0] = malloc(strlen(c->args[2]));
   strcpy(temp->args[0],c->args[2]);
   execute_command(temp);
   free(temp->args[0]);
   free(temp->args);
   free(temp);
  }
}

canonical_original_code:
void exec_nice(struct cmd_t *c)
{
 int which; int who; int prio;
 which = PRIO_PROCESS;
 who = 0;
 if(c->args[1]!=((void *)0))
 {
  prio = atoi(c->args[1]);
  if(prio<-19)
   prio = -19;
  else if(prio>20)
   prio = 20;
 }
 else prio = 4;
 getpriority(which, who);
 setpriority(which, who, prio);
 if(c->args[1]!=((void *)0))
  if(c->args[2]!=((void *)0))
  {
   struct cmd_t *temp;
   temp = malloc(sizeof(struct cmd_t));
    temp->args = malloc(sizeof(char *));
   temp->in = Tnil;
   temp->out = Tnil;
   temp->args[0] = malloc(strlen(c->args[2]));
   strcpy(temp->args[0],c->args[2]);
   execute_command(temp);
   free(temp->args[0]);
   free(temp->args);
   free(temp);
  }
}

variable_types: which int

variable_types: who int

variable_types: prio int


decompiled_code:
int exec_pwd()
{
signed int v0; // eax

v0 = (unsigned int)get_current_dir_name();
return puts((const char *)v0);
}

canonical_decompiled_code:
int func15()
{
signed int v0; // eax

v0 = (unsigned int)get_current_dir_name();
return puts((const char *)v0);
}

original_code:
void exec_pwd(Cmd c)
{
 char *dir;
 dir = (char*)get_current_dir_name();
 printf("%s\n",dir);
}

canonical_original_code:
void exec_pwd(struct cmd_t *c)
{
 char *dir;
 dir = (char *)get_current_dir_name();
 printf("%s\n",dir);
}

variable_types: struct cmd_t { enum Token { Terror=0, Tword=1, Tamp=2, Tpipe=3, Tsemi=4, Tin=5, Tout=6, Tapp=7, TpipeErr=8, ToutErr=9, TappErr=10, Tnl=11, Tnil=12, Tend=13 } exec; enum Token { Terror=0, Tword=1, Tamp=2, Tpipe=3, Tsemi=4, Tin=5, Tout=6, Tapp=7, TpipeErr=8, ToutErr=9, TappErr=10, Tnl=11, Tnil=12, Tend=13 } in; char *infile; int nargs; char **args; struct cmd_t *next; } *

variable_types:
dir
char *


decompiled_code:
int __fastcall exec_setenv(__int64 a1)
{
__int64 v1; // rax
const char *v2; // rdi
const char *v3; // rsi
char **v4; // rbx
char *i; // rdi

v1 = *(_QWORD *)(a1 + 40);
v2 = *(const char **)(v1 + 8);
if ( v2 )
{
v3 = *(const char **)(v1 + 16);
if ( !v3 )
v3 = &value;
LODWORD(v1) = setenv(v2, v3, 1);
}
else
{
v4 = environ;
for ( i = *environ; i; ++v4 )
{
LODWORD(v1) = puts(i);
i = v4[1];
}
}
return v1;
}

canonical_decompiled_code:
int __fastcall func16(__int64 a1)
{
__int64 v1; // rax
const char *v2; // rdi
const char *v3; // rsi
char **v4; // rbx
char *i; // rdi

v1 = *(_QWORD *)(a1 + 40);
v2 = *(const char **)(v1 + 8);
if ( v2 )
{
v3 = *(const char **)(v1 + 16);
if ( !v3 )
v3 = &value;
LODWORD(v1) = setenv(v2, v3, 1);
}
else
{
v4 = environ;
for ( i = *environ; i; ++v4 )
{
LODWORD(v1) = puts(i);
i = v4[1];
}
}
return v1;
}

original_code:
void exec_setenv(Cmd c)
{
 int i;
 if(c->args[1] == ((void *)0))
 {
  char **e = environ;
  while(*e)
  {
   printf("%s\n",*e);
   *e++;
  }
  return;
 }
 else if(c->args[2] == ((void *)0))
  setenv(c->args[1],"",1);
 else
  setenv(c->args[1],c->args[2],1);
}

canonical_original_code:
void exec_setenv(struct cmd_t *c)
{
 int i;
 if(c->args[1] == ((void *)0))
 {
  char **e = environ;
  while(*e)
  {
   printf("%s\n",*e);
   *e++;
  }
  return;
 }
 else if(c->args[2] == ((void *)0))
  setenv(c->args[1],"",1);
 else
  setenv(c->args[1],c->args[2],1);
}

variable_types:
e
char **


decompiled_code:
int __fastcall exec_unsetenv(__int64 a1)
{
const char *v1; // rdi
int result; // eax

v1 = *(const char **)(*(_QWORD *)(a1 + 40) + 8LL);
if ( v1 )
result = unsetenv(v1);
else
result = fwrite("unsetenv: too few arguments\n", 1uLL, 0x1CuLL, stderr);
return result;
}

canonical_decompiled_code:
int __fastcall func17(__int64 a1)
{
const char *v1; // rdi
int result; // eax

v1 = *(const char **)(*(_QWORD *)(a1 + 40) + 8LL);
if ( v1 )
result = unsetenv(v1);
else
result = fwrite("unsetenv: too few arguments\n", 1uLL, 0x1CuLL, stderr);
return result;
}

original_code:
void exec_unsetenv(Cmd c)
{
 if(c->args[1] == ((void *)0))
 {
  fprintf(stderr,"unsetenv: too few arguments\n");
  return;
 }
 else
 {
  unsetenv(c->args[1]);
 }
}

canonical_original_code:
void exec_unsetenv(struct cmd_t *c)
{
 if(c->args[1] == ((void *)0))
 {
  fprintf(stderr,"unsetenv: too few arguments\n");
  return;
 }
 else
 {
  unsetenv(c->args[1]);
 }
}


decompiled_code:
char *__fastcall exec_where(__int64 a1)
{
__int64 v1; // rax
int v2; // eax
char *result; // rax
const char *i; // rsi
char *v5; // rax
char v6[512]; // [rsp-200h] [rbp-408h] BYREF
char dest[16]; // [rsp+0h] [rbp-208h] BYREF
char v8[480]; // [rsp+10h] [rbp-1F8h] BYREF
int v9; // [rsp+1F0h] [rbp-18h]

*(_OWORD *)dest = 0LL;
memset(v8, 0, sizeof(v8));
v1 = *(_QWORD *)(a1 + 40);
v9 = 0;
v2 = isBuiltinCmd(*(char **)(v1 + 8));
if ( v2 != -1 )
puts((&builtin_cmds)[v2]);
strcpy(v6, path);
result = strtok(v6, ":");
for ( i = result; result; i = result )
{
v5 = stpcpy(dest, i);
*(_WORD *)v5 = 47;
strcpy(v5 + 1, *(const char **)(*(_QWORD *)(a1 + 40) + 8LL));
if ( !(unsigned int)iscmd(dest) )
puts(dest);
result = strtok(0LL, ":");
}
return result;
}

canonical_decompiled_code:
char *__fastcall func18(__int64 a1)
{
__int64 v1; // rax
int v2; // eax
char *result; // rax
const char *i; // rsi
char *v5; // rax
char v6[512]; // [rsp-200h] [rbp-408h] BYREF
char dest[16]; // [rsp+0h] [rbp-208h] BYREF
char v8[480]; // [rsp+10h] [rbp-1F8h] BYREF
int v9; // [rsp+1F0h] [rbp-18h]

*(_OWORD *)dest = 0LL;
memset(v8, 0, sizeof(v8));
v1 = *(_QWORD *)(a1 + 40);
v9 = 0;
v2 = func7(*(char **)(v1 + 8));
if ( v2 != -1 )
puts((&builtin_cmds)[v2]);
strcpy(v6, path);
result = strtok(v6, ":");
for ( i = result; result; i = result )
{
v5 = stpcpy(dest, i);
*(_WORD *)v5 = 47;
strcpy(v5 + 1, *(const char **)(*(_QWORD *)(a1 + 40) + 8LL));
if ( !(unsigned int)func13(dest) )
puts(dest);
result = strtok(0LL, ":");
}
return result;
}

original_code:
void exec_where(Cmd c)
{
 char *needle;
 char path1[500],cmd_path[500]="";
 int i=isBuiltinCmd(c->args[1]);
 if(i!=-1)
  printf("%s\n",builtin_cmds[i]);
 strcpy(path1,path);
 needle = strtok(path1,":");
 while(needle != ((void *)0))
 {
  strcpy(cmd_path,needle);
  strcat(cmd_path,"/");
  strcat(cmd_path,c->args[1]);
  if(!iscmd(cmd_path))
   printf("%s\n",cmd_path);
  needle = strtok(((void *)0),":");
 }
}

canonical_original_code:
void exec_where(struct cmd_t *c)
{
 char *needle;
 char path1[500]; char cmd_path[500] = "";
 int i = isBuiltinCmd(c->args[1]);
 if(i!=-1)
  printf("%s\n",builtin_cmds[i]);
 strcpy(path1,path);
 needle = strtok(path1,":");
 while(needle != ((void *)0))
 {
  strcpy(cmd_path,needle);
  strcat(cmd_path,"/");
  strcat(cmd_path,c->args[1]);
  if(!iscmd(cmd_path))
   printf("%s\n",cmd_path);
  needle = strtok(((void *)0),":");
 }
}

variable_types:
needle
char *


execCmd

decompiled_code:
void __fastcall execCmd(__int64 a1)
{
__int64 v1; // rax

v1 = **(_QWORD **)(a1 + 40);
if ( !strcmp((const char *)v1, "unsetenv") )
{
exec_unsetenv(a1);
}
else if ( *(_BYTE *)v1 == 99 && *(_BYTE *)(v1 + 1) == 100 && !*(_BYTE *)(v1 + 2) )
{
exec_cd(a1);
}
else if ( !strcmp((const char *)v1, "echo") )
{
exec_echo(a1);
}
else if ( !strcmp((const char *)v1, "nice") )
{
exec_nice(a1);
}
else if ( !strcmp((const char *)v1, "pwd") )
{
exec_pwd();
}
else if ( !strcmp((const char *)v1, "setenv") )
{
exec_setenv(a1);
}
else
{
if ( !strcmp((const char *)v1, "logout") )
exit(0);
if ( !strcmp((const char *)v1, "where") )
exec_where(a1);
}
}

canonical_decompiled_code:
void __fastcall func19(__int64 a1)
{
__int64 v1; // rax

v1 = **(_QWORD **)(a1 + 40);
if ( !strcmp((const char *)v1, "unsetenv") )
{
func17(a1);
}
else if ( *(_BYTE *)v1 == 99 && *(_BYTE *)(v1 + 1) == 100 && !*(_BYTE *)(v1 + 2) )
{
func12(a1);
}
else if ( !strcmp((const char *)v1, "echo") )
{
func10(a1);
}
else if ( !strcmp((const char *)v1, "nice") )
{
func14(a1);
}
else if ( !strcmp((const char *)v1, "pwd") )
{
func15();
}
else if ( !strcmp((const char *)v1, "setenv") )
{
func16(a1);
}
else
{
if ( !strcmp((const char *)v1, "logout") )
exit(0);
if ( !strcmp((const char *)v1, "where") )
func18(a1);
}
}

original_code:
void execCmd(Cmd c)
{
 if(!strcmp(c->args[0],"unsetenv")) exec_unsetenv(c);
 else if(!strcmp(c->args[0],"cd")) exec_cd(c);
 else if(!strcmp(c->args[0],"echo")) exec_echo(c);
 else if(!strcmp(c->args[0],"nice")) exec_nice(c);
 else if(!strcmp(c->args[0],"pwd")) exec_pwd(c);
 else if(!strcmp(c->args[0],"setenv")) exec_setenv(c);
 else if(!strcmp(c->args[0],"logout")) exit(0);
 else if(!strcmp(c->args[0],"where")) exec_where(c);
}

canonical_original_code:
void execCmd(struct cmd_t *c)
{
 if(!strcmp(c->args[0],"unsetenv")) exec_unsetenv(c);
 else if(!strcmp(c->args[0],"cd")) exec_cd(c);
 else if(!strcmp(c->args[0],"echo")) exec_echo(c);
 else if(!strcmp(c->args[0],"nice")) exec_nice(c);
 else if(!strcmp(c->args[0],"pwd")) exec_pwd(c);
 else if(!strcmp(c->args[0],"setenv")) exec_setenv(c);
 else if(!strcmp(c->args[0],"logout")) exit(0);
 else if(!strcmp(c->args[0],"where")) exec_where(c);
}


prPipe

decompiled_code:
int __fastcall prPipe(__int64 a1)
{
__int64 v1; // r12
const char *v2; // rsi
__int64 v3; // rbx
int v4; // ebp
int result; // eax

if ( a1 )
{
v1 = a1;
do
{
v2 = " Error";
if ( !*(_DWORD *)v1 )
v2 = &value;
printf("Begin pipe%s\n", v2);
v3 = *(_QWORD *)(v1 + 8);
if ( v3 )
{
v4 = 0;
do
{
printf(" Cmd #%d: ", (unsigned int)++v4);
prCmd(v3);
v3 = *(_QWORD *)(v3 + 48);
}
while ( v3 );
}
result = puts("End pipe");
v1 = *(_QWORD *)(v1 + 16);
}
while ( v1 );
}
return result;
}

canonical_decompiled_code:
int __fastcall func21(__int64 a1)
{
__int64 v1; // r12
const char *v2; // rsi
__int64 v3; // rbx
int v4; // ebp
int result; // eax

if ( a1 )
{
v1 = a1;
do
{
v2 = " Error";
if ( !*(_DWORD *)v1 )
v2 = &value;
printf("Begin pipe%s\n", v2);
v3 = *(_QWORD *)(v1 + 8);
if ( v3 )
{
v4 = 0;
do
{
printf(" Cmd #%d: ", (unsigned int)++v4);
func0(v3);
v3 = *(_QWORD *)(v3 + 48);
}
while ( v3 );
}
result = puts("End pipe");
v1 = *(_QWORD *)(v1 + 16);
}
while ( v1 );
}
return result;
}

original_code:
void prPipe(Pipe p)
{
  int i = 0;
  Cmd c;
  if ( p == ((void *)0) )
    return;
  printf("Begin pipe%s\n", p->type == Pout ? "" : " Error");
  for ( c = p->head; c != ((void *)0); c = c->next ) {
    printf("  Cmd #%d: ", ++i);
    prCmd(c);
  }
  printf("End pipe\n");
  prPipe(p->next);
}

canonical_original_code:
void prPipe(struct pipe_t *p)
{
  int i = 0;
  struct cmd_t *c;
  if ( p == ((void *)0) )
    return;
  printf("Begin pipe%s\n", p->type == Pout ? "" : " Error");
  for ( c = p->head; c != ((void *)0); c = c->next ) {
    printf("  Cmd #%d: ", ++i);
    prCmd(c);
  }
  printf("End pipe\n");
  prPipe(p->next);
}

variable_types: struct pipe_t { enum Ptype { Pout=0, PoutErr=1 } type; struct cmd_t { enum Token { Terror=0, Tword=1, Tamp=2, Tpipe=3, Tsemi=4, Tin=5, Tout=6, Tapp=7, TpipeErr=8, ToutErr=9, TappErr=10, Tnl=11, Tnil=12, Tend=13 } exec; enum Token { Terror=0, Tword=1, Tamp=2, Tpipe=3, Tsemi=4, Tin=5, Tout=6, Tapp=7, TpipeErr=8, ToutErr=9, TappErr=10, Tnl=11, Tnil=12, Tend=13 } in; char *infile; int nargs; char **args; struct cmd_t *next; } *head; struct pipe_t *next; } *

variable_types: i int


nextToken

decompiled_code:
__int64 nextToken()
{
char v0; // al
char v1; // bp
char *i; // rbx
char *v3; // r12
char v4; // al
char v5; // di
__int64 result; // rax
char v7; // al
int *v8; // rbx
char v9; // al
char v10; // al
char v11; // di
char v12; // al
FILE *v13; // rsi
char v14; // dl
char v15; // al

while ( 2 )
{
Word = 0;
v0 = getchar();
if ( v0 < 0 )
return 13LL;
v1 = v0;
if ( v0 > 62 )
{
if ( v0 == 124 )
{
v5 = getchar();
if ( v5 >= 0 )
{
result = 8LL;
if ( v5 != 38 )
{
ungetc(v5, stdin);
result = 3LL;
}
return result;
}
return 0LL;
}
}
else if ( v0 > 8 )
{
switch ( v0 )
{
case 9:
case 32:
continue;
case 10:
return 11LL;
case 34:
case 39:
v3 = &Word;
LABEL_20:
v7 = getchar();
if ( v1 == v7 )
{
v8 = (int *)v3;
goto LABEL_29;
}
if ( v7 != 10 && v7 >= 0 )
{
v8 = (int *)(v3 + 1);
*v3 = v7;
if ( v3 + 1 > (char *)&unk_A9F )
{
LABEL_38:
printf("String too long (> %d bytes)\n", 63LL);
while ( 1 )
{
v12 = getchar();
if ( v12 == 10 )
break;
if ( v12 <= 0 )
return 0LL;
}
return 0LL;
}
while ( 1 )
{
v9 = getchar();
if ( v1 == v9 )
break;
if ( v9 < 0 || v9 == 10 )
goto LABEL_30;
v8 = (int *)((char *)v8 + 1);
*((_BYTE *)v8 - 1) = v9;
if ( v8 == &LookAhead )
goto LABEL_38;
}
LABEL_29:
*(_BYTE *)v8 = 0;
return 1LL;
}
LABEL_30:
printf("Unmatched %c\n", (unsigned int)v1);
break;
case 38:
return 2LL;
case 59:
return 4LL;
case 60:
return 5LL;
case 62:
v10 = getchar();
v11 = v10;
if ( v10 < 0 )
return 0LL;
if ( v10 == 62 )
{
v14 = getchar();
if ( v14 < 0 )
return 0LL;
result = 10LL;
if ( v14 != 38 )
{
ungetc(v14, stdin);
result = 7LL;
}
}
else
{
result = 9LL;
if ( v11 != 38 )
{
ungetc(v11, stdin);
result = 6LL;
}
}
return result;
default:
goto LABEL_5;
}
return 0LL;
}
break;
}
LABEL_5:
for ( i = &Word; ; ++i )
{
if ( v1 == 92 )
{
v1 = getchar();
if ( v1 < 0 )
return 0LL;
}
v3 = i + 1;
*i = v1;
if ( i + 1 == (char *)&LookAhead )
break;
v4 = getchar();
v1 = v4;
if ( v4 < 0 )
return 0LL;
if ( v4 > 62 )
{
if ( v4 == 124 )
{
LABEL_42:
v13 = stdin;
i[1] = 0;
ungetc(v4, v13);
return 1LL;
}
}
else if ( v4 > 8 )
{
switch ( v4 )
{
case 9:
case 32:
i[1] = 0;
result = 1LL;
break;
case 10:
case 38:
case 59:
case 60:
case 62:
goto LABEL_42;
case 34:
case 39:
goto LABEL_20;
default:
continue;
}
return result;
}
}
printf("Word too long (> %d bytes)\n", 63LL);
while ( 1 )
{
v15 = getchar();
if ( v15 <= 0 )
break;
if ( v15 == 10 )
return 0LL;
}
return 0LL;
}

canonical_decompiled_code:
__int64 func0()
{
char v0; // al
char v1; // bp
char *i; // rbx
char *v3; // r12
char v4; // al
char v5; // di
__int64 result; // rax
char v7; // al
int *v8; // rbx
char v9; // al
char v10; // al
char v11; // di
char v12; // al
FILE *v13; // rsi
char v14; // dl
char v15; // al

while ( 2 )
{
Word = 0;
v0 = getchar();
if ( v0 < 0 )
return 13LL;
v1 = v0;
if ( v0 > 62 )
{
if ( v0 == 124 )
{
v5 = getchar();
if ( v5 >= 0 )
{
result = 8LL;
if ( v5 != 38 )
{
ungetc(v5, stdin);
result = 3LL;
}
return result;
}
return 0LL;
}
}
else if ( v0 > 8 )
{
switch ( v0 )
{
case 9:
case 32:
continue;
case 10:
return 11LL;
case 34:
case 39:
v3 = &Word;
LABEL_20:
v7 = getchar();
if ( v1 == v7 )
{
v8 = (int *)v3;
goto LABEL_29;
}
if ( v7 != 10 && v7 >= 0 )
{
v8 = (int *)(v3 + 1);
*v3 = v7;
if ( v3 + 1 > (char *)&unk_A9F )
{
LABEL_38:
printf("String too long (> %d bytes)\n", 63LL);
while ( 1 )
{
v12 = getchar();
if ( v12 == 10 )
break;
if ( v12 <= 0 )
return 0LL;
}
return 0LL;
}
while ( 1 )
{
v9 = getchar();
if ( v1 == v9 )
break;
if ( v9 < 0 || v9 == 10 )
goto LABEL_30;
v8 = (int *)((char *)v8 + 1);
*((_BYTE *)v8 - 1) = v9;
if ( v8 == &LookAhead )
goto LABEL_38;
}
LABEL_29:
*(_BYTE *)v8 = 0;
return 1LL;
}
LABEL_30:
printf("Unmatched %c\n", (unsigned int)v1);
break;
case 38:
return 2LL;
case 59:
return 4LL;
case 60:
return 5LL;
case 62:
v10 = getchar();
v11 = v10;
if ( v10 < 0 )
return 0LL;
if ( v10 == 62 )
{
v14 = getchar();
if ( v14 < 0 )
return 0LL;
result = 10LL;
if ( v14 != 38 )
{
ungetc(v14, stdin);
result = 7LL;
}
}
else
{
result = 9LL;
if ( v11 != 38 )
{
ungetc(v11, stdin);
result = 6LL;
}
}
return result;
default:
goto LABEL_5;
}
return 0LL;
}
break;
}
LABEL_5:
for ( i = &Word; ; ++i )
{
if ( v1 == 92 )
{
v1 = getchar();
if ( v1 < 0 )
return 0LL;
}
v3 = i + 1;
*i = v1;
if ( i + 1 == (char *)&LookAhead )
break;
v4 = getchar();
v1 = v4;
if ( v4 < 0 )
return 0LL;
if ( v4 > 62 )
{
if ( v4 == 124 )
{
LABEL_42:
v13 = stdin;
i[1] = 0;
ungetc(v4, v13);
return 1LL;
}
}
else if ( v4 > 8 )
{
switch ( v4 )
{
case 9:
case 32:
i[1] = 0;
result = 1LL;
break;
case 10:
case 38:
case 59:
case 60:
case 62:
goto LABEL_42;
case 34:
case 39:
goto LABEL_20;
default:
continue;
}
return result;
}
}
printf("Word too long (> %d bytes)\n", 63LL);
while ( 1 )
{
v15 = getchar();
if ( v15 <= 0 )
break;
if ( v15 == 10 )
return 0LL;
}
return 0LL;
}

original_code:
static Token nextToken()
{
  char* p;
  char c, q;
  Word[0] = '\0';
  p = Word;
  c = getchar();
  if ( c < 0 )
    return Tend;
  switch ( c ) {
  case ' ':
  case '\t':
    return nextToken();
  case '\n':
    return Tnl;
  case '&':
    return Tamp;
  case ';':
    return Tsemi;
  case '<':
    return Tin;
  case '|':
    do {c = getchar(); if (c < 0) return Terror;} while (0);
    if ( c == '&' )
      return TpipeErr;
    ungetc(c, stdin);
    return Tpipe;
  case '>':
    do {c = getchar(); if (c < 0) return Terror;} while (0);
    if ( c == '>' ) {
      do {c = getchar(); if (c < 0) return Terror;} while (0);
      if ( c == '&' )
 return TappErr;
      else {
 ungetc(c, stdin);
 return Tapp;
      }
    }
    else if ( c == '&' ) {
      return ToutErr;
    }
    else {
      ungetc(c, stdin);
      return Tout;
    }
    break;
  case '\'':
  case '"':
  string:
    q = c;
    c = getchar();
    while ( c != q ) {
      if ( c < 0 || c == '\n' ) {
 printf("Unmatched %c\n", q);
 return Terror;
      }
      *p++ = c;
      if ( p > Word + 63 ) {
 printf("String too long (> %d bytes)\n", 63);
 while ( (c = getchar()) > 0 && c != '\n' )
   ;
 return Terror;
      }
      c = getchar();
    }
    *p++ = '\0';
    p = Word;
    return Tword;
  default:
    while (1) {
      if ( c == '\\' ) {
 do {c = getchar(); if (c < 0) return Terror;} while (0);
      }
      *p++ = c;
      if ( p > Word + 63 ) {
 printf("Word too long (> %d bytes)\n", 63);
 while ( (c = getchar()) > 0 && c != '\n' )
   ;
 return Terror;
      }
      do {c = getchar(); if (c < 0) return Terror;} while (0);
      switch ( c ) {
      case ' ':
      case '\t':
 *p++ = '\0';
 return Tword;
      case '\n':
      case '&':
      case ';':
      case '<':
      case '|':
      case '>':
 *p++ = '\0';
 ungetc(c, stdin);
 p = Word;
 return Tword;
      case '\'':
      case '\"':
      goto string;
      default:
 break;
      }
    }
  }
}

canonical_original_code:
static enum Token nextToken()
{
  char *p;
  char c; char q;
  Word[0] = '\0';
  p = Word;
  c = getchar();
  if ( c < 0 )
    return Tend;
  switch ( c ) {
  case ' ':
  case '\t':
    return nextToken();
  case '\n':
    return Tnl;
  case '&':
    return Tamp;
  case ';':
    return Tsemi;
  case '<':
    return Tin;
  case '|':
    do {c = getchar(); if (c < 0) return Terror;} while (0);
    if ( c == '&' )
      return TpipeErr;
    ungetc(c, stdin);
    return Tpipe;
  case '>':
    do {c = getchar(); if (c < 0) return Terror;} while (0);
    if ( c == '>' ) {
      do {c = getchar(); if (c < 0) return Terror;} while (0);
      if ( c == '&' )
 return TappErr;
      else {
 ungetc(c, stdin);
 return Tapp;
      }
    }
    else if ( c == '&' ) {
      return ToutErr;
    }
    else {
      ungetc(c, stdin);
      return Tout;
    }
    break;
  case '\'':
  case '"':
  string:
    q = c;
    c = getchar();
    while ( c != q ) {
      if ( c < 0 || c == '\n' ) {
 printf("Unmatched %c\n", q);
 return Terror;
      }
      *p++ = c;
      if ( p > Word + 63 ) {
 printf("String too long (> %d bytes)\n", 63);
 while ( (c = getchar()) > 0 && c != '\n' )
   ;
 return Terror;
      }
      c = getchar();
    }
    *p++ = '\0';
    p = Word;
    return Tword;
  default:
    while (1) {
      if ( c == '\\' ) {
 do {c = getchar(); if (c < 0) return Terror;} while (0);
      }
      *p++ = c;
      if ( p > Word + 63 ) {
 printf("Word too long (> %d bytes)\n", 63);
 while ( (c = getchar()) > 0 && c != '\n' )
   ;
 return Terror;
      }
      do {c = getchar(); if (c < 0) return Terror;} while (0);
      switch ( c ) {
      case ' ':
      case '\t':
 *p++ = '\0';
 return Tword;
      case '\n':
      case '&':
      case ';':
      case '<':
      case '|':
      case '>':
 *p++ = '\0';
 ungetc(c, stdin);
 p = Word;
 return Tword;
      case '\'':
      case '\"':
      goto string;
      default:
 break;
      }
    }
  }
}

variable_types:
p
char *

variable_types: c char

variable_types: q char

return_type: enum Token { Terror=0, Tword=1, Tamp=2, Tpipe=3, Tsemi=4, Tin=5, Tout=6, Tapp=7, TpipeErr=8, ToutErr=9, TappErr=10, Tnl=11, Tnil=12, Tend=13 }


freeCmd

decompiled_code:
void __fastcall freeCmd(void *ptr)
{
void *v2; // rdi
void *v3; // rdi
_QWORD *v4; // rdi
__int64 v5; // rbx
void *v6; // rdi

if ( ptr != &Empty && ptr != 0LL && ptr != &End )
{
freeCmd(*((void **)ptr + 6));
v2 = (void *)*((_QWORD *)ptr + 2);
if ( v2 )
free(v2);
v3 = (void *)*((_QWORD *)ptr + 3);
if ( v3 )
free(v3);
v4 = (_QWORD *)*((_QWORD *)ptr + 5);
if ( v4 )
{
if ( *((int *)ptr + 8) > 0 )
{
v5 = 0LL;
do
{
v6 = (void *)v4[v5++];
free(v6);
v4 = (_QWORD *)*((_QWORD *)ptr + 5);
}
while ( *((_DWORD *)ptr + 8) > (int)v5 );
}
free(v4);
}
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
void *v2; // rdi
void *v3; // rdi
_QWORD *v4; // rdi
__int64 v5; // rbx
void *v6; // rdi

if ( ptr != &Empty && ptr != 0LL && ptr != &End )
{
func1(*((void **)ptr + 6));
v2 = (void *)*((_QWORD *)ptr + 2);
if ( v2 )
free(v2);
v3 = (void *)*((_QWORD *)ptr + 3);
if ( v3 )
free(v3);
v4 = (_QWORD *)*((_QWORD *)ptr + 5);
if ( v4 )
{
if ( *((int *)ptr + 8) > 0 )
{
v5 = 0LL;
do
{
v6 = (void *)v4[v5++];
free(v6);
v4 = (_QWORD *)*((_QWORD *)ptr + 5);
}
while ( *((_DWORD *)ptr + 8) > (int)v5 );
}
free(v4);
}
free(ptr);
}
}

original_code:
static void freeCmd(Cmd c)
{
  int i;
  if ( c == ((void *)0) || c == &Empty || c == &End ) return;
  freeCmd(c->next);
  if ( c->infile )
    free(c->infile);
  if ( c->outfile )
    free(c->outfile);
  if ( c->args ) {
    for ( i = 0; i < c->nargs; i++ )
      free(c->args[i]);
    free(c->args);
  }
  free(c);
}

canonical_original_code:
static void freeCmd(struct cmd_t *c)
{
  int i;
  if ( c == ((void *)0) || c == &Empty || c == &End ) return;
  freeCmd(c->next);
  if ( c->infile )
    free(c->infile);
  if ( c->outfile )
    free(c->outfile);
  if ( c->args ) {
    for ( i = 0; i < c->nargs; i++ )
      free(c->args[i]);
    free(c->args);
  }
  free(c);
}


ckmalloc

decompiled_code:
void *__fastcall ckmalloc(size_t size)
{
void *result; // rax
int *v2; // rax

result = malloc((unsigned int)size);
if ( !result )
{
perror("malloc");
v2 = _errno_location();
exit(*v2);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func2(size_t size)
{
void *result; // rax
int *v2; // rax

result = malloc((unsigned int)size);
if ( !result )
{
perror("malloc");
v2 = _errno_location();
exit(*v2);
}
return result;
}

original_code:
void *ckmalloc(unsigned l)
{
  void *p;
  p = malloc(l);
  if ( p == ((void *)0) ) {
    perror("malloc");
    exit((*__errno_location ()));
  }
  return p;
}

canonical_original_code:
void *ckmalloc(unsigned int l)
{
  void *p;
  p = malloc(l);
  if ( p == ((void *)0) ) {
    perror("malloc");
    exit((*__errno_location ()));
  }
  return p;
}

variable_types:
l
unsigned int

variable_types:
p
void *

return_type: void *


mkPipe

decompiled_code:
_QWORD *mkPipe()
{
_QWORD *v0; // rax
_QWORD *v1; // rbp
_QWORD *v2; // rax
_QWORD *v3; // r12
bool v4; // zf
int v5; // eax
int v6; // edi
_QWORD *v7; // rax
int v8; // eax
int v9; // eax
int i; // eax

v0 = mkCmd(12);
if ( v0 != (_QWORD *)&Empty )
{
v1 = v0;
if ( v0 )
{
v2 = ckmalloc(0x18uLL);
*(_DWORD *)v2 = 0;
v3 = v2;
v2[1] = v1;
while ( 1 )
{
v8 = LookAhead;
if ( LookAhead != 3 )
break;
if ( LookAhead == 8 )
goto LABEL_5;
if ( *((_DWORD *)v1 + 2) != 12 )
{
LABEL_12:
puts("Ambiguous output redirect.");
do
{
v9 = nextToken();
LookAhead = v9;
}
while ( ((v9 - 11) & 0xFFFFFFFD) != 0 && v9 );
freeCmd(v1);
return 0LL;
}
LABEL_6:
v5 = nextToken();
v6 = -(*(_DWORD *)v3 == 0);
LookAhead = v5;
v7 = mkCmd((v6 & 0xFFFFFFFB) + 8);
v1[6] = v7;
if ( !v7 )
goto LABEL_17;
if ( v7 == (_QWORD *)&Empty )
{
puts("Invalid null command.");
LABEL_17:
for ( i = LookAhead; ((i - 11) & 0xFFFFFFFD) != 0; LookAhead = i )
{
if ( !i )
break;
i = nextToken();
}
return 0LL;
}
v1 = v7;
}
if ( LookAhead != 8 )
{
v3[2] = 0LL;
if ( ((v8 - 11) & 0xFFFFFFFD) != 0 )
{
do
{
if ( !v8 )
break;
v3[2] = mkPipe();
v8 = LookAhead;
}
while ( ((LookAhead - 11) & 0xFFFFFFFD) != 0 );
}
return v3;
}
LABEL_5:
v4 = *((_DWORD *)v1 + 2) == 12;
*(_DWORD *)v3 = 1;
if ( !v4 )
goto LABEL_12;
goto LABEL_6;
}
}
return 0LL;
}

canonical_decompiled_code:
_QWORD *func5()
{
_QWORD *v0; // rax
_QWORD *v1; // rbp
_QWORD *v2; // rax
_QWORD *v3; // r12
bool v4; // zf
int v5; // eax
int v6; // edi
_QWORD *v7; // rax
int v8; // eax
int v9; // eax
int i; // eax

v0 = mkCmd(12);
if ( v0 != (_QWORD *)&Empty )
{
v1 = v0;
if ( v0 )
{
v2 = func2(0x18uLL);
*(_DWORD *)v2 = 0;
v3 = v2;
v2[1] = v1;
while ( 1 )
{
v8 = LookAhead;
if ( LookAhead != 3 )
break;
if ( LookAhead == 8 )
goto LABEL_5;
if ( *((_DWORD *)v1 + 2) != 12 )
{
LABEL_12:
puts("Ambiguous output redirect.");
do
{
v9 = func0();
LookAhead = v9;
}
while ( ((v9 - 11) & 0xFFFFFFFD) != 0 && v9 );
func1(v1);
return 0LL;
}
LABEL_6:
v5 = func0();
v6 = -(*(_DWORD *)v3 == 0);
LookAhead = v5;
v7 = mkCmd((v6 & 0xFFFFFFFB) + 8);
v1[6] = v7;
if ( !v7 )
goto LABEL_17;
if ( v7 == (_QWORD *)&Empty )
{
puts("Invalid null command.");
LABEL_17:
for ( i = LookAhead; ((i - 11) & 0xFFFFFFFD) != 0; LookAhead = i )
{
if ( !i )
break;
i = func0();
}
return 0LL;
}
v1 = v7;
}
if ( LookAhead != 8 )
{
v3[2] = 0LL;
if ( ((v8 - 11) & 0xFFFFFFFD) != 0 )
{
do
{
if ( !v8 )
break;
v3[2] = func5();
v8 = LookAhead;
}
while ( ((LookAhead - 11) & 0xFFFFFFFD) != 0 );
}
return v3;
}
LABEL_5:
v4 = *((_DWORD *)v1 + 2) == 12;
*(_DWORD *)v3 = 1;
if ( !v4 )
goto LABEL_12;
goto LABEL_6;
}
}
return 0LL;
}

original_code:
static Pipe mkPipe()
{
  Pipe p;
  Cmd c;
  c = mkCmd(Tnil);
  if ( c == ((void *)0) || c == &Empty )
    return ((void *)0);
  p = ckmalloc(sizeof(*p));
  p->type = Pout;
  p->head = c;
  while ( ((LookAhead)==Tpipe||(LookAhead)==TpipeErr) ) {
    if ( LookAhead == TpipeErr )
      p->type = PoutErr;
    if ( c->out != Tnil ) {
      printf("Ambiguous output redirect.\n");
      do {
 do { LookAhead = nextToken(); } while (0);
      } while ( !((LookAhead)==Tend||(LookAhead)==Tnl||(LookAhead)==Terror) );
      freeCmd(c);
      return ((void *)0);
    }
    do { LookAhead = nextToken(); } while (0);
    c->next = mkCmd(p->type == Pout ? Tpipe : TpipeErr);
    if ( c->next == ((void *)0) || c->next == &Empty ) {
      if ( c->next == &Empty )
 printf("Invalid null command.\n");
      while ( !((LookAhead)==Tend||(LookAhead)==Tnl||(LookAhead)==Terror) )
 do { LookAhead = nextToken(); } while (0);
      return ((void *)0);
    }
    c = c->next;
  }
  p->next = ((void *)0);
  while ( !((LookAhead)==Tend||(LookAhead)==Tnl||(LookAhead)==Terror) )
    p->next = mkPipe();
  return p;
}

canonical_original_code:
static struct pipe_t *mkPipe()
{
  struct pipe_t *p;
  struct cmd_t *c;
  c = mkCmd(Tnil);
  if ( c == ((void *)0) || c == &Empty )
    return ((void *)0);
  p = ckmalloc(sizeof(*p));
  p->type = Pout;
  p->head = c;
  while ( ((LookAhead)==Tpipe||(LookAhead)==TpipeErr) ) {
    if ( LookAhead == TpipeErr )
      p->type = PoutErr;
    if ( c->out != Tnil ) {
      printf("Ambiguous output redirect.\n");
      do {
 do { LookAhead = nextToken(); } while (0);
      } while ( !((LookAhead)==Tend||(LookAhead)==Tnl||(LookAhead)==Terror) );
      freeCmd(c);
      return ((void *)0);
    }
    do { LookAhead = nextToken(); } while (0);
    c->next = mkCmd(p->type == Pout ? Tpipe : TpipeErr);
    if ( c->next == ((void *)0) || c->next == &Empty ) {
      if ( c->next == &Empty )
 printf("Invalid null command.\n");
      while ( !((LookAhead)==Tend||(LookAhead)==Tnl||(LookAhead)==Terror) )
 do { LookAhead = nextToken(); } while (0);
      return ((void *)0);
    }
    c = c->next;
  }
  p->next = ((void *)0);
  while ( !((LookAhead)==Tend||(LookAhead)==Tnl||(LookAhead)==Terror) )
    p->next = mkPipe();
  return p;
}

return_type: struct pipe_t { enum Ptype { Pout=0, PoutErr=1 } type; struct cmd_t { enum Token { Terror=0, Tword=1, Tamp=2, Tpipe=3, Tsemi=4, Tin=5, Tout=6, Tapp=7, TpipeErr=8, ToutErr=9, TappErr=10, Tnl=11, Tnil=12, Tend=13 } exec; enum Token { Terror=0, Tword=1, Tamp=2, Tpipe=3, Tsemi=4, Tin=5, Tout=6, Tapp=7, TpipeErr=8, ToutErr=9, TappErr=10, Tnl=11, Tnil=12, Tend=13 } in; char *infile; int nargs; char **args; struct cmd_t *next; } *head; struct pipe_t *next; } *


parse

decompiled_code:
_QWORD *parse()
{
LookAhead = nextToken();
return mkPipe();
}

canonical_decompiled_code:
_QWORD *func6()
{
LookAhead = func0();
return func5();
}

original_code:
Pipe parse()
{
  Pipe p;
  do { LookAhead = nextToken(); } while (0);
  p = mkPipe();
  return p;
}

canonical_original_code:
struct pipe_t *parse()
{
  struct pipe_t *p;
  do { LookAhead = nextToken(); } while (0);
  p = mkPipe();
  return p;
}


freePipe

decompiled_code:
void __fastcall freePipe(void *ptr)
{
if ( ptr )
{
freeCmd(*((void **)ptr + 1));
freePipe(*((void **)ptr + 2));
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func7(void *ptr)
{
if ( ptr )
{
func1(*((void **)ptr + 1));
func7(*((void **)ptr + 2));
free(ptr);
}
}

original_code:
void freePipe(Pipe p)
{
  if ( p == ((void *)0) )
    return;
  freeCmd(p->head);
  freePipe(p->next);
  free(p);
}

canonical_original_code:
void freePipe(struct pipe_t *p)
{
  if ( p == ((void *)0) )
    return;
  freeCmd(p->head);
  freePipe(p->next);
  free(p);
}


sendPotato

decompiled_code:
ssize_t __fastcall sendPotato(__int64 a1, const char *a2, const char *a3, int a4)
{
size_t v6; // rax
ssize_t result; // rax
size_t v8; // rax
_BYTE v9[1032]; // [rsp+0h] [rbp-408h] BYREF

v6 = strlen(a3);
result = send(a4, a3, v6, 0);
if ( result == -1 )
goto LABEL_6;
if ( a2 )
{
memset(v9, 0, 0x3E8uLL);
if ( recv(a4, v9, 0x3E8uLL, 0) == -1 )
{
puts("Receive error! ");
exit(-1);
}
v8 = strlen(a2);
result = send(a4, a2, v8, 0);
if ( result == -1 )
{
LABEL_6:
puts("Sending failed");
exit(-1);
}
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func1(__int64 a1, const char *a2, const char *a3, int a4)
{
size_t v6; // rax
ssize_t result; // rax
size_t v8; // rax
_BYTE v9[1032]; // [rsp+0h] [rbp-408h] BYREF

v6 = strlen(a3);
result = send(a4, a3, v6, 0);
if ( result == -1 )
goto LABEL_6;
if ( a2 )
{
memset(v9, 0, 0x3E8uLL);
if ( recv(a4, v9, 0x3E8uLL, 0) == -1 )
{
puts("Receive error! ");
exit(-1);
}
v8 = strlen(a2);
result = send(a4, a2, v8, 0);
if ( result == -1 )
{
LABEL_6:
puts("Sending failed");
exit(-1);
}
}
return result;
}

original_code:
void sendPotato(potato p,char msg[],int sock)
{
 if(send(sock,msg,strlen(msg),0)==-1)
 {
  printf("Sending failed\n");
  exit(-1);
 }
 if(p.identities)
 {
  char buf[1000];
  memset(buf,0,1000);
  if(recv(sock, buf, 1000, 0) == -1)
  {
   printf("Receive error! \n");
   exit(-1);
  }
  if(send(sock,p.identities,strlen(p.identities),0)==-1)
  {
   printf("Sending failed\n");
   exit(-1);
  }
 }
}

canonical_original_code:
void sendPotato(struct potato_ p, char msg[], int sock)
{
 if(send(sock,msg,strlen(msg),0)==-1)
 {
  printf("Sending failed\n");
  exit(-1);
 }
 if(p.identities)
 {
  char buf[1000];
  memset(buf,0,1000);
  if(recv(sock, buf, 1000, 0) == -1)
  {
   printf("Receive error! \n");
   exit(-1);
  }
  if(send(sock,p.identities,strlen(p.identities),0)==-1)
  {
   printf("Sending failed\n");
   exit(-1);
  }
 }
}

variable_types: struct potato_ { int hops; char *identities; }

variable_types:
msg
char []

variable_types: sock int

variable_types:
buf
char [1000]


getRandom

decompiled_code:
__int64 __fastcall getRandom(int a1, int a2)
{
struct timeval v3; // [rsp+0h] [rbp-28h] BYREF

gettimeofday(&v3, 0LL);
srand(LODWORD(v3.tv_sec) + LODWORD(v3.tv_usec));
return (unsigned int)(rand() % (a2 - a1) + a1);
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1, int a2)
{
struct timeval v3; // [rsp+0h] [rbp-28h] BYREF

gettimeofday(&v3, 0LL);
srand(LODWORD(v3.tv_sec) + LODWORD(v3.tv_usec));
return (unsigned int)(rand() % (a2 - a1) + a1);
}

original_code:
int getRandom(int lower,int upper)
{
 struct timeval tv;
 int seed;
 gettimeofday(&tv,((void *)0));
 seed = tv.tv_sec+tv.tv_usec;
 srand(seed);
 return ((rand()%(upper-lower))+lower);
}

canonical_original_code:
int getRandom(int lower, int upper)
{
 struct timeval tv;
 int seed;
 gettimeofday(&tv,((void *)0));
 seed = tv.tv_sec+tv.tv_usec;
 srand(seed);
 return ((rand()%(upper-lower))+lower);
}

variable_types: lower int

variable_types: upper int

variable_types: struct timeval { long tv_sec; long tv_usec; }

variable_types: seed int


listenSocket

decompiled_code:
int __fastcall listenSocket(int a1)
{
int result; // eax

result = listen(a1, 10);
if ( result == -1 )
{
puts("listen error");
exit(-1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func4(int a1)
{
int result; // eax

result = listen(a1, 10);
if ( result == -1 )
{
puts("listen error");
exit(-1);
}
return result;
}

original_code:
void listenSocket(int soc)
{
        if(listen(soc,10) == -1) {
                printf("listen error\n");
                exit(-1);
        }
}

variable_types: soc int


acceptConnection

decompiled_code:
int __fastcall acceptConnection(int a1)
{
int result; // eax
socklen_t addr_len; // [rsp+Ch] [rbp-1Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-18h] BYREF

addr_len = 16;
result = accept(a1, &addr, &addr_len);
if ( result == -1 )
{
puts("accept call failed! ");
exit(-1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func5(int a1)
{
int result; // eax
socklen_t addr_len; // [rsp+Ch] [rbp-1Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-18h] BYREF

addr_len = 16;
result = accept(a1, &addr, &addr_len);
if ( result == -1 )
{
puts("accept call failed! ");
exit(-1);
}
return result;
}

original_code:
int acceptConnection(int soc)
{
 int conn_port;
 struct sockaddr_in sock_client;
 int slen = sizeof(sock_client);
 if ((conn_port = accept(soc, (struct sockaddr *) &sock_client, &slen)) == -1) {
  printf("accept call failed! \n");
  exit(-1);
 }
 return conn_port;
}

variable_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; }

variable_types: slen int

user_defined_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr sin_addr; unsigned char sin_zero[-1]; }

user_defined_types: struct sockaddr { unsigned short sa_family; char sa_data[14]; }


createConnection

decompiled_code:
int __fastcall createConnection(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, __int64 a7, int a8, int a9, int a10, int a11, int a12, int a13, int a14, int a15, int a16, int a17, int a18, int a19, int a20, int a21, int a22, __int64 a23, __int16 a24)
{
void *v24; // rdx
int result; // eax
struct sockaddr v26; // [rsp+0h] [rbp-18h] BYREF

v26 = 0LL;
v26.sa_family = 2;
*(_WORD *)v26.sa_data = _bswap_16(a24);
inet_pton(2, (const char *)&a7 + 4, v24);
result = connect(SHIDWORD(a23), &v26, 0x10u);
if ( result == -1 )
{
puts("Connect failed! Check the IP and port number of the Server! ");
exit(-1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func6(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, __int64 a7, int a8, int a9, int a10, int a11, int a12, int a13, int a14, int a15, int a16, int a17, int a18, int a19, int a20, int a21, int a22, __int64 a23, __int16 a24)
{
void *v24; // rdx
int result; // eax
struct sockaddr v26; // [rsp+0h] [rbp-18h] BYREF

v26 = 0LL;
v26.sa_family = 2;
*(_WORD *)v26.sa_data = func0(a24);
inet_pton(2, (const char *)&a7 + 4, v24);
result = connect(SHIDWORD(a23), &v26, 0x10u);
if ( result == -1 )
{
puts("Connect failed! Check the IP and port number of the Server! ");
exit(-1);
}
return result;
}

original_code:
int createConnection(player_tracker p)
{
        struct sockaddr_in sock_client;
 int slen = sizeof(sock_client);
        int ret;
        memset((char *) &sock_client, 0, sizeof(sock_client));
        sock_client.sin_family = 2;
        sock_client.sin_port = htons(p.listen_port);
 ret = inet_pton(2, p.ip_addr, (void *) &sock_client.sin_addr);
 ret = connect(p.conn_port, (struct sockaddr *) &sock_client, slen);
 if (ret == -1) {
  printf("Connect failed! Check the IP and port number of the Server! \n");
  exit(-1);
 }
}

canonical_original_code:
int createConnection(struct player_tracker_ p)
{
        struct sockaddr_in sock_client;
 int slen = sizeof(sock_client);
        int ret;
        memset((char *) &sock_client, 0, sizeof(sock_client));
        sock_client.sin_family = 2;
        sock_client.sin_port = htons(p.listen_port);
 ret = inet_pton(2, p.ip_addr, (void *) &sock_client.sin_addr);
 ret = connect(p.conn_port, (struct sockaddr *) &sock_client, slen);
 if (ret == -1) {
  printf("Connect failed! Check the IP and port number of the Server! \n");
  exit(-1);
 }
}

variable_types: struct player_tracker_ { int id; char ip_addr[128]; int conn_port; int listen_port; }

variable_types: ret int


createSocket

decompiled_code:
int createSocket()
{
int result; // eax

result = socket(2, 1, 0);
if ( result == -1 )
{
puts("error in socket creation");
exit(-1);
}
return result;
}

canonical_decompiled_code:
int func7()
{
int result; // eax

result = socket(2, 1, 0);
if ( result == -1 )
{
puts("error in socket creation");
exit(-1);
}
return result;
}

original_code:
int createSocket()
{
 int soc;
 if ((soc= socket(2, SOCK_STREAM, 0)) == -1) {
  printf("error in socket creation\n");
  exit(-1);
 }
 return soc;
}


bindSocket

decompiled_code:
__int64 __fastcall bindSocket(int fd, unsigned int a2)
{
unsigned int v2; // er12
const char *v3; // rdx
struct sockaddr v5; // [rsp+0h] [rbp-28h] BYREF

v2 = a2;
v5 = 0LL;
v5.sa_family = 2;
*(_WORD *)v5.sa_data = _bswap_16(a2);
*(_DWORD *)&v5.sa_data[2] = inet_addr(v3);
while ( bind(fd, &v5, 0x10u) == -1 )
{
v2 = getRandom(64500, 65636);
*(_WORD *)v5.sa_data = _bswap_16(v2);
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func8(int fd, unsigned int a2)
{
unsigned int v2; // er12
const char *v3; // rdx
struct sockaddr v5; // [rsp+0h] [rbp-28h] BYREF

v2 = a2;
v5 = 0LL;
v5.sa_family = 2;
*(_WORD *)v5.sa_data = func0(a2);
*(_DWORD *)&v5.sa_data[2] = inet_addr(v3);
while ( bind(fd, &v5, 0x10u) == -1 )
{
v2 = func3(64500, 65636);
*(_WORD *)v5.sa_data = func0(v2);
}
return v2;
}

original_code:
int bindSocket(int soc, int listen_port, char ip_addr[])
{
 struct sockaddr_in sock_server;
 memset((char *) &sock_server, 0, sizeof(sock_server));
 sock_server.sin_family = 2;
 sock_server.sin_port = htons(listen_port);
 sock_server.sin_addr.s_addr = inet_addr(ip_addr);
 while (bind(soc, (struct sockaddr *) &sock_server, sizeof(sock_server)) == -1) {
  listen_port = getRandom(64500,65636);
  sock_server.sin_port = htons(listen_port);
 }
 return listen_port;
}


repo: mazzoo/ogldump

decompiled_code:
void *append_file()
{
int v0; // ebx
char *v1; // rdi
unsigned int v2; // edx
char *v3; // rsi
int v4; // er8
float *v5; // rax
float v6; // xmm0_4
unsigned int v7; // ecx
float v8; // xmm1_4
float *v9; // rax
unsigned int v10; // ecx
float v11; // xmm0_4
float v12; // xmm1_4
int v14; // er9
float v15; // xmm0_4
float *v16; // rcx
float *v17; // rax
float v18; // xmm1_4
int *v19; // rax
char *v20; // rax

v0 = n_tot;
if ( t_tot )
t_tot = realloc(t_tot, (unsigned int)(50 * (n_tot + n_file)));
else
t_tot = malloc((unsigned int)(50 * n_file));
v1 = (char *)t_tot;
if ( !t_tot )
{
v19 = __errno_location();
v20 = strerror(*v19);
printf(
"!!! couldn't (re)alloc %d bytes to %d bytes: %s\n",
(unsigned int)n_tot,
(unsigned int)(50 * (n_tot + n_file)),
v20);
exit(1);
}
v2 = n_file;
v3 = (char *)t_file;
LODWORD(n_tot) = n_file + n_tot;
if ( opt_x_off )
{
if ( !(_DWORD)n_file )
return memcpy(&v1[50 * v0], v3, 50 * v2);
v14 = file_count;
v4 = file_count;
v15 = (float)(file_count * x_off);
v16 = (float *)((char *)t_file + 50 * (unsigned int)n_file);
v17 = (float *)t_file;
do
{
v18 = v17[3];
v17 = (float *)((char *)v17 + 50);
*(float *)((char *)v17 - 38) = v18 + v15;
*(float *)((char *)v17 - 26) = *(float *)((char *)v17 - 26) + v15;
*(float *)((char *)v17 - 14) = *(float *)((char *)v17 - 14) + v15;
}
while ( v17 != v16 );
if ( opt_y_off )
{
v5 = (float *)(v3 + 4);
goto LABEL_7;
}
if ( !opt_z_off )
return memcpy(&v1[50 * v0], v3, 50 * v2);
v9 = (float *)(v3 + 8);
LABEL_23:
v4 = v14;
LABEL_11:
v10 = 0;
v11 = (float)(z_off * v4);
do
{
v12 = v9[3];
++v10;
v9 = (float *)((char *)v9 + 50);
*(float *)((char *)v9 - 38) = v12 + v11;
*(float *)((char *)v9 - 26) = *(float *)((char *)v9 - 26) + v11;
*(float *)((char *)v9 - 14) = *(float *)((char *)v9 - 14) + v11;
}
while ( v2 > v10 );
return memcpy(&v1[50 * v0], v3, 50 * v2);
}
if ( !opt_y_off )
{
if ( !opt_z_off )
return memcpy(&v1[50 * v0], v3, 50 * v2);
v9 = (float *)((char *)t_file + 8);
if ( !(_DWORD)n_file )
return memcpy(&v1[50 * v0], v3, 50 * v2);
v14 = file_count;
goto LABEL_23;
}
v4 = file_count;
v5 = (float *)((char *)t_file + 4);
if ( !(_DWORD)n_file )
return memcpy(&v1[50 * v0], v3, 50 * v2);
LABEL_7:
v6 = (float)(v4 * y_off);
v7 = 0;
do
{
v8 = v5[3];
++v7;
v5 = (float *)((char *)v5 + 50);
*(float *)((char *)v5 - 38) = v8 + v6;
*(float *)((char *)v5 - 26) = *(float *)((char *)v5 - 26) + v6;
*(float *)((char *)v5 - 14) = *(float *)((char *)v5 - 14) + v6;
}
while ( v2 > v7 );
if ( opt_z_off )
{
v9 = (float *)(v3 + 8);
goto LABEL_11;
}
return memcpy(&v1[50 * v0], v3, 50 * v2);
}

canonical_decompiled_code:
void *func8()
{
int v0; // ebx
char *v1; // rdi
unsigned int v2; // edx
char *v3; // rsi
int v4; // er8
float *v5; // rax
float v6; // xmm0_4
unsigned int v7; // ecx
float v8; // xmm1_4
float *v9; // rax
unsigned int v10; // ecx
float v11; // xmm0_4
float v12; // xmm1_4
int v14; // er9
float v15; // xmm0_4
float *v16; // rcx
float *v17; // rax
float v18; // xmm1_4
int *v19; // rax
char *v20; // rax

v0 = n_tot;
if ( t_tot )
t_tot = realloc(t_tot, (unsigned int)(50 * (n_tot + n_file)));
else
t_tot = malloc((unsigned int)(50 * n_file));
v1 = (char *)t_tot;
if ( !t_tot )
{
v19 = __errno_location();
v20 = strerror(*v19);
printf(
"!!! couldn't (re)alloc %d bytes to %d bytes: %s\n",
(unsigned int)n_tot,
(unsigned int)(50 * (n_tot + n_file)),
v20);
exit(1);
}
v2 = n_file;
v3 = (char *)t_file;
LODWORD(n_tot) = n_file + n_tot;
if ( opt_x_off )
{
if ( !(_DWORD)n_file )
return memcpy(&v1[50 * v0], v3, 50 * v2);
v14 = file_count;
v4 = file_count;
v15 = (float)(file_count * x_off);
v16 = (float *)((char *)t_file + 50 * (unsigned int)n_file);
v17 = (float *)t_file;
do
{
v18 = v17[3];
v17 = (float *)((char *)v17 + 50);
*(float *)((char *)v17 - 38) = v18 + v15;
*(float *)((char *)v17 - 26) = *(float *)((char *)v17 - 26) + v15;
*(float *)((char *)v17 - 14) = *(float *)((char *)v17 - 14) + v15;
}
while ( v17 != v16 );
if ( opt_y_off )
{
v5 = (float *)(v3 + 4);
goto LABEL_7;
}
if ( !opt_z_off )
return memcpy(&v1[50 * v0], v3, 50 * v2);
v9 = (float *)(v3 + 8);
LABEL_23:
v4 = v14;
LABEL_11:
v10 = 0;
v11 = (float)(z_off * v4);
do
{
v12 = v9[3];
++v10;
v9 = (float *)((char *)v9 + 50);
*(float *)((char *)v9 - 38) = v12 + v11;
*(float *)((char *)v9 - 26) = *(float *)((char *)v9 - 26) + v11;
*(float *)((char *)v9 - 14) = *(float *)((char *)v9 - 14) + v11;
}
while ( v2 > v10 );
return memcpy(&v1[50 * v0], v3, 50 * v2);
}
if ( !opt_y_off )
{
if ( !opt_z_off )
return memcpy(&v1[50 * v0], v3, 50 * v2);
v9 = (float *)((char *)t_file + 8);
if ( !(_DWORD)n_file )
return memcpy(&v1[50 * v0], v3, 50 * v2);
v14 = file_count;
goto LABEL_23;
}
v4 = file_count;
v5 = (float *)((char *)t_file + 4);
if ( !(_DWORD)n_file )
return memcpy(&v1[50 * v0], v3, 50 * v2);
LABEL_7:
v6 = (float)(v4 * y_off);
v7 = 0;
do
{
v8 = v5[3];
++v7;
v5 = (float *)((char *)v5 + 50);
*(float *)((char *)v5 - 38) = v8 + v6;
*(float *)((char *)v5 - 26) = *(float *)((char *)v5 - 26) + v6;
*(float *)((char *)v5 - 14) = *(float *)((char *)v5 - 14) + v6;
}
while ( v2 > v7 );
if ( opt_z_off )
{
v9 = (float *)(v3 + 8);
goto LABEL_11;
}
return memcpy(&v1[50 * v0], v3, 50 * v2);
}

original_code:
void append_file(void)
{
    int n_old = n_tot;
    int i, j;
    float * p;
    if (!t_tot) {
        t_tot = malloc(50 * n_file);
    } else {
        t_tot = realloc(t_tot, (n_tot + n_file)*50);
    }
    if (!t_tot) {
        printf("!!! couldn't (re)alloc %d bytes to %d bytes: %s\n",
                n_tot,
                (n_tot + n_file) * 50,
                strerror((*__errno_location ())));
        exit(1);
    }
    n_tot += n_file;
    if (opt_x_off) {
        p = t_file;
        for (i=0; i<n_file; i++) {
            p += 3;
            for (j=0; j<3; j++){
                *p += x_off * file_count;
                p += 3;
            }
            p = (float *)((char *)p + 2);
        }
    }
    if (opt_y_off) {
        p = t_file;
        p++;
        for (i=0; i<n_file; i++) {
            p += 3;
            for (j=0; j<3; j++){
                *p += y_off * file_count;
                p += 3;
            }
            p = (float *)((char *)p + 2);
        }
    }
    if (opt_z_off) {
        p = t_file;
        p++;
        p++;
        for (i=0; i<n_file; i++) {
            p += 3;
            for (j=0; j<3; j++){
                *p += z_off * file_count;
                p += 3;
            }
            p = (float *)((char *)p + 2);
        }
    }
    memcpy((char *)t_tot + n_old * 50, t_file, n_file * 50);
}

canonical_original_code:
void append_file()
{
    int n_old = n_tot;
    int i; int j;
    float *p;
    if (!t_tot) {
        t_tot = malloc(50 * n_file);
    } else {
        t_tot = realloc(t_tot, (n_tot + n_file)*50);
    }
    if (!t_tot) {
        printf("!!! couldn't (re)alloc %d bytes to %d bytes: %s\n",
                n_tot,
                (n_tot + n_file) * 50,
                strerror((*__errno_location ())));
        exit(1);
    }
    n_tot += n_file;
    if (opt_x_off) {
        p = t_file;
        for (i=0; i<n_file; i++) {
            p += 3;
            for (j=0; j<3; j++){
                *p += x_off * file_count;
                p += 3;
            }
            p = (float *)((char *)p + 2);
        }
    }
    if (opt_y_off) {
        p = t_file;
        p++;
        for (i=0; i<n_file; i++) {
            p += 3;
            for (j=0; j<3; j++){
                *p += y_off * file_count;
                p += 3;
            }
            p = (float *)((char *)p + 2);
        }
    }
    if (opt_z_off) {
        p = t_file;
        p++;
        p++;
        for (i=0; i<n_file; i++) {
            p += 3;
            for (j=0; j<3; j++){
                *p += z_off * file_count;
                p += 3;
            }
            p = (float *)((char *)p + 2);
        }
    }
    memcpy((char *)t_tot + n_old * 50, t_file, n_file * 50);
}

variable_types: i int

variable_types: j int

variable_types:
p
float *


decompiled_code:
void free_file()
{
fclose(f);
free(t_file);
++file_count;
t_file = 0LL;
f = 0LL;
}

canonical_decompiled_code:
void func9()
{
fclose(f);
free(t_file);
++file_count;
t_file = 0LL;
f = 0LL;
}

original_code:
void free_file(void)
{
    fclose(f);
    free(t_file);
    t_file = ((void *)0);
    f = ((void *)0);
    file_count++;
}

canonical_original_code:
void free_file()
{
    fclose(f);
    free(t_file);
    t_file = ((void *)0);
    f = ((void *)0);
    file_count++;
}


decompiled_code:
int __fastcall stl_process(int a1, const char **a2)
{
__int64 v2; // rbp
const char **v3; // rbx
const char *v4; // rdi
int result; // eax

if ( a1 )
{
if ( a1 > 0 )
{
v2 = (__int64)&a2[(unsigned int)(a1 - 1) + 1];
v3 = a2;
do
{
v4 = *v3++;
read_file(v4);
append_file();
free_file();
}
while ( v3 != (const char **)v2 );
}
result = write_file();
}
return result;
}

canonical_decompiled_code:
int __fastcall func12(int a1, const char **a2)
{
__int64 v2; // rbp
const char **v3; // rbx
const char *v4; // rdi
int result; // eax

if ( a1 )
{
if ( a1 > 0 )
{
v2 = (__int64)&a2[(unsigned int)(a1 - 1) + 1];
v3 = a2;
do
{
v4 = *v3++;
func7(v4);
func8();
func9();
}
while ( v3 != (const char **)v2 );
}
result = func10();
}
return result;
}

original_code:
void stl_process(int argc, char ** argv)
{
    int i;
    if (!argc) {
    } else {
        for (i=0; i<argc; i++) {
            read_file(argv[i]);
            append_file();
            free_file();
        }
        write_file();
    }
}

canonical_original_code:
void stl_process(int argc, char **argv)
{
    int i;
    if (!argc) {
    } else {
        for (i=0; i<argc; i++) {
            read_file(argv[i]);
            append_file();
            free_file();
        }
        write_file();
    }
}

variable_types: argc int

variable_types:
argv
char **