decompiled_code:
__int64 __fastcall encrypt_link(__int64 a1)
{
_BYTE *v1; // rbx
unsigned int v2; // eax
int v3; // eax
unsigned int v4; // ebp
unsigned int v5; // er12

v1 = malloc(0x18uLL);
str2ba(a1, v1);
v1[6] = 1;
v2 = hci_get_route(v1);
v3 = hci_open_dev(v2);
v4 = v3;
if ( v3 < 0 )
{
v5 = -1;
}
else if ( ioctl(v3, 0x800448D5uLL, v1) )
{
v5 = -1;
}
else
{
v5 = (int)hci_encrypt_link(v4, *((unsigned __int16 *)v1 + 4), 1LL, 1000LL) >> 31;
}
free(v1);
close(v4);
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
_BYTE *v1; // rbx
unsigned int v2; // eax
int v3; // eax
unsigned int v4; // ebp
unsigned int v5; // er12

v1 = malloc(0x18uLL);
str2ba(a1, v1);
v1[6] = 1;
v2 = hci_get_route(v1);
v3 = hci_open_dev(v2);
v4 = v3;
if ( v3 < 0 )
{
v5 = -1;
}
else if ( ioctl(v3, 0x800448D5uLL, v1) )
{
v5 = -1;
}
else
{
v5 = (int)hci_encrypt_link(v4, *((unsigned __int16 *)v1 + 4), 1LL, 1000LL) >> 31;
}
free(v1);
close(v4);
return v5;
}

original_code:
int encrypt_link(char* bdaddr_dest)
{
  int ret = 0;
  int err = 0, dd;
  struct hci_conn_info_req *cr = 0;
  cr = (struct hci_conn_info_req*) malloc(
      sizeof(struct hci_conn_info_req) +
      sizeof(struct hci_conn_info));
  str2ba(bdaddr_dest, &cr->bdaddr);
  cr->type = 0x01;
  dd = hci_open_dev( hci_get_route( &cr->bdaddr ) );
  if( dd < 0 ) {
    ret = -1;
  }
  else
  {
    err = ioctl(dd, (((2U) << (((0 +8)+8)+14)) | ((('H')) << (0 +8)) | (((213)) << 0) | ((((sizeof(int)))) << ((0 +8)+8))), (unsigned long) cr );
    if( !err )
    {
      if(hci_encrypt_link(dd, cr->conn_info->handle, 0x01, 1000) < 0)
      {
        ret = -1;
      }
    }
    else
    {
      ret = -1;
    }
  }
  free(cr);
  close(dd);
  return ret;
}

canonical_original_code:
int encrypt_link(char *bdaddr_dest)
{
  int ret = 0;
  int err = 0; int dd;
  struct hci_conn_info_req *cr = 0;
  cr = (struct hci_conn_info_req *) malloc(
      sizeof(struct hci_conn_info_req) +
      sizeof(struct hci_conn_info));
  str2ba(bdaddr_dest, &cr->bdaddr);
  cr->type = 0x01;
  dd = hci_open_dev( hci_get_route( &cr->bdaddr ) );
  if( dd < 0 ) {
    ret = -1;
  }
  else
  {
    err = ioctl(dd, (((2U) << (((0 +8)+8)+14)) | ((('H')) << (0 +8)) | (((213)) << 0) | ((((sizeof(int)))) << ((0 +8)+8))), (unsigned long) cr );
    if( !err )
    {
      if(hci_encrypt_link(dd, cr->conn_info->handle, 0x01, 1000) < 0)
      {
        ret = -1;
      }
    }
    else
    {
      ret = -1;
    }
  }
  free(cr);
  close(dd);
  return ret;
}

user_defined_types: struct hci_conn_info { unsigned short handle; struct bdaddr_t bdaddr; unsigned char type; unsigned char out; unsigned short state; unsigned int link_mode; }


decompiled_code:
__int64 __fastcall sco_listen(unsigned __int16 a1)
{
int v1; // eax
unsigned int v2; // ebx
int optval; // [rsp+4h] [rbp-24h] BYREF
struct sockaddr addr; // [rsp+8h] [rbp-20h] BYREF

*(_QWORD *)&addr.sa_family = 0LL;
v1 = socket(31, 5, 2);
if ( v1 < 0 )
{
perror("socket");
exit(-1);
}
v2 = v1;
addr.sa_family = 31;
addr.sa_data[0] = 0;
addr.sa_data[1] = 0;
addr.sa_data[2] = 0;
addr.sa_data[3] = 0;
addr.sa_data[4] = 0;
addr.sa_data[5] = 0;
if ( bind(v1, &addr, 8u) < 0 )
{
perror("bind");
exit(-1);
}
optval = 1;
if ( setsockopt(v2, 274, 7, &optval, 4u) < 0 )
{
perror("setsockopt BT_DEFER_SETUP");
exit(-1);
}
if ( listen(v2, 10) < 0 )
{
perror("listen");
exit(-1);
}
printf("listening on psm: 0x%04x\n", a1);
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int16 a1)
{
int v1; // eax
unsigned int v2; // ebx
int optval; // [rsp+4h] [rbp-24h] BYREF
struct sockaddr addr; // [rsp+8h] [rbp-20h] BYREF

*(_QWORD *)&addr.sa_family = 0LL;
v1 = socket(31, 5, 2);
if ( v1 < 0 )
{
perror("socket");
exit(-1);
}
v2 = v1;
addr.sa_family = 31;
addr.sa_data[0] = 0;
addr.sa_data[1] = 0;
addr.sa_data[2] = 0;
addr.sa_data[3] = 0;
addr.sa_data[4] = 0;
addr.sa_data[5] = 0;
if ( bind(v1, &addr, 8u) < 0 )
{
perror("bind");
exit(-1);
}
optval = 1;
if ( setsockopt(v2, 274, 7, &optval, 4u) < 0 )
{
perror("setsockopt BT_DEFER_SETUP");
exit(-1);
}
if ( listen(v2, 10) < 0 )
{
perror("listen");
exit(-1);
}
printf("listening on psm: 0x%04x\n", a1);
return v2;
}

original_code:
int sco_listen(unsigned short psm)
{
  struct sockaddr_sco loc_addr = { 0 };
  int s;
  if((s = socket(31, SOCK_SEQPACKET, 2)) < 0)
  {
    perror("socket");
    exit(-1);
  }
  loc_addr.sco_family = 31;
  loc_addr.sco_bdaddr = *(&(bdaddr_t) {{0, 0, 0, 0, 0, 0}});
  if(bind(s, (struct sockaddr *) &loc_addr, sizeof(loc_addr)) < 0)
  {
    perror("bind");
    exit(-1);
  }
  int defer_setup = 1;
  if(setsockopt(s, 274, 7, &defer_setup, sizeof(defer_setup)) < 0)
  {
    perror("setsockopt BT_DEFER_SETUP");
    exit(-1);
  }
  if(listen(s, 10) < 0)
  {
    perror("listen");
    exit(-1);
  }
  printf("listening on psm: 0x%04x\n", psm);
  return s;
}

canonical_original_code:
int sco_listen(unsigned short psm)
{
  struct sockaddr_sco loc_addr = { 0 };
  int s;
  if((s = socket(31, SOCK_SEQPACKET, 2)) < 0)
  {
    perror("socket");
    exit(-1);
  }
  loc_addr.sco_family = 31;
  loc_addr.sco_bdaddr = *(&(struct bdaddr_t) {{0, 0, 0, 0, 0, 0}});
  if(bind(s, (struct sockaddr *) &loc_addr, sizeof(loc_addr)) < 0)
  {
    perror("bind");
    exit(-1);
  }
  int defer_setup = 1;
  if(setsockopt(s, 274, 7, &defer_setup, sizeof(defer_setup)) < 0)
  {
    perror("setsockopt BT_DEFER_SETUP");
    exit(-1);
  }
  if(listen(s, 10) < 0)
  {
    perror("listen");
    exit(-1);
  }
  printf("listening on psm: 0x%04x\n", psm);
  return s;
}

variable_types:
psm
unsigned short

variable_types: struct sockaddr_sco { unsigned short sco_family; struct bdaddr_t { unsigned char b[6]; } sco_bdaddr; }

variable_types: s int

return_type: int

user_defined_types: struct sockaddr_sco { unsigned short sco_family; struct bdaddr_t sco_bdaddr; }

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


decompiled_code:
__int64 __fastcall sco_accept(int a1)
{
int v1; // eax
unsigned int v2; // ebx
char optval; // [rsp+Bh] [rbp-2Dh] BYREF
socklen_t addr_len; // [rsp+Ch] [rbp-2Ch] BYREF
__int64 v6[2]; // [rsp+10h] [rbp-28h] BYREF
__int16 v7; // [rsp+20h] [rbp-18h]
char v8; // [rsp+22h] [rbp-16h]
struct sockaddr addr; // [rsp+28h] [rbp-10h] BYREF

*(_QWORD *)&addr.sa_family = 0LL;
v6[0] = 0LL;
v6[1] = 0LL;
v7 = 0;
v8 = 0;
addr_len = 8;
v1 = accept(a1, &addr, &addr_len);
if ( v1 < 0 )
{
perror("accept");
exit(-1);
}
v2 = v1;
ba2str(addr.sa_data, v6);
printf("accepted connection from %s (SCO)\n", (const char *)v6);
optval = 0;
if ( setsockopt(v2, 274, 9, &optval, 1u) < 0 )
perror("setsockopt BT_POWER");
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
int v1; // eax
unsigned int v2; // ebx
char optval; // [rsp+Bh] [rbp-2Dh] BYREF
socklen_t addr_len; // [rsp+Ch] [rbp-2Ch] BYREF
__int64 v6[2]; // [rsp+10h] [rbp-28h] BYREF
__int16 v7; // [rsp+20h] [rbp-18h]
char v8; // [rsp+22h] [rbp-16h]
struct sockaddr addr; // [rsp+28h] [rbp-10h] BYREF

*(_QWORD *)&addr.sa_family = 0LL;
v6[0] = 0LL;
v6[1] = 0LL;
v7 = 0;
v8 = 0;
addr_len = 8;
v1 = accept(a1, &addr, &addr_len);
if ( v1 < 0 )
{
perror("accept");
exit(-1);
}
v2 = v1;
ba2str(addr.sa_data, v6);
printf("accepted connection from %s (SCO)\n", (const char *)v6);
optval = 0;
if ( setsockopt(v2, 274, 9, &optval, 1u) < 0 )
perror("setsockopt BT_POWER");
return v2;
}

original_code:
int sco_accept(int s)
{
  struct sockaddr_sco rem_addr = { 0 };
  int client;
  char buf[sizeof("00:00:00:00:00:00")+1] = { 0 };
  socklen_t opt = sizeof(rem_addr);
  if((client = accept(s, (struct sockaddr *) &rem_addr, &opt)) < 0)
  {
    perror("accept");
    exit(-1);
  }
  ba2str(&rem_addr.sco_bdaddr, buf);
  printf("accepted connection from %s (SCO)\n", buf);
  struct bt_power pwr = {.force_active = 0};
  if (setsockopt(client, 274, 9, &pwr, sizeof(pwr)) < 0)
  {
    perror("setsockopt BT_POWER");
  }
  return client;
}

canonical_original_code:
int sco_accept(int s)
{
  struct sockaddr_sco rem_addr = { 0 };
  int client;
  char buf[-1] = { 0 };
  unsigned int opt = sizeof(rem_addr);
  if((client = accept(s, (struct sockaddr *) &rem_addr, &opt)) < 0)
  {
    perror("accept");
    exit(-1);
  }
  ba2str(&rem_addr.sco_bdaddr, buf);
  printf("accepted connection from %s (SCO)\n", buf);
  struct bt_power pwr = {.force_active = 0};
  if (setsockopt(client, 274, 9, &pwr, sizeof(pwr)) < 0)
  {
    perror("setsockopt BT_POWER");
  }
  return client;
}

variable_types: client int

variable_types:
buf
char [-1]

variable_types:
opt
unsigned int

variable_types: struct bt_power { unsigned char force_active; }


decompiled_code:
__int64 __fastcall sco_connect(__int64 a1, const char *a2)
{
int v2; // eax
unsigned int v3; // ebx
char optval; // [rsp+7h] [rbp-21h] BYREF
struct sockaddr addr; // [rsp+8h] [rbp-20h] BYREF

v2 = socket(31, 5, 2);
if ( v2 < 0 )
{
perror("socket");
exit(-1);
}
v3 = v2;
if ( a1 )
{
*(_DWORD *)addr.sa_data = 0;
*(_WORD *)&addr.sa_data[4] = 0;
addr.sa_family = 31;
str2ba(a1, addr.sa_data);
if ( bind(v3, &addr, 8u) < 0 )
{
perror("bind\n");
exit(-1);
}
}
*(_DWORD *)addr.sa_data = 0;
*(_WORD *)&addr.sa_data[4] = 0;
addr.sa_family = 31;
str2ba(a2, addr.sa_data);
if ( connect(v3, &addr, 8u) < 0 )
{
perror("connect");
exit(-1);
}
printf("connected to %s (SCO)\n", a2);
optval = 0;
if ( setsockopt(v3, 274, 9, &optval, 1u) < 0 )
perror("setsockopt BT_POWER");
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, const char *a2)
{
int v2; // eax
unsigned int v3; // ebx
char optval; // [rsp+7h] [rbp-21h] BYREF
struct sockaddr addr; // [rsp+8h] [rbp-20h] BYREF

v2 = socket(31, 5, 2);
if ( v2 < 0 )
{
perror("socket");
exit(-1);
}
v3 = v2;
if ( a1 )
{
*(_DWORD *)addr.sa_data = 0;
*(_WORD *)&addr.sa_data[4] = 0;
addr.sa_family = 31;
str2ba(a1, addr.sa_data);
if ( bind(v3, &addr, 8u) < 0 )
{
perror("bind\n");
exit(-1);
}
}
*(_DWORD *)addr.sa_data = 0;
*(_WORD *)&addr.sa_data[4] = 0;
addr.sa_family = 31;
str2ba(a2, addr.sa_data);
if ( connect(v3, &addr, 8u) < 0 )
{
perror("connect");
exit(-1);
}
printf("connected to %s (SCO)\n", a2);
optval = 0;
if ( setsockopt(v3, 274, 9, &optval, 1u) < 0 )
perror("setsockopt BT_POWER");
return v3;
}

original_code:
int sco_connect(const char *bdaddr_src, const char *bdaddr_dest)
{
  int fd;
  struct sockaddr_sco addr;
  if ((fd = socket(31, SOCK_SEQPACKET, 2)) < 0)
  {
    perror("socket");
    exit(-1);
  }
  if(bdaddr_src)
  {
    memset(&addr, 0, sizeof(addr));
    addr.sco_family = 31;
    str2ba(bdaddr_src, &addr.sco_bdaddr);
    if(bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
    {
      perror("bind\n");
      exit(-1);
    }
  }
  memset(&addr, 0, sizeof(addr));
  addr.sco_family = 31;
  str2ba(bdaddr_dest, &addr.sco_bdaddr);
  if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0)
  {
    perror("connect");
    exit(-1);
  }
  printf("connected to %s (SCO)\n", bdaddr_dest);
  struct bt_power pwr = {.force_active = 0};
  if (setsockopt(fd, 274, 9, &pwr, sizeof(pwr)) < 0)
  {
    perror("setsockopt BT_POWER");
  }
  return fd;
}

canonical_original_code:
int sco_connect(char *bdaddr_src, char *bdaddr_dest)
{
  int fd;
  struct sockaddr_sco addr;
  if ((fd = socket(31, SOCK_SEQPACKET, 2)) < 0)
  {
    perror("socket");
    exit(-1);
  }
  if(bdaddr_src)
  {
    memset(&addr, 0, sizeof(addr));
    addr.sco_family = 31;
    str2ba(bdaddr_src, &addr.sco_bdaddr);
    if(bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
    {
      perror("bind\n");
      exit(-1);
    }
  }
  memset(&addr, 0, sizeof(addr));
  addr.sco_family = 31;
  str2ba(bdaddr_dest, &addr.sco_bdaddr);
  if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0)
  {
    perror("connect");
    exit(-1);
  }
  printf("connected to %s (SCO)\n", bdaddr_dest);
  struct bt_power pwr = {.force_active = 0};
  if (setsockopt(fd, 274, 9, &pwr, sizeof(pwr)) < 0)
  {
    perror("setsockopt BT_POWER");
  }
  return fd;
}

variable_types: fd int

user_defined_types: struct bt_power { unsigned char force_active; }


setnonblock

repo: matrixj/scnet

decompiled_code:
int __fastcall setnonblock(int fd)
{
int v1; // eax
unsigned int v2; // ebp
__int64 v3; // rdx

v1 = fcntl(fd, 3);
v2 = (unsigned int)v1 >> 31;
if ( v1 < 0 )
perror("fcntl_get");
v3 = v2;
BYTE1(v3) = BYTE1(v2) | 8;
if ( fcntl(fd, 4, v3) < 0 )
perror("fcntl_set");
return printf("set fd:%d to nonblock:%s,%d\n", (unsigned int)fd, "sc_fd.h", 111LL);
}

canonical_decompiled_code:
int __fastcall func7(int fd)
{
int v1; // eax
unsigned int v2; // ebp
__int64 v3; // rdx

v1 = fcntl(fd, 3);
v2 = (unsigned int)v1 >> 31;
if ( v1 < 0 )
perror("fcntl_get");
v3 = v2;
BYTE1(v3) = BYTE1(v2) | 8;
if ( fcntl(fd, 4, v3) < 0 )
perror("fcntl_set");
return printf("set fd:%d to nonblock:%s,%d\n", (unsigned int)fd, "sc_fd.h", 111LL);
}

original_code:
static inline void setnonblock(int so_fd)
{
    int flag ;
    int fd= so_fd;
    if(flag = fcntl(fd, 3) < 0)
        perror("fcntl_get");
    flag |= 04000;
    if(fcntl(fd, 4, flag) < 0)
        perror("fcntl_set");
    printf("set fd:%d to nonblock:%s,%d\n", fd, "sc_fd.h", 111);
}

canonical_original_code:
static inline void setnonblock(int so_fd)
{
    int flag;
    int fd = so_fd;
    if(flag = fcntl(fd, 3) < 0)
        perror("fcntl_get");
    flag |= 04000;
    if(fcntl(fd, 4, flag) < 0)
        perror("fcntl_set");
    printf("set fd:%d to nonblock:%s,%d\n", fd, "sc_fd.h", 111);
}

variable_types: flag int

return_type: void


decompiled_code:
__int64 __fastcall sc_setsockopt_nodelay(int a1)
{
unsigned int v1; // ebx
int optval[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v1 = a1;
optval[0] = 1;
if ( setsockopt(a1, 6, 1, optval, 4u) == -1 )
{
fwrite("setsockopt error\n", 1uLL, 0x11uLL, stderr);
v1 = -1;
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func11(int a1)
{
unsigned int v1; // ebx
int optval[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v1 = a1;
optval[0] = 1;
if ( setsockopt(a1, 6, 1, optval, 4u) == -1 )
{
fwrite("setsockopt error\n", 1uLL, 0x11uLL, stderr);
v1 = -1;
}
return v1;
}

original_code:
static inline int sc_setsockopt_nodelay(int fd)
{
    int yes = 1;
    if (setsockopt(fd, IPPROTO_TCP, 1,
                   &yes, sizeof(yes)) == -1){
        fprintf(stderr, "setsockopt error\n");
        return -1;
    }
    return fd;
}

variable_types: yes int


decompiled_code:
__int64 __fastcall sc_setsockopt_nopush(int a1)
{
unsigned int v1; // ebx
int optval[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v1 = a1;
optval[0] = 1;
if ( setsockopt(a1, 6, 3, optval, 4u) == -1 )
{
fwrite("setsockopt_tcp_CORK\n", 1uLL, 0x14uLL, stderr);
v1 = -1;
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func12(int a1)
{
unsigned int v1; // ebx
int optval[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v1 = a1;
optval[0] = 1;
if ( setsockopt(a1, 6, 3, optval, 4u) == -1 )
{
fwrite("setsockopt_tcp_CORK\n", 1uLL, 0x14uLL, stderr);
v1 = -1;
}
return v1;
}

original_code:
static inline int sc_setsockopt_nopush(int fd)
{
    int on = 1;
    if(setsockopt(fd, 6, 3,
                  &on, sizeof(on)) == -1){
        fprintf(stderr, "setsockopt_tcp_CORK\n");
        return -1;
                  }
    return fd;
}

variable_types: on int


canonical_decompiled_code:
__int64 __fastcall func6(int a1)
{
unsigned int v1; // ebx
int optval[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v1 = a1;
optval[0] = 1;
if ( setsockopt(a1, 6, 1, optval, 4u) == -1 )
{
fwrite("setsockopt error\n", 1uLL, 0x11uLL, stderr);
v1 = -1;
}
return v1;
}

canonical_original_code:
static inline int sc_setsockopt_nodelay(int fd)
{
    int yes = 1;
    if (setsockopt(fd, IPPROTO_TCP, 1,
                   &yes, sizeof(yes)) == -1){
        fprintf(stderr, "setsockopt error\n");
        return -1;
    }
    return fd;
}


canonical_decompiled_code:
__int64 __fastcall func7(int a1)
{
unsigned int v1; // ebx
int optval[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v1 = a1;
optval[0] = 1;
if ( setsockopt(a1, 6, 3, optval, 4u) == -1 )
{
fwrite("setsockopt_tcp_CORK\n", 1uLL, 0x14uLL, stderr);
v1 = -1;
}
return v1;
}

canonical_original_code:
static inline int sc_setsockopt_nopush(int fd)
{
    int on = 1;
    if(setsockopt(fd, 6, 3,
                  &on, sizeof(on)) == -1){
        fprintf(stderr, "setsockopt_tcp_CORK\n");
        return -1;
                  }
    return fd;
}


main

canonical_name: main

repo: mattddowney/isbn

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char **v3; // rbx
const char **v4; // r12
int v5; // edx
int v6; // eax
__int64 v7; // rsi
__int64 v8; // rcx
__int64 v9; // r8
__int64 v10; // r9
__int64 v11; // rax
const char *v12; // rbp
int v13; // eax
char s; // [rsp+4h] [rbp-44h] BYREF
char v16[3]; // [rsp+5h] [rbp-43h] BYREF
char v17; // [rsp+8h] [rbp-40h] BYREF
char v18; // [rsp+9h] [rbp-3Fh]
char v19; // [rsp+Ah] [rbp-3Eh]
char v20; // [rsp+Bh] [rbp-3Dh]
char v21; // [rsp+Ch] [rbp-3Ch]
char v22; // [rsp+Dh] [rbp-3Bh]
char v23; // [rsp+Eh] [rbp-3Ah]
char v24; // [rsp+Fh] [rbp-39h]
char v25; // [rsp+10h] [rbp-38h]
char dest[3]; // [rsp+13h] [rbp-35h] BYREF
char v27; // [rsp+16h] [rbp-32h]
char v28; // [rsp+17h] [rbp-31h]
char v29; // [rsp+18h] [rbp-30h]
char v30; // [rsp+19h] [rbp-2Fh]
char v31; // [rsp+1Ah] [rbp-2Eh]
char v32; // [rsp+1Bh] [rbp-2Dh]
char v33; // [rsp+1Ch] [rbp-2Ch]
char v34; // [rsp+1Dh] [rbp-2Bh]
char v35; // [rsp+1Eh] [rbp-2Ah]

if ( argc <= 1 )
{
puts("Convert between ISBN-10 and ISBN-13 (EAN) and vice-versa.");
puts("Seperate ISBNs to convert by a space.");
puts("Example Usage: isbn 9781418837235 1418837237");
}
else
{
v3 = argv + 1;
v4 = &argv[(unsigned int)(argc - 2) + 2];
do
{
dest[0] = 0;
v17 = 0;
v16[0] = 0;
s = 0;
v12 = *v3;
v13 = strlen(*v3);
if ( v13 == 13 )
{
strcpy(dest, v12);
strncat(v16, dest, 3uLL);
if ( !strcmp(v16, "978") )
{
v5 = 3 * v34
- 144
+ 5 * v32
- 240
+ 9 * v28
- 432
+ 2 * (5 * v27 - 240)
+ 8 * v29
- 384
+ 8 * (v30 - 48)
- (v30
- 48)
+ 2 * (3 * v31 - 144)
+ 4 * v33
- 192
+ 2 * v35
- 96;
v6 = 11 * (v5 / 11) - v5;
if ( v6 == -1 )
sprintf(&s, "%s", "X");
else
sprintf(&s, "%d", (unsigned int)(v6 + 11));
printf(
"%c%c%c%c%c%c%c%c%c%c\n",
(unsigned int)v27,
(unsigned int)v28,
(unsigned int)v29,
(unsigned int)v30,
(unsigned int)v31,
(unsigned int)v32,
(unsigned int)v33,
(unsigned int)v34,
(unsigned int)v35,
(unsigned int)s);
}
else
{
puts("EAN must begin with 978.");
}
}
else if ( v13 == 10 )
{
strcpy(&v17, v12);
v7 = (unsigned int)v17;
v8 = (unsigned int)v19;
v9 = (unsigned int)v20;
v10 = (unsigned int)v21;
v11 = (unsigned int)((3 * v25
- 144
+ v24
+ 3 * v23
- 144
+ v22
+ 3 * (int)v10
- 144
+ (int)v9
+ 3 * (int)v8
- 144
+ v18
+ 3 * (int)v7
- 106
- 48
- 48
- 48
- 48)
% 10);
if ( (_DWORD)v11 )
v11 = (unsigned int)(10 - v11);
printf(
"978%c%c%c%c%c%c%c%c%c%d\n",
v7,
(unsigned int)v18,
v8,
v9,
v10,
(unsigned int)v22,
(unsigned int)v23,
(unsigned int)v24,
(unsigned int)v25,
v11);
}
else
{
puts("Invalid input.");
}
++v3;
}
while ( v3 != v4 );
}
return 0;
}

original_code:
int main(int argc, char *argv[])
{
 if(argc > 1)
 {
  int i, length, checkdigit;
  char EAN[13], ISBN[11], prefix[3], checkch[0];
  for(i = 1; i < argc; i++)
  {
   EAN[0] = '\0';
   ISBN[0] = '\0';
   prefix[0] = '\0';
   checkch[0] = '\0';
   length = strlen(argv[i]);
   if (length == 13)
   {
    sprintf(EAN, "%s", argv[i]);
    strncat(prefix, EAN, 3);
    if (!strcmp(prefix, "978"))
    {
     checkdigit = 11-((((EAN[3]-'0') * 10) + ((EAN[4]-'0') * 9) + ((EAN[5]-'0') * 8) + ((EAN[6]-'0') * 7) + ((EAN[7]-'0') * 6) + ((EAN[8]-'0') * 5) + ((EAN[9]-'0') * 4) + ((EAN[10]-'0') * 3) + ((EAN[11]-'0') * 2)) % 11);
     if (checkdigit == 10)
      sprintf(checkch, "%s", "X");
     else
      sprintf(checkch, "%d", checkdigit);
     printf("%c%c%c%c%c%c%c%c%c%c\n", EAN[3], EAN[4], EAN[5], EAN[6], EAN[7], EAN[8], EAN[9], EAN[10], EAN[11], checkch[0]);
    }
    else
     printf("EAN must begin with 978.\n");
   }
   else if (length == 10)
   {
    sprintf(ISBN, "%s", argv[i]);
    checkdigit = (38 + ((ISBN[0]-'0') * 3) + (ISBN[1]-'0') + ((ISBN[2]-'0') * 3) + (ISBN[3]-'0') + ((ISBN[4]-'0') * 3) + (ISBN[5]-'0') + ((ISBN[6]-'0') * 3) + (ISBN[7]-'0') + ((ISBN[8]-'0') * 3)) % 10;
    if (checkdigit != 0)
     checkdigit = 10 - checkdigit;
    printf("978%c%c%c%c%c%c%c%c%c%d\n", ISBN[0], ISBN[1], ISBN[2], ISBN[3], ISBN[4], ISBN[5], ISBN[6], ISBN[7], ISBN[8], checkdigit);
   }
   else
    printf("Invalid input.\n");
  }
 }
 else
 {
  printf("Convert between ISBN-10 and ISBN-13 (EAN) and vice-versa.\n");
  printf("Seperate ISBNs to convert by a space.\n");
  printf("Example Usage: isbn 9781418837235 1418837237\n");
 }
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
 if(argc > 1)
 {
  int i; int length; int checkdigit;
  char EAN[13]; char ISBN[11]; char prefix[3]; char checkch[];
  for(i = 1; i < argc; i++)
  {
   EAN[0] = '\0';
   ISBN[0] = '\0';
   prefix[0] = '\0';
   checkch[0] = '\0';
   length = strlen(argv[i]);
   if (length == 13)
   {
    sprintf(EAN, "%s", argv[i]);
    strncat(prefix, EAN, 3);
    if (!strcmp(prefix, "978"))
    {
     checkdigit = 11-((((EAN[3]-'0') * 10) + ((EAN[4]-'0') * 9) + ((EAN[5]-'0') * 8) + ((EAN[6]-'0') * 7) + ((EAN[7]-'0') * 6) + ((EAN[8]-'0') * 5) + ((EAN[9]-'0') * 4) + ((EAN[10]-'0') * 3) + ((EAN[11]-'0') * 2)) % 11);
     if (checkdigit == 10)
      sprintf(checkch, "%s", "X");
     else
      sprintf(checkch, "%d", checkdigit);
     printf("%c%c%c%c%c%c%c%c%c%c\n", EAN[3], EAN[4], EAN[5], EAN[6], EAN[7], EAN[8], EAN[9], EAN[10], EAN[11], checkch[0]);
    }
    else
     printf("EAN must begin with 978.\n");
   }
   else if (length == 10)
   {
    sprintf(ISBN, "%s", argv[i]);
    checkdigit = (38 + ((ISBN[0]-'0') * 3) + (ISBN[1]-'0') + ((ISBN[2]-'0') * 3) + (ISBN[3]-'0') + ((ISBN[4]-'0') * 3) + (ISBN[5]-'0') + ((ISBN[6]-'0') * 3) + (ISBN[7]-'0') + ((ISBN[8]-'0') * 3)) % 10;
    if (checkdigit != 0)
     checkdigit = 10 - checkdigit;
    printf("978%c%c%c%c%c%c%c%c%c%d\n", ISBN[0], ISBN[1], ISBN[2], ISBN[3], ISBN[4], ISBN[5], ISBN[6], ISBN[7], ISBN[8], checkdigit);
   }
   else
    printf("Invalid input.\n");
  }
 }
 else
 {
  printf("Convert between ISBN-10 and ISBN-13 (EAN) and vice-versa.\n");
  printf("Seperate ISBNs to convert by a space.\n");
  printf("Example Usage: isbn 9781418837235 1418837237\n");
 }
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: i int

variable_types: length int

variable_types: checkdigit int

variable_types:
EAN
char [13]

variable_types:
ISBN
char [11]

variable_types:
prefix
char [3]

variable_types:
checkch
char []


HashFunction

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

if ( (unsigned __int8)(a1 - 48) <= 9u )
return (unsigned int)(a1 - 48);
if ( (unsigned __int8)(a1 - 97) <= 0x19u )
result = (unsigned int)(a1 - 87);
return result;
}

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

if ( (unsigned __int8)(a1 - 48) <= 9u )
return (unsigned int)(a1 - 48);
if ( (unsigned __int8)(a1 - 97) <= 0x19u )
result = (unsigned int)(a1 - 87);
return result;
}

original_code:
int HashFunction(char letter){
    int tablePosition;
    if(letter >= 48 && letter <= 57){
        tablePosition = letter - 48;
    }
    if(letter >= 97 && letter <= 122){
        tablePosition = letter - 87;
    }
    return tablePosition;
}

variable_types: letter char

variable_types: tablePosition int


decompiled_code:
int __fastcall print_so(__int64 a1)
{
__int64 v1; // rbx
int result; // eax

if ( !a1 )
return puts("\nNone of the listed terms were contained in any file.");
v1 = a1;
do
{
if ( a1 == v1 )
puts("\nFiles containing one or more of the searched terms:");
result = puts(*(const char **)v1);
v1 = *(_QWORD *)(v1 + 16);
}
while ( v1 );
return result;
}

canonical_decompiled_code:
int __fastcall func13(__int64 a1)
{
__int64 v1; // rbx
int result; // eax

if ( !a1 )
return puts("\nNone of the listed terms were contained in any file.");
v1 = a1;
do
{
if ( a1 == v1 )
puts("\nFiles containing one or more of the searched terms:");
result = puts(*(const char **)v1);
v1 = *(_QWORD *)(v1 + 16);
}
while ( v1 );
return result;
}

original_code:
void print_so(file_node *front){
    file_node *curr;
    for(curr = front; curr != ((void *)0); curr = curr->nextFN){
        if(curr == front){
            printf("\nFiles containing one or more of the searched terms:\n");
        }
        printf("%s\n", curr->file);
    }
    if(front == ((void *)0)){
        printf("\nNone of the listed terms were contained in any file.\n");
    }
}

canonical_original_code:
void print_so(struct file_node *front){
    struct file_node *curr;
    for(curr = front; curr != ((void *)0); curr = curr->nextFN){
        if(curr == front){
            printf("\nFiles containing one or more of the searched terms:\n");
        }
        printf("%s\n", curr->file);
    }
    if(front == ((void *)0)){
        printf("\nNone of the listed terms were contained in any file.\n");
    }
}

variable_types: struct file_node { char *file; int freq; struct file_node *nextFN; } *


repo: matteorr/MultiThreadChat

decompiled_code:
void interrupt_handler()
{
endloop = 1;
close(sd);
puts("Interrupt recieved: shutting down server!");
}

canonical_decompiled_code:
void func7()
{
endloop = 1;
close(sd);
puts("Interrupt recieved: shutting down server!");
}

original_code:
void interrupt_handler(int sig){
 endloop = 1;
 close(sd);
 printf("Interrupt recieved: shutting down server!\n");
 return;
}

variable_types: sig int


chat

decompiled_code:
int __fastcall chat(int fd)
{
char *v3; // r12
char v4; // di
__int64 v5; // rbp
int v6; // eax
char *v7; // r12
char v8; // di
__int64 v9; // rbp
int v10; // eax
int v11; // eax
const char *v12; // rbp
int j; // er12
int v14; // eax
__int64 i; // rax
int v16; // eax
__int64 v17; // rbp
int v18; // edi
char v19[1008]; // [rsp+0h] [rbp-FD8h] BYREF
char s[1008]; // [rsp+3F0h] [rbp-BE8h] BYREF
char src[1008]; // [rsp+7E0h] [rbp-7F8h] BYREF
char s2[1032]; // [rsp+BD0h] [rbp-408h] BYREF

puts("CHAAAAAAAAAAAAT");
while ( 1 )
{
for ( i = 0LL; i != 1000; ++i )
{
s[i] = 0;
v19[i] = 0;
}
read(fd, s, 0x3E8uLL);
if ( !strcmp(s, "QUIT") )
break;
if ( s[0] == 60 )
{
v3 = strchr(s, 60);
v4 = v3[1];
if ( v4 == 62 )
{
v6 = 0;
}
else
{
v5 = 1LL;
do
{
src[v5 + 1007] = v4;
putchar(v4);
v6 = v5++;
v4 = v3[v5];
}
while ( v4 != 62 );
}
s2[v6] = 0;
v7 = strchr(s, 62);
v8 = v7[1];
if ( v8 )
{
v9 = 1LL;
do
{
s[v9 + 1007] = v8;
putchar(v8);
v10 = v9++;
v8 = v7[v9];
}
while ( v8 );
}
else
{
v10 = 0;
}
src[v10] = 0;
printf("\nMessage [%s] is for [%s]\n\n", src, s2);
v11 = 0;
if ( fd != ::fd[0] )
v11 = (fd != dword_404304) + 1;
sprintf(v19, "<%s> wrote: [%s]", &onlinecontacts[260 * v11], src);
v12 = onlinecontacts;
for ( j = 0; ; ++j )
{
v14 = strcmp(v12, s2);
v12 += 260;
if ( !v14 )
break;
}
write(::fd[65 * j], v19, 0x3E8uLL);
}
else
{
v16 = 0;
if ( fd != ::fd[0] )
v16 = (dword_404304 != fd) + 1;
sprintf(src, "<%s> wrote: [%s]", &onlinecontacts[260 * v16], s);
strcpy(v19, src);
if ( contacts > 0 )
{
v17 = 0LL;
do
{
v18 = ::fd[65 * v17];
if ( v18 != fd )
write(v18, v19, 0x3E8uLL);
++v17;
}
while ( contacts > (int)v17 );
}
}
}
strcpy(v19, "QUIT");
write(fd, v19, 0x3E8uLL);
return puts("\nchat has finished");
}

canonical_decompiled_code:
int __fastcall func9(int fd)
{
char *v3; // r12
char v4; // di
__int64 v5; // rbp
int v6; // eax
char *v7; // r12
char v8; // di
__int64 v9; // rbp
int v10; // eax
int v11; // eax
const char *v12; // rbp
int j; // er12
int v14; // eax
__int64 i; // rax
int v16; // eax
__int64 v17; // rbp
int v18; // edi
char v19[1008]; // [rsp+0h] [rbp-FD8h] BYREF
char s[1008]; // [rsp+3F0h] [rbp-BE8h] BYREF
char src[1008]; // [rsp+7E0h] [rbp-7F8h] BYREF
char s2[1032]; // [rsp+BD0h] [rbp-408h] BYREF

puts("CHAAAAAAAAAAAAT");
while ( 1 )
{
for ( i = 0LL; i != 1000; ++i )
{
s[i] = 0;
v19[i] = 0;
}
read(fd, s, 0x3E8uLL);
if ( !strcmp(s, "QUIT") )
break;
if ( s[0] == 60 )
{
v3 = strchr(s, 60);
v4 = v3[1];
if ( v4 == 62 )
{
v6 = 0;
}
else
{
v5 = 1LL;
do
{
src[v5 + 1007] = v4;
putchar(v4);
v6 = v5++;
v4 = v3[v5];
}
while ( v4 != 62 );
}
s2[v6] = 0;
v7 = strchr(s, 62);
v8 = v7[1];
if ( v8 )
{
v9 = 1LL;
do
{
s[v9 + 1007] = v8;
putchar(v8);
v10 = v9++;
v8 = v7[v9];
}
while ( v8 );
}
else
{
v10 = 0;
}
src[v10] = 0;
printf("\nMessage [%s] is for [%s]\n\n", src, s2);
v11 = 0;
if ( fd != ::fd[0] )
v11 = (fd != dword_404304) + 1;
sprintf(v19, "<%s> wrote: [%s]", &onlinecontacts[260 * v11], src);
v12 = onlinecontacts;
for ( j = 0; ; ++j )
{
v14 = strcmp(v12, s2);
v12 += 260;
if ( !v14 )
break;
}
write(::fd[65 * j], v19, 0x3E8uLL);
}
else
{
v16 = 0;
if ( fd != ::fd[0] )
v16 = (dword_404304 != fd) + 1;
sprintf(src, "<%s> wrote: [%s]", &onlinecontacts[260 * v16], s);
strcpy(v19, src);
if ( contacts > 0 )
{
v17 = 0LL;
do
{
v18 = ::fd[65 * v17];
if ( v18 != fd )
write(v18, v19, 0x3E8uLL);
++v17;
}
while ( contacts > (int)v17 );
}
}
}
strcpy(v19, "QUIT");
write(fd, v19, 0x3E8uLL);
return puts("\nchat has finished");
}

original_code:
void chat(int sd2){
 int n,i, served = 0;
 printf("CHAAAAAAAAAAAAT\n");
 char clientname[1000];
 char *var;
 char message[1000];
 char inbuf[1000];
 char outbuf[1000];
 while(served == 0){
  for(i = 0; i < 1000; i++){
   inbuf[i] = 0;
   outbuf[i] = 0;
  }
  n = read(sd2, inbuf, sizeof(inbuf));
  if(!strcmp(inbuf, "QUIT")){
   sprintf(outbuf, "QUIT");
   write(sd2, outbuf, sizeof(outbuf));
   served = 1;
  }else{
   if(!strncmp(inbuf, "<", 1)){
    var = strstr(inbuf, "<");
    i = 0;
    var++;
    while(*var != '>'){
     clientname[i] = *var;
     printf("%c", *var);
     var++;
     i++;
    }
    clientname[i] = '\0';
    var = strstr(inbuf, ">");
    i = 0;
    var++;
    while(*var != '\0'){
     message[i] = *var;
     printf("%c", *var);
     var++;
     i++;
    }
    message[i] = '\0';
    printf("\nMessage [%s] is for [%s]\n\n", message, clientname);
    for(i = 0; onlinecontacts[i].contactsd != sd2; i++);
    sprintf(outbuf, "<%s> wrote: [%s]", onlinecontacts[i].contactname, message);
    i = 0;
    while(strcmp(onlinecontacts[i].contactname, clientname)){
     i++;
    }
    write(onlinecontacts[i].contactsd, outbuf, sizeof(outbuf));
   }else{
    for(i = 0; onlinecontacts[i].contactsd != sd2; i++);
    sprintf(message, "<%s> wrote: [%s]", onlinecontacts[i].contactname, inbuf);
    strcpy(outbuf, message);
    for(i = 0; i < contacts; i++){
     if(onlinecontacts[i].contactsd != sd2)
      write(onlinecontacts[i].contactsd, outbuf, sizeof(outbuf));
    }
   }
  }
 }
 printf("\nchat has finished\n");
}

canonical_original_code:
void chat(int sd2){
 int n; int i; int served = 0;
 printf("CHAAAAAAAAAAAAT\n");
 char clientname[1000];
 char *var;
 char message[1000];
 char inbuf[1000];
 char outbuf[1000];
 while(served == 0){
  for(i = 0; i < 1000; i++){
   inbuf[i] = 0;
   outbuf[i] = 0;
  }
  n = read(sd2, inbuf, sizeof(inbuf));
  if(!strcmp(inbuf, "QUIT")){
   sprintf(outbuf, "QUIT");
   write(sd2, outbuf, sizeof(outbuf));
   served = 1;
  }else{
   if(!strncmp(inbuf, "<", 1)){
    var = strstr(inbuf, "<");
    i = 0;
    var++;
    while(*var != '>'){
     clientname[i] = *var;
     printf("%c", *var);
     var++;
     i++;
    }
    clientname[i] = '\0';
    var = strstr(inbuf, ">");
    i = 0;
    var++;
    while(*var != '\0'){
     message[i] = *var;
     printf("%c", *var);
     var++;
     i++;
    }
    message[i] = '\0';
    printf("\nMessage [%s] is for [%s]\n\n", message, clientname);
    for(i = 0; onlinecontacts[i].contactsd != sd2; i++);
    sprintf(outbuf, "<%s> wrote: [%s]", onlinecontacts[i].contactname, message);
    i = 0;
    while(strcmp(onlinecontacts[i].contactname, clientname)){
     i++;
    }
    write(onlinecontacts[i].contactsd, outbuf, sizeof(outbuf));
   }else{
    for(i = 0; onlinecontacts[i].contactsd != sd2; i++);
    sprintf(message, "<%s> wrote: [%s]", onlinecontacts[i].contactname, inbuf);
    strcpy(outbuf, message);
    for(i = 0; i < contacts; i++){
     if(onlinecontacts[i].contactsd != sd2)
      write(onlinecontacts[i].contactsd, outbuf, sizeof(outbuf));
    }
   }
  }
 }
 printf("\nchat has finished\n");
}

variable_types: n int

variable_types: served int

variable_types:
clientname
char [1000]

variable_types:
var
char *

variable_types:
message
char [1000]

variable_types:
inbuf
char [1000]

variable_types:
outbuf
char [1000]


decompiled_code:
void *__fastcall manage_connection(void *a1)
{
int v1; // er12
char *v2; // rax
const char *v3; // rbp
int v4; // ebx
int v6[250]; // [rsp+0h] [rbp-418h] BYREF
char v7; // [rsp+3E8h] [rbp-30h] BYREF

v1 = *(_DWORD *)a1;
v2 = (char *)v6;
do
*v2++ = 0;
while ( v2 != &v7 );
if ( contacts > 0 )
{
v3 = onlinecontacts;
v4 = 0;
do
{
sprintf((char *)v6, "[%d]: [%s]\n", (unsigned int)v4, v3);
write(v1, v6, 0x3E8uLL);
++v4;
v3 += 260;
}
while ( contacts > v4 );
}
v6[0] = 4476485;
write(v1, v6, 0x3E8uLL);
puts("-(IN THREAD)- sent online contacts");
puts("-(IN THREAD)- simulazione di chat");
chat(v1);
*(&tid + (int)(((_BYTE *)a1 - (_BYTE *)&active_socket) >> 2)) = -1LL;
close(v1);
puts("-(IN THREAD)- chiuso sd2");
--contacts;
return &thread_retval;
}

canonical_decompiled_code:
void *__fastcall func10(void *a1)
{
int v1; // er12
char *v2; // rax
const char *v3; // rbp
int v4; // ebx
int v6[250]; // [rsp+0h] [rbp-418h] BYREF
char v7; // [rsp+3E8h] [rbp-30h] BYREF

v1 = *(_DWORD *)a1;
v2 = (char *)v6;
do
*v2++ = 0;
while ( v2 != &v7 );
if ( contacts > 0 )
{
v3 = onlinecontacts;
v4 = 0;
do
{
sprintf((char *)v6, "[%d]: [%s]\n", (unsigned int)v4, v3);
write(v1, v6, 0x3E8uLL);
++v4;
v3 += 260;
}
while ( contacts > v4 );
}
v6[0] = 4476485;
write(v1, v6, 0x3E8uLL);
puts("-(IN THREAD)- sent online contacts");
puts("-(IN THREAD)- simulazione di chat");
func9(v1);
*(&tid + (int)(((_BYTE *)a1 - (_BYTE *)&active_socket) >> 2)) = -1LL;
close(v1);
puts("-(IN THREAD)- chiuso sd2");
--contacts;
return &thread_retval;
}

original_code:
void *manage_connection(void *sdp) {
 char inbuf[1000];
 char outbuf[1000];
 int i;
 int sd2 = *((int *)sdp);
 int j = ((int *)sdp)-active_socket;
 for(i = 0; i < 1000; i++){
  inbuf[i] = 0;
  outbuf[i] = 0;
 }
 for(i = 0; i < contacts; i++){
  sprintf(outbuf, "[%d]: [%s]\n", i, onlinecontacts[i].contactname);
  write(sd2, outbuf, sizeof(outbuf));
 }
 sprintf(outbuf, "END");
 write(sd2, outbuf, sizeof(outbuf));
 printf("-(IN THREAD)- sent online contacts\n");
 printf("-(IN THREAD)- simulazione di chat\n");
 chat(sd2);
 tid[j] = (pthread_t)-1;
 close(sd2);
 printf("-(IN THREAD)- chiuso sd2\n");
 contacts--;
 return &thread_retval;
}

variable_types:
sdp
void *

variable_types: i int

variable_types: j int

return_type: void *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rax
unsigned int v4; // ebp
int v5; // eax
int v6; // er12
unsigned __int16 v8; // bx
char *v9; // rax
__int64 v10; // rdx
char *v11; // r13
char v12; // di
__int64 v13; // rbx
int v14; // eax
char *v15; // rax
int v16; // ebx
int v17; // ebp
size_t v18; // rax
char v19[1000]; // [rsp+0h] [rbp-658h] BYREF
char v20; // [rsp+3E8h] [rbp-270h] BYREF
char s[5]; // [rsp+3EBh] [rbp-26Dh] BYREF
char src[268]; // [rsp+3F0h] [rbp-268h] BYREF
unsigned int v23; // [rsp+4FCh] [rbp-15Ch]
char dest[8]; // [rsp+500h] [rbp-158h] BYREF
__int64 v25; // [rsp+5FCh] [rbp-5Ch]
socklen_t addr_len; // [rsp+60Ch] [rbp-4Ch] BYREF
struct sockaddr addr; // [rsp+610h] [rbp-48h] BYREF
struct sockaddr inp[3]; // [rsp+620h] [rbp-38h] BYREF

if ( argc <= 1 )
return puts(
"\n"
"Usage: ./servertcp 'IP' ['port'] \n"
"Use 127.0.0.1 as IP if you want to test program on localhost, port number is optional!\n");
strcpy(s, "BUSY");
tid = -1LL;
active_socket[0] = -1;
qword_404438 = -1LL;
dword_40441C = -1;
qword_404440 = -1LL;
dword_404420 = -1;
v3 = v19;
do
*v3++ = 0;
while ( v3 != &v20 );
if ( argc == 3 )
{
v23 = atoi(argv[2]);
while ( v23 > 0x10000 )
{
printf("Bad port number, buond limits are (0,%d)\n\nEnter a new port number: ", 0x10000LL);
__isoc99_scanf();
}
}
else
{
v23 = 60000;
}
*(_QWORD *)&inp[0].sa_family = 2LL;
*(_QWORD *)&inp[0].sa_data[6] = 0LL;
inet_aton(argv[1], (struct in_addr *)&inp[0].sa_data[2]);
v4 = v23;
*(_WORD *)inp[0].sa_data = _bswap_16(v23);
printf("Server IP address and service port: [%s]:[%d]\n", argv[1], v4);
printf(
"Server IP address and Service Port [%d]:[%d]\n\n",
*(unsigned int *)&inp[0].sa_data[2],
*(unsigned __int16 *)inp[0].sa_data);
v5 = socket(2, 1, 0);
sd = v5;
if ( v5 < 0 )
{
perror("Socket creation failed\n");
exit(1);
}
printf("Socket created with sd: [%d]\n\n", (unsigned int)v5);
if ( bind(sd, inp, 0x10u) == -1 )
{
perror("Error in Bind\n");
exit(1);
}
if ( listen(sd, 1) < 0 )
{
perror("Listen failed\n");
exit(1);
}
signal(2, (__sighandler_t)interrupt_handler);
puts("Server in the service loop");
v6 = endloop;
while ( !endloop )
{
addr_len = 16;
puts("Server is waiting for a Client to serve...");
v17 = accept(sd, &addr, &addr_len);
if ( v17 < 0 )
{
if ( !endloop )
{
perror("Accept failed\n");
exit(1);
}
break;
}
if ( contacts <= 2 )
{
v8 = _bswap_16(*(__int16 *)addr.sa_data);
v9 = inet_ntoa(*(struct in_addr *)&addr.sa_data[2]);
printf("Connection with Client: [%s]:[%hu] - sd:[%d]\n", v9, v8, (unsigned int)v17);
v10 = 115LL;
if ( contacts <= 0 )
v10 = 32LL;
printf("This server is serving [%d] client%c\n", (unsigned int)(contacts + 1), v10);
read(v17, v19, 0x3E8uLL);
printf("Message from client: [%s]\n", v19);
v11 = strchr(v19, 60);
v12 = v11[1];
if ( v12 == 62 )
{
v14 = v6;
}
else
{
v13 = 1LL;
do
{
s[v13 + 4] = v12;
putchar(v12);
v14 = v13++;
v12 = v11[v13];
}
while ( v12 != 62 );
}
src[v14] = 0;
printf("\nClient name is: [%s]\n\n", src);
strcpy(dest, src);
HIDWORD(v25) = v17;
v15 = &onlinecontacts[260 * contacts];
*(_QWORD *)v15 = *(_QWORD *)dest;
*(_QWORD *)(v15 + 252) = v25;
qmemcpy(
(void *)((unsigned __int64)(v15 + 8) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)(dest - &v15[-((unsigned __int64)(v15 + 8) & 0xFFFFFFFFFFFFFFF8LL)]),
8LL * (((unsigned int)v15 - (((_DWORD)v15 + 8) & 0xFFFFFFF8) + 260) >> 3));
v16 = v6;
if ( tid != -1LL )
v16 = (qword_404438 != -1) + 1;
active_socket[v16] = v17;
if ( pthread_create(&tid + v16, 0LL, manage_connection, &active_socket[v16]) )
{
perror("Thread creation");
*(&tid + v16) = -1LL;
continue;
}
++contacts;
}
else
{
puts("Maximum threads active: closing connection");
v18 = strlen(s);
write(v17, s, v18 + 1);
close(v17);
}
puts("nuovo thread attivato");
}
puts("Server finished");
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rax
unsigned int v4; // ebp
int v5; // eax
int v6; // er12
unsigned __int16 v8; // bx
char *v9; // rax
__int64 v10; // rdx
char *v11; // r13
char v12; // di
__int64 v13; // rbx
int v14; // eax
char *v15; // rax
int v16; // ebx
int v17; // ebp
size_t v18; // rax
char v19[1000]; // [rsp+0h] [rbp-658h] BYREF
char v20; // [rsp+3E8h] [rbp-270h] BYREF
char s[5]; // [rsp+3EBh] [rbp-26Dh] BYREF
char src[268]; // [rsp+3F0h] [rbp-268h] BYREF
unsigned int v23; // [rsp+4FCh] [rbp-15Ch]
char dest[8]; // [rsp+500h] [rbp-158h] BYREF
__int64 v25; // [rsp+5FCh] [rbp-5Ch]
socklen_t addr_len; // [rsp+60Ch] [rbp-4Ch] BYREF
struct sockaddr addr; // [rsp+610h] [rbp-48h] BYREF
struct sockaddr inp[3]; // [rsp+620h] [rbp-38h] BYREF

if ( argc <= 1 )
return puts(
"\n"
"Usage: ./servertcp 'IP' ['port'] \n"
"Use 127.0.0.1 as IP if you want to test program on localhost, port number is optional!\n");
strcpy(s, "BUSY");
tid = -1LL;
active_socket[0] = -1;
qword_404438 = -1LL;
dword_40441C = -1;
qword_404440 = -1LL;
dword_404420 = -1;
v3 = v19;
do
*v3++ = 0;
while ( v3 != &v20 );
if ( argc == 3 )
{
v23 = atoi(argv[2]);
while ( v23 > 0x10000 )
{
printf("Bad port number, buond limits are (0,%d)\n\nEnter a new port number: ", 0x10000LL);
__isoc99_scanf();
}
}
else
{
v23 = 60000;
}
*(_QWORD *)&inp[0].sa_family = 2LL;
*(_QWORD *)&inp[0].sa_data[6] = 0LL;
inet_aton(argv[1], (struct in_addr *)&inp[0].sa_data[2]);
v4 = v23;
*(_WORD *)inp[0].sa_data = func8(v23);
printf("Server IP address and service port: [%s]:[%d]\n", argv[1], v4);
printf(
"Server IP address and Service Port [%d]:[%d]\n\n",
*(unsigned int *)&inp[0].sa_data[2],
*(unsigned __int16 *)inp[0].sa_data);
v5 = socket(2, 1, 0);
sd = v5;
if ( v5 < 0 )
{
perror("Socket creation failed\n");
exit(1);
}
printf("Socket created with sd: [%d]\n\n", (unsigned int)v5);
if ( bind(sd, inp, 0x10u) == -1 )
{
perror("Error in Bind\n");
exit(1);
}
if ( listen(sd, 1) < 0 )
{
perror("Listen failed\n");
exit(1);
}
signal(2, (__sighandler_t)interrupt_handler);
puts("Server in the service loop");
v6 = endloop;
while ( !endloop )
{
addr_len = 16;
puts("Server is waiting for a Client to serve...");
v17 = accept(sd, &addr, &addr_len);
if ( v17 < 0 )
{
if ( !endloop )
{
perror("Accept failed\n");
exit(1);
}
break;
}
if ( contacts <= 2 )
{
v8 = func8(*(__int16 *)addr.sa_data);
v9 = inet_ntoa(*(struct in_addr *)&addr.sa_data[2]);
printf("Connection with Client: [%s]:[%hu] - sd:[%d]\n", v9, v8, (unsigned int)v17);
v10 = 115LL;
if ( contacts <= 0 )
v10 = 32LL;
printf("This server is serving [%d] client%c\n", (unsigned int)(contacts + 1), v10);
read(v17, v19, 0x3E8uLL);
printf("Message from client: [%s]\n", v19);
v11 = strchr(v19, 60);
v12 = v11[1];
if ( v12 == 62 )
{
v14 = v6;
}
else
{
v13 = 1LL;
do
{
s[v13 + 4] = v12;
putchar(v12);
v14 = v13++;
v12 = v11[v13];
}
while ( v12 != 62 );
}
src[v14] = 0;
printf("\nClient name is: [%s]\n\n", src);
strcpy(dest, src);
HIDWORD(v25) = v17;
v15 = &onlinecontacts[260 * contacts];
*(_QWORD *)v15 = *(_QWORD *)dest;
*(_QWORD *)(v15 + 252) = v25;
qmemcpy(
(void *)((unsigned __int64)(v15 + 8) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)(dest - &v15[-((unsigned __int64)(v15 + 8) & 0xFFFFFFFFFFFFFFF8LL)]),
8LL * (((unsigned int)v15 - (((_DWORD)v15 + 8) & 0xFFFFFFF8) + 260) >> 3));
v16 = v6;
if ( tid != -1LL )
v16 = (qword_404438 != -1) + 1;
active_socket[v16] = v17;
if ( pthread_create(&tid + v16, 0LL, manage_connection, &active_socket[v16]) )
{
perror("Thread creation");
*(&tid + v16) = -1LL;
continue;
}
++contacts;
}
else
{
puts("Maximum threads active: closing connection");
v18 = strlen(s);
write(v17, s, v18 + 1);
close(v17);
}
puts("nuovo thread attivato");
}
puts("Server finished");
return 0;
}

original_code:
int main(int argc, char** argv){
 if(argc < 2){
  printf("\nUsage: ./servertcp 'IP' ['port'] \nUse 127.0.0.1 as IP if you want to test program on localhost, port number is optional!\n\n");
  return;
 }
 struct sockaddr_in sad;
 struct sockaddr_in cad;
 socklen_t alen;
 contact newcontact;
 int sd2, port, n, i, j = 0;
 char* var;
 char clientname[256];
 char busymsg[] = "BUSY";
 char buffer[1000];
 for(i=0;i<3;i++) {
  tid[i] = (pthread_t)-1;
  active_socket[i] = -1;
 }
 for(i = 0; i < 1000; i++){
  buffer[i] = 0;
 }
 if(argc == 3){
  port = atoi(argv[2]);
  while(port < 0 || port > 64 * 1024){
   printf("Bad port number, buond limits are (0,%d)\n\nEnter a new port number: ", 64 * 1024);
   scanf("%d", &port);
  }
 }else{
  port = 60000;
 }
 memset((char*)&sad, 0, sizeof(sad));
 sad.sin_family = 2;
 n = inet_aton(argv[1], &sad.sin_addr);
 sad.sin_port = htons((u_short)port);
 printf("Server IP address and service port: [%s]:[%d]\n", argv[1], port);
 printf("Server IP address and Service Port [%d]:[%d]\n\n", sad.sin_addr.s_addr, sad.sin_port);
 sd = socket(2, SOCK_STREAM, 0);
 if(sd < 0) {
  perror("Socket creation failed\n");
  exit(1);
 }
 printf("Socket created with sd: [%d]\n\n", sd);
 n = bind(sd, (struct sockaddr *)&sad, sizeof(sad));
 if(n == -1){
  perror("Error in Bind\n");
  exit(1);
 }
 n = listen(sd, 1);
 if(n < 0){
  perror("Listen failed\n");
  exit(1);
 }
 signal(2, interrupt_handler);
 printf("Server in the service loop\n");
 while(!endloop){
  alen = sizeof(cad);
  printf("Server is waiting for a Client to serve...\n");
  sd2 = accept(sd, (struct sockaddr *)&cad, &alen);
  if (sd2 < 0) {
   if(endloop) break;
   perror("Accept failed\n");
   exit(1);
  }
  if(contacts < 3) {
   printf("Connection with Client: [%s]:[%hu] - sd:[%d]\n", inet_ntoa(cad.sin_addr), ntohs(cad.sin_port), sd2);
   printf("This server is serving [%d] client%c\n", contacts + 1, (contacts > 0) ? 's' : ' ');
   n = read(sd2, buffer, sizeof(buffer));
   printf("Message from client: [%s]\n", buffer);
   var = strstr(buffer, "<");
   i = 0;
   var++;
   while(*var != '>'){
    clientname[i] = *var;
    printf("%c", *var);
    var++;
    i++;
   }
   clientname[i] = '\0';
   printf("\nClient name is:  [%s]\n\n", clientname);
   strcpy(newcontact.contactname, clientname);
   newcontact.contactsd = sd2;
   onlinecontacts[contacts] = newcontact;
   for(i=0;tid[i]!=(pthread_t)-1;i++);
   active_socket[i] = sd2;
   if(pthread_create(&tid[i], ((void *)0), manage_connection, &active_socket[i])!=0) {
    perror("Thread creation");
    tid[i] = (pthread_t)-1;
    continue;
   }
   contacts++;
  } else {
   printf("Maximum threads active: closing connection\n");
   write(sd2, busymsg, strlen(busymsg)+1);
   close(sd2);
  }
  printf("nuovo thread attivato\n");
 }
 printf("Server finished\n");
}

canonical_original_code:
int main(int argc, char **argv){
 if(argc < 2){
  printf("\nUsage: ./servertcp 'IP' ['port'] \nUse 127.0.0.1 as IP if you want to test program on localhost, port number is optional!\n\n");
  return;
 }
 struct sockaddr_in sad;
 struct sockaddr_in cad;
 unsigned int alen;
 struct contact newcontact;
 int sd2; int port; int n; int i; int j = 0;
 char *var;
 char clientname[256];
 char busymsg[] = "BUSY";
 char buffer[1000];
 for(i=0;i<3;i++) {
  tid[i] = (pthread_t)-1;
  active_socket[i] = -1;
 }
 for(i = 0; i < 1000; i++){
  buffer[i] = 0;
 }
 if(argc == 3){
  port = atoi(argv[2]);
  while(port < 0 || port > 64 * 1024){
   printf("Bad port number, buond limits are (0,%d)\n\nEnter a new port number: ", 64 * 1024);
   scanf("%d", &port);
  }
 }else{
  port = 60000;
 }
 memset((char *)&sad, 0, sizeof(sad));
 sad.sin_family = 2;
 n = inet_aton(argv[1], &sad.sin_addr);
 sad.sin_port = htons((unsigned short)port);
 printf("Server IP address and service port: [%s]:[%d]\n", argv[1], port);
 printf("Server IP address and Service Port [%d]:[%d]\n\n", sad.sin_addr.s_addr, sad.sin_port);
 sd = socket(2, SOCK_STREAM, 0);
 if(sd < 0) {
  perror("Socket creation failed\n");
  exit(1);
 }
 printf("Socket created with sd: [%d]\n\n", sd);
 n = bind(sd, (struct sockaddr *)&sad, sizeof(sad));
 if(n == -1){
  perror("Error in Bind\n");
  exit(1);
 }
 n = listen(sd, 1);
 if(n < 0){
  perror("Listen failed\n");
  exit(1);
 }
 signal(2, interrupt_handler);
 printf("Server in the service loop\n");
 while(!endloop){
  alen = sizeof(cad);
  printf("Server is waiting for a Client to serve...\n");
  sd2 = accept(sd, (struct sockaddr *)&cad, &alen);
  if (sd2 < 0) {
   if(endloop) break;
   perror("Accept failed\n");
   exit(1);
  }
  if(contacts < 3) {
   printf("Connection with Client: [%s]:[%hu] - sd:[%d]\n", inet_ntoa(cad.sin_addr), ntohs(cad.sin_port), sd2);
   printf("This server is serving [%d] client%c\n", contacts + 1, (contacts > 0) ? 's' : ' ');
   n = read(sd2, buffer, sizeof(buffer));
   printf("Message from client: [%s]\n", buffer);
   var = strstr(buffer, "<");
   i = 0;
   var++;
   while(*var != '>'){
    clientname[i] = *var;
    printf("%c", *var);
    var++;
    i++;
   }
   clientname[i] = '\0';
   printf("\nClient name is:  [%s]\n\n", clientname);
   strcpy(newcontact.contactname, clientname);
   newcontact.contactsd = sd2;
   onlinecontacts[contacts] = newcontact;
   for(i=0;tid[i]!=(pthread_t)-1;i++);
   active_socket[i] = sd2;
   if(pthread_create(&tid[i], ((void *)0), manage_connection, &active_socket[i])!=0) {
    perror("Thread creation");
    tid[i] = (pthread_t)-1;
    continue;
   }
   contacts++;
  } else {
   printf("Maximum threads active: closing connection\n");
   write(sd2, busymsg, strlen(busymsg)+1);
   close(sd2);
  }
  printf("nuovo thread attivato\n");
 }
 printf("Server finished\n");
}

variable_types: argc int

variable_types:
argv
char **

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

variable_types:
alen
unsigned int

variable_types: struct contact { char contactname[256]; int contactsd; }

variable_types: port int

variable_types:
clientname
char [256]

variable_types:
busymsg
char []

variable_types:
buffer
char [1000]

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


decompiled_code:
void __fastcall manage_reading(void *a1)
{
char *v1; // rax
char v2[1000]; // [rsp+0h] [rbp-408h] BYREF
char v3; // [rsp+3E8h] [rbp-20h] BYREF

if ( !served )
{
while ( 1 )
{
v1 = v2;
do
*v1++ = 0;
while ( &v3 != v1 );
read(sd, v2, 0x3E8uLL);
if ( !strcmp(v2, "QUIT") )
break;
printf("Received message!!: %s\n", v2);
if ( served )
return;
}
served = 1;
}
}

canonical_decompiled_code:
void __fastcall func7(void *a1)
{
char *v1; // rax
char v2[1000]; // [rsp+0h] [rbp-408h] BYREF
char v3; // [rsp+3E8h] [rbp-20h] BYREF

if ( !served )
{
while ( 1 )
{
v1 = v2;
do
*v1++ = 0;
while ( &v3 != v1 );
read(sd, v2, 0x3E8uLL);
if ( !strcmp(v2, "QUIT") )
break;
printf("Received message!!: %s\n", v2);
if ( served )
return;
}
served = 1;
}
}

original_code:
void *manage_reading(){
 int n, i;
 char inbuf[1000];
 while(served == 0){
  for(i = 0; i < 1000; i++){
   inbuf[i] = 0;
  }
  n = read(sd, inbuf, sizeof(inbuf));
  if(!strcmp(inbuf, "QUIT"))
   served = 1;
  else
   printf("Received message!!: %s\n", inbuf);
 }
}

canonical_original_code:
void *manage_reading(){
 int n; int i;
 char inbuf[1000];
 while(served == 0){
  for(i = 0; i < 1000; i++){
   inbuf[i] = 0;
  }
  n = read(sd, inbuf, sizeof(inbuf));
  if(!strcmp(inbuf, "QUIT"))
   served = 1;
  else
   printf("Received message!!: %s\n", inbuf);
 }
}

variable_types: n int


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rax
const char *v4; // rbx
int v5; // ebp
void *v7; // rcx
char *v8; // rax
pthread_t v9; // [rsp+0h] [rbp-818h] BYREF
char v10; // [rsp+Fh] [rbp-809h]
char s[1000]; // [rsp+10h] [rbp-808h] BYREF
char v12; // [rsp+3F8h] [rbp-420h] BYREF
char buf[1004]; // [rsp+400h] [rbp-418h] BYREF
unsigned int v14; // [rsp+7ECh] [rbp-2Ch]
struct sockaddr inp[2]; // [rsp+7F0h] [rbp-28h] BYREF

v3 = 0LL;
if ( argc <= 2 )
return puts(
"\n"
"Usage: ./clienttcp 'wanted client host name' 'IP'\n"
"Use 127.0.0.1 as IP if you want to test program on localhost!\n");
do
{
buf[v3] = 0;
s[v3++] = 0;
}
while ( v3 != 1000 );
if ( argc == 4 )
{
v14 = atoi(argv[3]);
while ( v14 > 0x10000 )
{
printf("Bad port number, buond limits are (0,%d)\n\nEnter a new port number: ", 0x10000LL);
__isoc99_scanf();
}
}
else
{
v14 = 60000;
}
*(_QWORD *)&inp[0].sa_family = 2LL;
*(_QWORD *)&inp[0].sa_data[6] = 0LL;
*(_WORD *)inp[0].sa_data = _bswap_16(v14);
inet_aton(argv[2], (struct in_addr *)&inp[0].sa_data[2]);
printf("\nServer to contact: [%s]:[%d]\n", argv[2], v14);
printf("Server to contact: [%d]:[%d]\n\n", *(unsigned int *)&inp[0].sa_data[2], *(unsigned __int16 *)inp[0].sa_data);
v4 = argv[1];
printf("Client's hostname is: [%s]\n\n", v4);
sd = socket(2, 1, 0);
printf("Connecting to [%d]:[%d]...\n\n", *(unsigned int *)&inp[0].sa_data[2], *(unsigned __int16 *)inp[0].sa_data);
sleep(5u);
connect(sd, inp, 0x10u);
printf("Connected to Server: [%d]:[%d]\n", *(unsigned int *)&inp[0].sa_data[2], *(unsigned __int16 *)inp[0].sa_data);
sprintf(s, "HELLO I AM <%s>", v4);
write(sd, s, 0x3E8uLL);
puts("waiting for Server status...\n");
sleep(5u);
read(sd, buf, 0x3E8uLL);
if ( !strcmp(buf, "BUSY") )
{
puts("\nServer Busy, closing connection");
close(sd);
}
else
{
puts("Online Contacts:");
v5 = 0;
while ( strcmp(buf, "END") )
{
printf("\t\t - %s\n", buf);
read(sd, buf, 0x3E8uLL);
++v5;
}
if ( !v5 )
{
while ( 1 )
{
puts("No contacts online, do you wish to close connection? (Y/N)");
__isoc99_scanf();
if ( v10 == 110 )
break;
if ( v10 > 110 )
{
if ( v10 == 121 )
goto LABEL_32;
}
else
{
if ( v10 == 78 )
break;
if ( v10 == 89 )
goto LABEL_32;
}
puts("wrong character pressed");
while ( getchar() != 10 )
;
}
}
puts(
"\t\t- <[contactname]:[message] to send a private message>\n"
"\t\t- <[message] to send a public message>\n"
"\t\t- <[QUIT] to exit>\n");
if ( pthread_create(&v9, 0LL, (void *(*)(void *))manage_reading, v7) )
perror("Thread creation");
while ( !served )
{
v8 = s;
do
*v8++ = 0;
while ( &v12 != v8 );
while ( getchar() != 10 )
;
__isoc99_scanf();
write(sd, s, 0x3E8uLL);
if ( !strcmp(s, "QUIT") )
served = 1;
}
}
LABEL_32:
puts("Closing connection...");
sleep(1u);
puts("Bye!");
sleep(5u);
close(sd);
puts("\n\nClient finished\n");
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rax
const char *v4; // rbx
int v5; // ebp
void *v7; // rcx
char *v8; // rax
pthread_t v9; // [rsp+0h] [rbp-818h] BYREF
char v10; // [rsp+Fh] [rbp-809h]
char s[1000]; // [rsp+10h] [rbp-808h] BYREF
char v12; // [rsp+3F8h] [rbp-420h] BYREF
char buf[1004]; // [rsp+400h] [rbp-418h] BYREF
unsigned int v14; // [rsp+7ECh] [rbp-2Ch]
struct sockaddr inp[2]; // [rsp+7F0h] [rbp-28h] BYREF

v3 = 0LL;
if ( argc <= 2 )
return puts(
"\n"
"Usage: ./clienttcp 'wanted client host name' 'IP'\n"
"Use 127.0.0.1 as IP if you want to test program on localhost!\n");
do
{
buf[v3] = 0;
s[v3++] = 0;
}
while ( v3 != 1000 );
if ( argc == 4 )
{
v14 = atoi(argv[3]);
while ( v14 > 0x10000 )
{
printf("Bad port number, buond limits are (0,%d)\n\nEnter a new port number: ", 0x10000LL);
__isoc99_scanf();
}
}
else
{
v14 = 60000;
}
*(_QWORD *)&inp[0].sa_family = 2LL;
*(_QWORD *)&inp[0].sa_data[6] = 0LL;
*(_WORD *)inp[0].sa_data = func8(v14);
inet_aton(argv[2], (struct in_addr *)&inp[0].sa_data[2]);
printf("\nServer to contact: [%s]:[%d]\n", argv[2], v14);
printf("Server to contact: [%d]:[%d]\n\n", *(unsigned int *)&inp[0].sa_data[2], *(unsigned __int16 *)inp[0].sa_data);
v4 = argv[1];
printf("Client's hostname is: [%s]\n\n", v4);
sd = socket(2, 1, 0);
printf("Connecting to [%d]:[%d]...\n\n", *(unsigned int *)&inp[0].sa_data[2], *(unsigned __int16 *)inp[0].sa_data);
sleep(5u);
connect(sd, inp, 0x10u);
printf("Connected to Server: [%d]:[%d]\n", *(unsigned int *)&inp[0].sa_data[2], *(unsigned __int16 *)inp[0].sa_data);
sprintf(s, "HELLO I AM <%s>", v4);
write(sd, s, 0x3E8uLL);
puts("waiting for Server status...\n");
sleep(5u);
read(sd, buf, 0x3E8uLL);
if ( !strcmp(buf, "BUSY") )
{
puts("\nServer Busy, closing connection");
close(sd);
}
else
{
puts("Online Contacts:");
v5 = 0;
while ( strcmp(buf, "END") )
{
printf("\t\t - %s\n", buf);
read(sd, buf, 0x3E8uLL);
++v5;
}
if ( !v5 )
{
while ( 1 )
{
puts("No contacts online, do you wish to close connection? (Y/N)");
__isoc99_scanf();
if ( v10 == 110 )
break;
if ( v10 > 110 )
{
if ( v10 == 121 )
goto LABEL_32;
}
else
{
if ( v10 == 78 )
break;
if ( v10 == 89 )
goto LABEL_32;
}
puts("wrong character pressed");
while ( getchar() != 10 )
;
}
}
puts(
"\t\t- <[contactname]:[message] to send a private message>\n"
"\t\t- <[message] to send a public message>\n"
"\t\t- <[QUIT] to exit>\n");
if ( pthread_create(&v9, 0LL, (void *(*)(void *))manage_reading, v7) )
perror("Thread creation");
while ( !served )
{
v8 = s;
do
*v8++ = 0;
while ( &v12 != v8 );
while ( getchar() != 10 )
;
__isoc99_scanf();
write(sd, s, 0x3E8uLL);
if ( !strcmp(s, "QUIT") )
served = 1;
}
}
LABEL_32:
puts("Closing connection...");
sleep(1u);
puts("Bye!");
sleep(5u);
close(sd);
puts("\n\nClient finished\n");
return 0;
}

original_code:
int main(int argc, char** argv){
 if(argc < 3){
  printf("\nUsage: ./clienttcp 'wanted client host name' 'IP'\nUse 127.0.0.1 as IP if you want to test program on localhost!\n\n");
  return;
 }
 struct sockaddr_in sad;
 socklen_t alen;
 int i, n, port, oc = 0;
 char inbuf[1000], outbuf[1000], outchar;
 char *hostname;
 pthread_t tid;
 for(i = 0; i < 1000; i++){
  inbuf[i] = 0;
  outbuf[i] = 0;
 }
 if(argc == 4){
  port = atoi(argv[3]);
  while(port < 0 || port > 64 * 1024){
   printf("Bad port number, buond limits are (0,%d)\n\nEnter a new port number: ", 64 * 1024);
   scanf("%d", &port);
  }
 }else{
  port = 60000;
 }
 memset((char*)&sad, 0, sizeof(sad));
 sad.sin_family = 2;
 sad.sin_port = htons((u_short)port);
 n = inet_aton(argv[2], &sad.sin_addr);
 printf("\nServer to contact: [%s]:[%d]\n", argv[2], port);
 printf("Server to contact: [%d]:[%d]\n\n", sad.sin_addr.s_addr, sad.sin_port);
 hostname = argv[1];
 printf("Client's hostname is: [%s]\n\n", hostname);
 sd = socket(2, SOCK_STREAM, 0);
 printf("Connecting to [%d]:[%d]...\n\n", sad.sin_addr.s_addr, sad.sin_port);
 sleep(5);
 connect(sd, (struct sockaddr*)&sad, sizeof(sad));
 printf("Connected to Server: [%d]:[%d]\n", sad.sin_addr.s_addr, sad.sin_port);
 sprintf(outbuf, "HELLO I AM <%s>", hostname);
 write(sd, outbuf, sizeof(outbuf));
 printf("waiting for Server status...\n\n");
 sleep(5);
 n = read(sd, inbuf, sizeof(inbuf));
 if(strcmp(inbuf, "BUSY")){
  printf("Online Contacts:\n");
  while(strcmp(inbuf, "END")){
   printf("\t\t - %s\n", inbuf);
   n = read(sd, inbuf, sizeof(inbuf));
   oc++;
  }
  if(oc == 0){
reask: printf("No contacts online, do you wish to close connection? (Y/N)\n");
   scanf("%c", &outchar);
   switch(outchar){
    case 'Y': goto out;
    case 'y': goto out;
    case 'N':
       break;
    case 'n':
       break;
    default:
     printf("wrong character pressed\n");
     while(getchar() != '\n');
     goto reask;
     break;
   }
  }
  printf("\t\t- <[contactname]:[message] to send a private message>\n\t\t- <[message] to send a public message>\n\t\t- <[QUIT] to exit>\n\n");
  if(pthread_create(&tid, ((void *)0), manage_reading)!=0) {
   perror("Thread creation");
  }
  while(served == 0){
   for(i = 0; i < 1000; i++){
    outbuf[i] = 0;
   }
   while(getchar() != '\n');
   scanf("%[^\n]s", outbuf);
   write(sd, outbuf, sizeof(outbuf));
   if(!strcmp(outbuf, "QUIT"))
    served = 1;
  }
 }else{
  printf("\nServer Busy, closing connection\n");
  close(sd);
 }
out: printf("Closing connection...\n");
 sleep(1);
 printf("Bye!\n");
 sleep(5);
 close(sd);
 printf("\n\nClient finished\n\n");
}

canonical_original_code:
int main(int argc, char **argv){
 if(argc < 3){
  printf("\nUsage: ./clienttcp 'wanted client host name' 'IP'\nUse 127.0.0.1 as IP if you want to test program on localhost!\n\n");
  return;
 }
 struct sockaddr_in sad;
 unsigned int alen;
 int i; int n; int port; int oc = 0;
 char inbuf[1000]; char outbuf[1000]; char outchar;
 char *hostname;
 unsigned long tid;
 for(i = 0; i < 1000; i++){
  inbuf[i] = 0;
  outbuf[i] = 0;
 }
 if(argc == 4){
  port = atoi(argv[3]);
  while(port < 0 || port > 64 * 1024){
   printf("Bad port number, buond limits are (0,%d)\n\nEnter a new port number: ", 64 * 1024);
   scanf("%d", &port);
  }
 }else{
  port = 60000;
 }
 memset((char *)&sad, 0, sizeof(sad));
 sad.sin_family = 2;
 sad.sin_port = htons((unsigned short)port);
 n = inet_aton(argv[2], &sad.sin_addr);
 printf("\nServer to contact: [%s]:[%d]\n", argv[2], port);
 printf("Server to contact: [%d]:[%d]\n\n", sad.sin_addr.s_addr, sad.sin_port);
 hostname = argv[1];
 printf("Client's hostname is: [%s]\n\n", hostname);
 sd = socket(2, SOCK_STREAM, 0);
 printf("Connecting to [%d]:[%d]...\n\n", sad.sin_addr.s_addr, sad.sin_port);
 sleep(5);
 connect(sd, (struct sockaddr *)&sad, sizeof(sad));
 printf("Connected to Server: [%d]:[%d]\n", sad.sin_addr.s_addr, sad.sin_port);
 sprintf(outbuf, "HELLO I AM <%s>", hostname);
 write(sd, outbuf, sizeof(outbuf));
 printf("waiting for Server status...\n\n");
 sleep(5);
 n = read(sd, inbuf, sizeof(inbuf));
 if(strcmp(inbuf, "BUSY")){
  printf("Online Contacts:\n");
  while(strcmp(inbuf, "END")){
   printf("\t\t - %s\n", inbuf);
   n = read(sd, inbuf, sizeof(inbuf));
   oc++;
  }
  if(oc == 0){
reask: printf("No contacts online, do you wish to close connection? (Y/N)\n");
   scanf("%c", &outchar);
   switch(outchar){
    case 'Y': goto out;
    case 'y': goto out;
    case 'N':
       break;
    case 'n':
       break;
    default:
     printf("wrong character pressed\n");
     while(getchar() != '\n');
     goto reask;
     break;
   }
  }
  printf("\t\t- <[contactname]:[message] to send a private message>\n\t\t- <[message] to send a public message>\n\t\t- <[QUIT] to exit>\n\n");
  if(pthread_create(&tid, ((void *)0), manage_reading)!=0) {
   perror("Thread creation");
  }
  while(served == 0){
   for(i = 0; i < 1000; i++){
    outbuf[i] = 0;
   }
   while(getchar() != '\n');
   scanf("%[^\n]s", outbuf);
   write(sd, outbuf, sizeof(outbuf));
   if(!strcmp(outbuf, "QUIT"))
    served = 1;
  }
 }else{
  printf("\nServer Busy, closing connection\n");
  close(sd);
 }
out: printf("Closing connection...\n");
 sleep(1);
 printf("Bye!\n");
 sleep(5);
 close(sd);
 printf("\n\nClient finished\n\n");
}

variable_types: oc int

variable_types: outchar char

variable_types:
hostname
char *

variable_types:
tid
unsigned long


decompiled_code:
__int64 __fastcall s_wk_simple_execute(__int64 a1)
{
unsigned int v1; // ebx
const char *v3; // [rsp+8h] [rbp-10h] BYREF

v3 = 0LL;
v1 = sqlite3_exec(sqlite, a1, 0LL, 0LL, &v3);
if ( v1 )
fprintf(stderr, "%s\n", v3);
sqlite3_free(v3);
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
unsigned int v1; // ebx
const char *v3; // [rsp+8h] [rbp-10h] BYREF

v3 = 0LL;
v1 = sqlite3_exec(sqlite, a1, 0LL, 0LL, &v3);
if ( v1 )
fprintf(stderr, "%s\n", v3);
sqlite3_free(v3);
return v1;
}

original_code:
static int s_wk_simple_execute(char* sql) {
 int ret;
 char* err = ((void *)0);
 ret = sqlite3_exec(sqlite, sql, ((void *)0), ((void *)0), &err);
 if (ret != 0)
  fprintf(stderr, "%s\n", err);
 sqlite3_free(err);
 return ret;
}

canonical_original_code:
static int s_wk_simple_execute(char *sql) {
 int ret;
 char *err = ((void *)0);
 ret = sqlite3_exec(sqlite, sql, ((void *)0), ((void *)0), &err);
 if (ret != 0)
  fprintf(stderr, "%s\n", err);
 sqlite3_free(err);
 return ret;
}

variable_types:
sql
char *

variable_types: ret int

variable_types:
err
char *


decompiled_code:
int s_print_sqlite_error()
{
const char *v0; // rax

v0 = (const char *)sqlite3_errmsg(sqlite);
return fprintf(stderr, "%s\n", v0);
}

canonical_decompiled_code:
int func1()
{
const char *v0; // rax

v0 = (const char *)sqlite3_errmsg(sqlite);
return fprintf(stderr, "%s\n", v0);
}

original_code:
void s_print_sqlite_error() {
 fprintf(stderr, "%s\n", sqlite3_errmsg(sqlite));
}


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

if ( (unsigned int)sqlite3_open("/Users/matt/lift/data/lift.sqlite3", &sqlite) )
return 0xFFFFFFFFLL;
result = s_wk_simple_execute((__int64)"BEGIN");
if ( (_DWORD)result )
result = 4294967294LL;
return result;
}

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

if ( (unsigned int)sqlite3_open("/Users/matt/lift/data/lift.sqlite3", &sqlite) )
return 0xFFFFFFFFLL;
result = func0((__int64)"BEGIN");
if ( (_DWORD)result )
result = 4294967294LL;
return result;
}

original_code:
int wk_db_open() {
 if (sqlite3_open("/Users/matt/lift/data/lift.sqlite3", &sqlite) == 0) {
  if (s_wk_simple_execute("BEGIN") == 0) {
   return 0;
  } else {
   return -2;
  }
 } else {
  return -1;
 }
}


decompiled_code:
__int64 __fastcall wk_db_close(int a1)
{
if ( a1 )
{
if ( !(unsigned int)s_wk_simple_execute((__int64)"COMMIT") )
return (unsigned int)-((unsigned int)sqlite3_close(sqlite) != 0);
}
else if ( !(unsigned int)s_wk_simple_execute((__int64)"ROLLBACK") )
{
return (unsigned int)-((unsigned int)sqlite3_close(sqlite) != 0);
}
sqlite3_close(sqlite);
return 4294967294LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(int a1)
{
if ( a1 )
{
if ( !(unsigned int)func0((__int64)"COMMIT") )
return (unsigned int)-((unsigned int)sqlite3_close(sqlite) != 0);
}
else if ( !(unsigned int)func0((__int64)"ROLLBACK") )
{
return (unsigned int)-((unsigned int)sqlite3_close(sqlite) != 0);
}
sqlite3_close(sqlite);
return 4294967294LL;
}

original_code:
int wk_db_close(int had_errors) {
 int ret = 0;
 if (had_errors) {
  if (s_wk_simple_execute("COMMIT") != 0)
   ret = -2;
 } else {
  if (s_wk_simple_execute("ROLLBACK") != 0)
   ret = -2;
 }
 if (sqlite3_close(sqlite) != 0 && ret == 0)
  ret = -1;
 return ret;
}


wkSetAllocInit

decompiled_code:
void *wkSetAllocInit()
{
return calloc(1uLL, 0x30uLL);
}

canonical_decompiled_code:
void *func0()
{
return calloc(1uLL, 0x30uLL);
}

original_code:
wkSet* wkSetAllocInit() {
 return calloc(1, sizeof(wkSet));
}

canonical_original_code:
struct wkSet *wkSetAllocInit() {
 return calloc(1, sizeof(wkSet));
}

return_type: struct wkSet { struct wkExercise { struct wkWorkout { struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; } start; struct tm end; char *mood; char *cardio; char *comments; int num_exercises; struct wkExercise **exercises; } *workout; char *name; int num_sets; struct wkSet **sets; } *exercise; int reps; double weight; int extend_reps; double extend_weight; char *comment; } *

user_defined_types: struct wkSet { struct wkExercise *exercise; int reps; double weight; int extend_reps; double extend_weight; char *comment; }

user_defined_types: struct wkExercise { struct wkWorkout *workout; char *name; int num_sets; struct wkSet **sets; }

user_defined_types: struct wkWorkout { struct tm start; struct tm end; char *mood; char *cardio; char *comments; int num_exercises; struct wkExercise **exercises; }


wkSetFree

decompiled_code:
void __fastcall wkSetFree(void *a1)
{
if ( a1 )
free(a1);
}

canonical_decompiled_code:
void __fastcall func1(void *a1)
{
if ( a1 )
free(a1);
}

original_code:
void wkSetFree(wkSet* s) {
 if (s) free(s);
}

canonical_original_code:
void wkSetFree(struct wkSet *s) {
 if (s) free(s);
}

variable_types: struct wkSet { struct wkExercise { struct wkWorkout { struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; } start; struct tm end; char *mood; char *cardio; char *comments; int num_exercises; struct wkExercise **exercises; } *workout; char *name; int num_sets; struct wkSet **sets; } *exercise; int reps; double weight; int extend_reps; double extend_weight; char *comment; } *


wkExerciseAllocInit

decompiled_code:
_QWORD *wkExerciseAllocInit()
{
_QWORD *v0; // rbx

v0 = calloc(1uLL, 0x20uLL);
v0[3] = calloc(0x14uLL, 0x30uLL);
return v0;
}

canonical_decompiled_code:
_QWORD *func2()
{
_QWORD *v0; // rbx

v0 = calloc(1uLL, 0x20uLL);
v0[3] = calloc(0x14uLL, 0x30uLL);
return v0;
}

original_code:
wkExercise* wkExerciseAllocInit() {
 wkExercise* ret = calloc(1, sizeof(wkExercise));
 ret->sets = calloc(20, sizeof(wkSet));
 return ret;
}

canonical_original_code:
struct wkExercise *wkExerciseAllocInit() {
 struct wkExercise *ret = calloc(1, sizeof(wkExercise));
 ret->sets = calloc(20, sizeof(wkSet));
 return ret;
}

variable_types: struct wkExercise { struct wkWorkout { struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; } start; struct tm end; char *mood; char *cardio; char *comments; int num_exercises; struct wkExercise **exercises; } *workout; char *name; int num_sets; struct wkSet { struct wkExercise *exercise; int reps; double weight; int extend_reps; double extend_weight; char *comment; } **sets; } *


wkExerciseFreeDeep

decompiled_code:
void __fastcall wkExerciseFreeDeep(void *ptr)
{
void *v2; // rdi
__int64 v3; // rbx

if ( ptr )
{
v2 = (void *)*((_QWORD *)ptr + 1);
if ( v2 )
free(v2);
if ( *((int *)ptr + 4) > 0 )
{
v3 = 0LL;
do
wkSetFree(*(void **)(*((_QWORD *)ptr + 3) + 8 * v3++));
while ( *((_DWORD *)ptr + 4) > (int)v3 );
}
free(*((void **)ptr + 3));
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func3(void *ptr)
{
void *v2; // rdi
__int64 v3; // rbx

if ( ptr )
{
v2 = (void *)*((_QWORD *)ptr + 1);
if ( v2 )
free(v2);
if ( *((int *)ptr + 4) > 0 )
{
v3 = 0LL;
do
func1(*(void **)(*((_QWORD *)ptr + 3) + 8 * v3++));
while ( *((_DWORD *)ptr + 4) > (int)v3 );
}
free(*((void **)ptr + 3));
free(ptr);
}
}

original_code:
void wkExerciseFreeDeep(wkExercise* e) {
 if (e) {
  if (e->name) free(e->name);
  for (int i = 0; i < e->num_sets; i++)
   wkSetFree(e->sets[i]);
  free(e->sets);
  free(e);
 }
}

canonical_original_code:
void wkExerciseFreeDeep(struct wkExercise *e) {
 if (e) {
  if (e->name) free(e->name);
  for (int i = 0; i < e->num_sets; i++)
   wkSetFree(e->sets[i]);
  free(e->sets);
  free(e);
 }
}

variable_types: i int

user_defined_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; }


wkWorkoutAllocInit

decompiled_code:
_QWORD *wkWorkoutAllocInit()
{
_QWORD *v0; // rbx

v0 = calloc(1uLL, 0x98uLL);
v0[18] = calloc(0x1EuLL, 0x20uLL);
return v0;
}

canonical_decompiled_code:
_QWORD *func4()
{
_QWORD *v0; // rbx

v0 = calloc(1uLL, 0x98uLL);
v0[18] = calloc(0x1EuLL, 0x20uLL);
return v0;
}

original_code:
wkWorkout* wkWorkoutAllocInit() {
 wkWorkout* ret = calloc(1, sizeof(wkWorkout));
 ret->exercises = calloc(30, sizeof(wkExercise));
 return ret;
}

canonical_original_code:
struct wkWorkout *wkWorkoutAllocInit() {
 struct wkWorkout *ret = calloc(1, sizeof(wkWorkout));
 ret->exercises = calloc(30, sizeof(wkExercise));
 return ret;
}

variable_types: struct wkWorkout { struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; } start; struct tm end; char *mood; char *cardio; char *comments; int num_exercises; struct wkExercise { struct wkWorkout *workout; char *name; int num_sets; struct wkSet { struct wkExercise *exercise; int reps; double weight; int extend_reps; double extend_weight; char *comment; } **sets; } **exercises; } *


wkWorkoutFreeDeep

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

if ( ptr )
{
v2 = (void *)*((_QWORD *)ptr + 14);
if ( v2 )
free(v2);
v3 = (void *)*((_QWORD *)ptr + 15);
if ( v3 )
free(v3);
v4 = (void *)*((_QWORD *)ptr + 16);
if ( v4 )
free(v4);
if ( *((int *)ptr + 34) > 0 )
{
v5 = 0LL;
do
wkExerciseFreeDeep(*(void **)(*((_QWORD *)ptr + 18) + 8 * v5++));
while ( *((_DWORD *)ptr + 34) > (int)v5 );
}
free(*((void **)ptr + 18));
free(ptr);
}
}

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

if ( ptr )
{
v2 = (void *)*((_QWORD *)ptr + 14);
if ( v2 )
free(v2);
v3 = (void *)*((_QWORD *)ptr + 15);
if ( v3 )
free(v3);
v4 = (void *)*((_QWORD *)ptr + 16);
if ( v4 )
free(v4);
if ( *((int *)ptr + 34) > 0 )
{
v5 = 0LL;
do
func3(*(void **)(*((_QWORD *)ptr + 18) + 8 * v5++));
while ( *((_DWORD *)ptr + 34) > (int)v5 );
}
free(*((void **)ptr + 18));
free(ptr);
}
}

original_code:
void wkWorkoutFreeDeep(wkWorkout* w) {
 if (w) {
  if (w->mood) free(w->mood);
  if (w->cardio) free(w->cardio);
  if (w->comments) free(w->comments);
  for (int i = 0; i < w->num_exercises; i++)
   wkExerciseFreeDeep(w->exercises[i]);
  free(w->exercises);
  free(w);
 }
}

canonical_original_code:
void wkWorkoutFreeDeep(struct wkWorkout *w) {
 if (w) {
  if (w->mood) free(w->mood);
  if (w->cardio) free(w->cardio);
  if (w->comments) free(w->comments);
  for (int i = 0; i < w->num_exercises; i++)
   wkExerciseFreeDeep(w->exercises[i]);
  free(w->exercises);
  free(w);
 }
}


repo: matthewdalton/arbint

original_code:
int
ai_get_hsb_position(aibase_t val)
{
  int bit = 0;
  unsigned short val_short;
  unsigned char val_byte;
  unsigned char val_nibble;
  unsigned char mask;
  if (val == 0) {
    return 0;
  }
  if (val & 0xFFFF0000) {
    bit += 16;
    val_short = (unsigned short)(val >> 16);
  }
  else {
    val_short = (unsigned short)val;
  }
  if (val_short & 0xFF00) {
    bit += 8;
    val_byte = (unsigned char)(val_short >> 8);
  }
  else {
    val_byte = (unsigned char)val_short;
  }
  if (val_byte & 0xF0) {
    bit += 4;
    val_nibble = (val_byte >> 4);
  }
  else {
    val_nibble = val_byte;
  }
  bit += 4;
  mask = 0x08;
  while (mask && !(val_nibble & mask)) {
    bit -= 1;
    mask >>= 1;
  }
  return bit;
}

canonical_original_code:
int ai_get_hsb_position(unsigned int val)
{
  int bit = 0;
  unsigned short val_short;
  unsigned char val_byte;
  unsigned char val_nibble;
  unsigned char mask;
  if (val == 0) {
    return 0;
  }
  if (val & 0xFFFF0000) {
    bit += 16;
    val_short = (unsigned short)(val >> 16);
  }
  else {
    val_short = (unsigned short)val;
  }
  if (val_short & 0xFF00) {
    bit += 8;
    val_byte = (unsigned char)(val_short >> 8);
  }
  else {
    val_byte = (unsigned char)val_short;
  }
  if (val_byte & 0xF0) {
    bit += 4;
    val_nibble = (val_byte >> 4);
  }
  else {
    val_nibble = val_byte;
  }
  bit += 4;
  mask = 0x08;
  while (mask && !(val_nibble & mask)) {
    bit -= 1;
    mask >>= 1;
  }
  return bit;
}

variable_types:
val
unsigned int

variable_types: bit int

variable_types:
mask
unsigned char


decompiled_code:
__int64 __fastcall arbint_get_hsb_position(__int64 *a1)
{
__int64 v1; // rdx
int v2; // ebx
__int64 v3; // rcx
__int64 v4; // rax
unsigned int v5; // edi

v1 = a1[1];
if ( !v1 )
return 0LL;
v2 = 4 * v1 - 4;
v3 = *a1;
v4 = 0LL;
while ( 1 )
{
v5 = *(_DWORD *)(v3 + 4 * v4);
if ( v5 )
break;
v2 -= 4;
if ( v1 == ++v4 )
return v5;
}
return (unsigned int)ai_get_hsb_position(v5) + v2;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 *a1)
{
__int64 v1; // rdx
int v2; // ebx
__int64 v3; // rcx
__int64 v4; // rax
unsigned int v5; // edi

v1 = a1[1];
if ( !v1 )
return 0LL;
v2 = 4 * v1 - 4;
v3 = *a1;
v4 = 0LL;
while ( 1 )
{
v5 = *(_DWORD *)(v3 + 4 * v4);
if ( v5 )
break;
v2 -= 4;
if ( v1 == ++v4 )
return v5;
}
return (unsigned int)func0(v5) + v2;
}

original_code:
aibase_t arbint_get_hsb_position(ArbInt const *ai)
{
  int i;
  int base = (ai->dataLen - 1) * sizeof(aibase_t);
  for (i = 0; i < ai->dataLen; i++) {
    if (ai->data[i] != 0) {
      return base + ai_get_hsb_position(ai->data[i]);
    }
    base -= sizeof(aibase_t);
  }
  return 0;
}

canonical_original_code:
unsigned int arbint_get_hsb_position(struct arbint *ai)
{
  int i;
  int base = (ai->dataLen - 1) * sizeof(aibase_t);
  for (i = 0; i < ai->dataLen; i++) {
    if (ai->data[i] != 0) {
      return base + ai_get_hsb_position(ai->data[i]);
    }
    base -= sizeof(aibase_t);
  }
  return 0;
}

variable_types: struct arbint { unsigned int *data; unsigned long dataLen; int sign; } *

variable_types: base int

return_type: unsigned int


decompiled_code:
__int64 test_ai_add_unsigned_with_lshift()
{
__int64 v0; // r13
__int64 v1; // r12
__int64 v2; // rbp
__int64 v3; // rbx
__int64 v4; // rbx
__int64 v5; // rax
__int64 v6; // rbp
__int64 v7; // rbx
const char *v9; // rbp
const char *v10; // rax
const char *v11; // rbp
const char *v12; // rax

v0 = AI_NewArbInt_FromLong(57005LL);
v1 = AI_NewArbInt_FromLong(200208384LL);
v2 = ai_add_unsigned_with_lshift(v0, v1, 1LL);
v3 = AI_NewArbInt_FromString("0x0BEEF0000000DEAD");
if ( !(unsigned int)AI_Equal(v2, v3) )
{
printf("TEST_EQUAL Failure (%s, line %d):\n", "test_ai_add_unsigned_with_lshift", 34LL);
v9 = (const char *)AI_ToString(v2);
v10 = (const char *)AI_ToString(v3);
printf(" expected %s, got %s\n", v10, v9);
goto LABEL_5;
}
AI_FreeArbInt(v0);
AI_FreeArbInt(v1);
AI_FreeArbInt(v2);
AI_FreeArbInt(v3);
v4 = AI_NewArbInt_FromString("0x12123434565600000000");
v5 = AI_NewArbInt_FromString("0x999988886666");
v6 = ai_add_unsigned_with_lshift(v4, v5, 2LL);
v7 = AI_NewArbInt_FromString("0x9999888878783434565600000000");
if ( !(unsigned int)AI_Equal(v6, v7) )
{
printf("TEST_EQUAL Failure (%s, line %d):\n", "test_ai_add_unsigned_with_lshift", 54LL);
v11 = (const char *)AI_ToString(v6);
v12 = (const char *)AI_ToString(v7);
printf(" expected %s, got %s\n", v12, v11);
LABEL_5:
exit(1);
}
printf("PASSED : %s\n", "test_ai_add_unsigned_with_lshift");
return 1LL;
}

canonical_decompiled_code:
__int64 func0()
{
__int64 v0; // r13
__int64 v1; // r12
__int64 v2; // rbp
__int64 v3; // rbx
__int64 v4; // rbx
__int64 v5; // rax
__int64 v6; // rbp
__int64 v7; // rbx
const char *v9; // rbp
const char *v10; // rax
const char *v11; // rbp
const char *v12; // rax

v0 = AI_NewArbInt_FromLong(57005LL);
v1 = AI_NewArbInt_FromLong(200208384LL);
v2 = ai_add_unsigned_with_lshift(v0, v1, 1LL);
v3 = AI_NewArbInt_FromString("0x0BEEF0000000DEAD");
if ( !(unsigned int)AI_Equal(v2, v3) )
{
printf("TEST_EQUAL Failure (%s, line %d):\n", "test_ai_add_unsigned_with_lshift", 34LL);
v9 = (const char *)AI_ToString(v2);
v10 = (const char *)AI_ToString(v3);
printf(" expected %s, got %s\n", v10, v9);
goto LABEL_5;
}
AI_FreeArbInt(v0);
AI_FreeArbInt(v1);
AI_FreeArbInt(v2);
AI_FreeArbInt(v3);
v4 = AI_NewArbInt_FromString("0x12123434565600000000");
v5 = AI_NewArbInt_FromString("0x999988886666");
v6 = ai_add_unsigned_with_lshift(v4, v5, 2LL);
v7 = AI_NewArbInt_FromString("0x9999888878783434565600000000");
if ( !(unsigned int)AI_Equal(v6, v7) )
{
printf("TEST_EQUAL Failure (%s, line %d):\n", "test_ai_add_unsigned_with_lshift", 54LL);
v11 = (const char *)AI_ToString(v6);
v12 = (const char *)AI_ToString(v7);
printf(" expected %s, got %s\n", v12, v11);
LABEL_5:
exit(1);
}
printf("PASSED : %s\n", "test_ai_add_unsigned_with_lshift");
return 1LL;
}

original_code:
int test_ai_add_unsigned_with_lshift(void)
{
  ;
  ArbInt *A;
  ArbInt *B;
  ArbInt *C;
  size_t B_shift;
  ArbInt *expected;
  A = AI_NewArbInt_FromLong(0x0000DEAD);
  B = AI_NewArbInt_FromLong(0x0BEEF000);
  B_shift = 1;
  C = ai_add_unsigned_with_lshift(A, B, B_shift);
  expected = AI_NewArbInt_FromString("0x0BEEF0000000DEAD");
  if (!(AI_Equal)((C), (expected))) { printf("TEST_EQUAL Failure (%s, line %d):\n", __func__, 34); printf("    expected %s, got %s\n", (AI_ToString)((expected)), (AI_ToString)((C))); goto test_exit; };
  AI_FreeArbInt(A);
  AI_FreeArbInt(B);
  AI_FreeArbInt(C);
  AI_FreeArbInt(expected);
  A = AI_NewArbInt_FromString("0x12123434565600000000");
  B = AI_NewArbInt_FromString("0x999988886666");
  B_shift = 2;
  C = ai_add_unsigned_with_lshift(A, B, B_shift);
  expected = AI_NewArbInt_FromString("0x9999888878783434565600000000");
  if (!(AI_Equal)((C), (expected))) { printf("TEST_EQUAL Failure (%s, line %d):\n", __func__, 54); printf("    expected %s, got %s\n", (AI_ToString)((expected)), (AI_ToString)((C))); goto test_exit; };
  printf("PASSED : %s\n", __func__); return 1; test_exit: exit(1);
}

canonical_original_code:
int test_ai_add_unsigned_with_lshift()
{
  ;
  struct arbint *A;
  struct arbint *B;
  struct arbint *C;
  unsigned long B_shift;
  struct arbint *expected;
  A = AI_NewArbInt_FromLong(0x0000DEAD);
  B = AI_NewArbInt_FromLong(0x0BEEF000);
  B_shift = 1;
  C = ai_add_unsigned_with_lshift(A, B, B_shift);
  expected = AI_NewArbInt_FromString("0x0BEEF0000000DEAD");
  if (!(AI_Equal)((C), (expected))) { printf("TEST_EQUAL Failure (%s, line %d):\n", __func__, 34); printf("    expected %s, got %s\n", (AI_ToString)((expected)), (AI_ToString)((C))); goto test_exit; };
  AI_FreeArbInt(A);
  AI_FreeArbInt(B);
  AI_FreeArbInt(C);
  AI_FreeArbInt(expected);
  A = AI_NewArbInt_FromString("0x12123434565600000000");
  B = AI_NewArbInt_FromString("0x999988886666");
  B_shift = 2;
  C = ai_add_unsigned_with_lshift(A, B, B_shift);
  expected = AI_NewArbInt_FromString("0x9999888878783434565600000000");
  if (!(AI_Equal)((C), (expected))) { printf("TEST_EQUAL Failure (%s, line %d):\n", __func__, 54); printf("    expected %s, got %s\n", (AI_ToString)((expected)), (AI_ToString)((C))); goto test_exit; };
  printf("PASSED : %s\n", __func__); return 1; test_exit: exit(1);
}

user_defined_types: struct arbint { unsigned int *data; unsigned long dataLen; int sign; }


decompiled_code:
__int64 test_ai_div_unsigned_by_subtraction__even_divide()
{
__int64 v0; // r14
__int64 v1; // r13
__int64 v2; // rbp
__int64 v3; // rbx
__int64 v4; // r12
const char *v6; // rbp
const char *v7; // rax
const char *v8; // rbx
const char *v9; // rax
__int64 v10[6]; // [rsp+8h] [rbp-30h] BYREF

v0 = AI_NewArbInt_FromLong(70LL);
v1 = AI_NewArbInt_FromLong(5LL);
v2 = ai_div_unsigned_by_subtraction(v0, v1, v10);
v3 = AI_NewArbInt_FromLong(14LL);
v4 = AI_NewArbInt_FromLong(0LL);
if ( !(unsigned int)AI_Equal(v2, v3) )
{
printf("TEST_EQUAL Failure (%s, line %d):\n", "test_ai_div_unsigned_by_subtraction__even_divide", 71LL);
v6 = (const char *)AI_ToString(v2);
v7 = (const char *)AI_ToString(v3);
printf(" expected %s, got %s\n", v7, v6);
goto LABEL_5;
}
if ( !(unsigned int)AI_Equal(v10[0], v4) )
{
printf("TEST_EQUAL Failure (%s, line %d):\n", "test_ai_div_unsigned_by_subtraction__even_divide", 72LL);
v8 = (const char *)AI_ToString(v10[0]);
v9 = (const char *)AI_ToString(v4);
printf(" expected %s, got %s\n", v9, v8);
LABEL_5:
exit(1);
}
AI_FreeArbInt(v0);
AI_FreeArbInt(v1);
AI_FreeArbInt(v2);
AI_FreeArbInt(v10[0]);
AI_FreeArbInt(v3);
AI_FreeArbInt(v4);
printf("PASSED : %s\n", "test_ai_div_unsigned_by_subtraction__even_divide");
return 1LL;
}

canonical_decompiled_code:
__int64 func1()
{
__int64 v0; // r14
__int64 v1; // r13
__int64 v2; // rbp
__int64 v3; // rbx
__int64 v4; // r12
const char *v6; // rbp
const char *v7; // rax
const char *v8; // rbx
const char *v9; // rax
__int64 v10[6]; // [rsp+8h] [rbp-30h] BYREF

v0 = AI_NewArbInt_FromLong(70LL);
v1 = AI_NewArbInt_FromLong(5LL);
v2 = ai_div_unsigned_by_subtraction(v0, v1, v10);
v3 = AI_NewArbInt_FromLong(14LL);
v4 = AI_NewArbInt_FromLong(0LL);
if ( !(unsigned int)AI_Equal(v2, v3) )
{
printf("TEST_EQUAL Failure (%s, line %d):\n", "test_ai_div_unsigned_by_subtraction__even_divide", 71LL);
v6 = (const char *)AI_ToString(v2);
v7 = (const char *)AI_ToString(v3);
printf(" expected %s, got %s\n", v7, v6);
goto LABEL_5;
}
if ( !(unsigned int)AI_Equal(v10[0], v4) )
{
printf("TEST_EQUAL Failure (%s, line %d):\n", "test_ai_div_unsigned_by_subtraction__even_divide", 72LL);
v8 = (const char *)AI_ToString(v10[0]);
v9 = (const char *)AI_ToString(v4);
printf(" expected %s, got %s\n", v9, v8);
LABEL_5:
exit(1);
}
AI_FreeArbInt(v0);
AI_FreeArbInt(v1);
AI_FreeArbInt(v2);
AI_FreeArbInt(v10[0]);
AI_FreeArbInt(v3);
AI_FreeArbInt(v4);
printf("PASSED : %s\n", "test_ai_div_unsigned_by_subtraction__even_divide");
return 1LL;
}

original_code:
int test_ai_div_unsigned_by_subtraction__even_divide(void)
{
  ;
  ArbInt *A = AI_NewArbInt_FromLong(70);
  ArbInt *B = AI_NewArbInt_FromLong(5);
  ArbInt *remainder;
  ArbInt *expected_div;
  ArbInt *expected_mod;
  ArbInt *C = ai_div_unsigned_by_subtraction(A, B, &remainder);
  expected_div = AI_NewArbInt_FromLong(14);
  expected_mod = AI_NewArbInt_FromLong(0);
  if (!(AI_Equal)((C), (expected_div))) { printf("TEST_EQUAL Failure (%s, line %d):\n", __func__, 71); printf("    expected %s, got %s\n", (AI_ToString)((expected_div)), (AI_ToString)((C))); goto test_exit; };
  if (!(AI_Equal)((remainder), (expected_mod))) { printf("TEST_EQUAL Failure (%s, line %d):\n", __func__, 72); printf("    expected %s, got %s\n", (AI_ToString)((expected_mod)), (AI_ToString)((remainder))); goto test_exit; };
  AI_FreeArbInt(A);
  AI_FreeArbInt(B);
  AI_FreeArbInt(C);
  AI_FreeArbInt(remainder);
  AI_FreeArbInt(expected_div);
  AI_FreeArbInt(expected_mod);
  printf("PASSED : %s\n", __func__); return 1; test_exit: exit(1);
}

canonical_original_code:
int test_ai_div_unsigned_by_subtraction__even_divide()
{
  ;
  struct arbint *A = AI_NewArbInt_FromLong(70);
  struct arbint *B = AI_NewArbInt_FromLong(5);
  struct arbint *remainder;
  struct arbint *expected_div;
  struct arbint *expected_mod;
  struct arbint *C = ai_div_unsigned_by_subtraction(A, B, &remainder);
  expected_div = AI_NewArbInt_FromLong(14);
  expected_mod = AI_NewArbInt_FromLong(0);
  if (!(AI_Equal)((C), (expected_div))) { printf("TEST_EQUAL Failure (%s, line %d):\n", __func__, 71); printf("    expected %s, got %s\n", (AI_ToString)((expected_div)), (AI_ToString)((C))); goto test_exit; };
  if (!(AI_Equal)((remainder), (expected_mod))) { printf("TEST_EQUAL Failure (%s, line %d):\n", __func__, 72); printf("    expected %s, got %s\n", (AI_ToString)((expected_mod)), (AI_ToString)((remainder))); goto test_exit; };
  AI_FreeArbInt(A);
  AI_FreeArbInt(B);
  AI_FreeArbInt(C);
  AI_FreeArbInt(remainder);
  AI_FreeArbInt(expected_div);
  AI_FreeArbInt(expected_mod);
  printf("PASSED : %s\n", __func__); return 1; test_exit: exit(1);
}


decompiled_code:
__int64 test_ai_div_unsigned_by_subtraction__zero_result()
{
__int64 v0; // r14
__int64 v1; // r13
__int64 v2; // rbp
__int64 v3; // rbx
__int64 v4; // r12
const char *v6; // rbp
const char *v7; // rax
const char *v8; // rbx
const char *v9; // rax
__int64 v10[6]; // [rsp+8h] [rbp-30h] BYREF

v0 = AI_NewArbInt_FromLong(5LL);
v1 = AI_NewArbInt_FromLong(12LL);
v2 = ai_div_unsigned_by_subtraction(v0, v1, v10);
v3 = AI_NewArbInt_FromLong(0LL);
v4 = AI_NewArbInt_FromLong(5LL);
if ( !(unsigned int)AI_Equal(v2, v3) )
{
printf("TEST_EQUAL Failure (%s, line %d):\n", "test_ai_div_unsigned_by_subtraction__zero_result", 96LL);
v6 = (const char *)AI_ToString(v2);
v7 = (const char *)AI_ToString(v3);
printf(" expected %s, got %s\n", v7, v6);
goto LABEL_5;
}
if ( !(unsigned int)AI_Equal(v10[0], v4) )
{
printf("TEST_EQUAL Failure (%s, line %d):\n", "test_ai_div_unsigned_by_subtraction__zero_result", 97LL);
v8 = (const char *)AI_ToString(v10[0]);
v9 = (const char *)AI_ToString(v4);
printf(" expected %s, got %s\n", v9, v8);
LABEL_5:
exit(1);
}
AI_FreeArbInt(v0);
AI_FreeArbInt(v1);
AI_FreeArbInt(v2);
AI_FreeArbInt(v10[0]);
AI_FreeArbInt(v3);
AI_FreeArbInt(v4);
printf("PASSED : %s\n", "test_ai_div_unsigned_by_subtraction__zero_result");
return 1LL;
}

canonical_decompiled_code:
__int64 func2()
{
__int64 v0; // r14
__int64 v1; // r13
__int64 v2; // rbp
__int64 v3; // rbx
__int64 v4; // r12
const char *v6; // rbp
const char *v7; // rax
const char *v8; // rbx
const char *v9; // rax
__int64 v10[6]; // [rsp+8h] [rbp-30h] BYREF

v0 = AI_NewArbInt_FromLong(5LL);
v1 = AI_NewArbInt_FromLong(12LL);
v2 = ai_div_unsigned_by_subtraction(v0, v1, v10);
v3 = AI_NewArbInt_FromLong(0LL);
v4 = AI_NewArbInt_FromLong(5LL);
if ( !(unsigned int)AI_Equal(v2, v3) )
{
printf("TEST_EQUAL Failure (%s, line %d):\n", "test_ai_div_unsigned_by_subtraction__zero_result", 96LL);
v6 = (const char *)AI_ToString(v2);
v7 = (const char *)AI_ToString(v3);
printf(" expected %s, got %s\n", v7, v6);
goto LABEL_5;
}
if ( !(unsigned int)AI_Equal(v10[0], v4) )
{
printf("TEST_EQUAL Failure (%s, line %d):\n", "test_ai_div_unsigned_by_subtraction__zero_result", 97LL);
v8 = (const char *)AI_ToString(v10[0]);
v9 = (const char *)AI_ToString(v4);
printf(" expected %s, got %s\n", v9, v8);
LABEL_5:
exit(1);
}
AI_FreeArbInt(v0);
AI_FreeArbInt(v1);
AI_FreeArbInt(v2);
AI_FreeArbInt(v10[0]);
AI_FreeArbInt(v3);
AI_FreeArbInt(v4);
printf("PASSED : %s\n", "test_ai_div_unsigned_by_subtraction__zero_result");
return 1LL;
}

original_code:
int test_ai_div_unsigned_by_subtraction__zero_result(void)
{
  ;
  ArbInt *A = AI_NewArbInt_FromLong(5);
  ArbInt *B = AI_NewArbInt_FromLong(12);
  ArbInt *remainder;
  ArbInt *expected_div;
  ArbInt *expected_mod;
  ArbInt *C = ai_div_unsigned_by_subtraction(A, B, &remainder);
  expected_div = AI_NewArbInt_FromLong(0);
  expected_mod = AI_NewArbInt_FromLong(5);
  if (!(AI_Equal)((C), (expected_div))) { printf("TEST_EQUAL Failure (%s, line %d):\n", __func__, 96); printf("    expected %s, got %s\n", (AI_ToString)((expected_div)), (AI_ToString)((C))); goto test_exit; };
  if (!(AI_Equal)((remainder), (expected_mod))) { printf("TEST_EQUAL Failure (%s, line %d):\n", __func__, 97); printf("    expected %s, got %s\n", (AI_ToString)((expected_mod)), (AI_ToString)((remainder))); goto test_exit; };
  AI_FreeArbInt(A);
  AI_FreeArbInt(B);
  AI_FreeArbInt(C);
  AI_FreeArbInt(remainder);
  AI_FreeArbInt(expected_div);
  AI_FreeArbInt(expected_mod);
  printf("PASSED : %s\n", __func__); return 1; test_exit: exit(1);
}

canonical_original_code:
int test_ai_div_unsigned_by_subtraction__zero_result()
{
  ;
  struct arbint *A = AI_NewArbInt_FromLong(5);
  struct arbint *B = AI_NewArbInt_FromLong(12);
  struct arbint *remainder;
  struct arbint *expected_div;
  struct arbint *expected_mod;
  struct arbint *C = ai_div_unsigned_by_subtraction(A, B, &remainder);
  expected_div = AI_NewArbInt_FromLong(0);
  expected_mod = AI_NewArbInt_FromLong(5);
  if (!(AI_Equal)((C), (expected_div))) { printf("TEST_EQUAL Failure (%s, line %d):\n", __func__, 96); printf("    expected %s, got %s\n", (AI_ToString)((expected_div)), (AI_ToString)((C))); goto test_exit; };
  if (!(AI_Equal)((remainder), (expected_mod))) { printf("TEST_EQUAL Failure (%s, line %d):\n", __func__, 97); printf("    expected %s, got %s\n", (AI_ToString)((expected_mod)), (AI_ToString)((remainder))); goto test_exit; };
  AI_FreeArbInt(A);
  AI_FreeArbInt(B);
  AI_FreeArbInt(C);
  AI_FreeArbInt(remainder);
  AI_FreeArbInt(expected_div);
  AI_FreeArbInt(expected_mod);
  printf("PASSED : %s\n", __func__); return 1; test_exit: exit(1);
}


decompiled_code:
__int64 test_AI_Greater()
{
__int64 v0; // rbx
__int64 v1; // rbp
__int64 v2; // r12

v0 = AI_NewArbInt_FromLong(4294967291LL);
v1 = AI_NewArbInt_FromLong(0LL);
v2 = AI_NewArbInt_FromLong(6LL);
if ( !(unsigned int)AI_Greater(v1, v0) )
{
printf("TEST_TRUE Failure (%s, %d): %s\n", "test_AI_Greater", 115LL, "AI_Greater(zero, negative)");
goto LABEL_6;
}
if ( !(unsigned int)AI_Greater(v2, v1) )
{
printf("TEST_TRUE Failure (%s, %d): %s\n", "test_AI_Greater", 116LL, "AI_Greater(positive, zero)");
goto LABEL_6;
}
if ( !(unsigned int)AI_Greater(v2, v0) )
{
printf("TEST_TRUE Failure (%s, %d): %s\n", "test_AI_Greater", 117LL, "AI_Greater(positive, negative)");
LABEL_6:
exit(1);
}
printf("PASSED : %s\n", "test_AI_Greater");
return 1LL;
}

canonical_decompiled_code:
__int64 func3()
{
__int64 v0; // rbx
__int64 v1; // rbp
__int64 v2; // r12

v0 = AI_NewArbInt_FromLong(4294967291LL);
v1 = AI_NewArbInt_FromLong(0LL);
v2 = AI_NewArbInt_FromLong(6LL);
if ( !(unsigned int)AI_Greater(v1, v0) )
{
printf("TEST_TRUE Failure (%s, %d): %s\n", "test_AI_Greater", 115LL, "AI_Greater(zero, negative)");
goto LABEL_6;
}
if ( !(unsigned int)AI_Greater(v2, v1) )
{
printf("TEST_TRUE Failure (%s, %d): %s\n", "test_AI_Greater", 116LL, "AI_Greater(positive, zero)");
goto LABEL_6;
}
if ( !(unsigned int)AI_Greater(v2, v0) )
{
printf("TEST_TRUE Failure (%s, %d): %s\n", "test_AI_Greater", 117LL, "AI_Greater(positive, negative)");
LABEL_6:
exit(1);
}
printf("PASSED : %s\n", "test_AI_Greater");
return 1LL;
}

original_code:
int test_AI_Greater(void) {
  ;
  ArbInt *negative = AI_NewArbInt_FromLong(-5);
  ArbInt *zero = AI_NewArbInt_FromLong(0);
  ArbInt *positive = AI_NewArbInt_FromLong(6);
  if (!(AI_Greater(zero, negative))) { printf("TEST_TRUE Failure (%s, %d): %s\n", __func__, 115, "AI_Greater(zero, negative)"); goto test_exit; };
  if (!(AI_Greater(positive, zero))) { printf("TEST_TRUE Failure (%s, %d): %s\n", __func__, 116, "AI_Greater(positive, zero)"); goto test_exit; };
  if (!(AI_Greater(positive, negative))) { printf("TEST_TRUE Failure (%s, %d): %s\n", __func__, 117, "AI_Greater(positive, negative)"); goto test_exit; };
  printf("PASSED : %s\n", __func__); return 1; test_exit: exit(1);
}

canonical_original_code:
int test_AI_Greater() {
  ;
  struct arbint *negative = AI_NewArbInt_FromLong(-5);
  struct arbint *zero = AI_NewArbInt_FromLong(0);
  struct arbint *positive = AI_NewArbInt_FromLong(6);
  if (!(AI_Greater(zero, negative))) { printf("TEST_TRUE Failure (%s, %d): %s\n", __func__, 115, "AI_Greater(zero, negative)"); goto test_exit; };
  if (!(AI_Greater(positive, zero))) { printf("TEST_TRUE Failure (%s, %d): %s\n", __func__, 116, "AI_Greater(positive, zero)"); goto test_exit; };
  if (!(AI_Greater(positive, negative))) { printf("TEST_TRUE Failure (%s, %d): %s\n", __func__, 117, "AI_Greater(positive, negative)"); goto test_exit; };
  printf("PASSED : %s\n", __func__); return 1; test_exit: exit(1);
}


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

result = test_AI_Greater();
if ( (_DWORD)result )
{
result = test_ai_add_unsigned_with_lshift();
if ( (_DWORD)result )
{
result = test_ai_div_unsigned_by_subtraction__zero_result();
if ( (_DWORD)result )
result = (unsigned int)test_ai_div_unsigned_by_subtraction__even_divide() != 0;
}
}
return result;
}

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

result = func3();
if ( (_DWORD)result )
{
result = func0();
if ( (_DWORD)result )
{
result = func2();
if ( (_DWORD)result )
result = (unsigned int)func1() != 0;
}
}
return result;
}

original_code:
int test_all(void)
{
  return
    test_AI_Greater() &&
    test_ai_add_unsigned_with_lshift() &&
    test_ai_div_unsigned_by_subtraction__zero_result() &&
    test_ai_div_unsigned_by_subtraction__even_divide() &&
    1;
}

canonical_original_code:
int test_all()
{
  return
    test_AI_Greater() &&
    test_ai_add_unsigned_with_lshift() &&
    test_ai_div_unsigned_by_subtraction__zero_result() &&
    test_ai_div_unsigned_by_subtraction__even_divide() &&
    1;
}


decompiled_code:
__int64 __fastcall next_iteration(__int64 a1, __int64 a2)
{
__int64 v2; // rbx
__int64 v3; // rbp
__int64 v4; // rbx
__int64 v5; // r12
__int64 v7[4]; // [rsp+8h] [rbp-20h] BYREF

v2 = AI_Div(a2, a1, v7);
AI_FreeArbInt(v7[0]);
v3 = AI_Add(a1, v2);
AI_FreeArbInt(v2);
v4 = AI_NewArbInt_FromLong(2LL);
v5 = AI_Div(v3, v4, v7);
AI_FreeArbInt(v3);
AI_FreeArbInt(v4);
AI_FreeArbInt(v7[0]);
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
__int64 v2; // rbx
__int64 v3; // rbp
__int64 v4; // rbx
__int64 v5; // r12
__int64 v7[4]; // [rsp+8h] [rbp-20h] BYREF

v2 = AI_Div(a2, a1, v7);
AI_FreeArbInt(v7[0]);
v3 = AI_Add(a1, v2);
AI_FreeArbInt(v2);
v4 = AI_NewArbInt_FromLong(2LL);
v5 = AI_Div(v3, v4, v7);
AI_FreeArbInt(v3);
AI_FreeArbInt(v4);
AI_FreeArbInt(v7[0]);
return v5;
}

original_code:
ArbInt *next_iteration(const ArbInt *curr, const ArbInt *n)
{
  ArbInt *rem;
  ArbInt *divResult = AI_Div(n, curr, &rem);
  AI_FreeArbInt(rem);
  ArbInt *addResult = AI_Add(curr, divResult);
  AI_FreeArbInt(divResult);
  ArbInt *two = AI_NewArbInt_FromLong(2);
  ArbInt *next = AI_Div(addResult, two, &rem);
  AI_FreeArbInt(addResult);
  AI_FreeArbInt(two);
  AI_FreeArbInt(rem);
  return next;
}

canonical_original_code:
struct arbint *next_iteration(struct arbint *curr, struct arbint *n)
{
  struct arbint *rem;
  struct arbint *divResult = AI_Div(n, curr, &rem);
  AI_FreeArbInt(rem);
  struct arbint *addResult = AI_Add(curr, divResult);
  AI_FreeArbInt(divResult);
  struct arbint *two = AI_NewArbInt_FromLong(2);
  struct arbint *next = AI_Div(addResult, two, &rem);
  AI_FreeArbInt(addResult);
  AI_FreeArbInt(two);
  AI_FreeArbInt(rem);
  return next;
}

variable_types:
curr
struct arbint *

variable_types:
n
struct arbint *

return_type: struct arbint { unsigned int *data; unsigned long dataLen; int sign; } *


decompiled_code:
__int64 __fastcall AI_isqrt(__int64 a1)
{
__int64 v1; // rbx
__int64 i; // rbp
__int64 v3; // rbx
__int64 v4; // r12

v1 = AI_NewArbInt_From32(0LL);
if ( (unsigned int)AI_Less(a1, v1) )
{
AI_FreeArbInt(v1);
v3 = 0LL;
}
else
{
AI_FreeArbInt(v1);
for ( i = AI_NewArbInt_FromCopy(a1); ; i = v3 )
{
v3 = next_iteration(i, a1);
v4 = AI_Add_Value(i, 1LL, 1LL);
if ( (unsigned int)AI_Equal(v3, i) || (unsigned int)AI_Equal(v3, v4) )
break;
AI_FreeArbInt(i);
AI_FreeArbInt(v4);
}
AI_FreeArbInt(i);
AI_FreeArbInt(v4);
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
__int64 v1; // rbx
__int64 i; // rbp
__int64 v3; // rbx
__int64 v4; // r12

v1 = AI_NewArbInt_From32(0LL);
if ( (unsigned int)AI_Less(a1, v1) )
{
AI_FreeArbInt(v1);
v3 = 0LL;
}
else
{
AI_FreeArbInt(v1);
for ( i = AI_NewArbInt_FromCopy(a1); ; i = v3 )
{
v3 = func0(i, a1);
v4 = AI_Add_Value(i, 1LL, 1LL);
if ( (unsigned int)AI_Equal(v3, i) || (unsigned int)AI_Equal(v3, v4) )
break;
AI_FreeArbInt(i);
AI_FreeArbInt(v4);
}
AI_FreeArbInt(i);
AI_FreeArbInt(v4);
}
return v3;
}

original_code:
ArbInt *AI_isqrt(ArbInt const *n)
{
  ArbInt *zero = AI_NewArbInt_From32(0);
  if (AI_Less(n, zero)) {
    AI_FreeArbInt(zero);
    return ((void *)0);
  }
  AI_FreeArbInt(zero);
  ArbInt *k = AI_NewArbInt_FromCopy(n);
  ArbInt *next_k;
  ArbInt *k_plus_one = ((void *)0);
  for (;;) {
    next_k = next_iteration(k, n);
    k_plus_one = AI_Add_Value(k, 1, 1);
    if (AI_Equal(next_k, k) || AI_Equal(next_k, k_plus_one)) {
      break;
    }
    AI_FreeArbInt(k);
    AI_FreeArbInt(k_plus_one);
    k = next_k;
  }
  AI_FreeArbInt(k);
  AI_FreeArbInt(k_plus_one);
  return next_k;
}

canonical_original_code:
struct arbint *AI_isqrt(struct arbint *n)
{
  struct arbint *zero = AI_NewArbInt_From32(0);
  if (AI_Less(n, zero)) {
    AI_FreeArbInt(zero);
    return ((void *)0);
  }
  AI_FreeArbInt(zero);
  struct arbint *k = AI_NewArbInt_FromCopy(n);
  struct arbint *next_k;
  struct arbint *k_plus_one = ((void *)0);
  for (;;) {
    next_k = next_iteration(k, n);
    k_plus_one = AI_Add_Value(k, 1, 1);
    if (AI_Equal(next_k, k) || AI_Equal(next_k, k_plus_one)) {
      break;
    }
    AI_FreeArbInt(k);
    AI_FreeArbInt(k_plus_one);
    k = next_k;
  }
  AI_FreeArbInt(k);
  AI_FreeArbInt(k_plus_one);
  return next_k;
}


decompiled_code:
void *__fastcall AI_malloc(size_t a1)
{
return malloc(a1);
}

canonical_decompiled_code:
void *__fastcall func0(size_t a1)
{
return malloc(a1);
}

original_code:
void *AI_malloc(size_t size)
{
  return malloc(size);
}

canonical_original_code:
void *AI_malloc(unsigned long size)
{
  return malloc(size);
}

variable_types:
size
unsigned long


decompiled_code:
void __fastcall AI_free(void *a1)
{
free(a1);
}

canonical_decompiled_code:
void __fastcall func1(void *a1)
{
free(a1);
}

original_code:
void AI_free(void *mem)
{
  free(mem);
}

canonical_original_code:
void AI_free(void *mem)
{
  free(mem);
}

variable_types:
mem
void *


decompiled_code:
void *__fastcall AI_memcpy(void *a1, const void *a2, size_t a3)
{
return memcpy(a1, a2, a3);
}

canonical_decompiled_code:
void *__fastcall func2(void *a1, const void *a2, size_t a3)
{
return memcpy(a1, a2, a3);
}

original_code:
void *AI_memcpy(void *dest, void const *src, size_t size)
{
  return memcpy(dest, src, size);
}

canonical_original_code:
void *AI_memcpy(void *dest, void *src, unsigned long size)
{
  return memcpy(dest, src, size);
}

variable_types:
dest
void *

variable_types:
src
void *


decompiled_code:
void *__fastcall AI_memset(void *a1, int a2, size_t a3)
{
return memset(a1, a2, a3);
}

canonical_decompiled_code:
void *__fastcall func3(void *a1, int a2, size_t a3)
{
return memset(a1, a2, a3);
}

original_code:
void *AI_memset(void *dest, int val, size_t len)
{
  return memset(dest, val, len);
}

canonical_original_code:
void *AI_memset(void *dest, int val, unsigned long len)
{
  return memset(dest, val, len);
}

variable_types: val int

variable_types:
len
unsigned long


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

result = AI_malloc(24LL);
if ( result )
{
*(_QWORD *)result = 0LL;
*(_QWORD *)(result + 8) = 0LL;
*(_DWORD *)(result + 16) = 1;
}
return result;
}

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

result = AI_malloc(24LL);
if ( result )
{
*(_QWORD *)result = 0LL;
*(_QWORD *)(result + 8) = 0LL;
*(_DWORD *)(result + 16) = 1;
}
return result;
}

original_code:
static
ArbInt *ai_new_empty(void)
{
  ArbInt *ai = AI_malloc(sizeof(ArbInt));
  if (ai != ((void *)0)) {
    ai->data = ((void *)0);
    ai->dataLen = 0;
    ai->sign = 1;
  }
  return ai;
}

canonical_original_code:
static struct arbint *ai_new_empty()
{
  struct arbint *ai = AI_malloc(sizeof(ArbInt));
  if (ai != ((void *)0)) {
    ai->data = ((void *)0);
    ai->dataLen = 0;
    ai->sign = 1;
  }
  return ai;
}