decompiled_code:
int __fastcall write_packet(char *a1, __int64 a2)
{
int v2; // edx
char v3; // al
__int64 v4; // rdx
const void *v5; // rax
const void *v6; // rax
const void *v7; // rax
const void *v8; // rax
int result; // eax
int v10; // eax

v2 = a2;
v3 = *a1;
if ( !seq_header_written_9 && (_QWORD)xmmword_AD0 && v3 == 9 )
{
v10 = *((_DWORD *)a1 + 2);
seq_header_written_9 = 1;
dword_AC8 = v10;
write_packet(&seq_header_pkt, a2, a2);
v3 = *a1;
v2 = a2;
}
v4 = (unsigned int)(*((_DWORD *)a1 + 2) + v2);
*((_DWORD *)a1 + 2) = v4;
if ( v3 == 8 && (unsigned int)v4 <= last_audio_timestamp )
{
result = quiet;
if ( !quiet )
result = fprintf(
stderr,
"Dropping overlapping audio packet with timestamp %d; last audio packet at %d\n",
v4,
last_audio_timestamp);
}
else
{
write_output(a1, 1uLL);
v5 = (const void *)format_ui24(*((unsigned int *)a1 + 1));
write_output(v5, 3uLL);
v6 = (const void *)format_ui24(*((unsigned int *)a1 + 2));
write_output(v6, 4uLL);
v7 = (const void *)format_ui24(*((unsigned int *)a1 + 3));
write_output(v7, 3uLL);
write_output(*((const void **)a1 + 2), *((unsigned int *)a1 + 1));
v8 = (const void *)format_ui32(*((unsigned int *)a1 + 6));
write_output(v8, 4uLL);
if ( *a1 == 9 )
{
last_video_timestamp = *((_DWORD *)a1 + 2);
}
else if ( *a1 == 8 )
{
last_audio_timestamp = *((unsigned int *)a1 + 2);
}
result = *((_DWORD *)a1 + 1);
last_packet_size = result;
}
return result;
}

canonical_decompiled_code:
int __fastcall func2(char *a1, __int64 a2)
{
int v2; // edx
char v3; // al
__int64 v4; // rdx
const void *v5; // rax
const void *v6; // rax
const void *v7; // rax
const void *v8; // rax
int result; // eax
int v10; // eax

v2 = a2;
v3 = *a1;
if ( !seq_header_written_9 && (_QWORD)xmmword_AD0 && v3 == 9 )
{
v10 = *((_DWORD *)a1 + 2);
seq_header_written_9 = 1;
dword_AC8 = v10;
func2(&seq_header_pkt, a2, a2);
v3 = *a1;
v2 = a2;
}
v4 = (unsigned int)(*((_DWORD *)a1 + 2) + v2);
*((_DWORD *)a1 + 2) = v4;
if ( v3 == 8 && (unsigned int)v4 <= last_audio_timestamp )
{
result = quiet;
if ( !quiet )
result = fprintf(
stderr,
"Dropping overlapping audio packet with timestamp %d; last audio packet at %d\n",
v4,
last_audio_timestamp);
}
else
{
func1(a1, 1uLL);
v5 = (const void *)format_ui24(*((unsigned int *)a1 + 1));
func1(v5, 3uLL);
v6 = (const void *)format_ui24(*((unsigned int *)a1 + 2));
func1(v6, 4uLL);
v7 = (const void *)format_ui24(*((unsigned int *)a1 + 3));
func1(v7, 3uLL);
func1(*((const void **)a1 + 2), *((unsigned int *)a1 + 1));
v8 = (const void *)format_ui32(*((unsigned int *)a1 + 6));
func1(v8, 4uLL);
if ( *a1 == 9 )
{
last_video_timestamp = *((_DWORD *)a1 + 2);
}
else if ( *a1 == 8 )
{
last_audio_timestamp = *((unsigned int *)a1 + 2);
}
result = *((_DWORD *)a1 + 1);
last_packet_size = result;
}
return result;
}

original_code:
static void write_packet(struct FLVpacket *packet, long file_start_timestamp)
{
    static char seq_header_written;
    if(!seq_header_written && seq_header_pkt.data && packet->type == 9)
    {
        seq_header_written = 1;
        seq_header_pkt.timestamp = packet->timestamp;
        write_packet(&seq_header_pkt, file_start_timestamp);
    }
    packet->timestamp += file_start_timestamp;
    if( packet->type == 8 && (long)packet->timestamp <= last_audio_timestamp )
    {
        if(!quiet)
            fprintf(stderr, "Dropping overlapping audio packet with timestamp %d; last audio packet at %d\n",
                    packet->timestamp, (unsigned int)last_audio_timestamp);
        return;
    }
    write_output(&packet->type, 1);
    write_output(format_ui24(packet->datasize), 3);
    write_output(format_ui24(packet->timestamp), 4);
    write_output(format_ui24(packet->streamid), 3);
    write_output(packet->data, packet->datasize);
    write_output(format_ui32(packet->backptr), 4);
    if( packet->type == 9 )
        last_video_timestamp = packet->timestamp;
    if( packet->type == 8 )
        last_audio_timestamp = packet->timestamp;
    last_packet_size = packet->datasize;
    return;
}

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


repo: pdkelly/flvjoin

decompiled_code:
void close_output()
{
int *v0; // rax
char *v1; // rax

if ( outfile != stdout )
{
if ( fclose(outfile) )
{
v0 = _errno_location();
v1 = strerror(*v0);
fprintf(stderr, "ERROR while closing output file %s: %s\n", filepath, v1);
exit(1);
}
}
}

canonical_decompiled_code:
void func3()
{
int *v0; // rax
char *v1; // rax

if ( outfile != stdout )
{
if ( fclose(outfile) )
{
v0 = _errno_location();
v1 = strerror(*v0);
fprintf(stderr, "ERROR while closing output file %s: %s\n", filepath, v1);
exit(1);
}
}
}

original_code:
static void close_output(void)
{
    if( outfile != stdout && fclose(outfile) != 0 )
    {
        fprintf(stderr, "ERROR while closing output file %s: %s\n",
                filepath, strerror((*__errno_location ())));
        exit(1);
    }
    return;
}

canonical_original_code:
static void close_output()
{
    if( outfile != stdout && fclose(outfile) != 0 )
    {
        fprintf(stderr, "ERROR while closing output file %s: %s\n",
                filepath, strerror((*__errno_location ())));
        exit(1);
    }
    return;
}

return_type: void


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er12
int v4; // eax
struct stat *v5; // rsi
struct _IO_FILE *v6; // rdx
char *v7; // rax
char *v8; // rax
int v9; // eax
__int64 v10; // rsi
double v12; // [rsp+0h] [rbp-C28h] BYREF
double v13; // [rsp+8h] [rbp-C20h] BYREF
char filename[1024]; // [rsp+10h] [rbp-C18h] BYREF
struct stat s[14]; // [rsp+410h] [rbp-818h] BYREF

v3 = 32000;
filepath[0] = 0;
while ( 2 )
{
v4 = getopt(argc, (char *const *)argv, "o:f:b:ndqh");
if ( v4 != -1 )
{
switch ( v4 )
{
case 'b':
v3 = atoi(optarg);
continue;
case 'd':
goto LABEL_5;
case 'f':
frame_interval = (int)(1000.0 / atof(optarg) + 0.5);
continue;
case 'n':
no_meta = 1;
continue;
case 'o':
strncpy(filepath, optarg, 0x400uLL);
continue;
case 'q':
quiet = 1;
continue;
default:
fprintf(stderr, "%s v%s\n", "flvjoin", "0.92");
fwrite(
"\nSynopsis: Reads a list of FLV files (with optional in-point and out-point)\n",
1uLL,
0x4CuLL,
stderr);
fwrite("from standard input and joins them together into one larger FLV file.\n\n", 1uLL, 0x47uLL, stderr);
fprintf(stderr, "Usage: %s -o <filename> [-f <framerate>] [-b <bitrate>] [-n] [-q] [-h]\n\n", "flvjoin");
fwrite(" -o <filename> Output File (- for stdout)\n", 1uLL, 0x2EuLL, stderr);
fprintf(
stderr,
" -f <framerate> Video frame rate in frames per second (default %.2f)\n",
1000.0 / (double)frame_interval);
fprintf(stderr, " -b <bitrate> Audio bitrate in bits per second (default %d)\n", (unsigned int)v3);
fwrite(" -n Don't write metadata to output file\n", 1uLL, 0x37uLL, stderr);
fwrite(" -q Don't display progress information\n", 1uLL, 0x36uLL, stderr);
fwrite(" -h Display this usage message and exit\n", 1uLL, 0x37uLL, stderr);
fputc(10, stderr);
LABEL_5:
exit(0);
}
}
break;
}
if ( filepath[0] )
{
v5 = (struct stat *)"-";
if ( strcmp(filepath, "-") )
{
v5 = s;
if ( !stat(filepath, s) )
{
fprintf(stderr, "ERROR: File %s exists; won't write header.\n", filepath);
exit(1);
}
}
open_output_constprop_0((__int64)filepath, (__int64)v5, v6);
write_flv_header();
if ( !no_meta )
{
v7 = (char *)generate_metadata_packet(outfile);
write_packet(v7, 0LL);
}
while ( fgets((char *)s, 2048, stdin) )
{
v8 = strchr((const char *)s, 10);
if ( v8 )
*v8 = 0;
v9 = _isoc99_sscanf(s, "%1023s %lf %lf", filename, &v12, &v13);
if ( v9 <= 2 )
{
v13 = *(double *)&LC37;
if ( v9 != 2 )
v12 = 0.0;
}
append_file(filename, (int)(1000.0 * v12 + 0.5), (int)(v13 * 1000.0 + 0.5));
}
if ( !no_meta )
{
if ( !quiet )
fwrite("Writing metadata...\n", 1uLL, 0x14uLL, stderr);
if ( (unsigned int)last_video_timestamp < last_audio_timestamp )
v10 = (unsigned int)(last_audio_timestamp + (int)((double)last_packet_size * 1000.0 * 8.0 / (double)v3 + 0.5));
else
v10 = (unsigned int)(frame_interval + last_video_timestamp);
write_metadata(outfile, v10);
}
if ( !quiet )
fprintf(stderr, "Closing output file %s\n", filepath);
close_output();
exit(0);
}
fwrite("ERROR: Output file must be specified with the -o option. (Use - for stdout).\n", 1uLL, 0x4DuLL, stderr);
exit(1);
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er12
int v4; // eax
struct stat *v5; // rsi
struct _IO_FILE *v6; // rdx
char *v7; // rax
char *v8; // rax
int v9; // eax
__int64 v10; // rsi
double v12; // [rsp+0h] [rbp-C28h] BYREF
double v13; // [rsp+8h] [rbp-C20h] BYREF
char filename[1024]; // [rsp+10h] [rbp-C18h] BYREF
struct stat s[14]; // [rsp+410h] [rbp-818h] BYREF

v3 = 32000;
filepath[0] = 0;
while ( 2 )
{
v4 = getopt(argc, (char *const *)argv, "o:f:b:ndqh");
if ( v4 != -1 )
{
switch ( v4 )
{
case 'b':
v3 = atoi(optarg);
continue;
case 'd':
goto LABEL_5;
case 'f':
frame_interval = (int)(1000.0 / atof(optarg) + 0.5);
continue;
case 'n':
no_meta = 1;
continue;
case 'o':
strncpy(filepath, optarg, 0x400uLL);
continue;
case 'q':
quiet = 1;
continue;
default:
fprintf(stderr, "%s v%s\n", "flvjoin", "0.92");
fwrite(
"\nSynopsis: Reads a list of FLV files (with optional in-point and out-point)\n",
1uLL,
0x4CuLL,
stderr);
fwrite("from standard input and joins them together into one larger FLV file.\n\n", 1uLL, 0x47uLL, stderr);
fprintf(stderr, "Usage: %s -o <filename> [-f <framerate>] [-b <bitrate>] [-n] [-q] [-h]\n\n", "flvjoin");
fwrite(" -o <filename> Output File (- for stdout)\n", 1uLL, 0x2EuLL, stderr);
fprintf(
stderr,
" -f <framerate> Video frame rate in frames per second (default %.2f)\n",
1000.0 / (double)frame_interval);
fprintf(stderr, " -b <bitrate> Audio bitrate in bits per second (default %d)\n", (unsigned int)v3);
fwrite(" -n Don't write metadata to output file\n", 1uLL, 0x37uLL, stderr);
fwrite(" -q Don't display progress information\n", 1uLL, 0x36uLL, stderr);
fwrite(" -h Display this usage message and exit\n", 1uLL, 0x37uLL, stderr);
fputc(10, stderr);
LABEL_5:
exit(0);
}
}
break;
}
if ( filepath[0] )
{
v5 = (struct stat *)"-";
if ( strcmp(filepath, "-") )
{
v5 = s;
if ( !stat(filepath, s) )
{
fprintf(stderr, "ERROR: File %s exists; won't write header.\n", filepath);
exit(1);
}
}
func4((__int64)filepath, (__int64)v5, v6);
func0();
if ( !no_meta )
{
v7 = (char *)generate_metadata_packet(outfile);
func2(v7, 0LL);
}
while ( fgets((char *)s, 2048, stdin) )
{
v8 = strchr((const char *)s, 10);
if ( v8 )
*v8 = 0;
v9 = _isoc99_sscanf(s, "%1023s %lf %lf", filename, &v12, &v13);
if ( v9 <= 2 )
{
v13 = *(double *)&LC37;
if ( v9 != 2 )
v12 = 0.0;
}
func6(filename, (int)(1000.0 * v12 + 0.5), (int)(v13 * 1000.0 + 0.5));
}
if ( !no_meta )
{
if ( !quiet )
fwrite("Writing metadata...\n", 1uLL, 0x14uLL, stderr);
if ( (unsigned int)last_video_timestamp < last_audio_timestamp )
v10 = (unsigned int)(last_audio_timestamp + (int)((double)last_packet_size * 1000.0 * 8.0 / (double)v3 + 0.5));
else
v10 = (unsigned int)(frame_interval + last_video_timestamp);
write_metadata(outfile, v10);
}
if ( !quiet )
fprintf(stderr, "Closing output file %s\n", filepath);
func3();
exit(0);
}
fwrite("ERROR: Output file must be specified with the -o option. (Use - for stdout).\n", 1uLL, 0x4DuLL, stderr);
exit(1);
}

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

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


original_code:
double conv_double(const unsigned char *ptr)
{
   if( (((unsigned char *) (&byte_order_test))[0] == 0) )
       return *(double *)ptr;
   else
   {
       int i = 0, j = sizeof(double);
       unsigned char doublebuff[sizeof(double)];
       while( --j >= 0 )
           doublebuff[j] = ptr[i++];
       return *(double *)doublebuff;
   }
}

canonical_original_code:
double conv_double(unsigned char *ptr)
{
   if( (((unsigned char *) (&byte_order_test))[0] == 0) )
       return *(double *)ptr;
   else
   {
       int i = 0; int j = sizeof(double);
       unsigned char doublebuff[-1];
       while( --j >= 0 )
           doublebuff[j] = ptr[i++];
       return *(double *)doublebuff;
   }
}

variable_types:
ptr
unsigned char *

variable_types: i int

variable_types: j int

variable_types:
doublebuff
unsigned char [-1]

return_type: double


decompiled_code:
__int64 __fastcall conv_ui32(unsigned int *a1)
{
return _byteswap_ulong(*a1);
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int *a1)
{
return _byteswap_ulong(*a1);
}

original_code:
unsigned int conv_ui32(const unsigned char *ptr)
{
   if( (((unsigned char *) (&byte_order_test))[0] == 0) )
       return *(unsigned int *)ptr;
   else
   {
       int i = 0, j = 4;
       unsigned char intbuff[4];
       while( --j >= 0 )
           intbuff[j] = ptr[i++];
       return *(unsigned int *)intbuff;
   }
}

canonical_original_code:
unsigned int conv_ui32(unsigned char *ptr)
{
   if( (((unsigned char *) (&byte_order_test))[0] == 0) )
       return *(unsigned int *)ptr;
   else
   {
       int i = 0; int j = 4;
       unsigned char intbuff[4];
       while( --j >= 0 )
           intbuff[j] = ptr[i++];
       return *(unsigned int *)intbuff;
   }
}

variable_types:
intbuff
unsigned char [4]

return_type: unsigned int


original_code:
unsigned int conv_ui24(const unsigned char *ptr, unsigned char highbyte)
{
   unsigned char intbuff[4];
   int i = 0, j;
   if( (((unsigned char *) (&byte_order_test))[0] == 0) )
   {
       intbuff[0] = highbyte;
       memcpy(&intbuff[1], ptr, 3);
   }
   else
   {
       intbuff[3] = highbyte;
       for( j = 2; j >= 0; j-- )
           intbuff[j] = ptr[i++];
   }
   return *(unsigned int *)intbuff;
}

canonical_original_code:
unsigned int conv_ui24(unsigned char *ptr, unsigned char highbyte)
{
   unsigned char intbuff[4];
   int i = 0; int j;
   if( (((unsigned char *) (&byte_order_test))[0] == 0) )
   {
       intbuff[0] = highbyte;
       memcpy(&intbuff[1], ptr, 3);
   }
   else
   {
       intbuff[3] = highbyte;
       for( j = 2; j >= 0; j-- )
           intbuff[j] = ptr[i++];
   }
   return *(unsigned int *)intbuff;
}

variable_types:
highbyte
unsigned char


decompiled_code:
__int16 __fastcall conv_ui16(_WORD *a1)
{
return __ROL2__(*a1, 8);
}

canonical_decompiled_code:
__int16 __fastcall func3(_WORD *a1)
{
return __ROL2__(*a1, 8);
}

original_code:
unsigned short conv_ui16(const unsigned char *ptr)
{
   if( (((unsigned char *) (&byte_order_test))[0] == 0) )
       return *(unsigned short *)ptr;
   else
   {
       unsigned char intbuff[2];
       intbuff[0] = ptr[1];
       intbuff[1] = ptr[0];
       return *(unsigned short *)intbuff;
   }
}

canonical_original_code:
unsigned short conv_ui16(unsigned char *ptr)
{
   if( (((unsigned char *) (&byte_order_test))[0] == 0) )
       return *(unsigned short *)ptr;
   else
   {
       unsigned char intbuff[2];
       intbuff[0] = ptr[1];
       intbuff[1] = ptr[0];
       return *(unsigned short *)intbuff;
   }
}

variable_types:
intbuff
unsigned char [2]

return_type: unsigned short


decompiled_code:
__int16 __fastcall conv_si16(_WORD *a1)
{
return __ROL2__(*a1, 8);
}

canonical_decompiled_code:
__int16 __fastcall func4(_WORD *a1)
{
return __ROL2__(*a1, 8);
}

original_code:
short conv_si16(const unsigned char *ptr)
{
   if( (((unsigned char *) (&byte_order_test))[0] == 0) )
       return *(short *)ptr;
   else
   {
       unsigned char intbuff[2];
       intbuff[0] = ptr[1];
       intbuff[1] = ptr[0];
       return *(short *)intbuff;
   }
}

canonical_original_code:
short conv_si16(unsigned char *ptr)
{
   if( (((unsigned char *) (&byte_order_test))[0] == 0) )
       return *(short *)ptr;
   else
   {
       unsigned char intbuff[2];
       intbuff[0] = ptr[1];
       intbuff[1] = ptr[0];
       return *(short *)intbuff;
   }
}

return_type: short


decompiled_code:
int *__fastcall format_ui32(unsigned int a1)
{
int *result; // rax

result = &outbuff_2;
outbuff_2 = _byteswap_ulong(a1);
return result;
}

canonical_decompiled_code:
int *__fastcall func6(unsigned int a1)
{
int *result; // rax

result = &outbuff_2;
outbuff_2 = _byteswap_ulong(a1);
return result;
}

original_code:
unsigned char *format_ui32(unsigned int number)
{
    static unsigned char outbuff[4];
    int i, j = 0;
    if( (((unsigned char *) (&byte_order_test))[0] == 0) )
        memcpy(outbuff, (unsigned char *)&number, 4);
    else
    {
        for( i = 3; i >= 0; i-- )
            outbuff[j++] = ((unsigned char *) &number)[i];
    }
    return outbuff;
}

canonical_original_code:
unsigned char *format_ui32(unsigned int number)
{
    static unsigned char outbuff[4];
    int i; int j = 0;
    if( (((unsigned char *) (&byte_order_test))[0] == 0) )
        memcpy(outbuff, (unsigned char *)&number, 4);
    else
    {
        for( i = 3; i >= 0; i-- )
            outbuff[j++] = ((unsigned char *) &number)[i];
    }
    return outbuff;
}

variable_types:
number
unsigned int

variable_types:
outbuff
unsigned char [4]

return_type: unsigned char *


decompiled_code:
char *__fastcall format_ui24(int a1)
{
byte_10D = HIBYTE(a1);
word_10B = __ROL2__(a1, 8);
outbuff_1 = BYTE2(a1);
return &outbuff_1;
}

canonical_decompiled_code:
char *__fastcall func7(int a1)
{
byte_10D = HIBYTE(a1);
word_10B = __ROL2__(a1, 8);
outbuff_1 = BYTE2(a1);
return &outbuff_1;
}

original_code:
unsigned char *format_ui24(unsigned int number)
{
    static unsigned char outbuff[4];
    int i, j = 0;
    if( (((unsigned char *) (&byte_order_test))[0] == 0) )
    {
        outbuff[3] = ((unsigned char *)&number)[0];
        for( i = 1; i < 4; i++ )
            outbuff[j++] = ((unsigned char *)&number)[i];
    }
    else
    {
        outbuff[3] = ((unsigned char *) &number)[3];
        for( i = 2; i >= 0; i-- )
            outbuff[j++] = ((unsigned char *) &number)[i];
    }
    return outbuff;
}

canonical_original_code:
unsigned char *format_ui24(unsigned int number)
{
    static unsigned char outbuff[4];
    int i; int j = 0;
    if( (((unsigned char *) (&byte_order_test))[0] == 0) )
    {
        outbuff[3] = ((unsigned char *)&number)[0];
        for( i = 1; i < 4; i++ )
            outbuff[j++] = ((unsigned char *)&number)[i];
    }
    else
    {
        outbuff[3] = ((unsigned char *) &number)[3];
        for( i = 2; i >= 0; i-- )
            outbuff[j++] = ((unsigned char *) &number)[i];
    }
    return outbuff;
}


decompiled_code:
char __fastcall add_meta_item(const char *a1, double a2)
{
int v2; // eax
bool v3; // zf

LOBYTE(v2) = (_BYTE)a1;
if ( a2 != -1.0 )
{
if ( !strcmp(a1, "width") )
{
qword_A48 = *(_QWORD *)&a2;
}
else if ( !strcmp(a1, "height") )
{
qword_A50 = *(_QWORD *)&a2;
}
else if ( !strcmp(a1, "framerate") )
{
qword_A58 = *(_QWORD *)&a2;
}
else if ( !strcmp(a1, "videocodecid") )
{
qword_A60 = *(_QWORD *)&a2;
}
else if ( !strcmp(a1, "audiosamplerate") )
{
qword_A68 = *(_QWORD *)&a2;
}
else if ( !strcmp(a1, "audiosamplesize") )
{
qword_A70 = *(_QWORD *)&a2;
}
else if ( !strcmp(a1, "stereo") )
{
v2 = (int)a2;
byte_A78 = (int)a2;
}
else
{
v3 = strcmp(a1, "audiocodecid") == 0;
LOBYTE(v2) = !v3;
if ( v3 )
qword_A80 = *(_QWORD *)&a2;
}
}
return v2;
}

canonical_decompiled_code:
char __fastcall func4(const char *a1, double a2)
{
int v2; // eax
bool v3; // zf

LOBYTE(v2) = (_BYTE)a1;
if ( a2 != -1.0 )
{
if ( !strcmp(a1, "width") )
{
qword_A48 = *(_QWORD *)&a2;
}
else if ( !strcmp(a1, "height") )
{
qword_A50 = *(_QWORD *)&a2;
}
else if ( !strcmp(a1, "framerate") )
{
qword_A58 = *(_QWORD *)&a2;
}
else if ( !strcmp(a1, "videocodecid") )
{
qword_A60 = *(_QWORD *)&a2;
}
else if ( !strcmp(a1, "audiosamplerate") )
{
qword_A68 = *(_QWORD *)&a2;
}
else if ( !strcmp(a1, "audiosamplesize") )
{
qword_A70 = *(_QWORD *)&a2;
}
else if ( !strcmp(a1, "stereo") )
{
v2 = (int)a2;
byte_A78 = (int)a2;
}
else
{
v3 = strcmp(a1, "audiocodecid") == 0;
LOBYTE(v2) = !v3;
if ( v3 )
qword_A80 = *(_QWORD *)&a2;
}
}
return v2;
}

original_code:
static void add_meta_item(const char *name, double value)
{
    if(value == -1)
        return;
    if(strcmp(name, "width") == 0)
        meta.width = value;
    else if(strcmp(name, "height") == 0)
        meta.height = value;
    else if(strcmp(name, "framerate") == 0)
        meta.framerate = value;
    else if(strcmp(name, "videocodecid") == 0)
        meta.videocodecid = value;
    else if(strcmp(name, "audiosamplerate") == 0)
        meta.audiosamplerate = value;
    else if(strcmp(name, "audiosamplesize") == 0)
        meta.audiosamplesize = value;
    else if(strcmp(name, "stereo") == 0)
        meta.stereo = (char)value;
    else if(strcmp(name, "audiocodecid") == 0)
        meta.audiocodecid = value;
    return;
}

canonical_original_code:
static void add_meta_item(char *name, double value)
{
    if(value == -1)
        return;
    if(strcmp(name, "width") == 0)
        meta.width = value;
    else if(strcmp(name, "height") == 0)
        meta.height = value;
    else if(strcmp(name, "framerate") == 0)
        meta.framerate = value;
    else if(strcmp(name, "videocodecid") == 0)
        meta.videocodecid = value;
    else if(strcmp(name, "audiosamplerate") == 0)
        meta.audiosamplerate = value;
    else if(strcmp(name, "audiosamplesize") == 0)
        meta.audiosamplesize = value;
    else if(strcmp(name, "stereo") == 0)
        meta.stereo = (char)value;
    else if(strcmp(name, "audiocodecid") == 0)
        meta.audiocodecid = value;
    return;
}

variable_types:
name
char *

variable_types: value double


decompiled_code:
double __fastcall parse_number(__int64 *a1)
{
unsigned __int8 *v1; // rax
__int64 v2; // rdx
double result; // xmm0_8
int v4; // edx
unsigned int v5; // esi
unsigned __int16 v6; // ax
int v7; // er12
int v8; // ebx
unsigned int v9; // esi
double v10; // xmm0_8
int v11; // eax
int v12; // er12
int v13; // ebx
double v14; // xmm0_8
char v15[1048]; // [rsp+0h] [rbp-418h] BYREF

v1 = (unsigned __int8 *)(*a1)++;
v2 = *v1;
switch ( (char)v2 )
{
case 0:
conv_double();
*a1 += 8LL;
return result;
case 1:
v4 = v1[1];
*a1 = (__int64)(v1 + 2);
return (double)v4;
case 2:
v5 = (unsigned __int16)conv_ui16();
goto LABEL_7;
case 3:
parse_script_object(a1);
return *(double *)&LC1;
case 7:
v6 = conv_ui16();
*a1 += 2LL;
return (double)v6;
case 8:
v7 = 0;
v8 = conv_ui32();
*a1 += 4LL;
if ( v8 > 0 )
{
do
{
++v7;
v9 = (unsigned __int16)conv_ui16();
*a1 = parse_script_string(*a1 + 2, v9, v15);
v10 = parse_number(a1);
add_meta_item(v15, v10);
}
while ( v8 != v7 );
}
goto LABEL_3;
case 10:
v11 = conv_ui32();
*a1 += 4LL;
v12 = v11;
if ( v11 <= 0 )
goto LABEL_3;
v13 = 0;
do
{
++v13;
result = parse_number(a1);
}
while ( v12 != v13 );
break;
case 11:
v14 = ((double (*)(void))conv_double)();
*a1 += 10LL;
return v14 / 1000.0;
case 12:
v5 = conv_ui32();
LABEL_7:
result = *(double *)&LC1;
*a1 = parse_script_string(*a1 + 2, v5, 0LL);
return result;
default:
fprintf(stderr, "WARNING: Unhandled script variable type %d\n", v2);
LABEL_3:
result = *(double *)&LC1;
break;
}
return result;
}

canonical_decompiled_code:
double __fastcall func5(__int64 *a1)
{
unsigned __int8 *v1; // rax
__int64 v2; // rdx
double result; // xmm0_8
int v4; // edx
unsigned int v5; // esi
unsigned __int16 v6; // ax
int v7; // er12
int v8; // ebx
unsigned int v9; // esi
double v10; // xmm0_8
int v11; // eax
int v12; // er12
int v13; // ebx
double v14; // xmm0_8
char v15[1048]; // [rsp+0h] [rbp-418h] BYREF

v1 = (unsigned __int8 *)(*a1)++;
v2 = *v1;
switch ( (char)v2 )
{
case 0:
conv_double();
*a1 += 8LL;
return result;
case 1:
v4 = v1[1];
*a1 = (__int64)(v1 + 2);
return (double)v4;
case 2:
v5 = (unsigned __int16)conv_ui16();
goto LABEL_7;
case 3:
parse_script_object(a1);
return *(double *)&LC1;
case 7:
v6 = conv_ui16();
*a1 += 2LL;
return (double)v6;
case 8:
v7 = 0;
v8 = conv_ui32();
*a1 += 4LL;
if ( v8 > 0 )
{
do
{
++v7;
v9 = (unsigned __int16)conv_ui16();
*a1 = func0(*a1 + 2, v9, v15);
v10 = func5(a1);
func4(v15, v10);
}
while ( v8 != v7 );
}
goto LABEL_3;
case 10:
v11 = conv_ui32();
*a1 += 4LL;
v12 = v11;
if ( v11 <= 0 )
goto LABEL_3;
v13 = 0;
do
{
++v13;
result = func5(a1);
}
while ( v12 != v13 );
break;
case 11:
v14 = ((double (*)(void))conv_double)();
*a1 += 10LL;
return v14 / 1000.0;
case 12:
v5 = conv_ui32();
LABEL_7:
result = *(double *)&LC1;
*a1 = func0(*a1 + 2, v5, 0LL);
return result;
default:
fprintf(stderr, "WARNING: Unhandled script variable type %d\n", v2);
LABEL_3:
result = *(double *)&LC1;
break;
}
return result;
}

original_code:
static double parse_number(unsigned char **pos)
{
    unsigned char variable_type = *(*pos)++;
    double value = -1;
    switch( variable_type )
    {
        case 0:
            value = conv_double(*pos);
            *pos += sizeof(double);
            break;
        case 1:
            value = (double)**pos;
            (*pos)++;
            break;
        case 2:
            *pos = parse_script_string(*pos + 2, conv_ui16(*pos), ((void *)0));
            break;
        case 3:
            parse_script_object(pos);
            break;
        case 7:
            value = conv_ui16(*pos);
            *pos += 2;
            break;
        case 8:
     {
         int array_length = conv_ui32(*pos);
  int count;
  *pos += 4;
  for( count = 0; count < array_length; count++ )
  {
             char buff[1024];
      double val;
      *pos = parse_script_string(*pos + 2, conv_ui16(*pos), buff);
      val = parse_number(pos);
                    add_meta_item(buff, val);
  }
     }
            break;
        case 10:
     {
         int array_length = conv_ui32(*pos);
  int count;
  *pos += 4;
  for( count = 0; count < array_length; count++ )
      value = parse_number(pos);
     }
            break;
        case 11:
            value = conv_double(*pos) / 1000;
     *pos += sizeof(double);
     *pos += 2;
            break;
        case 12:
            *pos = parse_script_string(*pos + 2, conv_ui32(*pos), ((void *)0));
            break;
        default:
            fprintf(stderr, "WARNING: Unhandled script variable type %d\n", variable_type);
            break;
   }
    return value;
}

variable_types:
pos
unsigned char **

variable_types: count int

variable_types:
buff
char [1024]

variable_types: val double


original_code:
int extract_metadata(struct FLVpacket *packet)
{
    unsigned char *pos = packet->data;
    unsigned char marker;
    int found_metadata_marker = 0;
    if(packet->type != 18)
        return 0;
    marker = *(pos++);
    if( marker != 2 )
        pos--;
    while(pos - packet->data <= packet->datasize - 6)
    {
        char buff[1024];
        double val;
        pos = parse_script_string(pos + 2, conv_ui16(pos), buff);
        if(strcmp(buff, "onMetaData") == 0)
            found_metadata_marker = 1;
        val = parse_number(&pos);
        add_meta_item(buff, val);
    }
    return found_metadata_marker;
}

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

variable_types:
pos
unsigned char *

variable_types:
marker
unsigned char


decompiled_code:
__int64 __fastcall parse_script_string(__int64 a1, unsigned int a2, _BYTE *a3)
{
__int64 i; // rax

if ( a3 )
{
if ( a2 )
{
for ( i = 0LL; i != a2; ++i )
a3[i] = *(_BYTE *)(a1 + i);
a3 += a2;
}
*a3 = 0;
}
return a1 + a2;
}

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

variable_types:
buff
char *

variable_types:
count
unsigned int


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

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

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

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


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

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

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

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:
infile
char [1024]

variable_types:
newline
char *

variable_types:
duration
unsigned int


decompiled_code:
char *__fastcall parse_script_string(char *a1, int a2)
{
char *v2; // r13
char *v3; // rbx
__int64 v4; // rbp
int v5; // edi

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

canonical_decompiled_code:
char *__fastcall func0(char *a1, int a2)
{
char *v2; // r13
char *v3; // rbx
__int64 v4; // rbp
int v5; // edi

v2 = a1;
if ( a2 )
{
v3 = a1;
v4 = (__int64)&a1[a2 - 1 + 1];
do
{
v5 = *v3++;
putchar(v5);
}
while ( v3 != (char *)v4 );
}
return &v2[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;
}


decompiled_code:
char *__fastcall parse_script_object(_BYTE *a1)
{
_BYTE *v1; // rbx
char v2; // r12
int v3; // esi
char *v4; // r12
char *v5; // r12

v1 = a1;
v2 = *a1;
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 ( !*(_WORD *)v5 && v5[2] == 9 )
return v5 + 3;
puts("WARNING: Script variable closing bytes missing.");
return v5;
}

canonical_decompiled_code:
char *__fastcall func2(_BYTE *a1)
{
_BYTE *v1; // rbx
char v2; // r12
int v3; // esi
char *v4; // r12
char *v5; // r12

v1 = a1;
v2 = *a1;
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 ( !*(_WORD *)v5 && v5[2] == 9 )
return v5 + 3;
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;
}


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


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
__int64 result; // rax
unsigned int v7; // [rsp+0h] [rbp-2Ch]

if ( !a3 )
return 2147549185LL;
if ( strlen(a1) > 0x1E )
return 2147680257LL;
v4 = malloc(0x30uLL);
v5 = v4;
if ( !v4 )
return 2147549186LL;
result = semaphore_init((__int64)v4, a1, a2);
if ( (_DWORD)result )
{
v7 = result;
free(v5);
*a3 = 0LL;
result = v7;
}
else
{
*a3 = v5;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1, __int16 a2, _QWORD *a3)
{
void *v4; // rax
void *v5; // rbp
__int64 result; // rax
unsigned int v7; // [rsp+0h] [rbp-2Ch]

if ( !a3 )
return 2147549185LL;
if ( strlen(a1) > 0x1E )
return 2147680257LL;
v4 = malloc(0x30uLL);
v5 = v4;
if ( !v4 )
return 2147549186LL;
result = func0((__int64)v4, a1, a2);
if ( (_DWORD)result )
{
v7 = result;
free(v5);
*a3 = 0LL;
result = v7;
}
else
{
*a3 = v5;
}
return result;
}

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, ...)
{
__int64 v10; // rdx
void *v11; // rsp
_WORD *v12; // rdi
_DWORD *v13; // rsi
_WORD *v14; // rax
unsigned int v15; // ecx
__int64 v16; // rdx
_DWORD *v17; // rdx
__int64 result; // rax
_WORD v19[4]; // [rsp+0h] [rbp-60h] BYREF
gcc_va_list va; // [rsp+8h] [rbp-58h] BYREF

if ( !a1 || *(_DWORD *)a1 != 1936026989 )
return 2147549185LL;
v10 = *(unsigned __int16 *)(a1 + 4);
v11 = alloca((2 * *(unsigned __int16 *)(a1 + 4) + 23) & 0x3FFF0);
va_start(va, a9);
if ( (_WORD)v10 )
{
v12 = &v19[v10];
v13 = va[0].overflow_arg_area;
v14 = v19;
v15 = 8;
do
{
while ( v15 <= 0x2F )
{
v16 = v15;
++v14;
v15 += 8;
*(v14 - 1) = *(_DWORD *)((char *)va[0].reg_save_area + v16);
if ( v14 == v12 )
goto LABEL_8;
}
v17 = v13;
++v14;
v13 += 2;
*(v14 - 1) = *v17;
}
while ( v14 != v12 );
}
LABEL_8:
result = semaphore_open(a1, 1);
if ( !(_DWORD)result )
return semaphore_set_values((_DWORD *)a1, (__int64)v19);
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, ...)
{
__int64 v10; // rdx
void *v11; // rsp
_WORD *v12; // rdi
_DWORD *v13; // rsi
_WORD *v14; // rax
unsigned int v15; // ecx
__int64 v16; // rdx
_DWORD *v17; // rdx
__int64 result; // rax
_WORD v19[4]; // [rsp+0h] [rbp-60h] BYREF
gcc_va_list va; // [rsp+8h] [rbp-58h] BYREF

if ( !a1 || *(_DWORD *)a1 != 1936026989 )
return 2147549185LL;
v10 = *(unsigned __int16 *)(a1 + 4);
v11 = alloca((2 * *(unsigned __int16 *)(a1 + 4) + 23) & 0x3FFF0);
va_start(va, a9);
if ( (_WORD)v10 )
{
v12 = &v19[v10];
v13 = va[0].overflow_arg_area;
v14 = v19;
v15 = 8;
do
{
while ( v15 <= 0x2F )
{
v16 = v15;
++v14;
v15 += 8;
*(v14 - 1) = *(_DWORD *)((char *)va[0].reg_save_area + v16);
if ( v14 == v12 )
goto LABEL_8;
}
v17 = v13;
++v14;
v13 += 2;
*(v14 - 1) = *v17;
}
while ( v14 != v12 );
}
LABEL_8:
result = func2(a1, 1);
if ( !(_DWORD)result )
return func4((_DWORD *)a1, (__int64)v19);
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)
{
int v2; // edi
struct sembuf sops; // [rsp+0h] [rbp-Eh] BYREF

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

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

if ( !a1 || *(_DWORD *)a1 != 1936026989 || *(_WORD *)(a1 + 4) <= a2 )
return 2147549185LL;
v2 = *(_DWORD *)(a1 + 44);
sops.sem_num = a2;
*(_DWORD *)&sops.sem_op = 268500991;
semop(v2, &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)
{
int v2; // edi
struct sembuf sops; // [rsp+0h] [rbp-Eh] BYREF

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

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

if ( !a1 || *(_DWORD *)a1 != 1936026989 || *(_WORD *)(a1 + 4) <= a2 )
return 2147549185LL;
v2 = *(_DWORD *)(a1 + 44);
sops.sem_num = a2;
*(_DWORD *)&sops.sem_op = 268435457;
semop(v2, &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; }


decompiled_code:
__int64 __fastcall semaphore_close(__int64 a1, int a2)
{
char v3[280]; // [rsp+0h] [rbp-118h] BYREF

if ( a2 )
{
semctl(*(_DWORD *)(a1 + 44), 0, 0);
sprintf(v3, "%s/%s", "/tmp", (const char *)(a1 + 6));
unlink(v3);
}
*(_QWORD *)(a1 + 40) = -1LL;
return 0LL;
}

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:
// attributes: thunk
void semaphore_free(void *ptr)
{
free(ptr);
}

canonical_decompiled_code:
void func9(void *ptr)
{
free(ptr);
}

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; // rax
char *v2; // rbp
char *v4; // rbp
void **v5; // rbx
void *v6; // rdi

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

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

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

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; // r12
const char *v4; // rax
const char *v5; // r12
__int128 v6[2]; // [rsp+0h] [rbp-28h] BYREF

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

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2)
{
__int64 v2; // r12
const char *v4; // rax
const char *v5; // r12
__int128 v6[2]; // [rsp+0h] [rbp-28h] BYREF

v2 = 0LL;
if ( a2 )
{
v6[0] = 0LL;
if ( (unsigned int)evhttp_parse_query_str(a2, v6)
|| (v4 = (const char *)evhttp_find_header(v6, "series"), (v5 = v4) == 0LL) )
{
v2 = 0LL;
}
else
{
printf("query is for %s\n", v4);
v2 = metrics_find_metric(a1, v5);
}
evhttp_clear_headers(v6);
}
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:
// attributes: thunk
int close_screen(void)
{
return endwin();
}

canonical_decompiled_code:
int func1(void)
{
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; // rbp
__int64 result; // rax
unsigned int v5; // [rsp+0h] [rbp-1Ch]

if ( !a2 )
return 2147549185LL;
v2 = malloc(0x30uLL);
v3 = v2;
if ( !v2 )
return 2147549186LL;
result = semaphore_init(v2, a1, 1LL);
if ( (_DWORD)result )
{
v5 = result;
free(v3);
*a2 = 0LL;
result = v5;
}
else
{
*a2 = v3;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, _QWORD *a2)
{
void *v2; // rax
void *v3; // rbp
__int64 result; // rax
unsigned int v5; // [rsp+0h] [rbp-1Ch]

if ( !a2 )
return 2147549185LL;
v2 = malloc(0x30uLL);
v3 = v2;
if ( !v2 )
return 2147549186LL;
result = semaphore_init(v2, a1, 1LL);
if ( (_DWORD)result )
{
v5 = result;
free(v3);
*a2 = 0LL;
result = v5;
}
else
{
*a2 = v3;
}
return result;
}

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:
// attributes: thunk
void lock_free(void *ptr)
{
free(ptr);
}

canonical_decompiled_code:
void func1(void *ptr)
{
free(ptr);
}

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; // r13
int v3; // ebp
int v4; // er12
__int64 v5; // rbx
__int64 v6; // rdi
__int64 v8; // rbx
__int64 v10[28]; // [rsp+18h] [rbp-E0h] BYREF

v10[0] = 0LL;
v1 = open_stats();
if ( v1 )
{
v2 = v1;
v3 = 0;
v4 = 0;
while ( 1 )
{
micro_sleep(0, 100000);
if ( __ROR4__(-858993459 * v4, 1) <= 0x19999999u )
break;
if ( v3 )
goto LABEL_5;
LABEL_7:
if ( ++v4 == 200 )
{
stats_close(v2);
stats_free(v2);
return 0LL;
}
}
v8 = v3++;
printf("writer %d: allocating counter %s\n", a1, counter_names[v8]);
stats_allocate_counter(v2, counter_names[v8], v10);
v10[v8 + 1] = v10[0];
LABEL_5:
v5 = 0LL;
do
{
v6 = v10[++v5];
counter_increment(v6);
}
while ( v3 > (int)v5 );
goto LABEL_7;
}
return 2147549184LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1)
{
__int64 v1; // rax
__int64 v2; // r13
int v3; // ebp
int v4; // er12
__int64 v5; // rbx
__int64 v6; // rdi
__int64 v8; // rbx
__int64 v10[28]; // [rsp+18h] [rbp-E0h] BYREF

v10[0] = 0LL;
v1 = func1();
if ( v1 )
{
v2 = v1;
v3 = 0;
v4 = 0;
while ( 1 )
{
func0(0, 100000);
if ( __ROR4__(-858993459 * v4, 1) <= 0x19999999u )
break;
if ( v3 )
goto LABEL_5;
LABEL_7:
if ( ++v4 == 200 )
{
stats_close(v2);
stats_free(v2);
return 0LL;
}
}
v8 = v3++;
printf("writer %d: allocating counter %s\n", a1, counter_names[v8]);
stats_allocate_counter(v2, counter_names[v8], v10);
v10[v8 + 1] = v10[0];
LABEL_5:
v5 = 0LL;
do
{
v6 = v10[++v5];
counter_increment(v6);
}
while ( v3 > (int)v5 );
goto LABEL_7;
}
return 2147549184LL;
}

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

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; // r8
__int64 result; // rax
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);
result = 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;
}
result = v2;
}
return result;
}

canonical_decompiled_code:
__int64 func1()
{
unsigned int v0; // eax
unsigned int v1; // eax
__int64 v2; // r8
__int64 result; // rax
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);
result = 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;
}
result = v2;
}
return result;
}

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; // ebp
unsigned int v4; // eax
unsigned int v5; // eax
int v6; // er12
int *v7; // r12
unsigned int v8; // eax
__int64 v9; // rcx
__WAIT_STATUS stat_loc; // [rsp+Ch] [rbp-2Ch] BYREF

v3 = 0;
while ( 1 )
{
v4 = getpid();
printf("calling fork from %d\n", v4);
fflush(stdout);
fflush(stderr);
v5 = fork();
LODWORD(stat_loc.__uptr) = v5;
v6 = v5;
if ( v5 == -1 )
{
puts("error: could not fork");
return v6;
}
if ( !v5 )
break;
++v3;
printf("Launched pid %d\n", v5);
if ( v3 == 3 )
{
puts("Waiting for child processes");
v7 = _errno_location();
while ( 1 )
{
v8 = wait((__WAIT_STATUS)&stat_loc);
v9 = (unsigned int)*v7;
if ( v8 == -1 )
break;
printf("wait returned %d, stat=%d, errno=%d\n", v8, LODWORD(stat_loc.__uptr), v9);
}
v6 = 0;
printf("wait returned %d, stat=%d, errno=%d\n", 0xFFFFFFFFLL, LODWORD(stat_loc.__uptr), v9);
return v6;
}
}
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; // ebp
unsigned int v4; // eax
unsigned int v5; // eax
int v6; // er12
int *v7; // r12
unsigned int v8; // eax
__int64 v9; // rcx
__WAIT_STATUS stat_loc; // [rsp+Ch] [rbp-2Ch] BYREF

v3 = 0;
while ( 1 )
{
v4 = getpid();
printf("calling fork from %d\n", v4);
fflush(stdout);
fflush(stderr);
v5 = fork();
LODWORD(stat_loc.__uptr) = v5;
v6 = v5;
if ( v5 == -1 )
{
puts("error: could not fork");
return v6;
}
if ( !v5 )
break;
++v3;
printf("Launched pid %d\n", v5);
if ( v3 == 3 )
{
puts("Waiting for child processes");
v7 = _errno_location();
while ( 1 )
{
v8 = wait((__WAIT_STATUS)&stat_loc);
v9 = (unsigned int)*v7;
if ( v8 == -1 )
break;
printf("wait returned %d, stat=%d, errno=%d\n", v8, LODWORD(stat_loc.__uptr), v9);
}
v6 = 0;
printf("wait returned %d, stat=%d, errno=%d\n", 0xFFFFFFFFLL, LODWORD(stat_loc.__uptr), v9);
return v6;
}
}
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; // rbp
__int64 result; // rax
unsigned int v9; // [rsp+0h] [rbp-2Ch]

if ( !a4 )
return 2147549185LL;
v6 = malloc(0x40uLL);
v7 = v6;
if ( !v6 )
return 2147549186LL;
result = shared_memory_init((__int64)v6, a1, a2, a3);
if ( (_DWORD)result )
{
v9 = result;
free(v7);
*a4 = 0LL;
result = v9;
}
else
{
*a4 = v7;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1, int a2, int a3, _QWORD *a4)
{
void *v6; // rax
void *v7; // rbp
__int64 result; // rax
unsigned int v9; // [rsp+0h] [rbp-2Ch]

if ( !a4 )
return 2147549185LL;
v6 = malloc(0x40uLL);
v7 = v6;
if ( !v6 )
return 2147549186LL;
result = func0((__int64)v6, a1, a2, a3);
if ( (_DWORD)result )
{
v9 = result;
free(v7);
*a4 = 0LL;
result = v9;
}
else
{
*a4 = v7;
}
return result;
}

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

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