decompiled_code:
__int64 __fastcall mask_32(int a1, int a2)
{
int v3; // edi
int v4; // er8
int v5; // eax
int v6; // er9

v3 = a2;
if ( a1 > a2 )
v3 = a1;
loop_mask_32(v3);
v5 = loop_mask_32(v4 - 1);
return v6 & (unsigned int)~v5;
}

canonical_decompiled_code:
__int64 __fastcall func8(int a1, int a2)
{
int v3; // edi
int v4; // er8
int v5; // eax
int v6; // er9

v3 = a2;
if ( a1 > a2 )
v3 = a1;
func7(v3);
v5 = func7(v4 - 1);
return v6 & (unsigned int)~v5;
}

original_code:
static uint32_t mask_32(int a, int b)
{
 if (a > b) {
  int t = a;
  a = b;
  b = t;
 }
 uint32_t b_mask = loop_mask_32(b);
 uint32_t a_mask = ~(loop_mask_32(a-1));
 return b_mask & a_mask;
}

canonical_original_code:
static unsigned int mask_32(int a, int b)
{
 if (a > b) {
  int t = a;
  a = b;
  b = t;
 }
 unsigned int b_mask = loop_mask_32(b);
 unsigned int a_mask = ~(loop_mask_32(a-1));
 return b_mask & a_mask;
}

variable_types: a int

variable_types: b int

variable_types: t int


repo: choueric/tools

decompiled_code:
__int64 __fastcall loop_mask_8(int a1)
{
int v1; // edi
int v2; // ecx
__int64 result; // rax
int v4; // edx

if ( a1 < 0 )
return 0LL;
v1 = a1 + 1;
v2 = 0;
LODWORD(result) = 0;
do
{
v4 = 1 << v2++;
result = v4 | (unsigned int)result;
}
while ( v1 != v2 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(int a1)
{
int v1; // edi
int v2; // ecx
__int64 result; // rax
int v4; // edx

if ( a1 < 0 )
return 0LL;
v1 = a1 + 1;
v2 = 0;
LODWORD(result) = 0;
do
{
v4 = 1 << v2++;
result = v4 | (unsigned int)result;
}
while ( v1 != v2 );
return result;
}

original_code:
static uint8_t loop_mask_8(int n)
{
 uint8_t val = 0;
 for (int i = 0; i <= n; i++)
  val |= (1 << i);
 return val;
}

canonical_original_code:
static unsigned char loop_mask_8(int n)
{
 unsigned char val = 0;
 for (int i = 0; i <= n; i++)
  val |= (1 << i);
 return val;
}

variable_types: n int

variable_types:
val
unsigned char

variable_types: i int

return_type: unsigned char


decompiled_code:
__int64 __fastcall mask_8(int a1, int a2)
{
int v2; // er8
int v3; // eax
int v4; // er9

if ( a1 > a2 )
goto LABEL_4;
if ( a1 != a2 )
{
a1 = a2;
LABEL_4:
loop_mask_8(a1);
v3 = loop_mask_8(v2 - 1);
return v4 & (unsigned int)~v3;
}
return (unsigned int)(1 << a1);
}

canonical_decompiled_code:
__int64 __fastcall func10(int a1, int a2)
{
int v2; // er8
int v3; // eax
int v4; // er9

if ( a1 > a2 )
goto LABEL_4;
if ( a1 != a2 )
{
a1 = a2;
LABEL_4:
func9(a1);
v3 = func9(v2 - 1);
return v4 & (unsigned int)~v3;
}
return (unsigned int)(1 << a1);
}

original_code:
static uint8_t mask_8(int a, int b)
{
 if (a > b) {
  int t = a;
  a = b;
  b = t;
 }
 if (a == b)
  return (1 << a);
 uint8_t b_mask = loop_mask_8(b);
 uint8_t a_mask = ~(loop_mask_8(a-1));
 return b_mask & a_mask;
}

canonical_original_code:
static unsigned char mask_8(int a, int b)
{
 if (a > b) {
  int t = a;
  a = b;
  b = t;
 }
 if (a == b)
  return (1 << a);
 unsigned char b_mask = loop_mask_8(b);
 unsigned char a_mask = ~(loop_mask_8(a-1));
 return b_mask & a_mask;
}


decompiled_code:
__int64 __fastcall read_nonblock(int a1, void *a2, size_t *a3)
{
ssize_t v4; // rax

v4 = read(a1, a2, *a3);
if ( v4 < 0 )
return (unsigned int)-*__errno_location();
*a3 = v4;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1, void *a2, size_t *a3)
{
ssize_t v4; // rax

v4 = read(a1, a2, *a3);
if ( v4 < 0 )
return (unsigned int)-*__errno_location();
*a3 = v4;
return 0LL;
}

original_code:
static int read_nonblock(int fd, void *data, size_t *p_nbytes)
{
 ssize_t ret;
 ret = read(fd, data, *p_nbytes);
    if (ret < 0)
  return -(*__errno_location ());
    *p_nbytes = ret;
 return 0;
}

canonical_original_code:
static int read_nonblock(int fd, void *data, unsigned long *p_nbytes)
{
 long ret;
 ret = read(fd, data, *p_nbytes);
    if (ret < 0)
  return -(*__errno_location ());
    *p_nbytes = ret;
 return 0;
}

variable_types: fd int

variable_types:
data
void *

variable_types: ret long

return_type: int


original_code:
static int select_read(context *ctx)
{
 fd_set fds;
 int ret;
 do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds)->__fds_bits)[0]) : "memory"); } while (0);
 ((void) (((&fds)->__fds_bits)[((ctx->fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((ctx->fd) % (8 * (int) sizeof (__fd_mask)))))));
 ret = select(ctx->fd + 1, &fds, ((void *)0), ((void *)0), ctx->p_timeout);
 if (ret < 0)
  return -(*__errno_location ());
 else if (ret == 0 && !((((&fds)->__fds_bits)[((ctx->fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((ctx->fd) % (8 * (int) sizeof (__fd_mask)))))) != 0))
  return -62;
 ret = read(ctx->fd, (uint8_t *)ctx->data + ctx->count, ctx->buf_size);
 if (ret < 0)
  return -(*__errno_location ());
 ctx->count += ret;
 ctx->buf_size = ctx->request_size - ctx->count;
 return 0;
}

canonical_original_code:
static int select_read(struct _context *ctx)
{
 struct fd_set fds;
 int ret;
 do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds)->__fds_bits)[0]) : "memory"); } while (0);
 ((void) (((&fds)->__fds_bits)[((ctx->fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((ctx->fd) % (8 * (int) sizeof (__fd_mask)))))));
 ret = select(ctx->fd + 1, &fds, ((void *)0), ((void *)0), ctx->p_timeout);
 if (ret < 0)
  return -(*__errno_location ());
 else if (ret == 0 && !((((&fds)->__fds_bits)[((ctx->fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((ctx->fd) % (8 * (int) sizeof (__fd_mask)))))) != 0))
  return -62;
 ret = read(ctx->fd, (unsigned char *)ctx->data + ctx->count, ctx->buf_size);
 if (ret < 0)
  return -(*__errno_location ());
 ctx->count += ret;
 ctx->buf_size = ctx->request_size - ctx->count;
 return 0;
}

variable_types: struct _context { int fd; void *data; unsigned long count; struct timeval { long tv_sec; long tv_usec; } *p_timeout; } *

variable_types:
fds
struct fd_set { long __fds_bits[-1]; }

variable_types: ret int

user_defined_types: struct _context { int fd; void *data; unsigned long count; struct timeval *p_timeout; }


decompiled_code:
__int64 __fastcall read_block(int a1, __int64 a2, __int64 *a3)
{
__int64 v4; // rdx
__int64 result; // rax
int v6; // [rsp+0h] [rbp-38h] BYREF
__int64 v7; // [rsp+8h] [rbp-30h]
unsigned __int64 v8; // [rsp+10h] [rbp-28h]
unsigned __int64 v9; // [rsp+18h] [rbp-20h]
__int64 v10; // [rsp+20h] [rbp-18h]
__int64 v11; // [rsp+28h] [rbp-10h]

v4 = *a3;
v6 = a1;
v7 = a2;
v8 = 0LL;
v9 = v4;
v10 = v4;
v11 = 0LL;
if ( v4 )
{
while ( 1 )
{
result = select_read((__int64)&v6);
if ( (int)result < 0 && (_DWORD)result != -4 && (_DWORD)result != -11 )
break;
v4 = v8;
if ( v8 >= v9 )
goto LABEL_8;
}
v4 = v8;
}
else
{
result = 0LL;
}
LABEL_8:
*a3 = v4;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(int a1, __int64 a2, __int64 *a3)
{
__int64 v4; // rdx
__int64 result; // rax
int v6; // [rsp+0h] [rbp-38h] BYREF
__int64 v7; // [rsp+8h] [rbp-30h]
unsigned __int64 v8; // [rsp+10h] [rbp-28h]
unsigned __int64 v9; // [rsp+18h] [rbp-20h]
__int64 v10; // [rsp+20h] [rbp-18h]
__int64 v11; // [rsp+28h] [rbp-10h]

v4 = *a3;
v6 = a1;
v7 = a2;
v8 = 0LL;
v9 = v4;
v10 = v4;
v11 = 0LL;
if ( v4 )
{
while ( 1 )
{
result = func8((__int64)&v6);
if ( (int)result < 0 && (_DWORD)result != -4 && (_DWORD)result != -11 )
break;
v4 = v8;
if ( v8 >= v9 )
goto LABEL_8;
}
v4 = v8;
}
else
{
result = 0LL;
}
LABEL_8:
*a3 = v4;
return result;
}

original_code:
static int read_block(int fd, void *data, size_t *p_nbytes)
{
 int retval = 0;
 context ctx = {
  .fd = fd,
  .data = data,
  .count = 0,
  .request_size = *p_nbytes,
  .buf_size = *p_nbytes,
  .p_timeout = ((void *)0),
 };
 while (ctx.count < ctx.request_size) {
  retval = select_read(&ctx);
  if (retval < 0) {
   if (retval != -4 && retval != -11)
    break;
  }
 }
 *p_nbytes = ctx.count;
 return retval;
}

canonical_original_code:
static int read_block(int fd, void *data, unsigned long *p_nbytes)
{
 int retval = 0;
 struct _context ctx = {
  .fd = fd,
  .data = data,
  .count = 0,
  .request_size = *p_nbytes,
  .buf_size = *p_nbytes,
  .p_timeout = ((void *)0),
 };
 while (ctx.count < ctx.request_size) {
  retval = select_read(&ctx);
  if (retval < 0) {
   if (retval != -4 && retval != -11)
    break;
  }
 }
 *p_nbytes = ctx.count;
 return retval;
}

variable_types: retval int

variable_types: struct _context { int fd; void *data; unsigned long count; struct timeval { long tv_sec; long tv_usec; } *p_timeout; }


decompiled_code:
__int64 __fastcall check_and_adjust_timeout(_DWORD *a1, int a2, _QWORD *a3)
{
int v4; // edx
__int64 result; // rax
struct timeval v6; // [rsp+0h] [rbp-28h] BYREF

gettimeofday(&v6, 0LL);
v4 = (LODWORD(v6.tv_usec) + 1000000 * (LODWORD(v6.tv_sec) - *a1) - a1[2]) / 1000;
result = 1LL;
if ( v4 <= a2 )
{
*a3 = (a2 - v4) / 0x3E8u;
result = 0LL;
a3[1] = (int)(1000 * ((a2 - v4) % 0x3E8u));
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(_DWORD *a1, int a2, _QWORD *a3)
{
int v4; // edx
__int64 result; // rax
struct timeval v6; // [rsp+0h] [rbp-28h] BYREF

gettimeofday(&v6, 0LL);
v4 = (LODWORD(v6.tv_usec) + 1000000 * (LODWORD(v6.tv_sec) - *a1) - a1[2]) / 1000;
result = 1LL;
if ( v4 <= a2 )
{
*a3 = (a2 - v4) / 0x3E8u;
result = 0LL;
a3[1] = (int)(1000 * ((a2 - v4) % 0x3E8u));
}
return result;
}

original_code:
static bool_t check_and_adjust_timeout(struct timeval *start, int timeout_ms,
  struct timeval *p_timeout)
{
 struct timeval now;
 int elapse_ms = 0;
 gettimeofday(&now, ((void *)0));
 elapse_ms = (now.tv_sec - start->tv_sec) * 1000000;
 elapse_ms += now.tv_usec - start->tv_usec;
 elapse_ms /= 1000;
 if (elapse_ms > timeout_ms)
  return TRUE;
 timeout_ms -= elapse_ms;
 p_timeout->tv_sec = timeout_ms / 1000;
 p_timeout->tv_usec = (timeout_ms % 1000) * 1000;
 return FALSE;
}

canonical_original_code:
static enum _bool_t check_and_adjust_timeout(struct timeval *start, int timeout_ms, struct timeval *p_timeout)
{
 struct timeval now;
 int elapse_ms = 0;
 gettimeofday(&now, ((void *)0));
 elapse_ms = (now.tv_sec - start->tv_sec) * 1000000;
 elapse_ms += now.tv_usec - start->tv_usec;
 elapse_ms /= 1000;
 if (elapse_ms > timeout_ms)
  return TRUE;
 timeout_ms -= elapse_ms;
 p_timeout->tv_sec = timeout_ms / 1000;
 p_timeout->tv_usec = (timeout_ms % 1000) * 1000;
 return FALSE;
}

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

return_type: enum _bool_t { FALSE=0, TRUE=1 }


decompiled_code:
__int64 __fastcall read_timeout(int a1, __int64 a2, __int64 *a3, int a4)
{
__int64 v6; // rax
unsigned __int64 v7; // rdx
__int64 result; // rax
__int64 v9; // [rsp+0h] [rbp-68h] BYREF
struct timeval tv; // [rsp+10h] [rbp-58h] BYREF
int v11; // [rsp+20h] [rbp-48h] BYREF
__int64 v12; // [rsp+28h] [rbp-40h]
unsigned __int64 v13; // [rsp+30h] [rbp-38h]
unsigned __int64 v14; // [rsp+38h] [rbp-30h]
__int64 v15; // [rsp+40h] [rbp-28h]
__int64 *v16; // [rsp+48h] [rbp-20h]

v6 = *a3;
v11 = a1;
v12 = a2;
v13 = 0LL;
v14 = v6;
v15 = v6;
v16 = &v9;
gettimeofday(&tv, 0LL);
v7 = v13;
if ( v13 < v14 )
{
while ( !(unsigned int)check_and_adjust_timeout(&tv, a4, v16) )
{
result = select_read((__int64)&v11);
if ( (int)result < 0 && (_DWORD)result != -4 && (_DWORD)result != -11 )
{
v7 = v13;
goto LABEL_9;
}
v7 = v13;
if ( v13 >= v14 )
goto LABEL_9;
}
puts("timeout");
result = 4294967234LL;
*a3 = v13;
}
else
{
result = 0LL;
LABEL_9:
*a3 = v7;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(int a1, __int64 a2, __int64 *a3, int a4)
{
__int64 v6; // rax
unsigned __int64 v7; // rdx
__int64 result; // rax
__int64 v9; // [rsp+0h] [rbp-68h] BYREF
struct timeval tv; // [rsp+10h] [rbp-58h] BYREF
int v11; // [rsp+20h] [rbp-48h] BYREF
__int64 v12; // [rsp+28h] [rbp-40h]
unsigned __int64 v13; // [rsp+30h] [rbp-38h]
unsigned __int64 v14; // [rsp+38h] [rbp-30h]
__int64 v15; // [rsp+40h] [rbp-28h]
__int64 *v16; // [rsp+48h] [rbp-20h]

v6 = *a3;
v11 = a1;
v12 = a2;
v13 = 0LL;
v14 = v6;
v15 = v6;
v16 = &v9;
gettimeofday(&tv, 0LL);
v7 = v13;
if ( v13 < v14 )
{
while ( !(unsigned int)func10(&tv, a4, v16) )
{
result = func8((__int64)&v11);
if ( (int)result < 0 && (_DWORD)result != -4 && (_DWORD)result != -11 )
{
v7 = v13;
goto LABEL_9;
}
v7 = v13;
if ( v13 >= v14 )
goto LABEL_9;
}
puts("timeout");
result = 4294967234LL;
*a3 = v13;
}
else
{
result = 0LL;
LABEL_9:
*a3 = v7;
}
return result;
}

original_code:
static int read_timeout(int fd, void *data, size_t *p_nbytes, int timeout_ms)
{
 int retval = 0;
 struct timeval timeout, start;
 context ctx = {
  .fd = fd,
  .data = data,
  .count = 0,
  .request_size = *p_nbytes,
  .buf_size = *p_nbytes,
  .p_timeout = &timeout,
 };
 gettimeofday(&start, ((void *)0));
 while (ctx.count < ctx.request_size) {
  if (check_and_adjust_timeout(&start, timeout_ms, ctx.p_timeout)) {
   retval = -62;
   printf("timeout\n");
   break;
  }
  retval = select_read(&ctx);
  if (retval < 0) {
   if (retval != -4 && retval != -11)
    break;
  }
 }
 *p_nbytes = ctx.count;
 return retval;
}

canonical_original_code:
static int read_timeout(int fd, void *data, unsigned long *p_nbytes, int timeout_ms)
{
 int retval = 0;
 struct timeval timeout; struct timeval start;
 struct _context ctx = {
  .fd = fd,
  .data = data,
  .count = 0,
  .request_size = *p_nbytes,
  .buf_size = *p_nbytes,
  .p_timeout = &timeout,
 };
 gettimeofday(&start, ((void *)0));
 while (ctx.count < ctx.request_size) {
  if (check_and_adjust_timeout(&start, timeout_ms, ctx.p_timeout)) {
   retval = -62;
   printf("timeout\n");
   break;
  }
  retval = select_read(&ctx);
  if (retval < 0) {
   if (retval != -4 && retval != -11)
    break;
  }
 }
 *p_nbytes = ctx.count;
 return retval;
}

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


decompiled_code:
__int64 __fastcall block_read(int a1, __int64 a2, __int64 *a3, int a4)
{
if ( !a4 )
return read_nonblock(a1, (void *)a2, (size_t *)a3);
if ( a4 < 0 )
return read_block(a1, a2, a3);
return read_timeout(a1, a2, a3, a4);
}

canonical_decompiled_code:
__int64 __fastcall func12(int a1, __int64 a2, __int64 *a3, int a4)
{
if ( !a4 )
return func7(a1, (void *)a2, (size_t *)a3);
if ( a4 < 0 )
return func9(a1, a2, a3);
return func11(a1, a2, a3, a4);
}

original_code:
int block_read(int fd, void *data, size_t *p_nbytes, int timeout_ms)
{
 if (timeout_ms == 0)
  return read_nonblock(fd, data, p_nbytes);
 else if (timeout_ms < 0)
  return read_block(fd, data, p_nbytes);
 else
  return read_timeout(fd, data, p_nbytes, timeout_ms);
}

canonical_original_code:
int block_read(int fd, void *data, unsigned long *p_nbytes, int timeout_ms)
{
 if (timeout_ms == 0)
  return read_nonblock(fd, data, p_nbytes);
 else if (timeout_ms < 0)
  return read_block(fd, data, p_nbytes);
 else
  return read_timeout(fd, data, p_nbytes, timeout_ms);
}


decompiled_code:
int init_term()
{
initscr();
noecho();
curs_set(0);
start_color();
init_pair(1, 7, 0);
return init_pair(2, 2, 0);
}

canonical_decompiled_code:
int func7()
{
initscr();
noecho();
curs_set(0);
start_color();
init_pair(1, 7, 0);
return init_pair(2, 2, 0);
}

original_code:
static void init_term()
{
 initscr();
 noecho();
 curs_set(0);
 start_color();
 init_pair(1, 7, 0);
 init_pair(2, 2, 0);
}

return_type: void


def

decompiled_code:
const char *__fastcall def(int a1)
{
const char *result; // rax

result = "enabled";
if ( !a1 )
result = "disabled";
return result;
}

canonical_decompiled_code:
const char *__fastcall func7(int a1)
{
const char *result; // rax

result = "enabled";
if ( !a1 )
result = "disabled";
return result;
}

original_code:
static const char *def(int val)
{
 return val ? "enabled": "disabled";
}

canonical_original_code:
static char *def(int val)
{
 return val ? "enabled": "disabled";
}

variable_types: val int

return_type: char *


decompiled_code:
__int64 __fastcall parse_args(int argc, char **argv)
{
int v2; // eax
__int64 v4; // [rsp+0h] [rbp-18h]

while ( 1 )
{
v2 = getopt(argc, argv, "d:l");
if ( v2 == -1 )
break;
if ( v2 == 100 )
{
device = (char *)optarg;
}
else
{
if ( v2 != 108 )
{
print_usage(*argv);
exit(0);
}
loop = 1;
}
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func10(int argc, char **argv)
{
int v2; // eax
__int64 v4; // [rsp+0h] [rbp-18h]

while ( 1 )
{
v2 = getopt(argc, argv, "d:l");
if ( v2 == -1 )
break;
if ( v2 == 100 )
{
device = (char *)optarg;
}
else
{
if ( v2 != 108 )
{
func9(*argv);
exit(0);
}
loop = 1;
}
}
return v4;
}

original_code:
static void parse_args(int argc, char **argv)
{
 int opt;
 while ( (opt = getopt(argc, argv, "d:l")) != -1) {
  switch (opt) {
   case 'd': device = optarg; break;
   case 'l': loop = 1; break;
   default:
    print_usage(argv[0]);
    exit(0);
  }
 }
}

variable_types: argc int

variable_types:
argv
char **

variable_types: opt int


decompiled_code:
void *__fastcall read_thread(void *a1)
{
int v1; // er12
void *v2; // rax
void *v3; // rbp
int v4; // eax
unsigned int v5; // ebx
int *v6; // rax
char *v7; // rax

v1 = *(_DWORD *)a1;
v2 = malloc(0x80uLL);
if ( v2 )
{
v3 = v2;
while ( 1 )
{
v4 = read(v1, v3, 0x80uLL);
v5 = v4;
if ( v4 < 0 )
break;
if ( v4 )
printf("--> recv: %s", (const char *)v3);
else
printf("no read %d\n", 0LL);
sleep(1u);
}
v6 = __errno_location();
v7 = strerror(*v6);
printf("read error: %d (%s)\n", v5, v7);
}
else
{
puts("mallc failed");
}
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func11(void *a1)
{
int v1; // er12
void *v2; // rax
void *v3; // rbp
int v4; // eax
unsigned int v5; // ebx
int *v6; // rax
char *v7; // rax

v1 = *(_DWORD *)a1;
v2 = malloc(0x80uLL);
if ( v2 )
{
v3 = v2;
while ( 1 )
{
v4 = read(v1, v3, 0x80uLL);
v5 = v4;
if ( v4 < 0 )
break;
if ( v4 )
printf("--> recv: %s", (const char *)v3);
else
printf("no read %d\n", 0LL);
sleep(1u);
}
v6 = __errno_location();
v7 = strerror(*v6);
printf("read error: %d (%s)\n", v5, v7);
}
else
{
puts("mallc failed");
}
return 0LL;
}

original_code:
static void *read_thread(void *data)
{
 int ret = 0;
 char *recv = ((void *)0);
 int size = 128;
 context *ctx = (context *)data;
 int fd = ctx->fd;
 recv = malloc(size * sizeof(char));
 if (recv == ((void *)0)) {
  printf("mallc failed\n");
  return ((void *)0);
 }
 while (1) {
  ret = read(fd, recv, size);
  if (ret < 0) {
   printf("read error: %d (%s)\n", ret, strerror((*__errno_location ())));
   break;
  }
  if (ret == 0) {
   printf("no read %d\n", ret);
  } else {
   printf("--> recv: %s", recv);
  }
  sleep(1);
 }
 return ((void *)0);
}

canonical_original_code:
static void *read_thread(void *data)
{
 int ret = 0;
 char *recv = ((void *)0);
 int size = 128;
 struct context *ctx = (context *)data;
 int fd = ctx->fd;
 recv = malloc(size * sizeof(char));
 if (recv == ((void *)0)) {
  printf("mallc failed\n");
  return ((void *)0);
 }
 while (1) {
  ret = read(fd, recv, size);
  if (ret < 0) {
   printf("read error: %d (%s)\n", ret, strerror((*__errno_location ())));
   break;
  }
  if (ret == 0) {
   printf("no read %d\n", ret);
  } else {
   printf("--> recv: %s", recv);
  }
  sleep(1);
 }
 return ((void *)0);
}

variable_types:
recv
char *

variable_types: size int

variable_types:
ctx
struct context { int fd; } *

return_type: void *


decompiled_code:
_BOOL8 __fastcall set_flag(int fd, int a2)
{
int v2; // eax

v2 = fcntl(fd, 3, 0LL);
if ( v2 >= 0 )
v2 = fcntl(fd, 4, a2 | (unsigned int)v2);
return v2 >= 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func15(int fd, int a2)
{
int v2; // eax

v2 = fcntl(fd, 3, 0LL);
if ( v2 >= 0 )
v2 = fcntl(fd, 4, a2 | (unsigned int)v2);
return v2 >= 0;
}

original_code:
int set_flag(int fd, int flags)
{
    int val=-1;
    if ((val=fcntl(fd, 3, 0))>=0) {
  val |= flags;
  val = fcntl(fd, 4, val);
 }
 return (val>=0)?1:0;
}

canonical_original_code:
int set_flag(int fd, int flags)
{
    int val = -1;
    if ((val=fcntl(fd, 3, 0))>=0) {
  val |= flags;
  val = fcntl(fd, 4, val);
 }
 return (val>=0)?1:0;
}

variable_types: fd int

variable_types: flags int


decompiled_code:
_BOOL8 __fastcall clr_flag(int fd, int a2)
{
int v2; // eax

v2 = fcntl(fd, 3, 0LL);
if ( v2 >= 0 )
v2 = fcntl(fd, 4, v2 & (unsigned int)~a2);
return v2 >= 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func16(int fd, int a2)
{
int v2; // eax

v2 = fcntl(fd, 3, 0LL);
if ( v2 >= 0 )
v2 = fcntl(fd, 4, v2 & (unsigned int)~a2);
return v2 >= 0;
}

original_code:
int clr_flag(int fd, int flags)
{
    int val=-1;
    if ((val=fcntl(fd, 3, 0))>=0) {
     val &= ~flags;
     val =fcntl(fd, 4, val);
    }
    return (val>=0)?1:0;
}

canonical_original_code:
int clr_flag(int fd, int flags)
{
    int val = -1;
    if ((val=fcntl(fd, 3, 0))>=0) {
     val &= ~flags;
     val =fcntl(fd, 4, val);
    }
    return (val>=0)?1:0;
}


decompiled_code:
__int64 __fastcall open_uart(const char *a1)
{
unsigned int v1; // er12

v1 = open(a1, 258);
printf("%s open Success!\n", a1);
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func17(const char *a1)
{
unsigned int v1; // er12

v1 = open(a1, 258);
printf("%s open Success!\n", a1);
return v1;
}

original_code:
unsigned int open_uart(const char *dev)
{
 unsigned int fd;
 fd = open(dev, 02 | 0400);
 if (fd < 0)
  printf("%s  open Failed!\n", dev);
 else
  printf("%s open Success!\n", dev);
 return fd;
}

canonical_original_code:
unsigned int open_uart(char *dev)
{
 unsigned int fd;
 fd = open(dev, 02 | 0400);
 if (fd < 0)
  printf("%s  open Failed!\n", dev);
 else
  printf("%s open Success!\n", dev);
 return fd;
}

variable_types:
dev
char *

variable_types:
fd
unsigned int

return_type: unsigned int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebp
int v4; // er12
int buf[7]; // [rsp+Ah] [rbp-1Eh] BYREF

strcpy((char *)buf, "test\n");
parse_args(argc, (char **)argv);
print_options();
printf("bdrate: %d\n", 115200LL);
g_ctx = open_uart(device);
v3 = g_ctx;
if ( (int)config_uart_constprop_0(g_ctx) < 0 )
{
v4 = -1;
puts("config uart failed!");
}
else
{
if ( loop )
v4 = test_loop_constprop_0(buf);
else
v4 = test_write_constprop_0(v3, buf);
close(v3);
puts("\n test success");
}
return v4;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebp
int v4; // er12
int buf[7]; // [rsp+Ah] [rbp-1Eh] BYREF

strcpy((char *)buf, "test\n");
func10(argc, (char **)argv);
func8();
printf("bdrate: %d\n", 115200LL);
g_ctx = func17(device);
v3 = g_ctx;
if ( (int)func14(g_ctx) < 0 )
{
v4 = -1;
puts("config uart failed!");
}
else
{
if ( loop )
v4 = func12(buf);
else
v4 = func13(v3, buf);
close(v3);
puts("\n test success");
}
return v4;
}

original_code:
int main(int argc, char **argv)
{
 unsigned int fd;
 int bdrate = 115200;
 char send_buf[] = "test\n";
 int ret;
 parse_args(argc, argv);
 print_options();
 printf("bdrate: %d\n", bdrate);
 fd = open_uart(device);
 if (fd < 0) {
  printf("open uart error: %d\n", fd);
  return -1;
 }
 g_ctx.fd = fd;
 if (config_uart(fd, bdrate, 8, 'N', 1) < 0) {
  printf("config uart failed!\n");
  return -1;
 }
 if (loop) {
  ret = test_loop(&g_ctx, send_buf, sizeof(send_buf));
 } else {
  ret = test_write(fd, send_buf, sizeof(send_buf));
 }
 close(fd);
 printf("\n test success\n");
 return ret;
}

variable_types: bdrate int


decompiled_code:
int __fastcall print_usage(const char *a1)
{
const char *v1; // rax

fprintf(stderr, "Usage: %s [-d device] [-l]\n", a1);
fprintf(stderr, " -d: tty device. Default is [%s]\n", device);
v1 = def(loop);
return fprintf(stderr, " -l: loop test. Default is [%s]\n", v1);
}

canonical_decompiled_code:
int __fastcall func9(const char *a1)
{
const char *v1; // rax

fprintf(stderr, "Usage: %s [-d device] [-l]\n", a1);
fprintf(stderr, " -d: tty device. Default is [%s]\n", device);
v1 = func7(loop);
return fprintf(stderr, " -l: loop test. Default is [%s]\n", v1);
}

original_code:
static void print_usage(char *p)
{
 fprintf(stderr, "Usage: %s [-d device] [-l]\n", p);
 fprintf(stderr, "    -d: tty device. Default is [%s]\n", device);
 fprintf(stderr, "    -l: loop test. Default is [%s]\n", def(loop));
}

variable_types:
p
char *


decompiled_code:
void hpet_sigio()
{
fwrite("hpet_sigio: called\n", 1uLL, 0x13uLL, _bss_start);
++hpet_sigio_count;
}

canonical_decompiled_code:
void func7()
{
fwrite("hpet_sigio: called\n", 1uLL, 0x13uLL, _bss_start);
++hpet_sigio_count;
}

original_code:
static void
hpet_sigio(int val)
{
 fprintf(stderr, "hpet_sigio: called\n");
 hpet_sigio_count++;
}


decompiled_code:
void __fastcall hpet_open_close(int a1, const char **a2)
{
int v2; // edi

if ( a1 == 1 )
{
v2 = open(*a2, 0);
if ( v2 < 0 )
{
fwrite("hpet_open_close: open failed\n", 1uLL, 0x1DuLL, _bss_start);
perror(" ");
}
else
{
close(v2);
}
}
else
{
fwrite("hpet_open_close: device-name\n", 1uLL, 0x1DuLL, _bss_start);
}
}

canonical_decompiled_code:
void __fastcall func8(int a1, const char **a2)
{
int v2; // edi

if ( a1 == 1 )
{
v2 = open(*a2, 0);
if ( v2 < 0 )
{
fwrite("hpet_open_close: open failed\n", 1uLL, 0x1DuLL, _bss_start);
perror(" ");
}
else
{
close(v2);
}
}
else
{
fwrite("hpet_open_close: device-name\n", 1uLL, 0x1DuLL, _bss_start);
}
}

original_code:
void
hpet_open_close(int argc, const char **argv)
{
 int fd;
 if (argc != 1) {
  fprintf(stderr, "hpet_open_close: device-name\n");
  return;
 }
 fd = open(argv[0], 00);
 if (fd < 0) {
  fprintf(stderr, "hpet_open_close: open failed\n");
        perror("    ");
    } else
  close(fd);
 return;
}

canonical_original_code:
void hpet_open_close(int argc, char **argv)
{
 int fd;
 if (argc != 1) {
  fprintf(stderr, "hpet_open_close: device-name\n");
  return;
 }
 fd = open(argv[0], 00);
 if (fd < 0) {
  fprintf(stderr, "hpet_open_close: open failed\n");
        perror("    ");
    } else
  close(fd);
 return;
}


decompiled_code:
int __fastcall hpet_info(int a1, const char **a2)
{
int v3; // eax
int v4; // ebp
__int64 v5[2]; // [rsp+0h] [rbp-38h] BYREF
unsigned __int16 v6; // [rsp+10h] [rbp-28h]
unsigned __int16 v7; // [rsp+12h] [rbp-26h]

if ( a1 != 1 )
return fwrite("hpet_info: device-name\n", 1uLL, 0x17uLL, _bss_start);
v3 = open(*a2, 0);
v4 = v3;
if ( v3 < 0 )
return fprintf(_bss_start, "hpet_info: open of %s failed\n", *a2);
if ( ioctl(v3, 0x80186803uLL, v5) < 0 )
{
fwrite("hpet_info: failed to get info\n", 1uLL, 0x1EuLL, _bss_start);
}
else
{
fprintf(_bss_start, "hpet_info: hi_irqfreq 0x%lx hi_flags 0x%lx ", v5[0], v5[1]);
fprintf(_bss_start, "hi_hpet %d hi_timer %d\n", v6, v7);
}
return close(v4);
}

canonical_decompiled_code:
int __fastcall func9(int a1, const char **a2)
{
int v3; // eax
int v4; // ebp
__int64 v5[2]; // [rsp+0h] [rbp-38h] BYREF
unsigned __int16 v6; // [rsp+10h] [rbp-28h]
unsigned __int16 v7; // [rsp+12h] [rbp-26h]

if ( a1 != 1 )
return fwrite("hpet_info: device-name\n", 1uLL, 0x17uLL, _bss_start);
v3 = open(*a2, 0);
v4 = v3;
if ( v3 < 0 )
return fprintf(_bss_start, "hpet_info: open of %s failed\n", *a2);
if ( ioctl(v3, 0x80186803uLL, v5) < 0 )
{
fwrite("hpet_info: failed to get info\n", 1uLL, 0x1EuLL, _bss_start);
}
else
{
fprintf(_bss_start, "hpet_info: hi_irqfreq 0x%lx hi_flags 0x%lx ", v5[0], v5[1]);
fprintf(_bss_start, "hi_hpet %d hi_timer %d\n", v6, v7);
}
return close(v4);
}

original_code:
void
hpet_info(int argc, const char **argv)
{
 struct hpet_info info;
 int fd;
 if (argc != 1) {
  fprintf(stderr, "hpet_info: device-name\n");
  return;
 }
 fd = open(argv[0], 00);
 if (fd < 0) {
  fprintf(stderr, "hpet_info: open of %s failed\n", argv[0]);
  return;
 }
 if (ioctl(fd, (((2U) << (((0 +8)+8)+14)) | ((('h')) << (0 +8)) | (((0x03)) << 0) | ((((sizeof(struct hpet_info)))) << ((0 +8)+8))), &info) < 0) {
  fprintf(stderr, "hpet_info: failed to get info\n");
  goto out;
 }
 fprintf(stderr, "hpet_info: hi_irqfreq 0x%lx hi_flags 0x%lx ",
  info.hi_ireqfreq, info.hi_flags);
 fprintf(stderr, "hi_hpet %d hi_timer %d\n",
  info.hi_hpet, info.hi_timer);
out:
 close(fd);
 return;
}

canonical_original_code:
void hpet_info(int argc, char **argv)
{
 struct hpet_info info;
 int fd;
 if (argc != 1) {
  fprintf(stderr, "hpet_info: device-name\n");
  return;
 }
 fd = open(argv[0], 00);
 if (fd < 0) {
  fprintf(stderr, "hpet_info: open of %s failed\n", argv[0]);
  return;
 }
 if (ioctl(fd, (((2U) << (((0 +8)+8)+14)) | ((('h')) << (0 +8)) | (((0x03)) << 0) | ((((sizeof(struct hpet_info)))) << ((0 +8)+8))), &info) < 0) {
  fprintf(stderr, "hpet_info: failed to get info\n");
  goto out;
 }
 fprintf(stderr, "hpet_info: hi_irqfreq 0x%lx hi_flags 0x%lx ",
  info.hi_ireqfreq, info.hi_flags);
 fprintf(stderr, "hi_hpet %d hi_timer %d\n",
  info.hi_hpet, info.hi_timer);
out:
 close(fd);
 return;
}

variable_types: struct hpet_info { unsigned long hi_ireqfreq; unsigned long hi_flags; unsigned short hi_hpet; unsigned short hi_timer; }


decompiled_code:
int __fastcall hpet_poll(int a1, const char **a2)
{
int v3; // er13
int v4; // er12
int v5; // eax
int v6; // ebp
int i; // ebx
__int64 buf; // [rsp+0h] [rbp-80h] BYREF
struct pollfd fds; // [rsp+8h] [rbp-78h] BYREF
struct timezone tz; // [rsp+10h] [rbp-70h] BYREF
struct timeval v11; // [rsp+18h] [rbp-68h] BYREF
struct timeval tv; // [rsp+28h] [rbp-58h] BYREF
char v13[8]; // [rsp+38h] [rbp-48h] BYREF
__int64 v14; // [rsp+40h] [rbp-40h]

if ( a1 != 3 )
return fwrite("hpet_poll: device-name freq iterations\n", 1uLL, 0x27uLL, _bss_start);
v3 = atoi(a2[1]);
v4 = atoi(a2[2]);
v5 = open(*a2, 0);
v6 = v5;
if ( v5 < 0 )
return fprintf(_bss_start, "hpet_poll: open of %s failed\n", *a2);
if ( ioctl(v5, 0x40086806uLL, v3) < 0 )
{
fwrite("hpet_poll: HPET_IRQFREQ failed\n", 1uLL, 0x1FuLL, _bss_start);
}
else if ( ioctl(v6, 0x80186803uLL, v13) < 0 )
{
fwrite("hpet_poll: failed to get info\n", 1uLL, 0x1EuLL, _bss_start);
}
else
{
fprintf(_bss_start, "hpet_poll: info.hi_flags 0x%lx\n", v14);
if ( v14 && ioctl(v6, 0x6804uLL, 0LL) < 0 )
{
fwrite("hpet_poll: HPET_EPI failed\n", 1uLL, 0x1BuLL, _bss_start);
}
else if ( ioctl(v6, 0x6801uLL, 0LL) < 0 )
{
fwrite("hpet_poll, HPET_IE_ON failed\n", 1uLL, 0x1DuLL, _bss_start);
}
else
{
fds.fd = v6;
fds.events = 1;
if ( v4 > 0 )
{
for ( i = 0; i != v4; ++i )
{
fds.revents = 0;
gettimeofday(&v11, &tz);
if ( poll(&fds, 1uLL, -1) >= 0 )
{
gettimeofday(&tv, &tz);
fprintf(
_bss_start,
"hpet_poll: expired time = 0x%lx\n",
tv.tv_usec + 1000000 * tv.tv_sec - (v11.tv_usec + 1000000 * v11.tv_sec));
fprintf(_bss_start, "hpet_poll: revents = 0x%x\n", (unsigned int)fds.revents);
if ( read(v6, &buf, 8uLL) == 8 )
fprintf(_bss_start, "hpet_poll: data 0x%lx\n", buf);
else
fwrite("hpet_poll: read failed\n", 1uLL, 0x17uLL, _bss_start);
}
else
{
fwrite("hpet_poll: poll failed\n", 1uLL, 0x17uLL, _bss_start);
}
}
}
}
}
return close(v6);
}

canonical_decompiled_code:
int __fastcall func10(int a1, const char **a2)
{
int v3; // er13
int v4; // er12
int v5; // eax
int v6; // ebp
int i; // ebx
__int64 buf; // [rsp+0h] [rbp-80h] BYREF
struct pollfd fds; // [rsp+8h] [rbp-78h] BYREF
struct timezone tz; // [rsp+10h] [rbp-70h] BYREF
struct timeval v11; // [rsp+18h] [rbp-68h] BYREF
struct timeval tv; // [rsp+28h] [rbp-58h] BYREF
char v13[8]; // [rsp+38h] [rbp-48h] BYREF
__int64 v14; // [rsp+40h] [rbp-40h]

if ( a1 != 3 )
return fwrite("hpet_poll: device-name freq iterations\n", 1uLL, 0x27uLL, _bss_start);
v3 = atoi(a2[1]);
v4 = atoi(a2[2]);
v5 = open(*a2, 0);
v6 = v5;
if ( v5 < 0 )
return fprintf(_bss_start, "hpet_poll: open of %s failed\n", *a2);
if ( ioctl(v5, 0x40086806uLL, v3) < 0 )
{
fwrite("hpet_poll: HPET_IRQFREQ failed\n", 1uLL, 0x1FuLL, _bss_start);
}
else if ( ioctl(v6, 0x80186803uLL, v13) < 0 )
{
fwrite("hpet_poll: failed to get info\n", 1uLL, 0x1EuLL, _bss_start);
}
else
{
fprintf(_bss_start, "hpet_poll: info.hi_flags 0x%lx\n", v14);
if ( v14 && ioctl(v6, 0x6804uLL, 0LL) < 0 )
{
fwrite("hpet_poll: HPET_EPI failed\n", 1uLL, 0x1BuLL, _bss_start);
}
else if ( ioctl(v6, 0x6801uLL, 0LL) < 0 )
{
fwrite("hpet_poll, HPET_IE_ON failed\n", 1uLL, 0x1DuLL, _bss_start);
}
else
{
fds.fd = v6;
fds.events = 1;
if ( v4 > 0 )
{
for ( i = 0; i != v4; ++i )
{
fds.revents = 0;
gettimeofday(&v11, &tz);
if ( poll(&fds, 1uLL, -1) >= 0 )
{
gettimeofday(&tv, &tz);
fprintf(
_bss_start,
"hpet_poll: expired time = 0x%lx\n",
tv.tv_usec + 1000000 * tv.tv_sec - (v11.tv_usec + 1000000 * v11.tv_sec));
fprintf(_bss_start, "hpet_poll: revents = 0x%x\n", (unsigned int)fds.revents);
if ( read(v6, &buf, 8uLL) == 8 )
fprintf(_bss_start, "hpet_poll: data 0x%lx\n", buf);
else
fwrite("hpet_poll: read failed\n", 1uLL, 0x17uLL, _bss_start);
}
else
{
fwrite("hpet_poll: poll failed\n", 1uLL, 0x17uLL, _bss_start);
}
}
}
}
}
return close(v6);
}

original_code:
void
hpet_poll(int argc, const char **argv)
{
 unsigned long freq;
 int iterations, i, fd;
 struct pollfd pfd;
 struct hpet_info info;
 struct timeval stv, etv;
 struct timezone tz;
 long usec;
 if (argc != 3) {
  fprintf(stderr, "hpet_poll: device-name freq iterations\n");
  return;
 }
 freq = atoi(argv[1]);
 iterations = atoi(argv[2]);
 fd = open(argv[0], 00);
 if (fd < 0) {
  fprintf(stderr, "hpet_poll: open of %s failed\n", argv[0]);
  return;
 }
 if (ioctl(fd, (((1U) << (((0 +8)+8)+14)) | ((('h')) << (0 +8)) | (((0x6)) << 0) | ((((sizeof(unsigned long)))) << ((0 +8)+8))), freq) < 0) {
  fprintf(stderr, "hpet_poll: HPET_IRQFREQ failed\n");
  goto out;
 }
 if (ioctl(fd, (((2U) << (((0 +8)+8)+14)) | ((('h')) << (0 +8)) | (((0x03)) << 0) | ((((sizeof(struct hpet_info)))) << ((0 +8)+8))), &info) < 0) {
  fprintf(stderr, "hpet_poll: failed to get info\n");
  goto out;
 }
 fprintf(stderr, "hpet_poll: info.hi_flags 0x%lx\n", info.hi_flags);
 if (info.hi_flags && (ioctl(fd, (((0U) << (((0 +8)+8)+14)) | ((('h')) << (0 +8)) | (((0x04)) << 0) | ((0) << ((0 +8)+8))), 0) < 0)) {
  fprintf(stderr, "hpet_poll: HPET_EPI failed\n");
  goto out;
 }
 if (ioctl(fd, (((0U) << (((0 +8)+8)+14)) | ((('h')) << (0 +8)) | (((0x01)) << 0) | ((0) << ((0 +8)+8))), 0) < 0) {
  fprintf(stderr, "hpet_poll, HPET_IE_ON failed\n");
  goto out;
 }
 pfd.fd = fd;
 pfd.events = 0x001;
 for (i = 0; i < iterations; i++) {
  pfd.revents = 0;
  gettimeofday(&stv, &tz);
  if (poll(&pfd, 1, -1) < 0)
   fprintf(stderr, "hpet_poll: poll failed\n");
  else {
   long data;
   gettimeofday(&etv, &tz);
   usec = stv.tv_sec * 1000000 + stv.tv_usec;
   usec = (etv.tv_sec * 1000000 + etv.tv_usec) - usec;
   fprintf(stderr,
    "hpet_poll: expired time = 0x%lx\n", usec);
   fprintf(stderr, "hpet_poll: revents = 0x%x\n",
    pfd.revents);
   if (read(fd, &data, sizeof(data)) != sizeof(data)) {
    fprintf(stderr, "hpet_poll: read failed\n");
   }
   else
    fprintf(stderr, "hpet_poll: data 0x%lx\n",
     data);
  }
 }
out:
 close(fd);
 return;
}

canonical_original_code:
void hpet_poll(int argc, char **argv)
{
 unsigned long freq;
 int iterations; int i; int fd;
 struct pollfd pfd;
 struct hpet_info info;
 struct timeval stv; struct timeval etv;
 struct timezone tz;
 long usec;
 if (argc != 3) {
  fprintf(stderr, "hpet_poll: device-name freq iterations\n");
  return;
 }
 freq = atoi(argv[1]);
 iterations = atoi(argv[2]);
 fd = open(argv[0], 00);
 if (fd < 0) {
  fprintf(stderr, "hpet_poll: open of %s failed\n", argv[0]);
  return;
 }
 if (ioctl(fd, (((1U) << (((0 +8)+8)+14)) | ((('h')) << (0 +8)) | (((0x6)) << 0) | ((((sizeof(unsigned long)))) << ((0 +8)+8))), freq) < 0) {
  fprintf(stderr, "hpet_poll: HPET_IRQFREQ failed\n");
  goto out;
 }
 if (ioctl(fd, (((2U) << (((0 +8)+8)+14)) | ((('h')) << (0 +8)) | (((0x03)) << 0) | ((((sizeof(struct hpet_info)))) << ((0 +8)+8))), &info) < 0) {
  fprintf(stderr, "hpet_poll: failed to get info\n");
  goto out;
 }
 fprintf(stderr, "hpet_poll: info.hi_flags 0x%lx\n", info.hi_flags);
 if (info.hi_flags && (ioctl(fd, (((0U) << (((0 +8)+8)+14)) | ((('h')) << (0 +8)) | (((0x04)) << 0) | ((0) << ((0 +8)+8))), 0) < 0)) {
  fprintf(stderr, "hpet_poll: HPET_EPI failed\n");
  goto out;
 }
 if (ioctl(fd, (((0U) << (((0 +8)+8)+14)) | ((('h')) << (0 +8)) | (((0x01)) << 0) | ((0) << ((0 +8)+8))), 0) < 0) {
  fprintf(stderr, "hpet_poll, HPET_IE_ON failed\n");
  goto out;
 }
 pfd.fd = fd;
 pfd.events = 0x001;
 for (i = 0; i < iterations; i++) {
  pfd.revents = 0;
  gettimeofday(&stv, &tz);
  if (poll(&pfd, 1, -1) < 0)
   fprintf(stderr, "hpet_poll: poll failed\n");
  else {
   long data;
   gettimeofday(&etv, &tz);
   usec = stv.tv_sec * 1000000 + stv.tv_usec;
   usec = (etv.tv_sec * 1000000 + etv.tv_usec) - usec;
   fprintf(stderr,
    "hpet_poll: expired time = 0x%lx\n", usec);
   fprintf(stderr, "hpet_poll: revents = 0x%x\n",
    pfd.revents);
   if (read(fd, &data, sizeof(data)) != sizeof(data)) {
    fprintf(stderr, "hpet_poll: read failed\n");
   }
   else
    fprintf(stderr, "hpet_poll: data 0x%lx\n",
     data);
  }
 }
out:
 close(fd);
 return;
}

variable_types:
freq
unsigned long

variable_types: iterations int

variable_types: i int

variable_types: struct pollfd { int fd; short events; short revents; }

variable_types: struct timezone { int tz_minuteswest; int tz_dsttime; }

variable_types: usec long

variable_types: data long

user_defined_types: struct hpet_info { unsigned long hi_ireqfreq; unsigned long hi_flags; unsigned short hi_hpet; unsigned short hi_timer; }

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


decompiled_code:
int __fastcall hpet_fasync(int a1, const char **a2)
{
__sighandler_t v2; // rax
void (*v3)(int); // rbp
int v4; // er12
unsigned int v5; // eax
unsigned int v6; // eax
int v7; // ebx
int v8; // er13
int result; // eax
int v10; // ebx
__int64 v11; // [rsp+0h] [rbp-48h] BYREF
__int64 v12; // [rsp+8h] [rbp-40h]

hpet_sigio_count = 0;
v2 = signal(29, (__sighandler_t)hpet_sigio);
if ( v2 == (__sighandler_t)-1LL )
return fwrite("hpet_fasync: failed to set signal handler\n", 1uLL, 0x2AuLL, _bss_start);
v3 = v2;
if ( a1 == 3 )
{
v4 = open(*a2, 0);
if ( v4 < 0 )
{
result = fprintf(_bss_start, "hpet_fasync: failed to open %s\n", *a2);
}
else
{
v5 = getpid();
if ( fcntl(v4, 8, v5) == 1 || (v6 = fcntl(v4, 3), v6 == 1) || (BYTE1(v6) |= 0x20u, fcntl(v4, 4, v6) == 1) )
{
fwrite("hpet_fasync: fcntl failed\n", 1uLL, 0x1AuLL, _bss_start);
}
else
{
v7 = atoi(a2[1]);
v8 = atoi(a2[2]);
if ( ioctl(v4, 0x40086806uLL, v7) < 0 )
{
fwrite("hpet_fasync: HPET_IRQFREQ failed\n", 1uLL, 0x21uLL, _bss_start);
}
else if ( ioctl(v4, 0x80186803uLL, &v11) < 0 )
{
fwrite("hpet_fasync: failed to get info\n", 1uLL, 0x20uLL, _bss_start);
}
else
{
fprintf(_bss_start, "hpet_fasync: info.hi_flags 0x%lx\n", v12);
if ( v12 && ioctl(v4, 0x6804uLL, 0LL) < 0 )
{
fwrite("hpet_fasync: HPET_EPI failed\n", 1uLL, 0x1DuLL, _bss_start);
}
else if ( ioctl(v4, 0x6801uLL, 0LL) < 0 )
{
fwrite("hpet_fasync, HPET_IE_ON failed\n", 1uLL, 0x1FuLL, _bss_start);
}
else
{
v10 = 0;
if ( v8 > 0 )
{
do
{
pause();
++v10;
fprintf(_bss_start, "hpet_fasync: count = %d\n", (unsigned int)hpet_sigio_count);
}
while ( v8 != v10 );
}
}
}
}
signal(29, v3);
result = close(v4);
}
}
else
{
fwrite("hpet_fasync: device-name freq iterations\n", 1uLL, 0x29uLL, _bss_start);
result = (unsigned int)signal(29, v3);
}
return result;
}

canonical_decompiled_code:
int __fastcall func11(int a1, const char **a2)
{
__sighandler_t v2; // rax
void (*v3)(int); // rbp
int v4; // er12
unsigned int v5; // eax
unsigned int v6; // eax
int v7; // ebx
int v8; // er13
int result; // eax
int v10; // ebx
__int64 v11; // [rsp+0h] [rbp-48h] BYREF
__int64 v12; // [rsp+8h] [rbp-40h]

hpet_sigio_count = 0;
v2 = signal(29, (__sighandler_t)hpet_sigio);
if ( v2 == (__sighandler_t)-1LL )
return fwrite("hpet_fasync: failed to set signal handler\n", 1uLL, 0x2AuLL, _bss_start);
v3 = v2;
if ( a1 == 3 )
{
v4 = open(*a2, 0);
if ( v4 < 0 )
{
result = fprintf(_bss_start, "hpet_fasync: failed to open %s\n", *a2);
}
else
{
v5 = getpid();
if ( fcntl(v4, 8, v5) == 1 || (v6 = fcntl(v4, 3), v6 == 1) || (BYTE1(v6) |= 0x20u, fcntl(v4, 4, v6) == 1) )
{
fwrite("hpet_fasync: fcntl failed\n", 1uLL, 0x1AuLL, _bss_start);
}
else
{
v7 = atoi(a2[1]);
v8 = atoi(a2[2]);
if ( ioctl(v4, 0x40086806uLL, v7) < 0 )
{
fwrite("hpet_fasync: HPET_IRQFREQ failed\n", 1uLL, 0x21uLL, _bss_start);
}
else if ( ioctl(v4, 0x80186803uLL, &v11) < 0 )
{
fwrite("hpet_fasync: failed to get info\n", 1uLL, 0x20uLL, _bss_start);
}
else
{
fprintf(_bss_start, "hpet_fasync: info.hi_flags 0x%lx\n", v12);
if ( v12 && ioctl(v4, 0x6804uLL, 0LL) < 0 )
{
fwrite("hpet_fasync: HPET_EPI failed\n", 1uLL, 0x1DuLL, _bss_start);
}
else if ( ioctl(v4, 0x6801uLL, 0LL) < 0 )
{
fwrite("hpet_fasync, HPET_IE_ON failed\n", 1uLL, 0x1FuLL, _bss_start);
}
else
{
v10 = 0;
if ( v8 > 0 )
{
do
{
pause();
++v10;
fprintf(_bss_start, "hpet_fasync: count = %d\n", (unsigned int)hpet_sigio_count);
}
while ( v8 != v10 );
}
}
}
}
signal(29, v3);
result = close(v4);
}
}
else
{
fwrite("hpet_fasync: device-name freq iterations\n", 1uLL, 0x29uLL, _bss_start);
result = (unsigned int)signal(29, v3);
}
return result;
}

original_code:
void
hpet_fasync(int argc, const char **argv)
{
 unsigned long freq;
 int iterations, i, fd, value;
 sig_t oldsig;
 struct hpet_info info;
 hpet_sigio_count = 0;
 fd = -1;
 if ((oldsig = signal(29, hpet_sigio)) == ((__sighandler_t) -1)) {
  fprintf(stderr, "hpet_fasync: failed to set signal handler\n");
  return;
 }
 if (argc != 3) {
  fprintf(stderr, "hpet_fasync: device-name freq iterations\n");
  goto out;
 }
 fd = open(argv[0], 00);
 if (fd < 0) {
  fprintf(stderr, "hpet_fasync: failed to open %s\n", argv[0]);
  return;
 }
 if ((fcntl(fd, 8, getpid()) == 1) ||
  ((value = fcntl(fd, 3)) == 1) ||
  (fcntl(fd, 4, value | 020000) == 1)) {
  fprintf(stderr, "hpet_fasync: fcntl failed\n");
  goto out;
 }
 freq = atoi(argv[1]);
 iterations = atoi(argv[2]);
 if (ioctl(fd, (((1U) << (((0 +8)+8)+14)) | ((('h')) << (0 +8)) | (((0x6)) << 0) | ((((sizeof(unsigned long)))) << ((0 +8)+8))), freq) < 0) {
  fprintf(stderr, "hpet_fasync: HPET_IRQFREQ failed\n");
  goto out;
 }
 if (ioctl(fd, (((2U) << (((0 +8)+8)+14)) | ((('h')) << (0 +8)) | (((0x03)) << 0) | ((((sizeof(struct hpet_info)))) << ((0 +8)+8))), &info) < 0) {
  fprintf(stderr, "hpet_fasync: failed to get info\n");
  goto out;
 }
 fprintf(stderr, "hpet_fasync: info.hi_flags 0x%lx\n", info.hi_flags);
 if (info.hi_flags && (ioctl(fd, (((0U) << (((0 +8)+8)+14)) | ((('h')) << (0 +8)) | (((0x04)) << 0) | ((0) << ((0 +8)+8))), 0) < 0)) {
  fprintf(stderr, "hpet_fasync: HPET_EPI failed\n");
  goto out;
 }
 if (ioctl(fd, (((0U) << (((0 +8)+8)+14)) | ((('h')) << (0 +8)) | (((0x01)) << 0) | ((0) << ((0 +8)+8))), 0) < 0) {
  fprintf(stderr, "hpet_fasync, HPET_IE_ON failed\n");
  goto out;
 }
 for (i = 0; i < iterations; i++) {
  (void) pause();
  fprintf(stderr, "hpet_fasync: count = %d\n", hpet_sigio_count);
 }
out:
 signal(29, oldsig);
 if (fd >= 0)
  close(fd);
 return;
}

canonical_original_code:
void hpet_fasync(int argc, char **argv)
{
 unsigned long freq;
 int iterations; int i; int fd; int value;
 void (*oldsig)(int);
 struct hpet_info info;
 hpet_sigio_count = 0;
 fd = -1;
 if ((oldsig = signal(29, hpet_sigio)) == ((__sighandler_t) -1)) {
  fprintf(stderr, "hpet_fasync: failed to set signal handler\n");
  return;
 }
 if (argc != 3) {
  fprintf(stderr, "hpet_fasync: device-name freq iterations\n");
  goto out;
 }
 fd = open(argv[0], 00);
 if (fd < 0) {
  fprintf(stderr, "hpet_fasync: failed to open %s\n", argv[0]);
  return;
 }
 if ((fcntl(fd, 8, getpid()) == 1) ||
  ((value = fcntl(fd, 3)) == 1) ||
  (fcntl(fd, 4, value | 020000) == 1)) {
  fprintf(stderr, "hpet_fasync: fcntl failed\n");
  goto out;
 }
 freq = atoi(argv[1]);
 iterations = atoi(argv[2]);
 if (ioctl(fd, (((1U) << (((0 +8)+8)+14)) | ((('h')) << (0 +8)) | (((0x6)) << 0) | ((((sizeof(unsigned long)))) << ((0 +8)+8))), freq) < 0) {
  fprintf(stderr, "hpet_fasync: HPET_IRQFREQ failed\n");
  goto out;
 }
 if (ioctl(fd, (((2U) << (((0 +8)+8)+14)) | ((('h')) << (0 +8)) | (((0x03)) << 0) | ((((sizeof(struct hpet_info)))) << ((0 +8)+8))), &info) < 0) {
  fprintf(stderr, "hpet_fasync: failed to get info\n");
  goto out;
 }
 fprintf(stderr, "hpet_fasync: info.hi_flags 0x%lx\n", info.hi_flags);
 if (info.hi_flags && (ioctl(fd, (((0U) << (((0 +8)+8)+14)) | ((('h')) << (0 +8)) | (((0x04)) << 0) | ((0) << ((0 +8)+8))), 0) < 0)) {
  fprintf(stderr, "hpet_fasync: HPET_EPI failed\n");
  goto out;
 }
 if (ioctl(fd, (((0U) << (((0 +8)+8)+14)) | ((('h')) << (0 +8)) | (((0x01)) << 0) | ((0) << ((0 +8)+8))), 0) < 0) {
  fprintf(stderr, "hpet_fasync, HPET_IE_ON failed\n");
  goto out;
 }
 for (i = 0; i < iterations; i++) {
  (void) pause();
  fprintf(stderr, "hpet_fasync: count = %d\n", hpet_sigio_count);
 }
out:
 signal(29, oldsig);
 if (fd >= 0)
  close(fd);
 return;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: value int

variable_types:
oldsig
void (*)(int)


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
FILE *v3; // r15
__int64 v4; // rbx
const char *v5; // r13
const char *v6; // rbp
int v7; // er12

v3 = _bss_start;
if ( argc == 1 )
{
fwrite("-hpet: requires command\n", 1uLL, 0x18uLL, _bss_start);
v7 = -1;
print_usage();
}
else
{
v4 = 0LL;
v5 = argv[1];
while ( 1 )
{
v6 = (&hpet_command)[2 * v4];
v7 = strcmp(v5, v6);
if ( !v7 )
break;
if ( ++v4 == 4 )
{
fprintf(v3, "do_hpet: command %s not implemented\n", v5);
return -1;
}
}
fprintf(v3, "-hpet: executing %s\n", v6);
((void (__fastcall *)(_QWORD, const char **))*(&off_4040C8 + 2 * (int)v4))((unsigned int)(argc - 2), argv + 2);
}
return v7;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
FILE *v3; // r15
__int64 v4; // rbx
const char *v5; // r13
const char *v6; // rbp
int v7; // er12

v3 = _bss_start;
if ( argc == 1 )
{
fwrite("-hpet: requires command\n", 1uLL, 0x18uLL, _bss_start);
v7 = -1;
func12();
}
else
{
v4 = 0LL;
v5 = argv[1];
while ( 1 )
{
v6 = (&hpet_command)[2 * v4];
v7 = strcmp(v5, v6);
if ( !v7 )
break;
if ( ++v4 == 4 )
{
fprintf(v3, "do_hpet: command %s not implemented\n", v5);
return -1;
}
}
fprintf(v3, "-hpet: executing %s\n", v6);
((void (__fastcall *)(_QWORD, const char **))*(&off_4040C8 + 2 * (int)v4))((unsigned int)(argc - 2), argv + 2);
}
return v7;
}

original_code:
int
main(int argc, const char ** argv)
{
 int i;
 argc--;
 argv++;
 if (!argc) {
  fprintf(stderr, "-hpet: requires command\n");
        print_usage();
  return -1;
 }
 for (i = 0; i < (sizeof (hpet_command) / sizeof (hpet_command[0])); i++)
  if (!strcmp(argv[0], hpet_command[i].command)) {
   argc--;
   argv++;
   fprintf(stderr, "-hpet: executing %s\n",
    hpet_command[i].command);
   hpet_command[i].func(argc, argv);
   return 0;
  }
 fprintf(stderr, "do_hpet: command %s not implemented\n", argv[0]);
 return -1;
}

canonical_original_code:
int main(int argc, char **argv)
{
 int i;
 argc--;
 argv++;
 if (!argc) {
  fprintf(stderr, "-hpet: requires command\n");
        print_usage();
  return -1;
 }
 for (i = 0; i < (sizeof (hpet_command) / sizeof (hpet_command[0])); i++)
  if (!strcmp(argv[0], hpet_command[i].command)) {
   argc--;
   argv++;
   fprintf(stderr, "-hpet: executing %s\n",
    hpet_command[i].command);
   hpet_command[i].func(argc, argv);
   return 0;
  }
 fprintf(stderr, "do_hpet: command %s not implemented\n", argv[0]);
 return -1;
}


decompiled_code:
int print_usage()
{
FILE **v0; // rbx
FILE *v1; // rsi
int result; // eax

v0 = (FILE **)&hpet_command;
puts("Usage:");
do
{
v1 = *v0;
v0 += 2;
result = printf("\t%s\n", (const char *)v1);
}
while ( v0 != &_bss_start );
return result;
}

canonical_decompiled_code:
int func12()
{
FILE **v0; // rbx
FILE *v1; // rsi
int result; // eax

v0 = (FILE **)&hpet_command;
puts("Usage:");
do
{
v1 = *v0;
v0 += 2;
result = printf("\t%s\n", (const char *)v1);
}
while ( v0 != &_bss_start );
return result;
}

original_code:
void print_usage()
{
    int i;
    printf("Usage:\n");
 for (i = 0; i < (sizeof (hpet_command) / sizeof (hpet_command[0])); i++) {
        printf("\t%s\n", hpet_command[i].command);
    }
}


decompiled_code:
__int64 GetFileSize_test()
{
unsigned int v0; // eax

v0 = GetFileSize("./filesystem.c");
printf("%s size = %d\n", "./filesystem.c", v0);
return 0LL;
}

canonical_decompiled_code:
__int64 func7()
{
unsigned int v0; // eax

v0 = func10("./filesystem.c");
printf("%s size = %d\n", "./filesystem.c", v0);
return 0LL;
}

original_code:
int GetFileSize_test() {
 const char *file = "./filesystem.c";
 int size = GetFileSize(file);
 printf("%s size = %d\n", file, size);
 return 0;
}

canonical_original_code:
int GetFileSize_test() {
 char *file = "./filesystem.c";
 int size = GetFileSize(file);
 printf("%s size = %d\n", file, size);
 return 0;
}

variable_types:
file
char *

variable_types: size int


decompiled_code:
__int64 DeleteFile_test()
{
return 0LL;
}

canonical_decompiled_code:
__int64 func8()
{
return 0LL;
}

original_code:
int DeleteFile_test() {
 return 0;
}

canonical_original_code:
int DeleteFile_test() {
 return 0;
}


decompiled_code:
__int64 GetFreeSpace_test()
{
unsigned int v0; // eax

v0 = GetFreeSpace("/boot");
printf("/boot free space is %d\n", v0);
return 0LL;
}

canonical_decompiled_code:
__int64 func9()
{
unsigned int v0; // eax

v0 = func12("/boot");
printf("/boot free space is %d\n", v0);
return 0LL;
}

original_code:
int GetFreeSpace_test() {
 int size = GetFreeSpace("/boot");
 printf("/boot free space is %d\n", size);
 return 0;
}


GetFileSize

decompiled_code:
__int64 __fastcall GetFileSize(char *a1)
{
struct stat v2; // [rsp-30h] [rbp-98h] BYREF

if ( !a1 )
return 0xFFFFFFFFLL;
if ( (int)stat(a1, &v2) >= 0 )
return LODWORD(v2.st_size);
perror("stat failed");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func10(char *a1)
{
struct stat v2; // [rsp-30h] [rbp-98h] BYREF

if ( !a1 )
return 0xFFFFFFFFLL;
if ( (int)func17(a1, &v2) >= 0 )
return LODWORD(v2.st_size);
perror("stat failed");
return 0xFFFFFFFFLL;
}

original_code:
int GetFileSize(const char *name)
{
 if (((void *)0) == name)
  return -1;
 struct stat st;
 if (stat(name, &st) < 0) {
  perror("stat failed");
  return -1;
 }
 return st.st_size;
}

canonical_original_code:
int GetFileSize(char *name)
{
 if (((void *)0) == name)
  return -1;
 struct stat st;
 if (stat(name, &st) < 0) {
  perror("stat failed");
  return -1;
 }
 return st.st_size;
}

variable_types:
name
char *

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; long st_atime; unsigned long st_atimensec; long st_mtime; unsigned long st_mtimensec; long st_ctime; unsigned long st_ctimensec; long __glibc_reserved[3]; }


DeleteFile

decompiled_code:
int __fastcall DeleteFile(const char *a1)
{
int result; // eax

if ( a1 )
result = unlink(a1);
else
result = -1;
return result;
}

canonical_decompiled_code:
int __fastcall func11(const char *a1)
{
int result; // eax

if ( a1 )
result = unlink(a1);
else
result = -1;
return result;
}

original_code:
int DeleteFile(const char *name)
{
 if (((void *)0) == name)
  return -1;
 return unlink(name);
}

canonical_original_code:
int DeleteFile(char *name)
{
 if (((void *)0) == name)
  return -1;
 return unlink(name);
}


GetFreeSpace

decompiled_code:
__fsblkcnt_t __fastcall GetFreeSpace(const char *a1)
{
__fsblkcnt_t result; // rax
struct statfs v2; // [rsp-8h] [rbp-88h] BYREF

if ( !a1 )
return 0xFFFFFFFFLL;
statfs(a1, &v2);
result = v2.f_bsize * v2.f_bfree;
if ( v2.f_bsize * v2.f_bfree > 0x7FFFFFFF )
result = 0x7FFFFFFFLL;
return result;
}

canonical_decompiled_code:
__fsblkcnt_t __fastcall func12(const char *a1)
{
__fsblkcnt_t result; // rax
struct statfs v2; // [rsp-8h] [rbp-88h] BYREF

if ( !a1 )
return 0xFFFFFFFFLL;
statfs(a1, &v2);
result = v2.f_bsize * v2.f_bfree;
if ( v2.f_bsize * v2.f_bfree > 0x7FFFFFFF )
result = 0x7FFFFFFFLL;
return result;
}

original_code:
int GetFreeSpace(const char *path)
{
 struct statfs diskInfo;
 unsigned long long freeDisk = 0;
 if (path == ((void *)0))
  return -1;
 statfs(path, &diskInfo);
 freeDisk = diskInfo.f_bfree * diskInfo.f_bsize;
 if( freeDisk > 0x7FFFFFFFUL )
  freeDisk = 0x7FFFFFFF;
 return (int)freeDisk;
}

canonical_original_code:
int GetFreeSpace(char *path)
{
 struct statfs diskInfo;
 unsigned long long freeDisk = 0;
 if (path == ((void *)0))
  return -1;
 statfs(path, &diskInfo);
 freeDisk = diskInfo.f_bfree * diskInfo.f_bsize;
 if( freeDisk > 0x7FFFFFFFUL )
  freeDisk = 0x7FFFFFFF;
 return (int)freeDisk;
}

variable_types:
path
char *

variable_types: struct statfs { long f_type; long f_bsize; unsigned long f_blocks; unsigned long f_bfree; unsigned long f_bavail; unsigned long f_files; unsigned long f_ffree; struct __fsid_t { int __val[2]; } f_fsid; long f_namelen; long f_frsize; long f_flags; long f_spare[4]; }

variable_types:
freeDisk
unsigned long long

user_defined_types: struct statfs { long f_type; long f_bsize; unsigned long f_blocks; unsigned long f_bfree; unsigned long f_bavail; unsigned long f_files; unsigned long f_ffree; struct __fsid_t f_fsid; long f_namelen; long f_frsize; long f_flags; long f_spare[4]; }


decompiled_code:
__int64 __fastcall page_iterate(__int64 a1, int a2, int a3, __int64 (__fastcall *a4)(__int64, _QWORD, __int64), __int64 a5)
{
int v5; // ebp
unsigned int v6; // ebx
unsigned int v8; // er12
int v9; // eax
__int64 v11; // [rsp+8h] [rbp-40h]

v5 = a3;
v6 = a2 * ((int)a1 / a2 + 1);
if ( a3 <= 0 )
return 0;
v8 = 0;
while ( a2 < v5 )
{
v11 = a5;
v9 = a4(a1, v6 - (unsigned int)a1, a5);
a5 = v11;
if ( v9 < 0 )
goto LABEL_8;
v5 -= v6 - a1;
v8 += v9;
a1 = v6;
if ( v5 <= 0 )
return v8;
v6 += a2;
}
v9 = a4(a1, (unsigned int)v5, a5);
if ( v9 < 0 )
{
LABEL_8:
v8 = -1;
printf("iterator func return failure, ret = %d\n", (unsigned int)v9);
return v8;
}
v8 += v9;
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1, int a2, int a3, __int64 (__fastcall *a4)(__int64, _QWORD, __int64), __int64 a5)
{
int v5; // ebp
unsigned int v6; // ebx
unsigned int v8; // er12
int v9; // eax
__int64 v11; // [rsp+8h] [rbp-40h]

v5 = a3;
v6 = a2 * ((int)a1 / a2 + 1);
if ( a3 <= 0 )
return 0;
v8 = 0;
while ( a2 < v5 )
{
v11 = a5;
v9 = a4(a1, v6 - (unsigned int)a1, a5);
a5 = v11;
if ( v9 < 0 )
goto LABEL_8;
v5 -= v6 - a1;
v8 += v9;
a1 = v6;
if ( v5 <= 0 )
return v8;
v6 += a2;
}
v9 = a4(a1, (unsigned int)v5, a5);
if ( v9 < 0 )
{
LABEL_8:
v8 = -1;
printf("iterator func return failure, ret = %d\n", (unsigned int)v9);
return v8;
}
v8 += v9;
return v8;
}

original_code:
int page_iterate(int start, int page_size, size_t count, iterator_func_t func, void *data)
{
 int left = 0;
 int offset = 0;
 int len = 0;
 int next_page_offset = 0;
 int sum = 0;
 int ret = 0;
 left = count;
 offset = start;
 next_page_offset = (offset / page_size + 1) * page_size;
 while (left > 0) {
  len = left > page_size ? next_page_offset - offset : left;
  if ( (ret = func(offset, len, data)) < 0) {
   printf("iterator func return failure, ret = %d\n", ret);
   return -1;
  }
  offset += len;
  left -= len;
  next_page_offset += page_size;
  sum += ret;
 }
 return sum;
}

canonical_original_code:
int page_iterate(int start, int page_size, unsigned long count, int func(int offset, int len, void *data), void *data)
{
 int left = 0;
 int offset = 0;
 int len = 0;
 int next_page_offset = 0;
 int sum = 0;
 int ret = 0;
 left = count;
 offset = start;
 next_page_offset = (offset / page_size + 1) * page_size;
 while (left > 0) {
  len = left > page_size ? next_page_offset - offset : left;
  if ( (ret = func(offset, len, data)) < 0) {
   printf("iterator func return failure, ret = %d\n", ret);
   return -1;
  }
  offset += len;
  left -= len;
  next_page_offset += page_size;
  sum += ret;
 }
 return sum;
}

variable_types: start int

variable_types:
count
unsigned long

variable_types:
func
int (int offset, int len, void *data)

variable_types:
data
void *

variable_types: left int

variable_types: offset int

variable_types: len int

variable_types: sum int

variable_types: ret int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
GetFileSize_test(argc, argv, envp);
GetFreeSpace_test();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
func7(argc, argv, envp);
func9();
return 0;
}

original_code:
int main(int argc, char **argv) {
 GetFileSize_test();
 GetFreeSpace_test();
 return 0;
}

variable_types: argc int

variable_types:
argv
char **


variable_types:
s
unsigned char

variable_types:
results
int [4]

variable_types: i int

variable_types: aa int

variable_types: bb int


qprint

decompiled_code:
int __fastcall qprint(const void *a1)
{
return printf("%p ", a1);
}

canonical_decompiled_code:
int __fastcall func7(const void *a1)
{
return printf("%p ", a1);
}

original_code:
static void qprint(void *data)
{
 printf("%p ", data);
}


decompiled_code:
__int64 test_queue()
{
void *v0; // rbp
const void *v1; // rax
const void *v2; // rax

v0 = (void *)NewQueue();
QueuePushBack(v0, 1LL);
QueuePushBack(v0, 2LL);
QueuePushBack(v0, 3LL);
QueuePrint(v0, qprint);
v1 = (const void *)QueueFront(v0);
printf("%p\n", v1);
QueuePopFront(v0);
QueuePrint(v0, qprint);
v2 = (const void *)QueueFront(v0);
printf("%p\n", v2);
QueuePopFront(v0);
QueuePopFront(v0);
QueuePrint(v0, qprint);
QueuePopFront(v0);
QueuePrint(v0, qprint);
return DestroyQueue(v0);
}

canonical_decompiled_code:
__int64 func10()
{
void *v0; // rbp
const void *v1; // rax
const void *v2; // rax

v0 = (void *)func11();
QueuePushBack(v0, 1LL);
QueuePushBack(v0, 2LL);
QueuePushBack(v0, 3LL);
func14(v0, qprint);
v1 = (const void *)QueueFront(v0);
printf("%p\n", v1);
func13(v0);
func14(v0, qprint);
v2 = (const void *)QueueFront(v0);
printf("%p\n", v2);
func13(v0);
func13(v0);
func14(v0, qprint);
func13(v0);
func14(v0, qprint);
return func12(v0);
}

original_code:
void test_queue()
{
 Queue *q = NewQueue();
 QueuePushBack(q, (void *)1);
 QueuePushBack(q, (void *)2);
 QueuePushBack(q, (void *)3);
 QueuePrint(q, qprint);
 void *data = QueueFront(q);
 printf("%p\n", data);
 QueuePopFront(q);
 QueuePrint(q, qprint);
 data = QueueFront(q);
 printf("%p\n", data);
 QueuePopFront(q);
 QueuePopFront(q);
 QueuePrint(q, qprint);
 QueuePopFront(q);
 QueuePrint(q, qprint);
 DestroyQueue(q);
}

canonical_original_code:
void test_queue()
{
 struct tailqHead *q = NewQueue();
 QueuePushBack(q, (void *)1);
 QueuePushBack(q, (void *)2);
 QueuePushBack(q, (void *)3);
 QueuePrint(q, qprint);
 void *data = QueueFront(q);
 printf("%p\n", data);
 QueuePopFront(q);
 QueuePrint(q, qprint);
 data = QueueFront(q);
 printf("%p\n", data);
 QueuePopFront(q);
 QueuePopFront(q);
 QueuePrint(q, qprint);
 QueuePopFront(q);
 QueuePrint(q, qprint);
 DestroyQueue(q);
}

variable_types: struct tailqHead { struct _element { void *data; struct { struct _element *tqe_next; struct _element **tqe_prev; } _elements; } *tqh_first; struct _element **tqh_last; } *

user_defined_types: struct tailqHead { struct _element *tqh_first; struct _element **tqh_last; }


NewQueue

decompiled_code:
_QWORD *NewQueue()
{
_QWORD *result; // rax

result = malloc(0x10uLL);
*result = 0LL;
result[1] = result;
return result;
}

canonical_decompiled_code:
_QWORD *func11()
{
_QWORD *result; // rax

result = malloc(0x10uLL);
*result = 0LL;
result[1] = result;
return result;
}

original_code:
Queue *NewQueue()
{
 Queue *q = malloc(sizeof(Queue));
 do { (q)->tqh_first = ((void *)0); (q)->tqh_last = &(q)->tqh_first; } while ( 0);
 return q;
}

canonical_original_code:
struct tailqHead *NewQueue()
{
 struct tailqHead *q = malloc(sizeof(Queue));
 do { (q)->tqh_first = ((void *)0); (q)->tqh_last = &(q)->tqh_first; } while ( 0);
 return q;
}

return_type: struct tailqHead { struct _element { void *data; struct { struct _element *tqe_next; struct _element **tqe_prev; } _elements; } *tqh_first; struct _element **tqh_last; } *


DestroyQueue

decompiled_code:
void __fastcall DestroyQueue(void *ptr)
{
_QWORD *v1; // rbx
__int64 v2; // rax

v1 = *(_QWORD **)ptr;
if ( *(_QWORD *)ptr )
{
do
{
v2 = v1[1];
if ( v2 )
*(_QWORD *)(v2 + 16) = v1[2];
*(_QWORD *)v1[2] = v2;
free(v1);
v1 = (_QWORD *)v1[1];
}
while ( v1 );
}
free(ptr);
}

canonical_decompiled_code:
void __fastcall func12(void *ptr)
{
_QWORD *v1; // rbx
__int64 v2; // rax

v1 = *(_QWORD **)ptr;
if ( *(_QWORD *)ptr )
{
do
{
v2 = v1[1];
if ( v2 )
*(_QWORD *)(v2 + 16) = v1[2];
*(_QWORD *)v1[2] = v2;
free(v1);
v1 = (_QWORD *)v1[1];
}
while ( v1 );
}
free(ptr);
}

original_code:
void DestroyQueue(Queue *q)
{
 struct tailqHead *pHead = q;
 struct _element *elm = ((void *)0);
 for ((elm) = ((pHead)->tqh_first); (elm); (elm) = ((elm)->_elements.tqe_next)) {
  do { if (((elm)->_elements.tqe_next) != ((void *)0)) (elm)->_elements.tqe_next->_elements.tqe_prev = (elm)->_elements.tqe_prev; else (pHead)->tqh_last = (elm)->_elements.tqe_prev; *(elm)->_elements.tqe_prev = (elm)->_elements.tqe_next; } while ( 0);
  free(elm);
 }
 free(q);
}

canonical_original_code:
void DestroyQueue(struct tailqHead *q)
{
 struct tailqHead *pHead = q;
 struct _element *elm = ((void *)0);
 for ((elm) = ((pHead)->tqh_first); (elm); (elm) = ((elm)->_elements.tqe_next)) {
  do { if (((elm)->_elements.tqe_next) != ((void *)0)) (elm)->_elements.tqe_next->_elements.tqe_prev = (elm)->_elements.tqe_prev; else (pHead)->tqh_last = (elm)->_elements.tqe_prev; *(elm)->_elements.tqe_prev = (elm)->_elements.tqe_next; } while ( 0);
  free(elm);
 }
 free(q);
}

variable_types: struct _element { void *data; struct { struct _element *tqe_next; struct _element **tqe_prev; } _elements; } *


QueuePopFront

original_code:
void QueuePopFront(Queue *q)
{
 struct _element *elm = ((q)->tqh_first);
 if (elm == ((void *)0))
  return;
 do { if (((elm)->_elements.tqe_next) != ((void *)0)) (elm)->_elements.tqe_next->_elements.tqe_prev = (elm)->_elements.tqe_prev; else (q)->tqh_last = (elm)->_elements.tqe_prev; *(elm)->_elements.tqe_prev = (elm)->_elements.tqe_next; } while ( 0);
 free(elm);
}

canonical_original_code:
void QueuePopFront(struct tailqHead *q)
{
 struct _element *elm = ((q)->tqh_first);
 if (elm == ((void *)0))
  return;
 do { if (((elm)->_elements.tqe_next) != ((void *)0)) (elm)->_elements.tqe_next->_elements.tqe_prev = (elm)->_elements.tqe_prev; else (q)->tqh_last = (elm)->_elements.tqe_prev; *(elm)->_elements.tqe_prev = (elm)->_elements.tqe_next; } while ( 0);
 free(elm);
}

user_defined_types: struct _element { void *data; struct { struct _element *tqe_next; struct _element **tqe_prev; } _elements; }


QueuePrint

decompiled_code:
int __fastcall QueuePrint(_QWORD *a1, void (__fastcall *a2)(_QWORD))
{
_QWORD *v2; // rbx

printf("queue: ");
v2 = (_QWORD *)*a1;
if ( *a1 )
{
do
{
a2(*v2);
v2 = (_QWORD *)v2[1];
}
while ( v2 );
}
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func14(_QWORD *a1, void (__fastcall *a2)(_QWORD))
{
_QWORD *v2; // rbx

printf("queue: ");
v2 = (_QWORD *)*a1;
if ( *a1 )
{
do
{
a2(*v2);
v2 = (_QWORD *)v2[1];
}
while ( v2 );
}
return putchar(10);
}

original_code:
void QueuePrint(Queue *q, void (*print)(void *data))
{
 struct _element *elm = ((void *)0);
 printf("queue: ");
 for ((elm) = ((q)->tqh_first); (elm); (elm) = ((elm)->_elements.tqe_next)) {
  print(elm->data);
 }
 printf("\n");
}

canonical_original_code:
void QueuePrint(struct tailqHead *q, void (*print)(void *data))
{
 struct _element *elm = ((void *)0);
 printf("queue: ");
 for ((elm) = ((q)->tqh_first); (elm); (elm) = ((elm)->_elements.tqe_next)) {
  print(elm->data);
 }
 printf("\n");
}

variable_types:
print
void (*)(void *data)


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
test_queue(argc, argv, envp);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
func10(argc, argv, envp);
return 0;
}

original_code:
int main(int argc, char **argv)
{
 test_queue();
    return 0;
}


decompiled_code:
int __fastcall init_statistic(__int64 a1, const char *a2, int a3)
{
int result; // eax

strncpy((char *)a1, a2, 0x20uLL);
*(_DWORD *)(a1 + 64) = 0;
result = gettimeofday((struct timeval *)(a1 + 32), 0LL);
*(_DWORD *)(a1 + 68) = a3;
return result;
}

canonical_decompiled_code:
int __fastcall func8(__int64 a1, const char *a2, int a3)
{
int result; // eax

strncpy((char *)a1, a2, 0x20uLL);
*(_DWORD *)(a1 + 64) = 0;
result = gettimeofday((struct timeval *)(a1 + 32), 0LL);
*(_DWORD *)(a1 + 68) = a3;
return result;
}

original_code:
void init_statistic(struct statistic *p, char *name, int interval)
{
  strncpy(p->name, name, 32);
  p->count = 0;
  gettimeofday(&p->last, ((void *)0));
  p->interval = interval;
}

variable_types: struct statistic { char name[32]; struct timeval { long tv_sec; long tv_usec; } last; struct timeval now; unsigned int count; int interval; } *

variable_types:
name
char *

variable_types: interval int

user_defined_types: struct statistic { char name[32]; struct timeval last; struct timeval now; unsigned int count; int interval; }


original_code:
void print_statistic(struct statistic *p)
{
  gettimeofday(&p->now, ((void *)0));
  p->count++;
  int interval = get_interval(&p->now, &p->last);
  if (interval <= p->interval) {
    return;
  }
  printf("\n[%s] %ld.%06ld: fps: %.2f in last %ds\n", p->name,
   p->now.tv_sec, p->now.tv_usec,
   (float)p->count * 1000000 / interval, p->interval / 1000000);
  p->count = 0;
  p->last.tv_sec = p->now.tv_sec;
  p->last.tv_usec = p->now.tv_usec;
}


decompiled_code:
_BOOL8 __fastcall DDC_Init(const char *a1)
{
if ( Ddcfd > 0 || !a1 )
return 0LL;
Ddcfd = open(a1, 2);
return Ddcfd >= 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func9(const char *a1)
{
if ( Ddcfd > 0 || !a1 )
return 0LL;
Ddcfd = open(a1, 2);
return Ddcfd >= 0;
}

original_code:
int DDC_Init(const char *devpath)
{
 if ((Ddcfd > 0) || (((void *)0) == devpath)) {
  return 0;
 }
    Ddcfd = open(devpath, 02);
 if (Ddcfd < 0) {
  return 0;
 }
 return 1;
}

canonical_original_code:
int DDC_Init(char *devpath)
{
 if ((Ddcfd > 0) || (((void *)0) == devpath)) {
  return 0;
 }
    Ddcfd = open(devpath, 02);
 if (Ddcfd < 0) {
  return 0;
 }
 return 1;
}

variable_types:
devpath
char *


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

if ( Ddcfd < 0 )
return 0LL;
close(Ddcfd);
result = 1LL;
Ddcfd = -1;
return result;
}

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

if ( Ddcfd < 0 )
return 0LL;
close(Ddcfd);
result = 1LL;
Ddcfd = -1;
return result;
}

original_code:
int DDC_Deinit()
{
 if (Ddcfd < 0)
  return 0;
    close(Ddcfd);
 Ddcfd = -1;
 return 1;
}


decompiled_code:
__int64 DDC_ProbeEdid()
{
if ( Ddcfd >= 0 )
{
memset(&EdidBuf, 0, 0x80uLL);
test_ddcci_isra_0(Ddcfd);
}
return 0LL;
}

canonical_decompiled_code:
__int64 func11()
{
if ( Ddcfd >= 0 )
{
memset(&EdidBuf, 0, 0x80uLL);
func8(Ddcfd);
}
return 0LL;
}

original_code:
struct edid_info *DDC_ProbeEdid()
{
 unsigned char *buf = EdidBuf;
 int fd = Ddcfd;
 if (fd < 0)
  return ((void *)0);
 memset(buf, 0, 128);
    test_ddcci(fd);
 return ((void *)0);
}

variable_types:
buf
unsigned char *

variable_types: fd int

return_type: struct edid_info { unsigned char version; unsigned char revision; unsigned char manufacturer[4]; unsigned char product[14]; unsigned char serial_no[14]; unsigned char ascii[14]; unsigned int model; unsigned int serial; unsigned int year; unsigned int week; int ratio_type; unsigned char hsize; unsigned char vsize; float landscape_ratio; float portait_ratio; int err; } *


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
__int16 s1; // [rsp+Ch] [rbp-Ch] BYREF
__int16 s2[5]; // [rsp+Eh] [rbp-Ah] BYREF

s2[0] = 512;
s1 = 512;
v3 = memcmp(&s1, s2, 2uLL);
printf("ret = %d\n", v3);
return 0;
}

original_code:
int main(int argc, char *argv[])
{
    struct edid_info *pinfo = ((void *)0);
    int interval = 0;
    int ret = 0;
 char a[] = {0x00, 0x02};
 char b[] = {0x00, 0x02};
 int i = memcmp(a, b, 2);
 printf("ret = %d\n", i);
 return 0;
    if (argc == 1) {
        DDC_Init("/dev/i2c-2");
        DDC_ProbeEdid();
        DDC_Deinit();
        return 0;
    } else
        print_usage();
    return 0;
}

variable_types:
argv
char *[]

variable_types: struct edid_info { unsigned char version; unsigned char revision; unsigned char manufacturer[4]; unsigned char product[14]; unsigned char serial_no[14]; unsigned char ascii[14]; unsigned int model; unsigned int serial; unsigned int year; unsigned int week; int ratio_type; unsigned char hsize; unsigned char vsize; float landscape_ratio; float portait_ratio; int err; } *

variable_types: ret int

variable_types:
a
char []

variable_types:
b
char []


SemaphoreCreate

decompiled_code:
__int64 __fastcall SemaphoreCreate(pthread_mutex_t **a1, int a2)
{
pthread_mutex_t *v2; // rax
pthread_mutex_t *v3; // rbx
__int64 result; // rax

v2 = (pthread_mutex_t *)malloc(0x60uLL);
if ( !v2 )
return 4294967284LL;
v3 = v2;
pthread_mutex_init(v2, 0LL);
pthread_cond_init((pthread_cond_t *)&v3[1], 0LL);
v3[2].__owner = a2;
result = 0LL;
*a1 = v3;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(pthread_mutex_t **a1, int a2)
{
pthread_mutex_t *v2; // rax
pthread_mutex_t *v3; // rbx
__int64 result; // rax

v2 = (pthread_mutex_t *)malloc(0x60uLL);
if ( !v2 )
return 4294967284LL;
v3 = v2;
pthread_mutex_init(v2, 0LL);
pthread_cond_init((pthread_cond_t *)&v3[1], 0LL);
v3[2].__owner = a2;
result = 0LL;
*a1 = v3;
return result;
}

original_code:
int SemaphoreCreate(SemaphoreHandle *Sem, uint32_t Cnt)
{
    Semaphore *s;
    s = malloc(sizeof(Semaphore));
    if (!s)
        return -12;
    (void)pthread_mutex_init( &s->mutex, 0 );
    (void)pthread_cond_init( &s->cond, 0 );
    s->count = Cnt;
    *Sem = s;
    return 0;
}

canonical_original_code:
int SemaphoreCreate(struct SemaphoreRec **Sem, unsigned int Cnt)
{
    struct SemaphoreRec *s;
    s = malloc(sizeof(Semaphore));
    if (!s)
        return -12;
    (void)pthread_mutex_init( &s->mutex, 0 );
    (void)pthread_cond_init( &s->cond, 0 );
    s->count = Cnt;
    *Sem = s;
    return 0;
}

variable_types: struct SemaphoreRec { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } cond; unsigned int count; } **

variable_types:
Cnt
unsigned int

user_defined_types: struct SemaphoreRec { union pthread_mutex_t mutex; union pthread_cond_t cond; unsigned int count; }

user_defined_types: union pthread_mutex_t { struct __pthread_mutex_s __data; char __size[40]; long __align; }

user_defined_types: union pthread_cond_t { struct __pthread_cond_s __data; char __size[48]; long long __align; }

user_defined_types: struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list __list; }


SemaphoreWaitTimeout

decompiled_code:
__int64 __fastcall SemaphoreWaitTimeout(pthread_mutex_t *mutex, unsigned int a2)
{
int v2; // eax
int v3; // eax
unsigned int v4; // er13
struct timeval v6; // [rsp+0h] [rbp-48h] BYREF
struct timespec abstime; // [rsp+10h] [rbp-38h] BYREF

pthread_mutex_lock(mutex);
v2 = mutex[2].__owner;
if ( v2 )
goto LABEL_9;
if ( !a2 )
goto LABEL_11;
if ( !gettimeofday(&v6, 0LL) )
{
abstime.tv_sec = v6.tv_sec + a2 / 0x3E8;
abstime.tv_nsec = 1000 * v6.tv_usec + 1000000 * a2 - 1000000000 * (a2 / 0x3E8);
v3 = pthread_cond_timedwait((pthread_cond_t *)&mutex[1], mutex, &abstime);
if ( v3 )
{
if ( v3 != 110 && v3 != 4 )
goto LABEL_7;
LABEL_11:
v4 = -110;
goto LABEL_10;
}
v2 = mutex[2].__owner;
LABEL_9:
v4 = 0;
mutex[2].__owner = v2 - 1;
goto LABEL_10;
}
LABEL_7:
v4 = -95;
LABEL_10:
pthread_mutex_unlock(mutex);
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func9(pthread_mutex_t *mutex, unsigned int a2)
{
int v2; // eax
int v3; // eax
unsigned int v4; // er13
struct timeval v6; // [rsp+0h] [rbp-48h] BYREF
struct timespec abstime; // [rsp+10h] [rbp-38h] BYREF

pthread_mutex_lock(mutex);
v2 = mutex[2].__owner;
if ( v2 )
goto LABEL_9;
if ( !a2 )
goto LABEL_11;
if ( !gettimeofday(&v6, 0LL) )
{
abstime.tv_sec = v6.tv_sec + a2 / 0x3E8;
abstime.tv_nsec = 1000 * v6.tv_usec + 1000000 * a2 - 1000000000 * (a2 / 0x3E8);
v3 = pthread_cond_timedwait((pthread_cond_t *)&mutex[1], mutex, &abstime);
if ( v3 )
{
if ( v3 != 110 && v3 != 4 )
goto LABEL_7;
LABEL_11:
v4 = -110;
goto LABEL_10;
}
v2 = mutex[2].__owner;
LABEL_9:
v4 = 0;
mutex[2].__owner = v2 - 1;
goto LABEL_10;
}
LABEL_7:
v4 = -95;
LABEL_10:
pthread_mutex_unlock(mutex);
return v4;
}

original_code:
int SemaphoreWaitTimeout(Semaphore *s, uint32_t msec)
{
    int ret = 0;
    pthread_mutex_lock( &s->mutex );
    if( s->count > 0 ) {
        goto wait_end;
    }
    if( !msec ) {
        ret = -110;
        goto wait_end;
    } else {
        struct timeval curTime;
        struct timespec timeoutVal;
        uint32_t timeout_sec;
        uint32_t timeout_nsec;
        int error;
        timeout_sec = msec / 1000;
        timeout_nsec = msec * 1000 * 1000 - (timeout_sec * 1000000000);
        if( gettimeofday(&curTime, ((void *)0)) ) {
            ret = -95;
            goto wait_end;
        }
        timeoutVal.tv_sec = curTime.tv_sec + timeout_sec;
        timeoutVal.tv_nsec = (curTime.tv_usec * 1000) + timeout_nsec;
        error = pthread_cond_timedwait( &s->cond, &s->mutex, &timeoutVal );
        if( error ) {
            if( error == 110 || error == 4 ) {
                ret = -110;
            } else {
                ret = -95;
            }
        }
    }
wait_end:
    if(ret == 0) {
        s->count--;
    }
    pthread_mutex_unlock( &s->mutex );
    return ret;
}

canonical_original_code:
int SemaphoreWaitTimeout(struct SemaphoreRec *s, unsigned int msec)
{
    int ret = 0;
    pthread_mutex_lock( &s->mutex );
    if( s->count > 0 ) {
        goto wait_end;
    }
    if( !msec ) {
        ret = -110;
        goto wait_end;
    } else {
        struct timeval curTime;
        struct timespec timeoutVal;
        unsigned int timeout_sec;
        unsigned int timeout_nsec;
        int error;
        timeout_sec = msec / 1000;
        timeout_nsec = msec * 1000 * 1000 - (timeout_sec * 1000000000);
        if( gettimeofday(&curTime, ((void *)0)) ) {
            ret = -95;
            goto wait_end;
        }
        timeoutVal.tv_sec = curTime.tv_sec + timeout_sec;
        timeoutVal.tv_nsec = (curTime.tv_usec * 1000) + timeout_nsec;
        error = pthread_cond_timedwait( &s->cond, &s->mutex, &timeoutVal );
        if( error ) {
            if( error == 110 || error == 4 ) {
                ret = -110;
            } else {
                ret = -95;
            }
        }
    }
wait_end:
    if(ret == 0) {
        s->count--;
    }
    pthread_mutex_unlock( &s->mutex );
    return ret;
}

variable_types: struct SemaphoreRec { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } cond; unsigned int count; } *

variable_types:
msec
unsigned int

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

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

variable_types: error int


SemaphoreWait

decompiled_code:
int __fastcall SemaphoreWait(pthread_mutex_t *mutex)
{
int i; // eax

pthread_mutex_lock(mutex);
for ( i = mutex[2].__owner; !i; i = mutex[2].__owner )
pthread_cond_wait((pthread_cond_t *)&mutex[1], mutex);
mutex[2].__owner = i - 1;
return pthread_mutex_unlock(mutex);
}

canonical_decompiled_code:
int __fastcall func10(pthread_mutex_t *mutex)
{
int i; // eax

pthread_mutex_lock(mutex);
for ( i = mutex[2].__owner; !i; i = mutex[2].__owner )
pthread_cond_wait((pthread_cond_t *)&mutex[1], mutex);
mutex[2].__owner = i - 1;
return pthread_mutex_unlock(mutex);
}

original_code:
void SemaphoreWait(Semaphore *s)
{
    pthread_mutex_lock( &s->mutex );
    while( !s->count ) {
        pthread_cond_wait( &s->cond, &s->mutex );
    }
    s->count--;
    pthread_mutex_unlock( &s->mutex );
}

canonical_original_code:
void SemaphoreWait(struct SemaphoreRec *s)
{
    pthread_mutex_lock( &s->mutex );
    while( !s->count ) {
        pthread_cond_wait( &s->cond, &s->mutex );
    }
    s->count--;
    pthread_mutex_unlock( &s->mutex );
}

user_defined_types: struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; }

user_defined_types: struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; }


SemaphoreSignal

decompiled_code:
int __fastcall SemaphoreSignal(pthread_mutex_t *mutex)
{
pthread_mutex_lock(mutex);
++mutex[2].__owner;
pthread_cond_signal((pthread_cond_t *)&mutex[1]);
return pthread_mutex_unlock(mutex);
}

canonical_decompiled_code:
int __fastcall func11(pthread_mutex_t *mutex)
{
pthread_mutex_lock(mutex);
++mutex[2].__owner;
pthread_cond_signal((pthread_cond_t *)&mutex[1]);
return pthread_mutex_unlock(mutex);
}

original_code:
void SemaphoreSignal(SemaphoreHandle s)
{
    pthread_mutex_lock( &s->mutex );
    s->count++;
    pthread_cond_signal( &s->cond );
    pthread_mutex_unlock( &s->mutex );
}

canonical_original_code:
void SemaphoreSignal(struct SemaphoreRec *s)
{
    pthread_mutex_lock( &s->mutex );
    s->count++;
    pthread_cond_signal( &s->cond );
    pthread_mutex_unlock( &s->mutex );
}


SemaphoreDestroy

decompiled_code:
void __fastcall SemaphoreDestroy(void *ptr)
{
pthread_mutex_destroy((pthread_mutex_t *)ptr);
pthread_cond_destroy((pthread_cond_t *)((char *)ptr + 40));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func12(void *ptr)
{
pthread_mutex_destroy((pthread_mutex_t *)ptr);
pthread_cond_destroy((pthread_cond_t *)((char *)ptr + 40));
free(ptr);
}

original_code:
void SemaphoreDestroy(SemaphoreHandle s)
{
    (void)pthread_mutex_destroy( &s->mutex );
    (void)pthread_cond_destroy( &s->cond );
    free(s);
}

canonical_original_code:
void SemaphoreDestroy(struct SemaphoreRec *s)
{
    (void)pthread_mutex_destroy( &s->mutex );
    (void)pthread_cond_destroy( &s->cond );
    free(s);
}


decompiled_code:
__int64 __fastcall list_ajust(__int64 a1)
{
unsigned __int64 v1; // rsi
size_t v2; // rsi
__int64 v3; // rax
void *v4; // rax
__int64 result; // rax

v1 = *(_QWORD *)(a1 + 16);
if ( *(_QWORD *)(a1 + 8) < v1 )
return 0LL;
if ( v1 )
{
v3 = 2 * v1;
v2 = 16 * v1;
}
else
{
v2 = 32LL;
v3 = 4LL;
}
*(_QWORD *)(a1 + 16) = v3;
v4 = realloc(*(void **)a1, v2);
*(_QWORD *)a1 = v4;
if ( v4 )
result = 0LL;
else
result = 4294967284LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
unsigned __int64 v1; // rsi
size_t v2; // rsi
__int64 v3; // rax
void *v4; // rax
__int64 result; // rax

v1 = *(_QWORD *)(a1 + 16);
if ( *(_QWORD *)(a1 + 8) < v1 )
return 0LL;
if ( v1 )
{
v3 = 2 * v1;
v2 = 16 * v1;
}
else
{
v2 = 32LL;
v3 = 4LL;
}
*(_QWORD *)(a1 + 16) = v3;
v4 = realloc(*(void **)a1, v2);
*(_QWORD *)a1 = v4;
if ( v4 )
result = 0LL;
else
result = 4294967284LL;
return result;
}

original_code:
static int list_ajust(list_t *list)
{
 if (list->len < list->cap)
  return 0;
 list->cap = (list->cap == 0 ? 4: list->cap * 2);
 size_t size = sizeof(type_t *) * list->cap;
 list->data = (type_t **)realloc(list->data, size);
 if (list->data == ((void *)0))
  return -12;
 return 0;
}

canonical_original_code:
static int list_ajust(struct list_t *list)
{
 if (list->len < list->cap)
  return 0;
 list->cap = (list->cap == 0 ? 4: list->cap * 2);
 unsigned long size = sizeof(type_t *) * list->cap;
 list->data = (struct type_t **)realloc(list->data, size);
 if (list->data == ((void *)0))
  return -12;
 return 0;
}

variable_types: struct list_t { struct type_t { char name[10]; } **data; unsigned long len; unsigned long cap; } *

variable_types:
size
unsigned long

user_defined_types: struct list_t { struct type_t **data; unsigned long len; unsigned long cap; }


decompiled_code:
void __fastcall list_destroy(void *ptr)
{
free(*(void **)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func8(void *ptr)
{
free(*(void **)ptr);
free(ptr);
}

original_code:
void list_destroy(list_t *list)
{
 free(list->data);
 free(list);
}

canonical_original_code:
void list_destroy(struct list_t *list)
{
 free(list->data);
 free(list);
}


decompiled_code:
__int64 __fastcall list_add(_QWORD *a1, __int64 a2)
{
__int64 result; // rax
__int64 v3; // rdx

if ( !a2 )
__assert_fail("NULL != member", "array_list.c", 0x38u, "list_add");
result = list_ajust((__int64)a1);
if ( !(_DWORD)result )
{
v3 = a1[1];
*(_QWORD *)(*a1 + 8 * v3) = a2;
a1[1] = v3 + 1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(_QWORD *a1, __int64 a2)
{
__int64 result; // rax
__int64 v3; // rdx

if ( !a2 )
__assert_fail("NULL != member", "array_list.c", 0x38u, "list_add");
result = func7((__int64)a1);
if ( !(_DWORD)result )
{
v3 = a1[1];
*(_QWORD *)(*a1 + 8 * v3) = a2;
a1[1] = v3 + 1;
}
return result;
}

original_code:
int list_add(list_t *list, type_t *member)
{
 ((void) sizeof ((((void *)0) != member) ? 1 : 0), __extension__ ({ if (((void *)0) != member) ; else __assert_fail ("NULL != member", "array_list.c", 56, __extension__ __PRETTY_FUNCTION__); }));
 int ret;
 if ( (ret = list_ajust(list)) != 0)
  return ret;
 list->data[list->len] = member;
 list->len++;
 return 0;
}

canonical_original_code:
int list_add(struct list_t *list, struct type_t *member)
{
 ((void) sizeof ((((void *)0) != member) ? 1 : 0), __extension__ ({ if (((void *)0) != member) ; else __assert_fail ("NULL != member", "array_list.c", 56, __extension__ __PRETTY_FUNCTION__); }));
 int ret;
 if ( (ret = list_ajust(list)) != 0)
  return ret;
 list->data[list->len] = member;
 list->len++;
 return 0;
}

variable_types:
member
struct type_t *


decompiled_code:
__int64 __fastcall list_del(__int64 *a1, const char *a2)
{
__int64 v2; // r15
unsigned __int64 v3; // rbx
const char *v4; // r14
unsigned int v5; // ebp
__int64 v6; // rcx
unsigned int v7; // er8
__int64 v8; // rdx
__int64 v9; // rsi
__int64 v10; // rax
__int64 v12; // [rsp+8h] [rbp-40h]
unsigned int v13; // [rsp+8h] [rbp-40h]

if ( !a2 )
__assert_fail("NULL != member", "array_list.c", 0x46u, "list_del");
v12 = a1[1];
if ( !v12 )
return (unsigned int)-17;
v2 = *a1;
v3 = 0LL;
while ( 1 )
{
v4 = *(const char **)(v2 + 8 * v3);
v5 = v3;
if ( !strcmp(a2, v4) )
break;
if ( ++v3 == v12 )
return (unsigned int)-17;
}
v3 = (int)v3;
printf("delete [%d] %s\n", (unsigned int)v3, v4);
v6 = a1[1];
v7 = 0;
v8 = v6 - 1;
if ( (int)v3 >= (unsigned __int64)(v6 - 1) )
{
v10 = *a1;
}
else
{
do
{
v9 = v5++;
v13 = v7;
printf("replace %d with %d\n", v9, v5);
v10 = *a1;
v6 = a1[1];
v7 = v13;
*(_QWORD *)(*a1 + 8 * v3) = *(_QWORD *)(*a1 + 8 * v3 + 8);
v8 = v6 - 1;
++v3;
}
while ( v6 - 1 > v3 );
}
*(_QWORD *)(v10 + 8 * v6 - 8) = 0LL;
a1[1] = v8;
return v7;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 *a1, const char *a2)
{
__int64 v2; // r15
unsigned __int64 v3; // rbx
const char *v4; // r14
unsigned int v5; // ebp
__int64 v6; // rcx
unsigned int v7; // er8
__int64 v8; // rdx
__int64 v9; // rsi
__int64 v10; // rax
__int64 v12; // [rsp+8h] [rbp-40h]
unsigned int v13; // [rsp+8h] [rbp-40h]

if ( !a2 )
__assert_fail("NULL != member", "array_list.c", 0x46u, "list_del");
v12 = a1[1];
if ( !v12 )
return (unsigned int)-17;
v2 = *a1;
v3 = 0LL;
while ( 1 )
{
v4 = *(const char **)(v2 + 8 * v3);
v5 = v3;
if ( !strcmp(a2, v4) )
break;
if ( ++v3 == v12 )
return (unsigned int)-17;
}
v3 = (int)v3;
printf("delete [%d] %s\n", (unsigned int)v3, v4);
v6 = a1[1];
v7 = 0;
v8 = v6 - 1;
if ( (int)v3 >= (unsigned __int64)(v6 - 1) )
{
v10 = *a1;
}
else
{
do
{
v9 = v5++;
v13 = v7;
printf("replace %d with %d\n", v9, v5);
v10 = *a1;
v6 = a1[1];
v7 = v13;
*(_QWORD *)(*a1 + 8 * v3) = *(_QWORD *)(*a1 + 8 * v3 + 8);
v8 = v6 - 1;
++v3;
}
while ( v6 - 1 > v3 );
}
*(_QWORD *)(v10 + 8 * v6 - 8) = 0LL;
a1[1] = v8;
return v7;
}

original_code:
int list_del(list_t *list, type_t *member)
{
 ((void) sizeof ((((void *)0) != member) ? 1 : 0), __extension__ ({ if (((void *)0) != member) ; else __assert_fail ("NULL != member", "array_list.c", 70, __extension__ __PRETTY_FUNCTION__); }));
 for (int i = 0; i < list->len; i++) {
  type_t *p = list->data[i];
  if (!strcmp(member->name, p->name)) {
   printf("delete [%d] %s\n", i, p->name);
   for (int j = i; j < list->len - 1; j++) {
    printf("replace %d with %d\n", j, j+1);
    list->data[j] = list->data[j+1];
   }
   list->data[list->len - 1] = ((void *)0);
   list->len--;
   return 0;
  }
 }
 return -17;
}

canonical_original_code:
int list_del(struct list_t *list, struct type_t *member)
{
 ((void) sizeof ((((void *)0) != member) ? 1 : 0), __extension__ ({ if (((void *)0) != member) ; else __assert_fail ("NULL != member", "array_list.c", 70, __extension__ __PRETTY_FUNCTION__); }));
 for (int i = 0; i < list->len; i++) {
  struct type_t *p = list->data[i];
  if (!strcmp(member->name, p->name)) {
   printf("delete [%d] %s\n", i, p->name);
   for (int j = i; j < list->len - 1; j++) {
    printf("replace %d with %d\n", j, j+1);
    list->data[j] = list->data[j+1];
   }
   list->data[list->len - 1] = ((void *)0);
   list->len--;
   return 0;
  }
 }
 return -17;
}

variable_types: i int

variable_types:
p
struct type_t { char name[10]; } *

variable_types: j int


decompiled_code:
int __fastcall list_show(_QWORD *a1)
{
int result; // eax
unsigned __int64 v2; // rbx
__int64 v3; // rsi
const char *v4; // rdx

result = printf("len: %zd, cap: %zd\n", a1[1], a1[2]);
if ( a1[1] )
{
v2 = 0LL;
do
{
v3 = (unsigned int)v2;
v4 = *(const char **)(*a1 + 8 * v2++);
result = printf(" [%d]: %s\n", v3, v4);
}
while ( a1[1] > v2 );
}
return result;
}

canonical_decompiled_code:
int __fastcall func11(_QWORD *a1)
{
int result; // eax
unsigned __int64 v2; // rbx
__int64 v3; // rsi
const char *v4; // rdx

result = printf("len: %zd, cap: %zd\n", a1[1], a1[2]);
if ( a1[1] )
{
v2 = 0LL;
do
{
v3 = (unsigned int)v2;
v4 = *(const char **)(*a1 + 8 * v2++);
result = printf(" [%d]: %s\n", v3, v4);
}
while ( a1[1] > v2 );
}
return result;
}

original_code:
void list_show(list_t *list)
{
 printf("len: %zd, cap: %zd\n", list->len, list->cap);
 for (int i = 0; i < list->len; i++)
  printf("  [%d]: %s\n", i, list->data[i]->name);
}

canonical_original_code:
void list_show(struct list_t *list)
{
 printf("len: %zd, cap: %zd\n", list->len, list->cap);
 for (int i = 0; i < list->len; i++)
  printf("  [%d]: %s\n", i, list->data[i]->name);
}


decompiled_code:
void dump_stack()
{
int v0; // ebp
char **v1; // rax
char **v2; // r12
const char **v3; // rbx
__int64 v4; // rbp
const char *v5; // rsi
void *v6[131]; // [rsp+0h] [rbp-418h] BYREF

v0 = backtrace(v6, 128);
v1 = backtrace_symbols(v6, v0);
v2 = v1;
if ( v0 > 0 )
{
v3 = (const char **)v1;
v4 = (__int64)&v1[(unsigned int)(v0 - 1) + 1];
do
{
v5 = *v3++;
printf("== %s\n", v5);
}
while ( v3 != (const char **)v4 );
}
free(v2);
}

canonical_decompiled_code:
void func10()
{
int v0; // ebp
char **v1; // rax
char **v2; // r12
const char **v3; // rbx
__int64 v4; // rbp
const char *v5; // rsi
void *v6[131]; // [rsp+0h] [rbp-418h] BYREF

v0 = backtrace(v6, 128);
v1 = backtrace_symbols(v6, v0);
v2 = v1;
if ( v0 > 0 )
{
v3 = (const char **)v1;
v4 = (__int64)&v1[(unsigned int)(v0 - 1) + 1];
do
{
v5 = *v3++;
printf("== %s\n", v5);
}
while ( v3 != (const char **)v4 );
}
free(v2);
}

original_code:
void dump_stack()
{
 void* callstack[128];
 int i, frames = backtrace(callstack, 128);
 char** strs = backtrace_symbols(callstack, frames);
 for (i = 0; i < frames; ++i) {
  printf("== %s\n", strs[i]);
 }
 free(strs);
}

canonical_original_code:
void dump_stack()
{
 void *callstack[128];
 int i; int frames = backtrace(callstack, 128);
 char **strs = backtrace_symbols(callstack, frames);
 for (i = 0; i < frames; ++i) {
  printf("== %s\n", strs[i]);
 }
 free(strs);
}

variable_types:
callstack
void *[128]

variable_types: frames int

variable_types:
strs
char **


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
puts("main()");
func_b_isra_0("main()", argv);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
puts("main()");
func9("main()", argv);
return 0;
}

original_code:
int main()
{
 printf("main()\n");
 func_b();
    return 0;
}


decompiled_code:
void print_stack()
{
__int64 v0; // rbp
char **v1; // rax
char **v2; // r12
const char **v3; // rbx
__int64 v4; // rbp
const char *v5; // rsi
void *v6[103]; // [rsp+0h] [rbp-338h] BYREF

v0 = (unsigned int)backtrace(v6, 100);
printf("backtrace() returned %d addresses\n", v0);
v1 = backtrace_symbols(v6, v0);
if ( v1 )
{
v2 = v1;
if ( (int)v0 > 0 )
{
v3 = (const char **)v1;
v4 = (__int64)&v1[(unsigned int)(v0 - 1) + 1];
do
{
v5 = *v3++;
printf("== %s\n", v5);
}
while ( v3 != (const char **)v4 );
}
free(v2);
}
else
{
perror("backtrace_symbols");
}
}

canonical_decompiled_code:
void func7()
{
__int64 v0; // rbp
char **v1; // rax
char **v2; // r12
const char **v3; // rbx
__int64 v4; // rbp
const char *v5; // rsi
void *v6[103]; // [rsp+0h] [rbp-338h] BYREF

v0 = (unsigned int)backtrace(v6, 100);
printf("backtrace() returned %d addresses\n", v0);
v1 = backtrace_symbols(v6, v0);
if ( v1 )
{
v2 = v1;
if ( (int)v0 > 0 )
{
v3 = (const char **)v1;
v4 = (__int64)&v1[(unsigned int)(v0 - 1) + 1];
do
{
v5 = *v3++;
printf("== %s\n", v5);
}
while ( v3 != (const char **)v4 );
}
free(v2);
}
else
{
perror("backtrace_symbols");
}
}

original_code:
static void print_stack(void)
{
 int j, nptrs;
 void *buffer[100];
 char **strings;
 nptrs = backtrace(buffer, 100);
 printf("backtrace() returned %d addresses\n", nptrs);
 strings = backtrace_symbols(buffer, nptrs);
 if (strings == ((void *)0)) {
  perror("backtrace_symbols");
  return;
 }
 for (j = 0; j < nptrs; j++)
  printf("== %s\n", strings[j]);
 free(strings);
}

canonical_original_code:
static void print_stack()
{
 int j; int nptrs;
 void *buffer[100];
 char **strings;
 nptrs = backtrace(buffer, 100);
 printf("backtrace() returned %d addresses\n", nptrs);
 strings = backtrace_symbols(buffer, nptrs);
 if (strings == ((void *)0)) {
  perror("backtrace_symbols");
  return;
 }
 for (j = 0; j < nptrs; j++)
  printf("== %s\n", strings[j]);
 free(strings);
}

variable_types: nptrs int

variable_types:
buffer
void *[100]

variable_types:
strings
char **


decompiled_code:
__int64 __fastcall buf_create(_QWORD *a1, size_t a2)
{
void *v2; // rax

v2 = malloc(a2);
*a1 = v2;
if ( !v2 )
return 4294967284LL;
a1[1] = a2;
a1[2] = v2;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(_QWORD *a1, size_t a2)
{
void *v2; // rax

v2 = malloc(a2);
*a1 = v2;
if ( !v2 )
return 4294967284LL;
a1[1] = a2;
a1[2] = v2;
return 0LL;
}

original_code:
int buf_create(buffer_t *buf, size_t size)
{
 buf->start = (uint8_t *)malloc(sizeof(uint8_t) * size);
 if (buf->start == ((void *)0))
  return -12;
 buf->ptr = buf->start;
 buf->size = size;
 return 0;
}

canonical_original_code:
int buf_create(struct buffer_t *buf, unsigned long size)
{
 buf->start = (unsigned char *)malloc(sizeof(unsigned char) * size);
 if (buf->start == ((void *)0))
  return -12;
 buf->ptr = buf->start;
 buf->size = size;
 return 0;
}

variable_types: struct buffer_t { unsigned char *start; unsigned long size; unsigned char *ptr; } *


decompiled_code:
void __fastcall buf_destroy(__int64 a1)
{
free(*(void **)a1);
*(_QWORD *)(a1 + 16) = 0LL;
*(_QWORD *)a1 = 0LL;
*(_QWORD *)(a1 + 8) = 0LL;
}

original_code:
void buf_destroy(buffer_t *buf)
{
 free(buf->start);
 buf->start = buf->ptr = ((void *)0);
 buf->size = 0;
}

canonical_original_code:
void buf_destroy(struct buffer_t *buf)
{
 free(buf->start);
 buf->start = buf->ptr = ((void *)0);
 buf->size = 0;
}

user_defined_types: struct buffer_t { unsigned char *start; unsigned long size; unsigned char *ptr; }


decompiled_code:
bool __fastcall buf_is_full(_QWORD *a1)
{
return a1[2] == *a1 + a1[1];
}

canonical_decompiled_code:
bool __fastcall func9(_QWORD *a1)
{
return a1[2] == *a1 + a1[1];
}

original_code:
_Bool buf_is_full(buffer_t *buf)
{
 return (buf->ptr == (buf->start + buf->size)) ? 1: 0;
}

canonical_original_code:
_Bool buf_is_full(struct buffer_t *buf)
{
 return (buf->ptr == (buf->start + buf->size)) ? 1: 0;
}


decompiled_code:
bool __fastcall buf_is_empty(_QWORD *a1)
{
return a1[2] == *a1;
}

canonical_decompiled_code:
bool __fastcall func10(_QWORD *a1)
{
return a1[2] == *a1;
}

original_code:
_Bool buf_is_empty(buffer_t *buf)
{
 return (buf->ptr == buf->start) ? 1: 0;
}

canonical_original_code:
_Bool buf_is_empty(struct buffer_t *buf)
{
 return (buf->ptr == buf->start) ? 1: 0;
}


decompiled_code:
__int64 __fastcall buf_remain(_QWORD *a1)
{
return a1[1] - (a1[2] - *a1);
}

canonical_decompiled_code:
__int64 __fastcall func11(_QWORD *a1)
{
return a1[1] - (a1[2] - *a1);
}

original_code:
size_t buf_remain(buffer_t *buf)
{
 return buf->size - (buf->ptr - buf->start);
}

canonical_original_code:
unsigned long buf_remain(struct buffer_t *buf)
{
 return buf->size - (buf->ptr - buf->start);
}

return_type: unsigned long


decompiled_code:
void __fastcall buf_write(__int64 a1, char *a2, unsigned __int64 a3)
{
unsigned __int64 v4; // r12
unsigned __int64 v5; // rax
unsigned __int64 v6; // rbx

if ( a3 )
{
v4 = a3;
do
{
if ( buf_is_full((_QWORD *)a1) )
buf_flush((__int64 *)a1);
v5 = buf_remain((_QWORD *)a1);
if ( v5 > v4 )
v5 = v4;
v6 = v5;
memcpy(*(void **)(a1 + 16), a2, v5);
a2 += v6;
*(_QWORD *)(a1 + 16) += v6;
v4 -= v6;
}
while ( v4 );
}
}

canonical_decompiled_code:
void __fastcall func13(__int64 a1, char *a2, unsigned __int64 a3)
{
unsigned __int64 v4; // r12
unsigned __int64 v5; // rax
unsigned __int64 v6; // rbx

if ( a3 )
{
v4 = a3;
do
{
if ( func9((_QWORD *)a1) )
func12((__int64 *)a1);
v5 = func11((_QWORD *)a1);
if ( v5 > v4 )
v5 = v4;
v6 = v5;
memcpy(*(void **)(a1 + 16), a2, v5);
a2 += v6;
*(_QWORD *)(a1 + 16) += v6;
v4 -= v6;
}
while ( v4 );
}
}

original_code:
void buf_write(buffer_t *buf, uint8_t *data, size_t nbytes)
{
 while (nbytes > 0) {
  size_t k, rem;
  if (buf_is_full(buf))
   buf_flush(buf);
  rem = buf_remain(buf);
  k = nbytes > rem ? rem: nbytes;
  memcpy(buf->ptr, data, k);
  buf->ptr += k;
  data += k;
  nbytes -= k;
 }
}

canonical_original_code:
void buf_write(struct buffer_t *buf, unsigned char *data, unsigned long nbytes)
{
 while (nbytes > 0) {
  unsigned long k; unsigned long rem;
  if (buf_is_full(buf))
   buf_flush(buf);
  rem = buf_remain(buf);
  k = nbytes > rem ? rem: nbytes;
  memcpy(buf->ptr, data, k);
  buf->ptr += k;
  data += k;
  nbytes -= k;
 }
}

variable_types:
data
unsigned char *

variable_types:
nbytes
unsigned long

variable_types:
k
unsigned long

variable_types:
rem
unsigned long


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
return 0;
}

original_code:
int main()
{
    return 0;
}

canonical_original_code:
int main()
{
    return 0;
}


decompiled_code:
__int64 __fastcall init_lib(void ***a1)
{
void **v2; // rax
void **v3; // rbx
void *v4; // rax
void *v5; // rax
char *v7; // rax
void **v8; // rdi
char *v9; // rax

v2 = (void **)malloc(0x10uLL);
*a1 = v2;
if ( !v2 )
return 12LL;
v3 = v2;
v4 = dlopen("/tmp/libvendor.so", 2);
*v3 = v4;
if ( v4 )
{
printf(">> Loaded lib: %s\n", "/tmp/libvendor.so");
v5 = dlsym(*v3, "add");
v3[1] = v5;
if ( v5 )
{
printf(">> Loaded sym: %s\n", "add");
return 0LL;
}
v9 = dlerror();
printf("!! Cannot load %s(%s)\n", "add", v9);
}
else
{
v7 = dlerror();
printf("!! Cannot load %s (%s)\n", "/tmp/libvendor.so", v7);
}
v8 = *a1;
if ( !*a1 )
return 11LL;
if ( *v8 )
{
dlclose(*v8);
v8 = *a1;
}
free(v8);
return 11LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(void ***a1)
{
void **v2; // rax
void **v3; // rbx
void *v4; // rax
void *v5; // rax
char *v7; // rax
void **v8; // rdi
char *v9; // rax

v2 = (void **)malloc(0x10uLL);
*a1 = v2;
if ( !v2 )
return 12LL;
v3 = v2;
v4 = dlopen("/tmp/libvendor.so", 2);
*v3 = v4;
if ( v4 )
{
printf(">> Loaded lib: %s\n", "/tmp/libvendor.so");
v5 = dlsym(*v3, "add");
v3[1] = v5;
if ( v5 )
{
printf(">> Loaded sym: %s\n", "add");
return 0LL;
}
v9 = dlerror();
printf("!! Cannot load %s(%s)\n", "add", v9);
}
else
{
v7 = dlerror();
printf("!! Cannot load %s (%s)\n", "/tmp/libvendor.so", v7);
}
v8 = *a1;
if ( !*a1 )
return 11LL;
if ( *v8 )
{
dlclose(*v8);
v8 = *a1;
}
free(v8);
return 11LL;
}

original_code:
static int init_lib(struct VendorFuncs **ppFuncs)
{
 VendorFuncs *f; int ret = 0; f = *ppFuncs = malloc(sizeof(*f)); if (!f) return 12; do { if (!((f->lib) = dlopen("/tmp/libvendor.so", 0x00002))) { printf("!! Cannot load %s (%s)\n", "/tmp/libvendor.so", dlerror()); ret = 11; goto error; } printf(">> Loaded lib: %s\n", "/tmp/libvendor.so"); } while (0);;
 do { if (!((f->add) = (tAddFunc*)dlsym(f->lib, "add"))) { printf("!! Cannot load %s(%s)\n", "add", dlerror()); ret = 11; goto error; } printf(">> Loaded sym: %s\n", "add"); } while (0);
 return 0; error: if (!*ppFuncs) return ret; if (*ppFuncs && (*ppFuncs)->lib) dlclose((*ppFuncs)->lib); free(*ppFuncs);; return ret;;
}

canonical_original_code:
static int init_lib(struct VendorFuncs **ppFuncs)
{
 struct VendorFuncs *f; int ret = 0; f = *ppFuncs = malloc(sizeof(*f)); if (!f) return 12; do { if (!((f->lib) = dlopen("/tmp/libvendor.so", 0x00002))) { printf("!! Cannot load %s (%s)\n", "/tmp/libvendor.so", dlerror()); ret = 11; goto error; } printf(">> Loaded lib: %s\n", "/tmp/libvendor.so"); } while (0);;
 do { if (!((f->add) = (int (*)(int x, int y))dlsym(f->lib, "add"))) { printf("!! Cannot load %s(%s)\n", "add", dlerror()); ret = 11; goto error; } printf(">> Loaded sym: %s\n", "add"); } while (0);
 return 0; error: if (!*ppFuncs) return ret; if (*ppFuncs && (*ppFuncs)->lib) dlclose((*ppFuncs)->lib); free(*ppFuncs);; return ret;;
}

variable_types: struct VendorFuncs { void *lib; int (*add)(int x, int y); } **

variable_types: ret int