decompiled_code:
char *__fastcall parse_script_variable(char *a1)
{
char *v1; // rbp
double v2; // xmm0_8
unsigned __int16 v4; // ax
unsigned __int16 v5; // ax
__int64 v6; // r12
unsigned int v7; // ebx
int v8; // esi
char *v9; // rbp
__int64 v10; // r12
unsigned int v11; // ebx
__int16 v12; // r12
struct tm *v13; // rax
char *v14; // rax
int v15; // eax
time_t timer; // [rsp+8h] [rbp-20h] BYREF

v1 = a1 + 1;
switch ( *a1 )
{
case 0:
v2 = conv_double(a1 + 1);
printf("Number\tValue: %.2f\n", v2);
v1 = a1 + 9;
break;
case 1:
printf("Boolean\tValue: %d\n", (unsigned __int8)a1[1]);
v1 = a1 + 2;
break;
case 2:
printf("String\tValue: ");
v4 = conv_ui16(v1);
v1 = parse_script_string(a1 + 3, v4);
putchar(10);
break;
case 3:
puts("Object");
v1 = (char *)parse_script_object(v1);
break;
case 4:
puts("MovieClip");
break;
case 5:
puts("Null");
break;
case 6:
puts("Undefined");
break;
case 7:
v5 = conv_ui16(a1 + 1);
printf("Reference\tValue: %d\n", v5);
v1 = a1 + 3;
break;
case 8:
printf("ECMA Array\t");
v6 = (unsigned int)conv_ui32(v1);
v1 = a1 + 5;
printf("Length: %d variables\n", v6);
if ( (int)v6 > 0 )
{
v7 = 0;
do
{
printf("Variable %d\tName: ", v7);
v8 = (unsigned __int16)conv_ui16(v1);
v9 = parse_script_string(v1 + 2, v8);
printf("\tType: ");
v1 = (char *)parse_script_variable(v9);
++v7;
}
while ( (_DWORD)v6 != v7 );
}
break;
case 10:
printf("Script Array\t");
v10 = (unsigned int)conv_ui32(v1);
v1 = a1 + 5;
printf("Length: %d variables\n", v10);
if ( (int)v10 > 0 )
{
v11 = 0;
do
{
printf("Variable %d\tType: ", v11);
v1 = (char *)parse_script_variable(v1);
++v11;
}
while ( (_DWORD)v10 != v11 );
}
break;
case 11:
printf("Date\t");
timer = (unsigned int)(int)(conv_double(v1) / 1000.0);
v12 = conv_si16(a1 + 9);
v1 = a1 + 11;
v13 = gmtime(&timer);
v14 = asctime(v13);
printf("Value: %s\tTimezone: %+g\n", v14, (double)v12 / 60.0);
break;
case 12:
printf("Long String\tValue: ");
v15 = conv_ui32(v1);
v1 = parse_script_string(a1 + 3, v15);
break;
default:
puts("ERROR");
break;
}
return v1;
}

canonical_decompiled_code:
char *__fastcall func1(char *a1)
{
char *v1; // rbp
double v2; // xmm0_8
unsigned __int16 v4; // ax
unsigned __int16 v5; // ax
__int64 v6; // r12
unsigned int v7; // ebx
int v8; // esi
char *v9; // rbp
__int64 v10; // r12
unsigned int v11; // ebx
__int16 v12; // r12
struct tm *v13; // rax
char *v14; // rax
int v15; // eax
time_t timer; // [rsp+8h] [rbp-20h] BYREF

v1 = a1 + 1;
switch ( *a1 )
{
case 0:
v2 = conv_double(a1 + 1);
printf("Number\tValue: %.2f\n", v2);
v1 = a1 + 9;
break;
case 1:
printf("Boolean\tValue: %d\n", (unsigned __int8)a1[1]);
v1 = a1 + 2;
break;
case 2:
printf("String\tValue: ");
v4 = conv_ui16(v1);
v1 = func0(a1 + 3, v4);
putchar(10);
break;
case 3:
puts("Object");
v1 = (char *)func2(v1);
break;
case 4:
puts("MovieClip");
break;
case 5:
puts("Null");
break;
case 6:
puts("Undefined");
break;
case 7:
v5 = conv_ui16(a1 + 1);
printf("Reference\tValue: %d\n", v5);
v1 = a1 + 3;
break;
case 8:
printf("ECMA Array\t");
v6 = (unsigned int)conv_ui32(v1);
v1 = a1 + 5;
printf("Length: %d variables\n", v6);
if ( (int)v6 > 0 )
{
v7 = 0;
do
{
printf("Variable %d\tName: ", v7);
v8 = (unsigned __int16)conv_ui16(v1);
v9 = func0(v1 + 2, v8);
printf("\tType: ");
v1 = (char *)func1(v9);
++v7;
}
while ( (_DWORD)v6 != v7 );
}
break;
case 10:
printf("Script Array\t");
v10 = (unsigned int)conv_ui32(v1);
v1 = a1 + 5;
printf("Length: %d variables\n", v10);
if ( (int)v10 > 0 )
{
v11 = 0;
do
{
printf("Variable %d\tType: ", v11);
v1 = (char *)func1(v1);
++v11;
}
while ( (_DWORD)v10 != v11 );
}
break;
case 11:
printf("Date\t");
timer = (unsigned int)(int)(conv_double(v1) / 1000.0);
v12 = conv_si16(a1 + 9);
v1 = a1 + 11;
v13 = gmtime(&timer);
v14 = asctime(v13);
printf("Value: %s\tTimezone: %+g\n", v14, (double)v12 / 60.0);
break;
case 12:
printf("Long String\tValue: ");
v15 = conv_ui32(v1);
v1 = func0(a1 + 3, v15);
break;
default:
puts("ERROR");
break;
}
return v1;
}

original_code:
unsigned char *parse_script_variable(unsigned char *pos)
{
    unsigned char variable_type = *(pos++);
    switch( variable_type )
    {
        case 0:
            printf("Number\tValue: %.2f\n", conv_double(pos));
            pos += sizeof(double);
            break;
        case 1:
            printf("Boolean\tValue: %d\n", *pos);
            pos++;
            break;
        case 2:
            printf("String\tValue: ");
            pos = parse_script_string(pos + 2, conv_ui16(pos));
            printf("\n");
            break;
        case 3:
            printf("Object\n");
            pos = parse_script_object(pos);
            break;
        case 4:
            printf("MovieClip\n");
            break;
        case 5:
            printf("Null\n");
            break;
        case 6:
            printf("Undefined\n");
            break;
        case 7:
            printf("Reference\tValue: %d\n", conv_ui16(pos));
            pos += 2;
            break;
        case 8:
            printf("ECMA Array\t");
            {
                int array_length = conv_ui32(pos);
                int count;
                pos += 4;
                printf("Length: %d variables\n", array_length);
                for( count = 0; count < array_length; count++ )
                {
                    printf( "Variable %d\tName: ", count);
                    pos = parse_script_string(pos + 2, conv_ui16(pos));
                    printf( "\tType: ");
                    pos = parse_script_variable(pos);
                }
            }
            break;
        case 10:
            printf("Script Array\t");
            {
                int array_length = conv_ui32(pos);
                int count;
                pos += 4;
                printf("Length: %d variables\n", array_length);
                for( count = 0; count < array_length; count++ )
                {
                    printf( "Variable %d\tType: ", count);
                    pos = parse_script_variable(pos);
                }
            }
            break;
        case 11:
            printf("Date\t");
            {
                time_t timestamp = conv_double(pos) / 1000;
                short tz_offset;
                pos += sizeof(double);
                tz_offset = conv_si16(pos);
                pos +=2;
                printf("Value: %s\tTimezone: %+g\n", asctime(gmtime(&timestamp)),
                       (double) tz_offset / 60);
            }
            break;
        case 12:
            printf("Long String\tValue: ");
            pos = parse_script_string(pos + 2, conv_ui32(pos));
            break;
        default:
            printf("ERROR\n");
            break;
    }
    return pos;
}

canonical_original_code:
unsigned char *parse_script_variable(unsigned char *pos)
{
    unsigned char variable_type = *(pos++);
    switch( variable_type )
    {
        case 0:
            printf("Number\tValue: %.2f\n", conv_double(pos));
            pos += sizeof(double);
            break;
        case 1:
            printf("Boolean\tValue: %d\n", *pos);
            pos++;
            break;
        case 2:
            printf("String\tValue: ");
            pos = parse_script_string(pos + 2, conv_ui16(pos));
            printf("\n");
            break;
        case 3:
            printf("Object\n");
            pos = parse_script_object(pos);
            break;
        case 4:
            printf("MovieClip\n");
            break;
        case 5:
            printf("Null\n");
            break;
        case 6:
            printf("Undefined\n");
            break;
        case 7:
            printf("Reference\tValue: %d\n", conv_ui16(pos));
            pos += 2;
            break;
        case 8:
            printf("ECMA Array\t");
            {
                int array_length = conv_ui32(pos);
                int count;
                pos += 4;
                printf("Length: %d variables\n", array_length);
                for( count = 0; count < array_length; count++ )
                {
                    printf( "Variable %d\tName: ", count);
                    pos = parse_script_string(pos + 2, conv_ui16(pos));
                    printf( "\tType: ");
                    pos = parse_script_variable(pos);
                }
            }
            break;
        case 10:
            printf("Script Array\t");
            {
                int array_length = conv_ui32(pos);
                int count;
                pos += 4;
                printf("Length: %d variables\n", array_length);
                for( count = 0; count < array_length; count++ )
                {
                    printf( "Variable %d\tType: ", count);
                    pos = parse_script_variable(pos);
                }
            }
            break;
        case 11:
            printf("Date\t");
            {
                long timestamp = conv_double(pos) / 1000;
                short tz_offset;
                pos += sizeof(double);
                tz_offset = conv_si16(pos);
                pos +=2;
                printf("Value: %s\tTimezone: %+g\n", asctime(gmtime(&timestamp)),
                       (double) tz_offset / 60);
            }
            break;
        case 12:
            printf("Long String\tValue: ");
            pos = parse_script_string(pos + 2, conv_ui32(pos));
            break;
        default:
            printf("ERROR\n");
            break;
    }
    return pos;
}

variable_types: timestamp long


main

canonical_name: main

repo: pdkelly/flvjoin

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
FILE *v3; // rbx

v3 = stdin;
if ( argc > 1 )
{
v3 = fopen(argv[1], "rb");
if ( !v3 )
{
fprintf(stderr, "Error opening file %s\n", argv[1]);
exit(1);
}
}
parse_file(v3);
fclose(v3);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
FILE *v3; // rbx

v3 = stdin;
if ( argc > 1 )
{
v3 = fopen(argv[1], "rb");
if ( !v3 )
{
fprintf(stderr, "Error opening file %s\n", argv[1]);
exit(1);
}
}
func3(v3);
fclose(v3);
return 0;
}

original_code:
int main (int argc, char ** argv)
{
    char buffer[2*1024];
    int audio_bitrate = 32000;
    int opt;
    filepath[0] = '\0';
    while ( (opt = getopt(argc, argv, "o:f:b:ndqh")) != -1 )
    {
        switch (opt)
        {
            case 'o':
                strncpy(filepath, optarg, sizeof(filepath));
                break;
            case 'f':
                frame_interval = (int)(0.5 + 1000 / atof(optarg));
                break;
            case 'b':
                audio_bitrate = atoi(optarg);
                break;
            case 'n':
                no_meta = 1;
                break;
            case 'd':
                exit(0);
            case 'q':
                quiet = 1;
                break;
            case 'h':
            default:
                fprintf(stderr,"%s v%s\n", "flvjoin", "0.92");
                fprintf(stderr,"\nSynopsis: Reads a list of FLV files (with optional in-point and out-point)\n");
                fprintf(stderr,"from standard input and joins them together into one larger FLV file.\n\n");
                fprintf(stderr,"Usage: %s -o <filename> [-f <framerate>] [-b <bitrate>] [-n] [-q] [-h]\n\n", "flvjoin");
                fprintf(stderr,"   -o <filename>   Output File (- for stdout)\n");
                fprintf(stderr,"   -f <framerate>  Video frame rate in frames per second (default %.2f)\n", 1000.0 / frame_interval);
                fprintf(stderr,"   -b <bitrate>    Audio bitrate in bits per second (default %d)\n", audio_bitrate);
                fprintf(stderr,"   -n              Don't write metadata to output file\n");
                fprintf(stderr,"   -q              Don't display progress information\n");
                fprintf(stderr,"   -h              Display this usage message and exit\n");
                fprintf(stderr,"\n");
                exit(0);
        }
    }
    if( strlen(filepath) == 0 )
    {
        fprintf(stderr, "ERROR: Output file must be specified with the -o option. (Use - for stdout).\n");
        exit(1);
    }
    {
        struct stat s;
        struct FLVpacket *packet;
        if( strcmp(filepath, "-") != 0 && stat(filepath, &s) == 0 )
        {
            fprintf(stderr,"ERROR: File %s exists; won't write header.\n", filepath);
            exit(1);
        }
        open_output("wb");
        write_flv_header();
        if(!no_meta)
        {
            packet = generate_metadata_packet(outfile);
            write_packet(packet, 0);
        }
    }
    while( fgets(buffer, sizeof(buffer), stdin) )
    {
        int num_params;
        double mark_in, mark_out;
        char infile[1024];
        char *newline = strchr(buffer, '\n');
        if(newline)
            *newline = '\0';
        num_params = sscanf(buffer, "%1023s %lf %lf", infile, &mark_in, &mark_out);
        if(num_params < 3)
            mark_out = 99999;
        if(num_params < 2)
            mark_in = 0;
        append_file(infile, (unsigned int)(0.5 + mark_in*1000), (unsigned int)(0.5 + mark_out*1000));
    }
    if(!no_meta)
    {
        unsigned int duration;
        if(!quiet)
            fprintf(stderr, "Writing metadata...\n");
        if(last_video_timestamp >= last_audio_timestamp)
            duration = last_video_timestamp + frame_interval;
        else
            duration = last_audio_timestamp + (unsigned int)(0.5 + 1000.0 * last_packet_size * 8 / audio_bitrate);
        write_metadata(outfile, duration);
    }
    if( !quiet )
        fprintf(stderr, "Closing output file %s\n", filepath);
    close_output();
    exit(0);
}

canonical_original_code:
int main(int argc, char **argv)
{
    char buffer[-1];
    int audio_bitrate = 32000;
    int opt;
    filepath[0] = '\0';
    while ( (opt = getopt(argc, argv, "o:f:b:ndqh")) != -1 )
    {
        switch (opt)
        {
            case 'o':
                strncpy(filepath, optarg, sizeof(filepath));
                break;
            case 'f':
                frame_interval = (int)(0.5 + 1000 / atof(optarg));
                break;
            case 'b':
                audio_bitrate = atoi(optarg);
                break;
            case 'n':
                no_meta = 1;
                break;
            case 'd':
                exit(0);
            case 'q':
                quiet = 1;
                break;
            case 'h':
            default:
                fprintf(stderr,"%s v%s\n", "flvjoin", "0.92");
                fprintf(stderr,"\nSynopsis: Reads a list of FLV files (with optional in-point and out-point)\n");
                fprintf(stderr,"from standard input and joins them together into one larger FLV file.\n\n");
                fprintf(stderr,"Usage: %s -o <filename> [-f <framerate>] [-b <bitrate>] [-n] [-q] [-h]\n\n", "flvjoin");
                fprintf(stderr,"   -o <filename>   Output File (- for stdout)\n");
                fprintf(stderr,"   -f <framerate>  Video frame rate in frames per second (default %.2f)\n", 1000.0 / frame_interval);
                fprintf(stderr,"   -b <bitrate>    Audio bitrate in bits per second (default %d)\n", audio_bitrate);
                fprintf(stderr,"   -n              Don't write metadata to output file\n");
                fprintf(stderr,"   -q              Don't display progress information\n");
                fprintf(stderr,"   -h              Display this usage message and exit\n");
                fprintf(stderr,"\n");
                exit(0);
        }
    }
    if( strlen(filepath) == 0 )
    {
        fprintf(stderr, "ERROR: Output file must be specified with the -o option. (Use - for stdout).\n");
        exit(1);
    }
    {
        struct stat s;
        struct FLVpacket *packet;
        if( strcmp(filepath, "-") != 0 && stat(filepath, &s) == 0 )
        {
            fprintf(stderr,"ERROR: File %s exists; won't write header.\n", filepath);
            exit(1);
        }
        open_output("wb");
        write_flv_header();
        if(!no_meta)
        {
            packet = generate_metadata_packet(outfile);
            write_packet(packet, 0);
        }
    }
    while( fgets(buffer, sizeof(buffer), stdin) )
    {
        int num_params;
        double mark_in; double mark_out;
        char infile[1024];
        char *newline = strchr(buffer, '\n');
        if(newline)
            *newline = '\0';
        num_params = sscanf(buffer, "%1023s %lf %lf", infile, &mark_in, &mark_out);
        if(num_params < 3)
            mark_out = 99999;
        if(num_params < 2)
            mark_in = 0;
        append_file(infile, (unsigned int)(0.5 + mark_in*1000), (unsigned int)(0.5 + mark_out*1000));
    }
    if(!no_meta)
    {
        unsigned int duration;
        if(!quiet)
            fprintf(stderr, "Writing metadata...\n");
        if(last_video_timestamp >= last_audio_timestamp)
            duration = last_video_timestamp + frame_interval;
        else
            duration = last_audio_timestamp + (unsigned int)(0.5 + 1000.0 * last_packet_size * 8 / audio_bitrate);
        write_metadata(outfile, duration);
    }
    if( !quiet )
        fprintf(stderr, "Closing output file %s\n", filepath);
    close_output();
    exit(0);
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
buffer
char [-1]

variable_types: opt int

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

variable_types: struct FLVpacket { unsigned char type; unsigned int datasize; unsigned char *data; unsigned int backptr; } *

variable_types:
infile
char [1024]

variable_types:
newline
char *

variable_types:
duration
unsigned int

return_type: int

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:
char *__fastcall parse_script_string(char *a1, int a2)
{
char *v2; // rbx

if ( a2 )
{
v2 = a1;
do
putchar(*v2++);
while ( v2 != &a1[a2 - 1 + 1] );
}
return &a1[a2];
}

canonical_decompiled_code:
char *__fastcall func0(char *a1, int a2)
{
char *v2; // rbx

if ( a2 )
{
v2 = a1;
do
putchar(*v2++);
while ( v2 != &a1[a2 - 1 + 1] );
}
return &a1[a2];
}

original_code:
unsigned char *parse_script_string(unsigned char *pos, unsigned int string_length)
{
    unsigned int count;
    for( count = 0; count < string_length; count++)
        putchar((char)pos[count]);
    pos += string_length;
    return pos;
}

variable_types:
pos
unsigned char *

variable_types:
count
unsigned int

return_type: unsigned char *


decompiled_code:
char *__fastcall parse_script_object(_BYTE *a1)
{
_BYTE *v1; // rbx
char v2; // bp
int v3; // esi
char *v4; // rbx
char *v5; // rbx
__int16 s2; // [rsp+Dh] [rbp-1Bh] BYREF
char v8; // [rsp+Fh] [rbp-19h]

v1 = a1;
v2 = *a1;
s2 = 0;
v8 = 9;
puts("--Script Object Start");
if ( v2 == 2 )
v1 = a1 + 1;
else
puts("WARNING: Script Object Marker Byte missing.");
printf("Object Name: ");
v3 = (unsigned __int16)conv_ui16(v1);
v4 = parse_script_string(v1 + 2, v3);
printf("\tType: ");
v5 = parse_script_variable(v4);
if ( !memcmp(v5, &s2, 3uLL) )
v5 += 3;
else
puts("WARNING: Script variable closing bytes missing.");
return v5;
}

canonical_decompiled_code:
char *__fastcall func2(_BYTE *a1)
{
_BYTE *v1; // rbx
char v2; // bp
int v3; // esi
char *v4; // rbx
char *v5; // rbx
__int16 s2; // [rsp+Dh] [rbp-1Bh] BYREF
char v8; // [rsp+Fh] [rbp-19h]

v1 = a1;
v2 = *a1;
s2 = 0;
v8 = 9;
puts("--Script Object Start");
if ( v2 == 2 )
v1 = a1 + 1;
else
puts("WARNING: Script Object Marker Byte missing.");
printf("Object Name: ");
v3 = (unsigned __int16)conv_ui16(v1);
v4 = func0(v1 + 2, v3);
printf("\tType: ");
v5 = func1(v4);
if ( !memcmp(v5, &s2, 3uLL) )
v5 += 3;
else
puts("WARNING: Script variable closing bytes missing.");
return v5;
}

original_code:
unsigned char *parse_script_object(unsigned char *pos)
{
    unsigned char marker = *(pos++);
    unsigned char variable_end[] = { 0, 0, 9 };
    printf("--Script Object Start\n");
    if( marker != 2 )
    {
        printf( "WARNING: Script Object Marker Byte missing.\n");
        pos--;
    }
    printf( "Object Name: ");
    pos = parse_script_string(pos + 2, conv_ui16(pos));
    printf( "\tType: ");
    pos = parse_script_variable(pos);
    if( memcmp(pos, variable_end, 3) != 0 )
        printf( "WARNING: Script variable closing bytes missing.\n");
    else
        pos += 3;
    return pos;
}

variable_types:
marker
unsigned char


sigfunc

repo: pearsonalan/stats

decompiled_code:
_DWORD *sigfunc()
{
_DWORD *result; // rax

result = &signal_received;
signal_received = 1;
return result;
}

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

result = &signal_received;
signal_received = 1;
return result;
}

original_code:
static void sigfunc(int sig_no)
{
    signal_received = 1;
}

return_type: void


original_code:
int semaphore_init(struct semaphore *sem, const char *name, unsigned short size)
{
    if (sem == ((void *)0))
        return ((int)(0x80000000 | 0x00010000 | 0x0001));
    if (strlen(name) >= 31)
        return ((int)(0x80000000 | 0x00030000 | 0x0001));
    memset(sem, 0, sizeof(struct semaphore));
    sem->magic = 'semm';
    sem->semkey = -1;
    sem->semid = -1;
    sem->size = size;
    strcpy(sem->name, name);
    return 0x00000000;
}

canonical_original_code:
int semaphore_init(struct semaphore *sem, char *name, unsigned short size)
{
    if (sem == ((void *)0))
        return ((int)(0x80000000 | 0x00010000 | 0x0001));
    if (strlen(name) >= 31)
        return ((int)(0x80000000 | 0x00030000 | 0x0001));
    memset(sem, 0, sizeof(struct semaphore));
    sem->magic = 'semm';
    sem->semkey = -1;
    sem->semid = -1;
    sem->size = size;
    strcpy(sem->name, name);
    return 0x00000000;
}

variable_types: struct semaphore { int magic; unsigned short size; char name[-1]; int semkey; int semid; } *

variable_types:
name
char *

variable_types:
size
unsigned short


decompiled_code:
__int64 __fastcall semaphore_create(const char *a1, __int16 a2, _QWORD *a3)
{
void *v4; // rax
void *v5; // rbp
unsigned int v6; // ebx

if ( !a3 )
return (unsigned int)-2147418111;
if ( strlen(a1) > 0x1E )
return (unsigned int)-2147287039;
v4 = malloc(0x30uLL);
v5 = v4;
if ( !v4 )
return (unsigned int)-2147418110;
v6 = semaphore_init((__int64)v4, a1, a2);
if ( v6 )
{
free(v5);
*a3 = 0LL;
}
else
{
*a3 = v5;
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1, __int16 a2, _QWORD *a3)
{
void *v4; // rax
void *v5; // rbp
unsigned int v6; // ebx

if ( !a3 )
return (unsigned int)-2147418111;
if ( strlen(a1) > 0x1E )
return (unsigned int)-2147287039;
v4 = malloc(0x30uLL);
v5 = v4;
if ( !v4 )
return (unsigned int)-2147418110;
v6 = func0((__int64)v4, a1, a2);
if ( v6 )
{
free(v5);
*a3 = 0LL;
}
else
{
*a3 = v5;
}
return v6;
}

original_code:
int semaphore_create(const char *name, unsigned short size, struct semaphore **sem_out )
{
    struct semaphore * sem;
    int err;
    if (sem_out == ((void *)0))
        return ((int)(0x80000000 | 0x00010000 | 0x0001));
    if (strlen(name) >= 31)
        return ((int)(0x80000000 | 0x00030000 | 0x0001));
    sem = malloc(sizeof(struct semaphore));
    if (!sem)
        return ((int)(0x80000000 | 0x00010000 | 0x0002));
    err = semaphore_init(sem,name,size);
    if (err == 0x00000000)
    {
        *sem_out = sem;
    }
    else
    {
        free(sem);
        *sem_out = ((void *)0);
    }
    return err;
}

canonical_original_code:
int semaphore_create(char *name, unsigned short size, struct semaphore **sem_out)
{
    struct semaphore *sem;
    int err;
    if (sem_out == ((void *)0))
        return ((int)(0x80000000 | 0x00010000 | 0x0001));
    if (strlen(name) >= 31)
        return ((int)(0x80000000 | 0x00030000 | 0x0001));
    sem = malloc(sizeof(struct semaphore));
    if (!sem)
        return ((int)(0x80000000 | 0x00010000 | 0x0002));
    err = semaphore_init(sem,name,size);
    if (err == 0x00000000)
    {
        *sem_out = sem;
    }
    else
    {
        free(sem);
        *sem_out = ((void *)0);
    }
    return err;
}

variable_types: struct semaphore { int magic; unsigned short size; char name[-1]; int semkey; int semid; } **

variable_types: err int


variable_types: flags int

variable_types:
path
char [255]

variable_types: mode int

variable_types: omode int

variable_types: fd int

user_defined_types: struct semaphore { int magic; unsigned short size; char name[-1]; int semkey; int semid; }

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


original_code:
int semaphore_set_value(struct semaphore *sem, unsigned short nsem, unsigned short value)
{
    union semun s;
    if (sem == ((void *)0) || sem->magic != 'semm')
        return ((int)(0x80000000 | 0x00010000 | 0x0001));
    if (nsem >= sem->size)
        return ((int)(0x80000000 | 0x00010000 | 0x0001));
    s.val = value;
    semctl(sem->semid, nsem, 16, s);
    return 0x00000000;
}

variable_types:
nsem
unsigned short

variable_types:
value
unsigned short

variable_types: union semun { int val; struct semid_ds { struct ipc_perm { int __key; unsigned int uid; unsigned int gid; unsigned int cuid; unsigned int cgid; unsigned short mode; unsigned short __pad1; unsigned short __seq; unsigned short __pad2; unsigned long __glibc_reserved1; unsigned long __glibc_reserved2; } sem_perm; long sem_otime; unsigned long __glibc_reserved1; long sem_ctime; unsigned long __glibc_reserved2; unsigned long sem_nsems; unsigned long __glibc_reserved3; unsigned long __glibc_reserved4; } *buf; unsigned short *array; struct seminfo { int semmap; int semmni; int semmns; int semmnu; int semmsl; int semopm; int semume; int semusz; int semvmx; int semaem; } *__buf; }

user_defined_types: union semun { int val; struct semid_ds *buf; unsigned short *array; struct seminfo *__buf; }

user_defined_types: struct semid_ds { struct ipc_perm sem_perm; long sem_otime; unsigned long __glibc_reserved1; long sem_ctime; unsigned long __glibc_reserved2; unsigned long sem_nsems; unsigned long __glibc_reserved3; unsigned long __glibc_reserved4; }


original_code:
int semaphore_set_values(struct semaphore *sem, unsigned short *values)
{
    union semun s;
    if (sem == ((void *)0) || sem->magic != 'semm')
        return ((int)(0x80000000 | 0x00010000 | 0x0001));
    s.array = values;
    semctl(sem->semid, 0, 17, s);
    return 0x00000000;
}

variable_types:
values
unsigned short *

user_defined_types: struct seminfo { int semmap; int semmni; int semmns; int semmnu; int semmsl; int semopm; int semume; int semusz; int semvmx; int semaem; }

user_defined_types: struct ipc_perm { int __key; unsigned int uid; unsigned int gid; unsigned int cuid; unsigned int cgid; unsigned short mode; unsigned short __pad1; unsigned short __seq; unsigned short __pad2; unsigned long __glibc_reserved1; unsigned long __glibc_reserved2; }


decompiled_code:
__int64 semaphore_open_and_set(__int64 a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9, ...)
{
void *v9; // rsp
unsigned __int16 v10; // dx
_WORD *v11; // rax
_DWORD *v12; // rcx
__int64 result; // rax
_WORD v14[4]; // [rsp+0h] [rbp-60h] BYREF
gcc_va_list va; // [rsp+8h] [rbp-58h] BYREF

if ( !a1 )
return 2147549185LL;
if ( *(_DWORD *)a1 != 1936026989 )
return 2147549185LL;
v9 = alloca((2 * *(unsigned __int16 *)(a1 + 4) + 23) & 0x3FFF0);
va_start(va, a9);
v10 = *(_WORD *)(a1 + 4);
if ( v10 )
{
v11 = v14;
do
{
if ( va[0].gp_offset > 0x2F )
{
v12 = va[0].overflow_arg_area;
va[0].overflow_arg_area = (char *)va[0].overflow_arg_area + 8;
}
else
{
v12 = (char *)va[0].reg_save_area + va[0].gp_offset;
va[0].gp_offset += 8;
}
*v11++ = *v12;
}
while ( v11 != &v14[v10] );
}
result = semaphore_open(a1, 1);
if ( !(_DWORD)result )
return semaphore_set_values((_DWORD *)a1, (__int64)v14);
if ( (_DWORD)result == -2147287034 )
result = semaphore_open(a1, 2);
return result;
}

canonical_decompiled_code:
__int64 func5(__int64 a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9, ...)
{
void *v9; // rsp
unsigned __int16 v10; // dx
_WORD *v11; // rax
_DWORD *v12; // rcx
__int64 result; // rax
_WORD v14[4]; // [rsp+0h] [rbp-60h] BYREF
gcc_va_list va; // [rsp+8h] [rbp-58h] BYREF

if ( !a1 )
return 2147549185LL;
if ( *(_DWORD *)a1 != 1936026989 )
return 2147549185LL;
v9 = alloca((2 * *(unsigned __int16 *)(a1 + 4) + 23) & 0x3FFF0);
va_start(va, a9);
v10 = *(_WORD *)(a1 + 4);
if ( v10 )
{
v11 = v14;
do
{
if ( va[0].gp_offset > 0x2F )
{
v12 = va[0].overflow_arg_area;
va[0].overflow_arg_area = (char *)va[0].overflow_arg_area + 8;
}
else
{
v12 = (char *)va[0].reg_save_area + va[0].gp_offset;
va[0].gp_offset += 8;
}
*v11++ = *v12;
}
while ( v11 != &v14[v10] );
}
result = func2(a1, 1);
if ( !(_DWORD)result )
return func4((_DWORD *)a1, (__int64)v14);
if ( (_DWORD)result == -2147287034 )
result = func2(a1, 2);
return result;
}

original_code:
int semaphore_open_and_set(struct semaphore *sem, ... )
{
    va_list ap;
    int i;
    unsigned short *values;
    int res;
    if (sem == ((void *)0) || sem->magic != 'semm')
        return ((int)(0x80000000 | 0x00010000 | 0x0001));
    values = (unsigned short *) __builtin_alloca (sizeof(unsigned short) * sem->size);
    __builtin_va_start(ap,sem);
    for (i = 0; i < sem->size; i++)
    {
        values[i] = (unsigned short) __builtin_va_arg(ap,int);
    }
    __builtin_va_end(ap);
    res = semaphore_open(sem, 0x0000001);
    if (res == 0x00000000)
    {
        if (0) printf("Created semaphore. setting initial value\n");
        res = semaphore_set_values(sem,values);
    }
    else if (res == ((int)(0x80000000 | 0x00030000 | 0x0006)))
    {
        res = semaphore_open(sem, 0x0000002);
    }
    return res;
}

canonical_original_code:
int semaphore_open_and_set(struct semaphore *sem, ...)
{
    __builtin_va_list ap;
    int i;
    unsigned short *values;
    int res;
    if (sem == ((void *)0) || sem->magic != 'semm')
        return ((int)(0x80000000 | 0x00010000 | 0x0001));
    values = (unsigned short *) __builtin_alloca (sizeof(unsigned short) * sem->size);
    __builtin_va_start(ap,sem);
    for (i = 0; i < sem->size; i++)
    {
        values[i] = (unsigned short) __builtin_va_arg(ap,int);
    }
    __builtin_va_end(ap);
    res = semaphore_open(sem, 0x0000001);
    if (res == 0x00000000)
    {
        if (0) printf("Created semaphore. setting initial value\n");
        res = semaphore_set_values(sem,values);
    }
    else if (res == ((int)(0x80000000 | 0x00030000 | 0x0006)))
    {
        res = semaphore_open(sem, 0x0000002);
    }
    return res;
}

variable_types: i int

variable_types: res int


decompiled_code:
__int64 __fastcall semaphore_P(__int64 a1, unsigned __int16 a2)
{
struct sembuf sops; // [rsp+0h] [rbp-Eh] BYREF

if ( !a1 )
return 2147549185LL;
if ( *(_DWORD *)a1 != 1936026989 )
return 2147549185LL;
if ( *(_WORD *)(a1 + 4) <= a2 )
return 2147549185LL;
sops.sem_num = a2;
sops.sem_op = -1;
sops.sem_flg = 4096;
semop(*(_DWORD *)(a1 + 44), &sops, 1uLL);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, unsigned __int16 a2)
{
struct sembuf sops; // [rsp+0h] [rbp-Eh] BYREF

if ( !a1 )
return 2147549185LL;
if ( *(_DWORD *)a1 != 1936026989 )
return 2147549185LL;
if ( *(_WORD *)(a1 + 4) <= a2 )
return 2147549185LL;
sops.sem_num = a2;
sops.sem_op = -1;
sops.sem_flg = 4096;
semop(*(_DWORD *)(a1 + 44), &sops, 1uLL);
return 0LL;
}

original_code:
int semaphore_P(struct semaphore *sem, unsigned short nsem)
{
    struct sembuf s;
    if (sem == ((void *)0) || sem->magic != 'semm')
        return ((int)(0x80000000 | 0x00010000 | 0x0001));
    if (nsem >= sem->size)
        return ((int)(0x80000000 | 0x00010000 | 0x0001));
    s.sem_num = nsem;
    s.sem_op = -1;
    s.sem_flg = 0x1000;
    if (semop(sem->semid,&s,1) == -1)
    {
    }
    return 0x00000000;
}

variable_types: struct sembuf { unsigned short sem_num; short sem_op; short sem_flg; }


decompiled_code:
__int64 __fastcall semaphore_V(__int64 a1, unsigned __int16 a2)
{
struct sembuf sops; // [rsp+0h] [rbp-Eh] BYREF

if ( !a1 )
return 2147549185LL;
if ( *(_DWORD *)a1 != 1936026989 )
return 2147549185LL;
if ( *(_WORD *)(a1 + 4) <= a2 )
return 2147549185LL;
sops.sem_num = a2;
sops.sem_op = 1;
sops.sem_flg = 4096;
semop(*(_DWORD *)(a1 + 44), &sops, 1uLL);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, unsigned __int16 a2)
{
struct sembuf sops; // [rsp+0h] [rbp-Eh] BYREF

if ( !a1 )
return 2147549185LL;
if ( *(_DWORD *)a1 != 1936026989 )
return 2147549185LL;
if ( *(_WORD *)(a1 + 4) <= a2 )
return 2147549185LL;
sops.sem_num = a2;
sops.sem_op = 1;
sops.sem_flg = 4096;
semop(*(_DWORD *)(a1 + 44), &sops, 1uLL);
return 0LL;
}

original_code:
int semaphore_V(struct semaphore *sem, unsigned short nsem)
{
    struct sembuf s;
    if (sem == ((void *)0) || sem->magic != 'semm')
        return ((int)(0x80000000 | 0x00010000 | 0x0001));
    if (nsem >= sem->size)
        return ((int)(0x80000000 | 0x00010000 | 0x0001));
    s.sem_num = nsem;
    s.sem_op = 1;
    s.sem_flg = 0x1000;
    if (semop(sem->semid,&s,1) == -1)
    {
    }
    return 0x00000000;
}

variable_types: struct semaphore { int magic; unsigned short size; char name[-1]; int semkey; int semid; } *

user_defined_types: struct sembuf { unsigned short sem_num; short sem_op; short sem_flg; }


original_code:
int semaphore_close(struct semaphore *sem, int remove)
{
    char path[255];
    if (remove)
    {
        if (0) printf("Destroying semaphore %s 0x%08x (%d).\n", sem->name, sem->semkey, sem->semid);
        semctl(sem->semid, 0, 0);
        sprintf(path, "%s/%s", "/tmp", sem->name);
        unlink(path);
    }
    sem->semid = -1;
    sem->semkey = -1;
    return 0x00000000;
}

variable_types: remove int


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

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

original_code:
void semaphore_free(struct semaphore *sem)
{
    free(sem);
}


decompiled_code:
__int64 __fastcall json_escape(__int64 a1)
{
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
return a1;
}

original_code:
static const char * json_escape(const char *str)
{
    return str;
}

canonical_original_code:
static char *json_escape(char *str)
{
    return str;
}

variable_types:
str
char *

return_type: char *


decompiled_code:
const char *__fastcall guess_content_type(const char *a1)
{
char *v1; // rbp
const char *result; // rax
char *v3; // rdx
char *v4; // rbp
void **v5; // rbx
void *v6; // rdi

v1 = strrchr(a1, 46);
result = "application/octet-stream";
if ( v1 )
{
v3 = strchr(v1, 47);
result = "application/octet-stream";
if ( !v3 )
{
v4 = v1 + 1;
v5 = &content_type_table;
v6 = &LC0;
while ( (unsigned int)evutil_ascii_strcasecmp(v6, v4) )
{
v5 += 2;
v6 = *v5;
if ( !*v5 )
return "application/octet-stream";
}
result = (const char *)v5[1];
}
}
return result;
}

canonical_decompiled_code:
const char *__fastcall func1(const char *a1)
{
char *v1; // rbp
const char *result; // rax
char *v3; // rdx
char *v4; // rbp
void **v5; // rbx
void *v6; // rdi

v1 = strrchr(a1, 46);
result = "application/octet-stream";
if ( v1 )
{
v3 = strchr(v1, 47);
result = "application/octet-stream";
if ( !v3 )
{
v4 = v1 + 1;
v5 = &content_type_table;
v6 = &LC0;
while ( (unsigned int)evutil_ascii_strcasecmp(v6, v4) )
{
v5 += 2;
v6 = *v5;
if ( !*v5 )
return "application/octet-stream";
}
result = (const char *)v5[1];
}
}
return result;
}

original_code:
static const char *guess_content_type(const char *path)
{
    const char *last_period, *extension;
    const struct table_entry *ent;
    last_period = strrchr(path, '.');
    if (!last_period || strchr(last_period, '/'))
        goto not_found;
    extension = last_period + 1;
    for (ent = &content_type_table[0]; ent->extension; ++ent) {
        if (!evutil_ascii_strcasecmp(ent->extension, extension))
            return ent->content_type;
    }
not_found:
    return "application/octet-stream";
}

canonical_original_code:
static char *guess_content_type(char *path)
{
    char *last_period; char *extension;
    struct table_entry *ent;
    last_period = strrchr(path, '.');
    if (!last_period || strchr(last_period, '/'))
        goto not_found;
    extension = last_period + 1;
    for (ent = &content_type_table[0]; ent->extension; ++ent) {
        if (!evutil_ascii_strcasecmp(ent->extension, extension))
            return ent->content_type;
    }
not_found:
    return "application/octet-stream";
}

variable_types:
path
char *

variable_types:
extension
char *

variable_types: struct table_entry { char *extension; char *content_type; } *


decompiled_code:
__int64 __fastcall http_find_metrics_from_query(__int64 a1, __int64 a2)
{
__int64 v2; // rbx
const char *v4; // rax
__int64 v5[5]; // [rsp+0h] [rbp-28h] BYREF

v2 = a2;
if ( a2 )
{
v5[0] = 0LL;
v5[1] = 0LL;
v2 = 0LL;
if ( !(unsigned int)evhttp_parse_query_str(a2, v5) )
{
v4 = (const char *)evhttp_find_header(v5, "series");
v2 = (__int64)v4;
if ( v4 )
{
printf("query is for %s\n", v4);
v2 = metrics_find_metric(a1, v2);
}
}
evhttp_clear_headers(v5);
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, __int64 a2)
{
__int64 v2; // rbx
const char *v4; // rax
__int64 v5[5]; // [rsp+0h] [rbp-28h] BYREF

v2 = a2;
if ( a2 )
{
v5[0] = 0LL;
v5[1] = 0LL;
v2 = 0LL;
if ( !(unsigned int)evhttp_parse_query_str(a2, v5) )
{
v4 = (const char *)evhttp_find_header(v5, "series");
v2 = (__int64)v4;
if ( v4 )
{
printf("query is for %s\n", v4);
v2 = metrics_find_metric(a1, v2);
}
}
evhttp_clear_headers(v5);
}
return v2;
}

original_code:
static struct metric_history * http_find_metrics_from_query(struct metrics *m, const char *query)
{
    struct metric_history *mh = ((void *)0);
    struct evkeyvalq params;
    const char *metric_name;
    if (query)
    {
        memset(&params, 0, sizeof(params));
        if (evhttp_parse_query_str(query, &params) == 0)
        {
            metric_name = evhttp_find_header(&params,"series");
            if (metric_name != ((void *)0))
            {
                printf("query is for %s\n", metric_name);
                mh = metrics_find_metric(m,metric_name);
            }
        }
        evhttp_clear_headers(&params);
    }
    return mh;
}

canonical_original_code:
static struct metric_history *http_find_metrics_from_query(struct metrics *m, char *query)
{
    struct metric_history *mh = ((void *)0);
    struct evkeyvalq params;
    char *metric_name;
    if (query)
    {
        memset(&params, 0, sizeof(params));
        if (evhttp_parse_query_str(query, &params) == 0)
        {
            metric_name = evhttp_find_header(&params,"series");
            if (metric_name != ((void *)0))
            {
                printf("query is for %s\n", metric_name);
                mh = metrics_find_metric(m,metric_name);
            }
        }
        evhttp_clear_headers(&params);
    }
    return mh;
}

variable_types: struct metrics { struct metric_history *metrics[3001]; } *

variable_types:
query
char *

variable_types: struct metric_history { char name[32]; int fd; struct hist_file { struct hist_file_header { int magic; int file_version; int nseries; int reserved; } hdr; } *h; } *

variable_types: struct evkeyvalq { struct evkeyval { struct { struct evkeyval *tqe_next; struct evkeyval **tqe_prev; } next; char *key; char *value; } *tqh_first; struct evkeyval **tqh_last; }

user_defined_types: struct evkeyvalq { struct evkeyval *tqh_first; struct evkeyval **tqh_last; }

user_defined_types: struct metric_history { char name[32]; int fd; struct hist_file *h; }

user_defined_types: struct hist_file { struct hist_file_header hdr; }


decompiled_code:
int init_screen()
{
initscr();
cbreak();
noecho();
nonl();
intrflush(stdscr, 0);
keypad(stdscr, 1);
scrollok(stdscr, 1);
wclear(stdscr);
return wrefresh(stdscr);
}

canonical_decompiled_code:
int func0()
{
initscr();
cbreak();
noecho();
nonl();
intrflush(stdscr, 0);
keypad(stdscr, 1);
scrollok(stdscr, 1);
wclear(stdscr);
return wrefresh(stdscr);
}

original_code:
void init_screen()
{
    initscr();
    cbreak();
    noecho();
    nonl();
    intrflush(stdscr, 0);
    keypad(stdscr, 1);
    scrollok(stdscr, 1);
    wclear(stdscr);
    wrefresh(stdscr);
}


decompiled_code:
int close_screen()
{
return endwin();
}

canonical_decompiled_code:
int func1()
{
return endwin();
}

original_code:
void close_screen()
{
    endwin();
}

canonical_original_code:
void close_screen()
{
    endwin();
}


decompiled_code:
__int64 __fastcall lock_create(__int64 a1, _QWORD *a2)
{
void *v2; // rax
void *v3; // rbx
unsigned int v4; // er12

if ( !a2 )
return (unsigned int)-2147418111;
v2 = malloc(0x30uLL);
v3 = v2;
if ( !v2 )
return (unsigned int)-2147418110;
v4 = semaphore_init(v2, a1, 1LL);
if ( v4 )
{
free(v3);
*a2 = 0LL;
}
else
{
*a2 = v3;
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, _QWORD *a2)
{
void *v2; // rax
void *v3; // rbx
unsigned int v4; // er12

if ( !a2 )
return (unsigned int)-2147418111;
v2 = malloc(0x30uLL);
v3 = v2;
if ( !v2 )
return (unsigned int)-2147418110;
v4 = semaphore_init(v2, a1, 1LL);
if ( v4 )
{
free(v3);
*a2 = 0LL;
}
else
{
*a2 = v3;
}
return v4;
}

original_code:
int lock_create(const char * name, struct lock **lock_out)
{
    struct lock * lock;
    int err;
    if (lock_out == ((void *)0))
        return ((int)(0x80000000 | 0x00010000 | 0x0001));
    lock = malloc(sizeof(struct lock));
    if (!lock)
        return ((int)(0x80000000 | 0x00010000 | 0x0002));
    err = semaphore_init(&(lock)->sem,name,1);
    if (err == 0x00000000)
    {
        *lock_out = lock;
    }
    else
    {
        free(lock);
        *lock_out = ((void *)0);
    }
    return err;
}

canonical_original_code:
int lock_create(char *name, struct lock **lock_out)
{
    struct lock *lock;
    int err;
    if (lock_out == ((void *)0))
        return ((int)(0x80000000 | 0x00010000 | 0x0001));
    lock = malloc(sizeof(struct lock));
    if (!lock)
        return ((int)(0x80000000 | 0x00010000 | 0x0002));
    err = semaphore_init(&(lock)->sem,name,1);
    if (err == 0x00000000)
    {
        *lock_out = lock;
    }
    else
    {
        free(lock);
        *lock_out = ((void *)0);
    }
    return err;
}

variable_types:
name
char *

variable_types: struct lock { struct semaphore { int magic; unsigned short size; char name[-1]; int semkey; int semid; } sem; } **

variable_types: err int


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

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

original_code:
void lock_free(struct lock *lock)
{
    free(lock);
}

variable_types: struct lock { struct semaphore { int magic; unsigned short size; char name[-1]; int semkey; int semid; } sem; } *


decompiled_code:
int __fastcall micro_sleep(int a1, int a2)
{
struct timespec v3; // [rsp+0h] [rbp-18h] BYREF

v3.tv_sec = a1;
v3.tv_nsec = 1000 * a2;
return nanosleep(&v3, 0LL);
}

canonical_decompiled_code:
int __fastcall func0(int a1, int a2)
{
struct timespec v3; // [rsp+0h] [rbp-18h] BYREF

v3.tv_sec = a1;
v3.tv_nsec = 1000 * a2;
return nanosleep(&v3, 0LL);
}

original_code:
void micro_sleep(int sec, int microsec)
{
    struct timespec ts;
    ts.tv_sec = sec;
    ts.tv_nsec = microsec * 1000;
    nanosleep(&ts, ((void *)0));
}

variable_types: sec int

variable_types: microsec int

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


writer

decompiled_code:
__int64 __fastcall writer(unsigned int a1)
{
__int64 v1; // rax
__int64 v2; // r14
int v3; // er13
int i; // er12
__int64 *v5; // rbx
__int64 v7; // [rsp+18h] [rbp-E0h] BYREF
__int64 v8[27]; // [rsp+20h] [rbp-D8h] BYREF

v7 = 0LL;
v1 = open_stats();
if ( !v1 )
return 2147549184LL;
v2 = v1;
v3 = 0;
for ( i = 0; i != 200; ++i )
{
micro_sleep(0, 100000);
if ( i == 10 * (i / 10) )
{
printf("writer %d: allocating counter %s\n", a1, counter_names[v3]);
stats_allocate_counter(v2, counter_names[v3], &v7);
v8[v3++] = v7;
}
if ( v3 > 0 )
{
v5 = v8;
do
counter_increment(*v5++);
while ( v5 != &v8[(unsigned int)(v3 - 1) + 1] );
}
}
stats_close(v2);
stats_free(v2);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1)
{
__int64 v1; // rax
__int64 v2; // r14
int v3; // er13
int i; // er12
__int64 *v5; // rbx
__int64 v7; // [rsp+18h] [rbp-E0h] BYREF
__int64 v8[27]; // [rsp+20h] [rbp-D8h] BYREF

v7 = 0LL;
v1 = func1();
if ( !v1 )
return 2147549184LL;
v2 = v1;
v3 = 0;
for ( i = 0; i != 200; ++i )
{
func0(0, 100000);
if ( i == 10 * (i / 10) )
{
printf("writer %d: allocating counter %s\n", a1, counter_names[v3]);
stats_allocate_counter(v2, counter_names[v3], &v7);
v8[v3++] = v7;
}
if ( v3 > 0 )
{
v5 = v8;
do
counter_increment(*v5++);
while ( v5 != &v8[(unsigned int)(v3 - 1) + 1] );
}
}
stats_close(v2);
stats_free(v2);
return 0LL;
}

original_code:
int writer(int n)
{
    struct stats *stats = ((void *)0);
    int err = 0x00000000;
    struct stats_counter *ctrs[20];
    struct stats_counter *ctr = ((void *)0);
    int i, j, nctr = 0;
    ((void) sizeof (((sizeof(counter_names) / sizeof(*counter_names)) == 20) ? 1 : 0), __extension__ ({ if ((sizeof(counter_names) / sizeof(*counter_names)) == 20) ; else __assert_fail ("NCOUNTERNAMES == 20", "test/stats_test.c", 83, __extension__ __PRETTY_FUNCTION__); }));
    stats = open_stats();
    if (!stats)
    {
        return ((int)(0x80000000 | 0x00010000 | 0x0000));
    }
    for (i = 0; i < (sizeof(counter_names) / sizeof(*counter_names)) * 10; i++)
    {
        micro_sleep(0,100000);
        if ((i % 10) == 0)
        {
            printf("writer %d: allocating counter %s\n", n, counter_names[nctr]);
            stats_allocate_counter(stats,counter_names[nctr],&ctr);
            ctrs[nctr++] = ctr;
        }
        for (j = 0; j < nctr; j++)
        {
            counter_increment(ctrs[j]);
        }
    }
    if (stats)
    {
        stats_close(stats);
        stats_free(stats);
    }
    return err;
}

canonical_original_code:
int writer(int n)
{
    struct stats *stats = ((void *)0);
    int err = 0x00000000;
    struct stats_counter *ctrs[20];
    struct stats_counter *ctr = ((void *)0);
    int i; int j; int nctr = 0;
    ((void) sizeof (((sizeof(counter_names) / sizeof(*counter_names)) == 20) ? 1 : 0), __extension__ ({ if ((sizeof(counter_names) / sizeof(*counter_names)) == 20) ; else __assert_fail ("NCOUNTERNAMES == 20", "test/stats_test.c", 83, __extension__ __PRETTY_FUNCTION__); }));
    stats = open_stats();
    if (!stats)
    {
        return ((int)(0x80000000 | 0x00010000 | 0x0000));
    }
    for (i = 0; i < (sizeof(counter_names) / sizeof(*counter_names)) * 10; i++)
    {
        micro_sleep(0,100000);
        if ((i % 10) == 0)
        {
            printf("writer %d: allocating counter %s\n", n, counter_names[nctr]);
            stats_allocate_counter(stats,counter_names[nctr],&ctr);
            ctrs[nctr++] = ctr;
        }
        for (j = 0; j < nctr; j++)
        {
            counter_increment(ctrs[j]);
        }
    }
    if (stats)
    {
        stats_close(stats);
        stats_free(stats);
    }
    return err;
}

variable_types: n int

variable_types: struct stats { int magic; struct shared_memory { int magic; int flags; int size; char name[-1]; int shmkey; int shmid; short created; void *ptr; } shmem; struct lock { struct semaphore { int magic; unsigned short size; char name[-1]; int semkey; int semid; } sem; } lock; struct stats_data { struct stats_header { int stats_magic; int stats_sequence_number; char reserved[8]; } hdr; struct stats_counter { int ctr_allocation_status; int ctr_allocation_seq; union STATS_VALUE { long long val64; struct { int hi; int lo; } val32; } ctr_value; int ctr_flags; int ctr_key_len; char ctr_key[32]; } ctr[2003]; } *data; } *

variable_types: struct stats_counter { int ctr_allocation_status; int ctr_allocation_seq; union STATS_VALUE { long long val64; struct { int hi; int lo; } val32; } ctr_value; int ctr_flags; int ctr_key_len; char ctr_key[32]; } *[20]

variable_types: i int

variable_types: j int

variable_types: nctr int

user_defined_types: struct stats { int magic; struct shared_memory shmem; struct lock lock; struct stats_data *data; }

user_defined_types: struct stats_counter { int ctr_allocation_status; int ctr_allocation_seq; union STATS_VALUE ctr_value; int ctr_flags; int ctr_key_len; char ctr_key[32]; }

user_defined_types: struct stats_data { struct stats_header hdr; struct stats_counter ctr[2003]; }


reader

decompiled_code:
__int64 __fastcall reader(unsigned int a1)
{
_QWORD *v1; // rax
_QWORD *v2; // rbp
__int64 v3; // r15
int v4; // er14
unsigned int v5; // er13
__int64 v6; // rbx
unsigned int v7; // eax
const char *v8; // rax
int v10; // [rsp+8h] [rbp-70h]
char v11[40]; // [rsp+10h] [rbp-68h] BYREF
int v12; // [rsp+38h] [rbp-40h] BYREF
int v13[15]; // [rsp+3Ch] [rbp-3Ch] BYREF

v12 = 0;
v1 = malloc(0x3E98uLL);
if ( v1 )
{
v2 = v1;
memset(v1, 0, 0x3E98uLL);
v3 = open_stats();
if ( v3 )
{
v10 = 0;
v4 = 0;
do
{
micro_sleep(1, 0);
v13[0] = *(_DWORD *)(*(_QWORD *)(v3 + 120) + 4LL);
v5 = 0;
if ( v13[0] != v10 )
{
v7 = stats_get_counters(v3, v2, 2003LL, &v12, v13);
v5 = v7;
if ( v7 )
{
v8 = (const char *)error_message(v7);
printf("reader %d: Error %08x getting counters %s\n", a1, v5, v8);
}
v10 = v13[0];
}
puts("\n\n[[===============");
if ( v12 > 0 )
{
v6 = 0LL;
do
{
counter_get_key(v2[v6], v11, 33LL);
printf("%s: %lld\n", v11, *(_QWORD *)(v2[v6++] + 8LL));
}
while ( v12 > (int)v6 );
}
puts("===============]]\n");
++v4;
}
while ( v4 <= 24 && !v5 );
stats_close(v3);
stats_free(v3);
}
else
{
v5 = -2147418112;
}
}
else
{
puts("failed to allocate memory");
v5 = -2147418112;
}
return v5;
}

original_code:
int reader(int n)
{
    struct stats *stats = ((void *)0);
    int err = 0x00000000;
    int i, j;
    int seq = 0, nseq;
    struct stats_counter **counters;
    int ncounters = 0;
    char counter_name[32 +1];
    counters = (struct stats_counter **) malloc(sizeof(struct stats_counter *) * 2003);
    if (!counters)
    {
        printf("failed to allocate memory\n");
        return ((int)(0x80000000 | 0x00010000 | 0x0000));
    }
    memset(counters, 0, sizeof(struct stats_counter *) * 2003);
    stats = open_stats();
    if (!stats)
    {
        return ((int)(0x80000000 | 0x00010000 | 0x0000));
    }
    for (i = 0; i < 25 && err == 0x00000000; i++)
    {
        micro_sleep(1,0);
        nseq = ((stats)->data->hdr.stats_sequence_number);
        if (nseq != seq)
        {
            err = stats_get_counters(stats, counters, 2003, &ncounters, &nseq);
            if (err != 0x00000000)
            {
                printf("reader %d: Error %08x getting counters %s\n",n,err,error_message(err));
            }
            seq = nseq;
        }
        printf("\n\n[[===============\n");
        for (j = 0; j < ncounters; j++)
        {
            counter_get_key(counters[j],counter_name,32 +1);
            printf("%s: %lld\n", counter_name, counters[j]->ctr_value.val64);
        }
        printf("===============]]\n\n");
    }
    if (stats)
    {
        stats_close(stats);
        stats_free(stats);
    }
    return err;
}

canonical_original_code:
int reader(int n)
{
    struct stats *stats = ((void *)0);
    int err = 0x00000000;
    int i; int j;
    int seq = 0; int nseq;
    struct stats_counter **counters;
    int ncounters = 0;
    char counter_name[-1];
    counters = (struct stats_counter **) malloc(sizeof(struct stats_counter *) * 2003);
    if (!counters)
    {
        printf("failed to allocate memory\n");
        return ((int)(0x80000000 | 0x00010000 | 0x0000));
    }
    memset(counters, 0, sizeof(struct stats_counter *) * 2003);
    stats = open_stats();
    if (!stats)
    {
        return ((int)(0x80000000 | 0x00010000 | 0x0000));
    }
    for (i = 0; i < 25 && err == 0x00000000; i++)
    {
        micro_sleep(1,0);
        nseq = ((stats)->data->hdr.stats_sequence_number);
        if (nseq != seq)
        {
            err = stats_get_counters(stats, counters, 2003, &ncounters, &nseq);
            if (err != 0x00000000)
            {
                printf("reader %d: Error %08x getting counters %s\n",n,err,error_message(err));
            }
            seq = nseq;
        }
        printf("\n\n[[===============\n");
        for (j = 0; j < ncounters; j++)
        {
            counter_get_key(counters[j],counter_name,32 +1);
            printf("%s: %lld\n", counter_name, counters[j]->ctr_value.val64);
        }
        printf("===============]]\n\n");
    }
    if (stats)
    {
        stats_close(stats);
        stats_free(stats);
    }
    return err;
}

variable_types: seq int

variable_types: nseq int

variable_types: struct stats_counter { int ctr_allocation_status; int ctr_allocation_seq; union STATS_VALUE { long long val64; struct { int hi; int lo; } val32; } ctr_value; int ctr_flags; int ctr_key_len; char ctr_key[32]; } **

variable_types: ncounters int

user_defined_types: struct shared_memory { int magic; int flags; int size; char name[-1]; int shmkey; int shmid; short created; void *ptr; }

user_defined_types: struct stats_header { int stats_magic; int stats_sequence_number; char reserved[8]; }


decompiled_code:
__int64 open_stats()
{
unsigned int v0; // eax
unsigned int v1; // eax
__int64 v2; // rdx
const char *v4; // rax
const char *v5; // rax
__int64 v6; // [rsp+8h] [rbp-10h] BYREF

v6 = 0LL;
v0 = stats_create("stattest", &v6);
if ( v0 )
{
v4 = (const char *)error_message(v0);
printf("Failed to create stats: %s\n", v4);
v2 = 0LL;
}
else
{
v1 = stats_open(v6);
v2 = v6;
if ( v1 )
{
v5 = (const char *)error_message(v1);
printf("Failed to open stats: %s\n", v5);
stats_free(v6);
v2 = 0LL;
}
}
return v2;
}

canonical_decompiled_code:
__int64 func1()
{
unsigned int v0; // eax
unsigned int v1; // eax
__int64 v2; // rdx
const char *v4; // rax
const char *v5; // rax
__int64 v6; // [rsp+8h] [rbp-10h] BYREF

v6 = 0LL;
v0 = stats_create("stattest", &v6);
if ( v0 )
{
v4 = (const char *)error_message(v0);
printf("Failed to create stats: %s\n", v4);
v2 = 0LL;
}
else
{
v1 = stats_open(v6);
v2 = v6;
if ( v1 )
{
v5 = (const char *)error_message(v1);
printf("Failed to open stats: %s\n", v5);
stats_free(v6);
v2 = 0LL;
}
}
return v2;
}

original_code:
struct stats *open_stats()
{
    struct stats *stats = ((void *)0);
    int err;
    err = stats_create("stattest",&stats);
    if (err != 0x00000000)
    {
        printf("Failed to create stats: %s\n", error_message(err));
        return ((void *)0);
    }
    err = stats_open(stats);
    if (err != 0x00000000)
    {
        printf("Failed to open stats: %s\n", error_message(err));
        stats_free(stats);
        return ((void *)0);
    }
    return stats;
}

return_type: struct stats { int magic; struct shared_memory { int magic; int flags; int size; char name[-1]; int shmkey; int shmid; short created; void *ptr; } shmem; struct lock { struct semaphore { int magic; unsigned short size; char name[-1]; int semkey; int semid; } sem; } lock; struct stats_data { struct stats_header { int stats_magic; int stats_sequence_number; char reserved[8]; } hdr; struct stats_counter { int ctr_allocation_status; int ctr_allocation_seq; union STATS_VALUE { long long val64; struct { int hi; int lo; } val32; } ctr_value; int ctr_flags; int ctr_key_len; char ctr_key[32]; } ctr[2003]; } *data; } *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // ebx
unsigned int v4; // eax
unsigned int v5; // eax
int v6; // er12
int *v7; // rax
unsigned int v8; // ebx
int *v9; // rax
__WAIT_STATUS stat_loc; // [rsp+Ch] [rbp-1Ch] BYREF

v3 = 0;
while ( 1 )
{
v4 = getpid();
printf("calling fork from %d\n", v4);
fflush(stdout);
fflush(stderr);
v5 = fork();
v6 = v5;
LODWORD(stat_loc.__uptr) = v5;
if ( v5 == -1 )
{
puts("error: could not fork");
return v6;
}
if ( !v5 )
break;
printf("Launched pid %d\n", v5);
if ( ++v3 == 3 )
{
puts("Waiting for child processes");
while ( 1 )
{
v8 = wait((__WAIT_STATUS)&stat_loc);
if ( v8 == -1 )
break;
v7 = _errno_location();
printf("wait returned %d, stat=%d, errno=%d\n", v8, LODWORD(stat_loc.__uptr), (unsigned int)*v7);
}
v9 = _errno_location();
printf("wait returned %d, stat=%d, errno=%d\n", 0xFFFFFFFFLL, LODWORD(stat_loc.__uptr), (unsigned int)*v9);
return 0;
}
}
puts("in child");
if ( v3 )
v6 = writer(v3);
else
v6 = reader(0);
return v6;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // ebx
unsigned int v4; // eax
unsigned int v5; // eax
int v6; // er12
int *v7; // rax
unsigned int v8; // ebx
int *v9; // rax
__WAIT_STATUS stat_loc; // [rsp+Ch] [rbp-1Ch] BYREF

v3 = 0;
while ( 1 )
{
v4 = getpid();
printf("calling fork from %d\n", v4);
fflush(stdout);
fflush(stderr);
v5 = fork();
v6 = v5;
LODWORD(stat_loc.__uptr) = v5;
if ( v5 == -1 )
{
puts("error: could not fork");
return v6;
}
if ( !v5 )
break;
printf("Launched pid %d\n", v5);
if ( ++v3 == 3 )
{
puts("Waiting for child processes");
while ( 1 )
{
v8 = wait((__WAIT_STATUS)&stat_loc);
if ( v8 == -1 )
break;
v7 = _errno_location();
printf("wait returned %d, stat=%d, errno=%d\n", v8, LODWORD(stat_loc.__uptr), (unsigned int)*v7);
}
v9 = _errno_location();
printf("wait returned %d, stat=%d, errno=%d\n", 0xFFFFFFFFLL, LODWORD(stat_loc.__uptr), (unsigned int)*v9);
return 0;
}
}
puts("in child");
if ( v3 )
v6 = func2(v3);
else
v6 = func3(0);
return v6;
}

original_code:
int main(int argc, char **argv)
{
    int i, n;
    for (i = 0; i < 3; i++)
    {
        printf("calling fork from %d\n", getpid());
        fflush(stdout);
        fflush(stderr);
        n = fork();
        if (n == -1)
        {
            printf("error: could not fork\n");
            return -1;
        }
        else if (n == 0)
        {
            printf("in child\n");
            if (i == 0)
                return reader(i);
            else
                return writer(i);
        }
        else
        {
            printf("Launched pid %d\n", n);
        }
    }
    printf("Waiting for child processes\n");
    while ((i = wait(&n)) != -1)
    {
        printf("wait returned %d, stat=%d, errno=%d\n", i, n, (*__errno_location ()));
    }
    printf("wait returned %d, stat=%d, errno=%d\n", i, n, (*__errno_location ()));
    return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
    int i; int n;
    for (i = 0; i < 3; i++)
    {
        printf("calling fork from %d\n", getpid());
        fflush(stdout);
        fflush(stderr);
        n = fork();
        if (n == -1)
        {
            printf("error: could not fork\n");
            return -1;
        }
        else if (n == 0)
        {
            printf("in child\n");
            if (i == 0)
                return reader(i);
            else
                return writer(i);
        }
        else
        {
            printf("Launched pid %d\n", n);
        }
    }
    printf("Waiting for child processes\n");
    while ((i = wait(&n)) != -1)
    {
        printf("wait returned %d, stat=%d, errno=%d\n", i, n, (*__errno_location ()));
    }
    printf("wait returned %d, stat=%d, errno=%d\n", i, n, (*__errno_location ()));
    return 0;
}

variable_types: argc int

variable_types:
argv
char **


original_code:
int shared_memory_init(struct shared_memory *shmem, const char *name, int flags, int size)
{
    if (shmem == ((void *)0))
        return ((int)(0x80000000 | 0x00010000 | 0x0001));
    if (strlen(name) >= 31)
        return ((int)(0x80000000 | 0x00020000 | 0x0001));
    memset(shmem, 0, sizeof(struct shared_memory));
    shmem->magic = 'shmm';
    shmem->shmkey = -1;
    shmem->shmid = -1;
    shmem->flags = flags;
    shmem->size = size;
    strcpy(shmem->name, name);
    return 0x00000000;
}

canonical_original_code:
int shared_memory_init(struct shared_memory *shmem, char *name, int flags, int size)
{
    if (shmem == ((void *)0))
        return ((int)(0x80000000 | 0x00010000 | 0x0001));
    if (strlen(name) >= 31)
        return ((int)(0x80000000 | 0x00020000 | 0x0001));
    memset(shmem, 0, sizeof(struct shared_memory));
    shmem->magic = 'shmm';
    shmem->shmkey = -1;
    shmem->shmid = -1;
    shmem->flags = flags;
    shmem->size = size;
    strcpy(shmem->name, name);
    return 0x00000000;
}

variable_types: struct shared_memory { int magic; int flags; int size; char name[-1]; int shmkey; int shmid; short created; void *ptr; } *

variable_types:
name
char *

variable_types: flags int

variable_types: size int


decompiled_code:
__int64 __fastcall shared_memory_create(const char *a1, int a2, int a3, _QWORD *a4)
{
void *v6; // rax
void *v7; // rbx
unsigned int v8; // er12

if ( !a4 )
return (unsigned int)-2147418111;
v6 = malloc(0x40uLL);
v7 = v6;
if ( !v6 )
return (unsigned int)-2147418110;
v8 = shared_memory_init((__int64)v6, a1, a2, a3);
if ( v8 )
{
free(v7);
*a4 = 0LL;
}
else
{
*a4 = v7;
}
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1, int a2, int a3, _QWORD *a4)
{
void *v6; // rax
void *v7; // rbx
unsigned int v8; // er12

if ( !a4 )
return (unsigned int)-2147418111;
v6 = malloc(0x40uLL);
v7 = v6;
if ( !v6 )
return (unsigned int)-2147418110;
v8 = func0((__int64)v6, a1, a2, a3);
if ( v8 )
{
free(v7);
*a4 = 0LL;
}
else
{
*a4 = v7;
}
return v8;
}

original_code:
int shared_memory_create(const char *name, int flags, int size, struct shared_memory **shmem_out )
{
    struct shared_memory * shmem;
    int err;
    if (shmem_out == ((void *)0))
        return ((int)(0x80000000 | 0x00010000 | 0x0001));
    shmem = malloc(sizeof(struct shared_memory));
    if (!shmem)
        return ((int)(0x80000000 | 0x00010000 | 0x0002));
    err = shared_memory_init(shmem,name,flags,size);
    if (err == 0x00000000)
    {
        *shmem_out = shmem;
    }
    else
    {
        free(shmem);
        *shmem_out = ((void *)0);
    }
    return err;
}

canonical_original_code:
int shared_memory_create(char *name, int flags, int size, struct shared_memory **shmem_out)
{
    struct shared_memory *shmem;
    int err;
    if (shmem_out == ((void *)0))
        return ((int)(0x80000000 | 0x00010000 | 0x0001));
    shmem = malloc(sizeof(struct shared_memory));
    if (!shmem)
        return ((int)(0x80000000 | 0x00010000 | 0x0002));
    err = shared_memory_init(shmem,name,flags,size);
    if (err == 0x00000000)
    {
        *shmem_out = shmem;
    }
    else
    {
        free(shmem);
        *shmem_out = ((void *)0);
    }
    return err;
}

variable_types: struct shared_memory { int magic; int flags; int size; char name[-1]; int shmkey; int shmid; short created; void *ptr; } **

variable_types: err int


original_code:
int shared_memory_open(struct shared_memory * shmem)
{
    struct stat s;
    char path[255];
    int mode;
    struct shmid_ds ds;
    int omode;
    if (shmem == ((void *)0) || shmem->magic != 'shmm')
        return ((int)(0x80000000 | 0x00010000 | 0x0001));
    omode = shmem->flags & 0x0000007;
    if (stat("/tmp",&s) != 0)
    {
        if (mkdir("/tmp", 0755) != 0)
            return ((int)(0x80000000 | 0x00020000 | 0x0002));
    }
    else
    {
        if ((s.st_mode & 0040000) != 0040000)
            return ((int)(0x80000000 | 0x00020000 | 0x0003));
    }
    sprintf(path, "%s/%s", "/tmp", shmem->name);
    if (access(path, 2) == -1)
    {
        int fd = creat(path, 0644 );
        if (fd == -1)
            return ((int)(0x80000000 | 0x00020000 | 0x0004));
        close(fd);
    }
    shmem->shmkey = ftok(path, 1);
    if (shmem->shmkey == -1)
    {
        return ((int)(0x80000000 | 0x00020000 | 0x0005));
    }
    switch (omode)
    {
    case 0x0000001:
        mode = 01000 | 02000 | 0644;
        break;
    case 0x0000002:
        mode = 01000 | 0644;
        break;
    case 0x0000004:
    default:
        mode = 0644;
        break;
    }
    if (0) printf("Opening shmkey %08x for %s (absolute path: %s), mode 0%o, size %d\n", shmem->shmkey, shmem->name, path, mode, shmem->size);
    shmem->shmid = shmget(shmem->shmkey, shmem->size, mode) ;
    if (shmem->shmid == -1)
    {
        if ((*__errno_location ()) == 17 && omode == 0x0000001)
            return ((int)(0x80000000 | 0x00020000 | 0x0006));
        if ((*__errno_location ()) == 2 && omode == 0x0000004)
            return ((int)(0x80000000 | 0x00020000 | 0x0007));
        if ((*__errno_location ()) == 22)
            return ((int)(0x80000000 | 0x00020000 | 0x0008));
        return ((int)(0x80000000 | 0x00020000 | 0x000A));
    }
    switch (omode)
    {
    case 0x0000001:
        shmem->created = 1;
        break;
    case 0x0000002:
        if (shmctl(shmem->shmid, 2, &ds) != 0)
            return ((int)(0x80000000 | 0x00020000 | 0x0009));
        if (ds.shm_cpid == getpid())
            shmem->created = 1;
        else
            shmem->created = 0;
        break;
    case 0x0000004:
        shmem->created = 0;
        break;
    }
    shmem->ptr = shmat(shmem->shmid, ((void *)0), 0);
    if ((intptr_t)shmem->ptr == -1)
    {
        shmem->ptr = ((void *)0);
        return ((int)(0x80000000 | 0x00020000 | 0x000B));
    }
    if (0) printf("Successfylly openend shm 0x%08x for %s.  Attached at 0x%016lx\n", shmem->shmkey, shmem->name, (intptr_t) shmem->ptr);
    return 0x00000000;
}

canonical_original_code:
int shared_memory_open(struct shared_memory *shmem)
{
    struct stat s;
    char path[255];
    int mode;
    struct shmid_ds ds;
    int omode;
    if (shmem == ((void *)0) || shmem->magic != 'shmm')
        return ((int)(0x80000000 | 0x00010000 | 0x0001));
    omode = shmem->flags & 0x0000007;
    if (stat("/tmp",&s) != 0)
    {
        if (mkdir("/tmp", 0755) != 0)
            return ((int)(0x80000000 | 0x00020000 | 0x0002));
    }
    else
    {
        if ((s.st_mode & 0040000) != 0040000)
            return ((int)(0x80000000 | 0x00020000 | 0x0003));
    }
    sprintf(path, "%s/%s", "/tmp", shmem->name);
    if (access(path, 2) == -1)
    {
        int fd = creat(path, 0644 );
        if (fd == -1)
            return ((int)(0x80000000 | 0x00020000 | 0x0004));
        close(fd);
    }
    shmem->shmkey = ftok(path, 1);
    if (shmem->shmkey == -1)
    {
        return ((int)(0x80000000 | 0x00020000 | 0x0005));
    }
    switch (omode)
    {
    case 0x0000001:
        mode = 01000 | 02000 | 0644;
        break;
    case 0x0000002:
        mode = 01000 | 0644;
        break;
    case 0x0000004:
    default:
        mode = 0644;
        break;
    }
    if (0) printf("Opening shmkey %08x for %s (absolute path: %s), mode 0%o, size %d\n", shmem->shmkey, shmem->name, path, mode, shmem->size);
    shmem->shmid = shmget(shmem->shmkey, shmem->size, mode) ;
    if (shmem->shmid == -1)
    {
        if ((*__errno_location ()) == 17 && omode == 0x0000001)
            return ((int)(0x80000000 | 0x00020000 | 0x0006));
        if ((*__errno_location ()) == 2 && omode == 0x0000004)
            return ((int)(0x80000000 | 0x00020000 | 0x0007));
        if ((*__errno_location ()) == 22)
            return ((int)(0x80000000 | 0x00020000 | 0x0008));
        return ((int)(0x80000000 | 0x00020000 | 0x000A));
    }
    switch (omode)
    {
    case 0x0000001:
        shmem->created = 1;
        break;
    case 0x0000002:
        if (shmctl(shmem->shmid, 2, &ds) != 0)
            return ((int)(0x80000000 | 0x00020000 | 0x0009));
        if (ds.shm_cpid == getpid())
            shmem->created = 1;
        else
            shmem->created = 0;
        break;
    case 0x0000004:
        shmem->created = 0;
        break;
    }
    shmem->ptr = shmat(shmem->shmid, ((void *)0), 0);
    if ((long)shmem->ptr == -1)
    {
        shmem->ptr = ((void *)0);
        return ((int)(0x80000000 | 0x00020000 | 0x000B));
    }
    if (0) printf("Successfylly openend shm 0x%08x for %s.  Attached at 0x%016lx\n", shmem->shmkey, shmem->name, (long) shmem->ptr);
    return 0x00000000;
}

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

variable_types:
path
char [255]

variable_types: mode int

variable_types: struct shmid_ds { struct ipc_perm { int __key; unsigned int uid; unsigned int gid; unsigned int cuid; unsigned int cgid; unsigned short mode; unsigned short __pad1; unsigned short __seq; unsigned short __pad2; unsigned long __glibc_reserved1; unsigned long __glibc_reserved2; } shm_perm; unsigned long shm_segsz; long shm_atime; long shm_dtime; long shm_ctime; int shm_cpid; int shm_lpid; unsigned long shm_nattch; unsigned long __glibc_reserved4; unsigned long __glibc_reserved5; }

variable_types: omode int

variable_types: fd int

user_defined_types: struct shmid_ds { struct ipc_perm shm_perm; unsigned long shm_segsz; long shm_atime; long shm_dtime; long shm_ctime; int shm_cpid; int shm_lpid; unsigned long shm_nattch; unsigned long __glibc_reserved4; unsigned long __glibc_reserved5; }


decompiled_code:
int __fastcall shared_memory_nattach(_DWORD *a1, _DWORD *a2)
{
int v2; // edi
int result; // eax
struct shmid_ds v4; // [rsp-58h] [rbp-78h] BYREF

if ( !a1 )
return -2147418111;
if ( *a1 != 1936223597 )
return -2147418111;
v2 = a1[12];
if ( v2 == -1 )
return -2147418111;
result = shmctl(v2, 2, &v4);
if ( result )
return -2147352567;
*a2 = v4.shm_nattch;
return result;
}

canonical_decompiled_code:
int __fastcall func3(_DWORD *a1, _DWORD *a2)
{
int v2; // edi
int result; // eax
struct shmid_ds v4; // [rsp-58h] [rbp-78h] BYREF

if ( !a1 )
return -2147418111;
if ( *a1 != 1936223597 )
return -2147418111;
v2 = a1[12];
if ( v2 == -1 )
return -2147418111;
result = shmctl(v2, 2, &v4);
if ( result )
return -2147352567;
*a2 = v4.shm_nattch;
return result;
}

original_code:
int shared_memory_nattach(struct shared_memory *shmem, int *attach_count_out)
{
    struct shmid_ds ds;
    if (!shmem || shmem->magic != 'shmm' || shmem->shmid == -1)
        return ((int)(0x80000000 | 0x00010000 | 0x0001));
    if (shmctl(shmem->shmid, 2, &ds) != 0)
        return ((int)(0x80000000 | 0x00020000 | 0x0009));
    *attach_count_out = ds.shm_nattch;
    return 0x00000000;
}


original_code:
int shared_memory_close(struct shared_memory *shmem, int *did_destroy)
{
    struct shmid_ds ds;
    int destroy_mode;
    int destroy = 0;
    char path[255];
    if (shmem == ((void *)0) || shmem->magic != 'shmm')
        return ((int)(0x80000000 | 0x00010000 | 0x0001));
    destroy_mode = shmem->flags & 0x0000030;
    if (shmem->ptr)
    {
        shmdt(shmem->ptr);
        shmem->ptr = ((void *)0);
    }
    if (shmem->shmid != -1)
    {
        if (destroy_mode == 0x0000020)
        {
            if (shmctl(shmem->shmid, 2, &ds) == 0)
            {
                if (ds.shm_nattch == 0)
                    destroy = 1;
            }
        }
        if (destroy_mode == 0x0000010)
            destroy = 1;
        if (destroy)
        {
            if (0) printf("Destroying shared memory %s 0x%08x (%d).\n", shmem->name, shmem->shmkey, shmem->shmid);
            shmctl(shmem->shmid, 0, ((void *)0));
            sprintf(path, "%s/%s", "/tmp", shmem->name);
            unlink(path);
            if (did_destroy != ((void *)0))
                *did_destroy = 1;
        }
        else
        {
            if (did_destroy != ((void *)0))
                *did_destroy = 0;
        }
        shmem->shmid = -1;
    }
    shmem->shmkey = -1;
    return 0x00000000;
}

variable_types: destroy int


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

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

original_code:
void shared_memory_free(struct shared_memory *shmem)
{
    free(shmem);
}


decompiled_code:
const char *__fastcall error_message(int a1)
{
const char *result; // rax

if ( a1 >= -2147352564 )
{
if ( a1 >= -2147287029 )
{
result = "ERROR_STATS_KEY_TOO_LONG";
if ( a1 != -2147221502 )
{
if ( a1 )
{
result = "UNKNOWN_ERROR";
if ( a1 == -2147221503 )
result = "ERROR_STATS_CANNOT_ALLOCATE_COUNTER";
}
else
{
result = "S_OK";
}
}
}
else if ( a1 < -2147287039 )
{
result = "UNKNOWN_ERROR";
}
else
{
switch ( a1 )
{
case -2147287039:
result = "ERROR_SEMAPHORE_NAME_TOO_LONG";
break;
case -2147287038:
result = "ERROR_SEMAPHORE_CANNOT_CREATE_DIRECTORY";
break;
case -2147287037:
result = "ERROR_SEMAPHORE_PATH_NOT_DIRECTORY";
break;
case -2147287036:
result = "ERROR_SEMAPHORE_CANNOT_CREATE_PATH";
break;
case -2147287035:
result = "ERROR_SEMAPHORE_CANNOT_CREATE_IPC_TOKEN";
break;
case -2147287034:
result = "ERROR_SEMAPHORE_ALREADY_EXISTS";
break;
case -2147287033:
result = "ERROR_SEMAPHORE_DOES_NOT_EXIST";
break;
case -2147287032:
result = "ERROR_SEMAPHORE_INVALID_SIZE";
break;
case -2147287030:
result = "ERROR_SEMAPHORE_CANNOT_OPEN";
break;
default:
result = "UNKNOWN_ERROR";
break;
}
}
}
else if ( a1 >= -2147352575 )
{
switch ( a1 )
{
case -2147352575:
result = "ERROR_SHARED_MEM_NAME_TOO_LONG";
break;
case -2147352574:
result = "ERROR_SHARED_MEM_CANNOT_CREATE_DIRECTORY";
break;
case -2147352573:
result = "ERROR_SHARED_MEM_PATH_NOT_DIRECTORY";
break;
case -2147352572:
result = "ERROR_SHARED_MEM_CANNOT_CREATE_PATH";
break;
case -2147352571:
result = "ERROR_SHARED_MEM_CANNOT_CREATE_IPC_TOKEN";
break;
case -2147352570:
result = "ERROR_SHARED_MEM_ALREADY_EXISTS";
break;
case -2147352569:
result = "ERROR_SHARED_MEM_DOES_NOT_EXIST";
break;
case -2147352568:
result = "ERROR_SHARED_MEM_INVALID_SIZE";
break;
case -2147352567:
result = "ERROR_SHARED_MEM_CANNOT_STAT";
break;
case -2147352566:
result = "ERROR_SHARED_MEM_CANNOT_OPEN";
break;
case -2147352565:
result = "ERROR_SHARED_MEM_CANNOT_ATTACH";
break;
default:
result = "UNKNOWN_ERROR";
break;
}
}
else
{
result = "ERROR_INVALID_PARAMETERS";
if ( a1 != -2147418111 )
{
if ( a1 == -2147418110 )
{
result = "ERROR_MEMORY";
}
else
{
result = "UNKNOWN_ERROR";
if ( a1 == -2147418112 )
result = "ERRROR_FAIL";
}
}
}
return result;
}

canonical_decompiled_code:
const char *__fastcall func0(int a1)
{
const char *result; // rax

if ( a1 >= -2147352564 )
{
if ( a1 >= -2147287029 )
{
result = "ERROR_STATS_KEY_TOO_LONG";
if ( a1 != -2147221502 )
{
if ( a1 )
{
result = "UNKNOWN_ERROR";
if ( a1 == -2147221503 )
result = "ERROR_STATS_CANNOT_ALLOCATE_COUNTER";
}
else
{
result = "S_OK";
}
}
}
else if ( a1 < -2147287039 )
{
result = "UNKNOWN_ERROR";
}
else
{
switch ( a1 )
{
case -2147287039:
result = "ERROR_SEMAPHORE_NAME_TOO_LONG";
break;
case -2147287038:
result = "ERROR_SEMAPHORE_CANNOT_CREATE_DIRECTORY";
break;
case -2147287037:
result = "ERROR_SEMAPHORE_PATH_NOT_DIRECTORY";
break;
case -2147287036:
result = "ERROR_SEMAPHORE_CANNOT_CREATE_PATH";
break;
case -2147287035:
result = "ERROR_SEMAPHORE_CANNOT_CREATE_IPC_TOKEN";
break;
case -2147287034:
result = "ERROR_SEMAPHORE_ALREADY_EXISTS";
break;
case -2147287033:
result = "ERROR_SEMAPHORE_DOES_NOT_EXIST";
break;
case -2147287032:
result = "ERROR_SEMAPHORE_INVALID_SIZE";
break;
case -2147287030:
result = "ERROR_SEMAPHORE_CANNOT_OPEN";
break;
default:
result = "UNKNOWN_ERROR";
break;
}
}
}
else if ( a1 >= -2147352575 )
{
switch ( a1 )
{
case -2147352575:
result = "ERROR_SHARED_MEM_NAME_TOO_LONG";
break;
case -2147352574:
result = "ERROR_SHARED_MEM_CANNOT_CREATE_DIRECTORY";
break;
case -2147352573:
result = "ERROR_SHARED_MEM_PATH_NOT_DIRECTORY";
break;
case -2147352572:
result = "ERROR_SHARED_MEM_CANNOT_CREATE_PATH";
break;
case -2147352571:
result = "ERROR_SHARED_MEM_CANNOT_CREATE_IPC_TOKEN";
break;
case -2147352570:
result = "ERROR_SHARED_MEM_ALREADY_EXISTS";
break;
case -2147352569:
result = "ERROR_SHARED_MEM_DOES_NOT_EXIST";
break;
case -2147352568:
result = "ERROR_SHARED_MEM_INVALID_SIZE";
break;
case -2147352567:
result = "ERROR_SHARED_MEM_CANNOT_STAT";
break;
case -2147352566:
result = "ERROR_SHARED_MEM_CANNOT_OPEN";
break;
case -2147352565:
result = "ERROR_SHARED_MEM_CANNOT_ATTACH";
break;
default:
result = "UNKNOWN_ERROR";
break;
}
}
else
{
result = "ERROR_INVALID_PARAMETERS";
if ( a1 != -2147418111 )
{
if ( a1 == -2147418110 )
{
result = "ERROR_MEMORY";
}
else
{
result = "UNKNOWN_ERROR";
if ( a1 == -2147418112 )
result = "ERRROR_FAIL";
}
}
}
return result;
}

original_code:
const char * error_message(int code)
{
    switch (code)
    {
    case S_OK:                                      return "S_OK";

    case ERROR_FAIL:                                return "ERRROR_FAIL";
    case ERROR_INVALID_PARAMETERS:                  return "ERROR_INVALID_PARAMETERS";
    case ERROR_MEMORY:                              return "ERROR_MEMORY";

    case ERROR_SHARED_MEM_NAME_TOO_LONG:            return "ERROR_SHARED_MEM_NAME_TOO_LONG";
    case ERROR_SHARED_MEM_CANNOT_CREATE_DIRECTORY:  return "ERROR_SHARED_MEM_CANNOT_CREATE_DIRECTORY";
    case ERROR_SHARED_MEM_PATH_NOT_DIRECTORY:       return "ERROR_SHARED_MEM_PATH_NOT_DIRECTORY";
    case ERROR_SHARED_MEM_CANNOT_CREATE_PATH:       return "ERROR_SHARED_MEM_CANNOT_CREATE_PATH";
    case ERROR_SHARED_MEM_CANNOT_CREATE_IPC_TOKEN:  return "ERROR_SHARED_MEM_CANNOT_CREATE_IPC_TOKEN";
    case ERROR_SHARED_MEM_ALREADY_EXISTS:           return "ERROR_SHARED_MEM_ALREADY_EXISTS";
    case ERROR_SHARED_MEM_DOES_NOT_EXIST:           return "ERROR_SHARED_MEM_DOES_NOT_EXIST";
    case ERROR_SHARED_MEM_INVALID_SIZE:             return "ERROR_SHARED_MEM_INVALID_SIZE";
    case ERROR_SHARED_MEM_CANNOT_STAT:              return "ERROR_SHARED_MEM_CANNOT_STAT";
    case ERROR_SHARED_MEM_CANNOT_OPEN:              return "ERROR_SHARED_MEM_CANNOT_OPEN";
    case ERROR_SHARED_MEM_CANNOT_ATTACH:            return "ERROR_SHARED_MEM_CANNOT_ATTACH";

    case ERROR_SEMAPHORE_NAME_TOO_LONG:             return "ERROR_SEMAPHORE_NAME_TOO_LONG";
    case ERROR_SEMAPHORE_CANNOT_CREATE_DIRECTORY:   return "ERROR_SEMAPHORE_CANNOT_CREATE_DIRECTORY";
    case ERROR_SEMAPHORE_PATH_NOT_DIRECTORY:        return "ERROR_SEMAPHORE_PATH_NOT_DIRECTORY";
    case ERROR_SEMAPHORE_CANNOT_CREATE_PATH:        return "ERROR_SEMAPHORE_CANNOT_CREATE_PATH";
    case ERROR_SEMAPHORE_CANNOT_CREATE_IPC_TOKEN:   return "ERROR_SEMAPHORE_CANNOT_CREATE_IPC_TOKEN";
    case ERROR_SEMAPHORE_ALREADY_EXISTS:            return "ERROR_SEMAPHORE_ALREADY_EXISTS";
    case ERROR_SEMAPHORE_DOES_NOT_EXIST:            return "ERROR_SEMAPHORE_DOES_NOT_EXIST";
    case ERROR_SEMAPHORE_INVALID_SIZE:              return "ERROR_SEMAPHORE_INVALID_SIZE";
    case ERROR_SEMAPHORE_CANNOT_OPEN:               return "ERROR_SEMAPHORE_CANNOT_OPEN";

    case ERROR_STATS_CANNOT_ALLOCATE_COUNTER:       return "ERROR_STATS_CANNOT_ALLOCATE_COUNTER";
    case ERROR_STATS_KEY_TOO_LONG:                  return "ERROR_STATS_KEY_TOO_LONG";

    }
    return "UNKNOWN_ERROR";
}

canonical_original_code:
char *error_message(int code)
{
    switch (code)
    {
    case S_OK:                                      return "S_OK";

    case ERROR_FAIL:                                return "ERRROR_FAIL";
    case ERROR_INVALID_PARAMETERS:                  return "ERROR_INVALID_PARAMETERS";
    case ERROR_MEMORY:                              return "ERROR_MEMORY";

    case ERROR_SHARED_MEM_NAME_TOO_LONG:            return "ERROR_SHARED_MEM_NAME_TOO_LONG";
    case ERROR_SHARED_MEM_CANNOT_CREATE_DIRECTORY:  return "ERROR_SHARED_MEM_CANNOT_CREATE_DIRECTORY";
    case ERROR_SHARED_MEM_PATH_NOT_DIRECTORY:       return "ERROR_SHARED_MEM_PATH_NOT_DIRECTORY";
    case ERROR_SHARED_MEM_CANNOT_CREATE_PATH:       return "ERROR_SHARED_MEM_CANNOT_CREATE_PATH";
    case ERROR_SHARED_MEM_CANNOT_CREATE_IPC_TOKEN:  return "ERROR_SHARED_MEM_CANNOT_CREATE_IPC_TOKEN";
    case ERROR_SHARED_MEM_ALREADY_EXISTS:           return "ERROR_SHARED_MEM_ALREADY_EXISTS";
    case ERROR_SHARED_MEM_DOES_NOT_EXIST:           return "ERROR_SHARED_MEM_DOES_NOT_EXIST";
    case ERROR_SHARED_MEM_INVALID_SIZE:             return "ERROR_SHARED_MEM_INVALID_SIZE";
    case ERROR_SHARED_MEM_CANNOT_STAT:              return "ERROR_SHARED_MEM_CANNOT_STAT";
    case ERROR_SHARED_MEM_CANNOT_OPEN:              return "ERROR_SHARED_MEM_CANNOT_OPEN";
    case ERROR_SHARED_MEM_CANNOT_ATTACH:            return "ERROR_SHARED_MEM_CANNOT_ATTACH";

    case ERROR_SEMAPHORE_NAME_TOO_LONG:             return "ERROR_SEMAPHORE_NAME_TOO_LONG";
    case ERROR_SEMAPHORE_CANNOT_CREATE_DIRECTORY:   return "ERROR_SEMAPHORE_CANNOT_CREATE_DIRECTORY";
    case ERROR_SEMAPHORE_PATH_NOT_DIRECTORY:        return "ERROR_SEMAPHORE_PATH_NOT_DIRECTORY";
    case ERROR_SEMAPHORE_CANNOT_CREATE_PATH:        return "ERROR_SEMAPHORE_CANNOT_CREATE_PATH";
    case ERROR_SEMAPHORE_CANNOT_CREATE_IPC_TOKEN:   return "ERROR_SEMAPHORE_CANNOT_CREATE_IPC_TOKEN";
    case ERROR_SEMAPHORE_ALREADY_EXISTS:            return "ERROR_SEMAPHORE_ALREADY_EXISTS";
    case ERROR_SEMAPHORE_DOES_NOT_EXIST:            return "ERROR_SEMAPHORE_DOES_NOT_EXIST";
    case ERROR_SEMAPHORE_INVALID_SIZE:              return "ERROR_SEMAPHORE_INVALID_SIZE";
    case ERROR_SEMAPHORE_CANNOT_OPEN:               return "ERROR_SEMAPHORE_CANNOT_OPEN";

    case ERROR_STATS_CANNOT_ALLOCATE_COUNTER:       return "ERROR_STATS_CANNOT_ALLOCATE_COUNTER";
    case ERROR_STATS_KEY_TOO_LONG:                  return "ERROR_STATS_KEY_TOO_LONG";

    }
    return "UNKNOWN_ERROR";
}

variable_types: code int


decompiled_code:
__int64 __fastcall send_message(int a1, __int16 a2, __int64 a3, int a4)
{
int v6; // ebx
__int64 result; // rax
_DWORD *v8; // rbp
int v9; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

v9 = 0;
*(_QWORD *)&addr.sa_data[6] = 0LL;
*(_DWORD *)&addr.sa_data[2] = a1;
*(_WORD *)addr.sa_data = _bswap_16(a2);
addr.sa_family = 2;
v6 = socket(2, 2, 0);
result = 1LL;
if ( v6 != -1 )
{
v8 = prepare_message(&v9, a3, a4);
if ( sendto(v6, v8, v9, 0, &addr, 0x10u) )
{
free(v8);
result = 0LL;
}
else
{
puts("sendto failed");
result = 2LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(int a1, __int16 a2, __int64 a3, int a4)
{
int v6; // ebx
__int64 result; // rax
_DWORD *v8; // rbp
int v9; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

v9 = 0;
*(_QWORD *)&addr.sa_data[6] = 0LL;
*(_DWORD *)&addr.sa_data[2] = a1;
*(_WORD *)addr.sa_data = func8(a2);
addr.sa_family = 2;
v6 = socket(2, 2, 0);
result = 1LL;
if ( v6 != -1 )
{
v8 = prepare_message(&v9, a3, a4);
if ( sendto(v6, v8, v9, 0, &addr, 0x10u) )
{
free(v8);
result = 0LL;
}
else
{
puts("sendto failed");
result = 2LL;
}
}
return result;
}

original_code:
int send_message(struct in_addr inaddr, int port, char *metric_name, int metric_value)
{
    struct sockaddr_in addr;
    char *buffer;
    int len = 0, s;
    memset(&addr, 0, sizeof(struct sockaddr_in));
    addr.sin_addr = inaddr;
    addr.sin_port = htons(port);
    addr.sin_family = 2;
    s = socket(2,SOCK_DGRAM,0);
    if (s == -1)
    {
        return 1;
    }
    buffer = prepare_message(&len, metric_name, metric_value);
    if (sendto(s, buffer, len, 0, (const struct sockaddr *)&addr, sizeof(struct sockaddr_in)) == 0)
    {
        printf("sendto failed\n");
        return 2;
    }
    free(buffer);
    return 0;
}

canonical_original_code:
int send_message(struct in_addr inaddr, int port, char *metric_name, int metric_value)
{
    struct sockaddr_in addr;
    char *buffer;
    int len = 0; int s;
    memset(&addr, 0, sizeof(struct sockaddr_in));
    addr.sin_addr = inaddr;
    addr.sin_port = htons(port);
    addr.sin_family = 2;
    s = socket(2,SOCK_DGRAM,0);
    if (s == -1)
    {
        return 1;
    }
    buffer = prepare_message(&len, metric_name, metric_value);
    if (sendto(s, buffer, len, 0, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) == 0)
    {
        printf("sendto failed\n");
        return 2;
    }
    free(buffer);
    return 0;
}

variable_types:
inaddr
struct in_addr { unsigned int s_addr; }

variable_types: port int

variable_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; }

variable_types:
buffer
char *

variable_types: len int

variable_types: s int

user_defined_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr sin_addr; unsigned char sin_zero[-1]; }

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


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
struct hostent *v5; // rax
struct hostent *v6; // r13
struct in_addr *v7; // rax
int v8; // er12
__int64 v9; // rbx
char *v10; // rax
int v11; // eax

if ( argc == 4 )
{
v5 = gethostbyname(argv[1]);
v6 = v5;
if ( v5 )
{
printf("sending to %s\n", v5->h_name);
v7 = (struct in_addr *)*v6->h_addr_list;
if ( v7 )
{
v8 = 0;
v9 = 0LL;
while ( 1 )
{
v10 = inet_ntoa((struct in_addr)v7->s_addr);
printf("trying %s\n", v10);
v11 = atoi(argv[3]);
v3 = send_message(*(_DWORD *)v6->h_addr_list[v9], 7010, (__int64)argv[2], v11);
if ( !v3 )
break;
v9 = (unsigned int)++v8;
v7 = (struct in_addr *)v6->h_addr_list[v9];
if ( !v7 )
return 0;
}
puts("ok");
}
else
{
v3 = 0;
}
}
else
{
puts("gethostbyname() failed");
v3 = 0;
}
}
else
{
puts("usage: histd_client addr metric_name metric_value");
v3 = 1;
}
return v3;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
struct hostent *v5; // rax
struct hostent *v6; // r13
struct in_addr *v7; // rax
int v8; // er12
__int64 v9; // rbx
char *v10; // rax
int v11; // eax

if ( argc == 4 )
{
v5 = gethostbyname(argv[1]);
v6 = v5;
if ( v5 )
{
printf("sending to %s\n", v5->h_name);
v7 = (struct in_addr *)*v6->h_addr_list;
if ( v7 )
{
v8 = 0;
v9 = 0LL;
while ( 1 )
{
v10 = inet_ntoa((struct in_addr)v7->s_addr);
printf("trying %s\n", v10);
v11 = atoi(argv[3]);
v3 = func11(*(_DWORD *)v6->h_addr_list[v9], 7010, (__int64)argv[2], v11);
if ( !v3 )
break;
v9 = (unsigned int)++v8;
v7 = (struct in_addr *)v6->h_addr_list[v9];
if ( !v7 )
return 0;
}
puts("ok");
}
else
{
v3 = 0;
}
}
else
{
puts("gethostbyname() failed");
v3 = 0;
}
}
else
{
puts("usage: histd_client addr metric_name metric_value");
v3 = 1;
}
return v3;
}

original_code:
int main(int argc, char **argv)
{
    struct hostent * h;
    unsigned int i;
    int port = 7010;
    if (argc != 4)
    {
        printf("usage: histd_client addr metric_name metric_value\n");
        return 1;
    }
    h = gethostbyname(argv[1]);
    if (h == ((void *)0))
    {
        printf("gethostbyname() failed\n");
    }
    else
    {
        printf("sending to %s\n", h->h_name);
        for (i = 0; h->h_addr_list[i] != ((void *)0); i++)
        {
            printf( "trying %s\n", inet_ntoa(*(struct in_addr *)(h->h_addr_list[i])));
            if (send_message(*(struct in_addr *)(h->h_addr_list[i]),port,argv[2],atoi(argv[3])) == 0)
            {
                printf("ok\n");
                break;
            }
        }
    }
    return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
    struct hostent *h;
    unsigned int i;
    int port = 7010;
    if (argc != 4)
    {
        printf("usage: histd_client addr metric_name metric_value\n");
        return 1;
    }
    h = gethostbyname(argv[1]);
    if (h == ((void *)0))
    {
        printf("gethostbyname() failed\n");
    }
    else
    {
        printf("sending to %s\n", h->h_name);
        for (i = 0; h->h_addr_list[i] != ((void *)0); i++)
        {
            printf( "trying %s\n", inet_ntoa(*(struct in_addr *)(h->h_addr_list[i])));
            if (send_message(*(struct in_addr *)(h->h_addr_list[i]),port,argv[2],atoi(argv[3])) == 0)
            {
                printf("ok\n");
                break;
            }
        }
    }
    return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; } *

variable_types:
i
unsigned int


decompiled_code:
size_t __fastcall json_encode_variable_name(char *dest, char *src)
{
size_t v2; // rax
size_t result; // rax

*(_WORD *)&dest[strlen(dest)] = 34;
v2 = strlen(src);
strncat(dest, src, v2);
result = strlen(dest);
strcpy(&dest[result], "\":");
return result;
}

canonical_decompiled_code:
size_t __fastcall func0(char *dest, char *src)
{
size_t v2; // rax
size_t result; // rax

*(_WORD *)&dest[strlen(dest)] = 34;
v2 = strlen(src);
strncat(dest, src, v2);
result = strlen(dest);
strcpy(&dest[result], "\":");
return result;
}

original_code:
static void json_encode_variable_name(char *sStr, char *sName)
{
    strncat(sStr, "\"", 1);
    strncat(sStr, sName, strlen(sName));
    strncat(sStr, "\":", 2);
}

variable_types:
sStr
char *

variable_types:
sName
char *


decompiled_code:
char *__fastcall json_encode_string(char *dest, char *a2, const char *a3)
{
size_t v4; // rax

json_encode_variable_name(dest, a2);
v4 = strlen(a3);
return strncat(dest, a3, v4);
}

canonical_decompiled_code:
char *__fastcall func2(char *dest, char *a2, const char *a3)
{
size_t v4; // rax

func0(dest, a2);
v4 = strlen(a3);
return strncat(dest, a3, v4);
}

original_code:
void json_encode_string(char *str, char *name, char *value)
{
    json_encode_variable_name(str, name);
    strncat(str, value, strlen(value));
}

variable_types:
str
char *

variable_types:
name
char *

variable_types:
value
char *


decompiled_code:
char *__fastcall json_encode_integer(char *dest, char *a2, unsigned int a3)
{
size_t v4; // rax
char v6[136]; // [rsp+0h] [rbp-88h] BYREF

json_encode_variable_name(dest, a2);
sprintf(v6, "%d", a3);
v4 = strlen(v6);
return strncat(dest, v6, v4);
}

canonical_decompiled_code:
char *__fastcall func3(char *dest, char *a2, unsigned int a3)
{
size_t v4; // rax
char v6[136]; // [rsp+0h] [rbp-88h] BYREF

func0(dest, a2);
sprintf(v6, "%d", a3);
v4 = strlen(v6);
return strncat(dest, v6, v4);
}

original_code:
void json_encode_integer(char *str, char *name, int value)
{
    char int_str[(100)];
    json_encode_variable_name(str, name);
    sprintf(int_str, "%d", value);
    strncat(str, int_str, strlen(int_str));
}

canonical_original_code:
void json_encode_integer(char *str, char *name, int value)
{
    char int_str[-1];
    json_encode_variable_name(str, name);
    sprintf(int_str, "%d", value);
    strncat(str, int_str, strlen(int_str));
}

variable_types: value int


decompiled_code:
char *__fastcall json_encode_real32(char *dest, char *a2, float a3)
{
size_t v3; // rax
char s[120]; // [rsp+10h] [rbp-78h] BYREF

json_encode_variable_name(dest, a2);
sprintf(s, "%.1f", a3);
v3 = strlen(s);
return strncat(dest, s, v3);
}

canonical_decompiled_code:
char *__fastcall func4(char *dest, char *a2, float a3)
{
size_t v3; // rax
char s[120]; // [rsp+10h] [rbp-78h] BYREF

func0(dest, a2);
sprintf(s, "%.1f", a3);
v3 = strlen(s);
return strncat(dest, s, v3);
}

original_code:
void json_encode_real32(char *str, char *name, real32 value)
{
    char real32_str[(100)];
    json_encode_variable_name(str, name);
    sprintf(real32_str, "%.1f", value);
    strncat(str, real32_str, strlen(real32_str));
}

canonical_original_code:
void json_encode_real32(char *str, char *name, float value)
{
    char real32_str[-1];
    json_encode_variable_name(str, name);
    sprintf(real32_str, "%.1f", value);
    strncat(str, real32_str, strlen(real32_str));
}

variable_types: value float


decompiled_code:
size_t __fastcall json_encode_object(char *dest, char *a2, const char *a3)
{
size_t v4; // rax
size_t result; // rax

json_encode_variable_name(dest, a2);
*(_WORD *)&dest[strlen(dest)] = 123;
v4 = strlen(a3);
strncat(dest, a3, v4);
result = strlen(dest);
*(_WORD *)&dest[result] = 125;
return result;
}

canonical_decompiled_code:
size_t __fastcall func5(char *dest, char *a2, const char *a3)
{
size_t v4; // rax
size_t result; // rax

func0(dest, a2);
*(_WORD *)&dest[strlen(dest)] = 123;
v4 = strlen(a3);
strncat(dest, a3, v4);
result = strlen(dest);
*(_WORD *)&dest[result] = 125;
return result;
}

original_code:
void json_encode_object(char *str, char *name, char *value)
{
    json_encode_variable_name(str, name);
    strncat(str, "{", 1);
    strncat(str, value, strlen(value));
    strncat(str, "}", 1);
}


decompiled_code:
__int64 __fastcall json_decode_variable_name(__int64 a1, char *a2)
{
char *v2; // rax
__int64 result; // rax

v2 = strtok((char *)(a1 + 1), "\"");
if ( v2 )
return (__int64)strcpy(a2, v2);
result = 0x746F6E5F656D616ELL;
strcpy(a2, "name_not_found");
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, char *a2)
{
char *v2; // rax
__int64 result; // rax

v2 = strtok((char *)(a1 + 1), "\"");
if ( v2 )
return (__int64)strcpy(a2, v2);
result = 0x746F6E5F656D616ELL;
strcpy(a2, "name_not_found");
return result;
}

original_code:
void json_decode_variable_name(char *str, char *name)
{
    char *sub_str;
    str = &str[1];
    sub_str = strtok(str, "\"");
    if (sub_str != ((void *)0))
    {
        strcpy(name, sub_str);
    }
    else
    {
        strcpy(name, "name_not_found");
    }
}


decompiled_code:
size_t __fastcall u32_json_decode_message(__int64 a1, const char *a2)
{
size_t v3; // rax
bool v5; // cf
bool v6; // zf
const char *v7; // rdi
__int64 v8; // rcx
char *v9; // rsi
char v10; // al
bool v11; // cf
bool v12; // zf
char *v13; // rsi
const char *v14; // rdi
__int64 v15; // rcx
char v16; // al
bool v17; // cf
bool v18; // zf
char *v19; // rsi
const char *v20; // rdi
__int64 v21; // rcx
_BYTE v22[112]; // [rsp+0h] [rbp-978h] BYREF
char v23[112]; // [rsp+70h] [rbp-908h] BYREF
char v24[112]; // [rsp+E0h] [rbp-898h] BYREF
char v25[2052]; // [rsp+150h] [rbp-828h] BYREF
char v26[36]; // [rsp+954h] [rbp-24h] BYREF

jsmn_init(v26);
v3 = strlen(a2);
jsmn_parse(v26, a2, v3, v25, 128LL);
get_json_token_str(2, (__int64)a2, (__int64)v25, v24);
get_json_token_str(3, (__int64)a2, (__int64)v25, v24);
if ( !strcmp(v24, "get") )
{
get_json_token_str(4, (__int64)a2, (__int64)v25, v24);
v7 = "digit_vars";
v8 = 11LL;
v9 = v24;
do
{
if ( !v8 )
break;
v5 = (unsigned __int8)*v9 < (unsigned int)*v7;
v6 = *v9++ == *v7++;
--v8;
}
while ( v6 );
v10 = (!v5 && !v6) - v5;
v11 = 0;
v12 = v10 == 0;
if ( v10 )
{
v13 = v24;
v14 = "control_vars";
v15 = 13LL;
do
{
if ( !v15 )
break;
v11 = (unsigned __int8)*v13 < (unsigned int)*v14;
v12 = *v13++ == *v14++;
--v15;
}
while ( v12 );
v16 = (!v11 && !v12) - v11;
v17 = 0;
v18 = v16 == 0;
if ( v16 )
{
v19 = v24;
v20 = "ds18b20_vars";
v21 = 13LL;
do
{
if ( !v21 )
break;
v17 = (unsigned __int8)*v19 < (unsigned int)*v20;
v18 = *v19++ == *v20++;
--v21;
}
while ( v18 );
if ( (!v17 && !v18) == v17 )
DS18B20_json_encode_vars(a2, v19);
}
else
{
ctrl_json_encode(a2, v13);
}
}
else
{
digit_json_encode_vars(a2, v9);
}
}
else if ( !strcmp(v24, "set") )
{
get_json_token_str(5, (__int64)a2, (__int64)v25, v24);
if ( !strcmp(v24, "digit_var") )
{
get_json_token_str(7, (__int64)a2, (__int64)v25, v22);
get_json_token_str(8, (__int64)a2, (__int64)v25, v23);
digit_set_var_by_name(v22, v23, a2);
}
else if ( !strcmp(v24, "control_var") )
{
get_json_token_str(7, (__int64)a2, (__int64)v25, v22);
get_json_token_str(8, (__int64)a2, (__int64)v25, v23);
ctrl_set_var_by_name(v22, v23, a2);
}
}
return strlen(a2);
}

original_code:
uint32 u32_json_decode_message(uint32 u32MsgLen, char *sMesg)
{
    int id;
    jsmn_parser parser;
    jsmntok_t tokens[(128)];
    char tokenStr[(100)];
    jsmn_init(&parser);
    jsmn_parse(&parser, sMesg, strlen(sMesg), tokens, (128));
    get_json_token_str(2, sMesg, tokens, tokenStr);
    id = atoi(tokenStr);
    get_json_token_str(3, sMesg, tokens, tokenStr);
    if (!strcmp(tokenStr, "get"))
    {
        get_json_token_str(4, sMesg, tokens, tokenStr);
        if (!strcmp(tokenStr, "digit_vars"))
        {
            digit_json_encode_vars(sMesg);
        }
        else if (!strcmp(tokenStr, "control_vars"))
        {
            ctrl_json_encode(sMesg);
        }
        else if (!strcmp(tokenStr, "ds18b20_vars"))
        {
            DS18B20_json_encode_vars(sMesg);
        }
    }
    else if (!strcmp(tokenStr, "set"))
    {
        get_json_token_str(5, sMesg, tokens, tokenStr);
        if (!strcmp(tokenStr, "digit_var"))
        {
            char sName[(100)], sValue[(100)];
            get_json_token_str(7, sMesg, tokens, sName);
            get_json_token_str(8, sMesg, tokens, sValue);
            digit_set_var_by_name(sName, sValue, sMesg);
        }
        else if (!strcmp(tokenStr, "control_var"))
        {
            char sName[(100)], sValue[(100)];
            get_json_token_str(7, sMesg, tokens, sName);
            get_json_token_str(8, sMesg, tokens, sValue);
            ctrl_set_var_by_name(sName, sValue, sMesg);
        }
    }
    else
    {
    }
    return strlen(sMesg);
}

canonical_original_code:
unsigned int u32_json_decode_message(unsigned int u32MsgLen, char *sMesg)
{
    int id;
    struct jsmn_parser parser;
    struct jsmntok_t tokens[-1];
    char tokenStr[-1];
    jsmn_init(&parser);
    jsmn_parse(&parser, sMesg, strlen(sMesg), tokens, (128));
    get_json_token_str(2, sMesg, tokens, tokenStr);
    id = atoi(tokenStr);
    get_json_token_str(3, sMesg, tokens, tokenStr);
    if (!strcmp(tokenStr, "get"))
    {
        get_json_token_str(4, sMesg, tokens, tokenStr);
        if (!strcmp(tokenStr, "digit_vars"))
        {
            digit_json_encode_vars(sMesg);
        }
        else if (!strcmp(tokenStr, "control_vars"))
        {
            ctrl_json_encode(sMesg);
        }
        else if (!strcmp(tokenStr, "ds18b20_vars"))
        {
            DS18B20_json_encode_vars(sMesg);
        }
    }
    else if (!strcmp(tokenStr, "set"))
    {
        get_json_token_str(5, sMesg, tokens, tokenStr);
        if (!strcmp(tokenStr, "digit_var"))
        {
            char sName[-1]; char sValue[-1];
            get_json_token_str(7, sMesg, tokens, sName);
            get_json_token_str(8, sMesg, tokens, sValue);
            digit_set_var_by_name(sName, sValue, sMesg);
        }
        else if (!strcmp(tokenStr, "control_var"))
        {
            char sName[-1]; char sValue[-1];
            get_json_token_str(7, sMesg, tokens, sName);
            get_json_token_str(8, sMesg, tokens, sValue);
            ctrl_set_var_by_name(sName, sValue, sMesg);
        }
    }
    else
    {
    }
    return strlen(sMesg);
}

variable_types:
sMesg
char *

variable_types: id int

variable_types: struct jsmn_parser { unsigned int pos; unsigned int toknext; int toksuper; }

variable_types: struct jsmntok_t { enum jsmntype_t { JSMN_PRIMITIVE=0, JSMN_OBJECT=1, JSMN_ARRAY=2, JSMN_STRING=3 } type; int start; int end; int size; } [-1]

variable_types:
tokenStr
char [-1]

variable_types:
sName
char [-1]

variable_types:
sValue
char [-1]

return_type: unsigned int

user_defined_types: struct jsmntok_t { enum jsmntype_t type; int start; int end; int size; }


decompiled_code:
int pre_heating_resistor_init()
{
relay_control_init(17LL);
return read_on_time_from_file();
}

canonical_decompiled_code:
int func2()
{
relay_control_init(17LL);
return func0();
}

original_code:
void pre_heating_resistor_init(void)
{
    relay_control_init(17);
    read_on_time_from_file();
}

canonical_original_code:
void pre_heating_resistor_init()
{
    relay_control_init(17);
    read_on_time_from_file();
}