decompiled_code:
void __fastcall __noreturn launch_callmgr(struct in_addr a1, __int64 a2, __int64 a3, __int64 *a4, __int64 a5)
{
int v6; // eax
__int64 v7; // [rsp+0h] [rbp-B8h] BYREF
char *v8; // [rsp+8h] [rbp-B0h]
__int64 v9; // [rsp+10h] [rbp-A8h]
char s[152]; // [rsp+20h] [rbp-98h] BYREF

v7 = *a4;
v8 = inet_ntoa(a1);
v9 = a2;
snprintf(s, 0x80uLL, "pptp: call manager for %s", v8);
if ( prctl(15, "pptpcm", 0LL, 0LL, 0LL) )
perror("prctl");
v6 = callmgr_main(3LL, &v7, a5);
exit(v6);
}

canonical_decompiled_code:
void __fastcall __noreturn func4(struct in_addr a1, __int64 a2, __int64 a3, __int64 *a4, __int64 a5)
{
int v6; // eax
__int64 v7; // [rsp+0h] [rbp-B8h] BYREF
char *v8; // [rsp+8h] [rbp-B0h]
__int64 v9; // [rsp+10h] [rbp-A8h]
char s[152]; // [rsp+20h] [rbp-98h] BYREF

v7 = *a4;
v8 = inet_ntoa(a1);
v9 = a2;
snprintf(s, 0x80uLL, "pptp: call manager for %s", v8);
if ( prctl(15, "pptpcm", 0LL, 0LL, 0LL) )
perror("prctl");
v6 = callmgr_main(3LL, &v7, a5);
exit(v6);
}

original_code:
void launch_callmgr(struct in_addr inetaddr, char *phonenr, int argc,
        char**argv,char**envp)
{
      char *my_argv[3] = { argv[0], inet_ntoa(inetaddr), phonenr };
      char buf[128];
      snprintf(buf, sizeof(buf), "pptp: call manager for %s", my_argv[1]);
      int rc;
      rc = prctl(15, "pptpcm", 0, 0, 0);
      if (rc != 0) perror("prctl");
      exit(callmgr_main(3, my_argv, envp));
}

canonical_original_code:
void launch_callmgr(struct in_addr inetaddr, char *phonenr, int argc, char **argv, char **envp)
{
      char *my_argv[3] = { argv[0], inet_ntoa(inetaddr), phonenr };
      char buf[128];
      snprintf(buf, sizeof(buf), "pptp: call manager for %s", my_argv[1]);
      int rc;
      rc = prctl(15, "pptpcm", 0, 0, 0);
      if (rc != 0) perror("prctl");
      exit(callmgr_main(3, my_argv, envp));
}

variable_types:
inetaddr
struct in_addr { unsigned int s_addr; }

variable_types:
phonenr
char *

variable_types: argc int

variable_types:
argv
char **

variable_types:
envp
char **

variable_types:
buf
char [128]

variable_types: rc int


repo: kcburge/pptpclient

decompiled_code:
__int64 __fastcall open_callmgr(struct in_addr a1, __int64 a2, unsigned int a3, __int64 *a4, __int64 a5, int a6, int fd)
{
int v10; // eax
unsigned int v11; // ebp
int v12; // ebx
__pid_t v13; // eax
int *v15; // rax
char *v16; // rax
int stat_loc; // [rsp+1Ch] [rbp-ACh] BYREF
struct sockaddr addr; // [rsp+20h] [rbp-A8h] BYREF

v10 = socket(1, 1, 0);
if ( v10 >= 0 )
{
v11 = v10;
v12 = 3;
callmgr_name_unixsock(&addr, a1.s_addr, (unsigned int)localbind);
while ( 1 )
{
if ( connect(v11, &addr, 0x6Eu) >= 0 )
return v11;
unlink(addr.sa_data);
v13 = fork();
if ( v13 == -1 )
{
fatal("open_callmgr", "pptp.c", 484LL, "fork() to launch call manager failed.");
LABEL_11:
close(v11);
close(a6);
close(fd);
launch_callmgr(a1, a2, a3, a4, a5);
}
if ( !v13 )
goto LABEL_11;
waitpid(v13, &stat_loc, 0);
if ( stat_loc )
break;
sleep(1u);
if ( !--v12 )
{
close(v11);
fatal("open_callmgr", "pptp.c", 504LL, "Could not launch call manager after %d tries.", 3LL);
return v11;
}
}
fatal("open_callmgr", "pptp.c", 496LL, "Call manager exited with error %d", (unsigned int)stat_loc);
}
v15 = _errno_location();
v16 = strerror(*v15);
fatal("open_callmgr", "pptp.c", 471LL, "Could not create unix domain socket: %s", v16);
return get_call_id((int)"open_callmgr");
}

canonical_decompiled_code:
__int64 __fastcall func5(struct in_addr a1, __int64 a2, unsigned int a3, __int64 *a4, __int64 a5, int a6, int fd)
{
int v10; // eax
unsigned int v11; // ebp
int v12; // ebx
__pid_t v13; // eax
int *v15; // rax
char *v16; // rax
int stat_loc; // [rsp+1Ch] [rbp-ACh] BYREF
struct sockaddr addr; // [rsp+20h] [rbp-A8h] BYREF

v10 = socket(1, 1, 0);
if ( v10 >= 0 )
{
v11 = v10;
v12 = 3;
callmgr_name_unixsock(&addr, a1.s_addr, (unsigned int)localbind);
while ( 1 )
{
if ( connect(v11, &addr, 0x6Eu) >= 0 )
return v11;
unlink(addr.sa_data);
v13 = fork();
if ( v13 == -1 )
{
fatal("open_callmgr", "pptp.c", 484LL, "fork() to launch call manager failed.");
LABEL_11:
close(v11);
close(a6);
close(fd);
func4(a1, a2, a3, a4, a5);
}
if ( !v13 )
goto LABEL_11;
waitpid(v13, &stat_loc, 0);
if ( stat_loc )
break;
sleep(1u);
if ( !--v12 )
{
close(v11);
fatal("open_callmgr", "pptp.c", 504LL, "Could not launch call manager after %d tries.", 3LL);
return v11;
}
}
fatal("open_callmgr", "pptp.c", 496LL, "Call manager exited with error %d", (unsigned int)stat_loc);
}
v15 = _errno_location();
v16 = strerror(*v15);
fatal("open_callmgr", "pptp.c", 471LL, "Could not create unix domain socket: %s", v16);
return func6((int)"open_callmgr");
}

original_code:
int open_callmgr(struct in_addr inetaddr, char *phonenr, int argc, char **argv,
        char **envp, int pty_fd, int gre_fd)
{
    struct sockaddr_un where;
    const int NUM_TRIES = 3;
    int i, fd;
    pid_t pid;
    int status;
    if ((fd = socket(1, SOCK_STREAM, 0)) < 0) {
        _fatal(__FUNCTION__,"pptp.c",471, "Could not create unix domain socket: %s" , strerror((*__errno_location ())));
    }
    callmgr_name_unixsock(&where, inetaddr, localbind);
    for (i = 0; i < NUM_TRIES; i++) {
        if (connect(fd, (struct sockaddr *) &where, sizeof(where)) < 0) {
            unlink (where.sun_path);
            switch (pid = fork()) {
                case -1:
                    _fatal(__FUNCTION__,"pptp.c",484, "fork() to launch call manager failed.");
                case 0:
                {
                    close (fd);
                    close(pty_fd);
                    close(gre_fd);
                    launch_callmgr(inetaddr, phonenr, argc, argv, envp);
                }
                default:
                    waitpid(pid, &status, 0);
                    if (status!= 0)
                        _fatal(__FUNCTION__,"pptp.c",496, "Call manager exited with error %d" , status);
                    break;
            }
            sleep(1);
        }
        else return fd;
    }
    close(fd);
    _fatal(__FUNCTION__,"pptp.c",504, "Could not launch call manager after %d tries." , i);
    return -1;
}

canonical_original_code:
int open_callmgr(struct in_addr inetaddr, char *phonenr, int argc, char **argv, char **envp, int pty_fd, int gre_fd)
{
    struct sockaddr_un where;
    int NUM_TRIES = 3;
    int i; int fd;
    int pid;
    int status;
    if ((fd = socket(1, SOCK_STREAM, 0)) < 0) {
        _fatal(__FUNCTION__,"pptp.c",471, "Could not create unix domain socket: %s" , strerror((*__errno_location ())));
    }
    callmgr_name_unixsock(&where, inetaddr, localbind);
    for (i = 0; i < NUM_TRIES; i++) {
        if (connect(fd, (struct sockaddr *) &where, sizeof(where)) < 0) {
            unlink (where.sun_path);
            switch (pid = fork()) {
                case -1:
                    _fatal(__FUNCTION__,"pptp.c",484, "fork() to launch call manager failed.");
                case 0:
                {
                    close (fd);
                    close(pty_fd);
                    close(gre_fd);
                    launch_callmgr(inetaddr, phonenr, argc, argv, envp);
                }
                default:
                    waitpid(pid, &status, 0);
                    if (status!= 0)
                        _fatal(__FUNCTION__,"pptp.c",496, "Call manager exited with error %d" , status);
                    break;
            }
            sleep(1);
        }
        else return fd;
    }
    close(fd);
    _fatal(__FUNCTION__,"pptp.c",504, "Could not launch call manager after %d tries." , i);
    return -1;
}

variable_types: struct sockaddr_un { unsigned short sun_family; char sun_path[108]; }

variable_types: i int

variable_types: fd int

variable_types: pid int

variable_types: status int

return_type: int

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


decompiled_code:
__int64 __fastcall get_call_id(int fd, int a2, int a3, _WORD *a4, _WORD *a5)
{
int v8; // [rsp+8h] [rbp-30h] BYREF
int buf[4]; // [rsp+Ch] [rbp-2Ch] BYREF
__int16 v10; // [rsp+1Ch] [rbp-1Ch] BYREF
__int16 v11[13]; // [rsp+1Eh] [rbp-1Ah] BYREF

buf[0] = a2;
v8 = a3;
if ( (unsigned int)write(fd, buf, 4uLL) != 4
|| (unsigned int)write(fd, &v8, 4uLL) != 4
|| (unsigned int)read(fd, &v10, 2uLL) != 2
|| (unsigned int)read(fd, v11, 2uLL) != 2 )
{
return 0xFFFFFFFFLL;
}
*a4 = v10;
*a5 = v11[0];
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(int fd, int a2, int a3, _WORD *a4, _WORD *a5)
{
int v8; // [rsp+8h] [rbp-30h] BYREF
int buf[4]; // [rsp+Ch] [rbp-2Ch] BYREF
__int16 v10; // [rsp+1Ch] [rbp-1Ch] BYREF
__int16 v11[13]; // [rsp+1Eh] [rbp-1Ah] BYREF

buf[0] = a2;
v8 = a3;
if ( (unsigned int)write(fd, buf, 4uLL) != 4
|| (unsigned int)write(fd, &v8, 4uLL) != 4
|| (unsigned int)read(fd, &v10, 2uLL) != 2
|| (unsigned int)read(fd, v11, 2uLL) != 2 )
{
return 0xFFFFFFFFLL;
}
*a4 = v10;
*a5 = v11[0];
return 0LL;
}

original_code:
int get_call_id(int sock, pid_t gre, pid_t pppd,
   u_int16_t *call_id, u_int16_t *peer_call_id)
{
    u_int16_t m_call_id, m_peer_call_id;
    int rc;
    rc = write(sock, &gre, sizeof(gre));
    if (rc != sizeof(gre))
        return -1;
    rc = write(sock, &pppd, sizeof(pppd));
    if (rc != sizeof(pppd))
        return -1;
    rc = read(sock, &m_call_id, sizeof(m_call_id));
    if (rc != sizeof(m_call_id))
        return -1;
    rc = read(sock, &m_peer_call_id, sizeof(m_peer_call_id));
    if (rc != sizeof(m_peer_call_id))
        return -1;
    *call_id = m_call_id;
    *peer_call_id = m_peer_call_id;
    return 0;
}

canonical_original_code:
int get_call_id(int sock, int gre, int pppd, unsigned int *call_id, unsigned int *peer_call_id)
{
    unsigned int m_call_id; unsigned int m_peer_call_id;
    int rc;
    rc = write(sock, &gre, sizeof(gre));
    if (rc != sizeof(gre))
        return -1;
    rc = write(sock, &pppd, sizeof(pppd));
    if (rc != sizeof(pppd))
        return -1;
    rc = read(sock, &m_call_id, sizeof(m_call_id));
    if (rc != sizeof(m_call_id))
        return -1;
    rc = read(sock, &m_peer_call_id, sizeof(m_peer_call_id));
    if (rc != sizeof(m_peer_call_id))
        return -1;
    *call_id = m_call_id;
    *peer_call_id = m_peer_call_id;
    return 0;
}

variable_types: sock int

variable_types: gre int

variable_types: pppd int


decompiled_code:
int __fastcall launch_pppd(__int64 a1, int a2, const void *a3)
{
void *v4; // rsp
unsigned __int64 v5; // rax
__int64 v7; // rdx
int v8; // ebx
char *v10[3]; // [rsp-6h] [rbp-18h] BYREF
void *retaddr; // [rsp+12h] [rbp+0h] BYREF

v4 = alloca(8LL * (a2 + 4));
v5 = ((unsigned __int64)v10 + 7) >> 3;
*(_QWORD *)(8 * v5) = "/usr/sbin/pppd";
qword_8[v5] = a1;
*((_QWORD *)sighandler + v5) = "38400";
if ( a2 <= 0 )
{
v8 = 3;
}
else
{
v7 = a2;
v8 = a2 + 3;
memcpy(&retaddr, a3, 8 * v7);
}
v10[v8] = 0LL;
return execvp("/usr/sbin/pppd", v10);
}

canonical_decompiled_code:
int __fastcall func7(__int64 a1, int a2, const void *a3)
{
void *v4; // rsp
unsigned __int64 v5; // rax
__int64 v7; // rdx
int v8; // ebx
char *v10[3]; // [rsp-6h] [rbp-18h] BYREF
void *retaddr; // [rsp+12h] [rbp+0h] BYREF

v4 = alloca(8LL * (a2 + 4));
v5 = ((unsigned __int64)v10 + 7) >> 3;
*(_QWORD *)(8 * v5) = "/usr/sbin/pppd";
qword_8[v5] = a1;
*((_QWORD *)sighandler + v5) = "38400";
if ( a2 <= 0 )
{
v8 = 3;
}
else
{
v7 = a2;
v8 = a2 + 3;
memcpy(&retaddr, a3, 8 * v7);
}
v10[v8] = 0LL;
return execvp("/usr/sbin/pppd", v10);
}

original_code:
void launch_pppd(char *ttydev, int argc, char **argv)
{
    char *new_argv[argc + 4];
    int i = 0, j;
    new_argv[i++] = "/usr/sbin/pppd";
    new_argv[i++] = ttydev;
    new_argv[i++] = "38400";
    for (j = 0; j < argc; j++)
        new_argv[i++] = argv[j];
    new_argv[i] = ((void *)0);
    execvp(new_argv[0], new_argv);
}

canonical_original_code:
void launch_pppd(char *ttydev, int argc, char **argv)
{
    char *new_argv[-1];
    int i = 0; int j;
    new_argv[i++] = "/usr/sbin/pppd";
    new_argv[i++] = ttydev;
    new_argv[i++] = "38400";
    for (j = 0; j < argc; j++)
        new_argv[i++] = argv[j];
    new_argv[i] = ((void *)0);
    execvp(new_argv[0], new_argv);
}

variable_types:
ttydev
char *

variable_types: j int

return_type: void


main

canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
double v3; // xmm0_8
double v4; // xmm1_8
int v6; // eax
int v7; // eax
int v8; // er13
char *v9; // rax
unsigned int v10; // eax
char *v11; // r13
struct in_addr v12; // er14
__pid_t v13; // eax
int v14; // eax
int v15; // eax
FILE *v16; // rcx
char *v17; // rcx
int v18; // [rsp+4h] [rbp-1134h]
unsigned __int16 v19; // [rsp+14h] [rbp-1124h] BYREF
unsigned __int16 v20; // [rsp+16h] [rbp-1122h] BYREF
int fd; // [rsp+18h] [rbp-1120h]
int amaster; // [rsp+1Ch] [rbp-111Ch] BYREF
int aslave; // [rsp+20h] [rbp-1118h] BYREF
__pid_t pid; // [rsp+24h] [rbp-1114h]
int v25; // [rsp+28h] [rbp-1110h]
int v26; // [rsp+2Ch] [rbp-110Ch]
int v27; // [rsp+30h] [rbp-1108h]
int longind; // [rsp+34h] [rbp-1104h] BYREF
char *v29; // [rsp+38h] [rbp-1100h]
char dest[80]; // [rsp+40h] [rbp-10F8h] BYREF
char s[128]; // [rsp+90h] [rbp-10A8h] BYREF
char name[4136]; // [rsp+110h] [rbp-1028h] BYREF

fd = -1;
v29 = 0LL;
v26 = 1;
v27 = 0;
while ( 1 )
{
while ( 1 )
{
longind = 0;
v6 = getopt_long(argc, (char *const *)argv, &shortopts, &long_options_3, &longind);
if ( v6 == -1 )
break;
if ( v6 )
goto LABEL_27;
if ( longind )
{
switch ( longind )
{
case 1:
v26 = 0;
break;
case 2:
v10 = find_quirk(optarg);
if ( (unsigned int)set_quirk_index(v10) )
goto LABEL_27;
break;
case 3:
v27 = 1;
break;
case 4:
syncppp = 1;
break;
case 5:
v11 = optarg;
v3 = atof(optarg);
*(float *)&v3 = v3;
v4 = *(float *)&v3;
if ( *(float *)&v3 < 0.009900000000000001 || *(float *)&v3 > 10.0 )
goto LABEL_55;
*(float *)&v3 = *(float *)&v3 * 1000000.0;
packet_timeout_usecs = (int)*(float *)&v3;
break;
case 6:
goto LABEL_43;
case 7:
if ( inet_pton(2, optarg, &localbind) <= 0 )
{
fprintf(stderr, "Local bind address %s invalid\n", optarg);
log(v3);
goto LABEL_46;
}
break;
case 8:
log_level = atoi(optarg);
if ( (unsigned int)log_level > 2 )
goto LABEL_27;
break;
case 9:
disable_buffer = 1;
break;
case 10:
v14 = atoi(optarg);
if ( v14 < 0 )
{
fwrite("--idle-wait must not be negative\n", 1uLL, 0x21uLL, stderr);
log(v3);
goto LABEL_46;
}
idle_wait = v14;
break;
case 11:
v15 = atoi(optarg);
v16 = stderr;
if ( v15 < 0 )
goto LABEL_74;
max_echo_wait = v15;
fwrite("--max-echo-wait ignored, not yet implemented\n", 1uLL, 0x2DuLL, stderr);
break;
case 12:
fprintf(stdout, "%s\n", version);
goto LABEL_72;
case 13:
test_type = atoi(optarg);
break;
case 14:
test_rate = atoi(optarg);
break;
case 15:
v7 = atoi(optarg);
v8 = v7;
if ( v7 <= 0 )
{
fwrite("--missing-window must be integer greater than zero\n", 1uLL, 0x33uLL, stderr);
log(v3);
goto LABEL_46;
}
if ( v7 > 299 )
{
fprintf(stderr, "--missing-window validation is active and set to: %d\n", (unsigned int)v7);
v3 = log(v3);
missing_window = v8;
}
else
{
fwrite(
"--missing-window is set very low: default=300, recommended=6000 - proceeding anyway\n",
1uLL,
0x54uLL,
stderr);
v3 = log(v3);
}
break;
}
}
else
{
v9 = strncpy(dest, optarg, 0x40uLL);
dest[64] = 0;
v29 = v9;
}
}
if ( optind >= argc )
LABEL_27:
usage(*argv, v3);
v12.s_addr = get_ip_address(argv[optind]);
v11 = (char *)(unsigned int)++optind;
v3 = log(v3);
v18 = pptp_gre_bind(v12.s_addr, "pptp.c", v3);
if ( v18 < 0 )
{
close(fd);
fatal("main", "pptp.c", 339LL, "Cannot bind GRE socket, aborting.");
LABEL_55:
fprintf(stderr, "Packet timeout %s (%f) out of range: should be between 0.01 and 10 seconds\n", v11, v4);
log(v4);
LABEL_46:
exit(2);
}
if ( !v26 )
{
aslave = 0;
amaster = 0;
close(1);
v25 = getpid();
pid = 0;
goto LABEL_58;
}
if ( openpty(&amaster, &aslave, name, 0LL, 0LL) < 0 )
{
close(fd);
fatal("main", "pptp.c", 347LL, "Could not find free pty.");
LABEL_74:
fwrite("--max-echo-wait must not be negative\n", 1uLL, 0x25uLL, v16);
log(v3);
goto LABEL_46;
}
signal(10, do_nothing);
signal(17, do_nothing);
pid = getpid();
v13 = fork();
v25 = v13;
if ( v13 == -1 )
{
fatal("main", "pptp.c", 356LL, "Could not fork pppd process");
JUMPOUT(0x1E4FLL);
}
if ( !v13 )
{
close(aslave);
signal(10, 0LL);
v25 = getpid();
do
LABEL_58:
fd = open_callmgr(v12, (__int64)v29, argc, (__int64 *)argv, (__int64)envp, amaster, v18);
while ( (int)get_call_id(fd, pid, v25, &v19, &v20) < 0 );
if ( v26 )
{
kill(pid, 10);
sleep(2u);
if ( !v27 && daemon(0, 0) )
perror("daemon");
}
else
{
file2fd("/dev/null", "wb", 2LL);
}
v17 = ttyname(aslave);
if ( !v17 )
v17 = "(null)";
snprintf(s, 0x80uLL, "pptp: GRE-to-PPP gateway on %s", v17);
if ( prctl(15, "pptpgw", 0LL, 0LL, 0LL) )
perror("prctl");
if ( !_sigsetjmp(env, 1) )
{
signal(2, sighandler);
signal(15, sighandler);
signal(9, sighandler);
signal(17, sighandler);
signal(10, (__sighandler_t)sigstats);
pptp_gre_copy(v19, v20, (unsigned int)amaster, (unsigned int)v18);
}
if ( v26 )
kill(pid, 15);
close(amaster);
close(fd);
LABEL_72:
exit(0);
}
close(amaster);
if ( !signaled )
LABEL_80:
pause();
if ( signaled == 17 )
{
fatal("main", "pptp.c", 374LL, "Child process died");
goto LABEL_80;
}
launch_pppd((__int64)name, argc - (_DWORD)v11, &argv[(int)v11]);
perror("Error");
fatal("main", "pptp.c", 378LL, "Could not launch pppd");
LABEL_43:
log_string = strdup(optarg);
}
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
double v3; // xmm0_8
double v4; // xmm1_8
int v6; // eax
int v7; // eax
int v8; // er13
char *v9; // rax
unsigned int v10; // eax
char *v11; // r13
struct in_addr v12; // er14
__pid_t v13; // eax
int v14; // eax
int v15; // eax
FILE *v16; // rcx
char *v17; // rcx
int v18; // [rsp+4h] [rbp-1134h]
unsigned __int16 v19; // [rsp+14h] [rbp-1124h] BYREF
unsigned __int16 v20; // [rsp+16h] [rbp-1122h] BYREF
int fd; // [rsp+18h] [rbp-1120h]
int amaster; // [rsp+1Ch] [rbp-111Ch] BYREF
int aslave; // [rsp+20h] [rbp-1118h] BYREF
__pid_t pid; // [rsp+24h] [rbp-1114h]
int v25; // [rsp+28h] [rbp-1110h]
int v26; // [rsp+2Ch] [rbp-110Ch]
int v27; // [rsp+30h] [rbp-1108h]
int longind; // [rsp+34h] [rbp-1104h] BYREF
char *v29; // [rsp+38h] [rbp-1100h]
char dest[80]; // [rsp+40h] [rbp-10F8h] BYREF
char s[128]; // [rsp+90h] [rbp-10A8h] BYREF
char name[4136]; // [rsp+110h] [rbp-1028h] BYREF

fd = -1;
v29 = 0LL;
v26 = 1;
v27 = 0;
while ( 1 )
{
while ( 1 )
{
longind = 0;
v6 = getopt_long(argc, (char *const *)argv, &shortopts, &long_options_3, &longind);
if ( v6 == -1 )
break;
if ( v6 )
goto LABEL_27;
if ( longind )
{
switch ( longind )
{
case 1:
v26 = 0;
break;
case 2:
v10 = find_quirk(optarg);
if ( (unsigned int)set_quirk_index(v10) )
goto LABEL_27;
break;
case 3:
v27 = 1;
break;
case 4:
syncppp = 1;
break;
case 5:
v11 = optarg;
v3 = atof(optarg);
*(float *)&v3 = v3;
v4 = *(float *)&v3;
if ( *(float *)&v3 < 0.009900000000000001 || *(float *)&v3 > 10.0 )
goto LABEL_55;
*(float *)&v3 = *(float *)&v3 * 1000000.0;
packet_timeout_usecs = (int)*(float *)&v3;
break;
case 6:
goto LABEL_43;
case 7:
if ( inet_pton(2, optarg, &localbind) <= 0 )
{
fprintf(stderr, "Local bind address %s invalid\n", optarg);
log(v3);
goto LABEL_46;
}
break;
case 8:
log_level = atoi(optarg);
if ( (unsigned int)log_level > 2 )
goto LABEL_27;
break;
case 9:
disable_buffer = 1;
break;
case 10:
v14 = atoi(optarg);
if ( v14 < 0 )
{
fwrite("--idle-wait must not be negative\n", 1uLL, 0x21uLL, stderr);
log(v3);
goto LABEL_46;
}
idle_wait = v14;
break;
case 11:
v15 = atoi(optarg);
v16 = stderr;
if ( v15 < 0 )
goto LABEL_74;
max_echo_wait = v15;
fwrite("--max-echo-wait ignored, not yet implemented\n", 1uLL, 0x2DuLL, stderr);
break;
case 12:
fprintf(stdout, "%s\n", version);
goto LABEL_72;
case 13:
test_type = atoi(optarg);
break;
case 14:
test_rate = atoi(optarg);
break;
case 15:
v7 = atoi(optarg);
v8 = v7;
if ( v7 <= 0 )
{
fwrite("--missing-window must be integer greater than zero\n", 1uLL, 0x33uLL, stderr);
log(v3);
goto LABEL_46;
}
if ( v7 > 299 )
{
fprintf(stderr, "--missing-window validation is active and set to: %d\n", (unsigned int)v7);
v3 = log(v3);
missing_window = v8;
}
else
{
fwrite(
"--missing-window is set very low: default=300, recommended=6000 - proceeding anyway\n",
1uLL,
0x54uLL,
stderr);
v3 = log(v3);
}
break;
}
}
else
{
v9 = strncpy(dest, optarg, 0x40uLL);
dest[64] = 0;
v29 = v9;
}
}
if ( optind >= argc )
LABEL_27:
func2(*argv, v3);
v12.s_addr = func3(argv[optind]);
v11 = (char *)(unsigned int)++optind;
v3 = log(v3);
v18 = pptp_gre_bind(v12.s_addr, "pptp.c", v3);
if ( v18 < 0 )
{
close(fd);
fatal("main", "pptp.c", 339LL, "Cannot bind GRE socket, aborting.");
LABEL_55:
fprintf(stderr, "Packet timeout %s (%f) out of range: should be between 0.01 and 10 seconds\n", v11, v4);
log(v4);
LABEL_46:
exit(2);
}
if ( !v26 )
{
aslave = 0;
amaster = 0;
close(1);
v25 = getpid();
pid = 0;
goto LABEL_58;
}
if ( openpty(&amaster, &aslave, name, 0LL, 0LL) < 0 )
{
close(fd);
fatal("main", "pptp.c", 347LL, "Could not find free pty.");
LABEL_74:
fwrite("--max-echo-wait must not be negative\n", 1uLL, 0x25uLL, v16);
log(v3);
goto LABEL_46;
}
signal(10, do_nothing);
signal(17, do_nothing);
pid = getpid();
v13 = fork();
v25 = v13;
if ( v13 == -1 )
{
fatal("main", "pptp.c", 356LL, "Could not fork pppd process");
JUMPOUT(0x1E4FLL);
}
if ( !v13 )
{
close(aslave);
signal(10, 0LL);
v25 = getpid();
do
LABEL_58:
fd = func5(v12, (__int64)v29, argc, (__int64 *)argv, (__int64)envp, amaster, v18);
while ( (int)func6(fd, pid, v25, &v19, &v20) < 0 );
if ( v26 )
{
kill(pid, 10);
sleep(2u);
if ( !v27 && daemon(0, 0) )
perror("daemon");
}
else
{
file2fd("/dev/null", "wb", 2LL);
}
v17 = ttyname(aslave);
if ( !v17 )
v17 = "(null)";
snprintf(s, 0x80uLL, "pptp: GRE-to-PPP gateway on %s", v17);
if ( prctl(15, "pptpgw", 0LL, 0LL, 0LL) )
perror("prctl");
if ( !_sigsetjmp(env, 1) )
{
signal(2, sighandler);
signal(15, sighandler);
signal(9, sighandler);
signal(17, sighandler);
signal(10, (__sighandler_t)sigstats);
pptp_gre_copy(v19, v20, (unsigned int)amaster, (unsigned int)v18);
}
if ( v26 )
kill(pid, 15);
close(amaster);
close(fd);
LABEL_72:
exit(0);
}
close(amaster);
if ( !signaled )
LABEL_80:
pause();
if ( signaled == 17 )
{
fatal("main", "pptp.c", 374LL, "Child process died");
goto LABEL_80;
}
func7((__int64)name, argc - (_DWORD)v11, &argv[(int)v11]);
perror("Error");
fatal("main", "pptp.c", 378LL, "Could not launch pppd");
LABEL_43:
log_string = strdup(optarg);
}
}

original_code:
int main(int argc, char **argv, char **envp)
{
    struct in_addr inetaddr;
    volatile int callmgr_sock = -1;
    char ttydev[4096];
    int pty_fd, tty_fd, gre_fd, rc;
    volatile pid_t parent_pid, child_pid;
    u_int16_t call_id, peer_call_id;
    char buf[128];
    int pppdargc;
    char **pppdargv;
    char phonenrbuf[65];
    char * volatile phonenr = ((void *)0);
    volatile int launchpppd = 1, debug = 0;
    while(1){
        static struct option long_options[] = {
            {"phone", 1, 0, 0},
            {"nolaunchpppd", 0, 0, 0},
            {"quirks", 1, 0, 0},
            {"debug", 0, 0, 0},
            {"sync", 0, 0, 0},
            {"timeout", 1, 0, 0},
            {"logstring", 1, 0, 0},
            {"localbind", 1, 0, 0},
            {"loglevel", 1, 0, 0},
     {"nobuffer", 0, 0, 0},
     {"idle-wait", 1, 0, 0},
     {"max-echo-wait", 1, 0, 0},
     {"version", 0, 0, 0},
     {"test-type", 1, 0, 0},
     {"test-rate", 1, 0, 0},
     {"missing-window", 1, 0, 0},
            {0, 0, 0, 0}
        };
        int option_index = 0;
        int c;
        c = getopt_long (argc, argv, "", long_options, &option_index);
        if (c == -1) break;
        switch (c) {
            case 0:
                if (option_index == 0) {
                    strncpy(phonenrbuf,optarg,sizeof(phonenrbuf));
                    phonenrbuf[sizeof(phonenrbuf) - 1] = '\0';
                    phonenr = phonenrbuf;
                } else if (option_index == 1) {
                    launchpppd = 0;
                } else if (option_index == 2) {
                    if (set_quirk_index(find_quirk(optarg)))
                        usage(argv[0]);
                } else if (option_index == 3) {
                    debug = 1;
                } else if (option_index == 4) {
                    syncppp = 1;
                } else if (option_index == 5) {
                    float new_packet_timeout = atof(optarg);
                    if (new_packet_timeout < 0.0099 ||
                            new_packet_timeout > 10) {
                        fprintf(stderr, "Packet timeout %s (%f) out of range: "
                                "should be between 0.01 and 10 seconds\n",
                                optarg, new_packet_timeout);
                        _log(__FUNCTION__,"pptp.c",248, "Packet timeout %s (%f) out of range: should be" "between 0.01 and 10 seconds" , optarg, new_packet_timeout);
                        exit(2);
                    } else {
                        packet_timeout_usecs = new_packet_timeout * 1000000;
                    }
                } else if (option_index == 6) {
                    log_string = strdup(optarg);
                } else if (option_index == 7) {
                    if (inet_pton(2, optarg, (void *) &localbind) < 1) {
                        fprintf(stderr, "Local bind address %s invalid\n",
    optarg);
                        _log(__FUNCTION__,"pptp.c",261, "Local bind address %s invalid\n" , optarg);
                        exit(2);
                    }
                } else if (option_index == 8) {
                    log_level = atoi(optarg);
                    if (log_level < 0 || log_level > 2)
                        usage(argv[0]);
                } else if (option_index == 9) {
      disable_buffer = 1;
                } else if (option_index == 10) {
                    int x = atoi(optarg);
                    if (x < 0) {
                        fprintf(stderr, "--idle-wait must not be negative\n");
                        _log(__FUNCTION__,"pptp.c",274, "--idle-wait must not be negative\n");
                        exit(2);
                    } else {
                        idle_wait = x;
                    }
                } else if (option_index == 11) {
                    int x = atoi(optarg);
                    if (x < 0) {
                        fprintf(stderr, "--max-echo-wait must not be negative\n");
                        _log(__FUNCTION__,"pptp.c",283, "--max-echo-wait must not be negative\n");
                        exit(2);
                    } else {
                        max_echo_wait = x;
                    }
      fprintf(stderr, "--max-echo-wait ignored, not yet implemented\n");
                } else if (option_index == 12) {
      fprintf(stdout, "%s\n", version);
      exit(0);
  } else if (option_index == 13) {
      test_type = atoi(optarg);
  } else if (option_index == 14) {
      test_rate = atoi(optarg);
  } else if (option_index == 15) {
      int x = atoi(optarg);
      if (x <= 0) {
    fprintf(stderr, "--missing-window must be integer greater than zero\n");
    _log(__FUNCTION__,"pptp.c",300, "--missing-window must be integer greater than zero\n");
    exit(2);
      } else if (x < 300) {
    fprintf(stderr, "--missing-window is set very low: default=300, recommended=6000 - proceeding anyway\n");
    _log(__FUNCTION__,"pptp.c",304, "--missing-window is set very low: default=300, recommended=6000 - proceeding anyway\n");
      } else {
    fprintf(stderr, "--missing-window validation is active and set to: %d\n", x);
    _log(__FUNCTION__,"pptp.c",307, "--missing-window validation is active and set to: %d\n" , x);
    missing_window = x;
      }
  }
                break;
            case '?':
            default:
  usage(argv[0]);
        }
        if (c == -1) break;
    }
    if (argc <= optind)
        usage(argv[0]);
    inetaddr = get_ip_address(argv[optind]);
    optind++;
    pppdargc = argc - optind;
    pppdargv = argv + optind;
    _log(__FUNCTION__,"pptp.c",331, "The synchronous pptp option is %sactivated\n" , syncppp ? "" : "NOT ");
    gre_fd = pptp_gre_bind(inetaddr);
    if (gre_fd < 0) {
        close(callmgr_sock);
        _fatal(__FUNCTION__,"pptp.c",339, "Cannot bind GRE socket, aborting.");
    }
    if(launchpppd){
        rc = openpty (&pty_fd, &tty_fd, ttydev, ((void *)0), ((void *)0));
        if (rc < 0) {
            close(callmgr_sock);
            _fatal(__FUNCTION__,"pptp.c",347, "Could not find free pty.");
        }
        signal(10, do_nothing);
        signal(17, do_nothing);
        parent_pid = getpid();
        switch (child_pid = fork()) {
            case -1:
                _fatal(__FUNCTION__,"pptp.c",356, "Could not fork pppd process");
            case 0:
                close (tty_fd);
                signal(10, ((__sighandler_t) 0));
                child_pid = getpid();
                break;
            default:
                close (pty_fd);
                if (!signaled) {
                    pause();
                }
                if (signaled == 17)
                    _fatal(__FUNCTION__,"pptp.c",374, "Child process died");
                launch_pppd(ttydev, pppdargc, pppdargv);
                perror("Error");
                _fatal(__FUNCTION__,"pptp.c",378, "Could not launch pppd");
        }
    } else {
        pty_fd = tty_fd = 0;
        close(1);
        child_pid = getpid();
        parent_pid = 0;
    }
    do {
        callmgr_sock = open_callmgr(inetaddr, phonenr, argc, argv, envp,
  pty_fd, gre_fd);
    } while (get_call_id(callmgr_sock, parent_pid, child_pid,
                &call_id, &peer_call_id) < 0);
    if (launchpppd) {
        kill(parent_pid, 10);
        sleep(2);
        if (!debug && daemon(0, 0) != 0) {
            perror("daemon");
        }
    } else {
        file2fd("/dev/null", "wb", 2);
    }
    char *tty_name = ttyname(tty_fd);
    snprintf(buf, sizeof(buf), "pptp: GRE-to-PPP gateway on %s",
              tty_name ? tty_name : "(null)");
    rc = prctl(15, "pptpgw", 0, 0, 0);
    if (rc != 0) perror("prctl");
    if (__sigsetjmp (env, 1)!= 0) goto shutdown;
    signal(2, sighandler);
    signal(15, sighandler);
    signal(9, sighandler);
    signal(17, sighandler);
    signal(10, sigstats);
    pptp_gre_copy(call_id, peer_call_id, pty_fd, gre_fd);
shutdown:
    if(launchpppd)
        kill(parent_pid, 15);
    close(pty_fd);
    close(callmgr_sock);
    exit(0);
}

canonical_original_code:
int main(int argc, char **argv, char **envp)
{
    struct in_addr inetaddr;
    int callmgr_sock = -1;
    char ttydev[4096];
    int pty_fd; int tty_fd; int gre_fd; int rc;
    int parent_pid; int child_pid;
    unsigned int call_id; unsigned int peer_call_id;
    char buf[128];
    int pppdargc;
    char **pppdargv;
    char phonenrbuf[65];
    char *phonenr = ((void *)0);
    int launchpppd = 1; int debug = 0;
    while(1){
        static struct option long_options[] = {
            {"phone", 1, 0, 0},
            {"nolaunchpppd", 0, 0, 0},
            {"quirks", 1, 0, 0},
            {"debug", 0, 0, 0},
            {"sync", 0, 0, 0},
            {"timeout", 1, 0, 0},
            {"logstring", 1, 0, 0},
            {"localbind", 1, 0, 0},
            {"loglevel", 1, 0, 0},
     {"nobuffer", 0, 0, 0},
     {"idle-wait", 1, 0, 0},
     {"max-echo-wait", 1, 0, 0},
     {"version", 0, 0, 0},
     {"test-type", 1, 0, 0},
     {"test-rate", 1, 0, 0},
     {"missing-window", 1, 0, 0},
            {0, 0, 0, 0}
        };
        int option_index = 0;
        int c;
        c = getopt_long (argc, argv, "", long_options, &option_index);
        if (c == -1) break;
        switch (c) {
            case 0:
                if (option_index == 0) {
                    strncpy(phonenrbuf,optarg,sizeof(phonenrbuf));
                    phonenrbuf[sizeof(phonenrbuf) - 1] = '\0';
                    phonenr = phonenrbuf;
                } else if (option_index == 1) {
                    launchpppd = 0;
                } else if (option_index == 2) {
                    if (set_quirk_index(find_quirk(optarg)))
                        usage(argv[0]);
                } else if (option_index == 3) {
                    debug = 1;
                } else if (option_index == 4) {
                    syncppp = 1;
                } else if (option_index == 5) {
                    float new_packet_timeout = atof(optarg);
                    if (new_packet_timeout < 0.0099 ||
                            new_packet_timeout > 10) {
                        fprintf(stderr, "Packet timeout %s (%f) out of range: "
                                "should be between 0.01 and 10 seconds\n",
                                optarg, new_packet_timeout);
                        _log(__FUNCTION__,"pptp.c",248, "Packet timeout %s (%f) out of range: should be" "between 0.01 and 10 seconds" , optarg, new_packet_timeout);
                        exit(2);
                    } else {
                        packet_timeout_usecs = new_packet_timeout * 1000000;
                    }
                } else if (option_index == 6) {
                    log_string = strdup(optarg);
                } else if (option_index == 7) {
                    if (inet_pton(2, optarg, (void *) &localbind) < 1) {
                        fprintf(stderr, "Local bind address %s invalid\n",
    optarg);
                        _log(__FUNCTION__,"pptp.c",261, "Local bind address %s invalid\n" , optarg);
                        exit(2);
                    }
                } else if (option_index == 8) {
                    log_level = atoi(optarg);
                    if (log_level < 0 || log_level > 2)
                        usage(argv[0]);
                } else if (option_index == 9) {
      disable_buffer = 1;
                } else if (option_index == 10) {
                    int x = atoi(optarg);
                    if (x < 0) {
                        fprintf(stderr, "--idle-wait must not be negative\n");
                        _log(__FUNCTION__,"pptp.c",274, "--idle-wait must not be negative\n");
                        exit(2);
                    } else {
                        idle_wait = x;
                    }
                } else if (option_index == 11) {
                    int x = atoi(optarg);
                    if (x < 0) {
                        fprintf(stderr, "--max-echo-wait must not be negative\n");
                        _log(__FUNCTION__,"pptp.c",283, "--max-echo-wait must not be negative\n");
                        exit(2);
                    } else {
                        max_echo_wait = x;
                    }
      fprintf(stderr, "--max-echo-wait ignored, not yet implemented\n");
                } else if (option_index == 12) {
      fprintf(stdout, "%s\n", version);
      exit(0);
  } else if (option_index == 13) {
      test_type = atoi(optarg);
  } else if (option_index == 14) {
      test_rate = atoi(optarg);
  } else if (option_index == 15) {
      int x = atoi(optarg);
      if (x <= 0) {
    fprintf(stderr, "--missing-window must be integer greater than zero\n");
    _log(__FUNCTION__,"pptp.c",300, "--missing-window must be integer greater than zero\n");
    exit(2);
      } else if (x < 300) {
    fprintf(stderr, "--missing-window is set very low: default=300, recommended=6000 - proceeding anyway\n");
    _log(__FUNCTION__,"pptp.c",304, "--missing-window is set very low: default=300, recommended=6000 - proceeding anyway\n");
      } else {
    fprintf(stderr, "--missing-window validation is active and set to: %d\n", x);
    _log(__FUNCTION__,"pptp.c",307, "--missing-window validation is active and set to: %d\n" , x);
    missing_window = x;
      }
  }
                break;
            case '?':
            default:
  usage(argv[0]);
        }
        if (c == -1) break;
    }
    if (argc <= optind)
        usage(argv[0]);
    inetaddr = get_ip_address(argv[optind]);
    optind++;
    pppdargc = argc - optind;
    pppdargv = argv + optind;
    _log(__FUNCTION__,"pptp.c",331, "The synchronous pptp option is %sactivated\n" , syncppp ? "" : "NOT ");
    gre_fd = pptp_gre_bind(inetaddr);
    if (gre_fd < 0) {
        close(callmgr_sock);
        _fatal(__FUNCTION__,"pptp.c",339, "Cannot bind GRE socket, aborting.");
    }
    if(launchpppd){
        rc = openpty (&pty_fd, &tty_fd, ttydev, ((void *)0), ((void *)0));
        if (rc < 0) {
            close(callmgr_sock);
            _fatal(__FUNCTION__,"pptp.c",347, "Could not find free pty.");
        }
        signal(10, do_nothing);
        signal(17, do_nothing);
        parent_pid = getpid();
        switch (child_pid = fork()) {
            case -1:
                _fatal(__FUNCTION__,"pptp.c",356, "Could not fork pppd process");
            case 0:
                close (tty_fd);
                signal(10, ((void (*)(int)) 0));
                child_pid = getpid();
                break;
            default:
                close (pty_fd);
                if (!signaled) {
                    pause();
                }
                if (signaled == 17)
                    _fatal(__FUNCTION__,"pptp.c",374, "Child process died");
                launch_pppd(ttydev, pppdargc, pppdargv);
                perror("Error");
                _fatal(__FUNCTION__,"pptp.c",378, "Could not launch pppd");
        }
    } else {
        pty_fd = tty_fd = 0;
        close(1);
        child_pid = getpid();
        parent_pid = 0;
    }
    do {
        callmgr_sock = open_callmgr(inetaddr, phonenr, argc, argv, envp,
  pty_fd, gre_fd);
    } while (get_call_id(callmgr_sock, parent_pid, child_pid,
                &call_id, &peer_call_id) < 0);
    if (launchpppd) {
        kill(parent_pid, 10);
        sleep(2);
        if (!debug && daemon(0, 0) != 0) {
            perror("daemon");
        }
    } else {
        file2fd("/dev/null", "wb", 2);
    }
    char *tty_name = ttyname(tty_fd);
    snprintf(buf, sizeof(buf), "pptp: GRE-to-PPP gateway on %s",
              tty_name ? tty_name : "(null)");
    rc = prctl(15, "pptpgw", 0, 0, 0);
    if (rc != 0) perror("prctl");
    if (__sigsetjmp (env, 1)!= 0) goto shutdown;
    signal(2, sighandler);
    signal(15, sighandler);
    signal(9, sighandler);
    signal(17, sighandler);
    signal(10, sigstats);
    pptp_gre_copy(call_id, peer_call_id, pty_fd, gre_fd);
shutdown:
    if(launchpppd)
        kill(parent_pid, 15);
    close(pty_fd);
    close(callmgr_sock);
    exit(0);
}

variable_types:
inetaddr
struct in_addr { unsigned int s_addr; }

variable_types:
ttydev
char [4096]

variable_types: pppdargc int

variable_types:
pppdargv
char **

variable_types:
phonenrbuf
char [65]

variable_types: launchpppd int

variable_types: debug int

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

variable_types: c int

variable_types: x int


decompiled_code:
ssize_t __fastcall write_reordered_swap(int a1, const void *a2, size_t a3, double a4)
{
ssize_t v6; // rbx
ssize_t result; // rax

if ( !test_ordering_phase )
{
if ( ++test_ordering_cycle == test_rate )
{
test_ordering_phase = 1;
return write(a1, a2, a3);
}
return write(a1, a2, a3);
}
if ( test_ordering_phase != 1 )
return write(a1, a2, a3);
if ( test_ordering_cycle++ == test_rate )
{
pocket_count_8 = a3;
pocket_buf_7 = malloc(a3);
memcpy(pocket_buf_7, a2, a3);
log(a4);
result = a3;
}
else
{
test_ordering_cycle = 0;
test_ordering_phase = 0;
v6 = (int)write(a1, a2, a3);
if ( v6 == a3 )
{
write(a1, pocket_buf_7, pocket_count_8);
free(pocket_buf_7);
log(a4);
}
result = v6;
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func0(int a1, const void *a2, size_t a3, double a4)
{
ssize_t v6; // rbx
ssize_t result; // rax

if ( !test_ordering_phase )
{
if ( ++test_ordering_cycle == test_rate )
{
test_ordering_phase = 1;
return write(a1, a2, a3);
}
return write(a1, a2, a3);
}
if ( test_ordering_phase != 1 )
return write(a1, a2, a3);
if ( test_ordering_cycle++ == test_rate )
{
pocket_count_8 = a3;
pocket_buf_7 = malloc(a3);
memcpy(pocket_buf_7, a2, a3);
log(a4);
result = a3;
}
else
{
test_ordering_cycle = 0;
test_ordering_phase = 0;
v6 = (int)write(a1, a2, a3);
if ( v6 == a3 )
{
write(a1, pocket_buf_7, pocket_count_8);
free(pocket_buf_7);
log(a4);
}
result = v6;
}
return result;
}

original_code:
static ssize_t write_reordered_swap(int fd, const void *buf, size_t count)
{
  static void *pocket_buf = ((void *)0);
  static int pocket_count = 0;
  int stat;
  switch (test_ordering_phase) {
  case 0:
    test_ordering_cycle++;
    if (test_ordering_cycle == test_rate) test_ordering_phase++;
    return write(fd, buf, count);
  case 1:
    test_ordering_cycle++;
    if (test_ordering_cycle == (test_rate+1)) {
      pocket_count = count;
      pocket_buf = malloc(count);
      memcpy(pocket_buf, buf, count);
      _log(__FUNCTION__,"test.c",46, "test order swap, packet buffered");
      return count;
    } else {
      test_ordering_cycle = 0;
      test_ordering_phase = 0;
      stat = write(fd, buf, count);
      if (stat != count) return stat;
      stat = write(fd, pocket_buf, pocket_count);
      free(pocket_buf);
      _log(__FUNCTION__,"test.c",59, "test order swap, packets sent");
      return count;
    }
  default:
    return write(fd, buf, count);
  }
}

canonical_original_code:
static long write_reordered_swap(int fd, void *buf, unsigned long count)
{
  static void *pocket_buf = ((void *)0);
  static int pocket_count = 0;
  int stat;
  switch (test_ordering_phase) {
  case 0:
    test_ordering_cycle++;
    if (test_ordering_cycle == test_rate) test_ordering_phase++;
    return write(fd, buf, count);
  case 1:
    test_ordering_cycle++;
    if (test_ordering_cycle == (test_rate+1)) {
      pocket_count = count;
      pocket_buf = malloc(count);
      memcpy(pocket_buf, buf, count);
      _log(__FUNCTION__,"test.c",46, "test order swap, packet buffered");
      return count;
    } else {
      test_ordering_cycle = 0;
      test_ordering_phase = 0;
      stat = write(fd, buf, count);
      if (stat != count) return stat;
      stat = write(fd, pocket_buf, pocket_count);
      free(pocket_buf);
      _log(__FUNCTION__,"test.c",59, "test order swap, packets sent");
      return count;
    }
  default:
    return write(fd, buf, count);
  }
}

variable_types: fd int

variable_types:
buf
void *

variable_types:
count
unsigned long

variable_types: stat int

return_type: long


decompiled_code:
ssize_t __fastcall write_reordered_retransmit(int fd, const void *a2, size_t a3, double a4)
{
ssize_t result; // rax
int v7; // eax
__int64 v8; // rbx
void *v9; // rax
__int64 i; // rbx
void *v11; // rdi

if ( test_ordering_phase == 1 )
{
v7 = test_ordering_cycle + 1;
test_ordering_cycle = v7;
if ( v7 == test_rate + 10 )
test_ordering_phase = 2;
v8 = v7 - test_rate - 1;
pocket_count_5[v8] = a3;
v9 = malloc(a3);
pocket_buf_4[v8] = (__int64)v9;
memcpy(v9, a2, a3);
goto LABEL_8;
}
if ( test_ordering_phase == 2 )
{
test_ordering_cycle = 0;
test_ordering_phase = 0;
result = (int)write(fd, a2, a3);
if ( (int)result != a3 )
return result;
for ( i = 0LL; i != 10; ++i )
{
write(fd, (const void *)pocket_buf_4[i], pocket_count_5[i]);
v11 = (void *)pocket_buf_4[i];
free(v11);
}
LABEL_8:
log(a4);
return a3;
}
if ( !test_ordering_phase && ++test_ordering_cycle == test_rate )
test_ordering_phase = 1;
return write(fd, a2, a3);
}

canonical_decompiled_code:
ssize_t __fastcall func1(int fd, const void *a2, size_t a3, double a4)
{
ssize_t result; // rax
int v7; // eax
__int64 v8; // rbx
void *v9; // rax
__int64 i; // rbx
void *v11; // rdi

if ( test_ordering_phase == 1 )
{
v7 = test_ordering_cycle + 1;
test_ordering_cycle = v7;
if ( v7 == test_rate + 10 )
test_ordering_phase = 2;
v8 = v7 - test_rate - 1;
pocket_count_5[v8] = a3;
v9 = malloc(a3);
pocket_buf_4[v8] = (__int64)v9;
memcpy(v9, a2, a3);
goto LABEL_8;
}
if ( test_ordering_phase == 2 )
{
test_ordering_cycle = 0;
test_ordering_phase = 0;
result = (int)write(fd, a2, a3);
if ( (int)result != a3 )
return result;
for ( i = 0LL; i != 10; ++i )
{
write(fd, (const void *)pocket_buf_4[i], pocket_count_5[i]);
v11 = (void *)pocket_buf_4[i];
free(v11);
}
LABEL_8:
log(a4);
return a3;
}
if ( !test_ordering_phase && ++test_ordering_cycle == test_rate )
test_ordering_phase = 1;
return write(fd, a2, a3);
}

original_code:
static ssize_t write_reordered_retransmit(int fd, const void *buf, size_t count)
{
  int test_length = 10;
  static void *pocket_buf[10];
  static int pocket_count[10];
  int stat, n;
  switch (test_ordering_phase) {
  case 0:
    test_ordering_cycle++;
    if (test_ordering_cycle == test_rate) test_ordering_phase++;
    return write(fd, buf, count);
  case 1:
    test_ordering_cycle++;
    if (test_ordering_cycle == (test_rate+test_length)) {
      test_ordering_phase = 2;
    }
    n = test_ordering_cycle - test_rate - 1;
    pocket_count[n] = count;
    pocket_buf[n] = malloc(count);
    memcpy(pocket_buf[n], buf, count);
    _log(__FUNCTION__,"test.c",90, "test order retransmit, packet buffered");
    return count;
  case 2:
    test_ordering_cycle = 0;
    test_ordering_phase = 0;
    stat = write(fd, buf, count);
    if (stat != count) return stat;
    for (n=0; n<test_length; n++) {
      stat = write(fd, pocket_buf[n], pocket_count[n]);
      free(pocket_buf[n]);
    }
    _log(__FUNCTION__,"test.c",106, "test order retransmit, packets sent");
    return count;
  default:
    return write(fd, buf, count);
  }
}

canonical_original_code:
static long write_reordered_retransmit(int fd, void *buf, unsigned long count)
{
  int test_length = 10;
  static void *pocket_buf[10];
  static int pocket_count[10];
  int stat; int n;
  switch (test_ordering_phase) {
  case 0:
    test_ordering_cycle++;
    if (test_ordering_cycle == test_rate) test_ordering_phase++;
    return write(fd, buf, count);
  case 1:
    test_ordering_cycle++;
    if (test_ordering_cycle == (test_rate+test_length)) {
      test_ordering_phase = 2;
    }
    n = test_ordering_cycle - test_rate - 1;
    pocket_count[n] = count;
    pocket_buf[n] = malloc(count);
    memcpy(pocket_buf[n], buf, count);
    _log(__FUNCTION__,"test.c",90, "test order retransmit, packet buffered");
    return count;
  case 2:
    test_ordering_cycle = 0;
    test_ordering_phase = 0;
    stat = write(fd, buf, count);
    if (stat != count) return stat;
    for (n=0; n<test_length; n++) {
      stat = write(fd, pocket_buf[n], pocket_count[n]);
      free(pocket_buf[n]);
    }
    _log(__FUNCTION__,"test.c",106, "test order retransmit, packets sent");
    return count;
  default:
    return write(fd, buf, count);
  }
}

variable_types: n int


decompiled_code:
ssize_t __fastcall write_reordered_reverse(int fd, const void *a2, size_t a3, double a4)
{
ssize_t result; // rax
int v6; // eax
__int64 v7; // rbx
void *v8; // rax
__int64 v9; // rbx

if ( test_ordering_phase == 1 )
{
v6 = test_ordering_cycle + 1;
test_ordering_cycle = v6;
if ( v6 == test_rate + 10 )
test_ordering_phase = 2;
v7 = v6 - test_rate - 1;
pocket_count_2[v7] = a3;
v8 = malloc(a3);
pocket_buf_1[v7] = (__int64)v8;
memcpy(v8, a2, a3);
goto LABEL_8;
}
if ( test_ordering_phase == 2 )
{
test_ordering_cycle = 0;
test_ordering_phase = 0;
result = (int)write(fd, a2, a3);
if ( (int)result != a3 )
return result;
v9 = 9LL;
do
{
write(fd, (const void *)pocket_buf_1[v9], pocket_count_2[v9]);
free((void *)pocket_buf_1[v9--]);
}
while ( v9 );
LABEL_8:
log(a4);
return a3;
}
if ( !test_ordering_phase && ++test_ordering_cycle == test_rate )
test_ordering_phase = 1;
return write(fd, a2, a3);
}

canonical_decompiled_code:
ssize_t __fastcall func2(int fd, const void *a2, size_t a3, double a4)
{
ssize_t result; // rax
int v6; // eax
__int64 v7; // rbx
void *v8; // rax
__int64 v9; // rbx

if ( test_ordering_phase == 1 )
{
v6 = test_ordering_cycle + 1;
test_ordering_cycle = v6;
if ( v6 == test_rate + 10 )
test_ordering_phase = 2;
v7 = v6 - test_rate - 1;
pocket_count_2[v7] = a3;
v8 = malloc(a3);
pocket_buf_1[v7] = (__int64)v8;
memcpy(v8, a2, a3);
goto LABEL_8;
}
if ( test_ordering_phase == 2 )
{
test_ordering_cycle = 0;
test_ordering_phase = 0;
result = (int)write(fd, a2, a3);
if ( (int)result != a3 )
return result;
v9 = 9LL;
do
{
write(fd, (const void *)pocket_buf_1[v9], pocket_count_2[v9]);
free((void *)pocket_buf_1[v9--]);
}
while ( v9 );
LABEL_8:
log(a4);
return a3;
}
if ( !test_ordering_phase && ++test_ordering_cycle == test_rate )
test_ordering_phase = 1;
return write(fd, a2, a3);
}

original_code:
static ssize_t write_reordered_reverse(int fd, const void *buf, size_t count)
{
  int test_length = 10;
  static void *pocket_buf[10];
  static int pocket_count[10];
  int stat, n;
  switch (test_ordering_phase) {
  case 0:
    test_ordering_cycle++;
    if (test_ordering_cycle == test_rate) test_ordering_phase++;
    return write(fd, buf, count);
  case 1:
    test_ordering_cycle++;
    if (test_ordering_cycle == (test_rate+test_length)) {
      test_ordering_phase = 2;
    }
    n = test_ordering_cycle - test_rate - 1;
    pocket_count[n] = count;
    pocket_buf[n] = malloc(count);
    memcpy(pocket_buf[n], buf, count);
    _log(__FUNCTION__,"test.c",136, "test order reverse, packet buffered");
    return count;
  case 2:
    test_ordering_cycle = 0;
    test_ordering_phase = 0;
    stat = write(fd, buf, count);
    if (stat != count) return stat;
    for (n=test_length-1; n>0; n--) {
      stat = write(fd, pocket_buf[n], pocket_count[n]);
      free(pocket_buf[n]);
    }
    _log(__FUNCTION__,"test.c",152, "test order reverse, packets sent");
    return count;
  default:
    return write(fd, buf, count);
  }
}

canonical_original_code:
static long write_reordered_reverse(int fd, void *buf, unsigned long count)
{
  int test_length = 10;
  static void *pocket_buf[10];
  static int pocket_count[10];
  int stat; int n;
  switch (test_ordering_phase) {
  case 0:
    test_ordering_cycle++;
    if (test_ordering_cycle == test_rate) test_ordering_phase++;
    return write(fd, buf, count);
  case 1:
    test_ordering_cycle++;
    if (test_ordering_cycle == (test_rate+test_length)) {
      test_ordering_phase = 2;
    }
    n = test_ordering_cycle - test_rate - 1;
    pocket_count[n] = count;
    pocket_buf[n] = malloc(count);
    memcpy(pocket_buf[n], buf, count);
    _log(__FUNCTION__,"test.c",136, "test order reverse, packet buffered");
    return count;
  case 2:
    test_ordering_cycle = 0;
    test_ordering_phase = 0;
    stat = write(fd, buf, count);
    if (stat != count) return stat;
    for (n=test_length-1; n>0; n--) {
      stat = write(fd, pocket_buf[n], pocket_count[n]);
      free(pocket_buf[n]);
    }
    _log(__FUNCTION__,"test.c",152, "test order reverse, packets sent");
    return count;
  default:
    return write(fd, buf, count);
  }
}


decompiled_code:
ssize_t __fastcall write_reordered(int a1, const void *a2, size_t a3, double a4)
{
switch ( test_type )
{
case 2:
return write_reordered_retransmit(a1, a2, a3, a4);
case 3:
return write_reordered_reverse(a1, a2, a3, a4);
case 1:
return write_reordered_swap(a1, a2, a3, a4);
}
return write(a1, a2, a3);
}

canonical_decompiled_code:
ssize_t __fastcall func3(int a1, const void *a2, size_t a3, double a4)
{
switch ( test_type )
{
case 2:
return func1(a1, a2, a3, a4);
case 3:
return func2(a1, a2, a3, a4);
case 1:
return func0(a1, a2, a3, a4);
}
return write(a1, a2, a3);
}

original_code:
static ssize_t write_reordered(int fd, const void *buf, size_t count)
{
  switch (test_type) {
  case 1:
    return write_reordered_swap(fd, buf, count);
  case 2:
    return write_reordered_retransmit(fd, buf, count);
  case 3:
    return write_reordered_reverse(fd, buf, count);
  default:
    return write(fd, buf, count);
  }
}

canonical_original_code:
static long write_reordered(int fd, void *buf, unsigned long count)
{
  switch (test_type) {
  case 1:
    return write_reordered_swap(fd, buf, count);
  case 2:
    return write_reordered_retransmit(fd, buf, count);
  case 3:
    return write_reordered_reverse(fd, buf, count);
  default:
    return write(fd, buf, count);
  }
}


decompiled_code:
void __fastcall sigpipe_handler(int a1)
{
int buf[3]; // [rsp+Ch] [rbp-Ch] BYREF

buf[0] = a1;
write(fd, buf, 4uLL);
signal(buf[0], sigpipe_handler);
}

canonical_decompiled_code:
void __fastcall func0(int a1)
{
int buf[3]; // [rsp+Ch] [rbp-Ch] BYREF

buf[0] = a1;
write(fd, buf, 4uLL);
signal(buf[0], sigpipe_handler);
}

original_code:
void sigpipe_handler(int signum)
{
  write(sigpipe[1], &signum, sizeof(signum));
  signal(signum, sigpipe_handler);
}

variable_types: signum int


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

result = pipe(sigpipe);
if ( result >= 0 )
{
fcntl(sigpipe[0], 2, 1LL);
fcntl(fd, 2, 1LL);
result = fcntl(fd, 3);
if ( result != -1 )
{
BYTE1(result) |= 8u;
result = fcntl(fd, 4, (unsigned int)result);
if ( result > 0 )
result = 0;
}
}
return result;
}

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

result = pipe(sigpipe);
if ( result >= 0 )
{
fcntl(sigpipe[0], 2, 1LL);
fcntl(fd, 2, 1LL);
result = fcntl(fd, 3);
if ( result != -1 )
{
BYTE1(result) |= 8u;
result = fcntl(fd, 4, (unsigned int)result);
if ( result > 0 )
result = 0;
}
}
return result;
}

original_code:
int sigpipe_create()
{
  int rc;
  rc = pipe(sigpipe);
  if (rc < 0) return rc;
  fcntl(sigpipe[0], 2, 1);
  fcntl(sigpipe[1], 2, 1);
  rc = fcntl(sigpipe[1], 3);
  if (rc != -1)
    rc = fcntl(sigpipe[1], 4, rc | 04000);
  if (rc < 0) return rc;
  return 0;
}

variable_types: rc int


decompiled_code:
int __fastcall sigpipe_assign(int sig)
{
struct sigaction v2; // [rsp+0h] [rbp-A8h] BYREF

v2.sa_handler = sigpipe_handler;
memset(&v2.sa_mask, 0, 0x90uLL);
return sigaction(sig, &v2, 0LL);
}

canonical_decompiled_code:
int __fastcall func6(int sig)
{
struct sigaction v2; // [rsp+0h] [rbp-A8h] BYREF

v2.sa_handler = sigpipe_handler;
memset(&v2.sa_mask, 0, 0x90uLL);
return sigaction(sig, &v2, 0LL);
}

original_code:
void sigpipe_assign(int signum)
{
  struct sigaction sa;
  memset(&sa, 0, sizeof(sa));
  sa.__sigaction_handler.sa_handler = sigpipe_handler;
  sigaction(signum, &sa, ((void *)0));
}

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

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

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


decompiled_code:
__int64 sigpipe_fd()
{
return (unsigned int)sigpipe[0];
}

canonical_decompiled_code:
__int64 func7()
{
return (unsigned int)sigpipe[0];
}

original_code:
int sigpipe_fd()
{
  return sigpipe[0];
}

canonical_original_code:
int sigpipe_fd()
{
  return sigpipe[0];
}


decompiled_code:
__int64 sigpipe_read()
{
unsigned int buf[3]; // [rsp+Ch] [rbp-Ch] BYREF

read(sigpipe[0], buf, 4uLL);
return buf[0];
}

canonical_decompiled_code:
__int64 func8()
{
unsigned int buf[3]; // [rsp+Ch] [rbp-Ch] BYREF

read(sigpipe[0], buf, 4uLL);
return buf[0];
}

original_code:
int sigpipe_read()
{
  int signum;
  read(sigpipe[0], &signum, sizeof(signum));
  return signum;
}


decompiled_code:
int sigpipe_close()
{
close(sigpipe[0]);
return close(fd);
}

canonical_decompiled_code:
int func9()
{
close(sigpipe[0]);
return close(fd);
}

original_code:
void sigpipe_close()
{
  close(sigpipe[0]);
  close(sigpipe[1]);
}


decompiled_code:
void open_log()
{
openlog("pptp", 1, 24);
}

canonical_decompiled_code:
void func10()
{
openlog("pptp", 1, 24);
}

original_code:
static void open_log(void) {
    openlog("pptp", 0x01, (3<<3));
}

canonical_original_code:
static void open_log() {
    openlog("pptp", 0x01, (3<<3));
}


decompiled_code:
// attributes: thunk
void close_log(void)
{
closelog();
}

canonical_decompiled_code:
void func11(void)
{
closelog();
}

original_code:
static void close_log(void)
{
    closelog();
}

canonical_original_code:
static void close_log()
{
    closelog();
}


decompiled_code:
__int64 __fastcall set_quirk_index(int a1)
{
if ( a1 )
return 0xFFFFFFFFLL;
quirk_index = 0;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
if ( a1 )
return 0xFFFFFFFFLL;
quirk_index = 0;
return 0LL;
}

original_code:
int set_quirk_index(int index)
{
    if (index >= 0 && index < fixups_sz) {
 quirk_index = index;
 return 0;
    }
    return -1;
}

variable_types: index int


decompiled_code:
__int64 get_quirk_index()
{
return (unsigned int)quirk_index;
}

canonical_decompiled_code:
__int64 func1()
{
return (unsigned int)quirk_index;
}

original_code:
int get_quirk_index()
{
    return quirk_index;
}


decompiled_code:
__int64 __fastcall find_quirk(char *s2)
{
__int64 result; // rax

if ( s2 && pptp_fixups )
result = (unsigned int)-(strcmp(pptp_fixups, s2) != 0);
else
result = 0xFFFFFFFFLL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(char *s2)
{
__int64 result; // rax

if ( s2 && pptp_fixups )
result = (unsigned int)-(strcmp(pptp_fixups, s2) != 0);
else
result = 0xFFFFFFFFLL;
return result;
}

original_code:
int find_quirk(const char* isp_name)
{
    int i = 0;
    if (isp_name) {
 while (i < fixups_sz && pptp_fixups[i].isp) {
     if (!strcmp(pptp_fixups[i].isp, isp_name)) {
  return i;
     }
     ++i;
 }
    }
    return -1;
}

canonical_original_code:
int find_quirk(char *isp_name)
{
    int i = 0;
    if (isp_name) {
 while (i < fixups_sz && pptp_fixups[i].isp) {
     if (!strcmp(pptp_fixups[i].isp, isp_name)) {
  return i;
     }
     ++i;
 }
    }
    return -1;
}

variable_types: i int


original_code:
int
orckit_atur3_build_hook(struct pptp_out_call_rqst* packet)
{
    unsigned int name_length = 10;
    struct pptp_out_call_rqst fixed_packet = {
 { htons(( (7==1)?sizeof(struct pptp_start_ctrl_conn): (7==2)?sizeof(struct pptp_start_ctrl_conn): (7==3 )?sizeof(struct pptp_stop_ctrl_conn): (7==4 )?sizeof(struct pptp_stop_ctrl_conn): (7==5 )?sizeof(struct pptp_echo_rqst): (7==6 )?sizeof(struct pptp_echo_rply): (7==7 )?sizeof(struct pptp_out_call_rqst): (7==8 )?sizeof(struct pptp_out_call_rply): (7==9 )?sizeof(struct pptp_in_call_rqst): (7==10 )?sizeof(struct pptp_in_call_rply): (7==11 )?sizeof(struct pptp_in_call_connect): (7==12 )?sizeof(struct pptp_call_clear_rqst): (7==13 )?sizeof(struct pptp_call_clear_ntfy): (7==14 )?sizeof(struct pptp_wan_err_ntfy): (7==15 )?sizeof(struct pptp_set_link_info): 0)), htons(1), htonl(0x1A2B3C4D), htons(7), 0 },
 0,
 0,
 htonl(2400), htonl(10000000),
 htonl(2), htonl(3),
 htons(3), 0, htons(name_length), 0,
 {'R','E','L','A','Y','_','P','P','P','1',0}, {0}
    };
    if (!packet)
 return -1;
    memcpy(packet, &fixed_packet, sizeof(*packet));
    return 0;
}

canonical_original_code:
int orckit_atur3_build_hook(struct pptp_out_call_rqst *packet)
{
    unsigned int name_length = 10;
    struct pptp_out_call_rqst fixed_packet = {
 { htons(( (7==1)?sizeof(struct pptp_start_ctrl_conn): (7==2)?sizeof(struct pptp_start_ctrl_conn): (7==3 )?sizeof(struct pptp_stop_ctrl_conn): (7==4 )?sizeof(struct pptp_stop_ctrl_conn): (7==5 )?sizeof(struct pptp_echo_rqst): (7==6 )?sizeof(struct pptp_echo_rply): (7==7 )?sizeof(struct pptp_out_call_rqst): (7==8 )?sizeof(struct pptp_out_call_rply): (7==9 )?sizeof(struct pptp_in_call_rqst): (7==10 )?sizeof(struct pptp_in_call_rply): (7==11 )?sizeof(struct pptp_in_call_connect): (7==12 )?sizeof(struct pptp_call_clear_rqst): (7==13 )?sizeof(struct pptp_call_clear_ntfy): (7==14 )?sizeof(struct pptp_wan_err_ntfy): (7==15 )?sizeof(struct pptp_set_link_info): 0)), htons(1), htonl(0x1A2B3C4D), htons(7), 0 },
 0,
 0,
 htonl(2400), htonl(10000000),
 htonl(2), htonl(3),
 htons(3), 0, htons(name_length), 0,
 {'R','E','L','A','Y','_','P','P','P','1',0}, {0}
    };
    if (!packet)
 return -1;
    memcpy(packet, &fixed_packet, sizeof(*packet));
    return 0;
}

variable_types: struct pptp_out_call_rqst { struct pptp_header { unsigned int length; unsigned int pptp_type; unsigned int magic; unsigned int ctrl_type; unsigned int reserved0; } header; unsigned int call_id; unsigned int call_sernum; unsigned int bps_min; unsigned int bps_max; unsigned int bearer; unsigned int framing; unsigned int recv_size; unsigned int delay; unsigned int phone_len; unsigned int reserved1; unsigned int phone_num[64]; unsigned int subaddress[64]; } *

user_defined_types: struct pptp_out_call_rqst { struct pptp_header header; unsigned int call_id; unsigned int call_sernum; unsigned int bps_min; unsigned int bps_max; unsigned int bearer; unsigned int framing; unsigned int recv_size; unsigned int delay; unsigned int phone_len; unsigned int reserved1; unsigned int phone_num[64]; unsigned int subaddress[64]; }


original_code:
int
orckit_atur3_set_link_hook(struct pptp_set_link_info* packet,
      int peer_call_id)
{
    struct pptp_set_link_info fixed_packet = {
 { htons(( (15==1)?sizeof(struct pptp_start_ctrl_conn): (15==2)?sizeof(struct pptp_start_ctrl_conn): (15==3 )?sizeof(struct pptp_stop_ctrl_conn): (15==4 )?sizeof(struct pptp_stop_ctrl_conn): (15==5 )?sizeof(struct pptp_echo_rqst): (15==6 )?sizeof(struct pptp_echo_rply): (15==7 )?sizeof(struct pptp_out_call_rqst): (15==8 )?sizeof(struct pptp_out_call_rply): (15==9 )?sizeof(struct pptp_in_call_rqst): (15==10 )?sizeof(struct pptp_in_call_rply): (15==11 )?sizeof(struct pptp_in_call_connect): (15==12 )?sizeof(struct pptp_call_clear_rqst): (15==13 )?sizeof(struct pptp_call_clear_ntfy): (15==14 )?sizeof(struct pptp_wan_err_ntfy): (15==15 )?sizeof(struct pptp_set_link_info): 0)), htons(1), htonl(0x1A2B3C4D), htons(15), 0 },
 htons(peer_call_id),
 0,
 0xffffffff,
 0xffffffff};
    if (!packet)
 return -1;
    memcpy(packet, &fixed_packet, sizeof(*packet));
    return 0;
}

canonical_original_code:
int orckit_atur3_set_link_hook(struct pptp_set_link_info *packet, int peer_call_id)
{
    struct pptp_set_link_info fixed_packet = {
 { htons(( (15==1)?sizeof(struct pptp_start_ctrl_conn): (15==2)?sizeof(struct pptp_start_ctrl_conn): (15==3 )?sizeof(struct pptp_stop_ctrl_conn): (15==4 )?sizeof(struct pptp_stop_ctrl_conn): (15==5 )?sizeof(struct pptp_echo_rqst): (15==6 )?sizeof(struct pptp_echo_rply): (15==7 )?sizeof(struct pptp_out_call_rqst): (15==8 )?sizeof(struct pptp_out_call_rply): (15==9 )?sizeof(struct pptp_in_call_rqst): (15==10 )?sizeof(struct pptp_in_call_rply): (15==11 )?sizeof(struct pptp_in_call_connect): (15==12 )?sizeof(struct pptp_call_clear_rqst): (15==13 )?sizeof(struct pptp_call_clear_ntfy): (15==14 )?sizeof(struct pptp_wan_err_ntfy): (15==15 )?sizeof(struct pptp_set_link_info): 0)), htons(1), htonl(0x1A2B3C4D), htons(15), 0 },
 htons(peer_call_id),
 0,
 0xffffffff,
 0xffffffff};
    if (!packet)
 return -1;
    memcpy(packet, &fixed_packet, sizeof(*packet));
    return 0;
}

variable_types: struct pptp_set_link_info { struct pptp_header { unsigned int length; unsigned int pptp_type; unsigned int magic; unsigned int ctrl_type; unsigned int reserved0; } header; unsigned int call_id_peer; unsigned int reserved1; unsigned int send_accm; unsigned int recv_accm; } *

user_defined_types: struct pptp_set_link_info { struct pptp_header header; unsigned int call_id_peer; unsigned int reserved1; unsigned int send_accm; unsigned int recv_accm; }


original_code:
int
orckit_atur3_start_ctrl_conn_hook(struct pptp_start_ctrl_conn* packet)
{
    struct pptp_start_ctrl_conn fixed_packet = {
 {0},
 htons(0x100), 0, 0,
 htonl(1), htonl(1),
 htons(0) ,
 htons(0x6021),
 {'R','E','L','A','Y','_','P','P','P','1',0},
 {'M','S',' ','W','i','n',' ','N','T',0}
    };
    if (!packet)
 return -1;
    memcpy(&fixed_packet.header, &packet->header, sizeof(struct pptp_header));
    memcpy(packet, &fixed_packet, sizeof(*packet));
    return 0;
}

canonical_original_code:
int orckit_atur3_start_ctrl_conn_hook(struct pptp_start_ctrl_conn *packet)
{
    struct pptp_start_ctrl_conn fixed_packet = {
 {0},
 htons(0x100), 0, 0,
 htonl(1), htonl(1),
 htons(0) ,
 htons(0x6021),
 {'R','E','L','A','Y','_','P','P','P','1',0},
 {'M','S',' ','W','i','n',' ','N','T',0}
    };
    if (!packet)
 return -1;
    memcpy(&fixed_packet.header, &packet->header, sizeof(struct pptp_header));
    memcpy(packet, &fixed_packet, sizeof(*packet));
    return 0;
}

variable_types: struct pptp_start_ctrl_conn { struct pptp_header { unsigned int length; unsigned int pptp_type; unsigned int magic; unsigned int ctrl_type; unsigned int reserved0; } header; unsigned int version; unsigned int result_code; unsigned int error_code; unsigned int framing_cap; unsigned int bearer_cap; unsigned int max_channels; unsigned int firmware_rev; unsigned int hostname[64]; unsigned int vendor[64]; } *

user_defined_types: struct pptp_start_ctrl_conn { struct pptp_header header; unsigned int version; unsigned int result_code; unsigned int error_code; unsigned int framing_cap; unsigned int bearer_cap; unsigned int max_channels; unsigned int firmware_rev; unsigned int hostname[64]; unsigned int vendor[64]; }


inststr

original_code:
void
inststr(int argc, char **argv, char **environ, char *src)
{
    if (strlen(src) <= strlen(argv[0]))
    {
        char *ptr;
        for (ptr = argv[0]; *ptr; *(ptr++) = '\0');
        strcpy(argv[0], src);
    } else
    {
        char *ptr, *ptr2;
        int count;
        UL mask = ~(UL)(sizeof(void *) == 4 ? 3 : sizeof(void *) == 8 ? 7 : sizeof(void *) == 16 ? 15 : 0);
        int aligned = (mask < ~(UL)0) && (((UL)(argv[0]) & mask) == (UL)(argv[0]));
        ptr = argv[0] + strlen(argv[0]);
        if (argv[argc - 1] >= argv[1]) {
            for (count = 1; count < argc; count++) {
                if (argv[count] == ptr + 1
                    ||
                    (aligned && argv[count] > ptr && argv[count] <= (char *)((UL)(ptr + sizeof(void *)) & mask))
                   ) {
                    ptr = argv[count] + strlen(argv[count]);
                }
            }
        } else
        {
            for (count = argc - 1; count > 0; count--) {
                if (argv[count] == ptr + 1
                    ||
                    (aligned && argv[count] > ptr && argv[count] <= (char *)((UL)(ptr + sizeof(void *)) & mask))
                   ) {
                    ptr = argv[count] + strlen(argv[count]);
                }
            }
        }
        for (count = 0; environ[count]; count++) {
            if (environ[count] == ptr + 1
                ||
                (aligned && environ[count] > ptr && environ[count] <= (char *)((UL)(ptr + sizeof(void *)) & mask))
               ) {
                   ptr = environ[count] + strlen(environ[count]);
            }
        }
        count = 0;
        for (ptr2 = argv[0]; ptr2 <= ptr; ptr2++) {
            *ptr2 = '\0';
            count++;
        }
        strncpy(argv[0], src, count);
    }
}

canonical_original_code:
void inststr(int argc, char **argv, char **environ, char *src)
{
    if (strlen(src) <= strlen(argv[0]))
    {
        char *ptr;
        for (ptr = argv[0]; *ptr; *(ptr++) = '\0');
        strcpy(argv[0], src);
    } else
    {
        char *ptr; char *ptr2;
        int count;
        unsigned long mask = ~(UL)(sizeof(void *) == 4 ? 3 : sizeof(void *) == 8 ? 7 : sizeof(void *) == 16 ? 15 : 0);
        int aligned = (mask < ~(UL)0) && (((UL)(argv[0]) & mask) == (UL)(argv[0]));
        ptr = argv[0] + strlen(argv[0]);
        if (argv[argc - 1] >= argv[1]) {
            for (count = 1; count < argc; count++) {
                if (argv[count] == ptr + 1
                    ||
                    (aligned && argv[count] > ptr && argv[count] <= (char *)((UL)(ptr + sizeof(void *)) & mask))
                   ) {
                    ptr = argv[count] + strlen(argv[count]);
                }
            }
        } else
        {
            for (count = argc - 1; count > 0; count--) {
                if (argv[count] == ptr + 1
                    ||
                    (aligned && argv[count] > ptr && argv[count] <= (char *)((UL)(ptr + sizeof(void *)) & mask))
                   ) {
                    ptr = argv[count] + strlen(argv[count]);
                }
            }
        }
        for (count = 0; environ[count]; count++) {
            if (environ[count] == ptr + 1
                ||
                (aligned && environ[count] > ptr && environ[count] <= (char *)((UL)(ptr + sizeof(void *)) & mask))
               ) {
                   ptr = environ[count] + strlen(environ[count]);
            }
        }
        count = 0;
        for (ptr2 = argv[0]; ptr2 <= ptr; ptr2++) {
            *ptr2 = '\0';
            count++;
        }
        strncpy(argv[0], src, count);
    }
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
environ
char **

variable_types:
src
char *

variable_types:
ptr
char *

variable_types: count int

variable_types:
mask
unsigned long

variable_types: aligned int


decompiled_code:
__int64 __fastcall pppfcs16(unsigned int a1, __int64 a2, unsigned int a3)
{
__int64 result; // rax
__int64 v4; // rcx
unsigned int v5; // edx

result = a1;
if ( a3 )
{
v4 = a2 + a3;
do
{
++a2;
HIWORD(v5) = WORD1(result);
LOWORD(v5) = fcstab[(unsigned __int8)(*(_BYTE *)(a2 - 1) ^ result)] ^ BYTE1(result);
result = v5;
}
while ( a2 != v4 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1, __int64 a2, unsigned int a3)
{
__int64 result; // rax
__int64 v4; // rcx
unsigned int v5; // edx

result = a1;
if ( a3 )
{
v4 = a2 + a3;
do
{
++a2;
HIWORD(v5) = WORD1(result);
LOWORD(v5) = fcstab[(unsigned __int8)(*(_BYTE *)(a2 - 1) ^ result)] ^ BYTE1(result);
result = v5;
}
while ( a2 != v4 );
}
return result;
}

original_code:
u16 pppfcs16(u16 fcs, void *_cp, int len)
{
    register unsigned char *cp = (unsigned char *)_cp;
    ((void) sizeof ((sizeof (u16) == 2) ? 1 : 0), __extension__ ({ if (sizeof (u16) == 2) ; else __assert_fail ("sizeof (u16) == 2", "ppp_fcs.c", 87, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((((u16) -1) > 0) ? 1 : 0), __extension__ ({ if (((u16) -1) > 0) ; else __assert_fail ("((u16) -1) > 0", "ppp_fcs.c", 88, __extension__ __PRETTY_FUNCTION__); }));
    while (len--)
        fcs = (fcs >> 8) ^ fcstab[(fcs ^ *cp++) & 0xff];
    return (fcs);
}

canonical_original_code:
unsigned int pppfcs16(unsigned int fcs, void *_cp, int len)
{
    register unsigned char *cp = (unsigned char *)_cp;
    ((void) sizeof ((sizeof (u16) == 2) ? 1 : 0), __extension__ ({ if (sizeof (u16) == 2) ; else __assert_fail ("sizeof (u16) == 2", "ppp_fcs.c", 87, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((((u16) -1) > 0) ? 1 : 0), __extension__ ({ if (((u16) -1) > 0) ; else __assert_fail ("((u16) -1) > 0", "ppp_fcs.c", 88, __extension__ __PRETTY_FUNCTION__); }));
    while (len--)
        fcs = (fcs >> 8) ^ fcstab[(fcs ^ *cp++) & 0xff];
    return (fcs);
}

variable_types:
fcs
unsigned int

variable_types: len int

variable_types:
cp
unsigned char *

return_type: unsigned int


decompiled_code:
__int64 __fastcall pqueue_alloc(int a1, const void *a2, int a3, char **a4)
{
__int64 v7; // r14
__int64 v8; // rax
int v9; // edx
char *v10; // rbx
__int64 v11; // rax
void *v12; // rax
void *v13; // rdi
__int64 v14; // rcx
__int64 result; // rax
__int64 *v16; // rcx
int v17; // er14
void *v18; // rax
char *v19; // rax
int *v20; // rax
char *v21; // rax
const char *v22; // rcx
__int64 v23; // rdx
char *v24; // r8
int *v25; // rax
char *v26; // rax
int *v27; // rax
char *v28; // rax

v7 = pq_freelist_head;
if ( pq_freelist_head )
{
v8 = pq_freelist_head;
while ( 1 )
{
v9 = *(_DWORD *)(v8 + 52);
v10 = (char *)v8;
v8 = *(_QWORD *)v8;
if ( v9 >= a3 )
break;
if ( !v8 )
{
v11 = *(_QWORD *)pq_freelist_head;
pq_freelist_head = v11;
if ( v11 )
*(_QWORD *)(v11 + 8) = 0LL;
v12 = realloc(*(void **)(v7 + 40), a3);
*(_QWORD *)(v7 + 40) = v12;
if ( v12 )
{
*(_DWORD *)(v7 + 52) = a3;
v9 = a3;
v10 = (char *)v7;
LABEL_9:
if ( !v9 )
goto LABEL_20;
goto LABEL_10;
}
v20 = _errno_location();
v21 = strerror(*v20);
v22 = "error reallocating packet: %s";
v23 = 76LL;
v24 = v21;
goto LABEL_28;
}
}
if ( (char *)pq_freelist_head == v10 )
{
pq_freelist_head = v8;
v7 = v8;
}
v16 = (__int64 *)*((_QWORD *)v10 + 1);
if ( v16 )
{
*v16 = v8;
v8 = *(_QWORD *)v10;
}
if ( v8 )
*(_QWORD *)(v8 + 8) = v16;
if ( !v7 )
goto LABEL_9;
*(_QWORD *)(v7 + 8) = 0LL;
if ( v9 )
{
LABEL_10:
v13 = (void *)*((_QWORD *)v10 + 5);
goto LABEL_11;
}
}
else
{
v19 = (char *)malloc(0x38uLL);
v10 = v19;
if ( !v19 )
{
v27 = _errno_location();
v28 = strerror(*v27);
v22 = "error allocating newent: %s";
v23 = 89LL;
v24 = v28;
goto LABEL_28;
}
*((_DWORD *)v19 + 13) = 0;
}
LABEL_20:
v17 = 128;
if ( a3 >= 128 )
v17 = a3;
v18 = malloc(v17);
*((_QWORD *)v10 + 5) = v18;
v13 = v18;
if ( !v18 )
{
v25 = _errno_location();
v26 = strerror(*v25);
v22 = "error allocating packet: %s";
v23 = 104LL;
v24 = v26;
LABEL_28:
warn("pqueue_alloc", "pqueue.c", v23, v22, v24);
return 0xFFFFFFFFLL;
}
*((_DWORD *)v10 + 13) = v17;
if ( a3 > v17 )
_assert_fail("newent->capacity >= packlen", "pqueue.c", 0x6Du, "pqueue_alloc");
LABEL_11:
memcpy(v13, a2, a3);
*((_DWORD *)v10 + 4) = a1;
*((_QWORD *)v10 + 1) = 0LL;
*(_QWORD *)v10 = 0LL;
*((_DWORD *)v10 + 12) = a3;
gettimeofday((struct timeval *)(v10 + 24), 0LL);
v14 = *((_QWORD *)v10 + 4) + packet_timeout_usecs;
*((_QWORD *)v10 + 3) += v14 / 1000000;
result = 0LL;
*((_QWORD *)v10 + 4) = v14 % 1000000;
*a4 = v10;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, const void *a2, int a3, char **a4)
{
__int64 v7; // r14
__int64 v8; // rax
int v9; // edx
char *v10; // rbx
__int64 v11; // rax
void *v12; // rax
void *v13; // rdi
__int64 v14; // rcx
__int64 result; // rax
__int64 *v16; // rcx
int v17; // er14
void *v18; // rax
char *v19; // rax
int *v20; // rax
char *v21; // rax
const char *v22; // rcx
__int64 v23; // rdx
char *v24; // r8
int *v25; // rax
char *v26; // rax
int *v27; // rax
char *v28; // rax

v7 = pq_freelist_head;
if ( pq_freelist_head )
{
v8 = pq_freelist_head;
while ( 1 )
{
v9 = *(_DWORD *)(v8 + 52);
v10 = (char *)v8;
v8 = *(_QWORD *)v8;
if ( v9 >= a3 )
break;
if ( !v8 )
{
v11 = *(_QWORD *)pq_freelist_head;
pq_freelist_head = v11;
if ( v11 )
*(_QWORD *)(v11 + 8) = 0LL;
v12 = realloc(*(void **)(v7 + 40), a3);
*(_QWORD *)(v7 + 40) = v12;
if ( v12 )
{
*(_DWORD *)(v7 + 52) = a3;
v9 = a3;
v10 = (char *)v7;
LABEL_9:
if ( !v9 )
goto LABEL_20;
goto LABEL_10;
}
v20 = _errno_location();
v21 = strerror(*v20);
v22 = "error reallocating packet: %s";
v23 = 76LL;
v24 = v21;
goto LABEL_28;
}
}
if ( (char *)pq_freelist_head == v10 )
{
pq_freelist_head = v8;
v7 = v8;
}
v16 = (__int64 *)*((_QWORD *)v10 + 1);
if ( v16 )
{
*v16 = v8;
v8 = *(_QWORD *)v10;
}
if ( v8 )
*(_QWORD *)(v8 + 8) = v16;
if ( !v7 )
goto LABEL_9;
*(_QWORD *)(v7 + 8) = 0LL;
if ( v9 )
{
LABEL_10:
v13 = (void *)*((_QWORD *)v10 + 5);
goto LABEL_11;
}
}
else
{
v19 = (char *)malloc(0x38uLL);
v10 = v19;
if ( !v19 )
{
v27 = _errno_location();
v28 = strerror(*v27);
v22 = "error allocating newent: %s";
v23 = 89LL;
v24 = v28;
goto LABEL_28;
}
*((_DWORD *)v19 + 13) = 0;
}
LABEL_20:
v17 = 128;
if ( a3 >= 128 )
v17 = a3;
v18 = malloc(v17);
*((_QWORD *)v10 + 5) = v18;
v13 = v18;
if ( !v18 )
{
v25 = _errno_location();
v26 = strerror(*v25);
v22 = "error allocating packet: %s";
v23 = 104LL;
v24 = v26;
LABEL_28:
warn("pqueue_alloc", "pqueue.c", v23, v22, v24);
return 0xFFFFFFFFLL;
}
*((_DWORD *)v10 + 13) = v17;
if ( a3 > v17 )
_assert_fail("newent->capacity >= packlen", "pqueue.c", 0x6Du, "pqueue_alloc");
LABEL_11:
memcpy(v13, a2, a3);
*((_DWORD *)v10 + 4) = a1;
*((_QWORD *)v10 + 1) = 0LL;
*(_QWORD *)v10 = 0LL;
*((_DWORD *)v10 + 12) = a3;
gettimeofday((struct timeval *)(v10 + 24), 0LL);
v14 = *((_QWORD *)v10 + 4) + packet_timeout_usecs;
*((_QWORD *)v10 + 3) += v14 / 1000000;
result = 0LL;
*((_QWORD *)v10 + 4) = v14 % 1000000;
*a4 = v10;
return result;
}

original_code:
static int pqueue_alloc(int seq, unsigned char *packet, int packlen, pqueue_t **new) {
  pqueue_t *newent;
  if (0) { _log(__FUNCTION__,"pqueue.c",36, "seq=%d, packlen=%d" , seq, packlen); };
  if (pq_freelist_head) {
    for (newent = pq_freelist_head; newent; newent = newent->next) {
      if (newent->capacity >= packlen) {
 if (pq_freelist_head == newent)
   pq_freelist_head = newent->next;
 if (newent->prev)
   newent->prev->next = newent->next;
 if (newent->next)
   newent->next->prev = newent->prev;
 if (pq_freelist_head)
   pq_freelist_head->prev = ((void *)0);
 break;
      }
    }
    if (((void *)0) == newent) {
      newent = pq_freelist_head;
      pq_freelist_head = pq_freelist_head->next;
      if (pq_freelist_head)
 pq_freelist_head->prev = ((void *)0);
      if (0) { _log(__FUNCTION__,"pqueue.c",71, "realloc capacity %d to %d" ,newent->capacity, packlen); };
      newent->packet = (unsigned char *)realloc(newent->packet, packlen);
      if (!newent->packet) {
 _warn(__FUNCTION__,"pqueue.c",76, "error reallocating packet: %s" , strerror((*__errno_location ())));
 return -1;
      }
      newent->capacity = packlen;
    }
    if (0) { _log(__FUNCTION__,"pqueue.c",82, "Recycle entry from freelist. Capacity: %d" , newent->capacity); };
  } else {
    newent = (pqueue_t *)malloc( sizeof(pqueue_t) );
    if (!newent) {
      _warn(__FUNCTION__,"pqueue.c",89, "error allocating newent: %s" , strerror((*__errno_location ())));
      return -1;
    }
    newent->capacity = 0;
    if (0) { _log(__FUNCTION__,"pqueue.c",94, "Alloc new queue entry"); };
  }
  if ( ! newent->capacity ) {
    int size = packlen < 128 ? 128 : packlen;
    if (0) { _log(__FUNCTION__,"pqueue.c",101, "allocating for packet size %d" , size); };
    newent->packet = (unsigned char *)malloc(size);
    if (!newent->packet) {
      _warn(__FUNCTION__,"pqueue.c",104, "error allocating packet: %s" , strerror((*__errno_location ())));
      return -1;
    }
    newent->capacity = size;
  }
  ((void) sizeof ((newent->capacity >= packlen) ? 1 : 0), __extension__ ({ if (newent->capacity >= packlen) ; else __assert_fail ("newent->capacity >= packlen", "pqueue.c", 109, __extension__ __PRETTY_FUNCTION__); }));
  memcpy(newent->packet, packet, packlen);
  newent->next = newent->prev = ((void *)0);
  newent->seq = seq;
  newent->packlen = packlen;
  gettimeofday(&newent->expires, ((void *)0));
  newent->expires.tv_usec += packet_timeout_usecs;
  newent->expires.tv_sec += (newent->expires.tv_usec / 1000000);
  newent->expires.tv_usec %= 1000000;
  *new = newent;
  return 0;
}

canonical_original_code:
static int pqueue_alloc(int seq, unsigned char *packet, int packlen, struct pqueue **new) {
  struct pqueue *newent;
  if (0) { _log(__FUNCTION__,"pqueue.c",36, "seq=%d, packlen=%d" , seq, packlen); };
  if (pq_freelist_head) {
    for (newent = pq_freelist_head; newent; newent = newent->next) {
      if (newent->capacity >= packlen) {
 if (pq_freelist_head == newent)
   pq_freelist_head = newent->next;
 if (newent->prev)
   newent->prev->next = newent->next;
 if (newent->next)
   newent->next->prev = newent->prev;
 if (pq_freelist_head)
   pq_freelist_head->prev = ((void *)0);
 break;
      }
    }
    if (((void *)0) == newent) {
      newent = pq_freelist_head;
      pq_freelist_head = pq_freelist_head->next;
      if (pq_freelist_head)
 pq_freelist_head->prev = ((void *)0);
      if (0) { _log(__FUNCTION__,"pqueue.c",71, "realloc capacity %d to %d" ,newent->capacity, packlen); };
      newent->packet = (unsigned char *)realloc(newent->packet, packlen);
      if (!newent->packet) {
 _warn(__FUNCTION__,"pqueue.c",76, "error reallocating packet: %s" , strerror((*__errno_location ())));
 return -1;
      }
      newent->capacity = packlen;
    }
    if (0) { _log(__FUNCTION__,"pqueue.c",82, "Recycle entry from freelist. Capacity: %d" , newent->capacity); };
  } else {
    newent = (struct pqueue *)malloc( sizeof(pqueue_t) );
    if (!newent) {
      _warn(__FUNCTION__,"pqueue.c",89, "error allocating newent: %s" , strerror((*__errno_location ())));
      return -1;
    }
    newent->capacity = 0;
    if (0) { _log(__FUNCTION__,"pqueue.c",94, "Alloc new queue entry"); };
  }
  if ( ! newent->capacity ) {
    int size = packlen < 128 ? 128 : packlen;
    if (0) { _log(__FUNCTION__,"pqueue.c",101, "allocating for packet size %d" , size); };
    newent->packet = (unsigned char *)malloc(size);
    if (!newent->packet) {
      _warn(__FUNCTION__,"pqueue.c",104, "error allocating packet: %s" , strerror((*__errno_location ())));
      return -1;
    }
    newent->capacity = size;
  }
  ((void) sizeof ((newent->capacity >= packlen) ? 1 : 0), __extension__ ({ if (newent->capacity >= packlen) ; else __assert_fail ("newent->capacity >= packlen", "pqueue.c", 109, __extension__ __PRETTY_FUNCTION__); }));
  memcpy(newent->packet, packet, packlen);
  newent->next = newent->prev = ((void *)0);
  newent->seq = seq;
  newent->packlen = packlen;
  gettimeofday(&newent->expires, ((void *)0));
  newent->expires.tv_usec += packet_timeout_usecs;
  newent->expires.tv_sec += (newent->expires.tv_usec / 1000000);
  newent->expires.tv_usec %= 1000000;
  *new = newent;
  return 0;
}

variable_types: seq int

variable_types:
packet
unsigned char *

variable_types: packlen int

variable_types: struct pqueue { struct pqueue *next; struct pqueue *prev; int seq; struct timeval { long tv_sec; long tv_usec; } expires; unsigned char *packet; int packlen; int capacity; } **

variable_types: size int

user_defined_types: struct pqueue { struct pqueue *next; struct pqueue *prev; int seq; struct timeval expires; unsigned char *packet; int packlen; int capacity; }


decompiled_code:
__int64 __fastcall pqueue_add(int a1, const void *a2, int a3)
{
__int64 v3; // rax
__int64 result; // rax
_QWORD *v5; // rcx
char *v6; // rdx
char *v7; // rax
__int64 v8; // rdx
char *v9; // [rsp+8h] [rbp-10h] BYREF

if ( (unsigned int)pqueue_alloc(a1, a2, a3, &v9) )
return 0xFFFFFFFFLL;
v3 = pq_head;
if ( pq_head )
{
while ( 1 )
{
if ( *(_DWORD *)(v3 + 16) == a1 )
{
warn("pqueue_add", "pqueue.c", 139LL, "discarding duplicate packet %d", (unsigned int)a1);
return 0xFFFFFFFFLL;
}
if ( *(_DWORD *)(v3 + 16) > a1 )
break;
v3 = *(_QWORD *)v3;
if ( !v3 )
{
v8 = pq_tail;
v7 = v9;
*(_QWORD *)pq_tail = v9;
goto LABEL_13;
}
}
v5 = *(_QWORD **)(v3 + 8);
v6 = v9;
if ( v5 )
*v5 = v9;
else
pq_head = (__int64)v9;
*(_QWORD *)v6 = v3;
*((_QWORD *)v6 + 1) = v5;
*(_QWORD *)(v3 + 8) = v6;
result = 0LL;
}
else
{
v7 = v9;
v8 = pq_tail;
pq_head = (__int64)v9;
LABEL_13:
*((_QWORD *)v7 + 1) = v8;
pq_tail = (__int64)v7;
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, const void *a2, int a3)
{
__int64 v3; // rax
__int64 result; // rax
_QWORD *v5; // rcx
char *v6; // rdx
char *v7; // rax
__int64 v8; // rdx
char *v9; // [rsp+8h] [rbp-10h] BYREF

if ( (unsigned int)func0(a1, a2, a3, &v9) )
return 0xFFFFFFFFLL;
v3 = pq_head;
if ( pq_head )
{
while ( 1 )
{
if ( *(_DWORD *)(v3 + 16) == a1 )
{
warn("pqueue_add", "pqueue.c", 139LL, "discarding duplicate packet %d", (unsigned int)a1);
return 0xFFFFFFFFLL;
}
if ( *(_DWORD *)(v3 + 16) > a1 )
break;
v3 = *(_QWORD *)v3;
if ( !v3 )
{
v8 = pq_tail;
v7 = v9;
*(_QWORD *)pq_tail = v9;
goto LABEL_13;
}
}
v5 = *(_QWORD **)(v3 + 8);
v6 = v9;
if ( v5 )
*v5 = v9;
else
pq_head = (__int64)v9;
*(_QWORD *)v6 = v3;
*((_QWORD *)v6 + 1) = v5;
*(_QWORD *)(v3 + 8) = v6;
result = 0LL;
}
else
{
v7 = v9;
v8 = pq_tail;
pq_head = (__int64)v9;
LABEL_13:
*((_QWORD *)v7 + 1) = v8;
pq_tail = (__int64)v7;
result = 0LL;
}
return result;
}

original_code:
int pqueue_add (int seq, unsigned char *packet, int packlen) {
  pqueue_t *newent, *point;
  if ( 0 != pqueue_alloc(seq, packet, packlen, &newent) ) {
    return -1;
  }
  for (point = pq_head; point != ((void *)0); point = point->next) {
    if (point->seq == seq) {
      _warn(__FUNCTION__,"pqueue.c",139, "discarding duplicate packet %d" , seq);
      return -1;
    }
    if (point->seq > seq) {
      if (point->prev) {
 if (0) { _log(__FUNCTION__,"pqueue.c",146, "adding %d between %d and %d" , seq, point->prev->seq, point->seq); };
 point->prev->next = newent;
      } else {
 if (0) { _log(__FUNCTION__,"pqueue.c",152, "adding %d before %d" , seq, point->seq); };
 pq_head = newent;
      }
      newent->prev = point->prev;
      newent->next = point;
      point->prev = newent;
      return 0;
    }
  }
  if (pq_head == ((void *)0)) {
    if (0) { _log(__FUNCTION__,"pqueue.c",169, "adding %d to empty queue" , seq); };
    pq_head = newent;
  } else {
    if (0) { _log(__FUNCTION__,"pqueue.c",172, "adding %d as tail, after %d" , seq, pq_tail->seq); };
    pq_tail->next = newent;
  }
  newent->prev = pq_tail;
  pq_tail = newent;
  return 0;
}

canonical_original_code:
int pqueue_add(int seq, unsigned char *packet, int packlen) {
  struct pqueue *newent; struct pqueue *point;
  if ( 0 != pqueue_alloc(seq, packet, packlen, &newent) ) {
    return -1;
  }
  for (point = pq_head; point != ((void *)0); point = point->next) {
    if (point->seq == seq) {
      _warn(__FUNCTION__,"pqueue.c",139, "discarding duplicate packet %d" , seq);
      return -1;
    }
    if (point->seq > seq) {
      if (point->prev) {
 if (0) { _log(__FUNCTION__,"pqueue.c",146, "adding %d between %d and %d" , seq, point->prev->seq, point->seq); };
 point->prev->next = newent;
      } else {
 if (0) { _log(__FUNCTION__,"pqueue.c",152, "adding %d before %d" , seq, point->seq); };
 pq_head = newent;
      }
      newent->prev = point->prev;
      newent->next = point;
      point->prev = newent;
      return 0;
    }
  }
  if (pq_head == ((void *)0)) {
    if (0) { _log(__FUNCTION__,"pqueue.c",169, "adding %d to empty queue" , seq); };
    pq_head = newent;
  } else {
    if (0) { _log(__FUNCTION__,"pqueue.c",172, "adding %d as tail, after %d" , seq, pq_tail->seq); };
    pq_tail->next = newent;
  }
  newent->prev = pq_tail;
  pq_tail = newent;
  return 0;
}

variable_types: struct pqueue { struct pqueue *next; struct pqueue *prev; int seq; struct timeval { long tv_sec; long tv_usec; } expires; unsigned char *packet; int packlen; int capacity; } *


original_code:
int pqueue_del (pqueue_t *point) {
  if (0) { _log(__FUNCTION__,"pqueue.c",185, "Move seq %d to freelist" , point->seq); };
  if (pq_head == point) pq_head = point->next;
  if (pq_tail == point) pq_tail = point->prev;
  if (point->prev) point->prev->next = point->next;
  if (point->next) point->next->prev = point->prev;
  point->next = pq_freelist_head;
  point->prev = ((void *)0);
  if (point->next)
    point->next->prev = point;
  pq_freelist_head = point;
  if (0) { int pq_count = 0; int pq_freelist_count = 0; pqueue_t *point; for ( point = pq_head; point ; point = point->next) { ++pq_count; } for ( point = pq_freelist_head; point ; point = point->next) { ++pq_freelist_count; } _log(__FUNCTION__,"pqueue.c",212, "queue length is %d, freelist length is %d" , pq_count, pq_freelist_count); };
  return 0;
}

canonical_original_code:
int pqueue_del(struct pqueue *point) {
  if (0) { _log(__FUNCTION__,"pqueue.c",185, "Move seq %d to freelist" , point->seq); };
  if (pq_head == point) pq_head = point->next;
  if (pq_tail == point) pq_tail = point->prev;
  if (point->prev) point->prev->next = point->next;
  if (point->next) point->next->prev = point->prev;
  point->next = pq_freelist_head;
  point->prev = ((void *)0);
  if (point->next)
    point->next->prev = point;
  pq_freelist_head = point;
  if (0) { int pq_count = 0; int pq_freelist_count = 0; struct pqueue *point; for ( point = pq_head; point ; point = point->next) { ++pq_count; } for ( point = pq_freelist_head; point ; point = point->next) { ++pq_freelist_count; } _log(__FUNCTION__,"pqueue.c",212, "queue length is %d, freelist length is %d" , pq_count, pq_freelist_count); };
  return 0;
}

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


decompiled_code:
__int64 pqueue_head()
{
return pq_head;
}

canonical_decompiled_code:
__int64 func3()
{
return pq_head;
}

original_code:
pqueue_t *pqueue_head () {
  return pq_head;
}

canonical_original_code:
struct pqueue *pqueue_head() {
  return pq_head;
}

return_type: struct pqueue { struct pqueue *next; struct pqueue *prev; int seq; struct timeval { long tv_sec; long tv_usec; } expires; unsigned char *packet; int packlen; int capacity; } *


decompiled_code:
__int64 __fastcall pqueue_expiry_time(__int64 a1)
{
struct timeval v2; // [rsp+0h] [rbp-18h] BYREF

gettimeofday(&v2, 0LL);
return *(_DWORD *)(a1 + 32)
+ 1000000 * ((unsigned int)*(_QWORD *)(a1 + 24) - LODWORD(v2.tv_sec))
- LODWORD(v2.tv_usec);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
struct timeval v2; // [rsp+0h] [rbp-18h] BYREF

gettimeofday(&v2, 0LL);
return *(_DWORD *)(a1 + 32)
+ 1000000 * ((unsigned int)*(_QWORD *)(a1 + 24) - LODWORD(v2.tv_sec))
- LODWORD(v2.tv_usec);
}

original_code:
int pqueue_expiry_time (pqueue_t *entry) {
  struct timeval tv;
  int expiry_time;
  gettimeofday(&tv, ((void *)0));
  expiry_time = (entry->expires.tv_sec - tv.tv_sec) * 1000000;
  expiry_time += (entry->expires.tv_usec - tv.tv_usec);
  return expiry_time;
}

canonical_original_code:
int pqueue_expiry_time(struct pqueue *entry) {
  struct timeval tv;
  int expiry_time;
  gettimeofday(&tv, ((void *)0));
  expiry_time = (entry->expires.tv_sec - tv.tv_sec) * 1000000;
  expiry_time += (entry->expires.tv_usec - tv.tv_usec);
  return expiry_time;
}

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


main

decompiled_code:
__int64 __fastcall main(int a1, char **a2, char **a3)
{
__int64 v3; // rbx
int v4; // eax
__int64 v5; // rsi
unsigned __int64 v7; // rax

v3 = 0x68000001DLL;
puts("{\\tt");
puts("\\noindent");
LABEL_2:
while ( 1 )
{
v4 = getchar();
if ( (_BYTE)v4 == 0xFF )
break;
while ( 1 )
{
v5 = (unsigned int)(char)v4;
if ( (_BYTE)v4 == 10 )
break;
if ( (char)v4 <= 10 )
{
if ( (_BYTE)v4 == 9 )
{
printf("{\\kern4.000em}");
goto LABEL_2;
}
}
else
{
if ( (_BYTE)v4 == 32 )
{
printf("{\\kern0.500em}");
goto LABEL_2;
}
if ( (char)v4 > 31 )
{
if ( (char)v4 <= 38 )
{
if ( (char)v4 > 33 )
goto LABEL_12;
}
else
{
v7 = (unsigned int)(v4 - 92);
if ( (unsigned __int8)v7 <= 0x22u && _bittest64(&v3, v7) )
{
LABEL_12:
printf("{\\char%d}", v5);
goto LABEL_2;
}
}
}
}
putchar(v5);
v4 = getchar();
if ( (_BYTE)v4 == 0xFF )
goto LABEL_8;
}
puts("\\hfill\n");
puts("\\noindent");
}
LABEL_8:
puts("}");
return 0LL;
}

original_code:
int main ()
{
  register char c;
  puts ("{\\tt");
  puts ("\\noindent");
  while ((c = getchar ()) != (-1))
    {
      switch (c)
 {
 case ' ':
   printf ("{\\kern0.500em}");
   break;
 case '\t':
   printf ("{\\kern4.000em}");
   break;
 case '\n':
   puts ("\\hfill\n");
   puts ("\\noindent");
   break;
 case '\\':
 case '$':
 case '&':
 case '{':
 case '}':
 case '#':
 case '~':
 case '`':
 case '_':
 case '^':
 case '"':
 case '%':
   printf ("{\\char%d}", c);
   break;
 default:
   putchar (c);
   break;
 }
    }
  puts ("}");
  return 0;
}

canonical_original_code:
int main()
{
  register char c;
  puts ("{\\tt");
  puts ("\\noindent");
  while ((c = getchar ()) != (-1))
    {
      switch (c)
 {
 case ' ':
   printf ("{\\kern0.500em}");
   break;
 case '\t':
   printf ("{\\kern4.000em}");
   break;
 case '\n':
   puts ("\\hfill\n");
   puts ("\\noindent");
   break;
 case '\\':
 case '$':
 case '&':
 case '{':
 case '}':
 case '#':
 case '~':
 case '`':
 case '_':
 case '^':
 case '"':
 case '%':
   printf ("{\\char%d}", c);
   break;
 default:
   putchar (c);
   break;
 }
    }
  puts ("}");
  return 0;
}

variable_types: c char


decompiled_code:
bool __fastcall is_slist(_QWORD *a1)
{
bool result; // al

result = 0;
if ( a1 )
result = *a1 == (_QWORD)"struct st_slist";
return result;
}

canonical_decompiled_code:
bool __fastcall func0(_QWORD *a1)
{
bool result; // al

result = 0;
if ( a1 )
result = *a1 == (_QWORD)"struct st_slist";
return result;
}

original_code:
_Bool is_slist (slist_ref slist) {
   return slist != ((void *)0) && slist->tag == slist_tag;
}

canonical_original_code:
_Bool is_slist(struct st_slist *slist) {
   return slist != ((void *)0) && slist->tag == slist_tag;
}

variable_types: struct st_slist { char *tag; int blkct; struct st_node { char *tag; char *permissions; char *uid; char *gid; char *mtime; char *fname; char *lfname; int blocks; int hlinks; int size; struct st_node *link; struct st_node *slink; } *head; struct st_node *stack_top; } *

user_defined_types: struct st_slist { char *tag; int blkct; struct st_node *head; struct st_node *stack_top; }


decompiled_code:
bool __fastcall is_slistnode(_QWORD *a1)
{
bool result; // al

result = 0;
if ( a1 )
result = *a1 == (_QWORD)"struct st_node";
return result;
}

canonical_decompiled_code:
bool __fastcall func1(_QWORD *a1)
{
bool result; // al

result = 0;
if ( a1 )
result = *a1 == (_QWORD)"struct st_node";
return result;
}

original_code:
_Bool is_slistnode (slist_node node) {
   return node != ((void *)0) && node->tag == slist_node_tag;
}

canonical_original_code:
_Bool is_slistnode(struct st_node *node) {
   return node != ((void *)0) && node->tag == slist_node_tag;
}

variable_types: struct st_node { char *tag; char *permissions; char *uid; char *gid; char *mtime; char *fname; char *lfname; int blocks; int hlinks; int size; struct st_node *link; struct st_node *slink; } *


decompiled_code:
bool __fastcall is_hidden(_QWORD *a1)
{
_BYTE *v1; // rax

if ( !is_slistnode(a1) || (v1 = (_BYTE *)a1[5]) == 0LL )
_assert_fail("is_slistnode (node) && node->fname != NULL", "slist.c", 0x32u, "is_hidden");
return *v1 == 46;
}

canonical_decompiled_code:
bool __fastcall func2(_QWORD *a1)
{
_BYTE *v1; // rax

if ( !func1(a1) || (v1 = (_BYTE *)a1[5]) == 0LL )
_assert_fail("is_slistnode (node) && node->fname != NULL", "slist.c", 0x32u, "is_hidden");
return *v1 == 46;
}

original_code:
_Bool is_hidden (slist_node node) {
   ((void) sizeof ((is_slistnode (node) && node->fname != ((void *)0)) ? 1 : 0), __extension__ ({ if (is_slistnode (node) && node->fname != ((void *)0)) ; else __assert_fail ("is_slistnode (node) && node->fname != NULL", "slist.c", 50, __extension__ __PRETTY_FUNCTION__); }));
   return node->fname[0] == '.';
}

canonical_original_code:
_Bool is_hidden(struct st_node *node) {
   ((void) sizeof ((is_slistnode (node) && node->fname != ((void *)0)) ? 1 : 0), __extension__ ({ if (is_slistnode (node) && node->fname != ((void *)0)) ; else __assert_fail ("is_slistnode (node) && node->fname != NULL", "slist.c", 50, __extension__ __PRETTY_FUNCTION__); }));
   return node->fname[0] == '.';
}

user_defined_types: struct st_node { char *tag; char *permissions; char *uid; char *gid; char *mtime; char *fname; char *lfname; int blocks; int hlinks; int size; struct st_node *link; struct st_node *slink; }


decompiled_code:
bool __fastcall is_dir(__int64 a1)
{
if ( !is_slistnode((_QWORD *)a1) )
_assert_fail("is_slistnode (node)", "slist.c", 0x37u, "is_dir");
return **(_BYTE **)(a1 + 8) == 100;
}

canonical_decompiled_code:
bool __fastcall func3(__int64 a1)
{
if ( !func1((_QWORD *)a1) )
_assert_fail("is_slistnode (node)", "slist.c", 0x37u, "is_dir");
return **(_BYTE **)(a1 + 8) == 100;
}

original_code:
_Bool is_dir (slist_node node) {
   ((void) sizeof ((is_slistnode (node)) ? 1 : 0), __extension__ ({ if (is_slistnode (node)) ; else __assert_fail ("is_slistnode (node)", "slist.c", 55, __extension__ __PRETTY_FUNCTION__); }));
   return node->permissions[0] == 'd';
}

canonical_original_code:
_Bool is_dir(struct st_node *node) {
   ((void) sizeof ((is_slistnode (node)) ? 1 : 0), __extension__ ({ if (is_slistnode (node)) ; else __assert_fail ("is_slistnode (node)", "slist.c", 55, __extension__ __PRETTY_FUNCTION__); }));
   return node->permissions[0] == 'd';
}


decompiled_code:
char *__fastcall get_uidstr(__uid_t a1)
{
struct passwd *v1; // rax

v1 = getpwuid(a1);
return strdup(v1->pw_name);
}

canonical_decompiled_code:
char *__fastcall func5(__uid_t a1)
{
struct passwd *v1; // rax

v1 = getpwuid(a1);
return strdup(v1->pw_name);
}

original_code:
char *get_uidstr (uid_t uid) {
   struct passwd *pwd;
   pwd = getpwuid (uid);
   return strdup (pwd->pw_name);
}

canonical_original_code:
char *get_uidstr(unsigned int uid) {
   struct passwd *pwd;
   pwd = getpwuid (uid);
   return strdup (pwd->pw_name);
}

variable_types:
uid
unsigned int

variable_types: struct passwd { char *pw_name; char *pw_passwd; unsigned int pw_uid; unsigned int pw_gid; char *pw_gecos; char *pw_dir; char *pw_shell; } *

return_type: char *


decompiled_code:
char *__fastcall get_gidstr(__gid_t a1)
{
struct group *v1; // rax

v1 = getgrgid(a1);
return strdup(v1->gr_name);
}

canonical_decompiled_code:
char *__fastcall func6(__gid_t a1)
{
struct group *v1; // rax

v1 = getgrgid(a1);
return strdup(v1->gr_name);
}

original_code:
char *get_gidstr (gid_t gid) {
   struct group *grp;
   grp = getgrgid (gid);
   return strdup (grp->gr_name);
}

canonical_original_code:
char *get_gidstr(unsigned int gid) {
   struct group *grp;
   grp = getgrgid (gid);
   return strdup (grp->gr_name);
}

variable_types:
gid
unsigned int

variable_types: struct group { char *gr_name; char *gr_passwd; unsigned int gr_gid; char **gr_mem; } *


decompiled_code:
char *__fastcall get_mtimestr(time_t a1)
{
struct tm *v1; // rax
time_t timer; // [rsp+8h] [rbp-20h] BYREF
char s[24]; // [rsp+10h] [rbp-18h] BYREF

timer = a1;
v1 = localtime(&timer);
strftime(s, 0x10uLL, "%b %e %H:%M", v1);
return strdup(s);
}

canonical_decompiled_code:
char *__fastcall func7(time_t a1)
{
struct tm *v1; // rax
time_t timer; // [rsp+8h] [rbp-20h] BYREF
char s[24]; // [rsp+10h] [rbp-18h] BYREF

timer = a1;
v1 = localtime(&timer);
strftime(s, 0x10uLL, "%b %e %H:%M", v1);
return strdup(s);
}

original_code:
char *get_mtimestr (time_t mtime) {
   char buffer[16];
   struct tm *mtinfo = localtime (&mtime);
   strftime (buffer, 16, "%b %e %H:%M", mtinfo);
   return strdup (buffer);
}

canonical_original_code:
char *get_mtimestr(long mtime) {
   char buffer[16];
   struct tm *mtinfo = localtime (&mtime);
   strftime (buffer, 16, "%b %e %H:%M", mtinfo);
   return strdup (buffer);
}

variable_types: mtime long

variable_types:
buffer
char [16]

variable_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; } *