decompiled_code:
__int64 __fastcall TFTP_NewReadRequest(__int64 a1)
{
int v2; // ebp
__int16 v3; // r12
__int16 i; // r13
int v6; // eax
int v7; // eax
int v8; // edi
int v9; // eax
int v10; // edi
int v11; // eax
socklen_t addr_len; // [rsp+Ch] [rbp-34Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-348h] BYREF
char v14[4]; // [rsp+90h] [rbp-2C8h] BYREF
char buf[520]; // [rsp+94h] [rbp-2C4h] BYREF
char v16; // [rsp+29Ch] [rbp-BCh] BYREF
char v17; // [rsp+29Dh] [rbp-BBh]
char v18; // [rsp+29Eh] [rbp-BAh]
char v19; // [rsp+29Fh] [rbp-B9h]
struct timeval timeout; // [rsp+2A0h] [rbp-B8h] BYREF
fd_set readfds; // [rsp+2B0h] [rbp-A8h] BYREF

v2 = 0;
v3 = 1;
for ( i = 0; ; i = v19 | (unsigned __int16)(v18 << 8) )
{
while ( 1 )
{
if ( i != v3 )
{
v14[0] = 0;
v14[1] = 3;
v14[2] = HIBYTE(v3);
v14[3] = v3;
v6 = read(*(_DWORD *)(a1 + 276), buf, 0x200uLL);
if ( v6 < 0 )
{
syslog(3, "%s: Read error: %d", (const char *)(a1 + 16), (unsigned int)v6);
return 0LL;
}
*(_DWORD *)(a1 + 288) += v6;
v2 = v6 + 4;
++v3;
}
v7 = sendto(*(_DWORD *)a1, v14, v2, 0, *(const struct sockaddr **)(a1 + 8), 0x10u);
if ( v7 < 0 )
{
syslog(3, "%s: sendto: %d", (const char *)(a1 + 16), (unsigned int)v7);
return 0LL;
}
memset(&readfds, 0, sizeof(readfds));
v8 = *(_DWORD *)a1;
v9 = *(_DWORD *)a1 + 63;
if ( *(int *)a1 >= 0 )
v9 = *(_DWORD *)a1;
readfds.fds_bits[v9 >> 6] |= 1LL << (v8 % 64);
timeout.tv_sec = 5LL;
timeout.tv_usec = 0LL;
if ( select(v8 + 1, &readfds, 0LL, 0LL, &timeout) > 0 )
{
v10 = *(_DWORD *)a1;
v11 = *(_DWORD *)a1 + 63;
if ( *(int *)a1 >= 0 )
v11 = *(_DWORD *)a1;
if ( (readfds.fds_bits[v11 >> 6] & (1LL << (v10 % 64))) != 0 )
break;
}
LABEL_8:
if ( (int)RunErrorHandler(a1) < 0 )
return 0LL;
}
addr_len = 16;
recvfrom(v10, &v16, 4uLL, 0, &addr, &addr_len);
if ( v17 != 4 )
break;
if ( v2 <= 515 )
{
PrintTransactionTime(a1);
return 0LL;
}
}
if ( v17 != 5 )
goto LABEL_8;
syslog(3, "%s: Error %d: %s", (const char *)(a1 + 16), (unsigned int)v19, (const char *)&timeout);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
int v2; // ebp
__int16 v3; // r12
__int16 i; // r13
int v6; // eax
int v7; // eax
int v8; // edi
int v9; // eax
int v10; // edi
int v11; // eax
socklen_t addr_len; // [rsp+Ch] [rbp-34Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-348h] BYREF
char v14[4]; // [rsp+90h] [rbp-2C8h] BYREF
char buf[520]; // [rsp+94h] [rbp-2C4h] BYREF
char v16; // [rsp+29Ch] [rbp-BCh] BYREF
char v17; // [rsp+29Dh] [rbp-BBh]
char v18; // [rsp+29Eh] [rbp-BAh]
char v19; // [rsp+29Fh] [rbp-B9h]
struct timeval timeout; // [rsp+2A0h] [rbp-B8h] BYREF
fd_set readfds; // [rsp+2B0h] [rbp-A8h] BYREF

v2 = 0;
v3 = 1;
for ( i = 0; ; i = v19 | (unsigned __int16)(v18 << 8) )
{
while ( 1 )
{
if ( i != v3 )
{
v14[0] = 0;
v14[1] = 3;
v14[2] = HIBYTE(v3);
v14[3] = v3;
v6 = read(*(_DWORD *)(a1 + 276), buf, 0x200uLL);
if ( v6 < 0 )
{
syslog(3, "%s: Read error: %d", (const char *)(a1 + 16), (unsigned int)v6);
return 0LL;
}
*(_DWORD *)(a1 + 288) += v6;
v2 = v6 + 4;
++v3;
}
v7 = sendto(*(_DWORD *)a1, v14, v2, 0, *(const struct sockaddr **)(a1 + 8), 0x10u);
if ( v7 < 0 )
{
syslog(3, "%s: sendto: %d", (const char *)(a1 + 16), (unsigned int)v7);
return 0LL;
}
memset(&readfds, 0, sizeof(readfds));
v8 = *(_DWORD *)a1;
v9 = *(_DWORD *)a1 + 63;
if ( *(int *)a1 >= 0 )
v9 = *(_DWORD *)a1;
readfds.fds_bits[v9 >> 6] |= 1LL << (v8 % 64);
timeout.tv_sec = 5LL;
timeout.tv_usec = 0LL;
if ( select(v8 + 1, &readfds, 0LL, 0LL, &timeout) > 0 )
{
v10 = *(_DWORD *)a1;
v11 = *(_DWORD *)a1 + 63;
if ( *(int *)a1 >= 0 )
v11 = *(_DWORD *)a1;
if ( (readfds.fds_bits[v11 >> 6] & (1LL << (v10 % 64))) != 0 )
break;
}
LABEL_8:
if ( (int)func0(a1) < 0 )
return 0LL;
}
addr_len = 16;
recvfrom(v10, &v16, 4uLL, 0, &addr, &addr_len);
if ( v17 != 4 )
break;
if ( v2 <= 515 )
{
func2(a1);
return 0LL;
}
}
if ( v17 != 5 )
goto LABEL_8;
syslog(3, "%s: Error %d: %s", (const char *)(a1 + 16), (unsigned int)v19, (const char *)&timeout);
return 0LL;
}

original_code:
int TFTP_NewReadRequest(struct Transaction *trans, char *data)
{
  fd_set readFD;
  struct timeval timeout;
  char rec_buff[(2+2)], send_buff[(512+2+2)];
  uint16_t packet_block = 0, last_block = 1;
  int packet_length = 0;
  do {
    if ( last_block != packet_block ) {
      send_buff[0] = 0;
      send_buff[1] = 3;
      send_buff[2] = (last_block / 256);
      send_buff[3] = (last_block % 256);
      packet_length = read(trans->file, send_buff+4, (512));
      if ( packet_length < 0 ) {
        syslog(3,"%s: Read error: %d", trans->client_name, packet_length );
        break;
      }
      trans->byte_count += packet_length;
      packet_length += 4;
      last_block++;
    }
    int rv = sendto(trans->socket, send_buff, packet_length, 0, (const struct sockaddr *) trans->address, sizeof(struct sockaddr));
    if ( rv < 0 ) {
      syslog(3,"%s: sendto: %d", trans->client_name, rv );
      break;
    }
    do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&readFD)->fds_bits)[0]) : "memory"); } while (0);
    ((void) (((&readFD)->fds_bits)[((trans->socket) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((trans->socket) % (8 * (int) sizeof (__fd_mask)))))));
    timeout.tv_sec = 5;
    timeout.tv_usec = 0;
    if ( select(trans->socket+1, &readFD, ((void *)0), ((void *)0), &timeout) > 0 ) {
      if ( ((((&readFD)->fds_bits)[((trans->socket) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((trans->socket) % (8 * (int) sizeof (__fd_mask)))))) != 0) ) {
        socklen_t addr_len = sizeof(struct sockaddr);
        struct sockaddr_storage their_addr;
        rv = recvfrom(trans->socket, rec_buff, (2+2), 0,(struct sockaddr *)&their_addr, &addr_len);
        int opcode = rec_buff[1];
        if ( opcode == 4 ) {
          packet_block = (rec_buff[2] * 256) | rec_buff[3];
          if ( packet_length < (512+2+2) ) {
            PrintTransactionTime(trans);
            break;
          }
          continue;
        } else if ( opcode == 5 ) {
          syslog(3,"%s: Error %d: %s", trans->client_name, rec_buff[3], rec_buff+4);
          break;
        }
      }
    }
    if ( RunErrorHandler(trans) < 0 ) {
      break;
    }
  } while ( 1 );
  return 0;
}

canonical_original_code:
int TFTP_NewReadRequest(struct Transaction *trans, char *data)
{
  struct fd_set readFD;
  struct timeval timeout;
  char rec_buff[-1]; char send_buff[-1];
  unsigned short packet_block = 0; unsigned short last_block = 1;
  int packet_length = 0;
  do {
    if ( last_block != packet_block ) {
      send_buff[0] = 0;
      send_buff[1] = 3;
      send_buff[2] = (last_block / 256);
      send_buff[3] = (last_block % 256);
      packet_length = read(trans->file, send_buff+4, (512));
      if ( packet_length < 0 ) {
        syslog(3,"%s: Read error: %d", trans->client_name, packet_length );
        break;
      }
      trans->byte_count += packet_length;
      packet_length += 4;
      last_block++;
    }
    int rv = sendto(trans->socket, send_buff, packet_length, 0, (const struct sockaddr *) trans->address, sizeof(struct sockaddr));
    if ( rv < 0 ) {
      syslog(3,"%s: sendto: %d", trans->client_name, rv );
      break;
    }
    do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&readFD)->fds_bits)[0]) : "memory"); } while (0);
    ((void) (((&readFD)->fds_bits)[((trans->socket) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((trans->socket) % (8 * (int) sizeof (__fd_mask)))))));
    timeout.tv_sec = 5;
    timeout.tv_usec = 0;
    if ( select(trans->socket+1, &readFD, ((void *)0), ((void *)0), &timeout) > 0 ) {
      if ( ((((&readFD)->fds_bits)[((trans->socket) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((trans->socket) % (8 * (int) sizeof (__fd_mask)))))) != 0) ) {
        unsigned int addr_len = sizeof(struct sockaddr);
        struct sockaddr_storage their_addr;
        rv = recvfrom(trans->socket, rec_buff, (2+2), 0,(struct sockaddr *)&their_addr, &addr_len);
        int opcode = rec_buff[1];
        if ( opcode == 4 ) {
          packet_block = (rec_buff[2] * 256) | rec_buff[3];
          if ( packet_length < (512+2+2) ) {
            PrintTransactionTime(trans);
            break;
          }
          continue;
        } else if ( opcode == 5 ) {
          syslog(3,"%s: Error %d: %s", trans->client_name, rec_buff[3], rec_buff+4);
          break;
        }
      }
    }
    if ( RunErrorHandler(trans) < 0 ) {
      break;
    }
  } while ( 1 );
  return 0;
}

variable_types:
data
char *

variable_types:
readFD
struct fd_set { long fds_bits[-1]; }

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

variable_types: rv int

variable_types: struct sockaddr_storage { unsigned short ss_family; char __ss_padding[-1]; unsigned long __ss_align; }

variable_types: opcode int


decompiled_code:
ssize_t __fastcall TFTP_SendAck(__int64 a1, int a2)
{
__int16 v2; // ax
char buf[12]; // [rsp+Ch] [rbp-Ch] BYREF

buf[0] = 0;
buf[1] = 4;
v2 = a2 + 255;
if ( a2 >= 0 )
HIBYTE(v2) = BYTE1(a2);
buf[2] = HIBYTE(v2);
buf[3] = a2;
return sendto(*(_DWORD *)a1, buf, 4uLL, 0, *(const struct sockaddr **)(a1 + 8), 0x10u);
}

canonical_decompiled_code:
ssize_t __fastcall func7(__int64 a1, int a2)
{
__int16 v2; // ax
char buf[12]; // [rsp+Ch] [rbp-Ch] BYREF

buf[0] = 0;
buf[1] = 4;
v2 = a2 + 255;
if ( a2 >= 0 )
HIBYTE(v2) = BYTE1(a2);
buf[2] = HIBYTE(v2);
buf[3] = a2;
return sendto(*(_DWORD *)a1, buf, 4uLL, 0, *(const struct sockaddr **)(a1 + 8), 0x10u);
}

original_code:
void TFTP_SendAck(struct Transaction *t, int block_number)
{
  char buf[4];
  buf[0] = 0;
  buf[1] = 4;
  buf[2] = (block_number / 256);
  buf[3] = (block_number % 256);
  sendto(t->socket, buf, 4, 0, (const struct sockaddr *) t->address, sizeof(struct sockaddr));
}

canonical_original_code:
void TFTP_SendAck(struct Transaction *t, int block_number)
{
  char buf[4];
  buf[0] = 0;
  buf[1] = 4;
  buf[2] = (block_number / 256);
  buf[3] = (block_number % 256);
  sendto(t->socket, buf, 4, 0, (struct sockaddr *) t->address, sizeof(struct sockaddr));
}

variable_types: struct Transaction { int socket; struct sockaddr_storage { unsigned short ss_family; char __ss_padding[-1]; unsigned long __ss_align; } *address; char client_name[256]; int errors; int file; long start_time; int byte_count; } *

variable_types:
buf
char [4]

return_type: void

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

user_defined_types: struct Transaction { int socket; struct sockaddr_storage *address; char client_name[256]; int errors; int file; long start_time; int byte_count; }


decompiled_code:
__int64 __fastcall TFTP_NewWriteRequest(__int64 a1)
{
unsigned __int16 i; // r12
int v4; // er14
int v5; // edi
int v6; // eax
int v7; // edi
int v8; // eax
int v9; // er13
socklen_t addr_len; // [rsp+Ch] [rbp-35Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-358h] BYREF
char v12; // [rsp+90h] [rbp-2D8h] BYREF
char v13; // [rsp+91h] [rbp-2D7h]
char v14; // [rsp+92h] [rbp-2D6h]
char v15; // [rsp+93h] [rbp-2D5h]
char buf[524]; // [rsp+94h] [rbp-2D4h] BYREF
struct timeval timeout; // [rsp+2A0h] [rbp-C8h] BYREF
fd_set readfds; // [rsp+2B0h] [rbp-B8h] BYREF

TFTP_SendAck(a1, 0);
for ( i = 0; ; TFTP_SendAck(a1, i) )
{
while ( 1 )
{
memset(&readfds, 0, sizeof(readfds));
v5 = *(_DWORD *)a1;
v6 = *(_DWORD *)a1 + 63;
if ( *(int *)a1 >= 0 )
v6 = *(_DWORD *)a1;
readfds.fds_bits[v6 >> 6] |= 1LL << (v5 % 64);
timeout.tv_sec = 5LL;
timeout.tv_usec = 0LL;
if ( select(v5 + 1, &readfds, 0LL, 0LL, &timeout) <= 0 )
goto LABEL_8;
v7 = *(_DWORD *)a1;
v8 = *(_DWORD *)a1 + 63;
if ( *(int *)a1 >= 0 )
v8 = *(_DWORD *)a1;
if ( (readfds.fds_bits[v8 >> 6] & (1LL << (v7 % 64))) == 0 )
{
syslog(3, "%s: Timeout: Block %d", (const char *)(a1 + 16), i);
TFTP_SendAck(a1, i);
goto LABEL_8;
}
addr_len = 16;
v9 = recvfrom(v7, &v12, 0x204uLL, 0, &addr, &addr_len);
if ( v9 <= 0 )
{
syslog(3, "%s: recvfrom: %d", (const char *)(a1 + 16), (unsigned int)v9);
return 0LL;
}
if ( v13 != 3 )
break;
if ( i == ((unsigned __int16)v15 | (unsigned __int16)(v14 << 8)) )
goto LABEL_8;
v4 = write(*(_DWORD *)(a1 + 276), buf, v9 - 4);
*(_DWORD *)(a1 + 288) += v9 - 4;
TFTP_SendAck(a1, ++i);
if ( v9 <= 515 || v4 < 0 )
{
PrintTransactionTime(a1);
return 0xFFFFFFFFLL;
}
}
if ( v13 == 5 )
{
syslog(3, "%s: Error %d: %s", (const char *)(a1 + 16), (unsigned int)v15, buf);
return 0xFFFFFFFFLL;
}
LABEL_8:
if ( (int)RunErrorHandler(a1) < 0 )
break;
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
unsigned __int16 i; // r12
int v4; // er14
int v5; // edi
int v6; // eax
int v7; // edi
int v8; // eax
int v9; // er13
socklen_t addr_len; // [rsp+Ch] [rbp-35Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-358h] BYREF
char v12; // [rsp+90h] [rbp-2D8h] BYREF
char v13; // [rsp+91h] [rbp-2D7h]
char v14; // [rsp+92h] [rbp-2D6h]
char v15; // [rsp+93h] [rbp-2D5h]
char buf[524]; // [rsp+94h] [rbp-2D4h] BYREF
struct timeval timeout; // [rsp+2A0h] [rbp-C8h] BYREF
fd_set readfds; // [rsp+2B0h] [rbp-B8h] BYREF

func7(a1, 0);
for ( i = 0; ; func7(a1, i) )
{
while ( 1 )
{
memset(&readfds, 0, sizeof(readfds));
v5 = *(_DWORD *)a1;
v6 = *(_DWORD *)a1 + 63;
if ( *(int *)a1 >= 0 )
v6 = *(_DWORD *)a1;
readfds.fds_bits[v6 >> 6] |= 1LL << (v5 % 64);
timeout.tv_sec = 5LL;
timeout.tv_usec = 0LL;
if ( select(v5 + 1, &readfds, 0LL, 0LL, &timeout) <= 0 )
goto LABEL_8;
v7 = *(_DWORD *)a1;
v8 = *(_DWORD *)a1 + 63;
if ( *(int *)a1 >= 0 )
v8 = *(_DWORD *)a1;
if ( (readfds.fds_bits[v8 >> 6] & (1LL << (v7 % 64))) == 0 )
{
syslog(3, "%s: Timeout: Block %d", (const char *)(a1 + 16), i);
func7(a1, i);
goto LABEL_8;
}
addr_len = 16;
v9 = recvfrom(v7, &v12, 0x204uLL, 0, &addr, &addr_len);
if ( v9 <= 0 )
{
syslog(3, "%s: recvfrom: %d", (const char *)(a1 + 16), (unsigned int)v9);
return 0LL;
}
if ( v13 != 3 )
break;
if ( i == ((unsigned __int16)v15 | (unsigned __int16)(v14 << 8)) )
goto LABEL_8;
v4 = write(*(_DWORD *)(a1 + 276), buf, v9 - 4);
*(_DWORD *)(a1 + 288) += v9 - 4;
func7(a1, ++i);
if ( v9 <= 515 || v4 < 0 )
{
func2(a1);
return 0xFFFFFFFFLL;
}
}
if ( v13 == 5 )
{
syslog(3, "%s: Error %d: %s", (const char *)(a1 + 16), (unsigned int)v15, buf);
return 0xFFFFFFFFLL;
}
LABEL_8:
if ( (int)func0(a1) < 0 )
break;
}
return 0xFFFFFFFFLL;
}

original_code:
int TFTP_NewWriteRequest(struct Transaction *trans, char *data)
{
  fd_set readFD;
  struct timeval timeout;
  uint16_t packet_block, last_block = 0;
  char packet_buff[(512+2+2)];
  TFTP_SendAck(trans, last_block);
  while ( 1 ) {
    do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&readFD)->fds_bits)[0]) : "memory"); } while (0);
    ((void) (((&readFD)->fds_bits)[((trans->socket) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((trans->socket) % (8 * (int) sizeof (__fd_mask)))))));
    timeout.tv_sec = 5;
    timeout.tv_usec = 0;
    if ( select(trans->socket+1, &readFD, ((void *)0), ((void *)0), &timeout) > 0 ) {
      if ( ((((&readFD)->fds_bits)[((trans->socket) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((trans->socket) % (8 * (int) sizeof (__fd_mask)))))) != 0) ) {
        socklen_t addr_len = sizeof(struct sockaddr);
        struct sockaddr_storage their_addr;
        int bytes = recvfrom(trans->socket, packet_buff, (512+2+2), 0,(struct sockaddr *)&their_addr, &addr_len);
        if ( bytes <= 0 ) {
          syslog(3,"%s: recvfrom: %d", trans->client_name, bytes);
          break;
        }
        int opcode = packet_buff[1];
        if ( opcode == 3 ) {
          packet_block = (packet_buff[2] * 256) | packet_buff[3];
          if ( packet_block != last_block ) {
            int rv = write( trans->file, packet_buff+4, bytes -4);
            trans->byte_count += bytes - 4;
            last_block++;
            TFTP_SendAck(trans, last_block);
            if (( bytes < (512+2+2)) || ( rv < 0 )) {
              PrintTransactionTime(trans);
              return -1;
            }
            continue;
          }
        } else if ( opcode == 5 ) {
          syslog(3,"%s: Error %d: %s", trans->client_name, packet_buff[3], packet_buff+4);
          return -1;
        }
      } else {
        syslog(3,"%s: Timeout: Block %d", trans->client_name, last_block);
        TFTP_SendAck(trans, last_block);
      }
    }
    if ( RunErrorHandler(trans) < 0 ) {
      return -1;
    }
    TFTP_SendAck(trans, last_block);
  }
  return 0;
}

canonical_original_code:
int TFTP_NewWriteRequest(struct Transaction *trans, char *data)
{
  struct fd_set readFD;
  struct timeval timeout;
  unsigned short packet_block; unsigned short last_block = 0;
  char packet_buff[-1];
  TFTP_SendAck(trans, last_block);
  while ( 1 ) {
    do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&readFD)->fds_bits)[0]) : "memory"); } while (0);
    ((void) (((&readFD)->fds_bits)[((trans->socket) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((trans->socket) % (8 * (int) sizeof (__fd_mask)))))));
    timeout.tv_sec = 5;
    timeout.tv_usec = 0;
    if ( select(trans->socket+1, &readFD, ((void *)0), ((void *)0), &timeout) > 0 ) {
      if ( ((((&readFD)->fds_bits)[((trans->socket) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((trans->socket) % (8 * (int) sizeof (__fd_mask)))))) != 0) ) {
        unsigned int addr_len = sizeof(struct sockaddr);
        struct sockaddr_storage their_addr;
        int bytes = recvfrom(trans->socket, packet_buff, (512+2+2), 0,(struct sockaddr *)&their_addr, &addr_len);
        if ( bytes <= 0 ) {
          syslog(3,"%s: recvfrom: %d", trans->client_name, bytes);
          break;
        }
        int opcode = packet_buff[1];
        if ( opcode == 3 ) {
          packet_block = (packet_buff[2] * 256) | packet_buff[3];
          if ( packet_block != last_block ) {
            int rv = write( trans->file, packet_buff+4, bytes -4);
            trans->byte_count += bytes - 4;
            last_block++;
            TFTP_SendAck(trans, last_block);
            if (( bytes < (512+2+2)) || ( rv < 0 )) {
              PrintTransactionTime(trans);
              return -1;
            }
            continue;
          }
        } else if ( opcode == 5 ) {
          syslog(3,"%s: Error %d: %s", trans->client_name, packet_buff[3], packet_buff+4);
          return -1;
        }
      } else {
        syslog(3,"%s: Timeout: Block %d", trans->client_name, last_block);
        TFTP_SendAck(trans, last_block);
      }
    }
    if ( RunErrorHandler(trans) < 0 ) {
      return -1;
    }
    TFTP_SendAck(trans, last_block);
  }
  return 0;
}

variable_types: bytes int

return_type: int

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

user_defined_types: struct sockaddr_storage { unsigned short ss_family; char __ss_padding[-1]; unsigned long __ss_align; }


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rdx
int v4; // eax
char *v5; // rbx
size_t v6; // rax
int v7; // eax
struct addrinfo *v8; // rbx
int v9; // eax
int v10; // ebp
int v11; // er12
__int64 v12; // r13
int v13; // eax
__pid_t v14; // eax
__int64 v15; // rcx
int v16; // ebx
const char *v18; // rax
socklen_t addr_len; // [rsp+Ch] [rbp-55Ch] BYREF
char buf[2]; // [rsp+10h] [rbp-558h] BYREF
char v21[526]; // [rsp+12h] [rbp-556h] BYREF
struct sigaction v22; // [rsp+220h] [rbp-348h] BYREF
fd_set readfds; // [rsp+2C0h] [rbp-2A8h] BYREF
struct sockaddr addr; // [rsp+340h] [rbp-228h] BYREF
struct timeval timeout; // [rsp+3C0h] [rbp-1A8h] BYREF
struct addrinfo *pai; // [rsp+3D8h] [rbp-190h] BYREF
struct addrinfo req; // [rsp+3E0h] [rbp-188h] BYREF
int v28[86]; // [rsp+410h] [rbp-158h] BYREF

memset(&v22.sa_mask, 0, 0x90uLL);
v22.sa_handler = (__sighandler_t)clean_up_child_process;
sigaction(17, &v22, 0LL);
openlog("TFTP Server", 1, 8);
syslog(5, "TFTP Server online");
if ( daemon(1, 0) < 0 )
{
syslog(3, "daemonise failed");
return -1;
}
v3 = "/tmp/";
if ( argc > 1 )
v3 = argv[1];
v4 = asprintf(&SystemDir, "%s", v3);
v5 = SystemDir;
if ( v4 < 0 || !SystemDir )
{
syslog(3, "SystemDir == NULL");
return -1;
}
v6 = strlen(SystemDir);
if ( v5[v6 - 1] != 47 )
*(_WORD *)&v5[v6] = 47;
syslog(5, "Directory set: %s", SystemDir);
*(_QWORD *)&req.ai_flags = 1LL;
*(_QWORD *)&req.ai_socktype = 2LL;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
v7 = getaddrinfo(0LL, "69", &req, &pai);
if ( v7 )
{
v18 = gai_strerror(v7);
syslog(3, "getaddrinfo: %s\n", v18);
return 1;
}
v8 = pai;
if ( !pai )
{
LABEL_31:
syslog(3, "listner: failed to bind socket");
return 2;
}
while ( 1 )
{
v9 = socket(v8->ai_family, v8->ai_socktype, v8->ai_protocol);
v10 = v9;
if ( v9 != -1 )
break;
syslog(3, "listner: socket");
LABEL_30:
v8 = v8->ai_next;
if ( !v8 )
goto LABEL_31;
}
if ( bind(v9, v8->ai_addr, v8->ai_addrlen) == -1 )
{
close(v10);
syslog(3, "listner: bind");
goto LABEL_30;
}
freeaddrinfo(pai);
v11 = v10 / 64;
v12 = 1LL << (v10 % 64);
do
{
while ( 1 )
{
do
{
do
{
memset(&readfds, 0, sizeof(readfds));
timeout.tv_sec = 5LL;
timeout.tv_usec = 0LL;
readfds.fds_bits[v11] |= v12;
}
while ( select(v10 + 1, &readfds, 0LL, 0LL, &timeout) <= 0 );
}
while ( (readfds.fds_bits[v11] & v12) == 0 );
addr_len = 16;
v13 = recvfrom(v10, buf, 0x204uLL, 0, &addr, &addr_len);
if ( v13 < 0 )
{
syslog(3, "recvfrom: %d", (unsigned int)v13);
exit(1);
}
if ( v13 )
{
v14 = fork();
if ( v14 <= 0 )
break;
}
}
if ( v14 < 0 )
{
syslog(3, "Fork error");
return -1;
}
memset(v28, 0, 0x128uLL);
}
while ( (int)Trans_SetupSocket((__int64)v28, &addr) < 0 );
v28[69] = OpenFile(v21, 1);
if ( v28[69] < 0 )
{
TFTP_Send_Error((__int64)v28, 1);
v16 = -1;
}
else
{
syslog(5, "%s: \"%s\"", (const char *)&v28[4], v21);
v28[68] = 5;
*(_QWORD *)&v28[70] = time(0LL);
v15 = (unsigned int)buf[1];
if ( (_DWORD)v15 == 1 )
{
v16 = TFTP_NewReadRequest((__int64)v28);
}
else if ( (_DWORD)v15 == 2 )
{
v16 = TFTP_NewWriteRequest((__int64)v28);
}
else
{
syslog(5, "%s: Bad opecode: %d ", (const char *)&v28[4], v15);
v16 = -1;
}
close(v28[69]);
close(v28[0]);
}
return v16;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rdx
int v4; // eax
char *v5; // rbx
size_t v6; // rax
int v7; // eax
struct addrinfo *v8; // rbx
int v9; // eax
int v10; // ebp
int v11; // er12
__int64 v12; // r13
int v13; // eax
__pid_t v14; // eax
__int64 v15; // rcx
int v16; // ebx
const char *v18; // rax
socklen_t addr_len; // [rsp+Ch] [rbp-55Ch] BYREF
char buf[2]; // [rsp+10h] [rbp-558h] BYREF
char v21[526]; // [rsp+12h] [rbp-556h] BYREF
struct sigaction v22; // [rsp+220h] [rbp-348h] BYREF
fd_set readfds; // [rsp+2C0h] [rbp-2A8h] BYREF
struct sockaddr addr; // [rsp+340h] [rbp-228h] BYREF
struct timeval timeout; // [rsp+3C0h] [rbp-1A8h] BYREF
struct addrinfo *pai; // [rsp+3D8h] [rbp-190h] BYREF
struct addrinfo req; // [rsp+3E0h] [rbp-188h] BYREF
int v28[86]; // [rsp+410h] [rbp-158h] BYREF

memset(&v22.sa_mask, 0, 0x90uLL);
v22.sa_handler = (__sighandler_t)clean_up_child_process;
sigaction(17, &v22, 0LL);
openlog("TFTP Server", 1, 8);
syslog(5, "TFTP Server online");
if ( daemon(1, 0) < 0 )
{
syslog(3, "daemonise failed");
return -1;
}
v3 = "/tmp/";
if ( argc > 1 )
v3 = argv[1];
v4 = asprintf(&SystemDir, "%s", v3);
v5 = SystemDir;
if ( v4 < 0 || !SystemDir )
{
syslog(3, "SystemDir == NULL");
return -1;
}
v6 = strlen(SystemDir);
if ( v5[v6 - 1] != 47 )
*(_WORD *)&v5[v6] = 47;
syslog(5, "Directory set: %s", SystemDir);
*(_QWORD *)&req.ai_flags = 1LL;
*(_QWORD *)&req.ai_socktype = 2LL;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
v7 = getaddrinfo(0LL, "69", &req, &pai);
if ( v7 )
{
v18 = gai_strerror(v7);
syslog(3, "getaddrinfo: %s\n", v18);
return 1;
}
v8 = pai;
if ( !pai )
{
LABEL_31:
syslog(3, "listner: failed to bind socket");
return 2;
}
while ( 1 )
{
v9 = socket(v8->ai_family, v8->ai_socktype, v8->ai_protocol);
v10 = v9;
if ( v9 != -1 )
break;
syslog(3, "listner: socket");
LABEL_30:
v8 = v8->ai_next;
if ( !v8 )
goto LABEL_31;
}
if ( bind(v9, v8->ai_addr, v8->ai_addrlen) == -1 )
{
close(v10);
syslog(3, "listner: bind");
goto LABEL_30;
}
freeaddrinfo(pai);
v11 = v10 / 64;
v12 = 1LL << (v10 % 64);
do
{
while ( 1 )
{
do
{
do
{
memset(&readfds, 0, sizeof(readfds));
timeout.tv_sec = 5LL;
timeout.tv_usec = 0LL;
readfds.fds_bits[v11] |= v12;
}
while ( select(v10 + 1, &readfds, 0LL, 0LL, &timeout) <= 0 );
}
while ( (readfds.fds_bits[v11] & v12) == 0 );
addr_len = 16;
v13 = recvfrom(v10, buf, 0x204uLL, 0, &addr, &addr_len);
if ( v13 < 0 )
{
syslog(3, "recvfrom: %d", (unsigned int)v13);
exit(1);
}
if ( v13 )
{
v14 = fork();
if ( v14 <= 0 )
break;
}
}
if ( v14 < 0 )
{
syslog(3, "Fork error");
return -1;
}
memset(v28, 0, 0x128uLL);
}
while ( (int)func4((__int64)v28, &addr) < 0 );
v28[69] = func3(v21, 1);
if ( v28[69] < 0 )
{
func5((__int64)v28, 1);
v16 = -1;
}
else
{
syslog(5, "%s: \"%s\"", (const char *)&v28[4], v21);
v28[68] = 5;
*(_QWORD *)&v28[70] = time(0LL);
v15 = (unsigned int)buf[1];
if ( (_DWORD)v15 == 1 )
{
v16 = func6((__int64)v28);
}
else if ( (_DWORD)v15 == 2 )
{
v16 = func8((__int64)v28);
}
else
{
syslog(5, "%s: Bad opecode: %d ", (const char *)&v28[4], v15);
v16 = -1;
}
close(v28[69]);
close(v28[0]);
}
return v16;
}

original_code:
int main( int argc, char *argv[] )
{
  struct Transaction trans;
  int ListenSocket;
  struct addrinfo hints, *servinfo, *p;
  struct timeval timeout;
  struct sockaddr_storage their_addr;
  fd_set ReadFD;
  struct sigaction sigchld_action;
  char packet_buff[(512+2+2)];
  int rv;
  const char *dir;
  memset (&sigchld_action, 0, sizeof (sigchld_action));
  sigchld_action.__sigaction_handler.sa_handler = &clean_up_child_process;
  sigaction (17, &sigchld_action, ((void *)0));
  openlog("TFTP Server", 0x01, (1<<3));
  syslog(5,"TFTP Server online");
  if ( daemon( 1, 0 ) < 0 ) {
    syslog(3,"daemonise failed");
    return -1;
  }
  dir = ( argc >= 2 )? argv[1]: DefaultDir;
  rv = asprintf(&SystemDir, "%s", dir);
  if (( SystemDir == ((void *)0) ) || ( rv < 0 )) {
    syslog(3, "SystemDir == NULL");
    return -1;
  } else {
    if ( SystemDir[strlen(SystemDir)-1] != '/' ) {
      strcat(SystemDir, "/");
    }
  }
  syslog(5,"Directory set: %s", SystemDir);
  memset(&hints, 0, sizeof hints);
  hints.ai_family = 0;
  hints.ai_socktype = SOCK_DGRAM;
  hints.ai_flags = 0x0001;
  if ((rv = getaddrinfo(((void *)0), "69", &hints, &servinfo)) != 0) {
    syslog(3,"getaddrinfo: %s\n", gai_strerror(rv));
    return 1;
  }
  for(p = servinfo; p != ((void *)0); p = p->ai_next) {
    if ((ListenSocket = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
      syslog(3,"listner: socket");
      continue;
    }
    if (bind(ListenSocket, p->ai_addr, p->ai_addrlen) == -1) {
      close(ListenSocket);
      syslog(3,"listner: bind");
      continue;
    }
    break;
  }
  if (p == ((void *)0)) {
    syslog(3,"listner: failed to bind socket");
    return 2;
  }
  freeaddrinfo(servinfo);
  while ( 1 ) {
    do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&ReadFD)->fds_bits)[0]) : "memory"); } while (0);
    timeout.tv_sec = 5;
    timeout.tv_usec = 0;
    ((void) (((&ReadFD)->fds_bits)[((ListenSocket) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((ListenSocket) % (8 * (int) sizeof (__fd_mask)))))));
    if ( select(ListenSocket+1, &ReadFD, ((void *)0), ((void *)0), &timeout) > 0 ) {
      if ( ((((&ReadFD)->fds_bits)[((ListenSocket) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((ListenSocket) % (8 * (int) sizeof (__fd_mask)))))) != 0) ) {
        socklen_t addr_len = sizeof(struct sockaddr);
        int bytes = recvfrom(ListenSocket, packet_buff, (512+2+2), 0,(struct sockaddr *)&their_addr, &addr_len);
        if ( bytes < 0) {
          syslog(3,"recvfrom: %d", bytes);
          exit(1);
        } else if ( bytes == 0 ) {
          continue;
        }
        pid_t pid = fork();
        if ( pid > 0 ) {
          continue;
        } else if ( pid < 0 ) {
          syslog(3,"Fork error");
          return -1;
        }
        memset(&trans, 0, sizeof(struct Transaction));
        if (Trans_SetupSocket(&trans, &their_addr) < 0 ) {
          continue;
        }
        trans.file = OpenFile((const char *) packet_buff+2, 1);
        if ( trans.file < 0 ) {
          TFTP_Send_Error(&trans, 1);
          return -1;
        }
        syslog(5,"%s: \"%s\"", trans.client_name, packet_buff+2);
        trans.errors = 5;
        trans.start_time = time(((void *)0));
        int opcode = packet_buff[1];
        if ( opcode == 1 ) {
          rv = TFTP_NewReadRequest(&trans, packet_buff);
        } else if ( opcode == 2 ) {
          rv = TFTP_NewWriteRequest(&trans, packet_buff);
        } else {
          syslog(5,"%s: Bad opecode: %d ", trans.client_name, opcode);
          rv = -1;
        }
        close(trans.file);
        close(trans.socket);
        return rv;
      }
    }
  }
  free(SystemDir);
  close(ListenSocket);
  return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
  struct Transaction trans;
  int ListenSocket;
  struct addrinfo hints; struct addrinfo *servinfo; struct addrinfo *p;
  struct timeval timeout;
  struct sockaddr_storage their_addr;
  struct fd_set ReadFD;
  struct sigaction sigchld_action;
  char packet_buff[-1];
  int rv;
  char *dir;
  memset (&sigchld_action, 0, sizeof (sigchld_action));
  sigchld_action.__sigaction_handler.sa_handler = &clean_up_child_process;
  sigaction (17, &sigchld_action, ((void *)0));
  openlog("TFTP Server", 0x01, (1<<3));
  syslog(5,"TFTP Server online");
  if ( daemon( 1, 0 ) < 0 ) {
    syslog(3,"daemonise failed");
    return -1;
  }
  dir = ( argc >= 2 )? argv[1]: DefaultDir;
  rv = asprintf(&SystemDir, "%s", dir);
  if (( SystemDir == ((void *)0) ) || ( rv < 0 )) {
    syslog(3, "SystemDir == NULL");
    return -1;
  } else {
    if ( SystemDir[strlen(SystemDir)-1] != '/' ) {
      strcat(SystemDir, "/");
    }
  }
  syslog(5,"Directory set: %s", SystemDir);
  memset(&hints, 0, sizeof hints);
  hints.ai_family = 0;
  hints.ai_socktype = SOCK_DGRAM;
  hints.ai_flags = 0x0001;
  if ((rv = getaddrinfo(((void *)0), "69", &hints, &servinfo)) != 0) {
    syslog(3,"getaddrinfo: %s\n", gai_strerror(rv));
    return 1;
  }
  for(p = servinfo; p != ((void *)0); p = p->ai_next) {
    if ((ListenSocket = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
      syslog(3,"listner: socket");
      continue;
    }
    if (bind(ListenSocket, p->ai_addr, p->ai_addrlen) == -1) {
      close(ListenSocket);
      syslog(3,"listner: bind");
      continue;
    }
    break;
  }
  if (p == ((void *)0)) {
    syslog(3,"listner: failed to bind socket");
    return 2;
  }
  freeaddrinfo(servinfo);
  while ( 1 ) {
    do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&ReadFD)->fds_bits)[0]) : "memory"); } while (0);
    timeout.tv_sec = 5;
    timeout.tv_usec = 0;
    ((void) (((&ReadFD)->fds_bits)[((ListenSocket) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((ListenSocket) % (8 * (int) sizeof (__fd_mask)))))));
    if ( select(ListenSocket+1, &ReadFD, ((void *)0), ((void *)0), &timeout) > 0 ) {
      if ( ((((&ReadFD)->fds_bits)[((ListenSocket) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((ListenSocket) % (8 * (int) sizeof (__fd_mask)))))) != 0) ) {
        unsigned int addr_len = sizeof(struct sockaddr);
        int bytes = recvfrom(ListenSocket, packet_buff, (512+2+2), 0,(struct sockaddr *)&their_addr, &addr_len);
        if ( bytes < 0) {
          syslog(3,"recvfrom: %d", bytes);
          exit(1);
        } else if ( bytes == 0 ) {
          continue;
        }
        int pid = fork();
        if ( pid > 0 ) {
          continue;
        } else if ( pid < 0 ) {
          syslog(3,"Fork error");
          return -1;
        }
        memset(&trans, 0, sizeof(struct Transaction));
        if (Trans_SetupSocket(&trans, &their_addr) < 0 ) {
          continue;
        }
        trans.file = OpenFile((char *) packet_buff+2, 1);
        if ( trans.file < 0 ) {
          TFTP_Send_Error(&trans, 1);
          return -1;
        }
        syslog(5,"%s: \"%s\"", trans.client_name, packet_buff+2);
        trans.errors = 5;
        trans.start_time = time(((void *)0));
        int opcode = packet_buff[1];
        if ( opcode == 1 ) {
          rv = TFTP_NewReadRequest(&trans, packet_buff);
        } else if ( opcode == 2 ) {
          rv = TFTP_NewWriteRequest(&trans, packet_buff);
        } else {
          syslog(5,"%s: Bad opecode: %d ", trans.client_name, opcode);
          rv = -1;
        }
        close(trans.file);
        close(trans.socket);
        return rv;
      }
    }
  }
  free(SystemDir);
  close(ListenSocket);
  return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: struct Transaction { int socket; struct sockaddr_storage { unsigned short ss_family; char __ss_padding[-1]; unsigned long __ss_align; } *address; char client_name[256]; int errors; int file; long start_time; int byte_count; }

variable_types: ListenSocket int

variable_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *ai_addr; char *ai_canonname; struct addrinfo *ai_next; } *

variable_types:
ReadFD
struct fd_set { long fds_bits[-1]; }

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

variable_types:
dir
char *

variable_types: pid int

user_defined_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr *ai_addr; char *ai_canonname; struct addrinfo *ai_next; }

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

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


decompiled_code:
__int64 __fastcall oct2dec(int a1)
{
int v1; // esi
unsigned int v2; // er8
int v3; // edx

if ( a1 <= 0 )
return 0;
v1 = 1;
v2 = 0;
do
{
v2 += v1 * (a1 % 10);
v3 = a1;
a1 /= 10;
v1 *= 8;
}
while ( v3 > 9 );
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1)
{
int v1; // esi
unsigned int v2; // er8
int v3; // edx

if ( a1 <= 0 )
return 0;
v1 = 1;
v2 = 0;
do
{
v2 += v1 * (a1 % 10);
v3 = a1;
a1 /= 10;
v1 *= 8;
}
while ( v3 > 9 );
return v2;
}

original_code:
int oct2dec ( int num )
{
 int total = 0,power=1;
 while(num > 0)
 {
 total +=power*(num%10) ;
 num /= 10;
 power *= 8;
 }
 return total;
}

canonical_original_code:
int oct2dec(int num)
{
 int total = 0; int power = 1;
 while(num > 0)
 {
 total +=power*(num%10) ;
 num /= 10;
 power *= 8;
 }
 return total;
}

variable_types: num int

variable_types: total int

variable_types: power int


iuntar

decompiled_code:
__int64 __fastcall iuntar(char *a1, char *a2, __int64 a3, char a4)
{
return untar(a1, a2, a3, a4);
}

canonical_decompiled_code:
__int64 __fastcall func9(char *a1, char *a2, __int64 a3, char a4)
{
return func8(a1, a2, a3, a4);
}

original_code:
int iuntar(char *tarfile, char *outdir, int chmod, _Bool extract)
{
 return untar(tarfile, outdir, chmod, extract);
}

variable_types:
tarfile
char *

variable_types:
outdir
char *

variable_types: chmod int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
size_t v4; // rax
size_t v5; // rax
char v6[256]; // [rsp+1h] [rbp-209h]
char v7[256]; // [rsp+2h] [rbp-208h] BYREF
char s[264]; // [rsp+102h] [rbp-108h] BYREF

switch ( argc )
{
case 2:
untar((char *)argv[1], "./", 493LL, 1);
break;
case 3:
untar((char *)argv[1], (char *)argv[2], 493LL, 1);
break;
case 1:
fwrite("Enter Tar File: ", 1uLL, 0x10uLL, stdout);
fgets(s, 256, stdin);
v4 = strlen(s);
if ( v7[v4 + 255] == 10 )
v7[v4 + 255] = 0;
fwrite("Enter Output Dir: ", 1uLL, 0x12uLL, stdout);
fgets(v7, 256, stdin);
v5 = strlen(v7);
if ( v6[v5] == 10 )
v6[v5] = 0;
untar(s, v7, 493LL, 1);
break;
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
size_t v4; // rax
size_t v5; // rax
char v6[256]; // [rsp+1h] [rbp-209h]
char v7[256]; // [rsp+2h] [rbp-208h] BYREF
char s[264]; // [rsp+102h] [rbp-108h] BYREF

switch ( argc )
{
case 2:
func8((char *)argv[1], "./", 493LL, 1);
break;
case 3:
func8((char *)argv[1], (char *)argv[2], 493LL, 1);
break;
case 1:
fwrite("Enter Tar File: ", 1uLL, 0x10uLL, stdout);
fgets(s, 256, stdin);
v4 = strlen(s);
if ( v7[v4 + 255] == 10 )
v7[v4 + 255] = 0;
fwrite("Enter Output Dir: ", 1uLL, 0x12uLL, stdout);
fgets(v7, 256, stdin);
v5 = strlen(v7);
if ( v6[v5] == 10 )
v6[v5] = 0;
func8(s, v7, 493LL, 1);
break;
}
return 0;
}

original_code:
int main(int argc, char *argv[])
{
 char tarfile[256], taroutf[256];
 if(argc==2) {
 untar(argv[1], "./", 0755, 1); }
 if(argc==3) {
 untar(argv[1], argv[2], 0755, 1); }
 if(argc == 1) {
 fprintf(stdout, "Enter Tar File: ");
 fgets(tarfile, 256, stdin);
 size_t tfln = strlen(tarfile) - 1;
 if(tarfile[tfln] == '\n') {
 tarfile[tfln] = '\0'; }
 fprintf(stdout, "Enter Output Dir: ");
 fgets(taroutf, 256, stdin);
 size_t tofln = strlen(taroutf) - 1;
 if(taroutf[tofln] == '\n') {
 taroutf[tofln] = '\0'; }
 untar(tarfile, taroutf, 0755, 1); }
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
 char tarfile[256]; char taroutf[256];
 if(argc==2) {
 untar(argv[1], "./", 0755, 1); }
 if(argc==3) {
 untar(argv[1], argv[2], 0755, 1); }
 if(argc == 1) {
 fprintf(stdout, "Enter Tar File: ");
 fgets(tarfile, 256, stdin);
 unsigned long tfln = strlen(tarfile) - 1;
 if(tarfile[tfln] == '\n') {
 tarfile[tfln] = '\0'; }
 fprintf(stdout, "Enter Output Dir: ");
 fgets(taroutf, 256, stdin);
 unsigned long tofln = strlen(taroutf) - 1;
 if(taroutf[tofln] == '\n') {
 taroutf[tofln] = '\0'; }
 untar(tarfile, taroutf, 0755, 1); }
 return 0;
}

variable_types:
tarfile
char [256]

variable_types:
taroutf
char [256]

variable_types:
tfln
unsigned long

variable_types:
tofln
unsigned long


variable_types: lat float

variable_types: lon float

variable_types:
x
float *

variable_types:
y
float *

variable_types: z int

variable_types: e float


printMatrix

repo: GavinHigham/C-Program-a-Day

decompiled_code:
int __fastcall printMatrix(__int64 a1, int a2)
{
int result; // eax
__int64 v3; // r13
__int64 v4; // rbx

puts("Printing a matrix:");
result = a2 * a2;
if ( a2 * a2 > 0 )
{
v3 = (unsigned int)(result + 1);
v4 = 1LL;
do
{
result = printf("%2d%c", *(unsigned int *)(a1 + 4 * v4 - 4), (int)v4 % a2 == 0 ? 10 : 32);
++v4;
}
while ( v4 != v3 );
}
return result;
}

canonical_decompiled_code:
int __fastcall func7(__int64 a1, int a2)
{
int result; // eax
__int64 v3; // r13
__int64 v4; // rbx

puts("Printing a matrix:");
result = a2 * a2;
if ( a2 * a2 > 0 )
{
v3 = (unsigned int)(result + 1);
v4 = 1LL;
do
{
result = printf("%2d%c", *(unsigned int *)(a1 + 4 * v4 - 4), (int)v4 % a2 == 0 ? 10 : 32);
++v4;
}
while ( v4 != v3 );
}
return result;
}

original_code:
void printMatrix(int *matrix, int n)
{
 printf("Printing a matrix:\n");
 for (int i = 0; i < n*n; i++) printf("%2d%c", matrix[(i/n)*n + i%n], (i+1)%n==0?'\n':' ');
}

variable_types:
matrix
int *

variable_types: n int

variable_types: i int


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

result = (unsigned int)*a1;
*a1 = *a2;
*a2 = *a3;
*a3 = *a4;
*a4 = result;
return result;
}

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

result = (unsigned int)*a1;
*a1 = *a2;
*a2 = *a3;
*a3 = *a4;
*a4 = result;
return result;
}

original_code:
void permute4(int *a, int *b, int *c, int *d)
{
 int tmp = *a;
 *a = *b;
 *b = *c;
 *c = *d;
 *d = tmp;
}

variable_types:
a
int *

variable_types:
b
int *

variable_types:
c
int *

variable_types:
d
int *

variable_types: tmp int


rotateMatrix

original_code:
void rotateMatrix(int *matrix, int d)
{
 int n = d-1;
 for (int i = 0; i < d/2; i++)
  for (int j = i; j < d-1-i; j++)
   permute4(&matrix[i*d+j], &matrix[(n-j)*d+i], &matrix[(n-i)*d+(n-j)], &matrix[j*d+(n-i)]);
}

variable_types: d int

variable_types: j int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // esi
void *v4; // rsp
__int64 v5; // rax
_DWORD v7[2]; // [rsp+0h] [rbp-10h] BYREF

v3 = DIMENSION;
v4 = alloca(4LL * v3 * v3);
if ( v3 * v3 > 0 )
{
v5 = 0LL;
do
{
v7[v5] = v5;
++v5;
}
while ( v5 != v3 * v3 );
}
printMatrix((__int64)v7, v3);
rotateMatrix((__int64)v7, DIMENSION);
printMatrix((__int64)v7, DIMENSION);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // esi
void *v4; // rsp
__int64 v5; // rax
_DWORD v7[2]; // [rsp+0h] [rbp-10h] BYREF

v3 = DIMENSION;
v4 = alloca(4LL * v3 * v3);
if ( v3 * v3 > 0 )
{
v5 = 0LL;
do
{
v7[v5] = v5;
++v5;
}
while ( v5 != v3 * v3 );
}
func7((__int64)v7, v3);
func9((__int64)v7, DIMENSION);
func7((__int64)v7, DIMENSION);
return 0;
}

original_code:
int main()
{
 int matMemory[DIMENSION*DIMENSION];
 int *testMat = (int *)matMemory;
 for (int i = 0; i < DIMENSION*DIMENSION; i++) testMat[i] = i;
 printMatrix(testMat, DIMENSION);
 rotateMatrix(testMat, DIMENSION);
 printMatrix(testMat, DIMENSION);
 return 0;
}

canonical_original_code:
int main()
{
 int matMemory[-1];
 int *testMat = (int *)matMemory;
 for (int i = 0; i < DIMENSION*DIMENSION; i++) testMat[i] = i;
 printMatrix(testMat, DIMENSION);
 rotateMatrix(testMat, DIMENSION);
 printMatrix(testMat, DIMENSION);
 return 0;
}

variable_types:
matMemory
int [-1]

variable_types:
testMat
int *


indexOfBiggest

decompiled_code:
__int64 __fastcall indexOfBiggest(__int64 a1, int a2)
{
__int64 v2; // rax
unsigned int v3; // edx

if ( a2 <= 1 )
return 0;
v2 = 1LL;
v3 = 0;
do
{
if ( *(double *)(a1 + 8 * v2) > *(double *)(a1 + 8LL * (int)v3) )
v3 = v2;
++v2;
}
while ( v2 != a2 );
return v3;
}

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

if ( a2 <= 1 )
return 0;
v2 = 1LL;
v3 = 0;
do
{
if ( *(double *)(a1 + 8 * v2) > *(double *)(a1 + 8LL * (int)v3) )
v3 = v2;
++v2;
}
while ( v2 != a2 );
return v3;
}

original_code:
int indexOfBiggest(double num[], int size)
{
 int indexOfLowest = 0;
 int i;
 for (i = 1; i < size; i++)
  if (num[i] > num[indexOfLowest]) indexOfLowest = i;
 return indexOfLowest;
}

variable_types:
num
double []

variable_types: size int

variable_types: indexOfLowest int


main

original_code:
int main()
{
 int i, j, k;
 int playerMoves[20] = {0};
 double costs[20][4];
 double playerWeight[20][4];
 for (i = 0; i < 20; i++)
  for (j = 0; j < 4; j++)
   costs[i][j] = playerWeight[i][j] = 0;
 for (i = 0; i < 20; i++) {
  double denominator = 0;
  for (j = 0; j < 4; j++) {
   double rowCostSum = 0;
   for (k = 0; k < i; k++)
    rowCostSum += costs[k][j];
   playerWeight[i][j] = 1 - 0.1*rowCostSum;
   denominator += playerWeight[i][j];
  }
  for (j = 0; j < 4; j++)
   playerWeight[i][j] /= denominator;
  playerMoves[i] = indexOfBiggest(playerWeight[i], 4);
  costs[i][playerMoves[i]] = playerWeight[i][playerMoves[i]];
 }
 double totalCost = 0;
 for (i = 0; i < 20; i++) {
  printf("Turn %i\n", i+1);
  printf("Player Weights | Adversary Set Costs\n");
  for (j = 0; j < 4; j++) {
   printf("%14f | %14f\n", playerWeight[i][j], costs[i][j]);
  }
  printf("\nPlayer chose move %i, and incurred cost %f\n\n", playerMoves[i], costs[i][playerMoves[i]]);
  totalCost += costs[i][playerMoves[i]];
 }
 printf("Total cost: %f\n", totalCost);
 return 0;
}

canonical_original_code:
int main()
{
 int i; int j; int k;
 int playerMoves[20] = {0};
 double costs[20][4];
 double playerWeight[20][4];
 for (i = 0; i < 20; i++)
  for (j = 0; j < 4; j++)
   costs[i][j] = playerWeight[i][j] = 0;
 for (i = 0; i < 20; i++) {
  double denominator = 0;
  for (j = 0; j < 4; j++) {
   double rowCostSum = 0;
   for (k = 0; k < i; k++)
    rowCostSum += costs[k][j];
   playerWeight[i][j] = 1 - 0.1*rowCostSum;
   denominator += playerWeight[i][j];
  }
  for (j = 0; j < 4; j++)
   playerWeight[i][j] /= denominator;
  playerMoves[i] = indexOfBiggest(playerWeight[i], 4);
  costs[i][playerMoves[i]] = playerWeight[i][playerMoves[i]];
 }
 double totalCost = 0;
 for (i = 0; i < 20; i++) {
  printf("Turn %i\n", i+1);
  printf("Player Weights | Adversary Set Costs\n");
  for (j = 0; j < 4; j++) {
   printf("%14f | %14f\n", playerWeight[i][j], costs[i][j]);
  }
  printf("\nPlayer chose move %i, and incurred cost %f\n\n", playerMoves[i], costs[i][playerMoves[i]]);
  totalCost += costs[i][playerMoves[i]];
 }
 printf("Total cost: %f\n", totalCost);
 return 0;
}

variable_types: k int

variable_types:
playerMoves
int [20]

variable_types:
costs
double [20][4]

variable_types:
playerWeight
double [20][4]

variable_types: denominator double

variable_types: rowCostSum double

variable_types: totalCost double


ctoint

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

if ( (unsigned __int8)(a1 - 97) <= 0x19u )
return (unsigned int)(a1 - 87);
if ( (unsigned __int8)(a1 - 65) <= 0x19u )
return (unsigned int)(a1 - 55);
result = (unsigned int)(a1 - 48);
if ( (unsigned __int8)(a1 - 48) >= 0xAu )
result = 0LL;
return result;
}

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

if ( (unsigned __int8)(a1 - 97) <= 0x19u )
return (unsigned int)(a1 - 87);
if ( (unsigned __int8)(a1 - 65) <= 0x19u )
return (unsigned int)(a1 - 55);
result = (unsigned int)(a1 - 48);
if ( (unsigned __int8)(a1 - 48) >= 0xAu )
result = 0LL;
return result;
}

variable_types: c char


original_code:
long long int str2int(char* str, int base)
{
 char c;
 long long int sum = 0;
 int i = 0;
 int neg = 0;
 if (str[0] == '-') {
  neg = 1;
  i++;
 }
 if ((str[i] == '0') && (str[i+1] == 'x')) i += 2;
 while ((c = str[i]) != '\0') {
  if (ctoint(c) >= base) break;
  sum *= base;
  sum += ctoint(c);
  i++;
 }
 if (neg) sum = ~sum + 1;
 return sum;
}

canonical_original_code:
long long str2int(char *str, int base)
{
 char c;
 long long sum = 0;
 int i = 0;
 int neg = 0;
 if (str[0] == '-') {
  neg = 1;
  i++;
 }
 if ((str[i] == '0') && (str[i+1] == 'x')) i += 2;
 while ((c = str[i]) != '\0') {
  if (ctoint(c) >= base) break;
  sum *= base;
  sum += ctoint(c);
  i++;
 }
 if (neg) sum = ~sum + 1;
 return sum;
}

variable_types:
str
char *

variable_types: base int

variable_types:
sum
long long

variable_types: i int

variable_types: neg int

return_type: long long


baseprint

original_code:
void baseprint(long long int val, int newbase) {
 char* receipt;
 char result[65] = {0};
 result[64] = '\0';
 int pos = 63;
 int tmp;
 while (val != 0) {
  tmp = val % newbase;
  if (tmp <= 9) result[pos] = tmp + '0';
  else result[pos] = tmp + 'a' - 10;
  val = val / newbase;
  pos--;
 }
 receipt = &result[pos + 1];
 if (newbase == 16) printf("0x");
 printf("%s\n", receipt);
}

canonical_original_code:
void baseprint(long long val, int newbase) {
 char *receipt;
 char result[65] = {0};
 result[64] = '\0';
 int pos = 63;
 int tmp;
 while (val != 0) {
  tmp = val % newbase;
  if (tmp <= 9) result[pos] = tmp + '0';
  else result[pos] = tmp + 'a' - 10;
  val = val / newbase;
  pos--;
 }
 receipt = &result[pos + 1];
 if (newbase == 16) printf("0x");
 printf("%s\n", receipt);
}

variable_types:
val
long long

variable_types: newbase int

variable_types:
receipt
char *

variable_types:
result
char [65]

variable_types: pos int

variable_types: tmp int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebp
int v4; // eax
int v5; // er12
char **v6; // rbx
__int64 v7; // rax

v3 = str2int((char *)argv[1], 10);
v4 = str2int((char *)argv[2], 10);
if ( argc <= 1 )
{
puts("Usage: base_changer [base of entry] [desired base of printed result] [list of decimal values...]");
}
else
{
v5 = v4;
if ( argc > 3 )
{
v6 = (char **)(argv + 3);
do
{
v7 = str2int(*v6, v3);
baseprint(v7, v5);
++v6;
}
while ( v6 != (char **)&argv[(unsigned int)(argc - 4) + 4] );
}
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebp
int v4; // eax
int v5; // er12
char **v6; // rbx
__int64 v7; // rax

v3 = func8((char *)argv[1], 10);
v4 = func8((char *)argv[2], 10);
if ( argc <= 1 )
{
puts("Usage: base_changer [base of entry] [desired base of printed result] [list of decimal values...]");
}
else
{
v5 = v4;
if ( argc > 3 )
{
v6 = (char **)(argv + 3);
do
{
v7 = func8(*v6, v3);
func9(v7, v5);
++v6;
}
while ( v6 != (char **)&argv[(unsigned int)(argc - 4) + 4] );
}
}
return 0;
}

original_code:
int main(int argc, char *argv[])
{
   int i = 1;
   int base1 = str2int(argv[1], 10);
   int base2 = str2int(argv[2], 10);
   if(argc < 2) printf("Usage: base_changer [base of entry] [desired base of printed result] [list of decimal values...]\n");
   for (i = 3; i < argc; i++) {
        baseprint(str2int(argv[i], base1), base2);
   }
   return 0;
}

variable_types: argc int

variable_types:
argv
char *[]


callMeMaybe

decompiled_code:
__int64 callMeMaybe()
{
printf("%i\n", (unsigned int)callCount);
++callCount;
return callMeMaybe();
}

canonical_decompiled_code:
__int64 func7()
{
printf("%i\n", (unsigned int)callCount);
++callCount;
return func7();
}

original_code:
void callMeMaybe()
{
 printf("%i\n", callCount);
 callCount++;
 callMeMaybe();
}


main

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

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

original_code:
int main() {
 callMeMaybe();
 return 0;
}

canonical_original_code:
int main() {
 callMeMaybe();
 return 0;
}


rand

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

result = (unsigned int)(69069 * randSeed + 1);
randSeed = 69069 * randSeed + 1;
return result;
}

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

result = (unsigned int)(69069 * randSeed + 1);
randSeed = 69069 * randSeed + 1;
return result;
}

original_code:
unsigned int rand(void)
{
 randSeed = (69069 * randSeed + 1);
 return randSeed;
}

canonical_original_code:
unsigned int rand()
{
 randSeed = (69069 * randSeed + 1);
 return randSeed;
}

return_type: unsigned int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rdi
int i; // ebx
unsigned int v5; // eax
unsigned int v6; // eax
unsigned int v7; // eax
unsigned int v8; // eax
unsigned int v9; // eax
__int64 j; // rbx
int v12[36]; // [rsp+0h] [rbp-A8h] BYREF
__int64 v13; // [rsp+90h] [rbp-18h] BYREF

memset(v12, 0, sizeof(v12));
v3 = (const char *)&v13;
for ( i = 0; i != 100000000; ++i )
{
randSeed = i;
v5 = rand(v3, argv, envp);
envp = (const char **)(36 * (v5 / 0x24));
if ( v5 % 0x24 == 6 )
{
v6 = rand(v3, argv, envp);
envp = (const char **)(36 * (v6 / 0x24));
if ( v6 % 0x24 == 19 )
{
v7 = rand(v3, argv, envp);
envp = (const char **)(36 * (v7 / 0x24));
if ( v7 % 0x24 == 16 )
{
v8 = rand(v3, argv, envp);
envp = (const char **)(36 * (v8 / 0x24));
if ( v8 % 0x24 == 29 )
{
v9 = rand(v3, argv, envp);
argv = (const char **)(v9 % 0x24);
++v12[v9 % 0x24];
v3 = "Next number mod 36: %i\n";
printf("Next number mod 36: %i\n", argv);
}
}
}
}
}
for ( j = 0LL; j != 36; ++j )
printf("Ind: %i, P: %i\n", (unsigned int)j, (unsigned int)v12[j]);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rdi
int i; // ebx
unsigned int v5; // eax
unsigned int v6; // eax
unsigned int v7; // eax
unsigned int v8; // eax
unsigned int v9; // eax
__int64 j; // rbx
int v12[36]; // [rsp+0h] [rbp-A8h] BYREF
__int64 v13; // [rsp+90h] [rbp-18h] BYREF

memset(v12, 0, sizeof(v12));
v3 = (const char *)&v13;
for ( i = 0; i != 100000000; ++i )
{
randSeed = i;
v5 = func7(v3, argv, envp);
envp = (const char **)(36 * (v5 / 0x24));
if ( v5 % 0x24 == 6 )
{
v6 = func7(v3, argv, envp);
envp = (const char **)(36 * (v6 / 0x24));
if ( v6 % 0x24 == 19 )
{
v7 = func7(v3, argv, envp);
envp = (const char **)(36 * (v7 / 0x24));
if ( v7 % 0x24 == 16 )
{
v8 = func7(v3, argv, envp);
envp = (const char **)(36 * (v8 / 0x24));
if ( v8 % 0x24 == 29 )
{
v9 = func7(v3, argv, envp);
argv = (const char **)(v9 % 0x24);
++v12[v9 % 0x24];
v3 = "Next number mod 36: %i\n";
printf("Next number mod 36: %i\n", argv);
}
}
}
}
}
for ( j = 0LL; j != 36; ++j )
printf("Ind: %i, P: %i\n", (unsigned int)j, (unsigned int)v12[j]);
return 0;
}

original_code:
int main()
{
 int i;
 int seed;
 int columncnt;
 int probs[36] = {0};
 for (i = 0; i < 100000000; i++) {
  randSeed = i;
  if (rand()%36 == 6 && rand()%36 == 19 && rand()%36 == 16 && rand()%36 == 29) {
   int next = rand()%36;
   probs[next]++;
   printf("Next number mod 36: %i\n", next);
  }
 }
 for (i = 0; i < 36; i++) {
  printf("Ind: %i, P: %i\n", i, probs[i]);
 }
 return 0;
}

variable_types: seed int

variable_types: columncnt int

variable_types:
probs
int [36]

variable_types: next int


repo: GeneralZero/ATF

decompiled_code:
void sig_USR2()
{
;
}

canonical_decompiled_code:
void func0()
{
;
}

original_code:
void sig_USR2(int sig)
{
 gl_env.sigflag = gl_env.sigflag;
}

variable_types: sig int


original_code:
void sig_USR1(int sig)
{
 gl_env.sigflag = 1;
}


decompiled_code:
void __noreturn sig_ALRM()
{
exit(0);
}

canonical_decompiled_code:
void __noreturn func2()
{
exit(0);
}

original_code:
void sig_ALRM(int sig)
{
 exit(0);
}

canonical_original_code:
void sig_ALRM(int sig)
{
 exit(0);
}


decompiled_code:
int __fastcall send_pid(int a1)
{
int v2; // ebp
int result; // eax

v2 = 32;
do
{
if ( a1 >= 0 )
kill(gl_env, 10);
else
kill(gl_env, 12);
a1 *= 2;
result = usleep(0x7D0u);
--v2;
}
while ( v2 );
return result;
}

canonical_decompiled_code:
int __fastcall func3(int a1)
{
int v2; // ebp
int result; // eax

v2 = 32;
do
{
if ( a1 >= 0 )
kill(gl_env, 10);
else
kill(gl_env, 12);
a1 *= 2;
result = usleep(0x7D0u);
--v2;
}
while ( v2 );
return result;
}

original_code:
void send_pid(pid_t mypid)
{
 int i;
 pid_t mask = 1<<(8*sizeof(pid_t)-1);
 for(i=0; i<sizeof(pid_t)*8; i++)
 {
  if(mask&mypid)
   kill(gl_env.cpid, 12);
  else
   kill(gl_env.cpid, 10);
  mypid = mypid << 1;
  usleep(2000);
 }
}

canonical_original_code:
void send_pid(int mypid)
{
 int i;
 int mask = 1<<(8*sizeof(pid_t)-1);
 for(i=0; i<sizeof(pid_t)*8; i++)
 {
  if(mask&mypid)
   kill(gl_env.cpid, 12);
  else
   kill(gl_env.cpid, 10);
  mypid = mypid << 1;
  usleep(2000);
 }
}

variable_types: mypid int

variable_types: mask int


decompiled_code:
int __fastcall send_char(char a1)
{
int v2; // ebp
int result; // eax

v2 = 8;
do
{
if ( a1 >= 0 )
kill(gl_env, 10);
else
kill(gl_env, 12);
a1 *= 2;
signal(12, (__sighandler_t)sig_USR2);
result = pause();
--v2;
}
while ( v2 );
return result;
}

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

v2 = 8;
do
{
if ( a1 >= 0 )
kill(gl_env, 10);
else
kill(gl_env, 12);
a1 *= 2;
signal(12, (__sighandler_t)sig_USR2);
result = pause();
--v2;
}
while ( v2 );
return result;
}

original_code:
void send_char(char output)
{
 int i,j;
 char mask = 1 << (8*sizeof(char)-1);
 for(j=0; j<8*sizeof(char); j++)
 {
  if(mask&output)
  {
   kill(gl_env.cpid, 12);
  }
  else
  {
   kill(gl_env.cpid, 10);
  }
  output = output << 1;
  signal(12,sig_USR2);
  pause();
 }
}

canonical_original_code:
void send_char(char output)
{
 int i; int j;
 char mask = 1 << (8*sizeof(char)-1);
 for(j=0; j<8*sizeof(char); j++)
 {
  if(mask&output)
  {
   kill(gl_env.cpid, 12);
  }
  else
  {
   kill(gl_env.cpid, 10);
  }
  output = output << 1;
  signal(12,sig_USR2);
  pause();
 }
}

variable_types: output char

variable_types: i int

variable_types: j int

variable_types: mask char


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

if ( argc <= 2 )
{
my_str("Use: server_PID [arg2 ...]\n", argv, envp);
}
else
{
v3 = my_vect2str(argv + 2);
v4 = my_strlen(v3);
*((_WORD *)&gl_env + 2) = 0;
gl_env = my_atoi(argv[1]);
signal(10, (__sighandler_t)sig_USR1);
signal(14, (__sighandler_t)sig_ALRM);
v5 = getpid();
send_pid(v5);
if ( !*((_WORD *)&gl_env + 2) )
{
alarm(2u);
pause();
}
alarm(0);
my_str("Sending:", sig_ALRM, v6);
if ( v4 > 0 )
{
v7 = (char *)v3;
v8 = v3 + (unsigned int)(v4 - 1) + 1;
do
{
my_char((unsigned int)*v7);
send_char(*v7++);
}
while ( v7 != (char *)v8 );
}
send_char(0);
my_char(10LL);
}
return 0;
}

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

if ( argc <= 2 )
{
my_str("Use: server_PID [arg2 ...]\n", argv, envp);
}
else
{
v3 = my_vect2str(argv + 2);
v4 = my_strlen(v3);
*((_WORD *)&gl_env + 2) = 0;
gl_env = my_atoi(argv[1]);
signal(10, (__sighandler_t)sig_USR1);
signal(14, (__sighandler_t)sig_ALRM);
v5 = getpid();
func3(v5);
if ( !*((_WORD *)&gl_env + 2) )
{
alarm(2u);
pause();
}
alarm(0);
my_str("Sending:", sig_ALRM, v6);
if ( v4 > 0 )
{
v7 = (char *)v3;
v8 = v3 + (unsigned int)(v4 - 1) + 1;
do
{
my_char((unsigned int)*v7);
func4(*v7++);
}
while ( v7 != (char *)v8 );
}
func4(0);
my_char(10LL);
}
return 0;
}

original_code:
int main(int argc, char** argv)
{
 if(argc>2)
 {
  char* message = my_vect2str(&argv[2]);
  int size = my_strlen(message);
  int i;
  gl_env.sigflag =0;
  gl_env.cpid = my_atoi(argv[1]);
  signal(10,sig_USR1);
  signal(14,sig_ALRM);
  send_pid(getpid());
  if(!gl_env.sigflag)
  {
   alarm(2);
   pause();
  }
  alarm(0);
  my_str("Sending:");
  for(i=0; i<size; i++)
  {
   my_char(message[i]);
   send_char(message[i]);
  }
  send_char('\0');
  my_char('\n');
 }
 else
  my_str("Use: server_PID [arg2 ...]\n");
}

canonical_original_code:
int main(int argc, char **argv)
{
 if(argc>2)
 {
  char *message = my_vect2str(&argv[2]);
  int size = my_strlen(message);
  int i;
  gl_env.sigflag =0;
  gl_env.cpid = my_atoi(argv[1]);
  signal(10,sig_USR1);
  signal(14,sig_ALRM);
  send_pid(getpid());
  if(!gl_env.sigflag)
  {
   alarm(2);
   pause();
  }
  alarm(0);
  my_str("Sending:");
  for(i=0; i<size; i++)
  {
   my_char(message[i]);
   send_char(message[i]);
  }
  send_char('\0');
  my_char('\n');
 }
 else
  my_str("Use: server_PID [arg2 ...]\n");
}

variable_types:
argv
char **

variable_types:
message
char *

variable_types: size int


ihashremove

original_code:
void* ihashremove(t_ihashtable *ht, int k){
     int home,i,j,pos;
     void* ret;
     ret = (void*)malloc(sizeof(void));
     if((ht!=((void *)0))&&(ht->n>0)&&(k>=0)){
          home = ht->h(k)%ht->M;
          for(pos = home,i=1; ht->HT[pos]!=(void*)0;i++)
               if((ht->HT[pos]!=(void*)1)&&(k==ht->getkey(ht->HT[pos]))){
                    ret = ht->HT[pos];
                    ht->HT[pos] = (void*)1;
                    break;
               }
          return ret;
     }
     return (void*)-1;
 }

canonical_original_code:
void *ihashremove(struct s_ihashtable *ht, int k){
     int home; int i; int j; int pos;
     void *ret;
     ret = (void *)malloc(sizeof(void));
     if((ht!=((void *)0))&&(ht->n>0)&&(k>=0)){
          home = ht->h(k)%ht->M;
          for(pos = home,i=1; ht->HT[pos]!=(void *)0;i++)
               if((ht->HT[pos]!=(void *)1)&&(k==ht->getkey(ht->HT[pos]))){
                    ret = ht->HT[pos];
                    ht->HT[pos] = (void *)1;
                    break;
               }
          return ret;
     }
     return (void *)-1;
 }

variable_types: struct s_ihashtable { void **HT; int M; int n; int (*getkey)(void *); int (*h)(int); int (*p)(int, int); } *

variable_types: k int

variable_types: home int

variable_types: pos int

variable_types:
ret
void *

return_type: void *


original_code:
int my_atoi(char* s_int)
{
 char flag=1;
 if(s_int[0] == '-')
 {
  flag = -1;
  s_int = &s_int[1];
 }
 int i;
 int ret;
 for(i=0, ret =0 ; s_int[i]!='\0' && s_int[i]!='.'; i++)
 {
  ret *= 10;
  ret += s_int[i] - '0';
 }
 return ret*flag;
}

canonical_original_code:
int my_atoi(char *s_int)
{
 char flag = 1;
 if(s_int[0] == '-')
 {
  flag = -1;
  s_int = &s_int[1];
 }
 int i;
 int ret;
 for(i=0, ret =0 ; s_int[i]!='\0' && s_int[i]!='.'; i++)
 {
  ret *= 10;
  ret += s_int[i] - '0';
 }
 return ret*flag;
}

variable_types: flag char

variable_types: ret int


decompiled_code:
__int64 __fastcall my_num_base(int a1, char *a2)
{
unsigned int v3; // eax
unsigned int v4; // ebp
unsigned int v5; // ecx
unsigned int v6; // ebx
int v7; // er13
unsigned int v8; // esi
int v10; // ebx

if ( a2 )
{
v3 = my_strlen(a2);
v4 = v3;
if ( v3 )
{
if ( a1 < 0 )
{
my_char(45LL);
if ( v4 != 1 )
{
v5 = -a1;
goto LABEL_5;
}
v10 = ~a1;
}
else
{
v5 = a1;
if ( v3 != 1 )
{
LABEL_5:
v6 = 1;
if ( v4 > v5 )
goto LABEL_19;
do
v6 *= v4;
while ( v5 / v6 >= v4 );
if ( v6 )
{
LABEL_19:
do
{
v7 = v5 % v6;
my_char((unsigned int)a2[v5 / v6]);
v5 = v7;
v8 = v6;
v6 /= v4;
}
while ( v4 <= v8 );
}
return my_char(10LL);
}
v10 = a1 - 1;
if ( !a1 )
return my_char(10LL);
}
do
my_char((unsigned int)*a2);
while ( v10-- );
return my_char(10LL);
}
}
return my_str("Invalid Base.");
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, char *a2)
{
unsigned int v3; // eax
unsigned int v4; // ebp
unsigned int v5; // ecx
unsigned int v6; // ebx
int v7; // er13
unsigned int v8; // esi
int v10; // ebx

if ( a2 )
{
v3 = my_strlen(a2);
v4 = v3;
if ( v3 )
{
if ( a1 < 0 )
{
my_char(45LL);
if ( v4 != 1 )
{
v5 = -a1;
goto LABEL_5;
}
v10 = ~a1;
}
else
{
v5 = a1;
if ( v3 != 1 )
{
LABEL_5:
v6 = 1;
if ( v4 > v5 )
goto LABEL_19;
do
v6 *= v4;
while ( v5 / v6 >= v4 );
if ( v6 )
{
LABEL_19:
do
{
v7 = v5 % v6;
my_char((unsigned int)a2[v5 / v6]);
v5 = v7;
v8 = v6;
v6 /= v4;
}
while ( v4 <= v8 );
}
return my_char(10LL);
}
v10 = a1 - 1;
if ( !a1 )
return my_char(10LL);
}
do
my_char((unsigned int)*a2);
while ( v10-- );
return my_char(10LL);
}
}
return my_str("Invalid Base.");
}

original_code:
void my_num_base(int num, char* string)
{
 unsigned int exp;
 unsigned int u;
 unsigned int base;
 if(string !=((void*)0) && (base = my_strlen(string)) > 0)
 {
  u=num;
  if (num <0){
   my_char('-');
   u = num * -1;
  }
  if(base==1)
   while(u-->0)
    my_char(*string);
  else
  {
   for(exp=1;(u/exp)>=base ;exp*=base);
   for(; exp; u%=exp, exp/=base)
    my_char(string[u/exp]);
  }
  my_char('\n');
 }
 else
  my_str("Invalid Base.");
}

canonical_original_code:
void my_num_base(int num, char *string)
{
 unsigned int exp;
 unsigned int u;
 unsigned int base;
 if(string !=((void *)0) && (base = my_strlen(string)) > 0)
 {
  u=num;
  if (num <0){
   my_char('-');
   u = num * -1;
  }
  if(base==1)
   while(u-->0)
    my_char(*string);
  else
  {
   for(exp=1;(u/exp)>=base ;exp*=base);
   for(; exp; u%=exp, exp/=base)
    my_char(string[u/exp]);
  }
  my_char('\n');
 }
 else
  my_str("Invalid Base.");
}

variable_types: num int

variable_types:
string
char *

variable_types:
exp
unsigned int

variable_types:
u
unsigned int

variable_types:
base
unsigned int


cccomp

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

result = 0LL;
if ( a1 != 0 )
{
if ( (_QWORD)a1 )
{
if ( *((_QWORD *)&a1 + 1) )
result = (unsigned int)(*(char *)a1 - (char)**((_BYTE **)&a1 + 1));
else
result = 1LL;
}
else
{
result = 0xFFFFFFFFLL;
}
}
return result;
}

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

result = 0LL;
if ( a1 != 0 )
{
if ( (_QWORD)a1 )
{
if ( *((_QWORD *)&a1 + 1) )
result = (unsigned int)(*(char *)a1 - (char)**((_BYTE **)&a1 + 1));
else
result = 1LL;
}
else
{
result = 0xFFFFFFFFLL;
}
}
return result;
}

original_code:
int cccomp(void *x ,void *y){
 if(x==((void *)0) && y==((void *)0))
  return 0;
 if(x==((void *)0))
  return -1;
 if(y==((void *)0))
  return 1;
 return *((char*)x) - *((char*)y);
}

canonical_original_code:
int cccomp(void *x, void *y){
 if(x==((void *)0) && y==((void *)0))
  return 0;
 if(x==((void *)0))
  return -1;
 if(y==((void *)0))
  return 1;
 return *((char *)x) - *((char *)y);
}

variable_types:
x
void *

variable_types:
y
void *


decompiled_code:
__int64 __fastcall my_str(char *a1)
{
char v2; // di
char *v3; // rbx
__int64 result; // rax

if ( a1 )
{
v2 = *a1;
if ( v2 )
{
v3 = a1 + 1;
do
{
result = my_char((unsigned int)v2);
v2 = *v3++;
}
while ( v2 );
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *a1)
{
char v2; // di
char *v3; // rbx
__int64 result; // rax

if ( a1 )
{
v2 = *a1;
if ( v2 )
{
v3 = a1 + 1;
do
{
result = my_char((unsigned int)v2);
v2 = *v3++;
}
while ( v2 );
}
}
return result;
}

original_code:
void my_str(char* string)
{
 if(string != ((void*)0)){
  int i;
  for(i=0;string[i]!='\0';i++)
   my_char(string[i]);
 }
}

canonical_original_code:
void my_str(char *string)
{
 if(string != ((void *)0)){
  int i;
  for(i=0;string[i]!='\0';i++)
   my_char(string[i]);
 }
}

variable_types: i int


original_code:
char* my_strcat(char* dest, char* src)
{
 if(dest==((void*)0) && src==((void*)0))
  return ((void*)0);
 else if(dest==((void*)0))
  return dest;
 if(src==((void*)0))
  return dest;
 else
 {
  int length = my_strlen(dest);
  int count=0;
  for(count=0; src[count]!='\0'; count++)
   dest[length+count] = src[count];
  dest[count+length] = '\0';
 }
 return dest;
}

canonical_original_code:
char *my_strcat(char *dest, char *src)
{
 if(dest==((void *)0) && src==((void *)0))
  return ((void *)0);
 else if(dest==((void *)0))
  return dest;
 if(src==((void *)0))
  return dest;
 else
 {
  int length = my_strlen(dest);
  int count = 0;
  for(count=0; src[count]!='\0'; count++)
   dest[length+count] = src[count];
  dest[count+length] = '\0';
 }
 return dest;
}

variable_types:
dest
char *

variable_types:
src
char *

variable_types: length int

variable_types: count int

return_type: char *


ihashsearch

original_code:
int ihashsearch(t_ihashtable* ht,int key){
 int home,i,pos;
 if((ht== ((void *)0))||(ht->n<=0))
         return -1;
      if((home=ht->h(key))<0)
         return -1;
 for(pos = home,i=1;(ht->HT[pos]!=(void*)0); i++, pos = (home+ht->p(key,i))%ht->M)
  if(ht->getkey(ht->HT[pos])==key)
   return pos;
 return -1;
}

canonical_original_code:
int ihashsearch(struct s_ihashtable *ht, int key){
 int home; int i; int pos;
 if((ht== ((void *)0))||(ht->n<=0))
         return -1;
      if((home=ht->h(key))<0)
         return -1;
 for(pos = home,i=1;(ht->HT[pos]!=(void *)0); i++, pos = (home+ht->p(key,i))%ht->M)
  if(ht->getkey(ht->HT[pos])==key)
   return pos;
 return -1;
}

variable_types: struct s_ihashtable { void **HT; int M; int n; int (*getkey)(void *); int (*h)(int); int (*p)(int, int); } *

variable_types: key int

variable_types: home int


decompiled_code:
__int64 init_terminal()
{
char *v0; // rax
int v1; // ebx
struct termios v3; // [rsp+0h] [rbp-48h] BYREF

v0 = ttyname(0);
v1 = open(v0, 1);
*((_DWORD *)&gl_env + 44) = dup(1);
dup2(v1, 1);
if ( tcgetattr(v1, (struct termios *)(unsigned int)((_DWORD)&gl_env + 112)) < 0 )
{
my_str("Error: linebakup");
exit(1);
}
*(__m128i *)&v3.c_iflag = _mm_load_si128((const __m128i *)&gl_env + 7);
*(__m128i *)&v3.c_line = _mm_load_si128((const __m128i *)&gl_env + 8);
*(__m128i *)&v3.c_cc[15] = _mm_load_si128((const __m128i *)&gl_env + 9);
*(_QWORD *)&v3.c_cc[31] = *((_QWORD *)&gl_env + 20);
v3.c_ospeed = *((_DWORD *)&gl_env + 42);
v3.c_lflag &= 0xFFFFFFF4;
*(_WORD *)&v3.c_cc[5] = 769;
if ( tcsetattr(v1, 2, &v3) < 0 )
{
my_str("Error with tcsetattr");
exit(1);
}
return get_win_size();
}

canonical_decompiled_code:
__int64 func0()
{
char *v0; // rax
int v1; // ebx
struct termios v3; // [rsp+0h] [rbp-48h] BYREF

v0 = ttyname(0);
v1 = open(v0, 1);
*((_DWORD *)&gl_env + 44) = dup(1);
dup2(v1, 1);
if ( tcgetattr(v1, (struct termios *)(unsigned int)((_DWORD)&gl_env + 112)) < 0 )
{
my_str("Error: linebakup");
exit(1);
}
*(__m128i *)&v3.c_iflag = _mm_load_si128((const __m128i *)&gl_env + 7);
*(__m128i *)&v3.c_line = _mm_load_si128((const __m128i *)&gl_env + 8);
*(__m128i *)&v3.c_cc[15] = _mm_load_si128((const __m128i *)&gl_env + 9);
*(_QWORD *)&v3.c_cc[31] = *((_QWORD *)&gl_env + 20);
v3.c_ospeed = *((_DWORD *)&gl_env + 42);
v3.c_lflag &= 0xFFFFFFF4;
*(_WORD *)&v3.c_cc[5] = 769;
if ( tcsetattr(v1, 2, &v3) < 0 )
{
my_str("Error with tcsetattr");
exit(1);
}
return get_win_size();
}

original_code:
void init_terminal()
{
 char* name;
 int fd;
 struct termios ts;
 name = ttyname(0);
 fd = open(name, 01);
 gl_env.stdio_backup = dup(1);
 dup2(fd, 1);
 if(tcgetattr(fd, &(gl_env.line_backup)) < 0)
 {
  my_str("Error: linebakup");
  exit(1);
 }
 ts = gl_env.line_backup;
 ts.c_lflag &= ~(0000002 | 0000010 | 0000001);
 ts.c_cc[5] = 1;
 ts.c_cc[6] = 3;
 if(tcsetattr(fd, 2, &ts) < 0)
 {
  my_str("Error with tcsetattr");
  exit(1);
 }
 get_win_size();
}

canonical_original_code:
void init_terminal()
{
 char *name;
 int fd;
 struct termios ts;
 name = ttyname(0);
 fd = open(name, 01);
 gl_env.stdio_backup = dup(1);
 dup2(fd, 1);
 if(tcgetattr(fd, &(gl_env.line_backup)) < 0)
 {
  my_str("Error: linebakup");
  exit(1);
 }
 ts = gl_env.line_backup;
 ts.c_lflag &= ~(0000002 | 0000010 | 0000001);
 ts.c_cc[5] = 1;
 ts.c_cc[6] = 3;
 if(tcsetattr(fd, 2, &ts) < 0)
 {
  my_str("Error with tcsetattr");
  exit(1);
 }
 get_win_size();
}

variable_types:
name
char *

variable_types: fd int

variable_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }


decompiled_code:
size_t __fastcall setup_elem(int a1, __int64 a2)
{
const char **v2; // rbx
size_t result; // rax
__int64 v4; // rbp
size_t v5; // rbx
_QWORD *v6; // r13
__int64 v7; // r13
glob_t v8; // [rsp+0h] [rbp-78h] BYREF

glob(*(const char **)(a2 + 8), 17, 0LL, &v8);
if ( a1 > 2 )
{
v2 = (const char **)(a2 + 16);
do
glob(*v2++, 49, 0LL, &v8);
while ( v2 != (const char **)(a2 + 8LL * (unsigned int)(a1 - 3) + 24) );
}
*((_QWORD *)&gl_env + 24) = my_xmalloc((unsigned int)(24 * LODWORD(v8.gl_pathc)));
result = v8.gl_pathc;
if ( v8.gl_pathc )
{
v4 = 0LL;
v5 = 0LL;
do
{
v6 = (_QWORD *)(*((_QWORD *)&gl_env + 24) + v4);
*v6 = my_strdup(v8.gl_pathv[v5]);
v7 = *((_QWORD *)&gl_env + 24) + v4;
*(_DWORD *)(v7 + 8) = my_strlen(v8.gl_pathv[v5]);
*(_DWORD *)(*((_QWORD *)&gl_env + 24) + v4 + 12) = 0;
*(_DWORD *)(*((_QWORD *)&gl_env + 24) + v4 + 16) = (int)v5 % *((unsigned __int16 *)&gl_env + 52);
*(_DWORD *)(*((_QWORD *)&gl_env + 24) + v4 + 20) = 0;
result = v8.gl_pathc;
++v5;
v4 += 24LL;
}
while ( v8.gl_pathc > v5 );
}
*((_DWORD *)&gl_env + 45) = result;
*((_DWORD *)&gl_env + 46) = 0;
return result;
}

canonical_decompiled_code:
size_t __fastcall func1(int a1, __int64 a2)
{
const char **v2; // rbx
size_t result; // rax
__int64 v4; // rbp
size_t v5; // rbx
_QWORD *v6; // r13
__int64 v7; // r13
glob_t v8; // [rsp+0h] [rbp-78h] BYREF

glob(*(const char **)(a2 + 8), 17, 0LL, &v8);
if ( a1 > 2 )
{
v2 = (const char **)(a2 + 16);
do
glob(*v2++, 49, 0LL, &v8);
while ( v2 != (const char **)(a2 + 8LL * (unsigned int)(a1 - 3) + 24) );
}
*((_QWORD *)&gl_env + 24) = my_xmalloc((unsigned int)(24 * LODWORD(v8.gl_pathc)));
result = v8.gl_pathc;
if ( v8.gl_pathc )
{
v4 = 0LL;
v5 = 0LL;
do
{
v6 = (_QWORD *)(*((_QWORD *)&gl_env + 24) + v4);
*v6 = my_strdup(v8.gl_pathv[v5]);
v7 = *((_QWORD *)&gl_env + 24) + v4;
*(_DWORD *)(v7 + 8) = my_strlen(v8.gl_pathv[v5]);
*(_DWORD *)(*((_QWORD *)&gl_env + 24) + v4 + 12) = 0;
*(_DWORD *)(*((_QWORD *)&gl_env + 24) + v4 + 16) = (int)v5 % *((unsigned __int16 *)&gl_env + 52);
*(_DWORD *)(*((_QWORD *)&gl_env + 24) + v4 + 20) = 0;
result = v8.gl_pathc;
++v5;
v4 += 24LL;
}
while ( v8.gl_pathc > v5 );
}
*((_DWORD *)&gl_env + 45) = result;
*((_DWORD *)&gl_env + 46) = 0;
return result;
}

original_code:
void setup_elem(int argc, char** argv)
{
 glob_t gt;
 int i;
 glob(argv[1], (1 << 4) | (1 << 0), ((void *)0), &gt);
 for (i = 2; i < argc; i++)
 {
  glob(argv[i], (1 << 4) | (1 << 0) | (1 << 5), ((void *)0), &gt);
 }
 gl_env.elements =(t_elem*)my_xmalloc(gt.gl_pathc*sizeof(t_elem));
 for(i=0; i<gt.gl_pathc; i++)
 {
  gl_env.elements[i].elem = my_strdup(gt.gl_pathv[i]);
  gl_env.elements[i].size = my_strlen(gt.gl_pathv[i]);
  gl_env.elements[i].x = 0;
  gl_env.elements[i].y = i%(gl_env.win.ws_row);
  gl_env.elements[i].mode = 0;
 }
 gl_env.nbelems=gt.gl_pathc;
 gl_env.pos = 0;
}

canonical_original_code:
void setup_elem(int argc, char **argv)
{
 struct glob_t gt;
 int i;
 glob(argv[1], (1 << 4) | (1 << 0), ((void *)0), &gt);
 for (i = 2; i < argc; i++)
 {
  glob(argv[i], (1 << 4) | (1 << 0) | (1 << 5), ((void *)0), &gt);
 }
 gl_env.elements =(struct s_elem *)my_xmalloc(gt.gl_pathc*sizeof(t_elem));
 for(i=0; i<gt.gl_pathc; i++)
 {
  gl_env.elements[i].elem = my_strdup(gt.gl_pathv[i]);
  gl_env.elements[i].size = my_strlen(gt.gl_pathv[i]);
  gl_env.elements[i].x = 0;
  gl_env.elements[i].y = i%(gl_env.win.ws_row);
  gl_env.elements[i].mode = 0;
 }
 gl_env.nbelems=gt.gl_pathc;
 gl_env.pos = 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: struct glob_t { unsigned long gl_pathc; char **gl_pathv; unsigned long gl_offs; int gl_flags; void (*gl_closedir)(void *); void *(*gl_readdir)(void *); void *(*gl_opendir)(char *); int (*gl_lstat)(char *, void *); int (*gl_stat)(char *, void *); }

user_defined_types: struct s_elem { char *elem; int size; int x; int y; int mode; }


decompiled_code:
char *init_caps()
{
char *v0; // rax
char *v1; // rax
char *v2; // rax
char *v3; // rax
char *v4; // rax
char *v5; // rax
char *v6; // rax
char *v7; // rax
char *v8; // rax
char *v9; // rax
char *v10; // rax
char *result; // rax
char v12[1024]; // [rsp+0h] [rbp-C08h] BYREF
char *v13[257]; // [rsp+400h] [rbp-808h] BYREF

v0 = getenv("TERM");
if ( !v0 )
{
my_str("Error: getenv term");
exit(1);
}
if ( tgetent(v12, v0) < 0 )
{
my_str("Error: tgetent term");
exit(1);
}
v1 = tgetstr("ku", v13);
*(_QWORD *)&gl_env.c_line = v1;
if ( !v1 )
{
*(_QWORD *)&gl_env.c_line = "\x1B[A";
my_str("up");
}
v2 = tgetstr("kd", v13);
*(_QWORD *)&gl_env.c_cc[7] = v2;
if ( !v2 )
{
*(_QWORD *)&gl_env.c_cc[7] = "\x1B[B";
my_str("down");
}
*(_QWORD *)&gl_env.c_iflag = tgetstr("kl", v13);
if ( !*(_QWORD *)&gl_env.c_iflag )
{
*(_QWORD *)&gl_env.c_iflag = "\x1B[D";
my_str("left");
}
v3 = tgetstr("kr", v13);
*(_QWORD *)&gl_env.c_cflag = v3;
if ( !v3 )
{
*(_QWORD *)&gl_env.c_cflag = "\x1B[C";
my_str("right");
}
v4 = tgetstr("cl", v13);
*((_QWORD *)&gl_env + 10) = v4;
if ( !v4 )
{
my_str("Error with seting clear");
exit(1);
}
v5 = tgetstr("cm", v13);
*((_QWORD *)&gl_env + 9) = v5;
if ( !v5 )
{
my_str("Error with seting clear");
exit(1);
}
v6 = tgetstr("so", v13);
*(_QWORD *)&gl_env.c_ospeed = v6;
if ( !v6 )
{
my_str("Error with seting start highlite");
exit(1);
}
v7 = tgetstr("se", v13);
*((_QWORD *)&gl_env + 8) = v7;
if ( !v7 )
{
my_str("Error with seting stop highlight");
exit(1);
}
v8 = tgetstr("us", v13);
*(_QWORD *)&gl_env.c_cc[23] = v8;
if ( !v8 )
{
my_str("Error with seting start underline");
exit(1);
}
v9 = tgetstr("ue", v13);
*(_QWORD *)&gl_env.c_cc[31] = v9;
if ( !v9 )
{
my_str("Error with seting end underline");
exit(1);
}
v10 = tgetstr("vi", v13);
*((_QWORD *)&gl_env + 11) = v10;
if ( !v10 )
{
my_str("Error with seting cursor off");
*((_QWORD *)&gl_env + 11) = "\x1B[?25h";
}
result = tgetstr("ve", v13);
*((_QWORD *)&gl_env + 12) = result;
if ( !result )
{
*((_QWORD *)&gl_env + 12) = "\x1B[?25l";
result = (char *)my_str("Error with seting cursor on");
}
*(_QWORD *)&gl_env.c_cc[15] = "\x1B";
return result;
}

canonical_decompiled_code:
char *func2()
{
char *v0; // rax
char *v1; // rax
char *v2; // rax
char *v3; // rax
char *v4; // rax
char *v5; // rax
char *v6; // rax
char *v7; // rax
char *v8; // rax
char *v9; // rax
char *v10; // rax
char *result; // rax
char v12[1024]; // [rsp+0h] [rbp-C08h] BYREF
char *v13[257]; // [rsp+400h] [rbp-808h] BYREF

v0 = getenv("TERM");
if ( !v0 )
{
my_str("Error: getenv term");
exit(1);
}
if ( tgetent(v12, v0) < 0 )
{
my_str("Error: tgetent term");
exit(1);
}
v1 = tgetstr("ku", v13);
*(_QWORD *)&gl_env.c_line = v1;
if ( !v1 )
{
*(_QWORD *)&gl_env.c_line = "\x1B[A";
my_str("up");
}
v2 = tgetstr("kd", v13);
*(_QWORD *)&gl_env.c_cc[7] = v2;
if ( !v2 )
{
*(_QWORD *)&gl_env.c_cc[7] = "\x1B[B";
my_str("down");
}
*(_QWORD *)&gl_env.c_iflag = tgetstr("kl", v13);
if ( !*(_QWORD *)&gl_env.c_iflag )
{
*(_QWORD *)&gl_env.c_iflag = "\x1B[D";
my_str("left");
}
v3 = tgetstr("kr", v13);
*(_QWORD *)&gl_env.c_cflag = v3;
if ( !v3 )
{
*(_QWORD *)&gl_env.c_cflag = "\x1B[C";
my_str("right");
}
v4 = tgetstr("cl", v13);
*((_QWORD *)&gl_env + 10) = v4;
if ( !v4 )
{
my_str("Error with seting clear");
exit(1);
}
v5 = tgetstr("cm", v13);
*((_QWORD *)&gl_env + 9) = v5;
if ( !v5 )
{
my_str("Error with seting clear");
exit(1);
}
v6 = tgetstr("so", v13);
*(_QWORD *)&gl_env.c_ospeed = v6;
if ( !v6 )
{
my_str("Error with seting start highlite");
exit(1);
}
v7 = tgetstr("se", v13);
*((_QWORD *)&gl_env + 8) = v7;
if ( !v7 )
{
my_str("Error with seting stop highlight");
exit(1);
}
v8 = tgetstr("us", v13);
*(_QWORD *)&gl_env.c_cc[23] = v8;
if ( !v8 )
{
my_str("Error with seting start underline");
exit(1);
}
v9 = tgetstr("ue", v13);
*(_QWORD *)&gl_env.c_cc[31] = v9;
if ( !v9 )
{
my_str("Error with seting end underline");
exit(1);
}
v10 = tgetstr("vi", v13);
*((_QWORD *)&gl_env + 11) = v10;
if ( !v10 )
{
my_str("Error with seting cursor off");
*((_QWORD *)&gl_env + 11) = "\x1B[?25h";
}
result = tgetstr("ve", v13);
*((_QWORD *)&gl_env + 12) = result;
if ( !result )
{
*((_QWORD *)&gl_env + 12) = "\x1B[?25l";
result = (char *)my_str("Error with seting cursor on");
}
*(_QWORD *)&gl_env.c_cc[15] = "\x1B";
return result;
}

original_code:
void init_caps()
{
 char area[2048];
 char* ptr;
 char tempBuff[1024];
 if((ptr = (char *)getenv("TERM")) == ((void *)0))
 {
  my_str("Error: getenv term");
  exit(1);
 }
 if((tgetent( tempBuff, ptr )) < 0)
 {
  my_str("Error: tgetent term");
  exit(1);
 }
 if(!(gl_env.up = tgetstr("ku", (char**)(&area))))
 {
  gl_env.up = "\E[A";
  my_str("up");
 }
 if(!(gl_env.down = (char *)tgetstr("kd", (char**)(&area))))
 {
  gl_env.down = "\E[B";
  my_str("down");
 }
 if(!(gl_env.left = (char *)tgetstr("kl", (char**)(&area))))
 {
  gl_env.left = "\E[D";
  my_str("left");
 }
 if(!(gl_env.right = (char *)tgetstr("kr", (char**)(&area))))
 {
  gl_env.right = "\E[C";
  my_str("right");
 }
 if(!(gl_env.clear = (char *)tgetstr("cl", (char**)(&area))))
 {
  my_str("Error with seting clear");
  exit(1);
 }
 if(!(gl_env.move = (char *)tgetstr("cm", (char**)(&area))))
 {
  my_str("Error with seting clear");
  exit(1);
 }
 if(!(gl_env.standout = (char *)tgetstr("so" , (char**)(&area))))
 {
  my_str("Error with seting start highlite");
  exit(1);
 }
 if(!(gl_env.stand_end = (char *)tgetstr("se", (char**)(&area))))
 {
  my_str("Error with seting stop highlight");
  exit(1);
 }
 if(!(gl_env.underline = (char *)tgetstr("us", (char**)(&area))))
 {
  my_str("Error with seting start underline");
  exit(1);
 }
 if(!(gl_env.under_end = (char *)tgetstr("ue", (char**)(&area))))
 {
  my_str("Error with seting end underline");
  exit(1);
 }
 if(!(gl_env.cursoroff = (char *)tgetstr("vi", (char**)(&area))))
 {
  my_str("Error with seting cursor off");
  gl_env.cursoroff = "\E[?25h";
 }
 if(!(gl_env.cursoron = (char *)tgetstr("ve", (char**)(&area))))
 {
  gl_env.cursoron = "\E[?25l";
  my_str("Error with seting cursor on");
 }
 gl_env.esc = "\033";
}

canonical_original_code:
void init_caps()
{
 char area[2048];
 char *ptr;
 char tempBuff[1024];
 if((ptr = (char *)getenv("TERM")) == ((void *)0))
 {
  my_str("Error: getenv term");
  exit(1);
 }
 if((tgetent( tempBuff, ptr )) < 0)
 {
  my_str("Error: tgetent term");
  exit(1);
 }
 if(!(gl_env.up = tgetstr("ku", (char **)(&area))))
 {
  gl_env.up = "\E[A";
  my_str("up");
 }
 if(!(gl_env.down = (char *)tgetstr("kd", (char **)(&area))))
 {
  gl_env.down = "\E[B";
  my_str("down");
 }
 if(!(gl_env.left = (char *)tgetstr("kl", (char **)(&area))))
 {
  gl_env.left = "\E[D";
  my_str("left");
 }
 if(!(gl_env.right = (char *)tgetstr("kr", (char **)(&area))))
 {
  gl_env.right = "\E[C";
  my_str("right");
 }
 if(!(gl_env.clear = (char *)tgetstr("cl", (char **)(&area))))
 {
  my_str("Error with seting clear");
  exit(1);
 }
 if(!(gl_env.move = (char *)tgetstr("cm", (char **)(&area))))
 {
  my_str("Error with seting clear");
  exit(1);
 }
 if(!(gl_env.standout = (char *)tgetstr("so" , (char **)(&area))))
 {
  my_str("Error with seting start highlite");
  exit(1);
 }
 if(!(gl_env.stand_end = (char *)tgetstr("se", (char **)(&area))))
 {
  my_str("Error with seting stop highlight");
  exit(1);
 }
 if(!(gl_env.underline = (char *)tgetstr("us", (char **)(&area))))
 {
  my_str("Error with seting start underline");
  exit(1);
 }
 if(!(gl_env.under_end = (char *)tgetstr("ue", (char **)(&area))))
 {
  my_str("Error with seting end underline");
  exit(1);
 }
 if(!(gl_env.cursoroff = (char *)tgetstr("vi", (char **)(&area))))
 {
  my_str("Error with seting cursor off");
  gl_env.cursoroff = "\E[?25h";
 }
 if(!(gl_env.cursoron = (char *)tgetstr("ve", (char **)(&area))))
 {
  gl_env.cursoron = "\E[?25l";
  my_str("Error with seting cursor on");
 }
 gl_env.esc = "\033";
}

variable_types:
area
char [2048]

variable_types:
ptr
char *

variable_types:
tempBuff
char [1024]


decompiled_code:
__int64 __fastcall my_int(int a1)
{
unsigned int v1; // er12
unsigned int v2; // ebx
int v3; // eax
unsigned int v4; // eax

v1 = a1;
if ( a1 < 0 )
{
my_char(45LL);
v1 = -a1;
}
if ( v1 <= 9 )
{
my_char((unsigned int)(char)(v1 + 48));
}
else
{
v2 = 1;
do
v2 *= 10;
while ( v1 / v2 > 9 );
if ( v2 )
{
do
{
v3 = v1 / v2;
v1 %= v2;
my_char((unsigned int)(char)(v3 + 48));
v4 = v2;
v2 /= 0xAu;
}
while ( v4 > 9 );
}
}
return my_char(10LL);
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
unsigned int v1; // er12
unsigned int v2; // ebx
int v3; // eax
unsigned int v4; // eax

v1 = a1;
if ( a1 < 0 )
{
my_char(45LL);
v1 = -a1;
}
if ( v1 <= 9 )
{
my_char((unsigned int)(char)(v1 + 48));
}
else
{
v2 = 1;
do
v2 *= 10;
while ( v1 / v2 > 9 );
if ( v2 )
{
do
{
v3 = v1 / v2;
v1 %= v2;
my_char((unsigned int)(char)(v3 + 48));
v4 = v2;
v2 /= 0xAu;
}
while ( v4 > 9 );
}
}
return my_char(10LL);
}

original_code:
void my_int(int num)
{
 unsigned int exp;
 unsigned int u;
 unsigned int base = 10;
 u=num;
 if (num <0){
  my_char('-');
  u = num * -1;
 }
 for(exp=1;(u/exp)>=base ;exp*=base);
 for(; exp; u%=exp, exp/=base)
  my_char('0'+(u/exp));
 my_char('\n');
}

variable_types: num int

variable_types:
exp
unsigned int

variable_types:
u
unsigned int

variable_types:
base
unsigned int


iicomph

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

result = 0LL;
if ( a1 != 0 )
{
if ( (_QWORD)a1 )
{
if ( *((_QWORD *)&a1 + 1) )
result = (unsigned int)(*(_DWORD *)a1 - **((_DWORD **)&a1 + 1));
else
result = 1LL;
}
else
{
result = 0xFFFFFFFFLL;
}
}
return result;
}

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

result = 0LL;
if ( a1 != 0 )
{
if ( (_QWORD)a1 )
{
if ( *((_QWORD *)&a1 + 1) )
result = (unsigned int)(*(_DWORD *)a1 - **((_DWORD **)&a1 + 1));
else
result = 1LL;
}
else
{
result = 0xFFFFFFFFLL;
}
}
return result;
}

original_code:
int iicomph(void* x ,void* y){
 int a,b;
        if(x==((void *)0) && y==((void *)0))
                return 0;
        if(x==((void *)0))
                return -1;
        if(y==((void *)0))
                return 1;
 a = *((int *) x);
 b = *((int *) y);
        return a-b;
}

canonical_original_code:
int iicomph(void *x, void *y){
 int a; int b;
        if(x==((void *)0) && y==((void *)0))
                return 0;
        if(x==((void *)0))
                return -1;
        if(y==((void *)0))
                return 1;
 a = *((int *) x);
 b = *((int *) y);
        return a-b;
}

variable_types:
x
void *

variable_types:
y
void *

variable_types: a int

variable_types: b int


popMinHeap

decompiled_code:
__int64 __fastcall popMinHeap(__int64 a1, __int64 a2)
{
int v2; // edx
__int64 result; // rax

if ( !a1 )
return 0LL;
if ( !a2 )
return 0LL;
v2 = *(_DWORD *)(a1 + 4);
result = 0LL;
if ( v2 > 0 )
{
*(_DWORD *)(a1 + 4) = v2 - 1;
swap(a1, 0LL);
shiftMinDown(a1, 0LL, a2);
result = *(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL * *(int *)(a1 + 4));
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
int v2; // edx
__int64 result; // rax

if ( !a1 )
return 0LL;
if ( !a2 )
return 0LL;
v2 = *(_DWORD *)(a1 + 4);
result = 0LL;
if ( v2 > 0 )
{
*(_DWORD *)(a1 + 4) = v2 - 1;
swap(a1, 0LL);
shiftMinDown(a1, 0LL, a2);
result = *(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL * *(int *)(a1 + 4));
}
return result;
}

original_code:
void* popMinHeap(t_heap* theHeap, t_comph comp){
 if(theHeap==((void *)0)||comp==((void *)0)||theHeap->current_size<=0)
  return ((void *)0);
 swap(theHeap,0,--(theHeap->current_size));
 shiftMinDown(theHeap, 0, comp);
 return theHeap->tree[theHeap->current_size];
}

canonical_original_code:
void *popMinHeap(struct s_heap *theHeap, int (*comp)(void *, void *)){
 if(theHeap==((void *)0)||comp==((void *)0)||theHeap->current_size<=0)
  return ((void *)0);
 swap(theHeap,0,--(theHeap->current_size));
 shiftMinDown(theHeap, 0, comp);
 return theHeap->tree[theHeap->current_size];
}

variable_types: struct s_heap { int size_max; int current_size; void **tree; } *

variable_types:
comp
int (*)(void *, void *)


theEndOfTheCli

decompiled_code:
void __fastcall theEndOfTheCli(int a1)
{
if ( a1 == 2 )
{
my_str("\nEnd of ze Client\n");
exit(1);
}
}

canonical_decompiled_code:
void __fastcall func0(int a1)
{
if ( a1 == 2 )
{
my_str("\nEnd of ze Client\n");
exit(1);
}
}

original_code:
void theEndOfTheCli(int sig)
{
 if(sig == 2)
 {
  my_str("\nEnd of ze Client\n");
  exit(1);
 }
}

variable_types: sig int


main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
__int16 v3; // bp
int v4; // er12
struct hostent *v5; // rax
char *v6; // rbp
int v7; // er8
int i; // ecx
char *v9; // rax
__int64 v10; // rsi
char *v11; // rdi
char v12; // dl
unsigned int v13; // eax
unsigned int v14; // eax
int v15; // er13
__int64 v16; // rsi
int j; // ecx
__int64 v18; // rax
char v19; // dl
struct sockaddr dest; // [rsp+0h] [rbp-438h] BYREF
char buf[1064]; // [rsp+10h] [rbp-428h] BYREF

if ( argc <= 2 )
{
my_str("Use: [Machine_Name] [port_num]\n");
exit(1);
}
v3 = my_atoi(argv[2]);
v4 = socket(2, 1, 0);
if ( v4 < 0 )
my_str("ERROR opening socket");
v5 = gethostbyname(argv[1]);
if ( !v5 )
{
my_str("ERROR, no such host\n");
exit(0);
}
*(_QWORD *)&dest.sa_family = 2LL;
*(_QWORD *)&dest.sa_data[6] = 0LL;
memmove(&dest.sa_data[2], *(const void **)v5->h_addr_list, v5->h_length);
*(_WORD *)dest.sa_data = _bswap_16(v3);
if ( connect(v4, &dest, 0x10u) < 0 )
{
my_str("Could not connect to ");
my_str(argv[1]);
my_char(10LL);
exit(0);
}
signal(2, theEndOfTheCli);
memset(buf, 0, 0x400uLL);
v6 = (char *)my_xmalloc(50LL);
my_str("Nickname: ");
v7 = read(0, v6, 0x31uLL);
v6[v7] = 0;
for ( i = 0; ; i = v10 )
{
v9 = &v6[i];
do
{
v10 = (unsigned int)((_DWORD)v9 - (_DWORD)v6);
v11 = v9;
v12 = *v9;
if ( !*v9 )
{
if ( v7 <= 0 )
my_str("Invalid nick name\n");
v13 = my_strlen(v6, v10);
if ( (int)write(v4, v6, v13) < 0 )
my_str("ERROR writing to socket\n");
LABEL_26:
memset(buf, 0, 0x400uLL);
my_str(v6);
my_str(": ");
v16 = (unsigned int)read(0, buf, 0x3FFuLL);
for ( j = 0; ; buf[j] = 0 )
{
v18 = j;
do
{
j = v18;
v19 = buf[v18];
if ( !v19 )
{
if ( (int)v16 <= 0 )
my_str("Unable to read\n");
v14 = my_strlen(buf, v16);
v15 = write(v4, buf, v14);
if ( !(unsigned int)my_strncmp(buf, "/exit", 5LL) )
exit(0);
if ( !(unsigned int)my_strncmp(buf, "/nick", 5LL) )
{
free(v6);
v6 = (char *)my_strdup(&buf[6]);
}
if ( v15 <= 0 )
my_str("Unable to Write\n");
goto LABEL_26;
}
++v18;
}
while ( v19 != 10 );
}
}
++v9;
}
while ( v12 != 10 );
*v11 = 0;
}
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
__int16 v3; // bp
int v4; // er12
struct hostent *v5; // rax
char *v6; // rbp
int v7; // er8
int i; // ecx
char *v9; // rax
__int64 v10; // rsi
char *v11; // rdi
char v12; // dl
unsigned int v13; // eax
unsigned int v14; // eax
int v15; // er13
__int64 v16; // rsi
int j; // ecx
__int64 v18; // rax
char v19; // dl
struct sockaddr dest; // [rsp+0h] [rbp-438h] BYREF
char buf[1064]; // [rsp+10h] [rbp-428h] BYREF

if ( argc <= 2 )
{
my_str("Use: [Machine_Name] [port_num]\n");
exit(1);
}
v3 = my_atoi(argv[2]);
v4 = socket(2, 1, 0);
if ( v4 < 0 )
my_str("ERROR opening socket");
v5 = gethostbyname(argv[1]);
if ( !v5 )
{
my_str("ERROR, no such host\n");
exit(0);
}
*(_QWORD *)&dest.sa_family = 2LL;
*(_QWORD *)&dest.sa_data[6] = 0LL;
memmove(&dest.sa_data[2], *(const void **)v5->h_addr_list, v5->h_length);
*(_WORD *)dest.sa_data = func1(v3);
if ( connect(v4, &dest, 0x10u) < 0 )
{
my_str("Could not connect to ");
my_str(argv[1]);
my_char(10LL);
exit(0);
}
signal(2, theEndOfTheCli);
memset(buf, 0, 0x400uLL);
v6 = (char *)my_xmalloc(50LL);
my_str("Nickname: ");
v7 = read(0, v6, 0x31uLL);
v6[v7] = 0;
for ( i = 0; ; i = v10 )
{
v9 = &v6[i];
do
{
v10 = (unsigned int)((_DWORD)v9 - (_DWORD)v6);
v11 = v9;
v12 = *v9;
if ( !*v9 )
{
if ( v7 <= 0 )
my_str("Invalid nick name\n");
v13 = my_strlen(v6, v10);
if ( (int)write(v4, v6, v13) < 0 )
my_str("ERROR writing to socket\n");
LABEL_26:
memset(buf, 0, 0x400uLL);
my_str(v6);
my_str(": ");
v16 = (unsigned int)read(0, buf, 0x3FFuLL);
for ( j = 0; ; buf[j] = 0 )
{
v18 = j;
do
{
j = v18;
v19 = buf[v18];
if ( !v19 )
{
if ( (int)v16 <= 0 )
my_str("Unable to read\n");
v14 = my_strlen(buf, v16);
v15 = write(v4, buf, v14);
if ( !(unsigned int)my_strncmp(buf, "/exit", 5LL) )
exit(0);
if ( !(unsigned int)my_strncmp(buf, "/nick", 5LL) )
{
free(v6);
v6 = (char *)my_strdup(&buf[6]);
}
if ( v15 <= 0 )
my_str("Unable to Write\n");
goto LABEL_26;
}
++v18;
}
while ( v19 != 10 );
}
}
++v9;
}
while ( v12 != 10 );
*v11 = 0;
}
}

original_code:
int main(int argc, char** argv)
{
 int sockfd;
 int n;
 int port_num;
 char buffer[1024];
 char* nname;
 struct sockaddr_in server_adderess;
 struct hostent* server;
 if(argc < 3)
 {
  my_str("Use: [Machine_Name] [port_num]\n");
  exit(1);
 }
 port_num = my_atoi(argv[2]);
    sockfd = socket(2, SOCK_STREAM, 0);
    if (sockfd < 0)
        my_str("ERROR opening socket");
    server = gethostbyname(argv[1]);
 if (server == ((void *)0))
 {
        my_str("ERROR, no such host\n");
        exit(0);
    }
 bzero((char*)&server_adderess, sizeof(server_adderess));
 server_adderess.sin_family = 2;
 bcopy((char *)server->h_addr_list[0], (char *)&server_adderess.sin_addr.s_addr, server->h_length);
 server_adderess.sin_port = htons(port_num);
 if(connect(sockfd, (struct sockaddr *)&server_adderess, sizeof(server_adderess))<0)
 {
  my_str("Could not connect to ");
  my_str(argv[1]);
  my_char('\n');
  exit(0);
 }
 signal(2, theEndOfTheCli);
 bzero(buffer, 1024);
 nname = (char*)my_xmalloc(50*sizeof(char));
 my_str("Nickname: ");
 n = read(0, nname, 49);
 nname[n] = '\0';
 int i=0;
 while(nname[i] != '\0')
 {
  if(nname[i++] == '\n')
   nname[--i] = '\0';
 }
 if(n <= 0)
  my_str("Invalid nick name\n");
 n = write(sockfd, nname, my_strlen(nname));
 if(n < 0)
  my_str("ERROR writing to socket\n");
 while(1)
 {
  bzero(buffer, 1024);
  my_str(nname);
  my_str(": ");
  n = read(0, buffer, 1024 -1);
  i=0;
  while(buffer[i] != '\0')
  {
   if(buffer[i++] == '\n')
    buffer[--i] = '\0';
  }
  if(n <= 0)
   my_str("Unable to read\n");
  n = write(sockfd, buffer, my_strlen(buffer));
  if(my_strncmp(buffer, "/exit", 5) == 0)
   exit(0);
  if(my_strncmp(buffer, "/nick", 5) == 0)
  {
   free(nname);
   nname = my_strdup(&buffer[6]);
  }
  if(n <= 0)
   my_str("Unable to Write\n");
 }
}

canonical_original_code:
int main(int argc, char **argv)
{
 int sockfd;
 int n;
 int port_num;
 char buffer[1024];
 char *nname;
 struct sockaddr_in server_adderess;
 struct hostent *server;
 if(argc < 3)
 {
  my_str("Use: [Machine_Name] [port_num]\n");
  exit(1);
 }
 port_num = my_atoi(argv[2]);
    sockfd = socket(2, SOCK_STREAM, 0);
    if (sockfd < 0)
        my_str("ERROR opening socket");
    server = gethostbyname(argv[1]);
 if (server == ((void *)0))
 {
        my_str("ERROR, no such host\n");
        exit(0);
    }
 bzero((char *)&server_adderess, sizeof(server_adderess));
 server_adderess.sin_family = 2;
 bcopy((char *)server->h_addr_list[0], (char *)&server_adderess.sin_addr.s_addr, server->h_length);
 server_adderess.sin_port = htons(port_num);
 if(connect(sockfd, (struct sockaddr *)&server_adderess, sizeof(server_adderess))<0)
 {
  my_str("Could not connect to ");
  my_str(argv[1]);
  my_char('\n');
  exit(0);
 }
 signal(2, theEndOfTheCli);
 bzero(buffer, 1024);
 nname = (char *)my_xmalloc(50*sizeof(char));
 my_str("Nickname: ");
 n = read(0, nname, 49);
 nname[n] = '\0';
 int i = 0;
 while(nname[i] != '\0')
 {
  if(nname[i++] == '\n')
   nname[--i] = '\0';
 }
 if(n <= 0)
  my_str("Invalid nick name\n");
 n = write(sockfd, nname, my_strlen(nname));
 if(n < 0)
  my_str("ERROR writing to socket\n");
 while(1)
 {
  bzero(buffer, 1024);
  my_str(nname);
  my_str(": ");
  n = read(0, buffer, 1024 -1);
  i=0;
  while(buffer[i] != '\0')
  {
   if(buffer[i++] == '\n')
    buffer[--i] = '\0';
  }
  if(n <= 0)
   my_str("Unable to read\n");
  n = write(sockfd, buffer, my_strlen(buffer));
  if(my_strncmp(buffer, "/exit", 5) == 0)
   exit(0);
  if(my_strncmp(buffer, "/nick", 5) == 0)
  {
   free(nname);
   nname = my_strdup(&buffer[6]);
  }
  if(n <= 0)
   my_str("Unable to Write\n");
 }
}

variable_types: argc int

variable_types: sockfd int

variable_types: n int

variable_types:
buffer
char [1024]

variable_types:
nname
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: struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; } *

variable_types: i int

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


decompiled_code:
__int64 show_elems()
{
__int64 result; // rax
__int64 v1; // rbx
int v2; // ebp
int v3; // er13
int v4; // er14
__int64 v5; // rcx

get_win_size();
*((_DWORD *)&gl_env + 43) = 0;
result = ((__int64 (*)(void))term_clear)();
if ( *((int *)&gl_env + 45) > 0 )
{
v1 = 0LL;
v2 = 0;
v3 = 0;
v4 = 0;
while ( 1 )
{
if ( *((unsigned __int16 *)&gl_env + 52) <= v3 )
{
v4 += v2 + 5;
v2 = 0;
v3 = 0;
}
v5 = *((_QWORD *)&gl_env + 24) + 24 * v1;
if ( v2 < *(_DWORD *)(v5 + 8) )
v2 = *(_DWORD *)(v5 + 8);
if ( v2 + v4 >= *((unsigned __int16 *)&gl_env + 53) )
break;
*(_DWORD *)(v5 + 12) = v4;
*(_DWORD *)(*((_QWORD *)&gl_env + 24) + 24 * v1 + 16) = v3;
result = refresh_out();
++v3;
if ( *((_DWORD *)&gl_env + 45) <= (int)++v1 )
goto LABEL_10;
}
term_clear((unsigned int)v1);
*((_DWORD *)&gl_env + 43) = 1;
term_move(0LL, 0LL);
result = my_str("Please Enlarge Window");
}
LABEL_10:
if ( !*((_DWORD *)&gl_env + 43) )
result = refresh_in();
return result;
}

canonical_decompiled_code:
__int64 func0()
{
__int64 result; // rax
__int64 v1; // rbx
int v2; // ebp
int v3; // er13
int v4; // er14
__int64 v5; // rcx

get_win_size();
*((_DWORD *)&gl_env + 43) = 0;
result = ((__int64 (*)(void))term_clear)();
if ( *((int *)&gl_env + 45) > 0 )
{
v1 = 0LL;
v2 = 0;
v3 = 0;
v4 = 0;
while ( 1 )
{
if ( *((unsigned __int16 *)&gl_env + 52) <= v3 )
{
v4 += v2 + 5;
v2 = 0;
v3 = 0;
}
v5 = *((_QWORD *)&gl_env + 24) + 24 * v1;
if ( v2 < *(_DWORD *)(v5 + 8) )
v2 = *(_DWORD *)(v5 + 8);
if ( v2 + v4 >= *((unsigned __int16 *)&gl_env + 53) )
break;
*(_DWORD *)(v5 + 12) = v4;
*(_DWORD *)(*((_QWORD *)&gl_env + 24) + 24 * v1 + 16) = v3;
result = refresh_out();
++v3;
if ( *((_DWORD *)&gl_env + 45) <= (int)++v1 )
goto LABEL_10;
}
term_clear((unsigned int)v1);
*((_DWORD *)&gl_env + 43) = 1;
term_move(0LL, 0LL);
result = my_str("Please Enlarge Window");
}
LABEL_10:
if ( !*((_DWORD *)&gl_env + 43) )
result = refresh_in();
return result;
}

original_code:
void show_elems()
{
 int i;
 int x;
 int y;
 int max;
 char space = 5;
 get_win_size();
 gl_env.flag = 0;
 term_clear();
 for(i=0, x=0, y=0, max=0; i<gl_env.nbelems; i++, y++)
 {
  if(y>= gl_env.win.ws_row)
  {
   x+=(max+ space);
   y=0;
   max=0;
  }
  if(max <gl_env.elements[i].size)
   max = gl_env.elements[i].size;
  if((x+max) >= gl_env.win.ws_col)
  {
   term_clear();
   gl_env.flag = 1;
   term_move(0,0);
   my_str("Please Enlarge Window");
   break;
  }
  gl_env.elements[i].x = x;
  gl_env.elements[i].y = y;
  refresh_out(i);
 }
 if(!gl_env.flag)
  refresh_in();
}

variable_types: x int

variable_types: y int

variable_types: max int

variable_types: space char


decompiled_code:
int get_win_size()
{
return ioctl(0, 0x5413uLL, (unsigned int)((_DWORD)&gl_env + 104));
}

canonical_decompiled_code:
int func0()
{
return ioctl(0, 0x5413uLL, (unsigned int)((_DWORD)&gl_env + 104));
}

original_code:
char get_win_size()
{
 ioctl(0, 0x5413, &gl_env.win);
}

return_type: char