decompiled_code:
__int64 __fastcall inat_get_avx_attribute(unsigned __int8 a1, unsigned __int8 a2, unsigned __int8 a3)
{
__int64 v4; // r13
int v5; // eax
__int64 result; // rax

if ( a2 > 0x1Fu )
return 0LL;
if ( a3 > 3u )
return 0LL;
v4 = inat_avx_tables[4 * a2];
if ( !v4 )
return 0LL;
v5 = inat_is_group(*(_DWORD *)(v4 + 4LL * a1));
if ( !a3 || v5 || (v4 = inat_avx_tables[4 * a2 + a3]) != 0 )
result = *(unsigned int *)(v4 + 4LL * a1);
else
result = 0LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(unsigned __int8 a1, unsigned __int8 a2, unsigned __int8 a3)
{
__int64 v4; // r13
int v5; // eax
__int64 result; // rax

if ( a2 > 0x1Fu )
return 0LL;
if ( a3 > 3u )
return 0LL;
v4 = inat_avx_tables[4 * a2];
if ( !v4 )
return 0LL;
v5 = func2(*(_DWORD *)(v4 + 4LL * a1));
if ( !a3 || v5 || (v4 = inat_avx_tables[4 * a2 + a3]) != 0 )
result = *(unsigned int *)(v4 + 4LL * a1);
else
result = 0LL;
return result;
}

original_code:
insn_attr_t inat_get_avx_attribute(insn_byte_t opcode, insn_byte_t vex_m,
       insn_byte_t vex_p)
{
 const insn_attr_t *table;
 if (vex_m > 0x1f || vex_p > 3)
  return 0;
 table = inat_avx_tables[vex_m][0];
 if (!table)
  return 0;
 if (!inat_is_group(table[opcode]) && vex_p) {
  table = inat_avx_tables[vex_m][vex_p];
  if (!table)
   return 0;
 }
 return table[opcode];
}

canonical_original_code:
unsigned int inat_get_avx_attribute(unsigned char opcode, unsigned char vex_m, unsigned char vex_p)
{
 unsigned int *table;
 if (vex_m > 0x1f || vex_p > 3)
  return 0;
 table = inat_avx_tables[vex_m][0];
 if (!table)
  return 0;
 if (!inat_is_group(table[opcode]) && vex_p) {
  table = inat_avx_tables[vex_m][vex_p];
  if (!table)
   return 0;
 }
 return table[opcode];
}


decompiled_code:
void __noreturn qbbs_chat_exit()
{
int i; // ebx
_QWORD *v1; // rbx
void *v2; // rdi

loginfo("<qbbschat> shutting down");
for ( i = 0; i != 256; ++i )
close(i);
v1 = chatlist_top;
chatlist = chatlist_top;
if ( chatlist_top )
{
do
{
v2 = v1;
v1 = (_QWORD *)v1[134];
free(v2);
chatlist = v1;
}
while ( v1 );
}
exit(0);
}

canonical_decompiled_code:
void __noreturn func0()
{
int i; // ebx
_QWORD *v1; // rbx
void *v2; // rdi

loginfo("<qbbschat> shutting down");
for ( i = 0; i != 256; ++i )
close(i);
v1 = chatlist_top;
chatlist = chatlist_top;
if ( chatlist_top )
{
do
{
v2 = v1;
v1 = (_QWORD *)v1[134];
free(v2);
chatlist = v1;
}
while ( v1 );
}
exit(0);
}

original_code:
void qbbs_chat_exit()
{
    int fd;
    struct chat_list *ptr;
    loginfo("<qbbschat> shutting down");
    for (fd = 0; fd < 256; fd++)
        close(fd);
    chatlist = chatlist_top;
    while(chatlist != ((void *)0))
    {
        ptr = chatlist->next;
        free(chatlist);
        chatlist = ptr;
    }
    exit(0);
}

variable_types: fd int

variable_types: struct chat_list { struct qbbs_chat_list { unsigned char command; char name[26]; long type; char data[1024]; } chat; unsigned char available; struct chat_list *next; } *

return_type: void

user_defined_types: struct chat_list { struct qbbs_chat_list chat; unsigned char available; struct chat_list *next; }


repo: dywisor/diskid

decompiled_code:
void __fastcall set_disk_type_none(__int64 a1)
{
*(_DWORD *)(a1 + 24) = 0;
}

canonical_decompiled_code:
void __fastcall func0(__int64 a1)
{
*(_DWORD *)(a1 + 24) = 0;
}

original_code:
static inline void set_disk_type_none ( struct disk_info* const pnode ) {
   pnode->type = DISK_TYPE_NONE;
}

canonical_original_code:
static inline void set_disk_type_none(struct disk_info *pnode) {
   pnode->type = DISK_TYPE_NONE;
}

variable_types: struct disk_info { char *device; char *name; char *var_name; enum disk_type { DISK_TYPE_NONE=0, DISK_TYPE_ATA=1, DISK_TYPE_SCSI=2, DISK_TYPE_ALL=expr } type; int fd; char *disk_id; } *

user_defined_types: struct disk_info { char *device; char *name; char *var_name; enum disk_type type; int fd; char *disk_id; }


decompiled_code:
void __fastcall set_disk_type_ata(__int64 a1)
{
*(_DWORD *)(a1 + 24) = 1;
}

canonical_decompiled_code:
void __fastcall func1(__int64 a1)
{
*(_DWORD *)(a1 + 24) = 1;
}

original_code:
static inline void set_disk_type_ata ( struct disk_info* const pnode ) {
   pnode->type = DISK_TYPE_ATA;
}

canonical_original_code:
static inline void set_disk_type_ata(struct disk_info *pnode) {
   pnode->type = DISK_TYPE_ATA;
}

user_defined_types: enum disk_type { DISK_TYPE_NONE=0, DISK_TYPE_ATA=1, DISK_TYPE_SCSI=2, DISK_TYPE_ALL=expr }


decompiled_code:
char *__fastcall get_uppercase(char *s)
{
char *v1; // rbx
unsigned __int64 v2; // rax

v1 = strdup(s);
v2 = strlen(s);
convert_to_uppercase((__int64)v1, 0LL, v2);
return v1;
}

canonical_decompiled_code:
char *__fastcall func3(char *s)
{
char *v1; // rbx
unsigned __int64 v2; // rax

v1 = strdup(s);
v2 = strlen(s);
func2((__int64)v1, 0LL, v2);
return v1;
}

original_code:
static inline char* get_uppercase ( const char* const str ) {
   char* upstr;
   upstr = strdup ( str );
   convert_to_uppercase ( upstr, 0, strlen ( str ) );
   return upstr;
}

canonical_original_code:
static inline char *get_uppercase(char *str) {
   char *upstr;
   upstr = strdup ( str );
   convert_to_uppercase ( upstr, 0, strlen ( str ) );
   return upstr;
}

variable_types:
str
char *

variable_types:
upstr
char *

return_type: char *


decompiled_code:
void __fastcall close_disk_info(void *ptr)
{
int v2; // edi
void *v3; // rdi
void *v4; // rdi

v2 = *((_DWORD *)ptr + 7);
if ( v2 >= 0 )
close(v2);
v3 = (void *)*((_QWORD *)ptr + 4);
if ( v3 )
free(v3);
v4 = (void *)*((_QWORD *)ptr + 2);
if ( v4 )
free(v4);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func6(void *ptr)
{
int v2; // edi
void *v3; // rdi
void *v4; // rdi

v2 = *((_DWORD *)ptr + 7);
if ( v2 >= 0 )
close(v2);
v3 = (void *)*((_QWORD *)ptr + 4);
if ( v3 )
free(v3);
v4 = (void *)*((_QWORD *)ptr + 2);
if ( v4 )
free(v4);
free(ptr);
}

original_code:
static inline void close_disk_info ( struct disk_info* pnode ) {
   if ( pnode->fd >= 0 ) {
      close ( pnode->fd );
      pnode->fd = -1;
   }
   if ( pnode->disk_id != ((void *)0) ) {
      free ( pnode->disk_id );
      pnode->disk_id = ((void *)0);
   }
   if ( pnode->var_name != ((void *)0) ) {
      free ( pnode->var_name );
   }
   free(pnode);
}

canonical_original_code:
static inline void close_disk_info(struct disk_info *pnode) {
   if ( pnode->fd >= 0 ) {
      close ( pnode->fd );
      pnode->fd = -1;
   }
   if ( pnode->disk_id != ((void *)0) ) {
      free ( pnode->disk_id );
      pnode->disk_id = ((void *)0);
   }
   if ( pnode->var_name != ((void *)0) ) {
      free ( pnode->var_name );
   }
   free(pnode);
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // er14
int v4; // er13
int v5; // er15
char *v6; // rax
int v7; // eax
int v8; // ebx
const char **v10; // r12
const char **v11; // r15
_QWORD *v12; // rax
void *v13; // rbp
unsigned int v14; // [rsp+Ch] [rbp-3Ch]

v3 = 0;
v4 = 0;
v5 = 0;
while ( 1 )
{
v7 = getopt_long(argc, (char *const *)argv, "xhm", &long_options_0, 0LL);
if ( v7 == -1 )
break;
switch ( v7 )
{
case 'm':
v3 = 1;
break;
case 'x':
v4 = 1;
break;
case 'h':
v6 = _xpg_basename((char *)*argv);
fprintf(
stdout,
"Usage: %s [-h] [-x] [-m] [<DEVICE>...]\n"
" -h, --help print this help message and exit\n"
" -x, --export print environment variables\n"
" -m, --mdev print environment variables for mdev\n"
"\n",
v6);
v5 = 1;
break;
default:
fflush(stdout);
fflush(stderr);
return 1;
}
}
if ( v5 == 1 )
{
v8 = 0;
goto LABEL_19;
}
if ( optind >= argc )
{
fwrite("no device specified\n", 1uLL, 0x14uLL, stderr);
v8 = 1;
LABEL_19:
fflush(stdout);
fflush(stderr);
return v8;
}
v14 = argc - optind;
v10 = &argv[optind];
v11 = &(&argv[optind])[(unsigned __int64)(unsigned int)(argc + ~optind) + 1];
while ( 1 )
{
v12 = init_disk_info((char *)*v10);
v13 = v12;
if ( !v12 )
{
fprintf(stderr, "failed to open device '%s'\n", *v10);
v8 = 1;
goto LABEL_19;
}
v8 = handle_device((__int64)v12, v4, v3, 3, v14);
if ( v8 )
break;
if ( ++v10 == v11 )
goto LABEL_22;
}
v8 = 1;
LABEL_22:
fflush(stdout);
fflush(stderr);
close_disk_info(v13);
return v8;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // er14
int v4; // er13
int v5; // er15
char *v6; // rax
int v7; // eax
int v8; // ebx
const char **v10; // r12
const char **v11; // r15
_QWORD *v12; // rax
void *v13; // rbp
unsigned int v14; // [rsp+Ch] [rbp-3Ch]

v3 = 0;
v4 = 0;
v5 = 0;
while ( 1 )
{
v7 = getopt_long(argc, (char *const *)argv, "xhm", &long_options_0, 0LL);
if ( v7 == -1 )
break;
switch ( v7 )
{
case 'm':
v3 = 1;
break;
case 'x':
v4 = 1;
break;
case 'h':
v6 = _xpg_basename((char *)*argv);
fprintf(
stdout,
"Usage: %s [-h] [-x] [-m] [<DEVICE>...]\n"
" -h, --help print this help message and exit\n"
" -x, --export print environment variables\n"
" -m, --mdev print environment variables for mdev\n"
"\n",
v6);
v5 = 1;
break;
default:
fflush(stdout);
fflush(stderr);
return 1;
}
}
if ( v5 == 1 )
{
v8 = 0;
goto LABEL_19;
}
if ( optind >= argc )
{
fwrite("no device specified\n", 1uLL, 0x14uLL, stderr);
v8 = 1;
LABEL_19:
fflush(stdout);
fflush(stderr);
return v8;
}
v14 = argc - optind;
v10 = &argv[optind];
v11 = &(&argv[optind])[(unsigned __int64)(unsigned int)(argc + ~optind) + 1];
while ( 1 )
{
v12 = init_disk_info((char *)*v10);
v13 = v12;
if ( !v12 )
{
fprintf(stderr, "failed to open device '%s'\n", *v10);
v8 = 1;
goto LABEL_19;
}
v8 = func5((__int64)v12, v4, v3, 3, v14);
if ( v8 )
break;
if ( ++v10 == v11 )
goto LABEL_22;
}
v8 = 1;
LABEL_22:
fflush(stdout);
fflush(stderr);
func6(v13);
return v8;
}

original_code:
int main ( const int argc, char* const* argv ) {
   int retcode = 0;
   struct disk_info* node = ((void *)0);
   int i;
   unsigned int node_count;
   unsigned int exit_after_getopt;
   unsigned int want_export;
   unsigned int want_mdev_export;
   static const struct option const long_options[] = {
      { "export", 0, ((void *)0), 'x' },
      { "mdev", 0, ((void *)0), 'm' },
      { "help", 0, ((void *)0), 'h' },
      {0}
   };
   exit_after_getopt = 0;
   want_export = 0;
   want_mdev_export = 0;
   while (
      ( i = getopt_long ( argc, argv, "xhm", long_options, ((void *)0) ) ) != -1
   ) {
      switch ( i ) {
         case 'h':
            fprintf ( stdout,
               (
                  "Usage: %s [-h] [-x] [-m] [<DEVICE>...]\n"
                  "  -h, --help           print this help message and exit\n"
                  "  -x, --export         print environment variables\n"
                  "  -m, --mdev           print environment variables for mdev\n"
                  "\n"
               ), __xpg_basename(argv[0])
            );
            exit_after_getopt = 1;
            break;
         case 'x':
            want_export = 1;
            break;
         case 'm':
            want_mdev_export = 1;
            break;
         default:
            retcode = 1;
            goto main_exit;
      }
   }
   if ( exit_after_getopt == 1 ) {
      goto main_exit;
   } else if ( optind < argc ) {
      node_count = (unsigned int)(argc - optind);
      for ( i = optind; i < argc; i++ ) {
         node = init_disk_info ( argv[i] );
         if ( node == ((void *)0) ) {
            fprintf ( stderr, "failed to open device '%s'\n", argv[i] );
            retcode = 1;
            goto main_exit;
         } else if (
            handle_device (
               node, want_export, want_mdev_export, DISK_TYPE_ALL, node_count
            ) != 0
         ) {
            retcode = 1;
            goto main_exit;
         }
      }
   } else {
      fprintf ( stderr, "no device specified\n" );
      retcode = 1;
      goto main_exit;
   }
main_exit:
   fflush ( stdout );
   fflush ( stderr );
   if ( node != ((void *)0) ) {
      close_disk_info ( node );
      node = ((void *)0);
   }
   return retcode;
}

canonical_original_code:
int main(int argc, char **argv) {
   int retcode = 0;
   struct disk_info *node = ((void *)0);
   int i;
   unsigned int node_count;
   unsigned int exit_after_getopt;
   unsigned int want_export;
   unsigned int want_mdev_export;
   static struct option long_options[] = {
      { "export", 0, ((void *)0), 'x' },
      { "mdev", 0, ((void *)0), 'm' },
      { "help", 0, ((void *)0), 'h' },
      {0}
   };
   exit_after_getopt = 0;
   want_export = 0;
   want_mdev_export = 0;
   while (
      ( i = getopt_long ( argc, argv, "xhm", long_options, ((void *)0) ) ) != -1
   ) {
      switch ( i ) {
         case 'h':
            fprintf ( stdout,
               (
                  "Usage: %s [-h] [-x] [-m] [<DEVICE>...]\n"
                  "  -h, --help           print this help message and exit\n"
                  "  -x, --export         print environment variables\n"
                  "  -m, --mdev           print environment variables for mdev\n"
                  "\n"
               ), __xpg_basename(argv[0])
            );
            exit_after_getopt = 1;
            break;
         case 'x':
            want_export = 1;
            break;
         case 'm':
            want_mdev_export = 1;
            break;
         default:
            retcode = 1;
            goto main_exit;
      }
   }
   if ( exit_after_getopt == 1 ) {
      goto main_exit;
   } else if ( optind < argc ) {
      node_count = (unsigned int)(argc - optind);
      for ( i = optind; i < argc; i++ ) {
         node = init_disk_info ( argv[i] );
         if ( node == ((void *)0) ) {
            fprintf ( stderr, "failed to open device '%s'\n", argv[i] );
            retcode = 1;
            goto main_exit;
         } else if (
            handle_device (
               node, want_export, want_mdev_export, DISK_TYPE_ALL, node_count
            ) != 0
         ) {
            retcode = 1;
            goto main_exit;
         }
      }
   } else {
      fprintf ( stderr, "no device specified\n" );
      retcode = 1;
      goto main_exit;
   }
main_exit:
   fflush ( stdout );
   fflush ( stderr );
   if ( node != ((void *)0) ) {
      close_disk_info ( node );
      node = ((void *)0);
   }
   return retcode;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: retcode int

variable_types: i int

variable_types: struct option { char *name; int has_arg; int *flag; int val; } []

return_type: int


decompiled_code:
__int64 __fastcall char_in_range(char a1, char a2, char a3)
{
return (a1 > a3) | (unsigned __int8)(a1 < a2);
}

canonical_decompiled_code:
__int64 __fastcall func0(char a1, char a2, char a3)
{
return (a1 > a3) | (unsigned __int8)(a1 < a2);
}

original_code:
static inline int char_in_range (
   const char c, const char low, const char high
) {
   return ( c >= low && c <= high ) ? 0 : 1;
}

canonical_original_code:
static inline int char_in_range(char c, char low, char high) {
   return ( c >= low && c <= high ) ? 0 : 1;
}

variable_types: c char

variable_types: low char

variable_types: high char


decompiled_code:
_BOOL8 __fastcall is_unicode_valid(unsigned int a1)
{
_BOOL8 result; // rax

result = 0LL;
if ( a1 <= 0x10FFFF && (a1 & 0xFFFFF800) != 55296 && a1 - 64976 > 0x1F )
result = (a1 & 0xFFFE) != 65534;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func1(unsigned int a1)
{
_BOOL8 result; // rax

result = 0LL;
if ( a1 <= 0x10FFFF && (a1 & 0xFFFFF800) != 55296 && a1 - 64976 > 0x1F )
result = (a1 & 0xFFFE) != 65534;
return result;
}

original_code:
static inline int is_unicode_valid(const uint32_t ch) {
        if (ch >= 0x110000)
                return 0;
        if ((ch & 0xFFFFF800) == 0xD800)
                return 0;
        if ((ch >= 0xFDD0) && (ch <= 0xFDEF))
                return 0;
        if ((ch & 0xFFFE) == 0xFFFE)
                return 0;
        return 1;
}

canonical_original_code:
static inline int is_unicode_valid(unsigned int ch) {
        if (ch >= 0x110000)
                return 0;
        if ((ch & 0xFFFFF800) == 0xD800)
                return 0;
        if ((ch >= 0xFDD0) && (ch <= 0xFDEF))
                return 0;
        if ((ch & 0xFFFE) == 0xFFFE)
                return 0;
        return 1;
}

variable_types:
ch
unsigned int


decompiled_code:
__int64 __fastcall utf8_unichar_to_encoded_len(int a1)
{
__int64 result; // rax

result = 1LL;
if ( a1 > 127 )
{
result = 2LL;
if ( a1 > 2047 )
{
result = 3LL;
if ( a1 > 0xFFFF )
{
result = 4LL;
if ( a1 > 0x1FFFFF )
result = (unsigned int)(a1 > 0x3FFFFFF) + 5;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1)
{
__int64 result; // rax

result = 1LL;
if ( a1 > 127 )
{
result = 2LL;
if ( a1 > 2047 )
{
result = 3LL;
if ( a1 > 0xFFFF )
{
result = 4LL;
if ( a1 > 0x1FFFFF )
result = (unsigned int)(a1 > 0x3FFFFFF) + 5;
}
}
}
return result;
}

original_code:
static int utf8_unichar_to_encoded_len(const int unichar) {
        if (unichar < 0x80)
                return 1;
        if (unichar < 0x800)
                return 2;
        if (unichar < 0x10000)
                return 3;
        if (unichar < 0x200000)
                return 4;
        if (unichar < 0x4000000)
                return 5;
        return 6;
}

canonical_original_code:
static int utf8_unichar_to_encoded_len(int unichar) {
        if (unichar < 0x80)
                return 1;
        if (unichar < 0x800)
                return 2;
        if (unichar < 0x10000)
                return 3;
        if (unichar < 0x200000)
                return 4;
        if (unichar < 0x4000000)
                return 5;
        return 6;
}

variable_types: unichar int


decompiled_code:
__int64 __fastcall utf8_encoded_to_unichar(char *a1)
{
int v2; // edi
unsigned int v3; // edx
__int64 v4; // rcx

v2 = utf8_encoded_expected_len(a1);
switch ( v2 )
{
case 1:
return (unsigned int)*a1;
case 2:
v3 = *a1 & 0x1F;
goto LABEL_4;
case 3:
v3 = *a1 & 0xF;
goto LABEL_4;
case 4:
v3 = *a1 & 7;
goto LABEL_4;
case 5:
v3 = *a1 & 3;
goto LABEL_4;
case 6:
v3 = *a1 & 1;
LABEL_4:
v4 = 1LL;
break;
default:
return (unsigned int)-1;
}
while ( (a1[v4] & 0xC0) == 128 )
{
v3 = a1[v4++] & 0x3F | (v3 << 6);
if ( v2 <= (int)v4 )
return v3;
}
return (unsigned int)-1;
}

canonical_decompiled_code:
__int64 __fastcall func4(char *a1)
{
int v2; // edi
unsigned int v3; // edx
__int64 v4; // rcx

v2 = func2(a1);
switch ( v2 )
{
case 1:
return (unsigned int)*a1;
case 2:
v3 = *a1 & 0x1F;
goto LABEL_4;
case 3:
v3 = *a1 & 0xF;
goto LABEL_4;
case 4:
v3 = *a1 & 7;
goto LABEL_4;
case 5:
v3 = *a1 & 3;
goto LABEL_4;
case 6:
v3 = *a1 & 1;
LABEL_4:
v4 = 1LL;
break;
default:
return (unsigned int)-1;
}
while ( (a1[v4] & 0xC0) == 128 )
{
v3 = a1[v4++] & 0x3F | (v3 << 6);
if ( v2 <= (int)v4 )
return v3;
}
return (unsigned int)-1;
}

original_code:
static int utf8_encoded_to_unichar(const char* const str) {
        int unichar;
        int len;
        int i;
        len = utf8_encoded_expected_len(str);
        switch (len) {
        case 1:
                return (int)str[0];
        case 2:
                unichar = str[0] & 0x1f;
                break;
        case 3:
                unichar = (int)str[0] & 0x0f;
                break;
        case 4:
                unichar = (int)str[0] & 0x07;
                break;
        case 5:
                unichar = (int)str[0] & 0x03;
                break;
        case 6:
                unichar = (int)str[0] & 0x01;
                break;
        default:
                return -1;
        }
        for (i = 1; i < len; i++) {
                if (((int)str[i] & 0xc0) != 0x80)
                        return -1;
                unichar <<= 6;
                unichar |= (int)str[i] & 0x3f;
        }
        return unichar;
}

canonical_original_code:
static int utf8_encoded_to_unichar(char *str) {
        int unichar;
        int len;
        int i;
        len = utf8_encoded_expected_len(str);
        switch (len) {
        case 1:
                return (int)str[0];
        case 2:
                unichar = str[0] & 0x1f;
                break;
        case 3:
                unichar = (int)str[0] & 0x0f;
                break;
        case 4:
                unichar = (int)str[0] & 0x07;
                break;
        case 5:
                unichar = (int)str[0] & 0x03;
                break;
        case 6:
                unichar = (int)str[0] & 0x01;
                break;
        default:
                return -1;
        }
        for (i = 1; i < len; i++) {
                if (((int)str[i] & 0xc0) != 0x80)
                        return -1;
                unichar <<= 6;
                unichar |= (int)str[i] & 0x3f;
        }
        return unichar;
}

variable_types: len int


decompiled_code:
__int64 __fastcall utf8_encoded_valid_unichar(char *a1)
{
int v1; // eax
unsigned int v2; // ebx
char *v3; // rax
unsigned int v4; // ebp

v1 = utf8_encoded_expected_len(a1);
if ( !v1 )
return (unsigned int)-1;
v2 = v1;
if ( v1 != 1 )
{
if ( v1 <= 0 )
{
LABEL_7:
v4 = utf8_encoded_to_unichar(a1);
if ( (unsigned int)utf8_unichar_to_encoded_len(v4) == v2 )
{
if ( !is_unicode_valid(v4) )
v2 = -1;
}
else
{
v2 = -1;
}
}
else
{
v3 = a1;
while ( *v3 < 0 )
{
if ( ++v3 == &a1[v2] )
goto LABEL_7;
}
v2 = -1;
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func5(char *a1)
{
int v1; // eax
unsigned int v2; // ebx
char *v3; // rax
unsigned int v4; // ebp

v1 = func2(a1);
if ( !v1 )
return (unsigned int)-1;
v2 = v1;
if ( v1 != 1 )
{
if ( v1 <= 0 )
{
LABEL_7:
v4 = func4(a1);
if ( (unsigned int)func3(v4) == v2 )
{
if ( !func1(v4) )
v2 = -1;
}
else
{
v2 = -1;
}
}
else
{
v3 = a1;
while ( *v3 < 0 )
{
if ( ++v3 == &a1[v2] )
goto LABEL_7;
}
v2 = -1;
}
}
return v2;
}

original_code:
static int utf8_encoded_valid_unichar(const char* const str) {
        int len;
        int unichar;
        int i;
        len = utf8_encoded_expected_len(str);
        if (len == 0)
                return -1;
        if (len == 1)
                return 1;
        for (i = 0; i < len; i++)
                if ((str[i] & 0x80) != 0x80)
                        return -1;
        unichar = utf8_encoded_to_unichar(str);
        if (utf8_unichar_to_encoded_len(unichar) != len)
                return -1;
        if (is_unicode_valid(unichar) == 0)
                return -1;
        return len;
}

canonical_original_code:
static int utf8_encoded_valid_unichar(char *str) {
        int len;
        int unichar;
        int i;
        len = utf8_encoded_expected_len(str);
        if (len == 0)
                return -1;
        if (len == 1)
                return 1;
        for (i = 0; i < len; i++)
                if ((str[i] & 0x80) != 0x80)
                        return -1;
        unichar = utf8_encoded_to_unichar(str);
        if (utf8_unichar_to_encoded_len(unichar) != len)
                return -1;
        if (is_unicode_valid(unichar) == 0)
                return -1;
        return len;
}


decompiled_code:
size_t __fastcall util_strnlen(const char *a1, size_t a2)
{
return strnlen(a1, a2);
}

canonical_decompiled_code:
size_t __fastcall func6(const char *a1, size_t a2)
{
return strnlen(a1, a2);
}

original_code:
static inline size_t util_strnlen ( const char* s, size_t maxlen ) {
   return strnlen ( s, maxlen );
}

canonical_original_code:
static inline unsigned long util_strnlen(char *s, unsigned long maxlen) {
   return strnlen ( s, maxlen );
}

variable_types:
s
char *

variable_types:
maxlen
unsigned long

return_type: unsigned long


decompiled_code:
__int64 __fastcall whitelisted_char_for_devnode(char a1, const char *a2)
{
unsigned int v2; // ebp

v2 = 1;
if ( !(unsigned int)char_in_range(a1, 48, 57) && !(unsigned int)char_in_range(a1, 65, 90) )
{
v2 = char_in_range(a1, 97, 122);
if ( v2 )
{
v2 = 1;
}
else if ( strchr("#+-.:=@_", a1) )
{
v2 = 1;
}
else if ( a2 )
{
v2 = strchr(a2, a1) != 0LL;
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func7(char a1, const char *a2)
{
unsigned int v2; // ebp

v2 = 1;
if ( !(unsigned int)func0(a1, 48, 57) && !(unsigned int)func0(a1, 65, 90) )
{
v2 = func0(a1, 97, 122);
if ( v2 )
{
v2 = 1;
}
else if ( strchr("#+-.:=@_", a1) )
{
v2 = 1;
}
else if ( a2 )
{
v2 = strchr(a2, a1) != 0LL;
}
}
return v2;
}

original_code:
static inline int whitelisted_char_for_devnode (
   const char c, const char* const white
) {
   return (
      char_in_range ( c, '0', '9' ) ||
      char_in_range ( c, 'A', 'Z' ) ||
      char_in_range ( c, 'a', 'z' ) ||
      strchr("#+-.:=@_", c) ||
      ( white != ((void *)0) && strchr(white, c) != ((void *)0) )
   ) ? 1 : 0;
}

canonical_original_code:
static inline int whitelisted_char_for_devnode(char c, char *white) {
   return (
      char_in_range ( c, '0', '9' ) ||
      char_in_range ( c, 'A', 'Z' ) ||
      char_in_range ( c, 'a', 'z' ) ||
      strchr("#+-.:=@_", c) ||
      ( white != ((void *)0) && strchr(white, c) != ((void *)0) )
   ) ? 1 : 0;
}

variable_types:
white
char *


original_code:
int util_replace_whitespace (
   const char* const str, char* const to, size_t len
) {
        size_t i, j;
        len = util_strnlen(str, len);
        while (len && ((*__ctype_b_loc ())[(int) ((str[len-1]))] & (unsigned short int) _ISspace))
                len--;
        i = 0;
        while (((*__ctype_b_loc ())[(int) ((str[i]))] & (unsigned short int) _ISspace) && (i < len))
                i++;
        j = 0;
        while (i < len) {
                if (((*__ctype_b_loc ())[(int) ((str[i]))] & (unsigned short int) _ISspace)) {
                        while (((*__ctype_b_loc ())[(int) ((str[i]))] & (unsigned short int) _ISspace))
                                i++;
                        to[j++] = '_';
                }
                to[j++] = str[i++];
        }
        to[j] = '\0';
        return 0;
}

canonical_original_code:
int util_replace_whitespace(char *str, char *to, unsigned long len) {
        unsigned long i; unsigned long j;
        len = util_strnlen(str, len);
        while (len && ((*__ctype_b_loc ())[(int) ((str[len-1]))] & (unsigned short) _ISspace))
                len--;
        i = 0;
        while (((*__ctype_b_loc ())[(int) ((str[i]))] & (unsigned short) _ISspace) && (i < len))
                i++;
        j = 0;
        while (i < len) {
                if (((*__ctype_b_loc ())[(int) ((str[i]))] & (unsigned short) _ISspace)) {
                        while (((*__ctype_b_loc ())[(int) ((str[i]))] & (unsigned short) _ISspace))
                                i++;
                        to[j++] = '_';
                }
                to[j++] = str[i++];
        }
        to[j] = '\0';
        return 0;
}

variable_types:
to
char *

variable_types:
len
unsigned long

variable_types:
i
unsigned long

variable_types:
j
unsigned long


decompiled_code:
__int64 __fastcall util_replace_chars(char *a1, const char *a2)
{
char v2; // bl
char *v3; // r12
unsigned int v4; // er15
__int64 v5; // rbp
int v6; // eax

v2 = *a1;
if ( !*a1 )
return 0;
v3 = a1;
v4 = 0;
v5 = 0LL;
do
{
if ( (unsigned int)whitelisted_char_for_devnode(v2, a2) )
{
++v5;
}
else if ( v2 == 92 && a1[v5 + 1] == 120 )
{
v5 += 2LL;
}
else
{
v6 = utf8_encoded_valid_unichar(v3);
if ( v6 > 1 )
{
v5 += v6;
}
else if ( ((*_ctype_b_loc())[v2] & 0x2000) != 0 && a2 && strchr(a2, 32) )
{
*v3 = 32;
++v5;
++v4;
}
else
{
*v3 = 95;
++v5;
++v4;
}
}
v3 = &a1[v5];
v2 = a1[v5];
}
while ( v2 );
return v4;
}

original_code:
int util_replace_chars ( char* const str, const char* const white ) {
        size_t i = 0;
        int replaced = 0;
        while (str[i] != '\0') {
                int len;
                if (whitelisted_char_for_devnode(str[i], white)) {
                        i++;
                        continue;
                }
                if (str[i] == '\\' && str[i+1] == 'x') {
                        i += 2;
                        continue;
                }
                len = utf8_encoded_valid_unichar(&str[i]);
                if (len > 1) {
                        i += len;
                        continue;
                }
                if (((*__ctype_b_loc ())[(int) ((str[i]))] & (unsigned short int) _ISspace) && white != ((void *)0) && strchr(white, ' ') != ((void *)0)) {
                        str[i] = ' ';
                        i++;
                        replaced++;
                        continue;
                }
                str[i] = '_';
                i++;
                replaced++;
        }
        return replaced;
}

canonical_original_code:
int util_replace_chars(char *str, char *white) {
        unsigned long i = 0;
        int replaced = 0;
        while (str[i] != '\0') {
                int len;
                if (whitelisted_char_for_devnode(str[i], white)) {
                        i++;
                        continue;
                }
                if (str[i] == '\\' && str[i+1] == 'x') {
                        i += 2;
                        continue;
                }
                len = utf8_encoded_valid_unichar(&str[i]);
                if (len > 1) {
                        i += len;
                        continue;
                }
                if (((*__ctype_b_loc ())[(int) ((str[i]))] & (unsigned short) _ISspace) && white != ((void *)0) && strchr(white, ' ') != ((void *)0)) {
                        str[i] = ' ';
                        i++;
                        replaced++;
                        continue;
                }
                str[i] = '_';
                i++;
                replaced++;
        }
        return replaced;
}

variable_types:
str
char *

variable_types: replaced int


decompiled_code:
__int64 __fastcall encode_devnode_name(void *src, __int64 a2, __int64 a3)
{
char *v4; // r14
unsigned __int8 v5; // r13
__int64 v6; // r12
__int64 v7; // rbp
int v8; // eax
int v9; // ebx
__int64 v10; // r13

if ( !src || !a2 )
return 0xFFFFFFFFLL;
v4 = (char *)src;
v5 = *(_BYTE *)src;
if ( *(_BYTE *)src )
{
v6 = 0LL;
v7 = 0LL;
while ( 1 )
{
v8 = utf8_encoded_valid_unichar(v4);
v9 = v8;
if ( v8 <= 1 )
{
if ( v5 != 92 && (unsigned int)whitelisted_char_for_devnode(v5, 0LL) )
{
if ( a3 == v6 )
return 0xFFFFFFFFLL;
*(_BYTE *)(a2 + v6++) = v5;
}
else
{
if ( (unsigned __int64)(a3 - v6) <= 3 )
return 0xFFFFFFFFLL;
sprintf((char *)(a2 + v6), "\\x%02x", v5);
v6 += 4LL;
}
}
else
{
v10 = v8;
if ( a3 - v6 < (unsigned __int64)v8 )
return 0xFFFFFFFFLL;
memcpy((void *)(a2 + v6), v4, v8);
v6 += v10;
v7 += v9 - 1;
}
++v7;
v4 = (char *)src + v7;
v5 = *((_BYTE *)src + v7);
if ( !v5 )
goto LABEL_16;
}
}
v6 = 0LL;
LABEL_16:
if ( a3 == v6 )
return 0xFFFFFFFFLL;
*(_BYTE *)(a2 + v6) = 0;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func10(void *src, __int64 a2, __int64 a3)
{
char *v4; // r14
unsigned __int8 v5; // r13
__int64 v6; // r12
__int64 v7; // rbp
int v8; // eax
int v9; // ebx
__int64 v10; // r13

if ( !src || !a2 )
return 0xFFFFFFFFLL;
v4 = (char *)src;
v5 = *(_BYTE *)src;
if ( *(_BYTE *)src )
{
v6 = 0LL;
v7 = 0LL;
while ( 1 )
{
v8 = func5(v4);
v9 = v8;
if ( v8 <= 1 )
{
if ( v5 != 92 && (unsigned int)func7(v5, 0LL) )
{
if ( a3 == v6 )
return 0xFFFFFFFFLL;
*(_BYTE *)(a2 + v6++) = v5;
}
else
{
if ( (unsigned __int64)(a3 - v6) <= 3 )
return 0xFFFFFFFFLL;
sprintf((char *)(a2 + v6), "\\x%02x", v5);
v6 += 4LL;
}
}
else
{
v10 = v8;
if ( a3 - v6 < (unsigned __int64)v8 )
return 0xFFFFFFFFLL;
memcpy((void *)(a2 + v6), v4, v8);
v6 += v10;
v7 += v9 - 1;
}
++v7;
v4 = (char *)src + v7;
v5 = *((_BYTE *)src + v7);
if ( !v5 )
goto LABEL_16;
}
}
v6 = 0LL;
LABEL_16:
if ( a3 == v6 )
return 0xFFFFFFFFLL;
*(_BYTE *)(a2 + v6) = 0;
return 0LL;
}

original_code:
int encode_devnode_name (
   const char* const str, char* const str_enc, const size_t len
) {
        size_t i, j;
        if (str == ((void *)0) || str_enc == ((void *)0))
                return -1;
        for (i = 0, j = 0; str[i] != '\0'; i++) {
                int seqlen;
                seqlen = utf8_encoded_valid_unichar(&str[i]);
                if (seqlen > 1) {
                        if (len-j < (size_t)seqlen)
                                goto err;
                        memcpy(&str_enc[j], &str[i], seqlen);
                        j += seqlen;
                        i += (seqlen-1);
                } else if (str[i] == '\\' || !whitelisted_char_for_devnode(str[i],((void *)0))) {
                        if (len-j < 4)
                                goto err;
                        sprintf(&str_enc[j], "\\x%02x", (unsigned char) str[i]);
                        j += 4;
                } else {
                        if (len-j < 1)
                                goto err;
                        str_enc[j] = str[i];
                        j++;
                }
        }
        if (len-j < 1)
                goto err;
        str_enc[j] = '\0';
        return 0;
err:
        return -1;
}

canonical_original_code:
int encode_devnode_name(char *str, char *str_enc, unsigned long len) {
        unsigned long i; unsigned long j;
        if (str == ((void *)0) || str_enc == ((void *)0))
                return -1;
        for (i = 0, j = 0; str[i] != '\0'; i++) {
                int seqlen;
                seqlen = utf8_encoded_valid_unichar(&str[i]);
                if (seqlen > 1) {
                        if (len-j < (unsigned long)seqlen)
                                goto err;
                        memcpy(&str_enc[j], &str[i], seqlen);
                        j += seqlen;
                        i += (seqlen-1);
                } else if (str[i] == '\\' || !whitelisted_char_for_devnode(str[i],((void *)0))) {
                        if (len-j < 4)
                                goto err;
                        sprintf(&str_enc[j], "\\x%02x", (unsigned char) str[i]);
                        j += 4;
                } else {
                        if (len-j < 1)
                                goto err;
                        str_enc[j] = str[i];
                        j++;
                }
        }
        if (len-j < 1)
                goto err;
        str_enc[j] = '\0';
        return 0;
err:
        return -1;
}

variable_types: seqlen int


decompiled_code:
__int64 __fastcall set_disk_id(__int64 a1, const char *a2, const char *a3)
{
void *v5; // rdi
__int64 result; // rax

v5 = *(void **)(a1 + 32);
if ( v5 )
{
free(v5);
*(_QWORD *)(a1 + 32) = 0LL;
}
if ( !a2 )
return 1LL;
result = 1LL;
if ( *a2 )
{
if ( a3 && *a3 )
*(_QWORD *)(a1 + 32) = join_str_triple(a2, "_", a3);
else
*(_QWORD *)(a1 + 32) = strdup(a2);
result = 2 * (unsigned int)(*(_QWORD *)(a1 + 32) == 0LL);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, const char *a2, const char *a3)
{
void *v5; // rdi
__int64 result; // rax

v5 = *(void **)(a1 + 32);
if ( v5 )
{
free(v5);
*(_QWORD *)(a1 + 32) = 0LL;
}
if ( !a2 )
return 1LL;
result = 1LL;
if ( *a2 )
{
if ( a3 && *a3 )
*(_QWORD *)(a1 + 32) = func0(a2, "_", a3);
else
*(_QWORD *)(a1 + 32) = strdup(a2);
result = 2 * (unsigned int)(*(_QWORD *)(a1 + 32) == 0LL);
}
return result;
}

original_code:
int set_disk_id (
   struct disk_info* const node,
   const char* const model, const char* const serial
) {
   if ( node->disk_id != ((void *)0) ) {
      free ( node->disk_id );
      node->disk_id = ((void *)0);
   }
   if ( model == ((void *)0) || model[0] == '\0' ) {
      return 1;
   } else if ( serial == ((void *)0) || serial[0] == '\0' ) {
      node->disk_id = strdup ( model );
   } else {
      node->disk_id = join_str_triple ( model, "_", serial );
   }
   return ( node->disk_id != ((void *)0) ) ? 0 : 2;
}

canonical_original_code:
int set_disk_id(struct disk_info *node, char *model, char *serial) {
   if ( node->disk_id != ((void *)0) ) {
      free ( node->disk_id );
      node->disk_id = ((void *)0);
   }
   if ( model == ((void *)0) || model[0] == '\0' ) {
      return 1;
   } else if ( serial == ((void *)0) || serial[0] == '\0' ) {
      node->disk_id = strdup ( model );
   } else {
      node->disk_id = join_str_triple ( model, "_", serial );
   }
   return ( node->disk_id != ((void *)0) ) ? 0 : 2;
}

variable_types: struct disk_info { char *device; char *name; char *var_name; enum disk_type { DISK_TYPE_NONE=0, DISK_TYPE_ATA=1, DISK_TYPE_SCSI=2, DISK_TYPE_ALL=expr } type; int fd; char *disk_id; } *

variable_types:
model
char *

variable_types:
serial
char *


repo: dzeban/keymon

decompiled_code:
void __fastcall __noreturn keymon_connect(void *a1)
{
while ( 1 )
{
pthread_mutex_lock(&sk_mutex);
nl_sock_connect();
pthread_mutex_unlock(&sk_mutex);
sleep(5u);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func0(void *a1)
{
while ( 1 )
{
pthread_mutex_lock(&sk_mutex);
nl_sock_connect();
pthread_mutex_unlock(&sk_mutex);
sleep(5u);
}
}

original_code:
void *keymon_connect()
{
    while(1)
    {
        pthread_mutex_lock( &sk_mutex );
        nl_sock_connect();
        pthread_mutex_unlock( &sk_mutex );
        sleep(5);
    }
    pthread_exit( ((void *)0) );
}

return_type: void *


decompiled_code:
void __fastcall __noreturn keymon_receive(void *a1)
{
while ( 1 )
{
pthread_mutex_lock(&sk_mutex);
nl_sock_receive();
pthread_mutex_unlock(&sk_mutex);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func1(void *a1)
{
while ( 1 )
{
pthread_mutex_lock(&sk_mutex);
nl_sock_receive();
pthread_mutex_unlock(&sk_mutex);
}
}

original_code:
void *keymon_receive()
{
    while(1)
    {
        pthread_mutex_lock( &sk_mutex );
        nl_sock_receive();
        pthread_mutex_unlock( &sk_mutex );
    }
    pthread_exit( ((void *)0) );
}


decompiled_code:
__int64 thread_init()
{
int v0; // eax
unsigned int v1; // eax
unsigned int v2; // eax
unsigned int v3; // eax
unsigned int v4; // eax
unsigned int v5; // ebx
pthread_attr_t v7; // [rsp+0h] [rbp-148h] BYREF
__sigset_t oldmask; // [rsp+40h] [rbp-108h] BYREF
sigset_t set; // [rsp+C0h] [rbp-88h] BYREF

v0 = pthread_attr_init(&v7);
if ( v0 < 0 )
{
syslog(3, "pthread_attr_init failed, rc %d\n", (unsigned int)v0);
LABEL_11:
nl_sock_cleanup();
return (unsigned int)-1;
}
pthread_attr_setdetachstate(&v7, 0);
pthread_mutex_init(&sk_mutex, 0LL);
sigemptyset(&set);
sigaddset(&set, 10);
v1 = pthread_sigmask(0, &set, &oldmask);
if ( v1 )
goto LABEL_10;
v2 = pthread_create(&connector_thread, &v7, (void *(*)(void *))keymon_connect, 0LL);
if ( v2 )
{
syslog(3, "pthread_create for connector thread failed, rc %d\n", v2);
goto LABEL_11;
}
v3 = pthread_create(&receiver_thread, &v7, (void *(*)(void *))keymon_receive, 0LL);
if ( v3 )
{
syslog(3, "pthread_create for receiver thread failed, rc %d\n", v3);
goto LABEL_11;
}
v1 = pthread_sigmask(2, &oldmask, 0LL);
if ( v1 )
{
LABEL_10:
syslog(3, "pthread_sigmask failed, rc %d\n", v1);
goto LABEL_11;
}
v4 = pthread_attr_destroy(&v7);
v5 = v4;
if ( v4 )
{
syslog(3, "pthread_attr_destroy failed, rc %d\n", v4);
goto LABEL_11;
}
pthread_join(connector_thread, 0LL);
pthread_join(receiver_thread, 0LL);
return v5;
}

canonical_decompiled_code:
__int64 func2()
{
int v0; // eax
unsigned int v1; // eax
unsigned int v2; // eax
unsigned int v3; // eax
unsigned int v4; // eax
unsigned int v5; // ebx
pthread_attr_t v7; // [rsp+0h] [rbp-148h] BYREF
__sigset_t oldmask; // [rsp+40h] [rbp-108h] BYREF
sigset_t set; // [rsp+C0h] [rbp-88h] BYREF

v0 = pthread_attr_init(&v7);
if ( v0 < 0 )
{
syslog(3, "pthread_attr_init failed, rc %d\n", (unsigned int)v0);
LABEL_11:
nl_sock_cleanup();
return (unsigned int)-1;
}
pthread_attr_setdetachstate(&v7, 0);
pthread_mutex_init(&sk_mutex, 0LL);
sigemptyset(&set);
sigaddset(&set, 10);
v1 = pthread_sigmask(0, &set, &oldmask);
if ( v1 )
goto LABEL_10;
v2 = pthread_create(&connector_thread, &v7, (void *(*)(void *))keymon_connect, 0LL);
if ( v2 )
{
syslog(3, "pthread_create for connector thread failed, rc %d\n", v2);
goto LABEL_11;
}
v3 = pthread_create(&receiver_thread, &v7, (void *(*)(void *))keymon_receive, 0LL);
if ( v3 )
{
syslog(3, "pthread_create for receiver thread failed, rc %d\n", v3);
goto LABEL_11;
}
v1 = pthread_sigmask(2, &oldmask, 0LL);
if ( v1 )
{
LABEL_10:
syslog(3, "pthread_sigmask failed, rc %d\n", v1);
goto LABEL_11;
}
v4 = pthread_attr_destroy(&v7);
v5 = v4;
if ( v4 )
{
syslog(3, "pthread_attr_destroy failed, rc %d\n", v4);
goto LABEL_11;
}
pthread_join(connector_thread, 0LL);
pthread_join(receiver_thread, 0LL);
return v5;
}

original_code:
int thread_init()
{
    int rc = 0;
    sigset_t sigset, oldset;
    pthread_attr_t attr;
    rc = pthread_attr_init( &attr );
    if( rc < 0 )
    {
        syslog(3, "pthread_attr_init failed, rc %d\n", rc);
        goto fail;
    }
    pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_JOINABLE );
    pthread_mutex_init( &sk_mutex, ((void *)0) );
    sigemptyset(&sigset);
    sigaddset(&sigset, 10);
    rc = pthread_sigmask(0, &sigset, &oldset);
    if( rc )
    {
        syslog(3, "pthread_sigmask failed, rc %d\n", rc);
        goto fail;
    }
    rc = pthread_create( &connector_thread, &attr, keymon_connect, ((void *)0) );
    if( rc )
    {
        syslog(3, "pthread_create for connector thread failed, rc %d\n", rc);
        goto fail;
    }
    rc = pthread_create( &receiver_thread, &attr, keymon_receive, ((void *)0) );
    if( rc )
    {
        syslog(3, "pthread_create for receiver thread failed, rc %d\n", rc);
        goto fail;
    }
    rc = pthread_sigmask(2, &oldset, ((void *)0));
    if( rc )
    {
        syslog(3, "pthread_sigmask failed, rc %d\n", rc);
        goto fail;
    }
    rc = pthread_attr_destroy( &attr );
    if( rc )
    {
        syslog(3, "pthread_attr_destroy failed, rc %d\n", rc);
        goto fail;
    }
    pthread_join(connector_thread, ((void *)0));
    pthread_join(receiver_thread, ((void *)0));
    return 0;
fail:
    nl_sock_cleanup();
    return -1;
}

canonical_original_code:
int thread_init()
{
    int rc = 0;
    struct __sigset_t sigset; struct __sigset_t oldset;
    union pthread_attr_t attr;
    rc = pthread_attr_init( &attr );
    if( rc < 0 )
    {
        syslog(3, "pthread_attr_init failed, rc %d\n", rc);
        goto fail;
    }
    pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_JOINABLE );
    pthread_mutex_init( &sk_mutex, ((void *)0) );
    sigemptyset(&sigset);
    sigaddset(&sigset, 10);
    rc = pthread_sigmask(0, &sigset, &oldset);
    if( rc )
    {
        syslog(3, "pthread_sigmask failed, rc %d\n", rc);
        goto fail;
    }
    rc = pthread_create( &connector_thread, &attr, keymon_connect, ((void *)0) );
    if( rc )
    {
        syslog(3, "pthread_create for connector thread failed, rc %d\n", rc);
        goto fail;
    }
    rc = pthread_create( &receiver_thread, &attr, keymon_receive, ((void *)0) );
    if( rc )
    {
        syslog(3, "pthread_create for receiver thread failed, rc %d\n", rc);
        goto fail;
    }
    rc = pthread_sigmask(2, &oldset, ((void *)0));
    if( rc )
    {
        syslog(3, "pthread_sigmask failed, rc %d\n", rc);
        goto fail;
    }
    rc = pthread_attr_destroy( &attr );
    if( rc )
    {
        syslog(3, "pthread_attr_destroy failed, rc %d\n", rc);
        goto fail;
    }
    pthread_join(connector_thread, ((void *)0));
    pthread_join(receiver_thread, ((void *)0));
    return 0;
fail:
    nl_sock_cleanup();
    return -1;
}

variable_types: rc int

variable_types: struct __sigset_t { unsigned long __val[-1]; }

variable_types: union pthread_attr_t { char __size[56]; long __align; }


decompiled_code:
int __fastcall signal_handler(unsigned int a1)
{
KM_DBG = ~KM_DBG;
return printf("Caught signal %d. KM_DBG %d\n", a1, (unsigned int)KM_DBG);
}

canonical_decompiled_code:
int __fastcall func0(unsigned int a1)
{
KM_DBG = ~KM_DBG;
return printf("Caught signal %d. KM_DBG %d\n", a1, (unsigned int)KM_DBG);
}

original_code:
void signal_handler(int signo)
{
    KM_DBG = ~KM_DBG;
    printf("Caught signal %d. KM_DBG %d\n", signo, KM_DBG);
}

variable_types: signo int


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
struct sigaction v4; // [rsp+0h] [rbp-A8h] BYREF

openlog("keymon", 0, 24);
v4.sa_handler = (__sighandler_t)signal_handler;
if ( sigaction(10, &v4, 0LL) )
{
syslog(4, "sigaction");
}
else if ( (int)db_init() < 0 )
{
syslog(3, "db_init");
}
else if ( (int)nl_sock_init() < 0 )
{
syslog(3, "nl_sock_init");
}
else
{
daemon(0, 0);
if ( (int)thread_init() >= 0 )
pthread_exit(0LL);
syslog(3, "thread_init");
}
return 1;
}

original_code:
int main(int argc, const char *argv[])
{
    int rc = 0;
    openlog("keymon", 0, (3<<3));
    struct sigaction sa_act;
    sa_act.__sigaction_handler.sa_handler = signal_handler;
    rc = sigaction(10, &sa_act, ((void *)0));
    if( rc )
    {
        syslog(4, "sigaction");
        return 1;
    }
    if( db_init() < 0 )
    {
        syslog(3, "db_init" );
        return 1;
    }
 if( nl_sock_init() < 0 )
 {
  syslog(3, "nl_sock_init" );
  return 1;
 }
    daemon(0,0);
    if( thread_init() < 0 )
    {
        syslog(3, "thread_init" );
        return 1;
    }
    pthread_exit( ((void *)0) );
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    int rc = 0;
    openlog("keymon", 0, (3<<3));
    struct sigaction sa_act;
    sa_act.__sigaction_handler.sa_handler = signal_handler;
    rc = sigaction(10, &sa_act, ((void *)0));
    if( rc )
    {
        syslog(4, "sigaction");
        return 1;
    }
    if( db_init() < 0 )
    {
        syslog(3, "db_init" );
        return 1;
    }
 if( nl_sock_init() < 0 )
 {
  syslog(3, "nl_sock_init" );
  return 1;
 }
    daemon(0,0);
    if( thread_init() < 0 )
    {
        syslog(3, "thread_init" );
        return 1;
    }
    pthread_exit( ((void *)0) );
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

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

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

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


decompiled_code:
void __fastcall keymon_db_store(__int64 a1, unsigned int a2)
{
unsigned int v2; // eax
const char *v3; // rax

v2 = rec(db, a1, a2, 0);
if ( v2 == -30988 )
{
rec(db, a1, a2, 1);
}
else if ( v2 )
{
v3 = (const char *)db_strerror(v2);
syslog(3, "Failed to store key %d: %s\n", (unsigned int)a1, v3);
}
else
{
rec(db, a1, a2, 2);
}
}

canonical_decompiled_code:
void __fastcall func2(__int64 a1, unsigned int a2)
{
unsigned int v2; // eax
const char *v3; // rax

v2 = func1(db, a1, a2, 0);
if ( v2 == -30988 )
{
func1(db, a1, a2, 1);
}
else if ( v2 )
{
v3 = (const char *)db_strerror(v2);
syslog(3, "Failed to store key %d: %s\n", (unsigned int)a1, v3);
}
else
{
func1(db, a1, a2, 2);
}
}

original_code:
void keymon_db_store( struct keymon_event event )
{
    int rc = 0;
    rc = rec(db, event, REC_CHECK);
    switch(rc)
    {
        case (-30988):
            rec(db, event, REC_CREATE);
            break;
        case 0:
            rec(db, event, REC_UPDATE);
            break;
        default:
            syslog(3, "Failed to store key %d: %s\n", event.value, db_strerror(rc));
            break;
    }
    return;
}

canonical_original_code:
void keymon_db_store(struct keymon_event event)
{
    int rc = 0;
    rc = rec(db, event, REC_CHECK);
    switch(rc)
    {
        case (-30988):
            rec(db, event, REC_CREATE);
            break;
        case 0:
            rec(db, event, REC_UPDATE);
            break;
        default:
            syslog(3, "Failed to store key %d: %s\n", event.value, db_strerror(rc));
            break;
    }
    return;
}

variable_types: struct keymon_event { int value; int down; int shift; }


decompiled_code:
void db_cleanup()
{
if ( db )
(*(void (__fastcall **)(__int64, _QWORD))(db + 568))(db, 0LL);
syslog(6, "Successfully closed DB.\n");
}

canonical_decompiled_code:
void func3()
{
if ( db )
(*(void (__fastcall **)(__int64, _QWORD))(db + 568))(db, 0LL);
syslog(6, "Successfully closed DB.\n");
}

original_code:
void db_cleanup()
{
 if( db )
 {
  db->close( db, 0 );
 }
 syslog(6, "Successfully closed DB.\n" );
}


decompiled_code:
__int64 db_init()
{
unsigned int v0; // eax
unsigned int v1; // eax
unsigned int v2; // ebx
const char *v4; // rax
const char *v5; // rax

KM_DBG = 0;
v0 = db_create(&db, 0LL, 0LL);
if ( v0 )
{
v4 = (const char *)db_strerror(v0);
syslog(3, "Failed to get DB handle. %s\n", v4);
v2 = -1;
}
else
{
syslog(6, "Got DB handle 0x%p\n", (const void *)db);
v1 = (*(__int64 (__fastcall **)(__int64, _QWORD, const char *, _QWORD, __int64, __int64, _QWORD))(db + 1008))(
db,
0LL,
"keymond.db",
0LL,
1LL,
1LL,
0LL);
v2 = v1;
if ( v1 )
{
v5 = (const char *)db_strerror(v1);
syslog(3, "Failed to open DB. %s\n", v5);
v2 = -1;
}
else
{
syslog(6, "Successfully opened DB %s!\n", "keymond.db");
}
}
return v2;
}

canonical_decompiled_code:
__int64 func4()
{
unsigned int v0; // eax
unsigned int v1; // eax
unsigned int v2; // ebx
const char *v4; // rax
const char *v5; // rax

KM_DBG = 0;
v0 = db_create(&db, 0LL, 0LL);
if ( v0 )
{
v4 = (const char *)db_strerror(v0);
syslog(3, "Failed to get DB handle. %s\n", v4);
v2 = -1;
}
else
{
syslog(6, "Got DB handle 0x%p\n", (const void *)db);
v1 = (*(__int64 (__fastcall **)(__int64, _QWORD, const char *, _QWORD, __int64, __int64, _QWORD))(db + 1008))(
db,
0LL,
"keymond.db",
0LL,
1LL,
1LL,
0LL);
v2 = v1;
if ( v1 )
{
v5 = (const char *)db_strerror(v1);
syslog(3, "Failed to open DB. %s\n", v5);
v2 = -1;
}
else
{
syslog(6, "Successfully opened DB %s!\n", "keymond.db");
}
}
return v2;
}

original_code:
int db_init()
{
 int ret;
    KM_DBG = 0;
 ret = db_create( &db, ((void *)0), 0 );
 if( ret )
 {
  syslog(3, "Failed to get DB handle. %s\n", db_strerror( ret ) );
  return -1;
 }
 syslog(6, "Got DB handle 0x%p\n", db );
 ret = db->open( db,
                 ((void *)0),
                 "keymond.db",
                 ((void *)0),
                 DB_BTREE,
                 0x00000001,
                 0);
 if( ret )
 {
  syslog(3, "Failed to open DB. %s\n", db_strerror( ret ) );
  return -1;
 }
 syslog(6, "Successfully opened DB %s!\n", "keymond.db" );
 return 0;
}

variable_types: ret int


decompiled_code:
void nl_sock_receive()
{
int v0; // eax

v0 = nl_recvmsgs_default(sk);
if ( v0 < 0 )
syslog(3, "nl_recvmsgs_default failed, rc %d\n", (unsigned int)v0);
}

canonical_decompiled_code:
void func2()
{
int v0; // eax

v0 = nl_recvmsgs_default(sk);
if ( v0 < 0 )
syslog(3, "nl_recvmsgs_default failed, rc %d\n", (unsigned int)v0);
}

original_code:
void nl_sock_receive()
{
    int rc = 0;
    rc = nl_recvmsgs_default( sk );
    if( rc < 0 )
    {
        syslog(3, "nl_recvmsgs_default failed, rc %d\n", rc);
        keymon_mc_group_id = -1;
    }
}


decompiled_code:
void nl_sock_connect()
{
int v0; // eax
int v1; // eax

v0 = genl_ctrl_resolve_grp(sk, "keymon", "keymon_mc_group");
if ( v0 < 0 )
{
syslog(3, "genl_ctrl_resolve_grp failed, rc %d\n", (unsigned int)v0);
}
else
{
v1 = nl_socket_add_memberships(sk, (unsigned int)v0, 0LL);
if ( v1 < 0 )
syslog(3, "nl_socket_add_membership failed, rc %d\n", (unsigned int)v1);
}
}

canonical_decompiled_code:
void func3()
{
int v0; // eax
int v1; // eax

v0 = genl_ctrl_resolve_grp(sk, "keymon", "keymon_mc_group");
if ( v0 < 0 )
{
syslog(3, "genl_ctrl_resolve_grp failed, rc %d\n", (unsigned int)v0);
}
else
{
v1 = nl_socket_add_memberships(sk, (unsigned int)v0, 0LL);
if ( v1 < 0 )
syslog(3, "nl_socket_add_membership failed, rc %d\n", (unsigned int)v1);
}
}

original_code:
void nl_sock_connect()
{
    int rc = 0;
    keymon_mc_group_id = genl_ctrl_resolve_grp( sk, "keymon",
            "keymon_mc_group" );
    if ( keymon_mc_group_id < 0 )
    {
        syslog(3, "genl_ctrl_resolve_grp failed, rc %d\n", keymon_mc_group_id);
        return;
    }
    rc = nl_socket_add_memberships( sk, keymon_mc_group_id, 0 );
    if ( rc < 0 )
    {
        syslog(3, "nl_socket_add_membership failed, rc %d\n", rc);
        keymon_mc_group_id = -1;
        return;
    }
    return;
}


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

if ( sk )
result = nl_socket_free();
return result;
}

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

if ( sk )
result = nl_socket_free();
return result;
}

original_code:
void nl_sock_cleanup()
{
 if( sk )
 {
  nl_socket_free( sk );
 }
    return;
}


repo: dziaduZLasu/dumbshell

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
char *v3; // rbx
int *v4; // rax
char *v5; // rax
__int64 v6; // [rsp+8h] [rbp-90010h] BYREF
char *file[65536]; // [rsp+10h] [rbp-90008h] BYREF
char s[65544]; // [rsp+80010h] [rbp-10008h] BYREF

v3 = getenv("PWD");
strcpy(&v3[strlen(v3)], "/bin");
setenv("PATH", v3, 1);
while ( 1 )
{
do
{
printf("dumbshell~$ ");
fgets(s, 0x10000, stdin);
parse_args(s, file, 0x10000LL, &v6);
}
while ( !v6 );
if ( !strcmp(file[0], "exit") )
exit(0);
if ( !strcmp(file[0], "cd") )
{
chdir(file[1]);
}
else if ( fork() )
{
wait(0LL);
}
else if ( execvp(file[0], file) )
{
v4 = __errno_location();
v5 = strerror(*v4);
puts(v5);
exit(127);
}
}
}

original_code:
int main(int argc, char *argv[], char *envp[])
{
   setenv("PATH",strcat(getenv("PWD"), "/bin"),1);
    char buffer[1<<16];
    char *args[1<<16];
    int *ret_status;
    size_t nargs;
    pid_t pid;
    while(1){
        printf("dumbshell~$ ");
        fgets(buffer, 1<<16, stdin);
        parse_args(buffer, args, 1<<16, &nargs);
        if (nargs==0) continue;
        if (!strcmp(args[0], "exit" )) exit(0);
        if (!strcmp(args[0], "cd" )) {
   chdir(args[1]);
   continue;
   }
        pid = fork();
        if (pid){
            pid = wait(ret_status);
        } else {
            if(execvp(args[0], args)) {
                puts(strerror((*__errno_location ())));
                exit(127);
            }
        }
    }
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[], char *envp[])
{
   setenv("PATH",strcat(getenv("PWD"), "/bin"),1);
    char buffer[-1];
    char *args[-1];
    int *ret_status;
    unsigned long nargs;
    int pid;
    while(1){
        printf("dumbshell~$ ");
        fgets(buffer, 1<<16, stdin);
        parse_args(buffer, args, 1<<16, &nargs);
        if (nargs==0) continue;
        if (!strcmp(args[0], "exit" )) exit(0);
        if (!strcmp(args[0], "cd" )) {
   chdir(args[1]);
   continue;
   }
        pid = fork();
        if (pid){
            pid = wait(ret_status);
        } else {
            if(execvp(args[0], args)) {
                puts(strerror((*__errno_location ())));
                exit(127);
            }
        }
    }
    return 0;
}

variable_types:
envp
char *[]

variable_types:
buffer
char [-1]

variable_types:
args
char *[-1]

variable_types:
nargs
unsigned long

variable_types: pid int


lockfile

repo: dzli/hproxy

original_code:
static int lockfile(int fd){
    struct flock fl;
    fl.l_type = 1;
    fl.l_start = 0;
    fl.l_whence = 0;
    fl.l_len = 0;
    return(fcntl(fd, 6, &fl));
}

variable_types: fd int

variable_types: struct flock { short l_type; short l_whence; long l_start; long l_len; int l_pid; }


decompiled_code:
__int64 __fastcall file_is_locked(int a1)
{
int v1; // edx
__int64 result; // rax
__int16 v3[4]; // [rsp+0h] [rbp-28h] BYREF
__int64 v4; // [rsp+8h] [rbp-20h]
__int64 v5; // [rsp+10h] [rbp-18h]
unsigned int v6; // [rsp+18h] [rbp-10h]

v3[0] = 1;
v4 = 0LL;
v3[1] = 0;
v5 = 0LL;
v1 = fcntl(a1, 5, v3);
result = 0LL;
if ( v1 >= 0 && v3[0] != 2 )
result = v6;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1)
{
int v1; // edx
__int64 result; // rax
__int16 v3[4]; // [rsp+0h] [rbp-28h] BYREF
__int64 v4; // [rsp+8h] [rbp-20h]
__int64 v5; // [rsp+10h] [rbp-18h]
unsigned int v6; // [rsp+18h] [rbp-10h]

v3[0] = 1;
v4 = 0LL;
v3[1] = 0;
v5 = 0LL;
v1 = fcntl(a1, 5, v3);
result = 0LL;
if ( v1 >= 0 && v3[0] != 2 )
result = v6;
return result;
}

original_code:
static pid_t file_is_locked(int fd)
{
    struct flock lock;
    lock.l_type = 1;
    lock.l_start = 0;
    lock.l_whence = 0;
    lock.l_len = 0;
    if (fcntl(fd, 5, &lock) < 0)
       return (0);
    if (lock.l_type == 2)
        return (0);
    return(lock.l_pid);
}

canonical_original_code:
static int file_is_locked(int fd)
{
    struct flock lock;
    lock.l_type = 1;
    lock.l_start = 0;
    lock.l_whence = 0;
    lock.l_len = 0;
    if (fcntl(fd, 5, &lock) < 0)
       return (0);
    if (lock.l_type == 2)
        return (0);
    return(lock.l_pid);
}

user_defined_types: struct flock { short l_type; short l_whence; long l_start; long l_len; int l_pid; }


decompiled_code:
__int64 __fastcall load_conf(_DWORD *a1, const char *a2)
{
__int64 v2; // rax
__int64 v3; // rbx
unsigned int v4; // er12
int v6; // [rsp+Ch] [rbp-9Ch] BYREF
char v7[152]; // [rsp+10h] [rbp-98h] BYREF

if ( a1 )
{
v2 = conf_open(a2);
v3 = v2;
if ( v2 )
{
if ( (unsigned int)conf_get_string(v2, "conf_file", a1, 512LL) )
{
log_error("%s not found!", "conf_file");
v4 = 2;
}
else if ( (unsigned int)conf_get_string(v3, "log_file", a1 + 256, 512LL) )
{
log_error("%s not found!", "log_file");
v4 = 2;
}
else if ( (unsigned int)conf_get_string(v3, "pid_file", a1 + 128, 512LL) )
{
log_error("%s not found!", "pid_file");
v4 = 2;
}
else if ( (unsigned int)conf_get_string(v3, "iptables", a1 + 384, 512LL) )
{
log_error("%s not found!", "iptables");
v4 = 2;
}
else if ( (unsigned int)conf_get_int(v3, "get_original_destination", &v6) )
{
log_error("%s not found!", "get_original_destination");
v4 = 2;
}
else
{
a1[512] = v6;
if ( (unsigned int)conf_get_int(v3, "log_level", &v6) )
{
log_error("%s not found!", "log_level");
v4 = 2;
}
else
{
a1[513] = v6;
if ( (unsigned int)conf_get_int(v3, "enable_http", &v6) )
{
log_error("%s not found!", "enable_http");
v4 = 2;
}
else
{
a1[514] = v6;
if ( (unsigned int)conf_get_int(v3, "http_proxy_listen_port", &v6) )
{
log_error("%s not found!", "http_proxy_listen_port");
v4 = 2;
}
else
{
a1[525] = v6;
if ( (unsigned int)conf_get_int(v3, "http_min_bind_port", &v6) )
{
log_error("%s not found!", "http_min_bind_port");
v4 = 2;
}
else
{
a1[526] = v6;
if ( (unsigned int)conf_get_int(v3, "http_max_bind_port", &v6) )
{
log_error("%s not found!", "http_max_bind_port");
v4 = 2;
}
else
{
a1[527] = v6;
v4 = conf_get_string(v3, "http_port", v7, 128LL);
if ( v4 )
{
log_error("%s not found!", "http_port");
v4 = 2;
}
else
{
split_ports((__int64)(a1 + 515), v7);
conf_close(v3);
}
}
}
}
}
}
}
}
else
{
log_error("can not open configuration file %s\n", a2);
v4 = -1;
}
}
else
{
log_error("conf is NULL!");
v4 = -1;
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func3(_DWORD *a1, const char *a2)
{
__int64 v2; // rax
__int64 v3; // rbx
unsigned int v4; // er12
int v6; // [rsp+Ch] [rbp-9Ch] BYREF
char v7[152]; // [rsp+10h] [rbp-98h] BYREF

if ( a1 )
{
v2 = conf_open(a2);
v3 = v2;
if ( v2 )
{
if ( (unsigned int)conf_get_string(v2, "conf_file", a1, 512LL) )
{
log_error("%s not found!", "conf_file");
v4 = 2;
}
else if ( (unsigned int)conf_get_string(v3, "log_file", a1 + 256, 512LL) )
{
log_error("%s not found!", "log_file");
v4 = 2;
}
else if ( (unsigned int)conf_get_string(v3, "pid_file", a1 + 128, 512LL) )
{
log_error("%s not found!", "pid_file");
v4 = 2;
}
else if ( (unsigned int)conf_get_string(v3, "iptables", a1 + 384, 512LL) )
{
log_error("%s not found!", "iptables");
v4 = 2;
}
else if ( (unsigned int)conf_get_int(v3, "get_original_destination", &v6) )
{
log_error("%s not found!", "get_original_destination");
v4 = 2;
}
else
{
a1[512] = v6;
if ( (unsigned int)conf_get_int(v3, "log_level", &v6) )
{
log_error("%s not found!", "log_level");
v4 = 2;
}
else
{
a1[513] = v6;
if ( (unsigned int)conf_get_int(v3, "enable_http", &v6) )
{
log_error("%s not found!", "enable_http");
v4 = 2;
}
else
{
a1[514] = v6;
if ( (unsigned int)conf_get_int(v3, "http_proxy_listen_port", &v6) )
{
log_error("%s not found!", "http_proxy_listen_port");
v4 = 2;
}
else
{
a1[525] = v6;
if ( (unsigned int)conf_get_int(v3, "http_min_bind_port", &v6) )
{
log_error("%s not found!", "http_min_bind_port");
v4 = 2;
}
else
{
a1[526] = v6;
if ( (unsigned int)conf_get_int(v3, "http_max_bind_port", &v6) )
{
log_error("%s not found!", "http_max_bind_port");
v4 = 2;
}
else
{
a1[527] = v6;
v4 = conf_get_string(v3, "http_port", v7, 128LL);
if ( v4 )
{
log_error("%s not found!", "http_port");
v4 = 2;
}
else
{
func0((__int64)(a1 + 515), v7);
conf_close(v3);
}
}
}
}
}
}
}
}
else
{
log_error("can not open configuration file %s\n", a2);
v4 = -1;
}
}
else
{
log_error("conf is NULL!");
v4 = -1;
}
return v4;
}

original_code:
int load_conf(struct fctl_conf_t* conf, char* filename){
    if (conf == ((void *)0)){
        log_error("conf is NULL!");;
        return -1;
    }
    char ports[128];
    struct ck_conf *pconf = conf_open(filename);
 if (!pconf){
  log_error("can not open configuration file  %s\n", filename);;
  return -1;
 }
 do{ if (! conf_get_string(pconf , "conf_file" ,conf->conf_file , 512)){ }else{ log_error("%s not found!", "conf_file");; return 2; }}while(0);
 do{ if (! conf_get_string(pconf , "log_file" ,conf->log_file , 512)){ }else{ log_error("%s not found!", "log_file");; return 2; }}while(0);
 do{ if (! conf_get_string(pconf , "pid_file" ,conf->pid_file , 512)){ }else{ log_error("%s not found!", "pid_file");; return 2; }}while(0);
    do{ if (! conf_get_string(pconf , "iptables" ,conf->iptables , 512)){ }else{ log_error("%s not found!", "iptables");; return 2; }}while(0);
    do{ int ivalue; if (! conf_get_int(pconf , "get_original_destination" ,&ivalue)){ conf->get_original_destination = ivalue; } else{ log_error("%s not found!", "get_original_destination");; return 2; }}while(0);
    do{ int ivalue; if (! conf_get_int(pconf , "log_level" ,&ivalue)){ conf->log_level = ivalue; } else{ log_error("%s not found!", "log_level");; return 2; }}while(0);
 do{ int ivalue; if (! conf_get_int(pconf , "enable_http" ,&ivalue)){ conf->enable_http = ivalue; } else{ log_error("%s not found!", "enable_http");; return 2; }}while(0);
 do{ int ivalue; if (! conf_get_int(pconf , "http_proxy_listen_port" ,&ivalue)){ conf->http_proxy_listen_port = ivalue; } else{ log_error("%s not found!", "http_proxy_listen_port");; return 2; }}while(0);
 do{ int ivalue; if (! conf_get_int(pconf , "http_min_bind_port" ,&ivalue)){ conf->http_min_bind_port = ivalue; } else{ log_error("%s not found!", "http_min_bind_port");; return 2; }}while(0);
 do{ int ivalue; if (! conf_get_int(pconf , "http_max_bind_port" ,&ivalue)){ conf->http_max_bind_port = ivalue; } else{ log_error("%s not found!", "http_max_bind_port");; return 2; }}while(0);
    do{ if (! conf_get_string(pconf , "http_port" ,ports , sizeof(ports))){ }else{ log_error("%s not found!", "http_port");; return 2; }}while(0);
    split_ports(conf->http_ports, ports);
    conf_close(pconf);
 return 0;
}

canonical_original_code:
int load_conf(struct fctl_conf_t *conf, char *filename){
    if (conf == ((void *)0)){
        log_error("conf is NULL!");;
        return -1;
    }
    char ports[128];
    struct ck_conf *pconf = conf_open(filename);
 if (!pconf){
  log_error("can not open configuration file  %s\n", filename);;
  return -1;
 }
 do{ if (! conf_get_string(pconf , "conf_file" ,conf->conf_file , 512)){ }else{ log_error("%s not found!", "conf_file");; return 2; }}while(0);
 do{ if (! conf_get_string(pconf , "log_file" ,conf->log_file , 512)){ }else{ log_error("%s not found!", "log_file");; return 2; }}while(0);
 do{ if (! conf_get_string(pconf , "pid_file" ,conf->pid_file , 512)){ }else{ log_error("%s not found!", "pid_file");; return 2; }}while(0);
    do{ if (! conf_get_string(pconf , "iptables" ,conf->iptables , 512)){ }else{ log_error("%s not found!", "iptables");; return 2; }}while(0);
    do{ int ivalue; if (! conf_get_int(pconf , "get_original_destination" ,&ivalue)){ conf->get_original_destination = ivalue; } else{ log_error("%s not found!", "get_original_destination");; return 2; }}while(0);
    do{ int ivalue; if (! conf_get_int(pconf , "log_level" ,&ivalue)){ conf->log_level = ivalue; } else{ log_error("%s not found!", "log_level");; return 2; }}while(0);
 do{ int ivalue; if (! conf_get_int(pconf , "enable_http" ,&ivalue)){ conf->enable_http = ivalue; } else{ log_error("%s not found!", "enable_http");; return 2; }}while(0);
 do{ int ivalue; if (! conf_get_int(pconf , "http_proxy_listen_port" ,&ivalue)){ conf->http_proxy_listen_port = ivalue; } else{ log_error("%s not found!", "http_proxy_listen_port");; return 2; }}while(0);
 do{ int ivalue; if (! conf_get_int(pconf , "http_min_bind_port" ,&ivalue)){ conf->http_min_bind_port = ivalue; } else{ log_error("%s not found!", "http_min_bind_port");; return 2; }}while(0);
 do{ int ivalue; if (! conf_get_int(pconf , "http_max_bind_port" ,&ivalue)){ conf->http_max_bind_port = ivalue; } else{ log_error("%s not found!", "http_max_bind_port");; return 2; }}while(0);
    do{ if (! conf_get_string(pconf , "http_port" ,ports , sizeof(ports))){ }else{ log_error("%s not found!", "http_port");; return 2; }}while(0);
    split_ports(conf->http_ports, ports);
    conf_close(pconf);
 return 0;
}

variable_types: struct fctl_conf_t { char conf_file[512]; char pid_file[512]; char log_file[512]; char iptables[512]; int get_original_destination; int log_level; int enable_http; int http_ports[10]; int http_proxy_listen_port; int http_min_bind_port; int http_max_bind_port; } *

variable_types:
filename
char *

variable_types:
ports
char [128]

variable_types: struct ck_conf { char file_name[256]; char *buf; int buflen; } *

variable_types: ivalue int


strncasestr

decompiled_code:
__int64 __fastcall strncasestr(__int64 a1, int a2, const char *a3)
{
unsigned int v4; // er15
__int64 v5; // rbx
int v6; // ebp
__int64 result; // rax

v4 = strlen(a3);
v5 = 0LL;
v6 = a2 - v4;
result = 0LL;
if ( (int)(a2 - v4) >= 0 )
{
while ( strncasecmp((const char *)(a1 + v5), a3, v4) )
{
if ( v6 < (int)++v5 )
return 0LL;
}
if ( v6 < (int)v5 )
result = 0LL;
else
result = a1 + (int)v5;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, int a2, const char *a3)
{
unsigned int v4; // er15
__int64 v5; // rbx
int v6; // ebp
__int64 result; // rax

v4 = strlen(a3);
v5 = 0LL;
v6 = a2 - v4;
result = 0LL;
if ( (int)(a2 - v4) >= 0 )
{
while ( strncasecmp((const char *)(a1 + v5), a3, v4) )
{
if ( v6 < (int)++v5 )
return 0LL;
}
if ( v6 < (int)v5 )
result = 0LL;
else
result = a1 + (int)v5;
}
return result;
}

original_code:
char* strncasestr(char* buf , int buflen , char*str){
    int i = 0;
 int str_len = strlen(str);
 int maxi = buflen - str_len ;
 while (i <= maxi && strncasecmp(buf+i, str, str_len) ) i++;
 if (i > maxi) return ((void *)0);
 return buf+i;
}

canonical_original_code:
char *strncasestr(char *buf, int buflen, char *str){
    int i = 0;
 int str_len = strlen(str);
 int maxi = buflen - str_len;
 while (i <= maxi && strncasecmp(buf+i, str, str_len) ) i++;
 if (i > maxi) return ((void *)0);
 return buf+i;
}

variable_types:
buf
char *

variable_types: buflen int

variable_types:
str
char *

variable_types: i int

variable_types: maxi int


decompiled_code:
size_t __fastcall format_time(time_t a1, char *a2, int a3)
{
struct tm *v4; // rax
time_t timer; // [rsp+8h] [rbp-20h] BYREF

timer = a1;
v4 = localtime(&timer);
return strftime(a2, a3, "%m/%d/%Y %T", v4);
}

canonical_decompiled_code:
size_t __fastcall func5(time_t a1, char *a2, int a3)
{
struct tm *v4; // rax
time_t timer; // [rsp+8h] [rbp-20h] BYREF

timer = a1;
v4 = localtime(&timer);
return strftime(a2, a3, "%m/%d/%Y %T", v4);
}

original_code:
void format_time(time_t time_int , char*time_str , int len ){
    strftime(time_str, len, "%m/%d/%Y %T", localtime(&time_int));
}

canonical_original_code:
void format_time(long time_int, char *time_str, int len){
    strftime(time_str, len, "%m/%d/%Y %T", localtime(&time_int));
}

variable_types: len int


decompiled_code:
size_t __fastcall format_date(time_t a1, char *a2, int a3)
{
struct tm *v4; // rax
time_t timer; // [rsp+8h] [rbp-20h] BYREF

timer = a1;
v4 = localtime(&timer);
return strftime(a2, a3, "%m/%d/%Y", v4);
}

canonical_decompiled_code:
size_t __fastcall func6(time_t a1, char *a2, int a3)
{
struct tm *v4; // rax
time_t timer; // [rsp+8h] [rbp-20h] BYREF

timer = a1;
v4 = localtime(&timer);
return strftime(a2, a3, "%m/%d/%Y", v4);
}

original_code:
void format_date(time_t time_int , char*time_str , int len ){
    strftime(time_str, len, "%m/%d/%Y", localtime(&time_int));
}

canonical_original_code:
void format_date(long time_int, char *time_str, int len){
    strftime(time_str, len, "%m/%d/%Y", localtime(&time_int));
}


decompiled_code:
__int64 __fastcall write_pid(const char *a1)
{
int v2; // eax
int v3; // ebx
int v4; // edi
__int64 result; // rax
int *v6; // rax
unsigned int v7; // eax
unsigned int v8; // eax
__pid_t v9; // eax
size_t v10; // rax
char v11[40]; // [rsp+0h] [rbp-28h] BYREF

v2 = open(a1, 66, 420LL);
if ( v2 < 0 )
{
v6 = _errno_location();
v7 = (unsigned int)strerror(*v6);
log_error("can't open %s: %s", a1, (const char *)v7);
result = 0xFFFFFFFFLL;
}
else
{
v3 = v2;
if ( lockfile(v2) >= 0 )
{
ftruncate(v3, 0LL);
v9 = getpid();
sprintf(v11, "%ld", v9);
v10 = strlen(v11);
write(v3, v11, v10 + 1);
result = 0LL;
}
else
{
v4 = *_errno_location();
if ( ((v4 - 11) & 0xFFFFFFFD) != 0 )
{
v8 = (unsigned int)strerror(v4);
log_error("can't lock %s: %s", a1, (const char *)v8);
result = 4294967294LL;
}
else
{
close(v3);
result = 1LL;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(const char *a1)
{
int v2; // eax
int v3; // ebx
int v4; // edi
__int64 result; // rax
int *v6; // rax
unsigned int v7; // eax
unsigned int v8; // eax
__pid_t v9; // eax
size_t v10; // rax
char v11[40]; // [rsp+0h] [rbp-28h] BYREF

v2 = open(a1, 66, 420LL);
if ( v2 < 0 )
{
v6 = _errno_location();
v7 = (unsigned int)strerror(*v6);
log_error("can't open %s: %s", a1, (const char *)v7);
result = 0xFFFFFFFFLL;
}
else
{
v3 = v2;
if ( func1(v2) >= 0 )
{
ftruncate(v3, 0LL);
v9 = getpid();
sprintf(v11, "%ld", v9);
v10 = strlen(v11);
write(v3, v11, v10 + 1);
result = 0LL;
}
else
{
v4 = *_errno_location();
if ( ((v4 - 11) & 0xFFFFFFFD) != 0 )
{
v8 = (unsigned int)strerror(v4);
log_error("can't lock %s: %s", a1, (const char *)v8);
result = 4294967294LL;
}
else
{
close(v3);
result = 1LL;
}
}
}
return result;
}

original_code:
int write_pid(char* pidfile){
    int fd;
    char buf[16];
    fd = open(pidfile, 02|0100, 0400|0200|(0400 >> 3)|((0400 >> 3) >> 3));
    if (fd < 0) {
        log_error("can't open %s: %s", pidfile, strerror((*__errno_location ())));;
        return -1;
    }
    if (lockfile(fd) < 0) {
        if ((*__errno_location ()) == 13 || (*__errno_location ()) == 11) {
            close(fd);
            return 1;
        }
        log_error("can't lock %s: %s", pidfile, strerror((*__errno_location ())));;
        return -2;
    }
    ftruncate(fd, 0);
    sprintf(buf, "%ld", (long)getpid());
    write(fd, buf, strlen(buf)+1);
    return(0);
}

canonical_original_code:
int write_pid(char *pidfile){
    int fd;
    char buf[16];
    fd = open(pidfile, 02|0100, 0400|0200|(0400 >> 3)|((0400 >> 3) >> 3));
    if (fd < 0) {
        log_error("can't open %s: %s", pidfile, strerror((*__errno_location ())));;
        return -1;
    }
    if (lockfile(fd) < 0) {
        if ((*__errno_location ()) == 13 || (*__errno_location ()) == 11) {
            close(fd);
            return 1;
        }
        log_error("can't lock %s: %s", pidfile, strerror((*__errno_location ())));;
        return -2;
    }
    ftruncate(fd, 0);
    sprintf(buf, "%ld", (long)getpid());
    write(fd, buf, strlen(buf)+1);
    return(0);
}

variable_types:
pidfile
char *

variable_types:
buf
char [16]


original_code:
int conf_get_string(struct ck_conf *pconf , const char *name , char* value , int maxvaluelen){
  int err = 0;
  char *start_pos ;
  char * tmp_ptr ;
  int real_value_len = 0;
  int seen_quote = 0;
  start_pos = pconf->buf;
  while( pconf->buflen - (long) start_pos + (long) pconf->buf >0 ){
    start_pos = __strstr(start_pos , pconf->buflen , name);
                if (start_pos != pconf->buf){
                    char prec = *(start_pos - 1);
                    if (prec != ' ' && prec != '\r' && prec != '\n' && prec != '\t'){
      err = __go_to_next_line(&start_pos, pconf->buflen - (long) start_pos + (long) pconf->buf);
      if (err) return 1;
                    }
                }
    if (!start_pos) return 1;
    tmp_ptr = start_pos;
    while( tmp_ptr != pconf->buf && *tmp_ptr != '\r' && *tmp_ptr != '\n' && *tmp_ptr != '#') tmp_ptr--;
    if (*tmp_ptr == '#'){
      err = __go_to_next_line(&start_pos, pconf->buflen - (long) start_pos + (long) pconf->buf);
      if (err) return 1;
    }
    else {
      break;
    }
  }
  start_pos += strlen(name);
  int line_len = __line_char_count(start_pos);
  tmp_ptr = __strstr(start_pos ,line_len , "=");
  if (tmp_ptr){
     start_pos += 1;
  }
  while ( *start_pos == ' ' || *start_pos == '\t') start_pos++;
  if ( *start_pos == '"') {
   seen_quote = 1;
   start_pos++;
  };
  while( pconf->buflen - (long) start_pos + (long) pconf->buf > 0 && *start_pos != '\r' && *start_pos != '\n' && *start_pos != '#' ){
    if ( real_value_len > maxvaluelen - 1 ) break;
    if ( seen_quote ){
      if ( *start_pos =='"') break;
    }else{
      if ( *start_pos == ' ' ) break;
    }
    value[real_value_len] = *start_pos;
    start_pos++;
    real_value_len++;
  }
  value[real_value_len] = 0;
  if ( real_value_len > maxvaluelen - 1 )
   return 5;
  return 0 ;
}

canonical_original_code:
int conf_get_string(struct ck_conf *pconf, char *name, char *value, int maxvaluelen){
  int err = 0;
  char *start_pos;
  char *tmp_ptr;
  int real_value_len = 0;
  int seen_quote = 0;
  start_pos = pconf->buf;
  while( pconf->buflen - (long) start_pos + (long) pconf->buf >0 ){
    start_pos = __strstr(start_pos , pconf->buflen , name);
                if (start_pos != pconf->buf){
                    char prec = *(start_pos - 1);
                    if (prec != ' ' && prec != '\r' && prec != '\n' && prec != '\t'){
      err = __go_to_next_line(&start_pos, pconf->buflen - (long) start_pos + (long) pconf->buf);
      if (err) return 1;
                    }
                }
    if (!start_pos) return 1;
    tmp_ptr = start_pos;
    while( tmp_ptr != pconf->buf && *tmp_ptr != '\r' && *tmp_ptr != '\n' && *tmp_ptr != '#') tmp_ptr--;
    if (*tmp_ptr == '#'){
      err = __go_to_next_line(&start_pos, pconf->buflen - (long) start_pos + (long) pconf->buf);
      if (err) return 1;
    }
    else {
      break;
    }
  }
  start_pos += strlen(name);
  int line_len = __line_char_count(start_pos);
  tmp_ptr = __strstr(start_pos ,line_len , "=");
  if (tmp_ptr){
     start_pos += 1;
  }
  while ( *start_pos == ' ' || *start_pos == '\t') start_pos++;
  if ( *start_pos == '"') {
   seen_quote = 1;
   start_pos++;
  };
  while( pconf->buflen - (long) start_pos + (long) pconf->buf > 0 && *start_pos != '\r' && *start_pos != '\n' && *start_pos != '#' ){
    if ( real_value_len > maxvaluelen - 1 ) break;
    if ( seen_quote ){
      if ( *start_pos =='"') break;
    }else{
      if ( *start_pos == ' ' ) break;
    }
    value[real_value_len] = *start_pos;
    start_pos++;
    real_value_len++;
  }
  value[real_value_len] = 0;
  if ( real_value_len > maxvaluelen - 1 )
   return 5;
  return 0 ;
}

variable_types:
name
char *

variable_types:
value
char *

variable_types: maxvaluelen int

variable_types: err int

variable_types: prec char

user_defined_types: struct ck_conf { char file_name[256]; char *buf; int buflen; }


decompiled_code:
__int64 __fastcall conf_get_int(__int64 a1, const char *a2, int *a3)
{
unsigned int v4; // ebx
char v6[88]; // [rsp+0h] [rbp-58h] BYREF

v4 = conf_get_string(a1, a2, v6, 50);
if ( !v4 )
*a3 = atoi(v6);
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, const char *a2, int *a3)
{
unsigned int v4; // ebx
char v6[88]; // [rsp+0h] [rbp-58h] BYREF

v4 = func6(a1, a2, v6, 50);
if ( !v4 )
*a3 = atoi(v6);
return v4;
}

original_code:
int conf_get_int(struct ck_conf *pconf , char *name , int *ivalue){
  char value[50];
  int ret = 0;
  ret = conf_get_string(pconf , name , value , 50);
  if (ret) return ret;
  int i = atoi(value);
  *ivalue = i;
  return 0;
}

canonical_original_code:
int conf_get_int(struct ck_conf *pconf, char *name, int *ivalue){
  char value[50];
  int ret = 0;
  ret = conf_get_string(pconf , name , value , 50);
  if (ret) return ret;
  int i = atoi(value);
  *ivalue = i;
  return 0;
}

variable_types:
ivalue
int *

variable_types:
value
char [50]

variable_types: ret int


decompiled_code:
char *__fastcall conf_open(char *src)
{
char *v1; // rax
char *v2; // rbx
void *v3; // rax

v1 = (char *)malloc(0x110uLL);
v2 = v1;
if ( v1 )
{
strncpy(v1, src, 8uLL);
v3 = _start_mmap(src, (_DWORD *)v2 + 66);
*((_QWORD *)v2 + 32) = v3;
if ( !v3 )
v2 = 0LL;
}
return v2;
}

canonical_decompiled_code:
char *__fastcall func9(char *src)
{
char *v1; // rax
char *v2; // rbx
void *v3; // rax

v1 = (char *)malloc(0x110uLL);
v2 = v1;
if ( v1 )
{
strncpy(v1, src, 8uLL);
v3 = func3(src, (_DWORD *)v2 + 66);
*((_QWORD *)v2 + 32) = v3;
if ( !v3 )
v2 = 0LL;
}
return v2;
}

original_code:
struct ck_conf * conf_open( char *filename){
  struct ck_conf *pconf = (struct ck_conf *)malloc(sizeof(struct ck_conf));
  if (!pconf) return ((void *)0);
        strncpy(pconf->file_name, filename , sizeof(pconf->file_name - 1));
  pconf->buf = (char*)__start_mmap(filename , &pconf->buflen);
  if (!pconf->buf) return ((void *)0);
  return pconf;
}

canonical_original_code:
struct ck_conf *conf_open(char *filename){
  struct ck_conf *pconf = (struct ck_conf *)malloc(sizeof(struct ck_conf));
  if (!pconf) return ((void *)0);
        strncpy(pconf->file_name, filename , sizeof(pconf->file_name - 1));
  pconf->buf = (char *)__start_mmap(filename , &pconf->buflen);
  if (!pconf->buf) return ((void *)0);
  return pconf;
}

variable_types:
filename
char *

variable_types: struct ck_conf { char file_name[256]; char *buf; int buflen; } *


decompiled_code:
void __fastcall conf_close(void *ptr)
{
if ( ptr )
{
_end_mmap(*((void **)ptr + 32));
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func10(void *ptr)
{
if ( ptr )
{
func4(*((void **)ptr + 32));
free(ptr);
}
}

original_code:
void conf_close( struct ck_conf *pconf ){
  if (!pconf) return ;
  __end_mmap(pconf->buf , pconf->buflen);
  free(pconf);
}

canonical_original_code:
void conf_close(struct ck_conf *pconf){
  if (!pconf) return ;
  __end_mmap(pconf->buf , pconf->buflen);
  free(pconf);
}


decompiled_code:
__int64 __fastcall conf_set_value_str(char *a1, const char *a2, const char *a3)
{
return conf_set_value(a1, a2, a3, 1);
}

canonical_decompiled_code:
__int64 __fastcall func11(char *a1, const char *a2, const char *a3)
{
return func2(a1, a2, a3, 1);
}

original_code:
int conf_set_value_str(char* file_name , char* name , char* value){
    return conf_set_value(file_name, name, value, 1);
}

canonical_original_code:
int conf_set_value_str(char *file_name, char *name, char *value){
    return conf_set_value(file_name, name, value, 1);
}


decompiled_code:
__int64 __fastcall conf_set_value_int(char *filename, const char *a2, unsigned int a3)
{
char v4[1048]; // [rsp+0h] [rbp-418h] BYREF

snprintf(v4, 0x400uLL, "%d", a3);
return conf_set_value(filename, a2, v4, 0);
}

canonical_decompiled_code:
__int64 __fastcall func12(char *filename, const char *a2, unsigned int a3)
{
char v4[1048]; // [rsp+0h] [rbp-418h] BYREF

snprintf(v4, 0x400uLL, "%d", a3);
return func2(filename, a2, v4, 0);
}

original_code:
int conf_set_value_int(char* file_name , char* name , int value){
    char str_value[1024];
    snprintf(str_value , sizeof(str_value), "%d", value);
    return conf_set_value(file_name , name , str_value, 0);
}

canonical_original_code:
int conf_set_value_int(char *file_name, char *name, int value){
    char str_value[1024];
    snprintf(str_value , sizeof(str_value), "%d", value);
    return conf_set_value(file_name , name , str_value, 0);
}

variable_types: value int


decompiled_code:
void __fastcall log_init(int a1, __int64 a2, char *a3)
{
log_level = a1;
sock_file = a3;
}

canonical_decompiled_code:
void __fastcall func0(int a1, __int64 a2, char *a3)
{
log_level = a1;
sock_file = a3;
}

original_code:
void log_init(int level, int source , char* file){
    log_level = level;
    sock_file = file;
    log_source = source;
}

canonical_original_code:
void log_init(int level, int source, char *file){
    log_level = level;
    sock_file = file;
    log_source = source;
}

variable_types: level int

variable_types: source int

variable_types:
file
char *


decompiled_code:
void __fastcall log_init_ex(int a1, __int64 a2, char *a3, int a4)
{
log_level = a1;
sock_file = a3;
where = a4;
}

canonical_decompiled_code:
void __fastcall func1(int a1, __int64 a2, char *a3, int a4)
{
log_level = a1;
sock_file = a3;
where = a4;
}

original_code:
void log_init_ex(int level, int source , char* file, int to){
    log_level = level;
    sock_file = file;
    log_source = source;
    where = to;
}

canonical_original_code:
void log_init_ex(int level, int source, char *file, int to){
    log_level = level;
    sock_file = file;
    log_source = source;
    where = to;
}

variable_types: to int


decompiled_code:
int __fastcall log_get_level_desc(int a1, char *a2, int a3)
{
int result; // eax

result = (int)a2;
if ( a1 == 3 )
return snprintf(a2, a3, "%s", "INFO");
if ( a1 > 3 )
{
if ( a1 == 4 )
result = snprintf(a2, a3, "%s", "DEBUG");
}
else if ( a1 == 1 )
{
result = snprintf(a2, a3, "%s", "ERROR");
}
else if ( a1 == 2 )
{
result = snprintf(a2, a3, "%s", "WARN");
}
return result;
}

canonical_decompiled_code:
int __fastcall func2(int a1, char *a2, int a3)
{
int result; // eax

result = (int)a2;
if ( a1 == 3 )
return snprintf(a2, a3, "%s", "INFO");
if ( a1 > 3 )
{
if ( a1 == 4 )
result = snprintf(a2, a3, "%s", "DEBUG");
}
else if ( a1 == 1 )
{
result = snprintf(a2, a3, "%s", "ERROR");
}
else if ( a1 == 2 )
{
result = snprintf(a2, a3, "%s", "WARN");
}
return result;
}

original_code:
void log_get_level_desc(int level, char* level_desc , int len){
    switch (level){
        case FCTLOG_LEVEL_ERROR :
            snprintf(level_desc, len , "%s", "ERROR");
            break;
        case FCTLOG_LEVEL_ALERT :
            snprintf(level_desc, len , "%s", "WARN");
            break;
        case FCTLOG_LEVEL_INFO :
            snprintf(level_desc, len , "%s", "INFO");
            break;
        case FCTLOG_LEVEL_DEBUG :
            snprintf(level_desc, len , "%s", "DEBUG");
            break;
    }
}

canonical_original_code:
void log_get_level_desc(int level, char *level_desc, int len){
    switch (level){
        case FCTLOG_LEVEL_ERROR :
            snprintf(level_desc, len , "%s", "ERROR");
            break;
        case FCTLOG_LEVEL_ALERT :
            snprintf(level_desc, len , "%s", "WARN");
            break;
        case FCTLOG_LEVEL_INFO :
            snprintf(level_desc, len , "%s", "INFO");
            break;
        case FCTLOG_LEVEL_DEBUG :
            snprintf(level_desc, len , "%s", "DEBUG");
            break;
    }
}

variable_types: len int


decompiled_code:
int log_error(char *a1, ...)
{
int result; // eax
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

if ( log_level > 0 )
{
va_start(va, a1);
result = _log(a1, 1, va);
}
return result;
}

canonical_decompiled_code:
int func4(char *a1, ...)
{
int result; // eax
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

if ( log_level > 0 )
{
va_start(va, a1);
result = func3(a1, 1, va);
}
return result;
}

original_code:
void log_error(const char *szMsg, ... )
{
        if (log_level < FCTLOG_LEVEL_ERROR)
            return;
  va_list marker;
  __builtin_va_start(marker,szMsg);
  __log(szMsg , FCTLOG_LEVEL_ERROR, marker);
  __builtin_va_end(marker);
}

canonical_original_code:
void log_error(char *szMsg, ...)
{
        if (log_level < FCTLOG_LEVEL_ERROR)
            return;
  __builtin_va_list marker;
  __builtin_va_start(marker,szMsg);
  __log(szMsg , FCTLOG_LEVEL_ERROR, marker);
  __builtin_va_end(marker);
}

variable_types:
szMsg
char *


decompiled_code:
int log_warn(char *a1, ...)
{
int result; // eax
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

if ( log_level > 1 )
{
va_start(va, a1);
result = _log(a1, 2, va);
}
return result;
}

canonical_decompiled_code:
int func5(char *a1, ...)
{
int result; // eax
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

if ( log_level > 1 )
{
va_start(va, a1);
result = func3(a1, 2, va);
}
return result;
}

original_code:
void log_warn(const char *szMsg, ... )
{
        if (log_level < FCTLOG_LEVEL_ALERT)
            return;
  va_list marker;
  __builtin_va_start(marker,szMsg);
  __log(szMsg , FCTLOG_LEVEL_ALERT, marker);
  __builtin_va_end(marker);
}

canonical_original_code:
void log_warn(char *szMsg, ...)
{
        if (log_level < FCTLOG_LEVEL_ALERT)
            return;
  __builtin_va_list marker;
  __builtin_va_start(marker,szMsg);
  __log(szMsg , FCTLOG_LEVEL_ALERT, marker);
  __builtin_va_end(marker);
}


decompiled_code:
int log_debug(char *a1, ...)
{
int result; // eax
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

if ( log_level > 3 )
{
va_start(va, a1);
result = _log(a1, 4, va);
}
return result;
}

canonical_decompiled_code:
int func6(char *a1, ...)
{
int result; // eax
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

if ( log_level > 3 )
{
va_start(va, a1);
result = func3(a1, 4, va);
}
return result;
}

original_code:
void log_debug(const char *szMsg, ... )
{
       if (log_level < FCTLOG_LEVEL_DEBUG)
            return;
  va_list marker;
  __builtin_va_start(marker,szMsg);
  __log(szMsg , FCTLOG_LEVEL_DEBUG, marker);
  __builtin_va_end(marker);
}

canonical_original_code:
void log_debug(char *szMsg, ...)
{
       if (log_level < FCTLOG_LEVEL_DEBUG)
            return;
  __builtin_va_list marker;
  __builtin_va_start(marker,szMsg);
  __log(szMsg , FCTLOG_LEVEL_DEBUG, marker);
  __builtin_va_end(marker);
}


decompiled_code:
int log_info(char *a1, ...)
{
int result; // eax
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

if ( log_level > 2 )
{
va_start(va, a1);
result = _log(a1, 3, va);
}
return result;
}

canonical_decompiled_code:
int func7(char *a1, ...)
{
int result; // eax
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

if ( log_level > 2 )
{
va_start(va, a1);
result = func3(a1, 3, va);
}
return result;
}

original_code:
void log_info(const char *szMsg, ... )
{
       if (log_level < FCTLOG_LEVEL_INFO)
            return;
  va_list marker;
  __builtin_va_start(marker,szMsg);
  __log(szMsg , FCTLOG_LEVEL_INFO, marker);
  __builtin_va_end(marker);
}

canonical_original_code:
void log_info(char *szMsg, ...)
{
       if (log_level < FCTLOG_LEVEL_INFO)
            return;
  __builtin_va_list marker;
  __builtin_va_start(marker,szMsg);
  __log(szMsg , FCTLOG_LEVEL_INFO, marker);
  __builtin_va_end(marker);
}


decompiled_code:
__int64 __fastcall log_get_level_id(const char *a1)
{
unsigned int v1; // edx

v1 = 1;
if ( strcmp(a1, "ERROR") )
{
v1 = 2;
if ( strcmp(a1, "WARN") )
{
v1 = 3;
if ( strcmp(a1, "INFO") )
v1 = strcmp(a1, "DEBUG") == 0 ? 4 : -1;
}
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func8(const char *a1)
{
unsigned int v1; // edx

v1 = 1;
if ( strcmp(a1, "ERROR") )
{
v1 = 2;
if ( strcmp(a1, "WARN") )
{
v1 = 3;
if ( strcmp(a1, "INFO") )
v1 = strcmp(a1, "DEBUG") == 0 ? 4 : -1;
}
}
return v1;
}

original_code:
int log_get_level_id(char* level_desc ){
    if (strcmp(level_desc , "ERROR") == 0){
        return FCTLOG_LEVEL_ERROR;
    }
    if (strcmp(level_desc , "WARN") == 0){
        return FCTLOG_LEVEL_ALERT;
    }
     if (strcmp(level_desc , "INFO") == 0){
        return FCTLOG_LEVEL_INFO;
    }
    if (strcmp(level_desc , "DEBUG") == 0){
        return FCTLOG_LEVEL_DEBUG;
    }
    return -1;
}

canonical_original_code:
int log_get_level_id(char *level_desc){
    if (strcmp(level_desc , "ERROR") == 0){
        return FCTLOG_LEVEL_ERROR;
    }
    if (strcmp(level_desc , "WARN") == 0){
        return FCTLOG_LEVEL_ALERT;
    }
     if (strcmp(level_desc , "INFO") == 0){
        return FCTLOG_LEVEL_INFO;
    }
    if (strcmp(level_desc , "DEBUG") == 0){
        return FCTLOG_LEVEL_DEBUG;
    }
    return -1;
}


decompiled_code:
int __fastcall log_get_source_desc(int a1, char *a2, int a3)
{
if ( !a1 )
return snprintf(a2, a3, "%s", "FortiProxy");
if ( a1 == 5 )
return snprintf(a2, a3, "%s", "AntiVirus");
return snprintf(a2, a3, "%s", "Unknown");
}

canonical_decompiled_code:
int __fastcall func9(int a1, char *a2, int a3)
{
if ( !a1 )
return snprintf(a2, a3, "%s", "FortiProxy");
if ( a1 == 5 )
return snprintf(a2, a3, "%s", "AntiVirus");
return snprintf(a2, a3, "%s", "Unknown");
}

original_code:
void log_get_source_desc(int source, char* source_desc , int len){
    switch (source){
        case FCTLOG_SOURCE_PROXY :
            snprintf(source_desc, len , "%s", "FortiProxy");
            break;
        case FCTLOG_SOURCE_AV :
            snprintf(source_desc, len , "%s", "AntiVirus");
            break;
        default:
            snprintf(source_desc, len , "%s", "Unknown");
            break;
   }
}

canonical_original_code:
void log_get_source_desc(int source, char *source_desc, int len){
    switch (source){
        case FCTLOG_SOURCE_PROXY :
            snprintf(source_desc, len , "%s", "FortiProxy");
            break;
        case FCTLOG_SOURCE_AV :
            snprintf(source_desc, len , "%s", "AntiVirus");
            break;
        default:
            snprintf(source_desc, len , "%s", "Unknown");
            break;
   }
}


decompiled_code:
__int64 __fastcall log_get_source_id(const char *a1)
{
bool v1; // zf
__int64 result; // rax

v1 = strcmp(a1, "FortiProxy") == 0;
result = !v1;
if ( !v1 )
result = strcmp(a1, "AntiVirus") == 0 ? 5 : -1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(const char *a1)
{
bool v1; // zf
__int64 result; // rax

v1 = strcmp(a1, "FortiProxy") == 0;
result = !v1;
if ( !v1 )
result = strcmp(a1, "AntiVirus") == 0 ? 5 : -1;
return result;
}

original_code:
int log_get_source_id(char* source_desc ){
    if (strcmp(source_desc , "FortiProxy") == 0){
        return FCTLOG_SOURCE_PROXY;
    }
    if (strcmp(source_desc , "AntiVirus") == 0){
        return FCTLOG_SOURCE_AV;
    }
    return -1;
}

canonical_original_code:
int log_get_source_id(char *source_desc){
    if (strcmp(source_desc , "FortiProxy") == 0){
        return FCTLOG_SOURCE_PROXY;
    }
    if (strcmp(source_desc , "AntiVirus") == 0){
        return FCTLOG_SOURCE_AV;
    }
    return -1;
}


decompiled_code:
__int64 http_connect()
{
return 0LL;
}

canonical_decompiled_code:
__int64 func0()
{
return 0LL;
}

original_code:
static int http_connect(struct fctl_session_t* session)
{
    return 0;
}

canonical_original_code:
static int http_connect(struct fctl_session_t *session)
{
    return 0;
}

variable_types: struct fctl_session_t { struct list_head { struct list_head *next; } list; struct fctl_proxy_t { int daemon; int port; int pid; struct fctl_proto_t { char proto_name[20]; int port; enum PROXY_TYPE { PROXY_SYN=0, PROXY_ASY=1 } proxy_type; int (*on_local_rev)(struct fctl_session_t *, char *, int); int (*on_remote_rev)(struct fctl_session_t *, char *, int); int (*on_connect)(struct fctl_session_t *); int (*on_disconnect)(struct fctl_session_t *); int (*on_notification)(struct fctl_session_t *, struct event_header_t { int session_id; int event_type; int event_len; } *); } *proto; int valid_port; int max_bind_port; int min_bind_port; char proto_name[20]; unsigned int cur_session_id; struct list_head session_head; int (*run)(struct fctl_proxy_t *); int (*get_port)(struct fctl_proxy_t *); int (*read)(int, char *, int); int (*write)(int, char *, int); int (*listen)(struct fctl_proxy_t *); int (*local_send)(struct fctl_proxy_t *, struct fctl_session_t *); int (*remote_send)(struct fctl_proxy_t *, struct fctl_session_t *); int (*local_close)(struct fctl_proxy_t *, struct fctl_session_t *); int (*remote_close)(struct fctl_proxy_t *, struct fctl_session_t *); int (*remote_connect)(struct fctl_proxy_t *, struct fctl_session_t *, char *, int); } *proxy; int local_fd; int remote_fd; 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]; } local_addr; int remote_src_port; int remote_dst_port; struct sockaddr_in remote_addr; struct rwbuff_t { char buf[-1]; char *readp; char *writep; char *sendp; int buf_len; int buf_default_len; struct cache_pool_t { int cache_count; struct list_head cache_head; } cache_pool; } local_buf; struct rwbuff_t remote_buf; int remote_status; int local_status; int proto_status; int local_retry_times; int remote_retry_times; void *private_data; long create_time; int closing_time; struct fctl_spool_t { int fd; char name[512]; int size; int exists; } spool; int session_id; int (*on_local_recv)(struct fctl_session_t *); int (*on_remote_recv)(struct fctl_session_t *); int (*on_local_send)(struct fctl_session_t *); int (*on_remote_send)(struct fctl_session_t *); int (*on_local_close)(struct fctl_session_t *); int (*on_local_connect)(struct fctl_session_t *); int (*on_remote_close)(struct fctl_session_t *); int (*on_remote_connect)(struct fctl_session_t *); int (*is_closed)(struct fctl_session_t *); int (*close)(struct fctl_session_t *); int (*free)(struct fctl_session_t *); } *

user_defined_types: struct fctl_session_t { struct list_head list; struct fctl_proxy_t *proxy; int local_fd; int remote_fd; struct sockaddr_in local_addr; int remote_src_port; int remote_dst_port; struct sockaddr_in remote_addr; struct rwbuff_t local_buf; struct rwbuff_t remote_buf; int remote_status; int local_status; int proto_status; int local_retry_times; int remote_retry_times; void *private_data; long create_time; int closing_time; struct fctl_spool_t spool; int session_id; int (*on_local_recv)(struct fctl_session_t *); int (*on_remote_recv)(struct fctl_session_t *); int (*on_local_send)(struct fctl_session_t *); int (*on_remote_send)(struct fctl_session_t *); int (*on_local_close)(struct fctl_session_t *); int (*on_local_connect)(struct fctl_session_t *); int (*on_remote_close)(struct fctl_session_t *); int (*on_remote_connect)(struct fctl_session_t *); int (*is_closed)(struct fctl_session_t *); int (*close)(struct fctl_session_t *); int (*free)(struct fctl_session_t *); }

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 rwbuff_t { char buf[-1]; char *readp; char *writep; char *sendp; int buf_len; int buf_default_len; struct cache_pool_t cache_pool; }

user_defined_types: struct fctl_proxy_t { int daemon; int port; int pid; struct fctl_proto_t *proto; int valid_port; int max_bind_port; int min_bind_port; char proto_name[20]; unsigned int cur_session_id; struct list_head session_head; int (*run)(struct fctl_proxy_t *); int (*get_port)(struct fctl_proxy_t *); int (*read)(int, char *, int); int (*write)(int, char *, int); int (*listen)(struct fctl_proxy_t *); int (*local_send)(struct fctl_proxy_t *, struct fctl_session_t *); int (*remote_send)(struct fctl_proxy_t *, struct fctl_session_t *); int (*local_close)(struct fctl_proxy_t *, struct fctl_session_t *); int (*remote_close)(struct fctl_proxy_t *, struct fctl_session_t *); int (*remote_connect)(struct fctl_proxy_t *, struct fctl_session_t *, char *, int); }

user_defined_types: struct fctl_proto_t { char proto_name[20]; int port; enum PROXY_TYPE proxy_type; int (*on_local_rev)(struct fctl_session_t *, char *, int); int (*on_remote_rev)(struct fctl_session_t *, char *, int); int (*on_connect)(struct fctl_session_t *); int (*on_disconnect)(struct fctl_session_t *); int (*on_notification)(struct fctl_session_t *, struct event_header_t *); }


decompiled_code:
__int64 http_disconnect()
{
return 0LL;
}

canonical_decompiled_code:
__int64 func1()
{
return 0LL;
}

original_code:
static int http_disconnect(struct fctl_session_t* session)
{
    return 0;
}

canonical_original_code:
static int http_disconnect(struct fctl_session_t *session)
{
    return 0;
}

user_defined_types: struct list_head { struct list_head *next; }

user_defined_types: struct fctl_spool_t { int fd; char name[512]; int size; int exists; }

user_defined_types: struct cache_pool_t { int cache_count; struct list_head cache_head; }

user_defined_types: enum PROXY_TYPE { PROXY_SYN=0, PROXY_ASY=1 }

user_defined_types: struct event_header_t { int session_id; int event_type; int event_len; }


decompiled_code:
__int64 __fastcall http_remote_rev(__int64 a1)
{
unsigned int v1; // eax

*(_DWORD *)(a1 + 67776) = 0;
v1 = rwbuff_get_readn(a1 + 33920);
rwbuff_read(a1 + 33920, v1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
unsigned int v1; // eax

*(_DWORD *)(a1 + 67776) = 0;
v1 = rwbuff_get_readn(a1 + 33920);
rwbuff_read(a1 + 33920, v1);
return 0LL;
}

original_code:
static int http_remote_rev(struct fctl_session_t* session, char* buf , int buflen)
{
 session->proto_status = HTTP_INITIALIZED;
    int space = rwbuff_get_readn(&session->remote_buf);
    rwbuff_read(&session->remote_buf, space);
    return PROTO_SEND;
}

canonical_original_code:
static int http_remote_rev(struct fctl_session_t *session, char *buf, int buflen)
{
 session->proto_status = HTTP_INITIALIZED;
    int space = rwbuff_get_readn(&session->remote_buf);
    rwbuff_read(&session->remote_buf, space);
    return PROTO_SEND;
}

variable_types:
buf
char *

variable_types: buflen int

variable_types: space int