decompiled_code:
__int64 fatal_trap_check()
{
__int64 *v0; // rax

v0 = sigtraps;
do
{
if ( *((_DWORD *)v0 + 8) && (*((_DWORD *)v0 + 9) & 0x240) != 0 )
return (unsigned int)(*(_DWORD *)v0 + 128);
v0 += 7;
}
while ( v0 != &sigtraps[462] );
return 0LL;
}

original_code:
int
fatal_trap_check(void)
{
 int i;
 Trap *p;
 for (p = sigtraps, i = (64 + 1)+1; --i >= 0; p++)
  if (p->set && (p->flags & ((1<<(6))|(1<<(9)))))
   return 128 + p->signal;
 return 0;
}

canonical_original_code:
int fatal_trap_check()
{
 int i;
 struct trap *p;
 for (p = sigtraps, i = (64 + 1)+1; --i >= 0; p++)
  if (p->set && (p->flags & ((1<<(6))|(1<<(9)))))
   return 128 + p->signal;
 return 0;
}

variable_types: i int

variable_types: struct trap { int signal; char *name; char *mess; char *trap; int set; int flags; void (*cursig)(int); void (*shtrap)(int); } *


repo: levaidaniel/ksh-openbsd

decompiled_code:
__int64 trap_pending()
{
__int64 *v0; // rax
_BYTE *v1; // rdx

v0 = sigtraps;
do
{
if ( *((_DWORD *)v0 + 8) )
{
v1 = (_BYTE *)v0[3];
if ( v1 )
{
if ( *v1 )
return *(unsigned int *)v0;
}
else if ( (*((_DWORD *)v0 + 9) & 0x240) != 0 )
{
return *(unsigned int *)v0;
}
}
v0 += 7;
}
while ( v0 != &sigtraps[462] );
return 0LL;
}

canonical_decompiled_code:
__int64 func2()
{
__int64 *v0; // rax
_BYTE *v1; // rdx

v0 = sigtraps;
do
{
if ( *((_DWORD *)v0 + 8) )
{
v1 = (_BYTE *)v0[3];
if ( v1 )
{
if ( *v1 )
return *(unsigned int *)v0;
}
else if ( (*((_DWORD *)v0 + 9) & 0x240) != 0 )
{
return *(unsigned int *)v0;
}
}
v0 += 7;
}
while ( v0 != &sigtraps[462] );
return 0LL;
}

original_code:
int
trap_pending(void)
{
 int i;
 Trap *p;
 for (p = sigtraps, i = (64 + 1)+1; --i >= 0; p++)
  if (p->set && ((p->trap && p->trap[0]) ||
      ((p->flags & ((1<<(6))|(1<<(9)))) && !p->trap)))
   return p->signal;
 return 0;
}

canonical_original_code:
int trap_pending()
{
 int i;
 struct trap *p;
 for (p = sigtraps, i = (64 + 1)+1; --i >= 0; p++)
  if (p->set && ((p->trap && p->trap[0]) ||
      ((p->flags & ((1<<(6))|(1<<(9)))) && !p->trap)))
   return p->signal;
 return 0;
}

return_type: int

user_defined_types: struct trap { int signal; char *name; char *mess; char *trap; int set; int flags; void (*cursig)(int); void (*shtrap)(int); }


runtrap

decompiled_code:
__int64 __fastcall runtrap(unsigned int *a1, __int64 a2)
{
int v2; // er12
_BYTE *v3; // rbp
__int64 result; // rax
unsigned int v5; // edx
unsigned int v6; // eax
__int16 v7; // r13
int v8; // er13

v3 = (_BYTE *)*((_QWORD *)a1 + 3);
result = *a1;
a1[8] = 0;
if ( v3 )
{
if ( !*v3 )
return result;
v2 = exstat;
a2 = current_lineno;
if ( (_DWORD)result && (_DWORD)result != 65 )
{
result = command(v3, current_lineno);
exstat = v2;
return result;
}
goto LABEL_10;
}
v5 = a1[9];
if ( (v5 & 0x200) != 0 )
{
exstat = result + 128;
unwind(4LL);
return runtraps();
}
if ( (v5 & 0x40) != 0 )
{
exstat = result + 128;
unwind(5LL);
LABEL_10:
v6 = a1[9];
*((_QWORD *)a1 + 3) = 0LL;
v7 = v6;
BYTE1(v6) &= 0xFEu;
a1[9] = v6;
v8 = v7 & 0x100;
command(v3, a2);
result = a1[9];
exstat = v2;
if ( (result & 0x100) != 0 )
{
afree(v3, &aperm);
result = a1[9];
}
else
{
*((_QWORD *)a1 + 3) = v3;
}
a1[9] = result | v8;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int *a1, __int64 a2)
{
int v2; // er12
_BYTE *v3; // rbp
__int64 result; // rax
unsigned int v5; // edx
unsigned int v6; // eax
__int16 v7; // r13
int v8; // er13

v3 = (_BYTE *)*((_QWORD *)a1 + 3);
result = *a1;
a1[8] = 0;
if ( v3 )
{
if ( !*v3 )
return result;
v2 = exstat;
a2 = current_lineno;
if ( (_DWORD)result && (_DWORD)result != 65 )
{
result = command(v3, current_lineno);
exstat = v2;
return result;
}
goto LABEL_10;
}
v5 = a1[9];
if ( (v5 & 0x200) != 0 )
{
exstat = result + 128;
unwind(4LL);
return func4();
}
if ( (v5 & 0x40) != 0 )
{
exstat = result + 128;
unwind(5LL);
LABEL_10:
v6 = a1[9];
*((_QWORD *)a1 + 3) = 0LL;
v7 = v6;
BYTE1(v6) &= 0xFEu;
a1[9] = v6;
v8 = v7 & 0x100;
command(v3, a2);
result = a1[9];
exstat = v2;
if ( (result & 0x100) != 0 )
{
afree(v3, &aperm);
result = a1[9];
}
else
{
*((_QWORD *)a1 + 3) = v3;
}
a1[9] = result | v8;
}
return result;
}

original_code:
void
runtrap(Trap *p)
{
 int i = p->signal;
 char *trapstr = p->trap;
 int oexstat;
 int old_changed = 0;
 p->set = 0;
 if (trapstr == ((void *)0)) {
  if (p->flags & (1<<(9))) {
   exstat = 128 + i;
   unwind(4);
  }
  if (p->flags & (1<<(6))) {
   exstat = 128 + i;
   unwind(5);
  }
  return;
 }
 if (trapstr[0] == '\0')
  return;
 if (i == 0 || i == (64 + 1)) {
  old_changed = p->flags & (1<<(8));
  p->flags &= ~(1<<(8));
  p->trap = ((void *)0);
 }
 oexstat = exstat;
 command(trapstr, current_lineno);
 exstat = oexstat;
 if (i == 0 || i == (64 + 1)) {
  if (p->flags & (1<<(8)))
   afree(trapstr, &aperm);
  else
   p->trap = trapstr;
  p->flags |= old_changed;
 }
}

canonical_original_code:
void runtrap(struct trap *p)
{
 int i = p->signal;
 char *trapstr = p->trap;
 int oexstat;
 int old_changed = 0;
 p->set = 0;
 if (trapstr == ((void *)0)) {
  if (p->flags & (1<<(9))) {
   exstat = 128 + i;
   unwind(4);
  }
  if (p->flags & (1<<(6))) {
   exstat = 128 + i;
   unwind(5);
  }
  return;
 }
 if (trapstr[0] == '\0')
  return;
 if (i == 0 || i == (64 + 1)) {
  old_changed = p->flags & (1<<(8));
  p->flags &= ~(1<<(8));
  p->trap = ((void *)0);
 }
 oexstat = exstat;
 command(trapstr, current_lineno);
 exstat = oexstat;
 if (i == 0 || i == (64 + 1)) {
  if (p->flags & (1<<(8)))
   afree(trapstr, &aperm);
  else
   p->trap = trapstr;
  p->flags |= old_changed;
 }
}

variable_types:
trapstr
char *

variable_types: oexstat int

return_type: void


runtraps

decompiled_code:
__int64 __fastcall runtraps(unsigned int a1, __int64 a2)
{
__int64 *v3; // rbx
__int64 result; // rax
unsigned int *v5; // rdi

if ( ksh_tmout_state == 2 )
{
ksh_tmout_state = 0;
warningf(0LL, "timed out waiting for input");
unwind(2LL);
result = intrcheck();
}
else
{
ksh_tmout_state = 0;
if ( a1 )
{
if ( (a1 & 0x40) != 0 )
intrsig = 0;
if ( (a1 & 0x200) != 0 )
fatal_trap = 0;
}
else
{
trap = 0;
}
v3 = sigtraps;
do
{
while ( 1 )
{
result = *((unsigned int *)v3 + 8);
if ( (_DWORD)result )
{
if ( !a1 )
break;
result = *((_DWORD *)v3 + 9) & a1;
if ( (*((_DWORD *)v3 + 9) & a1) != 0 && !v3[3] )
break;
}
v3 += 7;
if ( v3 == &sigtraps[462] )
return result;
}
v5 = (unsigned int *)v3;
v3 += 7;
result = runtrap(v5, a2);
}
while ( v3 != &sigtraps[462] );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int a1, __int64 a2)
{
__int64 *v3; // rbx
__int64 result; // rax
unsigned int *v5; // rdi

if ( ksh_tmout_state == 2 )
{
ksh_tmout_state = 0;
warningf(0LL, "timed out waiting for input");
unwind(2LL);
result = func5();
}
else
{
ksh_tmout_state = 0;
if ( a1 )
{
if ( (a1 & 0x40) != 0 )
intrsig = 0;
if ( (a1 & 0x200) != 0 )
fatal_trap = 0;
}
else
{
trap = 0;
}
v3 = sigtraps;
do
{
while ( 1 )
{
result = *((unsigned int *)v3 + 8);
if ( (_DWORD)result )
{
if ( !a1 )
break;
result = *((_DWORD *)v3 + 9) & a1;
if ( (*((_DWORD *)v3 + 9) & a1) != 0 && !v3[3] )
break;
}
v3 += 7;
if ( v3 == &sigtraps[462] )
return result;
}
v5 = (unsigned int *)v3;
v3 += 7;
result = func3(v5, a2);
}
while ( v3 != &sigtraps[462] );
}
return result;
}

original_code:
void
runtraps(int flag)
{
 int i;
 Trap *p;
 if (ksh_tmout_state == TMOUT_LEAVING) {
  ksh_tmout_state = TMOUT_EXECUTING;
  warningf(0, "timed out waiting for input");
  unwind(2);
 } else
  ksh_tmout_state = TMOUT_EXECUTING;
 if (!flag)
  trap = 0;
 if (flag & (1<<(6)))
  intrsig = 0;
 if (flag & (1<<(9)))
  fatal_trap = 0;
 for (p = sigtraps, i = (64 + 1)+1; --i >= 0; p++)
  if (p->set && (!flag ||
      ((p->flags & flag) && p->trap == ((void *)0))))
   runtrap(p);
}

canonical_original_code:
void runtraps(int flag)
{
 int i;
 struct trap *p;
 if (ksh_tmout_state == TMOUT_LEAVING) {
  ksh_tmout_state = TMOUT_EXECUTING;
  warningf(0, "timed out waiting for input");
  unwind(2);
 } else
  ksh_tmout_state = TMOUT_EXECUTING;
 if (!flag)
  trap = 0;
 if (flag & (1<<(6)))
  intrsig = 0;
 if (flag & (1<<(9)))
  fatal_trap = 0;
 for (p = sigtraps, i = (64 + 1)+1; --i >= 0; p++)
  if (p->set && (!flag ||
      ((p->flags & flag) && p->trap == ((void *)0))))
   runtrap(p);
}

variable_types: flag int


intrcheck

decompiled_code:
__int64 __fastcall intrcheck(__int64 a1, __int64 a2)
{
__int64 result; // rax

result = intrsig;
if ( intrsig )
result = runtraps(0x240u, a2);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2)
{
__int64 result; // rax

result = intrsig;
if ( intrsig )
result = func4(0x240u, a2);
return result;
}

original_code:
void
intrcheck(void)
{
 if (intrsig)
  runtraps((1<<(6))|(1<<(9)));
}

canonical_original_code:
void intrcheck()
{
 if (intrsig)
  runtraps((1<<(6))|(1<<(9)));
}


setexecsig

decompiled_code:
__int64 __fastcall setexecsig(const char **a1, char a2)
{
__int64 result; // rax
int v3; // esi
unsigned int v4; // edx

result = *((unsigned int *)a1 + 9);
if ( (result & 0xC) != 0 )
{
v3 = a2 & 3;
v4 = result & 0xFFFFFFCF;
if ( v3 == 2 )
{
*((_DWORD *)a1 + 9) = v4 | 0x20;
}
else if ( v3 == 3 )
{
*((_DWORD *)a1 + 9) = v4 | 0x10;
}
else
{
if ( v3 == 1 )
{
result = (*((_DWORD *)a1 + 9) & 4) == 0 ? 32 : 16;
v4 |= result;
}
*((_DWORD *)a1 + 9) = v4;
}
}
else
{
internal_errorf("%s: unset signal %d(%s)", "setexecsig", *(unsigned int *)a1, a1[1]);
result = setsig();
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(const char **a1, char a2)
{
__int64 result; // rax
int v3; // esi
unsigned int v4; // edx

result = *((unsigned int *)a1 + 9);
if ( (result & 0xC) != 0 )
{
v3 = a2 & 3;
v4 = result & 0xFFFFFFCF;
if ( v3 == 2 )
{
*((_DWORD *)a1 + 9) = v4 | 0x20;
}
else if ( v3 == 3 )
{
*((_DWORD *)a1 + 9) = v4 | 0x10;
}
else
{
if ( v3 == 1 )
{
result = (*((_DWORD *)a1 + 9) & 4) == 0 ? 32 : 16;
v4 |= result;
}
*((_DWORD *)a1 + 9) = v4;
}
}
else
{
internal_errorf("%s: unset signal %d(%s)", "setexecsig", *(unsigned int *)a1, a1[1]);
result = func7();
}
return result;
}

original_code:
void
setexecsig(Trap *p, int restore)
{
 if (!(p->flags & ((1<<(2))|(1<<(3)))))
  internal_errorf("%s: unset signal %d(%s)",
      __func__, p->signal, p->name);
 p->flags &= ~((1<<(4))|(1<<(5)));
 switch (restore & 0x3) {
 case 0:
  break;
 case 1:
  p->flags |= p->flags & (1<<(2)) ? (1<<(4)) : (1<<(5));
  break;
 case 2:
  p->flags |= (1<<(5));
  break;
 case 3:
  p->flags |= (1<<(4));
  break;
 }
}

canonical_original_code:
void setexecsig(struct trap *p, int restore)
{
 if (!(p->flags & ((1<<(2))|(1<<(3)))))
  internal_errorf("%s: unset signal %d(%s)",
      __func__, p->signal, p->name);
 p->flags &= ~((1<<(4))|(1<<(5)));
 switch (restore & 0x3) {
 case 0:
  break;
 case 1:
  p->flags |= p->flags & (1<<(2)) ? (1<<(4)) : (1<<(5));
  break;
 case 2:
  p->flags |= (1<<(5));
  break;
 case 3:
  p->flags |= (1<<(4));
  break;
 }
}

variable_types: restore int


setsig

decompiled_code:
__int64 __fastcall setsig(__int64 a1, void (*a2)(int), char a3)
{
int v4; // edi
int v5; // eax
void (*v6)(int); // r12
unsigned int v8; // er13
int v9; // edi
bool v11; // zf
int v12; // eax
struct sigaction v13; // [rsp+0h] [rbp-C8h] BYREF

v4 = *(_DWORD *)a1;
if ( !v4 || v4 == 65 )
return 1;
v5 = *(_DWORD *)(a1 + 36);
v6 = a2;
if ( (v5 & 0xC) != 0 )
{
v8 = a3 & 0x10;
if ( (v5 & 4) == 0 )
goto LABEL_9;
}
else
{
sigaction(v4, &Sigact_ign, &v13);
v11 = v13.sa_handler == (__sighandler_t)((char *)trapsig + 1);
*(_QWORD *)(a1 + 40) = 1LL;
v8 = a3 & 0x10;
v12 = *(_DWORD *)(a1 + 36) | (4 * !v11 + 4);
*(_DWORD *)(a1 + 36) = v12;
if ( (v12 & 4) == 0 )
{
LABEL_9:
setexecsig((const char **)a1, a3 & 3);
if ( !v8 )
*(_QWORD *)(a1 + 48) = 0LL;
goto LABEL_11;
}
}
if ( (a3 & 8) != 0 )
goto LABEL_9;
if ( v8 )
{
v8 = 0;
if ( shell_flags[9] )
{
setexecsig((const char **)a1, a3 & 3);
LABEL_11:
if ( (a3 & 0x20) != 0 )
{
*(_QWORD *)(a1 + 48) = a2;
v6 = (void (*)(int))trapsig;
}
if ( *(void (**)(int))(a1 + 40) != v6 )
{
*(_QWORD *)(a1 + 40) = v6;
sigemptyset(&v13.sa_mask);
v9 = *(_DWORD *)a1;
v13.sa_flags = 0;
v13.sa_handler = v6;
sigaction(v9, &v13, 0LL);
}
return 1;
}
}
return v8;
}

original_code:
int
setsig(Trap *p, sig_t f, int flags)
{
 struct sigaction sigact;
 if (p->signal == 0 || p->signal == (64 + 1))
  return 1;
 if (!(p->flags & ((1<<(2))|(1<<(3))))) {
  sigaction(p->signal, &Sigact_ign, &sigact);
  p->flags |= sigact.__sigaction_handler.sa_handler == ((__sighandler_t) 1) ?
      (1<<(2)) : (1<<(3));
  p->cursig = ((__sighandler_t) 1);
 }
 if ((p->flags & (1<<(2))) && !(flags & (1<<(3))) &&
     (!(flags & (1<<(4))) || !(shell_flags[(int) (FTALKING)])))
  return 0;
 setexecsig(p, flags & 0x3);
 if (!(flags & (1<<(4))))
  p->shtrap = ((void *)0);
 if (flags & (1<<(5))) {
  p->shtrap = f;
  f = trapsig;
 }
 if (p->cursig != f) {
  p->cursig = f;
  sigemptyset(&sigact.sa_mask);
  sigact.sa_flags = 0 ;
  sigact.__sigaction_handler.sa_handler = f;
  sigaction(p->signal, &sigact, ((void *)0));
 }
 return 1;
}

canonical_original_code:
int setsig(struct trap *p, void (*f)(int), int flags)
{
 struct sigaction sigact;
 if (p->signal == 0 || p->signal == (64 + 1))
  return 1;
 if (!(p->flags & ((1<<(2))|(1<<(3))))) {
  sigaction(p->signal, &Sigact_ign, &sigact);
  p->flags |= sigact.__sigaction_handler.sa_handler == ((void (*)(int)) 1) ?
      (1<<(2)) : (1<<(3));
  p->cursig = ((void (*)(int)) 1);
 }
 if ((p->flags & (1<<(2))) && !(flags & (1<<(3))) &&
     (!(flags & (1<<(4))) || !(shell_flags[(int) (FTALKING)])))
  return 0;
 setexecsig(p, flags & 0x3);
 if (!(flags & (1<<(4))))
  p->shtrap = ((void *)0);
 if (flags & (1<<(5))) {
  p->shtrap = f;
  f = trapsig;
 }
 if (p->cursig != f) {
  p->cursig = f;
  sigemptyset(&sigact.sa_mask);
  sigact.sa_flags = 0 ;
  sigact.__sigaction_handler.sa_handler = f;
  sigaction(p->signal, &sigact, ((void *)0));
 }
 return 1;
}

variable_types:
f
void (*)(int)

variable_types: flags int

variable_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval { int sival_int; void *sival_ptr; } si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; } *, void *); } __sigaction_handler; struct __sigset_t { unsigned long __val[-1]; } sa_mask; int sa_flags; void (*sa_restorer)(); }

user_defined_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t *, void *); } __sigaction_handler; struct __sigset_t sa_mask; int sa_flags; void (*sa_restorer)(); }

user_defined_types: struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; }


inittraps

decompiled_code:
__int64 inittraps()
{
int v0; // edx
__int64 v1; // rbp
int v2; // ebx
int v3; // edi

v0 = 0;
v1 = 1LL;
v2 = 1;
LODWORD(sigtraps[7]) = 1;
while ( 1 )
{
while ( LODWORD((&sys_signame)[2 * v0 + 1]) != v2 )
{
if ( ++v0 == 65 )
goto LABEL_5;
}
sigtraps[7 * v1 + 1] = (__int64)(&sys_signame)[2 * v0];
LABEL_5:
v3 = v2++;
sigtraps[7 * v1 + 2] = (__int64)strsignal(v3);
if ( v2 == 66 )
goto LABEL_9;
v1 = v2;
LODWORD(sigtraps[7 * v2]) = v2;
if ( v2 == 65 )
break;
v0 = 0;
}
sigtraps[456] = (__int64)"ERR";
sigtraps[457] = (__int64)"Error handler";
LABEL_9:
LODWORD(sigtraps[0]) = 0;
sigtraps[1] = (__int64)"EXIT";
sigtraps[2] = (__int64)"Exited";
sigemptyset(&set);
HIDWORD(sigtraps[18]) |= 0xC0u;
HIDWORD(sigtraps[25]) |= 0xC0u;
HIDWORD(sigtraps[109]) |= 0x40u;
HIDWORD(sigtraps[11]) |= 0x200u;
HIDWORD(sigtraps[123]) |= 1u;
dword_1D08 = 0;
Sigact_ign.sa_handler = (__sighandler_t)(trapsig + 1);
setsig((unsigned int)((_DWORD)sigtraps + 112), (void (*)(int))trapsig, 1);
setsig((unsigned int)((_DWORD)sigtraps + 168), (void (*)(int))trapsig, 1);
setsig((unsigned int)((_DWORD)sigtraps + 840), (void (*)(int))trapsig, 1);
return setsig((unsigned int)((_DWORD)sigtraps + 56), (void (*)(int))trapsig, 1);
}

canonical_decompiled_code:
__int64 func8()
{
int v0; // edx
__int64 v1; // rbp
int v2; // ebx
int v3; // edi

v0 = 0;
v1 = 1LL;
v2 = 1;
LODWORD(sigtraps[7]) = 1;
while ( 1 )
{
while ( LODWORD((&sys_signame)[2 * v0 + 1]) != v2 )
{
if ( ++v0 == 65 )
goto LABEL_5;
}
sigtraps[7 * v1 + 1] = (__int64)(&sys_signame)[2 * v0];
LABEL_5:
v3 = v2++;
sigtraps[7 * v1 + 2] = (__int64)strsignal(v3);
if ( v2 == 66 )
goto LABEL_9;
v1 = v2;
LODWORD(sigtraps[7 * v2]) = v2;
if ( v2 == 65 )
break;
v0 = 0;
}
sigtraps[456] = (__int64)"ERR";
sigtraps[457] = (__int64)"Error handler";
LABEL_9:
LODWORD(sigtraps[0]) = 0;
sigtraps[1] = (__int64)"EXIT";
sigtraps[2] = (__int64)"Exited";
sigemptyset(&set);
HIDWORD(sigtraps[18]) |= 0xC0u;
HIDWORD(sigtraps[25]) |= 0xC0u;
HIDWORD(sigtraps[109]) |= 0x40u;
HIDWORD(sigtraps[11]) |= 0x200u;
HIDWORD(sigtraps[123]) |= 1u;
dword_1D08 = 0;
Sigact_ign.sa_handler = (__sighandler_t)(trapsig + 1);
func7((unsigned int)((_DWORD)sigtraps + 112), (void (*)(int))trapsig, 1);
func7((unsigned int)((_DWORD)sigtraps + 168), (void (*)(int))trapsig, 1);
func7((unsigned int)((_DWORD)sigtraps + 840), (void (*)(int))trapsig, 1);
return func7((unsigned int)((_DWORD)sigtraps + 56), (void (*)(int))trapsig, 1);
}

original_code:
void
inittraps(void)
{
 int i, j;
 for (i = 1; i <= (64 + 1); i++) {
  sigtraps[i].signal = i;
  if (i == (64 + 1)) {
   sigtraps[i].name = "ERR";
   sigtraps[i].mess = "Error handler";
  } else {
   for(j = 0; j < (64 + 1); j++)
   {
    if( sys_signame[j].val == sigtraps[i].signal )
    {
     sigtraps[i].name = sys_signame[j].name;
     break;
    }
   }
   sigtraps[i].mess = strsignal(sigtraps[i].signal);
  }
 }
 sigtraps[0].signal = 0;
 sigtraps[0].name = "EXIT";
 sigtraps[0].mess = "Exited";
 sigemptyset(&Sigact_ign.sa_mask);
 Sigact_ign.sa_flags = 0;
 Sigact_ign.__sigaction_handler.sa_handler = ((__sighandler_t) 1);
 Sigact_trap = Sigact_ign;
 Sigact_trap.__sigaction_handler.sa_handler = trapsig;
 sigtraps[2].flags |= (1<<(6)) | (1<<(7));
 sigtraps[3].flags |= (1<<(6)) | (1<<(7));
 sigtraps[15].flags |= (1<<(6));
 sigtraps[1].flags |= (1<<(9));
 sigtraps[17].flags |= (1<<(0));
 setsig(&sigtraps[2], trapsig, 1);
 setsig(&sigtraps[3], trapsig, 1);
 setsig(&sigtraps[15], trapsig, 1);
 setsig(&sigtraps[1], trapsig, 1);
}

canonical_original_code:
void inittraps()
{
 int i; int j;
 for (i = 1; i <= (64 + 1); i++) {
  sigtraps[i].signal = i;
  if (i == (64 + 1)) {
   sigtraps[i].name = "ERR";
   sigtraps[i].mess = "Error handler";
  } else {
   for(j = 0; j < (64 + 1); j++)
   {
    if( sys_signame[j].val == sigtraps[i].signal )
    {
     sigtraps[i].name = sys_signame[j].name;
     break;
    }
   }
   sigtraps[i].mess = strsignal(sigtraps[i].signal);
  }
 }
 sigtraps[0].signal = 0;
 sigtraps[0].name = "EXIT";
 sigtraps[0].mess = "Exited";
 sigemptyset(&Sigact_ign.sa_mask);
 Sigact_ign.sa_flags = 0;
 Sigact_ign.__sigaction_handler.sa_handler = ((void (*)(int)) 1);
 Sigact_trap = Sigact_ign;
 Sigact_trap.__sigaction_handler.sa_handler = trapsig;
 sigtraps[2].flags |= (1<<(6)) | (1<<(7));
 sigtraps[3].flags |= (1<<(6)) | (1<<(7));
 sigtraps[15].flags |= (1<<(6));
 sigtraps[1].flags |= (1<<(9));
 sigtraps[17].flags |= (1<<(0));
 setsig(&sigtraps[2], trapsig, 1);
 setsig(&sigtraps[3], trapsig, 1);
 setsig(&sigtraps[15], trapsig, 1);
 setsig(&sigtraps[1], trapsig, 1);
}

variable_types: j int


decompiled_code:
__int64 alarm_init()
{
HIDWORD(sigtraps[102]) |= 1u;
return setsig((unsigned int)((_DWORD)sigtraps + 784), (void (*)(int))alarm_catcher, 41);
}

canonical_decompiled_code:
__int64 func9()
{
HIDWORD(sigtraps[102]) |= 1u;
return func7((unsigned int)((_DWORD)sigtraps + 784), (void (*)(int))alarm_catcher, 41);
}

original_code:
void
alarm_init(void)
{
 sigtraps[14].flags |= (1<<(0));
 setsig(&sigtraps[14], alarm_catcher,
  1|(1<<(3))|(1<<(5)));
}

canonical_original_code:
void alarm_init()
{
 sigtraps[14].flags |= (1<<(0));
 setsig(&sigtraps[14], alarm_catcher,
  1|(1<<(3))|(1<<(5)));
}


restoresigs

decompiled_code:
__int64 restoresigs()
{
__int64 *v0; // rbx
int v1; // esi
__int64 v2; // rdi
__int64 result; // rax

v0 = sigtraps;
do
{
while ( 1 )
{
v1 = *((_DWORD *)v0 + 9);
if ( (v1 & 0x30) != 0 )
break;
v0 += 7;
if ( v0 == &sigtraps[462] )
return result;
}
v2 = (__int64)v0;
v0 += 7;
result = setsig(v2, (void (*)(int))((v1 >> 4) & 1), 8);
}
while ( v0 != &sigtraps[462] );
return result;
}

canonical_decompiled_code:
__int64 func10()
{
__int64 *v0; // rbx
int v1; // esi
__int64 v2; // rdi
__int64 result; // rax

v0 = sigtraps;
do
{
while ( 1 )
{
v1 = *((_DWORD *)v0 + 9);
if ( (v1 & 0x30) != 0 )
break;
v0 += 7;
if ( v0 == &sigtraps[462] )
return result;
}
v2 = (__int64)v0;
v0 += 7;
result = func7(v2, (void (*)(int))((v1 >> 4) & 1), 8);
}
while ( v0 != &sigtraps[462] );
return result;
}


settrap

decompiled_code:
__int64 __fastcall settrap(__int64 a1, _BYTE *a2)
{
__int64 result; // rax
int v3; // edx
int v4; // edx
bool v5; // zf
void (*v6)(int); // rsi
int v7; // edx
unsigned int v8; // edx

afree(*(_QWORD *)(a1 + 24), &aperm);
*(_QWORD *)(a1 + 24) = str_save(a2, &aperm);
result = *(unsigned int *)(a1 + 36);
v3 = *(_DWORD *)(a1 + 36);
BYTE1(v3) |= 1u;
*(_DWORD *)(a1 + 36) = v3;
v4 = result | 0x102;
if ( a2 )
{
v5 = *a2 == 0;
v6 = (void (*)(int))(trapsig + 1);
*(_DWORD *)(a1 + 36) = v4;
if ( !v5 )
{
v6 = (void (*)(int))trapsig;
if ( (result & 1) == 0 )
return setsig(a1, v6, 16);
goto LABEL_6;
}
}
else
{
*(_DWORD *)(a1 + 36) = v4;
v6 = 0LL;
if ( (result & 0x240) != 0 )
{
v6 = (void (*)(int))trapsig;
return setsig(a1, v6, 16);
}
}
if ( (result & 1) == 0 )
return setsig(a1, v6, 16);
LABEL_6:
if ( (result & 4) == 0 || shell_flags[9] )
{
v7 = result & 0xFFFFFFCF;
result = result & 0xFFFFFFCF | 0x112;
v8 = v7 | 0x122;
if ( v6 != (void (*)(int))((char *)trapsig + 1) )
result = v8;
*(_DWORD *)(a1 + 36) = result;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, _BYTE *a2)
{
__int64 result; // rax
int v3; // edx
int v4; // edx
bool v5; // zf
void (*v6)(int); // rsi
int v7; // edx
unsigned int v8; // edx

afree(*(_QWORD *)(a1 + 24), &aperm);
*(_QWORD *)(a1 + 24) = str_save(a2, &aperm);
result = *(unsigned int *)(a1 + 36);
v3 = *(_DWORD *)(a1 + 36);
BYTE1(v3) |= 1u;
*(_DWORD *)(a1 + 36) = v3;
v4 = result | 0x102;
if ( a2 )
{
v5 = *a2 == 0;
v6 = (void (*)(int))(trapsig + 1);
*(_DWORD *)(a1 + 36) = v4;
if ( !v5 )
{
v6 = (void (*)(int))trapsig;
if ( (result & 1) == 0 )
return func7(a1, v6, 16);
goto LABEL_6;
}
}
else
{
*(_DWORD *)(a1 + 36) = v4;
v6 = 0LL;
if ( (result & 0x240) != 0 )
{
v6 = (void (*)(int))trapsig;
return func7(a1, v6, 16);
}
}
if ( (result & 1) == 0 )
return func7(a1, v6, 16);
LABEL_6:
if ( (result & 4) == 0 || shell_flags[9] )
{
v7 = result & 0xFFFFFFCF;
result = result & 0xFFFFFFCF | 0x112;
v8 = v7 | 0x122;
if ( v6 != (void (*)(int))((char *)trapsig + 1) )
result = v8;
*(_DWORD *)(a1 + 36) = result;
}
return result;
}

variable_types:
s
char *


cleartraps

original_code:
void
cleartraps(void)
{
 int i;
 Trap *p;
 trap = 0;
 intrsig = 0;
 fatal_trap = 0;
 for (i = (64 + 1)+1, p = sigtraps; --i >= 0; p++) {
  p->set = 0;
  if ((p->flags & (1<<(1))) && (p->trap && p->trap[0]))
   settrap(p, ((void *)0));
 }
}

canonical_original_code:
void cleartraps()
{
 int i;
 struct trap *p;
 trap = 0;
 intrsig = 0;
 fatal_trap = 0;
 for (i = (64 + 1)+1, p = sigtraps; --i >= 0; p++) {
  p->set = 0;
  if ((p->flags & (1<<(1))) && (p->trap && p->trap[0]))
   settrap(p, ((void *)0));
 }
}


decompiled_code:
__int64 block_pipe()
{
__int64 result; // rax

if ( (sigtraps[95] & 0xC00000000LL) != 0 )
{
result = 0LL;
if ( !sigtraps[96] )
{
setsig((unsigned int)((_DWORD)sigtraps + 728), (void (*)(int))((char *)trapsig + 1), 0);
result = 1LL;
}
}
else
{
setsig((unsigned int)((_DWORD)sigtraps + 728), (void (*)(int))((char *)trapsig + 1), 0);
result = (HIDWORD(sigtraps[95]) >> 3) & 1;
}
return result;
}

canonical_decompiled_code:
__int64 func13()
{
__int64 result; // rax

if ( (sigtraps[95] & 0xC00000000LL) != 0 )
{
result = 0LL;
if ( !sigtraps[96] )
{
func7((unsigned int)((_DWORD)sigtraps + 728), (void (*)(int))((char *)trapsig + 1), 0);
result = 1LL;
}
}
else
{
func7((unsigned int)((_DWORD)sigtraps + 728), (void (*)(int))((char *)trapsig + 1), 0);
result = (HIDWORD(sigtraps[95]) >> 3) & 1;
}
return result;
}

original_code:
int
block_pipe(void)
{
 int restore_dfl = 0;
 Trap *p = &sigtraps[13];
 if (!(p->flags & ((1<<(2))|(1<<(3))))) {
  setsig(p, ((__sighandler_t) 1), 0);
  if (p->flags & (1<<(3)))
   restore_dfl = 1;
 } else if (p->cursig == ((__sighandler_t) 0)) {
  setsig(p, ((__sighandler_t) 1), 0);
  restore_dfl = 1;
 }
 return restore_dfl;
}

canonical_original_code:
int block_pipe()
{
 int restore_dfl = 0;
 struct trap *p = &sigtraps[13];
 if (!(p->flags & ((1<<(2))|(1<<(3))))) {
  setsig(p, ((void (*)(int)) 1), 0);
  if (p->flags & (1<<(3)))
   restore_dfl = 1;
 } else if (p->cursig == ((void (*)(int)) 0)) {
  setsig(p, ((void (*)(int)) 1), 0);
  restore_dfl = 1;
 }
 return restore_dfl;
}


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

if ( a1 )
result = setsig((unsigned int)((_DWORD)sigtraps + 728), 0LL, 0);
return result;
}

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

if ( a1 )
result = func7((unsigned int)((_DWORD)sigtraps + 728), 0LL, 0);
return result;
}

original_code:
void
restore_pipe(int restore_dfl)
{
 if (restore_dfl)
  setsig(&sigtraps[13], ((__sighandler_t) 0), 0);
}

canonical_original_code:
void restore_pipe(int restore_dfl)
{
 if (restore_dfl)
  setsig(&sigtraps[13], ((void (*)(int)) 0), 0);
}


decompiled_code:
void *__fastcall ptest_getopnd(__int64 a1, int a2)
{
void **v2; // rax
void **v3; // rdx
void *result; // rax

v2 = *(void ***)(a1 + 8);
if ( (unsigned __int64)v2 >= *(_QWORD *)(a1 + 16) )
{
result = &unk_E10;
if ( a2 != 20 )
result = 0LL;
}
else
{
v3 = v2 + 1;
result = *v2;
*(_QWORD *)(a1 + 8) = v3;
}
return result;
}

canonical_decompiled_code:
void *__fastcall func0(__int64 a1, int a2)
{
void **v2; // rax
void **v3; // rdx
void *result; // rax

v2 = *(void ***)(a1 + 8);
if ( (unsigned __int64)v2 >= *(_QWORD *)(a1 + 16) )
{
result = &unk_E10;
if ( a2 != 20 )
result = 0LL;
}
else
{
v3 = v2 + 1;
result = *v2;
*(_QWORD *)(a1 + 8) = v3;
}
return result;
}

original_code:
static const char *
ptest_getopnd(Test_env *te, Test_op op, int do_eval)
{
 if (te->pos.wp >= te->wp_end)
  return op == TO_FILTT ? "1" : ((void *)0);
 return *te->pos.wp++;
}

canonical_original_code:
static char *ptest_getopnd(struct test_env *te, enum Test_op op, int do_eval)
{
 if (te->pos.wp >= te->wp_end)
  return op == TO_FILTT ? "1" : ((void *)0);
 return *te->pos.wp++;
}

variable_types: struct test_env { int flags; union { char **wp; struct XPtrV { void **cur; void **beg; } *av; } pos; char **wp_end; int (*isa)(struct test_env *, enum Test_meta { TM_OR=0, TM_AND=1, TM_NOT=2, TM_OPAREN=3, TM_CPAREN=4, TM_UNOP=5, TM_BINOP=6, TM_END=7 }); char *(*getopnd)(struct test_env *, enum Test_op { TO_NONOP=0, TO_STNZE=1, TO_STZER=2, TO_OPTION=3, TO_FILAXST=4, TO_FILEXST=5, TO_FILREG=6, TO_FILBDEV=7, TO_FILCDEV=8, TO_FILSYM=9, TO_FILFIFO=10, TO_FILSOCK=11, TO_FILCDF=12, TO_FILID=13, TO_FILGID=14, TO_FILSETG=15, TO_FILSTCK=16, TO_FILUID=17, TO_FILRD=18, TO_FILGZ=19, TO_FILTT=20, TO_FILSETU=21, TO_FILWR=22, TO_FILEX=23, TO_STEQL=24, TO_STNEQ=25, TO_STLT=26, TO_STGT=27, TO_INTEQ=28, TO_INTNE=29, TO_INTGT=30, TO_INTGE=31, TO_INTLT=32, TO_INTLE=33, TO_FILEQ=34, TO_FILNT=35, TO_FILOT=36 }, int); int (*eval)(struct test_env *, enum Test_op { TO_NONOP=0, TO_STNZE=1, TO_STZER=2, TO_OPTION=3, TO_FILAXST=4, TO_FILEXST=5, TO_FILREG=6, TO_FILBDEV=7, TO_FILCDEV=8, TO_FILSYM=9, TO_FILFIFO=10, TO_FILSOCK=11, TO_FILCDF=12, TO_FILID=13, TO_FILGID=14, TO_FILSETG=15, TO_FILSTCK=16, TO_FILUID=17, TO_FILRD=18, TO_FILGZ=19, TO_FILTT=20, TO_FILSETU=21, TO_FILWR=22, TO_FILEX=23, TO_STEQL=24, TO_STNEQ=25, TO_STLT=26, TO_STGT=27, TO_INTEQ=28, TO_INTNE=29, TO_INTGT=30, TO_INTGE=31, TO_INTLT=32, TO_INTLE=33, TO_FILEQ=34, TO_FILNT=35, TO_FILOT=36 }, char *, char *, int); void (*error)(struct test_env *, int, char *); } *

return_type: char *

user_defined_types: struct test_env { int flags; union { char **wp; struct XPtrV *av; } pos; char **wp_end; int (*isa)(struct test_env *, enum Test_meta); char *(*getopnd)(struct test_env *, enum Test_op, int); int (*eval)(struct test_env *, enum Test_op, char *, char *, int); void (*error)(struct test_env *, int, char *); }


decompiled_code:
__int64 __fastcall ptest_error(__int64 a1, int a2, const char *a3)
{
const char **v3; // rcx
int v4; // eax
const char *v6; // rsi

v3 = (const char **)(*(_QWORD *)(a1 + 8) + 8LL * a2);
v4 = *(_DWORD *)a1 | 1;
if ( (unsigned __int64)v3 >= *(_QWORD *)(a1 + 16) )
{
*(_DWORD *)a1 = v4;
return bi_errorf("%s", a3);
}
v6 = *v3;
*(_DWORD *)a1 = v4;
if ( !v6 )
return bi_errorf("%s", a3);
return bi_errorf("%s: %s", v6, a3);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, int a2, const char *a3)
{
const char **v3; // rcx
int v4; // eax
const char *v6; // rsi

v3 = (const char **)(*(_QWORD *)(a1 + 8) + 8LL * a2);
v4 = *(_DWORD *)a1 | 1;
if ( (unsigned __int64)v3 >= *(_QWORD *)(a1 + 16) )
{
*(_DWORD *)a1 = v4;
return bi_errorf("%s", a3);
}
v6 = *v3;
*(_DWORD *)a1 = v4;
if ( !v6 )
return bi_errorf("%s", a3);
return bi_errorf("%s: %s", v6, a3);
}

original_code:
static void
ptest_error(Test_env *te, int offset, const char *msg)
{
 const char *op = te->pos.wp + offset >= te->wp_end ?
     ((void *)0) : te->pos.wp[offset];
 te->flags |= (1<<(0));
 if (op)
  bi_errorf("%s: %s", op, msg);
 else
  bi_errorf("%s", msg);
}

canonical_original_code:
static void ptest_error(struct test_env *te, int offset, char *msg)
{
 char *op = te->pos.wp + offset >= te->wp_end ?
     ((void *)0) : te->pos.wp[offset];
 te->flags |= (1<<(0));
 if (op)
  bi_errorf("%s: %s", op, msg);
 else
  bi_errorf("%s", msg);
}

variable_types: offset int

variable_types:
msg
char *

variable_types:
op
char *

user_defined_types: struct XPtrV { void **cur; void **beg; }

user_defined_types: enum Test_meta { TM_OR=0, TM_AND=1, TM_NOT=2, TM_OPAREN=3, TM_CPAREN=4, TM_UNOP=5, TM_BINOP=6, TM_END=7 }

user_defined_types: enum Test_op { TO_NONOP=0, TO_STNZE=1, TO_STZER=2, TO_OPTION=3, TO_FILAXST=4, TO_FILEXST=5, TO_FILREG=6, TO_FILBDEV=7, TO_FILCDEV=8, TO_FILSYM=9, TO_FILFIFO=10, TO_FILSOCK=11, TO_FILCDF=12, TO_FILID=13, TO_FILGID=14, TO_FILSETG=15, TO_FILSTCK=16, TO_FILUID=17, TO_FILRD=18, TO_FILGZ=19, TO_FILTT=20, TO_FILSETU=21, TO_FILWR=22, TO_FILEX=23, TO_STEQL=24, TO_STNEQ=25, TO_STLT=26, TO_STGT=27, TO_INTEQ=28, TO_INTNE=29, TO_INTGT=30, TO_INTGE=31, TO_INTLT=32, TO_INTLE=33, TO_FILEQ=34, TO_FILNT=35, TO_FILOT=36 }


decompiled_code:
__int64 __fastcall test_eaccess(char *file, char a2)
{
unsigned int v2; // er12
struct stat v4; // [rsp+0h] [rbp-A8h] BYREF

v2 = access(file, a2);
if ( !v2 && !ksheuid && (a2 & 1) != 0 )
{
if ( stat(file, &v4) == -1 )
{
v2 = -1;
}
else if ( (v4.st_mode & 0xF000) != 0x4000 )
{
v2 = -((v4.st_mode & 0x49) == 0);
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func2(char *file, char a2)
{
unsigned int v2; // er12
struct stat v4; // [rsp+0h] [rbp-A8h] BYREF

v2 = access(file, a2);
if ( !v2 && !ksheuid && (a2 & 1) != 0 )
{
if ( stat(file, &v4) == -1 )
{
v2 = -1;
}
else if ( (v4.st_mode & 0xF000) != 0x4000 )
{
v2 = -((v4.st_mode & 0x49) == 0);
}
}
return v2;
}

original_code:
static int
test_eaccess(const char *path, int amode)
{
 int res;
 res = access(path, amode);
 if (res == 0 && ksheuid == 0 && (amode & 1)) {
  struct stat statb;
  if (stat(path, &statb) == -1)
   res = -1;
  else if (((((statb.st_mode)) & 0170000) == (0040000)))
   res = 0;
  else
   res = (statb.st_mode & (0100|(0100 >> 3)|((0100 >> 3) >> 3))) ?
       0 : -1;
 }
 return res;
}

canonical_original_code:
static int test_eaccess(char *path, int amode)
{
 int res;
 res = access(path, amode);
 if (res == 0 && ksheuid == 0 && (amode & 1)) {
  struct stat statb;
  if (stat(path, &statb) == -1)
   res = -1;
  else if (((((statb.st_mode)) & 0170000) == (0040000)))
   res = 0;
  else
   res = (statb.st_mode & (0100|(0100 >> 3)|((0100 >> 3) >> 3))) ?
       0 : -1;
 }
 return res;
}

variable_types:
path
char *

variable_types: amode int

variable_types: res int

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:
__int64 __fastcall test_isop(__int64 a1, int a2, __int64 a3)
{
const char *v3; // rbx
char v5; // bp

v3 = (const char *)&u_ops;
if ( a2 != 5 )
v3 = "=";
if ( !*(_BYTE *)a3 )
return 0LL;
v5 = *(_BYTE *)(a3 + 1);
while ( v3[1] != v5 || strcmp((const char *)a3, v3) )
{
v3 += 8;
if ( !*v3 )
return 0LL;
}
return *((unsigned int *)v3 + 1);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, int a2, __int64 a3)
{
const char *v3; // rbx
char v5; // bp

v3 = (const char *)&u_ops;
if ( a2 != 5 )
v3 = "=";
if ( !*(_BYTE *)a3 )
return 0LL;
v5 = *(_BYTE *)(a3 + 1);
while ( v3[1] != v5 || strcmp((const char *)a3, v3) )
{
v3 += 8;
if ( !*v3 )
return 0LL;
}
return *((unsigned int *)v3 + 1);
}

original_code:
Test_op
test_isop(Test_env *te, Test_meta meta, const char *s)
{
 char sc1;
 const struct t_op *otab;
 otab = meta == TM_UNOP ? u_ops : b_ops;
 if (*s) {
  sc1 = s[1];
  for (; otab->op_text[0]; otab++)
   if (sc1 == otab->op_text[1] &&
       strcmp(s, otab->op_text) == 0)
    return otab->op_num;
 }
 return TO_NONOP;
}

canonical_original_code:
enum Test_op test_isop(struct test_env *te, enum Test_meta meta, char *s)
{
 char sc1;
 struct t_op *otab;
 otab = meta == TM_UNOP ? u_ops : b_ops;
 if (*s) {
  sc1 = s[1];
  for (; otab->op_text[0]; otab++)
   if (sc1 == otab->op_text[1] &&
       strcmp(s, otab->op_text) == 0)
    return otab->op_num;
 }
 return TO_NONOP;
}

variable_types: enum Test_meta { TM_OR=0, TM_AND=1, TM_NOT=2, TM_OPAREN=3, TM_CPAREN=4, TM_UNOP=5, TM_BINOP=6, TM_END=7 }

variable_types: struct t_op { char op_text[4]; enum Test_op { TO_NONOP=0, TO_STNZE=1, TO_STZER=2, TO_OPTION=3, TO_FILAXST=4, TO_FILEXST=5, TO_FILREG=6, TO_FILBDEV=7, TO_FILCDEV=8, TO_FILSYM=9, TO_FILFIFO=10, TO_FILSOCK=11, TO_FILCDF=12, TO_FILID=13, TO_FILGID=14, TO_FILSETG=15, TO_FILSTCK=16, TO_FILUID=17, TO_FILRD=18, TO_FILGZ=19, TO_FILTT=20, TO_FILSETU=21, TO_FILWR=22, TO_FILEX=23, TO_STEQL=24, TO_STNEQ=25, TO_STLT=26, TO_STGT=27, TO_INTEQ=28, TO_INTNE=29, TO_INTGT=30, TO_INTGE=31, TO_INTLT=32, TO_INTLE=33, TO_FILEQ=34, TO_FILNT=35, TO_FILOT=36 } op_num; } *

user_defined_types: struct t_op { char op_text[4]; enum Test_op op_num; }


original_code:
static int
ptest_isa(Test_env *te, Test_meta meta)
{
 static const char *const tokens[] = {
  "-o", "-a", "!", "(", ")"
 };
 int ret;
 if (te->pos.wp >= te->wp_end)
  return meta == TM_END;
 if (meta == TM_UNOP || meta == TM_BINOP)
  ret = (int) test_isop(te, meta, *te->pos.wp);
 else if (meta == TM_END)
  ret = 0;
 else
  ret = strcmp(*te->pos.wp, tokens[(int) meta]) == 0;
 if (ret)
  te->pos.wp++;
 return ret;
}

canonical_original_code:
static int ptest_isa(struct test_env *te, enum Test_meta meta)
{
 static char *tokens[] = {
  "-o", "-a", "!", "(", ")"
 };
 int ret;
 if (te->pos.wp >= te->wp_end)
  return meta == TM_END;
 if (meta == TM_UNOP || meta == TM_BINOP)
  ret = (int) test_isop(te, meta, *te->pos.wp);
 else if (meta == TM_END)
  ret = 0;
 else
  ret = strcmp(*te->pos.wp, tokens[(int) meta]) == 0;
 if (ret)
  te->pos.wp++;
 return ret;
}

variable_types:
tokens
char *[]

variable_types: ret int


original_code:
static int
test_primary(Test_env *te, int do_eval)
{
 const char *opnd1, *opnd2;
 int res;
 Test_op op;
 if (te->flags & (1<<(0)))
  return 0;
 if ((*te->isa)(te, TM_OPAREN)) {
  res = test_oexpr(te, do_eval);
  if (te->flags & (1<<(0)))
   return 0;
  if (!(*te->isa)(te, TM_CPAREN)) {
   (*te->error)(te, 0, "missing closing paren");
   return 0;
  }
  return res;
 }
 if ((te->flags & (1<<(1))) || (&te->pos.wp[1] < te->wp_end &&
     !test_isop(te, TM_BINOP, te->pos.wp[1]))) {
  if ((op = (Test_op) (*te->isa)(te, TM_UNOP))) {
   opnd1 = (*te->getopnd)(te, op, do_eval);
   if (!opnd1) {
    (*te->error)(te, -1, "missing argument");
    return 0;
   }
   return (*te->eval)(te, op, opnd1, ((void *)0),
       do_eval);
  }
 }
 opnd1 = (*te->getopnd)(te, TO_NONOP, do_eval);
 if (!opnd1) {
  (*te->error)(te, 0, "expression expected");
  return 0;
 }
 if ((op = (Test_op) (*te->isa)(te, TM_BINOP))) {
  opnd2 = (*te->getopnd)(te, op, do_eval);
  if (!opnd2) {
   (*te->error)(te, -1, "missing second argument");
   return 0;
  }
  return (*te->eval)(te, op, opnd1, opnd2, do_eval);
 }
 if (te->flags & (1<<(1))) {
  (*te->error)(te, -1, "missing expression operator");
  return 0;
 }
 return (*te->eval)(te, TO_STNZE, opnd1, ((void *)0), do_eval);
}

canonical_original_code:
static int test_primary(struct test_env *te, int do_eval)
{
 char *opnd1; char *opnd2;
 int res;
 enum Test_op op;
 if (te->flags & (1<<(0)))
  return 0;
 if ((*te->isa)(te, TM_OPAREN)) {
  res = test_oexpr(te, do_eval);
  if (te->flags & (1<<(0)))
   return 0;
  if (!(*te->isa)(te, TM_CPAREN)) {
   (*te->error)(te, 0, "missing closing paren");
   return 0;
  }
  return res;
 }
 if ((te->flags & (1<<(1))) || (&te->pos.wp[1] < te->wp_end &&
     !test_isop(te, TM_BINOP, te->pos.wp[1]))) {
  if ((op = (Test_op) (*te->isa)(te, TM_UNOP))) {
   opnd1 = (*te->getopnd)(te, op, do_eval);
   if (!opnd1) {
    (*te->error)(te, -1, "missing argument");
    return 0;
   }
   return (*te->eval)(te, op, opnd1, ((void *)0),
       do_eval);
  }
 }
 opnd1 = (*te->getopnd)(te, TO_NONOP, do_eval);
 if (!opnd1) {
  (*te->error)(te, 0, "expression expected");
  return 0;
 }
 if ((op = (Test_op) (*te->isa)(te, TM_BINOP))) {
  opnd2 = (*te->getopnd)(te, op, do_eval);
  if (!opnd2) {
   (*te->error)(te, -1, "missing second argument");
   return 0;
  }
  return (*te->eval)(te, op, opnd1, opnd2, do_eval);
 }
 if (te->flags & (1<<(1))) {
  (*te->error)(te, -1, "missing expression operator");
  return 0;
 }
 return (*te->eval)(te, TO_STNZE, opnd1, ((void *)0), do_eval);
}

variable_types: enum Test_op { TO_NONOP=0, TO_STNZE=1, TO_STZER=2, TO_OPTION=3, TO_FILAXST=4, TO_FILEXST=5, TO_FILREG=6, TO_FILBDEV=7, TO_FILCDEV=8, TO_FILSYM=9, TO_FILFIFO=10, TO_FILSOCK=11, TO_FILCDF=12, TO_FILID=13, TO_FILGID=14, TO_FILSETG=15, TO_FILSTCK=16, TO_FILUID=17, TO_FILRD=18, TO_FILGZ=19, TO_FILTT=20, TO_FILSETU=21, TO_FILWR=22, TO_FILEX=23, TO_STEQL=24, TO_STNEQ=25, TO_STLT=26, TO_STGT=27, TO_INTEQ=28, TO_INTNE=29, TO_INTGT=30, TO_INTGE=31, TO_INTLT=32, TO_INTLE=33, TO_FILEQ=34, TO_FILNT=35, TO_FILOT=36 }


decompiled_code:
__int64 __fastcall test_nexpr(__int64 a1, unsigned int a2)
{
__int64 result; // rax

if ( (*(_BYTE *)a1 & 1) == 0 && (*(unsigned int (__fastcall **)(__int64, __int64))(a1 + 24))(a1, 2LL) )
result = (unsigned int)test_nexpr(a1, a2) == 0;
else
result = test_primary(a1, a2);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, unsigned int a2)
{
__int64 result; // rax

if ( (*(_BYTE *)a1 & 1) == 0 && (*(unsigned int (__fastcall **)(__int64, __int64))(a1 + 24))(a1, 2LL) )
result = (unsigned int)func6(a1, a2) == 0;
else
result = func5(a1, a2);
return result;
}

original_code:
static int
test_nexpr(Test_env *te, int do_eval)
{
 if (!(te->flags & (1<<(0))) && (*te->isa)(te, TM_NOT))
  return !test_nexpr(te, do_eval);
 return test_primary(te, do_eval);
}

canonical_original_code:
static int test_nexpr(struct test_env *te, int do_eval)
{
 if (!(te->flags & (1<<(0))) && (*te->isa)(te, TM_NOT))
  return !test_nexpr(te, do_eval);
 return test_primary(te, do_eval);
}


decompiled_code:
__int64 __fastcall test_aexpr(__int64 a1, unsigned int a2)
{
unsigned int v2; // er12
unsigned int v3; // er13
__int64 result; // rax

v2 = a2;
v3 = test_nexpr(a1, a2);
if ( !v3 )
v2 = 0;
if ( (*(_BYTE *)a1 & 1) == 0 && (*(unsigned int (__fastcall **)(__int64, __int64))(a1 + 24))(a1, 1LL) )
result = (v3 != 0) & (unsigned __int8)((unsigned int)test_aexpr(a1, v2) != 0);
else
result = v3;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, unsigned int a2)
{
unsigned int v2; // er12
unsigned int v3; // er13
__int64 result; // rax

v2 = a2;
v3 = func6(a1, a2);
if ( !v3 )
v2 = 0;
if ( (*(_BYTE *)a1 & 1) == 0 && (*(unsigned int (__fastcall **)(__int64, __int64))(a1 + 24))(a1, 1LL) )
result = (v3 != 0) & (unsigned __int8)((unsigned int)func7(a1, v2) != 0);
else
result = v3;
return result;
}

original_code:
static int
test_aexpr(Test_env *te, int do_eval)
{
 int res;
 res = test_nexpr(te, do_eval);
 if (!res)
  do_eval = 0;
 if (!(te->flags & (1<<(0))) && (*te->isa)(te, TM_AND))
  return test_aexpr(te, do_eval) && res;
 return res;
}

canonical_original_code:
static int test_aexpr(struct test_env *te, int do_eval)
{
 int res;
 res = test_nexpr(te, do_eval);
 if (!res)
  do_eval = 0;
 if (!(te->flags & (1<<(0))) && (*te->isa)(te, TM_AND))
  return test_aexpr(te, do_eval) && res;
 return res;
}


decompiled_code:
__int64 __fastcall test_oexpr(__int64 a1, unsigned int a2)
{
unsigned int v2; // er12
unsigned int v3; // er13
__int64 result; // rax

v2 = a2;
v3 = test_aexpr(a1, a2);
if ( v3 )
v2 = 0;
if ( (*(_BYTE *)a1 & 1) == 0 && (*(unsigned int (__fastcall **)(__int64, _QWORD))(a1 + 24))(a1, 0LL) )
result = (v3 | (unsigned int)test_oexpr(a1, v2)) != 0;
else
result = v3;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, unsigned int a2)
{
unsigned int v2; // er12
unsigned int v3; // er13
__int64 result; // rax

v2 = a2;
v3 = func7(a1, a2);
if ( v3 )
v2 = 0;
if ( (*(_BYTE *)a1 & 1) == 0 && (*(unsigned int (__fastcall **)(__int64, _QWORD))(a1 + 24))(a1, 0LL) )
result = (v3 | (unsigned int)func8(a1, v2)) != 0;
else
result = v3;
return result;
}

original_code:
static int
test_oexpr(Test_env *te, int do_eval)
{
 int res;
 res = test_aexpr(te, do_eval);
 if (res)
  do_eval = 0;
 if (!(te->flags & (1<<(0))) && (*te->isa)(te, TM_OR))
  return test_oexpr(te, do_eval) || res;
 return res;
}

canonical_original_code:
static int test_oexpr(struct test_env *te, int do_eval)
{
 int res;
 res = test_aexpr(te, do_eval);
 if (res)
  do_eval = 0;
 if (!(te->flags & (1<<(0))) && (*te->isa)(te, TM_OR))
  return test_oexpr(te, do_eval) || res;
 return res;
}


decompiled_code:
unsigned int __fastcall test_eval(__int64 a1, int a2, char *a3, const char *a4, int a5)
{
unsigned int result; // eax
unsigned int v8; // edx
char v9; // si
unsigned int v10; // edx
int v11; // eax
__gid_t v12; // ebx
int v13; // edi
int v14; // eax
struct stat v15; // [rsp+0h] [rbp-148h] BYREF
struct stat fd; // [rsp+90h] [rbp-B8h] BYREF

if ( !a5 )
return 0;
switch ( a2 )
{
case 1:
return *a3 != 0;
case 2:
return *a3 == 0;
case 3:
if ( *a3 == 33 )
{
v14 = option(a3 + 1);
if ( v14 < 0 )
return 0;
result = *((_BYTE *)&shell_flags + v14) == 0;
}
else
{
v11 = option(a3);
if ( v11 < 0 )
return 0;
result = *((char *)&shell_flags + v11);
}
return result;
case 4:
case 5:
return stat(a3, &v15) == 0;
case 6:
return !stat(a3, &v15) && (v15.st_mode & 0xF000) == 0x8000;
case 7:
return !stat(a3, &v15) && (v15.st_mode & 0xF000) == 24576;
case 8:
return !stat(a3, &v15) && (v15.st_mode & 0xF000) == 0x2000;
case 9:
return !lstat(a3, &v15) && (v15.st_mode & 0xF000) == 40960;
case 10:
return !stat(a3, &v15) && (v15.st_mode & 0xF000) == 4096;
case 11:
return !stat(a3, &v15) && (v15.st_mode & 0xF000) == 49152;
case 12:
return 0;
case 13:
return !stat(a3, &v15) && (v15.st_mode & 0xF000) == 0x4000;
case 14:
if ( stat(a3, &v15) )
return 0;
v12 = v15.st_gid;
return v12 == getegid();
case 15:
return !stat(a3, &v15) && (v15.st_mode & 0x400) != 0;
case 16:
return !stat(a3, &v15) && (v15.st_mode & 0x200) != 0;
case 17:
return !stat(a3, &v15) && v15.st_uid == ksheuid;
case 18:
v9 = 4;
return test_eaccess(a3, v9) == 0;
case 19:
return !stat(a3, &v15) && v15.st_size > 0;
case 20:
if ( a3 )
{
result = bi_getn(a3, &fd);
if ( !result )
{
*(_DWORD *)a1 |= 1u;
return result;
}
v13 = fd.st_dev;
}
else
{
v13 = 0;
}
result = isatty(v13);
break;
case 21:
return !stat(a3, &v15) && (v15.st_mode & 0x800) != 0;
case 22:
v9 = 2;
return test_eaccess(a3, v9) == 0;
case 23:
v9 = 1;
return test_eaccess(a3, v9) == 0;
case 24:
if ( (*(_BYTE *)a1 & 2) != 0 )
result = gmatch(a3, a4, 0LL);
else
result = strcmp(a3, a4) == 0;
return result;
case 25:
if ( (*(_BYTE *)a1 & 2) != 0 )
result = gmatch(a3, a4, 0LL) == 0;
else
result = strcmp(a3, a4) != 0;
return result;
case 26:
return (unsigned int)strcmp(a3, a4) >> 31;
case 27:
return strcmp(a3, a4) > 0;
case 28:
case 29:
case 30:
case 31:
case 32:
case 33:
if ( (unsigned int)evaluate(a3, &v15, 1LL, 0LL) && (unsigned int)evaluate(a4, &fd, 1LL, 0LL) )
{
switch ( a2 )
{
case 29:
result = v15.st_dev != fd.st_dev;
break;
case 30:
result = (signed __int64)v15.st_dev > (signed __int64)fd.st_dev;
break;
case 31:
result = (signed __int64)v15.st_dev >= (signed __int64)fd.st_dev;
break;
case 32:
result = (signed __int64)v15.st_dev < (signed __int64)fd.st_dev;
break;
case 33:
result = (signed __int64)v15.st_dev <= (signed __int64)fd.st_dev;
break;
default:
result = v15.st_dev == fd.st_dev;
break;
}
}
else
{
*(_DWORD *)a1 |= 1u;
result = 1;
}
return result;
case 34:
if ( stat(a3, &v15) || stat(a4, &fd) || v15.st_dev != fd.st_dev )
return 0;
return v15.st_ino == fd.st_ino;
case 35:
if ( stat(a3, &v15) )
return 0;
v8 = stat(a4, &fd);
result = v8 >> 31;
if ( !v8 )
result = v15.st_mtim.tv_sec > fd.st_mtim.tv_sec;
return result;
case 36:
if ( stat(a4, &fd) )
return 0;
v10 = stat(a3, &v15);
result = v10 >> 31;
if ( !v10 )
result = v15.st_mtim.tv_sec < fd.st_mtim.tv_sec;
return result;
default:
(*(void (__fastcall **)(__int64, _QWORD, const char *))(a1 + 48))(a1, 0LL, "internal error: unknown op");
return 1;
}
return result;
}

canonical_decompiled_code:
unsigned int __fastcall func9(__int64 a1, int a2, char *a3, const char *a4, int a5)
{
unsigned int result; // eax
unsigned int v8; // edx
char v9; // si
unsigned int v10; // edx
int v11; // eax
__gid_t v12; // ebx
int v13; // edi
int v14; // eax
struct stat v15; // [rsp+0h] [rbp-148h] BYREF
struct stat fd; // [rsp+90h] [rbp-B8h] BYREF

if ( !a5 )
return 0;
switch ( a2 )
{
case 1:
return *a3 != 0;
case 2:
return *a3 == 0;
case 3:
if ( *a3 == 33 )
{
v14 = option(a3 + 1);
if ( v14 < 0 )
return 0;
result = *((_BYTE *)&shell_flags + v14) == 0;
}
else
{
v11 = option(a3);
if ( v11 < 0 )
return 0;
result = *((char *)&shell_flags + v11);
}
return result;
case 4:
case 5:
return stat(a3, &v15) == 0;
case 6:
return !stat(a3, &v15) && (v15.st_mode & 0xF000) == 0x8000;
case 7:
return !stat(a3, &v15) && (v15.st_mode & 0xF000) == 24576;
case 8:
return !stat(a3, &v15) && (v15.st_mode & 0xF000) == 0x2000;
case 9:
return !lstat(a3, &v15) && (v15.st_mode & 0xF000) == 40960;
case 10:
return !stat(a3, &v15) && (v15.st_mode & 0xF000) == 4096;
case 11:
return !stat(a3, &v15) && (v15.st_mode & 0xF000) == 49152;
case 12:
return 0;
case 13:
return !stat(a3, &v15) && (v15.st_mode & 0xF000) == 0x4000;
case 14:
if ( stat(a3, &v15) )
return 0;
v12 = v15.st_gid;
return v12 == getegid();
case 15:
return !stat(a3, &v15) && (v15.st_mode & 0x400) != 0;
case 16:
return !stat(a3, &v15) && (v15.st_mode & 0x200) != 0;
case 17:
return !stat(a3, &v15) && v15.st_uid == ksheuid;
case 18:
v9 = 4;
return func2(a3, v9) == 0;
case 19:
return !stat(a3, &v15) && v15.st_size > 0;
case 20:
if ( a3 )
{
result = bi_getn(a3, &fd);
if ( !result )
{
*(_DWORD *)a1 |= 1u;
return result;
}
v13 = fd.st_dev;
}
else
{
v13 = 0;
}
result = isatty(v13);
break;
case 21:
return !stat(a3, &v15) && (v15.st_mode & 0x800) != 0;
case 22:
v9 = 2;
return func2(a3, v9) == 0;
case 23:
v9 = 1;
return func2(a3, v9) == 0;
case 24:
if ( (*(_BYTE *)a1 & 2) != 0 )
result = gmatch(a3, a4, 0LL);
else
result = strcmp(a3, a4) == 0;
return result;
case 25:
if ( (*(_BYTE *)a1 & 2) != 0 )
result = gmatch(a3, a4, 0LL) == 0;
else
result = strcmp(a3, a4) != 0;
return result;
case 26:
return (unsigned int)strcmp(a3, a4) >> 31;
case 27:
return strcmp(a3, a4) > 0;
case 28:
case 29:
case 30:
case 31:
case 32:
case 33:
if ( (unsigned int)evaluate(a3, &v15, 1LL, 0LL) && (unsigned int)evaluate(a4, &fd, 1LL, 0LL) )
{
switch ( a2 )
{
case 29:
result = v15.st_dev != fd.st_dev;
break;
case 30:
result = (signed __int64)v15.st_dev > (signed __int64)fd.st_dev;
break;
case 31:
result = (signed __int64)v15.st_dev >= (signed __int64)fd.st_dev;
break;
case 32:
result = (signed __int64)v15.st_dev < (signed __int64)fd.st_dev;
break;
case 33:
result = (signed __int64)v15.st_dev <= (signed __int64)fd.st_dev;
break;
default:
result = v15.st_dev == fd.st_dev;
break;
}
}
else
{
*(_DWORD *)a1 |= 1u;
result = 1;
}
return result;
case 34:
if ( stat(a3, &v15) || stat(a4, &fd) || v15.st_dev != fd.st_dev )
return 0;
return v15.st_ino == fd.st_ino;
case 35:
if ( stat(a3, &v15) )
return 0;
v8 = stat(a4, &fd);
result = v8 >> 31;
if ( !v8 )
result = v15.st_mtim.tv_sec > fd.st_mtim.tv_sec;
return result;
case 36:
if ( stat(a4, &fd) )
return 0;
v10 = stat(a3, &v15);
result = v10 >> 31;
if ( !v10 )
result = v15.st_mtim.tv_sec < fd.st_mtim.tv_sec;
return result;
default:
(*(void (__fastcall **)(__int64, _QWORD, const char *))(a1 + 48))(a1, 0LL, "internal error: unknown op");
return 1;
}
return result;
}

original_code:
int
test_eval(Test_env *te, Test_op op, const char *opnd1, const char *opnd2,
    int do_eval)
{
 int res;
 int not;
 struct stat b1, b2;
 if (!do_eval)
  return 0;
 switch ((int) op) {
 case TO_STNZE:
  return *opnd1 != '\0';
 case TO_STZER:
  return *opnd1 == '\0';
 case TO_OPTION:
  if ((not = *opnd1 == '!'))
   opnd1++;
  if ((res = option(opnd1)) < 0)
   res = 0;
  else {
   res = (shell_flags[(int) (res)]);
   if (not)
    res = !res;
  }
  return res;
 case TO_FILRD:
  return test_eaccess(opnd1, 4) == 0;
 case TO_FILWR:
  return test_eaccess(opnd1, 2) == 0;
 case TO_FILEX:
  return test_eaccess(opnd1, 1) == 0;
 case TO_FILAXST:
  return stat(opnd1, &b1) == 0;
 case TO_FILEXST:
  return stat(opnd1, &b1) == 0;
 case TO_FILREG:
  return stat(opnd1, &b1) == 0 && ((((b1.st_mode)) & 0170000) == (0100000));
 case TO_FILID:
  return stat(opnd1, &b1) == 0 && ((((b1.st_mode)) & 0170000) == (0040000));
 case TO_FILCDEV:
  return stat(opnd1, &b1) == 0 && ((((b1.st_mode)) & 0170000) == (0020000));
 case TO_FILBDEV:
  return stat(opnd1, &b1) == 0 && ((((b1.st_mode)) & 0170000) == (0060000));
 case TO_FILFIFO:
  return stat(opnd1, &b1) == 0 && ((((b1.st_mode)) & 0170000) == (0010000));
 case TO_FILSYM:
  return lstat(opnd1, &b1) == 0 && ((((b1.st_mode)) & 0170000) == (0120000));
 case TO_FILSOCK:
  return stat(opnd1, &b1) == 0 && ((((b1.st_mode)) & 0170000) == (0140000));
 case TO_FILCDF:
  return 0;
 case TO_FILSETU:
  return stat(opnd1, &b1) == 0 &&
      (b1.st_mode & 04000) == 04000;
 case TO_FILSETG:
  return stat(opnd1, &b1) == 0 &&
      (b1.st_mode & 02000) == 02000;
 case TO_FILSTCK:
  return stat(opnd1, &b1) == 0 &&
      (b1.st_mode & 01000) == 01000;
 case TO_FILGZ:
  return stat(opnd1, &b1) == 0 && b1.st_size > 0L;
 case TO_FILTT:
  if (opnd1 && !bi_getn(opnd1, &res)) {
   te->flags |= (1<<(0));
   res = 0;
  } else {
   res = isatty(opnd1 ? res : 0);
  }
  return res;
 case TO_FILUID:
  return stat(opnd1, &b1) == 0 && b1.st_uid == ksheuid;
 case TO_FILGID:
  return stat(opnd1, &b1) == 0 && b1.st_gid == getegid();
 case TO_STEQL:
  if (te->flags & (1<<(1)))
   return gmatch(opnd1, opnd2, 0);
  return strcmp(opnd1, opnd2) == 0;
 case TO_STNEQ:
  if (te->flags & (1<<(1)))
   return !gmatch(opnd1, opnd2, 0);
  return strcmp(opnd1, opnd2) != 0;
 case TO_STLT:
  return strcmp(opnd1, opnd2) < 0;
 case TO_STGT:
  return strcmp(opnd1, opnd2) > 0;
 case TO_INTEQ:
 case TO_INTNE:
 case TO_INTGE:
 case TO_INTGT:
 case TO_INTLE:
 case TO_INTLT:
  {
   int64_t v1, v2;
   if (!evaluate(opnd1, &v1, 0x1, 0) ||
       !evaluate(opnd2, &v2, 0x1, 0)) {
    te->flags |= (1<<(0));
    return 1;
   }
   switch ((int) op) {
   case TO_INTEQ:
    return v1 == v2;
   case TO_INTNE:
    return v1 != v2;
   case TO_INTGE:
    return v1 >= v2;
   case TO_INTGT:
    return v1 > v2;
   case TO_INTLE:
    return v1 <= v2;
   case TO_INTLT:
    return v1 < v2;
   }
  }
 case TO_FILNT:
  {
   int s2;
   return stat(opnd1, &b1) == 0 &&
       (((s2 = stat(opnd2, &b2)) == 0 &&
       b1.st_mtim.tv_sec > b2.st_mtim.tv_sec) || s2 < 0);
  }
 case TO_FILOT:
  {
   int s1;
   return stat(opnd2, &b2) == 0 &&
       (((s1 = stat(opnd1, &b1)) == 0 &&
       b1.st_mtim.tv_sec < b2.st_mtim.tv_sec) || s1 < 0);
  }
 case TO_FILEQ:
  return stat (opnd1, &b1) == 0 && stat (opnd2, &b2) == 0 &&
      b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino;
 }
 (*te->error)(te, 0, "internal error: unknown op");
 return 1;
}

canonical_original_code:
int test_eval(struct test_env *te, enum Test_op op, char *opnd1, char *opnd2, int do_eval)
{
 int res;
 int not;
 struct stat b1; struct stat b2;
 if (!do_eval)
  return 0;
 switch ((int) op) {
 case TO_STNZE:
  return *opnd1 != '\0';
 case TO_STZER:
  return *opnd1 == '\0';
 case TO_OPTION:
  if ((not = *opnd1 == '!'))
   opnd1++;
  if ((res = option(opnd1)) < 0)
   res = 0;
  else {
   res = (shell_flags[(int) (res)]);
   if (not)
    res = !res;
  }
  return res;
 case TO_FILRD:
  return test_eaccess(opnd1, 4) == 0;
 case TO_FILWR:
  return test_eaccess(opnd1, 2) == 0;
 case TO_FILEX:
  return test_eaccess(opnd1, 1) == 0;
 case TO_FILAXST:
  return stat(opnd1, &b1) == 0;
 case TO_FILEXST:
  return stat(opnd1, &b1) == 0;
 case TO_FILREG:
  return stat(opnd1, &b1) == 0 && ((((b1.st_mode)) & 0170000) == (0100000));
 case TO_FILID:
  return stat(opnd1, &b1) == 0 && ((((b1.st_mode)) & 0170000) == (0040000));
 case TO_FILCDEV:
  return stat(opnd1, &b1) == 0 && ((((b1.st_mode)) & 0170000) == (0020000));
 case TO_FILBDEV:
  return stat(opnd1, &b1) == 0 && ((((b1.st_mode)) & 0170000) == (0060000));
 case TO_FILFIFO:
  return stat(opnd1, &b1) == 0 && ((((b1.st_mode)) & 0170000) == (0010000));
 case TO_FILSYM:
  return lstat(opnd1, &b1) == 0 && ((((b1.st_mode)) & 0170000) == (0120000));
 case TO_FILSOCK:
  return stat(opnd1, &b1) == 0 && ((((b1.st_mode)) & 0170000) == (0140000));
 case TO_FILCDF:
  return 0;
 case TO_FILSETU:
  return stat(opnd1, &b1) == 0 &&
      (b1.st_mode & 04000) == 04000;
 case TO_FILSETG:
  return stat(opnd1, &b1) == 0 &&
      (b1.st_mode & 02000) == 02000;
 case TO_FILSTCK:
  return stat(opnd1, &b1) == 0 &&
      (b1.st_mode & 01000) == 01000;
 case TO_FILGZ:
  return stat(opnd1, &b1) == 0 && b1.st_size > 0L;
 case TO_FILTT:
  if (opnd1 && !bi_getn(opnd1, &res)) {
   te->flags |= (1<<(0));
   res = 0;
  } else {
   res = isatty(opnd1 ? res : 0);
  }
  return res;
 case TO_FILUID:
  return stat(opnd1, &b1) == 0 && b1.st_uid == ksheuid;
 case TO_FILGID:
  return stat(opnd1, &b1) == 0 && b1.st_gid == getegid();
 case TO_STEQL:
  if (te->flags & (1<<(1)))
   return gmatch(opnd1, opnd2, 0);
  return strcmp(opnd1, opnd2) == 0;
 case TO_STNEQ:
  if (te->flags & (1<<(1)))
   return !gmatch(opnd1, opnd2, 0);
  return strcmp(opnd1, opnd2) != 0;
 case TO_STLT:
  return strcmp(opnd1, opnd2) < 0;
 case TO_STGT:
  return strcmp(opnd1, opnd2) > 0;
 case TO_INTEQ:
 case TO_INTNE:
 case TO_INTGE:
 case TO_INTGT:
 case TO_INTLE:
 case TO_INTLT:
  {
   long v1; long v2;
   if (!evaluate(opnd1, &v1, 0x1, 0) ||
       !evaluate(opnd2, &v2, 0x1, 0)) {
    te->flags |= (1<<(0));
    return 1;
   }
   switch ((int) op) {
   case TO_INTEQ:
    return v1 == v2;
   case TO_INTNE:
    return v1 != v2;
   case TO_INTGE:
    return v1 >= v2;
   case TO_INTGT:
    return v1 > v2;
   case TO_INTLE:
    return v1 <= v2;
   case TO_INTLT:
    return v1 < v2;
   }
  }
 case TO_FILNT:
  {
   int s2;
   return stat(opnd1, &b1) == 0 &&
       (((s2 = stat(opnd2, &b2)) == 0 &&
       b1.st_mtim.tv_sec > b2.st_mtim.tv_sec) || s2 < 0);
  }
 case TO_FILOT:
  {
   int s1;
   return stat(opnd2, &b2) == 0 &&
       (((s1 = stat(opnd1, &b1)) == 0 &&
       b1.st_mtim.tv_sec < b2.st_mtim.tv_sec) || s1 < 0);
  }
 case TO_FILEQ:
  return stat (opnd1, &b1) == 0 && stat (opnd2, &b2) == 0 &&
      b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino;
 }
 (*te->error)(te, 0, "internal error: unknown op");
 return 1;
}

variable_types: not int

user_defined_types: struct timespec { long tv_sec; long tv_nsec; }


decompiled_code:
// attributes: thunk
unsigned int __fastcall ptest_eval(__int64 a1, int a2, char *a3, const char *a4, int a5)
{
return test_eval(a1, a2, a3, a4, a5);
}

canonical_decompiled_code:
unsigned int __fastcall func10(__int64 a1, int a2, char *a3, const char *a4, int a5)
{
return func9(a1, a2, a3, a4, a5);
}

original_code:
static int
ptest_eval(Test_env *te, Test_op op, const char *opnd1, const char *opnd2,
    int do_eval)
{
 return test_eval(te, op, opnd1, opnd2, do_eval);
}

canonical_original_code:
static int ptest_eval(struct test_env *te, enum Test_op op, char *opnd1, char *opnd2, int do_eval)
{
 return test_eval(te, op, opnd1, opnd2, do_eval);
}


decompiled_code:
__int64 __fastcall test_parse(__int64 a1)
{
int v1; // eax
__int64 result; // rax
int v3; // ebp

v1 = test_oexpr(a1, 1u);
if ( (*(_BYTE *)a1 & 1) != 0 )
goto LABEL_2;
v3 = v1;
if ( !(*(unsigned int (__fastcall **)(__int64, __int64))(a1 + 24))(a1, 7LL) )
(*(void (__fastcall **)(__int64, _QWORD, const char *))(a1 + 48))(a1, 0LL, "unexpected operator/operand");
if ( (*(_BYTE *)a1 & 1) != 0 )
LABEL_2:
result = 2LL;
else
result = v3 == 0;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1)
{
int v1; // eax
__int64 result; // rax
int v3; // ebp

v1 = func8(a1, 1u);
if ( (*(_BYTE *)a1 & 1) != 0 )
goto LABEL_2;
v3 = v1;
if ( !(*(unsigned int (__fastcall **)(__int64, __int64))(a1 + 24))(a1, 7LL) )
(*(void (__fastcall **)(__int64, _QWORD, const char *))(a1 + 48))(a1, 0LL, "unexpected operator/operand");
if ( (*(_BYTE *)a1 & 1) != 0 )
LABEL_2:
result = 2LL;
else
result = v3 == 0;
return result;
}

original_code:
int
test_parse(Test_env *te)
{
 int res;
 res = test_oexpr(te, 1);
 if (!(te->flags & (1<<(0))) && !(*te->isa)(te, TM_END))
  (*te->error)(te, 0, "unexpected operator/operand");
 return (te->flags & (1<<(0))) ? 2 : !res;
}

canonical_original_code:
int test_parse(struct test_env *te)
{
 int res;
 res = test_oexpr(te, 1);
 if (!(te->flags & (1<<(0))) && !(*te->isa)(te, TM_END))
  (*te->error)(te, 0, "unexpected operator/operand");
 return (te->flags & (1<<(0))) ? 2 : !res;
}


usage

decompiled_code:
void __noreturn usage()
{
fprintf(stderr, "usage: %s [-abcflnostw] [-F foldwidth] [file ...]\n", (const char *)program_invocation_short_name);
exit(1);
}

canonical_decompiled_code:
void __noreturn func8()
{
fprintf(stderr, "usage: %s [-abcflnostw] [-F foldwidth] [file ...]\n", (const char *)program_invocation_short_name);
exit(1);
}

original_code:
void
usage(void)
{
 extern char *__progname;
 fprintf(stderr, "usage: %s [-abcflnostw] [-F foldwidth] [file ...]\n",
     __progname);
 exit(1);
}

canonical_original_code:
void usage()
{
 extern char *__progname;
 fprintf(stderr, "usage: %s [-abcflnostw] [-F foldwidth] [file ...]\n",
     __progname);
 exit(1);
}


foldit

decompiled_code:
__int64 __fastcall foldit(char *a1, unsigned int a2, int a3)
{
unsigned int v3; // er8
int v4; // ebx
_BOOL4 i; // ecx
char v6; // al
char *v7; // rdx

v3 = a2;
v4 = a3 - 1;
for ( i = a2 != 0; ; i = 0 )
{
v6 = *a1;
if ( !*a1 )
break;
v7 = a1;
while ( 1 )
{
if ( v6 == 9 )
{
v3 = (v3 + 8) & 0xFFFFFFF8;
goto LABEL_6;
}
if ( v6 <= 9 )
{
if ( v6 == 8 )
{
v3 = (v3 == 0) + v3 - 1;
goto LABEL_6;
}
goto LABEL_16;
}
if ( v6 == 10 || v6 == 13 )
break;
LABEL_16:
++v3;
LABEL_6:
if ( (int)v3 >= v4 )
goto LABEL_13;
LABEL_7:
v6 = *++v7;
if ( !v6 )
return v3;
}
v3 = 0;
if ( v4 > 0 )
goto LABEL_7;
LABEL_13:
if ( !i )
return v3;
puts("\\");
v3 = 0;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func9(char *a1, unsigned int a2, int a3)
{
unsigned int v3; // er8
int v4; // ebx
_BOOL4 i; // ecx
char v6; // al
char *v7; // rdx

v3 = a2;
v4 = a3 - 1;
for ( i = a2 != 0; ; i = 0 )
{
v6 = *a1;
if ( !*a1 )
break;
v7 = a1;
while ( 1 )
{
if ( v6 == 9 )
{
v3 = (v3 + 8) & 0xFFFFFFF8;
goto LABEL_6;
}
if ( v6 <= 9 )
{
if ( v6 == 8 )
{
v3 = (v3 == 0) + v3 - 1;
goto LABEL_6;
}
goto LABEL_16;
}
if ( v6 == 10 || v6 == 13 )
break;
LABEL_16:
++v3;
LABEL_6:
if ( (int)v3 >= v4 )
goto LABEL_13;
LABEL_7:
v6 = *++v7;
if ( !v6 )
return v3;
}
v3 = 0;
if ( v4 > 0 )
goto LABEL_7;
LABEL_13:
if ( !i )
return v3;
puts("\\");
v3 = 0;
}
return v3;
}

original_code:
int
foldit(char *chunk, int col, int max)
{
 char *cp;
 int first = (col != 0);
again:
 cp = chunk;
 while (*cp) {
  switch(*cp) {
  case '\n':
  case '\r':
   col = 0;
   break;
  case '\t':
   col = (col + 8) & ~07;
   break;
  case '\b':
   col = col ? col - 1 : 0;
   break;
  default:
   col++;
  }
  if (col > (max - 2)) {
   if (!first)
    return (col);
   printf("\\\n");
   col = 0;
   first = 0;
   goto again;
  }
  cp++;
 }
 return (col);
}

variable_types:
chunk
char *

variable_types: col int

variable_types: max int

variable_types:
cp
char *

variable_types: first int


aresize

decompiled_code:
__int64 __fastcall aresize(__int64 a1, unsigned __int64 a2, _QWORD **a3)
{
__int64 v4; // r12
_QWORD *v5; // rbx
_QWORD *v6; // rax
__int64 result; // rax

if ( !a1 )
return alloc(a2, a3);
if ( a2 <= 0xFFFFFFFFFFFFFFEFLL
&& (v4 = *(_QWORD *)(a1 - 16), v5 = *(_QWORD **)(a1 - 8), (v6 = realloc((void *)(a1 - 16), a2 + 16)) != 0LL) )
{
if ( v4 )
*(_QWORD *)(v4 + 8) = v6;
else
*a3 = v6;
if ( v5 )
*v5 = v6;
result = (__int64)(v6 + 2);
}
else
{
internal_errorf("unable to allocate memory");
result = areallocarray();
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, unsigned __int64 a2, _QWORD **a3)
{
__int64 v4; // r12
_QWORD *v5; // rbx
_QWORD *v6; // rax
__int64 result; // rax

if ( !a1 )
return func1(a2, a3);
if ( a2 <= 0xFFFFFFFFFFFFFFEFLL
&& (v4 = *(_QWORD *)(a1 - 16), v5 = *(_QWORD **)(a1 - 8), (v6 = realloc((void *)(a1 - 16), a2 + 16)) != 0LL) )
{
if ( v4 )
*(_QWORD *)(v4 + 8) = v6;
else
*a3 = v6;
if ( v5 )
*v5 = v6;
result = (__int64)(v6 + 2);
}
else
{
internal_errorf("unable to allocate memory");
result = func3();
}
return result;
}

original_code:
void *
aresize(void *ptr, size_t size, Area *ap)
{
 struct link *l, *l2, *lprev, *lnext;
 if (ptr == ((void *)0))
  return alloc(size, ap);
 if (size > (18446744073709551615UL) - sizeof(struct link))
  internal_errorf("unable to allocate memory");
 l = ( (struct link *)(((char *)(ptr)) - sizeof(struct link)) );
 lprev = l->prev;
 lnext = l->next;
 l2 = realloc(l, sizeof(struct link) + size);
 if (l2 == ((void *)0))
  internal_errorf("unable to allocate memory");
 if (lprev)
  lprev->next = l2;
 else
  ap->freelist = l2;
 if (lnext)
  lnext->prev = l2;
 return ( (void *)(((char *)(l2)) + sizeof(struct link)) );
}

canonical_original_code:
void *aresize(void *ptr, unsigned long size, struct Area *ap)
{
 struct link *l; struct link *l2; struct link *lprev; struct link *lnext;
 if (ptr == ((void *)0))
  return alloc(size, ap);
 if (size > (18446744073709551615UL) - sizeof(struct link))
  internal_errorf("unable to allocate memory");
 l = ( (struct link *)(((char *)(ptr)) - sizeof(struct link)) );
 lprev = l->prev;
 lnext = l->next;
 l2 = realloc(l, sizeof(struct link) + size);
 if (l2 == ((void *)0))
  internal_errorf("unable to allocate memory");
 if (lprev)
  lprev->next = l2;
 else
  ap->freelist = l2;
 if (lnext)
  lnext->prev = l2;
 return ( (void *)(((char *)(l2)) + sizeof(struct link)) );
}

variable_types:
ptr
void *

variable_types:
size
unsigned long

variable_types: struct Area { struct link { struct link *prev; struct link *next; } *freelist; } *

return_type: void *


areallocarray

decompiled_code:
__int64 __fastcall areallocarray(__int64 a1, unsigned __int64 a2, unsigned __int64 a3, _QWORD **a4)
{
if ( a3 <= 0xFFFFFFFF && a2 <= 0xFFFFFFFF || !a2 || is_mul_ok(a2, a3) )
return aresize(a1, a3 * a2, a4);
internal_errorf("unable to allocate memory");
return afree();
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, unsigned __int64 a2, unsigned __int64 a3, _QWORD **a4)
{
if ( a3 <= 0xFFFFFFFF && a2 <= 0xFFFFFFFF || !a2 || is_mul_ok(a2, a3) )
return func2(a1, a3 * a2, a4);
internal_errorf("unable to allocate memory");
return func4();
}

original_code:
void *
areallocarray(void *ptr, size_t nmemb, size_t size, Area *ap)
{
 if ((nmemb >= (1UL << (sizeof(size_t) * 4)) || size >= (1UL << (sizeof(size_t) * 4))) &&
     nmemb > 0 && (18446744073709551615UL) / nmemb < size) {
  internal_errorf("unable to allocate memory");
 }
 return aresize(ptr, nmemb * size, ap);
}

canonical_original_code:
void *areallocarray(void *ptr, unsigned long nmemb, unsigned long size, struct Area *ap)
{
 if ((nmemb >= (1UL << (sizeof(unsigned long) * 4)) || size >= (1UL << (sizeof(unsigned long) * 4))) &&
     nmemb > 0 && (18446744073709551615UL) / nmemb < size) {
  internal_errorf("unable to allocate memory");
 }
 return aresize(ptr, nmemb * size, ap);
}

variable_types:
nmemb
unsigned long

user_defined_types: struct link { struct link *prev; struct link *next; }


afree

original_code:
void
afree(void *ptr, Area *ap)
{
 struct link *l;
 if (!ptr)
  return;
 l = ( (struct link *)(((char *)(ptr)) - sizeof(struct link)) );
 if (l->prev)
  l->prev->next = l->next;
 else
  ap->freelist = l->next;
 if (l->next)
  l->next->prev = l->prev;
 free(l);
}

canonical_original_code:
void afree(void *ptr, struct Area *ap)
{
 struct link *l;
 if (!ptr)
  return;
 l = ( (struct link *)(((char *)(ptr)) - sizeof(struct link)) );
 if (l->prev)
  l->prev->next = l->next;
 else
  ap->freelist = l->next;
 if (l->next)
  l->next->prev = l->prev;
 free(l);
}

variable_types: struct link { struct link *prev; struct link *next; } *


munset

decompiled_code:
__int64 __fastcall munset(_QWORD *a1)
{
_QWORD *v1; // rbp
_QWORD *v2; // rdi
__int64 result; // rax

if ( a1 )
{
v1 = a1;
while ( *v1 )
{
v2 = v1;
v1 = (_QWORD *)*v1;
afree(v2, &aperm);
}
afree(v1[1], &aperm);
result = afree(v1, &aperm);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(_QWORD *a1)
{
_QWORD *v1; // rbp
_QWORD *v2; // rdi
__int64 result; // rax

if ( a1 )
{
v1 = a1;
while ( *v1 )
{
v2 = v1;
v1 = (_QWORD *)*v1;
afree(v2, &aperm);
}
afree(v1[1], &aperm);
result = afree(v1, &aperm);
}
return result;
}

original_code:
static void
munset(mbox_t *mlist)
{
 mbox_t *mbp;
 while (mlist != ((void *)0)) {
  mbp = mlist;
  mlist = mbp->mb_next;
  if (!mlist)
   afree(mbp->mb_path, &aperm);
  afree(mbp, &aperm);
 }
}

canonical_original_code:
static void munset(struct mbox *mlist)
{
 struct mbox *mbp;
 while (mlist != ((void *)0)) {
  mbp = mlist;
  mlist = mbp->mb_next;
  if (!mlist)
   afree(mbp->mb_path, &aperm);
  afree(mbp, &aperm);
 }
}

variable_types: struct mbox { struct mbox *mb_next; char *mb_path; char *mb_msg; long mb_mtime; } *


mcset

decompiled_code:
void __fastcall mcset(__int64 a1)
{
mailcheck_interval = a1;
}

canonical_decompiled_code:
void __fastcall func3(__int64 a1)
{
mailcheck_interval = a1;
}

original_code:
void
mcset(int64_t interval)
{
 mailcheck_interval = interval;
}

canonical_original_code:
void mcset(long interval)
{
 mailcheck_interval = interval;
}

variable_types: interval long


mbset

decompiled_code:
__time_t __fastcall mbset(char *file)
{
__int64 v1; // rax
__time_t result; // rax
struct stat v3; // [rsp+0h] [rbp-98h] BYREF

afree(qword_450, &aperm);
afree(qword_448, &aperm);
v1 = str_save(file, &aperm);
qword_450 = 0LL;
qword_448 = v1;
if ( file && !stat(file, &v3) && (v3.st_mode & 0xF000) == 0x8000 )
result = v3.st_mtim.tv_sec;
else
result = 0LL;
qword_458 = result;
return result;
}

canonical_decompiled_code:
__time_t __fastcall func4(char *file)
{
__int64 v1; // rax
__time_t result; // rax
struct stat v3; // [rsp+0h] [rbp-98h] BYREF

afree(qword_450, &aperm);
afree(qword_448, &aperm);
v1 = str_save(file, &aperm);
qword_450 = 0LL;
qword_448 = v1;
if ( file && !stat(file, &v3) && (v3.st_mode & 0xF000) == 0x8000 )
result = v3.st_mtim.tv_sec;
else
result = 0LL;
qword_458 = result;
return result;
}

original_code:
void
mbset(char *p)
{
 struct stat stbuf;
 afree(mbox.mb_msg, &aperm);
 afree(mbox.mb_path, &aperm);
 mbox.mb_path = str_save(p, &aperm);
 mbox.mb_msg = ((void *)0);
 if (p && stat(p, &stbuf) == 0 && ((((stbuf.st_mode)) & 0170000) == (0100000)))
  mbox.mb_mtime = stbuf.st_mtim.tv_sec;
 else
  mbox.mb_mtime = 0;
}

variable_types:
p
char *

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]; }


mpset

decompiled_code:
char *__fastcall mpset(__int64 a1)
{
char *result; // rax
char *v2; // r12
char *v3; // rax
char *v4; // rbx
char *i; // rbp
bool v6; // zf
__int64 v7; // rsi
__int64 v8; // rdx
size_t v9; // rax

munset((_QWORD *)mplist);
mplist = 0LL;
result = (char *)str_save(a1, &aperm);
v2 = result;
if ( result )
{
while ( 2 )
{
v3 = strchr(v2, 58);
v4 = v3;
if ( v3 )
{
*v3 = 0;
v4 = v3 + 1;
}
for ( i = v2; ; memmove(i - 1, i, v9 + 1) )
{
result = strchr(i, 37);
i = result;
if ( !result )
break;
if ( result <= v2 || *(result - 1) != 92 )
goto LABEL_8;
v9 = strlen(result);
}
if ( shell_flags[23] )
goto LABEL_15;
result = strchr(v2, 63);
i = result;
if ( !result )
goto LABEL_15;
LABEL_8:
v6 = i[1] == 0;
*i = 0;
v7 = (__int64)(i + 1);
if ( !v6 )
goto LABEL_9;
LABEL_15:
v7 = 0LL;
LABEL_9:
if ( *v2 )
{
result = (char *)mballoc(v2, v7);
v8 = mplist;
mplist = (__int64)result;
*(_QWORD *)result = v8;
}
if ( v4 )
{
v2 = v4;
continue;
}
break;
}
}
return result;
}

canonical_decompiled_code:
char *__fastcall func5(__int64 a1)
{
char *result; // rax
char *v2; // r12
char *v3; // rax
char *v4; // rbx
char *i; // rbp
bool v6; // zf
__int64 v7; // rsi
__int64 v8; // rdx
size_t v9; // rax

func1((_QWORD *)mplist);
mplist = 0LL;
result = (char *)str_save(a1, &aperm);
v2 = result;
if ( result )
{
while ( 2 )
{
v3 = strchr(v2, 58);
v4 = v3;
if ( v3 )
{
*v3 = 0;
v4 = v3 + 1;
}
for ( i = v2; ; memmove(i - 1, i, v9 + 1) )
{
result = strchr(i, 37);
i = result;
if ( !result )
break;
if ( result <= v2 || *(result - 1) != 92 )
goto LABEL_8;
v9 = strlen(result);
}
if ( shell_flags[23] )
goto LABEL_15;
result = strchr(v2, 63);
i = result;
if ( !result )
goto LABEL_15;
LABEL_8:
v6 = i[1] == 0;
*i = 0;
v7 = (__int64)(i + 1);
if ( !v6 )
goto LABEL_9;
LABEL_15:
v7 = 0LL;
LABEL_9:
if ( *v2 )
{
result = (char *)mballoc(v2, v7);
v8 = mplist;
mplist = (__int64)result;
*(_QWORD *)result = v8;
}
if ( v4 )
{
v2 = v4;
continue;
}
break;
}
}
return result;
}

original_code:
void
mpset(char *mptoparse)
{
 mbox_t *mbp;
 char *mpath, *mmsg, *mval;
 char *p;
 munset( mplist );
 mplist = ((void *)0);
 mval = str_save(mptoparse, &aperm);
 while (mval) {
  mpath = mval;
  if ((mval = strchr(mval, ':')) != ((void *)0)) {
   *mval = '\0';
   mval++;
  }
  for (p = mpath; (mmsg = strchr(p, '%')); ) {
   if (mmsg > mpath && mmsg[-1] == '\\') {
    memmove(mmsg - 1, mmsg, strlen(mmsg) + 1);
    p = mmsg;
    continue;
   }
   break;
  }
  if (!mmsg && !(shell_flags[(int) (FPOSIX)]))
   mmsg = strchr(mpath, '?');
  if (mmsg) {
   *mmsg = '\0';
   mmsg++;
   if (*mmsg == '\0')
    mmsg = ((void *)0);
  }
  if (*mpath == '\0')
   continue;
  mbp = mballoc(mpath, mmsg);
  mbp->mb_next = mplist;
  mplist = mbp;
 }
}

canonical_original_code:
void mpset(char *mptoparse)
{
 struct mbox *mbp;
 char *mpath; char *mmsg; char *mval;
 char *p;
 munset( mplist );
 mplist = ((void *)0);
 mval = str_save(mptoparse, &aperm);
 while (mval) {
  mpath = mval;
  if ((mval = strchr(mval, ':')) != ((void *)0)) {
   *mval = '\0';
   mval++;
  }
  for (p = mpath; (mmsg = strchr(p, '%')); ) {
   if (mmsg > mpath && mmsg[-1] == '\\') {
    memmove(mmsg - 1, mmsg, strlen(mmsg) + 1);
    p = mmsg;
    continue;
   }
   break;
  }
  if (!mmsg && !(shell_flags[(int) (FPOSIX)]))
   mmsg = strchr(mpath, '?');
  if (mmsg) {
   *mmsg = '\0';
   mmsg++;
   if (*mmsg == '\0')
    mmsg = ((void *)0);
  }
  if (*mpath == '\0')
   continue;
  mbp = mballoc(mpath, mmsg);
  mbp->mb_next = mplist;
  mplist = mbp;
 }
}

variable_types:
mptoparse
char *

variable_types:
mpath
char *

variable_types:
mmsg
char *

variable_types:
mval
char *

user_defined_types: struct mbox { struct mbox *mb_next; char *mb_path; char *mb_msg; long mb_mtime; }


original_code:
void
simplify_path(char *path)
{
 char *cur;
 char *t;
 int isrooted;
 char *very_start = path;
 char *start;
 if (!*path)
  return;
 if ((isrooted = (path[0] == '/')))
  very_start++;
 for (cur = t = start = very_start; ; ) {
  while (*t == '/')
   t++;
  if (*t == '\0') {
   if (cur == path)
    *cur++ = '.';
   *cur = '\0';
   break;
  }
  if (t[0] == '.') {
   if (!t[1] || t[1] == '/') {
    t += 1;
    continue;
   } else if (t[1] == '.' && (!t[2] || t[2] == '/')) {
    if (!isrooted && cur == start) {
     if (cur != very_start)
      *cur++ = '/';
     *cur++ = '.';
     *cur++ = '.';
     start = cur;
    } else if (cur != start)
     while (--cur > start && *cur != '/')
      ;
    t += 2;
    continue;
   }
  }
  if (cur != very_start)
   *cur++ = '/';
  while (*t && *t != '/')
   *cur++ = *t++;
 }
}

variable_types:
path
char *

variable_types:
cur
char *

variable_types:
t
char *

variable_types: isrooted int

variable_types:
start
char *


decompiled_code:
void *__fastcall set_current_wd(char *s)
{
char *v1; // rbp
int v3; // eax
void *result; // rax
size_t v5; // r12
void *v6; // rdi
const char *v7; // rax

v1 = s;
if ( s )
{
LABEL_2:
v3 = strlen(v1) + 1;
if ( v3 <= current_wd_size )
return memcpy(current_wd, v1, v3);
goto LABEL_4;
}
v7 = (const char *)ksh_get_wd(0LL, 0LL);
v1 = (char *)v7;
if ( !v7 )
{
v1 = &null;
goto LABEL_2;
}
v3 = strlen(v7) + 1;
if ( current_wd_size < v3 )
{
LABEL_4:
v5 = v3;
current_wd_size = v3;
current_wd = (void *)aresize(current_wd, v3, &aperm);
v6 = current_wd;
goto LABEL_5;
}
v6 = current_wd;
v5 = v3;
LABEL_5:
result = memcpy(v6, v1, v5);
if ( s != v1 && v1 != &null )
result = (void *)afree(v1, genv + 8LL);
return result;
}

canonical_decompiled_code:
void *__fastcall func2(char *s)
{
char *v1; // rbp
int v3; // eax
void *result; // rax
size_t v5; // r12
void *v6; // rdi
const char *v7; // rax

v1 = s;
if ( s )
{
LABEL_2:
v3 = strlen(v1) + 1;
if ( v3 <= current_wd_size )
return memcpy(current_wd, v1, v3);
goto LABEL_4;
}
v7 = (const char *)ksh_get_wd(0LL, 0LL);
v1 = (char *)v7;
if ( !v7 )
{
v1 = &null;
goto LABEL_2;
}
v3 = strlen(v7) + 1;
if ( current_wd_size < v3 )
{
LABEL_4:
v5 = v3;
current_wd_size = v3;
current_wd = (void *)aresize(current_wd, v3, &aperm);
v6 = current_wd;
goto LABEL_5;
}
v6 = current_wd;
v5 = v3;
LABEL_5:
result = memcpy(v6, v1, v5);
if ( s != v1 && v1 != &null )
result = (void *)afree(v1, genv + 8LL);
return result;
}

original_code:
void
set_current_wd(char *path)
{
 int len;
 char *p = path;
 if (!p && !(p = ksh_get_wd(((void *)0), 0)))
  p = null;
 len = strlen(p) + 1;
 if (len > current_wd_size)
  current_wd = aresize(current_wd, current_wd_size = len, &aperm);
 memcpy(current_wd, p, len);
 if (p != path && p != null)
  afree(p, &genv->area);
}

variable_types: len int


stravis

decompiled_code:
__int64 __fastcall stravis(_QWORD *a1, char *a2, __int16 a3)
{
size_t v4; // rax
_BYTE *v5; // rax
void *v6; // rbx
unsigned int v7; // er12
int *v8; // rax
int v9; // ebp
int *v10; // r14
void *v11; // rax

v4 = strlen(a2);
v5 = (_BYTE *)reallocarray(0LL, 4LL, v4 + 1);
if ( !v5 )
return (unsigned int)-1;
v6 = v5;
v7 = strvis(v5, a2, a3);
v8 = _errno_location();
v9 = *v8;
v10 = v8;
v11 = realloc(v6, (int)(v7 + 1));
*a1 = v11;
if ( !v11 )
{
*a1 = v6;
*v10 = v9;
}
return v7;
}

canonical_decompiled_code:
__int64 __fastcall func2(_QWORD *a1, char *a2, __int16 a3)
{
size_t v4; // rax
_BYTE *v5; // rax
void *v6; // rbx
unsigned int v7; // er12
int *v8; // rax
int v9; // ebp
int *v10; // r14
void *v11; // rax

v4 = strlen(a2);
v5 = (_BYTE *)reallocarray(0LL, 4LL, v4 + 1);
if ( !v5 )
return (unsigned int)-1;
v6 = v5;
v7 = func0(v5, a2, a3);
v8 = _errno_location();
v9 = *v8;
v10 = v8;
v11 = realloc(v6, (int)(v7 + 1));
*a1 = v11;
if ( !v11 )
{
*a1 = v6;
*v10 = v9;
}
return v7;
}

original_code:
int
stravis(char **outp, const char *src, int flag)
{
 char *buf;
 int len, serrno;
 buf = reallocarray(((void *)0), 4, strlen(src) + 1);
 if (buf == ((void *)0))
  return -1;
 len = strvis(buf, src, flag);
 serrno = (*__errno_location ());
 *outp = realloc(buf, len + 1);
 if (*outp == ((void *)0)) {
  *outp = buf;
  (*__errno_location ()) = serrno;
 }
 return (len);
}

canonical_original_code:
int stravis(char **outp, char *src, int flag)
{
 char *buf;
 int len; int serrno;
 buf = reallocarray(((void *)0), 4, strlen(src) + 1);
 if (buf == ((void *)0))
  return -1;
 len = strvis(buf, src, flag);
 serrno = (*__errno_location ());
 *outp = realloc(buf, len + 1);
 if (*outp == ((void *)0)) {
  *outp = buf;
  (*__errno_location ()) = serrno;
 }
 return (len);
}

variable_types:
outp
char **

variable_types:
src
char *

variable_types: flag int

variable_types:
buf
char *

variable_types: serrno int


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

if ( tty_fd >= 0 )
{
result = close(tty_fd);
tty_fd = -1;
}
return result;
}

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

if ( tty_fd >= 0 )
{
result = close(tty_fd);
tty_fd = -1;
}
return result;
}

original_code:
void
tty_close(void)
{
 if (tty_fd >= 0) {
  close(tty_fd);
  tty_fd = -1;
 }
}

canonical_original_code:
void tty_close()
{
 if (tty_fd >= 0) {
  close(tty_fd);
  tty_fd = -1;
 }
}


decompiled_code:
int __fastcall tty_init(int a1)
{
int v1; // ebx
int v2; // ebp
int result; // eax
int *v4; // rax
char *v5; // rax
int *v6; // rax
char *v7; // rax

v1 = 1;
tty_close();
tty_devtty = 1;
v2 = open("/dev/tty", 2);
if ( v2 == -1 )
{
tty_devtty = 0;
v4 = _errno_location();
v5 = strerror(*v4);
warningf(0LL, "No controlling tty (open /dev/tty: %s)", v5);
v1 = isatty(0);
if ( v1 )
{
v2 = 0;
v1 = 0;
}
else
{
v2 = 2;
if ( !isatty(2) )
return warningf(0LL, "Can't find tty file descriptor");
}
}
result = fcntl(v2, 1030, 10LL);
tty_fd = result;
if ( result == -1 )
{
v6 = _errno_location();
v7 = strerror(*v6);
result = warningf(0LL, "%s: dup of tty fd failed: %s", "tty_init", v7);
}
else if ( a1 )
{
result = tcgetattr(result, &tty_state);
}
if ( v1 )
result = close(v2);
return result;
}

canonical_decompiled_code:
int __fastcall func1(int a1)
{
int v1; // ebx
int v2; // ebp
int result; // eax
int *v4; // rax
char *v5; // rax
int *v6; // rax
char *v7; // rax

v1 = 1;
func0();
tty_devtty = 1;
v2 = open("/dev/tty", 2);
if ( v2 == -1 )
{
tty_devtty = 0;
v4 = _errno_location();
v5 = strerror(*v4);
warningf(0LL, "No controlling tty (open /dev/tty: %s)", v5);
v1 = isatty(0);
if ( v1 )
{
v2 = 0;
v1 = 0;
}
else
{
v2 = 2;
if ( !isatty(2) )
return warningf(0LL, "Can't find tty file descriptor");
}
}
result = fcntl(v2, 1030, 10LL);
tty_fd = result;
if ( result == -1 )
{
v6 = _errno_location();
v7 = strerror(*v6);
result = warningf(0LL, "%s: dup of tty fd failed: %s", "tty_init", v7);
}
else if ( a1 )
{
result = tcgetattr(result, &tty_state);
}
if ( v1 )
result = close(v2);
return result;
}

original_code:
void
tty_init(int init_ttystate)
{
 int do_close = 1;
 int tfd;
 tty_close();
 tty_devtty = 1;
 tfd = open("/dev/tty", 02);
 if (tfd == -1) {
  tty_devtty = 0;
  warningf(0, "No controlling tty (open /dev/tty: %s)",
      strerror((*__errno_location ())));
  do_close = 0;
  if (isatty(0))
   tfd = 0;
  else if (isatty(2))
   tfd = 2;
  else {
   warningf(0, "Can't find tty file descriptor");
   return;
  }
 }
 if ((tty_fd = fcntl(tfd, 1030, 10)) == -1) {
  warningf(0, "%s: dup of tty fd failed: %s",
      __func__, strerror((*__errno_location ())));
 } else if (init_ttystate)
  tcgetattr(tty_fd, &tty_state);
 if (do_close)
  close(tfd);
}

variable_types: tfd int


decompiled_code:
void at_exit()
{
want_exit = 1;
}

canonical_decompiled_code:
void func7()
{
want_exit = 1;
}

original_code:
void at_exit(void) {
 want_exit = 1;
}

canonical_original_code:
void at_exit() {
 want_exit = 1;
}


output

decompiled_code:
int __fastcall output(unsigned int a1, const char *a2)
{
sem_wait(&guard_print);
printf("%2d %s\n", a1, a2);
return sem_post(&guard_print);
}

canonical_decompiled_code:
int __fastcall func8(unsigned int a1, const char *a2)
{
sem_wait(&guard_print);
printf("%2d %s\n", a1, a2);
return sem_post(&guard_print);
}

original_code:
void output(int i, char* text) {
 sem_wait(&(guard_print));
 printf("%2d %s\n", i, text);
 sem_post(&(guard_print));
}

canonical_original_code:
void output(int i, char *text) {
 sem_wait(&(guard_print));
 printf("%2d %s\n", i, text);
 sem_post(&(guard_print));
}

variable_types: i int

variable_types:
text
char *


idle

decompiled_code:
int idle()
{
return usleep(0);
}

canonical_decompiled_code:
int func9()
{
return usleep(0);
}

original_code:
void idle(int time) {
 usleep(time * (0));
}

variable_types: time int


eat

decompiled_code:
int __fastcall eat(signed int a1)
{
sem_wait(&guard_deadlock);
sem_wait((sem_t *)(32LL * a1 + 4211200));
sem_wait((sem_t *)(32LL * ((a1 + 1) % 20) + 4211200));
output(a1, "eating");
idle();
++*((_DWORD *)&stats + a1);
sem_post((sem_t *)(32LL * a1 + 4211200));
sem_post((sem_t *)(32LL * ((a1 + 1) % 20) + 4211200));
return sem_post(&guard_deadlock);
}

canonical_decompiled_code:
int __fastcall func10(signed int a1)
{
sem_wait(&guard_deadlock);
sem_wait((sem_t *)(32LL * a1 + 4211200));
sem_wait((sem_t *)(32LL * ((a1 + 1) % 20) + 4211200));
func8(a1, "eating");
func9();
++*((_DWORD *)&stats + a1);
sem_post((sem_t *)(32LL * a1 + 4211200));
sem_post((sem_t *)(32LL * ((a1 + 1) % 20) + 4211200));
return sem_post(&guard_deadlock);
}

original_code:
void eat(int i) {
 sem_wait(&guard_deadlock);
 sem_wait(&guard_fork[i]);
 sem_wait(&guard_fork[(i+1)%(20)]);
 output(i, "eating");
 idle(2);
 stats[i] += 1;
 sem_post(&guard_fork[i]);
 sem_post(&guard_fork[(i+1)%(20)]);
 sem_post(&guard_deadlock);
}


think

decompiled_code:
int __fastcall think(unsigned int a1)
{
output(a1, "thinking");
return idle();
}

canonical_decompiled_code:
int __fastcall func11(unsigned int a1)
{
func8(a1, "thinking");
return func9();
}

original_code:
void think(int i) {
 output(i, "thinking");
 idle(3);
}


dine

decompiled_code:
void __fastcall __noreturn dine(void *a1)
{
do
{
think((unsigned int)a1);
eat((signed int)a1);
}
while ( !want_exit );
pthread_exit(0LL);
}

canonical_decompiled_code:
void __fastcall __noreturn func12(void *a1)
{
do
{
func11((unsigned int)a1);
func10((signed int)a1);
}
while ( !want_exit );
pthread_exit(0LL);
}

original_code:
void dine(void* i) {
 int number = (int)i;
 while (1) {
  think(number);
  eat(number);
  if (want_exit) { pthread_exit(0); }
 }
 return;
}

canonical_original_code:
void dine(void *i) {
 int number = (int)i;
 while (1) {
  think(number);
  eat(number);
  if (want_exit) { pthread_exit(0); }
 }
 return;
}

variable_types:
i
void *

variable_types: number int


decompiled_code:
int print_statistics()
{
__int128 *v0; // rax
__int64 i; // rbp
int v2; // ebx
int result; // eax
float v4; // [rsp+Ch] [rbp-1Ch]

puts("\nStatistics\n==========");
v4 = 0.0;
v0 = &stats;
do
{
v0 = (__int128 *)((char *)v0 + 4);
v4 = fmaxf((float)*((int *)v0 - 1), v4);
}
while ( v0 != (__int128 *)&unk_4041D0 );
for ( i = 0LL; i != 20; ++i )
{
v2 = 0;
printf("%2d: %5d ", (unsigned int)i, *((unsigned int *)&stats + i));
if ( (float)((float)(100 * *((_DWORD *)&stats + i)) / v4) > 0.0 )
{
do
{
++v2;
putchar(43);
}
while ( (float)((float)(100 * *((_DWORD *)&stats + i)) / v4) > (float)v2 );
}
result = putchar(10);
}
return result;
}

canonical_decompiled_code:
int func13()
{
__int128 *v0; // rax
__int64 i; // rbp
int v2; // ebx
int result; // eax
float v4; // [rsp+Ch] [rbp-1Ch]

puts("\nStatistics\n==========");
v4 = 0.0;
v0 = &stats;
do
{
v0 = (__int128 *)((char *)v0 + 4);
v4 = fmaxf((float)*((int *)v0 - 1), v4);
}
while ( v0 != (__int128 *)&unk_4041D0 );
for ( i = 0LL; i != 20; ++i )
{
v2 = 0;
printf("%2d: %5d ", (unsigned int)i, *((unsigned int *)&stats + i));
if ( (float)((float)(100 * *((_DWORD *)&stats + i)) / v4) > 0.0 )
{
do
{
++v2;
putchar(43);
}
while ( (float)((float)(100 * *((_DWORD *)&stats + i)) / v4) > (float)v2 );
}
result = putchar(10);
}
return result;
}

original_code:
void print_statistics() {
 float max = 0;
 int c = 0;
  int i = 0;
  printf("\nStatistics\n==========\n");
 for (i=0; i<(20); i++) { if (max < stats[i]) { max = stats[i]; } }
 for (i=0; i<(20); i++) {
  printf("%2d: %5d ", i, stats[i]);
  for (c=0; c< 100 * stats[i]/max ; c++) { printf("+"); }
  printf("\n");
 }
}

variable_types: max float

variable_types: c int


main

canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
sem_t *v3; // rbx
sem_t *v4; // rdi
__int128 *v5; // rbp
char *v6; // rbx
pthread_t *v7; // r12
pthread_t v8; // rdi

v3 = &guard_fork;
__sysv_signal(2, at_exit);
sem_init(&guard_print, 0, 1u);
sem_init(&guard_deadlock, 0, 0x13u);
do
{
v4 = v3++;
sem_init(v4, 0, 1u);
}
while ( v3 != &guard_print );
v5 = (__int128 *)&dining_philosopher;
v6 = 0LL;
stats = 0LL;
v7 = &dining_philosopher;
xmmword_404190 = 0LL;
xmmword_4041A0 = 0LL;
xmmword_4041B0 = 0LL;
xmmword_4041C0 = 0LL;
do
pthread_create(v7++, 0LL, dine, v6++);
while ( v6 != (char *)20 );
do
{
v8 = *(_QWORD *)v5;
v5 = (__int128 *)((char *)v5 + 8);
pthread_join(v8, 0LL);
}
while ( v5 != &stats );
print_statistics();
exit(0);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
sem_t *v3; // rbx
sem_t *v4; // rdi
__int128 *v5; // rbp
char *v6; // rbx
pthread_t *v7; // r12
pthread_t v8; // rdi

v3 = &guard_fork;
__sysv_signal(2, at_exit);
sem_init(&guard_print, 0, 1u);
sem_init(&guard_deadlock, 0, 0x13u);
do
{
v4 = v3++;
sem_init(v4, 0, 1u);
}
while ( v3 != &guard_print );
v5 = (__int128 *)&dining_philosopher;
v6 = 0LL;
stats = 0LL;
v7 = &dining_philosopher;
xmmword_404190 = 0LL;
xmmword_4041A0 = 0LL;
xmmword_4041B0 = 0LL;
xmmword_4041C0 = 0LL;
do
pthread_create(v7++, 0LL, dine, v6++);
while ( v6 != (char *)20 );
do
{
v8 = *(_QWORD *)v5;
v5 = (__int128 *)((char *)v5 + 8);
pthread_join(v8, 0LL);
}
while ( v5 != &stats );
func13();
exit(0);
}

original_code:
int main(int argc, char** argv) {
 signal(2, (void*)at_exit);
 int i = 0;
 sem_init(&guard_print, 0, 1);
 sem_init(&guard_deadlock, 0, (20)-1);
 for (i=0; i<(20); i++) { sem_init(&guard_fork[i], 0, 1); }
 for (i=0; i<(20); i++) { stats[i] = 0; }
 for (i=0; i<(20); i++) {
  pthread_create(&dining_philosopher[i], 0, (void*)dine, (void*)i);
 }
  for (i=0; i<(20); i++) { pthread_join(dining_philosopher[i], ((void *)0)); }
  print_statistics();
  exit(0);
}

canonical_original_code:
int main(int argc, char **argv) {
 signal(2, (void *)at_exit);
 int i = 0;
 sem_init(&guard_print, 0, 1);
 sem_init(&guard_deadlock, 0, (20)-1);
 for (i=0; i<(20); i++) { sem_init(&guard_fork[i], 0, 1); }
 for (i=0; i<(20); i++) { stats[i] = 0; }
 for (i=0; i<(20); i++) {
  pthread_create(&dining_philosopher[i], 0, (void *)dine, (void *)i);
 }
  for (i=0; i<(20); i++) { pthread_join(dining_philosopher[i], ((void *)0)); }
  print_statistics();
  exit(0);
}

variable_types: argc int

variable_types:
argv
char **


compar

decompiled_code:
__int64 __fastcall compar(const void *a1, const void *a2)
{
__int64 result; // rax

result = *(_DWORD *)a1 > *(_DWORD *)a2;
if ( *(_DWORD *)a1 < *(_DWORD *)a2 )
result = 0xFFFFFFFFLL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(const void *a1, const void *a2)
{
__int64 result; // rax

result = *(_DWORD *)a1 > *(_DWORD *)a2;
if ( *(_DWORD *)a1 < *(_DWORD *)a2 )
result = 0xFFFFFFFFLL;
return result;
}

original_code:
int compar(const void* a, const void* b) {
 return (*(int*)a < *(int*)b) ? -1 : (*(int*)a > *(int*)b);
}

canonical_original_code:
int compar(void *a, void *b) {
 return (*(int *)a < *(int *)b) ? -1 : (*(int *)a > *(int *)b);
}

variable_types:
a
void *

variable_types:
b
void *


decompiled_code:
void __fastcall read_all(int fd, __int64 a2, size_t a3)
{
int v4; // er12
__int64 v6; // rsi
int v7; // eax

if ( a3 )
{
v4 = 0;
v6 = 0LL;
do
{
v7 = read(fd, (void *)(a2 + v6), a3);
if ( v7 <= 0 )
exit(255);
v4 += v7;
v6 = v4;
}
while ( v4 < a3 );
}
}

canonical_decompiled_code:
void __fastcall func8(int fd, __int64 a2, size_t a3)
{
int v4; // er12
__int64 v6; // rsi
int v7; // eax

if ( a3 )
{
v4 = 0;
v6 = 0LL;
do
{
v7 = read(fd, (void *)(a2 + v6), a3);
if ( v7 <= 0 )
exit(255);
v4 += v7;
v6 = v4;
}
while ( v4 < a3 );
}
}

original_code:
void read_all(int fd, void *buf, size_t nbyte) {
 int r = 0; int res = 0;
 while (r < nbyte) {
  res = read(fd, buf + r, nbyte);
  if (res > 0) {
   r += res;
  } else {
   exit(255);
  }
 }
}

canonical_original_code:
void read_all(int fd, void *buf, unsigned long nbyte) {
 int r = 0; int res = 0;
 while (r < nbyte) {
  res = read(fd, buf + r, nbyte);
  if (res > 0) {
   r += res;
  } else {
   exit(255);
  }
 }
}

variable_types: fd int

variable_types:
buf
void *

variable_types:
nbyte
unsigned long

variable_types: r int

variable_types: res int


sort

decompiled_code:
void __fastcall sort(void *a1, int a2)
{
qsort(a1, a2, 4uLL, (__compar_fn_t)compar);
}

canonical_decompiled_code:
void __fastcall func9(void *a1, int a2)
{
qsort(a1, a2, 4uLL, (__compar_fn_t)compar);
}

original_code:
void sort(int field[], int nelem) {
 qsort(field, nelem, sizeof(int), compar);
}

variable_types:
field
int []

variable_types: nelem int


merge

original_code:
void merge(int in[], int count) {
 int tmp[count];
 int center = count/2;
 int i = 0;
 int j = 0 + center;
 int k = 0;
 while (i < center && j < count) {
  if (in[i] < in[j]) {
   tmp[k] = in[i]; i++;
  } else {
   tmp[k] = in[j]; j++;
  }
  k++;
 }
 while (i < center) { tmp[k] = in[i]; i++; k++; }
 while (j < count) { tmp[k] = in[j]; j++; k++; }
 memcpy(in, tmp, sizeof(int)*count);
}

canonical_original_code:
void merge(int in[], int count) {
 int tmp[-1];
 int center = count/2;
 int i = 0;
 int j = 0 + center;
 int k = 0;
 while (i < center && j < count) {
  if (in[i] < in[j]) {
   tmp[k] = in[i]; i++;
  } else {
   tmp[k] = in[j]; j++;
  }
  k++;
 }
 while (i < center) { tmp[k] = in[i]; i++; k++; }
 while (j < count) { tmp[k] = in[j]; j++; k++; }
 memcpy(in, tmp, sizeof(int)*count);
}

variable_types:
in
int []

variable_types: count int

variable_types:
tmp
int [-1]

variable_types: center int

variable_types: i int

variable_types: j int

variable_types: k int


decompiled_code:
void __fastcall forking_mergesort(void *dest, int a2)
{
__pid_t v2; // eax
__pid_t v3; // er13
int v4; // er15
__int64 v5; // r14
int stat_loc; // [rsp+0h] [rbp-44h] BYREF
int pipedes[2]; // [rsp+4h] [rbp-40h] BYREF

if ( a2 > 1 )
{
pipe(pipedes);
v2 = fork();
v3 = v2;
if ( v2 < 0 )
{
perror("fork");
exit(0);
}
v4 = a2 >> 1;
v5 = 4LL * (a2 >> 1);
if ( !v2 )
{
close(pipedes[0]);
sort((char *)dest + v5, a2 - a2 / 2);
write(pipedes[1], (char *)dest + v5, 4LL * v4);
close(pipedes[1]);
exit(0);
}
close(pipedes[1]);
sort(dest, v4);
read_all(pipedes[0], (__int64)dest + v5, 4LL * v4);
waitpid(v3, &stat_loc, 0);
close(pipedes[0]);
merge(dest, a2);
}
}

canonical_decompiled_code:
void __fastcall func11(void *dest, int a2)
{
__pid_t v2; // eax
__pid_t v3; // er13
int v4; // er15
__int64 v5; // r14
int stat_loc; // [rsp+0h] [rbp-44h] BYREF
int pipedes[2]; // [rsp+4h] [rbp-40h] BYREF

if ( a2 > 1 )
{
pipe(pipedes);
v2 = fork();
v3 = v2;
if ( v2 < 0 )
{
perror("fork");
exit(0);
}
v4 = a2 >> 1;
v5 = 4LL * (a2 >> 1);
if ( !v2 )
{
close(pipedes[0]);
func9((char *)dest + v5, a2 - a2 / 2);
write(pipedes[1], (char *)dest + v5, 4LL * v4);
close(pipedes[1]);
exit(0);
}
close(pipedes[1]);
func9(dest, v4);
func8(pipedes[0], (__int64)dest + v5, 4LL * v4);
waitpid(v3, &stat_loc, 0);
close(pipedes[0]);
func10(dest, a2);
}
}

original_code:
void forking_mergesort(int field[], int nelem) {
 int fd[2];
 int pid;
 int stat;
 if (nelem <= 1) { return; }
 pipe(fd);
 if ((pid = fork()) < 0) {
  perror("fork");
  exit(0);
 } else {
  if (pid > 0) {
   close(fd[1]);
   sort(field, nelem/2);
   read_all(fd[0], field+nelem/2, nelem/2 * sizeof(int));
   waitpid(pid, &stat, 0);
   close(fd[0]);
  } else {
   close(fd[0]);
   sort(field+nelem/2, nelem - nelem/2);
   write(fd[1], field+nelem/2, nelem/2 * sizeof(int));
   close(fd[1]);
   exit(0);
  }
  merge(field, nelem);
 }
}

variable_types:
fd
int [2]

variable_types: pid int

variable_types: stat int


main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
char *v4; // rbx
char *v5; // r12
__int64 v6; // rsi
_BYTE v7[400]; // [rsp+0h] [rbp-1A8h] BYREF
char v8[24]; // [rsp+190h] [rbp-18h] BYREF

v3 = time(0LL);
v4 = v7;
v5 = v7;
srand(v3);
do
{
v5 += 4;
*((_DWORD *)v5 - 1) = rand() % 1000;
}
while ( v5 != v8 );
forking_mergesort(v7);
do
{
v6 = *(unsigned int *)v4;
v4 += 4;
printf("%i ", v6);
}
while ( v4 != v8 );
putchar(10);
exit(0);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
char *v4; // rbx
char *v5; // r12
__int64 v6; // rsi
_BYTE v7[400]; // [rsp+0h] [rbp-1A8h] BYREF
char v8[24]; // [rsp+190h] [rbp-18h] BYREF

v3 = time(0LL);
v4 = v7;
v5 = v7;
srand(v3);
do
{
v5 += 4;
*((_DWORD *)v5 - 1) = rand() % 1000;
}
while ( v5 != v8 );
func11(v7);
do
{
v6 = *(unsigned int *)v4;
v4 += 4;
printf("%i ", v6);
}
while ( v4 != v8 );
putchar(10);
exit(0);
}

original_code:
int main(int argc, char* argv[]) {
 int field[100];
 srand(time(((void *)0)));
 int i;
 for (i=0; i<100; i++) { field[i] = rand() % 1000; }
 forking_mergesort(field, 100);
 for (i=0; i<100; i++) { printf("%i ", field[i]); }
 printf("\n");
 exit(0);
}

canonical_original_code:
int main(int argc, char *argv[]) {
 int field[100];
 srand(time(((void *)0)));
 int i;
 for (i=0; i<100; i++) { field[i] = rand() % 1000; }
 forking_mergesort(field, 100);
 for (i=0; i<100; i++) { printf("%i ", field[i]); }
 printf("\n");
 exit(0);
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
field
int [100]


reader

decompiled_code:
__int64 __fastcall reader(void *a1)
{
signed int v1; // ebx
unsigned int v2; // er12
int v3; // ebp

v1 = 0;
v2 = file_open_read(a1);
do
{
sem_wait(&buf_full);
v3 = file_read(v2, 1588LL * v1 + 4216832, 1588LL);
buffill[v1] = v3;
v1 = (v1 + 1) % 0x2DFu;
sem_post(&buf_empty);
}
while ( v3 > 0 );
return file_close(v2);
}

canonical_decompiled_code:
__int64 __fastcall func7(void *a1)
{
signed int v1; // ebx
unsigned int v2; // er12
int v3; // ebp

v1 = 0;
v2 = func11(a1);
do
{
sem_wait(&buf_full);
v3 = func9(v2, 1588LL * v1 + 4216832, 1588LL);
buffill[v1] = v3;
v1 = (v1 + 1) % 0x2DFu;
sem_post(&buf_empty);
}
while ( v3 > 0 );
return func13(v2);
}

original_code:
void reader(char* srcpath) {
  int srcfd = file_open_read(srcpath);
  int bufpos = 0;
  int rcount;
  do {
    sem_wait(&buf_full);
    rcount = file_read(srcfd, buffer[bufpos], sizeof(buffer[bufpos]));
    buffill[bufpos] = rcount;
    bufpos = (bufpos + 1) % (735);
    sem_post(&buf_empty);
  } while(rcount > 0);
  file_close(srcfd);
}

canonical_original_code:
void reader(char *srcpath) {
  int srcfd = file_open_read(srcpath);
  int bufpos = 0;
  int rcount;
  do {
    sem_wait(&buf_full);
    rcount = file_read(srcfd, buffer[bufpos], sizeof(buffer[bufpos]));
    buffill[bufpos] = rcount;
    bufpos = (bufpos + 1) % (735);
    sem_post(&buf_empty);
  } while(rcount > 0);
  file_close(srcfd);
}

variable_types:
srcpath
char *

variable_types: srcfd int

variable_types: bufpos int

variable_types: rcount int


writer

decompiled_code:
__int64 __fastcall writer(void *a1)
{
unsigned int v1; // ebx
unsigned int v2; // ebp
int v3; // er13

v1 = 0;
v2 = file_open_write(a1);
do
{
sem_wait(&buf_empty);
v3 = file_write(v2);
v1 = (v1 + 1) % 0x2DF;
sem_post(&buf_full);
}
while ( v3 > 0 );
return file_close(v2);
}

canonical_decompiled_code:
__int64 __fastcall func8(void *a1)
{
unsigned int v1; // ebx
unsigned int v2; // ebp
int v3; // er13

v1 = 0;
v2 = func12(a1);
do
{
sem_wait(&buf_empty);
v3 = func10(v2);
v1 = (v1 + 1) % 0x2DF;
sem_post(&buf_full);
}
while ( v3 > 0 );
return func13(v2);
}

original_code:
void writer(char* destpath) {
  int destfd = file_open_write(destpath);
  int bufpos = 0;
  int wcount;
  do {
    sem_wait(&buf_empty);
    wcount = file_write(destfd, buffer[bufpos], buffill[bufpos]);
    bufpos = (bufpos + 1) % (735);
    sem_post(&buf_full);
  } while(wcount > 0);
  file_close(destfd);
}

canonical_original_code:
void writer(char *destpath) {
  int destfd = file_open_write(destpath);
  int bufpos = 0;
  int wcount;
  do {
    sem_wait(&buf_empty);
    wcount = file_write(destfd, buffer[bufpos], buffill[bufpos]);
    bufpos = (bufpos + 1) % (735);
    sem_post(&buf_full);
  } while(wcount > 0);
  file_close(destfd);
}

variable_types:
destpath
char *

variable_types: destfd int

variable_types: wcount int


decompiled_code:
ssize_t __fastcall file_read(int a1, void *a2, size_t a3)
{
ssize_t result; // rax

result = read(a1, a2, a3);
if ( result < 0 )
{
perror("error while reading from source file");
exit(3);
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func9(int a1, void *a2, size_t a3)
{
ssize_t result; // rax

result = read(a1, a2, a3);
if ( result < 0 )
{
perror("error while reading from source file");
exit(3);
}
return result;
}

original_code:
ssize_t file_read(int fd, void* buf, size_t count) {
  ssize_t n = read(fd, buf, count);
  if (n < 0) {
    perror("error while reading from source file");
    exit(3);
  }
  return n;
}

canonical_original_code:
long file_read(int fd, void *buf, unsigned long count) {
  long n = read(fd, buf, count);
  if (n < 0) {
    perror("error while reading from source file");
    exit(3);
  }
  return n;
}

variable_types: fd int

variable_types:
buf
void *

variable_types:
count
unsigned long

variable_types: n long

return_type: long