getY

decompiled_code:
__int64 getY()
{
return (unsigned int)y;
}

canonical_decompiled_code:
__int64 func1()
{
return (unsigned int)y;
}

original_code: int getY () { return y; }

canonical_original_code: int getY() { return y; }


hexdump

repo: walimis/usbdump

original_code:
void
hexdump(char *linebuf, void *address, int length)
{
 int n, i;
 unsigned char *buf;
 n = strlen(linebuf);
 buf = address;
 for (i = 0; i < length; i++) {
  if (n > 16383 - 4)
   continue;
  if (i && !(i & 0x01)) {
   linebuf[n++] = ' ';
   linebuf[n] = '\0';
  }
  if (i && !(i & 0x0f)) {
   linebuf[n++] = ' ';
   linebuf[n] = '\0';
  }
  snprintf(linebuf + n, 16383 - n, "%.2x", buf[i]);
  n += 2;
 }
}

canonical_original_code:
void hexdump(char *linebuf, void *address, int length)
{
 int n; int i;
 unsigned char *buf;
 n = strlen(linebuf);
 buf = address;
 for (i = 0; i < length; i++) {
  if (n > 16383 - 4)
   continue;
  if (i && !(i & 0x01)) {
   linebuf[n++] = ' ';
   linebuf[n] = '\0';
  }
  if (i && !(i & 0x0f)) {
   linebuf[n++] = ' ';
   linebuf[n] = '\0';
  }
  snprintf(linebuf + n, 16383 - n, "%.2x", buf[i]);
  n += 2;
 }
}

variable_types:
linebuf
char *

variable_types:
address
void *

variable_types: length int

variable_types: n int

variable_types: i int

variable_types:
buf
unsigned char *

return_type: void


decompiled_code:
void __fastcall process_packet(__int64 a1, __int64 a2)
{
char v3; // r14
char v4; // al
__int64 v5; // r12
int v6; // er15
char *v7; // rax
__int64 v8; // r13
char *v9; // rbp
__int64 v10; // r13
unsigned int v11; // er12
unsigned int v12; // er14
size_t v13; // rax
size_t v14; // rsi
char *v15; // rdi
int v16; // ebx
int v17; // er14
const char **v18; // r15
__int64 v19; // rsi
__int64 v20; // rbx
void *v21; // rdi
char **v22; // rbx
char *v23; // rax
unsigned int v25; // [rsp+Ch] [rbp-3Ch]

v3 = *(_BYTE *)(a1 + 10);
if ( *(_BYTE *)(a1 + 9) == 2 )
{
LABEL_4:
v5 = start_ts;
v6 = start_ts_us;
if ( !start_ts && !start_ts_us )
{
v5 = *(_QWORD *)(a1 + 16);
v6 = *(_DWORD *)(a1 + 24);
start_ts = v5;
start_ts_us = v6;
}
v7 = (char *)malloc(0x4000uLL);
v8 = *(_QWORD *)(a1 + 16);
*v7 = 0;
v9 = v7;
v10 = v8 - v5;
v11 = *(_DWORD *)(a1 + 24) - v6;
if ( (v11 & 0x80000000) != 0 )
{
--v10;
v11 += 1000000;
}
if ( v3 < 0 )
snprintf(v7, 0x3FFFuLL, "%d<-- ", v3 & 0x7F);
else
snprintf(v7, 0x3FFFuLL, "-->%d ", (unsigned __int8)v3);
v12 = *(_DWORD *)(a1 + 36);
if ( v12 )
{
v25 = *(_DWORD *)(a1 + 32);
v13 = strlen(v9);
v14 = 0x3FFF - v13;
v15 = &v9[v13];
if ( v12 == v25 )
snprintf(v15, v14, "%d: ", v12);
else
snprintf(v15, v14, "%d/%d: ", v12, v25);
hexdump(v9, a2, *(_DWORD *)(a1 + 36));
}
v16 = opt_unique_num;
if ( opt_unique_num )
{
v17 = unique_cnt_1;
v18 = (const char **)(lines + 8LL * unique_cnt_1);
if ( unique_ready_2 )
{
v19 = (unsigned int)(unique_cnt_1 + 1);
if ( strcmp(v9, *v18) )
{
if ( unique_setcnt_0 > 1 )
{
putchar(10);
v17 = unique_cnt_1;
}
v20 = 0LL;
if ( v17 > 0 )
{
do
{
printf("%3ld.%.6d %s\n", v10, v11, *(const char **)(lines + 8 * v20));
v21 = *(void **)(lines + 8 * v20++);
free(v21);
}
while ( unique_cnt_1 > (int)v20 );
}
printf("%3ld.%.6d %s\n", v10, v11, v9);
v22 = (char **)lines;
v23 = strdup(v9);
v19 = 1LL;
unique_setcnt_0 = 0;
unique_ready_2 = 0;
*v22 = v23;
v16 = opt_unique_num;
}
unique_cnt_1 = v19;
if ( (_DWORD)v19 != v16 )
goto LABEL_24;
goto LABEL_30;
}
*v18 = strdup(v9);
printf("%3ld.%.6d %s\n", v10, v11, v9);
v19 = (unsigned int)(unique_cnt_1 + 1);
unique_cnt_1 = v19;
if ( (_DWORD)v19 == opt_unique_num )
{
LABEL_30:
if ( unique_ready_2 )
{
printf("last %d lines repeated %d times\r", v19, (unsigned int)unique_setcnt_0);
fflush(stdout);
}
++unique_setcnt_0;
unique_ready_2 = 1;
unique_cnt_1 = 0;
goto LABEL_24;
}
}
else
{
printf("%3ld.%.6d %s\n", v10, v11, v9);
}
LABEL_24:
free(v9);
return;
}
v4 = *(_BYTE *)(a1 + 8);
if ( v3 < 0 )
{
if ( v4 != 83 )
goto LABEL_4;
}
else if ( v4 != 67 )
{
goto LABEL_4;
}
}

canonical_decompiled_code:
void __fastcall func1(__int64 a1, __int64 a2)
{
char v3; // r14
char v4; // al
__int64 v5; // r12
int v6; // er15
char *v7; // rax
__int64 v8; // r13
char *v9; // rbp
__int64 v10; // r13
unsigned int v11; // er12
unsigned int v12; // er14
size_t v13; // rax
size_t v14; // rsi
char *v15; // rdi
int v16; // ebx
int v17; // er14
const char **v18; // r15
__int64 v19; // rsi
__int64 v20; // rbx
void *v21; // rdi
char **v22; // rbx
char *v23; // rax
unsigned int v25; // [rsp+Ch] [rbp-3Ch]

v3 = *(_BYTE *)(a1 + 10);
if ( *(_BYTE *)(a1 + 9) == 2 )
{
LABEL_4:
v5 = start_ts;
v6 = start_ts_us;
if ( !start_ts && !start_ts_us )
{
v5 = *(_QWORD *)(a1 + 16);
v6 = *(_DWORD *)(a1 + 24);
start_ts = v5;
start_ts_us = v6;
}
v7 = (char *)malloc(0x4000uLL);
v8 = *(_QWORD *)(a1 + 16);
*v7 = 0;
v9 = v7;
v10 = v8 - v5;
v11 = *(_DWORD *)(a1 + 24) - v6;
if ( (v11 & 0x80000000) != 0 )
{
--v10;
v11 += 1000000;
}
if ( v3 < 0 )
snprintf(v7, 0x3FFFuLL, "%d<-- ", v3 & 0x7F);
else
snprintf(v7, 0x3FFFuLL, "-->%d ", (unsigned __int8)v3);
v12 = *(_DWORD *)(a1 + 36);
if ( v12 )
{
v25 = *(_DWORD *)(a1 + 32);
v13 = strlen(v9);
v14 = 0x3FFF - v13;
v15 = &v9[v13];
if ( v12 == v25 )
snprintf(v15, v14, "%d: ", v12);
else
snprintf(v15, v14, "%d/%d: ", v12, v25);
func0(v9, a2, *(_DWORD *)(a1 + 36));
}
v16 = opt_unique_num;
if ( opt_unique_num )
{
v17 = unique_cnt_1;
v18 = (const char **)(lines + 8LL * unique_cnt_1);
if ( unique_ready_2 )
{
v19 = (unsigned int)(unique_cnt_1 + 1);
if ( strcmp(v9, *v18) )
{
if ( unique_setcnt_0 > 1 )
{
putchar(10);
v17 = unique_cnt_1;
}
v20 = 0LL;
if ( v17 > 0 )
{
do
{
printf("%3ld.%.6d %s\n", v10, v11, *(const char **)(lines + 8 * v20));
v21 = *(void **)(lines + 8 * v20++);
free(v21);
}
while ( unique_cnt_1 > (int)v20 );
}
printf("%3ld.%.6d %s\n", v10, v11, v9);
v22 = (char **)lines;
v23 = strdup(v9);
v19 = 1LL;
unique_setcnt_0 = 0;
unique_ready_2 = 0;
*v22 = v23;
v16 = opt_unique_num;
}
unique_cnt_1 = v19;
if ( (_DWORD)v19 != v16 )
goto LABEL_24;
goto LABEL_30;
}
*v18 = strdup(v9);
printf("%3ld.%.6d %s\n", v10, v11, v9);
v19 = (unsigned int)(unique_cnt_1 + 1);
unique_cnt_1 = v19;
if ( (_DWORD)v19 == opt_unique_num )
{
LABEL_30:
if ( unique_ready_2 )
{
printf("last %d lines repeated %d times\r", v19, (unsigned int)unique_setcnt_0);
fflush(stdout);
}
++unique_setcnt_0;
unique_ready_2 = 1;
unique_cnt_1 = 0;
goto LABEL_24;
}
}
else
{
printf("%3ld.%.6d %s\n", v10, v11, v9);
}
LABEL_24:
free(v9);
return;
}
v4 = *(_BYTE *)(a1 + 8);
if ( v3 < 0 )
{
if ( v4 != 83 )
goto LABEL_4;
}
else if ( v4 != 67 )
{
goto LABEL_4;
}
}

original_code:
void
process_packet(struct usbmon_packet *hdr, char *data)
{
 static int unique_ready = 0, unique_cnt = 0, unique_setcnt = 0, cnt =
  0;
 int i;
 int64_t ts;
 int32_t ts_us;
 char *linebuf;
 if (hdr->xfer_type != 2) {
  if (hdr->epnum & 0x80) {
   if (hdr->type == 'S')
    return;
  }
  else {
   if (hdr->type == 'C')
    return;
  }
 }
 if (!start_ts && !start_ts_us) {
  start_ts = hdr->ts_sec;
  start_ts_us = hdr->ts_usec;
 }
 linebuf = malloc(16383 + 1);
 linebuf[0] = '\0';
 ts = hdr->ts_sec - start_ts;
 ts_us = hdr->ts_usec - start_ts_us;
 if (ts_us < 0) {
  ts -= 1;
  ts_us = 1000000 + ts_us;
 }
 if (hdr->epnum & 0x80)
  snprintf(linebuf + strlen(linebuf),
    16383 - strlen(linebuf), "%d<-- ",
    hdr->epnum & 0x7f);
 else
  snprintf(linebuf + strlen(linebuf),
    16383 - strlen(linebuf), "-->%d ",
    hdr->epnum & 0x7f);
 if (hdr->len_cap > 0) {
  if (hdr->len_cap == hdr->length)
   snprintf(linebuf + strlen(linebuf),
     16383 - strlen(linebuf), "%d: ",
     hdr->len_cap);
  else
   snprintf(linebuf + strlen(linebuf),
     16383 - strlen(linebuf), "%d/%d: ",
     hdr->len_cap, hdr->length);
  hexdump(linebuf, data, hdr->len_cap);
 }
 if (opt_unique_num) {
  if (!unique_ready) {
   lines[unique_cnt] = strdup(linebuf);
   printf("%3" "l" "d" ".%.6d %s\n", ts, ts_us, linebuf);
  }
  else if (strcmp(linebuf, lines[unique_cnt])) {
   if (unique_setcnt > 1)
    printf("\n");
   for (i = 0; i < unique_cnt; i++) {
    printf("%3" "l" "d" ".%.6d %s\n", ts, ts_us,
           lines[i]);
    free(lines[i]);
   }
   printf("%3" "l" "d" ".%.6d %s\n", ts, ts_us, linebuf);
   lines[0] = strdup(linebuf);
   unique_cnt = 0;
   unique_setcnt = 0;
   unique_ready = 0;
  }
  if (++unique_cnt == opt_unique_num) {
   if (unique_ready) {
    printf("last %d lines repeated %d times\r",
           opt_unique_num, unique_setcnt);
    fflush(stdout);
   }
   unique_ready = 1;
   unique_cnt = 0;
   unique_setcnt++;
  }
 }
 else
  printf("%3" "l" "d" ".%.6d %s\n", ts, ts_us, linebuf);
 free(linebuf);
}

canonical_original_code:
void process_packet(struct usbmon_packet *hdr, char *data)
{
 static int unique_ready = 0; static int unique_cnt = 0; static int unique_setcnt = 0; static int cnt = 0;
 int i;
 long ts;
 int ts_us;
 char *linebuf;
 if (hdr->xfer_type != 2) {
  if (hdr->epnum & 0x80) {
   if (hdr->type == 'S')
    return;
  }
  else {
   if (hdr->type == 'C')
    return;
  }
 }
 if (!start_ts && !start_ts_us) {
  start_ts = hdr->ts_sec;
  start_ts_us = hdr->ts_usec;
 }
 linebuf = malloc(16383 + 1);
 linebuf[0] = '\0';
 ts = hdr->ts_sec - start_ts;
 ts_us = hdr->ts_usec - start_ts_us;
 if (ts_us < 0) {
  ts -= 1;
  ts_us = 1000000 + ts_us;
 }
 if (hdr->epnum & 0x80)
  snprintf(linebuf + strlen(linebuf),
    16383 - strlen(linebuf), "%d<-- ",
    hdr->epnum & 0x7f);
 else
  snprintf(linebuf + strlen(linebuf),
    16383 - strlen(linebuf), "-->%d ",
    hdr->epnum & 0x7f);
 if (hdr->len_cap > 0) {
  if (hdr->len_cap == hdr->length)
   snprintf(linebuf + strlen(linebuf),
     16383 - strlen(linebuf), "%d: ",
     hdr->len_cap);
  else
   snprintf(linebuf + strlen(linebuf),
     16383 - strlen(linebuf), "%d/%d: ",
     hdr->len_cap, hdr->length);
  hexdump(linebuf, data, hdr->len_cap);
 }
 if (opt_unique_num) {
  if (!unique_ready) {
   lines[unique_cnt] = strdup(linebuf);
   printf("%3" "l" "d" ".%.6d %s\n", ts, ts_us, linebuf);
  }
  else if (strcmp(linebuf, lines[unique_cnt])) {
   if (unique_setcnt > 1)
    printf("\n");
   for (i = 0; i < unique_cnt; i++) {
    printf("%3" "l" "d" ".%.6d %s\n", ts, ts_us,
           lines[i]);
    free(lines[i]);
   }
   printf("%3" "l" "d" ".%.6d %s\n", ts, ts_us, linebuf);
   lines[0] = strdup(linebuf);
   unique_cnt = 0;
   unique_setcnt = 0;
   unique_ready = 0;
  }
  if (++unique_cnt == opt_unique_num) {
   if (unique_ready) {
    printf("last %d lines repeated %d times\r",
           opt_unique_num, unique_setcnt);
    fflush(stdout);
   }
   unique_ready = 1;
   unique_cnt = 0;
   unique_setcnt++;
  }
 }
 else
  printf("%3" "l" "d" ".%.6d %s\n", ts, ts_us, linebuf);
 free(linebuf);
}

variable_types: struct usbmon_packet { unsigned long id; unsigned char type; unsigned char xfer_type; unsigned char epnum; unsigned char devnum; unsigned short busnum; char flag_setup; char flag_data; long ts_sec; int ts_usec; int status; unsigned int length; unsigned int len_cap; union { unsigned char setup[8]; struct iso_rec { int error_count; int numdesc; } iso; } s; int interval; int start_frame; unsigned int xfer_flags; unsigned int ndesc; } *

variable_types:
data
char *

variable_types: cnt int

variable_types: ts long

user_defined_types: struct usbmon_packet { unsigned long id; unsigned char type; unsigned char xfer_type; unsigned char epnum; unsigned char devnum; unsigned short busnum; char flag_setup; char flag_data; long ts_sec; int ts_usec; int status; unsigned int length; unsigned int len_cap; union { unsigned char setup[8]; struct iso_rec iso; } s; int interval; int start_frame; unsigned int xfer_flags; unsigned int ndesc; }


decompiled_code:
int __fastcall usb_sniff(unsigned int a1, int a2)
{
int v3; // eax
int v4; // er13
int v5; // eax
unsigned int v6; // er15
_BYTE *v7; // rbp
int v8; // edx
__int64 v9; // rax
__int64 v10; // rcx
__int64 v11; // rdi
int *v12; // rax
char *v13; // rax
int result; // eax
int *v15; // rax
char *v16; // rax
int *v17; // rax
char *v18; // rax
int v19; // [rsp+4h] [rbp-114h]
__int64 v20; // [rsp+8h] [rbp-110h]
_DWORD *v21; // [rsp+10h] [rbp-108h] BYREF
unsigned int v22; // [rsp+18h] [rbp-100h]
int v23; // [rsp+1Ch] [rbp-FCh]
char s[64]; // [rsp+20h] [rbp-F8h] BYREF
_DWORD v25[46]; // [rsp+60h] [rbp-B8h] BYREF

snprintf(s, 0x3FuLL, "%s%d", "/dev/usbmon", a1);
v3 = open(s, 0);
if ( v3 == -1 )
{
v15 = _errno_location();
v16 = strerror(*v15);
result = printf("unable to open %s: %s\n", s, v16);
}
else
{
v4 = v3;
v5 = ioctl(v3, 0x9205uLL);
v6 = v5;
if ( v5 <= 0 )
{
v12 = _errno_location();
v13 = strerror(*v12);
result = printf("failed to determine kernel USB buffer size: %s\n", v13);
}
else
{
v7 = mmap(0LL, v5, 1, 1, v4, 0LL);
if ( v7 != (_BYTE *)-1LL )
{
if ( opt_unique_num )
lines = (__int64)malloc(8LL * opt_unique_num);
v8 = 0;
while ( 1 )
{
do
{
v23 = v8;
v21 = v25;
v22 = 32;
ioctl(v4, 0xC0109207uLL, &v21);
v8 = v22;
}
while ( !v22 );
v9 = 0LL;
do
{
v10 = (unsigned int)v25[v9];
v11 = (__int64)&v7[v10];
if ( v7[v10 + 8] != 64
&& (!a1 || *(unsigned __int16 *)(v11 + 12) == a1)
&& (!a2 || *(unsigned __int8 *)(v11 + 11) == a2) )
{
v20 = v9;
v19 = v8;
process_packet(v11, (__int64)&v7[v10 + 64]);
v9 = v20;
v8 = v19;
}
++v9;
}
while ( v22 > (unsigned int)v9 );
}
}
v17 = _errno_location();
v18 = strerror(*v17);
result = printf("unable to mmap %d bytes: %s\n", v6, v18);
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func2(unsigned int a1, int a2)
{
int v3; // eax
int v4; // er13
int v5; // eax
unsigned int v6; // er15
_BYTE *v7; // rbp
int v8; // edx
__int64 v9; // rax
__int64 v10; // rcx
__int64 v11; // rdi
int *v12; // rax
char *v13; // rax
int result; // eax
int *v15; // rax
char *v16; // rax
int *v17; // rax
char *v18; // rax
int v19; // [rsp+4h] [rbp-114h]
__int64 v20; // [rsp+8h] [rbp-110h]
_DWORD *v21; // [rsp+10h] [rbp-108h] BYREF
unsigned int v22; // [rsp+18h] [rbp-100h]
int v23; // [rsp+1Ch] [rbp-FCh]
char s[64]; // [rsp+20h] [rbp-F8h] BYREF
_DWORD v25[46]; // [rsp+60h] [rbp-B8h] BYREF

snprintf(s, 0x3FuLL, "%s%d", "/dev/usbmon", a1);
v3 = open(s, 0);
if ( v3 == -1 )
{
v15 = _errno_location();
v16 = strerror(*v15);
result = printf("unable to open %s: %s\n", s, v16);
}
else
{
v4 = v3;
v5 = ioctl(v3, 0x9205uLL);
v6 = v5;
if ( v5 <= 0 )
{
v12 = _errno_location();
v13 = strerror(*v12);
result = printf("failed to determine kernel USB buffer size: %s\n", v13);
}
else
{
v7 = mmap(0LL, v5, 1, 1, v4, 0LL);
if ( v7 != (_BYTE *)-1LL )
{
if ( opt_unique_num )
lines = (__int64)malloc(8LL * opt_unique_num);
v8 = 0;
while ( 1 )
{
do
{
v23 = v8;
v21 = v25;
v22 = 32;
ioctl(v4, 0xC0109207uLL, &v21);
v8 = v22;
}
while ( !v22 );
v9 = 0LL;
do
{
v10 = (unsigned int)v25[v9];
v11 = (__int64)&v7[v10];
if ( v7[v10 + 8] != 64
&& (!a1 || *(unsigned __int16 *)(v11 + 12) == a1)
&& (!a2 || *(unsigned __int8 *)(v11 + 11) == a2) )
{
v20 = v9;
v19 = v8;
func1(v11, (__int64)&v7[v10 + 64]);
v9 = v20;
v8 = v19;
}
++v9;
}
while ( v22 > (unsigned int)v9 );
}
}
v17 = _errno_location();
v18 = strerror(*v17);
result = printf("unable to mmap %d bytes: %s\n", v6, v18);
}
}
return result;
}

original_code:
void
usb_sniff(int bus, int address)
{
 struct mon_mfetch_arg mfetch;
 struct usbmon_packet *hdr;
 int kbuf_len, fd, nflush, i;
 char *mbuf, path[64];
 uint32_t vec[32];
 snprintf(path, 63, "%s%d", "/dev/usbmon", bus);
 if ((fd = open(path, 00)) == -1) {
  printf("unable to open %s: %s\n", path, strerror((*__errno_location ())));
  return;
 }
 if ((kbuf_len = ioctl(fd, (((0U) << (((0 +8)+8)+14)) | (((0x92)) << (0 +8)) | (((5)) << 0) | ((0) << ((0 +8)+8))))) <= 0) {
  printf("failed to determine kernel USB buffer size: %s\n",
         strerror((*__errno_location ())));
  return;
 }
 if ((mbuf =
      mmap(((void *)0), kbuf_len, 0x1, 0x01, fd,
    0)) == ((void *) -1)) {
  printf("unable to mmap %d bytes: %s\n", kbuf_len,
         strerror((*__errno_location ())));
  return;
 }
 if (opt_unique_num)
  lines = malloc(opt_unique_num * sizeof(char *));
 nflush = 0;
 while (1) {
  mfetch.offvec = vec;
  mfetch.nfetch = 32;
  mfetch.nflush = nflush;
  ioctl(fd, (((2U|1U) << (((0 +8)+8)+14)) | (((0x92)) << (0 +8)) | (((7)) << 0) | ((((sizeof(struct mon_mfetch_arg)))) << ((0 +8)+8))), &mfetch);
  nflush = mfetch.nfetch;
  for (i = 0; i < mfetch.nfetch; i++) {
   hdr = (struct usbmon_packet *) &mbuf[vec[i]];
   if (hdr->type == '@')
    continue;
   if ((bus != 0 && hdr->busnum != bus) ||
    ((address != 0) && (hdr->devnum != address)))
    continue;
   process_packet(hdr,
           &mbuf[vec[i]] +
           sizeof(struct usbmon_packet));
  }
 }
 free(lines);
 munmap(mbuf, kbuf_len);
 ioctl(fd, (((0U) << (((0 +8)+8)+14)) | (((0x92)) << (0 +8)) | (((8)) << 0) | ((0) << ((0 +8)+8))), mfetch.nfetch);
 close(fd);
}

canonical_original_code:
void usb_sniff(int bus, int address)
{
 struct mon_mfetch_arg mfetch;
 struct usbmon_packet *hdr;
 int kbuf_len; int fd; int nflush; int i;
 char *mbuf; char path[64];
 unsigned int vec[32];
 snprintf(path, 63, "%s%d", "/dev/usbmon", bus);
 if ((fd = open(path, 00)) == -1) {
  printf("unable to open %s: %s\n", path, strerror((*__errno_location ())));
  return;
 }
 if ((kbuf_len = ioctl(fd, (((0U) << (((0 +8)+8)+14)) | (((0x92)) << (0 +8)) | (((5)) << 0) | ((0) << ((0 +8)+8))))) <= 0) {
  printf("failed to determine kernel USB buffer size: %s\n",
         strerror((*__errno_location ())));
  return;
 }
 if ((mbuf =
      mmap(((void *)0), kbuf_len, 0x1, 0x01, fd,
    0)) == ((void *) -1)) {
  printf("unable to mmap %d bytes: %s\n", kbuf_len,
         strerror((*__errno_location ())));
  return;
 }
 if (opt_unique_num)
  lines = malloc(opt_unique_num * sizeof(char *));
 nflush = 0;
 while (1) {
  mfetch.offvec = vec;
  mfetch.nfetch = 32;
  mfetch.nflush = nflush;
  ioctl(fd, (((2U|1U) << (((0 +8)+8)+14)) | (((0x92)) << (0 +8)) | (((7)) << 0) | ((((sizeof(struct mon_mfetch_arg)))) << ((0 +8)+8))), &mfetch);
  nflush = mfetch.nfetch;
  for (i = 0; i < mfetch.nfetch; i++) {
   hdr = (struct usbmon_packet *) &mbuf[vec[i]];
   if (hdr->type == '@')
    continue;
   if ((bus != 0 && hdr->busnum != bus) ||
    ((address != 0) && (hdr->devnum != address)))
    continue;
   process_packet(hdr,
           &mbuf[vec[i]] +
           sizeof(struct usbmon_packet));
  }
 }
 free(lines);
 munmap(mbuf, kbuf_len);
 ioctl(fd, (((0U) << (((0 +8)+8)+14)) | (((0x92)) << (0 +8)) | (((8)) << 0) | ((0) << ((0 +8)+8))), mfetch.nfetch);
 close(fd);
}

variable_types: bus int

variable_types: address int

variable_types: struct mon_mfetch_arg { unsigned int *offvec; unsigned int nfetch; unsigned int nflush; }

variable_types: fd int

variable_types: nflush int

variable_types:
mbuf
char *

variable_types:
path
char [64]

variable_types:
vec
unsigned int [32]

user_defined_types: struct iso_rec { int error_count; int numdesc; }


decompiled_code:
__int64 __fastcall check_device(__int64 a1, const char *a2, _DWORD *a3, _DWORD *a4)
{
const char *v6; // r12
int v7; // eax
int v8; // eax
int v9; // ebp
unsigned int v10; // er15
int v11; // eax
int v12; // ebp
int v14; // edi
int v15; // edi
char s1[4]; // [rsp+6h] [rbp-C2h] BYREF
char v17; // [rsp+Ah] [rbp-BEh]
int buf; // [rsp+Bh] [rbp-BDh] BYREF
char v19; // [rsp+Fh] [rbp-B9h]
char s[184]; // [rsp+10h] [rbp-B8h] BYREF

v6 = (const char *)(a1 + 19);
v7 = *(_DWORD *)a2;
v17 = 0;
*(_DWORD *)s1 = v7;
sprintf(s, "%s/%s/idVendor", "/sys/bus/usb/devices", (const char *)(a1 + 19));
v8 = open(s, 0);
if ( v8 == -1 )
return 0;
v9 = v8;
v19 = 0;
if ( read(v8, &buf, 4uLL) != 4 || (v10 = strncmp(s1, (const char *)&buf, 4uLL)) != 0 )
{
close(v9);
return 0;
}
sprintf(s, "%s/%s/idProduct", "/sys/bus/usb/devices", v6);
v11 = open(s, 0);
v12 = v11;
if ( v11 == -1 )
return 0;
v19 = 0;
if ( read(v11, &buf, 4uLL) == 4 && !strncmp(a2 + 5, (const char *)&buf, 4uLL) )
{
sprintf(s, "%s/%s/busnum", "/sys/bus/usb/devices", v6);
v14 = open(s, 0);
if ( v14 != -1 )
{
v19 = 0;
buf = 0;
read(v14, &buf, 4uLL);
*a3 = strtol((const char *)&buf, 0LL, 10);
sprintf(s, "%s/%s/devnum", "/sys/bus/usb/devices", v6);
v15 = open(s, 0);
if ( v15 != -1 )
{
v19 = 0;
v10 = 1;
buf = 0;
read(v15, &buf, 4uLL);
*a4 = strtol((const char *)&buf, 0LL, 10);
return v10;
}
}
return 0;
}
close(v12);
return v10;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, const char *a2, _DWORD *a3, _DWORD *a4)
{
const char *v6; // r12
int v7; // eax
int v8; // eax
int v9; // ebp
unsigned int v10; // er15
int v11; // eax
int v12; // ebp
int v14; // edi
int v15; // edi
char s1[4]; // [rsp+6h] [rbp-C2h] BYREF
char v17; // [rsp+Ah] [rbp-BEh]
int buf; // [rsp+Bh] [rbp-BDh] BYREF
char v19; // [rsp+Fh] [rbp-B9h]
char s[184]; // [rsp+10h] [rbp-B8h] BYREF

v6 = (const char *)(a1 + 19);
v7 = *(_DWORD *)a2;
v17 = 0;
*(_DWORD *)s1 = v7;
sprintf(s, "%s/%s/idVendor", "/sys/bus/usb/devices", (const char *)(a1 + 19));
v8 = open(s, 0);
if ( v8 == -1 )
return 0;
v9 = v8;
v19 = 0;
if ( read(v8, &buf, 4uLL) != 4 || (v10 = strncmp(s1, (const char *)&buf, 4uLL)) != 0 )
{
close(v9);
return 0;
}
sprintf(s, "%s/%s/idProduct", "/sys/bus/usb/devices", v6);
v11 = open(s, 0);
v12 = v11;
if ( v11 == -1 )
return 0;
v19 = 0;
if ( read(v11, &buf, 4uLL) == 4 && !strncmp(a2 + 5, (const char *)&buf, 4uLL) )
{
sprintf(s, "%s/%s/busnum", "/sys/bus/usb/devices", v6);
v14 = open(s, 0);
if ( v14 != -1 )
{
v19 = 0;
buf = 0;
read(v14, &buf, 4uLL);
*a3 = strtol((const char *)&buf, 0LL, 10);
sprintf(s, "%s/%s/devnum", "/sys/bus/usb/devices", v6);
v15 = open(s, 0);
if ( v15 != -1 )
{
v19 = 0;
v10 = 1;
buf = 0;
read(v15, &buf, 4uLL);
*a4 = strtol((const char *)&buf, 0LL, 10);
return v10;
}
}
return 0;
}
close(v12);
return v10;
}

original_code:
int
check_device(struct dirent *de, char *vidpid, int *bus, int *address)
{
 int fd;
 char path[128], vid[5], buf[5];
 memcpy(vid, vidpid, 4);
 vid[4] = '\0';
 sprintf(path, "%s/%s/idVendor", "/sys/bus/usb/devices", de->d_name);
 if ((fd = open(path, 00)) == -1)
  return 0;
 buf[4] = '\0';
 if ((read(fd, buf, 4) != 4) || strncmp(vid, buf, 4)) {
  close(fd);
  return 0;
 }
 sprintf(path, "%s/%s/idProduct", "/sys/bus/usb/devices", de->d_name);
 if ((fd = open(path, 00)) == -1)
  return 0;
 buf[4] = '\0';
 if ((read(fd, buf, 4) != 4) || strncmp(vidpid + 5, buf, 4)) {
  close(fd);
  return 0;
 }
 sprintf(path, "%s/%s/busnum", "/sys/bus/usb/devices", de->d_name);
 if ((fd = open(path, 00)) == -1)
  return 0;
 memset(buf, 0, 5);
 read(fd, buf, 4);
 *bus = strtol(buf, ((void *)0), 10);
 sprintf(path, "%s/%s/devnum", "/sys/bus/usb/devices", de->d_name);
 if ((fd = open(path, 00)) == -1)
  return 0;
 memset(buf, 0, 5);
 read(fd, buf, 4);
 *address = strtol(buf, ((void *)0), 10);
 return 1;
}

canonical_original_code:
int check_device(struct dirent *de, char *vidpid, int *bus, int *address)
{
 int fd;
 char path[128]; char vid[5]; char buf[5];
 memcpy(vid, vidpid, 4);
 vid[4] = '\0';
 sprintf(path, "%s/%s/idVendor", "/sys/bus/usb/devices", de->d_name);
 if ((fd = open(path, 00)) == -1)
  return 0;
 buf[4] = '\0';
 if ((read(fd, buf, 4) != 4) || strncmp(vid, buf, 4)) {
  close(fd);
  return 0;
 }
 sprintf(path, "%s/%s/idProduct", "/sys/bus/usb/devices", de->d_name);
 if ((fd = open(path, 00)) == -1)
  return 0;
 buf[4] = '\0';
 if ((read(fd, buf, 4) != 4) || strncmp(vidpid + 5, buf, 4)) {
  close(fd);
  return 0;
 }
 sprintf(path, "%s/%s/busnum", "/sys/bus/usb/devices", de->d_name);
 if ((fd = open(path, 00)) == -1)
  return 0;
 memset(buf, 0, 5);
 read(fd, buf, 4);
 *bus = strtol(buf, ((void *)0), 10);
 sprintf(path, "%s/%s/devnum", "/sys/bus/usb/devices", de->d_name);
 if ((fd = open(path, 00)) == -1)
  return 0;
 memset(buf, 0, 5);
 read(fd, buf, 4);
 *address = strtol(buf, ((void *)0), 10);
 return 1;
}

variable_types: struct dirent { unsigned long d_ino; long d_off; unsigned short d_reclen; unsigned char d_type; char d_name[256]; } *

variable_types:
vidpid
char *

variable_types:
bus
int *

variable_types:
address
int *

variable_types:
path
char [128]

variable_types:
vid
char [5]

variable_types:
buf
char [5]

return_type: int


usage

decompiled_code:
int usage()
{
puts("usbdump Copyright (C) 2011 Bert Vermeulen <bert@biot.com>");
return puts("usage: usbdump [-d <vid:pid>] [-u <num lines>] [-b <bus number>]");
}

canonical_decompiled_code:
int func5()
{
puts("usbdump Copyright (C) 2011 Bert Vermeulen <bert@biot.com>");
return puts("usage: usbdump [-d <vid:pid>] [-u <num lines>] [-b <bus number>]");
}

original_code:
void
usage(void)
{
 printf("usbdump Copyright (C) 2011 Bert Vermeulen <bert@biot.com>\n");
 printf("usage: usbdump [-d <vid:pid>] [-u <num lines>] [-b <bus number>]\n");
}

canonical_original_code:
void usage()
{
 printf("usbdump Copyright (C) 2011 Bert Vermeulen <bert@biot.com>\n");
 printf("usage: usbdump [-d <vid:pid>] [-u <num lines>] [-b <bus number>]\n");
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r13
int v4; // eax
char *v5; // r12
int result; // eax
unsigned int v7; // [rsp+8h] [rbp-30h] BYREF
int v8[11]; // [rsp+Ch] [rbp-2Ch] BYREF

v3 = 0LL;
v8[0] = 0;
while ( 1 )
{
v4 = getopt(argc, (char *const *)argv, "d:u:b:");
if ( v4 == -1 )
break;
switch ( v4 )
{
case 'd':
v5 = optarg;
if ( strlen(optarg) == 9 && strspn(v5, "01234567890abcdef:") == 9 )
v3 = v5;
else
puts("invalid format for device id (aaaa:bbbb)");
break;
case 'u':
opt_unique_num = strtol(optarg, 0LL, 10);
break;
case 'b':
opt_bus_num = strtol(optarg, 0LL, 10);
break;
}
}
if ( !v3 )
{
if ( opt_bus_num == -1 )
{
usage();
return 1;
}
goto LABEL_13;
}
if ( opt_bus_num != -1 )
{
LABEL_13:
v7 = opt_bus_num;
usb_sniff(opt_bus_num, v8[0]);
return 0;
}
result = find_device(v3, &v7, v8);
if ( result )
{
usb_sniff(v7, v8[0]);
result = 0;
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r13
int v4; // eax
char *v5; // r12
int result; // eax
unsigned int v7; // [rsp+8h] [rbp-30h] BYREF
int v8[11]; // [rsp+Ch] [rbp-2Ch] BYREF

v3 = 0LL;
v8[0] = 0;
while ( 1 )
{
v4 = getopt(argc, (char *const *)argv, "d:u:b:");
if ( v4 == -1 )
break;
switch ( v4 )
{
case 'd':
v5 = optarg;
if ( strlen(optarg) == 9 && strspn(v5, "01234567890abcdef:") == 9 )
v3 = v5;
else
puts("invalid format for device id (aaaa:bbbb)");
break;
case 'u':
opt_unique_num = strtol(optarg, 0LL, 10);
break;
case 'b':
opt_bus_num = strtol(optarg, 0LL, 10);
break;
}
}
if ( !v3 )
{
if ( opt_bus_num == -1 )
{
func5();
return 1;
}
goto LABEL_13;
}
if ( opt_bus_num != -1 )
{
LABEL_13:
v7 = opt_bus_num;
func2(opt_bus_num, v8[0]);
return 0;
}
result = func4(v3, &v7, v8);
if ( result )
{
func2(v7, v8[0]);
result = 0;
}
return result;
}

original_code:
int
main(int argc, char **argv)
{
 int opt, bus, address = 0;
 char *device, *entry;
 device = ((void *)0);
 while ((opt = getopt(argc, argv, "d:u:b:")) != -1) {
  switch (opt) {
  case 'd':
   if (strlen(optarg) != 9
       || strspn(optarg, "01234567890abcdef:") != 9)
    printf("invalid format for device id (aaaa:bbbb)\n");
   else
    device = optarg;
   break;
  case 'u':
   opt_unique_num = strtol(optarg, ((void *)0), 10);
   break;
  case 'b':
   opt_bus_num = strtol(optarg, ((void *)0), 10);
   break;
  }
 }
 if (!device && opt_bus_num == -1) {
  usage();
  return 1;
 }
 if (opt_bus_num != -1) {
  bus = opt_bus_num;
  usb_sniff(bus, address);
 } else if (find_device(device, &bus, &address)) {
  usb_sniff(bus, address);
 }
 return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
 int opt; int bus; int address = 0;
 char *device; char *entry;
 device = ((void *)0);
 while ((opt = getopt(argc, argv, "d:u:b:")) != -1) {
  switch (opt) {
  case 'd':
   if (strlen(optarg) != 9
       || strspn(optarg, "01234567890abcdef:") != 9)
    printf("invalid format for device id (aaaa:bbbb)\n");
   else
    device = optarg;
   break;
  case 'u':
   opt_unique_num = strtol(optarg, ((void *)0), 10);
   break;
  case 'b':
   opt_bus_num = strtol(optarg, ((void *)0), 10);
   break;
  }
 }
 if (!device && opt_bus_num == -1) {
  usage();
  return 1;
 }
 if (opt_bus_num != -1) {
  bus = opt_bus_num;
  usb_sniff(bus, address);
 } else if (find_device(device, &bus, &address)) {
  usb_sniff(bus, address);
 }
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: opt int

variable_types:
device
char *

variable_types:
entry
char *


repo: wallymathieu/bbcode-c

decompiled_code:
__int64 __fastcall bbcode_q_find_last(__int64 a1, int a2, const char *a3)
{
__int64 v4; // rbx

if ( a2 < 0 )
return 0xFFFFFFFFLL;
v4 = a2;
while ( strcmp(*(const char **)(a1 + 8 * v4), a3) )
{
if ( (int)--v4 < 0 )
return 0xFFFFFFFFLL;
}
return (unsigned int)v4;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, int a2, const char *a3)
{
__int64 v4; // rbx

if ( a2 < 0 )
return 0xFFFFFFFFLL;
v4 = a2;
while ( strcmp(*(const char **)(a1 + 8 * v4), a3) )
{
if ( (int)--v4 < 0 )
return 0xFFFFFFFFLL;
}
return (unsigned int)v4;
}

original_code:
int bbcode_q_find_last(const char* queue[], const int length, const char* element)
{
    int i;
    for(i = length; i>=0;i--)
    {if (!strcmp(queue[i],element)) return i;}
    return -1;
}

canonical_original_code:
int bbcode_q_find_last(char *queue[], int length, char *element)
{
    int i;
    for(i = length; i>=0;i--)
    {if (!strcmp(queue[i],element)) return i;}
    return -1;
}

variable_types:
queue
char *[]

variable_types: length int

variable_types:
element
char *

variable_types: i int


decompiled_code:
__int64 __fastcall bbcode_q_add(__int64 a1, _DWORD *a2, __int64 a3)
{
__int64 result; // rax

LODWORD(result) = *a2 + 1;
*a2 = result;
result = (int)result;
*(_QWORD *)(a1 + 8LL * (int)result) = a3;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, _DWORD *a2, __int64 a3)
{
__int64 result; // rax

LODWORD(result) = *a2 + 1;
*a2 = result;
result = (int)result;
*(_QWORD *)(a1 + 8LL * (int)result) = a3;
return result;
}

original_code:
void bbcode_q_add(const char* queue[], int* index, const char* element)
{
    queue[++(*index)]=element;
}

canonical_original_code:
void bbcode_q_add(char *queue[], int *index, char *element)
{
    queue[++(*index)]=element;
}

variable_types:
index
int *


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

v1 = 0LL;
v2 = 0;
if ( !strcmp("b", s2) )
return v2;
while ( ++v1 != 5 )
{
v2 = v1;
if ( !strcmp((&BBTAGS)[v1], s2) )
return v2;
}
return 0xFFFFFFFFLL;
}

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

v1 = 0LL;
v2 = 0;
if ( !strcmp("b", s2) )
return v2;
while ( ++v1 != 5 )
{
v2 = v1;
if ( !strcmp((&BBTAGS)[v1], s2) )
return v2;
}
return 0xFFFFFFFFLL;
}

original_code:
int bbcode_find_tag_index(const char* tag)
{
    int i;
    for(i = 0;i<NUM_BBTAGS;i++) {if (!strcmp(BBTAGS[i],tag)) return i;}
    return -1;
}

canonical_original_code:
int bbcode_find_tag_index(char *tag)
{
    int i;
    for(i = 0;i<NUM_BBTAGS;i++) {if (!strcmp(BBTAGS[i],tag)) return i;}
    return -1;
}

variable_types:
tag
char *


decompiled_code:
char *__fastcall bbcode_get_htmltag(char *a1)
{
int v1; // eax
char *v2; // r8

v1 = bbcode_find_tag_index(a1);
v2 = 0LL;
if ( v1 >= 0 )
v2 = HTMLTAGS[v1];
return v2;
}

canonical_decompiled_code:
char *__fastcall func3(char *a1)
{
int v1; // eax
char *v2; // r8

v1 = func2(a1);
v2 = 0LL;
if ( v1 >= 0 )
v2 = HTMLTAGS[v1];
return v2;
}

original_code:
char* bbcode_get_htmltag(const char* tag)
{
    int idx = bbcode_find_tag_index(tag);
    if (idx>=0)
        return HTMLTAGS[idx];
    return ((void *)0);
}

canonical_original_code:
char *bbcode_get_htmltag(char *tag)
{
    int idx = bbcode_find_tag_index(tag);
    if (idx>=0)
        return HTMLTAGS[idx];
    return ((void *)0);
}

variable_types: idx int

return_type: char *


decompiled_code:
__int64 __fastcall bbcode_find_color_index(char *s2)
{
const char *v2; // rdi
__int64 i; // rbx
unsigned int v4; // er12

v2 = "black";
for ( i = 0LL; ; v2 = COLORNAMES[i] )
{
v4 = i;
if ( !strcmp(v2, s2) )
break;
if ( ++i == 18 )
return (unsigned int)-1;
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func4(char *s2)
{
const char *v2; // rdi
__int64 i; // rbx
unsigned int v4; // er12

v2 = "black";
for ( i = 0LL; ; v2 = COLORNAMES[i] )
{
v4 = i;
if ( !strcmp(v2, s2) )
break;
if ( ++i == 18 )
return (unsigned int)-1;
}
return v4;
}

original_code:
int bbcode_find_color_index(const char* attr)
{
    int i;
    for(i = 0;i<NUM_COLORS;i++) {if (!strcmp(COLORNAMES[i],attr)) return i;}
    return -1;
}

canonical_original_code:
int bbcode_find_color_index(char *attr)
{
    int i;
    for(i = 0;i<NUM_COLORS;i++) {if (!strcmp(COLORNAMES[i],attr)) return i;}
    return -1;
}

variable_types:
attr
char *


decompiled_code:
char *__fastcall bbcode_get_colorcode(char *a1)
{
int v1; // eax
char *v2; // r8

v1 = bbcode_find_color_index(a1);
v2 = 0LL;
if ( v1 >= 0 )
v2 = COLORCODES[v1];
return v2;
}

canonical_decompiled_code:
char *__fastcall func5(char *a1)
{
int v1; // eax
char *v2; // r8

v1 = func4(a1);
v2 = 0LL;
if ( v1 >= 0 )
v2 = COLORCODES[v1];
return v2;
}

original_code:
char* bbcode_get_colorcode(const char* attr)
{
    int idx = bbcode_find_color_index(attr);
    if (idx>=0)
        return COLORCODES[idx];
    return ((void *)0);
}

canonical_original_code:
char *bbcode_get_colorcode(char *attr)
{
    int idx = bbcode_find_color_index(attr);
    if (idx>=0)
        return COLORCODES[idx];
    return ((void *)0);
}


require

decompiled_code:
void __fastcall require(int a1, const char *a2)
{
if ( !a1 )
{
printf("Fail message: %s\n", a2);
exit(1);
}
}

canonical_decompiled_code:
void __fastcall func6(int a1, const char *a2)
{
if ( !a1 )
{
printf("Fail message: %s\n", a2);
exit(1);
}
}

original_code:
void require(int val, char* txt)
{
    if (!val)
    {
        printf("Fail message: %s\n",txt);
        exit(1);
    }
}

canonical_original_code:
void require(int val, char *txt)
{
    if (!val)
    {
        printf("Fail message: %s\n",txt);
        exit(1);
    }
}

variable_types: val int

variable_types:
txt
char *


decompiled_code:
__int64 __fastcall bbcode_get_attribute_type(char *s2)
{
int v1; // eax
unsigned int v2; // er8
int v3; // ecx
int v4; // eax
__int64 v5; // rsi
char *v6; // rdx
char v7; // cl
char *v8; // rsi
char *v10; // rcx
int v11; // esi
unsigned int v12; // er8
unsigned int v13; // eax

v1 = strlen(s2);
v2 = 3;
if ( !v1 )
return v2;
v3 = v1;
v4 = *s2;
if ( *s2 != 35 )
{
if ( v3 <= 0 )
return 2 * ((int)bbcode_find_color_index(s2) >= 0);
v5 = (unsigned int)(v3 - 1);
v6 = s2;
v7 = 1;
v8 = &s2[v5];
while ( 1 )
{
v7 &= (unsigned __int8)((v4 & 0xDF) - 65) <= 0x19u;
if ( v8 == v6 )
break;
LOBYTE(v4) = *++v6;
}
v2 = 0;
if ( v7 )
return 2 * ((int)bbcode_find_color_index(s2) >= 0);
return v2;
}
v2 = 0;
if ( v3 != 6 )
return v2;
v10 = s2 + 1;
v11 = 1;
v12 = 1;
while ( 1 )
{
if ( (unsigned __int8)((v4 & 0xDF) - 65) > 0x19u )
{
v13 = v4 - 48;
LOBYTE(v13) = v13 <= 9;
v11 &= v13;
v12 = (unsigned __int8)v11;
}
if ( v10 == s2 + 6 )
break;
v4 = *v10++;
}
return v12;
}

canonical_decompiled_code:
__int64 __fastcall func7(char *s2)
{
int v1; // eax
unsigned int v2; // er8
int v3; // ecx
int v4; // eax
__int64 v5; // rsi
char *v6; // rdx
char v7; // cl
char *v8; // rsi
char *v10; // rcx
int v11; // esi
unsigned int v12; // er8
unsigned int v13; // eax

v1 = strlen(s2);
v2 = 3;
if ( !v1 )
return v2;
v3 = v1;
v4 = *s2;
if ( *s2 != 35 )
{
if ( v3 <= 0 )
return 2 * ((int)func4(s2) >= 0);
v5 = (unsigned int)(v3 - 1);
v6 = s2;
v7 = 1;
v8 = &s2[v5];
while ( 1 )
{
v7 &= (unsigned __int8)((v4 & 0xDF) - 65) <= 0x19u;
if ( v8 == v6 )
break;
LOBYTE(v4) = *++v6;
}
v2 = 0;
if ( v7 )
return 2 * ((int)func4(s2) >= 0);
return v2;
}
v2 = 0;
if ( v3 != 6 )
return v2;
v10 = s2 + 1;
v11 = 1;
v12 = 1;
while ( 1 )
{
if ( (unsigned __int8)((v4 & 0xDF) - 65) > 0x19u )
{
v13 = v4 - 48;
LOBYTE(v13) = v13 <= 9;
v11 &= v13;
v12 = (unsigned __int8)v11;
}
if ( v10 == s2 + 6 )
break;
v4 = *v10++;
}
return v12;
}

original_code:
enum bbcode_attribute_type bbcode_get_attribute_type(const char* attr)
{
    int attr_len = strlen(attr);
    if (attr_len == 0) return empty;
    if (attr[0] == '#')
    {
        if (attr_len != 6)
            return invalid;
        _Bool allLettersAndDigits = 1;
        int i;
        for (i = 0; i < attr_len; i++)
        {
            int val = attr[i];
            allLettersAndDigits &= ((val>='a'&&val<='z')||(val>='A'&&val<='Z')||(val>='0'&&val<='9'));
        }
        if (!allLettersAndDigits) return invalid;
        return colorcode;
    }
    else
    {
        _Bool allLetters = 1;
        int i;
        for (i = 0; i < attr_len; i++)
        {
            int val = attr[i];
            allLetters &= ((val>='a'&&val<='z')||(val>='A'&&val<='Z'));
        }
        if (!allLetters) return invalid;
        int coloridx = bbcode_find_color_index(attr);
        if (coloridx < 0) return invalid;
        return colorname;
    }
}

canonical_original_code:
enum bbcode_attribute_type bbcode_get_attribute_type(char *attr)
{
    int attr_len = strlen(attr);
    if (attr_len == 0) return empty;
    if (attr[0] == '#')
    {
        if (attr_len != 6)
            return invalid;
        _Bool allLettersAndDigits = 1;
        int i;
        for (i = 0; i < attr_len; i++)
        {
            int val = attr[i];
            allLettersAndDigits &= ((val>='a'&&val<='z')||(val>='A'&&val<='Z')||(val>='0'&&val<='9'));
        }
        if (!allLettersAndDigits) return invalid;
        return colorcode;
    }
    else
    {
        _Bool allLetters = 1;
        int i;
        for (i = 0; i < attr_len; i++)
        {
            int val = attr[i];
            allLetters &= ((val>='a'&&val<='z')||(val>='A'&&val<='Z'));
        }
        if (!allLetters) return invalid;
        int coloridx = bbcode_find_color_index(attr);
        if (coloridx < 0) return invalid;
        return colorname;
    }
}

variable_types: coloridx int

return_type: enum bbcode_attribute_type { invalid=0, colorcode=1, colorname=2, empty=3 }


decompiled_code:
char __fastcall bbcode_render_attribute(char *s, const char *a2, char *a3, int a4)
{
bool v5; // zf
int v6; // eax
size_t v8; // rax
char *v9; // rdi
const char *v10; // rsi
char *v11; // rbx
__int64 v12; // rbp
size_t v13; // rax

v5 = strcmp(a2, "color") == 0;
LOBYTE(v6) = !v5;
if ( v5 )
{
if ( a4 == 2 )
{
v6 = bbcode_find_color_index(a3);
v12 = v6;
if ( v6 < 0 )
return v6;
v13 = strlen(s);
v10 = COLORCODES[v12];
v9 = &s[v13 + 15];
*(__m128i *)&s[v13] = _mm_load_si128((const __m128i *)&LC5);
goto LABEL_7;
}
if ( a4 > 2 )
{
if ( a4 == 3 )
return v6;
}
else
{
if ( !a4 )
goto LABEL_11;
if ( a4 == 1 )
{
v8 = strlen(s);
v9 = &s[v8 + 14];
qmemcpy(&s[v8], " style=\"color:", 14);
v10 = a3;
LABEL_7:
strcpy(v9, v10);
v11 = &s[strlen(s)];
LOBYTE(v6) = 34;
strcpy(v11, "\" ");
return v6;
}
}
printf(" Type: '%d'\n", (unsigned int)a4);
LABEL_11:
exit(1);
}
return v6;
}

canonical_decompiled_code:
char __fastcall func8(char *s, const char *a2, char *a3, int a4)
{
bool v5; // zf
int v6; // eax
size_t v8; // rax
char *v9; // rdi
const char *v10; // rsi
char *v11; // rbx
__int64 v12; // rbp
size_t v13; // rax

v5 = strcmp(a2, "color") == 0;
LOBYTE(v6) = !v5;
if ( v5 )
{
if ( a4 == 2 )
{
v6 = func4(a3);
v12 = v6;
if ( v6 < 0 )
return v6;
v13 = strlen(s);
v10 = COLORCODES[v12];
v9 = &s[v13 + 15];
*(__m128i *)&s[v13] = _mm_load_si128((const __m128i *)&LC5);
goto LABEL_7;
}
if ( a4 > 2 )
{
if ( a4 == 3 )
return v6;
}
else
{
if ( !a4 )
goto LABEL_11;
if ( a4 == 1 )
{
v8 = strlen(s);
v9 = &s[v8 + 14];
qmemcpy(&s[v8], " style=\"color:", 14);
v10 = a3;
LABEL_7:
strcpy(v9, v10);
v11 = &s[strlen(s)];
LOBYTE(v6) = 34;
strcpy(v11, "\" ");
return v6;
}
}
printf(" Type: '%d'\n", (unsigned int)a4);
LABEL_11:
exit(1);
}
return v6;
}

original_code:
void bbcode_render_attribute(char* output, const char* tag, const char* attr, const int type)
{
    if (!strcmp(tag, "color"))
    {
        switch (type)
        {
            case invalid: exit(1);
            case colorcode:
            {
                strcat(output," style=\"color:");
                strcat(output,attr);
                strcat(output,"\" ");
                break;
            }
            case colorname:
            {
                int coloridx = bbcode_find_color_index(attr);
                if (coloridx < 0)
                    break;
                strcat(output," style=\"color:#");
                strcat(output,COLORCODES[coloridx]);
                strcat(output,"\" ");
            }
            case empty: break;
            default:
                printf("  Type: '%d'\n",type);
                exit(1);
        }
    }
}

canonical_original_code:
void bbcode_render_attribute(char *output, char *tag, char *attr, int type)
{
    if (!strcmp(tag, "color"))
    {
        switch (type)
        {
            case invalid: exit(1);
            case colorcode:
            {
                strcat(output," style=\"color:");
                strcat(output,attr);
                strcat(output,"\" ");
                break;
            }
            case colorname:
            {
                int coloridx = bbcode_find_color_index(attr);
                if (coloridx < 0)
                    break;
                strcat(output," style=\"color:#");
                strcat(output,COLORCODES[coloridx]);
                strcat(output,"\" ");
            }
            case empty: break;
            default:
                printf("  Type: '%d'\n",type);
                exit(1);
        }
    }
}

variable_types:
output
char *

variable_types: type int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
return (unsigned __int8)bbcode_testfixture("test_bbcode.txt", argv, envp) ^ 1;
}

original_code:
int main (int argc, const char * argv[]) {
  if (!bbcode_testfixture("test_bbcode.txt")){
    return 1;
  }
  return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
  if (!bbcode_testfixture("test_bbcode.txt")){
    return 1;
  }
  return 0;
}

variable_types: argc int

variable_types:
argv
char *[]


decompiled_code:
__int64 __fastcall test_replace(unsigned int a1, __int64 a2, const char *a3)
{
const char *v4; // r12
int v5; // er8
__int64 result; // rax

v4 = (const char *)bbcode_replace(a2);
v5 = strcmp(v4, a3);
result = 1LL;
if ( v5 )
{
printf("TEST %d: '%s'\nEXPT %d: '%s'\n", a1, v4, a1, a3);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1, __int64 a2, const char *a3)
{
const char *v4; // r12
int v5; // er8
__int64 result; // rax

v4 = (const char *)bbcode_replace(a2);
v5 = strcmp(v4, a3);
result = 1LL;
if ( v5 )
{
printf("TEST %d: '%s'\nEXPT %d: '%s'\n", a1, v4, a1, a3);
result = 0LL;
}
return result;
}

original_code:
_Bool test_replace(int num,char* val,char* exp)
{
  char* repl = bbcode_replace(val);
  if (strcmp(repl,exp))
  {
    printf("TEST %d: '%s'\nEXPT %d: '%s'\n", num, repl, num, exp);
    return 0;
  }
  return 1;
}

canonical_original_code:
_Bool test_replace(int num, char *val, char *exp)
{
  char *repl = bbcode_replace(val);
  if (strcmp(repl,exp))
  {
    printf("TEST %d: '%s'\nEXPT %d: '%s'\n", num, repl, num, exp);
    return 0;
  }
  return 1;
}

variable_types: num int

variable_types:
val
char *

variable_types:
exp
char *

variable_types:
repl
char *


decompiled_code:
__int64 test_bbcode2()
{
const char *v0; // rax

v0 = (const char *)bbcode_get_htmltag("code");
printf("tag: %s\n", v0);
return 1LL;
}

canonical_decompiled_code:
__int64 func3()
{
const char *v0; // rax

v0 = (const char *)bbcode_get_htmltag("code");
printf("tag: %s\n", v0);
return 1LL;
}

original_code:
_Bool test_bbcode2()
{
  printf("tag: %s\n",bbcode_get_htmltag("code"));
  return 1;
}


decompiled_code:
__int64 __fastcall bbcode_testfixture(const char *a1)
{
int v1; // ebx

v1 = test_bbcode1(a1);
return v1 & (unsigned int)test_bbcode2();
}

canonical_decompiled_code:
__int64 __fastcall func4(const char *a1)
{
int v1; // ebx

v1 = func2(a1);
return v1 & (unsigned int)func3();
}

original_code:
_Bool bbcode_testfixture(char* file)
{
  _Bool retval = 1;
  retval&=test_bbcode1(file);
  retval&=test_bbcode2();
  return retval;
}

canonical_original_code:
_Bool bbcode_testfixture(char *file)
{
  _Bool retval = 1;
  retval&=test_bbcode1(file);
  retval&=test_bbcode2();
  return retval;
}

variable_types:
file
char *


repo: wangbaishi/tos

decompiled_code:
__int64 __fastcall read_data(__int64 a1)
{
__int64 v1; // rbx
__int64 result; // rax

v1 = a1;
do
{
v1 += 2LL;
result = inword(496LL);
*(_WORD *)(v1 - 2) = result;
}
while ( v1 != a1 + 512 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
__int64 v1; // rbx
__int64 result; // rax

v1 = a1;
do
{
v1 += 2LL;
result = inword(496LL);
*(_WORD *)(v1 - 2) = result;
}
while ( v1 != a1 + 512 );
return result;
}

original_code:
void read_data(char *buffer)
{
 int i;
 unsigned short *temp;
 temp=(unsigned short *)buffer;
 for(i=0;i<256;i++)
 {
  temp[i]=inword(0x1f0);
 }
}

variable_types:
buffer
char *

variable_types: i int

variable_types:
temp
unsigned short *


decompiled_code:
__int64 __fastcall init_opreation(unsigned int a1, int a2)
{
__int64 v3; // rsi

outbyte(502LL, 64LL);
outbyte(498LL, 0LL);
outbyte(499LL, HIBYTE(a1));
outbyte(500LL, 0LL);
outbyte(501LL, 0LL);
outbyte(498LL, 1LL);
outbyte(499LL, (unsigned __int8)a1);
outbyte(500LL, BYTE1(a1));
outbyte(501LL, BYTE2(a1));
v3 = 36LL;
if ( a2 )
v3 = 52LL;
return outbyte(503LL, v3);
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1, int a2)
{
__int64 v3; // rsi

outbyte(502LL, 64LL);
outbyte(498LL, 0LL);
outbyte(499LL, HIBYTE(a1));
outbyte(500LL, 0LL);
outbyte(501LL, 0LL);
outbyte(498LL, 1LL);
outbyte(499LL, (unsigned __int8)a1);
outbyte(500LL, BYTE1(a1));
outbyte(501LL, BYTE2(a1));
v3 = 36LL;
if ( a2 )
v3 = 52LL;
return outbyte(503LL, v3);
}

original_code:
void init_opreation(unsigned int addr,int flag)
{
 unsigned char temp;
 unsigned short count=1;
 outbyte(0x1f6,0x40);
 temp=(unsigned char)(count>>8);
 outbyte(0x1f2,temp);
 temp=(unsigned char)(addr>>24);
 outbyte(0x1f3,temp);
 outbyte(0x1f4,0);
 outbyte(0x1f5,0);
 temp=(unsigned char)count;
 outbyte(0x1f2,temp);
 temp=(unsigned char)addr;
 outbyte(0x1f3,temp);
 temp=(unsigned char)(addr>>8);
 outbyte(0x1f4,temp);
 temp=(unsigned char)(addr>>16);
 outbyte(0x1f5,temp);
 if(flag==0)
 {
  outbyte(0x1f7,0x24);
 }
 else{
  outbyte(0x1f7,0x34);
 }
}

canonical_original_code:
void init_opreation(unsigned int addr, int flag)
{
 unsigned char temp;
 unsigned short count = 1;
 outbyte(0x1f6,0x40);
 temp=(unsigned char)(count>>8);
 outbyte(0x1f2,temp);
 temp=(unsigned char)(addr>>24);
 outbyte(0x1f3,temp);
 outbyte(0x1f4,0);
 outbyte(0x1f5,0);
 temp=(unsigned char)count;
 outbyte(0x1f2,temp);
 temp=(unsigned char)addr;
 outbyte(0x1f3,temp);
 temp=(unsigned char)(addr>>8);
 outbyte(0x1f4,temp);
 temp=(unsigned char)(addr>>16);
 outbyte(0x1f5,temp);
 if(flag==0)
 {
  outbyte(0x1f7,0x24);
 }
 else{
  outbyte(0x1f7,0x34);
 }
}

variable_types:
addr
unsigned int

variable_types: flag int

variable_types:
temp
unsigned char

variable_types:
count
unsigned short


decompiled_code:
__int64 check_flags()
{
int v0; // ebx
char v1; // al
unsigned int v2; // er8

v0 = 4;
do
{
v1 = inbyte(503LL);
--v0;
}
while ( v0 );
for ( ; v1 < 0; v1 = inbyte(503LL) )
;
v2 = 1;
if ( (v1 & 8) == 0 )
{
while ( (inbyte(503LL) & 0x80u) != 0LL )
;
v2 = -1;
}
return v2;
}

original_code:
int check_flags(void)
{
 int i;
 unsigned char temp;
 for(i=0;i<4;i++){
  temp=inbyte(0x1f7);
 }
 while(temp&0x80){
  temp=inbyte(0x1f7);
 }
 if(temp&8){
  return 1;
 }
 while((temp=inbyte(0x1f7))&0x80);
 if(temp&0x21){
  return -1;
 }
}

canonical_original_code:
int check_flags()
{
 int i;
 unsigned char temp;
 for(i=0;i<4;i++){
  temp=inbyte(0x1f7);
 }
 while(temp&0x80){
  temp=inbyte(0x1f7);
 }
 if(temp&8){
  return 1;
 }
 while((temp=inbyte(0x1f7))&0x80);
 if(temp&0x21){
  return -1;
 }
}


Bread

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

init_opreation(a1, 0);
result = check_flags();
if ( (_DWORD)result == 1 )
result = read_data(a2);
return result;
}

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

func1(a1, 0);
result = func2();
if ( (_DWORD)result == 1 )
result = func0(a2);
return result;
}

original_code:
void Bread(unsigned int addr,char *buffer)
{
 int flag;
 init_opreation(addr,0);
 flag=check_flags();
 if(flag==1){
  read_data(buffer);
 }
 else{
 }
}

canonical_original_code:
void Bread(unsigned int addr, char *buffer)
{
 int flag;
 init_opreation(addr,0);
 flag=check_flags();
 if(flag==1){
  read_data(buffer);
 }
 else{
 }
}


decompiled_code:
__int64 __fastcall write_data(unsigned __int16 *a1)
{
unsigned __int16 *v1; // rbx
__int64 v2; // rsi

v1 = a1;
do
{
v2 = *v1++;
outword(496LL, v2);
}
while ( v1 != a1 + 256 );
return outbyte(503LL, 231LL);
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned __int16 *a1)
{
unsigned __int16 *v1; // rbx
__int64 v2; // rsi

v1 = a1;
do
{
v2 = *v1++;
outword(496LL, v2);
}
while ( v1 != a1 + 256 );
return outbyte(503LL, 231LL);
}

original_code:
void write_data(char *b)
{
 unsigned short *temp;
 int i,j;
 temp=(unsigned short *)b;
 for(i=0;i<256;i++){
  outword(0x1f0,temp[i]);
  for(j=0;j<10;j++);
 }
 outbyte(0x1f7,0xe7);
}

canonical_original_code:
void write_data(char *b)
{
 unsigned short *temp;
 int i; int j;
 temp=(unsigned short *)b;
 for(i=0;i<256;i++){
  outword(0x1f0,temp[i]);
  for(j=0;j<10;j++);
 }
 outbyte(0x1f7,0xe7);
}

variable_types:
b
char *

variable_types: j int


Bwrite

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

init_opreation(a1, 1);
result = check_flags();
if ( (_DWORD)result == 1 )
result = write_data(a2);
return result;
}

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

func1(a1, 1);
result = func2();
if ( (_DWORD)result == 1 )
result = func4(a2);
return result;
}

original_code:
void Bwrite(unsigned int addr,char *buffer)
{
 int flag;
 init_opreation(addr,1);
 flag=check_flags();
 if(flag==1){
  write_data(buffer);
 }
 else{
 }
}

canonical_original_code:
void Bwrite(unsigned int addr, char *buffer)
{
 int flag;
 init_opreation(addr,1);
 flag=check_flags();
 if(flag==1){
  write_data(buffer);
 }
 else{
 }
}


copyd

original_code:
void copyd(char *source,int offset,char *dest,int count)
{
 int i;
 char *true_source;
 true_source=source+offset;
 for(i=0;i<count;i++){
  *(dest++)=*(true_source++);
 }
}

canonical_original_code:
void copyd(char *source, int offset, char *dest, int count)
{
 int i;
 char *true_source;
 true_source=source+offset;
 for(i=0;i<count;i++){
  *(dest++)=*(true_source++);
 }
}

variable_types:
source
char *

variable_types: offset int

variable_types:
dest
char *

variable_types: count int


repo: wangguibao/programmingwithposixthreads

decompiled_code:
void *__fastcall thread_routine(void *a1)
{
return a1;
}

canonical_decompiled_code:
void *__fastcall func7(void *a1)
{
return a1;
}

original_code:
void *thread_routine(void *arg)
{
    return arg;
}

variable_types:
arg
void *

return_type: void *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v4; // eax
char *v6; // rax
__int64 v7; // r8
const char *v8; // rdx
const char *v9; // r9
char *v10; // rax
pthread_t th; // [rsp+0h] [rbp-18h] BYREF
void *thread_return; // [rsp+8h] [rbp-10h] BYREF

v3 = pthread_create(&th, 0LL, thread_routine, 0LL);
if ( v3 )
{
v6 = strerror(v3);
v7 = 17LL;
v8 = "pthread_create() error";
v9 = v6;
goto LABEL_5;
}
v4 = pthread_join(th, &thread_return);
if ( v4 )
{
v10 = strerror(v4);
v7 = 22LL;
v8 = "pthread_join() error";
v9 = v10;
LABEL_5:
fprintf(stderr, "%s at \"%s\":%d: %s\n", v8, "lifecycle.c", v7, v9);
abort();
}
return thread_return != 0LL;
}

original_code:
int main(int argc, char **argv)
{
    pthread_t thread_id;
    void *thread_result;
    int status;
    status = pthread_create(&thread_id, ((void *)0), thread_routine, ((void *)0));
    if (status != 0) {
 do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "pthread_create() error", "lifecycle.c", 17, strerror (status)); abort (); } while (0);
    }
    status = pthread_join(thread_id, &thread_result);
    if (status != 0) {
 do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "pthread_join() error", "lifecycle.c", 22, strerror (status)); abort (); } while (0);
    }
    return !(thread_result == ((void *)0));
}

canonical_original_code:
int main(int argc, char **argv)
{
    unsigned long thread_id;
    void *thread_result;
    int status;
    status = pthread_create(&thread_id, ((void *)0), thread_routine, ((void *)0));
    if (status != 0) {
 do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "pthread_create() error", "lifecycle.c", 17, strerror (status)); abort (); } while (0);
    }
    status = pthread_join(thread_id, &thread_result);
    if (status != 0) {
 do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "pthread_join() error", "lifecycle.c", 22, strerror (status)); abort (); } while (0);
    }
    return !(thread_result == ((void *)0));
}

variable_types: argc int

variable_types:
argv
char **

variable_types: status int


decompiled_code:
void *__fastcall counter_thread(void *a1)
{
int v1; // eax
int v2; // eax
char *v4; // rax
__int64 v5; // r8
const char *v6; // rdx
const char *v7; // r9
char *v8; // rax

while ( time(0LL) < end_time )
{
v1 = pthread_mutex_lock(&mutex);
if ( v1 )
{
v4 = strerror(v1);
v5 = 27LL;
v6 = "Lock mutex";
v7 = v4;
goto LABEL_8;
}
counter += 10000000LL;
v2 = pthread_mutex_unlock(&mutex);
if ( v2 )
{
v8 = strerror(v2);
v5 = 35LL;
v6 = "Unlock mutex";
v7 = v8;
LABEL_8:
fprintf(_bss_start, "%s at \"%s\":%d: %s\n", v6, "trylock.c", v5, v7);
abort();
}
sleep(1u);
}
printf("Counter is %#lx\n", counter);
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func7(void *a1)
{
int v1; // eax
int v2; // eax
char *v4; // rax
__int64 v5; // r8
const char *v6; // rdx
const char *v7; // r9
char *v8; // rax

while ( time(0LL) < end_time )
{
v1 = pthread_mutex_lock(&mutex);
if ( v1 )
{
v4 = strerror(v1);
v5 = 27LL;
v6 = "Lock mutex";
v7 = v4;
goto LABEL_8;
}
counter += 10000000LL;
v2 = pthread_mutex_unlock(&mutex);
if ( v2 )
{
v8 = strerror(v2);
v5 = 35LL;
v6 = "Unlock mutex";
v7 = v8;
LABEL_8:
fprintf(_bss_start, "%s at \"%s\":%d: %s\n", v6, "trylock.c", v5, v7);
abort();
}
sleep(1u);
}
printf("Counter is %#lx\n", counter);
return 0LL;
}

original_code:
void *counter_thread(void *arg)
{
    int status;
    int spin;
    while (time(((void *)0)) < end_time) {
 if ((status = pthread_mutex_lock(&mutex)) != 0) {
     do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "Lock mutex", "trylock.c", 27, strerror (status)); abort (); } while (0);
 }
 for (spin = 0; spin < 10000000; spin++) {
     counter++;
 }
 if ((status = pthread_mutex_unlock(&mutex)) != 0) {
     do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "Unlock mutex", "trylock.c", 35, strerror (status)); abort (); } while (0);
 }
 sleep(1);
    }
    printf("Counter is %#lx\n", counter);
    return ((void *)0);
}

variable_types: spin int


decompiled_code:
void *__fastcall monitor_thread(void *a1)
{
unsigned int v1; // ebp
int v2; // eax
int v3; // eax
char *v4; // rax
__int64 v5; // r8
const char *v6; // rdx
const char *v7; // r9
char *v9; // rax

v1 = 0;
while ( time(0LL) < end_time )
{
while ( 1 )
{
sleep(3u);
v2 = pthread_mutex_trylock(&mutex);
if ( v2 != 16 )
break;
++v1;
if ( time(0LL) >= end_time )
goto LABEL_9;
}
if ( v2 )
{
v9 = strerror(v2);
v5 = 62LL;
v6 = "Trylock";
v7 = v9;
LABEL_7:
fprintf(_bss_start, "%s at \"%s\":%d: %s\n", v6, "trylock.c", v5, v7);
abort();
}
printf("Counter is %ld\n", counter / 10000000);
v3 = pthread_mutex_unlock(&mutex);
if ( v3 )
{
v4 = strerror(v3);
v5 = 66LL;
v6 = "Unlock mutex";
v7 = v4;
goto LABEL_7;
}
}
LABEL_9:
printf("Monitor thread missed update %d times\n", v1);
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func8(void *a1)
{
unsigned int v1; // ebp
int v2; // eax
int v3; // eax
char *v4; // rax
__int64 v5; // r8
const char *v6; // rdx
const char *v7; // r9
char *v9; // rax

v1 = 0;
while ( time(0LL) < end_time )
{
while ( 1 )
{
sleep(3u);
v2 = pthread_mutex_trylock(&mutex);
if ( v2 != 16 )
break;
++v1;
if ( time(0LL) >= end_time )
goto LABEL_9;
}
if ( v2 )
{
v9 = strerror(v2);
v5 = 62LL;
v6 = "Trylock";
v7 = v9;
LABEL_7:
fprintf(_bss_start, "%s at \"%s\":%d: %s\n", v6, "trylock.c", v5, v7);
abort();
}
printf("Counter is %ld\n", counter / 10000000);
v3 = pthread_mutex_unlock(&mutex);
if ( v3 )
{
v4 = strerror(v3);
v5 = 66LL;
v6 = "Unlock mutex";
v7 = v4;
goto LABEL_7;
}
}
LABEL_9:
printf("Monitor thread missed update %d times\n", v1);
return 0LL;
}

original_code:
void *monitor_thread(void *arg)
{
    int status;
    int misses = 0;
    while (time(((void *)0)) < end_time) {
 sleep(3);
 status = pthread_mutex_trylock(&mutex);
 if (status != 16) {
     if (status != 0) {
  do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "Trylock", "trylock.c", 62, strerror (status)); abort (); } while (0);
     }
     printf("Counter is %ld\n", counter/10000000);
     if ((status = pthread_mutex_unlock(&mutex)) != 0) {
  do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "Unlock mutex", "trylock.c", 66, strerror (status)); abort (); } while (0);
     }
 }
 else {
     misses++;
 }
    }
    printf("Monitor thread missed update %d times\n", misses);
    return ((void *)0);
}

variable_types: misses int


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v4; // eax
int v5; // eax
int v6; // eax
char *v8; // rax
__int64 v9; // r8
const char *v10; // rdx
const char *v11; // r9
char *v12; // rax
char *v13; // rax
char *v14; // rax
pthread_t th; // [rsp+0h] [rbp-18h] BYREF
pthread_t newthread[2]; // [rsp+8h] [rbp-10h] BYREF

end_time = time(0LL) + 60;
v3 = pthread_create(&th, 0LL, counter_thread, 0LL);
if ( v3 )
{
v8 = strerror(v3);
v9 = 87LL;
v10 = "Create counter thread";
v11 = v8;
goto LABEL_7;
}
v4 = pthread_create(newthread, 0LL, monitor_thread, 0LL);
if ( v4 )
{
v14 = strerror(v4);
v9 = 92LL;
v10 = "Create monitor thread";
v11 = v14;
LABEL_7:
fprintf(_bss_start, "%s at \"%s\":%d: %s\n", v10, "trylock.c", v9, v11);
abort();
}
v5 = pthread_join(th, 0LL);
if ( v5 )
{
v13 = strerror(v5);
v9 = 96LL;
v10 = "Join couter thread";
v11 = v13;
goto LABEL_7;
}
v6 = pthread_join(newthread[0], 0LL);
if ( v6 )
{
v12 = strerror(v6);
v9 = 100LL;
v10 = "Join monitor thread";
v11 = v12;
goto LABEL_7;
}
return 0;
}

original_code:
int main(int argc, char **argv)
{
    int status;
    pthread_t counter_thread_id;
    pthread_t monitor_thread_id;
    end_time = time(((void *)0)) + 60;
    if ((status = pthread_create(&counter_thread_id, ((void *)0),
     counter_thread, ((void *)0))) != 0) {
 do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "Create counter thread", "trylock.c", 87, strerror (status)); abort (); } while (0);
    }
    if ((status = pthread_create(&monitor_thread_id, ((void *)0),
     monitor_thread, ((void *)0))) != 0) {
 do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "Create monitor thread", "trylock.c", 92, strerror (status)); abort (); } while (0);
    }
    if ((status = pthread_join(counter_thread_id, ((void *)0))) != 0) {
 do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "Join couter thread", "trylock.c", 96, strerror (status)); abort (); } while (0);
    }
    if ((status = pthread_join(monitor_thread_id, ((void *)0))) != 0) {
 do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "Join monitor thread", "trylock.c", 100, strerror (status)); abort (); } while (0);
    }
    return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
    int status;
    unsigned long counter_thread_id;
    unsigned long monitor_thread_id;
    end_time = time(((void *)0)) + 60;
    if ((status = pthread_create(&counter_thread_id, ((void *)0),
     counter_thread, ((void *)0))) != 0) {
 do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "Create counter thread", "trylock.c", 87, strerror (status)); abort (); } while (0);
    }
    if ((status = pthread_create(&monitor_thread_id, ((void *)0),
     monitor_thread, ((void *)0))) != 0) {
 do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "Create monitor thread", "trylock.c", 92, strerror (status)); abort (); } while (0);
    }
    if ((status = pthread_join(counter_thread_id, ((void *)0))) != 0) {
 do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "Join couter thread", "trylock.c", 96, strerror (status)); abort (); } while (0);
    }
    if ((status = pthread_join(monitor_thread_id, ((void *)0))) != 0) {
 do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "Join monitor thread", "trylock.c", 100, strerror (status)); abort (); } while (0);
    }
    return 0;
}


decompiled_code:
void *__fastcall wait_thread(void *a1)
{
int v1; // eax
int v2; // eax
int v3; // eax
char *v5; // rax
__int64 v6; // r8
const char *v7; // rdx
const char *v8; // r9
char *v9; // rax
char *v10; // rax

sleep(hibernation);
v1 = pthread_mutex_lock(&data);
if ( v1 )
{
v5 = strerror(v1);
v6 = 33LL;
v7 = "Lock mutex";
v8 = v5;
goto LABEL_6;
}
dword_404118 = 1;
v2 = pthread_cond_signal(&cond);
if ( v2 )
{
v10 = strerror(v2);
v6 = 39LL;
v7 = "Signal condition";
v8 = v10;
LABEL_6:
fprintf(stderr, "%s at \"%s\":%d: %s\n", v7, "cond.c", v6, v8);
abort();
}
v3 = pthread_mutex_unlock(&data);
if ( v3 )
{
v9 = strerror(v3);
v6 = 44LL;
v7 = "Unlock mutex";
v8 = v9;
goto LABEL_6;
}
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func7(void *a1)
{
int v1; // eax
int v2; // eax
int v3; // eax
char *v5; // rax
__int64 v6; // r8
const char *v7; // rdx
const char *v8; // r9
char *v9; // rax
char *v10; // rax

sleep(hibernation);
v1 = pthread_mutex_lock(&data);
if ( v1 )
{
v5 = strerror(v1);
v6 = 33LL;
v7 = "Lock mutex";
v8 = v5;
goto LABEL_6;
}
dword_404118 = 1;
v2 = pthread_cond_signal(&cond);
if ( v2 )
{
v10 = strerror(v2);
v6 = 39LL;
v7 = "Signal condition";
v8 = v10;
LABEL_6:
fprintf(stderr, "%s at \"%s\":%d: %s\n", v7, "cond.c", v6, v8);
abort();
}
v3 = pthread_mutex_unlock(&data);
if ( v3 )
{
v9 = strerror(v3);
v6 = 44LL;
v7 = "Unlock mutex";
v8 = v9;
goto LABEL_6;
}
return 0LL;
}

original_code:
void *wait_thread(void *arg)
{
    int status;
    sleep(hibernation);
    status = pthread_mutex_lock(&data.mutex);
    if (status != 0) {
        do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "Lock mutex", "cond.c", 33, strerror (status)); abort (); } while (0);
    }
    data.value = 1;
    status = pthread_cond_signal(&data.cond);
    if (status != 0) {
        do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "Signal condition", "cond.c", 39, strerror (status)); abort (); } while (0);
    }
    status = pthread_mutex_unlock(&data.mutex);
    if (status != 0) {
        do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "Unlock mutex", "cond.c", 44, strerror (status)); abort (); } while (0);
    }
    return ((void *)0);
}


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
time_t v4; // rax
int v5; // eax
int v6; // eax
int v7; // eax
char *v9; // rax
char *v10; // rax
__int64 v11; // r8
const char *v12; // rdx
const char *v13; // r9
char *v14; // rax
char *v15; // rax
pthread_t newthread; // [rsp+8h] [rbp-20h] BYREF
struct timespec abstime; // [rsp+10h] [rbp-18h] BYREF

if ( argc > 1 )
hibernation = atoi(argv[1]);
v3 = pthread_create(&newthread, 0LL, wait_thread, 0LL);
if ( v3 )
{
v14 = strerror(v3);
v11 = 71LL;
v12 = "Create wait thread";
v13 = v14;
goto LABEL_15;
}
v4 = time(0LL);
abstime.tv_nsec = 0LL;
abstime.tv_sec = v4 + 2;
v5 = pthread_mutex_lock(&data);
if ( v5 )
{
v10 = strerror(v5);
v11 = 84LL;
v12 = "Lock mutex";
v13 = v10;
goto LABEL_15;
}
while ( 1 )
{
if ( dword_404118 )
goto LABEL_9;
v6 = pthread_cond_timedwait(&cond, &data, &abstime);
if ( v6 == 110 )
break;
if ( v6 )
{
v9 = strerror(v6);
v11 = 95LL;
v12 = "Wait on condition";
v13 = v9;
goto LABEL_15;
}
}
puts("Condition wait timed out");
if ( dword_404118 )
LABEL_9:
puts("Condition was signaled.");
v7 = pthread_mutex_unlock(&data);
if ( v7 )
{
v15 = strerror(v7);
v11 = 105LL;
v12 = "Unlock mutex";
v13 = v15;
LABEL_15:
fprintf(stderr, "%s at \"%s\":%d: %s\n", v12, "cond.c", v11, v13);
abort();
}
return 0;
}

original_code:
int main(int argc, char *argv[])
{
    int status;
    pthread_t wait_thread_id;
    struct timespec timeout;
    if (argc > 1) {
        hibernation = atoi(argv[1]);
    }
    status = pthread_create(&wait_thread_id, ((void *)0), wait_thread, ((void *)0));
    if (status != 0) {
        do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "Create wait thread", "cond.c", 71, strerror (status)); abort (); } while (0);
    }
    timeout.tv_sec = time(((void *)0)) + 2;
    timeout.tv_nsec = 0;
    status = pthread_mutex_lock(&data.mutex);
    if (status != 0) {
        do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "Lock mutex", "cond.c", 84, strerror (status)); abort (); } while (0);
    }
    while (data.value == 0) {
        status = pthread_cond_timedwait(
                &data.cond, &data.mutex, &timeout);
        if (status == 110) {
            printf("Condition wait timed out\n");
            break;
        }
        else if (status != 0) {
            do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "Wait on condition", "cond.c", 95, strerror (status)); abort (); } while (0);
        }
    }
    if (data.value != 0) {
        printf("Condition was signaled.\n");
    }
    status = pthread_mutex_unlock(&data.mutex);
    if (status != 0) {
        do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "Unlock mutex", "cond.c", 105, strerror (status)); abort (); } while (0);
    }
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    int status;
    unsigned long wait_thread_id;
    struct timespec timeout;
    if (argc > 1) {
        hibernation = atoi(argv[1]);
    }
    status = pthread_create(&wait_thread_id, ((void *)0), wait_thread, ((void *)0));
    if (status != 0) {
        do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "Create wait thread", "cond.c", 71, strerror (status)); abort (); } while (0);
    }
    timeout.tv_sec = time(((void *)0)) + 2;
    timeout.tv_nsec = 0;
    status = pthread_mutex_lock(&data.mutex);
    if (status != 0) {
        do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "Lock mutex", "cond.c", 84, strerror (status)); abort (); } while (0);
    }
    while (data.value == 0) {
        status = pthread_cond_timedwait(
                &data.cond, &data.mutex, &timeout);
        if (status == 110) {
            printf("Condition wait timed out\n");
            break;
        }
        else if (status != 0) {
            do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "Wait on condition", "cond.c", 95, strerror (status)); abort (); } while (0);
        }
    }
    if (data.value != 0) {
        printf("Condition was signaled.\n");
    }
    status = pthread_mutex_unlock(&data.mutex);
    if (status != 0) {
        do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "Unlock mutex", "cond.c", 105, strerror (status)); abort (); } while (0);
    }
    return 0;
}

variable_types:
argv
char *[]

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


decompiled_code:
void *__fastcall lock_forward(void *a1)
{
int v1; // er13
unsigned int v2; // er12
int v3; // ebx
pthread_mutex_t *v4; // rbp
int v5; // eax
int v6; // eax
int v7; // eax
int v9; // eax
char *v10; // rax
__int64 v11; // r8
const char *v12; // rdx
const char *v13; // r9
char *v14; // rax
char *v15; // rax

v1 = 10;
do
{
v2 = 0;
v3 = 0;
do
{
LABEL_10:
if ( v3 )
goto LABEL_3;
do
{
v7 = pthread_mutex_lock(&mutex);
if ( v7 )
{
v15 = strerror(v7);
v11 = 46LL;
v12 = "First lock";
v13 = v15;
LABEL_24:
fprintf(stderr, "%s at \"%s\":%d: %s\n", v12, "backoff.c", v11, v13);
abort();
}
v3 = 1;
if ( yield_flag )
{
if ( yield_flag > 0 )
goto LABEL_8;
goto LABEL_14;
}
LABEL_3:
v4 = &mutex + v3;
if ( !backoff )
{
v5 = pthread_mutex_lock(v4);
if ( v5 == 16 )
goto LABEL_18;
LABEL_5:
if ( v5 )
{
v14 = strerror(v5);
v11 = 68LL;
v12 = "Lock mutex";
v13 = v14;
goto LABEL_24;
}
v6 = yield_flag;
++v3;
if ( yield_flag )
goto LABEL_7;
LABEL_9:
if ( v3 == 3 )
goto LABEL_15;
goto LABEL_10;
}
v5 = pthread_mutex_trylock(v4);
if ( v5 != 16 )
goto LABEL_5;
LABEL_18:
++v2;
do
{
v9 = pthread_mutex_unlock(v4);
if ( v9 )
{
v10 = strerror(v9);
v11 = 62LL;
v12 = "Backoff";
v13 = v10;
goto LABEL_24;
}
--v3;
--v4;
}
while ( v3 != -1 );
}
while ( !yield_flag );
v6 = yield_flag;
v3 = 0;
LABEL_7:
if ( v6 > 0 )
{
LABEL_8:
sched_yield();
goto LABEL_9;
}
LABEL_14:
sleep(1u);
}
while ( v3 != 3 );
LABEL_15:
printf("lock forward got all locks. %d backoffs\n", v2);
pthread_mutex_unlock(&stru_404130);
pthread_mutex_unlock(&stru_404108);
pthread_mutex_unlock(&mutex);
sched_yield();
--v1;
}
while ( v1 );
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func7(void *a1)
{
int v1; // er13
unsigned int v2; // er12
int v3; // ebx
pthread_mutex_t *v4; // rbp
int v5; // eax
int v6; // eax
int v7; // eax
int v9; // eax
char *v10; // rax
__int64 v11; // r8
const char *v12; // rdx
const char *v13; // r9
char *v14; // rax
char *v15; // rax

v1 = 10;
do
{
v2 = 0;
v3 = 0;
do
{
LABEL_10:
if ( v3 )
goto LABEL_3;
do
{
v7 = pthread_mutex_lock(&mutex);
if ( v7 )
{
v15 = strerror(v7);
v11 = 46LL;
v12 = "First lock";
v13 = v15;
LABEL_24:
fprintf(stderr, "%s at \"%s\":%d: %s\n", v12, "backoff.c", v11, v13);
abort();
}
v3 = 1;
if ( yield_flag )
{
if ( yield_flag > 0 )
goto LABEL_8;
goto LABEL_14;
}
LABEL_3:
v4 = &mutex + v3;
if ( !backoff )
{
v5 = pthread_mutex_lock(v4);
if ( v5 == 16 )
goto LABEL_18;
LABEL_5:
if ( v5 )
{
v14 = strerror(v5);
v11 = 68LL;
v12 = "Lock mutex";
v13 = v14;
goto LABEL_24;
}
v6 = yield_flag;
++v3;
if ( yield_flag )
goto LABEL_7;
LABEL_9:
if ( v3 == 3 )
goto LABEL_15;
goto LABEL_10;
}
v5 = pthread_mutex_trylock(v4);
if ( v5 != 16 )
goto LABEL_5;
LABEL_18:
++v2;
do
{
v9 = pthread_mutex_unlock(v4);
if ( v9 )
{
v10 = strerror(v9);
v11 = 62LL;
v12 = "Backoff";
v13 = v10;
goto LABEL_24;
}
--v3;
--v4;
}
while ( v3 != -1 );
}
while ( !yield_flag );
v6 = yield_flag;
v3 = 0;
LABEL_7:
if ( v6 > 0 )
{
LABEL_8:
sched_yield();
goto LABEL_9;
}
LABEL_14:
sleep(1u);
}
while ( v3 != 3 );
LABEL_15:
printf("lock forward got all locks. %d backoffs\n", v2);
pthread_mutex_unlock(&stru_404130);
pthread_mutex_unlock(&stru_404108);
pthread_mutex_unlock(&mutex);
sched_yield();
--v1;
}
while ( v1 );
return 0LL;
}

original_code:
void *lock_forward(void *arg)
{
    int i, iterate, backoffs;
    int status;
    for (iterate = 0; iterate < 10; iterate++) {
        backoffs = 0;
        for (i = 0; i < 3; i++) {
            if (i == 0) {
                status = pthread_mutex_lock(&mutex[i]);
                if (status != 0) {
                    do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "First lock", "backoff.c", 46, strerror (status)); abort (); } while (0);
                }
            }
            else {
                if (backoff) {
                    status = pthread_mutex_trylock(&mutex[i]);
                }
                else {
                    status = pthread_mutex_lock(&mutex[i]);
                }
                if (status == 16) {
                    backoffs++;
                    ;
                    for (; i >= 0; i--) {
                        status = pthread_mutex_unlock(&mutex[i]);
                        if (status != 0) {
                            do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "Backoff", "backoff.c", 62, strerror (status)); abort (); } while (0);
                        }
                    }
                }
                else {
                    if (status != 0) {
                        do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "Lock mutex", "backoff.c", 68, strerror (status)); abort (); } while (0);
                    }
                    ;
                }
            }
            if (yield_flag) {
                if (yield_flag > 0) {
                    sched_yield();
                }
                else {
                    sleep(1);
                }
            }
        }
        printf("lock forward got all locks. %d backoffs\n", backoffs);
        pthread_mutex_unlock(&mutex[2]);
        pthread_mutex_unlock(&mutex[1]);
        pthread_mutex_unlock(&mutex[0]);
        sched_yield();
    }
    return ((void *)0);
}

canonical_original_code:
void *lock_forward(void *arg)
{
    int i; int iterate; int backoffs;
    int status;
    for (iterate = 0; iterate < 10; iterate++) {
        backoffs = 0;
        for (i = 0; i < 3; i++) {
            if (i == 0) {
                status = pthread_mutex_lock(&mutex[i]);
                if (status != 0) {
                    do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "First lock", "backoff.c", 46, strerror (status)); abort (); } while (0);
                }
            }
            else {
                if (backoff) {
                    status = pthread_mutex_trylock(&mutex[i]);
                }
                else {
                    status = pthread_mutex_lock(&mutex[i]);
                }
                if (status == 16) {
                    backoffs++;
                    ;
                    for (; i >= 0; i--) {
                        status = pthread_mutex_unlock(&mutex[i]);
                        if (status != 0) {
                            do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "Backoff", "backoff.c", 62, strerror (status)); abort (); } while (0);
                        }
                    }
                }
                else {
                    if (status != 0) {
                        do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "Lock mutex", "backoff.c", 68, strerror (status)); abort (); } while (0);
                    }
                    ;
                }
            }
            if (yield_flag) {
                if (yield_flag > 0) {
                    sched_yield();
                }
                else {
                    sleep(1);
                }
            }
        }
        printf("lock forward got all locks. %d backoffs\n", backoffs);
        pthread_mutex_unlock(&mutex[2]);
        pthread_mutex_unlock(&mutex[1]);
        pthread_mutex_unlock(&mutex[0]);
        sched_yield();
    }
    return ((void *)0);
}

variable_types: i int

variable_types: iterate int

variable_types: backoffs int


decompiled_code:
void *__fastcall lock_backward(void *a1)
{
int v1; // er13
unsigned int v2; // er12
int v3; // ebx
int v4; // eax
int v5; // eax
pthread_mutex_t *v6; // rbp
int v7; // eax
int v9; // eax
char *v10; // rax
__int64 v11; // r8
const char *v12; // rdx
const char *v13; // r9
char *v14; // rax
char *v15; // rax

v1 = 10;
do
{
v2 = 0;
v3 = 2;
do
{
do
{
while ( v3 != 2 )
{
v6 = &mutex + v3;
if ( backoff )
{
v4 = pthread_mutex_trylock(v6);
if ( v4 == 16 )
goto LABEL_12;
}
else
{
v4 = pthread_mutex_lock(v6);
if ( v4 == 16 )
{
LABEL_12:
++v2;
while ( 1 )
{
v7 = pthread_mutex_unlock(v6);
if ( v7 )
break;
++v3;
++v6;
if ( v3 == 3 )
{
v3 = 2;
goto LABEL_21;
}
}
v10 = strerror(v7);
v11 = 129LL;
v12 = "Backoff";
v13 = v10;
LABEL_24:
fprintf(stderr, "%s at \"%s\":%d: %s\n", v12, "backoff.c", v11, v13);
abort();
}
}
if ( v4 )
{
v14 = strerror(v4);
v11 = 135LL;
v12 = "Lock mutex";
v13 = v14;
goto LABEL_24;
}
v5 = yield_flag;
--v3;
if ( yield_flag )
goto LABEL_6;
LABEL_8:
if ( v3 == -1 )
goto LABEL_17;
}
v9 = pthread_mutex_lock(&stru_404130);
if ( v9 )
{
v15 = strerror(v9);
v11 = 113LL;
v12 = "First lock";
v13 = v15;
goto LABEL_24;
}
v3 = 1;
LABEL_21:
v5 = yield_flag;
}
while ( !yield_flag );
LABEL_6:
if ( v5 > 0 )
{
sched_yield();
goto LABEL_8;
}
sleep(1u);
}
while ( v3 != -1 );
LABEL_17:
printf("lock backword got all locks. %d backoffs\n", v2);
pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(&stru_404108);
pthread_mutex_unlock(&stru_404130);
sched_yield();
--v1;
}
while ( v1 );
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func8(void *a1)
{
int v1; // er13
unsigned int v2; // er12
int v3; // ebx
int v4; // eax
int v5; // eax
pthread_mutex_t *v6; // rbp
int v7; // eax
int v9; // eax
char *v10; // rax
__int64 v11; // r8
const char *v12; // rdx
const char *v13; // r9
char *v14; // rax
char *v15; // rax

v1 = 10;
do
{
v2 = 0;
v3 = 2;
do
{
do
{
while ( v3 != 2 )
{
v6 = &mutex + v3;
if ( backoff )
{
v4 = pthread_mutex_trylock(v6);
if ( v4 == 16 )
goto LABEL_12;
}
else
{
v4 = pthread_mutex_lock(v6);
if ( v4 == 16 )
{
LABEL_12:
++v2;
while ( 1 )
{
v7 = pthread_mutex_unlock(v6);
if ( v7 )
break;
++v3;
++v6;
if ( v3 == 3 )
{
v3 = 2;
goto LABEL_21;
}
}
v10 = strerror(v7);
v11 = 129LL;
v12 = "Backoff";
v13 = v10;
LABEL_24:
fprintf(stderr, "%s at \"%s\":%d: %s\n", v12, "backoff.c", v11, v13);
abort();
}
}
if ( v4 )
{
v14 = strerror(v4);
v11 = 135LL;
v12 = "Lock mutex";
v13 = v14;
goto LABEL_24;
}
v5 = yield_flag;
--v3;
if ( yield_flag )
goto LABEL_6;
LABEL_8:
if ( v3 == -1 )
goto LABEL_17;
}
v9 = pthread_mutex_lock(&stru_404130);
if ( v9 )
{
v15 = strerror(v9);
v11 = 113LL;
v12 = "First lock";
v13 = v15;
goto LABEL_24;
}
v3 = 1;
LABEL_21:
v5 = yield_flag;
}
while ( !yield_flag );
LABEL_6:
if ( v5 > 0 )
{
sched_yield();
goto LABEL_8;
}
sleep(1u);
}
while ( v3 != -1 );
LABEL_17:
printf("lock backword got all locks. %d backoffs\n", v2);
pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(&stru_404108);
pthread_mutex_unlock(&stru_404130);
sched_yield();
--v1;
}
while ( v1 );
return 0LL;
}

original_code:
void *lock_backward(void *arg)
{
    int i, iterate, backoffs;
    int status;
    for (iterate = 0; iterate < 10; iterate++) {
        backoffs = 0;
        for (i = 2; i >= 0; i--) {
            if (i == 2) {
                status = pthread_mutex_lock(&mutex[i]);
                if (status != 0) {
                    do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "First lock", "backoff.c", 113, strerror (status)); abort (); } while (0);
                }
            }
            else {
                if (backoff) {
                    status = pthread_mutex_trylock(&mutex[i]);
                }
                else {
                    status = pthread_mutex_lock(&mutex[i]);
                }
                if (status == 16) {
                    backoffs++;
                    ;
                    for (; i < 3; i++) {
                        status = pthread_mutex_unlock(&mutex[i]);
                        if (status != 0) {
                            do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "Backoff", "backoff.c", 129, strerror (status)); abort (); } while (0);
                        }
                    }
                }
                else {
                    if (status != 0) {
                        do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "Lock mutex", "backoff.c", 135, strerror (status)); abort (); } while (0);
                    }
                    ;
                }
            }
            if (yield_flag) {
                if (yield_flag > 0) {
                    sched_yield();
                }
                else {
                    sleep(1);
                }
            }
        }
        printf("lock backword got all locks. %d backoffs\n", backoffs);
        pthread_mutex_unlock(&mutex[0]);
        pthread_mutex_unlock(&mutex[1]);
        pthread_mutex_unlock(&mutex[2]);
        sched_yield();
    }
    return ((void *)0);
}

canonical_original_code:
void *lock_backward(void *arg)
{
    int i; int iterate; int backoffs;
    int status;
    for (iterate = 0; iterate < 10; iterate++) {
        backoffs = 0;
        for (i = 2; i >= 0; i--) {
            if (i == 2) {
                status = pthread_mutex_lock(&mutex[i]);
                if (status != 0) {
                    do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "First lock", "backoff.c", 113, strerror (status)); abort (); } while (0);
                }
            }
            else {
                if (backoff) {
                    status = pthread_mutex_trylock(&mutex[i]);
                }
                else {
                    status = pthread_mutex_lock(&mutex[i]);
                }
                if (status == 16) {
                    backoffs++;
                    ;
                    for (; i < 3; i++) {
                        status = pthread_mutex_unlock(&mutex[i]);
                        if (status != 0) {
                            do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "Backoff", "backoff.c", 129, strerror (status)); abort (); } while (0);
                        }
                    }
                }
                else {
                    if (status != 0) {
                        do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "Lock mutex", "backoff.c", 135, strerror (status)); abort (); } while (0);
                    }
                    ;
                }
            }
            if (yield_flag) {
                if (yield_flag > 0) {
                    sched_yield();
                }
                else {
                    sleep(1);
                }
            }
        }
        printf("lock backword got all locks. %d backoffs\n", backoffs);
        pthread_mutex_unlock(&mutex[0]);
        pthread_mutex_unlock(&mutex[1]);
        pthread_mutex_unlock(&mutex[2]);
        sched_yield();
    }
    return ((void *)0);
}


decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v4; // eax
char *v5; // rax
__int64 v6; // r8
const char *v7; // rdx
const char *v8; // r9
char *v9; // rax
pthread_t v10; // [rsp+0h] [rbp-28h] BYREF
pthread_t newthread[4]; // [rsp+8h] [rbp-20h] BYREF

if ( argc > 1 )
{
backoff = atoi(argv[1]);
if ( argc != 2 )
yield_flag = atoi(argv[2]);
}
v3 = pthread_create(&v10, 0LL, lock_forward, 0LL);
if ( v3 )
{
v5 = strerror(v3);
v6 = 195LL;
v7 = "Create forward";
v8 = v5;
}
else
{
v4 = pthread_create(newthread, 0LL, lock_backward, 0LL);
if ( !v4 )
pthread_exit(0LL);
v9 = strerror(v4);
v6 = 199LL;
v7 = "Create backword";
v8 = v9;
}
fprintf(stderr, "%s at \"%s\":%d: %s\n", v7, "backoff.c", v6, v8);
abort();
}

original_code:
int main(int argc, char **argv)
{
    pthread_t forward, backword;
    int status;
    if (argc > 1) {
        backoff = atoi(argv[1]);
    }
    if (argc > 2) {
        yield_flag = atoi(argv[2]);
    }
    status = pthread_create(&forward, ((void *)0), lock_forward, ((void *)0));
    if (status != 0) {
        do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "Create forward", "backoff.c", 195, strerror (status)); abort (); } while (0);
    }
    status = pthread_create(&backword, ((void *)0), lock_backward, ((void *)0));
    if (status != 0) {
        do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "Create backword", "backoff.c", 199, strerror (status)); abort (); } while (0);
    }
    pthread_exit(((void *)0));
}

canonical_original_code:
int main(int argc, char **argv)
{
    unsigned long forward; unsigned long backword;
    int status;
    if (argc > 1) {
        backoff = atoi(argv[1]);
    }
    if (argc > 2) {
        yield_flag = atoi(argv[2]);
    }
    status = pthread_create(&forward, ((void *)0), lock_forward, ((void *)0));
    if (status != 0) {
        do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "Create forward", "backoff.c", 195, strerror (status)); abort (); } while (0);
    }
    status = pthread_create(&backword, ((void *)0), lock_backward, ((void *)0));
    if (status != 0) {
        do { fprintf (stderr, "%s at \"%s\":%d: %s\n", "Create backword", "backoff.c", 199, strerror (status)); abort (); } while (0);
    }
    pthread_exit(((void *)0));
}

variable_types:
forward
unsigned long

variable_types:
backword
unsigned long


repo: wangkendy/thesis

decompiled_code:
void sigchld_handler()
{
while ( waitpid(-1, 0LL, 1) > 0 )
;
}

canonical_decompiled_code:
void func7()
{
while ( waitpid(-1, 0LL, 1) > 0 )
;
}

original_code:
void sigchld_handler(int sig)
{
    while (waitpid(-1, 0, 1) > 0)
        ;
    return;
}

variable_types: sig int


decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // ebp
int v4; // er12
int v5; // ebp
socklen_t addr_len; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

addr_len = 16;
if ( argc != 2 )
{
fprintf(_bss_start, "usage: %s <port>\n", *argv);
exit(0);
}
v3 = atoi(argv[1]);
signal(17, sigchld_handler);
v4 = open_listenfd(v3);
while ( 1 )
{
v5 = accept(v4, &addr, &addr_len);
if ( !fork() )
break;
close(v5);
}
close(v4);
echo(v5);
close(v5);
exit(0);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // ebp
int v4; // er12
int v5; // ebp
socklen_t addr_len; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

addr_len = 16;
if ( argc != 2 )
{
fprintf(_bss_start, "usage: %s <port>\n", *argv);
exit(0);
}
v3 = atoi(argv[1]);
signal(17, sigchld_handler);
v4 = func10(v3);
while ( 1 )
{
v5 = accept(v4, &addr, &addr_len);
if ( !fork() )
break;
close(v5);
}
close(v4);
func11(v5);
close(v5);
exit(0);
}

original_code:
int main(int argc, char **argv)
{
    int listenfd, connfd, port;
    socklen_t client_len = sizeof(struct sockaddr_in);
    struct sockaddr_in client_addr;
    if (argc != 2) {
        fprintf(stderr, "usage: %s <port>\n", argv[0]);
        exit(0);
    }
    port = atoi(argv[1]);
    signal(17, sigchld_handler);
    listenfd = open_listenfd(port);
    while (1) {
        connfd = accept(listenfd, (struct sockaddr*)&client_addr, &client_len);
        if (fork() == 0) {
            close(listenfd);
            echo(connfd);
            close(connfd);
            exit(0);
        }
        close(connfd);
    }
    return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
    int listenfd; int connfd; int port;
    unsigned int client_len = sizeof(struct sockaddr_in);
    struct sockaddr_in client_addr;
    if (argc != 2) {
        fprintf(stderr, "usage: %s <port>\n", argv[0]);
        exit(0);
    }
    port = atoi(argv[1]);
    signal(17, sigchld_handler);
    listenfd = open_listenfd(port);
    while (1) {
        connfd = accept(listenfd, (struct sockaddr *)&client_addr, &client_len);
        if (fork() == 0) {
            close(listenfd);
            echo(connfd);
            close(connfd);
            exit(0);
        }
        close(connfd);
    }
    return 0;
}

variable_types: listenfd int

variable_types: connfd int

variable_types: port int

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

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

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


service

decompiled_code:
void *__fastcall service(void *a1)
{
int v1; // er12
pthread_t v2; // rax

v1 = *(_DWORD *)a1;
v2 = pthread_self();
pthread_detach(v2);
free(a1);
echo(v1);
close(v1);
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func7(void *a1)
{
int v1; // er12
pthread_t v2; // rax

v1 = *(_DWORD *)a1;
v2 = pthread_self();
pthread_detach(v2);
free(a1);
func11(v1);
close(v1);
return 0LL;
}

original_code:
void *service(void *vargp)
{
    int connfd = *((int*)vargp);
    pthread_detach(pthread_self());
    free(vargp);
    echo(connfd);
    close(connfd);
    return ((void *)0);
}

variable_types:
vargp
void *


main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
int v4; // ebx
_DWORD *v5; // r12
socklen_t v6; // [rsp+0h] [rbp-78h] BYREF
size_t stacksize; // [rsp+4h] [rbp-74h] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-68h] BYREF
pthread_attr_t attr; // [rsp+20h] [rbp-58h] BYREF

v6 = 16;
pthread_attr_init(&attr);
LODWORD(stacksize) = 0x100000;
pthread_attr_setstacksize(&attr, 0x100000uLL);
pthread_attr_getstacksize(&attr, &stacksize);
printf("stack size :%d\n", (unsigned int)stacksize);
if ( argc != 2 )
{
fprintf(stderr, "usage: %s <port>\n", *argv);
exit(0);
}
v3 = atoi(argv[1]);
v4 = open_listenfd(v3);
while ( 1 )
{
v5 = malloc(4uLL);
*v5 = accept(v4, &addr, &v6);
pthread_create((size_t *)((char *)&stacksize + 4), &attr, service, v5);
}
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
int v4; // ebx
_DWORD *v5; // r12
socklen_t v6; // [rsp+0h] [rbp-78h] BYREF
size_t stacksize; // [rsp+4h] [rbp-74h] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-68h] BYREF
pthread_attr_t attr; // [rsp+20h] [rbp-58h] BYREF

v6 = 16;
pthread_attr_init(&attr);
LODWORD(stacksize) = 0x100000;
pthread_attr_setstacksize(&attr, 0x100000uLL);
pthread_attr_getstacksize(&attr, &stacksize);
printf("stack size :%d\n", (unsigned int)stacksize);
if ( argc != 2 )
{
fprintf(stderr, "usage: %s <port>\n", *argv);
exit(0);
}
v3 = atoi(argv[1]);
v4 = func10(v3);
while ( 1 )
{
v5 = malloc(4uLL);
*v5 = accept(v4, &addr, &v6);
pthread_create((size_t *)((char *)&stacksize + 4), &attr, service, v5);
}
}

original_code:
int main(int argc, char **argv)
{
    int listenfd, *connfdp, port;
    socklen_t clientlen = sizeof(struct sockaddr_in);
    struct sockaddr_in client_addr;
    pthread_t tid;
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    unsigned int size = 0;
    size = 1024 * 1024;
    pthread_attr_setstacksize(&attr,size);
    pthread_attr_getstacksize(&attr,&size);
    printf("stack size :%d\n",size);
    if (argc != 2) {
        fprintf(stderr, "usage: %s <port>\n", argv[0]);
        exit(0);
    }
    port = atoi(argv[1]);
    listenfd = open_listenfd(port);
    while (1) {
        connfdp = malloc(sizeof(int));
        *connfdp = accept(listenfd, (struct sockaddr*)&client_addr, &clientlen);
        pthread_create(&tid, &attr, service, connfdp);
    }
}

canonical_original_code:
int main(int argc, char **argv)
{
    int listenfd; int *connfdp; int port;
    unsigned int clientlen = sizeof(struct sockaddr_in);
    struct sockaddr_in client_addr;
    unsigned long tid;
    union pthread_attr_t attr;
    pthread_attr_init(&attr);
    unsigned int size = 0;
    size = 1024 * 1024;
    pthread_attr_setstacksize(&attr,size);
    pthread_attr_getstacksize(&attr,&size);
    printf("stack size :%d\n",size);
    if (argc != 2) {
        fprintf(stderr, "usage: %s <port>\n", argv[0]);
        exit(0);
    }
    port = atoi(argv[1]);
    listenfd = open_listenfd(port);
    while (1) {
        connfdp = malloc(sizeof(int));
        *connfdp = accept(listenfd, (struct sockaddr *)&client_addr, &clientlen);
        pthread_create(&tid, &attr, service, connfdp);
    }
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
connfdp
int *

variable_types:
clientlen
unsigned int

variable_types:
tid
unsigned long

variable_types: union pthread_attr_t { char __size[56]; long __align; }

variable_types:
size
unsigned int


maxargs

repo: wangxinalex/compiler

original_code:
int maxargs(A_stm prog, int *max){
 int args = 0;
 if(prog->kind == A_compoundStm){
  maxargs(prog->u.compound.stm1, max);
  maxargs(prog->u.compound.stm2, max);
 }else if(prog->kind == A_assignStm){
  if(prog->u.assign.exp->kind == A_eseqExp){
   A_stm ex_prog = prog->u.assign.exp->u.eseq.stm;
   maxargs(ex_prog, max);
  }
 }else if(prog->kind == A_printStm){
  A_expList list = prog->u.print.exps;
  while(list->kind != A_lastExpList){
   args++;
   if(list->u.pair.head->kind == A_eseqExp){
    A_stm ex_prog = list->u.pair.head->u.eseq.stm;
    maxargs(ex_prog, max);
   }
   list = list->u.pair.tail;
  }
  if(++args > *max){
   *max = args;
  }
 }
 return 0;
}

canonical_original_code:
int maxargs(struct A_stm_ *prog, int *max){
 int args = 0;
 if(prog->kind == A_compoundStm){
  maxargs(prog->u.compound.stm1, max);
  maxargs(prog->u.compound.stm2, max);
 }else if(prog->kind == A_assignStm){
  if(prog->u.assign.exp->kind == A_eseqExp){
   struct A_stm_ *ex_prog = prog->u.assign.exp->u.eseq.stm;
   maxargs(ex_prog, max);
  }
 }else if(prog->kind == A_printStm){
  struct A_expList_ *list = prog->u.print.exps;
  while(list->kind != A_lastExpList){
   args++;
   if(list->u.pair.head->kind == A_eseqExp){
    struct A_stm_ *ex_prog = list->u.pair.head->u.eseq.stm;
    maxargs(ex_prog, max);
   }
   list = list->u.pair.tail;
  }
  if(++args > *max){
   *max = args;
  }
 }
 return 0;
}

variable_types: struct A_stm_ { enum { A_compoundStm=0, A_assignStm=1, A_printStm=2 } kind; union { struct { struct A_stm_ *stm1; } compound; struct { char *id; struct A_exp_ { enum { A_idExp=0, A_numExp=1, A_opExp=2, A_eseqExp=3 } kind; union { char *id; int num; struct { struct A_exp_ *left; enum A_binop { A_plus=0, A_minus=1, A_times=2, A_div=3 } oper; struct A_exp_ *right; } op; struct { struct A_stm_ *stm; struct A_exp_ *exp; } eseq; } u; } *exp; } assign; struct { struct A_expList_ { enum { A_pairExpList=0, A_lastExpList=1 } kind; union { struct { struct A_exp_ *head; struct A_expList_ *tail; } pair; struct A_exp_ *last; } u; } *exps; } print; } u; } *

variable_types:
max
int *

variable_types: args int

variable_types: struct A_expList_ { enum { A_pairExpList=0, A_lastExpList=1 } kind; union { struct { struct A_exp_ { enum { A_idExp=0, A_numExp=1, A_opExp=2, A_eseqExp=3 } kind; union { char *id; int num; struct { struct A_exp_ *left; enum A_binop { A_plus=0, A_minus=1, A_times=2, A_div=3 } oper; struct A_exp_ *right; } op; struct { struct A_stm_ { enum { A_compoundStm=0, A_assignStm=1, A_printStm=2 } kind; union { struct { struct A_stm_ *stm1; } compound; struct { char *id; struct A_exp_ *exp; } assign; struct { struct A_expList_ *exps; } print; } u; } *stm; struct A_exp_ *exp; } eseq; } u; } *head; struct A_expList_ *tail; } pair; struct A_exp_ *last; } u; } *

user_defined_types: struct A_exp_ { enum { A_idExp=0, A_numExp=1, A_opExp=2, A_eseqExp=3 } kind; union { char *id; int num; struct { struct A_exp_ *left; enum A_binop oper; struct A_exp_ *right; } op; struct { struct A_stm_ *stm; struct A_exp_ *exp; } eseq; } u; }


lookup

decompiled_code:
__int64 __fastcall lookup(char *s2, __int64 a2)
{
__int64 v2; // rbx

if ( !a2 )
{
LABEL_6:
printf("[Error] Identifier %s does not exist!\n\n", s2);
exit(-1);
}
v2 = a2;
while ( strcmp(*(const char **)v2, s2) )
{
v2 = *(_QWORD *)(v2 + 16);
if ( !v2 )
goto LABEL_6;
}
return *(unsigned int *)(v2 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func1(char *s2, __int64 a2)
{
__int64 v2; // rbx

if ( !a2 )
{
LABEL_6:
printf("[Error] Identifier %s does not exist!\n\n", s2);
exit(-1);
}
v2 = a2;
while ( strcmp(*(const char **)v2, s2) )
{
v2 = *(_QWORD *)(v2 + 16);
if ( !v2 )
goto LABEL_6;
}
return *(unsigned int *)(v2 + 8);
}

original_code:
int lookup(string s, A_table t){
 while(t != ((void *)0)){
  if(strcmp(t->id, s)==0){
   return t->value;
  }
  t = t->tail;
 }
 printf("[Error] Identifier %s does not exist!\n\n",s);
 exit(-1);
}

canonical_original_code:
int lookup(char *s, struct A_table_ *t){
 while(t != ((void *)0)){
  if(strcmp(t->id, s)==0){
   return t->value;
  }
  t = t->tail;
 }
 printf("[Error] Identifier %s does not exist!\n\n",s);
 exit(-1);
}

variable_types:
s
char *

variable_types: struct A_table_ { char *id; int value; struct A_table_ *tail; } *


decompiled_code:
int __fastcall print_table(__int64 a1)
{
__int64 v1; // r14
const char *v2; // r15
const char **v3; // rbx
int v4; // ebp
__int64 v6; // rdx
int v7; // [rsp+Ch] [rbp-35Ch]
__int64 v8[107]; // [rsp+10h] [rbp-358h] BYREF

v1 = a1;
puts("\n Id:Value ");
puts("[^_^] Now dump Variable");
v7 = 0;
if ( a1 )
{
do
{
v2 = *(const char **)v1;
if ( !v7 )
goto LABEL_10;
v3 = (const char **)v8;
v4 = 0;
do
{
if ( !strcmp(*v3, v2) )
v4 = 1;
++v3;
}
while ( v3 != (const char **)&v8[(unsigned int)(v7 - 1) + 1] );
if ( !v4 )
{
LABEL_10:
v6 = *(unsigned int *)(v1 + 8);
v8[v7] = (__int64)v2;
printf(" %s=%d \n", v2, v6);
++v7;
}
v1 = *(_QWORD *)(v1 + 16);
}
while ( v1 );
}
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func2(__int64 a1)
{
__int64 v1; // r14
const char *v2; // r15
const char **v3; // rbx
int v4; // ebp
__int64 v6; // rdx
int v7; // [rsp+Ch] [rbp-35Ch]
__int64 v8[107]; // [rsp+10h] [rbp-358h] BYREF

v1 = a1;
puts("\n Id:Value ");
puts("[^_^] Now dump Variable");
v7 = 0;
if ( a1 )
{
do
{
v2 = *(const char **)v1;
if ( !v7 )
goto LABEL_10;
v3 = (const char **)v8;
v4 = 0;
do
{
if ( !strcmp(*v3, v2) )
v4 = 1;
++v3;
}
while ( v3 != (const char **)&v8[(unsigned int)(v7 - 1) + 1] );
if ( !v4 )
{
LABEL_10:
v6 = *(unsigned int *)(v1 + 8);
v8[v7] = (__int64)v2;
printf(" %s=%d \n", v2, v6);
++v7;
}
v1 = *(_QWORD *)(v1 + 16);
}
while ( v1 );
}
return putchar(10);
}

original_code:
void print_table(A_table t){
 printf("\n        Id:Value     \n");
 printf("[^_^] Now dump Variable\n");
 int index = 0, i = 0;
 string s_list[100];
 int is_dup = 0;
 while(t != ((void *)0)){
  is_dup = 0;
  for(i = 0; i < index; i++){
   if(strcmp(s_list[i], t->id)==0){
    is_dup = 1;
   }
  }
  if(is_dup == 0){
   s_list[index++] = t->id;
   printf("         %s=%d        \n", t->id, t->value);
  }
  t = t->tail;
 }
 printf("\n");
}

canonical_original_code:
void print_table(struct A_table_ *t){
 printf("\n        Id:Value     \n");
 printf("[^_^] Now dump Variable\n");
 int index = 0; int i = 0;
 char *s_list[100];
 int is_dup = 0;
 while(t != ((void *)0)){
  is_dup = 0;
  for(i = 0; i < index; i++){
   if(strcmp(s_list[i], t->id)==0){
    is_dup = 1;
   }
  }
  if(is_dup == 0){
   s_list[index++] = t->id;
   printf("         %s=%d        \n", t->id, t->value);
  }
  t = t->tail;
 }
 printf("\n");
}

variable_types: index int

variable_types: i int

user_defined_types: struct A_table_ { char *id; int value; struct A_table_ *tail; }


calculate

decompiled_code:
__int64 __fastcall calculate(int a1, int a2, unsigned int a3)
{
unsigned int v4; // edi
__int64 result; // rax

if ( a3 == 2 )
return (unsigned int)(a2 * a1);
if ( a3 > 2 )
{
if ( a3 == 3 )
{
if ( !a2 )
{
puts("[Error] divisor cannot be zero!\n");
exit(-2);
}
result = (unsigned int)(a1 / a2);
}
else
{
result = 0LL;
}
}
else
{
v4 = a1 - a2;
result = (unsigned int)(a2 + a1);
if ( a3 )
result = v4;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1, int a2, unsigned int a3)
{
unsigned int v4; // edi
__int64 result; // rax

if ( a3 == 2 )
return (unsigned int)(a2 * a1);
if ( a3 > 2 )
{
if ( a3 == 3 )
{
if ( !a2 )
{
puts("[Error] divisor cannot be zero!\n");
exit(-2);
}
result = (unsigned int)(a1 / a2);
}
else
{
result = 0LL;
}
}
else
{
v4 = a1 - a2;
result = (unsigned int)(a2 + a1);
if ( a3 )
result = v4;
}
return result;
}

original_code:
int calculate(int left, int right, A_binop op){
 int result;
 switch(op){
  case A_plus:
   result = left + right; break;
  case A_minus:
   result = left - right; break;
  case A_times:
   result = left * right; break;
  case A_div:
   if(right == 0){
    printf("[Error] divisor cannot be zero!\n\n");
    exit(-2);
   }
   result = left / right; break;
  default:
   result = 0;
 }
 return result;
}

canonical_original_code:
int calculate(int left, int right, enum A_binop op){
 int result;
 switch(op){
  case A_plus:
   result = left + right; break;
  case A_minus:
   result = left - right; break;
  case A_times:
   result = left * right; break;
  case A_div:
   if(right == 0){
    printf("[Error] divisor cannot be zero!\n\n");
    exit(-2);
   }
   result = left / right; break;
  default:
   result = 0;
 }
 return result;
}

variable_types: left int

variable_types: right int

variable_types: enum A_binop { A_plus=0, A_minus=1, A_times=2, A_div=3 }

variable_types: result int