repo: innodrivers/mflib

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r12
int v4; // eax
int v5; // eax
int v6; // er13
char *v7; // rax
char *v8; // r12
const char *v9; // r14
char *v10; // rbp
char *v11; // rbx
const char *v12; // r14
char *v13; // rbx
const char *v14; // r14
char *v15; // rbp
char *v16; // rbx
const char *v17; // r14
char *v18; // rbp
char *v19; // rbx

progname = (int)basename(*argv);
if ( argc <= 1 )
usage(1);
v3 = argv[1];
input_file = (__int64)v3;
while ( 1 )
{
v4 = getopt(argc, (char *const *)argv, "c:m:a:k:h");
if ( v4 == -1 )
break;
switch ( v4 )
{
case 'a':
atag_addr = (char *)optarg;
break;
case 'c':
cmdline = (char *)optarg;
break;
case 'h':
usage(0);
case 'k':
kernel_addr = (char *)optarg;
break;
case 'm':
mach_id = (char *)optarg;
break;
default:
usage(1);
}
}
if ( cmdline && (int)strlen(cmdline) > 256 )
{
fwrite("Command line string too long\n", 1uLL, 0x1DuLL, stderr);
return -1;
}
v5 = open(v3, 2);
v6 = v5;
if ( v5 < 0 || (v7 = (char *)mmap(0LL, 0x4000uLL, 3, 1, v5, 0LL), v8 = v7, v7 == (char *)-1LL) )
{
fwrite("Could not open input image", 1uLL, 0x1AuLL, stderr);
goto LABEL_46;
}
v9 = cmdline;
if ( cmdline )
{
v10 = v7 + 0x4000;
v11 = v7;
while ( memcmp(v11, "CMDLINE:", 8uLL) )
{
v11 += 4;
if ( v11 == v10 )
{
fwrite("Command line marker not found!\n", 1uLL, 0x1FuLL, stderr);
goto LABEL_21;
}
}
*((_QWORD *)v11 + 1) = 0LL;
*((_QWORD *)v11 + 32) = 0LL;
memset(
(void *)((unsigned __int64)(v11 + 16) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((_DWORD)v11 + 8 - (((_DWORD)v11 + 16) & 0xFFFFFFF8) + 256) >> 3));
strcpy(v11 + 8, v9);
msync(v11 + 8, 0x100uLL, 6);
v12 = mach_id;
if ( mach_id )
{
LABEL_26:
v13 = v8;
while ( memcmp(v13, "MACHID:", 7uLL) )
{
v13 += 4;
if ( v13 >= v10 )
{
fwrite("Machine ID marker not found!\n", 1uLL, 0x1DuLL, stderr);
goto LABEL_30;
}
}
*(_QWORD *)(v13 + 7) = 0LL;
*(_QWORD *)(v13 + 15) = 0LL;
strcpy(v13 + 7, v12);
msync(v13 + 7, 0x10uLL, 6);
v14 = atag_addr;
v15 = v8 + 0x4000;
if ( atag_addr )
{
LABEL_33:
v16 = v8;
while ( memcmp(v16, "ATAGADDR:", 9uLL) )
{
v16 += 4;
if ( v16 >= v15 )
{
fwrite("ATAG Address marker not found!\n", 1uLL, 0x1FuLL, stderr);
goto LABEL_37;
}
}
*(_QWORD *)(v16 + 9) = 0LL;
*(_QWORD *)(v16 + 17) = 0LL;
strcpy(v16 + 9, v14);
msync(v16 + 9, 0x10uLL, 6);
v17 = kernel_addr;
v18 = v8 + 0x4000;
if ( !kernel_addr )
goto LABEL_45;
goto LABEL_40;
}
goto LABEL_37;
}
}
else
{
LABEL_21:
v12 = mach_id;
if ( mach_id )
{
v10 = v8 + 0x4000;
goto LABEL_26;
}
}
LABEL_30:
v14 = atag_addr;
if ( atag_addr )
{
v15 = v8 + 0x4000;
goto LABEL_33;
}
LABEL_37:
v17 = kernel_addr;
if ( kernel_addr )
{
v18 = v8 + 0x4000;
LABEL_40:
v19 = v8;
while ( memcmp(v19, "KERNELADDR:", 0xBuLL) )
{
v19 += 4;
if ( v19 >= v18 )
{
fwrite("Kernel Address marker not found!\n", 1uLL, 0x21uLL, stderr);
goto LABEL_45;
}
}
*(_QWORD *)(v19 + 11) = 0LL;
*(_QWORD *)(v19 + 19) = 0LL;
strcpy(v19 + 11, v17);
msync(v19 + 11, 0x10uLL, 6);
}
LABEL_45:
munmap(v8, 0x4000uLL);
LABEL_46:
if ( v6 > 0 )
close(v6);
return -1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r12
int v4; // eax
int v5; // eax
int v6; // er13
char *v7; // rax
char *v8; // r12
const char *v9; // r14
char *v10; // rbp
char *v11; // rbx
const char *v12; // r14
char *v13; // rbx
const char *v14; // r14
char *v15; // rbp
char *v16; // rbx
const char *v17; // r14
char *v18; // rbp
char *v19; // rbx

progname = (int)basename(*argv);
if ( argc <= 1 )
func7(1);
v3 = argv[1];
input_file = (__int64)v3;
while ( 1 )
{
v4 = getopt(argc, (char *const *)argv, "c:m:a:k:h");
if ( v4 == -1 )
break;
switch ( v4 )
{
case 'a':
atag_addr = (char *)optarg;
break;
case 'c':
cmdline = (char *)optarg;
break;
case 'h':
func7(0);
case 'k':
kernel_addr = (char *)optarg;
break;
case 'm':
mach_id = (char *)optarg;
break;
default:
func7(1);
}
}
if ( cmdline && (int)strlen(cmdline) > 256 )
{
fwrite("Command line string too long\n", 1uLL, 0x1DuLL, stderr);
return -1;
}
v5 = open(v3, 2);
v6 = v5;
if ( v5 < 0 || (v7 = (char *)mmap(0LL, 0x4000uLL, 3, 1, v5, 0LL), v8 = v7, v7 == (char *)-1LL) )
{
fwrite("Could not open input image", 1uLL, 0x1AuLL, stderr);
goto LABEL_46;
}
v9 = cmdline;
if ( cmdline )
{
v10 = v7 + 0x4000;
v11 = v7;
while ( memcmp(v11, "CMDLINE:", 8uLL) )
{
v11 += 4;
if ( v11 == v10 )
{
fwrite("Command line marker not found!\n", 1uLL, 0x1FuLL, stderr);
goto LABEL_21;
}
}
*((_QWORD *)v11 + 1) = 0LL;
*((_QWORD *)v11 + 32) = 0LL;
memset(
(void *)((unsigned __int64)(v11 + 16) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((_DWORD)v11 + 8 - (((_DWORD)v11 + 16) & 0xFFFFFFF8) + 256) >> 3));
strcpy(v11 + 8, v9);
msync(v11 + 8, 0x100uLL, 6);
v12 = mach_id;
if ( mach_id )
{
LABEL_26:
v13 = v8;
while ( memcmp(v13, "MACHID:", 7uLL) )
{
v13 += 4;
if ( v13 >= v10 )
{
fwrite("Machine ID marker not found!\n", 1uLL, 0x1DuLL, stderr);
goto LABEL_30;
}
}
*(_QWORD *)(v13 + 7) = 0LL;
*(_QWORD *)(v13 + 15) = 0LL;
strcpy(v13 + 7, v12);
msync(v13 + 7, 0x10uLL, 6);
v14 = atag_addr;
v15 = v8 + 0x4000;
if ( atag_addr )
{
LABEL_33:
v16 = v8;
while ( memcmp(v16, "ATAGADDR:", 9uLL) )
{
v16 += 4;
if ( v16 >= v15 )
{
fwrite("ATAG Address marker not found!\n", 1uLL, 0x1FuLL, stderr);
goto LABEL_37;
}
}
*(_QWORD *)(v16 + 9) = 0LL;
*(_QWORD *)(v16 + 17) = 0LL;
strcpy(v16 + 9, v14);
msync(v16 + 9, 0x10uLL, 6);
v17 = kernel_addr;
v18 = v8 + 0x4000;
if ( !kernel_addr )
goto LABEL_45;
goto LABEL_40;
}
goto LABEL_37;
}
}
else
{
LABEL_21:
v12 = mach_id;
if ( mach_id )
{
v10 = v8 + 0x4000;
goto LABEL_26;
}
}
LABEL_30:
v14 = atag_addr;
if ( atag_addr )
{
v15 = v8 + 0x4000;
goto LABEL_33;
}
LABEL_37:
v17 = kernel_addr;
if ( kernel_addr )
{
v18 = v8 + 0x4000;
LABEL_40:
v19 = v8;
while ( memcmp(v19, "KERNELADDR:", 0xBuLL) )
{
v19 += 4;
if ( v19 >= v18 )
{
fwrite("Kernel Address marker not found!\n", 1uLL, 0x21uLL, stderr);
goto LABEL_45;
}
}
*(_QWORD *)(v19 + 11) = 0LL;
*(_QWORD *)(v19 + 19) = 0LL;
strcpy(v19 + 11, v17);
msync(v19 + 11, 0x10uLL, 6);
}
LABEL_45:
munmap(v8, 0x4000uLL);
LABEL_46:
if ( v6 > 0 )
close(v6);
return -1;
}

original_code:
int main(int argc, char **argv)
{
 int fd, found = 0, len, ret = -1;
 char *ptr, *p;
 progname = (char*)basename(argv[0]);
 if (argc < 2)
  usage(1);
 input_file = argv[1];
 while (1) {
  int c;
  c = getopt(argc, argv, "c:m:a:k:h");
  if (c == -1)
   break;
  switch (c) {
  case 'c':
   cmdline = optarg;
   break;
  case 'm':
   mach_id = optarg;
   break;
  case 'a':
   atag_addr = optarg;
   break;
  case 'k':
   kernel_addr = optarg;
   break;
  case 'h':
   usage(0);
   break;
  default:
   usage(1);
   break;
  }
 }
 if (cmdline != ((void *)0)) {
  len = strlen(cmdline);
  if (len > 256) {
   fprintf(stderr, "Command line string too long\n");
   goto err1;
  }
 }
 if (((fd = open(input_file, 02)) < 0) ||
  (ptr = (char *) mmap(0, (16 * 1024), 0x1|0x2, 0x01, fd, 0)) == (void *) (-1)) {
  fprintf(stderr, "Could not open input image");
  goto err2;
 }
 if (cmdline != ((void *)0)) {
  found = 0;
  for (p = ptr; p < (ptr + (16 * 1024)); p += 4) {
   if (memcmp(p, "CMDLINE:", 8) == 0) {
    found = 1;
    p += 8;
    break;
   }
  }
  if (found) {
   memset(p, 0, 256);
   strcpy(p, cmdline);
   msync(p, 256, 4 | 2);
  } else {
   fprintf(stderr, "Command line marker not found!\n");
  }
 }
 if (mach_id != ((void *)0)) {
  found = 0;
  for (p = ptr; p < (ptr + (16 * 1024)); p += 4) {
   if (memcmp(p, "MACHID:", 7) == 0) {
    found = 1;
    p += 7;
    break;
   }
  }
  if (!found) {
   fprintf(stderr, "Machine ID marker not found!\n");
  } else {
   memset(p, 0, 16);
   strcpy(p, mach_id);
   msync(p, 16, 4 | 2);
  }
 }
 if (atag_addr != ((void *)0)) {
  found = 0;
  for (p = ptr; p < (ptr + (16 * 1024)); p += 4) {
   if (memcmp(p, "ATAGADDR:", 9) == 0) {
    found = 1;
    p += 9;
    break;
   }
  }
  if (!found) {
   fprintf(stderr, "ATAG Address marker not found!\n");
  } else {
   memset(p, 0, 16);
   strcpy(p, atag_addr);
   msync(p, 16, 4 | 2);
  }
 }
 if (kernel_addr != ((void *)0)) {
  found = 0;
  for (p = ptr; p < (ptr + (16 * 1024)); p += 4) {
   if (memcmp(p, "KERNELADDR:", 11) == 0) {
    found = 1;
    p += 11;
    break;
   }
  }
  if (!found) {
   fprintf(stderr, "Kernel Address marker not found!\n");
  } else {
   memset(p, 0, 16);
   strcpy(p, kernel_addr);
   msync(p, 16, 4 | 2);
  }
 }
 munmap((void *) ptr, (16 * 1024));
err2:
 if (fd > 0)
  close(fd);
err1:
 return ret;
}

canonical_original_code:
int main(int argc, char **argv)
{
 int fd; int found = 0; int len; int ret = -1;
 char *ptr; char *p;
 progname = (char *)basename(argv[0]);
 if (argc < 2)
  usage(1);
 input_file = argv[1];
 while (1) {
  int c;
  c = getopt(argc, argv, "c:m:a:k:h");
  if (c == -1)
   break;
  switch (c) {
  case 'c':
   cmdline = optarg;
   break;
  case 'm':
   mach_id = optarg;
   break;
  case 'a':
   atag_addr = optarg;
   break;
  case 'k':
   kernel_addr = optarg;
   break;
  case 'h':
   usage(0);
   break;
  default:
   usage(1);
   break;
  }
 }
 if (cmdline != ((void *)0)) {
  len = strlen(cmdline);
  if (len > 256) {
   fprintf(stderr, "Command line string too long\n");
   goto err1;
  }
 }
 if (((fd = open(input_file, 02)) < 0) ||
  (ptr = (char *) mmap(0, (16 * 1024), 0x1|0x2, 0x01, fd, 0)) == (void *) (-1)) {
  fprintf(stderr, "Could not open input image");
  goto err2;
 }
 if (cmdline != ((void *)0)) {
  found = 0;
  for (p = ptr; p < (ptr + (16 * 1024)); p += 4) {
   if (memcmp(p, "CMDLINE:", 8) == 0) {
    found = 1;
    p += 8;
    break;
   }
  }
  if (found) {
   memset(p, 0, 256);
   strcpy(p, cmdline);
   msync(p, 256, 4 | 2);
  } else {
   fprintf(stderr, "Command line marker not found!\n");
  }
 }
 if (mach_id != ((void *)0)) {
  found = 0;
  for (p = ptr; p < (ptr + (16 * 1024)); p += 4) {
   if (memcmp(p, "MACHID:", 7) == 0) {
    found = 1;
    p += 7;
    break;
   }
  }
  if (!found) {
   fprintf(stderr, "Machine ID marker not found!\n");
  } else {
   memset(p, 0, 16);
   strcpy(p, mach_id);
   msync(p, 16, 4 | 2);
  }
 }
 if (atag_addr != ((void *)0)) {
  found = 0;
  for (p = ptr; p < (ptr + (16 * 1024)); p += 4) {
   if (memcmp(p, "ATAGADDR:", 9) == 0) {
    found = 1;
    p += 9;
    break;
   }
  }
  if (!found) {
   fprintf(stderr, "ATAG Address marker not found!\n");
  } else {
   memset(p, 0, 16);
   strcpy(p, atag_addr);
   msync(p, 16, 4 | 2);
  }
 }
 if (kernel_addr != ((void *)0)) {
  found = 0;
  for (p = ptr; p < (ptr + (16 * 1024)); p += 4) {
   if (memcmp(p, "KERNELADDR:", 11) == 0) {
    found = 1;
    p += 11;
    break;
   }
  }
  if (!found) {
   fprintf(stderr, "Kernel Address marker not found!\n");
  } else {
   memset(p, 0, 16);
   strcpy(p, kernel_addr);
   msync(p, 16, 4 | 2);
  }
 }
 munmap((void *) ptr, (16 * 1024));
err2:
 if (fd > 0)
  close(fd);
err1:
 return ret;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: fd int

variable_types: found int

variable_types: len int

variable_types: ret int

variable_types:
ptr
char *

variable_types:
p
char *


repo: inorindesu/mustached-octo-hipster

original_code:
int move_to(const FT_Vector* to, void* user)
{
  VecData* vec = (VecData*)user;
  fprintf(stderr, "MOVE_TO: (%d, %d)\n", to->x, vec->yHigh - to->y);
  printf(" M %d %d ", to->x, vec->yHigh - to->y);
  return 0;
}

canonical_original_code:
int move_to(struct FT_Vector_ *to, void *user)
{
  struct VecData_ *vec = (VecData*)user;
  fprintf(stderr, "MOVE_TO: (%d, %d)\n", to->x, vec->yHigh - to->y);
  printf(" M %d %d ", to->x, vec->yHigh - to->y);
  return 0;
}

variable_types:
to
struct FT_Vector_ { long x; long y; } *

variable_types:
user
void *

variable_types:
vec
struct VecData_ { int xLow; } *

return_type: int

user_defined_types:
struct FT_Vector_ { long x; long y; }
struct VecData_ { int xLow; }


original_code:
int line_to(const FT_Vector* to, void* user)
{
  VecData* vec = (VecData*)user;
  fprintf(stderr, "LINE_TO: (%d, %d)\n", to->x, vec->yHigh - to->y);
  printf(" L %d %d ", to->x, vec->yHigh - to->y);
  return 0;
}

canonical_original_code:
int line_to(struct FT_Vector_ *to, void *user)
{
  struct VecData_ *vec = (VecData*)user;
  fprintf(stderr, "LINE_TO: (%d, %d)\n", to->x, vec->yHigh - to->y);
  printf(" L %d %d ", to->x, vec->yHigh - to->y);
  return 0;
}


original_code:
int conic_to(const FT_Vector* ctrl, const FT_Vector* to, void* user)
{
  VecData* vec = (VecData*)user;
  fprintf(stderr, "CONIC_TO: *(%d, %d) (%d, %d)\n", ctrl->x, vec->yHigh - ctrl->y, to->x, vec->yHigh - to->y);
  printf(" Q %d %d %d %d ", ctrl->x, vec->yHigh - ctrl->y, to->x, vec->yHigh - to->y);
  return 0;
}

canonical_original_code:
int conic_to(struct FT_Vector_ *ctrl, struct FT_Vector_ *to, void *user)
{
  struct VecData_ *vec = (VecData*)user;
  fprintf(stderr, "CONIC_TO: *(%d, %d) (%d, %d)\n", ctrl->x, vec->yHigh - ctrl->y, to->x, vec->yHigh - to->y);
  printf(" Q %d %d %d %d ", ctrl->x, vec->yHigh - ctrl->y, to->x, vec->yHigh - to->y);
  return 0;
}

variable_types:
ctrl
struct FT_Vector_ { long x; long y; } *

variable_types:
to
struct FT_Vector_ *


original_code:
int cubic_to(const FT_Vector* ctrl1, const FT_Vector* ctrl2, const FT_Vector* to,
             void* user)
{
  VecData* vec = (VecData*)user;
  fprintf(stderr, "CUBIC_TO: *(%d, %d)*(%d, %d) (%d, %d)\n", ctrl1->x, vec->yHigh - ctrl1->y
         , ctrl2->x, vec->yHigh - ctrl2->y, to->x, vec->yHigh - to->y);
  printf(" C %d %d %d %d ", ctrl1->x, vec->yHigh - ctrl1->y, ctrl2->x, vec->yHigh - ctrl2->y, to->x
         , vec->yHigh - to->y);
  return 0;
}

canonical_original_code:
int cubic_to(struct FT_Vector_ *ctrl1, struct FT_Vector_ *ctrl2, struct FT_Vector_ *to, void *user)
{
  struct VecData_ *vec = (VecData*)user;
  fprintf(stderr, "CUBIC_TO: *(%d, %d)*(%d, %d) (%d, %d)\n", ctrl1->x, vec->yHigh - ctrl1->y
         , ctrl2->x, vec->yHigh - ctrl2->y, to->x, vec->yHigh - to->y);
  printf(" C %d %d %d %d ", ctrl1->x, vec->yHigh - ctrl1->y, ctrl2->x, vec->yHigh - ctrl2->y, to->x
         , vec->yHigh - to->y);
  return 0;
}


decompiled_code:
__int64 __fastcall update_vector(_QWORD *a1, int *a2)
{
__int64 v2; // rax
__int64 result; // rax

if ( *a1 > a2[1] )
a2[1] = *a1;
if ( *a1 < *a2 )
*a2 = *a1;
v2 = a1[1];
if ( v2 > a2[3] )
a2[3] = v2;
result = a1[1];
if ( result < a2[2] )
a2[2] = result;
return result;
}

original_code:
void update_vector(const FT_Vector* src, VecData* data)
{
  if (src->x > data->xHigh)
    data->xHigh = src->x;
  if (src->x < data->xLow)
    data->xLow = src->x;
  if (src->y > data->yHigh)
    data->yHigh = src->y;
  if (src->y < data->yLow)
    data->yLow = src->y;
}

canonical_original_code:
void update_vector(struct FT_Vector_ *src, struct VecData_ *data)
{
  if (src->x > data->xHigh)
    data->xHigh = src->x;
  if (src->x < data->xLow)
    data->xLow = src->x;
  if (src->y > data->yHigh)
    data->yHigh = src->y;
  if (src->y < data->yLow)
    data->yLow = src->y;
}

variable_types:
src
struct FT_Vector_ { long x; long y; } *

variable_types:
data
struct VecData_ { int xLow; } *

return_type: void


decompiled_code:
__int64 __fastcall test_move_to(_QWORD *a1, int *a2)
{
update_vector(a1, a2);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func12(_QWORD *a1, int *a2)
{
func11(a1, a2);
return 0LL;
}

original_code:
int test_move_to(const FT_Vector* to, void* user)
{
  update_vector(to, (VecData*)user);
  return 0;
}

canonical_original_code:
int test_move_to(struct FT_Vector_ *to, void *user)
{
  update_vector(to, (struct VecData_ *)user);
  return 0;
}


decompiled_code:
__int64 __fastcall test_line_to(_QWORD *a1, int *a2)
{
update_vector(a1, a2);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func13(_QWORD *a1, int *a2)
{
func11(a1, a2);
return 0LL;
}

original_code:
int test_line_to(const FT_Vector* to, void* user)
{
  update_vector(to, (VecData*)user);
  return 0;
}

canonical_original_code:
int test_line_to(struct FT_Vector_ *to, void *user)
{
  update_vector(to, (struct VecData_ *)user);
  return 0;
}


decompiled_code:
__int64 __fastcall test_conic_to(_QWORD *a1, _QWORD *a2, int *a3)
{
update_vector(a1, a3);
update_vector(a2, a3);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func14(_QWORD *a1, _QWORD *a2, int *a3)
{
func11(a1, a3);
func11(a2, a3);
return 0LL;
}

original_code:
int test_conic_to(const FT_Vector* ctrl, const FT_Vector* to, void* user)
{
  update_vector(ctrl, (VecData*)user);
  update_vector(to, (VecData*)user);
  return 0;
}

canonical_original_code:
int test_conic_to(struct FT_Vector_ *ctrl, struct FT_Vector_ *to, void *user)
{
  update_vector(ctrl, (struct VecData_ *)user);
  update_vector(to, (struct VecData_ *)user);
  return 0;
}


decompiled_code:
__int64 __fastcall test_cubic_to(_QWORD *a1, _QWORD *a2, _QWORD *a3, int *a4)
{
update_vector(a1, a4);
update_vector(a2, a4);
update_vector(a3, a4);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func15(_QWORD *a1, _QWORD *a2, _QWORD *a3, int *a4)
{
func11(a1, a4);
func11(a2, a4);
func11(a3, a4);
return 0LL;
}

original_code:
int test_cubic_to(const FT_Vector* ctrl1, const FT_Vector* ctrl2, const FT_Vector* to,
             void* user)
{
  update_vector(ctrl1, (VecData*)user);
  update_vector(ctrl2, (VecData*)user);
  update_vector(to, (VecData*)user);
  return 0;
}

canonical_original_code:
int test_cubic_to(struct FT_Vector_ *ctrl1, struct FT_Vector_ *ctrl2, struct FT_Vector_ *to, void *user)
{
  update_vector(ctrl1, (struct VecData_ *)user);
  update_vector(ctrl2, (struct VecData_ *)user);
  update_vector(to, (struct VecData_ *)user);
  return 0;
}


original_code:
unsigned int utf8_to_utf32(char* str, char** next)
{
  char leader = str[0];
  unsigned int ret = 0;
  char* nextBuf = ((void *)0);
  if ((leader & 0xF0) == 0xF0)
    {
      ret += (str[0] & 0x07) << 18;
      ret += (str[1] & 0x3F) << 12;
      ret += (str[2] & 0x3F) << 6;
      ret += (str[3] & 0x3F);
      nextBuf = &str[4];
    }
  else if ((leader & 0xE0) == 0xE0)
    {
      ret += (str[0] & 0x0F) << 12;
      ret += (str[1] & 0x3F) << 6;
      ret += (str[2] & 0x3F);
      nextBuf = &str[3];
    }
  else if ((leader & 0xC0) == 0xC0)
    {
      ret += (str[0] & 0x1F) << 6;
      ret += (str[1] & 0x3F);
      nextBuf = &str[2];
    }
  else
    {
      ret = str[0];
      nextBuf = &str[1];
    }
  if (next != ((void *)0))
    *next = nextBuf;
  return ret;
}

canonical_original_code:
unsigned int utf8_to_utf32(char *str, char **next)
{
  char leader = str[0];
  unsigned int ret = 0;
  char *nextBuf = ((void *)0);
  if ((leader & 0xF0) == 0xF0)
    {
      ret += (str[0] & 0x07) << 18;
      ret += (str[1] & 0x3F) << 12;
      ret += (str[2] & 0x3F) << 6;
      ret += (str[3] & 0x3F);
      nextBuf = &str[4];
    }
  else if ((leader & 0xE0) == 0xE0)
    {
      ret += (str[0] & 0x0F) << 12;
      ret += (str[1] & 0x3F) << 6;
      ret += (str[2] & 0x3F);
      nextBuf = &str[3];
    }
  else if ((leader & 0xC0) == 0xC0)
    {
      ret += (str[0] & 0x1F) << 6;
      ret += (str[1] & 0x3F);
      nextBuf = &str[2];
    }
  else
    {
      ret = str[0];
      nextBuf = &str[1];
    }
  if (next != ((void *)0))
    *next = nextBuf;
  return ret;
}

variable_types:
str
char *

variable_types:
next
char **

variable_types: leader char

variable_types:
ret
unsigned int

variable_types:
nextBuf
char *

return_type: unsigned int


repo: inorindesu/pango-itemizer

decompiled_code:
void print_header()
{
;
}

canonical_decompiled_code:
void func8()
{
;
}

original_code:
void print_header()
{
}

canonical_original_code:
void print_header()
{
}


repo: inovex/vnc-reflector

decompiled_code:
size_t __fastcall report_usage(const char *a1)
{
fprintf(stderr, "VNC Reflector %s. Copyright (C) 2001-2003 HorizonLive.com, Inc.\n\n", "1.2.4");
fprintf(stderr, "Usage: %s [OPTIONS...] HOST_INFO_FILE\n\n", a1);
fwrite(
"Options:\n"
" -i PID_FILE - write pid file, appending listening port to the filename\n"
" -p PASSWD_FILE - read a plaintext client password file [default: passwd]\n"
" -a ACTIVE_FILE - create file during times when a host is connected\n"
" -c ACTIONS_FILE - on events, execute commands specified in a file\n"
" -l LISTEN_PORT - port to listen for client connections [default: 5999]\n"
" -b IP_ADDRESS - bind listening sockets to a specific IP [default: any]\n",
1uLL,
0x1C1uLL,
stderr);
fwrite(
" -s FBS_PREFIX - save host sessions in rfbproxy-compatible files\n"
" (optionally appending 3-digit session IDs to the\n"
" filename prefix, only if used without the -j option)\n"
" -j - join saved sessions (see -s option) in one session file\n"
" -t - use Tight encoding for host communications if possible\n"
" -T COMPR_LEVEL - like -t, but use the specified compression level (1..9)\n",
1uLL,
0x1B5uLL,
stderr);
fprintf(
stderr,
" -g LOG_FILE - write logs to the specified file [default: reflector.log]\n"
" -v LOG_LEVEL - set verbosity level for the log file (0..%d) [default: %d]\n"
" -f LOG_LEVEL - run in foreground, show logs on stderr at the specified\n"
" verbosity level (0..%d) [note: use %d for normal output]\n"
" -q - suppress printing copyright banner at startup\n"
" -h - print this help message\n"
"\n",
6LL,
4LL,
6LL,
3LL);
return fwrite(
"Please refer to the README file for a description of the file formats for\n"
" HOST_INFO_FILE and PASSWD_FILE files mentioned above in this help text.\n"
"\n",
1uLL,
0x95uLL,
stderr);
}

canonical_decompiled_code:
size_t __fastcall func0(const char *a1)
{
fprintf(stderr, "VNC Reflector %s. Copyright (C) 2001-2003 HorizonLive.com, Inc.\n\n", "1.2.4");
fprintf(stderr, "Usage: %s [OPTIONS...] HOST_INFO_FILE\n\n", a1);
fwrite(
"Options:\n"
" -i PID_FILE - write pid file, appending listening port to the filename\n"
" -p PASSWD_FILE - read a plaintext client password file [default: passwd]\n"
" -a ACTIVE_FILE - create file during times when a host is connected\n"
" -c ACTIONS_FILE - on events, execute commands specified in a file\n"
" -l LISTEN_PORT - port to listen for client connections [default: 5999]\n"
" -b IP_ADDRESS - bind listening sockets to a specific IP [default: any]\n",
1uLL,
0x1C1uLL,
stderr);
fwrite(
" -s FBS_PREFIX - save host sessions in rfbproxy-compatible files\n"
" (optionally appending 3-digit session IDs to the\n"
" filename prefix, only if used without the -j option)\n"
" -j - join saved sessions (see -s option) in one session file\n"
" -t - use Tight encoding for host communications if possible\n"
" -T COMPR_LEVEL - like -t, but use the specified compression level (1..9)\n",
1uLL,
0x1B5uLL,
stderr);
fprintf(
stderr,
" -g LOG_FILE - write logs to the specified file [default: reflector.log]\n"
" -v LOG_LEVEL - set verbosity level for the log file (0..%d) [default: %d]\n"
" -f LOG_LEVEL - run in foreground, show logs on stderr at the specified\n"
" verbosity level (0..%d) [note: use %d for normal output]\n"
" -q - suppress printing copyright banner at startup\n"
" -h - print this help message\n"
"\n",
6LL,
4LL,
6LL,
3LL);
return fwrite(
"Please refer to the README file for a description of the file formats for\n"
" HOST_INFO_FILE and PASSWD_FILE files mentioned above in this help text.\n"
"\n",
1uLL,
0x95uLL,
stderr);
}

original_code:
static void report_usage(char *program_name)
{
  fprintf(stderr,
          "VNC Reflector %s.  Copyright (C) 2001-2003 HorizonLive.com, Inc."
          "\n\n",
          "1.2.4");
  fprintf(stderr, "Usage: %s [OPTIONS...] HOST_INFO_FILE\n\n",
          program_name);
  fprintf(stderr,
          "Options:\n"
          "  -i PID_FILE     - write pid file, appending listening port"
          " to the filename\n"
          "  -p PASSWD_FILE  - read a plaintext client password file"
          " [default: passwd]\n"
          "  -a ACTIVE_FILE  - create file during times when a host is"
          " connected\n"
          "  -c ACTIONS_FILE - on events, execute commands specified in"
          " a file\n"
          "  -l LISTEN_PORT  - port to listen for client connections"
          " [default: 5999]\n"
          "  -b IP_ADDRESS   - bind listening sockets to a specific IP"
          " [default: any]\n");
  fprintf(stderr,
          "  -s FBS_PREFIX   - save host sessions in rfbproxy-compatible"
          " files\n"
          "                    (optionally appending 3-digit session IDs"
          " to the\n"
          "                    filename prefix, only if used without the"
          " -j option)\n"
          "  -j              - join saved sessions (see -s option) in one"
          " session file\n"
          "  -t              - use Tight encoding for host communications"
          " if possible\n"
          "  -T COMPR_LEVEL  - like -t, but use the specified compression"
          " level (1..9)\n");
  fprintf(stderr,
          "  -g LOG_FILE     - write logs to the specified file"
          " [default: reflector.log]\n"
          "  -v LOG_LEVEL    - set verbosity level for the log file (0..%d)"
          " [default: %d]\n"
          "  -f LOG_LEVEL    - run in foreground, show logs on stderr"
          " at the specified\n"
          "                    verbosity level (0..%d) [note: use %d for"
          " normal output]\n"
          "  -q              - suppress printing copyright banner at startup\n"
          "  -h              - print this help message\n\n",
          6, 4, 6, 3);
  fprintf(stderr,
          "Please refer to the README file for a description of the file"
          " formats for\n"
          "  HOST_INFO_FILE and PASSWD_FILE files mentioned above in this"
          " help text.\n\n");
}


decompiled_code:
char *__fastcall parse_args(int argc, char **argv)
{
char *v2; // r12
int v3; // eax
int v4; // eax
char *result; // rax
char v6[56]; // [rsp+0h] [rbp-38h] BYREF

opt_no_banner = 0;
opt_foreground = 0;
opt_stderr_loglevel = -1;
opt_file_loglevel = -1;
opt_passwd_filename = 0LL;
opt_log_filename = 0LL;
opt_active_filename = 0LL;
opt_cl_listen_port = -1;
opt_pid_file = 0;
opt_fbs_prefix = 0LL;
opt_join_sessions = 0;
opt_bind_ip = 0LL;
opt_request_tight = 0;
opt_tight_level = -1;
v2 = 0LL;
while ( 1 )
{
v3 = getopt(argc, argv, "hqjv:f:p:a:c:g:l:i:s:b:tT:");
if ( v3 == -1 )
break;
switch ( v3 )
{
case 'T':
if ( opt_request_tight )
goto LABEL_20;
opt_request_tight = 1;
opt_tight_level = atoi(optarg);
if ( (unsigned int)(opt_tight_level - 1) > 8 )
goto LABEL_20;
continue;
case 'a':
if ( opt_active_filename )
goto LABEL_20;
opt_active_filename = (__int64)optarg;
break;
case 'b':
if ( opt_bind_ip )
goto LABEL_20;
opt_bind_ip = (__int64)optarg;
break;
case 'c':
if ( opt_actions_filename )
goto LABEL_20;
opt_actions_filename = (struct aioinit *)optarg;
break;
case 'f':
opt_foreground = 1;
if ( opt_stderr_loglevel != -1 )
goto LABEL_20;
opt_stderr_loglevel = atoi(optarg);
break;
case 'g':
if ( opt_log_filename )
goto LABEL_20;
opt_log_filename = (__int64)optarg;
break;
case 'i':
if ( v2 )
goto LABEL_20;
v2 = optarg;
break;
case 'j':
opt_join_sessions = 1;
continue;
case 'l':
if ( opt_cl_listen_port != -1 )
goto LABEL_20;
opt_cl_listen_port = atoi(optarg);
if ( opt_cl_listen_port <= 0 )
goto LABEL_20;
continue;
case 'p':
if ( opt_passwd_filename )
goto LABEL_20;
opt_passwd_filename = optarg;
break;
case 'q':
opt_no_banner = 1;
continue;
case 's':
if ( opt_fbs_prefix )
goto LABEL_20;
opt_fbs_prefix = (__int64)optarg;
break;
case 't':
if ( opt_request_tight )
goto LABEL_20;
opt_request_tight = 1;
break;
case 'v':
if ( opt_file_loglevel != -1 )
goto LABEL_20;
opt_file_loglevel = atoi(optarg);
break;
default:
goto LABEL_20;
}
}
if ( argc - 1 != optind )
{
LABEL_20:
report_usage(*argv);
exit(1);
}
if ( opt_file_loglevel == -1 )
opt_file_loglevel = 4;
if ( !opt_passwd_filename )
opt_passwd_filename = "passwd";
if ( !opt_log_filename )
opt_log_filename = (__int64)"reflector.log";
if ( opt_cl_listen_port == -1 )
opt_cl_listen_port = 5999;
if ( v2 )
{
sprintf(v6, "%d", (unsigned int)opt_cl_listen_port);
v4 = strlen(v6);
sprintf(&opt_pid_file, "%.*s.%s", 254 - v4, v2, v6);
}
result = argv[optind];
opt_host_info_file = (__int64)result;
return result;
}

canonical_decompiled_code:
char *__fastcall func1(int argc, char **argv)
{
char *v2; // r12
int v3; // eax
int v4; // eax
char *result; // rax
char v6[56]; // [rsp+0h] [rbp-38h] BYREF

opt_no_banner = 0;
opt_foreground = 0;
opt_stderr_loglevel = -1;
opt_file_loglevel = -1;
opt_passwd_filename = 0LL;
opt_log_filename = 0LL;
opt_active_filename = 0LL;
opt_cl_listen_port = -1;
opt_pid_file = 0;
opt_fbs_prefix = 0LL;
opt_join_sessions = 0;
opt_bind_ip = 0LL;
opt_request_tight = 0;
opt_tight_level = -1;
v2 = 0LL;
while ( 1 )
{
v3 = getopt(argc, argv, "hqjv:f:p:a:c:g:l:i:s:b:tT:");
if ( v3 == -1 )
break;
switch ( v3 )
{
case 'T':
if ( opt_request_tight )
goto LABEL_20;
opt_request_tight = 1;
opt_tight_level = atoi(optarg);
if ( (unsigned int)(opt_tight_level - 1) > 8 )
goto LABEL_20;
continue;
case 'a':
if ( opt_active_filename )
goto LABEL_20;
opt_active_filename = (__int64)optarg;
break;
case 'b':
if ( opt_bind_ip )
goto LABEL_20;
opt_bind_ip = (__int64)optarg;
break;
case 'c':
if ( opt_actions_filename )
goto LABEL_20;
opt_actions_filename = (struct aioinit *)optarg;
break;
case 'f':
opt_foreground = 1;
if ( opt_stderr_loglevel != -1 )
goto LABEL_20;
opt_stderr_loglevel = atoi(optarg);
break;
case 'g':
if ( opt_log_filename )
goto LABEL_20;
opt_log_filename = (__int64)optarg;
break;
case 'i':
if ( v2 )
goto LABEL_20;
v2 = optarg;
break;
case 'j':
opt_join_sessions = 1;
continue;
case 'l':
if ( opt_cl_listen_port != -1 )
goto LABEL_20;
opt_cl_listen_port = atoi(optarg);
if ( opt_cl_listen_port <= 0 )
goto LABEL_20;
continue;
case 'p':
if ( opt_passwd_filename )
goto LABEL_20;
opt_passwd_filename = optarg;
break;
case 'q':
opt_no_banner = 1;
continue;
case 's':
if ( opt_fbs_prefix )
goto LABEL_20;
opt_fbs_prefix = (__int64)optarg;
break;
case 't':
if ( opt_request_tight )
goto LABEL_20;
opt_request_tight = 1;
break;
case 'v':
if ( opt_file_loglevel != -1 )
goto LABEL_20;
opt_file_loglevel = atoi(optarg);
break;
default:
goto LABEL_20;
}
}
if ( argc - 1 != optind )
{
LABEL_20:
func0(*argv);
exit(1);
}
if ( opt_file_loglevel == -1 )
opt_file_loglevel = 4;
if ( !opt_passwd_filename )
opt_passwd_filename = "passwd";
if ( !opt_log_filename )
opt_log_filename = (__int64)"reflector.log";
if ( opt_cl_listen_port == -1 )
opt_cl_listen_port = 5999;
if ( v2 )
{
sprintf(v6, "%d", (unsigned int)opt_cl_listen_port);
v4 = strlen(v6);
sprintf(&opt_pid_file, "%.*s.%s", 254 - v4, v2, v6);
}
result = argv[optind];
opt_host_info_file = (__int64)result;
return result;
}

original_code:
static void parse_args(int argc, char **argv)
{
  int err = 0;
  int c;
  char *temp_pid_file = ((void *)0);
  char temp_buf[32];
  opt_no_banner = 0;
  opt_foreground = 0;
  opt_stderr_loglevel = -1;
  opt_file_loglevel = -1;
  opt_passwd_filename = ((void *)0);
  opt_log_filename = ((void *)0);
  opt_active_filename = ((void *)0);
  opt_cl_listen_port = -1;
  opt_pid_file[0] = '\0';
  opt_fbs_prefix = ((void *)0);
  opt_join_sessions = 0;
  opt_bind_ip = ((void *)0);
  opt_request_tight = 0;
  opt_tight_level = -1;
  while (!err &&
         (c = getopt(argc, argv, "hqjv:f:p:a:c:g:l:i:s:b:tT:")) != -1) {
    switch (c) {
    case 'h':
      err = 1;
      break;
    case 'q':
      opt_no_banner = 1;
      break;
    case 'j':
      opt_join_sessions = 1;
      break;
    case 'v':
      if (opt_file_loglevel != -1)
        err = 1;
      else
        opt_file_loglevel = atoi(optarg);
      break;
    case 'f':
      opt_foreground = 1;
      if (opt_stderr_loglevel != -1)
        err = 1;
      else
        opt_stderr_loglevel = atoi(optarg);
      break;
    case 'p':
      if (opt_passwd_filename != ((void *)0))
        err = 1;
      else
        opt_passwd_filename = optarg;
      break;
    case 'g':
      if (opt_log_filename != ((void *)0))
        err = 1;
      else
        opt_log_filename = optarg;
      break;
    case 'a':
      if (opt_active_filename != ((void *)0))
        err = 1;
      else
        opt_active_filename = optarg;
      break;
    case 'c':
      if (opt_actions_filename != ((void *)0))
        err = 1;
      else
        opt_actions_filename = optarg;
      break;
    case 'l':
      if (opt_cl_listen_port != -1)
        err = 1;
      else {
        opt_cl_listen_port = atoi(optarg);
        if (opt_cl_listen_port <= 0)
          err = 1;
      }
      break;
    case 'i':
      if (temp_pid_file != ((void *)0))
        err = 1;
      else
        temp_pid_file = optarg;
      break;
    case 's':
      if (opt_fbs_prefix != ((void *)0))
        err = 1;
      else
        opt_fbs_prefix = optarg;
      break;
    case 'b':
      if (opt_bind_ip != ((void *)0))
        err = 1;
      else
        opt_bind_ip = optarg;
      break;
    case 't':
      if (opt_request_tight)
        err = 1;
      else
        opt_request_tight = 1;
      break;
    case 'T':
      if (opt_request_tight) {
        err = 1;
      } else {
        opt_request_tight = 1;
        opt_tight_level = atoi(optarg);
        if (opt_tight_level <= 0 || opt_tight_level > 9)
          err = 1;
      }
      break;
    default:
      err = 1;
    }
  }
  if (err || optind != argc - 1) {
    report_usage(argv[0]);
    exit(1);
  }
  if (opt_file_loglevel == -1)
    opt_file_loglevel = 4;
  if (opt_passwd_filename == ((void *)0))
    opt_passwd_filename = "passwd";
  if (opt_log_filename == ((void *)0))
    opt_log_filename = "reflector.log";
  if (opt_cl_listen_port == -1)
    opt_cl_listen_port = 5999;
  if (temp_pid_file != ((void *)0)) {
    sprintf(temp_buf, "%d", opt_cl_listen_port);
    sprintf(opt_pid_file, "%.*s.%s", (int)(255 - strlen(temp_buf) - 1),
            temp_pid_file, temp_buf);
  }
  opt_host_info_file = argv[optind];
}

variable_types: argc int

variable_types:
argv
char **

variable_types: err int

variable_types: c int


decompiled_code:
__int64 write_pid_file()
{
__int64 result; // rax
int v1; // ebx
unsigned int v2; // eax
size_t v3; // rbp
char v4[56]; // [rsp-38h] [rbp-38h] BYREF

result = 1LL;
if ( opt_pid_file )
{
v1 = open(&opt_pid_file, 193, 420LL);
if ( v1 == -1 )
{
log_write(1LL, "Pid file exists, another instance may be running");
result = 0LL;
}
else
{
v2 = getpid();
sprintf(v4, "%d\n", v2);
v3 = (int)strlen(v4);
if ( write(v1, v4, v3) == v3 )
{
log_write(6LL, "Wrote pid file: %s", &opt_pid_file);
close(v1);
result = 1LL;
}
else
{
close(v1);
log_write(1LL, "Error writing to pid file");
result = 0LL;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 func4()
{
__int64 result; // rax
int v1; // ebx
unsigned int v2; // eax
size_t v3; // rbp
char v4[56]; // [rsp-38h] [rbp-38h] BYREF

result = 1LL;
if ( opt_pid_file )
{
v1 = open(&opt_pid_file, 193, 420LL);
if ( v1 == -1 )
{
log_write(1LL, "Pid file exists, another instance may be running");
result = 0LL;
}
else
{
v2 = getpid();
sprintf(v4, "%d\n", v2);
v3 = (int)strlen(v4);
if ( write(v1, v4, v3) == v3 )
{
log_write(6LL, "Wrote pid file: %s", &opt_pid_file);
close(v1);
result = 1LL;
}
else
{
close(v1);
log_write(1LL, "Error writing to pid file");
result = 0LL;
}
}
}
return result;
}

original_code:
static int write_pid_file(void)
{
  int pid_fd, len;
  char buf[32];
  if (opt_pid_file[0] == '\0')
    return 1;
  pid_fd = open(opt_pid_file, 01 | 0100 | 0200, 0644);
  if (pid_fd == -1) {
    log_write(1, "Pid file exists, another instance may be running");
    return 0;
  }
  sprintf(buf, "%d\n", (int)getpid());
  len = strlen(buf);
  if (write(pid_fd, buf, len) != len) {
    close(pid_fd);
    log_write(1, "Error writing to pid file");
    return 0;
  }
  log_write(6, "Wrote pid file: %s", opt_pid_file);
  close(pid_fd);
  return 1;
}

canonical_original_code:
static int write_pid_file()
{
  int pid_fd; int len;
  char buf[32];
  if (opt_pid_file[0] == '\0')
    return 1;
  pid_fd = open(opt_pid_file, 01 | 0100 | 0200, 0644);
  if (pid_fd == -1) {
    log_write(1, "Pid file exists, another instance may be running");
    return 0;
  }
  sprintf(buf, "%d\n", (int)getpid());
  len = strlen(buf);
  if (write(pid_fd, buf, len) != len) {
    close(pid_fd);
    log_write(1, "Error writing to pid file");
    return 0;
  }
  log_write(6, "Wrote pid file: %s", opt_pid_file);
  close(pid_fd);
  return 1;
}

variable_types: len int

variable_types:
buf
char [32]


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rdx
const struct aioinit *v5; // rdi
void *v6; // rdi
__int64 v7; // [rsp+0h] [rbp-18h] BYREF
__int64 v8; // [rsp+8h] [rbp-10h] BYREF

parse_args(argc, (char **)argv);
if ( !opt_no_banner )
fprintf(
stderr,
"VNC Reflector %s. Copyright (C) 2001-2003 HorizonLive.com, Inc.\n"
"\n"
"HorizonLive provides e-Learning and collaborative synchronous presentation\n"
"solutions in a totally Web-based environment. For more information about\n"
"HorizonLive, please see our website at http://www.horizonlive.com/\n"
"\n",
"1.2.4");
v3 = 0xFFFFFFFFLL;
if ( opt_foreground )
v3 = (unsigned int)opt_stderr_loglevel;
if ( !(unsigned int)log_open(opt_log_filename, (unsigned int)opt_file_loglevel, v3) )
fprintf(stderr, "%s: error opening log file (ignoring this error)\n", *argv);
log_write(3LL, "Starting VNC Reflector %s", "1.2.4");
if ( opt_foreground )
{
LABEL_15:
if ( (unsigned int)init_screen_info() )
{
read_password_file();
set_host_encodings((unsigned int)opt_request_tight, (unsigned int)opt_tight_level);
set_client_passwords(opt_client_password, opt_client_ro_password);
fbs_set_prefix(opt_fbs_prefix, (unsigned int)opt_join_sessions);
set_active_file(opt_active_filename);
v5 = opt_actions_filename;
set_actions_file(opt_actions_filename);
aio_init(v5);
if ( opt_bind_ip )
{
if ( (unsigned int)aio_set_bind_address() )
log_write(4LL, "Would bind listening sockets to address %s", (const char *)opt_bind_ip);
else
log_write(2LL, "Illegal address to bind listening sockets to: %s", (const char *)opt_bind_ip);
}
if ( (unsigned int)connect_to_host(opt_host_info_file, (unsigned int)opt_cl_listen_port)
&& (unsigned int)write_pid_file() )
{
set_control_signals();
aio_mainloop();
remove_pid_file();
}
if ( g_framebuffer )
{
log_write(5LL, "Freeing framebuffer and associated structures");
free(g_framebuffer);
free_enc_cache();
}
v6 = (void *)*((_QWORD *)&g_screen_info + 3);
if ( v6 )
free(v6);
get_hextile_caching_stats(&v8, &v7);
if ( v7 + v8 )
log_write(
4LL,
"Hextile BGR233 caching efficiency: %d%%",
(unsigned int)((100 * v8 + (v7 + v8) / 2) / (v7 + v8)));
}
log_write(3LL, "Terminating");
if ( !(unsigned int)log_close() && opt_foreground )
fprintf(stderr, "%s: error closing log file (ignoring this error)\n", *argv);
exit(1);
}
if ( !opt_no_banner )
fwrite("Starting in the background, see the log file for errors and other messages.\n", 1uLL, 0x4CuLL, stderr);
if ( getpid() == 1 )
{
LABEL_14:
close(0);
close(1);
close(2);
log_write(4LL, "Switched to the background mode");
goto LABEL_15;
}
signal(21, handler);
signal(22, handler);
signal(20, handler);
if ( !fork() )
{
setsid();
goto LABEL_14;
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rdx
const struct aioinit *v5; // rdi
void *v6; // rdi
__int64 v7; // [rsp+0h] [rbp-18h] BYREF
__int64 v8; // [rsp+8h] [rbp-10h] BYREF

func1(argc, (char **)argv);
if ( !opt_no_banner )
fprintf(
stderr,
"VNC Reflector %s. Copyright (C) 2001-2003 HorizonLive.com, Inc.\n"
"\n"
"HorizonLive provides e-Learning and collaborative synchronous presentation\n"
"solutions in a totally Web-based environment. For more information about\n"
"HorizonLive, please see our website at http://www.horizonlive.com/\n"
"\n",
"1.2.4");
v3 = 0xFFFFFFFFLL;
if ( opt_foreground )
v3 = (unsigned int)opt_stderr_loglevel;
if ( !(unsigned int)log_open(opt_log_filename, (unsigned int)opt_file_loglevel, v3) )
fprintf(stderr, "%s: error opening log file (ignoring this error)\n", *argv);
log_write(3LL, "Starting VNC Reflector %s", "1.2.4");
if ( opt_foreground )
{
LABEL_15:
if ( (unsigned int)func2() )
{
func3();
set_host_encodings((unsigned int)opt_request_tight, (unsigned int)opt_tight_level);
set_client_passwords(opt_client_password, opt_client_ro_password);
fbs_set_prefix(opt_fbs_prefix, (unsigned int)opt_join_sessions);
set_active_file(opt_active_filename);
v5 = opt_actions_filename;
set_actions_file(opt_actions_filename);
aio_init(v5);
if ( opt_bind_ip )
{
if ( (unsigned int)aio_set_bind_address() )
log_write(4LL, "Would bind listening sockets to address %s", (const char *)opt_bind_ip);
else
log_write(2LL, "Illegal address to bind listening sockets to: %s", (const char *)opt_bind_ip);
}
if ( (unsigned int)connect_to_host(opt_host_info_file, (unsigned int)opt_cl_listen_port)
&& (unsigned int)func4() )
{
set_control_signals();
aio_mainloop();
func5();
}
if ( g_framebuffer )
{
log_write(5LL, "Freeing framebuffer and associated structures");
free(g_framebuffer);
free_enc_cache();
}
v6 = (void *)*((_QWORD *)&g_screen_info + 3);
if ( v6 )
free(v6);
get_hextile_caching_stats(&v8, &v7);
if ( v7 + v8 )
log_write(
4LL,
"Hextile BGR233 caching efficiency: %d%%",
(unsigned int)((100 * v8 + (v7 + v8) / 2) / (v7 + v8)));
}
log_write(3LL, "Terminating");
if ( !(unsigned int)log_close() && opt_foreground )
fprintf(stderr, "%s: error closing log file (ignoring this error)\n", *argv);
exit(1);
}
if ( !opt_no_banner )
fwrite("Starting in the background, see the log file for errors and other messages.\n", 1uLL, 0x4CuLL, stderr);
if ( getpid() == 1 )
{
LABEL_14:
close(0);
close(1);
close(2);
log_write(4LL, "Switched to the background mode");
goto LABEL_15;
}
signal(21, handler);
signal(22, handler);
signal(20, handler);
if ( !fork() )
{
setsid();
goto LABEL_14;
}
return 0;
}

original_code:
int main(int argc, char **argv)
{
  long cache_hits, cache_misses;
  parse_args(argc, argv);
  if (!opt_no_banner) {
    fprintf(stderr,
"VNC Reflector %s.  Copyright (C) 2001-2003 HorizonLive.com, Inc.\n\n"
"HorizonLive provides e-Learning and collaborative synchronous presentation\n"
"solutions in a totally Web-based environment.  For more information about\n"
"HorizonLive, please see our website at http://www.horizonlive.com/\n\n",
            "1.2.4");
  }
  if (!log_open(opt_log_filename, opt_file_loglevel,
                (opt_foreground) ? opt_stderr_loglevel : -1)) {
    fprintf(stderr, "%s: error opening log file (ignoring this error)\n",
            argv[0]);
  }
  log_write(3, "Starting VNC Reflector %s", "1.2.4");
  if (!opt_foreground) {
    if (!opt_no_banner) {
      fprintf(stderr, "Starting in the background, "
              "see the log file for errors and other messages.\n");
    }
    if (getpid() != 1) {
      signal(21, ((__sighandler_t) 1));
      signal(22, ((__sighandler_t) 1));
      signal(20, ((__sighandler_t) 1));
      if (fork ())
        return 0;
      setsid();
    }
    close(0);
    close(1);
    close(2);
    log_write(4, "Switched to the background mode");
  }
  if (init_screen_info()) {
    read_password_file();
    set_host_encodings(opt_request_tight, opt_tight_level);
    set_client_passwords(opt_client_password, opt_client_ro_password);
    fbs_set_prefix(opt_fbs_prefix, opt_join_sessions);
    set_active_file(opt_active_filename);
    set_actions_file(opt_actions_filename);
    aio_init();
    if (opt_bind_ip != ((void *)0)) {
      if (aio_set_bind_address(opt_bind_ip)) {
        log_write(4, "Would bind listening sockets to address %s",
                  opt_bind_ip);
      } else {
        log_write(2, "Illegal address to bind listening sockets to: %s",
                  opt_bind_ip);
      }
    }
    if (connect_to_host(opt_host_info_file, opt_cl_listen_port)) {
      if (write_pid_file()) {
        set_control_signals();
        aio_mainloop();
        remove_pid_file();
      }
    }
    if (g_framebuffer != ((void *)0)) {
      log_write(5, "Freeing framebuffer and associated structures");
      free(g_framebuffer);
      free_enc_cache();
    }
    if (g_screen_info.name != ((void *)0))
      free(g_screen_info.name);
    get_hextile_caching_stats(&cache_hits, &cache_misses);
    if (cache_hits + cache_misses != 0) {
      log_write(4, "Hextile BGR233 caching efficiency: %d%%",
                (int)((cache_hits * 100 + (cache_hits + cache_misses) / 2)
                      / (cache_hits + cache_misses)));
    }
  }
  log_write(3, "Terminating");
  if (!log_close() && opt_foreground) {
    fprintf(stderr, "%s: error closing log file (ignoring this error)\n",
            argv[0]);
  }
  exit(1);
}

canonical_original_code:
int main(int argc, char **argv)
{
  long cache_hits; long cache_misses;
  parse_args(argc, argv);
  if (!opt_no_banner) {
    fprintf(stderr,
"VNC Reflector %s.  Copyright (C) 2001-2003 HorizonLive.com, Inc.\n\n"
"HorizonLive provides e-Learning and collaborative synchronous presentation\n"
"solutions in a totally Web-based environment.  For more information about\n"
"HorizonLive, please see our website at http://www.horizonlive.com/\n\n",
            "1.2.4");
  }
  if (!log_open(opt_log_filename, opt_file_loglevel,
                (opt_foreground) ? opt_stderr_loglevel : -1)) {
    fprintf(stderr, "%s: error opening log file (ignoring this error)\n",
            argv[0]);
  }
  log_write(3, "Starting VNC Reflector %s", "1.2.4");
  if (!opt_foreground) {
    if (!opt_no_banner) {
      fprintf(stderr, "Starting in the background, "
              "see the log file for errors and other messages.\n");
    }
    if (getpid() != 1) {
      signal(21, ((void (*)(int)) 1));
      signal(22, ((void (*)(int)) 1));
      signal(20, ((void (*)(int)) 1));
      if (fork ())
        return 0;
      setsid();
    }
    close(0);
    close(1);
    close(2);
    log_write(4, "Switched to the background mode");
  }
  if (init_screen_info()) {
    read_password_file();
    set_host_encodings(opt_request_tight, opt_tight_level);
    set_client_passwords(opt_client_password, opt_client_ro_password);
    fbs_set_prefix(opt_fbs_prefix, opt_join_sessions);
    set_active_file(opt_active_filename);
    set_actions_file(opt_actions_filename);
    aio_init();
    if (opt_bind_ip != ((void *)0)) {
      if (aio_set_bind_address(opt_bind_ip)) {
        log_write(4, "Would bind listening sockets to address %s",
                  opt_bind_ip);
      } else {
        log_write(2, "Illegal address to bind listening sockets to: %s",
                  opt_bind_ip);
      }
    }
    if (connect_to_host(opt_host_info_file, opt_cl_listen_port)) {
      if (write_pid_file()) {
        set_control_signals();
        aio_mainloop();
        remove_pid_file();
      }
    }
    if (g_framebuffer != ((void *)0)) {
      log_write(5, "Freeing framebuffer and associated structures");
      free(g_framebuffer);
      free_enc_cache();
    }
    if (g_screen_info.name != ((void *)0))
      free(g_screen_info.name);
    get_hextile_caching_stats(&cache_hits, &cache_misses);
    if (cache_hits + cache_misses != 0) {
      log_write(4, "Hextile BGR233 caching efficiency: %d%%",
                (int)((cache_hits * 100 + (cache_hits + cache_misses) / 2)
                      / (cache_hits + cache_misses)));
    }
  }
  log_write(3, "Terminating");
  if (!log_close() && opt_foreground) {
    fprintf(stderr, "%s: error closing log file (ignoring this error)\n",
            argv[0]);
  }
  exit(1);
}


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

if ( !log_fname )
return 0LL;
result = 0LL;
if ( log_fp )
{
if ( fclose(log_fp) == -1 )
{
free(log_fname);
log_fname = 0LL;
log_fp = 0LL;
result = 0LL;
}
else
{
log_fp = fopen(log_fname, "a");
result = 1LL;
if ( !log_fp )
{
free(log_fname);
log_fname = 0LL;
result = 0LL;
}
}
}
return result;
}

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

if ( !log_fname )
return 0LL;
result = 0LL;
if ( log_fp )
{
if ( fclose(log_fp) == -1 )
{
free(log_fname);
log_fname = 0LL;
log_fp = 0LL;
result = 0LL;
}
else
{
log_fp = fopen(log_fname, "a");
result = 1LL;
if ( !log_fp )
{
free(log_fname);
log_fname = 0LL;
result = 0LL;
}
}
}
return result;
}

original_code:
int log_reopen(void)
{
  if (log_fname == ((void *)0) || log_fp == ((void *)0))
    return 0;
  if (fclose(log_fp) == (-1)) {
    free(log_fname);
    log_fname = ((void *)0);
    log_fp = ((void *)0);
    return 0;
  }
  log_fp = fopen(log_fname, "a");
  if (log_fp == ((void *)0)) {
    free(log_fname);
    log_fname = ((void *)0);
    return 0;
  }
  return 1;
}

canonical_original_code:
int log_reopen()
{
  if (log_fname == ((void *)0) || log_fp == ((void *)0))
    return 0;
  if (fclose(log_fp) == (-1)) {
    free(log_fname);
    log_fname = ((void *)0);
    log_fp = ((void *)0);
    return 0;
  }
  log_fp = fopen(log_fname, "a");
  if (log_fp == ((void *)0)) {
    free(log_fname);
    log_fname = ((void *)0);
    return 0;
  }
  return 1;
}


decompiled_code:
_BOOL8 log_close()
{
_BOOL8 result; // rax

if ( log_fname )
{
free(log_fname);
log_fname = 0LL;
}
result = 1LL;
if ( log_fp )
{
result = fclose(log_fp) != -1;
log_fp = 0LL;
}
log_file_level = -1;
log_stderr_level = -1;
return result;
}

canonical_decompiled_code:
_BOOL8 func1()
{
_BOOL8 result; // rax

if ( log_fname )
{
free(log_fname);
log_fname = 0LL;
}
result = 1LL;
if ( log_fp )
{
result = fclose(log_fp) != -1;
log_fp = 0LL;
}
log_file_level = -1;
log_stderr_level = -1;
return result;
}

original_code:
int log_close(void)
{
  int success = 1;
  if (log_fname != ((void *)0)) {
    free(log_fname);
    log_fname = ((void *)0);
  }
  if (log_fp != ((void *)0)) {
    success = (fclose(log_fp) != (-1));
    log_fp = ((void *)0);
  }
  log_file_level = -1;
  log_stderr_level = -1;
  return success;
}

canonical_original_code:
int log_close()
{
  int success = 1;
  if (log_fname != ((void *)0)) {
    free(log_fname);
    log_fname = ((void *)0);
  }
  if (log_fp != ((void *)0)) {
    success = (fclose(log_fp) != (-1));
    log_fp = ((void *)0);
  }
  log_file_level = -1;
  log_stderr_level = -1;
  return success;
}

variable_types: success int


decompiled_code:
void log_write(signed int a1, const char *a2, ...)
{
FILE *v2; // rbp
struct tm *v3; // rax
char v4; // r13
struct tm *v5; // rax
char v6[32]; // [rsp+0h] [rbp-118h] BYREF
time_t timer; // [rsp+20h] [rbp-F8h] BYREF
gcc_va_list arg; // [rsp+28h] [rbp-F0h] BYREF

v2 = log_fp;
if ( log_fp && log_file_level >= a1 )
{
timer = time(0LL);
v5 = localtime(&timer);
strftime(v6, 0x1FuLL, "%d/%m/%y %H:%M:%S", v5);
v4 = 32;
if ( (unsigned int)a1 > 6 )
{
LABEL_9:
fprintf(v2, "%s %c ", v6, (unsigned int)v4);
va_start(arg, a2);
vfprintf(log_fp, a2, arg);
fputc(10, log_fp);
fflush(log_fp);
goto LABEL_12;
}
}
else
{
if ( log_stderr_level < a1 )
return;
timer = time(0LL);
v3 = localtime(&timer);
strftime(v6, 0x1FuLL, "%d/%m/%y %H:%M:%S", v3);
if ( (unsigned int)a1 > 6 )
{
if ( a1 > log_file_level )
{
v4 = 32;
LABEL_7:
fprintf(stderr, "%s %c ", v6, (unsigned int)v4);
va_end(arg);
va_start(arg, a2);
vfprintf(stderr, a2, arg);
fputc(10, stderr);
fflush(stderr);
return;
}
v4 = 32;
goto LABEL_9;
}
}
v4 = log_lchar[a1];
if ( log_file_level >= a1 )
goto LABEL_9;
LABEL_12:
if ( log_stderr_level >= a1 )
goto LABEL_7;
}

canonical_decompiled_code:
void func2(signed int a1, const char *a2, ...)
{
FILE *v2; // rbp
struct tm *v3; // rax
char v4; // r13
struct tm *v5; // rax
char v6[32]; // [rsp+0h] [rbp-118h] BYREF
time_t timer; // [rsp+20h] [rbp-F8h] BYREF
gcc_va_list arg; // [rsp+28h] [rbp-F0h] BYREF

v2 = log_fp;
if ( log_fp && log_file_level >= a1 )
{
timer = time(0LL);
v5 = localtime(&timer);
strftime(v6, 0x1FuLL, "%d/%m/%y %H:%M:%S", v5);
v4 = 32;
if ( (unsigned int)a1 > 6 )
{
LABEL_9:
fprintf(v2, "%s %c ", v6, (unsigned int)v4);
va_start(arg, a2);
vfprintf(log_fp, a2, arg);
fputc(10, log_fp);
fflush(log_fp);
goto LABEL_12;
}
}
else
{
if ( log_stderr_level < a1 )
return;
timer = time(0LL);
v3 = localtime(&timer);
strftime(v6, 0x1FuLL, "%d/%m/%y %H:%M:%S", v3);
if ( (unsigned int)a1 > 6 )
{
if ( a1 > log_file_level )
{
v4 = 32;
LABEL_7:
fprintf(stderr, "%s %c ", v6, (unsigned int)v4);
va_end(arg);
va_start(arg, a2);
vfprintf(stderr, a2, arg);
fputc(10, stderr);
fflush(stderr);
return;
}
v4 = 32;
goto LABEL_9;
}
}
v4 = log_lchar[a1];
if ( log_file_level >= a1 )
goto LABEL_9;
LABEL_12:
if ( log_stderr_level >= a1 )
goto LABEL_7;
}

original_code:
void log_write(int level, char *format, ...)
{
  va_list arg_list;
  time_t now;
  char time_buf[32];
  char level_char = ' ';
  if ( (log_fp != ((void *)0) && level <= log_file_level) ||
       level <= log_stderr_level ) {
    now = time(((void *)0));
    strftime(time_buf, 31, "%d/%m/%y %H:%M:%S", localtime(&now));
    if (level >= 0 && level < sizeof(log_lchar) - 1)
      level_char = log_lchar[level];
    if (level <= log_file_level) {
      fprintf(log_fp, "%s %c ", time_buf, (int)level_char);
      __builtin_va_start(arg_list,format);
      vfprintf(log_fp, format, arg_list);
      __builtin_va_end(arg_list);
      fprintf(log_fp, "\n");
      fflush(log_fp);
    }
    if (level <= log_stderr_level) {
      fprintf(stderr, "%s %c ", time_buf, (int)level_char);
      __builtin_va_start(arg_list,format);
      vfprintf(stderr, format, arg_list);
      __builtin_va_end(arg_list);
      fprintf(stderr, "\n");
      fflush(stderr);
    }
  }
}

canonical_original_code:
void log_write(int level, char *format, ...)
{
  __builtin_va_list arg_list;
  long now;
  char time_buf[32];
  char level_char = ' ';
  if ( (log_fp != ((void *)0) && level <= log_file_level) ||
       level <= log_stderr_level ) {
    now = time(((void *)0));
    strftime(time_buf, 31, "%d/%m/%y %H:%M:%S", localtime(&now));
    if (level >= 0 && level < sizeof(log_lchar) - 1)
      level_char = log_lchar[level];
    if (level <= log_file_level) {
      fprintf(log_fp, "%s %c ", time_buf, (int)level_char);
      __builtin_va_start(arg_list,format);
      vfprintf(log_fp, format, arg_list);
      __builtin_va_end(arg_list);
      fprintf(log_fp, "\n");
      fflush(log_fp);
    }
    if (level <= log_stderr_level) {
      fprintf(stderr, "%s %c ", time_buf, (int)level_char);
      __builtin_va_start(arg_list,format);
      vfprintf(stderr, format, arg_list);
      __builtin_va_end(arg_list);
      fprintf(stderr, "\n");
      fflush(stderr);
    }
  }
}

variable_types: level int

variable_types:
format
char *

variable_types: now long


decompiled_code:
__int64 __fastcall log_open(char *src, int a2, int a3)
{
__int64 result; // rax
FILE *v5; // r12
size_t v6; // rax
char *v7; // rdi

if ( log_fname || log_fp )
{
log_write(0, "Trying to open already opened log");
result = 0LL;
}
else
{
log_stderr_level = a3;
v5 = fopen(src, "a");
log_fp = v5;
result = 0LL;
if ( v5 )
{
v6 = strlen(src);
v7 = (char *)malloc(v6 + 1);
log_fname = v7;
if ( v7 )
{
strcpy(v7, src);
log_file_level = a2;
fputc(10, v5);
fflush(log_fp);
result = 1LL;
}
else
{
fclose(v5);
log_fp = 0LL;
result = 0LL;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *src, int a2, int a3)
{
__int64 result; // rax
FILE *v5; // r12
size_t v6; // rax
char *v7; // rdi

if ( log_fname || log_fp )
{
func2(0, "Trying to open already opened log");
result = 0LL;
}
else
{
log_stderr_level = a3;
v5 = fopen(src, "a");
log_fp = v5;
result = 0LL;
if ( v5 )
{
v6 = strlen(src);
v7 = (char *)malloc(v6 + 1);
log_fname = v7;
if ( v7 )
{
strcpy(v7, src);
log_file_level = a2;
fputc(10, v5);
fflush(log_fp);
result = 1LL;
}
else
{
fclose(v5);
log_fp = 0LL;
result = 0LL;
}
}
}
return result;
}

original_code:
int log_open(char *filename, int file_level, int stderr_level)
{
  if (log_fname != ((void *)0) || log_fp != ((void *)0)) {
    log_write(0, "Trying to open already opened log");
    return 0;
  }
  log_stderr_level = stderr_level;
  log_fp = fopen(filename, "a");
  if (log_fp == ((void *)0))
    return 0;
  log_fname = malloc(strlen(filename) + 1);
  if (log_fname == ((void *)0)) {
    fclose(log_fp);
    log_fp = ((void *)0);
    return 0;
  }
  strcpy(log_fname, filename);
  log_file_level = file_level;
  fprintf(log_fp, "\n");
  fflush(log_fp);
  return 1;
}

variable_types:
filename
char *


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

result = cur_slot;
*cur_slot = 2;
return result;
}

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

result = cur_slot;
*cur_slot = 2;
return result;
}

original_code:
static void host_listen_init_hook(void)
{
  cur_slot->type = 2;
}

canonical_original_code:
static void host_listen_init_hook()
{
  cur_slot->type = 2;
}


decompiled_code:
__int64 host_init_hook()
{
*cur_slot = 3;
aio_setclose(&host_close_hook);
return aio_setread(rf_host_ver, 0LL, 12LL);
}

canonical_decompiled_code:
__int64 func1()
{
*cur_slot = 3;
aio_setclose(&host_close_hook);
return aio_setread(rf_host_ver, 0LL, 12LL);
}

original_code:
static void host_init_hook(void)
{
  cur_slot->type = 3;
  aio_setclose(host_close_hook);
  aio_setread(rf_host_ver, ((void *)0), 12);
}

canonical_original_code:
static void host_init_hook()
{
  cur_slot->type = 3;
  aio_setclose(host_close_hook);
  aio_setread(rf_host_ver, ((void *)0), 12);
}


decompiled_code:
__int64 host_accept_hook()
{
log_write(3LL, "Accepted host connection from %s", *(const char **)(cur_slot + 16LL));
return host_init_hook();
}

canonical_decompiled_code:
__int64 func2()
{
log_write(3LL, "Accepted host connection from %s", *(const char **)(cur_slot + 16LL));
return func1();
}

original_code:
static void host_accept_hook(void)
{
  log_write(3, "Accepted host connection from %s", cur_slot->name);
  host_init_hook();
}

canonical_original_code:
static void host_accept_hook()
{
  log_write(3, "Accepted host connection from %s", cur_slot->name);
  host_init_hook();
}


decompiled_code:
__int64 rf_host_initmsg()
{
__int64 v0; // rbx
unsigned __int16 *v1; // rax
unsigned __int16 v2; // dx
unsigned __int16 v3; // cx
__int64 v4; // rdx

v0 = cur_slot;
v1 = *(unsigned __int16 **)(cur_slot + 32LL);
v2 = _byteswap_ushort(*v1);
*(_WORD *)(cur_slot + 372LL) = v2;
v3 = _byteswap_ushort(v1[1]);
*(_WORD *)(v0 + 374) = v3;
log_write(3LL, "Remote desktop geometry is %dx%d", v2, v3);
v4 = _byteswap_ulong(*(_DWORD *)(*(_QWORD *)(cur_slot + 32LL) + 20LL));
*(_DWORD *)(v0 + 368) = v4;
return aio_setread(rf_host_set_formats, 0LL, v4);
}

canonical_decompiled_code:
__int64 func3()
{
__int64 v0; // rbx
unsigned __int16 *v1; // rax
unsigned __int16 v2; // dx
unsigned __int16 v3; // cx
__int64 v4; // rdx

v0 = cur_slot;
v1 = *(unsigned __int16 **)(cur_slot + 32LL);
v2 = _byteswap_ushort(*v1);
*(_WORD *)(cur_slot + 372LL) = v2;
v3 = _byteswap_ushort(v1[1]);
*(_WORD *)(v0 + 374) = v3;
log_write(3LL, "Remote desktop geometry is %dx%d", v2, v3);
v4 = _byteswap_ulong(*(_DWORD *)(*(_QWORD *)(cur_slot + 32LL) + 20LL));
*(_DWORD *)(v0 + 368) = v4;
return aio_setread(rf_host_set_formats, 0LL, v4);
}

original_code:
static void rf_host_initmsg(void)
{
  HOST_SLOT *hs = (HOST_SLOT *)cur_slot;
  hs->fb_width = ((u_int16_t)((u_int8_t *)cur_slot->readbuf)[0] << 8 | (u_int16_t)((u_int8_t *)cur_slot->readbuf)[1]);
  hs->fb_height = ((u_int16_t)((u_int8_t *)&cur_slot->readbuf[2])[0] << 8 | (u_int16_t)((u_int8_t *)&cur_slot->readbuf[2])[1]);
  log_write(3, "Remote desktop geometry is %dx%d",
            (int)hs->fb_width, (int)hs->fb_height);
  hs->temp_len = ((u_int32_t)((u_int8_t *)&cur_slot->readbuf[20])[0] << 24 | (u_int32_t)((u_int8_t *)&cur_slot->readbuf[20])[1] << 16 | (u_int32_t)((u_int8_t *)&cur_slot->readbuf[20])[2] << 8 | (u_int32_t)((u_int8_t *)&cur_slot->readbuf[20])[3]);;
  aio_setread(rf_host_set_formats, ((void *)0), hs->temp_len);
}

canonical_original_code:
static void rf_host_initmsg()
{
  struct _HOST_SLOT *hs = (HOST_SLOT *)cur_slot;
  hs->fb_width = ((u_int16_t)((unsigned int *)cur_slot->readbuf)[0] << 8 | (u_int16_t)((unsigned int *)cur_slot->readbuf)[1]);
  hs->fb_height = ((u_int16_t)((unsigned int *)&cur_slot->readbuf[2])[0] << 8 | (u_int16_t)((unsigned int *)&cur_slot->readbuf[2])[1]);
  log_write(3, "Remote desktop geometry is %dx%d",
            (int)hs->fb_width, (int)hs->fb_height);
  hs->temp_len = ((u_int32_t)((unsigned int *)&cur_slot->readbuf[20])[0] << 24 | (u_int32_t)((unsigned int *)&cur_slot->readbuf[20])[1] << 16 | (u_int32_t)((unsigned int *)&cur_slot->readbuf[20])[2] << 8 | (u_int32_t)((unsigned int *)&cur_slot->readbuf[20])[3]);;
  aio_setread(rf_host_set_formats, ((void *)0), hs->temp_len);
}

variable_types: struct _HOST_SLOT { struct _AIO_SLOT { int type; int fd; int idx; char *name; void (*readfunc)(); unsigned char *readbuf; unsigned long bytes_to_read; unsigned long bytes_ready; unsigned char buf256[256]; struct _AIO_BLOCK { struct _AIO_BLOCK *next; void (*func)(); unsigned long data_size; unsigned char data[1]; } *outqueue; struct _AIO_BLOCK *outqueue_last; unsigned long bytes_written; void (*closefunc)(); unsigned int listening_f; unsigned int alloc_f; unsigned int close_f; unsigned int fd_closed_f; unsigned int errio_f; unsigned int errread_f; unsigned int errwrite_f; int io_errno; struct _AIO_SLOT *next; struct _AIO_SLOT *prev; } s; unsigned int temp_len; unsigned int fb_width; unsigned int fb_height; } *

user_defined_types: struct _HOST_SLOT { struct _AIO_SLOT s; unsigned int temp_len; unsigned int fb_width; unsigned int fb_height; }

user_defined_types: struct _AIO_SLOT { int type; int fd; int idx; char *name; void (*readfunc)(); unsigned char *readbuf; unsigned long bytes_to_read; unsigned long bytes_ready; unsigned char buf256[256]; struct _AIO_BLOCK *outqueue; struct _AIO_BLOCK *outqueue_last; unsigned long bytes_written; void (*closefunc)(); unsigned int listening_f; unsigned int alloc_f; unsigned int close_f; unsigned int fd_closed_f; unsigned int errio_f; unsigned int errread_f; unsigned int errwrite_f; int io_errno; struct _AIO_SLOT *next; struct _AIO_SLOT *prev; }


decompiled_code:
__int64 rf_host_conn_failed()
{
log_write(1LL, "VNC connection failed: %.*s", *(unsigned int *)(cur_slot + 368LL), *(_QWORD *)(cur_slot + 32LL));
return aio_close(0LL);
}

canonical_decompiled_code:
__int64 func4()
{
log_write(1LL, "VNC connection failed: %.*s", *(unsigned int *)(cur_slot + 368LL), *(_QWORD *)(cur_slot + 32LL));
return aio_close(0LL);
}

original_code:
static void rf_host_conn_failed(void)
{
  HOST_SLOT *hs = (HOST_SLOT *)cur_slot;
  log_write(1, "VNC connection failed: %.*s",
            (int)hs->temp_len, cur_slot->readbuf);
  aio_close(0);
}

canonical_original_code:
static void rf_host_conn_failed()
{
  struct _HOST_SLOT *hs = (HOST_SLOT *)cur_slot;
  log_write(1, "VNC connection failed: %.*s",
            (int)hs->temp_len, cur_slot->readbuf);
  aio_close(0);
}

user_defined_types: struct _AIO_BLOCK { struct _AIO_BLOCK *next; void (*func)(); unsigned long data_size; unsigned char data[1]; }


decompiled_code:
__int64 rf_host_ver()
{
__int64 v0; // rbx
const unsigned __int16 *v1; // rax
__int64 result; // rax
unsigned int v3; // ebp
__int64 v4; // rbx
char v5[40]; // [rsp+0h] [rbp-28h] BYREF

v0 = *(_QWORD *)(cur_slot + 32LL);
if ( !memcmp((const void *)v0, "RFB ", 4uLL)
&& (v1 = *_ctype_b_loc(), (v1[*(char *)(v0 + 4)] & 0x800) != 0)
&& (v1[*(char *)(v0 + 5)] & 0x800) != 0
&& (v1[*(char *)(v0 + 6)] & 0x800) != 0
&& *(_BYTE *)(v0 + 7) == 46
&& (v1[*(char *)(v0 + 8)] & 0x800) != 0
&& (v1[*(char *)(v0 + 9)] & 0x800) != 0
&& (v1[*(char *)(v0 + 10)] & 0x800) != 0
&& *(_BYTE *)(v0 + 11) == 10 )
{
v3 = atoi((const char *)(v0 + 4));
v4 = (unsigned int)atoi((const char *)(v0 + 8));
log_write(4LL, "Remote RFB Protocol version is %d.%d", v3, v4);
if ( v3 == 3 )
{
if ( (_DWORD)v4 != 3 )
log_write(2LL, "Protocol sub-version does not match (ignoring)");
sprintf(v5, "RFB %03d.%03d\n", 3LL, 3LL);
aio_write(0LL);
result = aio_setread(rf_host_auth, 0LL, 4LL);
}
else
{
log_write(1LL, "Wrong protocol version, expected %d.x", 3LL);
result = aio_close(0LL);
}
}
else
{
log_write(1LL, "Wrong greeting data received from host");
result = aio_close(0LL);
}
return result;
}

canonical_decompiled_code:
__int64 func5()
{
__int64 v0; // rbx
const unsigned __int16 *v1; // rax
__int64 result; // rax
unsigned int v3; // ebp
__int64 v4; // rbx
char v5[40]; // [rsp+0h] [rbp-28h] BYREF

v0 = *(_QWORD *)(cur_slot + 32LL);
if ( !memcmp((const void *)v0, "RFB ", 4uLL)
&& (v1 = *_ctype_b_loc(), (v1[*(char *)(v0 + 4)] & 0x800) != 0)
&& (v1[*(char *)(v0 + 5)] & 0x800) != 0
&& (v1[*(char *)(v0 + 6)] & 0x800) != 0
&& *(_BYTE *)(v0 + 7) == 46
&& (v1[*(char *)(v0 + 8)] & 0x800) != 0
&& (v1[*(char *)(v0 + 9)] & 0x800) != 0
&& (v1[*(char *)(v0 + 10)] & 0x800) != 0
&& *(_BYTE *)(v0 + 11) == 10 )
{
v3 = atoi((const char *)(v0 + 4));
v4 = (unsigned int)atoi((const char *)(v0 + 8));
log_write(4LL, "Remote RFB Protocol version is %d.%d", v3, v4);
if ( v3 == 3 )
{
if ( (_DWORD)v4 != 3 )
log_write(2LL, "Protocol sub-version does not match (ignoring)");
sprintf(v5, "RFB %03d.%03d\n", 3LL, 3LL);
aio_write(0LL);
result = aio_setread(rf_host_auth, 0LL, 4LL);
}
else
{
log_write(1LL, "Wrong protocol version, expected %d.x", 3LL);
result = aio_close(0LL);
}
}
else
{
log_write(1LL, "Wrong greeting data received from host");
result = aio_close(0LL);
}
return result;
}

original_code:
static void rf_host_ver(void)
{
  char *buf = (char *)cur_slot->readbuf;
  int major = 3, minor = 3;
  int remote_major, remote_minor;
  char ver_msg[16];
  if ( strncmp(buf, "RFB ", 4) != 0 || !((*__ctype_b_loc ())[(int) ((buf[4]))] & (unsigned short int) _ISdigit) ||
       !((*__ctype_b_loc ())[(int) ((buf[4]))] & (unsigned short int) _ISdigit) || !((*__ctype_b_loc ())[(int) ((buf[5]))] & (unsigned short int) _ISdigit) || !((*__ctype_b_loc ())[(int) ((buf[6]))] & (unsigned short int) _ISdigit) ||
       buf[7] != '.' || !((*__ctype_b_loc ())[(int) ((buf[8]))] & (unsigned short int) _ISdigit) || !((*__ctype_b_loc ())[(int) ((buf[9]))] & (unsigned short int) _ISdigit) ||
       !((*__ctype_b_loc ())[(int) ((buf[10]))] & (unsigned short int) _ISdigit) || buf[11] != '\n' ) {
    log_write(1, "Wrong greeting data received from host");
    aio_close(0);
    return;
  }
  remote_major = atoi(&buf[4]);
  remote_minor = atoi(&buf[8]);
  log_write(4, "Remote RFB Protocol version is %d.%d",
            remote_major, remote_minor);
  if (remote_major != major) {
    log_write(1, "Wrong protocol version, expected %d.x", major);
    aio_close(0);
    return;
  } else if (remote_minor != minor) {
    log_write(2, "Protocol sub-version does not match (ignoring)");
  }
  sprintf(ver_msg, "RFB %03d.%03d\n", abs(major) % 999, abs(minor) % 999);
  aio_write(((void *)0), ver_msg, 12);
  aio_setread(rf_host_auth, ((void *)0), 4);
}

canonical_original_code:
static void rf_host_ver()
{
  char *buf = (char *)cur_slot->readbuf;
  int major = 3; int minor = 3;
  int remote_major; int remote_minor;
  char ver_msg[16];
  if ( strncmp(buf, "RFB ", 4) != 0 || !((*__ctype_b_loc ())[(int) ((buf[4]))] & (unsigned short) _ISdigit) ||
       !((*__ctype_b_loc ())[(int) ((buf[4]))] & (unsigned short) _ISdigit) || !((*__ctype_b_loc ())[(int) ((buf[5]))] & (unsigned short) _ISdigit) || !((*__ctype_b_loc ())[(int) ((buf[6]))] & (unsigned short) _ISdigit) ||
       buf[7] != '.' || !((*__ctype_b_loc ())[(int) ((buf[8]))] & (unsigned short) _ISdigit) || !((*__ctype_b_loc ())[(int) ((buf[9]))] & (unsigned short) _ISdigit) ||
       !((*__ctype_b_loc ())[(int) ((buf[10]))] & (unsigned short) _ISdigit) || buf[11] != '\n' ) {
    log_write(1, "Wrong greeting data received from host");
    aio_close(0);
    return;
  }
  remote_major = atoi(&buf[4]);
  remote_minor = atoi(&buf[8]);
  log_write(4, "Remote RFB Protocol version is %d.%d",
            remote_major, remote_minor);
  if (remote_major != major) {
    log_write(1, "Wrong protocol version, expected %d.x", major);
    aio_close(0);
    return;
  } else if (remote_minor != minor) {
    log_write(2, "Protocol sub-version does not match (ignoring)");
  }
  sprintf(ver_msg, "RFB %03d.%03d\n", abs(major) % 999, abs(minor) % 999);
  aio_write(((void *)0), ver_msg, 12);
  aio_setread(rf_host_auth, ((void *)0), 4);
}

variable_types:
buf
char *

variable_types: major int

variable_types: minor int


decompiled_code:
__int64 send_client_initmsg()
{
log_write(5LL, "Requesting %s session", "non-shared");
aio_write(0LL);
return aio_setread(rf_host_initmsg, 0LL, 24LL);
}

canonical_decompiled_code:
__int64 func6()
{
log_write(5LL, "Requesting %s session", "non-shared");
aio_write(0LL);
return aio_setread(rf_host_initmsg, 0LL, 24LL);
}

original_code:
static void send_client_initmsg(void)
{
  u_int8_t shared_session = 1;
  log_write(5, "Requesting %s session",
            (shared_session != 0) ? "non-shared" : "shared");
  aio_write(((void *)0), &shared_session, 1);
  aio_setread(rf_host_initmsg, ((void *)0), 24);
}

canonical_original_code:
static void send_client_initmsg()
{
  unsigned int shared_session = 1;
  log_write(5, "Requesting %s session",
            (shared_session != 0) ? "non-shared" : "shared");
  aio_write(((void *)0), &shared_session, 1);
  aio_setread(rf_host_initmsg, ((void *)0), 24);
}


decompiled_code:
__int64 rf_host_auth()
{
unsigned int *v0; // rdx
unsigned int v1; // eax
__int64 result; // rax
__int64 v3; // rdx

v0 = *(unsigned int **)(cur_slot + 32LL);
v1 = _byteswap_ulong(*v0);
if ( v1 == 1 )
{
log_write(3LL, "No authentication required at host side");
result = send_client_initmsg();
}
else if ( v1 == 2 )
{
log_write(5LL, "VNC authentication requested by host");
result = aio_setread(rf_host_crypt, 0LL, 16LL);
}
else if ( v1 )
{
log_write(1LL, "Unknown authentication scheme requested by host");
result = aio_close(0LL);
}
else
{
v3 = _byteswap_ulong(v0[1]);
*(_DWORD *)(cur_slot + 368LL) = v3;
result = aio_setread(rf_host_conn_failed, 0LL, v3);
}
return result;
}

canonical_decompiled_code:
__int64 func7()
{
unsigned int *v0; // rdx
unsigned int v1; // eax
__int64 result; // rax
__int64 v3; // rdx

v0 = *(unsigned int **)(cur_slot + 32LL);
v1 = _byteswap_ulong(*v0);
if ( v1 == 1 )
{
log_write(3LL, "No authentication required at host side");
result = func6();
}
else if ( v1 == 2 )
{
log_write(5LL, "VNC authentication requested by host");
result = aio_setread(rf_host_crypt, 0LL, 16LL);
}
else if ( v1 )
{
log_write(1LL, "Unknown authentication scheme requested by host");
result = aio_close(0LL);
}
else
{
v3 = _byteswap_ulong(v0[1]);
*(_DWORD *)(cur_slot + 368LL) = v3;
result = aio_setread(rf_host_conn_failed, 0LL, v3);
}
return result;
}

original_code:
static void rf_host_auth(void)
{
  HOST_SLOT *hs = (HOST_SLOT *)cur_slot;
  u_int32_t value32;
  value32 = ((u_int32_t)((u_int8_t *)cur_slot->readbuf)[0] << 24 | (u_int32_t)((u_int8_t *)cur_slot->readbuf)[1] << 16 | (u_int32_t)((u_int8_t *)cur_slot->readbuf)[2] << 8 | (u_int32_t)((u_int8_t *)cur_slot->readbuf)[3]);;
  switch (value32) {
  case 0:
    hs->temp_len = ((u_int32_t)((u_int8_t *)&cur_slot->readbuf[4])[0] << 24 | (u_int32_t)((u_int8_t *)&cur_slot->readbuf[4])[1] << 16 | (u_int32_t)((u_int8_t *)&cur_slot->readbuf[4])[2] << 8 | (u_int32_t)((u_int8_t *)&cur_slot->readbuf[4])[3]);;
    aio_setread(rf_host_conn_failed, ((void *)0), hs->temp_len);
    break;
  case 1:
    log_write(3, "No authentication required at host side");
    send_client_initmsg();
    break;
  case 2:
    log_write(5, "VNC authentication requested by host");
    aio_setread(rf_host_crypt, ((void *)0), 16);
    break;
  default:
    log_write(1, "Unknown authentication scheme requested by host");
    aio_close(0);
    return;
  }
}

canonical_original_code:
static void rf_host_auth()
{
  struct _HOST_SLOT *hs = (HOST_SLOT *)cur_slot;
  unsigned int value32;
  value32 = ((u_int32_t)((unsigned int *)cur_slot->readbuf)[0] << 24 | (u_int32_t)((unsigned int *)cur_slot->readbuf)[1] << 16 | (u_int32_t)((unsigned int *)cur_slot->readbuf)[2] << 8 | (u_int32_t)((unsigned int *)cur_slot->readbuf)[3]);;
  switch (value32) {
  case 0:
    hs->temp_len = ((u_int32_t)((unsigned int *)&cur_slot->readbuf[4])[0] << 24 | (u_int32_t)((unsigned int *)&cur_slot->readbuf[4])[1] << 16 | (u_int32_t)((unsigned int *)&cur_slot->readbuf[4])[2] << 8 | (u_int32_t)((unsigned int *)&cur_slot->readbuf[4])[3]);;
    aio_setread(rf_host_conn_failed, ((void *)0), hs->temp_len);
    break;
  case 1:
    log_write(3, "No authentication required at host side");
    send_client_initmsg();
    break;
  case 2:
    log_write(5, "VNC authentication requested by host");
    aio_setread(rf_host_crypt, ((void *)0), 16);
    break;
  default:
    log_write(1, "Unknown authentication scheme requested by host");
    aio_close(0);
    return;
  }
}


decompiled_code:
__int64 rf_host_auth_done()
{
unsigned int v0; // eax
__int64 result; // rax

v0 = _byteswap_ulong(**(_DWORD **)(cur_slot + 32LL));
if ( v0 == 1 )
{
log_write(1LL, "Authentication failed");
result = aio_close(0LL);
}
else if ( v0 == 2 )
{
log_write(1LL, "Authentication failed, too many tries");
result = aio_close(0LL);
}
else if ( v0 )
{
log_write(1LL, "Unknown authentication result");
result = aio_close(0LL);
}
else
{
log_write(3LL, "Authentication successful");
result = send_client_initmsg();
}
return result;
}

canonical_decompiled_code:
__int64 func8()
{
unsigned int v0; // eax
__int64 result; // rax

v0 = _byteswap_ulong(**(_DWORD **)(cur_slot + 32LL));
if ( v0 == 1 )
{
log_write(1LL, "Authentication failed");
result = aio_close(0LL);
}
else if ( v0 == 2 )
{
log_write(1LL, "Authentication failed, too many tries");
result = aio_close(0LL);
}
else if ( v0 )
{
log_write(1LL, "Unknown authentication result");
result = aio_close(0LL);
}
else
{
log_write(3LL, "Authentication successful");
result = func6();
}
return result;
}

original_code:
static void rf_host_auth_done(void)
{
  u_int32_t value32;
  value32 = ((u_int32_t)((u_int8_t *)cur_slot->readbuf)[0] << 24 | (u_int32_t)((u_int8_t *)cur_slot->readbuf)[1] << 16 | (u_int32_t)((u_int8_t *)cur_slot->readbuf)[2] << 8 | (u_int32_t)((u_int8_t *)cur_slot->readbuf)[3]);;
  switch(value32) {
  case 0:
    log_write(3, "Authentication successful");
    send_client_initmsg();
    break;
  case 1:
    log_write(1, "Authentication failed");
    aio_close(0);
    break;
  case 2:
    log_write(1, "Authentication failed, too many tries");
    aio_close(0);
    break;
  default:
    log_write(1, "Unknown authentication result");
    aio_close(0);
  }
}

canonical_original_code:
static void rf_host_auth_done()
{
  unsigned int value32;
  value32 = ((u_int32_t)((unsigned int *)cur_slot->readbuf)[0] << 24 | (u_int32_t)((unsigned int *)cur_slot->readbuf)[1] << 16 | (u_int32_t)((unsigned int *)cur_slot->readbuf)[2] << 8 | (u_int32_t)((unsigned int *)cur_slot->readbuf)[3]);;
  switch(value32) {
  case 0:
    log_write(3, "Authentication successful");
    send_client_initmsg();
    break;
  case 1:
    log_write(1, "Authentication failed");
    aio_close(0);
    break;
  case 2:
    log_write(1, "Authentication failed, too many tries");
    aio_close(0);
    break;
  default:
    log_write(1, "Unknown authentication result");
    aio_close(0);
  }
}


decompiled_code:
__int64 rf_host_crypt()
{
__int64 v1; // [rsp+0h] [rbp-18h] BYREF

log_write(6LL, "Received random challenge");
rfb_crypt(&v1, *(_QWORD *)(cur_slot + 32LL), &s_host_password);
log_write(6LL, "Sending DES-encrypted response");
aio_write(0LL);
return aio_setread(rf_host_auth_done, 0LL, 4LL);
}

canonical_decompiled_code:
__int64 func9()
{
__int64 v1; // [rsp+0h] [rbp-18h] BYREF

log_write(6LL, "Received random challenge");
rfb_crypt(&v1, *(_QWORD *)(cur_slot + 32LL), &s_host_password);
log_write(6LL, "Sending DES-encrypted response");
aio_write(0LL);
return aio_setread(rf_host_auth_done, 0LL, 4LL);
}

original_code:
static void rf_host_crypt(void)
{
  unsigned char response[16];
  log_write(6, "Received random challenge");
  rfb_crypt(response, cur_slot->readbuf, s_host_password);
  log_write(6, "Sending DES-encrypted response");
  aio_write(((void *)0), response, 16);
  aio_setread(rf_host_auth_done, ((void *)0), 4);
}

canonical_original_code:
static void rf_host_crypt()
{
  unsigned char response[16];
  log_write(6, "Received random challenge");
  rfb_crypt(response, cur_slot->readbuf, s_host_password);
  log_write(6, "Sending DES-encrypted response");
  aio_write(((void *)0), response, 16);
  aio_setread(rf_host_auth_done, ((void *)0), 4);
}

variable_types:
response
unsigned char [16]


decompiled_code:
__int64 rf_host_set_formats()
{
__int64 v0; // rbp
void *v1; // rax
void *v2; // rbx
void *v3; // rdi
unsigned int v4; // edx
__int64 *v5; // rsi
__int64 v6; // rdi
int *v8; // rax
char *v9; // rax
__int64 v10; // [rsp+0h] [rbp-58h] BYREF
__int64 v11; // [rsp+8h] [rbp-50h]
__int64 v12; // [rsp+10h] [rbp-48h]
__int64 v13; // [rsp+18h] [rbp-40h]
int v14; // [rsp+20h] [rbp-38h]
char v15[52]; // [rsp+24h] [rbp-34h] BYREF

v0 = cur_slot;
v10 = 2LL;
v11 = 0LL;
v12 = 0LL;
v13 = 0LL;
log_write(4LL, "Remote desktop name: %.*s", *(unsigned int *)(cur_slot + 368LL), *(_QWORD *)(cur_slot + 32LL));
v1 = malloc(*(unsigned int *)(v0 + 368) + 1LL);
if ( v1 )
{
v2 = v1;
v3 = (void *)*((_QWORD *)&g_screen_info + 3);
if ( v3 )
free(v3);
*((_QWORD *)&g_screen_info + 3) = v2;
v4 = *(_DWORD *)(v0 + 368);
*((_DWORD *)&g_screen_info + 5) = v4;
memcpy(v2, *(const void **)(cur_slot + 32LL), v4);
*((_BYTE *)v2 + *(unsigned int *)(v0 + 368)) = 0;
}
log_write(5LL, "Setting up pixel format");
v14 = 0;
((void (__fastcall *)(char *, _QWORD))buf_put_pixfmt)(v15, (unsigned int)((_DWORD)&g_screen_info + 4));
log_write(6LL, "Sending SetPixelFormat message");
aio_write(0LL);
if ( s_request_tight )
{
log_write(5LL, "Requesting Tight encoding");
HIDWORD(v10) = 117440512;
v11 = 0x100000005000000LL;
v12 = 0x20FFFFFF00000000LL;
LODWORD(v13) = 570425343;
if ( (unsigned int)s_tight_level <= 9 )
{
log_write(5LL, "Requesting compression level %d", (unsigned int)s_tight_level);
BYTE4(v13) = (unsigned int)(s_tight_level - 256) >> 24;
BYTE5(v13) = (unsigned int)(s_tight_level - 256) >> 16;
BYTE6(v13) = (unsigned __int16)(s_tight_level - 256) >> 8;
HIBYTE(v13) = s_tight_level;
WORD1(v10) = 1792;
}
else
{
WORD1(v10) = 1536;
}
}
else
{
log_write(5LL, "Requesting Hextile encoding");
HIBYTE(v10) = 5;
v11 = 0x1000000LL;
LODWORD(v12) = 570425343;
BYTE2(v10) = 0;
*(_DWORD *)((char *)&v10 + 3) = 4;
}
log_write(6LL, "Sending SetEncodings message");
v5 = &v10;
v6 = 0LL;
aio_write(0LL);
if ( g_framebuffer )
return host_activate(v6, v5);
v5 = 0LL;
v6 = (unsigned int)s_cl_listen_port;
if ( (unsigned int)aio_listen((unsigned int)s_cl_listen_port, 0LL, &af_client_accept, 992LL) )
return host_activate(v6, v5);
v8 = _errno_location();
v9 = strerror(*v8);
log_write(1LL, "Error creating listening socket: %s", v9);
return aio_close(1LL);
}

canonical_decompiled_code:
__int64 func10()
{
__int64 v0; // rbp
void *v1; // rax
void *v2; // rbx
void *v3; // rdi
unsigned int v4; // edx
__int64 *v5; // rsi
__int64 v6; // rdi
int *v8; // rax
char *v9; // rax
__int64 v10; // [rsp+0h] [rbp-58h] BYREF
__int64 v11; // [rsp+8h] [rbp-50h]
__int64 v12; // [rsp+10h] [rbp-48h]
__int64 v13; // [rsp+18h] [rbp-40h]
int v14; // [rsp+20h] [rbp-38h]
char v15[52]; // [rsp+24h] [rbp-34h] BYREF

v0 = cur_slot;
v10 = 2LL;
v11 = 0LL;
v12 = 0LL;
v13 = 0LL;
log_write(4LL, "Remote desktop name: %.*s", *(unsigned int *)(cur_slot + 368LL), *(_QWORD *)(cur_slot + 32LL));
v1 = malloc(*(unsigned int *)(v0 + 368) + 1LL);
if ( v1 )
{
v2 = v1;
v3 = (void *)*((_QWORD *)&g_screen_info + 3);
if ( v3 )
free(v3);
*((_QWORD *)&g_screen_info + 3) = v2;
v4 = *(_DWORD *)(v0 + 368);
*((_DWORD *)&g_screen_info + 5) = v4;
memcpy(v2, *(const void **)(cur_slot + 32LL), v4);
*((_BYTE *)v2 + *(unsigned int *)(v0 + 368)) = 0;
}
log_write(5LL, "Setting up pixel format");
v14 = 0;
((void (__fastcall *)(char *, _QWORD))buf_put_pixfmt)(v15, (unsigned int)((_DWORD)&g_screen_info + 4));
log_write(6LL, "Sending SetPixelFormat message");
aio_write(0LL);
if ( s_request_tight )
{
log_write(5LL, "Requesting Tight encoding");
HIDWORD(v10) = 117440512;
v11 = 0x100000005000000LL;
v12 = 0x20FFFFFF00000000LL;
LODWORD(v13) = 570425343;
if ( (unsigned int)s_tight_level <= 9 )
{
log_write(5LL, "Requesting compression level %d", (unsigned int)s_tight_level);
BYTE4(v13) = (unsigned int)(s_tight_level - 256) >> 24;
BYTE5(v13) = (unsigned int)(s_tight_level - 256) >> 16;
BYTE6(v13) = (unsigned __int16)(s_tight_level - 256) >> 8;
HIBYTE(v13) = s_tight_level;
WORD1(v10) = 1792;
}
else
{
WORD1(v10) = 1536;
}
}
else
{
log_write(5LL, "Requesting Hextile encoding");
HIBYTE(v10) = 5;
v11 = 0x1000000LL;
LODWORD(v12) = 570425343;
BYTE2(v10) = 0;
*(_DWORD *)((char *)&v10 + 3) = 4;
}
log_write(6LL, "Sending SetEncodings message");
v5 = &v10;
v6 = 0LL;
aio_write(0LL);
if ( g_framebuffer )
return host_activate(v6, v5);
v5 = 0LL;
v6 = (unsigned int)s_cl_listen_port;
if ( (unsigned int)aio_listen((unsigned int)s_cl_listen_port, 0LL, &af_client_accept, 992LL) )
return host_activate(v6, v5);
v8 = _errno_location();
v9 = strerror(*v8);
log_write(1LL, "Error creating listening socket: %s", v9);
return aio_close(1LL);
}

original_code:
static void rf_host_set_formats(void)
{
  HOST_SLOT *hs = (HOST_SLOT *)cur_slot;
  u_int8_t *new_name;
  unsigned char setpixfmt_msg[4 + 16];
  unsigned char setenc_msg[32] = {
    2,
    0,
    0, 0
  };
  int setenc_msg_size;
  log_write(4, "Remote desktop name: %.*s",
            (int)hs->temp_len, cur_slot->readbuf);
  new_name = malloc((size_t)hs->temp_len + 1);
  if (new_name != ((void *)0)) {
    if (g_screen_info.name != ((void *)0))
      free(g_screen_info.name);
    g_screen_info.name = new_name;
    g_screen_info.name_length = hs->temp_len;
    memcpy(g_screen_info.name, cur_slot->readbuf, hs->temp_len);
    g_screen_info.name[hs->temp_len] = '\0';
  }
  log_write(5, "Setting up pixel format");
  memset(setpixfmt_msg, 0, 4);
  buf_put_pixfmt(&setpixfmt_msg[4], &g_screen_info.pixformat);
  log_write(6, "Sending SetPixelFormat message");
  aio_write(((void *)0), setpixfmt_msg, sizeof(setpixfmt_msg));
  if (s_request_tight) {
    log_write(5, "Requesting Tight encoding");
    { ((u_int8_t *)&setenc_msg[4])[0] = (u_int8_t)(7 >> 24); ((u_int8_t *)&setenc_msg[4])[1] = (u_int8_t)(7 >> 16); ((u_int8_t *)&setenc_msg[4])[2] = (u_int8_t)(7 >> 8); ((u_int8_t *)&setenc_msg[4])[3] = (u_int8_t)7; };
    { ((u_int8_t *)&setenc_msg[8])[0] = (u_int8_t)(5 >> 24); ((u_int8_t *)&setenc_msg[8])[1] = (u_int8_t)(5 >> 16); ((u_int8_t *)&setenc_msg[8])[2] = (u_int8_t)(5 >> 8); ((u_int8_t *)&setenc_msg[8])[3] = (u_int8_t)5; };
    { ((u_int8_t *)&setenc_msg[12])[0] = (u_int8_t)(1 >> 24); ((u_int8_t *)&setenc_msg[12])[1] = (u_int8_t)(1 >> 16); ((u_int8_t *)&setenc_msg[12])[2] = (u_int8_t)(1 >> 8); ((u_int8_t *)&setenc_msg[12])[3] = (u_int8_t)1; };
    { ((u_int8_t *)&setenc_msg[16])[0] = (u_int8_t)(0 >> 24); ((u_int8_t *)&setenc_msg[16])[1] = (u_int8_t)(0 >> 16); ((u_int8_t *)&setenc_msg[16])[2] = (u_int8_t)(0 >> 8); ((u_int8_t *)&setenc_msg[16])[3] = (u_int8_t)0; };
    { ((u_int8_t *)&setenc_msg[20])[0] = (u_int8_t)(0xFFFFFF20 >> 24); ((u_int8_t *)&setenc_msg[20])[1] = (u_int8_t)(0xFFFFFF20 >> 16); ((u_int8_t *)&setenc_msg[20])[2] = (u_int8_t)(0xFFFFFF20 >> 8); ((u_int8_t *)&setenc_msg[20])[3] = (u_int8_t)0xFFFFFF20; };
    { ((u_int8_t *)&setenc_msg[24])[0] = (u_int8_t)(0xFFFFFF21 >> 24); ((u_int8_t *)&setenc_msg[24])[1] = (u_int8_t)(0xFFFFFF21 >> 16); ((u_int8_t *)&setenc_msg[24])[2] = (u_int8_t)(0xFFFFFF21 >> 8); ((u_int8_t *)&setenc_msg[24])[3] = (u_int8_t)0xFFFFFF21; };
    if (s_tight_level >= 0 && s_tight_level <= 9) {
      log_write(5, "Requesting compression level %d", s_tight_level);
      { ((u_int8_t *)&setenc_msg[28])[0] = (u_int8_t)((0xFFFFFF00 + (u_int32_t)s_tight_level) >> 24); ((u_int8_t *)&setenc_msg[28])[1] = (u_int8_t)((0xFFFFFF00 + (u_int32_t)s_tight_level) >> 16); ((u_int8_t *)&setenc_msg[28])[2] = (u_int8_t)((0xFFFFFF00 + (u_int32_t)s_tight_level) >> 8); ((u_int8_t *)&setenc_msg[28])[3] = (u_int8_t)(0xFFFFFF00 + (u_int32_t)s_tight_level); };
      { ((u_int8_t *)&setenc_msg[2])[0] = (u_int8_t)(7 >> 8); ((u_int8_t *)&setenc_msg[2])[1] = (u_int8_t)7; };
      setenc_msg_size = 32;
    } else {
      { ((u_int8_t *)&setenc_msg[2])[0] = (u_int8_t)(6 >> 8); ((u_int8_t *)&setenc_msg[2])[1] = (u_int8_t)6; };
      setenc_msg_size = 28;
    }
  } else {
    log_write(5, "Requesting Hextile encoding");
    { ((u_int8_t *)&setenc_msg[4])[0] = (u_int8_t)(5 >> 24); ((u_int8_t *)&setenc_msg[4])[1] = (u_int8_t)(5 >> 16); ((u_int8_t *)&setenc_msg[4])[2] = (u_int8_t)(5 >> 8); ((u_int8_t *)&setenc_msg[4])[3] = (u_int8_t)5; };
    { ((u_int8_t *)&setenc_msg[8])[0] = (u_int8_t)(1 >> 24); ((u_int8_t *)&setenc_msg[8])[1] = (u_int8_t)(1 >> 16); ((u_int8_t *)&setenc_msg[8])[2] = (u_int8_t)(1 >> 8); ((u_int8_t *)&setenc_msg[8])[3] = (u_int8_t)1; };
    { ((u_int8_t *)&setenc_msg[12])[0] = (u_int8_t)(0 >> 24); ((u_int8_t *)&setenc_msg[12])[1] = (u_int8_t)(0 >> 16); ((u_int8_t *)&setenc_msg[12])[2] = (u_int8_t)(0 >> 8); ((u_int8_t *)&setenc_msg[12])[3] = (u_int8_t)0; };
    { ((u_int8_t *)&setenc_msg[16])[0] = (u_int8_t)(0xFFFFFF21 >> 24); ((u_int8_t *)&setenc_msg[16])[1] = (u_int8_t)(0xFFFFFF21 >> 16); ((u_int8_t *)&setenc_msg[16])[2] = (u_int8_t)(0xFFFFFF21 >> 8); ((u_int8_t *)&setenc_msg[16])[3] = (u_int8_t)0xFFFFFF21; };
    { ((u_int8_t *)&setenc_msg[2])[0] = (u_int8_t)(4 >> 8); ((u_int8_t *)&setenc_msg[2])[1] = (u_int8_t)4; };
    setenc_msg_size = 20;
  }
  log_write(6, "Sending SetEncodings message");
  aio_write(((void *)0), setenc_msg, setenc_msg_size);
  if (g_framebuffer == ((void *)0)) {
    if (!aio_listen(s_cl_listen_port, ((void *)0), af_client_accept,
                    sizeof(CL_SLOT))) {
      log_write(1, "Error creating listening socket: %s",
                strerror((*__errno_location ())));
      aio_close(1);
      return;
    }
  }
  host_activate();
}

canonical_original_code:
static void rf_host_set_formats()
{
  struct _HOST_SLOT *hs = (HOST_SLOT *)cur_slot;
  unsigned int *new_name;
  unsigned char setpixfmt_msg[-1];
  unsigned char setenc_msg[32] = {
    2,
    0,
    0, 0
  };
  int setenc_msg_size;
  log_write(4, "Remote desktop name: %.*s",
            (int)hs->temp_len, cur_slot->readbuf);
  new_name = malloc((unsigned long)hs->temp_len + 1);
  if (new_name != ((void *)0)) {
    if (g_screen_info.name != ((void *)0))
      free(g_screen_info.name);
    g_screen_info.name = new_name;
    g_screen_info.name_length = hs->temp_len;
    memcpy(g_screen_info.name, cur_slot->readbuf, hs->temp_len);
    g_screen_info.name[hs->temp_len] = '\0';
  }
  log_write(5, "Setting up pixel format");
  memset(setpixfmt_msg, 0, 4);
  buf_put_pixfmt(&setpixfmt_msg[4], &g_screen_info.pixformat);
  log_write(6, "Sending SetPixelFormat message");
  aio_write(((void *)0), setpixfmt_msg, sizeof(setpixfmt_msg));
  if (s_request_tight) {
    log_write(5, "Requesting Tight encoding");
    { ((unsigned int *)&setenc_msg[4])[0] = (u_int8_t)(7 >> 24); ((unsigned int *)&setenc_msg[4])[1] = (u_int8_t)(7 >> 16); ((unsigned int *)&setenc_msg[4])[2] = (u_int8_t)(7 >> 8); ((unsigned int *)&setenc_msg[4])[3] = (unsigned int)7; };
    { ((unsigned int *)&setenc_msg[8])[0] = (u_int8_t)(5 >> 24); ((unsigned int *)&setenc_msg[8])[1] = (u_int8_t)(5 >> 16); ((unsigned int *)&setenc_msg[8])[2] = (u_int8_t)(5 >> 8); ((unsigned int *)&setenc_msg[8])[3] = (unsigned int)5; };
    { ((unsigned int *)&setenc_msg[12])[0] = (u_int8_t)(1 >> 24); ((unsigned int *)&setenc_msg[12])[1] = (u_int8_t)(1 >> 16); ((unsigned int *)&setenc_msg[12])[2] = (u_int8_t)(1 >> 8); ((unsigned int *)&setenc_msg[12])[3] = (unsigned int)1; };
    { ((unsigned int *)&setenc_msg[16])[0] = (u_int8_t)(0 >> 24); ((unsigned int *)&setenc_msg[16])[1] = (u_int8_t)(0 >> 16); ((unsigned int *)&setenc_msg[16])[2] = (u_int8_t)(0 >> 8); ((unsigned int *)&setenc_msg[16])[3] = (unsigned int)0; };
    { ((unsigned int *)&setenc_msg[20])[0] = (u_int8_t)(0xFFFFFF20 >> 24); ((unsigned int *)&setenc_msg[20])[1] = (u_int8_t)(0xFFFFFF20 >> 16); ((unsigned int *)&setenc_msg[20])[2] = (u_int8_t)(0xFFFFFF20 >> 8); ((unsigned int *)&setenc_msg[20])[3] = (unsigned int)0xFFFFFF20; };
    { ((unsigned int *)&setenc_msg[24])[0] = (u_int8_t)(0xFFFFFF21 >> 24); ((unsigned int *)&setenc_msg[24])[1] = (u_int8_t)(0xFFFFFF21 >> 16); ((unsigned int *)&setenc_msg[24])[2] = (u_int8_t)(0xFFFFFF21 >> 8); ((unsigned int *)&setenc_msg[24])[3] = (unsigned int)0xFFFFFF21; };
    if (s_tight_level >= 0 && s_tight_level <= 9) {
      log_write(5, "Requesting compression level %d", s_tight_level);
      { ((unsigned int *)&setenc_msg[28])[0] = (u_int8_t)((0xFFFFFF00 + (unsigned int)s_tight_level) >> 24); ((unsigned int *)&setenc_msg[28])[1] = (u_int8_t)((0xFFFFFF00 + (unsigned int)s_tight_level) >> 16); ((unsigned int *)&setenc_msg[28])[2] = (u_int8_t)((0xFFFFFF00 + (unsigned int)s_tight_level) >> 8); ((unsigned int *)&setenc_msg[28])[3] = (u_int8_t)(0xFFFFFF00 + (unsigned int)s_tight_level); };
      { ((unsigned int *)&setenc_msg[2])[0] = (u_int8_t)(7 >> 8); ((unsigned int *)&setenc_msg[2])[1] = (unsigned int)7; };
      setenc_msg_size = 32;
    } else {
      { ((unsigned int *)&setenc_msg[2])[0] = (u_int8_t)(6 >> 8); ((unsigned int *)&setenc_msg[2])[1] = (unsigned int)6; };
      setenc_msg_size = 28;
    }
  } else {
    log_write(5, "Requesting Hextile encoding");
    { ((unsigned int *)&setenc_msg[4])[0] = (u_int8_t)(5 >> 24); ((unsigned int *)&setenc_msg[4])[1] = (u_int8_t)(5 >> 16); ((unsigned int *)&setenc_msg[4])[2] = (u_int8_t)(5 >> 8); ((unsigned int *)&setenc_msg[4])[3] = (unsigned int)5; };
    { ((unsigned int *)&setenc_msg[8])[0] = (u_int8_t)(1 >> 24); ((unsigned int *)&setenc_msg[8])[1] = (u_int8_t)(1 >> 16); ((unsigned int *)&setenc_msg[8])[2] = (u_int8_t)(1 >> 8); ((unsigned int *)&setenc_msg[8])[3] = (unsigned int)1; };
    { ((unsigned int *)&setenc_msg[12])[0] = (u_int8_t)(0 >> 24); ((unsigned int *)&setenc_msg[12])[1] = (u_int8_t)(0 >> 16); ((unsigned int *)&setenc_msg[12])[2] = (u_int8_t)(0 >> 8); ((unsigned int *)&setenc_msg[12])[3] = (unsigned int)0; };
    { ((unsigned int *)&setenc_msg[16])[0] = (u_int8_t)(0xFFFFFF21 >> 24); ((unsigned int *)&setenc_msg[16])[1] = (u_int8_t)(0xFFFFFF21 >> 16); ((unsigned int *)&setenc_msg[16])[2] = (u_int8_t)(0xFFFFFF21 >> 8); ((unsigned int *)&setenc_msg[16])[3] = (unsigned int)0xFFFFFF21; };
    { ((unsigned int *)&setenc_msg[2])[0] = (u_int8_t)(4 >> 8); ((unsigned int *)&setenc_msg[2])[1] = (unsigned int)4; };
    setenc_msg_size = 20;
  }
  log_write(6, "Sending SetEncodings message");
  aio_write(((void *)0), setenc_msg, setenc_msg_size);
  if (g_framebuffer == ((void *)0)) {
    if (!aio_listen(s_cl_listen_port, ((void *)0), af_client_accept,
                    sizeof(CL_SLOT))) {
      log_write(1, "Error creating listening socket: %s",
                strerror((*__errno_location ())));
      aio_close(1);
      return;
    }
  }
  host_activate();
}


decompiled_code:
void __fastcall set_host_encodings(int a1, int a2)
{
s_request_tight = a1;
s_tight_level = a2;
}

canonical_decompiled_code:
void __fastcall func13(int a1, int a2)
{
s_request_tight = a1;
s_tight_level = a2;
}

original_code:
void set_host_encodings(int request_tight, int tight_level)
{
  s_request_tight = request_tight;
  s_tight_level = tight_level;
}


decompiled_code:
__int64 __fastcall connect_to_host(char *a1, int a2)
{
unsigned int v2; // ebx
struct hostent *v3; // rax
int v4; // eax
unsigned int v5; // ebx
int *v6; // rax
char *v7; // rax
int *v8; // rax
char *v9; // rax
int *v10; // rax
char *v11; // rax
int *v13; // rax
char *v14; // rax
struct sockaddr v15; // [rsp+0h] [rbp-18h] BYREF

if ( a1 )
s_host_info_file = a1;
if ( a2 )
s_cl_listen_port = a2;
v2 = parse_host_info();
if ( v2 )
{
if ( !strcmp(s_hostname, "*") )
{
v2 = aio_listen((unsigned int)s_host_port, host_listen_init_hook, host_accept_hook, 376LL);
if ( v2 )
{
log_write(3LL, "Listening for host connections on port %d", (unsigned int)s_host_port);
v2 = 1;
}
else
{
v13 = _errno_location();
v14 = strerror(*v13);
log_write(1LL, "Error creating listening socket: %s", v14);
}
}
else
{
log_write(3LL, "Connecting to %s, port %d", s_hostname, (unsigned int)s_host_port);
v3 = gethostbyname(s_hostname);
if ( v3 )
{
v15.sa_family = 2;
memcpy(&v15.sa_data[2], *(const void **)v3->h_addr_list, v3->h_length);
*(_WORD *)v15.sa_data = _bswap_16(s_host_port);
v4 = socket(2, 1, 0);
v5 = v4;
if ( v4 == -1 )
{
v8 = _errno_location();
v9 = strerror(*v8);
log_write(1LL, "Could not create socket: %s", v9);
v2 = 0;
}
else if ( connect(v4, &v15, 0x10u) )
{
v10 = _errno_location();
v11 = strerror(*v10);
log_write(1LL, "Could not connect: %s", v11);
close(v5);
v2 = 0;
}
else
{
log_write(3LL, "Connection established");
aio_add_slot(v5, 0LL, host_init_hook, 376LL);
v2 = 1;
}
}
else
{
v6 = _errno_location();
v7 = strerror(*v6);
log_write(1LL, "Could not get host address: %s", v7);
v2 = 0;
}
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func14(char *a1, int a2)
{
unsigned int v2; // ebx
struct hostent *v3; // rax
int v4; // eax
unsigned int v5; // ebx
int *v6; // rax
char *v7; // rax
int *v8; // rax
char *v9; // rax
int *v10; // rax
char *v11; // rax
int *v13; // rax
char *v14; // rax
struct sockaddr v15; // [rsp+0h] [rbp-18h] BYREF

if ( a1 )
s_host_info_file = a1;
if ( a2 )
s_cl_listen_port = a2;
v2 = func11();
if ( v2 )
{
if ( !strcmp(s_hostname, "*") )
{
v2 = aio_listen((unsigned int)s_host_port, host_listen_init_hook, host_accept_hook, 376LL);
if ( v2 )
{
log_write(3LL, "Listening for host connections on port %d", (unsigned int)s_host_port);
v2 = 1;
}
else
{
v13 = _errno_location();
v14 = strerror(*v13);
log_write(1LL, "Error creating listening socket: %s", v14);
}
}
else
{
log_write(3LL, "Connecting to %s, port %d", s_hostname, (unsigned int)s_host_port);
v3 = gethostbyname(s_hostname);
if ( v3 )
{
v15.sa_family = 2;
memcpy(&v15.sa_data[2], *(const void **)v3->h_addr_list, v3->h_length);
*(_WORD *)v15.sa_data = func12(s_host_port);
v4 = socket(2, 1, 0);
v5 = v4;
if ( v4 == -1 )
{
v8 = _errno_location();
v9 = strerror(*v8);
log_write(1LL, "Could not create socket: %s", v9);
v2 = 0;
}
else if ( connect(v4, &v15, 0x10u) )
{
v10 = _errno_location();
v11 = strerror(*v10);
log_write(1LL, "Could not connect: %s", v11);
close(v5);
v2 = 0;
}
else
{
log_write(3LL, "Connection established");
aio_add_slot(v5, 0LL, host_init_hook, 376LL);
v2 = 1;
}
}
else
{
v6 = _errno_location();
v7 = strerror(*v6);
log_write(1LL, "Could not get host address: %s", v7);
v2 = 0;
}
}
}
return v2;
}

original_code:
int connect_to_host(char *host_info_file, int cl_listen_port)
{
  int host_fd;
  struct hostent *phe;
  struct sockaddr_in host_addr;
  if (host_info_file != ((void *)0))
    s_host_info_file = host_info_file;
  if (cl_listen_port != 0)
    s_cl_listen_port = cl_listen_port;
  if (!parse_host_info())
    return 0;
  if (strcmp(s_hostname, "*") != 0) {
    log_write(3, "Connecting to %s, port %d", s_hostname, s_host_port);
    phe = gethostbyname(s_hostname);
    if (phe == ((void *)0)) {
      log_write(1, "Could not get host address: %s", strerror((*__errno_location ())));
      return 0;
    }
    host_addr.sin_family = 2;
    memcpy(&host_addr.sin_addr.s_addr, phe->h_addr_list[0], phe->h_length);
    host_addr.sin_port = htons((unsigned short)s_host_port);
    host_fd = socket(2, SOCK_STREAM, 0);
    if (host_fd == -1) {
      log_write(1, "Could not create socket: %s", strerror((*__errno_location ())));
      return 0;
    }
    if (connect(host_fd, (struct sockaddr *)&host_addr,
                sizeof(host_addr)) != 0) {
      log_write(1, "Could not connect: %s", strerror((*__errno_location ())));
      close(host_fd);
      return 0;
    }
    log_write(3, "Connection established");
    aio_add_slot(host_fd, ((void *)0), host_init_hook, sizeof(HOST_SLOT));
  } else {
    if (!aio_listen(s_host_port, host_listen_init_hook, host_accept_hook,
                   sizeof(HOST_SLOT))) {
      log_write(1, "Error creating listening socket: %s",
                strerror((*__errno_location ())));
      return 0;
    }
    log_write(3, "Listening for host connections on port %d",
              s_host_port);
  }
  return 1;
}

variable_types: struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; } *

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]; }

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

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


decompiled_code:
__int64 __fastcall alloc_framebuffer(int a1, int a2)
{
__int16 v4; // di
__int16 v5; // si
size_t v6; // rbx
unsigned int v7; // ebx
unsigned int v8; // eax

if ( g_framebuffer )
{
if ( g_fb_width >= a1 && g_fb_height >= a2 )
{
log_write(5LL, "No need to reallocate framebuffer and cache");
return 1;
}
log_write(5LL, "Freeing the framebuffer");
free(g_framebuffer);
v4 = g_fb_width;
if ( g_fb_width < a1 )
v4 = a1;
g_fb_width = v4;
v5 = g_fb_height;
if ( g_fb_height < a2 )
v5 = a2;
g_fb_height = v5;
}
else
{
g_fb_width = a1;
g_fb_height = a2;
}
v6 = 4LL * g_fb_height * g_fb_width;
g_framebuffer = malloc(v6);
if ( g_framebuffer )
{
log_write(5LL, "(Re)allocated framebuffer, %d bytes", v6);
v7 = allocate_enc_cache();
if ( v7 )
{
v8 = sizeof_enc_cache();
log_write(5LL, "(Re)allocated cache for encoded data, %d bytes", v8);
v7 = 1;
}
else
{
free(g_framebuffer);
g_framebuffer = 0LL;
log_write(1LL, "Error allocating cache for encoded data");
}
}
else
{
log_write(1LL, "Error allocating framebuffer");
v7 = 0;
}
return v7;
}

canonical_decompiled_code:
__int64 __fastcall func15(int a1, int a2)
{
__int16 v4; // di
__int16 v5; // si
size_t v6; // rbx
unsigned int v7; // ebx
unsigned int v8; // eax

if ( g_framebuffer )
{
if ( g_fb_width >= a1 && g_fb_height >= a2 )
{
log_write(5LL, "No need to reallocate framebuffer and cache");
return 1;
}
log_write(5LL, "Freeing the framebuffer");
free(g_framebuffer);
v4 = g_fb_width;
if ( g_fb_width < a1 )
v4 = a1;
g_fb_width = v4;
v5 = g_fb_height;
if ( g_fb_height < a2 )
v5 = a2;
g_fb_height = v5;
}
else
{
g_fb_width = a1;
g_fb_height = a2;
}
v6 = 4LL * g_fb_height * g_fb_width;
g_framebuffer = malloc(v6);
if ( g_framebuffer )
{
log_write(5LL, "(Re)allocated framebuffer, %d bytes", v6);
v7 = allocate_enc_cache();
if ( v7 )
{
v8 = sizeof_enc_cache();
log_write(5LL, "(Re)allocated cache for encoded data, %d bytes", v8);
v7 = 1;
}
else
{
free(g_framebuffer);
g_framebuffer = 0LL;
log_write(1LL, "Error allocating cache for encoded data");
}
}
else
{
log_write(1LL, "Error allocating framebuffer");
v7 = 0;
}
return v7;
}

original_code:
int alloc_framebuffer(int w, int h)
{
  int fb_size;
  if (g_framebuffer == ((void *)0)) {
    g_fb_width = w;
    g_fb_height = h;
  } else {
    if (w <= g_fb_width && h <= g_fb_height) {
      log_write(5, "No need to reallocate framebuffer and cache");
      return 1;
    }
    log_write(5, "Freeing the framebuffer");
    free(g_framebuffer);
    g_fb_width = (w > g_fb_width) ? w : g_fb_width;
    g_fb_height = (h > g_fb_height) ? h : g_fb_height;
  }
  fb_size = (int)g_fb_width * (int)g_fb_height;
  g_framebuffer = malloc(fb_size * sizeof(u_int32_t));
  if (g_framebuffer == ((void *)0)) {
    log_write(1, "Error allocating framebuffer");
    return 0;
  }
  log_write(5, "(Re)allocated framebuffer, %d bytes",
            fb_size * sizeof(u_int32_t));
  if (!allocate_enc_cache()) {
    free(g_framebuffer);
    g_framebuffer = ((void *)0);
    log_write(1, "Error allocating cache for encoded data");
    return 0;
  }
  log_write(5, "(Re)allocated cache for encoded data, %d bytes",
            sizeof_enc_cache());
  return 1;
}

variable_types: w int

variable_types: h int