repo: shanet/Irrational

decompiled_code:
void __fastcall calc_term(void *a1)
{
char v1[24]; // [rsp+20h] [rbp-38h] BYREF

switch ( *(_DWORD *)a1 )
{
case 1:
strcpy(v1, "239");
goto LABEL_3;
case 2:
strcpy(v1, "1023");
goto LABEL_3;
case 3:
strcpy(v1, "5832");
goto LABEL_7;
case 4:
strcpy(v1, "113021");
goto LABEL_3;
case 5:
strcpy(v1, "6826318");
goto LABEL_7;
case 6:
strcpy(v1, "33366019650");
LABEL_7:
mpfr_set_default_prec();
mpfr_init_set_str();
mpfr_atan2();
mpfr_mul_ui();
pthread_mutex_lock(&sum_mutex);
mpfr_sub();
break;
case 7:
strcpy(v1, "43599522992503626068");
LABEL_3:
mpfr_set_default_prec();
mpfr_init_set_str();
mpfr_atan2();
mpfr_mul_ui();
pthread_mutex_lock(&sum_mutex);
mpfr_add();
break;
default:
pthread_exit(0LL);
}
pthread_mutex_unlock(&sum_mutex);
mpfr_clears();
mpfr_free_cache();
pthread_exit(0LL);
}

canonical_decompiled_code:
void __fastcall func7(void *a1)
{
char v1[24]; // [rsp+20h] [rbp-38h] BYREF

switch ( *(_DWORD *)a1 )
{
case 1:
strcpy(v1, "239");
goto LABEL_3;
case 2:
strcpy(v1, "1023");
goto LABEL_3;
case 3:
strcpy(v1, "5832");
goto LABEL_7;
case 4:
strcpy(v1, "113021");
goto LABEL_3;
case 5:
strcpy(v1, "6826318");
goto LABEL_7;
case 6:
strcpy(v1, "33366019650");
LABEL_7:
mpfr_set_default_prec();
mpfr_init_set_str();
mpfr_atan2();
mpfr_mul_ui();
pthread_mutex_lock(&sum_mutex);
mpfr_sub();
break;
case 7:
strcpy(v1, "43599522992503626068");
LABEL_3:
mpfr_set_default_prec();
mpfr_init_set_str();
mpfr_atan2();
mpfr_mul_ui();
pthread_mutex_lock(&sum_mutex);
mpfr_add();
break;
default:
pthread_exit(0LL);
}
pthread_mutex_unlock(&sum_mutex);
mpfr_clears();
mpfr_free_cache();
pthread_exit(0LL);
}

original_code:
void* calc_term(void* term_num) {
    char x_str[22];
    int op;
    int coeff;
    mpfr_t *term;
    switch(*(int *)term_num) {
        case 1:
            term = &t1;
            coeff = 183;
            op = 0;
            sprintf(x_str, "239");
            break;
        case 2:
            term = &t2;
            coeff = 32;
            op = 0;
            sprintf(x_str, "1023");
            break;
        case 3:
            term = &t3;
            coeff = 68;
            op = 1;
            sprintf(x_str, "5832");
            break;
        case 4:
            term = &t4;
            coeff = 12;
            op = 0;
            sprintf(x_str, "113021");
            break;
        case 5:
            term = &t5;
            coeff = 100;
            op = 1;
            sprintf(x_str, "6826318");
            break;
        case 6:
            term = &t6;
            coeff = 12;
            op = 1;
            sprintf(x_str, "33366019650");
            break;
        case 7:
            term = &t7;
            coeff = 12;
            op = 0;
            sprintf(x_str, "43599522992503626068");
            break;
        default:
            pthread_exit(((void *)0));
    }
    mpfr_t x;
    mpfr_set_default_prec(precision);
    mpfr_init_set_str(x, x_str, 10, MPFR_RNDN);
    mpfr_atan2(*term, one, x, MPFR_RNDN);
    (__builtin_constant_p (coeff) && (mpfr_ulong) (coeff) >= 1 && ((mpfr_ulong) (coeff) & ((mpfr_ulong) (coeff) - 1)) == 0 ? mpfr_mul_2si((*term), (*term), __builtin_ctzl (coeff), (MPFR_RNDN)) : mpfr_mul_ui ((*term), (*term), (coeff), (MPFR_RNDN)));
    pthread_mutex_lock(&sum_mutex);
    if(op == 0) {
        mpfr_add(pi, pi, *term, MPFR_RNDN);
    } else {
        mpfr_sub(pi, pi, *term, MPFR_RNDN);
    }
    pthread_mutex_unlock(&sum_mutex);
    mpfr_clears(*term, x, ((void *)0));
    mpfr_free_cache();
    pthread_exit(((void *)0));
}

canonical_original_code:
void *calc_term(void *term_num) {
    char x_str[22];
    int op;
    int coeff;
    struct __mpfr_struct (*term)[1];
    switch(*(int *)term_num) {
        case 1:
            term = &t1;
            coeff = 183;
            op = 0;
            sprintf(x_str, "239");
            break;
        case 2:
            term = &t2;
            coeff = 32;
            op = 0;
            sprintf(x_str, "1023");
            break;
        case 3:
            term = &t3;
            coeff = 68;
            op = 1;
            sprintf(x_str, "5832");
            break;
        case 4:
            term = &t4;
            coeff = 12;
            op = 0;
            sprintf(x_str, "113021");
            break;
        case 5:
            term = &t5;
            coeff = 100;
            op = 1;
            sprintf(x_str, "6826318");
            break;
        case 6:
            term = &t6;
            coeff = 12;
            op = 1;
            sprintf(x_str, "33366019650");
            break;
        case 7:
            term = &t7;
            coeff = 12;
            op = 0;
            sprintf(x_str, "43599522992503626068");
            break;
        default:
            pthread_exit(((void *)0));
    }
    struct __mpfr_struct x[1];
    mpfr_set_default_prec(precision);
    mpfr_init_set_str(x, x_str, 10, MPFR_RNDN);
    mpfr_atan2(*term, one, x, MPFR_RNDN);
    (__builtin_constant_p (coeff) && (mpfr_ulong) (coeff) >= 1 && ((mpfr_ulong) (coeff) & ((mpfr_ulong) (coeff) - 1)) == 0 ? mpfr_mul_2si((*term), (*term), __builtin_ctzl (coeff), (MPFR_RNDN)) : mpfr_mul_ui ((*term), (*term), (coeff), (MPFR_RNDN)));
    pthread_mutex_lock(&sum_mutex);
    if(op == 0) {
        mpfr_add(pi, pi, *term, MPFR_RNDN);
    } else {
        mpfr_sub(pi, pi, *term, MPFR_RNDN);
    }
    pthread_mutex_unlock(&sum_mutex);
    mpfr_clears(*term, x, ((void *)0));
    mpfr_free_cache();
    pthread_exit(((void *)0));
}

variable_types: op int

variable_types: coeff int

variable_types: struct __mpfr_struct { long _mpfr_prec; int _mpfr_sign; long _mpfr_exp; unsigned long *_mpfr_d; } (*)[1]

variable_types: struct __mpfr_struct { long _mpfr_prec; int _mpfr_sign; long _mpfr_exp; unsigned long *_mpfr_d; } [1]

return_type: void *


biscSendLedCommand

repo: shanet/libbiscuit

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

result = biscSendByte(139LL);
if ( (_DWORD)result != -1 )
{
result = biscSendByte(ledState[0]);
if ( (_DWORD)result != -1 )
{
result = biscSendByte(ledState[1]);
if ( (_DWORD)result != -1 )
result = (unsigned int)-((unsigned int)biscSendByte(ledState[2]) == -1);
}
}
return result;
}

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

result = biscSendByte(139LL);
if ( (_DWORD)result != -1 )
{
result = biscSendByte(ledState[0]);
if ( (_DWORD)result != -1 )
{
result = biscSendByte(ledState[1]);
if ( (_DWORD)result != -1 )
result = (unsigned int)-((unsigned int)biscSendByte(ledState[2]) == -1);
}
}
return result;
}

original_code:
int biscSendLedCommand(void) {
    if(biscSendByte(139) == -1) return -1;
    if(biscSendByte(ledState.playAdvanceState) == -1) return -1;
    if(biscSendByte(ledState.powerLedColor) == -1) return -1;
    if(biscSendByte(ledState.powerLedIntensity) == -1) return -1;
    return 0;
}

canonical_original_code:
int biscSendLedCommand() {
    if(biscSendByte(139) == -1) return -1;
    if(biscSendByte(ledState.playAdvanceState) == -1) return -1;
    if(biscSendByte(ledState.powerLedColor) == -1) return -1;
    if(biscSendByte(ledState.powerLedIntensity) == -1) return -1;
    return 0;
}

return_type: int


biscTurnOnAdvanceLed

decompiled_code:
__int64 biscTurnOnAdvanceLed()
{
ledState[0] |= 8u;
return biscSendLedCommand();
}

canonical_decompiled_code:
__int64 func1()
{
ledState[0] |= 8u;
return func0();
}

original_code:
int biscTurnOnAdvanceLed(void) {
    ledState.playAdvanceState |= 8;
    return biscSendLedCommand();
}

canonical_original_code:
int biscTurnOnAdvanceLed() {
    ledState.playAdvanceState |= 8;
    return biscSendLedCommand();
}


biscTurnOffAdvanceLed

decompiled_code:
__int64 biscTurnOffAdvanceLed()
{
ledState[0] ^= 8u;
return biscSendLedCommand();
}

canonical_decompiled_code:
__int64 func2()
{
ledState[0] ^= 8u;
return func0();
}

original_code:
int biscTurnOffAdvanceLed(void) {
    ledState.playAdvanceState ^= 1 << 3;
    return biscSendLedCommand();
}

canonical_original_code:
int biscTurnOffAdvanceLed() {
    ledState.playAdvanceState ^= 1 << 3;
    return biscSendLedCommand();
}


biscTurnOnPlayLed

decompiled_code:
__int64 biscTurnOnPlayLed()
{
ledState[0] |= 2u;
return biscSendLedCommand();
}

canonical_decompiled_code:
__int64 func3()
{
ledState[0] |= 2u;
return func0();
}

original_code:
int biscTurnOnPlayLed(void) {
    ledState.playAdvanceState |= 2;
    return biscSendLedCommand();
}

canonical_original_code:
int biscTurnOnPlayLed() {
    ledState.playAdvanceState |= 2;
    return biscSendLedCommand();
}


biscTurnOffPlayLed

decompiled_code:
__int64 biscTurnOffPlayLed()
{
ledState[0] ^= 2u;
return biscSendLedCommand();
}

canonical_decompiled_code:
__int64 func4()
{
ledState[0] ^= 2u;
return func0();
}

original_code:
int biscTurnOffPlayLed(void) {
    ledState.playAdvanceState ^= 1 << 1;
    return biscSendLedCommand();
}

canonical_original_code:
int biscTurnOffPlayLed() {
    ledState.playAdvanceState ^= 1 << 1;
    return biscSendLedCommand();
}


biscSetPowerLed

decompiled_code:
__int64 __fastcall biscSetPowerLed(unsigned __int8 a1, unsigned __int8 a2)
{
ledState[1] = a1;
ledState[2] = a2;
return biscSendLedCommand();
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned __int8 a1, unsigned __int8 a2)
{
ledState[1] = a1;
ledState[2] = a2;
return func0();
}

original_code:
int biscSetPowerLed(unsigned char color, unsigned char intensity) {
    ledState.powerLedColor = color;
    ledState.powerLedIntensity = intensity;
    return biscSendLedCommand();
}

variable_types:
color
unsigned char

variable_types:
intensity
unsigned char


biscTurnOffPowerLed

decompiled_code:
__int64 biscTurnOffPowerLed()
{
ledState[2] = 0;
return biscSendLedCommand();
}

canonical_decompiled_code:
__int64 func6()
{
ledState[2] = 0;
return func0();
}

original_code:
int biscTurnOffPowerLed(void) {
    ledState.powerLedIntensity = 0;
    return biscSendLedCommand();
}

canonical_original_code:
int biscTurnOffPowerLed() {
    ledState.powerLedIntensity = 0;
    return biscSendLedCommand();
}


biscFlashLed

decompiled_code:
__int64 __fastcall biscFlashLed(unsigned __int8 a1, int a2, unsigned int a3)
{
unsigned __int8 v4; // r15
unsigned __int8 v5; // r14
int v6; // er13
__int64 result; // rax
unsigned __int8 v8; // [rsp+Fh] [rbp-39h]

if ( (int)a3 <= 0 || a2 <= 0 || a1 > 2u )
return 0xFFFFFFFFLL;
v4 = ledState[0];
v5 = ledState[1];
v8 = ledState[2];
v6 = 0;
do
{
if ( a1 == 1 )
{
result = biscTurnOnPlayLed();
if ( (_DWORD)result == -1 )
return result;
result = biscWaitTime(a3);
if ( (_DWORD)result == -1 )
return result;
result = biscTurnOffPlayLed();
if ( (_DWORD)result == -1 )
return result;
}
else if ( a1 == 2 )
{
result = biscTurnOnAdvanceLed();
if ( (_DWORD)result == -1 )
return result;
result = biscWaitTime(a3);
if ( (_DWORD)result == -1 )
return result;
result = biscTurnOffAdvanceLed();
if ( (_DWORD)result == -1 )
return result;
}
else
{
result = biscSetPowerLed(ledState[1], 0xFFu);
if ( (_DWORD)result == -1 )
return result;
result = biscWaitTime(a3);
if ( (_DWORD)result == -1 )
return result;
result = biscSetPowerLed(ledState[1], 0);
if ( (_DWORD)result == -1 )
return result;
}
result = biscWaitTime(a3);
if ( (_DWORD)result == -1 )
return result;
++v6;
}
while ( a2 > v6 );
ledState[0] = v4;
ledState[1] = v5;
ledState[2] = v8;
return (unsigned int)-((unsigned int)biscSendLedCommand() == -1);
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned __int8 a1, int a2, unsigned int a3)
{
unsigned __int8 v4; // r15
unsigned __int8 v5; // r14
int v6; // er13
__int64 result; // rax
unsigned __int8 v8; // [rsp+Fh] [rbp-39h]

if ( (int)a3 <= 0 || a2 <= 0 || a1 > 2u )
return 0xFFFFFFFFLL;
v4 = ledState[0];
v5 = ledState[1];
v8 = ledState[2];
v6 = 0;
do
{
if ( a1 == 1 )
{
result = func3();
if ( (_DWORD)result == -1 )
return result;
result = biscWaitTime(a3);
if ( (_DWORD)result == -1 )
return result;
result = func4();
if ( (_DWORD)result == -1 )
return result;
}
else if ( a1 == 2 )
{
result = func1();
if ( (_DWORD)result == -1 )
return result;
result = biscWaitTime(a3);
if ( (_DWORD)result == -1 )
return result;
result = func2();
if ( (_DWORD)result == -1 )
return result;
}
else
{
result = func5(ledState[1], 0xFFu);
if ( (_DWORD)result == -1 )
return result;
result = biscWaitTime(a3);
if ( (_DWORD)result == -1 )
return result;
result = func5(ledState[1], 0);
if ( (_DWORD)result == -1 )
return result;
}
result = biscWaitTime(a3);
if ( (_DWORD)result == -1 )
return result;
++v6;
}
while ( a2 > v6 );
ledState[0] = v4;
ledState[1] = v5;
ledState[2] = v8;
return (unsigned int)-((unsigned int)func0() == -1);
}

original_code:
int biscFlashLed(char led, int numFlashes, int flashDurationMS) {
    if(led < 0 || led > 2) return -1;
    if(numFlashes <= 0) return -1;
    if(flashDurationMS <= 0) return -1;
    LedState stateBackup = ledState;
    for(int i=0; i<numFlashes; i++) {
        switch(led) {
            case 1:
                if(biscTurnOnPlayLed() == -1) return -1;
                break;
            case 2:
                if(biscTurnOnAdvanceLed() == -1) return -1;
                break;
            case 0:
                if(biscSetPowerLed(ledState.powerLedColor, 255) == -1) return -1;
                break;
            default:
                return -1;
        }
        if(biscWaitTime(flashDurationMS) == -1) return -1;
        switch(led) {
            case 1:
                if(biscTurnOffPlayLed() == -1) return -1;
                break;
            case 2:
                if(biscTurnOffAdvanceLed() == -1) return -1;
                break;
            case 0:
                if(biscSetPowerLed(ledState.powerLedColor, 0) == -1) return -1;
                break;
            default:
                return -1;
        }
        if(biscWaitTime(flashDurationMS) == -1) return -1;
    }
    ledState = stateBackup;
    if(biscSendLedCommand() == -1) return -1;
    return 0;
}

canonical_original_code:
int biscFlashLed(char led, int numFlashes, int flashDurationMS) {
    if(led < 0 || led > 2) return -1;
    if(numFlashes <= 0) return -1;
    if(flashDurationMS <= 0) return -1;
    struct LedState stateBackup = ledState;
    for(int i = 0; i<numFlashes; i++) {
        switch(led) {
            case 1:
                if(biscTurnOnPlayLed() == -1) return -1;
                break;
            case 2:
                if(biscTurnOnAdvanceLed() == -1) return -1;
                break;
            case 0:
                if(biscSetPowerLed(ledState.powerLedColor, 255) == -1) return -1;
                break;
            default:
                return -1;
        }
        if(biscWaitTime(flashDurationMS) == -1) return -1;
        switch(led) {
            case 1:
                if(biscTurnOffPlayLed() == -1) return -1;
                break;
            case 2:
                if(biscTurnOffAdvanceLed() == -1) return -1;
                break;
            case 0:
                if(biscSetPowerLed(ledState.powerLedColor, 0) == -1) return -1;
                break;
            default:
                return -1;
        }
        if(biscWaitTime(flashDurationMS) == -1) return -1;
    }
    ledState = stateBackup;
    if(biscSendLedCommand() == -1) return -1;
    return 0;
}

variable_types: led char

variable_types: numFlashes int

variable_types: flashDurationMS int

variable_types: struct LedState { char playAdvanceState; char powerLedColor; char powerLedIntensity; }

variable_types: i int


biscConnect

decompiled_code:
int __fastcall biscConnect(const char *a1)
{
int result; // eax
struct termios v2; // [rsp+0h] [rbp-58h] BYREF

if ( !a1 )
return -1;
result = open(a1, 2306);
deviceDescriptor = result;
if ( result != -1 )
{
tcgetattr(result, &v2);
v2.c_iflag = 5;
v2.c_lflag = 0;
v2.c_oflag = 0;
v2.c_cflag = 2224;
cfsetispeed(&v2, 0x1001u);
cfsetospeed(&v2, 0x1001u);
tcsetattr(deviceDescriptor, 0, &v2);
result = deviceDescriptor;
}
return result;
}

canonical_decompiled_code:
int __fastcall func0(const char *a1)
{
int result; // eax
struct termios v2; // [rsp+0h] [rbp-58h] BYREF

if ( !a1 )
return -1;
result = open(a1, 2306);
deviceDescriptor = result;
if ( result != -1 )
{
tcgetattr(result, &v2);
v2.c_iflag = 5;
v2.c_lflag = 0;
v2.c_oflag = 0;
v2.c_cflag = 2224;
cfsetispeed(&v2, 0x1001u);
cfsetospeed(&v2, 0x1001u);
tcsetattr(deviceDescriptor, 0, &v2);
result = deviceDescriptor;
}
return result;
}

original_code:
int biscConnect(char *device) {
    if(device == ((void *)0)) return -1;
    struct termios tty;
    if((deviceDescriptor = open(device, 02 | 0400 | 04000)) == -1) {
        return -1;
    }
    tcgetattr(deviceDescriptor, &tty);
    tty.c_iflag = 0000001 | 0000004;
    tty.c_lflag = 0;
    tty.c_oflag = 0;
    tty.c_cflag = 0000200 | 0000060 | 0004000;
    cfsetispeed(&tty, 0010001);
    cfsetospeed(&tty, 0010001);
    tcsetattr(deviceDescriptor, 0, &tty);
    return deviceDescriptor;
}

variable_types:
device
char *

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


biscDisconnect

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

if ( !deviceDescriptor )
return -1;
result = close(deviceDescriptor);
if ( result != -1 )
{
deviceDescriptor = 0;
result = 0;
}
return result;
}

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

if ( !deviceDescriptor )
return -1;
result = close(deviceDescriptor);
if ( result != -1 )
{
deviceDescriptor = 0;
result = 0;
}
return result;
}

original_code:
int biscDisconnect(void) {
    if(deviceDescriptor == 0) {
        return -1;
    }
    if(close(deviceDescriptor) == -1) {
        return -1;
    }
    deviceDescriptor = 0;
    return 0;
}

canonical_original_code:
int biscDisconnect() {
    if(deviceDescriptor == 0) {
        return -1;
    }
    if(close(deviceDescriptor) == -1) {
        return -1;
    }
    deviceDescriptor = 0;
    return 0;
}


biscSendByte

decompiled_code:
__int64 __fastcall biscSendByte(char a1)
{
char buf[12]; // [rsp+Ch] [rbp-Ch] BYREF

buf[0] = a1;
usleep(0x2710u);
return (unsigned int)-(write(deviceDescriptor, buf, 1uLL) != 1);
}

canonical_decompiled_code:
__int64 __fastcall func2(char a1)
{
char buf[12]; // [rsp+Ch] [rbp-Ch] BYREF

buf[0] = a1;
usleep(0x2710u);
return (unsigned int)-(write(deviceDescriptor, buf, 1uLL) != 1);
}

original_code:
int biscSendByte(unsigned char byte) {
    usleep(10000);
    return (write(deviceDescriptor, &byte, 1) == 1 ? 0 : -1);
}

variable_types:
byte
unsigned char


biscInit

decompiled_code:
int __fastcall biscInit(const char *a1)
{
int result; // eax

if ( !a1 )
return -1;
result = biscConnect(a1);
if ( result != -1 )
{
result = biscSendByte(128);
if ( result != -1 )
{
result = biscSendByte(131);
if ( result != -1 )
{
ledState[0] = 0;
ledState[1] = 0;
ledState[2] = -1;
result = 0;
}
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func3(const char *a1)
{
int result; // eax

if ( !a1 )
return -1;
result = func0(a1);
if ( result != -1 )
{
result = func2(128);
if ( result != -1 )
{
result = func2(131);
if ( result != -1 )
{
ledState[0] = 0;
ledState[1] = 0;
ledState[2] = -1;
result = 0;
}
}
}
return result;
}

original_code:
int biscInit(char *device) {
    if(device == ((void *)0)) return -1;
    if(biscConnect(device) == -1) return -1;
    if(biscSendByte(128) == -1) return -1;
    if(biscSendByte(131) == -1) return -1;
    ledState.playAdvanceState = 0;
    ledState.powerLedColor = 0;
    ledState.powerLedIntensity = 255;
    return 0;
}


biscChangeMode

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

if ( (unsigned __int8)(a1 + 125) > 1u && a1 != (char)0x80 )
return 0xFFFFFFFFLL;
result = biscSendByte(128);
if ( (_DWORD)result != -1 )
result = biscSendByte(a1);
return result;
}

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

if ( (unsigned __int8)(a1 + 125) > 1u && a1 != (char)0x80 )
return 0xFFFFFFFFLL;
result = func2(128);
if ( (_DWORD)result != -1 )
result = func2(a1);
return result;
}

original_code:
int biscChangeMode(unsigned char mode) {
    if(mode != 132 && mode != 131 && mode != 128) return -1;
    if(biscSendByte(128) == -1) return -1;
    return biscSendByte(mode);
}

variable_types:
mode
unsigned char


biscHighByte

decompiled_code:
__int64 __fastcall biscHighByte(int a1)
{
return (unsigned int)(a1 >> 8);
}

canonical_decompiled_code:
__int64 __fastcall func5(int a1)
{
return (unsigned int)(a1 >> 8);
}

original_code:
char biscHighByte(int num) {
    return (num >> 8) & 0xff;
}

variable_types: num int

return_type: char


biscLowByte

decompiled_code:
__int64 __fastcall biscLowByte(unsigned int a1)
{
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned int a1)
{
return a1;
}

original_code:
char biscLowByte(int num) {
    return num & 0xff;
}


biscSendInt

decompiled_code:
__int64 __fastcall biscSendInt(unsigned int a1)
{
char v1; // al
__int64 result; // rax
char v3; // al

v1 = biscHighByte(a1);
result = biscSendByte(v1);
if ( (_DWORD)result != -1 )
{
v3 = biscLowByte(a1);
result = (unsigned int)-((unsigned int)biscSendByte(v3) == -1);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned int a1)
{
char v1; // al
__int64 result; // rax
char v3; // al

v1 = func5(a1);
result = func2(v1);
if ( (_DWORD)result != -1 )
{
v3 = func6(a1);
result = (unsigned int)-((unsigned int)func2(v3) == -1);
}
return result;
}

original_code:
int biscSendInt(int num) {
    if(biscSendByte(biscHighByte(num)) == -1) return -1;
    if(biscSendByte(biscLowByte(num)) == -1) return -1;
    return 0;
}


biscGetVersion

decompiled_code:
const char *biscGetVersion()
{
return "1.0.0";
}

canonical_decompiled_code:
const char *func8()
{
return "1.0.0";
}

original_code:
char* biscGetVersion(void) {
    return "1.0.0";
}

return_type: char *


biscWaitTime

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

if ( a1 <= 0 )
return 0xFFFFFFFFLL;
result = biscSendByte(155);
if ( (_DWORD)result != -1 )
result = (unsigned int)-((unsigned int)biscSendByte((unsigned __int8)((1374389535LL * a1) >> 37) - (unsigned __int8)(a1 >> 31)) == -1);
return result;
}

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

if ( a1 <= 0 )
return 0xFFFFFFFFLL;
result = func2(155);
if ( (_DWORD)result != -1 )
result = (unsigned int)-((unsigned int)func2((unsigned __int8)((1374389535LL * a1) >> 37) - (unsigned __int8)(a1 >> 31)) == -1);
return result;
}

original_code:
int biscWaitTime(int mseconds) {
    if(mseconds <= 0) return -1;
    if(biscSendByte(155) == -1) return -1;
    if(biscSendByte(mseconds / 100) == -1) return -1;
    return 0;
}

variable_types: mseconds int


biscWaitDistance

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

result = biscSendByte(156);
if ( (_DWORD)result != -1 )
result = (unsigned int)-((unsigned int)biscSendInt(a1) == -1);
return result;
}

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

result = func2(156);
if ( (_DWORD)result != -1 )
result = (unsigned int)-((unsigned int)func7(a1) == -1);
return result;
}

original_code:
int biscWaitDistance(int distanceMM) {
    if(biscSendByte(156) == -1) return -1;
    if(biscSendInt(distanceMM) == -1) return -1;
    return 0;
}

variable_types: distanceMM int


biscWaitAngle

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

result = biscSendByte(157);
if ( (_DWORD)result != -1 )
result = (unsigned int)-((unsigned int)biscSendInt(a1) == -1);
return result;
}

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

result = func2(157);
if ( (_DWORD)result != -1 )
result = (unsigned int)-((unsigned int)func7(a1) == -1);
return result;
}

original_code:
int biscWaitAngle(int degrees) {
    if(biscSendByte(157) == -1) return -1;
    if(biscSendInt(degrees) == -1) return -1;
    return 0;
}

variable_types: degrees int


biscWaitEvent

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

if ( a1 > 0x16 )
return 0xFFFFFFFFLL;
result = biscSendByte(158);
if ( (_DWORD)result != -1 )
result = (unsigned int)-((unsigned int)biscSendByte(a1) == -1);
return result;
}

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

if ( a1 > 0x16 )
return 0xFFFFFFFFLL;
result = func2(158);
if ( (_DWORD)result != -1 )
result = (unsigned int)-((unsigned int)func2(a1) == -1);
return result;
}

original_code:
int biscWaitEvent(int eventCode) {
    if(eventCode < 0 || eventCode > 22) return -1;
    if(biscSendByte(158) == -1) return -1;
    if(biscSendByte(eventCode) == -1) return -1;
    return 0;
}

variable_types: eventCode int


biscDrive

decompiled_code:
__int64 __fastcall biscDrive(unsigned int a1, unsigned int a2)
{
__int64 result; // rax

if ( a1 + 500 > 0x3E8 )
return 0xFFFFFFFFLL;
result = biscSendByte(137LL);
if ( (_DWORD)result != -1 )
{
result = biscSendInt(a1);
if ( (_DWORD)result != -1 )
{
result = biscSendInt(a2);
if ( (_DWORD)result != -1 )
result = (unsigned int)-((unsigned int)biscSendLedCommand() == -1);
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1, unsigned int a2)
{
__int64 result; // rax

if ( a1 + 500 > 0x3E8 )
return 0xFFFFFFFFLL;
result = biscSendByte(137LL);
if ( (_DWORD)result != -1 )
{
result = biscSendInt(a1);
if ( (_DWORD)result != -1 )
{
result = biscSendInt(a2);
if ( (_DWORD)result != -1 )
result = (unsigned int)-((unsigned int)biscSendLedCommand() == -1);
}
}
return result;
}

original_code:
int biscDrive(int velocity, int radius) {
    if(velocity < -500 || velocity > 500) return -1;
    if(biscSendByte(137) == -1) return -1;
    if(biscSendInt(velocity) == -1) return -1;
    if(biscSendInt(radius) == -1) return -1;
    if(biscSendLedCommand() == -1) return -1;
    return 0;
}

variable_types: velocity int

variable_types: radius int


biscDirectDrive

decompiled_code:
__int64 __fastcall biscDirectDrive(unsigned int a1, unsigned int a2)
{
__int64 result; // rax

if ( a1 + 500 > 0x3E8 )
return 0xFFFFFFFFLL;
if ( a2 + 500 > 0x3E8 )
return 0xFFFFFFFFLL;
result = biscSendByte(145LL);
if ( (_DWORD)result != -1 )
{
result = biscSendInt(a1);
if ( (_DWORD)result != -1 )
result = (unsigned int)-((unsigned int)biscSendInt(a2) == -1);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1, unsigned int a2)
{
__int64 result; // rax

if ( a1 + 500 > 0x3E8 )
return 0xFFFFFFFFLL;
if ( a2 + 500 > 0x3E8 )
return 0xFFFFFFFFLL;
result = biscSendByte(145LL);
if ( (_DWORD)result != -1 )
{
result = biscSendInt(a1);
if ( (_DWORD)result != -1 )
result = (unsigned int)-((unsigned int)biscSendInt(a2) == -1);
}
return result;
}

original_code:
int biscDirectDrive(int rightVelocity, int leftVelocity) {
    if(rightVelocity < -500 || rightVelocity > 500) return -1;
    if(leftVelocity < -500 || leftVelocity > 500) return -1;
    if(biscSendByte(145) == -1) return -1;
    if(biscSendInt(rightVelocity) == -1) return -1;
    if(biscSendInt(leftVelocity) == -1) return -1;
    return 0;
}

variable_types: rightVelocity int

variable_types: leftVelocity int


biscDriveStraight

decompiled_code:
__int64 __fastcall biscDriveStraight(unsigned int a1)
{
return biscDrive(a1, 0x7FFFu);
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1)
{
return func0(a1, 0x7FFFu);
}

original_code:
int biscDriveStraight(int velocity) {
    return biscDrive(velocity, 0x7FFF);
}


biscSpin

decompiled_code:
__int64 __fastcall biscSpin(unsigned int a1)
{
return biscDrive(a1, 0xFFFFu);
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int a1)
{
return func0(a1, 0xFFFFu);
}

original_code:
int biscSpin(int velocity) {
    return biscDrive(velocity, 0xFFFF);
}


biscDriveStop

decompiled_code:
__int64 biscDriveStop()
{
return biscDrive(0, 0);
}

canonical_decompiled_code:
__int64 func4()
{
return func0(0, 0);
}

original_code:
int biscDriveStop(void) {
    return biscDrive(0, 0);
}

canonical_original_code:
int biscDriveStop() {
    return biscDrive(0, 0);
}


biscTimedDrive

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

if ( a3 <= 0 )
return 0xFFFFFFFFLL;
result = biscDrive(a1, a2);
if ( (_DWORD)result != -1 )
{
result = biscWaitTime((unsigned int)a3);
if ( (_DWORD)result != -1 )
result = (unsigned int)-((unsigned int)biscDriveStop() == -1);
}
return result;
}

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

if ( a3 <= 0 )
return 0xFFFFFFFFLL;
result = func0(a1, a2);
if ( (_DWORD)result != -1 )
{
result = biscWaitTime((unsigned int)a3);
if ( (_DWORD)result != -1 )
result = (unsigned int)-((unsigned int)func4() == -1);
}
return result;
}

original_code:
int biscTimedDrive(int velocity, int radius, int mseconds) {
    if(mseconds <= 0) return -1;
    if(biscDrive(velocity, radius) == -1) return -1;
    if(biscWaitTime(mseconds) == -1) return -1;
    if(biscDriveStop() == -1) return -1;
    return 0;
}


biscTimedDriveStraight

decompiled_code:
__int64 __fastcall biscTimedDriveStraight(unsigned int a1, int a2)
{
return biscTimedDrive(a1, 0x7FFFu, a2);
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned int a1, int a2)
{
return func5(a1, 0x7FFFu, a2);
}

original_code:
int biscTimedDriveStraight(int velocity, int mseconds) {
    return biscTimedDrive(velocity, 0x7FFF, mseconds);
}


biscDriveDistance

decompiled_code:
__int64 __fastcall biscDriveDistance(signed int a1, unsigned int a2, int a3)
{
__int64 result; // rax

if ( a1 > 0 && a3 < 0 )
return 0xFFFFFFFFLL;
if ( a1 < 0 && a3 > 0 )
return 0xFFFFFFFFLL;
result = biscDrive(a1, a2);
if ( (_DWORD)result != -1 )
{
result = biscWaitDistance((unsigned int)a3);
if ( (_DWORD)result != -1 )
result = (unsigned int)-((unsigned int)biscDriveStop() == -1);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(signed int a1, unsigned int a2, int a3)
{
__int64 result; // rax

if ( a1 > 0 && a3 < 0 )
return 0xFFFFFFFFLL;
if ( a1 < 0 && a3 > 0 )
return 0xFFFFFFFFLL;
result = func0(a1, a2);
if ( (_DWORD)result != -1 )
{
result = biscWaitDistance((unsigned int)a3);
if ( (_DWORD)result != -1 )
result = (unsigned int)-((unsigned int)func4() == -1);
}
return result;
}

original_code:
int biscDriveDistance(int velocity, int radius, int distanceMM) {
    if((velocity > 0 && distanceMM < 0) || (velocity < 0 && distanceMM > 0)) return -1;
    if(biscDrive(velocity, radius) == -1) return -1;
    if(biscWaitDistance(distanceMM) == -1) return -1;
    if(biscDriveStop() == -1) return -1;
    return 0;
}


biscDriveDistanceStraight

decompiled_code:
__int64 __fastcall biscDriveDistanceStraight(signed int a1, int a2)
{
return biscDriveDistance(a1, 0x7FFFu, a2);
}

canonical_decompiled_code:
__int64 __fastcall func8(signed int a1, int a2)
{
return func7(a1, 0x7FFFu, a2);
}

original_code:
int biscDriveDistanceStraight(int velocity, int distanceMM) {
    return biscDriveDistance(velocity, 0x7FFF, distanceMM);
}


biscTimedSpin

decompiled_code:
__int64 __fastcall biscTimedSpin(unsigned int a1, int a2)
{
__int64 result; // rax

if ( a2 <= 0 )
return 0xFFFFFFFFLL;
result = biscSpin(a1);
if ( (_DWORD)result != -1 )
{
result = biscWaitTime((unsigned int)a2);
if ( (_DWORD)result != -1 )
result = (unsigned int)-((unsigned int)biscDriveStop() == -1);
}
return result;
}

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

if ( a2 <= 0 )
return 0xFFFFFFFFLL;
result = func3(a1);
if ( (_DWORD)result != -1 )
{
result = biscWaitTime((unsigned int)a2);
if ( (_DWORD)result != -1 )
result = (unsigned int)-((unsigned int)func4() == -1);
}
return result;
}

original_code:
int biscTimedSpin(int velocity, int mseconds) {
    if(mseconds <= 0) return -1;
    if(biscSpin(velocity) == -1) return -1;
    if(biscWaitTime(mseconds) == -1) return -1;
    if(biscDriveStop() == -1) return -1;
    return 0;
}


biscSpinAngle

decompiled_code:
__int64 __fastcall biscSpinAngle(unsigned int a1, unsigned int a2)
{
__int64 result; // rax

result = biscSpin(a1);
if ( (_DWORD)result != -1 )
{
result = biscWaitAngle(a2);
if ( (_DWORD)result != -1 )
result = (unsigned int)-((unsigned int)biscDriveStop() == -1);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(unsigned int a1, unsigned int a2)
{
__int64 result; // rax

result = func3(a1);
if ( (_DWORD)result != -1 )
{
result = biscWaitAngle(a2);
if ( (_DWORD)result != -1 )
result = (unsigned int)-((unsigned int)func4() == -1);
}
return result;
}

original_code:
int biscSpinAngle(int velocity, int degrees) {
    if(biscSpin(velocity) == -1) return -1;
    if(biscWaitAngle(degrees) == -1) return -1;
    if(biscDriveStop() == -1) return -1;
    return 0;
}


biscDefineSong

decompiled_code:
__int64 __fastcall biscDefineSong(unsigned __int8 a1, __int64 a2, __int64 a3, int a4)
{
unsigned __int8 v5; // r14
int v6; // ebp
__int64 result; // rax
__int64 i; // rbx
unsigned __int8 v9; // di

if ( a1 > 0xFu )
return 0xFFFFFFFFLL;
v5 = a4;
v6 = a4 - 1;
if ( (unsigned int)(a4 - 1) > 0xF )
return 0xFFFFFFFFLL;
result = biscSendByte(140LL);
if ( (_DWORD)result != -1 )
{
result = biscSendByte(a1);
if ( (_DWORD)result != -1 )
{
result = biscSendByte(v5);
if ( (_DWORD)result != -1 )
{
for ( i = 0LL; ; ++i )
{
v9 = *(_BYTE *)(a2 + i);
if ( (unsigned __int8)(v9 - 31) > 0x60u )
break;
result = biscSendByte(v9);
if ( (_DWORD)result == -1 )
return result;
result = biscSendByte(*(unsigned __int8 *)(a3 + i));
if ( (_DWORD)result == -1 )
return result;
if ( v6 == i )
return 0LL;
}
result = 0xFFFFFFFFLL;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int8 a1, __int64 a2, __int64 a3, int a4)
{
unsigned __int8 v5; // r14
int v6; // ebp
__int64 result; // rax
__int64 i; // rbx
unsigned __int8 v9; // di

if ( a1 > 0xFu )
return 0xFFFFFFFFLL;
v5 = a4;
v6 = a4 - 1;
if ( (unsigned int)(a4 - 1) > 0xF )
return 0xFFFFFFFFLL;
result = biscSendByte(140LL);
if ( (_DWORD)result != -1 )
{
result = biscSendByte(a1);
if ( (_DWORD)result != -1 )
{
result = biscSendByte(v5);
if ( (_DWORD)result != -1 )
{
for ( i = 0LL; ; ++i )
{
v9 = *(_BYTE *)(a2 + i);
if ( (unsigned __int8)(v9 - 31) > 0x60u )
break;
result = biscSendByte(v9);
if ( (_DWORD)result == -1 )
return result;
result = biscSendByte(*(unsigned __int8 *)(a3 + i));
if ( (_DWORD)result == -1 )
return result;
if ( v6 == i )
return 0LL;
}
result = 0xFFFFFFFFLL;
}
}
}
return result;
}

original_code:
int biscDefineSong(unsigned char songNum, unsigned char *notes, unsigned char *notesDurations, unsigned int notesLen) {
    if(songNum > 15) return -1;
    if(sizeof(notes) != sizeof(notesDurations)) return -1;
    if(notesLen <= 0 || notesLen > 16) return -1;
    if(biscSendByte(140) == -1) return -1;
    if(biscSendByte(songNum) == -1) return -1;
    if(biscSendByte(notesLen) == -1) return -1;
    for(unsigned int i=0; i<notesLen; i++) {
        if(notes[i] < 31 || notes[i] > 127) return -1;
        if(biscSendByte(notes[i]) == -1) return -1;
        if(biscSendByte(notesDurations[i]) == -1) return -1;
    }
    return 0;
}

canonical_original_code:
int biscDefineSong(unsigned char songNum, unsigned char *notes, unsigned char *notesDurations, unsigned int notesLen) {
    if(songNum > 15) return -1;
    if(sizeof(notes) != sizeof(notesDurations)) return -1;
    if(notesLen <= 0 || notesLen > 16) return -1;
    if(biscSendByte(140) == -1) return -1;
    if(biscSendByte(songNum) == -1) return -1;
    if(biscSendByte(notesLen) == -1) return -1;
    for(unsigned int i = 0; i<notesLen; i++) {
        if(notes[i] < 31 || notes[i] > 127) return -1;
        if(biscSendByte(notes[i]) == -1) return -1;
        if(biscSendByte(notesDurations[i]) == -1) return -1;
    }
    return 0;
}

variable_types:
songNum
unsigned char

variable_types:
notes
unsigned char *

variable_types:
notesDurations
unsigned char *

variable_types:
notesLen
unsigned int

variable_types:
i
unsigned int


biscPlaySong

decompiled_code:
__int64 __fastcall biscPlaySong(unsigned __int8 a1)
{
__int64 result; // rax

if ( a1 > 0xFu )
return 0xFFFFFFFFLL;
result = biscSendByte(141LL);
if ( (_DWORD)result != -1 )
result = (unsigned int)-((unsigned int)biscSendByte(a1) == -1);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned __int8 a1)
{
__int64 result; // rax

if ( a1 > 0xFu )
return 0xFFFFFFFFLL;
result = biscSendByte(141LL);
if ( (_DWORD)result != -1 )
result = (unsigned int)-((unsigned int)biscSendByte(a1) == -1);
return result;
}

original_code:
int biscPlaySong(unsigned char songNum) {
    if(songNum > 15) return -1;
    if(biscSendByte(141) == -1) return -1;
    if(biscSendByte(songNum) == -1) return -1;
    return 0;
}


biscBeep

decompiled_code:
__int64 biscBeep()
{
__int64 result; // rax
char v1; // [rsp+Eh] [rbp-Ah] BYREF
char v2[9]; // [rsp+Fh] [rbp-9h] BYREF

v2[0] = 70;
v1 = 10;
result = biscDefineSong(0, (__int64)v2, (__int64)&v1, 1);
if ( (_DWORD)result != -1 )
result = biscPlaySong(0);
return result;
}

canonical_decompiled_code:
__int64 func2()
{
__int64 result; // rax
char v1; // [rsp+Eh] [rbp-Ah] BYREF
char v2[9]; // [rsp+Fh] [rbp-9h] BYREF

v2[0] = 70;
v1 = 10;
result = func0(0, (__int64)v2, (__int64)&v1, 1);
if ( (_DWORD)result != -1 )
result = func1(0);
return result;
}

original_code:
int biscBeep(void) {
    unsigned char beep[] = {70};
    unsigned char beepd[] = {10};
    if(biscDefineSong(0, beep, beepd, sizeof(beepd)) == -1) return -1;
    return biscPlaySong(0);
}

canonical_original_code:
int biscBeep() {
    unsigned char beep[] = {70};
    unsigned char beepd[] = {10};
    if(biscDefineSong(0, beep, beepd, sizeof(beepd)) == -1) return -1;
    return biscPlaySong(0);
}

variable_types:
beep
unsigned char []

variable_types:
beepd
unsigned char []


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rax
__int64 v4; // r12
__int64 v5; // rax
__int64 v6; // r13
unsigned int i; // ebp
int v8; // ebx
int v9; // eax
struct timespec v11; // [rsp+0h] [rbp-38h] BYREF

v3 = init_requests_queue(&request_mutex, &got_request, envp);
if ( !v3 )
_assert_fail("requests", "main.c", 0x2Bu, "main");
v4 = v3;
v5 = init_handler_threads_pool(&request_mutex, &got_request, v3);
v6 = v5;
if ( !v5 )
_assert_fail("handler_threads", "main.c", 0x30u, "main");
add_handler_thread(v5);
add_handler_thread(v6);
add_handler_thread(v6);
for ( i = 0; i != 600; ++i )
{
add_request(v4, i);
v8 = get_requests_number(v4);
v9 = get_handler_threads_number(v6);
if ( v8 > 15 && v9 <= 13 )
{
printf("main: adding thread: '%d' requests, '%d' threads\n", (unsigned int)v8, (unsigned int)v9);
add_handler_thread(v6);
}
else if ( v8 <= 2 && v9 > 3 )
{
printf("main: deleting thread: '%d' requests, '%d' threads\n", (unsigned int)v8, (unsigned int)v9);
delete_handler_thread(v6);
}
if ( rand() > 1610612733 )
{
v11.tv_sec = 0LL;
v11.tv_nsec = 1LL;
nanosleep(&v11, 0LL);
}
}
pthread_mutex_lock(&request_mutex);
done_creating_requests = 1;
pthread_cond_broadcast(&got_request);
pthread_mutex_unlock(&request_mutex);
delete_handler_threads_pool(v6);
delete_requests_queue(v4);
puts("Glory, we are done.");
return 0;
}

original_code:
int
main(int argc, char* argv[])
{
    int i;
    struct timespec delay;
    struct requests_queue* requests = ((void *)0);
    struct handler_threads_pool* handler_threads = ((void *)0);
    requests = init_requests_queue(&request_mutex, &got_request);
    ((void) sizeof ((requests) ? 1 : 0), __extension__ ({ if (requests) ; else __assert_fail ("requests", "main.c", 43, __extension__ __PRETTY_FUNCTION__); }));
    handler_threads =
 init_handler_threads_pool(&request_mutex, &got_request, requests);
    ((void) sizeof ((handler_threads) ? 1 : 0), __extension__ ({ if (handler_threads) ; else __assert_fail ("handler_threads", "main.c", 48, __extension__ __PRETTY_FUNCTION__); }));
    for (i=0; i<3; i++) {
 add_handler_thread(handler_threads);
    }
    for (i=0; i<600; i++) {
 int num_requests;
 int num_threads;
 add_request(requests, i);
 num_requests = get_requests_number(requests);
 num_threads = get_handler_threads_number(handler_threads);
 if (num_requests > 15 &&
     num_threads < 14) {
  printf("main: adding thread: '%d' requests, '%d' threads\n",
         num_requests, num_threads);
  add_handler_thread(handler_threads);
 }
 if (num_requests < 3 &&
   num_threads > 3) {
     printf("main: deleting thread: '%d' requests, '%d' threads\n",
     num_requests, num_threads);
     delete_handler_thread(handler_threads);
 }
 if (rand() > 3*(2147483647/4)) {
     delay.tv_sec = 0;
     delay.tv_nsec = 1;
     nanosleep(&delay, ((void *)0));
 }
    }
    {
        int rc;
        rc = pthread_mutex_lock(&request_mutex);
        done_creating_requests = 1;
        rc = pthread_cond_broadcast(&got_request);
        rc = pthread_mutex_unlock(&request_mutex);
    }
    delete_handler_threads_pool(handler_threads);
    delete_requests_queue(requests);
    printf("Glory,  we are done.\n");
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    int i;
    struct timespec delay;
    struct requests_queue *requests = ((void *)0);
    struct handler_threads_pool *handler_threads = ((void *)0);
    requests = init_requests_queue(&request_mutex, &got_request);
    ((void) sizeof ((requests) ? 1 : 0), __extension__ ({ if (requests) ; else __assert_fail ("requests", "main.c", 43, __extension__ __PRETTY_FUNCTION__); }));
    handler_threads =
 init_handler_threads_pool(&request_mutex, &got_request, requests);
    ((void) sizeof ((handler_threads) ? 1 : 0), __extension__ ({ if (handler_threads) ; else __assert_fail ("handler_threads", "main.c", 48, __extension__ __PRETTY_FUNCTION__); }));
    for (i=0; i<3; i++) {
 add_handler_thread(handler_threads);
    }
    for (i=0; i<600; i++) {
 int num_requests;
 int num_threads;
 add_request(requests, i);
 num_requests = get_requests_number(requests);
 num_threads = get_handler_threads_number(handler_threads);
 if (num_requests > 15 &&
     num_threads < 14) {
  printf("main: adding thread: '%d' requests, '%d' threads\n",
         num_requests, num_threads);
  add_handler_thread(handler_threads);
 }
 if (num_requests < 3 &&
   num_threads > 3) {
     printf("main: deleting thread: '%d' requests, '%d' threads\n",
     num_requests, num_threads);
     delete_handler_thread(handler_threads);
 }
 if (rand() > 3*(2147483647/4)) {
     delay.tv_sec = 0;
     delay.tv_nsec = 1;
     nanosleep(&delay, ((void *)0));
 }
    }
    {
        int rc;
        rc = pthread_mutex_lock(&request_mutex);
        done_creating_requests = 1;
        rc = pthread_cond_broadcast(&got_request);
        rc = pthread_mutex_unlock(&request_mutex);
    }
    delete_handler_threads_pool(handler_threads);
    delete_requests_queue(requests);
    printf("Glory,  we are done.\n");
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: i int

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

variable_types: struct requests_queue { struct request { int number; struct request *next; } *requests; struct request *last_request; int num_requests; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } *p_mutex; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } *p_cond_var; } *

variable_types: struct handler_threads_pool { struct handler_thread { unsigned long thread; int thr_id; struct handler_thread *next; } *threads; struct handler_thread *last_thread; int num_threads; int max_thr_id; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } *p_mutex; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } *p_cond_var; struct requests_queue { struct request { int number; struct request *next; } *requests; struct request *last_request; int num_requests; union pthread_mutex_t *p_mutex; union pthread_cond_t *p_cond_var; } *requests; } *

variable_types: rc int

user_defined_types: struct requests_queue { struct request *requests; struct request *last_request; int num_requests; union pthread_mutex_t *p_mutex; union pthread_cond_t *p_cond_var; }

user_defined_types: struct handler_threads_pool { struct handler_thread *threads; struct handler_thread *last_thread; int num_threads; int max_thr_id; union pthread_mutex_t *p_mutex; union pthread_cond_t *p_cond_var; struct requests_queue *requests; }

user_defined_types: union pthread_mutex_t { struct __pthread_mutex_s __data; char __size[40]; long __align; }

user_defined_types: union pthread_cond_t { struct __pthread_cond_s __data; char __size[48]; long long __align; }

user_defined_types: struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list __list; }


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

if ( !a1 )
_assert_fail("pool", "handler_threads_pool.c", 0x5Eu, "remove_first_handler_thread");
result = 0LL;
if ( *((int *)a1 + 4) > 0 )
{
result = *a1;
if ( *a1 )
{
*a1 = *(_QWORD *)(result + 16);
*(_QWORD *)(result + 16) = 0LL;
--*((_DWORD *)a1 + 4);
}
}
return result;
}

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

if ( !a1 )
_assert_fail("pool", "handler_threads_pool.c", 0x5Eu, "remove_first_handler_thread");
result = 0LL;
if ( *((int *)a1 + 4) > 0 )
{
result = *a1;
if ( *a1 )
{
*a1 = *(_QWORD *)(result + 16);
*(_QWORD *)(result + 16) = 0LL;
--*((_DWORD *)a1 + 4);
}
}
return result;
}

original_code:
static struct handler_thread*
remove_first_handler_thread(struct handler_threads_pool* pool)
{
    struct handler_thread* a_thread = ((void *)0);
    ((void) sizeof ((pool) ? 1 : 0), __extension__ ({ if (pool) ; else __assert_fail ("pool", "handler_threads_pool.c", 94, __extension__ __PRETTY_FUNCTION__); }));
    if (pool->num_threads > 0 && pool->threads) {
 a_thread = pool->threads;
 pool->threads = a_thread->next;
 a_thread->next = ((void *)0);
        pool->num_threads--;
    }
    return a_thread;
}

canonical_original_code:
static struct handler_thread *remove_first_handler_thread(struct handler_threads_pool *pool)
{
    struct handler_thread *a_thread = ((void *)0);
    ((void) sizeof ((pool) ? 1 : 0), __extension__ ({ if (pool) ; else __assert_fail ("pool", "handler_threads_pool.c", 94, __extension__ __PRETTY_FUNCTION__); }));
    if (pool->num_threads > 0 && pool->threads) {
 a_thread = pool->threads;
 pool->threads = a_thread->next;
 a_thread->next = ((void *)0);
        pool->num_threads--;
    }
    return a_thread;
}

variable_types: struct handler_threads_pool { struct handler_thread *threads; struct handler_thread *last_thread; int num_threads; int max_thr_id; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } *p_mutex; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } *p_cond_var; struct requests_queue { struct request { int number; struct request *next; } *requests; struct request *last_request; int num_requests; union pthread_mutex_t *p_mutex; union pthread_cond_t *p_cond_var; } *requests; } *

variable_types: struct handler_thread { unsigned long thread; int thr_id; struct handler_thread *next; } *


decompiled_code:
int __fastcall add_handler_thread(__int64 a1)
{
_DWORD *v1; // rbp
int v2; // er12
_QWORD *v3; // rax
__int64 v4; // rax

if ( !a1 )
_assert_fail("pool", "handler_threads_pool.c", 0x2Cu, "add_handler_thread");
v1 = malloc(0x18uLL);
if ( !v1 )
{
fwrite("add_handler_thread: out of memory. exiting\n", 1uLL, 0x2BuLL, stderr);
exit(1);
}
v2 = *(_DWORD *)(a1 + 20);
*(_DWORD *)(a1 + 20) = v2 + 1;
v1[2] = v2;
*((_QWORD *)v1 + 2) = 0LL;
v3 = malloc(0x20uLL);
if ( !v3 )
{
fwrite("add_handler_thread: out of memory. exiting\n", 1uLL, 0x2BuLL, stderr);
exit(1);
}
*(_DWORD *)v3 = v2;
v3[1] = *(_QWORD *)(a1 + 24);
v3[2] = *(_QWORD *)(a1 + 32);
v3[3] = *(_QWORD *)(a1 + 40);
LODWORD(v4) = pthread_create((pthread_t *)v1, 0LL, (void *(*)(void *))&handle_requests_loop, v3);
if ( *(_DWORD *)(a1 + 16) )
{
v4 = *(_QWORD *)(a1 + 8);
*(_QWORD *)(v4 + 16) = v1;
}
else
{
*(_QWORD *)a1 = v1;
}
*(_QWORD *)(a1 + 8) = v1;
++*(_DWORD *)(a1 + 16);
return v4;
}

canonical_decompiled_code:
int __fastcall func1(__int64 a1)
{
_DWORD *v1; // rbp
int v2; // er12
_QWORD *v3; // rax
__int64 v4; // rax

if ( !a1 )
_assert_fail("pool", "handler_threads_pool.c", 0x2Cu, "add_handler_thread");
v1 = malloc(0x18uLL);
if ( !v1 )
{
fwrite("add_handler_thread: out of memory. exiting\n", 1uLL, 0x2BuLL, stderr);
exit(1);
}
v2 = *(_DWORD *)(a1 + 20);
*(_DWORD *)(a1 + 20) = v2 + 1;
v1[2] = v2;
*((_QWORD *)v1 + 2) = 0LL;
v3 = malloc(0x20uLL);
if ( !v3 )
{
fwrite("add_handler_thread: out of memory. exiting\n", 1uLL, 0x2BuLL, stderr);
exit(1);
}
*(_DWORD *)v3 = v2;
v3[1] = *(_QWORD *)(a1 + 24);
v3[2] = *(_QWORD *)(a1 + 32);
v3[3] = *(_QWORD *)(a1 + 40);
LODWORD(v4) = pthread_create((pthread_t *)v1, 0LL, (void *(*)(void *))&handle_requests_loop, v3);
if ( *(_DWORD *)(a1 + 16) )
{
v4 = *(_QWORD *)(a1 + 8);
*(_QWORD *)(v4 + 16) = v1;
}
else
{
*(_QWORD *)a1 = v1;
}
*(_QWORD *)(a1 + 8) = v1;
++*(_DWORD *)(a1 + 16);
return v4;
}

original_code:
void
add_handler_thread(struct handler_threads_pool* pool)
{
    struct handler_thread* a_thread;
    struct handler_thread_params* params;
    ((void) sizeof ((pool) ? 1 : 0), __extension__ ({ if (pool) ; else __assert_fail ("pool", "handler_threads_pool.c", 44, __extension__ __PRETTY_FUNCTION__); }));
    a_thread = (struct handler_thread*)malloc(sizeof(struct handler_thread));
    if (!a_thread) {
 fprintf(stderr, "add_handler_thread: out of memory. exiting\n");
 exit(1);
    }
    a_thread->thr_id = pool->max_thr_id++;
    a_thread->next = ((void *)0);
    params = (struct handler_thread_params*)
                            malloc(sizeof(struct handler_thread_params));
    if (!params) {
 fprintf(stderr, "add_handler_thread: out of memory. exiting\n");
 exit(1);
    }
    params->thread_id = a_thread->thr_id;
    params->request_mutex = pool->p_mutex;
    params->got_request = pool->p_cond_var;
    params->requests = pool->requests;
    pthread_create(&a_thread->thread,
     ((void *)0),
     handle_requests_loop,
     (void*)params);
    if (pool->num_threads == 0) {
        pool->threads = a_thread;
        pool->last_thread = a_thread;
    }
    else {
        pool->last_thread->next = a_thread;
        pool->last_thread = a_thread;
    }
    pool->num_threads++;
}

canonical_original_code:
void add_handler_thread(struct handler_threads_pool *pool)
{
    struct handler_thread *a_thread;
    struct handler_thread_params *params;
    ((void) sizeof ((pool) ? 1 : 0), __extension__ ({ if (pool) ; else __assert_fail ("pool", "handler_threads_pool.c", 44, __extension__ __PRETTY_FUNCTION__); }));
    a_thread = (struct handler_thread *)malloc(sizeof(struct handler_thread));
    if (!a_thread) {
 fprintf(stderr, "add_handler_thread: out of memory. exiting\n");
 exit(1);
    }
    a_thread->thr_id = pool->max_thr_id++;
    a_thread->next = ((void *)0);
    params = (struct handler_thread_params *)
                            malloc(sizeof(struct handler_thread_params));
    if (!params) {
 fprintf(stderr, "add_handler_thread: out of memory. exiting\n");
 exit(1);
    }
    params->thread_id = a_thread->thr_id;
    params->request_mutex = pool->p_mutex;
    params->got_request = pool->p_cond_var;
    params->requests = pool->requests;
    pthread_create(&a_thread->thread,
     ((void *)0),
     handle_requests_loop,
     (void *)params);
    if (pool->num_threads == 0) {
        pool->threads = a_thread;
        pool->last_thread = a_thread;
    }
    else {
        pool->last_thread->next = a_thread;
        pool->last_thread = a_thread;
    }
    pool->num_threads++;
}

variable_types: struct handler_thread_params { int thread_id; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } *request_mutex; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } *got_request; struct requests_queue { struct request { int number; struct request *next; } *requests; struct request *last_request; int num_requests; union pthread_mutex_t *p_mutex; union pthread_cond_t *p_cond_var; } *requests; } *

return_type: void

user_defined_types: struct handler_thread { unsigned long thread; int thr_id; struct handler_thread *next; }

user_defined_types: struct handler_thread_params { int thread_id; union pthread_mutex_t *request_mutex; union pthread_cond_t *got_request; struct requests_queue *requests; }


decompiled_code:
void __fastcall delete_handler_thread(__int64 *a1)
{
pthread_t *v1; // rax
pthread_t *v2; // rbx

if ( !a1 )
_assert_fail("pool", "handler_threads_pool.c", 0x71u, "delete_handler_thread");
v1 = (pthread_t *)remove_first_handler_thread(a1);
v2 = v1;
if ( v1 )
{
pthread_cancel(*v1);
free(v2);
}
}

canonical_decompiled_code:
void __fastcall func2(__int64 *a1)
{
pthread_t *v1; // rax
pthread_t *v2; // rbx

if ( !a1 )
_assert_fail("pool", "handler_threads_pool.c", 0x71u, "delete_handler_thread");
v1 = (pthread_t *)func0(a1);
v2 = v1;
if ( v1 )
{
pthread_cancel(*v1);
free(v2);
}
}

original_code:
void
delete_handler_thread(struct handler_threads_pool* pool)
{
    struct handler_thread* a_thread;
    ((void) sizeof ((pool) ? 1 : 0), __extension__ ({ if (pool) ; else __assert_fail ("pool", "handler_threads_pool.c", 113, __extension__ __PRETTY_FUNCTION__); }));
    a_thread = remove_first_handler_thread(pool);
    if (a_thread) {
 pthread_cancel(a_thread->thread);
        free(a_thread);
    }
}

canonical_original_code:
void delete_handler_thread(struct handler_threads_pool *pool)
{
    struct handler_thread *a_thread;
    ((void) sizeof ((pool) ? 1 : 0), __extension__ ({ if (pool) ; else __assert_fail ("pool", "handler_threads_pool.c", 113, __extension__ __PRETTY_FUNCTION__); }));
    a_thread = remove_first_handler_thread(pool);
    if (a_thread) {
 pthread_cancel(a_thread->thread);
        free(a_thread);
    }
}

user_defined_types: struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; }

user_defined_types: struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; }

user_defined_types: struct request { int number; struct request *next; }


decompiled_code:
__int64 __fastcall get_handler_threads_number(__int64 a1)
{
if ( !a1 )
_assert_fail("pool", "handler_threads_pool.c", 0x7Fu, "get_handler_threads_number");
return *(unsigned int *)(a1 + 16);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
if ( !a1 )
_assert_fail("pool", "handler_threads_pool.c", 0x7Fu, "get_handler_threads_number");
return *(unsigned int *)(a1 + 16);
}

original_code:
int
get_handler_threads_number(struct handler_threads_pool* pool)
{
    ((void) sizeof ((pool) ? 1 : 0), __extension__ ({ if (pool) ; else __assert_fail ("pool", "handler_threads_pool.c", 127, __extension__ __PRETTY_FUNCTION__); }));
    return pool->num_threads;
}

canonical_original_code:
int get_handler_threads_number(struct handler_threads_pool *pool)
{
    ((void) sizeof ((pool) ? 1 : 0), __extension__ ({ if (pool) ; else __assert_fail ("pool", "handler_threads_pool.c", 127, __extension__ __PRETTY_FUNCTION__); }));
    return pool->num_threads;
}


decompiled_code:
void __fastcall delete_handler_threads_pool(__int64 a1)
{
pthread_t *v1; // rax
pthread_t *v2; // rbx
void *thread_return; // [rsp+8h] [rbp-20h] BYREF

if ( !a1 )
_assert_fail("pool", "handler_threads_pool.c", 0x8Fu, "delete_handler_threads_pool");
while ( *(int *)(a1 + 16) > 0 )
{
v1 = (pthread_t *)remove_first_handler_thread((__int64 *)a1);
v2 = v1;
if ( !v1 )
_assert_fail("a_thread", "handler_threads_pool.c", 0x94u, "delete_handler_threads_pool");
pthread_join(*v1, &thread_return);
free(v2);
}
}

canonical_decompiled_code:
void __fastcall func4(__int64 a1)
{
pthread_t *v1; // rax
pthread_t *v2; // rbx
void *thread_return; // [rsp+8h] [rbp-20h] BYREF

if ( !a1 )
_assert_fail("pool", "handler_threads_pool.c", 0x8Fu, "delete_handler_threads_pool");
while ( *(int *)(a1 + 16) > 0 )
{
v1 = (pthread_t *)func0((__int64 *)a1);
v2 = v1;
if ( !v1 )
_assert_fail("a_thread", "handler_threads_pool.c", 0x94u, "delete_handler_threads_pool");
pthread_join(*v1, &thread_return);
free(v2);
}
}

original_code:
void
delete_handler_threads_pool(struct handler_threads_pool* pool)
{
    void* thr_retval;
    struct handler_thread* a_thread;
    ((void) sizeof ((pool) ? 1 : 0), __extension__ ({ if (pool) ; else __assert_fail ("pool", "handler_threads_pool.c", 143, __extension__ __PRETTY_FUNCTION__); }));
    while (pool->num_threads > 0) {
 a_thread = remove_first_handler_thread(pool);
 ((void) sizeof ((a_thread) ? 1 : 0), __extension__ ({ if (a_thread) ; else __assert_fail ("a_thread", "handler_threads_pool.c", 148, __extension__ __PRETTY_FUNCTION__); }));
 pthread_join(a_thread->thread, &thr_retval);
        free(a_thread);
    }
}

canonical_original_code:
void delete_handler_threads_pool(struct handler_threads_pool *pool)
{
    void *thr_retval;
    struct handler_thread *a_thread;
    ((void) sizeof ((pool) ? 1 : 0), __extension__ ({ if (pool) ; else __assert_fail ("pool", "handler_threads_pool.c", 143, __extension__ __PRETTY_FUNCTION__); }));
    while (pool->num_threads > 0) {
 a_thread = remove_first_handler_thread(pool);
 ((void) sizeof ((a_thread) ? 1 : 0), __extension__ ({ if (a_thread) ; else __assert_fail ("a_thread", "handler_threads_pool.c", 148, __extension__ __PRETTY_FUNCTION__); }));
 pthread_join(a_thread->thread, &thr_retval);
        free(a_thread);
    }
}


decompiled_code:
int __fastcall add_request(__int64 a1, int a2)
{
_QWORD *v2; // rax
_QWORD *v3; // rbp

if ( !a1 )
_assert_fail("queue", "requests_queue.c", 0x2Fu, "add_request");
v2 = malloc(0x10uLL);
v3 = v2;
if ( !v2 )
{
fwrite("add_request: out of memory\n", 1uLL, 0x1BuLL, stderr);
exit(1);
}
*(_DWORD *)v2 = a2;
v2[1] = 0LL;
pthread_mutex_lock(*(pthread_mutex_t **)(a1 + 24));
if ( *(_DWORD *)(a1 + 16) )
*(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL) = v3;
else
*(_QWORD *)a1 = v3;
*(_QWORD *)(a1 + 8) = v3;
++*(_DWORD *)(a1 + 16);
pthread_mutex_unlock(*(pthread_mutex_t **)(a1 + 24));
return pthread_cond_signal(*(pthread_cond_t **)(a1 + 32));
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1, int a2)
{
_QWORD *v2; // rax
_QWORD *v3; // rbp

if ( !a1 )
_assert_fail("queue", "requests_queue.c", 0x2Fu, "add_request");
v2 = malloc(0x10uLL);
v3 = v2;
if ( !v2 )
{
fwrite("add_request: out of memory\n", 1uLL, 0x1BuLL, stderr);
exit(1);
}
*(_DWORD *)v2 = a2;
v2[1] = 0LL;
pthread_mutex_lock(*(pthread_mutex_t **)(a1 + 24));
if ( *(_DWORD *)(a1 + 16) )
*(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL) = v3;
else
*(_QWORD *)a1 = v3;
*(_QWORD *)(a1 + 8) = v3;
++*(_DWORD *)(a1 + 16);
pthread_mutex_unlock(*(pthread_mutex_t **)(a1 + 24));
return pthread_cond_signal(*(pthread_cond_t **)(a1 + 32));
}

original_code:
void
add_request(struct requests_queue* queue, int request_num)
{
    int rc;
    struct request* a_request;
    ((void) sizeof ((queue) ? 1 : 0), __extension__ ({ if (queue) ; else __assert_fail ("queue", "requests_queue.c", 47, __extension__ __PRETTY_FUNCTION__); }));
    a_request = (struct request*)malloc(sizeof(struct request));
    if (!a_request) {
 fprintf(stderr, "add_request: out of memory\n");
 exit(1);
    }
    a_request->number = request_num;
    a_request->next = ((void *)0);
    rc = pthread_mutex_lock(queue->p_mutex);
    if (queue->num_requests == 0) {
 queue->requests = a_request;
 queue->last_request = a_request;
    }
    else {
 queue->last_request->next = a_request;
 queue->last_request = a_request;
    }
    queue->num_requests++;
    rc = pthread_mutex_unlock(queue->p_mutex);
    rc = pthread_cond_signal(queue->p_cond_var);
}

canonical_original_code:
void add_request(struct requests_queue *queue, int request_num)
{
    int rc;
    struct request *a_request;
    ((void) sizeof ((queue) ? 1 : 0), __extension__ ({ if (queue) ; else __assert_fail ("queue", "requests_queue.c", 47, __extension__ __PRETTY_FUNCTION__); }));
    a_request = (struct request *)malloc(sizeof(struct request));
    if (!a_request) {
 fprintf(stderr, "add_request: out of memory\n");
 exit(1);
    }
    a_request->number = request_num;
    a_request->next = ((void *)0);
    rc = pthread_mutex_lock(queue->p_mutex);
    if (queue->num_requests == 0) {
 queue->requests = a_request;
 queue->last_request = a_request;
    }
    else {
 queue->last_request->next = a_request;
 queue->last_request = a_request;
    }
    queue->num_requests++;
    rc = pthread_mutex_unlock(queue->p_mutex);
    rc = pthread_cond_signal(queue->p_cond_var);
}

variable_types: struct request { int number; struct request *next; } *


decompiled_code:
__int64 __fastcall get_request(__int64 *a1)
{
int v1; // eax
__int64 v2; // rbp
__int64 v3; // rdx

if ( !a1 )
_assert_fail("queue", "requests_queue.c", 0x67u, "get_request");
pthread_mutex_lock((pthread_mutex_t *)a1[3]);
v1 = *((_DWORD *)a1 + 4);
v2 = 0LL;
if ( v1 > 0 )
{
v2 = *a1;
v3 = *(_QWORD *)(*a1 + 8);
*a1 = v3;
if ( !v3 )
a1[1] = 0LL;
*((_DWORD *)a1 + 4) = v1 - 1;
}
pthread_mutex_unlock((pthread_mutex_t *)a1[3]);
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 *a1)
{
int v1; // eax
__int64 v2; // rbp
__int64 v3; // rdx

if ( !a1 )
_assert_fail("queue", "requests_queue.c", 0x67u, "get_request");
pthread_mutex_lock((pthread_mutex_t *)a1[3]);
v1 = *((_DWORD *)a1 + 4);
v2 = 0LL;
if ( v1 > 0 )
{
v2 = *a1;
v3 = *(_QWORD *)(*a1 + 8);
*a1 = v3;
if ( !v3 )
a1[1] = 0LL;
*((_DWORD *)a1 + 4) = v1 - 1;
}
pthread_mutex_unlock((pthread_mutex_t *)a1[3]);
return v2;
}

original_code:
struct request*
get_request(struct requests_queue* queue)
{
    int rc;
    struct request* a_request;
    ((void) sizeof ((queue) ? 1 : 0), __extension__ ({ if (queue) ; else __assert_fail ("queue", "requests_queue.c", 103, __extension__ __PRETTY_FUNCTION__); }));
    rc = pthread_mutex_lock(queue->p_mutex);
    if (queue->num_requests > 0) {
 a_request = queue->requests;
 queue->requests = a_request->next;
 if (queue->requests == ((void *)0)) {
     queue->last_request = ((void *)0);
 }
 queue->num_requests--;
    }
    else {
 a_request = ((void *)0);
    }
    rc = pthread_mutex_unlock(queue->p_mutex);
    return a_request;
}

canonical_original_code:
struct request *get_request(struct requests_queue *queue)
{
    int rc;
    struct request *a_request;
    ((void) sizeof ((queue) ? 1 : 0), __extension__ ({ if (queue) ; else __assert_fail ("queue", "requests_queue.c", 103, __extension__ __PRETTY_FUNCTION__); }));
    rc = pthread_mutex_lock(queue->p_mutex);
    if (queue->num_requests > 0) {
 a_request = queue->requests;
 queue->requests = a_request->next;
 if (queue->requests == ((void *)0)) {
     queue->last_request = ((void *)0);
 }
 queue->num_requests--;
    }
    else {
 a_request = ((void *)0);
    }
    rc = pthread_mutex_unlock(queue->p_mutex);
    return a_request;
}

variable_types: struct requests_queue { struct request *requests; struct request *last_request; int num_requests; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } *p_mutex; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } *p_cond_var; } *

return_type: struct request { int number; struct request *next; } *


decompiled_code:
__int64 __fastcall get_requests_number(__int64 a1)
{
unsigned int v1; // ebp

if ( !a1 )
_assert_fail("queue", "requests_queue.c", 0x8Cu, "get_requests_number");
pthread_mutex_lock(*(pthread_mutex_t **)(a1 + 24));
v1 = *(_DWORD *)(a1 + 16);
pthread_mutex_unlock(*(pthread_mutex_t **)(a1 + 24));
return v1;
}

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

if ( !a1 )
_assert_fail("queue", "requests_queue.c", 0x8Cu, "get_requests_number");
pthread_mutex_lock(*(pthread_mutex_t **)(a1 + 24));
v1 = *(_DWORD *)(a1 + 16);
pthread_mutex_unlock(*(pthread_mutex_t **)(a1 + 24));
return v1;
}

original_code:
int
get_requests_number(struct requests_queue* queue)
{
    int rc;
    int num_requests;
    ((void) sizeof ((queue) ? 1 : 0), __extension__ ({ if (queue) ; else __assert_fail ("queue", "requests_queue.c", 140, __extension__ __PRETTY_FUNCTION__); }));
    rc = pthread_mutex_lock(queue->p_mutex);
    num_requests = queue->num_requests;
    rc = pthread_mutex_unlock(queue->p_mutex);
    return num_requests;
}

canonical_original_code:
int get_requests_number(struct requests_queue *queue)
{
    int rc;
    int num_requests;
    ((void) sizeof ((queue) ? 1 : 0), __extension__ ({ if (queue) ; else __assert_fail ("queue", "requests_queue.c", 140, __extension__ __PRETTY_FUNCTION__); }));
    rc = pthread_mutex_lock(queue->p_mutex);
    num_requests = queue->num_requests;
    rc = pthread_mutex_unlock(queue->p_mutex);
    return num_requests;
}


decompiled_code:
void __fastcall delete_requests_queue(void *ptr)
{
void *v1; // rax

if ( !ptr )
_assert_fail("queue", "requests_queue.c", 0xA5u, "delete_requests_queue");
while ( *((int *)ptr + 4) > 0 )
{
v1 = (void *)get_request((__int64 *)ptr);
free(v1);
}
free(ptr);
}

canonical_decompiled_code:
void __fastcall func3(void *ptr)
{
void *v1; // rax

if ( !ptr )
_assert_fail("queue", "requests_queue.c", 0xA5u, "delete_requests_queue");
while ( *((int *)ptr + 4) > 0 )
{
v1 = (void *)func1((__int64 *)ptr);
free(v1);
}
free(ptr);
}

original_code:
void
delete_requests_queue(struct requests_queue* queue)
{
    struct request* a_request;
    ((void) sizeof ((queue) ? 1 : 0), __extension__ ({ if (queue) ; else __assert_fail ("queue", "requests_queue.c", 165, __extension__ __PRETTY_FUNCTION__); }));
    while (queue->num_requests > 0) {
 a_request = get_request(queue);
 free(a_request);
    }
    free(queue);
}

canonical_original_code:
void delete_requests_queue(struct requests_queue *queue)
{
    struct request *a_request;
    ((void) sizeof ((queue) ? 1 : 0), __extension__ ({ if (queue) ; else __assert_fail ("queue", "requests_queue.c", 165, __extension__ __PRETTY_FUNCTION__); }));
    while (queue->num_requests > 0) {
 a_request = get_request(queue);
 free(a_request);
    }
    free(queue);
}


decompiled_code:
int __fastcall cleanup_free_mutex(pthread_mutex_t *a1)
{
int result; // eax

if ( a1 )
result = pthread_mutex_unlock(a1);
return result;
}

canonical_decompiled_code:
int __fastcall func0(pthread_mutex_t *a1)
{
int result; // eax

if ( a1 )
result = pthread_mutex_unlock(a1);
return result;
}

original_code:
static void
cleanup_free_mutex(void* a_mutex)
{
    pthread_mutex_t* p_mutex = (pthread_mutex_t*)a_mutex;
    if (p_mutex)
        pthread_mutex_unlock(p_mutex);
}

canonical_original_code:
static void cleanup_free_mutex(void *a_mutex)
{
    union pthread_mutex_t *p_mutex = (pthread_mutex_t*)a_mutex;
    if (p_mutex)
        pthread_mutex_unlock(p_mutex);
}

variable_types: union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } *


decompiled_code:
void __fastcall __noreturn handle_requests_loop(unsigned int *a1)
{
void *v1; // rbx
pthread_mutex_t *v2; // [rsp+8h] [rbp-90h]
__pthread_unwind_buf_t buf; // [rsp+10h] [rbp-88h] BYREF

if ( a1 )
{
printf("Starting thread '%d'\n", *a1);
fflush(stdout);
pthread_setcancelstate(0, 0LL);
pthread_setcanceltype(0, 0LL);
v2 = (pthread_mutex_t *)*((_QWORD *)a1 + 1);
if ( !_sigsetjmp((struct __jmp_buf_tag *)&buf, 0) )
{
_pthread_register_cancel(&buf);
pthread_mutex_lock(*((pthread_mutex_t **)a1 + 1));
while ( 1 )
{
while ( (int)get_requests_number(*((_QWORD *)a1 + 3)) <= 0 )
{
if ( done_creating_requests )
{
pthread_mutex_unlock(*((pthread_mutex_t **)a1 + 1));
printf("thread '%d' exiting\n", *a1);
fflush(stdout);
pthread_exit(0LL);
}
pthread_cond_wait(*((pthread_cond_t **)a1 + 2), *((pthread_mutex_t **)a1 + 1));
}
v1 = (void *)get_request(*((_QWORD *)a1 + 3));
if ( v1 )
{
pthread_mutex_unlock(*((pthread_mutex_t **)a1 + 1));
free(v1);
pthread_mutex_lock(*((pthread_mutex_t **)a1 + 1));
}
}
}
cleanup_free_mutex(v2);
_pthread_unwind_next(&buf);
}
_assert_fail("data", "handler_thread.c", 0x42u, "handle_requests_loop");
}

canonical_decompiled_code:
void __fastcall __noreturn func1(unsigned int *a1)
{
void *v1; // rbx
pthread_mutex_t *v2; // [rsp+8h] [rbp-90h]
__pthread_unwind_buf_t buf; // [rsp+10h] [rbp-88h] BYREF

if ( a1 )
{
printf("Starting thread '%d'\n", *a1);
fflush(stdout);
pthread_setcancelstate(0, 0LL);
pthread_setcanceltype(0, 0LL);
v2 = (pthread_mutex_t *)*((_QWORD *)a1 + 1);
if ( !_sigsetjmp((struct __jmp_buf_tag *)&buf, 0) )
{
_pthread_register_cancel(&buf);
pthread_mutex_lock(*((pthread_mutex_t **)a1 + 1));
while ( 1 )
{
while ( (int)get_requests_number(*((_QWORD *)a1 + 3)) <= 0 )
{
if ( done_creating_requests )
{
pthread_mutex_unlock(*((pthread_mutex_t **)a1 + 1));
printf("thread '%d' exiting\n", *a1);
fflush(stdout);
pthread_exit(0LL);
}
pthread_cond_wait(*((pthread_cond_t **)a1 + 2), *((pthread_mutex_t **)a1 + 1));
}
v1 = (void *)get_request(*((_QWORD *)a1 + 3));
if ( v1 )
{
pthread_mutex_unlock(*((pthread_mutex_t **)a1 + 1));
free(v1);
pthread_mutex_lock(*((pthread_mutex_t **)a1 + 1));
}
}
}
func0(v2);
_pthread_unwind_next(&buf);
}
_assert_fail("data", "handler_thread.c", 0x42u, "handle_requests_loop");
}

original_code:
void*
handle_requests_loop(void* thread_params)
{
    int rc;
    struct request* a_request;
    struct handler_thread_params *data;
    data = (struct handler_thread_params*)thread_params;
    ((void) sizeof ((data) ? 1 : 0), __extension__ ({ if (data) ; else __assert_fail ("data", "handler_thread.c", 66, __extension__ __PRETTY_FUNCTION__); }));
    printf("Starting thread '%d'\n", data->thread_id);
    fflush(stdout);
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, ((void *)0));
    pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, ((void *)0));
    do { __pthread_unwind_buf_t __cancel_buf; void (*__cancel_routine) (void *) = (cleanup_free_mutex); void *__cancel_arg = ((void*)data->request_mutex); int __not_first_call = __sigsetjmp ((struct __jmp_buf_tag *) (void *) __cancel_buf.__cancel_jmp_buf, 0); if (__builtin_expect ((__not_first_call), 0)) { __cancel_routine (__cancel_arg); __pthread_unwind_next (&__cancel_buf); } __pthread_register_cancel (&__cancel_buf); do {;
    rc = pthread_mutex_lock(data->request_mutex);
    while (1) {
        int num_requests = get_requests_number(data->requests);
 if (num_requests > 0) {
     a_request = get_request(data->requests);
     if (a_request) {
      rc = pthread_mutex_unlock(data->request_mutex);
  handle_request(a_request, data->thread_id);
  free(a_request);
      rc = pthread_mutex_lock(data->request_mutex);
     }
 }
 else {
     if (done_creating_requests) {
                pthread_mutex_unlock(data->request_mutex);
  printf("thread '%d' exiting\n", data->thread_id);
  fflush(stdout);
  pthread_exit(((void *)0));
     }
     rc = pthread_cond_wait(data->got_request, data->request_mutex);
 }
    }
    do { } while (0); } while (0); __pthread_unregister_cancel (&__cancel_buf); if (0) __cancel_routine (__cancel_arg); } while (0);
}

canonical_original_code:
void *handle_requests_loop(void *thread_params)
{
    int rc;
    struct request *a_request;
    struct handler_thread_params *data;
    data = (struct handler_thread_params *)thread_params;
    ((void) sizeof ((data) ? 1 : 0), __extension__ ({ if (data) ; else __assert_fail ("data", "handler_thread.c", 66, __extension__ __PRETTY_FUNCTION__); }));
    printf("Starting thread '%d'\n", data->thread_id);
    fflush(stdout);
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, ((void *)0));
    pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, ((void *)0));
    do { struct __pthread_unwind_buf_t __cancel_buf; void (*__cancel_routine)(void *) = (cleanup_free_mutex); void *__cancel_arg = ((void*)data->request_mutex); int __not_first_call = __sigsetjmp ((struct __jmp_buf_tag *) (void *) __cancel_buf.__cancel_jmp_buf, 0); if (__builtin_expect ((__not_first_call), 0)) { __cancel_routine (__cancel_arg); __pthread_unwind_next (&__cancel_buf); } __pthread_register_cancel (&__cancel_buf); do {;
    rc = pthread_mutex_lock(data->request_mutex);
    while (1) {
        int num_requests = get_requests_number(data->requests);
 if (num_requests > 0) {
     a_request = get_request(data->requests);
     if (a_request) {
      rc = pthread_mutex_unlock(data->request_mutex);
  handle_request(a_request, data->thread_id);
  free(a_request);
      rc = pthread_mutex_lock(data->request_mutex);
     }
 }
 else {
     if (done_creating_requests) {
                pthread_mutex_unlock(data->request_mutex);
  printf("thread '%d' exiting\n", data->thread_id);
  fflush(stdout);
  pthread_exit(((void *)0));
     }
     rc = pthread_cond_wait(data->got_request, data->request_mutex);
 }
    }
    do { } while (0); } while (0); __pthread_unregister_cancel (&__cancel_buf); if (0) __cancel_routine (__cancel_arg); } while (0);
}

variable_types: struct __pthread_unwind_buf_t { struct { long __cancel_jmp_buf[8]; int __mask_was_saved; } __cancel_jmp_buf[1]; void *__pad[4]; }


zlibVersion

decompiled_code:
const char *zlibVersion()
{
return "1.2.3";
}

canonical_decompiled_code:
const char *func0()
{
return "1.2.3";
}

original_code:
const char * zlibVersion()
{
    return "1.2.3";
}

canonical_original_code:
char *zlibVersion()
{
    return "1.2.3";
}


zlibCompileFlags

decompiled_code:
__int64 zlibCompileFlags()
{
return 169LL;
}

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

original_code:
uLong zlibCompileFlags()
{
    uLong flags;
    flags = 0;
    switch (sizeof(uInt)) {
    case 2: break;
    case 4: flags += 1; break;
    case 8: flags += 2; break;
    default: flags += 3;
    }
    switch (sizeof(uLong)) {
    case 2: break;
    case 4: flags += 1 << 2; break;
    case 8: flags += 2 << 2; break;
    default: flags += 3 << 2;
    }
    switch (sizeof(voidpf)) {
    case 2: break;
    case 4: flags += 1 << 4; break;
    case 8: flags += 2 << 4; break;
    default: flags += 3 << 4;
    }
    switch (sizeof(long)) {
    case 2: break;
    case 4: flags += 1 << 6; break;
    case 8: flags += 2 << 6; break;
    default: flags += 3 << 6;
    }
    return flags;
}

canonical_original_code:
unsigned long zlibCompileFlags()
{
    unsigned long flags;
    flags = 0;
    switch (sizeof(uInt)) {
    case 2: break;
    case 4: flags += 1; break;
    case 8: flags += 2; break;
    default: flags += 3;
    }
    switch (sizeof(uLong)) {
    case 2: break;
    case 4: flags += 1 << 2; break;
    case 8: flags += 2 << 2; break;
    default: flags += 3 << 2;
    }
    switch (sizeof(voidpf)) {
    case 2: break;
    case 4: flags += 1 << 4; break;
    case 8: flags += 2 << 4; break;
    default: flags += 3 << 4;
    }
    switch (sizeof(long)) {
    case 2: break;
    case 4: flags += 1 << 6; break;
    case 8: flags += 2 << 6; break;
    default: flags += 3 << 6;
    }
    return flags;
}

variable_types:
flags
unsigned long

return_type: unsigned long


decompiled_code:
__int64 *get_crc_table()
{
return crc_table;
}

canonical_decompiled_code:
__int64 *func3()
{
return crc_table;
}

original_code:
const unsigned long * get_crc_table()
{
    return (const unsigned long *)crc_table;
}

canonical_original_code:
unsigned long *get_crc_table()
{
    return (unsigned long *)crc_table;
}

return_type: unsigned long *


decompiled_code:
int print_config()
{
printf("In this computer, sizeof int = %u\n", 4LL);
return printf("and sizeof uint16_t = %u\n\n", 2LL);
}

canonical_decompiled_code:
int func0()
{
printf("In this computer, sizeof int = %u\n", 4LL);
return printf("and sizeof uint16_t = %u\n\n", 2LL);
}

original_code:
void print_config(void)
{
    printf("In this computer, sizeof int = %u\n",sizeof(int));
    printf("and sizeof uint16_t = %u\n\n",sizeof(uint16_t));
}

canonical_original_code:
void print_config()
{
    printf("In this computer, sizeof int = %u\n",sizeof(int));
    printf("and sizeof uint16_t = %u\n\n",sizeof(unsigned short));
}