uartInit

decompiled_code:
__int64 uartInit()
{
__int64 result; // rax
int v1; // eax
int v2; // ebx
struct termios v3; // [rsp+0h] [rbp-48h] BYREF

if ( uart0_filestream == -1 )
{
v1 = open("/dev/ttyAMA0", 2306);
v2 = v1;
uart0_filestream = v1;
if ( v1 == -1 )
{
fprintf(stderr, "uartInit: unable to open %s\n", "/dev/ttyAMA0");
result = 0LL;
}
else
{
tcgetattr(v1, &v3);
v3.c_cflag = 2237;
v3.c_iflag = 4;
v3.c_oflag = 0;
v3.c_lflag = 0;
tcflush(v2, 0);
tcsetattr(v2, 0, &v3);
result = 1LL;
}
}
else
{
fwrite("uartInit: uart already open\n", 1uLL, 0x1CuLL, stderr);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 func20()
{
__int64 result; // rax
int v1; // eax
int v2; // ebx
struct termios v3; // [rsp+0h] [rbp-48h] BYREF

if ( uart0_filestream == -1 )
{
v1 = open("/dev/ttyAMA0", 2306);
v2 = v1;
uart0_filestream = v1;
if ( v1 == -1 )
{
fprintf(stderr, "uartInit: unable to open %s\n", "/dev/ttyAMA0");
result = 0LL;
}
else
{
tcgetattr(v1, &v3);
v3.c_cflag = 2237;
v3.c_iflag = 4;
v3.c_oflag = 0;
v3.c_lflag = 0;
tcflush(v2, 0);
tcsetattr(v2, 0, &v3);
result = 1LL;
}
}
else
{
fwrite("uartInit: uart already open\n", 1uLL, 0x1CuLL, stderr);
result = 0LL;
}
return result;
}

original_code:
char uartInit()
{
    if (uart0_filestream != -1)
    {
        fprintf(stderr, "uartInit: uart already open\n");
        return (1==2);
    }
    uart0_filestream = open("/dev/ttyAMA0", 02 | 0400 | 04000);
    if (uart0_filestream == -1)
    {
        fprintf(stderr, "uartInit: unable to open %s\n", "/dev/ttyAMA0");
        return (1==2);
    }
    struct termios options;
    tcgetattr(uart0_filestream, &options);
    options.c_cflag = 0000015 | 0000060 | 0004000 | 0000200;
    options.c_iflag = 0000004;
    options.c_oflag = 0;
    options.c_lflag = 0;
    tcflush(uart0_filestream, 0);
    tcsetattr(uart0_filestream, 0, &options);
    return (1==1);
}

variable_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }


uartClose

repo: rtucker/raspi-fridge-overseer

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

if ( uart0_filestream != -1 )
result = close(uart0_filestream);
uart0_filestream = -1;
return result;
}

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

if ( uart0_filestream != -1 )
result = close(uart0_filestream);
uart0_filestream = -1;
return result;
}

original_code:
void uartClose()
{
    if (uart0_filestream != -1)
    {
        close(uart0_filestream);
    }
    uart0_filestream = -1;
    return;
}

return_type: void


uartTx

decompiled_code:
__int64 __fastcall uartTx(void *buf, size_t n)
{
int v2; // edx
__int64 result; // rax

if ( uart0_filestream == -1 )
{
fwrite("uartTx: uart not initialized (call uartInit first)\n", 1uLL, 0x33uLL, stderr);
result = 0LL;
}
else
{
v2 = write(uart0_filestream, buf, n);
result = 1LL;
if ( v2 < 0 )
{
fwrite("uartTx: uart tx error\n", 1uLL, 0x16uLL, stderr);
result = 1LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func22(void *buf, size_t n)
{
int v2; // edx
__int64 result; // rax

if ( uart0_filestream == -1 )
{
fwrite("uartTx: uart not initialized (call uartInit first)\n", 1uLL, 0x33uLL, stderr);
result = 0LL;
}
else
{
v2 = write(uart0_filestream, buf, n);
result = 1LL;
if ( v2 < 0 )
{
fwrite("uartTx: uart tx error\n", 1uLL, 0x16uLL, stderr);
result = 1LL;
}
}
return result;
}

original_code:
char uartTx(char *txbuf, size_t len)
{
    if (uart0_filestream == -1)
    {
        fprintf(stderr, "uartTx: uart not initialized (call uartInit first)\n");
        return (1==2);
    }
    int tx_len = write(uart0_filestream, txbuf, len);
    if (tx_len < 0)
    {
        fprintf(stderr, "uartTx: uart tx error\n");
    }
    return (1==1);
}

canonical_original_code:
char uartTx(char *txbuf, unsigned long len)
{
    if (uart0_filestream == -1)
    {
        fprintf(stderr, "uartTx: uart not initialized (call uartInit first)\n");
        return (1==2);
    }
    int tx_len = write(uart0_filestream, txbuf, len);
    if (tx_len < 0)
    {
        fprintf(stderr, "uartTx: uart tx error\n");
    }
    return (1==1);
}

variable_types:
txbuf
char *

variable_types:
len
unsigned long

return_type: char


uartRx

decompiled_code:
__int64 __fastcall uartRx(void *buf, size_t nbytes)
{
__int64 result; // rax

if ( uart0_filestream == -1 )
{
fwrite("uartRx: uart not initialized (call uartInit first)\n", 1uLL, 0x33uLL, stderr);
result = 0LL;
}
else
{
LODWORD(result) = read(uart0_filestream, buf, nbytes);
if ( (int)result < 0 )
{
fwrite("uartRx: uart rx error\n", 1uLL, 0x16uLL, stderr);
result = 0LL;
}
else
{
result = (int)result;
*((_BYTE *)buf + (int)result) = 0;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func23(void *buf, size_t nbytes)
{
__int64 result; // rax

if ( uart0_filestream == -1 )
{
fwrite("uartRx: uart not initialized (call uartInit first)\n", 1uLL, 0x33uLL, stderr);
result = 0LL;
}
else
{
LODWORD(result) = read(uart0_filestream, buf, nbytes);
if ( (int)result < 0 )
{
fwrite("uartRx: uart rx error\n", 1uLL, 0x16uLL, stderr);
result = 0LL;
}
else
{
result = (int)result;
*((_BYTE *)buf + (int)result) = 0;
}
}
return result;
}

original_code:
size_t uartRx(char *rxbuf, size_t maxlen)
{
    if (uart0_filestream == -1)
    {
        fprintf(stderr, "uartRx: uart not initialized (call uartInit first)\n");
        return 0;
    }
    int rx_len = read(uart0_filestream, rxbuf, maxlen);
    if (rx_len < 0)
    {
        fprintf(stderr, "uartRx: uart rx error\n");
        return 0;
    }
    else
    {
        rxbuf[rx_len] = '\0';
        return rx_len;
    }
}

canonical_original_code:
unsigned long uartRx(char *rxbuf, unsigned long maxlen)
{
    if (uart0_filestream == -1)
    {
        fprintf(stderr, "uartRx: uart not initialized (call uartInit first)\n");
        return 0;
    }
    int rx_len = read(uart0_filestream, rxbuf, maxlen);
    if (rx_len < 0)
    {
        fprintf(stderr, "uartRx: uart rx error\n");
        return 0;
    }
    else
    {
        rxbuf[rx_len] = '\0';
        return rx_len;
    }
}

variable_types:
rxbuf
char *

variable_types:
maxlen
unsigned long

return_type: unsigned long


decompiled_code:
__int64 __fastcall float_cmp(const void *a1, const void *a2)
{
return (unsigned int)(*(_DWORD *)a1 - *(_DWORD *)a2);
}

canonical_decompiled_code:
__int64 __fastcall func0(const void *a1, const void *a2)
{
return (unsigned int)(*(_DWORD *)a1 - *(_DWORD *)a2);
}

original_code:
int float_cmp(void const *a, void const *b)
{
    int const *ia = (int const *)a;
    int const *ib = (int const *)b;
    return *ia - *ib;
}

canonical_original_code:
int float_cmp(void *a, void *b)
{
    int *ia = (int const *)a;
    int *ib = (int const *)b;
    return *ia - *ib;
}

variable_types:
a
void *

variable_types:
b
void *

variable_types:
ia
int *

variable_types:
ib
int *

return_type: int


convertTemperature

decompiled_code:
float __fastcall convertTemperature(char a1, char a2)
{
int v2; // edx

v2 = 0;
if ( a1 < 0 )
v2 = -65536;
return (float)(v2 + (a1 << 8) + a2) * 0.0625;
}

original_code:
float convertTemperature(char msb, char lsb)
{
    signed int rawval = 0;
    if ((msb & (1 << 7)) > 0)
    {
        rawval -= 0x10000;
    }
    rawval += msb << 8;
    rawval += lsb;
    return (float)rawval * 0.0625;
}

canonical_original_code:
float convertTemperature(char msb, char lsb)
{
    int rawval = 0;
    if ((msb & (1 << 7)) > 0)
    {
        rawval -= 0x10000;
    }
    rawval += msb << 8;
    rawval += lsb;
    return (float)rawval * 0.0625;
}

variable_types: msb char

variable_types: lsb char

variable_types: rawval int

return_type: float


repo: rudymatela/evenmoreutils

decompiled_code:
__int64 __fastcall custom_callback(const char *a1)
{
printf("Custom callback with %s as parameter\n", a1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(const char *a1)
{
printf("Custom callback with %s as parameter\n", a1);
return 0LL;
}

original_code:
int custom_callback(const char *arg, void *pvar)
{
 printf("Custom callback with %s as parameter\n", arg);
 return 0;
}

canonical_original_code:
int custom_callback(char *arg, void *pvar)
{
 printf("Custom callback with %s as parameter\n", arg);
 return 0;
}

variable_types:
arg
char *

variable_types:
pvar
void *


sgetoptnormal

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

for ( result = a1; *(_BYTE *)result || *(_QWORD *)(result + 8); result += 40LL )
;
return result;
}

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

for ( result = a1; *(_BYTE *)result || *(_QWORD *)(result + 8); result += 40LL )
;
return result;
}

original_code:
const static struct soption *sgetoptnormal(const struct soption optable[])
{
 while (optable->sname || optable->lname)
  optable++;
 return optable;
}

canonical_original_code:
static struct soption *sgetoptnormal(struct soption optable[])
{
 while (optable->sname || optable->lname)
  optable++;
 return optable;
}

variable_types:
optable
struct soption []

return_type: struct soption { char sname; char *lname; int has_arg; int (*func)(char *, void *); void *arg; } *


sgetoptfromc

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

for ( result = a2; *(_BYTE *)result || *(_QWORD *)(result + 8); result += 40LL )
{
if ( *(_BYTE *)result == a1 )
return result;
}
if ( a1 )
result = *(_QWORD *)(result + 8);
return result;
}

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

for ( result = a2; *(_BYTE *)result || *(_QWORD *)(result + 8); result += 40LL )
{
if ( *(_BYTE *)result == a1 )
return result;
}
if ( a1 )
result = *(_QWORD *)(result + 8);
return result;
}

original_code:
const static struct soption *sgetoptfromc(char c, const struct soption optable[])
{
 while (optable->sname || optable->lname) {
  if (optable->sname == c)
   return optable;
  optable++;
 }
 return c ? ((void *)0): optable;
}

canonical_original_code:
static struct soption *sgetoptfromc(char c, struct soption optable[])
{
 while (optable->sname || optable->lname) {
  if (optable->sname == c)
   return optable;
  optable++;
 }
 return c ? ((void *)0): optable;
}

variable_types: c char

user_defined_types: struct soption { char sname; char *lname; int has_arg; int (*func)(char *, void *); void *arg; }


sgetoptfroms

decompiled_code:
__int64 __fastcall sgetoptfroms(_BYTE *a1, __int64 a2)
{
char *v4; // rdi

while ( 1 )
{
if ( *(_BYTE *)a2 )
{
v4 = *(char **)(a2 + 8);
if ( !v4 )
goto LABEL_4;
goto LABEL_3;
}
v4 = *(char **)(a2 + 8);
if ( !v4 )
break;
LABEL_3:
if ( !(unsigned int)strchrcmp(v4, a1, 61) )
return a2;
LABEL_4:
a2 += 40LL;
}
if ( a1 )
a2 = *(_QWORD *)(a2 + 8);
return a2;
}

canonical_decompiled_code:
__int64 __fastcall func11(_BYTE *a1, __int64 a2)
{
char *v4; // rdi

while ( 1 )
{
if ( *(_BYTE *)a2 )
{
v4 = *(char **)(a2 + 8);
if ( !v4 )
goto LABEL_4;
goto LABEL_3;
}
v4 = *(char **)(a2 + 8);
if ( !v4 )
break;
LABEL_3:
if ( !(unsigned int)func10(v4, a1, 61) )
return a2;
LABEL_4:
a2 += 40LL;
}
if ( a1 )
a2 = *(_QWORD *)(a2 + 8);
return a2;
}

original_code:
const static struct soption *sgetoptfroms(const char *s, const struct soption optable[])
{
 while (optable->sname || optable->lname) {
  if (optable->lname && strchrcmp(optable->lname,s,'=') == 0)
   return optable;
  optable++;
 }
 return s ? ((void *)0): optable;
}

canonical_original_code:
static struct soption *sgetoptfroms(char *s, struct soption optable[])
{
 while (optable->sname || optable->lname) {
  if (optable->lname && strchrcmp(optable->lname,s,'=') == 0)
   return optable;
  optable++;
 }
 return s ? ((void *)0): optable;
}

variable_types:
s
char *


decompiled_code:
_BOOL8 __fastcall capture_int(const char *a1, _DWORD *a2)
{
char *endptr; // [rsp+8h] [rbp-20h] BYREF

*a2 = strtol(a1, &endptr, 10);
return endptr == a1;
}

canonical_decompiled_code:
_BOOL8 __fastcall func13(const char *a1, _DWORD *a2)
{
char *endptr; // [rsp+8h] [rbp-20h] BYREF

*a2 = strtol(a1, &endptr, 10);
return endptr == a1;
}

original_code:
int capture_int(const char *carg, void *pvar)
{
 int *pi = pvar;
 char *end;
 *pi = strtol(carg, &end, 10);
 return end!=carg ? 0: 1;
}

canonical_original_code:
int capture_int(char *carg, void *pvar)
{
 int *pi = pvar;
 char *end;
 *pi = strtol(carg, &end, 10);
 return end!=carg ? 0: 1;
}

variable_types:
carg
char *

variable_types:
pi
int *

variable_types:
end
char *


decompiled_code:
_BOOL8 __fastcall capture_int_pedantic(const char *a1, _DWORD *a2)
{
_BOOL8 result; // rax
char *endptr; // [rsp+8h] [rbp-20h] BYREF

*a2 = strtol(a1, &endptr, 10);
result = 1LL;
if ( endptr != a1 )
result = *endptr != 0;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func14(const char *a1, _DWORD *a2)
{
_BOOL8 result; // rax
char *endptr; // [rsp+8h] [rbp-20h] BYREF

*a2 = strtol(a1, &endptr, 10);
result = 1LL;
if ( endptr != a1 )
result = *endptr != 0;
return result;
}

original_code:
int capture_int_pedantic(const char *carg, void *pvar)
{
 int *pi = pvar;
 char *end;
 *pi = strtol(carg, &end, 10);
 return end!=carg && !*end ? 0: 1;
}

canonical_original_code:
int capture_int_pedantic(char *carg, void *pvar)
{
 int *pi = pvar;
 char *end;
 *pi = strtol(carg, &end, 10);
 return end!=carg && !*end ? 0: 1;
}


decompiled_code:
_BOOL8 __fastcall capture_char(char *a1, char *a2)
{
char v2; // al

v2 = *a1;
*a2 = *a1;
return v2 == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func15(char *a1, char *a2)
{
char v2; // al

v2 = *a1;
*a2 = *a1;
return v2 == 0;
}

original_code:
int capture_char(const char *carg, void *pvar)
{
 char *pc = pvar;
 *pc = *carg;
 return *pc ? 0 : 1;
}

canonical_original_code:
int capture_char(char *carg, void *pvar)
{
 char *pc = pvar;
 *pc = *carg;
 return *pc ? 0 : 1;
}

variable_types:
pc
char *


decompiled_code:
_BOOL8 capture_float()
{
return (unsigned int)__isoc99_sscanf() != 1;
}

canonical_decompiled_code:
_BOOL8 func16()
{
return (unsigned int)__isoc99_sscanf() != 1;
}

original_code:
int capture_float(const char *carg, void *pvar)
{
 float *pf = pvar;
 return sscanf(carg, "%f",pf) == 1 ? 0 : 1;
}

canonical_original_code:
int capture_float(char *carg, void *pvar)
{
 float *pf = pvar;
 return sscanf(carg, "%f",pf) == 1 ? 0 : 1;
}

variable_types:
pf
float *


decompiled_code:
_BOOL8 capture_double()
{
return (unsigned int)__isoc99_sscanf() != 1;
}

canonical_decompiled_code:
_BOOL8 func17()
{
return (unsigned int)__isoc99_sscanf() != 1;
}

original_code:
int capture_double(const char *carg, void *pvar)
{
 double *pf = pvar;
 return sscanf(carg, "%lf",pf) == 1 ? 0 : 1;
}

canonical_original_code:
int capture_double(char *carg, void *pvar)
{
 double *pf = pvar;
 return sscanf(carg, "%lf",pf) == 1 ? 0 : 1;
}

variable_types:
pf
double *


decompiled_code:
__int64 __fastcall capture_charpointer(__int64 a1, _QWORD *a2)
{
*a2 = a1;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func18(__int64 a1, _QWORD *a2)
{
*a2 = a1;
return 0LL;
}

original_code:
int capture_charpointer(const char *carg, void *pvar)
{
 char **ppchar = pvar;
 *ppchar = (void*)carg;
 return 0;
}

canonical_original_code:
int capture_charpointer(char *carg, void *pvar)
{
 char **ppchar = pvar;
 *ppchar = (void *)carg;
 return 0;
}

variable_types:
ppchar
char **


decompiled_code:
__int64 __fastcall capture_presence(__int64 a1, _DWORD *a2)
{
*a2 = 1;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func19(__int64 a1, _DWORD *a2)
{
*a2 = 1;
return 0LL;
}

original_code:
int capture_presence(const char *carg, void *pvar)
{
 int *pi = pvar;
 *pi = 1;
 return 0;
}

canonical_original_code:
int capture_presence(char *carg, void *pvar)
{
 int *pi = pvar;
 *pi = 1;
 return 0;
}


decompiled_code:
__int64 __fastcall capture_presence_as_0(__int64 a1, _DWORD *a2)
{
*a2 = 0;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func20(__int64 a1, _DWORD *a2)
{
*a2 = 0;
return 0LL;
}

original_code:
int capture_presence_as_0(const char *carg, void *pvar)
{
 int *pi = pvar;
 *pi = 0;
 return 0;
}

canonical_original_code:
int capture_presence_as_0(char *carg, void *pvar)
{
 int *pi = pvar;
 *pi = 0;
 return 0;
}


decompiled_code:
__int64 __fastcall capture_nonoption(__int64 a1, __int64 a2)
{
unsigned int v2; // edx
int v3; // eax

if ( a2 )
{
v2 = 0;
if ( !a1 )
v2 = i_0;
v3 = i_0++;
*(_QWORD *)(a2 + 8LL * v3) = a1;
}
else
{
v2 = i_0;
i_0 = 0;
}
return v2;
}

original_code:
int capture_nonoption(const char *carg, void *pvar)
{
    static int i = 0;
    int r = 0;
    const char **opts = (const char**)pvar;
    if (opts) {
        if (!carg)
            r = i;
        opts[i++] = carg;
    } else {
        r = i;
        i = 0;
    }
    return r;
}

canonical_original_code:
int capture_nonoption(char *carg, void *pvar)
{
    static int i = 0;
    int r = 0;
    char **opts = (const char**)pvar;
    if (opts) {
        if (!carg)
            r = i;
        opts[i++] = carg;
    } else {
        r = i;
        i = 0;
    }
    return r;
}

variable_types: i int

variable_types: r int

variable_types:
opts
char **


ppcharlen

decompiled_code:
__int64 __fastcall ppcharlen(__int64 a1)
{
__int64 v1; // rax
unsigned int v2; // edx

v1 = 0LL;
do
v2 = v1++;
while ( *(_QWORD *)(a1 + 8 * v1 - 8) );
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func22(__int64 a1)
{
__int64 v1; // rax
unsigned int v2; // edx

v1 = 0LL;
do
v2 = v1++;
while ( *(_QWORD *)(a1 + 8 * v1 - 8) );
return v2;
}

original_code:
int ppcharlen(char * const ppchar[])
{
 int i = -1;
 while (ppchar[++i]);
 return i;
}

canonical_original_code:
int ppcharlen(char *ppchar[])
{
 int i = -1;
 while (ppchar[++i]);
 return i;
}

variable_types:
ppchar
char *[]


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebp
int v4; // er12
const char *v5; // rdi
const char **v6; // rbx
char v8[8]; // [rsp+0h] [rbp-108h] BYREF
const char *v9; // [rsp+8h] [rbp-100h]
int v10; // [rsp+10h] [rbp-F8h]
__int64 (__fastcall *v11)(); // [rsp+18h] [rbp-F0h]
int *v12; // [rsp+20h] [rbp-E8h]
char v13; // [rsp+28h] [rbp-E0h]
__int64 v14; // [rsp+30h] [rbp-D8h]
int v15; // [rsp+38h] [rbp-D0h]
__int64 (__fastcall *v16)(); // [rsp+40h] [rbp-C8h]
int *v17; // [rsp+48h] [rbp-C0h]
char v18; // [rsp+50h] [rbp-B8h]
const char *v19; // [rsp+58h] [rbp-B0h]
int v20; // [rsp+60h] [rbp-A8h]
__int64 (__fastcall *v21)(); // [rsp+68h] [rbp-A0h]
int *v22; // [rsp+70h] [rbp-98h]
char v23; // [rsp+78h] [rbp-90h]
const char *v24; // [rsp+80h] [rbp-88h]
int v25; // [rsp+88h] [rbp-80h]
__int64 (__fastcall *v26)(); // [rsp+90h] [rbp-78h]
__int64 *v27; // [rsp+98h] [rbp-70h]
char v28; // [rsp+A0h] [rbp-68h]
const char *v29; // [rsp+A8h] [rbp-60h]
int v30; // [rsp+B0h] [rbp-58h]
__int64 (__fastcall *v31)(const char *); // [rsp+B8h] [rbp-50h]
__int64 v32; // [rsp+C0h] [rbp-48h]
char v33; // [rsp+C8h] [rbp-40h]
__int64 v34; // [rsp+D0h] [rbp-38h]
int v35; // [rsp+D8h] [rbp-30h]
__int64 v36; // [rsp+E0h] [rbp-28h]
__int64 v37; // [rsp+E8h] [rbp-20h]

v8[0] = 104;
v9 = "help";
v10 = 0;
v11 = capture_presence;
v12 = &help_3;
v13 = 111;
v14 = 0LL;
v15 = 0;
v16 = capture_presence;
v17 = &show_nonoption_arguments_2;
v18 = 105;
v19 = "integer";
v20 = 1;
v21 = capture_int;
v22 = &some_integer_1;
v23 = 115;
v24 = "string";
v25 = 1;
v26 = capture_charpointer;
v27 = &some_string_0;
v28 = 99;
v29 = "custom";
v30 = 1;
v31 = custom_callback;
v32 = 0LL;
v33 = 0;
v34 = 0LL;
v35 = 0;
v36 = 0LL;
v37 = 0LL;
if ( (unsigned int)sgetopt(argc, argv, v8, argv + 1, 0LL) )
{
puts("Error parsing one of the command line options");
v3 = 1;
}
else
{
v3 = 0;
v4 = help_3;
if ( help_3 )
{
puts("Usage: check source for details");
}
else
{
if ( show_nonoption_arguments_2 )
{
v5 = argv[1];
if ( v5 )
{
v6 = argv + 2;
do
{
puts(v5);
v5 = *v6++;
}
while ( v5 );
}
}
printf("integer = %i, string = %s\n", (unsigned int)some_integer_1, (const char *)some_string_0);
v3 = v4;
}
}
return v3;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebp
int v4; // er12
const char *v5; // rdi
const char **v6; // rbx
char v8[8]; // [rsp+0h] [rbp-108h] BYREF
const char *v9; // [rsp+8h] [rbp-100h]
int v10; // [rsp+10h] [rbp-F8h]
__int64 (__fastcall *v11)(); // [rsp+18h] [rbp-F0h]
int *v12; // [rsp+20h] [rbp-E8h]
char v13; // [rsp+28h] [rbp-E0h]
__int64 v14; // [rsp+30h] [rbp-D8h]
int v15; // [rsp+38h] [rbp-D0h]
__int64 (__fastcall *v16)(); // [rsp+40h] [rbp-C8h]
int *v17; // [rsp+48h] [rbp-C0h]
char v18; // [rsp+50h] [rbp-B8h]
const char *v19; // [rsp+58h] [rbp-B0h]
int v20; // [rsp+60h] [rbp-A8h]
__int64 (__fastcall *v21)(); // [rsp+68h] [rbp-A0h]
int *v22; // [rsp+70h] [rbp-98h]
char v23; // [rsp+78h] [rbp-90h]
const char *v24; // [rsp+80h] [rbp-88h]
int v25; // [rsp+88h] [rbp-80h]
__int64 (__fastcall *v26)(); // [rsp+90h] [rbp-78h]
__int64 *v27; // [rsp+98h] [rbp-70h]
char v28; // [rsp+A0h] [rbp-68h]
const char *v29; // [rsp+A8h] [rbp-60h]
int v30; // [rsp+B0h] [rbp-58h]
__int64 (__fastcall *v31)(const char *); // [rsp+B8h] [rbp-50h]
__int64 v32; // [rsp+C0h] [rbp-48h]
char v33; // [rsp+C8h] [rbp-40h]
__int64 v34; // [rsp+D0h] [rbp-38h]
int v35; // [rsp+D8h] [rbp-30h]
__int64 v36; // [rsp+E0h] [rbp-28h]
__int64 v37; // [rsp+E8h] [rbp-20h]

v8[0] = 104;
v9 = "help";
v10 = 0;
v11 = capture_presence;
v12 = &help_3;
v13 = 111;
v14 = 0LL;
v15 = 0;
v16 = capture_presence;
v17 = &show_nonoption_arguments_2;
v18 = 105;
v19 = "integer";
v20 = 1;
v21 = capture_int;
v22 = &some_integer_1;
v23 = 115;
v24 = "string";
v25 = 1;
v26 = capture_charpointer;
v27 = &some_string_0;
v28 = 99;
v29 = "custom";
v30 = 1;
v31 = custom_callback;
v32 = 0LL;
v33 = 0;
v34 = 0LL;
v35 = 0;
v36 = 0LL;
v37 = 0LL;
if ( (unsigned int)func12(argc, argv, v8, argv + 1, 0LL) )
{
puts("Error parsing one of the command line options");
v3 = 1;
}
else
{
v3 = 0;
v4 = help_3;
if ( help_3 )
{
puts("Usage: check source for details");
}
else
{
if ( show_nonoption_arguments_2 )
{
v5 = argv[1];
if ( v5 )
{
v6 = argv + 2;
do
{
puts(v5);
v5 = *v6++;
}
while ( v5 );
}
}
printf("integer = %i, string = %s\n", (unsigned int)some_integer_1, (const char *)some_string_0);
v3 = v4;
}
}
return v3;
}

original_code:
int main(int argc, char **argv)
{
 static int help;
 static int show_nonoption_arguments;
 static int some_integer;
 static char *some_string;
 struct soption opttable[] = {
  { 'h', "help", 0, capture_presence, &help },
  { 'o', ((void *)0), 0, capture_presence, &show_nonoption_arguments },
  { 'i', "integer", 1, capture_int, &some_integer },
  { 's', "string", 1, capture_charpointer, &some_string },
  { 'c', "custom", 1, custom_callback, 0 },
  { 0, 0, 0, 0, 0 }
 };
 int i;
 if (sgetopt(argc, argv, opttable, argv+1, 0)) {
  printf("Error parsing one of the command line options\n");
  return 1;
 }
 if (help) {
  printf("Usage: check source for details\n");
  return 0;
 }
 if (show_nonoption_arguments)
  for (i=1; argv[i]; i++)
   printf("%s\n", argv[i]);
 printf("integer = %i, string = %s\n", some_integer, some_string);
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: help int

variable_types: struct soption { char sname; char *lname; int has_arg; int (*func)(char *, void *); void *arg; } []


difftimespec

decompiled_code:
double __fastcall difftimespec(_DWORD *a1, _DWORD *a2)
{
return (double)(a1[2] - a2[2]) / 1000000000.0 + (double)(*a1 - *a2);
}

canonical_decompiled_code:
double __fastcall func0(_DWORD *a1, _DWORD *a2)
{
return (double)(a1[2] - a2[2]) / 1000000000.0 + (double)(*a1 - *a2);
}

original_code:
double difftimespec(struct timespec *buftime1, struct timespec *buftime0)
{
 return buftime1->tv_sec - buftime0->tv_sec +
        (double)(buftime1->tv_nsec - buftime0->tv_nsec) / 1000000000.;
}

variable_types: struct timespec { long tv_sec; long tv_nsec; } *

return_type: double


decompiled_code:
int __fastcall stat_time(const char *a1, __m128i *a2, char a3)
{
int result; // eax
struct stat v5; // [rsp+0h] [rbp-A8h] BYREF

result = stat(a1, &v5);
if ( !result )
{
switch ( a3 )
{
case 'c':
*a2 = _mm_loadu_si128((const __m128i *)&v5.st_ctim);
break;
case 'm':
*a2 = _mm_loadu_si128((const __m128i *)&v5.st_mtim);
break;
case 'a':
*a2 = _mm_loadu_si128((const __m128i *)&v5.st_atim);
break;
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func1(const char *a1, __m128i *a2, char a3)
{
int result; // eax
struct stat v5; // [rsp+0h] [rbp-A8h] BYREF

result = stat(a1, &v5);
if ( !result )
{
switch ( a3 )
{
case 'c':
*a2 = _mm_loadu_si128((const __m128i *)&v5.st_ctim);
break;
case 'm':
*a2 = _mm_loadu_si128((const __m128i *)&v5.st_mtim);
break;
case 'a':
*a2 = _mm_loadu_si128((const __m128i *)&v5.st_atim);
break;
}
}
return result;
}

original_code:
int stat_time(const char *path, struct timespec *buf, char type)
{
 struct stat statbuf;
 int r;
 r = stat(path,&statbuf);
 if (r)
  return r;
 switch (type) {
 case 'a':
  (*buf) = statbuf.st_atim;
  break;
 case 'm':
  (*buf) = statbuf.st_mtim;
  break;
 case 'c':
  (*buf) = statbuf.st_ctim;
  break;
 }
 return r;
}

canonical_original_code:
int stat_time(char *path, struct timespec *buf, char type)
{
 struct stat statbuf;
 int r;
 r = stat(path,&statbuf);
 if (r)
  return r;
 switch (type) {
 case 'a':
  (*buf) = statbuf.st_atim;
  break;
 case 'm':
  (*buf) = statbuf.st_mtim;
  break;
 case 'c':
  (*buf) = statbuf.st_ctim;
  break;
 }
 return r;
}

variable_types:
path
char *

variable_types: type char

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec { long tv_sec; long tv_nsec; } st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }

user_defined_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }


decompiled_code:
double __fastcall stat_age(const char *a1, char a2)
{
double result; // xmm0_8
struct timespec v3; // [rsp+0h] [rbp-28h] BYREF
__m128i v4; // [rsp+10h] [rbp-18h] BYREF

if ( stat_time(a1, &v4, a2) || clock_gettime(0, &v3) )
result = 0.0 / 0.0;
else
result = difftimespec(&v3, &v4);
return result;
}

canonical_decompiled_code:
double __fastcall func2(const char *a1, char a2)
{
double result; // xmm0_8
struct timespec v3; // [rsp+0h] [rbp-28h] BYREF
__m128i v4; // [rsp+10h] [rbp-18h] BYREF

if ( func1(a1, &v4, a2) || clock_gettime(0, &v3) )
result = 0.0 / 0.0;
else
result = func0(&v3, &v4);
return result;
}

original_code:
double stat_age(const char *path, char type)
{
 struct timespec file_time;
 struct timespec curr_time;
 if (stat_time(path, &file_time, type) || clock_gettime(0, &curr_time))
  return 0./0.;
 return difftimespec(&curr_time, &file_time);
}

canonical_original_code:
double stat_age(char *path, char type)
{
 struct timespec file_time;
 struct timespec curr_time;
 if (stat_time(path, &file_time, type) || clock_gettime(0, &curr_time))
  return 0./0.;
 return difftimespec(&curr_time, &file_time);
}

variable_types: struct timespec { long tv_sec; long tv_nsec; }


decompiled_code:
void __fastcall init_TCB(__int64 a1, void (*a2)(void), __int64 a3, int a4)
{
*(_QWORD *)a1 = 0LL;
*(_QWORD *)(a1 + 976) = 0LL;
memset(
(void *)((a1 + 8) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)a1 - (((_DWORD)a1 + 8) & 0xFFFFFFF8) + 984) >> 3));
getcontext((ucontext_t *)(a1 + 16));
*(_QWORD *)(a1 + 32) = a3;
*(_QWORD *)(a1 + 48) = a4;
makecontext((ucontext_t *)(a1 + 16), a2, 0);
}

canonical_decompiled_code:
void __fastcall func7(__int64 a1, void (*a2)(void), __int64 a3, int a4)
{
*(_QWORD *)a1 = 0LL;
*(_QWORD *)(a1 + 976) = 0LL;
memset(
(void *)((a1 + 8) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)a1 - (((_DWORD)a1 + 8) & 0xFFFFFFF8) + 984) >> 3));
getcontext((ucontext_t *)(a1 + 16));
*(_QWORD *)(a1 + 32) = a3;
*(_QWORD *)(a1 + 48) = a4;
makecontext((ucontext_t *)(a1 + 16), a2, 0);
}

original_code:
void init_TCB(TCB_t *tcb, void *function, void *stackP, int stack_size)
{
    memset(tcb, '\0', sizeof(TCB_t));
    getcontext(&tcb->context);
    tcb->context.uc_stack.ss_sp = stackP;
    tcb->context.uc_stack.ss_size = (size_t) stack_size;
    makecontext(&tcb->context, function, 0);
}

canonical_original_code:
void init_TCB(struct TCB_t *tcb, void *function, void *stackP, int stack_size)
{
    memset(tcb, '\0', sizeof(TCB_t));
    getcontext(&tcb->context);
    tcb->context.uc_stack.ss_sp = stackP;
    tcb->context.uc_stack.ss_size = (unsigned long) stack_size;
    makecontext(&tcb->context, function, 0);
}

variable_types: struct TCB_t { struct TCB_t *next; struct TCB_t *prev; struct ucontext_t { unsigned long uc_flags; struct ucontext_t *uc_link; struct stack_t { void *ss_sp; int ss_flags; unsigned long ss_size; } uc_stack; struct mcontext_t { long long gregs[23]; struct _libc_fpstate { unsigned short cwd; unsigned short swd; unsigned short ftw; unsigned short fop; unsigned long rip; unsigned long rdp; unsigned int mxcsr; unsigned int mxcr_mask; struct _libc_fpxreg { unsigned short significand[4]; unsigned short exponent; unsigned short __glibc_reserved1[3]; } _st[8]; struct _libc_xmmreg { unsigned int element[4]; } _xmm[16]; unsigned int __glibc_reserved1[24]; } *fpregs; unsigned long long __reserved1[8]; } uc_mcontext; struct __sigset_t { unsigned long __val[-1]; } uc_sigmask; struct _libc_fpstate __fpregs_mem; unsigned long long __ssp[4]; } context; } *

variable_types:
function
void *

variable_types:
stackP
void *

user_defined_types: struct TCB_t { struct TCB_t *next; struct TCB_t *prev; struct ucontext_t context; }

user_defined_types: struct ucontext_t { unsigned long uc_flags; struct ucontext_t *uc_link; struct stack_t uc_stack; struct mcontext_t uc_mcontext; struct __sigset_t uc_sigmask; struct _libc_fpstate __fpregs_mem; unsigned long long __ssp[4]; }

user_defined_types: struct mcontext_t { long long gregs[23]; struct _libc_fpstate *fpregs; unsigned long long __reserved1[8]; }

user_defined_types: struct _libc_fpstate { unsigned short cwd; unsigned short swd; unsigned short ftw; unsigned short fop; unsigned long rip; unsigned long rdp; unsigned int mxcsr; unsigned int mxcr_mask; struct _libc_fpxreg _st[8]; struct _libc_xmmreg _xmm[16]; unsigned int __glibc_reserved1[24]; }


deleteQ

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

v1 = *a1;
if ( !*a1 )
{
puts("There is no element in the queue ");
exit(-1);
}
if ( v1 == (_QWORD *)*v1 )
{
temp = (__int64)*a1;
*a1 = 0LL;
}
else
{
temp = (__int64)*a1;
*(_QWORD *)(*v1 + 8LL) = v1[1];
*(_QWORD *)(*a1)[1] = **a1;
*a1 = (_QWORD *)**a1;
}
return temp;
}

canonical_decompiled_code:
__int64 __fastcall func10(_QWORD **a1)
{
_QWORD *v1; // rax

v1 = *a1;
if ( !*a1 )
{
puts("There is no element in the queue ");
exit(-1);
}
if ( v1 == (_QWORD *)*v1 )
{
temp = (__int64)*a1;
*a1 = 0LL;
}
else
{
temp = (__int64)*a1;
*(_QWORD *)(*v1 + 8LL) = v1[1];
*(_QWORD *)(*a1)[1] = **a1;
*a1 = (_QWORD *)**a1;
}
return temp;
}

original_code:
TCB_t* deleteQ(TCB_t **q)
{
  if (*q == ((void *)0))
 {
   printf("There is no element in the queue \n");
   exit(-1);
 }
 else if ((*q)-> next == *q)
 {
          temp = *q;
  *q = ((void *)0);
 }
 else
 {
  temp = *q;
  ((*q)->next)->prev = (*q)->prev;
  ((*q)->prev)->next = (*q)->next;
  *q = (*q)->next;
 }
 return temp;
}

canonical_original_code:
struct TCB_t *deleteQ(struct TCB_t **q)
{
  if (*q == ((void *)0))
 {
   printf("There is no element in the queue \n");
   exit(-1);
 }
 else if ((*q)-> next == *q)
 {
          temp = *q;
  *q = ((void *)0);
 }
 else
 {
  temp = *q;
  ((*q)->next)->prev = (*q)->prev;
  ((*q)->prev)->next = (*q)->next;
  *q = (*q)->next;
 }
 return temp;
}

variable_types:
q
struct TCB_t **

return_type: struct TCB_t { struct TCB_t *next; struct TCB_t *prev; struct ucontext_t { unsigned long uc_flags; struct ucontext_t *uc_link; struct stack_t { void *ss_sp; int ss_flags; unsigned long ss_size; } uc_stack; struct mcontext_t { long long gregs[23]; struct _libc_fpstate { unsigned short cwd; unsigned short swd; unsigned short ftw; unsigned short fop; unsigned long rip; unsigned long rdp; unsigned int mxcsr; unsigned int mxcr_mask; struct _libc_fpxreg { unsigned short significand[4]; unsigned short exponent; unsigned short __glibc_reserved1[3]; } _st[8]; struct _libc_xmmreg { unsigned int element[4]; } _xmm[16]; unsigned int __glibc_reserved1[24]; } *fpregs; unsigned long long __reserved1[8]; } uc_mcontext; struct __sigset_t { unsigned long __val[-1]; } uc_sigmask; struct _libc_fpstate __fpregs_mem; unsigned long long __ssp[4]; } context; } *


run

decompiled_code:
int run()
{
ucontext_t v1; // [rsp+0h] [rbp-3D8h] BYREF

getcontext(&v1);
return swapcontext(&v1, (const ucontext_t *)(head + 16));
}

canonical_decompiled_code:
int func13()
{
ucontext_t v1; // [rsp+0h] [rbp-3D8h] BYREF

getcontext(&v1);
return swapcontext(&v1, (const ucontext_t *)(head + 16));
}

original_code:
void run()
{
 ucontext_t initial_TCB;
 getcontext(&initial_TCB);
 swapcontext(&initial_TCB, &(head->context));
}

canonical_original_code:
void run()
{
 struct ucontext_t initial_TCB;
 getcontext(&initial_TCB);
 swapcontext(&initial_TCB, &(head->context));
}

variable_types: struct ucontext_t { unsigned long uc_flags; struct ucontext_t *uc_link; struct stack_t { void *ss_sp; int ss_flags; unsigned long ss_size; } uc_stack; struct mcontext_t { long long gregs[23]; struct _libc_fpstate { unsigned short cwd; unsigned short swd; unsigned short ftw; unsigned short fop; unsigned long rip; unsigned long rdp; unsigned int mxcsr; unsigned int mxcr_mask; struct _libc_fpxreg { unsigned short significand[4]; unsigned short exponent; unsigned short __glibc_reserved1[3]; } _st[8]; struct _libc_xmmreg { unsigned int element[4]; } _xmm[16]; unsigned int __glibc_reserved1[24]; } *fpregs; unsigned long long __reserved1[8]; } uc_mcontext; struct __sigset_t { unsigned long __val[-1]; } uc_sigmask; struct _libc_fpstate __fpregs_mem; unsigned long long __ssp[4]; }


yield

decompiled_code:
int yield()
{
__int64 v0; // rbx

v0 = head;
rotateQ(&head);
return swapcontext((ucontext_t *)(v0 + 16), (const ucontext_t *)(head + 16));
}

canonical_decompiled_code:
int func14()
{
__int64 v0; // rbx

v0 = head;
func11(&head);
return swapcontext((ucontext_t *)(v0 + 16), (const ucontext_t *)(head + 16));
}

original_code:
void yield()
{
     TCB_t *prev;
 prev=head;
 rotateQ(&head);
 swapcontext(&(prev->context),&(head->context));
}

canonical_original_code:
void yield()
{
     struct TCB_t *prev;
 prev=head;
 rotateQ(&head);
 swapcontext(&(prev->context),&(head->context));
}

user_defined_types: struct stack_t { void *ss_sp; int ss_flags; unsigned long ss_size; }

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

user_defined_types: struct _libc_fpxreg { unsigned short significand[4]; unsigned short exponent; unsigned short __glibc_reserved1[3]; }

user_defined_types: struct _libc_xmmreg { unsigned int element[4]; }


P

decompiled_code:
int __fastcall P(__int64 a1)
{
bool v1; // sf
int result; // eax
__int64 v3; // rbp

v1 = *(_DWORD *)a1 - 1 < 0;
result = *(_DWORD *)a1 - 1;
*(_DWORD *)a1 = result;
if ( v1 )
{
v3 = deleteQ((_QWORD **)&head);
addQ((__int64 *)(a1 + 8), (_QWORD *)v3);
result = swapcontext((ucontext_t *)(v3 + 16), (const ucontext_t *)(head + 16));
}
return result;
}

canonical_decompiled_code:
int __fastcall func15(__int64 a1)
{
bool v1; // sf
int result; // eax
__int64 v3; // rbp

v1 = *(_DWORD *)a1 - 1 < 0;
result = *(_DWORD *)a1 - 1;
*(_DWORD *)a1 = result;
if ( v1 )
{
v3 = func10((_QWORD **)&head);
func9((__int64 *)(a1 + 8), (_QWORD *)v3);
result = swapcontext((ucontext_t *)(v3 + 16), (const ucontext_t *)(head + 16));
}
return result;
}

original_code:
void P(Semaphore *sem)
{
 TCB_t *blockProcess = 0, *nextProcess = 0;
 sem->value--;
 if(sem->value < 0)
 {
  blockProcess = (TCB_t *)deleteQ(&head);
  addQ(&(sem->waitQ), blockProcess);
  nextProcess = head;
  swapcontext(&(blockProcess->context), &(nextProcess->context));
 }
}

canonical_original_code:
void P(struct Semaphore *sem)
{
 struct TCB_t *blockProcess = 0; struct TCB_t *nextProcess = 0;
 sem->value--;
 if(sem->value < 0)
 {
  blockProcess = (struct TCB_t *)deleteQ(&head);
  addQ(&(sem->waitQ), blockProcess);
  nextProcess = head;
  swapcontext(&(blockProcess->context), &(nextProcess->context));
 }
}

variable_types: struct Semaphore { int value; struct TCB_t { struct TCB_t *next; struct TCB_t *prev; struct ucontext_t { unsigned long uc_flags; struct ucontext_t *uc_link; struct stack_t { void *ss_sp; int ss_flags; unsigned long ss_size; } uc_stack; struct mcontext_t { long long gregs[23]; struct _libc_fpstate { unsigned short cwd; unsigned short swd; unsigned short ftw; unsigned short fop; unsigned long rip; unsigned long rdp; unsigned int mxcsr; unsigned int mxcr_mask; struct _libc_fpxreg { unsigned short significand[4]; unsigned short exponent; unsigned short __glibc_reserved1[3]; } _st[8]; struct _libc_xmmreg { unsigned int element[4]; } _xmm[16]; unsigned int __glibc_reserved1[24]; } *fpregs; unsigned long long __reserved1[8]; } uc_mcontext; struct __sigset_t { unsigned long __val[-1]; } uc_sigmask; struct _libc_fpstate __fpregs_mem; unsigned long long __ssp[4]; } context; } *waitQ; } *

user_defined_types: struct Semaphore { int value; struct TCB_t *waitQ; }


V

decompiled_code:
int __fastcall V(__int64 a1)
{
int v1; // eax
_QWORD *v3; // rax

v1 = *(_DWORD *)a1 + 1;
*(_DWORD *)a1 = v1;
if ( v1 <= 0 )
{
v3 = (_QWORD *)deleteQ((_QWORD **)(a1 + 8));
addQ(&head, v3);
}
return yield();
}

canonical_decompiled_code:
int __fastcall func16(__int64 a1)
{
int v1; // eax
_QWORD *v3; // rax

v1 = *(_DWORD *)a1 + 1;
*(_DWORD *)a1 = v1;
if ( v1 <= 0 )
{
v3 = (_QWORD *)func10((_QWORD **)(a1 + 8));
func9(&head, v3);
}
return func14();
}

original_code:
void V(Semaphore *sem)
{
 TCB_t *unblockProcess;
 sem->value++;
 if(sem->value <=0)
 {
  unblockProcess = (TCB_t *)deleteQ(&(sem->waitQ));
  addQ(&head, unblockProcess);
 }
 yield();
}

canonical_original_code:
void V(struct Semaphore *sem)
{
 struct TCB_t *unblockProcess;
 sem->value++;
 if(sem->value <=0)
 {
  unblockProcess = (struct TCB_t *)deleteQ(&(sem->waitQ));
  addQ(&head, unblockProcess);
 }
 yield();
}


decompiled_code:
int __fastcall reader_entry(unsigned int a1)
{
int result; // eax

printf("[reader: #%d]\tpreparing to read\n", a1);
P(mutex);
if ( wwc > 0 || wc > 0 )
{
printf("[reader: #%d]\tblocking for writer\n", a1);
++rwc;
V(mutex);
P(r_sem);
--rwc;
}
++rc;
if ( rwc <= 0 )
result = V(mutex);
else
result = V(r_sem);
return result;
}

canonical_decompiled_code:
int __fastcall func17(unsigned int a1)
{
int result; // eax

printf("[reader: #%d]\tpreparing to read\n", a1);
func15(mutex);
if ( wwc > 0 || wc > 0 )
{
printf("[reader: #%d]\tblocking for writer\n", a1);
++rwc;
func16(mutex);
func15(r_sem);
--rwc;
}
++rc;
if ( rwc <= 0 )
result = func16(mutex);
else
result = func16(r_sem);
return result;
}

original_code:
void reader_entry(int ID)
{
 printf("[reader: #%d]\tpreparing to read\n", ID);
 P(mutex);
 if (wwc > 0 || wc > 0) {
     printf("[reader: #%d]\tblocking for writer\n", ID);
     rwc++;
     V(mutex);
     P(r_sem);
     rwc--;
 }
 rc++;
 if (rwc > 0)
     V(r_sem);
 else
     V(mutex);
}

variable_types: ID int


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

P(mutex);
if ( --rc || wwc <= 0 )
result = V(mutex);
else
result = V(w_sem);
return result;
}

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

func15(mutex);
if ( --rc || wwc <= 0 )
result = func16(mutex);
else
result = func16(w_sem);
return result;
}

original_code:
void reader_exit(int ID)
{
 P(mutex);
 rc--;
 if (rc == 0 && wwc > 0) {
     V(w_sem);
 } else
     V(mutex);
}


reader

decompiled_code:
void __noreturn reader()
{
unsigned int v0; // ebx

P(mutex);
v0 = global_ID++;
V(mutex);
while ( 1 )
{
reader_entry(v0);
printf("[reader #%d]\t****READING****\n", v0);
sleep(1u);
reader_exit();
}
}

canonical_decompiled_code:
void __noreturn func19()
{
unsigned int v0; // ebx

func15(mutex);
v0 = global_ID++;
func16(mutex);
while ( 1 )
{
func17(v0);
printf("[reader #%d]\t****READING****\n", v0);
sleep(1u);
func18();
}
}

original_code:
void reader(void)
{
  int ID;
  P(mutex); ID = global_ID++; V(mutex);
  while(1){
 reader_entry(ID);
 printf("[reader #%d]\t****READING****\n", ID);
 sleep(1);
 reader_exit(ID);
  };
}

canonical_original_code:
void reader()
{
  int ID;
  P(mutex); ID = global_ID++; V(mutex);
  while(1){
 reader_entry(ID);
 printf("[reader #%d]\t****READING****\n", ID);
 sleep(1);
 reader_exit(ID);
  };
}


decompiled_code:
int __fastcall writer_entry(unsigned int a1)
{
printf("\t\t\t\t[writer: #%d]\tpreparing to write\n", a1);
P(mutex);
if ( rc > 0 || wc > 0 )
{
printf("\t\t\t\t[writer: #%d] blocking for others\n", a1);
++wwc;
V(mutex);
P(w_sem);
--wwc;
}
++wc;
return V(mutex);
}

canonical_decompiled_code:
int __fastcall func20(unsigned int a1)
{
printf("\t\t\t\t[writer: #%d]\tpreparing to write\n", a1);
func15(mutex);
if ( rc > 0 || wc > 0 )
{
printf("\t\t\t\t[writer: #%d] blocking for others\n", a1);
++wwc;
func16(mutex);
func15(w_sem);
--wwc;
}
++wc;
return func16(mutex);
}

original_code:
void writer_entry(int ID)
{
 printf("\t\t\t\t[writer: #%d]\tpreparing to write\n", ID);
 P(mutex);
 if (rc > 0 || wc > 0) {
     printf("\t\t\t\t[writer: #%d] blocking for others\n",ID);
     wwc++;
     V(mutex);
     P(w_sem);
     wwc--;
 }
 wc++;
 V(mutex);
}


decompiled_code:
int writer_exit()
{
P(mutex);
--wc;
if ( rwc > 0 )
return V(r_sem);
if ( wwc <= 0 )
return V(mutex);
return V(w_sem);
}

canonical_decompiled_code:
int func21()
{
func15(mutex);
--wc;
if ( rwc > 0 )
return func16(r_sem);
if ( wwc <= 0 )
return func16(mutex);
return func16(w_sem);
}

original_code:
void writer_exit(int ID)
{
 P(mutex);
 wc--;
 if (rwc > 0) {
     V(r_sem);
 } else if (wwc > 0) {
     V(w_sem);
 } else {
     V(mutex);
 }
}


writer

decompiled_code:
void __noreturn writer()
{
unsigned int v0; // ebx

P(mutex);
v0 = global_ID++;
V(mutex);
while ( 1 )
{
writer_entry(v0);
printf("\t\t\t\t[writer: #%d]\t&&&WRITING!&&&\n", v0);
sleep(1u);
writer_exit();
}
}

canonical_decompiled_code:
void __noreturn func22()
{
unsigned int v0; // ebx

func15(mutex);
v0 = global_ID++;
func16(mutex);
while ( 1 )
{
func20(v0);
printf("\t\t\t\t[writer: #%d]\t&&&WRITING!&&&\n", v0);
sleep(1u);
func21();
}
}

original_code:
void writer(void)
{
  int ID;
  P(mutex); ID = global_ID++; V(mutex);
  while(1){
  writer_entry(ID);
 printf("\t\t\t\t[writer: #%d]\t&&&WRITING!&&&\n", ID);
 sleep(1);
 writer_exit(ID);
  };
}

canonical_original_code:
void writer()
{
  int ID;
  P(mutex); ID = global_ID++; V(mutex);
  while(1){
  writer_entry(ID);
 printf("\t\t\t\t[writer: #%d]\t&&&WRITING!&&&\n", ID);
 sleep(1);
 writer_exit(ID);
  };
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
mutex = (__int64)createSem(1);
r_sem = (__int64)createSem(0);
w_sem = (__int64)createSem(0);
start_thread((void (*)(void))reader);
start_thread((void (*)(void))reader);
start_thread((void (*)(void))reader);
start_thread((void (*)(void))reader);
start_thread((void (*)(void))writer);
start_thread((void (*)(void))writer);
run();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
mutex = (__int64)createSem(1);
r_sem = (__int64)createSem(0);
w_sem = (__int64)createSem(0);
func12((void (*)(void))reader);
func12((void (*)(void))reader);
func12((void (*)(void))reader);
func12((void (*)(void))reader);
func12((void (*)(void))writer);
func12((void (*)(void))writer);
func13();
return 0;
}

original_code:
int main()
{
    mutex = createSem(1);
    r_sem = createSem(0);
    w_sem = createSem(0);
    start_thread(reader);
    start_thread(reader);
    start_thread(reader);
    start_thread(reader);
    start_thread(writer);
    start_thread(writer);
    run();
}


repo: ruijiege/Multi-thread-Web-Server

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__pid_t v3; // eax
_DWORD *v4; // rax
__int64 v5; // rax
int v6; // ebx
time_t timer; // [rsp+8h] [rbp-420h] BYREF
char buf[1048]; // [rsp+10h] [rbp-418h] BYREF

puts("Starting server...");
getOptions((unsigned int)argc, argv);
if ( debugMode )
{
LABEL_6:
network();
initializeRequestQueue();
mallocThreadpool();
initializeMutex();
createThreadpool();
initializeSchedulingThread();
puts("Welcome!! myhttps server is started!!!");
while ( 1 )
{
lengthOfAddress = 16;
clientSocketDescr = accept(serverSocketDescr, &addressOfClient, &lengthOfAddress);
if ( !clientSocketDescr )
{
puts("failed to create the socket..");
exit(0);
}
puts("New request from client..");
recv(clientSocketDescr, buf, 0x400uLL, 0);
pthread_mutex_lock(&mutexOfReadyQ);
v5 = 0LL;
while ( 1 )
{
v6 = v5;
if ( !*(_DWORD *)queueOfRequests[v5] )
break;
if ( ++v5 == 50 )
goto LABEL_9;
}
v4 = (_DWORD *)parseRequest(buf);
queueOfRequests[v6] = (__int64)v4;
*v4 = clientSocketDescr;
time(&timer);
*(_QWORD *)(queueOfRequests[v6] + 24) = timer;
sem_post(&cnt);
LABEL_9:
pthread_mutex_unlock(&mutexOfReadyQ);
}
}
v3 = fork();
if ( v3 >= 0 )
{
if ( v3 )
exit(0);
setsid();
chdir(directoryName);
umask(0);
goto LABEL_6;
}
return -1;
}

original_code:
int main(int argc, char **argv)
{
 printf("Starting server...\n");
 getOptions(argc, argv);
 if (debugMode == 0)
 {
  pid_t processID;
  if ((processID = fork()) < 0)
   return (-1);
  else if (processID != 0)
   exit(0);
  setsid();
  chdir(directoryName);
  umask(0);
 }
 network();
 initializeRequestQueue();
 mallocThreadpool();
 initializeMutex();
 createThreadpool();
 initializeSchedulingThread();
 printf("Welcome!! myhttps server is started!!!\n");
 char inputRequestBuffer[1024];
 time_t tm;
 while (1)
 {
  lengthOfAddress = sizeof(addressOfClient);
  clientSocketDescr = accept(serverSocketDescr, (struct sockaddr *) &addressOfClient, &lengthOfAddress);
  if (clientSocketDescr == 0)
  {
   printf("failed to create the socket..\n");
   exit(0);
  }
  else
  {
   int j;
   printf("New request from client..\n");
   recv(clientSocketDescr, inputRequestBuffer, 1024, 0);
   pthread_mutex_lock(&mutexOfReadyQ);
   for (j = 0; j < 50; j++)
   {
    if (queueOfRequests[j]->reqNumber == 0)
    {
     queueOfRequests[j] = parseRequest(inputRequestBuffer);
     queueOfRequests[j]->reqNumber = clientSocketDescr;
     time(&tm);
     queueOfRequests[j]->timeOfRequest = tm;
     sem_post(&cnt);
     break;
    }
   }
   pthread_mutex_unlock(&mutexOfReadyQ);
  }
 }
}

canonical_original_code:
int main(int argc, char **argv)
{
 printf("Starting server...\n");
 getOptions(argc, argv);
 if (debugMode == 0)
 {
  int processID;
  if ((processID = fork()) < 0)
   return (-1);
  else if (processID != 0)
   exit(0);
  setsid();
  chdir(directoryName);
  umask(0);
 }
 network();
 initializeRequestQueue();
 mallocThreadpool();
 initializeMutex();
 createThreadpool();
 initializeSchedulingThread();
 printf("Welcome!! myhttps server is started!!!\n");
 char inputRequestBuffer[1024];
 long tm;
 while (1)
 {
  lengthOfAddress = sizeof(addressOfClient);
  clientSocketDescr = accept(serverSocketDescr, (struct sockaddr *) &addressOfClient, &lengthOfAddress);
  if (clientSocketDescr == 0)
  {
   printf("failed to create the socket..\n");
   exit(0);
  }
  else
  {
   int j;
   printf("New request from client..\n");
   recv(clientSocketDescr, inputRequestBuffer, 1024, 0);
   pthread_mutex_lock(&mutexOfReadyQ);
   for (j = 0; j < 50; j++)
   {
    if (queueOfRequests[j]->reqNumber == 0)
    {
     queueOfRequests[j] = parseRequest(inputRequestBuffer);
     queueOfRequests[j]->reqNumber = clientSocketDescr;
     time(&tm);
     queueOfRequests[j]->timeOfRequest = tm;
     sem_post(&cnt);
     break;
    }
   }
   pthread_mutex_unlock(&mutexOfReadyQ);
  }
 }
}

variable_types: processID int

variable_types:
inputRequestBuffer
char [1024]

variable_types: tm long

variable_types: j int

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


getOptions

decompiled_code:
int __fastcall getOptions(int argc, char **argv)
{
int v2; // eax
int result; // eax
char *v4; // rbx
char *v5; // rbx

opterr = 0;
while ( 1 )
{
v2 = getopt(argc, argv, "l:p:r:t:n:s:dh");
if ( v2 == -1 )
break;
switch ( v2 )
{
case 'd':
debugMode = 1;
break;
case 'h':
puts(
"-d\t:Enter debugging mode. That is, do not daemonize, only accept one connection at a time and \t\t\t\t\t\tena"
"ble logging to stdout.");
puts("-h\t:Print a usage summary with all options and exit.");
puts("-l file\t:Log all requests to the given file.");
puts("-p port\t:Listen on the given port. If not provided, myhttpd will listen on port 8080.");
puts("-r dir\t:Set the root directory for the http server to dir");
puts("-t time\t:Set the queuing time to time seconds. The default should be 60 seconds");
puts(
"-n threadnum\t:Set number of threads waiting ready in the execution thread pool to threadnum. \t\t\t\t\t\tThe "
"default should be 4 execution threads.");
puts("-s sched\t:Set the scheduling policy. It can be either FCFS or SJF. The default will be \t\t\t\t\t\tFCFS.");
exit(1);
case 'l':
logFilename = (__int64)malloc(8uLL);
strcpy((char *)logFilename, optarg);
isLog = 1;
break;
case 'n':
numberOfThreads = atoi(optarg);
break;
case 'p':
portNumber = atoi(optarg);
break;
case 'r':
directoryName = (__int64)malloc(8uLL);
strcpy((char *)directoryName, optarg);
break;
case 's':
schedAlgo = (int)optarg;
break;
case 't':
timeOfWait = atoi(optarg);
break;
default:
printf("Please enter correct options...");
break;
}
}
if ( debugMode == 1 )
{
puts("Mode: Debugging mode");
printf("Port: %d\n", (unsigned int)portNumber);
if ( directoryName )
{
printf("Directory: %s\n", (const char *)directoryName);
}
else
{
getcwd(dircLocation, 0x64uLL);
v4 = (char *)malloc(0x64uLL);
directoryName = (__int64)v4;
strcpy(v4, dircLocation);
printf("Directory: %s\n", v4);
}
result = printf("Queueing time: %d seconds\n", (unsigned int)timeOfWait);
numberOfThreads = 1;
}
else
{
if ( isLog == 1 )
printf("Log filename: %s\n", (const char *)logFilename);
printf("Port: %d\n", (unsigned int)portNumber);
if ( directoryName )
{
printf("Directory: %s\n", (const char *)directoryName);
}
else
{
getcwd(dircLocation, 0x64uLL);
v5 = (char *)malloc(0x64uLL);
directoryName = (__int64)v5;
strcpy(v5, dircLocation);
printf("Directory: %s\n", v5);
}
printf("Queueing time:%d seconds\n", (unsigned int)timeOfWait);
printf("Number of execution threads: %d\n", (unsigned int)numberOfThreads);
if ( schedAlgo )
result = puts("Scheduling policy: SJF");
else
result = puts("Scheduling policy: FCFS");
}
return result;
}

canonical_decompiled_code:
int __fastcall func0(int argc, char **argv)
{
int v2; // eax
int result; // eax
char *v4; // rbx
char *v5; // rbx

opterr = 0;
while ( 1 )
{
v2 = getopt(argc, argv, "l:p:r:t:n:s:dh");
if ( v2 == -1 )
break;
switch ( v2 )
{
case 'd':
debugMode = 1;
break;
case 'h':
puts(
"-d\t:Enter debugging mode. That is, do not daemonize, only accept one connection at a time and \t\t\t\t\t\tena"
"ble logging to stdout.");
puts("-h\t:Print a usage summary with all options and exit.");
puts("-l file\t:Log all requests to the given file.");
puts("-p port\t:Listen on the given port. If not provided, myhttpd will listen on port 8080.");
puts("-r dir\t:Set the root directory for the http server to dir");
puts("-t time\t:Set the queuing time to time seconds. The default should be 60 seconds");
puts(
"-n threadnum\t:Set number of threads waiting ready in the execution thread pool to threadnum. \t\t\t\t\t\tThe "
"default should be 4 execution threads.");
puts("-s sched\t:Set the scheduling policy. It can be either FCFS or SJF. The default will be \t\t\t\t\t\tFCFS.");
exit(1);
case 'l':
logFilename = (__int64)malloc(8uLL);
strcpy((char *)logFilename, optarg);
isLog = 1;
break;
case 'n':
numberOfThreads = atoi(optarg);
break;
case 'p':
portNumber = atoi(optarg);
break;
case 'r':
directoryName = (__int64)malloc(8uLL);
strcpy((char *)directoryName, optarg);
break;
case 's':
schedAlgo = (int)optarg;
break;
case 't':
timeOfWait = atoi(optarg);
break;
default:
printf("Please enter correct options...");
break;
}
}
if ( debugMode == 1 )
{
puts("Mode: Debugging mode");
printf("Port: %d\n", (unsigned int)portNumber);
if ( directoryName )
{
printf("Directory: %s\n", (const char *)directoryName);
}
else
{
getcwd(dircLocation, 0x64uLL);
v4 = (char *)malloc(0x64uLL);
directoryName = (__int64)v4;
strcpy(v4, dircLocation);
printf("Directory: %s\n", v4);
}
result = printf("Queueing time: %d seconds\n", (unsigned int)timeOfWait);
numberOfThreads = 1;
}
else
{
if ( isLog == 1 )
printf("Log filename: %s\n", (const char *)logFilename);
printf("Port: %d\n", (unsigned int)portNumber);
if ( directoryName )
{
printf("Directory: %s\n", (const char *)directoryName);
}
else
{
getcwd(dircLocation, 0x64uLL);
v5 = (char *)malloc(0x64uLL);
directoryName = (__int64)v5;
strcpy(v5, dircLocation);
printf("Directory: %s\n", v5);
}
printf("Queueing time:%d seconds\n", (unsigned int)timeOfWait);
printf("Number of execution threads: %d\n", (unsigned int)numberOfThreads);
if ( schedAlgo )
result = puts("Scheduling policy: SJF");
else
result = puts("Scheduling policy: FCFS");
}
return result;
}

original_code:
void getOptions(int argc, char **argv)
{
 int choice;
 opterr = 0;
  while ((choice = getopt(argc, argv, "l:p:r:t:n:s:dh")) != -1)
  {
   switch (choice)
   {
    case 'd':
     debugMode = 1;
     break;
    case 'h':
     printf("-d\t:Enter debugging mode. That is, do not daemonize, only accept one connection at a time and 						enable logging to stdout.\n");
     printf("-h\t:Print a usage summary with all options and exit.\n");
     printf("-l file\t:Log all requests to the given file.\n");
     printf("-p port\t:Listen on the given port. If not provided, myhttpd will listen on port 8080.\n");
     printf("-r dir\t:Set the root directory for the http server to dir\n");
     printf("-t time\t:Set the queuing time to time seconds. The default should be 60 seconds\n");
     printf("-n threadnum\t:Set number of threads waiting ready in the execution thread pool to threadnum. 						The default should be 4 execution threads.\n");
     printf("-s sched\t:Set the scheduling policy. It can be either FCFS or SJF. The default will be 						FCFS.\n");
     exit(1);
     break;
    case 'l':
     logFilename = (char *) malloc(sizeof optarg);
     strcpy(logFilename, optarg);
     isLog = 1;
     break;
    case 'p':
     portNumber = atoi(optarg);
     break;
    case 'r':
     directoryName = (char *) malloc(sizeof optarg);
     strcpy(directoryName, optarg);
     break;
    case 't':
     timeOfWait = atoi(optarg);
     break;
    case 'n':
     numberOfThreads = atoi(optarg);
     break;
    case 's':
     schedAlgo = optarg;
     break;
    default:
     printf("Please enter correct options...");
   }
  }
 if (debugMode == 1)
 {
  printf("Mode: Debugging mode\n");
  printf("Port: %d\n", portNumber);
  if (directoryName != ((void *)0))
  {
   printf("Directory: %s\n", directoryName);
  }
         else
  {
   getcwd(dircLocation, sizeof(dircLocation));
   directoryName = (char *) malloc(sizeof dircLocation);
   strcpy(directoryName, dircLocation);
   printf("Directory: %s\n", directoryName);
  }
  printf("Queueing time: %d seconds\n", timeOfWait);
  numberOfThreads = 1;
 }
     else
     {
  if (isLog == 1)
  {
   printf("Log filename: %s\n", logFilename);
  }
  printf("Port: %d\n", portNumber);
  if (directoryName != ((void *)0))
  {
   printf("Directory: %s\n", directoryName);
  }
  else
  {
   getcwd(dircLocation, sizeof(dircLocation));
   directoryName = (char *) malloc(sizeof dircLocation);
   strcpy(directoryName, dircLocation);
   printf("Directory: %s\n", directoryName);
  }
  printf("Queueing time:%d seconds\n", timeOfWait);
  printf("Number of execution threads: %d\n", numberOfThreads);
  if (schedAlgo == 0)
  {
   printf("Scheduling policy: FCFS\n");
  }
  else
  {
   printf("Scheduling policy: SJF\n");
  }
 }
}

variable_types: argc int

variable_types:
argv
char **

variable_types: choice int


scheduleRequestUsingFCFS

decompiled_code:
void __fastcall __noreturn scheduleRequestUsingFCFS(void *a1)
{
__int64 v1; // r12
__int64 *v2; // rax
_DWORD *v3; // rdx
__int64 v4; // rbp
__int64 v5; // rbx
__int64 v6; // rsi
time_t timer; // [rsp+8h] [rbp-20h] BYREF

sleep(timeOfWait);
while ( 1 )
{
sem_wait(&cnt);
pthread_mutex_lock(&mutexOfReadyQ);
v1 = queueOfRequests;
v2 = &queueOfRequests;
do
{
v3 = (_DWORD *)v2[1];
*v2++ = (__int64)v3;
}
while ( *v3 );
pthread_mutex_unlock(&mutexOfReadyQ);
sem_wait(&waitingThreading);
pthread_mutex_lock(&mutexOfQueue);
if ( (int)numberOfThreads > 0 )
{
v4 = 0LL;
v5 = 0LL;
while ( 1 )
{
v6 = readyQThreads + v4;
if ( *(_DWORD *)(readyQThreads + v4 + 16) == 1 )
break;
++v5;
v4 += 24LL;
if ( v5 == numberOfThreads )
goto LABEL_3;
}
*(_QWORD *)(v6 + 8) = v1;
*(_DWORD *)(v6 + 16) = 0;
printf("file %sl is assigned to thread %d\n", *(const char **)(v1 + 8), (unsigned int)(v5 + 1));
time(&timer);
*(_QWORD *)(*(_QWORD *)(readyQThreads + v4 + 8) + 32LL) = timer;
sem_post((sem_t *)(wThreads + 32 * v5));
}
LABEL_3:
pthread_mutex_unlock(&mutexOfQueue);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func0(void *a1)
{
__int64 v1; // r12
__int64 *v2; // rax
_DWORD *v3; // rdx
__int64 v4; // rbp
__int64 v5; // rbx
__int64 v6; // rsi
time_t timer; // [rsp+8h] [rbp-20h] BYREF

sleep(timeOfWait);
while ( 1 )
{
sem_wait(&cnt);
pthread_mutex_lock(&mutexOfReadyQ);
v1 = queueOfRequests;
v2 = &queueOfRequests;
do
{
v3 = (_DWORD *)v2[1];
*v2++ = (__int64)v3;
}
while ( *v3 );
pthread_mutex_unlock(&mutexOfReadyQ);
sem_wait(&waitingThreading);
pthread_mutex_lock(&mutexOfQueue);
if ( (int)numberOfThreads > 0 )
{
v4 = 0LL;
v5 = 0LL;
while ( 1 )
{
v6 = readyQThreads + v4;
if ( *(_DWORD *)(readyQThreads + v4 + 16) == 1 )
break;
++v5;
v4 += 24LL;
if ( v5 == numberOfThreads )
goto LABEL_3;
}
*(_QWORD *)(v6 + 8) = v1;
*(_DWORD *)(v6 + 16) = 0;
printf("file %sl is assigned to thread %d\n", *(const char **)(v1 + 8), (unsigned int)(v5 + 1));
time(&timer);
*(_QWORD *)(*(_QWORD *)(readyQThreads + v4 + 8) + 32LL) = timer;
sem_post((sem_t *)(wThreads + 32 * v5));
}
LABEL_3:
pthread_mutex_unlock(&mutexOfQueue);
}
}

original_code:
void* scheduleRequestUsingFCFS(void* ptr)
{
 sleep(timeOfWait);
 while (1)
 {
  int i, j;
  sem_wait(&cnt);
  pthread_mutex_lock(&mutexOfReadyQ);
  struct queueRequest* userRequest = queueOfRequests[0];
  for (j = 0; j < 50; j++)
  {
   queueOfRequests[j] = queueOfRequests[j + 1];
   if (queueOfRequests[j + 1]->reqNumber == 0)
   {
    break;
   }
  }
  pthread_mutex_unlock(&mutexOfReadyQ);
  sem_wait(&waitingThreading);
  pthread_mutex_lock(&mutexOfQueue);
  time_t assignedTime;
  for (i = 0; i < numberOfThreads; i++)
  {
   if ((readyQThreads + i)->isSched == 1)
   {
    (readyQThreads + i)->queueRequestptr = userRequest;
    (readyQThreads + i)->isSched = 0;
    printf("file %sl is assigned to thread %d\n", (readyQThreads + i)->queueRequestptr->locationOfRequest, i + 1);
    time(&assignedTime);
    (readyQThreads + i)->queueRequestptr->timeOfExec = assignedTime;
    sem_post(&wThreads[i]);
    break;
   }
  }
  pthread_mutex_unlock(&mutexOfQueue);
 }
}

canonical_original_code:
void *scheduleRequestUsingFCFS(void *ptr)
{
 sleep(timeOfWait);
 while (1)
 {
  int i; int j;
  sem_wait(&cnt);
  pthread_mutex_lock(&mutexOfReadyQ);
  struct queueRequest *userRequest = queueOfRequests[0];
  for (j = 0; j < 50; j++)
  {
   queueOfRequests[j] = queueOfRequests[j + 1];
   if (queueOfRequests[j + 1]->reqNumber == 0)
   {
    break;
   }
  }
  pthread_mutex_unlock(&mutexOfReadyQ);
  sem_wait(&waitingThreading);
  pthread_mutex_lock(&mutexOfQueue);
  long assignedTime;
  for (i = 0; i < numberOfThreads; i++)
  {
   if ((readyQThreads + i)->isSched == 1)
   {
    (readyQThreads + i)->queueRequestptr = userRequest;
    (readyQThreads + i)->isSched = 0;
    printf("file %sl is assigned to thread %d\n", (readyQThreads + i)->queueRequestptr->locationOfRequest, i + 1);
    time(&assignedTime);
    (readyQThreads + i)->queueRequestptr->timeOfExec = assignedTime;
    sem_post(&wThreads[i]);
    break;
   }
  }
  pthread_mutex_unlock(&mutexOfQueue);
 }
}

variable_types:
ptr
void *

variable_types: i int

variable_types: struct queueRequest { int reqNumber; int typeOfRequest; char *locationOfRequest; int len; long timeOfRequest; long timeOfExec; } *

variable_types: assignedTime long

return_type: void *


scheduleRequestUsingSJF

decompiled_code:
void __fastcall __noreturn scheduleRequestUsingSJF(void *a1)
{
__int64 v1; // rax
_DWORD *v2; // rdx
__int64 v3; // rbp
__int64 v4; // r13
__int64 v5; // r14
__int64 v6; // rbx
__int64 v7; // rsi
_DWORD *v8; // rax
time_t timer; // [rsp+8h] [rbp-30h] BYREF

sleep(timeOfWait);
while ( 1 )
{
sem_wait(&cnt);
pthread_mutex_lock(&mutexOfReadyQ);
v4 = queueOfRequests[0];
v1 = 1LL;
LODWORD(v3) = 0;
do
{
v2 = (_DWORD *)queueOfRequests[v1];
if ( !*v2 )
break;
if ( v2[4] < *(_DWORD *)(v4 + 16) )
{
LODWORD(v3) = v1;
v4 = queueOfRequests[v1];
}
++v1;
}
while ( v1 != 50 );
pthread_mutex_unlock(&mutexOfReadyQ);
sem_wait(&waitingThreading);
pthread_mutex_lock(&mutexOfQueue);
if ( (int)numberOfThreads > 0 )
{
v5 = 0LL;
v6 = 0LL;
while ( 1 )
{
v7 = readyQThreads + v5;
if ( *(_DWORD *)(readyQThreads + v5 + 16) == 1 )
break;
++v6;
v5 += 24LL;
if ( v6 == numberOfThreads )
goto LABEL_12;
}
*(_QWORD *)(v7 + 8) = v4;
*(_DWORD *)(v7 + 16) = 0;
printf("file %s is assigned to thread %d\n", *(const char **)(v4 + 8), (unsigned int)(v6 + 1));
time(&timer);
*(_QWORD *)(*(_QWORD *)(readyQThreads + v5 + 8) + 32LL) = timer;
sem_post((sem_t *)(wThreads + 32 * v6));
}
LABEL_12:
pthread_mutex_unlock(&mutexOfQueue);
pthread_mutex_lock(&mutexOfReadyQ);
v3 = (int)v3;
do
{
if ( (int)v3 > 49 )
break;
v8 = (_DWORD *)queueOfRequests[v3 + 1];
queueOfRequests[v3++] = (__int64)v8;
}
while ( *v8 );
pthread_mutex_unlock(&mutexOfReadyQ);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func1(void *a1)
{
__int64 v1; // rax
_DWORD *v2; // rdx
__int64 v3; // rbp
__int64 v4; // r13
__int64 v5; // r14
__int64 v6; // rbx
__int64 v7; // rsi
_DWORD *v8; // rax
time_t timer; // [rsp+8h] [rbp-30h] BYREF

sleep(timeOfWait);
while ( 1 )
{
sem_wait(&cnt);
pthread_mutex_lock(&mutexOfReadyQ);
v4 = queueOfRequests[0];
v1 = 1LL;
LODWORD(v3) = 0;
do
{
v2 = (_DWORD *)queueOfRequests[v1];
if ( !*v2 )
break;
if ( v2[4] < *(_DWORD *)(v4 + 16) )
{
LODWORD(v3) = v1;
v4 = queueOfRequests[v1];
}
++v1;
}
while ( v1 != 50 );
pthread_mutex_unlock(&mutexOfReadyQ);
sem_wait(&waitingThreading);
pthread_mutex_lock(&mutexOfQueue);
if ( (int)numberOfThreads > 0 )
{
v5 = 0LL;
v6 = 0LL;
while ( 1 )
{
v7 = readyQThreads + v5;
if ( *(_DWORD *)(readyQThreads + v5 + 16) == 1 )
break;
++v6;
v5 += 24LL;
if ( v6 == numberOfThreads )
goto LABEL_12;
}
*(_QWORD *)(v7 + 8) = v4;
*(_DWORD *)(v7 + 16) = 0;
printf("file %s is assigned to thread %d\n", *(const char **)(v4 + 8), (unsigned int)(v6 + 1));
time(&timer);
*(_QWORD *)(*(_QWORD *)(readyQThreads + v5 + 8) + 32LL) = timer;
sem_post((sem_t *)(wThreads + 32 * v6));
}
LABEL_12:
pthread_mutex_unlock(&mutexOfQueue);
pthread_mutex_lock(&mutexOfReadyQ);
v3 = (int)v3;
do
{
if ( (int)v3 > 49 )
break;
v8 = (_DWORD *)queueOfRequests[v3 + 1];
queueOfRequests[v3++] = (__int64)v8;
}
while ( *v8 );
pthread_mutex_unlock(&mutexOfReadyQ);
}
}

original_code:
void* scheduleRequestUsingSJF(void* ptr)
{
 sleep(timeOfWait);
 while (1)
 {
  sem_wait(&cnt);
  pthread_mutex_lock(&mutexOfReadyQ);
  struct queueRequest* userRequest = queueOfRequests[0];
  int i, j = 0;
  for (i = 1; i < 50; i++)
  {
   if (queueOfRequests[i]->reqNumber == 0)
    break;
   if ((queueOfRequests[i]->len) < (userRequest->len))
   {
    userRequest = queueOfRequests[i];
    j = i;
   }
  }
  pthread_mutex_unlock(&mutexOfReadyQ);
  sem_wait(&waitingThreading);
  pthread_mutex_lock(&mutexOfQueue);
  time_t assignedTime;
  int a;
  for (a = 0; a < numberOfThreads; a++)
  {
   if ((readyQThreads + a)->isSched == 1)
   {
    (readyQThreads + a)->queueRequestptr = userRequest;
    (readyQThreads + a)->isSched = 0;
    printf("file %s is assigned to thread %d\n", (readyQThreads + a)->queueRequestptr->locationOfRequest, a + 1);
    time(&assignedTime);
    (readyQThreads + a)->queueRequestptr->timeOfExec = assignedTime;
    sem_post(&wThreads[a]);
    break;
   }
  }
  pthread_mutex_unlock(&mutexOfQueue);
  pthread_mutex_lock(&mutexOfReadyQ);
  int m;
  for (m = j; m < 50; m++)
  {
   queueOfRequests[m] = queueOfRequests[m + 1];
   if (queueOfRequests[m + 1]->reqNumber == 0)
   {
    break;
   }
  }
  pthread_mutex_unlock(&mutexOfReadyQ);
 }
}

canonical_original_code:
void *scheduleRequestUsingSJF(void *ptr)
{
 sleep(timeOfWait);
 while (1)
 {
  sem_wait(&cnt);
  pthread_mutex_lock(&mutexOfReadyQ);
  struct queueRequest *userRequest = queueOfRequests[0];
  int i; int j = 0;
  for (i = 1; i < 50; i++)
  {
   if (queueOfRequests[i]->reqNumber == 0)
    break;
   if ((queueOfRequests[i]->len) < (userRequest->len))
   {
    userRequest = queueOfRequests[i];
    j = i;
   }
  }
  pthread_mutex_unlock(&mutexOfReadyQ);
  sem_wait(&waitingThreading);
  pthread_mutex_lock(&mutexOfQueue);
  long assignedTime;
  int a;
  for (a = 0; a < numberOfThreads; a++)
  {
   if ((readyQThreads + a)->isSched == 1)
   {
    (readyQThreads + a)->queueRequestptr = userRequest;
    (readyQThreads + a)->isSched = 0;
    printf("file %s is assigned to thread %d\n", (readyQThreads + a)->queueRequestptr->locationOfRequest, a + 1);
    time(&assignedTime);
    (readyQThreads + a)->queueRequestptr->timeOfExec = assignedTime;
    sem_post(&wThreads[a]);
    break;
   }
  }
  pthread_mutex_unlock(&mutexOfQueue);
  pthread_mutex_lock(&mutexOfReadyQ);
  int m;
  for (m = j; m < 50; m++)
  {
   queueOfRequests[m] = queueOfRequests[m + 1];
   if (queueOfRequests[m + 1]->reqNumber == 0)
   {
    break;
   }
  }
  pthread_mutex_unlock(&mutexOfReadyQ);
 }
}

variable_types: a int

variable_types: m int

user_defined_types: struct queueRequest { int reqNumber; int typeOfRequest; char *locationOfRequest; int len; long timeOfRequest; long timeOfExec; }


initializeRequestQueue

decompiled_code:
void *initializeRequestQueue()
{
__int64 *v0; // rbx
void *result; // rax

v0 = queueOfRequests;
do
{
result = malloc(0x28uLL);
*v0++ = (__int64)result;
}
while ( v0 != (__int64 *)(unsigned int)((_DWORD)queueOfRequests + 400) );
return result;
}

canonical_decompiled_code:
void *func2()
{
__int64 *v0; // rbx
void *result; // rax

v0 = queueOfRequests;
do
{
result = malloc(0x28uLL);
*v0++ = (__int64)result;
}
while ( v0 != (__int64 *)(unsigned int)((_DWORD)queueOfRequests + 400) );
return result;
}

original_code:
void initializeRequestQueue()
{
 int i;
 for (i = 0; i < 50; i++)
     {
  queueOfRequests[i] = (struct queueRequest*) malloc(sizeof(struct queueRequest));
 }
}

canonical_original_code:
void initializeRequestQueue()
{
 int i;
 for (i = 0; i < 50; i++)
     {
  queueOfRequests[i] = (struct queueRequest *) malloc(sizeof(struct queueRequest));
 }
}


mallocThreadpool

decompiled_code:
void *mallocThreadpool()
{
__int64 v0; // rbx
void *result; // rax

v0 = (int)numberOfThreads;
readyQThreads = (__int64)malloc(24LL * (int)numberOfThreads);
result = malloc(32 * v0);
wThreads = (__int64)result;
return result;
}

canonical_decompiled_code:
void *func3()
{
__int64 v0; // rbx
void *result; // rax

v0 = (int)numberOfThreads;
readyQThreads = (__int64)malloc(24LL * (int)numberOfThreads);
result = malloc(32 * v0);
wThreads = (__int64)result;
return result;
}

original_code:
void mallocThreadpool()
{
 readyQThreads = (struct readyQueueRequest*) malloc(numberOfThreads * sizeof(struct readyQueueRequest));
 wThreads = (sem_t *) malloc(numberOfThreads * sizeof(sem_t));
}

canonical_original_code:
void mallocThreadpool()
{
 readyQThreads = (struct readyQueueRequest *) malloc(numberOfThreads * sizeof(struct readyQueueRequest));
 wThreads = (union sem_t *) malloc(numberOfThreads * sizeof(sem_t));
}

user_defined_types: struct readyQueueRequest { unsigned long thread; struct queueRequest *queueRequestptr; int isSched; }

user_defined_types: union sem_t { char __size[32]; long __align; }


initializeMutex

decompiled_code:
int initializeMutex()
{
int result; // eax
__int64 v1; // rbx

pthread_mutex_init(&mutexOfQueue, 0LL);
pthread_cond_init(&condVarOfQueue, 0LL);
pthread_mutex_init(&mutexOfReadyQ, 0LL);
sem_init(&cnt, 0, 0);
result = sem_init(&waitingThreading, 0, numberOfThreads);
if ( (int)numberOfThreads > 0 )
{
v1 = 0LL;
do
result = sem_init((sem_t *)(wThreads + 32 * v1++), 0, 0);
while ( (int)numberOfThreads > (int)v1 );
}
return result;
}

canonical_decompiled_code:
int func4()
{
int result; // eax
__int64 v1; // rbx

pthread_mutex_init(&mutexOfQueue, 0LL);
pthread_cond_init(&condVarOfQueue, 0LL);
pthread_mutex_init(&mutexOfReadyQ, 0LL);
sem_init(&cnt, 0, 0);
result = sem_init(&waitingThreading, 0, numberOfThreads);
if ( (int)numberOfThreads > 0 )
{
v1 = 0LL;
do
result = sem_init((sem_t *)(wThreads + 32 * v1++), 0, 0);
while ( (int)numberOfThreads > (int)v1 );
}
return result;
}

original_code:
void initializeMutex()
{
 int k;
 pthread_mutex_init(&mutexOfQueue, ((void *)0));
 pthread_cond_init(&condVarOfQueue, ((void *)0));
 pthread_mutex_init(&mutexOfReadyQ, ((void *)0));
 sem_init(&cnt, 0, 0);
 sem_init(&waitingThreading, 0, numberOfThreads);
 for (k = 0; k < numberOfThreads; k++)
 {
  sem_init(&wThreads[k], 0, 0);
 }
}

variable_types: k int


createThreadpool

decompiled_code:
__int64 createThreadpool()
{
__int64 result; // rax
int arg[3]; // [rsp+Ch] [rbp-Ch] BYREF

arg[0] = 0;
if ( (int)numberOfThreads > 0 )
{
do
{
pthread_mutex_lock(&mutexOfQueue);
pthread_create((pthread_t *)(readyQThreads + 24LL * arg[0]), 0LL, (void *(*)(void *))&sendResponse, arg);
*(_DWORD *)(readyQThreads + 24LL * arg[0] + 16) = 1;
pthread_cond_wait(&condVarOfQueue, &mutexOfQueue);
pthread_mutex_unlock(&mutexOfQueue);
result = (unsigned int)(arg[0] + 1);
arg[0] = result;
}
while ( (int)result < (int)numberOfThreads );
}
return result;
}

canonical_decompiled_code:
__int64 func5()
{
__int64 result; // rax
int arg[3]; // [rsp+Ch] [rbp-Ch] BYREF

arg[0] = 0;
if ( (int)numberOfThreads > 0 )
{
do
{
pthread_mutex_lock(&mutexOfQueue);
pthread_create((pthread_t *)(readyQThreads + 24LL * arg[0]), 0LL, (void *(*)(void *))&sendResponse, arg);
*(_DWORD *)(readyQThreads + 24LL * arg[0] + 16) = 1;
pthread_cond_wait(&condVarOfQueue, &mutexOfQueue);
pthread_mutex_unlock(&mutexOfQueue);
result = (unsigned int)(arg[0] + 1);
arg[0] = result;
}
while ( (int)result < (int)numberOfThreads );
}
return result;
}

original_code:
void createThreadpool()
{
 int j = 0;
 for (j = 0; j < numberOfThreads; j++)
 {
  pthread_mutex_lock(&mutexOfQueue);
  pthread_create(&((readyQThreads + j)->thread), ((void *)0), sendResponse, &j);
  (readyQThreads + j)->isSched = 1;
  pthread_cond_wait(&condVarOfQueue, &mutexOfQueue);
  pthread_mutex_unlock(&mutexOfQueue);
 }
}


initializeSchedulingThread

decompiled_code:
int initializeSchedulingThread()
{
int result; // eax
pthread_t newthread[2]; // [rsp+8h] [rbp-10h] BYREF

if ( schedAlgo )
result = pthread_create(newthread, 0LL, (void *(*)(void *))scheduleRequestUsingSJF, 0LL);
else
result = pthread_create(newthread, 0LL, (void *(*)(void *))scheduleRequestUsingFCFS, 0LL);
return result;
}

canonical_decompiled_code:
int func6()
{
int result; // eax
pthread_t newthread[2]; // [rsp+8h] [rbp-10h] BYREF

if ( schedAlgo )
result = pthread_create(newthread, 0LL, (void *(*)(void *))scheduleRequestUsingSJF, 0LL);
else
result = pthread_create(newthread, 0LL, (void *(*)(void *))scheduleRequestUsingFCFS, 0LL);
return result;
}

original_code:
void initializeSchedulingThread()
{
 pthread_t schthreads;
 if (schedAlgo == 0)
 {
  pthread_create(&schthreads, ((void *)0), scheduleRequestUsingFCFS, ((void *)0));
 }
 else
 {
  pthread_create(&schthreads, ((void *)0), scheduleRequestUsingSJF, ((void *)0));
 }
}

canonical_original_code:
void initializeSchedulingThread()
{
 unsigned long schthreads;
 if (schedAlgo == 0)
 {
  pthread_create(&schthreads, ((void *)0), scheduleRequestUsingFCFS, ((void *)0));
 }
 else
 {
  pthread_create(&schthreads, ((void *)0), scheduleRequestUsingSJF, ((void *)0));
 }
}

variable_types:
schthreads
unsigned long


network

decompiled_code:
int network()
{
int v0; // ebx

v0 = socket(2, 1, 0);
serverSocketDescr = v0;
address.sa_family = 2;
*(_WORD *)address.sa_data = _bswap_16(portNumber);
*(_DWORD *)&address.sa_data[2] = _bswap_32(0);
bind(v0, &address, 0x10u);
return listen(serverSocketDescr, 50);
}

canonical_decompiled_code:
int func2()
{
int v0; // ebx

v0 = socket(2, 1, 0);
serverSocketDescr = v0;
address.sa_family = 2;
*(_WORD *)address.sa_data = func0(portNumber);
*(_DWORD *)&address.sa_data[2] = func1(0);
bind(v0, &address, 0x10u);
return listen(serverSocketDescr, 50);
}

original_code:
void network()
{
 serverSocketDescr = socket(2, SOCK_STREAM, 0);
 address.sin_family = 2;
 address.sin_port = htons(portNumber);
 address.sin_addr.s_addr = htonl(((in_addr_t) 0x00000000));
 bind(serverSocketDescr, (struct sockaddr *) &address, sizeof(address));
 listen(serverSocketDescr, 50);
}

canonical_original_code:
void network()
{
 serverSocketDescr = socket(2, SOCK_STREAM, 0);
 address.sin_family = 2;
 address.sin_port = htons(portNumber);
 address.sin_addr.s_addr = htonl(((unsigned int) 0x00000000));
 bind(serverSocketDescr, (struct sockaddr *) &address, sizeof(address));
 listen(serverSocketDescr, 50);
}


repo: ruijiege/System-Programming

decompiled_code:
void *init_opt()
{
void *result; // rax

*(_DWORD *)&s[5] = 0;
strcpy(s, "FIFO");
result = memset(buf, 0, 0x2710uLL);
frameNumber = 5;
replaceAlg = 0;
return result;
}

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

*(_DWORD *)&s[5] = 0;
strcpy(s, "FIFO");
result = memset(buf, 0, 0x2710uLL);
frameNumber = 5;
replaceAlg = 0;
return result;
}

original_code:
void init_opt()
{
    bzero(s,sizeof(s));
    strcpy(s,"FIFO");
    bzero(buf,sizeof(buf));
    frameNumber=5;
 replaceAlg=0;
}


GetPage

decompiled_code:
__int64 __fastcall GetPage(__int64 a1, const char *a2)
{
char *v2; // rbp
int v3; // er12
int v4; // er13
char v5; // bl
char *v6; // rbx

v2 = (char *)malloc((size_t)&unk_F4240);
strcpy(v2, a2);
if ( flen > 0 )
{
v3 = 0;
v4 = 1;
while ( 1 )
{
v5 = *v2;
if ( (*v2 & 0xDF) != 0 )
{
if ( ((*_ctype_b_loc())[v5] & 0x800) == 0 && (unsigned __int8)(v5 - 9) > 1u )
{
puts("The input page sequence has illegale character!");
exit(0);
}
}
else if ( v5 == 32 )
{
++v2;
goto LABEL_6;
}
switch ( v5 )
{
case 10:
++v2;
break;
case 9:
++v2;
break;
case 0:
return (unsigned int)(v4 - 1);
default:
v6 = strchr(v2, 32);
*v6 = 0;
*(_DWORD *)(a1 + 4LL * v4) = atoi(v2);
*v6 = 32;
++v4;
v2 = v6;
break;
}
LABEL_6:
if ( flen <= ++v3 )
return (unsigned int)(v4 - 1);
}
}
v4 = 1;
return (unsigned int)(v4 - 1);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, const char *a2)
{
char *v2; // rbp
int v3; // er12
int v4; // er13
char v5; // bl
char *v6; // rbx

v2 = (char *)malloc((size_t)&unk_F4240);
strcpy(v2, a2);
if ( flen > 0 )
{
v3 = 0;
v4 = 1;
while ( 1 )
{
v5 = *v2;
if ( (*v2 & 0xDF) != 0 )
{
if ( ((*_ctype_b_loc())[v5] & 0x800) == 0 && (unsigned __int8)(v5 - 9) > 1u )
{
puts("The input page sequence has illegale character!");
exit(0);
}
}
else if ( v5 == 32 )
{
++v2;
goto LABEL_6;
}
switch ( v5 )
{
case 10:
++v2;
break;
case 9:
++v2;
break;
case 0:
return (unsigned int)(v4 - 1);
default:
v6 = strchr(v2, 32);
*v6 = 0;
*(_DWORD *)(a1 + 4LL * v4) = atoi(v2);
*v6 = 32;
++v4;
v2 = v6;
break;
}
LABEL_6:
if ( flen <= ++v3 )
return (unsigned int)(v4 - 1);
}
}
v4 = 1;
return (unsigned int)(v4 - 1);
}

original_code:
int GetPage(int pageSequence[1000000],char* buf)
{
    char* refplace;
 char* reffile;
 reffile=(char*) malloc(1000000);
 strcpy(reffile,buf);
    refplace=reffile;
 int i=1;
    int j=0;
    for (j=0; j<flen; j++)
    {
        if((*refplace!='\0') && (*refplace!=' ') && (!(((*__ctype_b_loc ())[(int) ((*refplace))] & (unsigned short int) _ISdigit)))&&(*refplace!='\n')&&(*refplace!='\t'))
        {
            printf("The input page sequence has illegale character!\n");
            exit(0);
        }
        else if ((*refplace==' '))
        {
            refplace++;
        }
        else if ((*refplace=='\n'))
        {
            refplace++;
        }
        else if ((*refplace=='\t'))
        {
            refplace++;
        }
        else if((*refplace=='\0'))
        {
            goto X;
        }
        else
        {
            reffile=refplace;
            refplace=strchr(reffile,' ');
            *refplace='\0';
            pageSequence[i]=atoi(reffile);
            *refplace=' ';
            i++;
        }
    }
X:{};
 return i-1;
}

canonical_original_code:
int GetPage(int pageSequence[1000000], char *buf)
{
    char *refplace;
 char *reffile;
 reffile=(char *) malloc(1000000);
 strcpy(reffile,buf);
    refplace=reffile;
 int i = 1;
    int j = 0;
    for (j=0; j<flen; j++)
    {
        if((*refplace!='\0') && (*refplace!=' ') && (!(((*__ctype_b_loc ())[(int) ((*refplace))] & (unsigned short) _ISdigit)))&&(*refplace!='\n')&&(*refplace!='\t'))
        {
            printf("The input page sequence has illegale character!\n");
            exit(0);
        }
        else if ((*refplace==' '))
        {
            refplace++;
        }
        else if ((*refplace=='\n'))
        {
            refplace++;
        }
        else if ((*refplace=='\t'))
        {
            refplace++;
        }
        else if((*refplace=='\0'))
        {
            goto X;
        }
        else
        {
            reffile=refplace;
            refplace=strchr(reffile,' ');
            *refplace='\0';
            pageSequence[i]=atoi(reffile);
            *refplace=' ';
            i++;
        }
    }
X:{};
 return i-1;
}

variable_types:
pageSequence
int [1000000]

variable_types:
buf
char *

variable_types:
refplace
char *

variable_types:
reffile
char *


options

decompiled_code:
__int64 __fastcall options(int argc, char **argv)
{
int v2; // er13
int v3; // eax
char *v4; // r12

init_opt();
opterr = 0;
memset(buf, 0, 0x2710uLL);
v2 = 1;
while ( 1 )
{
v3 = getopt(argc, argv, "hr:f:i:");
if ( v3 == -1 )
break;
if ( v3 == 105 )
{
memset(inputFile, 0, sizeof(inputFile));
strcpy(inputFile, optarg);
lstat(inputFile, &attri);
v2 = 0;
if ( (attri.st_mode & 0xF000) == 0x8000 )
{
storebuf(buf);
pageNumber = GetPage((__int64)&pageSequence, buf);
}
}
else if ( v3 > 105 )
{
if ( v3 != 114 )
goto LABEL_23;
strcpy(s, optarg);
if ( !strcmp(s, "FIFO") )
{
replaceAlg = 0;
}
else if ( !strcmp(s, "LRU_REF8") )
{
replaceAlg = 4;
}
else if ( !strcmp(s, "LFU") )
{
replaceAlg = 2;
}
else if ( !strcmp(s, "LRU_CLOCK") )
{
replaceAlg = 1;
}
else
{
if ( strcmp(s, "LRU_STACK") )
{
puts("Wrong replace-policy settings, please set again!");
exit(0);
}
replaceAlg = 3;
}
}
else if ( v3 == 102 )
{
v4 = (char *)malloc(8uLL);
d = (__int64)v4;
strcpy(v4, optarg);
frameNumber = atoi(v4);
}
else
{
if ( v3 == 104 )
{
puts("\nOptions you can choose:");
puts("-h : Print a usage summary with all options and exit.");
puts("-f You can set your own frame number,the default value of frame number is 5.");
puts(
"-r You can set your own replacement-policy from here.You have the options of FIFO LRU_CLOCK LRU_STACK, LRU_RE"
"F8, and LFU. The default replacement-policy is FIFO.");
puts(
"-i You can set your own input file frome here.If you don't use the option, then please input the page sequrenc"
" from keyboard.\n");
exit(0);
}
LABEL_23:
printf("Other Options");
}
}
if ( v2 )
{
printf("Please input the page sequence:\n ");
gets(buf);
*(_WORD *)&buf[strlen(buf)] = 32;
flen = 10000;
pageNumber = GetPage((__int64)&pageSequence, buf);
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(int argc, char **argv)
{
int v2; // er13
int v3; // eax
char *v4; // r12

func0();
opterr = 0;
memset(buf, 0, 0x2710uLL);
v2 = 1;
while ( 1 )
{
v3 = getopt(argc, argv, "hr:f:i:");
if ( v3 == -1 )
break;
if ( v3 == 105 )
{
memset(inputFile, 0, sizeof(inputFile));
strcpy(inputFile, optarg);
lstat(inputFile, &attri);
v2 = 0;
if ( (attri.st_mode & 0xF000) == 0x8000 )
{
func1(buf);
pageNumber = func2((__int64)&pageSequence, buf);
}
}
else if ( v3 > 105 )
{
if ( v3 != 114 )
goto LABEL_23;
strcpy(s, optarg);
if ( !strcmp(s, "FIFO") )
{
replaceAlg = 0;
}
else if ( !strcmp(s, "LRU_REF8") )
{
replaceAlg = 4;
}
else if ( !strcmp(s, "LFU") )
{
replaceAlg = 2;
}
else if ( !strcmp(s, "LRU_CLOCK") )
{
replaceAlg = 1;
}
else
{
if ( strcmp(s, "LRU_STACK") )
{
puts("Wrong replace-policy settings, please set again!");
exit(0);
}
replaceAlg = 3;
}
}
else if ( v3 == 102 )
{
v4 = (char *)malloc(8uLL);
d = (__int64)v4;
strcpy(v4, optarg);
frameNumber = atoi(v4);
}
else
{
if ( v3 == 104 )
{
puts("\nOptions you can choose:");
puts("-h : Print a usage summary with all options and exit.");
puts("-f You can set your own frame number,the default value of frame number is 5.");
puts(
"-r You can set your own replacement-policy from here.You have the options of FIFO LRU_CLOCK LRU_STACK, LRU_RE"
"F8, and LFU. The default replacement-policy is FIFO.");
puts(
"-i You can set your own input file frome here.If you don't use the option, then please input the page sequrenc"
" from keyboard.\n");
exit(0);
}
LABEL_23:
printf("Other Options");
}
}
if ( v2 )
{
printf("Please input the page sequence:\n ");
gets(buf);
*(_WORD *)&buf[strlen(buf)] = 32;
flen = 10000;
pageNumber = func2((__int64)&pageSequence, buf);
}
return 1LL;
}

original_code:
int options(int argc,char **argv)
{
    init_opt();
    int c,i;
    char ch;
    int stdin_flag=1,bufcnt=0;
    i=1;
    opterr=0;
    bzero(buf,sizeof(buf));
    while ((c = getopt(argc, argv, "hr:f:i:")) != -1)
    {
            switch(c)
            {
                case 'h':
                    printf("\nOptions you can choose:\n");
                    printf("-h : Print a usage summary with all options and exit.\n");
                    printf("-f You can set your own frame number,the default value of frame number is 5.\n");
                    printf("-r You can set your own replacement-policy from here.You have the options of FIFO LRU_CLOCK  LRU_STACK, LRU_REF8, and LFU. The default replacement-policy is FIFO.\n");
                    printf("-i You can set your own input file frome here.If you don't use the option, then please input the page sequrenc from keyboard.\n\n");
                    exit(0);
                    break;
                case 'f':
                    d= (char *) malloc(sizeof optarg);
                    strcpy(d,optarg);
                    frameNumber=atoi(d);
                    i=i+2;
                    break;
                case 'r':
                    strcpy(s,optarg);
                    if(!strcmp(s,"FIFO"))
                    {
                        replaceAlg=0;
                    }
                    else if(!strcmp(s,"LRU_REF8"))
                    {
                        replaceAlg=4;
                    }
                    else if(!strcmp(s,"LFU"))
                    {
                        replaceAlg=2;
                    }
                    else if(!strcmp(s,"LRU_CLOCK"))
                    {
                        replaceAlg=1;
                    }
                    else if(!strcmp(s,"LRU_STACK"))
                    {
                        replaceAlg=3;
                    }
                    else
                    {
                        printf("Wrong replace-policy settings, please set again!\n");
                        exit(0);
                    }
                    i=i+2;
                    break;
                case 'i':
                    stdin_flag=0;
                    i++;
                    bzero(inputFile,sizeof(inputFile));
                    strcpy(inputFile,optarg);
                    lstat(inputFile,&attri);
                    if(((((attri.st_mode)) & 0170000) == (0100000)))
                    {
                        storebuf(buf);
                        pageNumber=GetPage(pageSequence,buf);
                    }
                    else
                    {
                    }
                    break;
                default:
                    printf("Other Options");
            }
    }
    if (stdin_flag)
    {
        printf("Please input the page sequence:\n ");
        gets(buf);
        strcat(buf," ");
        flen=sizeof(buf);
        pageNumber=GetPage(pageSequence,buf);
    }
    return 1;
}

canonical_original_code:
int options(int argc, char **argv)
{
    init_opt();
    int c; int i;
    char ch;
    int stdin_flag = 1; int bufcnt = 0;
    i=1;
    opterr=0;
    bzero(buf,sizeof(buf));
    while ((c = getopt(argc, argv, "hr:f:i:")) != -1)
    {
            switch(c)
            {
                case 'h':
                    printf("\nOptions you can choose:\n");
                    printf("-h : Print a usage summary with all options and exit.\n");
                    printf("-f You can set your own frame number,the default value of frame number is 5.\n");
                    printf("-r You can set your own replacement-policy from here.You have the options of FIFO LRU_CLOCK  LRU_STACK, LRU_REF8, and LFU. The default replacement-policy is FIFO.\n");
                    printf("-i You can set your own input file frome here.If you don't use the option, then please input the page sequrenc from keyboard.\n\n");
                    exit(0);
                    break;
                case 'f':
                    d= (char *) malloc(sizeof optarg);
                    strcpy(d,optarg);
                    frameNumber=atoi(d);
                    i=i+2;
                    break;
                case 'r':
                    strcpy(s,optarg);
                    if(!strcmp(s,"FIFO"))
                    {
                        replaceAlg=0;
                    }
                    else if(!strcmp(s,"LRU_REF8"))
                    {
                        replaceAlg=4;
                    }
                    else if(!strcmp(s,"LFU"))
                    {
                        replaceAlg=2;
                    }
                    else if(!strcmp(s,"LRU_CLOCK"))
                    {
                        replaceAlg=1;
                    }
                    else if(!strcmp(s,"LRU_STACK"))
                    {
                        replaceAlg=3;
                    }
                    else
                    {
                        printf("Wrong replace-policy settings, please set again!\n");
                        exit(0);
                    }
                    i=i+2;
                    break;
                case 'i':
                    stdin_flag=0;
                    i++;
                    bzero(inputFile,sizeof(inputFile));
                    strcpy(inputFile,optarg);
                    lstat(inputFile,&attri);
                    if(((((attri.st_mode)) & 0170000) == (0100000)))
                    {
                        storebuf(buf);
                        pageNumber=GetPage(pageSequence,buf);
                    }
                    else
                    {
                    }
                    break;
                default:
                    printf("Other Options");
            }
    }
    if (stdin_flag)
    {
        printf("Please input the page sequence:\n ");
        gets(buf);
        strcat(buf," ");
        flen=sizeof(buf);
        pageNumber=GetPage(pageSequence,buf);
    }
    return 1;
}

variable_types: c int

variable_types: ch char

variable_types: bufcnt int


original_code:
int LRUREF8Alg(int pageSequence[],int pageNumber,int frameNumber)
{
    gettimeofday(&tpstart,((void *)0));
    int i=1,k=1,j=1;
    int min,mint,add2,m;
 int count=0,r=0,add=0,mark=0,cnt=0;
 int frames[frameNumber+1];
 unsigned int ref[pageNumber+1];
    int pageshowtime[frameNumber+1];
    for(i=1;i<=frameNumber;i++) pageshowtime[i]=0;
 for(i=1;i<=frameNumber;i++) ref[i]=0;
 for(i=1;i<=frameNumber;i++) frames[i]=0;
 for(i=1;i<=pageNumber;i++)
    {
     if(!Search(frames,frameNumber,pageSequence[i]))
        {
         if(count<frameNumber)
            {
          frames[k]=pageSequence[i];
       count++;
                for (j=1; j<=frameNumber; j++)
                {
                    ref[j]=ref[j]>>1;
                }
                ref[k]=ref[k]|0x0080;k++;
                pageshowtime[cnt]=cnt;cnt++;
            }
   else
            {
       r++;
                mint=ref[1];
                mark=1;
                for (j=1; j<=frameNumber; j++)
                {
                    if(ref[j]<mint)
                    {
                        mint=ref[j];
                        add2=j;
                        min=pageshowtime[add2];
                        mark=j;
                    }
                }
                for (m=1; m<=frameNumber; m++)
                {
                    if (mint==ref[m])
                    {
                        if (pageshowtime[m]<min)
                        {
                            min=pageshowtime[m];
                            mark=m;
                        }
                    }
                }
                frames[mark]=pageSequence[i];
                for (j=1; j<=frameNumber; j++)
                {
                    ref[j]=ref[j]>>1;
                }
                ref[mark]=ref[mark]|0x0080;
            }
        }
  else
        {
            add=Search(frames,frameNumber,pageSequence[i]);
            for (j=1; j<=frameNumber; j++)
            {
                ref[j]=ref[j]>>1;
            }
            ref[add]=ref[add]|0x0080;
            pageshowtime[add]=cnt;
            cnt++;
        }
    }
    gettimeofday(&tpend,((void *)0));
    timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec;
    return r;
}

canonical_original_code:
int LRUREF8Alg(int pageSequence[], int pageNumber, int frameNumber)
{
    gettimeofday(&tpstart,((void *)0));
    int i = 1; int k = 1; int j = 1;
    int min; int mint; int add2; int m;
 int count = 0; int r = 0; int add = 0; int mark = 0; int cnt = 0;
 int frames[-1];
 unsigned int ref[-1];
    int pageshowtime[-1];
    for(i=1;i<=frameNumber;i++) pageshowtime[i]=0;
 for(i=1;i<=frameNumber;i++) ref[i]=0;
 for(i=1;i<=frameNumber;i++) frames[i]=0;
 for(i=1;i<=pageNumber;i++)
    {
     if(!Search(frames,frameNumber,pageSequence[i]))
        {
         if(count<frameNumber)
            {
          frames[k]=pageSequence[i];
       count++;
                for (j=1; j<=frameNumber; j++)
                {
                    ref[j]=ref[j]>>1;
                }
                ref[k]=ref[k]|0x0080;k++;
                pageshowtime[cnt]=cnt;cnt++;
            }
   else
            {
       r++;
                mint=ref[1];
                mark=1;
                for (j=1; j<=frameNumber; j++)
                {
                    if(ref[j]<mint)
                    {
                        mint=ref[j];
                        add2=j;
                        min=pageshowtime[add2];
                        mark=j;
                    }
                }
                for (m=1; m<=frameNumber; m++)
                {
                    if (mint==ref[m])
                    {
                        if (pageshowtime[m]<min)
                        {
                            min=pageshowtime[m];
                            mark=m;
                        }
                    }
                }
                frames[mark]=pageSequence[i];
                for (j=1; j<=frameNumber; j++)
                {
                    ref[j]=ref[j]>>1;
                }
                ref[mark]=ref[mark]|0x0080;
            }
        }
  else
        {
            add=Search(frames,frameNumber,pageSequence[i]);
            for (j=1; j<=frameNumber; j++)
            {
                ref[j]=ref[j]>>1;
            }
            ref[add]=ref[add]|0x0080;
            pageshowtime[add]=cnt;
            cnt++;
        }
    }
    gettimeofday(&tpend,((void *)0));
    timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec;
    return r;
}

variable_types:
pageSequence
int []

variable_types: pageNumber int

variable_types: frameNumber int

variable_types: j int

variable_types: min int

variable_types: mint int

variable_types: count int

variable_types: r int

variable_types: add int

variable_types: mark int

variable_types: cnt int

variable_types:
frames
int [-1]

variable_types:
ref
unsigned int [-1]

variable_types:
pageshowtime
int [-1]


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
if ( (unsigned int)options(argc, argv, envp) )
{
if ( replaceAlg )
{
switch ( replaceAlg )
{
case 4:
replacePageNum = LRUREF8Alg(&pageSequence, (unsigned int)pageNumber, (unsigned int)frameNumber);
break;
case 3:
replacePageNum = LRUStackAlg(&pageSequence, (unsigned int)pageNumber, (unsigned int)frameNumber);
break;
case 1:
replacePageNum = LRUClockAlg(&pageSequence, (unsigned int)pageNumber, (unsigned int)frameNumber);
break;
case 2:
replacePageNum = LFUAlg(&pageSequence, (unsigned int)pageNumber, (unsigned int)frameNumber);
break;
}
}
else
{
replacePageNum = FifoAlg(&pageSequence, (unsigned int)pageNumber, (unsigned int)frameNumber);
}
optReplacePageNum = OptAlg(&pageSequence, (unsigned int)pageNumber, (unsigned int)frameNumber);
*(float *)&algTime = (float)((float)(*(float *)&opttimeuse - *(float *)&timeuse) * 100.0) / *(float *)&opttimeuse;
printf("\n# of page replacements with %s : %d", s, (unsigned int)replacePageNum);
printf("\n# of page replacements with Optimal : %d", (unsigned int)optReplacePageNum);
*(float *)&rui = (float)replacePageNum;
*(float *)&jie = (float)optReplacePageNum;
*(float *)&penalty = (float)((float)((float)replacePageNum - (float)optReplacePageNum) / (float)optReplacePageNum)
* 100.0;
printf("\n%% page replacement penalty using %s : %.1f%%\n", s, *(float *)&penalty);
printf("\nTotal time to run %s algorithm :%.f msec", s, *(float *)&timeuse);
printf("\nTotal time to run Optimal algorithm :%.f msec", *(float *)&opttimeuse);
printf("\n%s is %.1f%% faster than Optimal algorithm.\n\n", s, *(float *)&algTime);
}
else
{
puts("Can't get your input command!");
}
return 0;
}

original_code:
int main(int argc, char* argv[])
{
    if(!(options(argc,argv)))
    {
        printf("Can't get your input command!\n");
        return 0;
    }
 if(replaceAlg==0)
     replacePageNum=FifoAlg(pageSequence,pageNumber,frameNumber);
    else if(replaceAlg==4)
        replacePageNum=LRUREF8Alg(pageSequence,pageNumber,frameNumber);
    else if(replaceAlg==3)
        replacePageNum=LRUStackAlg(pageSequence,pageNumber,frameNumber);
    else if(replaceAlg==1)
     replacePageNum=LRUClockAlg(pageSequence,pageNumber,frameNumber);
 else if(replaceAlg==2)
        replacePageNum=LFUAlg(pageSequence,pageNumber,frameNumber);
    optReplacePageNum=OptAlg(pageSequence,pageNumber,frameNumber);
    algTime=100*(opttimeuse-timeuse)/opttimeuse;
 printf("\n# of page replacements with %s : %d",s,replacePageNum);
 printf("\n# of page replacements with Optimal : %d",optReplacePageNum);
    rui=(float)replacePageNum;
 jie=(float)optReplacePageNum;
 penalty=((rui-jie)/jie)*100;
 printf("\n%% page replacement penalty using %s : %.1f%%\n",s,penalty);
    printf("\nTotal time to run %s algorithm :%.f msec",s,timeuse);
    printf("\nTotal time to run Optimal algorithm :%.f msec",opttimeuse);
    printf("\n%s is %.1f%% faster than Optimal algorithm.\n\n",s,algTime);
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    if(!(options(argc,argv)))
    {
        printf("Can't get your input command!\n");
        return 0;
    }
 if(replaceAlg==0)
     replacePageNum=FifoAlg(pageSequence,pageNumber,frameNumber);
    else if(replaceAlg==4)
        replacePageNum=LRUREF8Alg(pageSequence,pageNumber,frameNumber);
    else if(replaceAlg==3)
        replacePageNum=LRUStackAlg(pageSequence,pageNumber,frameNumber);
    else if(replaceAlg==1)
     replacePageNum=LRUClockAlg(pageSequence,pageNumber,frameNumber);
 else if(replaceAlg==2)
        replacePageNum=LFUAlg(pageSequence,pageNumber,frameNumber);
    optReplacePageNum=OptAlg(pageSequence,pageNumber,frameNumber);
    algTime=100*(opttimeuse-timeuse)/opttimeuse;
 printf("\n# of page replacements with %s : %d",s,replacePageNum);
 printf("\n# of page replacements with Optimal : %d",optReplacePageNum);
    rui=(float)replacePageNum;
 jie=(float)optReplacePageNum;
 penalty=((rui-jie)/jie)*100;
 printf("\n%% page replacement penalty using %s : %.1f%%\n",s,penalty);
    printf("\nTotal time to run %s algorithm :%.f msec",s,timeuse);
    printf("\nTotal time to run Optimal algorithm :%.f msec",opttimeuse);
    printf("\n%s is %.1f%% faster than Optimal algorithm.\n\n",s,algTime);
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]


Search

decompiled_code:
__int64 __fastcall Search(__int64 a1, int a2, int a3)
{
__int64 v3; // rax
unsigned int v4; // ecx

if ( a2 <= 0 )
return 0;
v3 = 1LL;
while ( 1 )
{
v4 = v3;
if ( *(_DWORD *)(a1 + 4 * v3) == a3 )
break;
if ( a2 < (int)++v3 )
return 0;
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, int a2, int a3)
{
__int64 v3; // rax
unsigned int v4; // ecx

if ( a2 <= 0 )
return 0;
v3 = 1LL;
while ( 1 )
{
v4 = v3;
if ( *(_DWORD *)(a1 + 4 * v3) == a3 )
break;
if ( a2 < (int)++v3 )
return 0;
}
return v4;
}

original_code:
int Search(int f[],int frameNumber,int singlepage)
{
    int i=1;
 while(i<=frameNumber)
    {
     if(f[i]==singlepage) return i;
        i++;
    }
 return 0;
}

canonical_original_code:
int Search(int f[], int frameNumber, int singlepage)
{
    int i = 1;
 while(i<=frameNumber)
    {
     if(f[i]==singlepage) return i;
        i++;
    }
 return 0;
}

variable_types:
f
int []

variable_types: singlepage int

variable_types: i int