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

KM_DBG = 0;
v0 = db_create(&db, 0LL, 0LL);
if ( v0 )
{
v1 = -1;
v3 = (const char *)db_strerror(v0);
syslog(3, "Failed to get DB handle. %s\n", v3);
}
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);
if ( v1 )
{
v4 = v1;
v1 = -1;
v5 = (const char *)db_strerror(v4);
syslog(3, "Failed to open DB. %s\n", v5);
}
else
{
syslog(6, "Successfully opened DB %s!\n", "keymond.db");
}
}
return v1;
}

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

KM_DBG = 0;
v0 = db_create(&db, 0LL, 0LL);
if ( v0 )
{
v1 = -1;
v3 = (const char *)db_strerror(v0);
syslog(3, "Failed to get DB handle. %s\n", v3);
}
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);
if ( v1 )
{
v4 = v1;
v1 = -1;
v5 = (const char *)db_strerror(v4);
syslog(3, "Failed to open DB. %s\n", v5);
}
else
{
syslog(6, "Successfully opened DB %s!\n", "keymond.db");
}
}
return v1;
}

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


repo: dzeban/keymon

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

variable_types: rc int

return_type: void


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


main

canonical_name: main

repo: dziaduZLasu/dumbshell

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
char *v3; // rsi
int *v4; // rax
char *v5; // rax
__int64 v6; // [rsp+8h] [rbp-90020h] BYREF
char s[65536]; // [rsp+10h] [rbp-90018h] BYREF
char *argva[65539]; // [rsp+10010h] [rbp-80018h] 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, argva, 0x10000LL, &v6);
}
while ( !v6 );
if ( !strcmp(argva[0], "exit") )
break;
if ( *argva[0] == 99 && argva[0][1] == 100 && !argva[0][2] )
{
chdir(argva[1]);
}
else if ( fork() )
{
wait(0LL);
}
else if ( execvp(argva[0], argva) )
{
v4 = __errno_location();
v5 = strerror(*v4);
puts(v5);
exit(127);
}
}
exit(0);
}

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: argc int

variable_types:
argv
char *[]

variable_types:
envp
char *[]

variable_types:
buffer
char [-1]

variable_types:
args
char *[-1]

variable_types:
nargs
unsigned long

variable_types: pid int

return_type: int


lockfile

repo: dzli/hproxy

decompiled_code:
int __fastcall lockfile(int a1)
{
int v2; // [rsp+0h] [rbp-28h] BYREF
__int64 v3; // [rsp+8h] [rbp-20h]
__int64 v4; // [rsp+10h] [rbp-18h]

v2 = 1;
v3 = 0LL;
v4 = 0LL;
return fcntl(a1, 6, &v2);
}

canonical_decompiled_code:
int __fastcall func1(int a1)
{
int v2; // [rsp+0h] [rbp-28h] BYREF
__int64 v3; // [rsp+8h] [rbp-20h]
__int64 v4; // [rsp+10h] [rbp-18h]

v2 = 1;
v3 = 0LL;
v4 = 0LL;
return fcntl(a1, 6, &v2);
}

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; // er8
__int64 result; // rax
int v3; // [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 = 1;
v4 = 0LL;
v5 = 0LL;
v1 = fcntl(a1, 5, &v3);
result = 0LL;
if ( v1 >= 0 && (_WORD)v3 != 2 )
result = v6;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1)
{
int v1; // er8
__int64 result; // rax
int v3; // [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 = 1;
v4 = 0LL;
v5 = 0LL;
v1 = fcntl(a1, 5, &v3);
result = 0LL;
if ( v1 >= 0 && (_WORD)v3 != 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; // rbp
__int64 result; // rax
int v5[38]; // [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");
result = 2LL;
}
else if ( (unsigned int)conf_get_string(v3, "log_file", a1 + 256, 512LL) )
{
log_error("%s not found!", "log_file");
result = 2LL;
}
else if ( (unsigned int)conf_get_string(v3, "pid_file", a1 + 128, 512LL) )
{
log_error("%s not found!", "pid_file");
result = 2LL;
}
else if ( (unsigned int)conf_get_string(v3, "iptables", a1 + 384, 512LL) )
{
log_error("%s not found!", "iptables");
result = 2LL;
}
else if ( (unsigned int)conf_get_int(v3, "get_original_destination", v5) )
{
log_error("%s not found!", "get_original_destination");
result = 2LL;
}
else
{
a1[512] = v5[0];
if ( (unsigned int)conf_get_int(v3, "log_level", v5) )
{
log_error("%s not found!", "log_level");
result = 2LL;
}
else
{
a1[513] = v5[0];
if ( (unsigned int)conf_get_int(v3, "enable_http", v5) )
{
log_error("%s not found!", "enable_http");
result = 2LL;
}
else
{
a1[514] = v5[0];
if ( (unsigned int)conf_get_int(v3, "http_proxy_listen_port", v5) )
{
log_error("%s not found!", "http_proxy_listen_port");
result = 2LL;
}
else
{
a1[525] = v5[0];
if ( (unsigned int)conf_get_int(v3, "http_min_bind_port", v5) )
{
log_error("%s not found!", "http_min_bind_port");
result = 2LL;
}
else
{
a1[526] = v5[0];
if ( (unsigned int)conf_get_int(v3, "http_max_bind_port", v5) )
{
log_error("%s not found!", "http_max_bind_port");
result = 2LL;
}
else
{
a1[527] = v5[0];
if ( (unsigned int)conf_get_string(v3, "http_port", v5, 128LL) )
{
log_error("%s not found!", "http_port");
result = 2LL;
}
else
{
split_ports(a1 + 515, (char *)v5);
conf_close(v3);
result = 0LL;
}
}
}
}
}
}
}
}
else
{
log_error("can not open configuration file %s\n", a2);
result = 0xFFFFFFFFLL;
}
}
else
{
log_error("conf is NULL!");
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(_DWORD *a1, const char *a2)
{
__int64 v2; // rax
__int64 v3; // rbp
__int64 result; // rax
int v5[38]; // [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");
result = 2LL;
}
else if ( (unsigned int)conf_get_string(v3, "log_file", a1 + 256, 512LL) )
{
log_error("%s not found!", "log_file");
result = 2LL;
}
else if ( (unsigned int)conf_get_string(v3, "pid_file", a1 + 128, 512LL) )
{
log_error("%s not found!", "pid_file");
result = 2LL;
}
else if ( (unsigned int)conf_get_string(v3, "iptables", a1 + 384, 512LL) )
{
log_error("%s not found!", "iptables");
result = 2LL;
}
else if ( (unsigned int)conf_get_int(v3, "get_original_destination", v5) )
{
log_error("%s not found!", "get_original_destination");
result = 2LL;
}
else
{
a1[512] = v5[0];
if ( (unsigned int)conf_get_int(v3, "log_level", v5) )
{
log_error("%s not found!", "log_level");
result = 2LL;
}
else
{
a1[513] = v5[0];
if ( (unsigned int)conf_get_int(v3, "enable_http", v5) )
{
log_error("%s not found!", "enable_http");
result = 2LL;
}
else
{
a1[514] = v5[0];
if ( (unsigned int)conf_get_int(v3, "http_proxy_listen_port", v5) )
{
log_error("%s not found!", "http_proxy_listen_port");
result = 2LL;
}
else
{
a1[525] = v5[0];
if ( (unsigned int)conf_get_int(v3, "http_min_bind_port", v5) )
{
log_error("%s not found!", "http_min_bind_port");
result = 2LL;
}
else
{
a1[526] = v5[0];
if ( (unsigned int)conf_get_int(v3, "http_max_bind_port", v5) )
{
log_error("%s not found!", "http_max_bind_port");
result = 2LL;
}
else
{
a1[527] = v5[0];
if ( (unsigned int)conf_get_string(v3, "http_port", v5, 128LL) )
{
log_error("%s not found!", "http_port");
result = 2LL;
}
else
{
func0(a1 + 515, (char *)v5);
conf_close(v3);
result = 0LL;
}
}
}
}
}
}
}
}
else
{
log_error("can not open configuration file %s\n", a2);
result = 0xFFFFFFFFLL;
}
}
else
{
log_error("conf is NULL!");
result = 0xFFFFFFFFLL;
}
return result;
}

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; // eax
int v5; // ebp
unsigned int v6; // er15
__int64 v7; // rbx
__int64 v8; // r12

v4 = strlen(a3);
v5 = a2 - v4;
if ( (int)(a2 - v4) < 0 )
return 0LL;
v6 = v4;
v7 = 0LL;
while ( 1 )
{
v8 = a1 + v7;
if ( !strncasecmp((const char *)(a1 + v7), a3, v6) )
break;
if ( v5 < (int)++v7 )
return 0LL;
}
return v8;
}

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

v4 = strlen(a3);
v5 = a2 - v4;
if ( (int)(a2 - v4) < 0 )
return 0LL;
v6 = v4;
v7 = 0LL;
while ( 1 )
{
v8 = a1 + v7;
if ( !strncasecmp((const char *)(a1 + v7), a3, v6) )
break;
if ( v5 < (int)++v7 )
return 0LL;
}
return v8;
}

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

return_type: char *


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; // ebp
int v4; // edi
__int64 result; // rax
__pid_t v6; // eax
size_t v7; // rax
unsigned int v8; // eax
int *v9; // rax
unsigned int v10; // eax
char v11[40]; // [rsp+0h] [rbp-28h] BYREF

v2 = open(a1, 66, 420LL);
if ( v2 < 0 )
{
v9 = _errno_location();
v10 = (unsigned int)strerror(*v9);
log_error("can't open %s: %s", a1, (const char *)v10);
result = 0xFFFFFFFFLL;
}
else
{
v3 = v2;
if ( lockfile(v2) >= 0 )
{
ftruncate(v3, 0LL);
v6 = getpid();
sprintf(v11, "%ld", v6);
v7 = strlen(v11);
write(v3, v11, v7 + 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; // ebp
int v4; // edi
__int64 result; // rax
__pid_t v6; // eax
size_t v7; // rax
unsigned int v8; // eax
int *v9; // rax
unsigned int v10; // eax
char v11[40]; // [rsp+0h] [rbp-28h] BYREF

v2 = open(a1, 66, 420LL);
if ( v2 < 0 )
{
v9 = _errno_location();
v10 = (unsigned int)strerror(*v9);
log_error("can't open %s: %s", a1, (const char *)v10);
result = 0xFFFFFFFFLL;
}
else
{
v3 = v2;
if ( func1(v2) >= 0 )
{
ftruncate(v3, 0LL);
v6 = getpid();
sprintf(v11, "%ld", v6);
v7 = strlen(v11);
write(v3, v11, v7 + 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; // er12
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; // er12
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; // r12
void *v3; // rax

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

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

v1 = (char *)malloc(0x110uLL);
if ( !v1
|| (v2 = v1, strncpy(v1, src, 8uLL), v3 = func3(src, (_DWORD *)v2 + 66), (*((_QWORD *)v2 + 32) = v3) == 0LL) )
{
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 func9(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, char *a2, const char *a3)
{
return conf_set_value(a1, a2, a3, 1);
}

canonical_decompiled_code:
__int64 __fastcall func10(char *a1, 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, char *s2, unsigned int a3)
{
char v4[1048]; // [rsp+0h] [rbp-418h] BYREF

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

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

snprintf(v4, 0x400uLL, "%d", a3);
return func2(filename, s2, 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

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

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

result = log_level;
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

result = log_level;
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; // er8

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; // er8

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:
// attributes: thunk
__int64 http_disconnect()
{
return http_connect();
}

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

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


original_code:
static int http_local_rev(struct fctl_session_t* session, char* buf , int buflen){
    int space = 0;
    if (session->proto_status != HTTP_INITIALIZED)
        goto ret;
    char * header_tail = strstr(buf , "\r\n\r\n");
    if ( header_tail == ((void *)0))
        return PROTO_RESERVED;
    session->proto_status = HTTP_HEADER_RECV;
   int header_len = header_tail - buf + 4;
 int left_len = buflen - header_len;
 char* left_content = ((void *)0);
 if (left_len > 0){
  char* left_content = (char*)malloc(left_len);
  memcpy(left_content, buf + header_len, left_len);
 }
 struct http_parser_t hp = {0};
 http_update_req(&hp, buf, header_len);
 char* new_url = strstr(hp.hlines[0].new_line, " ");
 if (new_url)
  new_url++;
 log_info("url: %s\n", new_url);;
 int need_proxy = url_need_proxy(new_url);
 if (session->remote_fd <= 0){
   int r = 0;
   if (need_proxy){
      char proxy_ip[32] = {0};
      new_sogou_proxy_addr(proxy_ip, sizeof(proxy_ip));
      printf(">>>proxy ip: %s\n", proxy_ip);
      r = session->proxy->remote_connect(session->proxy, session, proxy_ip[0] == 0?((void *)0):proxy_ip, 80);
       }else
          r = session->proxy->remote_connect(session->proxy, session, ((void *)0), 0);
     if (r < 0){
      if (left_content)
       free(left_content);
      http_parser_reset(&hp);
            return r;
        }
    }
    if (need_proxy){
  rwbuff_rewind_writep(&session->local_buf);
   int i = 0;
  for (; i < hp.cur_hline; i++) {
   char* line = ((void *)0);
   int line_len = 0;
   if (hp.hlines[i].new_line != ((void *)0)){
    line = hp.hlines[i].new_line;
    line_len = hp.hlines[i].new_line_len;
    log_debug("n %s", hp.hlines[i].new_line);;
   }else{
    line = hp.hlines[i].ori_line;
    line_len = hp.hlines[i].ori_line_len;
    log_debug("o %s", hp.hlines[i].ori_line);;
   }
   if (line){
    rwbuff_append_data(&session->local_buf, line, line_len);
   }
  }
  rwbuff_append_data(&session->local_buf, "\r\n", 2);
  if (left_content){
   rwbuff_append_data(&session->local_buf, left_content, left_len);
  }
 }
 http_parser_reset(&hp);
 if (left_content)
  free(left_content);
 ret:
    space = rwbuff_get_readn(&session->local_buf);
    rwbuff_read(&session->local_buf, space);
    return PROTO_SEND;
}

canonical_original_code:
static int http_local_rev(struct fctl_session_t *session, char *buf, int buflen){
    int space = 0;
    if (session->proto_status != HTTP_INITIALIZED)
        goto ret;
    char *header_tail = strstr(buf , "\r\n\r\n");
    if ( header_tail == ((void *)0))
        return PROTO_RESERVED;
    session->proto_status = HTTP_HEADER_RECV;
   int header_len = header_tail - buf + 4;
 int left_len = buflen - header_len;
 char *left_content = ((void *)0);
 if (left_len > 0){
  char *left_content = (char*)malloc(left_len);
  memcpy(left_content, buf + header_len, left_len);
 }
 struct http_parser_t hp = {0};
 http_update_req(&hp, buf, header_len);
 char *new_url = strstr(hp.hlines[0].new_line, " ");
 if (new_url)
  new_url++;
 log_info("url: %s\n", new_url);;
 int need_proxy = url_need_proxy(new_url);
 if (session->remote_fd <= 0){
   int r = 0;
   if (need_proxy){
      char proxy_ip[32] = {0};
      new_sogou_proxy_addr(proxy_ip, sizeof(proxy_ip));
      printf(">>>proxy ip: %s\n", proxy_ip);
      r = session->proxy->remote_connect(session->proxy, session, proxy_ip[0] == 0?((void *)0):proxy_ip, 80);
       }else
          r = session->proxy->remote_connect(session->proxy, session, ((void *)0), 0);
     if (r < 0){
      if (left_content)
       free(left_content);
      http_parser_reset(&hp);
            return r;
        }
    }
    if (need_proxy){
  rwbuff_rewind_writep(&session->local_buf);
   int i = 0;
  for (; i < hp.cur_hline; i++) {
   char *line = ((void *)0);
   int line_len = 0;
   if (hp.hlines[i].new_line != ((void *)0)){
    line = hp.hlines[i].new_line;
    line_len = hp.hlines[i].new_line_len;
    log_debug("n %s", hp.hlines[i].new_line);;
   }else{
    line = hp.hlines[i].ori_line;
    line_len = hp.hlines[i].ori_line_len;
    log_debug("o %s", hp.hlines[i].ori_line);;
   }
   if (line){
    rwbuff_append_data(&session->local_buf, line, line_len);
   }
  }
  rwbuff_append_data(&session->local_buf, "\r\n", 2);
  if (left_content){
   rwbuff_append_data(&session->local_buf, left_content, left_len);
  }
 }
 http_parser_reset(&hp);
 if (left_content)
  free(left_content);
 ret:
    space = rwbuff_get_readn(&session->local_buf);
    rwbuff_read(&session->local_buf, space);
    return PROTO_SEND;
}

variable_types: struct http_parser_t { int reqid; int uid; unsigned int pid; int net_protocol; union { 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]; } addr4; struct sockaddr_in6 { unsigned short sin6_family; unsigned short sin6_port; unsigned int sin6_flowinfo; struct in6_addr { union { unsigned char __u6_addr8[16]; unsigned short __u6_addr16[8]; unsigned int __u6_addr32[4]; } __in6_u; } sin6_addr; unsigned int sin6_scope_id; } addr6; } local; union { struct sockaddr_in addr4; struct sockaddr_in6 addr6; } remote; char host[64]; int urllen; char url[1024]; char cache_line[2048]; int cache_line_index; short status; char http_version_str[16]; char method[16]; int safe_search_enabled; struct header_line_t { int type; char *ori_line; int ori_line_len; char *new_line; int new_line_len; } hlines[32]; int cur_hline; int hostlen; }

variable_types: r int

variable_types: i int

variable_types:
line
char *

user_defined_types: struct http_parser_t { int reqid; int uid; unsigned int pid; int net_protocol; union { struct sockaddr_in addr4; struct sockaddr_in6 addr6; } local; union { struct sockaddr_in addr4; struct sockaddr_in6 addr6; } remote; char host[64]; int urllen; char url[1024]; char cache_line[2048]; int cache_line_index; short status; char http_version_str[16]; char method[16]; int safe_search_enabled; struct header_line_t hlines[32]; int cur_hline; int hostlen; }

user_defined_types: struct sockaddr_in6 { unsigned short sin6_family; unsigned short sin6_port; unsigned int sin6_flowinfo; struct in6_addr sin6_addr; unsigned int sin6_scope_id; }


decompiled_code:
char *http_new()
{
char *result; // rax

result = (char *)calloc(0x48uLL, 1uLL);
if ( result )
{
strcpy(result, "HTTP");
*((_DWORD *)result + 5) = 80;
*((_QWORD *)result + 6) = http_connect;
*((_QWORD *)result + 7) = http_disconnect;
*((_QWORD *)result + 4) = http_local_rev;
*((_QWORD *)result + 5) = http_remote_rev;
}
return result;
}

canonical_decompiled_code:
char *func4()
{
char *result; // rax

result = (char *)calloc(0x48uLL, 1uLL);
if ( result )
{
strcpy(result, "HTTP");
*((_DWORD *)result + 5) = 80;
*((_QWORD *)result + 6) = http_connect;
*((_QWORD *)result + 7) = http_disconnect;
*((_QWORD *)result + 4) = http_local_rev;
*((_QWORD *)result + 5) = http_remote_rev;
}
return result;
}

original_code:
struct fctl_proto_t * http_new(void){
    struct fctl_proto_t* ret = ((void *)0);
    ret = (struct fctl_proto_t*)malloc(sizeof(struct fctl_proto_t));
    if (ret == ((void *)0))
        return ((void *)0);
    memset(ret , 0 , sizeof(struct fctl_proto_t));
    strcpy(ret->proto_name, "HTTP");
    ret->port = 80;
    ret->on_connect= http_connect;
    ret->on_disconnect= http_disconnect;
    ret->on_local_rev= http_local_rev;
    ret->on_remote_rev= http_remote_rev;
    return ret;
}

canonical_original_code:
struct fctl_proto_t *http_new(){
    struct fctl_proto_t *ret = ((void *)0);
    ret = (struct fctl_proto_t *)malloc(sizeof(struct fctl_proto_t));
    if (ret == ((void *)0))
        return ((void *)0);
    memset(ret , 0 , sizeof(struct fctl_proto_t));
    strcpy(ret->proto_name, "HTTP");
    ret->port = 80;
    ret->on_connect= http_connect;
    ret->on_disconnect= http_disconnect;
    ret->on_local_rev= http_local_rev;
    ret->on_remote_rev= http_remote_rev;
    return ret;
}

variable_types: 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 { struct list_head { struct list_head *next; } list; 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); } *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 *); } *, 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; } *); } *


decompiled_code:
void sig_term()
{
stop = 1;
}

canonical_decompiled_code:
void func2()
{
stop = 1;
}

original_code:
static void sig_term(int signo)
{
    stop = 1;
}

variable_types: signo int


start

decompiled_code:
unsigned int start()
{
unsigned int v0; // eax
int v1; // edi
int v2; // edi
__int64 v4; // rax
__int64 v5; // rax
__int64 v6; // rdi
__int64 v7; // rbx

signal(13, handler);
signal(1, sig_reload_config);
atexit((void (*)(void))on_exit);
init_iptables_nat_rules();
memset(&proxies, 0, (size_t)&unk_18260);
if ( *(_DWORD *)&g_conf[2056] )
{
v4 = http_new();
v5 = np_new(v4, *(unsigned int *)&g_conf[2100], *(unsigned int *)&g_conf[2108], *(unsigned int *)&g_conf[2104]);
pc_add_proxy(&proxies, v5);
v6 = *(unsigned int *)&g_conf[2060];
if ( (_DWORD)v6 )
{
v7 = (unsigned int)((_DWORD)g_conf + 2064);
do
{
v7 += 4LL;
create_iptables_nat_rule(v6, *(unsigned int *)&g_conf[2100]);
v6 = *(unsigned int *)(v7 - 4);
}
while ( (_DWORD)v6 );
}
}
pc_run_all(&proxies);
signal(15, (__sighandler_t)sig_term);
signal(2, (__sighandler_t)sig_term);
signal(17, sig_chld);
while ( !stop )
pause();
v0 = getpid();
printf("pid= %d\n", v0);
pc_kill_all(&proxies, 15LL);
v1 = 2;
do
v1 = sleep(v1);
while ( v1 );
pc_kill_all(&proxies, 9LL);
v2 = 1;
do
v2 = sleep(v2);
while ( v2 );
log_info(" FortiClient stopped.\n");
unlink((const char *)(unsigned int)((_DWORD)g_conf + 512));
destroy_iptables_nat_rules();
return sleep(1u);
}

canonical_decompiled_code:
unsigned int func3()
{
unsigned int v0; // eax
int v1; // edi
int v2; // edi
__int64 v4; // rax
__int64 v5; // rax
__int64 v6; // rdi
__int64 v7; // rbx

signal(13, handler);
signal(1, sig_reload_config);
atexit((void (*)(void))on_exit);
init_iptables_nat_rules();
memset(&proxies, 0, (size_t)&unk_18260);
if ( *(_DWORD *)&g_conf[2056] )
{
v4 = http_new();
v5 = np_new(v4, *(unsigned int *)&g_conf[2100], *(unsigned int *)&g_conf[2108], *(unsigned int *)&g_conf[2104]);
pc_add_proxy(&proxies, v5);
v6 = *(unsigned int *)&g_conf[2060];
if ( (_DWORD)v6 )
{
v7 = (unsigned int)((_DWORD)g_conf + 2064);
do
{
v7 += 4LL;
create_iptables_nat_rule(v6, *(unsigned int *)&g_conf[2100]);
v6 = *(unsigned int *)(v7 - 4);
}
while ( (_DWORD)v6 );
}
}
pc_run_all(&proxies);
signal(15, (__sighandler_t)sig_term);
signal(2, (__sighandler_t)sig_term);
signal(17, sig_chld);
while ( !stop )
pause();
v0 = getpid();
printf("pid= %d\n", v0);
pc_kill_all(&proxies, 15LL);
v1 = 2;
do
v1 = sleep(v1);
while ( v1 );
pc_kill_all(&proxies, 9LL);
v2 = 1;
do
v2 = sleep(v2);
while ( v2 );
log_info(" FortiClient stopped.\n");
unlink((const char *)(unsigned int)((_DWORD)g_conf + 512));
destroy_iptables_nat_rules();
return sleep(1u);
}

original_code:
static void start(){
    signal(13 , ((__sighandler_t) 1) );
    signal(1 , sig_reload_config);
 atexit(_on_exit);
    init_iptables_nat_rules();
    memset(&proxies ,0 , sizeof(proxies));
    if (g_conf.enable_http){ struct fctl_proto_t* http_proto = http_new(); struct fctl_proxy_t* http_proxy = np_new( http_proto, g_conf.http_proxy_listen_port, g_conf.http_max_bind_port , g_conf.http_min_bind_port); pc_add_proxy(&proxies , http_proxy); int i = 0; while(g_conf.http_ports[i] != 0){ create_iptables_nat_rule(g_conf.http_ports[i] , g_conf.http_proxy_listen_port); i++; } };
   pc_run_all(&proxies);
    signal(15 , sig_term);
    signal(2, sig_term);
    signal(17, sig_chld);
    while(!stop)
     pause();
    printf("pid= %d\n", getpid());
    pc_kill_all(&proxies, 15);
    unsigned int unsleep = 2;
    while(unsleep > 0){
        unsleep = sleep(unsleep);
    }
    pc_kill_all(&proxies, 9);
    unsleep = 1;
    while(unsleep > 0){
        unsleep = sleep(unsleep);
    }
    log_info(" FortiClient stopped.\n");;
    unlink(g_conf.pid_file);
    destroy_iptables_nat_rules();
   sleep(1);
}

canonical_original_code:
static void start(){
    signal(13 , ((void (*)(int)) 1) );
    signal(1 , sig_reload_config);
 atexit(_on_exit);
    init_iptables_nat_rules();
    memset(&proxies ,0 , sizeof(proxies));
    if (g_conf.enable_http){ struct fctl_proto_t *http_proto = http_new(); struct fctl_proxy_t *http_proxy = np_new( http_proto, g_conf.http_proxy_listen_port, g_conf.http_max_bind_port , g_conf.http_min_bind_port); pc_add_proxy(&proxies , http_proxy); int i = 0; while(g_conf.http_ports[i] != 0){ create_iptables_nat_rule(g_conf.http_ports[i] , g_conf.http_proxy_listen_port); i++; } };
   pc_run_all(&proxies);
    signal(15 , sig_term);
    signal(2, sig_term);
    signal(17, sig_chld);
    while(!stop)
     pause();
    printf("pid= %d\n", getpid());
    pc_kill_all(&proxies, 15);
    unsigned int unsleep = 2;
    while(unsleep > 0){
        unsleep = sleep(unsleep);
    }
    pc_kill_all(&proxies, 9);
    unsleep = 1;
    while(unsleep > 0){
        unsleep = sleep(unsleep);
    }
    log_info(" FortiClient stopped.\n");;
    unlink(g_conf.pid_file);
    destroy_iptables_nat_rules();
   sleep(1);
}

variable_types: 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 { struct list_head { struct list_head *next; } list; struct fctl_proxy_t *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 *); } *, 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); } *

variable_types:
unsleep
unsigned int


decompiled_code:
void sig_chld()
{
__pid_t v0; // eax
unsigned int v1; // ebp
int stat_loc[3]; // [rsp+Ch] [rbp-Ch] BYREF

while ( 1 )
{
v0 = waitpid(-1, stat_loc, 1);
v1 = v0;
if ( v0 <= 0 )
break;
printf("daemon %d terminated.\n", (unsigned int)v0);
log_info("proxy %d terminated", v1);
if ( stop )
pc_remove_pid(&proxies, v1);
else
pc_restart_others(&proxies, v1);
}
}

canonical_decompiled_code:
void func5()
{
__pid_t v0; // eax
unsigned int v1; // ebp
int stat_loc[3]; // [rsp+Ch] [rbp-Ch] BYREF

while ( 1 )
{
v0 = waitpid(-1, stat_loc, 1);
v1 = v0;
if ( v0 <= 0 )
break;
printf("daemon %d terminated.\n", (unsigned int)v0);
log_info("proxy %d terminated", v1);
if ( stop )
pc_remove_pid(&proxies, v1);
else
pc_restart_others(&proxies, v1);
}
}

original_code:
void sig_chld(int signo){
    pid_t pid;
    int stat;
    while ( (pid = waitpid(-1, &stat, 1)) > 0){
        printf("daemon %d terminated.\n", pid);
        log_info("proxy %d terminated", pid);;
        if (!stop)
            pc_restart_others(&proxies, pid);
        else
            pc_remove_pid(&proxies, pid);
    }
    return;
}

canonical_original_code:
void sig_chld(int signo){
    int pid;
    int stat;
    while ( (pid = waitpid(-1, &stat, 1)) > 0){
        printf("daemon %d terminated.\n", pid);
        log_info("proxy %d terminated", pid);;
        if (!stop)
            pc_restart_others(&proxies, pid);
        else
            pc_remove_pid(&proxies, pid);
    }
    return;
}

variable_types: pid int

variable_types: stat int


daemonize

decompiled_code:
int daemonize()
{
__pid_t v0; // eax
__pid_t v1; // eax
rlim_t v2; // rbx
int v3; // edi
struct rlimit v5; // [rsp+0h] [rbp-B8h] BYREF
struct sigaction v6; // [rsp+10h] [rbp-A8h] BYREF

umask(0);
if ( getrlimit(RLIMIT_NOFILE, &v5) < 0 )
{
log_error("can't get file limit");
exit(-1);
}
v0 = fork();
if ( v0 < 0 )
goto LABEL_14;
if ( v0 )
goto LABEL_12;
setsid();
v6.sa_handler = handler;
sigemptyset(&v6.sa_mask);
v6.sa_flags = 0;
if ( sigaction(1, &v6, 0LL) < 0 )
{
log_error("can't ignore SIGHUP");
exit(-3);
}
v1 = fork();
if ( v1 < 0 )
{
LABEL_14:
log_error("can't fork");
exit(-2);
}
if ( v1 )
LABEL_12:
exit(0);
if ( v5.rlim_max == -1LL )
{
v5.rlim_max = 1024LL;
}
else if ( !v5.rlim_max )
{
goto LABEL_11;
}
v2 = 0LL;
do
{
v3 = v2++;
close(v3);
}
while ( v5.rlim_max > v2 );
LABEL_11:
open("/dev/null", 0);
open("/dev/null", 0);
return open("/dev/null", 0);
}

canonical_decompiled_code:
int func7()
{
__pid_t v0; // eax
__pid_t v1; // eax
rlim_t v2; // rbx
int v3; // edi
struct rlimit v5; // [rsp+0h] [rbp-B8h] BYREF
struct sigaction v6; // [rsp+10h] [rbp-A8h] BYREF

umask(0);
if ( getrlimit(RLIMIT_NOFILE, &v5) < 0 )
{
log_error("can't get file limit");
exit(-1);
}
v0 = fork();
if ( v0 < 0 )
goto LABEL_14;
if ( v0 )
goto LABEL_12;
setsid();
v6.sa_handler = handler;
sigemptyset(&v6.sa_mask);
v6.sa_flags = 0;
if ( sigaction(1, &v6, 0LL) < 0 )
{
log_error("can't ignore SIGHUP");
exit(-3);
}
v1 = fork();
if ( v1 < 0 )
{
LABEL_14:
log_error("can't fork");
exit(-2);
}
if ( v1 )
LABEL_12:
exit(0);
if ( v5.rlim_max == -1LL )
{
v5.rlim_max = 1024LL;
}
else if ( !v5.rlim_max )
{
goto LABEL_11;
}
v2 = 0LL;
do
{
v3 = v2++;
close(v3);
}
while ( v5.rlim_max > v2 );
LABEL_11:
open("/dev/null", 0);
open("/dev/null", 0);
return open("/dev/null", 0);
}

original_code:
void daemonize(){
    int i;
    pid_t pid;
    struct rlimit rl;
    struct sigaction sa;
    umask(0);
    if (getrlimit(RLIMIT_NOFILE, &rl) < 0){
        log_error("can't get file limit");;
        exit(-1);
    }
    if ((pid = fork()) < 0){
        log_error("can't fork");;
        exit(-2);
    }
    else if (pid != 0)
        exit(0);
    setsid();
    sa.__sigaction_handler.sa_handler = ((__sighandler_t) 1);
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = 0;
    if (sigaction(1, &sa, ((void *)0)) < 0){
        log_error("can't ignore SIGHUP");;
        exit(-3);
    }
    if ((pid = fork()) < 0){
        log_error("can't fork");;
        exit(-2);
    }
    else if (pid != 0)
        exit(0);
    if (rl.rlim_max == ((__rlim_t) -1))
        rl.rlim_max = 1024;
    for (i = 0; i < rl.rlim_max; i++)
        close(i);
    open("/dev/null", 00);
    open("/dev/null", 00);
    open("/dev/null", 00);
}

canonical_original_code:
void daemonize(){
    int i;
    int pid;
    struct rlimit rl;
    struct sigaction sa;
    umask(0);
    if (getrlimit(RLIMIT_NOFILE, &rl) < 0){
        log_error("can't get file limit");;
        exit(-1);
    }
    if ((pid = fork()) < 0){
        log_error("can't fork");;
        exit(-2);
    }
    else if (pid != 0)
        exit(0);
    setsid();
    sa.__sigaction_handler.sa_handler = ((void (*)(int)) 1);
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = 0;
    if (sigaction(1, &sa, ((void *)0)) < 0){
        log_error("can't ignore SIGHUP");;
        exit(-3);
    }
    if ((pid = fork()) < 0){
        log_error("can't fork");;
        exit(-2);
    }
    else if (pid != 0)
        exit(0);
    if (rl.rlim_max == ((__rlim_t) -1))
        rl.rlim_max = 1024;
    for (i = 0; i < rl.rlim_max; i++)
        close(i);
    open("/dev/null", 00);
    open("/dev/null", 00);
    open("/dev/null", 00);
}

variable_types: struct rlimit { unsigned long rlim_cur; unsigned long rlim_max; }

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


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
const char **v4; // rdx
const char *v5; // rbp
int v6; // er12
int v7; // eax
__int64 v9; // rbp
int v10; // eax
__pid_t v11; // er13

memset(&proxies, 0, (size_t)&unk_18260);
strcpy(&g_configfile[16], ".conf");
*(__m128i *)g_configfile = _mm_load_si128((const __m128i *)&LC28);
while ( 2 )
{
v3 = getopt_long(argc, (char *const *)argv, "hvc:l:f", &long_options_0, 0LL);
if ( v3 != -1 )
{
switch ( v3 )
{
case 'c':
strcpy(g_configfile, optarg);
continue;
case 'f':
g_foregound = 1;
continue;
case 'h':
usage_constprop_0();
exit(0);
case 'l':
g_log_level = atoi(optarg);
continue;
case 'v':
printf("hproxy %s\n", "V3.0.0");
exit(0);
default:
goto LABEL_26;
}
}
break;
}
v4 = &argv[optind];
if ( argc - optind <= 0 || (v5 = *v4) == 0LL )
{
LABEL_26:
usage_constprop_0();
exit(-2);
}
v6 = load_conf(g_conf, g_configfile);
if ( v6 )
{
v6 = -3;
log_error("read config file %s error!", g_configfile);
return v6;
}
if ( g_log_level < 0 )
g_log_level = *(_DWORD *)&g_conf[2052];
if ( g_foregound )
log_init_ex((unsigned int)g_log_level, 0LL, (unsigned int)((_DWORD)g_conf + 1024), 3LL);
else
log_init_ex((unsigned int)g_log_level, 0LL, (unsigned int)((_DWORD)g_conf + 1024), 2LL);
if ( !strcasecmp(v5, "start") )
{
if ( !g_foregound )
daemonize();
printf("pid file: %s\n", (const char *)(unsigned int)((_DWORD)g_conf + 512));
v7 = write_pid((unsigned int)((_DWORD)g_conf + 512));
if ( !v7 )
{
start();
return 0;
}
if ( v7 <= 0 )
return -4;
log_error("forticlient proxy has already run!");
}
if ( !strcasecmp(v5, "stop") )
{
v10 = get_pid((unsigned int)((_DWORD)g_conf + 512));
v11 = v10;
if ( v10 > 0 )
{
log_debug("pid = %d", (unsigned int)v10);
kill(v11, 15);
}
}
if ( !strcasecmp(v5, "reload") )
{
v6 = 0;
v9 = (unsigned int)get_pid((unsigned int)((_DWORD)g_conf + 512));
log_debug("pid = %d", v9);
kill(v9, 1);
}
return v6;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
const char **v4; // rdx
const char *v5; // rbp
int v6; // er12
int v7; // eax
__int64 v9; // rbp
int v10; // eax
__pid_t v11; // er13

memset(&proxies, 0, (size_t)&unk_18260);
strcpy(&g_configfile[16], ".conf");
*(__m128i *)g_configfile = _mm_load_si128((const __m128i *)&LC28);
while ( 2 )
{
v3 = getopt_long(argc, (char *const *)argv, "hvc:l:f", &long_options_0, 0LL);
if ( v3 != -1 )
{
switch ( v3 )
{
case 'c':
strcpy(g_configfile, optarg);
continue;
case 'f':
g_foregound = 1;
continue;
case 'h':
func6();
exit(0);
case 'l':
g_log_level = atoi(optarg);
continue;
case 'v':
printf("hproxy %s\n", "V3.0.0");
exit(0);
default:
goto LABEL_26;
}
}
break;
}
v4 = &argv[optind];
if ( argc - optind <= 0 || (v5 = *v4) == 0LL )
{
LABEL_26:
func6();
exit(-2);
}
v6 = load_conf(g_conf, g_configfile);
if ( v6 )
{
v6 = -3;
log_error("read config file %s error!", g_configfile);
return v6;
}
if ( g_log_level < 0 )
g_log_level = *(_DWORD *)&g_conf[2052];
if ( g_foregound )
log_init_ex((unsigned int)g_log_level, 0LL, (unsigned int)((_DWORD)g_conf + 1024), 3LL);
else
log_init_ex((unsigned int)g_log_level, 0LL, (unsigned int)((_DWORD)g_conf + 1024), 2LL);
if ( !strcasecmp(v5, "start") )
{
if ( !g_foregound )
func7();
printf("pid file: %s\n", (const char *)(unsigned int)((_DWORD)g_conf + 512));
v7 = write_pid((unsigned int)((_DWORD)g_conf + 512));
if ( !v7 )
{
func3();
return 0;
}
if ( v7 <= 0 )
return -4;
log_error("forticlient proxy has already run!");
}
if ( !strcasecmp(v5, "stop") )
{
v10 = get_pid((unsigned int)((_DWORD)g_conf + 512));
v11 = v10;
if ( v10 > 0 )
{
log_debug("pid = %d", (unsigned int)v10);
kill(v11, 15);
}
}
if ( !strcasecmp(v5, "reload") )
{
v6 = 0;
v9 = (unsigned int)get_pid((unsigned int)((_DWORD)g_conf + 512));
log_debug("pid = %d", v9);
kill(v9, 1);
}
return v6;
}

original_code:
int main(int argc , char* argv[]){
    int err = 0 , found_config = 0 , c = 0;
    char* cmd = ((void *)0);
    extern char * optarg;
    extern int optind;
    const char *short_options = "hvc:l:f";
    static struct option long_options [] ={
     {"help", 0, ((void *)0), 'h' },
     {"version", 0, ((void *)0), 'v' },
     {"config", 1, ((void *)0), 'c' },
     {"log", 1, ((void *)0), 'l' },
     {"foreground", 0, ((void *)0), 'f' },
     {((void *)0), 0, ((void *)0), 0 }
    };
    memset(&proxies, 0 , sizeof(proxies));
    strcpy(g_configfile , "/etc/forticlient.conf");
    while ((c=getopt_long(argc, argv, short_options,long_options,((void *)0))) != -1){
        switch (c){
        case 'v':
            printf("hproxy  %s\n", "V3.0.0");
            exit(0);
            break;
        case 'h':
            usage(argv[0]);
            exit(0);
        case 'c':
            found_config = 1;
            strcpy(g_configfile , optarg);
            break;
        case 'f':
      g_foregound = 1;
            break;
        case 'l':
            g_log_level = atoi(optarg);
            break;
        default:
            usage(argv[0]);
            exit(-2);
        }
    }
    argc -= optind;
    argv += optind;
    if (argc > 0){
        cmd = argv[0];
        --argc; ++argv;
    }
    if (cmd == ((void *)0)){
        usage(argv[0]);
        exit(-2);
    }
    err = load_conf(&g_conf , g_configfile);
    if (err){
        log_error("read config file %s error!", g_configfile);;
        return -3;
    }
    if (g_log_level < 0)
        g_log_level = g_conf.log_level;
    if ( g_foregound )
        log_init_ex(g_log_level, FCTLOG_SOURCE_PROXY ,
         g_conf.log_file, FCTLOG_TO_CONSOLE|FCTLOG_TO_FILE);
    else
        log_init_ex(g_log_level, FCTLOG_SOURCE_PROXY ,
         g_conf.log_file, FCTLOG_TO_FILE);
    if ( strcasecmp(cmd , "start") == 0){
        if (!g_foregound)
         daemonize();
        printf("pid file: %s\n", g_conf.pid_file);
     err = write_pid(g_conf.pid_file);
     if ( err == 0 ){
        start();
           return 0;
        }else if ( err > 0){
         log_error("forticlient proxy has already run!");;
        }else{
         return -4;
        }
 }
    if ( strcasecmp(cmd , "stop") == 0){
        int pid = get_pid(g_conf.pid_file);
        if (pid > 0){
            log_debug("pid = %d", pid);;
            kill(pid , 15);
        }
    };
    if ( strcasecmp(cmd , "reload") == 0){
        int pid = get_pid(g_conf.pid_file);
        log_debug("pid = %d", pid);;
        kill(pid , 1);
    };
   return 0;
}

canonical_original_code:
int main(int argc, char *argv[]){
    int err = 0; int found_config = 0; int c = 0;
    char *cmd = ((void *)0);
    extern char *optarg;
    extern int optind;
    char *short_options = "hvc:l:f";
    static struct option long_options[] = {
     {"help", 0, ((void *)0), 'h' },
     {"version", 0, ((void *)0), 'v' },
     {"config", 1, ((void *)0), 'c' },
     {"log", 1, ((void *)0), 'l' },
     {"foreground", 0, ((void *)0), 'f' },
     {((void *)0), 0, ((void *)0), 0 }
    };
    memset(&proxies, 0 , sizeof(proxies));
    strcpy(g_configfile , "/etc/forticlient.conf");
    while ((c=getopt_long(argc, argv, short_options,long_options,((void *)0))) != -1){
        switch (c){
        case 'v':
            printf("hproxy  %s\n", "V3.0.0");
            exit(0);
            break;
        case 'h':
            usage(argv[0]);
            exit(0);
        case 'c':
            found_config = 1;
            strcpy(g_configfile , optarg);
            break;
        case 'f':
      g_foregound = 1;
            break;
        case 'l':
            g_log_level = atoi(optarg);
            break;
        default:
            usage(argv[0]);
            exit(-2);
        }
    }
    argc -= optind;
    argv += optind;
    if (argc > 0){
        cmd = argv[0];
        --argc; ++argv;
    }
    if (cmd == ((void *)0)){
        usage(argv[0]);
        exit(-2);
    }
    err = load_conf(&g_conf , g_configfile);
    if (err){
        log_error("read config file %s error!", g_configfile);;
        return -3;
    }
    if (g_log_level < 0)
        g_log_level = g_conf.log_level;
    if ( g_foregound )
        log_init_ex(g_log_level, FCTLOG_SOURCE_PROXY ,
         g_conf.log_file, FCTLOG_TO_CONSOLE|FCTLOG_TO_FILE);
    else
        log_init_ex(g_log_level, FCTLOG_SOURCE_PROXY ,
         g_conf.log_file, FCTLOG_TO_FILE);
    if ( strcasecmp(cmd , "start") == 0){
        if (!g_foregound)
         daemonize();
        printf("pid file: %s\n", g_conf.pid_file);
     err = write_pid(g_conf.pid_file);
     if ( err == 0 ){
        start();
           return 0;
        }else if ( err > 0){
         log_error("forticlient proxy has already run!");;
        }else{
         return -4;
        }
 }
    if ( strcasecmp(cmd , "stop") == 0){
        int pid = get_pid(g_conf.pid_file);
        if (pid > 0){
            log_debug("pid = %d", pid);;
            kill(pid , 15);
        }
    };
    if ( strcasecmp(cmd , "reload") == 0){
        int pid = get_pid(g_conf.pid_file);
        log_debug("pid = %d", pid);;
        kill(pid , 1);
    };
   return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: err int

variable_types: c int

variable_types:
cmd
char *

variable_types:
optarg
char *

variable_types: optind int

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


decompiled_code:
void sig_reload_config()
{
load_conf(g_conf, g_configfile);
log_init(*(unsigned int *)&g_conf[2052], 0LL, (unsigned int)((_DWORD)g_conf + 1024));
pc_kill_all(&proxies, 1LL);
}

canonical_decompiled_code:
void func4()
{
load_conf(g_conf, g_configfile);
log_init(*(unsigned int *)&g_conf[2052], 0LL, (unsigned int)((_DWORD)g_conf + 1024));
pc_kill_all(&proxies, 1LL);
}

original_code:
static void sig_reload_config(int signo){
    load_conf(&g_conf , g_configfile);
    log_init(g_conf.log_level, FCTLOG_SOURCE_PROXY , g_conf.log_file);
   pc_kill_all(&proxies, 1);
}


decompiled_code:
void __fastcall list_del(__int64 a1)
{
_QWORD *v1; // rax

_list_del(*(_QWORD **)(a1 + 8), *(_QWORD *)a1);
*v1 = 0LL;
v1[1] = 0LL;
}

original_code:
static inline void list_del(struct list_head *entry)
{
 __list_del(entry->prev, entry->next);
 entry->next = ((void *)0);
 entry->prev = ((void *)0);
}

variable_types: struct list_head { struct list_head *next; } *


original_code:
static int mem_read(struct common_cache_t* cache,char* buf, int buflen){
    int real_count = 0;
    struct mem_cache_t* mcache = (struct mem_cache_t*)cache;
    int left = mcache->mem_len - mcache->read_count;
    if (left == 0) return 0;
    real_count = left > buflen?buflen: left;
    memcpy(buf , mcache->mem + mcache->read_count, real_count);
    mcache->read_count += real_count;
    return real_count;
}

canonical_original_code:
static int mem_read(struct common_cache_t *cache, char *buf, int buflen){
    int real_count = 0;
    struct mem_cache_t *mcache = (struct mem_cache_t*)cache;
    int left = mcache->mem_len - mcache->read_count;
    if (left == 0) return 0;
    real_count = left > buflen?buflen: left;
    memcpy(buf , mcache->mem + mcache->read_count, real_count);
    mcache->read_count += real_count;
    return real_count;
}

variable_types: struct common_cache_t { struct list_head { struct list_head *next; } list; int (*read)(struct common_cache_t *, char *, int); void (*destroy)(struct common_cache_t *); } *

variable_types:
buf
char *

variable_types: buflen int

variable_types: struct mem_cache_t { struct common_cache_t { struct list_head { struct list_head *next; } list; int (*read)(struct common_cache_t *, char *, int); void (*destroy)(struct common_cache_t *); } cache; int mem_len; int read_count; char mem[]; } *

variable_types: left int

user_defined_types: struct common_cache_t { struct list_head list; int (*read)(struct common_cache_t *, char *, int); void (*destroy)(struct common_cache_t *); }

user_defined_types: struct mem_cache_t { struct common_cache_t cache; int mem_len; int read_count; char mem[]; }


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

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

original_code:
static void mem_destroy(struct common_cache_t* cache){
    if (cache != ((void *)0))
        free(cache);
}

canonical_original_code:
static void mem_destroy(struct common_cache_t *cache){
    if (cache != ((void *)0))
        free(cache);
}


decompiled_code:
void __fastcall file_destroy(void *ptr)
{
int v2; // edi

if ( ptr )
{
v2 = *((_DWORD *)ptr + 8);
if ( v2 > 0 )
{
while ( close(v2) == -1 && *_errno_location() == 4 )
v2 = *((_DWORD *)ptr + 8);
}
*((_DWORD *)ptr + 8) = -1;
unlink((const char *)ptr + 36);
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func7(void *ptr)
{
int v2; // edi

if ( ptr )
{
v2 = *((_DWORD *)ptr + 8);
if ( v2 > 0 )
{
while ( close(v2) == -1 && *_errno_location() == 4 )
v2 = *((_DWORD *)ptr + 8);
}
*((_DWORD *)ptr + 8) = -1;
unlink((const char *)ptr + 36);
free(ptr);
}
}

original_code:
static void file_destroy(struct common_cache_t* cache){
    if (cache == ((void *)0)) return ;
    struct file_cache_t* fcache = (struct file_cache_t*)cache;
    if (fcache->fd > 0) {while (close(fcache->fd) == -1 && (*__errno_location ()) == 4);};
    fcache->fd = -1;
    unlink(fcache->file_name);
    free(cache);
}

canonical_original_code:
static void file_destroy(struct common_cache_t *cache){
    if (cache == ((void *)0)) return ;
    struct file_cache_t *fcache = (struct file_cache_t*)cache;
    if (fcache->fd > 0) {while (close(fcache->fd) == -1 && (*__errno_location ()) == 4);};
    fcache->fd = -1;
    unlink(fcache->file_name);
    free(cache);
}

variable_types: struct file_cache_t { struct common_cache_t { struct list_head { struct list_head *next; } list; int (*read)(struct common_cache_t *, char *, int); void (*destroy)(struct common_cache_t *); } cache; int fd; char file_name[512]; } *

user_defined_types: struct file_cache_t { struct common_cache_t cache; int fd; char file_name[512]; }


decompiled_code:
__int64 __fastcall file_read(__int64 a1, void *a2, unsigned int a3)
{
int v4; // edi
int v6; // er12

if ( !a1 )
return 0LL;
v4 = *(_DWORD *)(a1 + 32);
v6 = 0;
if ( v4 >= 0 )
{
while ( 1 )
{
v6 = read(v4, a2, a3);
if ( v6 >= 0 || *_errno_location() != 4 )
break;
v4 = *(_DWORD *)(a1 + 32);
}
}
return (unsigned int)v6;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, void *a2, unsigned int a3)
{
int v4; // edi
int v6; // er12

if ( !a1 )
return 0LL;
v4 = *(_DWORD *)(a1 + 32);
v6 = 0;
if ( v4 >= 0 )
{
while ( 1 )
{
v6 = read(v4, a2, a3);
if ( v6 >= 0 || *_errno_location() != 4 )
break;
v4 = *(_DWORD *)(a1 + 32);
}
}
return (unsigned int)v6;
}

original_code:
static int file_read(struct common_cache_t* cache,char* buf, int buflen){
    if (cache == ((void *)0)) return 0;
    struct file_cache_t* fcache = (struct file_cache_t*)cache;
    if (fcache->fd < 0) return 0;
    int n = 0;
    do {
        n = read(fcache->fd, buf , buflen);
    } while ( n < 0 && (*__errno_location ()) == 4);
    return n;
}

canonical_original_code:
static int file_read(struct common_cache_t *cache, char *buf, int buflen){
    if (cache == ((void *)0)) return 0;
    struct file_cache_t *fcache = (struct file_cache_t*)cache;
    if (fcache->fd < 0) return 0;
    int n = 0;
    do {
        n = read(fcache->fd, buf , buflen);
    } while ( n < 0 && (*__errno_location ()) == 4);
    return n;
}

variable_types: n int


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

result = a1 + 8;
*(_DWORD *)a1 = 0;
*(_QWORD *)(a1 + 8) = a1 + 8;
*(_QWORD *)(a1 + 16) = a1 + 8;
return result;
}

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

result = a1 + 8;
*(_DWORD *)a1 = 0;
*(_QWORD *)(a1 + 8) = a1 + 8;
*(_QWORD *)(a1 + 16) = a1 + 8;
return result;
}

original_code:
void cp_init(struct cache_pool_t* cp){
    cp->cache_count = 0;
    do { (&cp->cache_head)->next = (&cp->cache_head); (&cp->cache_head)->prev = (&cp->cache_head); } while (0);
}

canonical_original_code:
void cp_init(struct cache_pool_t *cp){
    cp->cache_count = 0;
    do { (&cp->cache_head)->next = (&cp->cache_head); (&cp->cache_head)->prev = (&cp->cache_head); } while (0);
}

variable_types: struct cache_pool_t { int cache_count; struct list_head { struct list_head *next; } cache_head; } *


decompiled_code:
_BOOL8 __fastcall cp_is_empty(_DWORD *a1)
{
return *a1 <= 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func11(_DWORD *a1)
{
return *a1 <= 0;
}

original_code:
int cp_is_empty(struct cache_pool_t* cp){
    return (cp->cache_count > 0)? 0 : 1;
}

canonical_original_code:
int cp_is_empty(struct cache_pool_t *cp){
    return (cp->cache_count > 0)? 0 : 1;
}


decompiled_code:
__int64 __fastcall cp_read(__int64 a1, __int64 a2, unsigned int a3)
{
__int64 result; // rax
__int64 v6; // rbx

if ( !*(_DWORD *)a1 )
return 0LL;
do
{
v6 = *(_QWORD *)(a1 + 8);
if ( v6 == a1 + 8 )
break;
result = (*(__int64 (__fastcall **)(_QWORD, __int64, _QWORD))(v6 + 16))(*(_QWORD *)(a1 + 8), a2, a3);
if ( (int)result > 0 )
return result;
list_del(v6);
(*(void (__fastcall **)(__int64))(v6 + 24))(v6);
}
while ( (*(_DWORD *)a1)-- != 1 );
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1, __int64 a2, unsigned int a3)
{
__int64 result; // rax
__int64 v6; // rbx

if ( !*(_DWORD *)a1 )
return 0LL;
do
{
v6 = *(_QWORD *)(a1 + 8);
if ( v6 == a1 + 8 )
break;
result = (*(__int64 (__fastcall **)(_QWORD, __int64, _QWORD))(v6 + 16))(*(_QWORD *)(a1 + 8), a2, a3);
if ( (int)result > 0 )
return result;
func3(v6);
(*(void (__fastcall **)(__int64))(v6 + 24))(v6);
}
while ( (*(_DWORD *)a1)-- != 1 );
return 0LL;
}

original_code:
int cp_read(struct cache_pool_t* cp ,char* buf, int buflen){
    for(;;){
        if (cp->cache_count == 0)
            return 0;
        struct common_cache_t* cur_cache =
            (struct common_cache_t*)cp->cache_head.next;
        if ((unsigned long)cur_cache == (unsigned long)(&cp->cache_head))
            return 0;
        int n = cur_cache->read(cur_cache , buf , buflen);
        if (n <= 0){
            list_del(&cur_cache->list);
            cur_cache->destroy(cur_cache);
            cp->cache_count--;
        }else
            return n;
    }
    return 0;
}

canonical_original_code:
int cp_read(struct cache_pool_t *cp, char *buf, int buflen){
    for(;;){
        if (cp->cache_count == 0)
            return 0;
        struct common_cache_t *cur_cache = (struct common_cache_t*)cp->cache_head.next;
        if ((unsigned long)cur_cache == (unsigned long)(&cp->cache_head))
            return 0;
        int n = cur_cache->read(cur_cache , buf , buflen);
        if (n <= 0){
            list_del(&cur_cache->list);
            cur_cache->destroy(cur_cache);
            cp->cache_count--;
        }else
            return n;
    }
    return 0;
}


decompiled_code:
__int64 __fastcall cp_destroy(unsigned int *a1)
{
__int64 result; // rax
__int64 v2; // rdx
__int64 *v3; // rbx
__int64 v4; // rdx

if ( a1 )
{
result = *a1;
if ( (_DWORD)result )
{
v2 = *((_QWORD *)a1 + 1);
if ( (unsigned int *)v2 != a1 + 2 )
{
do
{
v3 = *(__int64 **)(v2 + 8);
list_del(v2);
result = (*(__int64 (__fastcall **)(__int64))(v4 + 24))(v4);
v2 = *v3;
}
while ( (unsigned int *)*v3 != a1 + 2 );
}
*a1 = 0;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func13(unsigned int *a1)
{
__int64 result; // rax
__int64 v2; // rdx
__int64 *v3; // rbx
__int64 v4; // rdx

if ( a1 )
{
result = *a1;
if ( (_DWORD)result )
{
v2 = *((_QWORD *)a1 + 1);
if ( (unsigned int *)v2 != a1 + 2 )
{
do
{
v3 = *(__int64 **)(v2 + 8);
func3(v2);
result = (*(__int64 (__fastcall **)(__int64))(v4 + 24))(v4);
v2 = *v3;
}
while ( (unsigned int *)*v3 != a1 + 2 );
}
*a1 = 0;
}
}
return result;
}

original_code:
void cp_destroy(struct cache_pool_t* cp){
    struct list_head *pos = ((void *)0);
    struct common_cache_t* cur_cache = ((void *)0);
    if (cp == ((void *)0) || cp->cache_count == 0) return;
    for (pos = (&cp->cache_head)->next; pos != (&cp->cache_head); pos = pos->next){
        cur_cache = (struct common_cache_t*)pos;
        pos = pos->prev;
        list_del(&cur_cache->list);
        cur_cache->destroy(cur_cache);
    }
    cp->cache_count = 0;
}

canonical_original_code:
void cp_destroy(struct cache_pool_t *cp){
    struct list_head *pos = ((void *)0);
    struct common_cache_t *cur_cache = ((void *)0);
    if (cp == ((void *)0) || cp->cache_count == 0) return;
    for (pos = (&cp->cache_head)->next; pos != (&cp->cache_head); pos = pos->next){
        cur_cache = (struct common_cache_t *)pos;
        pos = pos->prev;
        list_del(&cur_cache->list);
        cur_cache->destroy(cur_cache);
    }
    cp->cache_count = 0;
}