decompiled_code:
__int64 __fastcall bnet_thread_server(__int64 a1, unsigned int a2, __int16 a3, __int64 a4)
{
int v5; // ebx
int *v6; // rbp
int v7; // eax
fd_set readfds; // [rsp+10h] [rbp-F8h] BYREF
int optval; // [rsp+9Ch] [rbp-6Ch] BYREF
struct sockaddr v11; // [rsp+A0h] [rbp-68h] BYREF
socklen_t addr_len; // [rsp+B8h] [rbp-50h] BYREF
int v13; // [rsp+BCh] [rbp-4Ch] BYREF
struct sockaddr addr; // [rsp+C0h] [rbp-48h] BYREF

addr.sa_family = 2;
*(_DWORD *)&addr.sa_data[2] = 0;
*(_WORD *)addr.sa_data = _bswap_16(a3);
optval = 1;
v5 = socket(2, 1, 0);
setsockopt(v5, 1, 2, &optval, 4u);
if ( bind(v5, &addr, 0x10u) )
{
puts("Can not bind address!");
return 0xFFFFFFFFLL;
}
if ( listen(v5, 20) )
{
puts("Can not listen the port!");
return 0xFFFFFFFFLL;
}
workq_init(a1, a2, a4);
v6 = _errno_location();
do
{
LABEL_4:
while ( 1 )
{
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[(unsigned int)v5 >> 6] |= 1LL << v5;
*v6 = 0;
if ( select(v5 + 1, &readfds, 0LL, 0LL, 0LL) < 0 )
break;
if ( (readfds.fds_bits[v5 / 64] & (1LL << (v5 % 64))) != 0 )
{
while ( 1 )
{
addr_len = 16;
v7 = accept(v5, &v11, &addr_len);
v13 = v7;
if ( v7 >= 0 )
break;
if ( *v6 != 4 )
goto LABEL_4;
}
printf("%s %d connectd fd:%d\n", "server.c", 348LL, (unsigned int)v7);
workq_add(a1, &v13, 0LL, 0LL);
}
}
}
while ( *v6 == 4 );
close(v5);
return workq_destroy(a1);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, unsigned int a2, __int16 a3, __int64 a4)
{
int v5; // ebx
int *v6; // rbp
int v7; // eax
fd_set readfds; // [rsp+10h] [rbp-F8h] BYREF
int optval; // [rsp+9Ch] [rbp-6Ch] BYREF
struct sockaddr v11; // [rsp+A0h] [rbp-68h] BYREF
socklen_t addr_len; // [rsp+B8h] [rbp-50h] BYREF
int v13; // [rsp+BCh] [rbp-4Ch] BYREF
struct sockaddr addr; // [rsp+C0h] [rbp-48h] BYREF

addr.sa_family = 2;
*(_DWORD *)&addr.sa_data[2] = 0;
*(_WORD *)addr.sa_data = func2(a3);
optval = 1;
v5 = socket(2, 1, 0);
setsockopt(v5, 1, 2, &optval, 4u);
if ( bind(v5, &addr, 0x10u) )
{
puts("Can not bind address!");
return 0xFFFFFFFFLL;
}
if ( listen(v5, 20) )
{
puts("Can not listen the port!");
return 0xFFFFFFFFLL;
}
workq_init(a1, a2, a4);
v6 = _errno_location();
do
{
LABEL_4:
while ( 1 )
{
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[(unsigned int)v5 >> 6] |= 1LL << v5;
*v6 = 0;
if ( select(v5 + 1, &readfds, 0LL, 0LL, 0LL) < 0 )
break;
if ( (readfds.fds_bits[v5 / 64] & (1LL << (v5 % 64))) != 0 )
{
while ( 1 )
{
addr_len = 16;
v7 = accept(v5, &v11, &addr_len);
v13 = v7;
if ( v7 >= 0 )
break;
if ( *v6 != 4 )
goto LABEL_4;
}
printf("%s %d connectd fd:%d\n", "server.c", 348LL, (unsigned int)v7);
workq_add(a1, &v13, 0LL, 0LL);
}
}
}
while ( *v6 == 4 );
close(v5);
return workq_destroy(a1);
}

original_code:
int bnet_thread_server(workq_t *client_wq,int max_client_count,int port,void *handle_client_request(void * data)) {
 struct sockaddr_in s_addr;
 s_addr.sin_family = 2;
 s_addr.sin_addr.s_addr = ((in_addr_t) 0x00000000);
 s_addr.sin_port = htons(port);
 int newsockfd, fd;
 socklen_t clilen;
 struct sockaddr cli_addr;
 int reuse = 1;
 fd = socket(2, SOCK_STREAM, 0);
 setsockopt(fd, 1, 2, &reuse, sizeof(int));
 if (bind(fd, (struct sockaddr*) &s_addr, sizeof(struct sockaddr))!= 0) {
   printf("Can not bind address!\n");
   return -1;
 }
 if (listen(fd, 20) != 0) {
  printf("Can not listen the port!\n");
  return -1;
 }
     workq_init(client_wq,max_client_count,handle_client_request);
 for (;;) {
  unsigned int maxfd = 0;
  fd_set sockset;
  do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&sockset)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&sockset)->__fds_bits)[(((unsigned) fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << (((unsigned) fd) % (8 * (int) sizeof (__fd_mask)))))));
  maxfd = maxfd > (unsigned) fd ? maxfd : fd;
  (*__errno_location ()) = 0;
  if (select(maxfd + 1, &sockset, ((void *)0), ((void *)0), ((void *)0)) < 0) {
   if ((*__errno_location ()) == 4)
    continue;
   close(fd);
   break;
  }
  if (((((&sockset)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
   do {
    clilen = sizeof(cli_addr);
    newsockfd = accept(fd, &cli_addr, &clilen);
   } while (newsockfd < 0 && (*__errno_location ()) == 4);
   if (newsockfd < 0) {
    continue;
   }
   printf("%s %d connectd fd:%d\n","server.c",348,newsockfd);
   workq_add(client_wq,(void *)&newsockfd,((void *)0),0);
  }
 }
 workq_destroy(client_wq);
}

canonical_original_code:
int bnet_thread_server(struct workq_tag *client_wq, int max_client_count, int port, void *handle_client_request(void *data)) {
 struct sockaddr_in s_addr;
 s_addr.sin_family = 2;
 s_addr.sin_addr.s_addr = ((unsigned int) 0x00000000);
 s_addr.sin_port = htons(port);
 int newsockfd; int fd;
 unsigned int clilen;
 struct sockaddr cli_addr;
 int reuse = 1;
 fd = socket(2, SOCK_STREAM, 0);
 setsockopt(fd, 1, 2, &reuse, sizeof(int));
 if (bind(fd, (struct sockaddr *) &s_addr, sizeof(struct sockaddr))!= 0) {
   printf("Can not bind address!\n");
   return -1;
 }
 if (listen(fd, 20) != 0) {
  printf("Can not listen the port!\n");
  return -1;
 }
     workq_init(client_wq,max_client_count,handle_client_request);
 for (;;) {
  unsigned int maxfd = 0;
  struct fd_set sockset;
  do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&sockset)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&sockset)->__fds_bits)[(((unsigned int) fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << (((unsigned int) fd) % (8 * (int) sizeof (__fd_mask)))))));
  maxfd = maxfd > (unsigned int) fd ? maxfd : fd;
  (*__errno_location ()) = 0;
  if (select(maxfd + 1, &sockset, ((void *)0), ((void *)0), ((void *)0)) < 0) {
   if ((*__errno_location ()) == 4)
    continue;
   close(fd);
   break;
  }
  if (((((&sockset)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
   do {
    clilen = sizeof(cli_addr);
    newsockfd = accept(fd, &cli_addr, &clilen);
   } while (newsockfd < 0 && (*__errno_location ()) == 4);
   if (newsockfd < 0) {
    continue;
   }
   printf("%s %d connectd fd:%d\n","server.c",348,newsockfd);
   workq_add(client_wq,(void *)&newsockfd,((void *)0),0);
  }
 }
 workq_destroy(client_wq);
}

variable_types: struct workq_tag { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } work; union pthread_attr_t { char __size[56]; long __align; } attr; struct workq_ele_tag { struct workq_ele_tag *next; void *data; } *first; int valid; int quit; int max_workers; int num_workers; int idle_workers; void *(*engine)(void *arg); } *

variable_types: port int

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

user_defined_types: struct workq_tag { union pthread_mutex_t mutex; union pthread_cond_t work; union pthread_attr_t attr; struct workq_ele_tag *first; int valid; int quit; int max_workers; int num_workers; int idle_workers; void *(*engine)(void *arg); }


repo: zhoupj/Deduper

decompiled_code:
__int64 __fastcall test_data(unsigned int *a1)
{
unsigned int v1; // ebx
int v2; // eax
int v3; // ebp
double v5; // [rsp+0h] [rbp-10068h]
double v6; // [rsp+8h] [rbp-10060h]
int v7; // [rsp+1Ch] [rbp-1004Ch] BYREF
char v8[65552]; // [rsp+20h] [rbp-10048h] BYREF
struct timeval v9; // [rsp+10030h] [rbp-38h] BYREF
struct timeval tv; // [rsp+10040h] [rbp-28h] BYREF

v7 = 0;
v1 = *a1;
v2 = open("./test_bandwidth", 577, 420LL);
if ( v2 < 0 )
{
printf("%s,%d create file error!\n", "server.c", 152LL);
}
else
{
v3 = v2;
gettimeofday(&tv, 0LL);
v5 = 0.0;
while ( (int)bnet_recv(v1, v8, &v7) > 0 )
v5 = (double)v7 + v5;
gettimeofday(&v9, 0LL);
v6 = (double)(LODWORD(v9.tv_usec) - LODWORD(tv.tv_usec)) / 1000000.0
+ (double)(LODWORD(v9.tv_sec) - LODWORD(tv.tv_sec))
+ 0.0;
close(v3);
close(v1);
printf("total time=%.4f %.4fMB/s\n", v6, v5 / v6 / 1036288.0);
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int *a1)
{
unsigned int v1; // ebx
int v2; // eax
int v3; // ebp
double v5; // [rsp+0h] [rbp-10068h]
double v6; // [rsp+8h] [rbp-10060h]
int v7; // [rsp+1Ch] [rbp-1004Ch] BYREF
char v8[65552]; // [rsp+20h] [rbp-10048h] BYREF
struct timeval v9; // [rsp+10030h] [rbp-38h] BYREF
struct timeval tv; // [rsp+10040h] [rbp-28h] BYREF

v7 = 0;
v1 = *a1;
v2 = open("./test_bandwidth", 577, 420LL);
if ( v2 < 0 )
{
printf("%s,%d create file error!\n", "server.c", 152LL);
}
else
{
v3 = v2;
gettimeofday(&tv, 0LL);
v5 = 0.0;
while ( (int)bnet_recv(v1, v8, &v7) > 0 )
v5 = (double)v7 + v5;
gettimeofday(&v9, 0LL);
v6 = (double)(LODWORD(v9.tv_usec) - LODWORD(tv.tv_usec)) / 1000000.0
+ (double)(LODWORD(v9.tv_sec) - LODWORD(tv.tv_sec))
+ 0.0;
close(v3);
close(v1);
printf("total time=%.4f %.4fMB/s\n", v6, v5 / v6 / 1036288.0);
}
return 0LL;
}

original_code:
void* test_data(void *arg){
 struct timeval start,end;
 char buf[64*1024 +1];
 int len=0;
 double total_time=0;
 double total_len=0;
 int fd=-1;
 int socket=*(int *)arg;
 if((fd = open("./test_bandwidth", 0100 | 01000 | 01,00644))<0){
   printf("%s,%d create  file error!\n","server.c",152);
  return ((void *)0);
  }
 gettimeofday(&start, ((void *)0));
 while(bnet_recv(socket,buf, &len)>0){
  total_len+=len;
 }
 gettimeofday(&end, ((void *)0));
 (total_time)+=(end.tv_usec-start.tv_usec)*1.0/1000000+(end.tv_sec-start.tv_sec);
 close(fd);
 close(socket);
 printf("total time=%.4f  %.4fMB/s\n",total_time,total_len/total_time/1036288);
 return ((void *)0);
}

canonical_original_code:
void *test_data(void *arg){
 struct timeval start; struct timeval end;
 char buf[-1];
 int len = 0;
 double total_time = 0;
 double total_len = 0;
 int fd = -1;
 int socket = *(int *)arg;
 if((fd = open("./test_bandwidth", 0100 | 01000 | 01,00644))<0){
   printf("%s,%d create  file error!\n","server.c",152);
  return ((void *)0);
  }
 gettimeofday(&start, ((void *)0));
 while(bnet_recv(socket,buf, &len)>0){
  total_len+=len;
 }
 gettimeofday(&end, ((void *)0));
 (total_time)+=(end.tv_usec-start.tv_usec)*1.0/1000000+(end.tv_sec-start.tv_sec);
 close(fd);
 close(socket);
 printf("total time=%.4f  %.4fMB/s\n",total_time,total_len/total_time/1036288);
 return ((void *)0);
}

variable_types:
arg
void *

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

variable_types:
buf
char [-1]

variable_types: len int

variable_types: fd int

variable_types: socket int

return_type: void *


usage

decompiled_code:
int usage()
{
puts("\n===========usage============");
puts("-p # pipeline ");
puts("-s number # recv fingerprints number per transmittion ");
puts("-v #display filenames sent or recieved ");
puts("-t # test network bandwidth ");
return puts("-h # give this help list ");
}

canonical_decompiled_code:
int func4()
{
puts("\n===========usage============");
puts("-p # pipeline ");
puts("-s number # recv fingerprints number per transmittion ");
puts("-v #display filenames sent or recieved ");
puts("-t # test network bandwidth ");
return puts("-h # give this help list ");
}

original_code:
void usage(){
 printf("\n===========usage============\n");
 printf("-p             # pipeline \n");
 printf("-s number      # recv fingerprints number per transmittion \n");
 printf("-v             #display filenames sent or recieved \n");
 printf("-t             # test network bandwidth \n");
 printf("-h             # give this help list \n");
}

return_type: void


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // er13
int v4; // eax
char *v5; // r15
int v6; // er12
const char *v8; // rsi
const char *v9; // rsi
unsigned int v10; // [rsp+8h] [rbp-80h]
int v11; // [rsp+Ch] [rbp-7Ch]
__int64 dest[6]; // [rsp+10h] [rbp-78h] BYREF
__int16 v13; // [rsp+40h] [rbp-48h]
pthread_t th[8]; // [rsp+48h] [rbp-40h] BYREF

dest[0] = 0LL;
dest[1] = 0LL;
dest[2] = 0LL;
dest[3] = 0LL;
dest[4] = 0LL;
dest[5] = 0LL;
v13 = 0;
v3 = 0;
v10 = 0;
while ( 2 )
{
v4 = getopt(argc, (char *const *)argv, "ps:S:tvhoc:C:f:T:P:RDd:a:r:O:");
if ( v4 == -1 )
{
puts("==========parameters========== :");
printf("Operation Path=%s\n", &BackupVolPath);
v8 = "no";
if ( G_NOT_DEDUP != 1 )
v8 = "yes";
printf("deduption ?%s \n", v8);
v9 = "yes";
if ( G_PIPELINE != 1 )
v9 = "no";
printf("pipeline is true ? %s \n", v9);
printf("transmittiong size:%d \n", MAX_FINGERS_NUM);
printf("scan size:%d \n", SCAN_REGION_SIZE);
printf("caching container num when restore: %d\n", G_CONTAINER_NUM);
printf("container size:%d \n", DEFAULT_CONTAINER_SIZE);
printf("defrag method: id %d \n", G_DEFRAGMENT);
printf("capping numbers %d per %d MB \n", G_CAPPING_T, SCAN_REGION_SIZE);
if ( G_Sanitization )
{
sanitization();
}
else if ( v3 )
{
local_simulate_restore(v3);
}
else if ( v10 )
{
local_delete_job(v10);
}
else if ( G_TEST_DATA )
{
bnet_thread_server((__int64)&g_client_wq, g_client_count, 8888, (__int64)test_data);
}
else
{
make_dir();
if ( G_PIPELINE )
pthread_create(th, 0LL, (void *(*)(void *))wait_conncet, 0LL);
bnet_thread_server((__int64)&g_client_wq, g_client_count, 8888, (__int64)handle_client_request);
if ( G_PIPELINE )
pthread_join(th[0], 0LL);
}
}
else
{
switch ( v4 )
{
case 'C':
DEFAULT_CONTAINER_SIZE = (__int64)atoi(optarg) << 20;
continue;
case 'D':
G_Sanitization = 1;
continue;
case 'O':
memcpy(dest, optarg, v11 + 1);
printf("%s \n", (const char *)dest);
G_FILE_FD = open((const char *)dest, 1090, 384LL);
if ( G_FILE_FD >= 0 )
continue;
printf("%s, %d, Can not create %s !\n", "server.c", 39LL, (const char *)dest);
break;
case 'P':
v5 = optarg;
v6 = strlen(optarg);
v11 = v6;
memcpy(&BackupVolPath, v5, v6 + 1);
if ( *(&BackupVolPath + v6 - 1) != 47 )
{
*(&BackupVolPath + v6) = 47;
*(&BackupVolPath + v6 + 1) = 0;
}
continue;
case 'R':
test_read_container();
continue;
case 'S':
SCAN_REGION_SIZE = atoi(optarg) << 20;
continue;
case 'T':
G_THRESHOLD = (double)atoi(optarg) / 100.0;
printf("Threshold=%f \n", G_THRESHOLD);
continue;
case 'a':
G_CAPPING_T = atoi(optarg);
continue;
case 'c':
G_CONTAINER_NUM = atoi(optarg);
continue;
case 'd':
v10 = atoi(optarg);
continue;
case 'f':
G_DEFRAGMENT = atoi(optarg);
continue;
case 'h':
usage();
break;
case 'o':
G_NOT_DEDUP = 1;
continue;
case 'p':
G_PIPELINE = 1;
continue;
case 'r':
v3 = atoi(optarg);
continue;
case 's':
MAX_FINGERS_NUM = atoi(optarg) << 20;
continue;
case 't':
G_TEST_DATA = 1;
continue;
case 'v':
G_VERBOSE = 1;
continue;
default:
puts("Your command is wrong ");
usage();
break;
}
}
break;
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // er13
int v4; // eax
char *v5; // r15
int v6; // er12
const char *v8; // rsi
const char *v9; // rsi
unsigned int v10; // [rsp+8h] [rbp-80h]
int v11; // [rsp+Ch] [rbp-7Ch]
__int64 dest[6]; // [rsp+10h] [rbp-78h] BYREF
__int16 v13; // [rsp+40h] [rbp-48h]
pthread_t th[8]; // [rsp+48h] [rbp-40h] BYREF

dest[0] = 0LL;
dest[1] = 0LL;
dest[2] = 0LL;
dest[3] = 0LL;
dest[4] = 0LL;
dest[5] = 0LL;
v13 = 0;
v3 = 0;
v10 = 0;
while ( 2 )
{
v4 = getopt(argc, (char *const *)argv, "ps:S:tvhoc:C:f:T:P:RDd:a:r:O:");
if ( v4 == -1 )
{
puts("==========parameters========== :");
printf("Operation Path=%s\n", &BackupVolPath);
v8 = "no";
if ( G_NOT_DEDUP != 1 )
v8 = "yes";
printf("deduption ?%s \n", v8);
v9 = "yes";
if ( G_PIPELINE != 1 )
v9 = "no";
printf("pipeline is true ? %s \n", v9);
printf("transmittiong size:%d \n", MAX_FINGERS_NUM);
printf("scan size:%d \n", SCAN_REGION_SIZE);
printf("caching container num when restore: %d\n", G_CONTAINER_NUM);
printf("container size:%d \n", DEFAULT_CONTAINER_SIZE);
printf("defrag method: id %d \n", G_DEFRAGMENT);
printf("capping numbers %d per %d MB \n", G_CAPPING_T, SCAN_REGION_SIZE);
if ( G_Sanitization )
{
sanitization();
}
else if ( v3 )
{
local_simulate_restore(v3);
}
else if ( v10 )
{
local_delete_job(v10);
}
else if ( G_TEST_DATA )
{
func6((__int64)&g_client_wq, g_client_count, 8888, (__int64)test_data);
}
else
{
func5();
if ( G_PIPELINE )
pthread_create(th, 0LL, (void *(*)(void *))wait_conncet, 0LL);
func6((__int64)&g_client_wq, g_client_count, 8888, (__int64)handle_client_request);
if ( G_PIPELINE )
pthread_join(th[0], 0LL);
}
}
else
{
switch ( v4 )
{
case 'C':
DEFAULT_CONTAINER_SIZE = (__int64)atoi(optarg) << 20;
continue;
case 'D':
G_Sanitization = 1;
continue;
case 'O':
memcpy(dest, optarg, v11 + 1);
printf("%s \n", (const char *)dest);
G_FILE_FD = open((const char *)dest, 1090, 384LL);
if ( G_FILE_FD >= 0 )
continue;
printf("%s, %d, Can not create %s !\n", "server.c", 39LL, (const char *)dest);
break;
case 'P':
v5 = optarg;
v6 = strlen(optarg);
v11 = v6;
memcpy(&BackupVolPath, v5, v6 + 1);
if ( *(&BackupVolPath + v6 - 1) != 47 )
{
*(&BackupVolPath + v6) = 47;
*(&BackupVolPath + v6 + 1) = 0;
}
continue;
case 'R':
test_read_container();
continue;
case 'S':
SCAN_REGION_SIZE = atoi(optarg) << 20;
continue;
case 'T':
G_THRESHOLD = (double)atoi(optarg) / 100.0;
printf("Threshold=%f \n", G_THRESHOLD);
continue;
case 'a':
G_CAPPING_T = atoi(optarg);
continue;
case 'c':
G_CONTAINER_NUM = atoi(optarg);
continue;
case 'd':
v10 = atoi(optarg);
continue;
case 'f':
G_DEFRAGMENT = atoi(optarg);
continue;
case 'h':
func4();
break;
case 'o':
G_NOT_DEDUP = 1;
continue;
case 'p':
G_PIPELINE = 1;
continue;
case 'r':
v3 = atoi(optarg);
continue;
case 's':
MAX_FINGERS_NUM = atoi(optarg) << 20;
continue;
case 't':
G_TEST_DATA = 1;
continue;
case 'v':
G_VERBOSE = 1;
continue;
default:
puts("Your command is wrong ");
func4();
break;
}
}
break;
}
return 0;
}

original_code:
int main(int argc, char **argv){
 int opt;
 int len;
 pthread_t data_socket;
 int id=0;
 int Restore_jobid=0;
 char out_file[50]={0};
         while((opt=getopt(argc, argv, "ps:S:tvhoc:C:f:T:P:RDd:a:r:O:"))!=-1){
         switch(opt){
     case 'p':
     G_PIPELINE=1;
   break;
   case 'P':
   len=strlen(optarg);
    memcpy(BackupVolPath,optarg,len+1);
   if(BackupVolPath[len-1]!='/'){
    BackupVolPath[len]='/';
    BackupVolPath[len+1]=0;
   }
   break;
  case 'O':
    memcpy(out_file,optarg,len+1);
   printf("%s \n",out_file);
   if((G_FILE_FD= open(out_file, 02 | 0100 |02000, 0400|0200))<0){
            printf("%s, %d, Can not create %s !\n","server.c",39,out_file);
           return 0;
   }
         break;
  case 'a':
      G_CAPPING_T=atoi(optarg);
   break;
   case 's':
      MAX_FINGERS_NUM=atoi(optarg)*1024*1024;
   break;
  case 'S':
      SCAN_REGION_SIZE=atoi(optarg)*1024*1024;
   break;
  case 'c':
    G_CONTAINER_NUM=atoi(optarg);
   break;
  case 'f':
   G_DEFRAGMENT=atoi(optarg);
   break;
  case 'r':
   Restore_jobid=atoi(optarg);
   break;
  case 'R':
   test_read_container();
   break;
  case 'C':
    DEFAULT_CONTAINER_SIZE=(int64_t)atoi(optarg)*1024*1024;
   break;
  case 'D':
   G_Sanitization=1;
   break;
  case 'd':
   id=atoi(optarg);
   break;
  case 't':
   G_TEST_DATA=1;
   break;
  case 'T':
   G_THRESHOLD=(double)atoi(optarg)/100.0;
   printf("Threshold=%f \n",G_THRESHOLD);
   break;
  case 'v':
   G_VERBOSE=1;
   break;
  case 'o':
   G_NOT_DEDUP=1;
   break;
  case 'h':
   usage();
   return 0;
     default:
      printf("Your command is wrong \n");
   usage();
   return 0;
         }
         }
 printf("==========parameters========== :\n");
 printf("Operation Path=%s\n",BackupVolPath);
 printf("deduption ?%s \n",G_NOT_DEDUP==1? "no":"yes");
         printf("pipeline is true ? %s \n", G_PIPELINE==1? "yes":"no");
 printf("transmittiong size:%d \n", MAX_FINGERS_NUM);
 printf("scan size:%d \n",SCAN_REGION_SIZE);
 printf("caching container num when restore: %d\n", G_CONTAINER_NUM);
 printf("container size:%d \n",DEFAULT_CONTAINER_SIZE);
 printf("defrag method: id %d \n",G_DEFRAGMENT);
 printf("capping numbers %d per %d MB \n",G_CAPPING_T,SCAN_REGION_SIZE);
 if(G_Sanitization) {
  sanitization();
  return 0;
 }
 if(Restore_jobid){
  local_simulate_restore(Restore_jobid);
  return 0;
 }
 if(id){
  local_delete_job(id);
  return 0;
 }
 if(G_TEST_DATA){
  bnet_thread_server(&g_client_wq,g_client_count,8888,test_data);
  return 0;
 }
 make_dir();
 if(G_PIPELINE)
  pthread_create(&data_socket, ((void *)0), wait_conncet, ((void *)0));
 bnet_thread_server(&g_client_wq,g_client_count,8888,handle_client_request);
 if(G_PIPELINE)
  pthread_join(data_socket,((void *)0));
 return 0;
}

canonical_original_code:
int main(int argc, char **argv){
 int opt;
 int len;
 unsigned long data_socket;
 int id = 0;
 int Restore_jobid = 0;
 char out_file[50] = {0};
         while((opt=getopt(argc, argv, "ps:S:tvhoc:C:f:T:P:RDd:a:r:O:"))!=-1){
         switch(opt){
     case 'p':
     G_PIPELINE=1;
   break;
   case 'P':
   len=strlen(optarg);
    memcpy(BackupVolPath,optarg,len+1);
   if(BackupVolPath[len-1]!='/'){
    BackupVolPath[len]='/';
    BackupVolPath[len+1]=0;
   }
   break;
  case 'O':
    memcpy(out_file,optarg,len+1);
   printf("%s \n",out_file);
   if((G_FILE_FD= open(out_file, 02 | 0100 |02000, 0400|0200))<0){
            printf("%s, %d, Can not create %s !\n","server.c",39,out_file);
           return 0;
   }
         break;
  case 'a':
      G_CAPPING_T=atoi(optarg);
   break;
   case 's':
      MAX_FINGERS_NUM=atoi(optarg)*1024*1024;
   break;
  case 'S':
      SCAN_REGION_SIZE=atoi(optarg)*1024*1024;
   break;
  case 'c':
    G_CONTAINER_NUM=atoi(optarg);
   break;
  case 'f':
   G_DEFRAGMENT=atoi(optarg);
   break;
  case 'r':
   Restore_jobid=atoi(optarg);
   break;
  case 'R':
   test_read_container();
   break;
  case 'C':
    DEFAULT_CONTAINER_SIZE=(long)atoi(optarg)*1024*1024;
   break;
  case 'D':
   G_Sanitization=1;
   break;
  case 'd':
   id=atoi(optarg);
   break;
  case 't':
   G_TEST_DATA=1;
   break;
  case 'T':
   G_THRESHOLD=(double)atoi(optarg)/100.0;
   printf("Threshold=%f \n",G_THRESHOLD);
   break;
  case 'v':
   G_VERBOSE=1;
   break;
  case 'o':
   G_NOT_DEDUP=1;
   break;
  case 'h':
   usage();
   return 0;
     default:
      printf("Your command is wrong \n");
   usage();
   return 0;
         }
         }
 printf("==========parameters========== :\n");
 printf("Operation Path=%s\n",BackupVolPath);
 printf("deduption ?%s \n",G_NOT_DEDUP==1? "no":"yes");
         printf("pipeline is true ? %s \n", G_PIPELINE==1? "yes":"no");
 printf("transmittiong size:%d \n", MAX_FINGERS_NUM);
 printf("scan size:%d \n",SCAN_REGION_SIZE);
 printf("caching container num when restore: %d\n", G_CONTAINER_NUM);
 printf("container size:%d \n",DEFAULT_CONTAINER_SIZE);
 printf("defrag method: id %d \n",G_DEFRAGMENT);
 printf("capping numbers %d per %d MB \n",G_CAPPING_T,SCAN_REGION_SIZE);
 if(G_Sanitization) {
  sanitization();
  return 0;
 }
 if(Restore_jobid){
  local_simulate_restore(Restore_jobid);
  return 0;
 }
 if(id){
  local_delete_job(id);
  return 0;
 }
 if(G_TEST_DATA){
  bnet_thread_server(&g_client_wq,g_client_count,8888,test_data);
  return 0;
 }
 make_dir();
 if(G_PIPELINE)
  pthread_create(&data_socket, ((void *)0), wait_conncet, ((void *)0));
 bnet_thread_server(&g_client_wq,g_client_count,8888,handle_client_request);
 if(G_PIPELINE)
  pthread_join(data_socket,((void *)0));
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: opt int

variable_types: id int

return_type: int


decompiled_code:
__int64 __fastcall recv_files(__int64 a1)
{
int v1; // er15
const char *v2; // r12
int v3; // er14
char *v4; // rax
char *v5; // rbx
const char *v6; // r14
const char *v8; // r12
char *v9; // rax
char *v10; // rbx
unsigned int v11; // ebx
int v12; // eax
unsigned int v13; // [rsp+Ch] [rbp-101DCh] BYREF
char name[400]; // [rsp+10h] [rbp-101D8h] BYREF
__int64 v15[2]; // [rsp+1A0h] [rbp-10048h] BYREF
char s[65530]; // [rsp+1B0h] [rbp-10038h] BYREF

v15[0] = 0LL;
v15[1] = 0LL;
memset(s, 0, sizeof(s));
v13 = 0;
v1 = strlen((const char *)(a1 + 352));
v2 = (const char *)(a1 + 353);
v3 = strlen((const char *)(a1 + 96));
while ( 1 )
{
v4 = strchr(v2, 47);
v5 = v4;
if ( !v4 )
break;
++v2;
if ( *v4 != *(v4 - 1) )
{
*v4 = 0;
if ( access((const char *)(a1 + 352), 0) )
mkdir((const char *)(a1 + 352), 0x1FFu);
*v5 = 47;
v2 = v5 + 1;
}
}
v6 = (char *)v15 + v3;
while ( (unsigned int)bnet_recv(*(unsigned int *)(a1 + 4), v15, &v13) != -100 )
{
if ( v13 == -6 )
{
printf("%s,%d restore is over\n", "restore.c", 95LL);
return 0LL;
}
++*(_DWORD *)(a1 + 12);
strcpy(name, (const char *)(a1 + 352));
strcat(name, v6);
v8 = &name[v1];
while ( 1 )
{
v9 = strchr(v8, 47);
v10 = v9;
if ( !v9 )
break;
++v8;
if ( *v9 != *(v9 - 1) )
{
*v9 = 0;
if ( access(name, 0) )
mkdir(name, 0x1FFu);
*v10 = 47;
v8 = v10 + 1;
}
}
if ( G_VERBOSE )
printf("restore file %s \n", name);
v11 = open(name, 577, 420LL);
while ( (int)bnet_recv(*(unsigned int *)(a1 + 4), v15, &v13) > 0 )
{
v12 = writen(v11, v15, v13);
if ( v12 != v13 )
printf("%s, %d: failed to write filedata!\n", "restore.c", 128LL);
++*(_DWORD *)(a1 + 32);
*(_QWORD *)(a1 + 16) += (int)v13;
}
close(v11);
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
int v1; // er15
const char *v2; // r12
int v3; // er14
char *v4; // rax
char *v5; // rbx
const char *v6; // r14
const char *v8; // r12
char *v9; // rax
char *v10; // rbx
unsigned int v11; // ebx
int v12; // eax
unsigned int v13; // [rsp+Ch] [rbp-101DCh] BYREF
char name[400]; // [rsp+10h] [rbp-101D8h] BYREF
__int64 v15[2]; // [rsp+1A0h] [rbp-10048h] BYREF
char s[65530]; // [rsp+1B0h] [rbp-10038h] BYREF

v15[0] = 0LL;
v15[1] = 0LL;
memset(s, 0, sizeof(s));
v13 = 0;
v1 = strlen((const char *)(a1 + 352));
v2 = (const char *)(a1 + 353);
v3 = strlen((const char *)(a1 + 96));
while ( 1 )
{
v4 = strchr(v2, 47);
v5 = v4;
if ( !v4 )
break;
++v2;
if ( *v4 != *(v4 - 1) )
{
*v4 = 0;
if ( access((const char *)(a1 + 352), 0) )
mkdir((const char *)(a1 + 352), 0x1FFu);
*v5 = 47;
v2 = v5 + 1;
}
}
v6 = (char *)v15 + v3;
while ( (unsigned int)bnet_recv(*(unsigned int *)(a1 + 4), v15, &v13) != -100 )
{
if ( v13 == -6 )
{
printf("%s,%d restore is over\n", "restore.c", 95LL);
return 0LL;
}
++*(_DWORD *)(a1 + 12);
strcpy(name, (const char *)(a1 + 352));
strcat(name, v6);
v8 = &name[v1];
while ( 1 )
{
v9 = strchr(v8, 47);
v10 = v9;
if ( !v9 )
break;
++v8;
if ( *v9 != *(v9 - 1) )
{
*v9 = 0;
if ( access(name, 0) )
mkdir(name, 0x1FFu);
*v10 = 47;
v8 = v10 + 1;
}
}
if ( G_VERBOSE )
printf("restore file %s \n", name);
v11 = open(name, 577, 420LL);
while ( (int)bnet_recv(*(unsigned int *)(a1 + 4), v15, &v13) > 0 )
{
v12 = writen(v11, v15, v13);
if ( v12 != v13 )
printf("%s, %d: failed to write filedata!\n", "restore.c", 128LL);
++*(_DWORD *)(a1 + 32);
*(_QWORD *)(a1 + 16) += (int)v13;
}
close(v11);
}
return 0LL;
}

original_code:
int recv_files(JCR *psJcr) {
 char buf[64*1024 +10]={0};
 char filename[400];;
 int buflen=0;
 char *p, *q;
 int fd=-1;
 int len = strlen(psJcr->restore_path);
 q = psJcr->restore_path+ 1;
 int blen=strlen(psJcr->backup_path);
 while ((p = strchr(q, '/'))) {
  if (*p == *(p - 1)) {
   q++;
   continue;
  }
  *p = 0;
  if (access(psJcr->restore_path, 0) != 0) {
   mkdir(psJcr->restore_path, (0400|0200|0100) | ((0400|0200|0100) >> 3) | (((0400|0200|0100) >> 3) >> 3));
  }
  *p = '/';
  q = p + 1;
 }
 while (bnet_recv(psJcr->data_socket, buf, &buflen) !=-100) {
  if(buflen==-6){
   printf("%s,%d restore is over\n","restore.c",95);
   break;
  }
  psJcr->file_count++;
  strcpy(filename, psJcr->restore_path);
  strcat(filename, buf+blen);
  q = filename + len;
  while ((p = strchr(q, '/'))) {
   if (*p == *(p - 1)) {
    q++;
    continue;
   }
   *p = 0;
   if (access(filename, 0) != 0) {
    mkdir(filename, (0400|0200|0100) | ((0400|0200|0100) >> 3) | (((0400|0200|0100) >> 3) >> 3));
   }
   *p = '/';
   q = p + 1;
  }
  if(G_VERBOSE)
   printf("restore file %s \n",filename);
  fd = open(filename, 0100 | 01000 | 01,00644);
  while (bnet_recv(psJcr->data_socket, buf, &buflen) >0) {
   if(writen(fd,buf,buflen)!=buflen)
    printf("%s, %d: failed to write filedata!\n", "restore.c", 128);
             psJcr->chunk_count++;
   psJcr->old_size+=buflen;
  }
  close(fd);
 }
 return 0;
}

canonical_original_code:
int recv_files(struct jcr *psJcr) {
 char buf[-1] = {0};
 char filename[400];;
 int buflen = 0;
 char *p; char *q;
 int fd = -1;
 int len = strlen(psJcr->restore_path);
 q = psJcr->restore_path+ 1;
 int blen = strlen(psJcr->backup_path);
 while ((p = strchr(q, '/'))) {
  if (*p == *(p - 1)) {
   q++;
   continue;
  }
  *p = 0;
  if (access(psJcr->restore_path, 0) != 0) {
   mkdir(psJcr->restore_path, (0400|0200|0100) | ((0400|0200|0100) >> 3) | (((0400|0200|0100) >> 3) >> 3));
  }
  *p = '/';
  q = p + 1;
 }
 while (bnet_recv(psJcr->data_socket, buf, &buflen) !=-100) {
  if(buflen==-6){
   printf("%s,%d restore is over\n","restore.c",95);
   break;
  }
  psJcr->file_count++;
  strcpy(filename, psJcr->restore_path);
  strcat(filename, buf+blen);
  q = filename + len;
  while ((p = strchr(q, '/'))) {
   if (*p == *(p - 1)) {
    q++;
    continue;
   }
   *p = 0;
   if (access(filename, 0) != 0) {
    mkdir(filename, (0400|0200|0100) | ((0400|0200|0100) >> 3) | (((0400|0200|0100) >> 3) >> 3));
   }
   *p = '/';
   q = p + 1;
  }
  if(G_VERBOSE)
   printf("restore file %s \n",filename);
  fd = open(filename, 0100 | 01000 | 01,00644);
  while (bnet_recv(psJcr->data_socket, buf, &buflen) >0) {
   if(writen(fd,buf,buflen)!=buflen)
    printf("%s, %d: failed to write filedata!\n", "restore.c", 128);
             psJcr->chunk_count++;
   psJcr->old_size+=buflen;
  }
  close(fd);
 }
 return 0;
}

variable_types: struct jcr { int id; int data_socket; int finger_socket; int file_count; long old_size; long dedup_size; int chunk_count; int dedup_chunk_count; double read_time; double chunk_time; double sha_time; double search_time; double read2_time; double send_time; double total_time; char backup_path[256]; char restore_path[256]; } *

variable_types:
filename
char [400]

variable_types: buflen int

variable_types:
p
char *

variable_types:
q
char *

variable_types: blen int


original_code:
int restore_client(int sock, char* arg) {
 char buf[256]={0};
 int length;
 JCR *jcr = jcr_new();
 jcr->data_socket= sock;
 if (sscanf(arg, "%d,%s", &jcr->id, jcr->restore_path) == 2) {
 } else if (sscanf(arg, "%d", &jcr->id) == 1) {
  jcr->restore_path[0] = 0;
 } else {
  puts("illigal parameters!");
  goto FAIL;
 }
 struct timeval start,end;
 gettimeofday(&start, ((void *)0));
 sprintf(buf, restore_cmd, jcr->id);
 bnet_send(jcr->data_socket, buf, strlen(buf) );
 if (bnet_recv(jcr->data_socket, jcr->backup_path, &length) <= 0) {
  printf("%s, %d: failed to recv backup path!\n", "restore.c", 32);
  goto FAIL;
 }
 if (length < 0 || length>256) {
  printf("no such job!\n");
  goto FAIL;
 }
 length=strlen(jcr->restore_path);
 if (length>0){
  if(jcr->restore_path[length-1]!='/')
   strcat(jcr->restore_path, "/");
 } else
  strcpy(jcr->restore_path, jcr->backup_path);
 printf("restore jobid=%d,path=%s\n",jcr->id,jcr->restore_path);
 if (recv_files(jcr) != 0) {
  goto FAIL;
 }
 gettimeofday(&end, ((void *)0));
 (jcr->total_time)+=(end.tv_usec-start.tv_usec)*1.0/1000000+(end.tv_sec-start.tv_sec);
 printf("number_of_files: %d \n", jcr->file_count);
 printf("chunks_of_files: %d \n", jcr->chunk_count);
 printf("restore time:   %.3fs  %ld %.3f MB/s\n", jcr->total_time,jcr->old_size,jcr->old_size*1.0/jcr->total_time/1000000);
 jcr_free( jcr);
 return 0;
 FAIL:
  jcr_free( jcr);
  return -1;
}

canonical_original_code:
int restore_client(int sock, char *arg) {
 char buf[256] = {0};
 int length;
 struct jcr *jcr = jcr_new();
 jcr->data_socket= sock;
 if (sscanf(arg, "%d,%s", &jcr->id, jcr->restore_path) == 2) {
 } else if (sscanf(arg, "%d", &jcr->id) == 1) {
  jcr->restore_path[0] = 0;
 } else {
  puts("illigal parameters!");
  goto FAIL;
 }
 struct timeval start; struct timeval end;
 gettimeofday(&start, ((void *)0));
 sprintf(buf, restore_cmd, jcr->id);
 bnet_send(jcr->data_socket, buf, strlen(buf) );
 if (bnet_recv(jcr->data_socket, jcr->backup_path, &length) <= 0) {
  printf("%s, %d: failed to recv backup path!\n", "restore.c", 32);
  goto FAIL;
 }
 if (length < 0 || length>256) {
  printf("no such job!\n");
  goto FAIL;
 }
 length=strlen(jcr->restore_path);
 if (length>0){
  if(jcr->restore_path[length-1]!='/')
   strcat(jcr->restore_path, "/");
 } else
  strcpy(jcr->restore_path, jcr->backup_path);
 printf("restore jobid=%d,path=%s\n",jcr->id,jcr->restore_path);
 if (recv_files(jcr) != 0) {
  goto FAIL;
 }
 gettimeofday(&end, ((void *)0));
 (jcr->total_time)+=(end.tv_usec-start.tv_usec)*1.0/1000000+(end.tv_sec-start.tv_sec);
 printf("number_of_files: %d \n", jcr->file_count);
 printf("chunks_of_files: %d \n", jcr->chunk_count);
 printf("restore time:   %.3fs  %ld %.3f MB/s\n", jcr->total_time,jcr->old_size,jcr->old_size*1.0/jcr->total_time/1000000);
 jcr_free( jcr);
 return 0;
 FAIL:
  jcr_free( jcr);
  return -1;
}

variable_types: sock int

variable_types:
arg
char *

variable_types:
buf
char [256]

variable_types: length int

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

user_defined_types: struct jcr { int id; int data_socket; int finger_socket; int file_count; long old_size; long dedup_size; int chunk_count; int dedup_chunk_count; double read_time; double chunk_time; double sha_time; double search_time; double read2_time; double send_time; double total_time; char backup_path[256]; char restore_path[256]; }


mxmlEntityGetName

repo: zhum/simpleweather-screensaver

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

result = "lt";
if ( a1 != 60 )
{
if ( a1 > 60 )
{
result = "gt";
if ( a1 != 62 )
result = 0LL;
}
else
{
result = "quot";
if ( a1 != 34 )
{
result = "amp";
if ( a1 != 38 )
result = 0LL;
}
}
}
return result;
}

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

result = "lt";
if ( a1 != 60 )
{
if ( a1 > 60 )
{
result = "gt";
if ( a1 != 62 )
result = 0LL;
}
else
{
result = "quot";
if ( a1 != 34 )
{
result = "amp";
if ( a1 != 38 )
result = 0LL;
}
}
}
return result;
}

original_code:
const char *				/* O - Entity name or NULL */
mxmlEntityGetName(int val)		/* I - Character value */
{
  switch (val)
  {
    case '&' :
        return ("amp");

    case '<' :
        return ("lt");

    case '>' :
        return ("gt");

    case '\"' :
        return ("quot");

    default :
        return (NULL);
  }
}

canonical_original_code:
char *mxmlEntityGetName(int val)		/* I - Character value */
{
  switch (val)
  {
    case '&' :
        return ("amp");

    case '<' :
        return ("lt");

    case '>' :
        return ("gt");

    case '\"' :
        return ("quot");

    default :
        return (NULL);
  }
}

variable_types: val int

return_type: char *


original_code:
int nl_if_close (struct nl_interface *nl_if)
{
 sem_wait(&nl_if->access_in);
 sem_wait(&nl_if->access_out);
 free(nl_if->iov_in.iov_base);
 free(nl_if->iov_out.iov_base);
 close(nl_if->sock_fd);
 sem_post(&nl_if->access_in);
 sem_post(&nl_if->access_out);
 return 0;
}

canonical_original_code:
int nl_if_close(struct nl_interface *nl_if)
{
 sem_wait(&nl_if->access_in);
 sem_wait(&nl_if->access_out);
 free(nl_if->iov_in.iov_base);
 free(nl_if->iov_out.iov_base);
 close(nl_if->sock_fd);
 sem_post(&nl_if->access_in);
 sem_post(&nl_if->access_out);
 return 0;
}

variable_types: struct nl_interface { struct iovec { void *iov_base; unsigned long iov_len; } iov_in; struct sockaddr_nl { unsigned short nl_family; unsigned short nl_pad; unsigned int nl_pid; unsigned int nl_groups; } src_addr; struct msghdr { void *msg_name; unsigned int msg_namelen; struct iovec *msg_iov; unsigned long msg_iovlen; void *msg_control; unsigned long msg_controllen; int msg_flags; } msg_in; int sock_fd; union sem_t { char __size[32]; long __align; } access_in; union sem_t access_out; } *

user_defined_types: struct nl_interface { struct iovec iov_in; struct sockaddr_nl src_addr; struct msghdr msg_in; int sock_fd; union sem_t access_in; union sem_t access_out; }


original_code:
int send_raw(struct nl_interface *nl_if, unsigned char *data,
    unsigned int data_len, unsigned short msg_type)
{
 int ret;
 struct nlmsghdr *nlh = (struct nlmsghdr *) nl_if->iov_out.iov_base;
 sem_wait(&nl_if->access_out);
 nlh->nlmsg_type = msg_type;
 nlh->nlmsg_len = data_len;
 nlh->nlmsg_pid = getpid();
 nlh->nlmsg_flags = 0;
 nl_if->iov_out.iov_len = ( ((((data_len) + ((int) ( ((sizeof(struct nlmsghdr))+4U -1) & ~(4U -1) ))))+4U -1) & ~(4U -1) );
 memcpy(((void*)(((char*)nlh) + ((0) + ((int) ( ((sizeof(struct nlmsghdr))+4U -1) & ~(4U -1) ))))), data, data_len);
 ret = sendmsg(nl_if->sock_fd, &nl_if->msg_out, 0);
 sem_post(&nl_if->access_out);
 return ret;
}

canonical_original_code:
int send_raw(struct nl_interface *nl_if, unsigned char *data, unsigned int data_len, unsigned short msg_type)
{
 int ret;
 struct nlmsghdr *nlh = (struct nlmsghdr *) nl_if->iov_out.iov_base;
 sem_wait(&nl_if->access_out);
 nlh->nlmsg_type = msg_type;
 nlh->nlmsg_len = data_len;
 nlh->nlmsg_pid = getpid();
 nlh->nlmsg_flags = 0;
 nl_if->iov_out.iov_len = ( ((((data_len) + ((int) ( ((sizeof(struct nlmsghdr))+4U -1) & ~(4U -1) ))))+4U -1) & ~(4U -1) );
 memcpy(((void *)(((char *)nlh) + ((0) + ((int) ( ((sizeof(struct nlmsghdr))+4U -1) & ~(4U -1) ))))), data, data_len);
 ret = sendmsg(nl_if->sock_fd, &nl_if->msg_out, 0);
 sem_post(&nl_if->access_out);
 return ret;
}

variable_types:
data
unsigned char *

variable_types: ret int

variable_types: struct nlmsghdr { unsigned int nlmsg_len; unsigned short nlmsg_type; unsigned short nlmsg_flags; unsigned int nlmsg_seq; unsigned int nlmsg_pid; } *

user_defined_types: struct iovec { void *iov_base; unsigned long iov_len; }

user_defined_types: struct sockaddr_nl { unsigned short nl_family; unsigned short nl_pad; unsigned int nl_pid; unsigned int nl_groups; }

user_defined_types: struct msghdr { void *msg_name; unsigned int msg_namelen; struct iovec *msg_iov; unsigned long msg_iovlen; void *msg_control; unsigned long msg_controllen; int msg_flags; }

user_defined_types: union sem_t { char __size[32]; long __align; }


original_code:
int nl_if_init (struct nl_interface *nl_if)
{
 struct nlmsghdr *nlh_in, *nlh_out;
 int ret;
 nl_if->sock_fd = socket(16, SOCK_RAW, 20);
 memset(&nl_if->msg_in, 0, sizeof(struct msghdr));
 memset(&nl_if->msg_out, 0, sizeof(struct msghdr));
 memset(&nl_if->src_addr, 0, sizeof(struct sockaddr_nl));
 memset(&nl_if->dest_addr, 0, sizeof(struct sockaddr_nl));
 nl_if->src_addr.nl_family = 16;
 nl_if->src_addr.nl_pid = getpid();
 nl_if->src_addr.nl_groups = 0;
 nl_if->dest_addr.nl_family = 16;
 nl_if->dest_addr.nl_pid = 0;
 nl_if->dest_addr.nl_groups = 0;
 ret = bind(nl_if->sock_fd, (struct sockaddr*)&nl_if->src_addr, sizeof(struct sockaddr_nl));
 nlh_in = (struct nlmsghdr *) malloc(( ((((1000) + ((int) ( ((sizeof(struct nlmsghdr))+4U -1) & ~(4U -1) ))))+4U -1) & ~(4U -1) ));
 nlh_out = (struct nlmsghdr *) malloc(( ((((1000) + ((int) ( ((sizeof(struct nlmsghdr))+4U -1) & ~(4U -1) ))))+4U -1) & ~(4U -1) ));
 nl_if->iov_in.iov_base = (void *)nlh_in;
 nl_if->iov_in.iov_len = ( ((((1000) + ((int) ( ((sizeof(struct nlmsghdr))+4U -1) & ~(4U -1) ))))+4U -1) & ~(4U -1) );
 nl_if->msg_in.msg_name = (void *)&nl_if->dest_addr;
 nl_if->msg_in.msg_namelen = sizeof(nl_if->dest_addr);
 nl_if->msg_in.msg_iov = &nl_if->iov_in;
 nl_if->msg_in.msg_iovlen = 1;
 nl_if->iov_out.iov_base = (void *)nlh_out;
 nl_if->iov_out.iov_len = ( ((((1000) + ((int) ( ((sizeof(struct nlmsghdr))+4U -1) & ~(4U -1) ))))+4U -1) & ~(4U -1) );
 nl_if->msg_out.msg_name = (void *)&nl_if->dest_addr;
 nl_if->msg_out.msg_namelen = sizeof(nl_if->dest_addr);
 nl_if->msg_out.msg_iov = &nl_if->iov_out;
 nl_if->msg_out.msg_iovlen = 1;
 sem_init(&nl_if->access_in, 0, 1);
 sem_init(&nl_if->access_out, 0, 1);
 send_raw(nl_if, 0, 0, 0xffff);
 return ret;
}

canonical_original_code:
int nl_if_init(struct nl_interface *nl_if)
{
 struct nlmsghdr *nlh_in; struct nlmsghdr *nlh_out;
 int ret;
 nl_if->sock_fd = socket(16, SOCK_RAW, 20);
 memset(&nl_if->msg_in, 0, sizeof(struct msghdr));
 memset(&nl_if->msg_out, 0, sizeof(struct msghdr));
 memset(&nl_if->src_addr, 0, sizeof(struct sockaddr_nl));
 memset(&nl_if->dest_addr, 0, sizeof(struct sockaddr_nl));
 nl_if->src_addr.nl_family = 16;
 nl_if->src_addr.nl_pid = getpid();
 nl_if->src_addr.nl_groups = 0;
 nl_if->dest_addr.nl_family = 16;
 nl_if->dest_addr.nl_pid = 0;
 nl_if->dest_addr.nl_groups = 0;
 ret = bind(nl_if->sock_fd, (struct sockaddr *)&nl_if->src_addr, sizeof(struct sockaddr_nl));
 nlh_in = (struct nlmsghdr *) malloc(( ((((1000) + ((int) ( ((sizeof(struct nlmsghdr))+4U -1) & ~(4U -1) ))))+4U -1) & ~(4U -1) ));
 nlh_out = (struct nlmsghdr *) malloc(( ((((1000) + ((int) ( ((sizeof(struct nlmsghdr))+4U -1) & ~(4U -1) ))))+4U -1) & ~(4U -1) ));
 nl_if->iov_in.iov_base = (void *)nlh_in;
 nl_if->iov_in.iov_len = ( ((((1000) + ((int) ( ((sizeof(struct nlmsghdr))+4U -1) & ~(4U -1) ))))+4U -1) & ~(4U -1) );
 nl_if->msg_in.msg_name = (void *)&nl_if->dest_addr;
 nl_if->msg_in.msg_namelen = sizeof(nl_if->dest_addr);
 nl_if->msg_in.msg_iov = &nl_if->iov_in;
 nl_if->msg_in.msg_iovlen = 1;
 nl_if->iov_out.iov_base = (void *)nlh_out;
 nl_if->iov_out.iov_len = ( ((((1000) + ((int) ( ((sizeof(struct nlmsghdr))+4U -1) & ~(4U -1) ))))+4U -1) & ~(4U -1) );
 nl_if->msg_out.msg_name = (void *)&nl_if->dest_addr;
 nl_if->msg_out.msg_namelen = sizeof(nl_if->dest_addr);
 nl_if->msg_out.msg_iov = &nl_if->iov_out;
 nl_if->msg_out.msg_iovlen = 1;
 sem_init(&nl_if->access_in, 0, 1);
 sem_init(&nl_if->access_out, 0, 1);
 send_raw(nl_if, 0, 0, 0xffff);
 return ret;
}

user_defined_types: struct nlmsghdr { unsigned int nlmsg_len; unsigned short nlmsg_type; unsigned short nlmsg_flags; unsigned int nlmsg_seq; unsigned int nlmsg_pid; }


original_code:
int recv_raw(struct nl_interface *nl_if, struct nl_data_header *nl_data_h,
    struct goosehdr *goose_h, unsigned char *apdu)
{
 unsigned char *nlh = (unsigned char *) nl_if->iov_in.iov_base;
 unsigned short apdu_len;
 sem_wait(&nl_if->access_in);
 recvmsg(nl_if->sock_fd, &nl_if->msg_in, 0);
 memcpy(nl_data_h, nlh, sizeof(struct nl_data_header));
 nlh += sizeof(struct nl_data_header) + 2;
 memcpy(goose_h, nlh, sizeof(struct goosehdr));
 goose_h->len = ntohs(goose_h->len);
 goose_h->appid = ntohs(goose_h->appid);
 apdu_len = goose_h->len - sizeof(struct goosehdr);
 nlh += sizeof(struct goosehdr);
 memcpy(apdu, nlh, apdu_len);
 sem_post(&nl_if->access_in);
 return apdu_len;
}

variable_types: struct nl_data_header { char dev_name[16]; unsigned char daddr[6]; unsigned char saddr[6]; } *

variable_types: struct goosehdr { unsigned short appid; unsigned short len; unsigned char reserv1; unsigned char reserv2; unsigned char reserv3; unsigned char reserv4; } *

variable_types:
apdu
unsigned char *

variable_types:
nlh
unsigned char *


original_code:
int send_goose_data(struct nl_interface *nl_if, struct nl_data_header *nl_data_h,
     struct goosehdr *goose_h, unsigned char *apdu,
     unsigned int apdu_len, unsigned short msg_type)
{
 int ret;
 struct nlmsghdr *nlh = (struct nlmsghdr *) nl_if->iov_out.iov_base;
 unsigned int nl_data_h_len = sizeof(struct nl_data_header);
 unsigned int goose_h_len = sizeof(struct goosehdr);
 unsigned int data_len = apdu_len + goose_h_len + nl_data_h_len;
 goose_h->len = htons(apdu_len + goose_h_len);
 sem_wait(&nl_if->access_out);
 nlh->nlmsg_type = msg_type;
 nlh->nlmsg_len = data_len;
 nlh->nlmsg_pid = getpid();
 nlh->nlmsg_flags = 0;
 nl_if->iov_out.iov_len = ( ((((data_len) + ((int) ( ((sizeof(struct nlmsghdr))+4U -1) & ~(4U -1) ))))+4U -1) & ~(4U -1) );
 memcpy(((void*)(((char*)nlh) + ((0) + ((int) ( ((sizeof(struct nlmsghdr))+4U -1) & ~(4U -1) ))))), nl_data_h, nl_data_h_len);
 memcpy(((void*)(((char*)nlh) + ((0) + ((int) ( ((sizeof(struct nlmsghdr))+4U -1) & ~(4U -1) ))))) + nl_data_h_len, goose_h, goose_h_len);
 memcpy(((void*)(((char*)nlh) + ((0) + ((int) ( ((sizeof(struct nlmsghdr))+4U -1) & ~(4U -1) ))))) + nl_data_h_len + goose_h_len,
     apdu, apdu_len);
 ret = sendmsg(nl_if->sock_fd, &nl_if->msg_out, 0);
 sem_post(&nl_if->access_out);
 return ret;
}

canonical_original_code:
int send_goose_data(struct nl_interface *nl_if, struct nl_data_header *nl_data_h, struct goosehdr *goose_h, unsigned char *apdu, unsigned int apdu_len, unsigned short msg_type)
{
 int ret;
 struct nlmsghdr *nlh = (struct nlmsghdr *) nl_if->iov_out.iov_base;
 unsigned int nl_data_h_len = sizeof(struct nl_data_header);
 unsigned int goose_h_len = sizeof(struct goosehdr);
 unsigned int data_len = apdu_len + goose_h_len + nl_data_h_len;
 goose_h->len = htons(apdu_len + goose_h_len);
 sem_wait(&nl_if->access_out);
 nlh->nlmsg_type = msg_type;
 nlh->nlmsg_len = data_len;
 nlh->nlmsg_pid = getpid();
 nlh->nlmsg_flags = 0;
 nl_if->iov_out.iov_len = ( ((((data_len) + ((int) ( ((sizeof(struct nlmsghdr))+4U -1) & ~(4U -1) ))))+4U -1) & ~(4U -1) );
 memcpy(((void *)(((char *)nlh) + ((0) + ((int) ( ((sizeof(struct nlmsghdr))+4U -1) & ~(4U -1) ))))), nl_data_h, nl_data_h_len);
 memcpy(((void *)(((char *)nlh) + ((0) + ((int) ( ((sizeof(struct nlmsghdr))+4U -1) & ~(4U -1) ))))) + nl_data_h_len, goose_h, goose_h_len);
 memcpy(((void *)(((char *)nlh) + ((0) + ((int) ( ((sizeof(struct nlmsghdr))+4U -1) & ~(4U -1) ))))) + nl_data_h_len + goose_h_len,
     apdu, apdu_len);
 ret = sendmsg(nl_if->sock_fd, &nl_if->msg_out, 0);
 sem_post(&nl_if->access_out);
 return ret;
}

user_defined_types: struct nl_data_header { char dev_name[16]; unsigned char daddr[6]; unsigned char saddr[6]; }

user_defined_types: struct goosehdr { unsigned short appid; unsigned short len; unsigned char reserv1; unsigned char reserv2; unsigned char reserv3; unsigned char reserv4; }


original_code:
int send_goose_data_old(struct nl_interface *nl_if, struct nl_data_header *nl_data_h,
      struct goosehdr *goose_h, unsigned char *apdu,
      unsigned int apdu_len, unsigned short msg_type)
{
 int ret;
 unsigned int goose_len = apdu_len + sizeof(struct goosehdr);
 unsigned int raw_data_len = goose_len + sizeof(struct nl_data_header);
 unsigned char *raw_data = malloc(raw_data_len);
 goose_h->len = htons(goose_len);
 memcpy(raw_data,
     nl_data_h, sizeof(struct nl_data_header));
 memcpy(raw_data + sizeof(struct nl_data_header),
     goose_h, sizeof(struct goosehdr));
 memcpy(raw_data + sizeof(struct nl_data_header) + sizeof (struct goosehdr),
     apdu, apdu_len);
 ret = send_raw(nl_if, raw_data, raw_data_len, msg_type);
 free(raw_data);
 return ret;
}


decompiled_code:
__int64 __fastcall send_goose_ctrl(__int64 a1, const void *a2)
{
return send_raw(a1, a2, 0x20u, 1);
}

canonical_decompiled_code:
__int64 __fastcall func14(__int64 a1, const void *a2)
{
return func9(a1, a2, 0x20u, 1);
}

original_code:
inline int send_goose_ctrl(struct nl_interface *nl_if, struct nl_ctrl_header *ctrl_info)
{
 return send_raw(nl_if, (unsigned char*) ctrl_info,
     sizeof(struct nl_ctrl_header), 0x0001);
}

canonical_original_code:
inline int send_goose_ctrl(struct nl_interface *nl_if, struct nl_ctrl_header *ctrl_info)
{
 return send_raw(nl_if, (unsigned char *) ctrl_info,
     sizeof(struct nl_ctrl_header), 0x0001);
}

variable_types: struct nl_ctrl_header { struct goose_param_t { unsigned int thresh; unsigned int intvl_init; unsigned int intvl_max; unsigned int intvl_incre; } goose_param; char def_dev[16]; } *

user_defined_types: struct nl_ctrl_header { struct goose_param_t goose_param; char def_dev[16]; }


newTcpSocket

repo: zinkem/zhttpd

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

result = socket(2, 1, 6);
if ( result == -1 )
{
perror("cannot create socket");
exit(1);
}
return result;
}

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

result = socket(2, 1, 6);
if ( result == -1 )
{
perror("cannot create socket");
exit(1);
}
return result;
}

original_code:
int newTcpSocket(){
 int domain = 2;
 int type = SOCK_STREAM;
 int protocol = IPPROTO_TCP;
 int socketfd = socket(domain, type, protocol);
 if(socketfd == -1){
  perror("cannot create socket");
  exit(1);
 }
 return socketfd;
}

variable_types: domain int

variable_types: type int

variable_types: protocol int

variable_types: socketfd int


bindAndListen

decompiled_code:
int __fastcall bindAndListen(int fd, unsigned int a2, unsigned int a3)
{
struct sockaddr *v4; // rbx

v4 = (struct sockaddr *)malloc(0x10uLL);
*(_QWORD *)&v4->sa_family = 0LL;
*(_QWORD *)&v4->sa_data[6] = 0LL;
v4->sa_family = 2;
*(_WORD *)v4->sa_data = _bswap_16(a2);
if ( bind(fd, v4, 0x10u) == -1 )
{
perror("Error bind failed");
close(fd);
exit(1);
}
if ( listen(fd, a3) == -1 )
{
perror("error listen failed");
close(fd);
exit(1);
}
return printf("Listening on port %d with a queue size of %d\n", a2, a3);
}

canonical_decompiled_code:
int __fastcall func2(int fd, unsigned int a2, unsigned int a3)
{
struct sockaddr *v4; // rbx

v4 = (struct sockaddr *)malloc(0x10uLL);
*(_QWORD *)&v4->sa_family = 0LL;
*(_QWORD *)&v4->sa_data[6] = 0LL;
v4->sa_family = 2;
*(_WORD *)v4->sa_data = func0(a2);
if ( bind(fd, v4, 0x10u) == -1 )
{
perror("Error bind failed");
close(fd);
exit(1);
}
if ( listen(fd, a3) == -1 )
{
perror("error listen failed");
close(fd);
exit(1);
}
return printf("Listening on port %d with a queue size of %d\n", a2, a3);
}

original_code:
struct sockaddr_in* bindAndListen(int sfd, int port, int qsize){
 struct sockaddr_in* addr;
 addr = (struct sockaddr_in *)malloc(sizeof(struct sockaddr));
 memset(addr, 0, sizeof(*addr));
 addr->sin_family = 2;
 addr->sin_port = htons(port);
 addr->sin_addr.s_addr = ((in_addr_t) 0x00000000);
 int b = bind(sfd, (struct sockaddr *)addr, sizeof(*addr));
 if(b == -1){
  perror("Error bind failed");
  close(sfd);
  exit(1);
 }
 int l = listen(sfd, qsize);
 if(l == -1){
  perror("error listen failed");
  close(sfd);
  exit(1);
 }
 printf("Listening on port %d with a queue size of %d\n", port, qsize);
}

canonical_original_code:
struct sockaddr_in *bindAndListen(int sfd, int port, int qsize){
 struct sockaddr_in *addr;
 addr = (struct sockaddr_in *)malloc(sizeof(struct sockaddr));
 memset(addr, 0, sizeof(*addr));
 addr->sin_family = 2;
 addr->sin_port = htons(port);
 addr->sin_addr.s_addr = ((unsigned int) 0x00000000);
 int b = bind(sfd, (struct sockaddr *)addr, sizeof(*addr));
 if(b == -1){
  perror("Error bind failed");
  close(sfd);
  exit(1);
 }
 int l = listen(sfd, qsize);
 if(l == -1){
  perror("error listen failed");
  close(sfd);
  exit(1);
 }
 printf("Listening on port %d with a queue size of %d\n", port, qsize);
}

variable_types: sfd int

variable_types: port int

variable_types: qsize int

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

variable_types: b int

variable_types: l 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]; }


printDict

decompiled_code:
__int64 __fastcall printDict(__int64 a1)
{
__int64 v1; // rsi
__int64 result; // rax
const char **v3; // rdx

i = 0;
v1 = 0LL;
do
{
v3 = *(const char ***)(*(_QWORD *)(a1 + 8) + 8LL * (int)v1);
if ( v3 )
printf("%d: %p: k='%s' v='%s'\n", v1, v3, v3[1], *v3);
result = (unsigned int)i;
v1 = (unsigned int)(i + 1);
i = v1;
}
while ( (int)v1 <= 40 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
__int64 v1; // rsi
__int64 result; // rax
const char **v3; // rdx

i = 0;
v1 = 0LL;
do
{
v3 = *(const char ***)(*(_QWORD *)(a1 + 8) + 8LL * (int)v1);
if ( v3 )
printf("%d: %p: k='%s' v='%s'\n", v1, v3, v3[1], *v3);
result = (unsigned int)i;
v1 = (unsigned int)(i + 1);
i = v1;
}
while ( (int)v1 <= 40 );
return result;
}

original_code:
void printDict(Dictionary *dict){
 for(i = 0; i < 41; i++){
  if(dict->table[i] != ((void *)0)){
   printf("%d: %p: k='%s' v='%s'\n", i,
     dict->table[i],
     dict->table[i]->key,
     dict->table[i]->val);
  }
 }
}

canonical_original_code:
void printDict(struct Dictionary *dict){
 for(i = 0; i < 41; i++){
  if(dict->table[i] != ((void *)0)){
   printf("%d: %p: k='%s' v='%s'\n", i,
     dict->table[i],
     dict->table[i]->key,
     dict->table[i]->val);
  }
 }
}

variable_types: struct Dictionary { int size; int capacity; struct DictionaryEntry { char *val; char *key; struct DictionaryEntry *next; } **table; } *

user_defined_types: struct Dictionary { int size; int capacity; struct DictionaryEntry **table; }


newDictionary

decompiled_code:
_QWORD *newDictionary()
{
_QWORD *v0; // rbx
void *v1; // rdx

v0 = malloc(0x10uLL);
v1 = malloc(0x148uLL);
memset(v1, 0, 0x148uLL);
v0[1] = v1;
*(_DWORD *)v0 = 0;
*((_DWORD *)v0 + 1) = 41;
return v0;
}

canonical_decompiled_code:
_QWORD *func1()
{
_QWORD *v0; // rbx
void *v1; // rdx

v0 = malloc(0x10uLL);
v1 = malloc(0x148uLL);
memset(v1, 0, 0x148uLL);
v0[1] = v1;
*(_DWORD *)v0 = 0;
*((_DWORD *)v0 + 1) = 41;
return v0;
}

original_code:
Dictionary* newDictionary(){
 Dictionary *newObject = (Dictionary *)malloc(sizeof(struct Dictionary));
 DictionaryEntry** newTable = (DictionaryEntry **)malloc(sizeof(struct DictionaryEntry*)* 41);
 memset(newTable, 0, sizeof(struct DictionaryEntry*)*41);
 newObject->table = newTable;
 newObject->size = 0;
 newObject->capacity = 41;
 return newObject;
}

canonical_original_code:
struct Dictionary *newDictionary(){
 struct Dictionary *newObject = (Dictionary *)malloc(sizeof(struct Dictionary));
 struct DictionaryEntry **newTable = (DictionaryEntry **)malloc(sizeof(struct DictionaryEntry*)* 41);
 memset(newTable, 0, sizeof(struct DictionaryEntry *)*41);
 newObject->table = newTable;
 newObject->size = 0;
 newObject->capacity = 41;
 return newObject;
}

return_type: struct Dictionary { int size; int capacity; struct DictionaryEntry { char *val; char *key; struct DictionaryEntry *next; } **table; } *


hashForKey

decompiled_code:
__int64 __fastcall hashForKey(char *a1)
{
char v1; // al
unsigned int v2; // ecx

v1 = *a1;
if ( *a1 )
{
v2 = 0;
do
{
v2 = v1 + 31 * v2;
v1 = *++a1;
}
while ( *a1 );
}
else
{
v2 = 0;
}
return v2 % 0x29;
}

canonical_decompiled_code:
__int64 __fastcall func2(char *a1)
{
char v1; // al
unsigned int v2; // ecx

v1 = *a1;
if ( *a1 )
{
v2 = 0;
do
{
v2 = v1 + 31 * v2;
v1 = *++a1;
}
while ( *a1 );
}
else
{
v2 = 0;
}
return v2 % 0x29;
}

original_code:
unsigned int hashForKey(char *key){
    unsigned int hashval;
    char *k = key;
    for(hashval = 0; *k != '\0'; k++)
     hashval = *k + 31 * hashval;
    return hashval % 41;
}

variable_types:
key
char *

variable_types:
hashval
unsigned int

variable_types:
k
char *

return_type: unsigned int


freeDictEntry

decompiled_code:
void __fastcall freeDictEntry(__int64 a1)
{
free(*(void **)a1);
free(*(void **)(a1 + 8));
if ( *(_QWORD *)(a1 + 16) )
freeDictEntry();
free((void *)a1);
}

canonical_decompiled_code:
void __fastcall func3(__int64 a1)
{
free(*(void **)a1);
free(*(void **)(a1 + 8));
if ( *(_QWORD *)(a1 + 16) )
func3();
free((void *)a1);
}

original_code:
void freeDictEntry(DictionaryEntry * de){
 free(de->val);
 free(de->key);
 if(de->next != ((void *)0))
  freeDictEntry(de->next);
 free(de);
}

canonical_original_code:
void freeDictEntry(struct DictionaryEntry *de){
 free(de->val);
 free(de->key);
 if(de->next != ((void *)0))
  freeDictEntry(de->next);
 free(de);
}

variable_types: struct DictionaryEntry { char *val; char *key; struct DictionaryEntry *next; } *


freeDictionary

decompiled_code:
void __fastcall freeDictionary(void *ptr)
{
__int64 i; // rbx
__int64 v3; // rdi

for ( i = 0LL; i != 328; i += 8LL )
{
v3 = *(_QWORD *)(*((_QWORD *)ptr + 1) + i);
if ( v3 )
freeDictEntry(v3);
}
free(ptr);
}

canonical_decompiled_code:
void __fastcall func4(void *ptr)
{
__int64 i; // rbx
__int64 v3; // rdi

for ( i = 0LL; i != 328; i += 8LL )
{
v3 = *(_QWORD *)(*((_QWORD *)ptr + 1) + i);
if ( v3 )
func3(v3);
}
free(ptr);
}

original_code:
void freeDictionary(Dictionary * dict){
 int i;
 for(i = 0; i < 41; i++){
  if(dict->table[i] != ((void *)0)){
   freeDictEntry(dict->table[i]);
  }
 }
 free(dict);
}

canonical_original_code:
void freeDictionary(struct Dictionary *dict){
 int i;
 for(i = 0; i < 41; i++){
  if(dict->table[i] != ((void *)0)){
   freeDictEntry(dict->table[i]);
  }
 }
 free(dict);
}

variable_types: i int

user_defined_types: struct DictionaryEntry { char *val; char *key; struct DictionaryEntry *next; }


newString

decompiled_code:
char *__fastcall newString(const char *a1)
{
char *ptr; // [rsp+8h] [rbp-10h] BYREF

asprintf(&ptr, "%s", a1);
return ptr;
}

canonical_decompiled_code:
char *__fastcall func0(const char *a1)
{
char *ptr; // [rsp+8h] [rbp-10h] BYREF

asprintf(&ptr, "%s", a1);
return ptr;
}

original_code:
char * newString(char * s){
 char * new_string;
 asprintf(&new_string, "%s", s);
 return new_string;
}

canonical_original_code:
char *newString(char *s){
 char *new_string;
 asprintf(&new_string, "%s", s);
 return new_string;
}

variable_types:
s
char *


newNextLine

original_code:
char * newNextLine(char * s, int *index){
 char buffer[1024];
 int start = *index;
 char c = s[start];
 int skipped = 0;
 while(c != '\n' && c != '\0'){
  if(c != '\r'){
   buffer[(*index)-start-skipped] = c;
  } else {
   skipped++;
  }
  (*index)++;
  c = s[(*index)];
 }
 buffer[(*index)-start-skipped] = '\0';
 (*index)++;
 return newString(buffer);
}

canonical_original_code:
char *newNextLine(char *s, int *index){
 char buffer[1024];
 int start = *index;
 char c = s[start];
 int skipped = 0;
 while(c != '\n' && c != '\0'){
  if(c != '\r'){
   buffer[(*index)-start-skipped] = c;
  } else {
   skipped++;
  }
  (*index)++;
  c = s[(*index)];
 }
 buffer[(*index)-start-skipped] = '\0';
 (*index)++;
 return newString(buffer);
}

variable_types:
index
int *

variable_types:
buffer
char [1024]

variable_types: start int

variable_types: c char

variable_types: skipped int


newIndexToChar

original_code:
char * newIndexToChar(char *s, int index, char end){
 char buffer[1024];
 int i = 0;
 int start = index;
 while( s[index+i] != end && s[index+i] != '\0'){
  buffer[i] = s[index+i];
  i++;
 }
 buffer[i] = '\0';
 return newString(buffer);
}

canonical_original_code:
char *newIndexToChar(char *s, int index, char end){
 char buffer[1024];
 int i = 0;
 int start = index;
 while( s[index+i] != end && s[index+i] != '\0'){
  buffer[i] = s[index+i];
  i++;
 }
 buffer[i] = '\0';
 return newString(buffer);
}

variable_types: index int

variable_types: end char


mutateToLower

decompiled_code:
char *__fastcall mutateToLower(char *s)
{
size_t i; // rbx

for ( i = 0LL; strlen(s) > i; ++i )
s[i] = (*_ctype_tolower_loc())[s[i]];
return s;
}

canonical_decompiled_code:
char *__fastcall func3(char *s)
{
size_t i; // rbx

for ( i = 0LL; strlen(s) > i; ++i )
s[i] = (*_ctype_tolower_loc())[s[i]];
return s;
}

original_code:
char * mutateToLower(char * s){
 int i;
 for(i = 0; i < strlen(s); i++){
  s[i] = tolower(s[i]);
 }
 return s;
}

canonical_original_code:
char *mutateToLower(char *s){
 int i;
 for(i = 0; i < strlen(s); i++){
  s[i] = tolower(s[i]);
 }
 return s;
}


repo: ziocroc/snbtlib

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

if ( a2 )
result = gzwrite(a1, a2, 1LL);
else
result = 4294967292LL;
return result;
}

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

if ( a2 )
result = gzwrite(a1, a2, 1LL);
else
result = 4294967292LL;
return result;
}

original_code:
static int nbt_write_byte(gzFile file, nbt_byte *payload)
{
 return payload ? gzwrite(file, payload, 1) : -4;
}

canonical_original_code:
static int nbt_write_byte(struct gzFile_s *file, char *payload)
{
 return payload ? gzwrite(file, payload, 1) : -4;
}

variable_types: struct gzFile_s { unsigned int have; unsigned char *next; long pos; } *

variable_types:
payload
char *


decompiled_code:
__int64 __fastcall nbt_write_short(__int64 a1, __int64 a2)
{
int v2; // ebp

if ( !a2 )
return 4294967292LL;
v2 = gzwrite(a1, a2 + 1, 1LL);
return v2 + (unsigned int)gzwrite(a1, a2, 1LL);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2)
{
int v2; // ebp

if ( !a2 )
return 4294967292LL;
v2 = gzwrite(a1, a2 + 1, 1LL);
return v2 + (unsigned int)gzwrite(a1, a2, 1LL);
}

original_code:
static int nbt_write_short(gzFile file, nbt_short *payload)
{
 int written = 0;
 if (!payload) {
  return -4;
 }
 written += gzwrite(file, ((nbt_byte *) payload) + 1, 1);
 written += gzwrite(file, ((nbt_byte *) payload), 1);
 return written;
}

canonical_original_code:
static int nbt_write_short(struct gzFile_s *file, short *payload)
{
 int written = 0;
 if (!payload) {
  return -4;
 }
 written += gzwrite(file, ((char *) payload) + 1, 1);
 written += gzwrite(file, ((char *) payload), 1);
 return written;
}

variable_types:
payload
short *

variable_types: written int

user_defined_types: struct gzFile_s { unsigned int have; unsigned char *next; long pos; }


decompiled_code:
__int64 __fastcall nbt_write_string(__int64 a1, __int16 *a2)
{
unsigned int v2; // er12
__int64 v3; // rbx

if ( !a2 )
return (unsigned int)-4;
v2 = nbt_write_short(a1, (__int64)a2);
if ( v2 && *a2 > 0 )
{
v3 = 0LL;
do
v2 += gzwrite(a1, *((_QWORD *)a2 + 1) + v3++, 1LL);
while ( *a2 > (int)v3 );
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int16 *a2)
{
unsigned int v2; // er12
__int64 v3; // rbx

if ( !a2 )
return (unsigned int)-4;
v2 = func1(a1, (__int64)a2);
if ( v2 && *a2 > 0 )
{
v3 = 0LL;
do
v2 += gzwrite(a1, *((_QWORD *)a2 + 1) + v3++, 1LL);
while ( *a2 > (int)v3 );
}
return v2;
}

original_code:
static int nbt_write_string(gzFile file, nbt_string *payload)
{
 int i, res, written = 0;
 if (!payload) {
  return -4;
 }
 res = nbt_write_short(file, &payload->length);
 if (!res) {
  return written;
 }
 written += res;
 for (i = 0; i < payload->length; i++) {
  res = gzwrite(file, payload->chars + i, 1);
  if (res) {
   written += res;
  }
 }
 return written;
}

canonical_original_code:
static int nbt_write_string(struct gzFile_s *file, struct nbt_string *payload)
{
 int i; int res; int written = 0;
 if (!payload) {
  return -4;
 }
 res = nbt_write_short(file, &payload->length);
 if (!res) {
  return written;
 }
 written += res;
 for (i = 0; i < payload->length; i++) {
  res = gzwrite(file, payload->chars + i, 1);
  if (res) {
   written += res;
  }
 }
 return written;
}

variable_types: struct nbt_string { short length; char *chars; } *

variable_types: res int


decompiled_code:
__int64 __fastcall nbt_write_int(__int64 a1, __int64 a2)
{
int v2; // ebx
int v3; // ebx
int v4; // ebx

if ( !a2 )
return 4294967292LL;
v2 = gzwrite(a1, a2 + 3, 1LL);
v3 = gzwrite(a1, a2 + 2, 1LL) + v2;
v4 = gzwrite(a1, a2 + 1, 1LL) + v3;
return v4 + (unsigned int)gzwrite(a1, a2, 1LL);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2)
{
int v2; // ebx
int v3; // ebx
int v4; // ebx

if ( !a2 )
return 4294967292LL;
v2 = gzwrite(a1, a2 + 3, 1LL);
v3 = gzwrite(a1, a2 + 2, 1LL) + v2;
v4 = gzwrite(a1, a2 + 1, 1LL) + v3;
return v4 + (unsigned int)gzwrite(a1, a2, 1LL);
}

original_code:
static int nbt_write_int(gzFile file, nbt_int *payload)
{
 int written = 0;
 if (!payload) {
  return -4;
 }
 written += gzwrite(file, ((nbt_byte *) payload) + 3, 1);
 written += gzwrite(file, ((nbt_byte *) payload) + 2, 1);
 written += gzwrite(file, ((nbt_byte *) payload) + 1, 1);
 written += gzwrite(file, ((nbt_byte *) payload), 1);
 return written;
}

canonical_original_code:
static int nbt_write_int(struct gzFile_s *file, int *payload)
{
 int written = 0;
 if (!payload) {
  return -4;
 }
 written += gzwrite(file, ((char *) payload) + 3, 1);
 written += gzwrite(file, ((char *) payload) + 2, 1);
 written += gzwrite(file, ((char *) payload) + 1, 1);
 written += gzwrite(file, ((char *) payload), 1);
 return written;
}

variable_types:
payload
int *


decompiled_code:
__int64 __fastcall nbt_write_int_array(__int64 a1, __int64 a2)
{
unsigned int v2; // er12
__int64 v3; // rbx

if ( !a2 )
return (unsigned int)-4;
v2 = nbt_write_int(a1, a2);
if ( v2 && *(int *)a2 > 0 )
{
v3 = 0LL;
do
v2 += nbt_write_int(a1, *(_QWORD *)(a2 + 8) + 4 * v3++);
while ( *(_DWORD *)a2 > (int)v3 );
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2)
{
unsigned int v2; // er12
__int64 v3; // rbx

if ( !a2 )
return (unsigned int)-4;
v2 = func3(a1, a2);
if ( v2 && *(int *)a2 > 0 )
{
v3 = 0LL;
do
v2 += func3(a1, *(_QWORD *)(a2 + 8) + 4 * v3++);
while ( *(_DWORD *)a2 > (int)v3 );
}
return v2;
}

original_code:
static int nbt_write_int_array(gzFile file, nbt_int_array *payload)
{
 int i, res, written = 0;
 if (!payload) {
  return -4;
 }
 res = nbt_write_int(file, &payload->size);
 if (!res) {
  return written;
 }
 written += res;
 for (i = 0; i < payload->size; i++) {
  res = nbt_write_int(file, payload->ints + i);
  if (res) {
   written += res;
  }
 }
 return written;
}

canonical_original_code:
static int nbt_write_int_array(struct gzFile_s *file, struct nbt_int_array *payload)
{
 int i; int res; int written = 0;
 if (!payload) {
  return -4;
 }
 res = nbt_write_int(file, &payload->size);
 if (!res) {
  return written;
 }
 written += res;
 for (i = 0; i < payload->size; i++) {
  res = nbt_write_int(file, payload->ints + i);
  if (res) {
   written += res;
  }
 }
 return written;
}

variable_types: struct nbt_int_array { int size; int *ints; } *


decompiled_code:
__int64 __fastcall nbt_write_long(__int64 a1, __int64 a2)
{
int v2; // ebx
int v3; // ebx
int v4; // ebx
int v5; // ebx
int v6; // ebx
int v7; // ebx
int v8; // ebx

if ( !a2 )
return 4294967292LL;
v2 = gzwrite(a1, a2 + 7, 1LL);
v3 = gzwrite(a1, a2 + 6, 1LL) + v2;
v4 = gzwrite(a1, a2 + 5, 1LL) + v3;
v5 = gzwrite(a1, a2 + 4, 1LL) + v4;
v6 = gzwrite(a1, a2 + 3, 1LL) + v5;
v7 = gzwrite(a1, a2 + 2, 1LL) + v6;
v8 = gzwrite(a1, a2 + 1, 1LL) + v7;
return v8 + (unsigned int)gzwrite(a1, a2, 1LL);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2)
{
int v2; // ebx
int v3; // ebx
int v4; // ebx
int v5; // ebx
int v6; // ebx
int v7; // ebx
int v8; // ebx

if ( !a2 )
return 4294967292LL;
v2 = gzwrite(a1, a2 + 7, 1LL);
v3 = gzwrite(a1, a2 + 6, 1LL) + v2;
v4 = gzwrite(a1, a2 + 5, 1LL) + v3;
v5 = gzwrite(a1, a2 + 4, 1LL) + v4;
v6 = gzwrite(a1, a2 + 3, 1LL) + v5;
v7 = gzwrite(a1, a2 + 2, 1LL) + v6;
v8 = gzwrite(a1, a2 + 1, 1LL) + v7;
return v8 + (unsigned int)gzwrite(a1, a2, 1LL);
}

original_code:
static int nbt_write_long(gzFile file, nbt_long *payload)
{
 int written = 0;
 if (!payload) {
  return -4;
 }
 written += gzwrite(file, ((nbt_byte *) payload) + 7, 1);
 written += gzwrite(file, ((nbt_byte *) payload) + 6, 1);
 written += gzwrite(file, ((nbt_byte *) payload) + 5, 1);
 written += gzwrite(file, ((nbt_byte *) payload) + 4, 1);
 written += gzwrite(file, ((nbt_byte *) payload) + 3, 1);
 written += gzwrite(file, ((nbt_byte *) payload) + 2, 1);
 written += gzwrite(file, ((nbt_byte *) payload) + 1, 1);
 written += gzwrite(file, ((nbt_byte *) payload), 1);
 return written;
}

canonical_original_code:
static int nbt_write_long(struct gzFile_s *file, long *payload)
{
 int written = 0;
 if (!payload) {
  return -4;
 }
 written += gzwrite(file, ((char *) payload) + 7, 1);
 written += gzwrite(file, ((char *) payload) + 6, 1);
 written += gzwrite(file, ((char *) payload) + 5, 1);
 written += gzwrite(file, ((char *) payload) + 4, 1);
 written += gzwrite(file, ((char *) payload) + 3, 1);
 written += gzwrite(file, ((char *) payload) + 2, 1);
 written += gzwrite(file, ((char *) payload) + 1, 1);
 written += gzwrite(file, ((char *) payload), 1);
 return written;
}

variable_types:
payload
long *


decompiled_code:
__int64 __fastcall nbt_write_byte_array(__int64 a1, __int64 a2)
{
unsigned int v2; // er12
__int64 v3; // rbx

if ( !a2 )
return (unsigned int)-4;
v2 = nbt_write_int(a1, a2);
if ( v2 && *(int *)a2 > 0 )
{
v3 = 0LL;
do
v2 += gzwrite(a1, *(_QWORD *)(a2 + 8) + v3++, 1LL);
while ( *(_DWORD *)a2 > (int)v3 );
}
return v2;
}

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

if ( !a2 )
return (unsigned int)-4;
v2 = func3(a1, a2);
if ( v2 && *(int *)a2 > 0 )
{
v3 = 0LL;
do
v2 += gzwrite(a1, *(_QWORD *)(a2 + 8) + v3++, 1LL);
while ( *(_DWORD *)a2 > (int)v3 );
}
return v2;
}

original_code:
static int nbt_write_byte_array(gzFile file, nbt_byte_array *payload)
{
 int i, res, written = 0;
 if (!payload) {
  return -4;
 }
 res = nbt_write_int(file, &payload->size);
 if (!res) {
  return written;
 }
 written += res;
 for (i = 0; i < payload->size; i++) {
  res = gzwrite(file, payload->bytes + i, 1);
  if (res) {
   written += res;
  }
 }
 return written;
}

canonical_original_code:
static int nbt_write_byte_array(struct gzFile_s *file, struct nbt_byte_array *payload)
{
 int i; int res; int written = 0;
 if (!payload) {
  return -4;
 }
 res = nbt_write_int(file, &payload->size);
 if (!res) {
  return written;
 }
 written += res;
 for (i = 0; i < payload->size; i++) {
  res = gzwrite(file, payload->bytes + i, 1);
  if (res) {
   written += res;
  }
 }
 return written;
}

variable_types: struct nbt_byte_array { int size; char *bytes; } *

variable_types: i int


decompiled_code:
__int64 __fastcall nbt_write_payload(__int64 a1, char a2, __int16 *a3)
{
__int64 result; // rax

switch ( a2 )
{
case 0:
result = 0LL;
break;
case 1:
result = nbt_write_byte(a1, (__int64)a3);
break;
case 2:
result = nbt_write_short(a1, (__int64)a3);
break;
case 3:
result = nbt_write_int(a1, (__int64)a3);
break;
case 4:
result = nbt_write_long(a1, (__int64)a3);
break;
case 5:
result = nbt_write_float(a1, (__int64)a3);
break;
case 6:
result = nbt_write_double(a1, (__int64)a3);
break;
case 7:
result = nbt_write_byte_array(a1, (__int64)a3);
break;
case 8:
result = nbt_write_string(a1, a3);
break;
case 9:
result = nbt_write_list(a1, a3);
break;
case 10:
result = nbt_write_compound(a1, a3);
break;
case 11:
result = nbt_write_int_array(a1, (__int64)a3);
break;
default:
result = 4294967293LL;
break;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, char a2, __int16 *a3)
{
__int64 result; // rax

switch ( a2 )
{
case 0:
result = 0LL;
break;
case 1:
result = func0(a1, (__int64)a3);
break;
case 2:
result = func1(a1, (__int64)a3);
break;
case 3:
result = func3(a1, (__int64)a3);
break;
case 4:
result = func5(a1, (__int64)a3);
break;
case 5:
result = func6(a1, (__int64)a3);
break;
case 6:
result = func7(a1, (__int64)a3);
break;
case 7:
result = func8(a1, (__int64)a3);
break;
case 8:
result = func2(a1, a3);
break;
case 9:
result = func12(a1, a3);
break;
case 10:
result = func11(a1, a3);
break;
case 11:
result = func4(a1, (__int64)a3);
break;
default:
result = 4294967293LL;
break;
}
return result;
}

original_code:
static int nbt_write_payload(gzFile file, nbt_byte id, nbt_payload *payload)
{
 switch (id) {
  case 0:
   return 0;
  case 1:
   return nbt_write_byte(file, (nbt_byte *) payload);
  case 2:
   return nbt_write_short(file, (nbt_short *) payload);
  case 3:
   return nbt_write_int(file, (nbt_int *) payload);
  case 4:
   return nbt_write_long(file, (nbt_long *) payload);
  case 5:
   return nbt_write_float(file, (nbt_float *) payload);
  case 6:
   return nbt_write_double(file, (nbt_double *) payload);
  case 7:
   return nbt_write_byte_array(file, (nbt_byte_array *) payload);
  case 8:
   return nbt_write_string(file, (nbt_string *) payload);
  case 9:
   return nbt_write_list(file, (nbt_list *) payload);
  case 10:
   return nbt_write_compound(file, (nbt_compound *) payload);
  case 11:
   return nbt_write_int_array(file, (nbt_int_array *) payload);
  default:
   return -3;
 }
}

canonical_original_code:
static int nbt_write_payload(struct gzFile_s *file, char id, union nbt_payload *payload)
{
 switch (id) {
  case 0:
   return 0;
  case 1:
   return nbt_write_byte(file, (char *) payload);
  case 2:
   return nbt_write_short(file, (short *) payload);
  case 3:
   return nbt_write_int(file, (int *) payload);
  case 4:
   return nbt_write_long(file, (long *) payload);
  case 5:
   return nbt_write_float(file, (float *) payload);
  case 6:
   return nbt_write_double(file, (double *) payload);
  case 7:
   return nbt_write_byte_array(file, (struct nbt_byte_array *) payload);
  case 8:
   return nbt_write_string(file, (struct nbt_string *) payload);
  case 9:
   return nbt_write_list(file, (struct nbt_list *) payload);
  case 10:
   return nbt_write_compound(file, (struct nbt_tag **) payload);
  case 11:
   return nbt_write_int_array(file, (struct nbt_int_array *) payload);
  default:
   return -3;
 }
}

variable_types: id char

variable_types: union nbt_payload { char byte_payload; short short_payload; int int_payload; long long_payload; float float_payload; double double_payload; struct nbt_byte_array { int size; char *bytes; } byte_array_payload; struct nbt_string { short length; char *chars; } string_payload; struct nbt_list { char tag_id; int size; union nbt_payload *tags; } list_payload; struct nbt_tag { char id; union nbt_payload payload; unsigned long position; struct nbt_string name; } *compound_payload; struct nbt_int_array { int size; int *ints; } int_array_payload; } *

user_defined_types: union nbt_payload { char byte_payload; short short_payload; int int_payload; long long_payload; float float_payload; double double_payload; struct nbt_byte_array byte_array_payload; struct nbt_string string_payload; struct nbt_list list_payload; struct nbt_tag *compound_payload; struct nbt_int_array int_array_payload; }


decompiled_code:
__int64 __fastcall nbt_write_tag(__int64 a1, __int64 a2)
{
unsigned int v2; // ebp
int v4; // ebp

if ( !a2 )
return (unsigned int)-4;
v2 = nbt_write_byte(a1, a2);
if ( *(_BYTE *)a2 )
{
v4 = nbt_write_string(a1, (__int16 *)(a2 + 32)) + v2;
v2 = nbt_write_payload(a1, *(_BYTE *)a2, (__int16 *)(a2 + 8)) + v4;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, __int64 a2)
{
unsigned int v2; // ebp
int v4; // ebp

if ( !a2 )
return (unsigned int)-4;
v2 = func0(a1, a2);
if ( *(_BYTE *)a2 )
{
v4 = func2(a1, (__int16 *)(a2 + 32)) + v2;
v2 = func9(a1, *(_BYTE *)a2, (__int16 *)(a2 + 8)) + v4;
}
return v2;
}

original_code:
static int nbt_write_tag(gzFile file, nbt_tag *tag)
{
 int written = 0;
 if (!tag) {
  return -4;
 }
 written += nbt_write_byte(file, &tag->id);
 if (tag->id != 0) {
  written += nbt_write_string(file, &tag->name);
  written += nbt_write_payload(file, tag->id, &tag->payload);
 }
 return written;
}

canonical_original_code:
static int nbt_write_tag(struct gzFile_s *file, struct nbt_tag *tag)
{
 int written = 0;
 if (!tag) {
  return -4;
 }
 written += nbt_write_byte(file, &tag->id);
 if (tag->id != 0) {
  written += nbt_write_string(file, &tag->name);
  written += nbt_write_payload(file, tag->id, &tag->payload);
 }
 return written;
}

variable_types: struct nbt_tag { char id; union nbt_payload { char byte_payload; short short_payload; int int_payload; long long_payload; float float_payload; double double_payload; struct nbt_byte_array { int size; char *bytes; } byte_array_payload; struct nbt_string { short length; char *chars; } string_payload; struct nbt_list { char tag_id; int size; union nbt_payload *tags; } list_payload; struct nbt_tag *compound_payload; struct nbt_int_array { int size; int *ints; } int_array_payload; } payload; unsigned long position; struct nbt_string name; } *

user_defined_types: struct nbt_tag { char id; union nbt_payload payload; unsigned long position; struct nbt_string name; }


decompiled_code:
__int64 __fastcall nbt_write_compound(__int64 a1, _BYTE **a2)
{
_BYTE *v2; // rbx
unsigned int v3; // ebp

if ( !a2 )
return (unsigned int)-4;
v2 = *a2;
v3 = 0;
while ( 1 )
{
v3 += nbt_write_tag(a1, (__int64)v2);
if ( !*v2 )
break;
v2 += 48;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, _BYTE **a2)
{
_BYTE *v2; // rbx
unsigned int v3; // ebp

if ( !a2 )
return (unsigned int)-4;
v2 = *a2;
v3 = 0;
while ( 1 )
{
v3 += func10(a1, (__int64)v2);
if ( !*v2 )
break;
v2 += 48;
}
return v3;
}

original_code:
static int nbt_write_compound(gzFile file, nbt_compound *payload)
{
 int res, written = 0;
 nbt_tag *curr;
 if (!payload) {
  return -4;
 }
 for (curr = * (nbt_tag **) payload;; curr++) {
  res = nbt_write_tag(file, curr);
  if (res) {
   written += res;
  }
  if (curr->id == 0) {
   break;
  }
 }
 return written;
}

canonical_original_code:
static int nbt_write_compound(struct gzFile_s *file, struct nbt_tag **payload)
{
 int res; int written = 0;
 struct nbt_tag *curr;
 if (!payload) {
  return -4;
 }
 for (curr = * (struct nbt_tag **) payload;; curr++) {
  res = nbt_write_tag(file, curr);
  if (res) {
   written += res;
  }
  if (curr->id == 0) {
   break;
  }
 }
 return written;
}

variable_types: struct nbt_tag { char id; union nbt_payload { char byte_payload; short short_payload; int int_payload; long long_payload; float float_payload; double double_payload; struct nbt_byte_array { int size; char *bytes; } byte_array_payload; struct nbt_string { short length; char *chars; } string_payload; struct nbt_list { char tag_id; int size; union nbt_payload *tags; } list_payload; struct nbt_tag *compound_payload; struct nbt_int_array { int size; int *ints; } int_array_payload; } payload; unsigned long position; struct nbt_string name; } **


decompiled_code:
__int64 __fastcall nbt_write_list(__int64 a1, __int64 a2)
{
unsigned int v2; // ebp
int v4; // eax
__int64 v5; // r12

if ( !a2 )
return (unsigned int)-4;
v2 = nbt_write_byte(a1, a2);
if ( v2 )
{
v4 = nbt_write_int(a1, a2 + 4);
if ( v4 )
{
v2 += v4;
if ( *(int *)(a2 + 4) > 0 )
{
v5 = 0LL;
do
v2 += nbt_write_payload(a1, *(_BYTE *)a2, (__int16 *)(*(_QWORD *)(a2 + 8) + 16 * v5++));
while ( *(_DWORD *)(a2 + 4) > (int)v5 );
}
}
}
return v2;
}

original_code:
static int nbt_write_list(gzFile file, nbt_list *payload)
{
 int i, res, written = 0;
 if (!payload) {
  return -4;
 }
 res = nbt_write_byte(file, &payload->tag_id);
 if (!res) {
  return written;
 }
 written += res;
 res = nbt_write_int(file, &payload->size);
 if (!res) {
  return written;
 }
 written += res;
 for (i = 0; i < payload->size; i++) {
  res = nbt_write_payload(file, payload->tag_id, (nbt_payload *) (payload->tags + i));
  if (res) {
   written += res;
  }
 }
 return written;
}

canonical_original_code:
static int nbt_write_list(struct gzFile_s *file, struct nbt_list *payload)
{
 int i; int res; int written = 0;
 if (!payload) {
  return -4;
 }
 res = nbt_write_byte(file, &payload->tag_id);
 if (!res) {
  return written;
 }
 written += res;
 res = nbt_write_int(file, &payload->size);
 if (!res) {
  return written;
 }
 written += res;
 for (i = 0; i < payload->size; i++) {
  res = nbt_write_payload(file, payload->tag_id, (union nbt_payload *) (payload->tags + i));
  if (res) {
   written += res;
  }
 }
 return written;
}

variable_types: struct nbt_list { char tag_id; int size; union nbt_payload { char byte_payload; short short_payload; int int_payload; long long_payload; float float_payload; double double_payload; struct nbt_byte_array { int size; char *bytes; } byte_array_payload; struct nbt_string { short length; char *chars; } string_payload; struct nbt_list list_payload; struct nbt_tag { char id; union nbt_payload payload; unsigned long position; struct nbt_string name; } *compound_payload; struct nbt_int_array { int size; int *ints; } int_array_payload; } *tags; } *

user_defined_types: struct nbt_list { char tag_id; int size; union nbt_payload *tags; }


decompiled_code:
__int64 __fastcall nbt_write(__int64 a1, __int64 a2)
{
__int64 v2; // rax
__int64 v3; // rbx
unsigned int v4; // ebp

if ( !a1 )
return (unsigned int)-5;
v2 = gzopen(a2, &unk_5E8);
v3 = v2;
if ( !v2 )
return (unsigned int)-1;
v4 = nbt_write_tag(v2, a1);
gzclose(v3);
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1, __int64 a2)
{
__int64 v2; // rax
__int64 v3; // rbx
unsigned int v4; // ebp

if ( !a1 )
return (unsigned int)-5;
v2 = gzopen(a2, &unk_5E8);
v3 = v2;
if ( !v2 )
return (unsigned int)-1;
v4 = func10(v2, a1);
gzclose(v3);
return v4;
}

original_code:
int nbt_write(nbt_tag *tag, char *path)
{
 int res;
 gzFile file;
 if (!tag) {
  return -5;
 }
 file = gzopen(path, "wb");
 if (!file) {
  return -1;
 }
 res = nbt_write_tag(file, tag);
 gzclose(file);
 return res;
}

canonical_original_code:
int nbt_write(struct nbt_tag *tag, char *path)
{
 int res;
 struct gzFile_s *file;
 if (!tag) {
  return -5;
 }
 file = gzopen(path, "wb");
 if (!file) {
  return -1;
 }
 res = nbt_write_tag(file, tag);
 gzclose(file);
 return res;
}

variable_types:
path
char *

user_defined_types: struct nbt_byte_array { int size; char *bytes; }

user_defined_types: struct nbt_string { short length; char *chars; }

user_defined_types: struct nbt_int_array { int size; int *ints; }


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

if ( a2 )
result = gzread(a1, a2, 1LL);
else
result = 4294967292LL;
return result;
}

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

if ( a2 )
result = gzread(a1, a2, 1LL);
else
result = 4294967292LL;
return result;
}

original_code:
static int nbt_read_byte(gzFile file, nbt_byte *payload)
{
 return payload ? gzread(file, payload, 1) : -4;
}

canonical_original_code:
static int nbt_read_byte(struct gzFile_s *file, char *payload)
{
 return payload ? gzread(file, payload, 1) : -4;
}

variable_types: struct gzFile_s { unsigned int have; unsigned char *next; long pos; } *


decompiled_code:
__int64 __fastcall nbt_read_short(__int64 a1, __int64 a2)
{
int v2; // ebp

if ( !a2 )
return 4294967292LL;
v2 = gzread(a1, a2 + 1, 1LL);
return v2 + (unsigned int)gzread(a1, a2, 1LL);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2)
{
int v2; // ebp

if ( !a2 )
return 4294967292LL;
v2 = gzread(a1, a2 + 1, 1LL);
return v2 + (unsigned int)gzread(a1, a2, 1LL);
}

original_code:
static int nbt_read_short(gzFile file, nbt_short *payload)
{
 int read = 0;
 if (!payload) {
  return -4;
 }
 read += gzread(file, ((nbt_byte *) payload) + 1, 1);
 read += gzread(file, ((nbt_byte *) payload), 1);
 return read;
}

canonical_original_code:
static int nbt_read_short(struct gzFile_s *file, short *payload)
{
 int read = 0;
 if (!payload) {
  return -4;
 }
 read += gzread(file, ((char *) payload) + 1, 1);
 read += gzread(file, ((char *) payload), 1);
 return read;
}

variable_types: read int


decompiled_code:
__int64 __fastcall nbt_read_int(__int64 a1, __int64 a2)
{
int v2; // ebx
int v3; // ebx
int v4; // ebx

if ( !a2 )
return 4294967292LL;
v2 = gzread(a1, a2 + 3, 1LL);
v3 = gzread(a1, a2 + 2, 1LL) + v2;
v4 = gzread(a1, a2 + 1, 1LL) + v3;
return v4 + (unsigned int)gzread(a1, a2, 1LL);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2)
{
int v2; // ebx
int v3; // ebx
int v4; // ebx

if ( !a2 )
return 4294967292LL;
v2 = gzread(a1, a2 + 3, 1LL);
v3 = gzread(a1, a2 + 2, 1LL) + v2;
v4 = gzread(a1, a2 + 1, 1LL) + v3;
return v4 + (unsigned int)gzread(a1, a2, 1LL);
}

original_code:
static int nbt_read_int(gzFile file, nbt_int *payload)
{
 int read = 0;
 if (!payload) {
  return -4;
 }
 read += gzread(file, ((nbt_byte *) payload) + 3, 1);
 read += gzread(file, ((nbt_byte *) payload) + 2, 1);
 read += gzread(file, ((nbt_byte *) payload) + 1, 1);
 read += gzread(file, ((nbt_byte *) payload), 1);
 return read;
}

canonical_original_code:
static int nbt_read_int(struct gzFile_s *file, int *payload)
{
 int read = 0;
 if (!payload) {
  return -4;
 }
 read += gzread(file, ((char *) payload) + 3, 1);
 read += gzread(file, ((char *) payload) + 2, 1);
 read += gzread(file, ((char *) payload) + 1, 1);
 read += gzread(file, ((char *) payload), 1);
 return read;
}


decompiled_code:
__int64 __fastcall nbt_read_long(__int64 a1, __int64 a2)
{
int v2; // ebx
int v3; // ebx
int v4; // ebx
int v5; // ebx
int v6; // ebx
int v7; // ebx
int v8; // ebx

if ( !a2 )
return 4294967292LL;
v2 = gzread(a1, a2 + 7, 1LL);
v3 = gzread(a1, a2 + 6, 1LL) + v2;
v4 = gzread(a1, a2 + 5, 1LL) + v3;
v5 = gzread(a1, a2 + 4, 1LL) + v4;
v6 = gzread(a1, a2 + 3, 1LL) + v5;
v7 = gzread(a1, a2 + 2, 1LL) + v6;
v8 = gzread(a1, a2 + 1, 1LL) + v7;
return v8 + (unsigned int)gzread(a1, a2, 1LL);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2)
{
int v2; // ebx
int v3; // ebx
int v4; // ebx
int v5; // ebx
int v6; // ebx
int v7; // ebx
int v8; // ebx

if ( !a2 )
return 4294967292LL;
v2 = gzread(a1, a2 + 7, 1LL);
v3 = gzread(a1, a2 + 6, 1LL) + v2;
v4 = gzread(a1, a2 + 5, 1LL) + v3;
v5 = gzread(a1, a2 + 4, 1LL) + v4;
v6 = gzread(a1, a2 + 3, 1LL) + v5;
v7 = gzread(a1, a2 + 2, 1LL) + v6;
v8 = gzread(a1, a2 + 1, 1LL) + v7;
return v8 + (unsigned int)gzread(a1, a2, 1LL);
}

original_code:
static int nbt_read_long(gzFile file, nbt_long *payload)
{
 int read = 0;
 if (!payload) {
  return -4;
 }
 read += gzread(file, ((nbt_byte *) payload) + 7, 1);
 read += gzread(file, ((nbt_byte *) payload) + 6, 1);
 read += gzread(file, ((nbt_byte *) payload) + 5, 1);
 read += gzread(file, ((nbt_byte *) payload) + 4, 1);
 read += gzread(file, ((nbt_byte *) payload) + 3, 1);
 read += gzread(file, ((nbt_byte *) payload) + 2, 1);
 read += gzread(file, ((nbt_byte *) payload) + 1, 1);
 read += gzread(file, ((nbt_byte *) payload), 1);
 return read;
}

canonical_original_code:
static int nbt_read_long(struct gzFile_s *file, long *payload)
{
 int read = 0;
 if (!payload) {
  return -4;
 }
 read += gzread(file, ((char *) payload) + 7, 1);
 read += gzread(file, ((char *) payload) + 6, 1);
 read += gzread(file, ((char *) payload) + 5, 1);
 read += gzread(file, ((char *) payload) + 4, 1);
 read += gzread(file, ((char *) payload) + 3, 1);
 read += gzread(file, ((char *) payload) + 2, 1);
 read += gzread(file, ((char *) payload) + 1, 1);
 read += gzread(file, ((char *) payload), 1);
 return read;
}


decompiled_code:
__int64 __fastcall nbt_read_string(__int64 a1, __int16 *a2)
{
__int16 v2; // bx
void *v3; // rax
__int64 v4; // rbx
int v5; // edx

if ( !a2 )
return 4294967292LL;
if ( !(unsigned int)nbt_read_short(a1, (__int64)a2) )
return 0xFFFFFFFFLL;
v2 = *a2;
v3 = malloc(*a2 + 1LL);
*((_QWORD *)a2 + 1) = v3;
if ( !v3 )
return 4294967294LL;
if ( v2 <= 0 )
{
v5 = 0;
}
else
{
v4 = 0LL;
do
{
if ( !(unsigned int)gzread(a1, *((_QWORD *)a2 + 1) + v4, 1LL) )
*(_BYTE *)(*((_QWORD *)a2 + 1) + v4) = 0;
v5 = ++v4;
}
while ( *a2 > (int)v4 );
}
*(_BYTE *)(*((_QWORD *)a2 + 1) + v5) = 0;
return (unsigned int)(*a2 + 2);
}

original_code:
static int nbt_read_string(gzFile file, nbt_string *payload)
{
 int i;
 if (!payload) {
  return -4;
 }
 if (!nbt_read_short(file, &payload->length)) {
  return -1;
 }
 payload->chars = malloc(sizeof(char) * payload->length + 1);
 if (!payload->chars) {
  return -2;
 }
 for (i = 0; i < payload->length; i++) {
  if (!gzread(file, payload->chars + i, 1)) {
   payload->chars[i] = 0;
  }
 }
 payload->chars[i] = 0;
 return payload->length + sizeof(nbt_short);
}

canonical_original_code:
static int nbt_read_string(struct gzFile_s *file, struct nbt_string *payload)
{
 int i;
 if (!payload) {
  return -4;
 }
 if (!nbt_read_short(file, &payload->length)) {
  return -1;
 }
 payload->chars = malloc(sizeof(char) * payload->length + 1);
 if (!payload->chars) {
  return -2;
 }
 for (i = 0; i < payload->length; i++) {
  if (!gzread(file, payload->chars + i, 1)) {
   payload->chars[i] = 0;
  }
 }
 payload->chars[i] = 0;
 return payload->length + sizeof(nbt_short);
}


decompiled_code:
__int64 __fastcall nbt_read_byte_array(__int64 a1, int *a2)
{
int v2; // er12
void *v3; // rax
__int64 v4; // rbx

if ( !a2 )
return 4294967292LL;
if ( !(unsigned int)nbt_read_int(a1, (__int64)a2) )
return 0xFFFFFFFFLL;
v2 = *a2;
v3 = malloc(*a2);
*((_QWORD *)a2 + 1) = v3;
if ( !v3 )
return 4294967294LL;
if ( v2 > 0 )
{
v4 = 0LL;
do
{
if ( !(unsigned int)nbt_read_byte(a1, *((_QWORD *)a2 + 1) + v4) )
*(_BYTE *)(*((_QWORD *)a2 + 1) + v4) = 0;
v2 = *a2;
++v4;
}
while ( *a2 > (int)v4 );
}
return (unsigned int)(v2 + 4);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, int *a2)
{
int v2; // er12
void *v3; // rax
__int64 v4; // rbx

if ( !a2 )
return 4294967292LL;
if ( !(unsigned int)func2(a1, (__int64)a2) )
return 0xFFFFFFFFLL;
v2 = *a2;
v3 = malloc(*a2);
*((_QWORD *)a2 + 1) = v3;
if ( !v3 )
return 4294967294LL;
if ( v2 > 0 )
{
v4 = 0LL;
do
{
if ( !(unsigned int)func0(a1, *((_QWORD *)a2 + 1) + v4) )
*(_BYTE *)(*((_QWORD *)a2 + 1) + v4) = 0;
v2 = *a2;
++v4;
}
while ( *a2 > (int)v4 );
}
return (unsigned int)(v2 + 4);
}

original_code:
static int nbt_read_byte_array(gzFile file, nbt_byte_array *payload)
{
 int i;
 if (!payload) {
  return -4;
 }
 if (!nbt_read_int(file, &payload->size)) {
  return -1;
 }
 payload->bytes = (nbt_byte *) malloc(sizeof(nbt_byte) * payload->size);
 if (!payload->bytes) {
  return -2;
 }
 for (i = 0; i < payload->size; i++) {
  if (!nbt_read_byte(file, payload->bytes + i)) {
   payload->bytes[i] = 0;
  }
 }
 return payload->size + sizeof(nbt_int);
}

canonical_original_code:
static int nbt_read_byte_array(struct gzFile_s *file, struct nbt_byte_array *payload)
{
 int i;
 if (!payload) {
  return -4;
 }
 if (!nbt_read_int(file, &payload->size)) {
  return -1;
 }
 payload->bytes = (char *) malloc(sizeof(nbt_byte) * payload->size);
 if (!payload->bytes) {
  return -2;
 }
 for (i = 0; i < payload->size; i++) {
  if (!nbt_read_byte(file, payload->bytes + i)) {
   payload->bytes[i] = 0;
  }
 }
 return payload->size + sizeof(nbt_int);
}


decompiled_code:
__int64 __fastcall nbt_read_int_array(__int64 a1, int *a2)
{
int v2; // er12
void *v3; // rax
__int64 v4; // rbx

if ( !a2 )
return 4294967292LL;
if ( !(unsigned int)nbt_read_int(a1, (__int64)a2) )
return 0xFFFFFFFFLL;
v2 = *a2;
v3 = malloc(4LL * *a2);
*((_QWORD *)a2 + 1) = v3;
if ( !v3 )
return 4294967294LL;
if ( v2 > 0 )
{
v4 = 0LL;
do
{
if ( !(unsigned int)nbt_read_int(a1, *((_QWORD *)a2 + 1) + 4 * v4) )
*(_DWORD *)(*((_QWORD *)a2 + 1) + 4 * v4) = 0;
v2 = *a2;
++v4;
}
while ( *a2 > (int)v4 );
}
return (unsigned int)(v2 + 4);
}

original_code:
static int nbt_read_int_array(gzFile file, nbt_int_array *payload)
{
 int i;
 if (!payload) {
  return -4;
 }
 if (!nbt_read_int(file, &payload->size)) {
  return -1;
 }
 payload->ints = (nbt_int *) malloc(sizeof(nbt_int) * payload->size);
 if (!payload->ints) {
  return -2;
 }
 for (i = 0; i < payload->size; i++) {
  if (!nbt_read_int(file, payload->ints + i)) {
   payload->ints[i] = 0;
  }
 }
 return payload->size + sizeof(nbt_int);
}

canonical_original_code:
static int nbt_read_int_array(struct gzFile_s *file, struct nbt_int_array *payload)
{
 int i;
 if (!payload) {
  return -4;
 }
 if (!nbt_read_int(file, &payload->size)) {
  return -1;
 }
 payload->ints = (int *) malloc(sizeof(nbt_int) * payload->size);
 if (!payload->ints) {
  return -2;
 }
 for (i = 0; i < payload->size; i++) {
  if (!nbt_read_int(file, payload->ints + i)) {
   payload->ints[i] = 0;
  }
 }
 return payload->size + sizeof(nbt_int);
}


decompiled_code:
__int64 __fastcall nbt_read_compound(__int64 a1, void **a2)
{
__int64 v2; // rbp
unsigned int v3; // er15
int v4; // er14
int i; // er13
void *v6; // rdi
int v7; // eax
char v8; // al
void *v9; // rax

if ( !a2 )
return (unsigned int)-4;
*a2 = 0LL;
v2 = 0LL;
v3 = 0;
v4 = 0;
for ( i = 0; ; ++i )
{
if ( i >= v4 )
{
v4 += 8;
v9 = realloc(*a2, 48LL * v4);
if ( !v9 )
{
v6 = *a2;
v3 = -2;
if ( !*a2 )
return v3;
goto LABEL_12;
}
*a2 = v9;
}
v7 = nbt_read_tag(a1, (char *)*a2 + v2);
if ( v7 <= 0 )
break;
v3 += v7;
v8 = *((_BYTE *)*a2 + v2);
v2 += 48LL;
if ( !v8 )
return v3;
}
v6 = *a2;
v3 = v7;
if ( !*a2 )
return v3;
LABEL_12:
free(v6);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, void **a2)
{
__int64 v2; // rbp
unsigned int v3; // er15
int v4; // er14
int i; // er13
void *v6; // rdi
int v7; // eax
char v8; // al
void *v9; // rax

if ( !a2 )
return (unsigned int)-4;
*a2 = 0LL;
v2 = 0LL;
v3 = 0;
v4 = 0;
for ( i = 0; ; ++i )
{
if ( i >= v4 )
{
v4 += 8;
v9 = realloc(*a2, 48LL * v4);
if ( !v9 )
{
v6 = *a2;
v3 = -2;
if ( !*a2 )
return v3;
goto LABEL_12;
}
*a2 = v9;
}
v7 = func11(a1, (char *)*a2 + v2);
if ( v7 <= 0 )
break;
v3 += v7;
v8 = *((_BYTE *)*a2 + v2);
v2 += 48LL;
if ( !v8 )
return v3;
}
v6 = *a2;
v3 = v7;
if ( !*a2 )
return v3;
LABEL_12:
free(v6);
return v3;
}

original_code:
static int nbt_read_compound(gzFile file, nbt_compound *payload)
{
 int i, max = 0, res, read = 0;
 void *tmpptr;
 if (!payload) {
  return -4;
 }
 *payload = (nbt_tag *) 0;
 for (i = 0;; i++) {
  if (i >= max) {
   max += 8;
   tmpptr = realloc(*payload, sizeof(struct nbt_tag) * max);
   if (!tmpptr) {
    if (*payload) {
     free(*payload);
    }
    return -2;
   }
   *payload = tmpptr;
  }
  res = nbt_read_tag(file, *payload + i);
  if (res > 0) {
   read += res;
   if ((*payload + i)->id == 0) {
    return read;
   }
  } else {
   if (*payload) {
    free(*payload);
   }
   return res;
  }
 }
 return read;
}

canonical_original_code:
static int nbt_read_compound(struct gzFile_s *file, struct nbt_tag **payload)
{
 int i; int max = 0; int res; int read = 0;
 void *tmpptr;
 if (!payload) {
  return -4;
 }
 *payload = (struct nbt_tag *) 0;
 for (i = 0;; i++) {
  if (i >= max) {
   max += 8;
   tmpptr = realloc(*payload, sizeof(struct nbt_tag) * max);
   if (!tmpptr) {
    if (*payload) {
     free(*payload);
    }
    return -2;
   }
   *payload = tmpptr;
  }
  res = nbt_read_tag(file, *payload + i);
  if (res > 0) {
   read += res;
   if ((*payload + i)->id == 0) {
    return read;
   }
  } else {
   if (*payload) {
    free(*payload);
   }
   return res;
  }
 }
 return read;
}

variable_types: max int

variable_types:
tmpptr
void *


decompiled_code:
__int64 __fastcall nbt_read_payload(__int64 a1, char a2, int *a3)
{
__int64 result; // rax

switch ( a2 )
{
case 0:
result = 0LL;
break;
case 1:
result = nbt_read_byte(a1, (__int64)a3);
break;
case 2:
result = nbt_read_short(a1, (__int64)a3);
break;
case 3:
result = nbt_read_int(a1, (__int64)a3);
break;
case 4:
result = nbt_read_long(a1, (__int64)a3);
break;
case 5:
result = nbt_read_float(a1, (__int64)a3);
break;
case 6:
result = nbt_read_double(a1, (__int64)a3);
break;
case 7:
result = nbt_read_byte_array(a1, a3);
break;
case 8:
result = nbt_read_string(a1, (__int16 *)a3);
break;
case 9:
result = nbt_read_list(a1, a3);
break;
case 10:
result = nbt_read_compound(a1, (void **)a3);
break;
case 11:
result = nbt_read_int_array(a1, a3);
break;
default:
result = 4294967293LL;
break;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, char a2, int *a3)
{
__int64 result; // rax

switch ( a2 )
{
case 0:
result = 0LL;
break;
case 1:
result = func0(a1, (__int64)a3);
break;
case 2:
result = func1(a1, (__int64)a3);
break;
case 3:
result = func2(a1, (__int64)a3);
break;
case 4:
result = func3(a1, (__int64)a3);
break;
case 5:
result = func4(a1, (__int64)a3);
break;
case 6:
result = func5(a1, (__int64)a3);
break;
case 7:
result = func7(a1, a3);
break;
case 8:
result = func6(a1, (__int16 *)a3);
break;
case 9:
result = func12(a1, a3);
break;
case 10:
result = func9(a1, (void **)a3);
break;
case 11:
result = func8(a1, a3);
break;
default:
result = 4294967293LL;
break;
}
return result;
}

original_code:
static int nbt_read_payload(gzFile file, nbt_byte id, nbt_payload *payload)
{
 switch (id) {
  case 0:
   return 0;
  case 1:
   return nbt_read_byte(file, (nbt_byte *) payload);
  case 2:
   return nbt_read_short(file, (nbt_short *) payload);
  case 3:
   return nbt_read_int(file, (nbt_int *) payload);
  case 4:
   return nbt_read_long(file, (nbt_long *) payload);
  case 5:
   return nbt_read_float(file, (nbt_float *) payload);
  case 6:
   return nbt_read_double(file, (nbt_double *) payload);
  case 7:
   return nbt_read_byte_array(file, (nbt_byte_array *) payload);
  case 8:
   return nbt_read_string(file, (nbt_string *) payload);
  case 9:
   return nbt_read_list(file, (nbt_list *) payload);
  case 10:
   return nbt_read_compound(file, (nbt_compound *) payload);
  case 11:
   return nbt_read_int_array(file, (nbt_int_array *) payload);
  default:
   return -3;
 }
}

canonical_original_code:
static int nbt_read_payload(struct gzFile_s *file, char id, union nbt_payload *payload)
{
 switch (id) {
  case 0:
   return 0;
  case 1:
   return nbt_read_byte(file, (char *) payload);
  case 2:
   return nbt_read_short(file, (short *) payload);
  case 3:
   return nbt_read_int(file, (int *) payload);
  case 4:
   return nbt_read_long(file, (long *) payload);
  case 5:
   return nbt_read_float(file, (float *) payload);
  case 6:
   return nbt_read_double(file, (double *) payload);
  case 7:
   return nbt_read_byte_array(file, (struct nbt_byte_array *) payload);
  case 8:
   return nbt_read_string(file, (struct nbt_string *) payload);
  case 9:
   return nbt_read_list(file, (struct nbt_list *) payload);
  case 10:
   return nbt_read_compound(file, (struct nbt_tag **) payload);
  case 11:
   return nbt_read_int_array(file, (struct nbt_int_array *) payload);
  default:
   return -3;
 }
}


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

if ( !a2 )
return 4294967294LL;
if ( !(unsigned int)nbt_read_byte(a1, a2) )
return 0xFFFFFFFFLL;
if ( *(_BYTE *)a2 && !(unsigned int)nbt_read_string(a1, (__int16 *)(a2 + 32)) )
return 0xFFFFFFFFLL;
result = nbt_read_payload(a1, *(_BYTE *)a2, (int *)(a2 + 8));
if ( (int)result >= 0 )
result = (unsigned int)(result + *(__int16 *)(a2 + 32) + 3);
return result;
}

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

if ( !a2 )
return 4294967294LL;
if ( !(unsigned int)func0(a1, a2) )
return 0xFFFFFFFFLL;
if ( *(_BYTE *)a2 && !(unsigned int)func6(a1, (__int16 *)(a2 + 32)) )
return 0xFFFFFFFFLL;
result = func10(a1, *(_BYTE *)a2, (int *)(a2 + 8));
if ( (int)result >= 0 )
result = (unsigned int)(result + *(__int16 *)(a2 + 32) + 3);
return result;
}

original_code:
static int nbt_read_tag(gzFile file, nbt_tag *tag)
{
 int res;
 if (!tag) {
  return -2;
 }
 if (!(
  nbt_read_byte(file, &tag->id) &&
  (tag->id == 0 || nbt_read_string(file, &tag->name))
 )) {
  return -1;
 }
 res = nbt_read_payload(file, tag->id, &tag->payload);
 if (res >= 0) {
  return sizeof(nbt_byte) + sizeof(nbt_short) + tag->name.length + res;
 } else {
  return res;
 }
}

canonical_original_code:
static int nbt_read_tag(struct gzFile_s *file, struct nbt_tag *tag)
{
 int res;
 if (!tag) {
  return -2;
 }
 if (!(
  nbt_read_byte(file, &tag->id) &&
  (tag->id == 0 || nbt_read_string(file, &tag->name))
 )) {
  return -1;
 }
 res = nbt_read_payload(file, tag->id, &tag->payload);
 if (res >= 0) {
  return sizeof(nbt_byte) + sizeof(nbt_short) + tag->name.length + res;
 } else {
  return res;
 }
}