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

bfd = open(boot, 1);
if ( bfd == -1 )
die("open boot image");
result = open(kernel, 0);
kfd = result;
if ( result == -1 )
die("open kernel image");
return result;
}

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

bfd = open(boot, 1);
if ( bfd == -1 )
func7("open boot image");
result = open(kernel, 0);
kfd = result;
if ( result == -1 )
func7("open kernel image");
return result;
}

original_code:
static void open_image(void)
{
 bfd = open(boot, 01);
 if (bfd == -1)
  die("open boot image");
 kfd = open(kernel, 00);
 if (kfd == -1)
  die("open kernel image");
}

canonical_original_code:
static void open_image()
{
 bfd = open(boot, 01);
 if (bfd == -1)
  die("open boot image");
 kfd = open(kernel, 00);
 if (kfd == -1)
  die("open kernel image");
}


repo: chobits/tinyos

decompiled_code:
int close_image()
{
close(bfd);
return close(kfd);
}

canonical_decompiled_code:
int func11()
{
close(bfd);
return close(kfd);
}

original_code:
static void close_image(void)
{
 close(bfd);
 close(kfd);
}

canonical_original_code:
static void close_image()
{
 close(bfd);
 close(kfd);
}

return_type: void


decompiled_code:
ssize_t build_image()
{
int v0; // eax
int v1; // edx
int v2; // ebx
int v3; // ebx
int v4; // ebx
ssize_t result; // rax

v0 = lseek(kfd, 0LL, 2);
ksize = v0;
if ( v0 < 0 )
die("lseek kernel image");
v1 = v0 + 1022;
if ( v0 + 511 >= 0 )
v1 = v0 + 511;
ksize = v1 >> 9;
if ( v0 > 33553920 )
die("kernel image size is too large!");
v2 = bfd;
if ( lseek(bfd, 508LL, 0) != 508 )
die("lseek boot image");
if ( write(v2, &ksize, 2uLL) != 2 )
die("write boot image");
fsoff = (ksize + 9) / 2;
if ( ksize > 131062 )
die("file system offset is too large!");
v3 = bfd;
if ( lseek(bfd, 506LL, 0) != 506 )
die("lseek boot image");
if ( write(v3, &fsoff, 2uLL) != 2 )
die("write boot image");
fwriteoffset(fsoff << 10);
v4 = bfd;
if ( lseek(bfd, 500LL, 0) != 500 )
die("lseek boot image");
result = write(v4, &sectors, 4uLL);
if ( result != 4 )
die("write boot image");
return result;
}

canonical_decompiled_code:
ssize_t func13()
{
int v0; // eax
int v1; // edx
int v2; // ebx
int v3; // ebx
int v4; // ebx
ssize_t result; // rax

v0 = lseek(kfd, 0LL, 2);
ksize = v0;
if ( v0 < 0 )
func7("lseek kernel image");
v1 = v0 + 1022;
if ( v0 + 511 >= 0 )
v1 = v0 + 511;
ksize = v1 >> 9;
if ( v0 > 33553920 )
func7("kernel image size is too large!");
v2 = bfd;
if ( lseek(bfd, 508LL, 0) != 508 )
func7("lseek boot image");
if ( write(v2, &ksize, 2uLL) != 2 )
func7("write boot image");
fsoff = (ksize + 9) / 2;
if ( ksize > 131062 )
func7("file system offset is too large!");
v3 = bfd;
if ( lseek(bfd, 506LL, 0) != 506 )
func7("lseek boot image");
if ( write(v3, &fsoff, 2uLL) != 2 )
func7("write boot image");
func12(fsoff << 10);
v4 = bfd;
if ( lseek(bfd, 500LL, 0) != 500 )
func7("lseek boot image");
result = write(v4, &sectors, 4uLL);
if ( result != 4 )
func7("write boot image");
return result;
}

original_code:
static void build_image(void)
{
 ksize = lseek(kfd, 0, 2);
 if (ksize < 0)
  die("lseek kernel image");
 ksize = (ksize + 511) / 512;
 if (ksize > 0xffff)
  die("kernel image size is too large!");
 if (lseek(bfd, 508, 0) != 508)
  die("lseek boot image");
 if (write(bfd, &ksize, 2) != 2)
  die("write boot image");
 fsoff = ((8 + ksize) + 1) / 2;
 if (fsoff > 0xffff)
  die("file system offset is too large!");
 if (lseek(bfd, 506, 0) != 506)
  die("lseek boot image");
 if (write(bfd, &fsoff, 2) != 2)
  die("write boot image");
 fwriteoffset(fsoff * 1024);
 if (lseek(bfd, 500, 0) != 500)
  die("lseek boot image");
 if (write(bfd, &sectors, 4) != 4)
  die("write boot image");
}

canonical_original_code:
static void build_image()
{
 ksize = lseek(kfd, 0, 2);
 if (ksize < 0)
  die("lseek kernel image");
 ksize = (ksize + 511) / 512;
 if (ksize > 0xffff)
  die("kernel image size is too large!");
 if (lseek(bfd, 508, 0) != 508)
  die("lseek boot image");
 if (write(bfd, &ksize, 2) != 2)
  die("write boot image");
 fsoff = ((8 + ksize) + 1) / 2;
 if (fsoff > 0xffff)
  die("file system offset is too large!");
 if (lseek(bfd, 506, 0) != 506)
  die("lseek boot image");
 if (write(bfd, &fsoff, 2) != 2)
  die("write boot image");
 fwriteoffset(fsoff * 1024);
 if (lseek(bfd, 500, 0) != 500)
  die("lseek boot image");
 if (write(bfd, &sectors, 4) != 4)
  die("write boot image");
}


usage

decompiled_code:
void __noreturn usage()
{
fwrite("build bootimage kernelimage sectors\n", 1uLL, 0x24uLL, stderr);
exit(1);
}

canonical_decompiled_code:
void __noreturn func8()
{
fwrite("build bootimage kernelimage sectors\n", 1uLL, 0x24uLL, stderr);
exit(1);
}

original_code:
static void usage(void)
{
 fprintf(stderr, "build bootimage kernelimage sectors\n");
 exit(1);
}

canonical_original_code:
static void usage()
{
 fprintf(stderr, "build bootimage kernelimage sectors\n");
 exit(1);
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
parse_args(argc, (__int64)argv);
open_image();
build_image();
close_image();
return 0;
}

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

original_code:
int main(int argc, char **argv)
{
 parse_args(argc, argv);
 open_image();
 build_image();
 close_image();
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

return_type: int


vsprintf

original_code:
int vsprintf(char *buf, char *fmt, va_list args)
{
 char *dst = buf;
 unsigned int u;
 int d;
 char *s;
 void *p;
 int zero;
 int width;
 int left;
 int len;
 int swidth;
 while (*fmt) {
  if (*fmt != '%') {
   *dst++ = *fmt++;
   continue;
  }
  fmt++;
  zero = 0;
  width = 0;
  swidth = 0;
  left = 0;
  while (*fmt == '+' || *fmt == '-') {
   left = (*fmt == '-');
   fmt++;
  }
  while (*fmt == '0') {
   zero = 1;
   fmt++;
  }
  if (*fmt >= '1' && *fmt <= '9')
   width = strtoi(fmt, &fmt, 10);
  if (*fmt == '*') {
   swidth = (args += ((sizeof(int) + (sizeof(int) - 1)) & ~(sizeof(int) - 1)), *(int *)(args - ((sizeof(int) + (sizeof(int) - 1)) & ~(sizeof(int) - 1))));
   width = 0;
   fmt++;
  }
  switch (*fmt) {
  case '%':
   *dst = '%';
   len = 1;
   break;
  case 'c':
   *dst = (args += ((sizeof(char) + (sizeof(int) - 1)) & ~(sizeof(int) - 1)), *(char *)(args - ((sizeof(char) + (sizeof(int) - 1)) & ~(sizeof(int) - 1))));
   len = 1;
   break;
  case 'd':
   d = (args += ((sizeof(int) + (sizeof(int) - 1)) & ~(sizeof(int) - 1)), *(int *)(args - ((sizeof(int) + (sizeof(int) - 1)) & ~(sizeof(int) - 1))));
   len = itostr(d, dst, 10);
   break;
  case 'u':
   u = (args += ((sizeof(unsigned int) + (sizeof(int) - 1)) & ~(sizeof(int) - 1)), *(unsigned int *)(args - ((sizeof(unsigned int) + (sizeof(int) - 1)) & ~(sizeof(int) - 1))));
   len = utostr(u, dst, 10);
   break;
  case 'x':
   u = (args += ((sizeof(unsigned int) + (sizeof(int) - 1)) & ~(sizeof(int) - 1)), *(unsigned int *)(args - ((sizeof(unsigned int) + (sizeof(int) - 1)) & ~(sizeof(int) - 1))));
   len = utostr(u, dst, 16);
   break;
  case 'p':
   p = (args += ((sizeof(void *) + (sizeof(int) - 1)) & ~(sizeof(int) - 1)), *(void * *)(args - ((sizeof(void *) + (sizeof(int) - 1)) & ~(sizeof(int) - 1))));
   *dst++ = '0';
   *dst++ = 'x';
   len = utostr((unsigned int)p, dst, 16);
   break;
  case 's':
   s = (args += ((sizeof(char *) + (sizeof(int) - 1)) & ~(sizeof(int) - 1)), *(char * *)(args - ((sizeof(char *) + (sizeof(int) - 1)) & ~(sizeof(int) - 1))));
   if (swidth > 0)
    len = strnncpy(dst, s, swidth);
   else
    len = strcpy(dst, s);
   break;
  }
  if (width > len) {
   if (left) {
    memset(dst + len, ' ', width - len);
   } else {
    memmove(dst + width - len, dst, len);
    memset(dst, zero ? '0' : ' ', width - len);
   }
   len = width;
  }
  fmt++;
  dst += len;
 }
 *dst = '\0';
 return (dst - buf);
}

canonical_original_code:
int vsprintf(char *buf, char *fmt, char *args)
{
 char *dst = buf;
 unsigned int u;
 int d;
 char *s;
 void *p;
 int zero;
 int width;
 int left;
 int len;
 int swidth;
 while (*fmt) {
  if (*fmt != '%') {
   *dst++ = *fmt++;
   continue;
  }
  fmt++;
  zero = 0;
  width = 0;
  swidth = 0;
  left = 0;
  while (*fmt == '+' || *fmt == '-') {
   left = (*fmt == '-');
   fmt++;
  }
  while (*fmt == '0') {
   zero = 1;
   fmt++;
  }
  if (*fmt >= '1' && *fmt <= '9')
   width = strtoi(fmt, &fmt, 10);
  if (*fmt == '*') {
   swidth = (args += ((sizeof(int) + (sizeof(int) - 1)) & ~(sizeof(int) - 1)), *(int *)(args - ((sizeof(int) + (sizeof(int) - 1)) & ~(sizeof(int) - 1))));
   width = 0;
   fmt++;
  }
  switch (*fmt) {
  case '%':
   *dst = '%';
   len = 1;
   break;
  case 'c':
   *dst = (args += ((sizeof(char) + (sizeof(int) - 1)) & ~(sizeof(int) - 1)), *(char *)(args - ((sizeof(char) + (sizeof(int) - 1)) & ~(sizeof(int) - 1))));
   len = 1;
   break;
  case 'd':
   d = (args += ((sizeof(int) + (sizeof(int) - 1)) & ~(sizeof(int) - 1)), *(int *)(args - ((sizeof(int) + (sizeof(int) - 1)) & ~(sizeof(int) - 1))));
   len = itostr(d, dst, 10);
   break;
  case 'u':
   u = (args += ((sizeof(unsigned int) + (sizeof(int) - 1)) & ~(sizeof(int) - 1)), *(unsigned int *)(args - ((sizeof(unsigned int) + (sizeof(int) - 1)) & ~(sizeof(int) - 1))));
   len = utostr(u, dst, 10);
   break;
  case 'x':
   u = (args += ((sizeof(unsigned int) + (sizeof(int) - 1)) & ~(sizeof(int) - 1)), *(unsigned int *)(args - ((sizeof(unsigned int) + (sizeof(int) - 1)) & ~(sizeof(int) - 1))));
   len = utostr(u, dst, 16);
   break;
  case 'p':
   p = (args += ((sizeof(void *) + (sizeof(int) - 1)) & ~(sizeof(int) - 1)), *(void **)(args - ((sizeof(void *) + (sizeof(int) - 1)) & ~(sizeof(int) - 1))));
   *dst++ = '0';
   *dst++ = 'x';
   len = utostr((unsigned int)p, dst, 16);
   break;
  case 's':
   s = (args += ((sizeof(char *) + (sizeof(int) - 1)) & ~(sizeof(int) - 1)), *(char **)(args - ((sizeof(char *) + (sizeof(int) - 1)) & ~(sizeof(int) - 1))));
   if (swidth > 0)
    len = strnncpy(dst, s, swidth);
   else
    len = strcpy(dst, s);
   break;
  }
  if (width > len) {
   if (left) {
    memset(dst + len, ' ', width - len);
   } else {
    memmove(dst + width - len, dst, len);
    memset(dst, zero ? '0' : ' ', width - len);
   }
   len = width;
  }
  fmt++;
  dst += len;
 }
 *dst = '\0';
 return (dst - buf);
}

variable_types:
buf
char *

variable_types:
fmt
char *

variable_types:
args
char *

variable_types:
dst
char *

variable_types:
u
unsigned int

variable_types: d int

variable_types:
s
char *

variable_types:
p
void *

variable_types: zero int

variable_types: width int

variable_types: left int

variable_types: len int

variable_types: swidth int


printf

decompiled_code:
__int64 __fastcall printf(_BYTE *a1, __int64 a2, __int64 a3, __int64 a4)
{
unsigned int v5; // [rsp+10h] [rbp-8h] BYREF

vsprintf(&ustrbuf, a1, &v5, a4);
return usys_puts(&ustrbuf);
}

canonical_decompiled_code:
__int64 __fastcall func1(_BYTE *a1, __int64 a2, __int64 a3, __int64 a4)
{
unsigned int v5; // [rsp+10h] [rbp-8h] BYREF

func0(&ustrbuf, a1, &v5, a4);
return usys_puts(&ustrbuf);
}

original_code:
int printf(char *format, ...)
{
 va_list args;
 int i;
 (args = (va_list)&(format) + ((sizeof(format) + (sizeof(int) - 1)) & ~(sizeof(int) - 1)));
 i = vsprintf(ustrbuf, format, args);
 ;
 return usys_puts(ustrbuf);
}

canonical_original_code:
int printf(char *format, ...)
{
 char *args;
 int i;
 (args = (char *)&(format) + ((sizeof(format) + (sizeof(int) - 1)) & ~(sizeof(int) - 1)));
 i = vsprintf(ustrbuf, format, args);
 ;
 return usys_puts(ustrbuf);
}

variable_types:
format
char *

variable_types: i int


gets

decompiled_code:
__int64 gets()
{
return usys_gets();
}

canonical_decompiled_code:
__int64 func2()
{
return usys_gets();
}

original_code:
int gets(char *buf, int size)
{
 return usys_gets(buf, size);
}

variable_types: size int


fork

decompiled_code:
__int64 fork()
{
return usys_fork();
}

canonical_decompiled_code:
__int64 func0()
{
return usys_fork();
}

original_code:
int fork(void)
{
 return usys_fork();
}

canonical_original_code:
int fork()
{
 return usys_fork();
}


yield

decompiled_code:
__int64 yield()
{
return usys_yield();
}

canonical_decompiled_code:
__int64 func1()
{
return usys_yield();
}

original_code:
void yield(void)
{
 return usys_yield();
}

canonical_original_code:
void yield()
{
 return usys_yield();
}


getpid

decompiled_code:
__int64 getpid()
{
return usys_getpid();
}

canonical_decompiled_code:
__int64 func2()
{
return usys_getpid();
}

original_code:
int getpid(void)
{
 return usys_getpid();
}

canonical_original_code:
int getpid()
{
 return usys_getpid();
}


exit

decompiled_code:
void __noreturn exit()
{
usys_exit();
wait();
}

canonical_decompiled_code:
void __noreturn func3()
{
usys_exit();
func4();
}

original_code:
void exit(int status)
{
 usys_exit(status);
}

variable_types: status int


wait

decompiled_code:
__int64 wait()
{
return usys_wait();
}

canonical_decompiled_code:
__int64 func4()
{
return usys_wait();
}

original_code:
int wait(int *status)
{
 return usys_wait(status);
}

variable_types:
status
int *


execute

decompiled_code:
__int64 execute()
{
return usys_execute();
}

canonical_decompiled_code:
__int64 func5()
{
return usys_execute();
}

original_code:
int execute(char *path, int argc, char **argv)
{
 return usys_execute(path, argc, argv);
}

variable_types:
path
char *

variable_types: argc int

variable_types:
argv
char **


decompiled_code:
__int64 __fastcall debug_inode(unsigned __int16 *a1)
{
unsigned __int16 *v1; // rbx

printk(
"mode: %8x uid: %8x size: %d\ntime: %8x gid: %8x link: %8x\n",
*a1,
a1[1],
*(_QWORD *)(a1 + 2),
*(_QWORD *)(a1 + 6),
*((unsigned __int8 *)a1 + 20),
*((unsigned __int8 *)a1 + 21));
printk("zones: ");
v1 = a1 + 11;
do
printk("%4d ", *v1++);
while ( v1 != a1 + 20 );
return printk("\n");
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int16 *a1)
{
unsigned __int16 *v1; // rbx

printk(
"mode: %8x uid: %8x size: %d\ntime: %8x gid: %8x link: %8x\n",
*a1,
a1[1],
*(_QWORD *)(a1 + 2),
*(_QWORD *)(a1 + 6),
*((unsigned __int8 *)a1 + 20),
*((unsigned __int8 *)a1 + 21));
printk("zones: ");
v1 = a1 + 11;
do
printk("%4d ", *v1++);
while ( v1 != a1 + 20 );
return printk("\n");
}

original_code:
static void debug_inode(struct minix_d_inode *mdi)
{
 int i;
 printk("mode: %8x uid: %8x size: %d\n"
  "time: %8x gid: %8x link: %8x\n",
  mdi->i_mode, mdi->i_uid, mdi->i_size,
  mdi->i_time, mdi->i_gid, mdi->i_nlinks);
 printk("zones: ");
 for (i = 0; i < 9; i++)
  printk("%4d ", mdi->i_zone[i]);
 printk("\n");
}

variable_types: struct minix_d_inode { unsigned short i_mode; unsigned short i_uid; unsigned long i_size; unsigned long i_time; unsigned char i_gid; unsigned char i_nlinks; unsigned short i_zone[9]; } *


exit

decompiled_code:
void __noreturn exit(int status)
{
exit(status);
}

canonical_decompiled_code:
void __noreturn func0(int status)
{
func0(status);
}

canonical_original_code:
void exit(int status)
{
 usys_exit(status);
}


client

repo: dacav/Tmp-Experiments

decompiled_code:
int client()
{
int result; // eax
char v1[268]; // [rsp+0h] [rbp-140h] BYREF
struct sockaddr addr; // [rsp+110h] [rbp-30h] BYREF
int fd; // [rsp+120h] [rbp-20h]

sock_data_init((__int64)&addr, "127.0.0.1", 9000);
if ( (unsigned int)tcp_connect(&addr) )
{
__xpg_strerror_r();
result = fprintf(_bss_start, "Connecting to server: %s\n", v1);
}
else
{
write(fd, "hello world", 0xBuLL);
result = close(fd);
}
return result;
}

canonical_decompiled_code:
int func9()
{
int result; // eax
char v1[268]; // [rsp+0h] [rbp-140h] BYREF
struct sockaddr addr; // [rsp+110h] [rbp-30h] BYREF
int fd; // [rsp+120h] [rbp-20h]

func8((__int64)&addr, "127.0.0.1", 9000);
if ( (unsigned int)func12(&addr) )
{
__xpg_strerror_r();
result = fprintf(_bss_start, "Connecting to server: %s\n", v1);
}
else
{
write(fd, "hello world", 0xBuLL);
result = close(fd);
}
return result;
}

original_code:
static void client ()
{
    sock_data_t self;
    char buffer[BUFLEN];
    int e;
    sock_data_init(&self, "127.0.0.1", 9000);
    if (tcp_connect(&self, &e)) {
        strerror_r(e, buffer, BUFLEN);
        fprintf(stderr, "Connecting to server: %s\n", buffer);
        return;
    }
    write(self.fd, "hello world", strlen("hello world"));
    close(self.fd);
}

canonical_original_code:
static void client()
{
    struct sock_data_t self;
    char buffer[-1];
    int e;
    sock_data_init(&self, "127.0.0.1", 9000);
    if (tcp_connect(&self, &e)) {
        strerror_r(e, buffer, BUFLEN);
        fprintf(stderr, "Connecting to server: %s\n", buffer);
        return;
    }
    write(self.fd, "hello world", strlen("hello world"));
    close(self.fd);
}

variable_types: struct sock_data_t { 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]; } addr; int fd; }

variable_types:
buffer
char [-1]

variable_types: e int

user_defined_types: struct sock_data_t { struct sockaddr_in addr; int fd; }

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


decompiled_code:
const char *__fastcall sock_data_ipstring(__int64 a1, char *a2)
{
return inet_ntop(2, (const void *)(a1 + 4), a2, 0x10u);
}

canonical_decompiled_code:
const char *__fastcall func10(__int64 a1, char *a2)
{
return inet_ntop(2, (const void *)(a1 + 4), a2, 0x10u);
}

original_code:
static inline
const char * sock_data_ipstring (const sock_data_t *s, char * strrep)
{
    return inet_ntop(2, (const void *) &s->addr.sin_addr, strrep,
                     16);
}

canonical_original_code:
static inline char *sock_data_ipstring(struct sock_data_t *s, char *strrep)
{
    return inet_ntop(2, (void *) &s->addr.sin_addr, strrep,
                     16);
}

variable_types: struct sock_data_t { 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]; } addr; int fd; } *

variable_types:
strrep
char *

return_type: char *


decompiled_code:
__int64 __fastcall tcp_connect(struct sockaddr *addr, int *a2)
{
unsigned int v2; // ebp
int v4; // eax

v2 = 0;
if ( *(_DWORD *)&addr[1].sa_family == -1 )
{
v4 = socket(2, 1, 0);
v2 = v4;
if ( v4 == -1 )
{
if ( a2 )
*a2 = *__errno_location();
}
else if ( connect(v4, addr, 0x10u) == -1 )
{
if ( a2 )
*a2 = *__errno_location();
close(v2);
v2 = -2;
}
else
{
*(_DWORD *)&addr[1].sa_family = v2;
v2 = 0;
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func12(struct sockaddr *addr, int *a2)
{
unsigned int v2; // ebp
int v4; // eax

v2 = 0;
if ( *(_DWORD *)&addr[1].sa_family == -1 )
{
v4 = socket(2, 1, 0);
v2 = v4;
if ( v4 == -1 )
{
if ( a2 )
*a2 = *__errno_location();
}
else if ( connect(v4, addr, 0x10u) == -1 )
{
if ( a2 )
*a2 = *__errno_location();
close(v2);
v2 = -2;
}
else
{
*(_DWORD *)&addr[1].sa_family = v2;
v2 = 0;
}
}
return v2;
}

original_code:
int tcp_connect (sock_data_t *sd, int *e)
{
    int fd;
    if (sd->fd != -1) {
        return 0;
    }
    fd = socket(2, SOCK_STREAM, 0);
    if (fd == -1) {
        if (e) *e = (*__errno_location ());
        return -1;
    }
    if (connect(fd, (struct sockaddr *) &sd->addr,
            sizeof(struct sockaddr_in)) == -1) {
        if (e) *e = (*__errno_location ());
        close(fd);
        return -2;
    }
    sd->fd = fd;
    return 0;
}

canonical_original_code:
int tcp_connect(struct sock_data_t *sd, int *e)
{
    int fd;
    if (sd->fd != -1) {
        return 0;
    }
    fd = socket(2, SOCK_STREAM, 0);
    if (fd == -1) {
        if (e) *e = (*__errno_location ());
        return -1;
    }
    if (connect(fd, (struct sockaddr *) &sd->addr,
            sizeof(struct sockaddr_in)) == -1) {
        if (e) *e = (*__errno_location ());
        close(fd);
        return -2;
    }
    sd->fd = fd;
    return 0;
}

variable_types:
e
int *

variable_types: fd int

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


decompiled_code:
__int64 __fastcall tcp_serve(struct sockaddr *addr, int n, int *a3)
{
int v4; // eax
int v5; // ebx
unsigned int v6; // ebp

v4 = socket(2, 1, 0);
v5 = v4;
if ( v4 == -1 )
{
v6 = -1;
if ( a3 )
*a3 = *__errno_location();
}
else
{
v6 = bind(v4, addr, 0x10u);
if ( v6 )
{
if ( a3 )
*a3 = *__errno_location();
close(v5);
v6 = -2;
}
else if ( listen(v5, n) == -1 )
{
if ( a3 )
*a3 = *__errno_location();
close(v5);
v6 = -3;
}
else
{
*(_DWORD *)&addr[1].sa_family = v5;
}
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func13(struct sockaddr *addr, int n, int *a3)
{
int v4; // eax
int v5; // ebx
unsigned int v6; // ebp

v4 = socket(2, 1, 0);
v5 = v4;
if ( v4 == -1 )
{
v6 = -1;
if ( a3 )
*a3 = *__errno_location();
}
else
{
v6 = bind(v4, addr, 0x10u);
if ( v6 )
{
if ( a3 )
*a3 = *__errno_location();
close(v5);
v6 = -2;
}
else if ( listen(v5, n) == -1 )
{
if ( a3 )
*a3 = *__errno_location();
close(v5);
v6 = -3;
}
else
{
*(_DWORD *)&addr[1].sa_family = v5;
}
}
return v6;
}

original_code:
int tcp_serve (sock_data_t *sd, int backlog, int *e)
{
    int fd;
    fd = socket(2, SOCK_STREAM, 0);
    if (fd == -1) {
        if (e) *e = (*__errno_location ());
        return -1;
    }
    if (bind(fd, (struct sockaddr *) &sd->addr,
            sizeof(struct sockaddr_in))) {
        if (e) *e = (*__errno_location ());
        close(fd);
        return -2;
    }
    if (listen(fd, backlog) == -1) {
        if (e) *e = (*__errno_location ());
        close(fd);
        return -3;
    }
    sd->fd = fd;
    return 0;
}

canonical_original_code:
int tcp_serve(struct sock_data_t *sd, int backlog, int *e)
{
    int fd;
    fd = socket(2, SOCK_STREAM, 0);
    if (fd == -1) {
        if (e) *e = (*__errno_location ());
        return -1;
    }
    if (bind(fd, (struct sockaddr *) &sd->addr,
            sizeof(struct sockaddr_in))) {
        if (e) *e = (*__errno_location ());
        close(fd);
        return -2;
    }
    if (listen(fd, backlog) == -1) {
        if (e) *e = (*__errno_location ());
        close(fd);
        return -3;
    }
    sd->fd = fd;
    return 0;
}

variable_types: backlog int


comp

repo: david-young/Port-Forwarder

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

result = 1LL;
if ( *a1 <= *a2 )
result = (unsigned int)-(*a1 != *a2);
return result;
}

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

result = 1LL;
if ( *a1 <= *a2 )
result = (unsigned int)-(*a1 != *a2);
return result;
}

original_code:
int comp(void *p1, void *p2) {
 Connection *c1, *c2;
 c1 = (Connection *)p1;
 c2 = (Connection *)p2;
 if (c1->src > c2->src) {
  return 1;
 } else if (c1->src == c2->src) {
  return 0;
 } else {
  return -1;
 }
}

canonical_original_code:
int comp(void *p1, void *p2) {
 struct Connection *c1; struct Connection *c2;
 c1 = (struct Connection *)p1;
 c2 = (struct Connection *)p2;
 if (c1->src > c2->src) {
  return 1;
 } else if (c1->src == c2->src) {
  return 0;
 } else {
  return -1;
 }
}

variable_types: struct Connection { int src; int dst; } *


decompiled_code:
void __noreturn close_fd()
{
int v0; // eax

i = 0;
if ( servers > 0 )
{
v0 = 0;
do
{
close(fd_servers[v0]);
v0 = i + 1;
i = v0;
}
while ( v0 < servers );
}
exit(0);
}

canonical_decompiled_code:
void __noreturn func1()
{
int v0; // eax

i = 0;
if ( servers > 0 )
{
v0 = 0;
do
{
close(fd_servers[v0]);
v0 = i + 1;
i = v0;
}
while ( v0 < servers );
}
exit(0);
}

original_code:
void close_fd (int signo) {
 for (i = 0; i < servers; i++)
  close(fd_servers[i]);
 exit (0);
}

canonical_original_code:
void close_fd(int signo) {
 for (i = 0; i < servers; i++)
  close(fd_servers[i]);
 exit (0);
}

variable_types: signo int


looprecord

decompiled_code:
void __fastcall __noreturn looprecord(void *a1)
{
int v1; // ebp
int v2; // ebx
struct tm *v3; // rax
time_t timer; // [rsp+8h] [rbp-50h] BYREF
struct timeval tv; // [rsp+10h] [rbp-48h] BYREF
char s[56]; // [rsp+20h] [rbp-38h] BYREF

v1 = 0;
v2 = 0;
while ( 1 )
{
v1 += v2;
v2 = svr_close - v1;
gettimeofday(&tv, 0LL);
timer = tv.tv_sec;
v3 = localtime(&timer);
strftime(s, 0x1EuLL, "%m-%d-%Y %H:%M:%S.", v3);
printf(
"%s%ld \tTCP Errors: %i \tCompleted: %i \tActive: %i \tCompleted/Update(%is): %i\n",
s,
tv.tv_usec,
(unsigned int)svr_error,
(unsigned int)(svr_close / 2),
(unsigned int)((svr_accept - svr_close) / 2),
10LL,
(unsigned int)(v2 / 2));
sleep(0xAu);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func3(void *a1)
{
int v1; // ebp
int v2; // ebx
struct tm *v3; // rax
time_t timer; // [rsp+8h] [rbp-50h] BYREF
struct timeval tv; // [rsp+10h] [rbp-48h] BYREF
char s[56]; // [rsp+20h] [rbp-38h] BYREF

v1 = 0;
v2 = 0;
while ( 1 )
{
v1 += v2;
v2 = svr_close - v1;
gettimeofday(&tv, 0LL);
timer = tv.tv_sec;
v3 = localtime(&timer);
strftime(s, 0x1EuLL, "%m-%d-%Y %H:%M:%S.", v3);
printf(
"%s%ld \tTCP Errors: %i \tCompleted: %i \tActive: %i \tCompleted/Update(%is): %i\n",
s,
tv.tv_usec,
(unsigned int)svr_error,
(unsigned int)(svr_close / 2),
(unsigned int)((svr_accept - svr_close) / 2),
10LL,
(unsigned int)(v2 / 2));
sleep(0xAu);
}
}

original_code:
void* looprecord() {
 int svr_complete=0, j=0;
 char time[30];
 struct timeval tv;
 time_t curtime;
 while(1) {
  j += svr_complete;
  svr_complete = svr_close - j;
  gettimeofday(&tv, ((void *)0));
  curtime = tv.tv_sec;
  strftime(time,30,"%m-%d-%Y  %H:%M:%S.",localtime(&curtime));
  printf("%s%ld \tTCP Errors: %i \tCompleted: %i \tActive: %i \tCompleted/Update(%is): %i\n", time,tv.tv_usec, svr_error, svr_close/2, (svr_accept-svr_close)/2, 10, svr_complete/2);
  sleep(10);
 }
 return 0;
}

canonical_original_code:
void *looprecord() {
 int svr_complete = 0; int j = 0;
 char time[30];
 struct timeval tv;
 long curtime;
 while(1) {
  j += svr_complete;
  svr_complete = svr_close - j;
  gettimeofday(&tv, ((void *)0));
  curtime = tv.tv_sec;
  strftime(time,30,"%m-%d-%Y  %H:%M:%S.",localtime(&curtime));
  printf("%s%ld \tTCP Errors: %i \tCompleted: %i \tActive: %i \tCompleted/Update(%is): %i\n", time,tv.tv_usec, svr_error, svr_close/2, (svr_accept-svr_close)/2, 10, svr_complete/2);
  sleep(10);
 }
 return 0;
}

variable_types: j int

variable_types:
time
char [30]

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

variable_types: curtime long

return_type: void *


forwardsocket

decompiled_code:
__int64 __fastcall forwardsocket(int fd, __int64 a2)
{
__int64 v2; // rax
int v3; // eax
int v4; // ebp
int v5; // ebx
int v6; // eax
__int64 result; // rax
int *v8; // rdx
int fda[2]; // [rsp+8h] [rbp-2830h] BYREF
char buf[10280]; // [rsp+10h] [rbp-2828h] BYREF

fda[0] = fd;
v2 = find_node(fda);
if ( !v2 )
{
fprintf(stderr, "Couldn't find node with src fd %d line %d\n", (unsigned int)fda[0], 244LL);
return 1LL;
}
*(_QWORD *)fda = 0LL;
*(_QWORD *)fda = **(_QWORD **)(v2 + 24);
while ( 1 )
{
v3 = recv(fd, buf, 0x2800uLL, 0);
v4 = v3;
if ( v3 <= 0 )
break;
v5 = 0;
do
{
v6 = send(fda[1], &buf[v5], v4 - v5, 0);
if ( !v6 )
break;
if ( v6 != -1 )
v5 += v6;
}
while ( v5 != v4 );
}
if ( !v3 )
goto LABEL_15;
result = 0LL;
if ( v4 == -1 )
{
v8 = _errno_location();
result = 0LL;
if ( *v8 != 11 )
{
++svr_error;
perror("recv failed");
LABEL_15:
remove_connection_from_tree((unsigned int)fda[0], (unsigned int)fda[1], a2);
return 1LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(int fd, __int64 a2)
{
__int64 v2; // rax
int v3; // eax
int v4; // ebp
int v5; // ebx
int v6; // eax
__int64 result; // rax
int *v8; // rdx
int fda[2]; // [rsp+8h] [rbp-2830h] BYREF
char buf[10280]; // [rsp+10h] [rbp-2828h] BYREF

fda[0] = fd;
v2 = find_node(fda);
if ( !v2 )
{
fprintf(stderr, "Couldn't find node with src fd %d line %d\n", (unsigned int)fda[0], 244LL);
return 1LL;
}
*(_QWORD *)fda = 0LL;
*(_QWORD *)fda = **(_QWORD **)(v2 + 24);
while ( 1 )
{
v3 = recv(fd, buf, 0x2800uLL, 0);
v4 = v3;
if ( v3 <= 0 )
break;
v5 = 0;
do
{
v6 = send(fda[1], &buf[v5], v4 - v5, 0);
if ( !v6 )
break;
if ( v6 != -1 )
v5 += v6;
}
while ( v5 != v4 );
}
if ( !v3 )
goto LABEL_15;
result = 0LL;
if ( v4 == -1 )
{
v8 = _errno_location();
result = 0LL;
if ( *v8 != 11 )
{
++svr_error;
perror("recv failed");
LABEL_15:
remove_connection_from_tree((unsigned int)fda[0], (unsigned int)fda[1], a2);
return 1LL;
}
}
return result;
}

original_code:
int forwardsocket (int fd, BTree *tree) {
 int n = 0;
 char buf[10240];
 int n_sent, n_totalsent;
 Node *node;
 Connection conn;
 conn.src = fd;
 if ((node = find_node(&conn, tree)) == ((void *)0)) {
  fprintf(stderr, "Couldn't find node with src fd %d line %d\n", conn.src, 244);
  return 1;
 }
 bzero(&conn, sizeof(Connection));
 memcpy(&conn, node->data, sizeof(Connection));
 while ((n = recv (fd, buf, 10240, 0)) > 0) {
  n_sent = 0, n_totalsent = 0;
  while (n_totalsent != n && (n_sent = send (conn.dst, buf+n_totalsent, n-n_totalsent, 0))) {
   if (n_sent == -1) {
    continue;
   }
   n_totalsent += n_sent;
  }
  if (n_sent == -1) {
  }
 }
 if (n == 0 || (n == -1 && (*__errno_location ()) != 11 && (*__errno_location ()) != 11)) {
  if (n == -1) {
   ++svr_error;
   perror("recv failed");
  }
  if (remove_connection_from_tree(conn.src, conn.dst, tree) == 1) {
  }
  return 1;
 } else if (n == -1 && ((*__errno_location ()) == 11 || (*__errno_location ()) == 11)) {
  return 0;
 }
 return 0;
}

canonical_original_code:
int forwardsocket(int fd, struct BTree *tree) {
 int n = 0;
 char buf[10240];
 int n_sent; int n_totalsent;
 struct node *node;
 struct Connection conn;
 conn.src = fd;
 if ((node = find_node(&conn, tree)) == ((void *)0)) {
  fprintf(stderr, "Couldn't find node with src fd %d line %d\n", conn.src, 244);
  return 1;
 }
 bzero(&conn, sizeof(Connection));
 memcpy(&conn, node->data, sizeof(Connection));
 while ((n = recv (fd, buf, 10240, 0)) > 0) {
  n_sent = 0, n_totalsent = 0;
  while (n_totalsent != n && (n_sent = send (conn.dst, buf+n_totalsent, n-n_totalsent, 0))) {
   if (n_sent == -1) {
    continue;
   }
   n_totalsent += n_sent;
  }
  if (n_sent == -1) {
  }
 }
 if (n == 0 || (n == -1 && (*__errno_location ()) != 11 && (*__errno_location ()) != 11)) {
  if (n == -1) {
   ++svr_error;
   perror("recv failed");
  }
  if (remove_connection_from_tree(conn.src, conn.dst, tree) == 1) {
  }
  return 1;
 } else if (n == -1 && ((*__errno_location ()) == 11 || (*__errno_location ()) == 11)) {
  return 0;
 }
 return 0;
}

variable_types: struct BTree { struct node { struct node *parent; struct node *left; struct node *right; void *data; } *head; int (*__compare_func)(void *item1, void *item2); unsigned int n_children; } *

variable_types: n int

variable_types:
buf
char [10240]

variable_types:
conn
struct Connection { int src; int dst; }

user_defined_types: struct BTree { struct node *head; int (*__compare_func)(void *item1, void *item2); unsigned int n_children; }


SystemFatal

decompiled_code:
void __fastcall __noreturn SystemFatal(const char *a1)
{
perror(a1);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func6(const char *a1)
{
perror(a1);
exit(1);
}

original_code:
void SystemFatal(const char* message) {
    perror (message);
    exit (1);
}

canonical_original_code:
void SystemFatal(char *message) {
    perror (message);
    exit (1);
}

variable_types:
message
char *


storeipport

decompiled_code:
__int64 __fastcall storeipport(__int64 a1)
{
__int16 v1; // di
__int64 result; // rax
__int64 v3; // rbx
__int64 v4; // rbx
int v5; // edx
char s[15]; // [rsp+15h] [rbp-3Bh] BYREF
unsigned int v7; // [rsp+24h] [rbp-2Ch] BYREF
unsigned int v8; // [rsp+28h] [rbp-28h] BYREF
unsigned int v9; // [rsp+2Ch] [rbp-24h] BYREF
unsigned int v10; // [rsp+30h] [rbp-20h] BYREF
unsigned int v11; // [rsp+34h] [rbp-1Ch] BYREF

_isoc99_sscanf(a1, "%u.%u.%u.%u:%u", &v11, &v10, &v9, &v8, &v7);
v1 = v7;
result = 1LL;
if ( v7 <= 0xFFFF )
{
v3 = 2LL * servers;
forwardingrules[v3] = 0LL;
forwardingrules[v3 + 1] = 0LL;
LOWORD(forwardingrules[v3]) = 2;
WORD1(forwardingrules[v3]) = _bswap_16(v1);
HIDWORD(forwardingrules[v3]) = _bswap_32(0);
forwardingrules_server[v3] = 0LL;
forwardingrules_server[v3 + 1] = 0LL;
sprintf(s, "%u.%u.%u.%u", v11, v10, v9, v8);
v4 = 2LL * servers;
LOWORD(forwardingrules_server[v4]) = 2;
WORD1(forwardingrules_server[v4]) = _bswap_16(v7);
v5 = inet_pton(2, s, (char *)&forwardingrules_server[v4] + 4);
result = 1LL;
if ( v5 == 1 )
{
++servers;
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
__int16 v1; // di
__int64 result; // rax
__int64 v3; // rbx
__int64 v4; // rbx
int v5; // edx
char s[15]; // [rsp+15h] [rbp-3Bh] BYREF
unsigned int v7; // [rsp+24h] [rbp-2Ch] BYREF
unsigned int v8; // [rsp+28h] [rbp-28h] BYREF
unsigned int v9; // [rsp+2Ch] [rbp-24h] BYREF
unsigned int v10; // [rsp+30h] [rbp-20h] BYREF
unsigned int v11; // [rsp+34h] [rbp-1Ch] BYREF

_isoc99_sscanf(a1, "%u.%u.%u.%u:%u", &v11, &v10, &v9, &v8, &v7);
v1 = v7;
result = 1LL;
if ( v7 <= 0xFFFF )
{
v3 = 2LL * servers;
forwardingrules[v3] = 0LL;
forwardingrules[v3 + 1] = 0LL;
LOWORD(forwardingrules[v3]) = 2;
WORD1(forwardingrules[v3]) = func2(v1);
HIDWORD(forwardingrules[v3]) = func4(0);
forwardingrules_server[v3] = 0LL;
forwardingrules_server[v3 + 1] = 0LL;
sprintf(s, "%u.%u.%u.%u", v11, v10, v9, v8);
v4 = 2LL * servers;
LOWORD(forwardingrules_server[v4]) = 2;
WORD1(forwardingrules_server[v4]) = func2(v7);
v5 = inet_pton(2, s, (char *)&forwardingrules_server[v4] + 4);
result = 1LL;
if ( v5 == 1 )
{
++servers;
result = 0LL;
}
}
return result;
}

original_code:
int storeipport(char *ipport) {
 unsigned ip1, ip2, ip3, ip4, port;
 char fullip[15];
 sscanf(ipport, "%u.%u.%u.%u:%u", &ip1, &ip2, &ip3, &ip4, &port);
 if (port > 65535 || port < 0) {
  return 1;
 }
 bzero(&forwardingrules[servers], sizeof(struct sockaddr_in));
 forwardingrules[servers].sin_family = 2;
 forwardingrules[servers].sin_port = htons(port);
 forwardingrules[servers].sin_addr.s_addr = htonl(((in_addr_t) 0x00000000));
 bzero(&forwardingrules_server[servers], sizeof(struct sockaddr_in));
 sprintf(fullip, "%u.%u.%u.%u", ip1, ip2, ip3, ip4);
 forwardingrules_server[servers].sin_family = 2;
 forwardingrules_server[servers].sin_port = htons(port);
 if (inet_pton(2, fullip, &forwardingrules_server[servers].sin_addr) != 1)
  return 1;
 servers++;
 return 0;
}

canonical_original_code:
int storeipport(char *ipport) {
 unsigned int ip1; unsigned int ip2; unsigned int ip3; unsigned int ip4; unsigned int port;
 char fullip[15];
 sscanf(ipport, "%u.%u.%u.%u:%u", &ip1, &ip2, &ip3, &ip4, &port);
 if (port > 65535 || port < 0) {
  return 1;
 }
 bzero(&forwardingrules[servers], sizeof(struct sockaddr_in));
 forwardingrules[servers].sin_family = 2;
 forwardingrules[servers].sin_port = htons(port);
 forwardingrules[servers].sin_addr.s_addr = htonl(((unsigned int) 0x00000000));
 bzero(&forwardingrules_server[servers], sizeof(struct sockaddr_in));
 sprintf(fullip, "%u.%u.%u.%u", ip1, ip2, ip3, ip4);
 forwardingrules_server[servers].sin_family = 2;
 forwardingrules_server[servers].sin_port = htons(port);
 if (inet_pton(2, fullip, &forwardingrules_server[servers].sin_addr) != 1)
  return 1;
 servers++;
 return 0;
}

variable_types:
ipport
char *

variable_types:
port
unsigned int

variable_types:
fullip
char [15]


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rdi
int v4; // er15
int v5; // eax
int v6; // edi
unsigned int v7; // eax
int v8; // eax
int v9; // er14
int v10; // eax
__int64 v11; // rbx
unsigned __int16 v12; // bp
const char *v13; // rax
int v14; // edx
int v15; // edx
__int64 v16; // rax
char v17; // cl
int v18; // edi
int v19; // eax
unsigned int v20; // ebp
unsigned int v21; // eax
int v22; // eax
unsigned int v23; // er12
unsigned int v24; // eax
__int64 v26; // [rsp+0h] [rbp-128h]
int epfd; // [rsp+Ch] [rbp-11Ch]
char buf[24]; // [rsp+10h] [rbp-118h] BYREF
pthread_t newthread; // [rsp+28h] [rbp-100h] BYREF
struct sigaction v30; // [rsp+30h] [rbp-F8h] BYREF
socklen_t addr_len; // [rsp+CCh] [rbp-5Ch] BYREF
struct sockaddr addr; // [rsp+D0h] [rbp-58h] BYREF
int optval[15]; // [rsp+ECh] [rbp-3Ch] BYREF

addr_len = 16;
v26 = create_tree_with_cmp_func(comp, argv, envp);
if ( v26 )
{
if ( argc == 1 )
{
v3 = "servers.config";
}
else
{
if ( argc != 2 )
{
fprintf(stderr, "Usage: %s [configfile \"default name is %s\"]\n", *argv, "servers.config");
exit(1);
}
v3 = argv[1];
}
v4 = readconfigfile(v3);
if ( v4 )
SystemFatal("failed to read configfile");
if ( servers <= 0 )
SystemFatal("no forwarding rules");
v30.sa_handler = (__sighandler_t)close_fd;
v30.sa_flags = 0;
if ( sigemptyset(&v30.sa_mask) == -1 || sigaction(2, &v30, 0LL) == -1 )
SystemFatal("Failed to set SIGINT handler");
epfd = epoll_create((int)&unk_3E800);
if ( epfd == -1 )
SystemFatal("epoll_create");
i = 0;
if ( servers > 0 )
{
v5 = v4;
do
{
fd_servers[v5] = 0;
fd_servers[v5] = socket(2, 1, 0);
v6 = fd_servers[i];
if ( v6 == -1 )
SystemFatal("socket");
optval[0] = 1;
if ( setsockopt(v6, 1, 2, optval, 4u) == -1 )
SystemFatal("setsockopt");
v7 = fcntl(fd_servers[i], 3, 0LL);
BYTE1(v7) |= 8u;
if ( fcntl(fd_servers[i], 4, v7) == -1 )
SystemFatal("fcntl");
if ( bind(fd_servers[i], (const struct sockaddr *)&forwardingrules[2 * i], 0x10u) == -1 )
{
v11 = 2LL * i;
v12 = _bswap_16(WORD1(forwardingrules[v11]));
v13 = inet_ntop(2, (char *)&forwardingrules[v11] + 4, buf, 0x14u);
printf("%d %s %d\n", (unsigned int)i, v13, v12);
SystemFatal("bind");
}
if ( listen(fd_servers[i], 128) == -1 )
SystemFatal("listen");
event_2.events = 27;
event_2.data.fd = fd_servers[i];
if ( epoll_ctl(epfd, 1, event_2.data.fd, &event_2) == -1 )
SystemFatal("epoll_ctl");
v5 = i + 1;
i = v5;
}
while ( v5 < servers );
}
pthread_create(&newthread, 0LL, (void *(*)(void *))looprecord, 0LL);
do
{
LABEL_22:
v8 = epoll_wait(epfd, &events_1, (int)&unk_3E800, -1);
v9 = v8;
if ( v8 < 0 )
{
++svr_error;
SystemFatal("Error in epoll_wait!");
}
i = 0;
}
while ( v8 <= 0 );
v10 = v4;
while ( 1 )
{
v14 = *(&events_1.events + 3 * v10);
if ( (v14 & 2) != 0 )
break;
if ( (v14 & 0x18) == 0 )
goto LABEL_41;
++svr_error;
perror("epoll: EPOLLERR");
close(*(&events_1.data.fd + 3 * i));
LABEL_38:
v10 = i + 1;
i = v10;
if ( v10 >= v9 )
goto LABEL_22;
}
handle_OOB(*(&events_1.data.u32 + 3 * v10));
LABEL_41:
if ( (*(_BYTE *)(&events_1.events + 3 * i) & 1) == 0 )
_assert_fail("events[i].events & EPOLLIN", "svr.c", 0x8Du, "main");
y = 0;
if ( servers <= 0 )
{
LABEL_47:
if ( (unsigned int)forwardsocket(*(&events_1.data.fd + 3 * i), v26) )
svr_close += 2;
}
else
{
v15 = v4;
v16 = 1LL;
v17 = 0;
while ( 1 )
{
v18 = fd_servers[v16 - 1];
if ( *(&events_1.data.fd + 3 * i) == v18 )
break;
v15 = v16++;
v17 = 1;
if ( v16 == servers + 1 )
{
y = v15;
goto LABEL_47;
}
}
if ( v17 )
y = v15;
*(_QWORD *)&addr.sa_family = 0LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
v19 = accept(v18, &addr, &addr_len);
v20 = v19;
if ( v19 == -1 )
{
if ( *_errno_location() != 11 )
{
++svr_error;
perror("accept");
}
}
else
{
v21 = fcntl(v19, 3, 0LL);
BYTE1(v21) |= 8u;
if ( fcntl(v20, 4, v21) == -1 )
{
++svr_error;
SystemFatal("fcntl");
}
event_2.data.fd = v20;
if ( epoll_ctl(epfd, 1, v20, &event_2) == -1 )
{
++svr_error;
SystemFatal("epoll_ctl");
}
v22 = socket(2, 1, 0);
v23 = v22;
if ( v22 == -1 )
SystemFatal("Couldn't create server socket");
if ( connect(v22, (const struct sockaddr *)&forwardingrules_server[2 * y], 0x10u) == -1 )
{
++svr_error;
perror("Can't connect to server");
close(v23);
close(*(&events_1.data.fd + 3 * i));
}
else
{
svr_accept += 2;
v24 = fcntl(v23, 3, 0LL);
BYTE1(v24) |= 8u;
if ( fcntl(v23, 4, v24) == -1 )
SystemFatal("fcntl sock");
event_2.events = -2147483623;
event_2.data.fd = v23;
if ( epoll_ctl(epfd, 1, v23, &event_2) == -1 )
SystemFatal("epoll_ctl");
add_connection_to_tree(v23, v20, v26);
}
}
}
goto LABEL_38;
}
fwrite("Couldn't create BinaryTree. Exiting...\n", 1uLL, 0x27uLL, stderr);
return 1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rdi
int v4; // er15
int v5; // eax
int v6; // edi
unsigned int v7; // eax
int v8; // eax
int v9; // er14
int v10; // eax
__int64 v11; // rbx
unsigned __int16 v12; // bp
const char *v13; // rax
int v14; // edx
int v15; // edx
__int64 v16; // rax
char v17; // cl
int v18; // edi
int v19; // eax
unsigned int v20; // ebp
unsigned int v21; // eax
int v22; // eax
unsigned int v23; // er12
unsigned int v24; // eax
__int64 v26; // [rsp+0h] [rbp-128h]
int epfd; // [rsp+Ch] [rbp-11Ch]
char buf[24]; // [rsp+10h] [rbp-118h] BYREF
pthread_t newthread; // [rsp+28h] [rbp-100h] BYREF
struct sigaction v30; // [rsp+30h] [rbp-F8h] BYREF
socklen_t addr_len; // [rsp+CCh] [rbp-5Ch] BYREF
struct sockaddr addr; // [rsp+D0h] [rbp-58h] BYREF
int optval[15]; // [rsp+ECh] [rbp-3Ch] BYREF

addr_len = 16;
v26 = create_tree_with_cmp_func(comp, argv, envp);
if ( v26 )
{
if ( argc == 1 )
{
v3 = "servers.config";
}
else
{
if ( argc != 2 )
{
fprintf(stderr, "Usage: %s [configfile \"default name is %s\"]\n", *argv, "servers.config");
exit(1);
}
v3 = argv[1];
}
v4 = func8(v3);
if ( v4 )
func6("failed to read configfile");
if ( servers <= 0 )
func6("no forwarding rules");
v30.sa_handler = (__sighandler_t)close_fd;
v30.sa_flags = 0;
if ( sigemptyset(&v30.sa_mask) == -1 || sigaction(2, &v30, 0LL) == -1 )
func6("Failed to set SIGINT handler");
epfd = epoll_create((int)&unk_3E800);
if ( epfd == -1 )
func6("epoll_create");
i = 0;
if ( servers > 0 )
{
v5 = v4;
do
{
fd_servers[v5] = 0;
fd_servers[v5] = socket(2, 1, 0);
v6 = fd_servers[i];
if ( v6 == -1 )
func6("socket");
optval[0] = 1;
if ( setsockopt(v6, 1, 2, optval, 4u) == -1 )
func6("setsockopt");
v7 = fcntl(fd_servers[i], 3, 0LL);
BYTE1(v7) |= 8u;
if ( fcntl(fd_servers[i], 4, v7) == -1 )
func6("fcntl");
if ( bind(fd_servers[i], (const struct sockaddr *)&forwardingrules[2 * i], 0x10u) == -1 )
{
v11 = 2LL * i;
v12 = func2(WORD1(forwardingrules[v11]));
v13 = inet_ntop(2, (char *)&forwardingrules[v11] + 4, buf, 0x14u);
printf("%d %s %d\n", (unsigned int)i, v13, v12);
func6("bind");
}
if ( listen(fd_servers[i], 128) == -1 )
func6("listen");
event_2.events = 27;
event_2.data.fd = fd_servers[i];
if ( epoll_ctl(epfd, 1, event_2.data.fd, &event_2) == -1 )
func6("epoll_ctl");
v5 = i + 1;
i = v5;
}
while ( v5 < servers );
}
pthread_create(&newthread, 0LL, (void *(*)(void *))looprecord, 0LL);
do
{
LABEL_22:
v8 = epoll_wait(epfd, &events_1, (int)&unk_3E800, -1);
v9 = v8;
if ( v8 < 0 )
{
++svr_error;
func6("Error in epoll_wait!");
}
i = 0;
}
while ( v8 <= 0 );
v10 = v4;
while ( 1 )
{
v14 = *(&events_1.events + 3 * v10);
if ( (v14 & 2) != 0 )
break;
if ( (v14 & 0x18) == 0 )
goto LABEL_41;
++svr_error;
perror("epoll: EPOLLERR");
close(*(&events_1.data.fd + 3 * i));
LABEL_38:
v10 = i + 1;
i = v10;
if ( v10 >= v9 )
goto LABEL_22;
}
handle_OOB(*(&events_1.data.u32 + 3 * v10));
LABEL_41:
if ( (*(_BYTE *)(&events_1.events + 3 * i) & 1) == 0 )
_assert_fail("events[i].events & EPOLLIN", "svr.c", 0x8Du, "main");
y = 0;
if ( servers <= 0 )
{
LABEL_47:
if ( (unsigned int)func5(*(&events_1.data.fd + 3 * i), v26) )
svr_close += 2;
}
else
{
v15 = v4;
v16 = 1LL;
v17 = 0;
while ( 1 )
{
v18 = fd_servers[v16 - 1];
if ( *(&events_1.data.fd + 3 * i) == v18 )
break;
v15 = v16++;
v17 = 1;
if ( v16 == servers + 1 )
{
y = v15;
goto LABEL_47;
}
}
if ( v17 )
y = v15;
*(_QWORD *)&addr.sa_family = 0LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
v19 = accept(v18, &addr, &addr_len);
v20 = v19;
if ( v19 == -1 )
{
if ( *_errno_location() != 11 )
{
++svr_error;
perror("accept");
}
}
else
{
v21 = fcntl(v19, 3, 0LL);
BYTE1(v21) |= 8u;
if ( fcntl(v20, 4, v21) == -1 )
{
++svr_error;
func6("fcntl");
}
event_2.data.fd = v20;
if ( epoll_ctl(epfd, 1, v20, &event_2) == -1 )
{
++svr_error;
func6("epoll_ctl");
}
v22 = socket(2, 1, 0);
v23 = v22;
if ( v22 == -1 )
func6("Couldn't create server socket");
if ( connect(v22, (const struct sockaddr *)&forwardingrules_server[2 * y], 0x10u) == -1 )
{
++svr_error;
perror("Can't connect to server");
close(v23);
close(*(&events_1.data.fd + 3 * i));
}
else
{
svr_accept += 2;
v24 = fcntl(v23, 3, 0LL);
BYTE1(v24) |= 8u;
if ( fcntl(v23, 4, v24) == -1 )
func6("fcntl sock");
event_2.events = -2147483623;
event_2.data.fd = v23;
if ( epoll_ctl(epfd, 1, v23, &event_2) == -1 )
func6("epoll_ctl");
add_connection_to_tree(v23, v20, v26);
}
}
}
goto LABEL_38;
}
fwrite("Couldn't create BinaryTree. Exiting...\n", 1uLL, 0x27uLL, stderr);
return 1;
}

original_code:
int main (int argc, char* argv[]) {
 int arg, cont=0, sock;
 int num_fds, fd_new, epoll_fd;
 static struct epoll_event events[256000], event;
 struct sockaddr_in remote_addr;
 socklen_t addr_size = sizeof(struct sockaddr_in);
 struct sigaction act;
 pthread_t record;
 char *filename;
 char ipbuf[20];
 BTree *tree;
 if ((tree = create_tree_with_cmp_func(comp)) == ((void *)0)) {
  fprintf(stderr, "Couldn't create BinaryTree. Exiting...\n");
  return 1;
 }
 switch(argc) {
  case 1:
   filename = "servers.config";
  break;
  case 2:
   filename = argv[1];
  break;
  default:
   fprintf(stderr, "Usage: %s [configfile \"default name is %s\"]\n", argv[0], "servers.config");
   exit(1);
 }
 if (readconfigfile(filename))
  SystemFatal("failed to read configfile");
 if (servers<=0)
  SystemFatal("no forwarding rules");
    act.__sigaction_handler.sa_handler = close_fd;
    act.sa_flags = 0;
    if ((sigemptyset (&act.sa_mask) == -1 || sigaction (2, &act, ((void *)0)) == -1))
  SystemFatal("Failed to set SIGINT handler");
 epoll_fd = epoll_create(256000);
 if (epoll_fd == -1)
  SystemFatal("epoll_create");
 for(i = 0; i < servers; i++) {
  bzero(&fd_servers[i], sizeof(int));
  fd_servers[i] = socket (2, SOCK_STREAM, 0);
  if (fd_servers[i] == -1)
   SystemFatal("socket");
  arg = 1;
  if (setsockopt (fd_servers[i], 1, 2, &arg, sizeof(arg)) == -1)
   SystemFatal("setsockopt");
  if (fcntl (fd_servers[i], 4, 04000 | fcntl (fd_servers[i], 3, 0)) == -1)
   SystemFatal("fcntl");
  if (bind (fd_servers[i], (struct sockaddr*) &forwardingrules[i], sizeof(forwardingrules[i])) == -1) {
   printf("%d %s %d\n", i, inet_ntop(2, &forwardingrules[i].sin_addr, ipbuf, 20), ntohs(forwardingrules[i].sin_port));
   SystemFatal("bind");
  }
  if (listen (fd_servers[i], 128) == -1)
   SystemFatal("listen");
  event.events = EPOLLIN | EPOLLERR | EPOLLHUP | EPOLLPRI;
  event.data.fd = fd_servers[i];
  if (epoll_ctl (epoll_fd, 1, fd_servers[i], &event) == -1)
   SystemFatal("epoll_ctl");
 }
 pthread_create(&record, ((void *)0), looprecord, ((void *)0));
 while (1) {
  num_fds = epoll_wait (epoll_fd, events, 256000, -1);
  if (num_fds < 0) {
   ++svr_error;
   SystemFatal ("Error in epoll_wait!");
  }
  for (i = 0; i < num_fds; i++) {
   if (events[i].events & EPOLLPRI) {
    handle_OOB(events[i].data.fd);
   } else if (events[i].events & (EPOLLHUP | EPOLLERR)) {
    ++svr_error;
    perror("epoll: EPOLLERR");
    close(events[i].data.fd);
    continue;
   }
   ((void) sizeof ((events[i].events & EPOLLIN) ? 1 : 0), __extension__ ({ if (events[i].events & EPOLLIN) ; else __assert_fail ("events[i].events & EPOLLIN", "svr.c", 141, __extension__ __PRETTY_FUNCTION__); }));
   for (y = 0; y < servers; y++) {
    if (events[i].data.fd == fd_servers[y]) {
     bzero(&remote_addr, sizeof(struct sockaddr_in));
     fd_new = accept (fd_servers[y], (struct sockaddr*) &remote_addr, &addr_size);
     if (fd_new == -1) {
      if ((*__errno_location ()) != 11 && (*__errno_location ()) != 11) {
       ++svr_error;
       perror("accept");
         }
      cont++;
      break;
     }
     if (fcntl (fd_new, 4, 04000 | fcntl(fd_new, 3, 0)) == -1) {
      ++svr_error;
      SystemFatal("fcntl");
     }
     event.data.fd = fd_new;
     if (epoll_ctl (epoll_fd, 1, fd_new, &event) == -1) {
      ++svr_error;
      SystemFatal ("epoll_ctl");
     }
     if ((sock = socket(2, SOCK_STREAM, 0)) == -1)
      SystemFatal("Couldn't create server socket");
     if (connect(sock, (struct sockaddr *)&forwardingrules_server[y], sizeof(forwardingrules_server[y])) == -1) {
      ++svr_error;
      perror("Can't connect to server");
      close(sock);
      close (events[i].data.fd);
     } else {
      svr_accept += 2;
         if (fcntl (sock, 4, 04000 | fcntl (sock, 3, 0)) == -1)
       SystemFatal("fcntl sock");
         event.events = EPOLLIN | EPOLLERR | EPOLLHUP | EPOLLET;
         event.data.fd = sock;
         if (epoll_ctl (epoll_fd, 1, sock, &event) == -1)
       SystemFatal("epoll_ctl");
      if (add_connection_to_tree(sock, fd_new, tree) == 0) {
      }
     }
     cont++;
     break;
    }
   }
   if (cont !=0) {
    cont = 0;
    continue;
   }
   if (forwardsocket(events[i].data.fd, tree)) {
    svr_close += 2;
      }
  }
 }
 close_fd(0);
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
 int arg; int cont = 0; int sock;
 int num_fds; int fd_new; int epoll_fd;
 static struct epoll_event events[256000]; static struct epoll_event event;
 struct sockaddr_in remote_addr;
 unsigned int addr_size = sizeof(struct sockaddr_in);
 struct sigaction act;
 unsigned long record;
 char *filename;
 char ipbuf[20];
 struct BTree *tree;
 if ((tree = create_tree_with_cmp_func(comp)) == ((void *)0)) {
  fprintf(stderr, "Couldn't create BinaryTree. Exiting...\n");
  return 1;
 }
 switch(argc) {
  case 1:
   filename = "servers.config";
  break;
  case 2:
   filename = argv[1];
  break;
  default:
   fprintf(stderr, "Usage: %s [configfile \"default name is %s\"]\n", argv[0], "servers.config");
   exit(1);
 }
 if (readconfigfile(filename))
  SystemFatal("failed to read configfile");
 if (servers<=0)
  SystemFatal("no forwarding rules");
    act.__sigaction_handler.sa_handler = close_fd;
    act.sa_flags = 0;
    if ((sigemptyset (&act.sa_mask) == -1 || sigaction (2, &act, ((void *)0)) == -1))
  SystemFatal("Failed to set SIGINT handler");
 epoll_fd = epoll_create(256000);
 if (epoll_fd == -1)
  SystemFatal("epoll_create");
 for(i = 0; i < servers; i++) {
  bzero(&fd_servers[i], sizeof(int));
  fd_servers[i] = socket (2, SOCK_STREAM, 0);
  if (fd_servers[i] == -1)
   SystemFatal("socket");
  arg = 1;
  if (setsockopt (fd_servers[i], 1, 2, &arg, sizeof(arg)) == -1)
   SystemFatal("setsockopt");
  if (fcntl (fd_servers[i], 4, 04000 | fcntl (fd_servers[i], 3, 0)) == -1)
   SystemFatal("fcntl");
  if (bind (fd_servers[i], (struct sockaddr *) &forwardingrules[i], sizeof(forwardingrules[i])) == -1) {
   printf("%d %s %d\n", i, inet_ntop(2, &forwardingrules[i].sin_addr, ipbuf, 20), ntohs(forwardingrules[i].sin_port));
   SystemFatal("bind");
  }
  if (listen (fd_servers[i], 128) == -1)
   SystemFatal("listen");
  event.events = EPOLLIN | EPOLLERR | EPOLLHUP | EPOLLPRI;
  event.data.fd = fd_servers[i];
  if (epoll_ctl (epoll_fd, 1, fd_servers[i], &event) == -1)
   SystemFatal("epoll_ctl");
 }
 pthread_create(&record, ((void *)0), looprecord, ((void *)0));
 while (1) {
  num_fds = epoll_wait (epoll_fd, events, 256000, -1);
  if (num_fds < 0) {
   ++svr_error;
   SystemFatal ("Error in epoll_wait!");
  }
  for (i = 0; i < num_fds; i++) {
   if (events[i].events & EPOLLPRI) {
    handle_OOB(events[i].data.fd);
   } else if (events[i].events & (EPOLLHUP | EPOLLERR)) {
    ++svr_error;
    perror("epoll: EPOLLERR");
    close(events[i].data.fd);
    continue;
   }
   ((void) sizeof ((events[i].events & EPOLLIN) ? 1 : 0), __extension__ ({ if (events[i].events & EPOLLIN) ; else __assert_fail ("events[i].events & EPOLLIN", "svr.c", 141, __extension__ __PRETTY_FUNCTION__); }));
   for (y = 0; y < servers; y++) {
    if (events[i].data.fd == fd_servers[y]) {
     bzero(&remote_addr, sizeof(struct sockaddr_in));
     fd_new = accept (fd_servers[y], (struct sockaddr *) &remote_addr, &addr_size);
     if (fd_new == -1) {
      if ((*__errno_location ()) != 11 && (*__errno_location ()) != 11) {
       ++svr_error;
       perror("accept");
         }
      cont++;
      break;
     }
     if (fcntl (fd_new, 4, 04000 | fcntl(fd_new, 3, 0)) == -1) {
      ++svr_error;
      SystemFatal("fcntl");
     }
     event.data.fd = fd_new;
     if (epoll_ctl (epoll_fd, 1, fd_new, &event) == -1) {
      ++svr_error;
      SystemFatal ("epoll_ctl");
     }
     if ((sock = socket(2, SOCK_STREAM, 0)) == -1)
      SystemFatal("Couldn't create server socket");
     if (connect(sock, (struct sockaddr *)&forwardingrules_server[y], sizeof(forwardingrules_server[y])) == -1) {
      ++svr_error;
      perror("Can't connect to server");
      close(sock);
      close (events[i].data.fd);
     } else {
      svr_accept += 2;
         if (fcntl (sock, 4, 04000 | fcntl (sock, 3, 0)) == -1)
       SystemFatal("fcntl sock");
         event.events = EPOLLIN | EPOLLERR | EPOLLHUP | EPOLLET;
         event.data.fd = sock;
         if (epoll_ctl (epoll_fd, 1, sock, &event) == -1)
       SystemFatal("epoll_ctl");
      if (add_connection_to_tree(sock, fd_new, tree) == 0) {
      }
     }
     cont++;
     break;
    }
   }
   if (cont !=0) {
    cont = 0;
    continue;
   }
   if (forwardsocket(events[i].data.fd, tree)) {
    svr_close += 2;
      }
  }
 }
 close_fd(0);
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: arg int

variable_types: cont int

variable_types: sock int

variable_types: struct epoll_event { unsigned int events; union epoll_data { void *ptr; int fd; unsigned int u32; unsigned long u64; } data; } [256000]

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

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

variable_types:
record
unsigned long

variable_types:
filename
char *

variable_types:
ipbuf
char [20]

user_defined_types: struct epoll_event { unsigned int events; union epoll_data data; }

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

user_defined_types: struct node { struct node *parent; struct node *left; struct node *right; void *data; }

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


decompiled_code:
__int64 __fastcall n_side_children(__int64 a1, unsigned int a2)
{
__int64 v2; // rax
__int64 result; // rax
__int64 v4; // rdx

if ( !a1 )
return 0xFFFFFFFFLL;
if ( a2 )
{
result = 0LL;
if ( a2 == 1 )
{
v4 = *(_QWORD *)(a1 + 16);
if ( v4 )
{
do
{
result = (unsigned int)(result + 1);
v4 = *(_QWORD *)(v4 + 16);
}
while ( v4 );
}
else
{
result = 0xFFFFFFFFLL;
}
}
}
else
{
v2 = *(_QWORD *)(a1 + 8);
if ( v2 )
{
do
{
++a2;
v2 = *(_QWORD *)(v2 + 8);
}
while ( v2 );
result = a2;
}
else
{
result = 0xFFFFFFFFLL;
}
}
return result;
}

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

if ( !a1 )
return 0xFFFFFFFFLL;
if ( a2 )
{
result = 0LL;
if ( a2 == 1 )
{
v4 = *(_QWORD *)(a1 + 16);
if ( v4 )
{
do
{
result = (unsigned int)(result + 1);
v4 = *(_QWORD *)(v4 + 16);
}
while ( v4 );
}
else
{
result = 0xFFFFFFFFLL;
}
}
}
else
{
v2 = *(_QWORD *)(a1 + 8);
if ( v2 )
{
do
{
++a2;
v2 = *(_QWORD *)(v2 + 8);
}
while ( v2 );
result = a2;
}
else
{
result = 0xFFFFFFFFLL;
}
}
return result;
}

original_code:
int n_side_children(Node *node, int side) {
 Node *current_node = node;
 int count = 0;
 if (node == ((void *)0)) {
  return -1;
 }
 if (side == 0) {
  if (node->left == ((void *)0))
   return -1;
  while (current_node->left != ((void *)0)) {
   current_node = current_node->left;
   count++;
  }
 } else if (side == 1) {
  if (node->right == ((void *)0))
   return -1;
  while (current_node->right != ((void *)0)) {
   current_node = current_node->right;
   count++;
  }
 }
 return count;
}

canonical_original_code:
int n_side_children(struct node *node, int side) {
 struct node *current_node = node;
 int count = 0;
 if (node == ((void *)0)) {
  return -1;
 }
 if (side == 0) {
  if (node->left == ((void *)0))
   return -1;
  while (current_node->left != ((void *)0)) {
   current_node = current_node->left;
   count++;
  }
 } else if (side == 1) {
  if (node->right == ((void *)0))
   return -1;
  while (current_node->right != ((void *)0)) {
   current_node = current_node->right;
   count++;
  }
 }
 return count;
}

variable_types: struct node { struct node *parent; struct node *left; struct node *right; void *data; } *

variable_types: side int

variable_types: count int


decompiled_code:
__int64 __fastcall delete_node(void *ptr, __int64 a2)
{
__int64 v2; // rbx
int v3; // er13
__int64 v4; // rax
__int64 result; // rax
__int64 v6; // rax
__int64 v7; // rax
__int64 v8; // rdx

if ( !a2 )
return 0xFFFFFFFFLL;
if ( !ptr )
return 0LL;
v2 = *((_QWORD *)ptr + 1);
if ( *(_OWORD *)((char *)ptr + 8) == 0LL )
{
if ( *(void **)a2 != ptr )
{
v2 = *(_QWORD *)ptr;
if ( *(void **)(*(_QWORD *)ptr + 8LL) == ptr )
{
*(_QWORD *)(v2 + 8) = 0LL;
}
else
{
result = 0xFFFFFFFFLL;
if ( *(void **)(v2 + 16) != ptr )
return result;
*(_QWORD *)(v2 + 16) = 0LL;
}
goto LABEL_23;
}
*(_QWORD *)a2 = 0LL;
}
else
{
v3 = n_side_children(*((_QWORD *)ptr + 1), 1u);
if ( !v3 || (int)n_side_children(*((_QWORD *)ptr + 2), 0) > v3 )
v2 = *((_QWORD *)ptr + 2);
if ( *(void **)a2 == ptr )
*(_QWORD *)a2 = v2;
if ( *(_QWORD *)ptr )
{
if ( !v2 )
printf("wtf?");
v4 = *(_QWORD *)ptr;
*(_QWORD *)v2 = *(_QWORD *)ptr;
if ( *(void **)(v4 + 8) == ptr )
{
*(_QWORD *)(v4 + 8) = v2;
}
else if ( *(void **)(v4 + 16) == ptr )
{
*(_QWORD *)(v4 + 16) = v2;
}
goto LABEL_23;
}
if ( v2 )
{
LABEL_23:
v6 = *((_QWORD *)ptr + 1);
if ( v6 == v2 )
{
do
{
v7 = v2;
v2 = *(_QWORD *)(v2 + 16);
}
while ( v2 );
*(_QWORD *)(v7 + 16) = *((_QWORD *)ptr + 2);
}
else if ( *((_QWORD *)ptr + 2) == v2 )
{
do
{
v8 = v2;
v2 = *(_QWORD *)(v2 + 8);
}
while ( v2 );
*(_QWORD *)(v8 + 8) = v6;
}
goto LABEL_25;
}
}
LABEL_25:
free(*((void **)ptr + 3));
free(ptr);
--*(_DWORD *)(a2 + 16);
return 1LL;
}

original_code:
int delete_node(Node *node_to_delete, BTree *tree) {
 Node *current_node;
 int skip = 0, n_children;
 if (tree == ((void *)0)) {
  return -1;
 }
 if (node_to_delete == ((void *)0)) {
  return 0;
 } else if (node_to_delete->left == ((void *)0) && node_to_delete->right == ((void *)0)) {
  if (node_to_delete == tree->head) {
   tree->head = ((void *)0);
   current_node = tree->head;
  } else {
   current_node = node_to_delete->parent;
   if (node_to_delete == current_node->left) {
    current_node->left = ((void *)0);
   } else if (node_to_delete == current_node->right) {
    current_node->right = ((void *)0);
   } else {
    return -1;
   }
  }
  skip = 1;
 } else if ((n_children = n_side_children(node_to_delete->left, 1)) == 0 ||
      n_side_children(node_to_delete->right, 0) > n_children) {
  current_node = node_to_delete->right;
 } else {
  current_node = node_to_delete->left;
 }
 if (!skip && node_to_delete == tree->head) {
  tree->head = current_node;
 }
 if (!skip && node_to_delete->parent != ((void *)0)) {
  if (current_node == ((void *)0))
   printf("wtf?");
  current_node->parent = node_to_delete->parent;
  if (node_to_delete == node_to_delete->parent->left) {
   node_to_delete->parent->left = current_node;
  } else if (node_to_delete == node_to_delete->parent->right) {
   node_to_delete->parent->right = current_node;
  }
 }
 if (current_node != ((void *)0)) {
  if (current_node == node_to_delete->left) {
   while (current_node->right != ((void *)0)) {
    current_node = current_node->right;
   }
   current_node->right = node_to_delete->right;
  } else if (current_node == node_to_delete->right) {
   while (current_node->left != ((void *)0)) {
    current_node = current_node->left;
   }
   current_node->left = node_to_delete->left;
  }
 }
 free(node_to_delete->data);
 free(node_to_delete);
 tree->n_children--;
 return 1;
}

canonical_original_code:
int delete_node(struct node *node_to_delete, struct BTree *tree) {
 struct node *current_node;
 int skip = 0; int n_children;
 if (tree == ((void *)0)) {
  return -1;
 }
 if (node_to_delete == ((void *)0)) {
  return 0;
 } else if (node_to_delete->left == ((void *)0) && node_to_delete->right == ((void *)0)) {
  if (node_to_delete == tree->head) {
   tree->head = ((void *)0);
   current_node = tree->head;
  } else {
   current_node = node_to_delete->parent;
   if (node_to_delete == current_node->left) {
    current_node->left = ((void *)0);
   } else if (node_to_delete == current_node->right) {
    current_node->right = ((void *)0);
   } else {
    return -1;
   }
  }
  skip = 1;
 } else if ((n_children = n_side_children(node_to_delete->left, 1)) == 0 ||
      n_side_children(node_to_delete->right, 0) > n_children) {
  current_node = node_to_delete->right;
 } else {
  current_node = node_to_delete->left;
 }
 if (!skip && node_to_delete == tree->head) {
  tree->head = current_node;
 }
 if (!skip && node_to_delete->parent != ((void *)0)) {
  if (current_node == ((void *)0))
   printf("wtf?");
  current_node->parent = node_to_delete->parent;
  if (node_to_delete == node_to_delete->parent->left) {
   node_to_delete->parent->left = current_node;
  } else if (node_to_delete == node_to_delete->parent->right) {
   node_to_delete->parent->right = current_node;
  }
 }
 if (current_node != ((void *)0)) {
  if (current_node == node_to_delete->left) {
   while (current_node->right != ((void *)0)) {
    current_node = current_node->right;
   }
   current_node->right = node_to_delete->right;
  } else if (current_node == node_to_delete->right) {
   while (current_node->left != ((void *)0)) {
    current_node = current_node->left;
   }
   current_node->left = node_to_delete->left;
  }
 }
 free(node_to_delete->data);
 free(node_to_delete);
 tree->n_children--;
 return 1;
}

variable_types: struct BTree { struct node *head; int (*__compare_func)(void *item1, void *item2); unsigned int n_children; } *

variable_types: skip int


decompiled_code:
__int64 __fastcall add_connection_to_tree(int a1, int a2, __int64 a3)
{
__int64 v4; // rdx
__int64 result; // rax
int v6; // [rsp+8h] [rbp-20h] BYREF
int v7; // [rsp+Ch] [rbp-1Ch]

v6 = a1;
v7 = a2;
if ( !add_object_to_tree(&v6, 8LL) )
goto LABEL_7;
if ( !find_node(&v6, a3) )
fwrite("Node was not saved successfully.\n", 1uLL, 0x21uLL, stderr);
v6 = a2;
v7 = a1;
if ( add_object_to_tree(&v6, 8LL) )
{
v4 = find_node(&v6, a3);
result = 1LL;
if ( !v4 )
{
fwrite("Node was not saved successfully.\n", 1uLL, 0x21uLL, stderr);
result = 1LL;
}
}
else
{
LABEL_7:
fwrite("unable to add node\n", 1uLL, 0x13uLL, stderr);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, int a2, __int64 a3)
{
__int64 v4; // rdx
__int64 result; // rax
int v6; // [rsp+8h] [rbp-20h] BYREF
int v7; // [rsp+Ch] [rbp-1Ch]

v6 = a1;
v7 = a2;
if ( !add_object_to_tree(&v6, 8LL) )
goto LABEL_7;
if ( !find_node(&v6, a3) )
fwrite("Node was not saved successfully.\n", 1uLL, 0x21uLL, stderr);
v6 = a2;
v7 = a1;
if ( add_object_to_tree(&v6, 8LL) )
{
v4 = find_node(&v6, a3);
result = 1LL;
if ( !v4 )
{
fwrite("Node was not saved successfully.\n", 1uLL, 0x21uLL, stderr);
result = 1LL;
}
}
else
{
LABEL_7:
fwrite("unable to add node\n", 1uLL, 0x13uLL, stderr);
result = 0LL;
}
return result;
}

original_code:
int add_connection_to_tree(int sock1, int sock2, BTree *tree) {
 Connection conn;
 conn.src = sock1;
 conn.dst = sock2;
 if (add_object_to_tree((void *)&conn, sizeof(conn), tree) == ((void *)0)) {
  fprintf(stderr, "unable to add node\n");
  return 0;
 }
 if (find_node(&conn, tree) == ((void *)0))
  fprintf(stderr, "Node was not saved successfully.\n");
 conn.src = sock2;
 conn.dst = sock1;
 if (add_object_to_tree((void *)&conn, sizeof(conn), tree) == ((void *)0)) {
  fprintf(stderr, "unable to add node\n");
  return 0;
 }
 if (find_node(&conn, tree) == ((void *)0))
  fprintf(stderr, "Node was not saved successfully.\n");
 return 1;
}

canonical_original_code:
int add_connection_to_tree(int sock1, int sock2, struct BTree *tree) {
 struct Connection conn;
 conn.src = sock1;
 conn.dst = sock2;
 if (add_object_to_tree((void *)&conn, sizeof(conn), tree) == ((void *)0)) {
  fprintf(stderr, "unable to add node\n");
  return 0;
 }
 if (find_node(&conn, tree) == ((void *)0))
  fprintf(stderr, "Node was not saved successfully.\n");
 conn.src = sock2;
 conn.dst = sock1;
 if (add_object_to_tree((void *)&conn, sizeof(conn), tree) == ((void *)0)) {
  fprintf(stderr, "unable to add node\n");
  return 0;
 }
 if (find_node(&conn, tree) == ((void *)0))
  fprintf(stderr, "Node was not saved successfully.\n");
 return 1;
}

variable_types: struct BTree { struct node { struct node *parent; struct node *left; struct node *right; void *data; } *head; int (*__compare_func)(void *item1, void *item2); unsigned int n_children; } *

variable_types:
conn
struct Connection { int src; int dst; }


decompiled_code:
__int64 __fastcall remove_connection_from_tree(int fd, int a2, __int64 a3)
{
__int64 v5; // rax
__int64 v6; // rdi
int v7; // edx
__int64 result; // rax
int v9[8]; // [rsp+8h] [rbp-20h] BYREF

v9[0] = a2;
v5 = find_node(v9, a3);
if ( !v5 )
{
fprintf(stderr, "Couldn't find node with src fd %d line %d\n", (unsigned int)v9[0], 41LL);
return 0LL;
}
if ( (int)delete_node(v5, a3) <= 0 )
goto LABEL_10;
v9[0] = fd;
v6 = find_node(v9, a3);
if ( !v6 )
{
fprintf(stderr, "Couldn't find node with src fd %d line %d\n", (unsigned int)v9[0], 52LL);
return 0LL;
}
if ( (int)delete_node(v6, a3) <= 0 )
{
LABEL_10:
fwrite("Unable to delete node.\n", 1uLL, 0x17uLL, stderr);
return 0LL;
}
shutdown(fd, 2);
if ( close(fd) )
perror("close failed");
shutdown(a2, 2);
v7 = close(a2);
result = 1LL;
if ( v7 )
{
perror("close failed");
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int fd, int a2, __int64 a3)
{
__int64 v5; // rax
__int64 v6; // rdi
int v7; // edx
__int64 result; // rax
int v9[8]; // [rsp+8h] [rbp-20h] BYREF

v9[0] = a2;
v5 = find_node(v9, a3);
if ( !v5 )
{
fprintf(stderr, "Couldn't find node with src fd %d line %d\n", (unsigned int)v9[0], 41LL);
return 0LL;
}
if ( (int)delete_node(v5, a3) <= 0 )
goto LABEL_10;
v9[0] = fd;
v6 = find_node(v9, a3);
if ( !v6 )
{
fprintf(stderr, "Couldn't find node with src fd %d line %d\n", (unsigned int)v9[0], 52LL);
return 0LL;
}
if ( (int)delete_node(v6, a3) <= 0 )
{
LABEL_10:
fwrite("Unable to delete node.\n", 1uLL, 0x17uLL, stderr);
return 0LL;
}
shutdown(fd, 2);
if ( close(fd) )
perror("close failed");
shutdown(a2, 2);
v7 = close(a2);
result = 1LL;
if ( v7 )
{
perror("close failed");
result = 1LL;
}
return result;
}

original_code:
int remove_connection_from_tree(int sock1, int sock2, BTree *tree) {
 Connection conn;
 Node *node;
 conn.src = sock2;
 if ((node = find_node(&conn, tree)) == ((void *)0)) {
  fprintf(stderr, "Couldn't find node with src fd %d line %d\n", conn.src, 41);
  return 0;
 }
 if (delete_node(node, tree) <= 0) {
  fprintf(stderr, "Unable to delete node.\n");
  return 0;
 }
 conn.src = sock1;
 if ((node = find_node(&conn, tree)) == ((void *)0)) {
  fprintf(stderr, "Couldn't find node with src fd %d line %d\n", conn.src, 52);
  return 0;
 }
 if (delete_node(node, tree) <= 0) {
  fprintf(stderr, "Unable to delete node.\n");
  return 0;
 }
 shutdown(sock1, SHUT_RDWR);
 if (close(sock1) != 0) {
  perror("close failed");
 }
 shutdown(sock2, SHUT_RDWR);
 if (close(sock2) != 0) {
  perror("close failed");
 }
 return 1;
}

canonical_original_code:
int remove_connection_from_tree(int sock1, int sock2, struct BTree *tree) {
 struct Connection conn;
 struct node *node;
 conn.src = sock2;
 if ((node = find_node(&conn, tree)) == ((void *)0)) {
  fprintf(stderr, "Couldn't find node with src fd %d line %d\n", conn.src, 41);
  return 0;
 }
 if (delete_node(node, tree) <= 0) {
  fprintf(stderr, "Unable to delete node.\n");
  return 0;
 }
 conn.src = sock1;
 if ((node = find_node(&conn, tree)) == ((void *)0)) {
  fprintf(stderr, "Couldn't find node with src fd %d line %d\n", conn.src, 52);
  return 0;
 }
 if (delete_node(node, tree) <= 0) {
  fprintf(stderr, "Unable to delete node.\n");
  return 0;
 }
 shutdown(sock1, SHUT_RDWR);
 if (close(sock1) != 0) {
  perror("close failed");
 }
 shutdown(sock2, SHUT_RDWR);
 if (close(sock2) != 0) {
  perror("close failed");
 }
 return 1;
}


decompiled_code:
int handle_OOB()
{
return puts("Received OOB data! We don't care!");
}

canonical_decompiled_code:
int func2()
{
return puts("Received OOB data! We don't care!");
}

original_code:
void handle_OOB(int sock) {
 printf("Received OOB data! We don't care!\n");
}


repo: dbader/potatoes

decompiled_code:
void *__fastcall potatoes_bzero(void *a1, unsigned int a2)
{
return memset(a1, 0, a2);
}

canonical_decompiled_code:
void *__fastcall func0(void *a1, unsigned int a2)
{
return memset(a1, 0, a2);
}

original_code:
void potatoes_bzero(void *dest, potatoes_uint32 count)
{
 bzero(dest, count);
}

canonical_original_code:
void potatoes_bzero(void *dest, unsigned int count)
{
 bzero(dest, count);
}

variable_types:
dest
void *

variable_types:
count
unsigned int


decompiled_code:
void *__fastcall potatoes_malloc(size_t size)
{
return malloc((unsigned int)size);
}

canonical_decompiled_code:
void *__fastcall func1(size_t size)
{
return malloc((unsigned int)size);
}

original_code:
void *potatoes_malloc(potatoes_uint32 size)
{
 return malloc(size);
}

canonical_original_code:
void *potatoes_malloc(unsigned int size)
{
 return malloc(size);
}

variable_types:
size
unsigned int


decompiled_code:
void *__fastcall potatoes_mallocn(size_t size)
{
return malloc((unsigned int)size);
}

canonical_decompiled_code:
void *__fastcall func2(size_t size)
{
return malloc((unsigned int)size);
}

original_code:
void *potatoes_mallocn(potatoes_uint32 size, char *name)
{
 return malloc(size);
}

canonical_original_code:
void *potatoes_mallocn(unsigned int size, char *name)
{
 return malloc(size);
}

variable_types:
name
char *


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

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

original_code:
void potatoes_free(void *start)
{
 free(start);
}

variable_types:
start
void *


decompiled_code:
size_t potatoes_mem_dump()
{
return fwrite("NOT IMPLEMENTED: potatoes_mem_dump\n", 1uLL, 0x23uLL, stderr);
}

canonical_decompiled_code:
size_t func4()
{
return fwrite("NOT IMPLEMENTED: potatoes_mem_dump\n", 1uLL, 0x23uLL, stderr);
}

original_code:
void potatoes_mem_dump()
{
 fprintf(stderr, "NOT IMPLEMENTED: potatoes_mem_dump\n");
}


decompiled_code:
char *__fastcall potatoes_strncpy(char *a1, const char *a2, size_t a3)
{
return strncpy(a1, a2, a3);
}

canonical_decompiled_code:
char *__fastcall func5(char *a1, const char *a2, size_t a3)
{
return strncpy(a1, a2, a3);
}

original_code:
char *potatoes_strncpy(char *dest, char *src, size_t n)
{
 return strncpy(dest, src, n);
}

canonical_original_code:
char *potatoes_strncpy(char *dest, char *src, unsigned long n)
{
 return strncpy(dest, src, n);
}

variable_types:
dest
char *

variable_types:
src
char *

variable_types:
n
unsigned long


decompiled_code:
size_t __fastcall potatoes_strlen(const char *a1)
{
return strlen(a1);
}

canonical_decompiled_code:
size_t __fastcall func6(const char *a1)
{
return strlen(a1);
}

original_code:
potatoes_uint32 potatoes_strlen(char* str)
{
 return strlen(str);
}

canonical_original_code:
unsigned int potatoes_strlen(char *str)
{
 return strlen(str);
}

variable_types:
str
char *

return_type: unsigned int


decompiled_code:
char *__fastcall potatoes_strdup(const char *a1)
{
return strdup(a1);
}

canonical_decompiled_code:
char *__fastcall func7(const char *a1)
{
return strdup(a1);
}

original_code:
char *potatoes_strdup(char* str)
{
 return strdup(str);
}

canonical_original_code:
char *potatoes_strdup(char *str)
{
 return strdup(str);
}


decompiled_code:
char *__fastcall potatoes_strsep(char **a1, const char *a2)
{
return strsep(a1, a2);
}

canonical_decompiled_code:
char *__fastcall func8(char **a1, const char *a2)
{
return strsep(a1, a2);
}

original_code:
char *potatoes_strsep(char **str_ptr, char *delims)
{
 return strsep(str_ptr, delims);
}

variable_types:
delims
char *


decompiled_code:
int __fastcall potatoes_strcmp(const char *a1, const char *a2)
{
return strcmp(a1, a2);
}

canonical_decompiled_code:
int __fastcall func9(const char *a1, const char *a2)
{
return strcmp(a1, a2);
}

original_code:
potatoes_sint32 potatoes_strcmp(char *s1, char *s2)
{
 return strcmp(s1, s2);
}

canonical_original_code:
int potatoes_strcmp(char *s1, char *s2)
{
 return strcmp(s1, s2);
}


decompiled_code:
void *__fastcall potatoes_memcpy(void *a1, const void *a2, unsigned int a3)
{
return memcpy(a1, a2, a3);
}

canonical_decompiled_code:
void *__fastcall func10(void *a1, const void *a2, unsigned int a3)
{
return memcpy(a1, a2, a3);
}

original_code:
void *potatoes_memcpy(void *dest, void *src, potatoes_size_t count)
{
 return memcpy(dest, src, count);
}

canonical_original_code:
void *potatoes_memcpy(void *dest, void *src, unsigned int count)
{
 return memcpy(dest, src, count);
}

variable_types:
src
void *


decompiled_code:
int potatoes_printf(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
return vfprintf(stderr, format, arg);
}

canonical_decompiled_code:
int func11(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
return vfprintf(stderr, format, arg);
}

original_code:
void potatoes_printf(char *fmt, ...)
{
 va_list va;
 __builtin_va_start(va,fmt);
 vfprintf(stderr, fmt, va);
 __builtin_va_end(va);
}

canonical_original_code:
void potatoes_printf(char *fmt, ...)
{
 __builtin_va_list va;
 __builtin_va_start(va,fmt);
 vfprintf(stderr, fmt, va);
 __builtin_va_end(va);
}

variable_types:
fmt
char *


decompiled_code:
char *__fastcall potatoes_strncat(char *a1, const char *a2, unsigned int a3)
{
return strncat(a1, a2, a3);
}

canonical_decompiled_code:
char *__fastcall func12(char *a1, const char *a2, unsigned int a3)
{
return strncat(a1, a2, a3);
}

original_code:
char *potatoes_strncat(char *s1, char *s2, potatoes_size_t n)
{
 return strncat(s1, s2, n);
}

canonical_original_code:
char *potatoes_strncat(char *s1, char *s2, unsigned int n)
{
 return strncat(s1, s2, n);
}

variable_types:
n
unsigned int


decompiled_code:
char *__fastcall potatoes_strcat(char *a1, const char *a2)
{
return strcat(a1, a2);
}

canonical_decompiled_code:
char *__fastcall func13(char *a1, const char *a2)
{
return strcat(a1, a2);
}

original_code:
char *potatoes_strcat(char *s1, char *s2)
{
 return strcat(s1, s2);
}


decompiled_code:
void __fastcall __noreturn potatoes_panic(const char *a1)
{
fprintf(stderr, "PANIC: %s (I'll refuse to crash your os...)\n", a1);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func14(const char *a1)
{
fprintf(stderr, "PANIC: %s (I'll refuse to crash your os...)\n", a1);
exit(1);
}

original_code:
void potatoes_panic(char *msg)
{
 fprintf(stderr, "PANIC: %s (I'll refuse to crash your os...)\n", msg);
 exit(1);
}

variable_types:
msg
char *


decompiled_code:
char *__fastcall potatoes_itoa(unsigned int a1, char *a2, int a3)
{
if ( a3 != 10 )
fwrite("WARNING: base is currently ignored in itoa...\n", 1uLL, 0x2EuLL, stderr);
sprintf(a2, "%i", a1);
return a2;
}

canonical_decompiled_code:
char *__fastcall func15(unsigned int a1, char *a2, int a3)
{
if ( a3 != 10 )
fwrite("WARNING: base is currently ignored in itoa...\n", 1uLL, 0x2EuLL, stderr);
sprintf(a2, "%i", a1);
return a2;
}

original_code:
char *potatoes_itoa(int n, char *str, unsigned int base)
{
 if(base != 10)
 {
  fprintf(stderr, "WARNING: base is currently ignored in itoa...\n");
 }
 sprintf(str, "%i", n);
 return str;
}

variable_types: n int

variable_types:
base
unsigned int


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

result = a2;
strcpy(a2, "time2str stub.");
return result;
}

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

result = a2;
strcpy(a2, "time2str stub.");
return result;
}

original_code:
char *potatoes_time2str(potatoes_time_t timestamp, char buf[24])
{
 strcpy(buf, "time2str stub.");
 return buf;
}

canonical_original_code:
char *potatoes_time2str(struct potatoes_time timestamp, char buf[24])
{
 strcpy(buf, "time2str stub.");
 return buf;
}

variable_types: struct potatoes_time { unsigned char sec; unsigned char min; unsigned char hour; unsigned char weekday; unsigned char day; unsigned char month; unsigned char year; unsigned char century; }

variable_types:
buf
char [24]


decompiled_code:
__int64 potatoes_get_hdsize()
{
if ( potatoes_current_disc )
return *(unsigned int *)(potatoes_current_disc + 8);
fwrite("ERROR: no disc selected in get_hdsize\n", 1uLL, 0x26uLL, stderr);
return 0LL;
}

canonical_decompiled_code:
__int64 func17()
{
if ( potatoes_current_disc )
return *(unsigned int *)(potatoes_current_disc + 8);
fwrite("ERROR: no disc selected in get_hdsize\n", 1uLL, 0x26uLL, stderr);
return 0LL;
}

original_code:
potatoes_uint32 potatoes_get_hdsize()
{
 if(potatoes_current_disc == ((void *)0))
 {
  fprintf(stderr, "ERROR: no disc selected in get_hdsize\n");
  return 0;
 }
 return potatoes_current_disc->size;
}

canonical_original_code:
unsigned int potatoes_get_hdsize()
{
 if(potatoes_current_disc == ((void *)0))
 {
  fprintf(stderr, "ERROR: no disc selected in get_hdsize\n");
  return 0;
 }
 return potatoes_current_disc->size;
}


decompiled_code:
int __fastcall potatoes_hd_write_sector(unsigned int a1, char *a2)
{
unsigned int v2; // ecx
_QWORD *v3; // rcx
__int64 v4; // rax

if ( potatoes_current_disc )
{
v2 = (a1 + 1) << 9;
if ( v2 > *(_DWORD *)(potatoes_current_disc + 8) )
{
LODWORD(v4) = fprintf(stderr, "ERROR: no sector %i in hd_write_sector\n", a1);
}
else
{
v3 = (_QWORD *)(*(_QWORD *)potatoes_current_disc + v2 - 512);
*v3 = *(_QWORD *)a2;
v4 = *((_QWORD *)a2 + 63);
v3[63] = v4;
qmemcpy(
(void *)((unsigned __int64)(v3 + 1) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)(a2 - ((char *)v3 - ((unsigned __int64)(v3 + 1) & 0xFFFFFFFFFFFFFFF8LL))),
8LL * (((unsigned int)v3 - (((_DWORD)v3 + 8) & 0xFFFFFFF8) + 512) >> 3));
}
}
else
{
LODWORD(v4) = fwrite("ERROR: no disc selected in hd_write_sector\n", 1uLL, 0x2BuLL, stderr);
}
return v4;
}

canonical_decompiled_code:
int __fastcall func18(unsigned int a1, char *a2)
{
unsigned int v2; // ecx
_QWORD *v3; // rcx
__int64 v4; // rax

if ( potatoes_current_disc )
{
v2 = (a1 + 1) << 9;
if ( v2 > *(_DWORD *)(potatoes_current_disc + 8) )
{
LODWORD(v4) = fprintf(stderr, "ERROR: no sector %i in hd_write_sector\n", a1);
}
else
{
v3 = (_QWORD *)(*(_QWORD *)potatoes_current_disc + v2 - 512);
*v3 = *(_QWORD *)a2;
v4 = *((_QWORD *)a2 + 63);
v3[63] = v4;
qmemcpy(
(void *)((unsigned __int64)(v3 + 1) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)(a2 - ((char *)v3 - ((unsigned __int64)(v3 + 1) & 0xFFFFFFFFFFFFFFF8LL))),
8LL * (((unsigned int)v3 - (((_DWORD)v3 + 8) & 0xFFFFFFF8) + 512) >> 3));
}
}
else
{
LODWORD(v4) = fwrite("ERROR: no disc selected in hd_write_sector\n", 1uLL, 0x2BuLL, stderr);
}
return v4;
}

original_code:
void potatoes_hd_write_sector(potatoes_uint32 dest, void* src)
{
 if(potatoes_current_disc == ((void *)0))
 {
  fprintf(stderr, "ERROR: no disc selected in hd_write_sector\n");
  return;
 }
 if((dest + 1) * 512 > potatoes_current_disc->size)
 {
  fprintf(stderr, "ERROR: no sector %i in hd_write_sector\n", dest);
  return;
 }
 memcpy(potatoes_current_disc->data + (dest * 512), src, 512);
}

canonical_original_code:
void potatoes_hd_write_sector(unsigned int dest, void *src)
{
 if(potatoes_current_disc == ((void *)0))
 {
  fprintf(stderr, "ERROR: no disc selected in hd_write_sector\n");
  return;
 }
 if((dest + 1) * 512 > potatoes_current_disc->size)
 {
  fprintf(stderr, "ERROR: no sector %i in hd_write_sector\n", dest);
  return;
 }
 memcpy(potatoes_current_disc->data + (dest * 512), src, 512);
}

variable_types:
dest
unsigned int


decompiled_code:
int __fastcall potatoes_hd_read_sector(_QWORD *a1, unsigned int a2)
{
unsigned int v2; // eax
char *v3; // rsi
__int64 v4; // rax

if ( potatoes_current_disc )
{
v2 = (a2 + 1) << 9;
if ( v2 > *(_DWORD *)(potatoes_current_disc + 8) )
{
LODWORD(v4) = fprintf(stderr, "ERROR: no sector %i in hd_read_sector\n", a2);
}
else
{
v3 = (char *)(*(_QWORD *)potatoes_current_disc + v2 - 512);
*a1 = *(_QWORD *)v3;
v4 = *((_QWORD *)v3 + 63);
a1[63] = v4;
qmemcpy(
(void *)((unsigned __int64)(a1 + 1) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)(v3 - ((char *)a1 - ((unsigned __int64)(a1 + 1) & 0xFFFFFFFFFFFFFFF8LL))),
8LL * (((unsigned int)a1 - (((_DWORD)a1 + 8) & 0xFFFFFFF8) + 512) >> 3));
}
}
else
{
LODWORD(v4) = fwrite("ERROR: no disc selected in hd_read_sector\n", 1uLL, 0x2AuLL, stderr);
}
return v4;
}

canonical_decompiled_code:
int __fastcall func19(_QWORD *a1, unsigned int a2)
{
unsigned int v2; // eax
char *v3; // rsi
__int64 v4; // rax

if ( potatoes_current_disc )
{
v2 = (a2 + 1) << 9;
if ( v2 > *(_DWORD *)(potatoes_current_disc + 8) )
{
LODWORD(v4) = fprintf(stderr, "ERROR: no sector %i in hd_read_sector\n", a2);
}
else
{
v3 = (char *)(*(_QWORD *)potatoes_current_disc + v2 - 512);
*a1 = *(_QWORD *)v3;
v4 = *((_QWORD *)v3 + 63);
a1[63] = v4;
qmemcpy(
(void *)((unsigned __int64)(a1 + 1) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)(v3 - ((char *)a1 - ((unsigned __int64)(a1 + 1) & 0xFFFFFFFFFFFFFFF8LL))),
8LL * (((unsigned int)a1 - (((_DWORD)a1 + 8) & 0xFFFFFFF8) + 512) >> 3));
}
}
else
{
LODWORD(v4) = fwrite("ERROR: no disc selected in hd_read_sector\n", 1uLL, 0x2AuLL, stderr);
}
return v4;
}

original_code:
void potatoes_hd_read_sector(void* dest, potatoes_uint32 src)
{
 if(potatoes_current_disc == ((void *)0))
 {
  fprintf(stderr, "ERROR: no disc selected in hd_read_sector\n");
  return;
 }
 if((src + 1) * 512 > potatoes_current_disc->size)
 {
  fprintf(stderr, "ERROR: no sector %i in hd_read_sector\n", src);
  return;
 }
 memcpy(dest, potatoes_current_disc->data + (src * 512), 512);
}

canonical_original_code:
void potatoes_hd_read_sector(void *dest, unsigned int src)
{
 if(potatoes_current_disc == ((void *)0))
 {
  fprintf(stderr, "ERROR: no disc selected in hd_read_sector\n");
  return;
 }
 if((src + 1) * 512 > potatoes_current_disc->size)
 {
  fprintf(stderr, "ERROR: no sector %i in hd_read_sector\n", src);
  return;
 }
 memcpy(dest, potatoes_current_disc->data + (src * 512), 512);
}

variable_types:
dest
void *

variable_types:
src
unsigned int


decompiled_code:
void __fastcall potatoes_disc_destroy(void *ptr)
{
if ( ptr )
{
munmap(*(void **)ptr, *((unsigned int *)ptr + 2));
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func20(void *ptr)
{
if ( ptr )
{
munmap(*(void **)ptr, *((unsigned int *)ptr + 2));
free(ptr);
}
}

original_code:
void potatoes_disc_destroy(PotatoesDisk *disk)
{
 if(disk == ((void *)0))
  return;
 munmap(disk->data, disk->size);
 free(disk);
}

canonical_original_code:
void potatoes_disc_destroy(struct PotatoesDisk *disk)
{
 if(disk == ((void *)0))
  return;
 munmap(disk->data, disk->size);
 free(disk);
}

variable_types: struct PotatoesDisk { unsigned char *data; unsigned int size; } *


decompiled_code:
void __fastcall potatoes_set_current_disk(__int64 a1)
{
potatoes_current_disc = a1;
}

canonical_decompiled_code:
void __fastcall func21(__int64 a1)
{
potatoes_current_disc = a1;
}

original_code:
void potatoes_set_current_disk(PotatoesDisk *disk)
{
 potatoes_current_disc = disk;
}

canonical_original_code:
void potatoes_set_current_disk(struct PotatoesDisk *disk)
{
 potatoes_current_disc = disk;
}

user_defined_types: struct PotatoesDisk { unsigned char *data; unsigned int size; }


decompiled_code:
__int64 *init_file_table()
{
__int64 *result; // rax

result = &gft;
do
{
*(_WORD *)result = -1;
result += 4;
}
while ( result != (__int64 *)(unsigned int)((_DWORD)&gft + 320) );
return result;
}

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

result = &gft;
do
{
*(_WORD *)result = -1;
result += 4;
}
while ( result != (__int64 *)(unsigned int)((_DWORD)&gft + 320) );
return result;
}

original_code:
void init_file_table()
{
        for (int i = 0; i < 10; i++) {
                gft[i].f_desc = -1;
        }
}

variable_types: i int


decompiled_code:
__int64 *alloc_file()
{
__int64 v0; // rax
__int64 v2; // rcx

v0 = 0LL;
while ( LOWORD(gft[4 * v0]) != 0xFFFF )
{
if ( ++v0 == 10 )
return 0LL;
}
v2 = 4LL * (int)v0;
LOWORD(gft[v2]) = v0;
WORD1(gft[v2 + 3]) = 0;
return &gft[v2];
}

canonical_decompiled_code:
__int64 *func1()
{
__int64 v0; // rax
__int64 v2; // rcx

v0 = 0LL;
while ( LOWORD(gft[4 * v0]) != 0xFFFF )
{
if ( ++v0 == 10 )
return 0LL;
}
v2 = 4LL * (int)v0;
LOWORD(gft[v2]) = v0;
WORD1(gft[v2 + 3]) = 0;
return &gft[v2];
}

original_code:
file* alloc_file()
{
        for (int i = 0; i < 10; i++) {
                if (gft[i].f_desc == -1) {
                        gft[i].f_desc = i;
                        gft[i].f_count = 0;
                        return &gft[i];
                }
        }
        return (file *) 0;
}

canonical_original_code:
struct file *alloc_file()
{
        for (int i = 0; i < 10; i++) {
                if (gft[i].f_desc == -1) {
                        gft[i].f_desc = i;
                        gft[i].f_count = 0;
                        return &gft[i];
                }
        }
        return (struct file *) 0;
}

return_type: struct file { short f_desc; struct m_inode { short i_num; unsigned int i_adr; unsigned short i_mode; unsigned int i_size; struct time { unsigned char sec; unsigned char min; unsigned char hour; unsigned char weekday; unsigned char day; unsigned char month; unsigned char year; unsigned char century; } i_create_ts; struct time i_modify_ts; unsigned int i_direct_pointer[30]; unsigned int i_single_indirect_pointer; unsigned int i_double_indirect_pointer; } *f_inode; char *f_name; unsigned char f_mode; unsigned short f_count; } *

user_defined_types: struct file { short f_desc; struct m_inode *f_inode; char *f_name; unsigned char f_mode; unsigned short f_count; }

user_defined_types: struct m_inode { short i_num; unsigned int i_adr; unsigned short i_mode; unsigned int i_size; struct time i_create_ts; struct time i_modify_ts; unsigned int i_direct_pointer[30]; unsigned int i_single_indirect_pointer; unsigned int i_double_indirect_pointer; }


decompiled_code:
__int64 __fastcall insert_proc_file(_WORD *a1, __int16 a2)
{
_WORD *v2; // rax
unsigned __int16 *v3; // rbp

v2 = alloc_proc_file(a1);
if ( !v2 )
return 0xFFFFFFFFLL;
v3 = v2;
v2[1] = a2;
*((_DWORD *)v2 + 1) = 0;
if ( get_file(a2) )
inc_count(a2);
return *v3;
}

canonical_decompiled_code:
__int64 __fastcall func3(_WORD *a1, __int16 a2)
{
_WORD *v2; // rax
unsigned __int16 *v3; // rbp

v2 = alloc_proc_file(a1);
if ( !v2 )
return 0xFFFFFFFFLL;
v3 = v2;
v2[1] = a2;
*((_DWORD *)v2 + 1) = 0;
if ( get_file(a2) )
inc_count(a2);
return *v3;
}

original_code:
file_nr insert_proc_file(proc_file pft[20], file_nr glo_fd)
{
        proc_file *fd = alloc_proc_file(pft);
        if (fd == 0) {
                ((void)0);
                return -1;
        }
        fd->pf_f_desc = glo_fd;
        fd->pf_pos = 0;
        file *f = get_file(glo_fd);
        if (f != 0) {
                inc_count(glo_fd);
        }
        return fd->pf_desc;
}

canonical_original_code:
short insert_proc_file(struct proc_file pft[20], short glo_fd)
{
        struct proc_file *fd = alloc_proc_file(pft);
        if (fd == 0) {
                ((void)0);
                return -1;
        }
        fd->pf_f_desc = glo_fd;
        fd->pf_pos = 0;
        struct file *f = get_file(glo_fd);
        if (f != 0) {
                inc_count(glo_fd);
        }
        return fd->pf_desc;
}

variable_types: struct proc_file { short pf_desc; short pf_f_desc; unsigned int pf_pos; } [20]

variable_types: struct proc_file { short pf_desc; short pf_f_desc; unsigned int pf_pos; } *

variable_types: struct file { short f_desc; struct m_inode { short i_num; unsigned int i_adr; unsigned short i_mode; unsigned int i_size; struct time { unsigned char sec; unsigned char min; unsigned char hour; unsigned char weekday; unsigned char day; unsigned char month; unsigned char year; unsigned char century; } i_create_ts; struct time i_modify_ts; unsigned int i_direct_pointer[30]; unsigned int i_single_indirect_pointer; unsigned int i_double_indirect_pointer; } *f_inode; char *f_name; unsigned char f_mode; unsigned short f_count; } *

return_type: short


lseek

decompiled_code:
__int64 __fastcall lseek(_WORD *a1, __int16 a2, int a3, int a4)
{
_WORD *v6; // rax
unsigned int v7; // ebx

v6 = get_proc_file(a1, a2);
if ( !v6 )
return (unsigned int)-1;
v7 = a4 + a3;
*((_DWORD *)v6 + 1) = v7;
return v7;
}

canonical_decompiled_code:
__int64 __fastcall func4(_WORD *a1, __int16 a2, int a3, int a4)
{
_WORD *v6; // rax
unsigned int v7; // ebx

v6 = get_proc_file(a1, a2);
if ( !v6 )
return (unsigned int)-1;
v7 = a4 + a3;
*((_DWORD *)v6 + 1) = v7;
return v7;
}

original_code:
size_t lseek(proc_file pft[20], file_nr fd, sint32 offset, uint32 origin)
{
        proc_file *pf = get_proc_file(pft, fd);
        if (pf == 0){
                return -1;
        }
        pf->pf_pos = origin + offset;
        return pf->pf_pos;
}

canonical_original_code:
unsigned int lseek(struct proc_file pft[20], short fd, int offset, unsigned int origin)
{
        struct proc_file *pf = get_proc_file(pft, fd);
        if (pf == 0){
                return -1;
        }
        pf->pf_pos = origin + offset;
        return pf->pf_pos;
}

variable_types: fd short

variable_types: offset int

variable_types:
origin
unsigned int

user_defined_types: struct proc_file { short pf_desc; short pf_f_desc; unsigned int pf_pos; }


decompiled_code:
__int64 __fastcall inode2desc(_WORD *a1)
{
__int64 v1; // rax

v1 = 0LL;
while ( *a1 != *(_WORD *)gft[4 * v1 + 1] )
{
if ( ++v1 == 10 )
return 0xFFFFFFFFLL;
}
return LOWORD(gft[4 * (int)v1]);
}

canonical_decompiled_code:
__int64 __fastcall func7(_WORD *a1)
{
__int64 v1; // rax

v1 = 0LL;
while ( *a1 != *(_WORD *)gft[4 * v1 + 1] )
{
if ( ++v1 == 10 )
return 0xFFFFFFFFLL;
}
return LOWORD(gft[4 * (int)v1]);
}

original_code:
file_nr inode2desc(m_inode *inode)
{
        for (int i = 0; i < 10; i++) {
                if ( inode->i_num == ((m_inode *)(gft[i].f_inode))->i_num ) {
                        return gft[i].f_desc;
                }
        }
        return -1;
}

canonical_original_code:
short inode2desc(struct m_inode *inode)
{
        for (int i = 0; i < 10; i++) {
                if ( inode->i_num == ((struct m_inode *)(gft[i].f_inode))->i_num ) {
                        return gft[i].f_desc;
                }
        }
        return -1;
}

variable_types: struct m_inode { short i_num; unsigned int i_adr; unsigned short i_mode; unsigned int i_size; struct time { unsigned char sec; unsigned char min; unsigned char hour; unsigned char weekday; unsigned char day; unsigned char month; unsigned char year; unsigned char century; } i_create_ts; struct time i_modify_ts; unsigned int i_direct_pointer[30]; unsigned int i_single_indirect_pointer; unsigned int i_double_indirect_pointer; } *


decompiled_code:
bool __fastcall contains_file(__int16 a1)
{
return get_file(a1) != 0LL;
}

canonical_decompiled_code:
bool __fastcall func8(__int16 a1)
{
return get_file(a1) != 0LL;
}

original_code:
bool contains_file(file_nr fd)
{
        return ( get_file(fd) != 0);
}

canonical_original_code:
unsigned char contains_file(short fd)
{
        return ( get_file(fd) != 0);
}

return_type: unsigned char


decompiled_code:
void dump_file()
{
;
}

canonical_decompiled_code:
void func10()
{
;
}

original_code:
void dump_file(file *f)
{
        ((void)0);
}

canonical_original_code:
void dump_file(struct file *f)
{
        ((void)0);
}

user_defined_types: struct time { unsigned char sec; unsigned char min; unsigned char hour; unsigned char weekday; unsigned char day; unsigned char month; unsigned char year; unsigned char century; }


decompiled_code:
void dump_files()
{
;
}

canonical_decompiled_code:
void func11()
{
;
}

original_code:
void dump_files()
{
        ((void)0);
        for (int i = 0; i < 10; i++) {
                if(gft[i].f_desc >= 0)
                        dump_file(&gft[i]);
        }
}


decompiled_code:
void dump_proc_file()
{
;
}

canonical_decompiled_code:
void func12()
{
;
}

original_code:
void dump_proc_file(proc_file *pf)
{
        ((void)0);
}

canonical_original_code:
void dump_proc_file(struct proc_file *pf)
{
        ((void)0);
}


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

LODWORD(result) = 20;
do
result = (unsigned int)(result - 1);
while ( (_DWORD)result );
return result;
}

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

LODWORD(result) = 20;
do
result = (unsigned int)(result - 1);
while ( (_DWORD)result );
return result;
}

original_code:
void dump_proc_files(proc_file pft[20])
{
        ((void)0);
        for (int i = 0; i < 20; i++) {
                dump_proc_file(&pft[i]);
        }
}

canonical_original_code:
void dump_proc_files(struct proc_file pft[20])
{
        ((void)0);
        for (int i = 0; i < 20; i++) {
                dump_proc_file(&pft[i]);
        }
}


decompiled_code:
__int64 write_bmap()
{
__int64 result; // rax
__int64 v1; // rbp
unsigned int v2; // ebx

result = (unsigned int)(num_bmap_blocks + 3);
if ( (unsigned int)num_bmap_blocks < 0xFFFFFFFD && (_DWORD)result != 3 )
{
v1 = 0LL;
v2 = 3;
do
{
wrt_block(v2++, bmap + v1, 512LL);
v1 += 512LL;
result = (unsigned int)(num_bmap_blocks + 3);
}
while ( (unsigned int)result > v2 );
}
return result;
}

canonical_decompiled_code:
__int64 func2()
{
__int64 result; // rax
__int64 v1; // rbp
unsigned int v2; // ebx

result = (unsigned int)(num_bmap_blocks + 3);
if ( (unsigned int)num_bmap_blocks < 0xFFFFFFFD && (_DWORD)result != 3 )
{
v1 = 0LL;
v2 = 3;
do
{
wrt_block(v2++, bmap + v1, 512LL);
v1 += 512LL;
result = (unsigned int)(num_bmap_blocks + 3);
}
while ( (unsigned int)result > v2 );
}
return result;
}

original_code:
void write_bmap()
{
        ((void)0);
        int j = 0;
        for (int i = ((block_nr) 3); i < ((block_nr) 3) + num_bmap_blocks; i++, j++) {
                wrt_block(i, &bmap[j*512], 512);
        }
}

canonical_original_code:
void write_bmap()
{
        ((void)0);
        int j = 0;
        for (int i = ((block_nr) 3); i < ((unsigned int) 3) + num_bmap_blocks; i++, j++) {
                wrt_block(i, &bmap[j*512], 512);
        }
}

variable_types: j int


original_code:
void mark_block(block_nr blk_nr, bool flag)
{
        uint8 byte = bmap[blk_nr / 8];
        uint8 bit = blk_nr % 8;
        byte = (byte >> (8 - bit)) | (byte << bit);
        if (flag == 1) {
                byte = byte | 0x80;
        } else {
                byte = byte & 0x7F;
        }
        byte = (byte >> bit) | (byte << (8 - bit));
        bmap[blk_nr / 8] = byte;
        ((void)0);
        wrt_block(((block_nr) 3) + (blk_nr / 8 / 512), &bmap[(blk_nr / 8 / 512) * 512], 512);
}

canonical_original_code:
void mark_block(unsigned int blk_nr, unsigned char flag)
{
        unsigned char byte = bmap[blk_nr / 8];
        unsigned char bit = blk_nr % 8;
        byte = (byte >> (8 - bit)) | (byte << bit);
        if (flag == 1) {
                byte = byte | 0x80;
        } else {
                byte = byte & 0x7F;
        }
        byte = (byte >> bit) | (byte << (8 - bit));
        bmap[blk_nr / 8] = byte;
        ((void)0);
        wrt_block(((unsigned int) 3) + (blk_nr / 8 / 512), &bmap[(blk_nr / 8 / 512) * 512], 512);
}

variable_types:
flag
unsigned char

variable_types:
byte
unsigned char

variable_types:
bit
unsigned char


decompiled_code:
__int64 __fastcall is_allocated_block(unsigned int a1)
{
return (unsigned int)(*(unsigned __int8 *)(bmap + (a1 >> 3)) << (a1 & 7)) >> 7;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int a1)
{
return (unsigned int)(*(unsigned __int8 *)(bmap + (a1 >> 3)) << (a1 & 7)) >> 7;
}

original_code:
bool is_allocated_block(block_nr blk_nr)
{
        uint8 byte = bmap[blk_nr / 8];
        uint8 bit = blk_nr % 8;
        byte = byte << bit;
        byte = byte & 0x80;
        return (byte != 0);
}

canonical_original_code:
unsigned char is_allocated_block(unsigned int blk_nr)
{
        unsigned char byte = bmap[blk_nr / 8];
        unsigned char bit = blk_nr % 8;
        byte = byte << bit;
        byte = byte & 0x80;
        return (byte != 0);
}


decompiled_code:
__int64 __fastcall alloc_block(unsigned int a1)
{
unsigned int v1; // ebx

v1 = get_free_block(a1);
mark_block(v1, 1);
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned int a1)
{
unsigned int v1; // ebx

v1 = func6(a1);
func3(v1, 1);
return v1;
}

original_code:
block_nr alloc_block(block_nr start)
{
        block_nr blk_nr;
        blk_nr = get_free_block(start);
        mark_block(blk_nr, 1);
        return blk_nr;
}

canonical_original_code:
unsigned int alloc_block(unsigned int start)
{
        unsigned int blk_nr;
        blk_nr = get_free_block(start);
        mark_block(blk_nr, 1);
        return blk_nr;
}

variable_types:
start
unsigned int


decompiled_code:
void test_reload()
{
;
}

canonical_decompiled_code:
void func3()
{
;
}

original_code:
void test_reload()
{
}

canonical_original_code:
void test_reload()
{
}