decompiled_code:
__int64 __fastcall csv_archive(__int64 a1, __int64 a2, _QWORD *a3)
{
__int64 v4; // rax
__int64 v5; // rbp
unsigned int v6; // eax
__int64 v7; // rbx
struct tm *v8; // rax
void *ptr; // [rsp+8h] [rbp-60h] BYREF
__int64 v11; // [rsp+10h] [rbp-58h] BYREF
__int64 v12; // [rsp+18h] [rbp-50h] BYREF
char s[72]; // [rsp+20h] [rbp-48h] BYREF

v11 = 0LL;
v12 = 0LL;
((void (*)(void))list_iterator_stop)();
list_iterator_start(a1);
while ( (unsigned int)list_iterator_hasnext(a1) )
{
v4 = list_iterator_next(a1);
v5 = v4;
if ( gDebug )
printf(
"%s[%d]: elapsed time %d <> %d TM_HOUR\n",
*(const char **)(*(_QWORD *)(v4 + 8) + 8LL),
*(unsigned __int16 *)(*(_QWORD *)(v4 + 8) + 24LL),
(unsigned int)*(_QWORD *)(v4 + 32) - *(_DWORD *)(v4 + 24),
3600LL);
while ( (__int64)(*(_QWORD *)(v5 + 32) - *(_QWORD *)(v5 + 24)) > 3599 )
{
fmash_csv_to_msh(v5, &ptr, &v11);
if ( gDebug )
{
fprintf(stdout, "compressed data size is %lu bytes\n", v11);
format_data(ptr, v11, 0LL, 0LL);
if ( (unsigned int)fmash_msh_to_csv(&v12, ptr, v11) )
{
if ( v12 )
{
if ( *(_QWORD *)(v12 + 8) )
{
if ( *(_QWORD *)(v12 + 16) )
{
fwrite("=== FMASH VERIFICATION =================================\n", 1uLL, 0x39uLL, stdout);
fprintf(stdout, " file: %s\n", *(const char **)v5);
fprintf(stdout, " channel: %d\n", *(unsigned __int16 *)(*(_QWORD *)(v5 + 8) + 24LL));
fprintf(stdout, " description: %s\n", *(const char **)(*(_QWORD *)(v5 + 8) + 8LL));
v6 = list_size(*(_QWORD *)(v12 + 16));
fprintf(stdout, " lines: %d\n", v6);
list_iterator_stop(*(_QWORD *)(v12 + 16));
list_iterator_start(*(_QWORD *)(v12 + 16));
fwrite(" ---------------------- ----------- ----------- ----------- \n", 1uLL, 0x44uLL, stdout);
fwrite(" | Timestamp | Average | High | Low |\n", 1uLL, 0x44uLL, stdout);
fwrite(" ---------------------- ----------- ----------- ----------- \n", 1uLL, 0x44uLL, stdout);
while ( (unsigned int)list_iterator_hasnext(*(_QWORD *)(v12 + 16)) )
{
v7 = list_iterator_next(*(_QWORD *)(v12 + 16));
v8 = gmtime((const time_t *)v7);
strftime(s, 0x1FuLL, "%Y/%m/%d %H:%M:%S", v8);
printf(
" | %s | % 9d | % 9d | % 9d |\n",
s,
*(unsigned int *)(v7 + 8),
*(unsigned int *)(v7 + 12),
*(unsigned int *)(v7 + 16));
}
putchar(10);
list_iterator_stop(*(_QWORD *)(v12 + 16));
v12 = csv_buffer_destroy(v12);
fflush(stdout);
}
else
{
fwrite("final_csv->list not created\n", 1uLL, 0x1CuLL, stdout);
}
}
else
{
fwrite("final_csv->header not created\n", 1uLL, 0x1EuLL, stdout);
}
}
else
{
fwrite("final_csv not created\n", 1uLL, 0x16uLL, stdout);
}
}
else
{
fwrite("FMash to CSV conversion failed\n", 1uLL, 0x1FuLL, stdout);
}
}
if ( ptr )
{
if ( v11 )
{
QueueOpaque(ptr, v11, *a3, a3[1], a3[3], a3[4], "FALC~");
free(ptr);
ptr = 0LL;
}
}
}
}
return list_iterator_stop(a1);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2, _QWORD *a3)
{
__int64 v4; // rax
__int64 v5; // rbp
unsigned int v6; // eax
__int64 v7; // rbx
struct tm *v8; // rax
void *ptr; // [rsp+8h] [rbp-60h] BYREF
__int64 v11; // [rsp+10h] [rbp-58h] BYREF
__int64 v12; // [rsp+18h] [rbp-50h] BYREF
char s[72]; // [rsp+20h] [rbp-48h] BYREF

v11 = 0LL;
v12 = 0LL;
((void (*)(void))list_iterator_stop)();
list_iterator_start(a1);
while ( (unsigned int)list_iterator_hasnext(a1) )
{
v4 = list_iterator_next(a1);
v5 = v4;
if ( gDebug )
printf(
"%s[%d]: elapsed time %d <> %d TM_HOUR\n",
*(const char **)(*(_QWORD *)(v4 + 8) + 8LL),
*(unsigned __int16 *)(*(_QWORD *)(v4 + 8) + 24LL),
(unsigned int)*(_QWORD *)(v4 + 32) - *(_DWORD *)(v4 + 24),
3600LL);
while ( (__int64)(*(_QWORD *)(v5 + 32) - *(_QWORD *)(v5 + 24)) > 3599 )
{
fmash_csv_to_msh(v5, &ptr, &v11);
if ( gDebug )
{
fprintf(stdout, "compressed data size is %lu bytes\n", v11);
format_data(ptr, v11, 0LL, 0LL);
if ( (unsigned int)fmash_msh_to_csv(&v12, ptr, v11) )
{
if ( v12 )
{
if ( *(_QWORD *)(v12 + 8) )
{
if ( *(_QWORD *)(v12 + 16) )
{
fwrite("=== FMASH VERIFICATION =================================\n", 1uLL, 0x39uLL, stdout);
fprintf(stdout, " file: %s\n", *(const char **)v5);
fprintf(stdout, " channel: %d\n", *(unsigned __int16 *)(*(_QWORD *)(v5 + 8) + 24LL));
fprintf(stdout, " description: %s\n", *(const char **)(*(_QWORD *)(v5 + 8) + 8LL));
v6 = list_size(*(_QWORD *)(v12 + 16));
fprintf(stdout, " lines: %d\n", v6);
list_iterator_stop(*(_QWORD *)(v12 + 16));
list_iterator_start(*(_QWORD *)(v12 + 16));
fwrite(" ---------------------- ----------- ----------- ----------- \n", 1uLL, 0x44uLL, stdout);
fwrite(" | Timestamp | Average | High | Low |\n", 1uLL, 0x44uLL, stdout);
fwrite(" ---------------------- ----------- ----------- ----------- \n", 1uLL, 0x44uLL, stdout);
while ( (unsigned int)list_iterator_hasnext(*(_QWORD *)(v12 + 16)) )
{
v7 = list_iterator_next(*(_QWORD *)(v12 + 16));
v8 = gmtime((const time_t *)v7);
strftime(s, 0x1FuLL, "%Y/%m/%d %H:%M:%S", v8);
printf(
" | %s | % 9d | % 9d | % 9d |\n",
s,
*(unsigned int *)(v7 + 8),
*(unsigned int *)(v7 + 12),
*(unsigned int *)(v7 + 16));
}
putchar(10);
list_iterator_stop(*(_QWORD *)(v12 + 16));
v12 = csv_buffer_destroy(v12);
fflush(stdout);
}
else
{
fwrite("final_csv->list not created\n", 1uLL, 0x1CuLL, stdout);
}
}
else
{
fwrite("final_csv->header not created\n", 1uLL, 0x1EuLL, stdout);
}
}
else
{
fwrite("final_csv not created\n", 1uLL, 0x16uLL, stdout);
}
}
else
{
fwrite("FMash to CSV conversion failed\n", 1uLL, 0x1FuLL, stdout);
}
}
if ( ptr )
{
if ( v11 )
{
QueueOpaque(ptr, v11, *a3, a3[1], a3[3], a3[4], "FALC~");
free(ptr);
ptr = 0LL;
}
}
}
}
return list_iterator_stop(a1);
}

original_code:
void csv_archive( csv_context_t* csv_buffer_list, buffer_t* url_str,
                  st_info_t* st_info )
{
    csv_buffer_t* csv_buffer;
    csv_row_t* csv_row;
    uint8_t* msh_data;
    size_t msh_length = 0;
    csv_buffer_t* final_csv = ((void *)0);
    char time_string[32];
    list_iterator_stop(csv_buffer_list);
    list_iterator_start(csv_buffer_list);
    while (list_iterator_hasnext(csv_buffer_list))
    {
        csv_buffer = (csv_buffer_t*)list_iterator_next(csv_buffer_list);
        if (gDebug)
        {
          printf("%s[%d]: elapsed time %d <> %d TM_HOUR\n",
            csv_buffer->header->description, csv_buffer->header->channel,
            (int)(csv_buffer->end_time - csv_buffer->start_time), 3600);
        }
        while ((csv_buffer->end_time - csv_buffer->start_time) >= 3600)
        {
            fmash_csv_to_msh(csv_buffer, &msh_data, &msh_length);
            if (gDebug) {
                fprintf(stdout, "compressed data size is %lu bytes\n", (unsigned long)msh_length);
                format_data(msh_data, msh_length, 0, 0);
                if (!fmash_msh_to_csv( &final_csv, msh_data, msh_length )) {
                    fprintf(stdout, "FMash to CSV conversion failed\n");
                    goto queue_it;
                }
                if (!final_csv) {
                    fprintf(stdout, "final_csv not created\n");
                    goto queue_it;
                }
                if (!final_csv->header) {
                    fprintf(stdout, "final_csv->header not created\n");
                    goto queue_it;
                }
                if (!final_csv->list) {
                    fprintf(stdout, "final_csv->list not created\n");
                    goto queue_it;
                }
                fprintf(stdout, "=== FMASH VERIFICATION =================================\n");
                fprintf(stdout, "    file:         %s\n", csv_buffer->file_name);
                fprintf(stdout, "    channel:      %d\n", csv_buffer->header->channel);
                fprintf(stdout, "    description:  %s\n", csv_buffer->header->description);
                fprintf(stdout, "    lines:        %d\n", list_size(final_csv->list));
                list_iterator_stop(final_csv->list);
                list_iterator_start(final_csv->list);
                    fprintf(stdout, "        ---------------------- ----------- ----------- ----------- \n");
                    fprintf(stdout, "       | Timestamp            | Average   | High      | Low       |\n");
                    fprintf(stdout, "        ---------------------- ----------- ----------- ----------- \n");
                while (list_iterator_hasnext(final_csv->list))
                {
                    csv_row = (csv_row_t*)list_iterator_next(final_csv->list);
                    strftime(time_string, 31, "%Y/%m/%d %H:%M:%S", gmtime(&(csv_row->timestamp)));
                    printf("       | %s | % 9d | % 9d | % 9d |\n", time_string,
                           csv_row->average, csv_row->high, csv_row->low );
                }
                printf("\n");
                list_iterator_stop(final_csv->list);
                final_csv = csv_buffer_destroy(final_csv);
                fflush(stdout);
            }
 queue_it:
            if (msh_data && msh_length) {
                QueueOpaque(msh_data, msh_length, st_info->station,
                            st_info->network, st_info->csv_chan,
                            st_info->location, "FALC~");
                free(msh_data);
                msh_data = ((void *)0);
            }
        }
        csv_buffer = ((void *)0);
    }
    list_iterator_stop(csv_buffer_list);
}

canonical_original_code:
void csv_archive(struct list_t *csv_buffer_list, struct _buffer *url_str, struct st_info_t *st_info)
{
    struct csv_buffer_t *csv_buffer;
    struct csv_row_t *csv_row;
    unsigned char *msh_data;
    unsigned long msh_length = 0;
    struct csv_buffer_t *final_csv = ((void *)0);
    char time_string[32];
    list_iterator_stop(csv_buffer_list);
    list_iterator_start(csv_buffer_list);
    while (list_iterator_hasnext(csv_buffer_list))
    {
        csv_buffer = (struct csv_buffer_t *)list_iterator_next(csv_buffer_list);
        if (gDebug)
        {
          printf("%s[%d]: elapsed time %d <> %d TM_HOUR\n",
            csv_buffer->header->description, csv_buffer->header->channel,
            (int)(csv_buffer->end_time - csv_buffer->start_time), 3600);
        }
        while ((csv_buffer->end_time - csv_buffer->start_time) >= 3600)
        {
            fmash_csv_to_msh(csv_buffer, &msh_data, &msh_length);
            if (gDebug) {
                fprintf(stdout, "compressed data size is %lu bytes\n", (unsigned long)msh_length);
                format_data(msh_data, msh_length, 0, 0);
                if (!fmash_msh_to_csv( &final_csv, msh_data, msh_length )) {
                    fprintf(stdout, "FMash to CSV conversion failed\n");
                    goto queue_it;
                }
                if (!final_csv) {
                    fprintf(stdout, "final_csv not created\n");
                    goto queue_it;
                }
                if (!final_csv->header) {
                    fprintf(stdout, "final_csv->header not created\n");
                    goto queue_it;
                }
                if (!final_csv->list) {
                    fprintf(stdout, "final_csv->list not created\n");
                    goto queue_it;
                }
                fprintf(stdout, "=== FMASH VERIFICATION =================================\n");
                fprintf(stdout, "    file:         %s\n", csv_buffer->file_name);
                fprintf(stdout, "    channel:      %d\n", csv_buffer->header->channel);
                fprintf(stdout, "    description:  %s\n", csv_buffer->header->description);
                fprintf(stdout, "    lines:        %d\n", list_size(final_csv->list));
                list_iterator_stop(final_csv->list);
                list_iterator_start(final_csv->list);
                    fprintf(stdout, "        ---------------------- ----------- ----------- ----------- \n");
                    fprintf(stdout, "       | Timestamp            | Average   | High      | Low       |\n");
                    fprintf(stdout, "        ---------------------- ----------- ----------- ----------- \n");
                while (list_iterator_hasnext(final_csv->list))
                {
                    csv_row = (struct csv_row_t *)list_iterator_next(final_csv->list);
                    strftime(time_string, 31, "%Y/%m/%d %H:%M:%S", gmtime(&(csv_row->timestamp)));
                    printf("       | %s | % 9d | % 9d | % 9d |\n", time_string,
                           csv_row->average, csv_row->high, csv_row->low );
                }
                printf("\n");
                list_iterator_stop(final_csv->list);
                final_csv = csv_buffer_destroy(final_csv);
                fflush(stdout);
            }
 queue_it:
            if (msh_data && msh_length) {
                QueueOpaque(msh_data, msh_length, st_info->station,
                            st_info->network, st_info->csv_chan,
                            st_info->location, "FALC~");
                free(msh_data);
                msh_data = ((void *)0);
            }
        }
        csv_buffer = ((void *)0);
    }
    list_iterator_stop(csv_buffer_list);
}

variable_types: struct _buffer { unsigned char *content; unsigned long length; unsigned long capacity; unsigned long offset; } *

variable_types: struct st_info_t { char *station; char *network; char *alarm_chan; char *csv_chan; char *location; } *

variable_types: struct csv_buffer_t { char *file_name; struct csv_header_t { char *name; char *description; char *unit_of_measure; unsigned short channel; } *header; struct list_t { struct list_entry_s { void *data; struct list_entry_s *next; struct list_entry_s *prev; } *head_sentinel; struct list_entry_s *tail_sentinel; struct list_entry_s *mid; unsigned int numels; struct list_entry_s **spareels; unsigned int spareelsnum; int iter_active; unsigned int iter_pos; struct list_entry_s *iter_curentry; struct list_attributes_s { int (*comparator)(void *a, void *b); int (*seeker)(void *el, void *indicator); unsigned long (*meter)(void *el); int copy_data; int (*hasher)(void *el); void *(*serializer)(void *el, unsigned int *serializ_len); void *(*unserializer)(void *data, unsigned int *data_len); } attrs; } *list; long start_time; long end_time; long last_flush; } *

variable_types: struct csv_row_t { long timestamp; int average; int high; int low; unsigned char empty; } *

user_defined_types: struct csv_buffer_t { char *file_name; struct csv_header_t *header; struct list_t *list; long start_time; long end_time; long last_flush; }


repo: joeledwards/asl-station-processor

decompiled_code:
void __fastcall csv_poll(__int64 a1, _QWORD *a2, __int64 a3, time_t a4)
{
const char **v4; // r12
void *v5; // rax
void *v6; // rbp
__int64 *v7; // r13
void *v8; // rax
__int64 v9; // rax
int v10; // eax
const char **v11; // rbx
size_t v12; // rax
__int64 v13; // rax
char v14; // [rsp+7h] [rbp-81h]
const char *v15; // [rsp+8h] [rbp-80h]
char *ptr; // [rsp+10h] [rbp-78h]
__int64 v18; // [rsp+20h] [rbp-68h] BYREF
__int128 v19; // [rsp+28h] [rbp-60h]
__int128 v20; // [rsp+38h] [rbp-50h]
__int64 v21; // [rsp+48h] [rbp-40h]

v4 = (const char **)buffer_init();
buffer_write(v4, *a2, a2[1]);
buffer_write(v4, "/data/minute", 12LL);
buffer_terminate(v4);
v5 = malloc(0x78uLL);
if ( v5 )
{
v6 = v5;
list_init(v5);
list_attributes_seeker(v6, file_list_seeker);
list_attributes_comparator(v6, file_list_comparator);
v7 = (__int64 *)buffer_init();
get_page(*v4, v7);
if ( (unsigned int)csv_get_file_list((__int64)v6, (const char **)v7) )
{
buffer_reset(v7);
buffer_reset(v4);
while ( !(unsigned int)list_empty(v6) )
{
v9 = list_fetch(v6);
v21 = 0LL;
ptr = (char *)v9;
v18 = v9;
v19 = 0LL;
v20 = 0LL;
v10 = list_locate(a1, &v18);
if ( v10 < 0 )
{
v11 = (const char **)csv_buffer_init(a1, (unsigned int)v10);
*v11 = ptr;
list_append(a1, v11);
v14 = 0;
}
else
{
v14 = 1;
v11 = (const char **)list_get_at(a1, (unsigned int)v10);
}
buffer_write(v4, *a2, a2[1]);
v15 = *v11;
v12 = strlen(*v11);
buffer_write(v4, v15, v12);
buffer_terminate(v4);
if ( gDebug )
printf("getting page %s\n", *v4);
get_page(*v4, v7);
v13 = murmur_64_b(*v7, v7[1], 0xDE62D51808ECB849LL);
if ( gDebug )
{
fprintf(stderr, "file '%s' [0x%016llx] uncompressed size is %lu bytes\n", *v11, v13, v7[1]);
if ( !strcmp("/data/minute/logm1.csv", *v11) )
fprintf(stderr, "'%s'\n", (const char *)*v7);
}
csv_parse_file(v11, v7, a4);
if ( gDebug )
fprintf(stderr, "The CSV buffer contains %u rows\n", *((unsigned int *)v11[2] + 6));
buffer_reset(v7);
buffer_reset(v4);
if ( v14 )
free(ptr);
}
}
buffer_destroy(v7);
buffer_destroy(v4);
while ( !(unsigned int)list_empty(v6) )
{
v8 = (void *)list_fetch(v6);
if ( v8 )
free(v8);
}
list_destroy(v6);
free(v6);
}
else
{
buffer_destroy(0LL);
buffer_destroy(v4);
}
}

canonical_decompiled_code:
void __fastcall func5(__int64 a1, _QWORD *a2, __int64 a3, time_t a4)
{
const char **v4; // r12
void *v5; // rax
void *v6; // rbp
__int64 *v7; // r13
void *v8; // rax
__int64 v9; // rax
int v10; // eax
const char **v11; // rbx
size_t v12; // rax
__int64 v13; // rax
char v14; // [rsp+7h] [rbp-81h]
const char *v15; // [rsp+8h] [rbp-80h]
char *ptr; // [rsp+10h] [rbp-78h]
__int64 v18; // [rsp+20h] [rbp-68h] BYREF
__int128 v19; // [rsp+28h] [rbp-60h]
__int128 v20; // [rsp+38h] [rbp-50h]
__int64 v21; // [rsp+48h] [rbp-40h]

v4 = (const char **)buffer_init();
buffer_write(v4, *a2, a2[1]);
buffer_write(v4, "/data/minute", 12LL);
buffer_terminate(v4);
v5 = malloc(0x78uLL);
if ( v5 )
{
v6 = v5;
list_init(v5);
list_attributes_seeker(v6, file_list_seeker);
list_attributes_comparator(v6, file_list_comparator);
v7 = (__int64 *)buffer_init();
get_page(*v4, v7);
if ( (unsigned int)func4((__int64)v6, (const char **)v7) )
{
buffer_reset(v7);
buffer_reset(v4);
while ( !(unsigned int)list_empty(v6) )
{
v9 = list_fetch(v6);
v21 = 0LL;
ptr = (char *)v9;
v18 = v9;
v19 = 0LL;
v20 = 0LL;
v10 = list_locate(a1, &v18);
if ( v10 < 0 )
{
v11 = (const char **)csv_buffer_init(a1, (unsigned int)v10);
*v11 = ptr;
list_append(a1, v11);
v14 = 0;
}
else
{
v14 = 1;
v11 = (const char **)list_get_at(a1, (unsigned int)v10);
}
buffer_write(v4, *a2, a2[1]);
v15 = *v11;
v12 = strlen(*v11);
buffer_write(v4, v15, v12);
buffer_terminate(v4);
if ( gDebug )
printf("getting page %s\n", *v4);
get_page(*v4, v7);
v13 = murmur_64_b(*v7, v7[1], 0xDE62D51808ECB849LL);
if ( gDebug )
{
fprintf(stderr, "file '%s' [0x%016llx] uncompressed size is %lu bytes\n", *v11, v13, v7[1]);
if ( !strcmp("/data/minute/logm1.csv", *v11) )
fprintf(stderr, "'%s'\n", (const char *)*v7);
}
func3(v11, v7, a4);
if ( gDebug )
fprintf(stderr, "The CSV buffer contains %u rows\n", *((unsigned int *)v11[2] + 6));
buffer_reset(v7);
buffer_reset(v4);
if ( v14 )
free(ptr);
}
}
buffer_destroy(v7);
buffer_destroy(v4);
while ( !(unsigned int)list_empty(v6) )
{
v8 = (void *)list_fetch(v6);
if ( v8 )
free(v8);
}
list_destroy(v6);
free(v6);
}
else
{
buffer_destroy(0LL);
buffer_destroy(v4);
}
}

original_code:
void csv_poll( csv_context_t* csv_buffer_list, buffer_t* url_str,
               st_info_t* st_info, time_t initial_time )
{
    list_t* file_list = ((void *)0);
    buffer_t* buf = ((void *)0);
    buffer_t* url = ((void *)0);
    char* file_name = ((void *)0);
    const char* path = "/data/minute";
    csv_buffer_t csv_tmp;
    csv_buffer_t* csv_buffer;
    int location = 0;
    uint64_t file_hash = 0LL;
    uint8_t buffer_found = 0;
    int tally = 0;
    url = buffer_init();
    buffer_write(url, url_str->content, url_str->length);
    buffer_write(url, (uint8_t*)path, strlen(path));
    buffer_terminate(url);
    file_list = (list_t*)malloc(sizeof(list_t));
    if (!file_list)
        goto clean;
    list_init(file_list);
    list_attributes_seeker( file_list, _file_list_seeker );
    list_attributes_comparator( file_list, _file_list_comparator );
    buf = buffer_init();
    get_page((char*)url->content, buf);
    if (!csv_get_file_list(file_list, buf))
        goto clean;
    buffer_reset(buf);
    buffer_reset(url);
    while (!list_empty(file_list))
    {
        tally++;
        file_name = (char*)list_fetch(file_list);
        memset(&csv_tmp, 0, sizeof(csv_tmp));
        csv_tmp.file_name = file_name;
        if ((location = list_locate(csv_buffer_list, &csv_tmp)) < 0)
        {
            csv_buffer = csv_buffer_init();
            csv_buffer->file_name = file_name;
            list_append(csv_buffer_list, csv_buffer);
            buffer_found = 0;
        } else {
            csv_buffer = (csv_buffer_t*)list_get_at(csv_buffer_list, location);
            buffer_found = 1;
        }
        buffer_write(url, url_str->content, url_str->length);
        buffer_write(url, (uint8_t*)csv_buffer->file_name,
                     strlen(csv_buffer->file_name));
        buffer_terminate(url);
        if (gDebug) {
            printf("getting page %s\n", url->content);
        }
        get_page((char*)url->content, buf);
        file_hash = murmur_64_b( buf->content, buf->length, 0xde62d51808ecb849LL );
        if (gDebug) {
            fprintf(stderr, "file '%s' [0x%016llx] uncompressed size is %lu bytes\n",
                   csv_buffer->file_name, (unsigned long long)file_hash,
                   (unsigned long)buf->length);
            if (strcmp("/data/minute/logm1.csv", csv_buffer->file_name) == 0)
              fprintf(stderr, "'%s'\n", buf->content);
        }
        csv_parse_file(csv_buffer, buf, initial_time);
        if (gDebug) {
            fprintf(stderr, "The CSV buffer contains %u rows\n", csv_buffer->list->numels);
        }
        buffer_reset(buf);
        buffer_reset(url);
        if (buffer_found) {
            free(file_name);
        }
        file_name = ((void *)0);
        csv_buffer = ((void *)0);
    }
clean:
    buf = buffer_destroy(buf);
    url = buffer_destroy(url);
    if (file_list)
    {
        while(!list_empty(file_list))
        {
            file_name = list_fetch(file_list);
            if (file_name)
            {
                free(file_name);
            }
        }
        list_destroy(file_list);
        free(file_list);
    }
}

canonical_original_code:
void csv_poll(struct list_t *csv_buffer_list, struct _buffer *url_str, struct st_info_t *st_info, long initial_time)
{
    struct list_t *file_list = ((void *)0);
    struct _buffer *buf = ((void *)0);
    struct _buffer *url = ((void *)0);
    char *file_name = ((void *)0);
    char *path = "/data/minute";
    struct csv_buffer_t csv_tmp;
    struct csv_buffer_t *csv_buffer;
    int location = 0;
    unsigned long file_hash = 0LL;
    unsigned char buffer_found = 0;
    int tally = 0;
    url = buffer_init();
    buffer_write(url, url_str->content, url_str->length);
    buffer_write(url, (unsigned char *)path, strlen(path));
    buffer_terminate(url);
    file_list = (struct list_t *)malloc(sizeof(list_t));
    if (!file_list)
        goto clean;
    list_init(file_list);
    list_attributes_seeker( file_list, _file_list_seeker );
    list_attributes_comparator( file_list, _file_list_comparator );
    buf = buffer_init();
    get_page((char *)url->content, buf);
    if (!csv_get_file_list(file_list, buf))
        goto clean;
    buffer_reset(buf);
    buffer_reset(url);
    while (!list_empty(file_list))
    {
        tally++;
        file_name = (char *)list_fetch(file_list);
        memset(&csv_tmp, 0, sizeof(csv_tmp));
        csv_tmp.file_name = file_name;
        if ((location = list_locate(csv_buffer_list, &csv_tmp)) < 0)
        {
            csv_buffer = csv_buffer_init();
            csv_buffer->file_name = file_name;
            list_append(csv_buffer_list, csv_buffer);
            buffer_found = 0;
        } else {
            csv_buffer = (struct csv_buffer_t *)list_get_at(csv_buffer_list, location);
            buffer_found = 1;
        }
        buffer_write(url, url_str->content, url_str->length);
        buffer_write(url, (unsigned char *)csv_buffer->file_name,
                     strlen(csv_buffer->file_name));
        buffer_terminate(url);
        if (gDebug) {
            printf("getting page %s\n", url->content);
        }
        get_page((char *)url->content, buf);
        file_hash = murmur_64_b( buf->content, buf->length, 0xde62d51808ecb849LL );
        if (gDebug) {
            fprintf(stderr, "file '%s' [0x%016llx] uncompressed size is %lu bytes\n",
                   csv_buffer->file_name, (unsigned long long)file_hash,
                   (unsigned long)buf->length);
            if (strcmp("/data/minute/logm1.csv", csv_buffer->file_name) == 0)
              fprintf(stderr, "'%s'\n", buf->content);
        }
        csv_parse_file(csv_buffer, buf, initial_time);
        if (gDebug) {
            fprintf(stderr, "The CSV buffer contains %u rows\n", csv_buffer->list->numels);
        }
        buffer_reset(buf);
        buffer_reset(url);
        if (buffer_found) {
            free(file_name);
        }
        file_name = ((void *)0);
        csv_buffer = ((void *)0);
    }
clean:
    buf = buffer_destroy(buf);
    url = buffer_destroy(url);
    if (file_list)
    {
        while(!list_empty(file_list))
        {
            file_name = list_fetch(file_list);
            if (file_name)
            {
                free(file_name);
            }
        }
        list_destroy(file_list);
        free(file_list);
    }
}

variable_types:
path
char *

variable_types: struct csv_buffer_t { char *file_name; struct csv_header_t { char *name; char *description; char *unit_of_measure; unsigned short channel; } *header; struct list_t { struct list_entry_s { void *data; struct list_entry_s *next; struct list_entry_s *prev; } *head_sentinel; struct list_entry_s *tail_sentinel; struct list_entry_s *mid; unsigned int numels; struct list_entry_s **spareels; unsigned int spareelsnum; int iter_active; unsigned int iter_pos; struct list_entry_s *iter_curentry; struct list_attributes_s { int (*comparator)(void *a, void *b); int (*seeker)(void *el, void *indicator); unsigned long (*meter)(void *el); int copy_data; int (*hasher)(void *el); void *(*serializer)(void *el, unsigned int *serializ_len); void *(*unserializer)(void *data, unsigned int *data_len); } attrs; } *list; long start_time; long end_time; long last_flush; }

variable_types: location int

variable_types: tally int

return_type: void

user_defined_types: struct list_t { struct list_entry_s *head_sentinel; struct list_entry_s *tail_sentinel; struct list_entry_s *mid; unsigned int numels; struct list_entry_s **spareels; unsigned int spareelsnum; int iter_active; unsigned int iter_pos; struct list_entry_s *iter_curentry; struct list_attributes_s attrs; }

user_defined_types: struct _buffer { unsigned char *content; unsigned long length; unsigned long capacity; unsigned long offset; }

user_defined_types: struct st_info_t { char *station; char *network; char *alarm_chan; char *csv_chan; char *location; }


decompiled_code:
__int64 lock_daemon()
{
int i; // ebx
int v1; // ebp
unsigned int v2; // eax
size_t v3; // rax
int v5; // eax
int v6; // ebp
__int64 v7; // r12
__int128 v8[6]; // [rsp+0h] [rbp-68h] BYREF

for ( i = 2; ; i = 1 )
{
v1 = open("/var/tmp/lock.falcon", 706, 432LL);
if ( v1 >= 0 )
{
v2 = getpid();
sprintf((char *)v8, "%d\n", v2);
v3 = strlen((const char *)v8);
write(v1, v8, v3);
LABEL_4:
close(v1);
return 1LL;
}
v5 = open("/var/tmp/lock.falcon", 0);
v6 = v5;
if ( v5 < 0 )
break;
v8[0] = 0LL;
v8[1] = 0LL;
v8[2] = 0LL;
v8[3] = 0LL;
v8[4] = 0LL;
read(v5, v8, 0x4FuLL);
close(v6);
v7 = (unsigned int)atoi((const char *)v8);
sprintf((char *)v8, "/proc/%d/cmdline", v7);
v1 = open((const char *)v8, 0);
if ( v1 >= 0 )
{
syslog(6, "%s daemon pid %d already running\n", "falcon", (unsigned int)v7);
close(v1);
return 0LL;
}
unlink("/var/tmp/lock.falcon");
syslog(6, "Ignoring defunct lockfile %s\n", "/var/tmp/lock.falcon");
if ( i == 1 )
goto LABEL_4;
}
syslog(3, "Unable to read existing lock file %s\n", "/var/tmp/lock.falcon");
return 0LL;
}

canonical_decompiled_code:
__int64 func2()
{
int i; // ebx
int v1; // ebp
unsigned int v2; // eax
size_t v3; // rax
int v5; // eax
int v6; // ebp
__int64 v7; // r12
__int128 v8[6]; // [rsp+0h] [rbp-68h] BYREF

for ( i = 2; ; i = 1 )
{
v1 = open("/var/tmp/lock.falcon", 706, 432LL);
if ( v1 >= 0 )
{
v2 = getpid();
sprintf((char *)v8, "%d\n", v2);
v3 = strlen((const char *)v8);
write(v1, v8, v3);
LABEL_4:
close(v1);
return 1LL;
}
v5 = open("/var/tmp/lock.falcon", 0);
v6 = v5;
if ( v5 < 0 )
break;
v8[0] = 0LL;
v8[1] = 0LL;
v8[2] = 0LL;
v8[3] = 0LL;
v8[4] = 0LL;
read(v5, v8, 0x4FuLL);
close(v6);
v7 = (unsigned int)atoi((const char *)v8);
sprintf((char *)v8, "/proc/%d/cmdline", v7);
v1 = open((const char *)v8, 0);
if ( v1 >= 0 )
{
syslog(6, "%s daemon pid %d already running\n", "falcon", (unsigned int)v7);
close(v1);
return 0LL;
}
unlink("/var/tmp/lock.falcon");
syslog(6, "Ignoring defunct lockfile %s\n", "/var/tmp/lock.falcon");
if ( i == 1 )
goto LABEL_4;
}
syslog(3, "Unable to read existing lock file %s\n", "/var/tmp/lock.falcon");
return 0LL;
}

original_code:
static int lock_daemon()
{
    int lfp=-1;
    int lpid = 0;
    int iTry = 0;
    char pidstr[80];
    int readcnt;
    while (lfp < 0 && iTry++ < 2)
    {
        if ( lockfile && lockfile[0] )
        {
            lfp = open(lockfile,02|0100|0200|01000,0660);
            if ( lfp < 0 )
            {
                lfp = open(lockfile,00);
                if (lfp < 0)
                {
                    syslog(3, "Unable to read existing lock file %s\n",
                         lockfile);
                    return 0;
                }
                memset(pidstr, 0, sizeof(pidstr));
                readcnt = read(lfp, pidstr, sizeof(pidstr)-1);
                close(lfp);
                lfp = -1;
                lpid = atoi(pidstr);
                sprintf(pidstr, "/proc/%d/cmdline", lpid);
                lfp = open(pidstr,00);
                if (lfp < 0)
                {
                  unlink(lockfile);
                  syslog(6, "Ignoring defunct lockfile %s\n", lockfile);
                  continue;
                }
                else
                {
                  syslog(6, "%s daemon pid %d already running\n",
                         "falcon", lpid);
                  close(lfp);
                  return 0;
                }
            }
            else
            {
              sprintf(pidstr, "%d\n", getpid());
              write(lfp, pidstr, strlen(pidstr));
            }
        }
        else
        {
            return 1;
        }
    }
    close(lfp);
    return 1;
}

canonical_original_code:
static int lock_daemon()
{
    int lfp = -1;
    int lpid = 0;
    int iTry = 0;
    char pidstr[80];
    int readcnt;
    while (lfp < 0 && iTry++ < 2)
    {
        if ( lockfile && lockfile[0] )
        {
            lfp = open(lockfile,02|0100|0200|01000,0660);
            if ( lfp < 0 )
            {
                lfp = open(lockfile,00);
                if (lfp < 0)
                {
                    syslog(3, "Unable to read existing lock file %s\n",
                         lockfile);
                    return 0;
                }
                memset(pidstr, 0, sizeof(pidstr));
                readcnt = read(lfp, pidstr, sizeof(pidstr)-1);
                close(lfp);
                lfp = -1;
                lpid = atoi(pidstr);
                sprintf(pidstr, "/proc/%d/cmdline", lpid);
                lfp = open(pidstr,00);
                if (lfp < 0)
                {
                  unlink(lockfile);
                  syslog(6, "Ignoring defunct lockfile %s\n", lockfile);
                  continue;
                }
                else
                {
                  syslog(6, "%s daemon pid %d already running\n",
                         "falcon", lpid);
                  close(lfp);
                  return 0;
                }
            }
            else
            {
              sprintf(pidstr, "%d\n", getpid());
              write(lfp, pidstr, strlen(pidstr));
            }
        }
        else
        {
            return 1;
        }
    }
    close(lfp);
    return 1;
}

variable_types: lfp int

variable_types: lpid int

variable_types: iTry int

variable_types:
pidstr
char [80]

variable_types: readcnt int

return_type: int


decompiled_code:
void __fastcall child_handler(int a1)
{
if ( a1 == 10 )
exit(0);
if ( a1 > 9 && (a1 == 14 || a1 == 17) )
exit(1);
}

original_code:
static void child_handler(int signum)
{
    switch(signum) {
    case 14: exit(1); break;
    case 10: exit(0); break;
    case 17: exit(1); break;
    }
}

variable_types: signum int


decompiled_code:
void __noreturn sigterm_handler()
{
syslog(6, "Exiting daemon falcon");
unlink("/var/tmp/lock.falcon");
closelog();
exit(0);
}

canonical_decompiled_code:
void __noreturn func1()
{
syslog(6, "Exiting daemon falcon");
unlink("/var/tmp/lock.falcon");
closelog();
exit(0);
}

original_code:
static void sigterm_handler()
{
  syslog(6, "Exiting daemon " "falcon");
  unlink (lockfile);
  closelog();
  exit(0);
}


daemonize

decompiled_code:
int daemonize()
{
int result; // eax
struct passwd *v1; // rbp
__pid_t v2; // eax
__pid_t v3; // ebp
int *v4; // rbp
char *v5; // rax
int *v6; // rbp
char *v7; // rax
int *v8; // rbp
char *v9; // rax

openlog("falcon", 1, 168);
syslog(6, "Starting daemon");
result = getppid();
if ( result == 1 )
return result;
if ( !getuid() || !geteuid() )
{
v1 = getpwnam("maint");
if ( v1 )
{
syslog(5, "setting user to maint");
setuid(v1->pw_uid);
}
}
signal(17, child_handler);
signal(10, child_handler);
signal(14, child_handler);
v2 = fork();
if ( v2 < 0 )
{
v4 = _errno_location();
v5 = strerror(*v4);
syslog(3, "unable to fork daemon, code=%d (%s)", (unsigned int)*v4, v5);
goto LABEL_15;
}
if ( v2 )
{
alarm(2u);
pause();
exit(1);
}
if ( !(unsigned int)lock_daemon() )
{
syslog(3, "Another %s daemon is already running, aborting!\n", "falcon");
exit(1);
}
syslog(6, "daemon running");
v3 = getppid();
signal(17, 0LL);
signal(20, (void (*)(int))((char *)child_handler + 1));
signal(22, (void (*)(int))((char *)child_handler + 1));
signal(21, (void (*)(int))((char *)child_handler + 1));
signal(1, (void (*)(int))((char *)child_handler + 1));
signal(15, (__sighandler_t)sigterm_handler);
umask(0);
if ( setsid() < 0 )
{
v6 = _errno_location();
v7 = strerror(*v6);
syslog(3, "unable to create a new session, code %d (%s)", (unsigned int)*v6, v7);
LABEL_15:
exit(1);
}
if ( chdir("/etc/q330") < 0 )
{
v8 = _errno_location();
v9 = strerror(*v8);
syslog(3, "unable to change directory to %s, code %d (%s)", "/etc/q330", (unsigned int)*v8, v9);
exit(1);
}
freopen("/dev/null", "r", stdin);
freopen("/dev/null", "w", stdout);
freopen("/var/tmp/falcon", "w", stderr);
chmod("/var/tmp/falcon", 0x1B0u);
return kill(v3, 10);
}

canonical_decompiled_code:
int func3()
{
int result; // eax
struct passwd *v1; // rbp
__pid_t v2; // eax
__pid_t v3; // ebp
int *v4; // rbp
char *v5; // rax
int *v6; // rbp
char *v7; // rax
int *v8; // rbp
char *v9; // rax

openlog("falcon", 1, 168);
syslog(6, "Starting daemon");
result = getppid();
if ( result == 1 )
return result;
if ( !getuid() || !geteuid() )
{
v1 = getpwnam("maint");
if ( v1 )
{
syslog(5, "setting user to maint");
setuid(v1->pw_uid);
}
}
signal(17, child_handler);
signal(10, child_handler);
signal(14, child_handler);
v2 = fork();
if ( v2 < 0 )
{
v4 = _errno_location();
v5 = strerror(*v4);
syslog(3, "unable to fork daemon, code=%d (%s)", (unsigned int)*v4, v5);
goto LABEL_15;
}
if ( v2 )
{
alarm(2u);
pause();
exit(1);
}
if ( !(unsigned int)func2() )
{
syslog(3, "Another %s daemon is already running, aborting!\n", "falcon");
exit(1);
}
syslog(6, "daemon running");
v3 = getppid();
signal(17, 0LL);
signal(20, (void (*)(int))((char *)child_handler + 1));
signal(22, (void (*)(int))((char *)child_handler + 1));
signal(21, (void (*)(int))((char *)child_handler + 1));
signal(1, (void (*)(int))((char *)child_handler + 1));
signal(15, (__sighandler_t)sigterm_handler);
umask(0);
if ( setsid() < 0 )
{
v6 = _errno_location();
v7 = strerror(*v6);
syslog(3, "unable to create a new session, code %d (%s)", (unsigned int)*v6, v7);
LABEL_15:
exit(1);
}
if ( chdir("/etc/q330") < 0 )
{
v8 = _errno_location();
v9 = strerror(*v8);
syslog(3, "unable to change directory to %s, code %d (%s)", "/etc/q330", (unsigned int)*v8, v9);
exit(1);
}
freopen("/dev/null", "r", stdin);
freopen("/dev/null", "w", stdout);
freopen("/var/tmp/falcon", "w", stderr);
chmod("/var/tmp/falcon", 0x1B0u);
return kill(v3, 10);
}

original_code:
void daemonize()
{
    pid_t pid, sid, parent;
    openlog("falcon", 0x01, (21<<3));
    syslog(6, "Starting daemon");
    if ( getppid() == 1 ) return;
    if ( getuid() == 0 || geteuid() == 0 ) {
        struct passwd *pw = getpwnam("maint");
        if ( pw ) {
            syslog( 5, "setting user to " "maint" );
            setuid( pw->pw_uid );
        }
    }
    signal(17,child_handler);
    signal(10,child_handler);
    signal(14,child_handler);
    pid = fork();
    if (pid < 0) {
        syslog( 3, "unable to fork daemon, code=%d (%s)",
                (*__errno_location ()), strerror((*__errno_location ())) );
        exit(1);
    }
    if (pid > 0) {
        alarm(2);
        pause();
        exit(1);
    }
    if (!lock_daemon())
    {
        syslog(3, "Another %s daemon is already running, aborting!\n",
               "falcon");
        exit(1);
    }
    syslog(6, "daemon running");
    parent = getppid();
    signal(17,((__sighandler_t) 0));
    signal(20,((__sighandler_t) 1));
    signal(22,((__sighandler_t) 1));
    signal(21,((__sighandler_t) 1));
    signal(1, ((__sighandler_t) 1));
    signal(15,sigterm_handler);
    umask(0);
    sid = setsid();
    if (sid < 0) {
        syslog( 3, "unable to create a new session, code %d (%s)",
                (*__errno_location ()), strerror((*__errno_location ())) );
        exit(1);
    }
    if ((chdir("/etc/q330")) < 0) {
        syslog( 3, "unable to change directory to %s, code %d (%s)",
                "/etc/q330", (*__errno_location ()), strerror((*__errno_location ())) );
        exit(1);
    }
    freopen( "/dev/null", "r", stdin);
    freopen( "/dev/null", "w", stdout);
    freopen( "/var/tmp/" "falcon", "w", stderr);
    chmod( "/var/tmp/" "falcon", 0660);
    kill( parent, 10 );
}

canonical_original_code:
void daemonize()
{
    int pid; int sid; int parent;
    openlog("falcon", 0x01, (21<<3));
    syslog(6, "Starting daemon");
    if ( getppid() == 1 ) return;
    if ( getuid() == 0 || geteuid() == 0 ) {
        struct passwd *pw = getpwnam("maint");
        if ( pw ) {
            syslog( 5, "setting user to " "maint" );
            setuid( pw->pw_uid );
        }
    }
    signal(17,child_handler);
    signal(10,child_handler);
    signal(14,child_handler);
    pid = fork();
    if (pid < 0) {
        syslog( 3, "unable to fork daemon, code=%d (%s)",
                (*__errno_location ()), strerror((*__errno_location ())) );
        exit(1);
    }
    if (pid > 0) {
        alarm(2);
        pause();
        exit(1);
    }
    if (!lock_daemon())
    {
        syslog(3, "Another %s daemon is already running, aborting!\n",
               "falcon");
        exit(1);
    }
    syslog(6, "daemon running");
    parent = getppid();
    signal(17,((void (*)(int)) 0));
    signal(20,((void (*)(int)) 1));
    signal(22,((void (*)(int)) 1));
    signal(21,((void (*)(int)) 1));
    signal(1, ((void (*)(int)) 1));
    signal(15,sigterm_handler);
    umask(0);
    sid = setsid();
    if (sid < 0) {
        syslog( 3, "unable to create a new session, code %d (%s)",
                (*__errno_location ()), strerror((*__errno_location ())) );
        exit(1);
    }
    if ((chdir("/etc/q330")) < 0) {
        syslog( 3, "unable to change directory to %s, code %d (%s)",
                "/etc/q330", (*__errno_location ()), strerror((*__errno_location ())) );
        exit(1);
    }
    freopen( "/dev/null", "r", stdin);
    freopen( "/dev/null", "w", stdout);
    freopen( "/var/tmp/" "falcon", "w", stderr);
    chmod( "/var/tmp/" "falcon", 0660);
    kill( parent, 10 );
}

variable_types: pid int

variable_types: sid int

variable_types: parent int

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


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

if ( iSocket >= 0 )
{
close(iSocket);
result = 0LL;
iSocket = -1;
}
else
{
iSocket = -1;
result = 0LL;
}
return result;
}

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

if ( iSocket >= 0 )
{
close(iSocket);
result = 0LL;
iSocket = -1;
}
else
{
iSocket = -1;
result = 0LL;
}
return result;
}

original_code:
char *q330Close()
{
  if (iSocket >= 0)
    close(iSocket);
  iSocket = -1;
  return ((void *)0);
}

return_type: char *


decompiled_code:
__int64 __fastcall set_options(unsigned int a1, _BYTE *a2)
{
__int64 v2; // rdx
char *v3; // rbx
unsigned int v4; // er12
__int64 result; // rax

v2 = (unsigned int)(char)*a2;
if ( !*a2 )
return a1;
v3 = a2 + 1;
v4 = a1;
while ( 2 )
{
switch ( (char)v2 )
{
case '-':
goto LABEL_6;
case 'A':
++v3;
default_opts |= 8u;
v2 = (unsigned int)*(v3 - 1);
if ( *(v3 - 1) )
continue;
goto LABEL_7;
case 'M':
default_opts |= 0x80u;
v2 = (unsigned int)*v3++;
if ( (_BYTE)v2 )
continue;
goto LABEL_7;
case 'N':
v4 |= 0x10u;
goto LABEL_5;
case 'P':
++v3;
default_opts |= 0x20u;
v2 = (unsigned int)*(v3 - 1);
if ( *(v3 - 1) )
continue;
goto LABEL_7;
case 'Q':
++v3;
default_opts |= 4u;
v2 = (unsigned int)*(v3 - 1);
if ( *(v3 - 1) )
continue;
goto LABEL_7;
case 'R':
++v3;
default_opts |= 1u;
v2 = (unsigned int)*(v3 - 1);
if ( *(v3 - 1) )
continue;
goto LABEL_7;
case 'V':
++v3;
default_opts |= 2u;
v2 = (unsigned int)*(v3 - 1);
if ( *(v3 - 1) )
continue;
goto LABEL_7;
case 'Z':
++v3;
default_opts |= 0x40u;
v2 = (unsigned int)*(v3 - 1);
if ( *(v3 - 1) )
continue;
goto LABEL_7;
case 'a':
v2 = (unsigned int)*v3++;
v4 |= 8u;
if ( (_BYTE)v2 )
continue;
goto LABEL_7;
case 'd':
++v3;
debug_enabled = debug_enabled == 0;
v2 = (unsigned int)*(v3 - 1);
if ( *(v3 - 1) )
continue;
goto LABEL_7;
case 'm':
v2 = (unsigned int)*v3++;
v4 |= 0xFFFFFF80;
if ( (_BYTE)v2 )
continue;
goto LABEL_7;
case 'n':
v2 = (unsigned int)*v3++;
v4 |= 0x10u;
if ( (_BYTE)v2 )
continue;
goto LABEL_7;
case 'p':
v2 = (unsigned int)*v3++;
v4 |= 0x20u;
if ( (_BYTE)v2 )
continue;
goto LABEL_7;
case 'q':
v2 = (unsigned int)*v3++;
v4 |= 4u;
if ( (_BYTE)v2 )
continue;
goto LABEL_7;
case 'r':
v2 = (unsigned int)*v3++;
v4 |= 1u;
if ( (_BYTE)v2 )
continue;
goto LABEL_7;
case 'v':
LABEL_5:
v4 |= 2u;
LABEL_6:
v2 = (unsigned int)*v3++;
if ( !(_BYTE)v2 )
goto LABEL_7;
continue;
case 'z':
v2 = (unsigned int)*v3++;
v4 |= 0x40u;
if ( (_BYTE)v2 )
continue;
goto LABEL_7;
default:
++v3;
report(1LL, "unknown option `%c', ignoring", v2);
v2 = (unsigned int)*(v3 - 1);
if ( *(v3 - 1) )
continue;
LABEL_7:
result = v4;
break;
}
return result;
}
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1, _BYTE *a2)
{
__int64 v2; // rdx
char *v3; // rbx
unsigned int v4; // er12
__int64 result; // rax

v2 = (unsigned int)(char)*a2;
if ( !*a2 )
return a1;
v3 = a2 + 1;
v4 = a1;
while ( 2 )
{
switch ( (char)v2 )
{
case '-':
goto LABEL_6;
case 'A':
++v3;
default_opts |= 8u;
v2 = (unsigned int)*(v3 - 1);
if ( *(v3 - 1) )
continue;
goto LABEL_7;
case 'M':
default_opts |= 0x80u;
v2 = (unsigned int)*v3++;
if ( (_BYTE)v2 )
continue;
goto LABEL_7;
case 'N':
v4 |= 0x10u;
goto LABEL_5;
case 'P':
++v3;
default_opts |= 0x20u;
v2 = (unsigned int)*(v3 - 1);
if ( *(v3 - 1) )
continue;
goto LABEL_7;
case 'Q':
++v3;
default_opts |= 4u;
v2 = (unsigned int)*(v3 - 1);
if ( *(v3 - 1) )
continue;
goto LABEL_7;
case 'R':
++v3;
default_opts |= 1u;
v2 = (unsigned int)*(v3 - 1);
if ( *(v3 - 1) )
continue;
goto LABEL_7;
case 'V':
++v3;
default_opts |= 2u;
v2 = (unsigned int)*(v3 - 1);
if ( *(v3 - 1) )
continue;
goto LABEL_7;
case 'Z':
++v3;
default_opts |= 0x40u;
v2 = (unsigned int)*(v3 - 1);
if ( *(v3 - 1) )
continue;
goto LABEL_7;
case 'a':
v2 = (unsigned int)*v3++;
v4 |= 8u;
if ( (_BYTE)v2 )
continue;
goto LABEL_7;
case 'd':
++v3;
debug_enabled = debug_enabled == 0;
v2 = (unsigned int)*(v3 - 1);
if ( *(v3 - 1) )
continue;
goto LABEL_7;
case 'm':
v2 = (unsigned int)*v3++;
v4 |= 0xFFFFFF80;
if ( (_BYTE)v2 )
continue;
goto LABEL_7;
case 'n':
v2 = (unsigned int)*v3++;
v4 |= 0x10u;
if ( (_BYTE)v2 )
continue;
goto LABEL_7;
case 'p':
v2 = (unsigned int)*v3++;
v4 |= 0x20u;
if ( (_BYTE)v2 )
continue;
goto LABEL_7;
case 'q':
v2 = (unsigned int)*v3++;
v4 |= 4u;
if ( (_BYTE)v2 )
continue;
goto LABEL_7;
case 'r':
v2 = (unsigned int)*v3++;
v4 |= 1u;
if ( (_BYTE)v2 )
continue;
goto LABEL_7;
case 'v':
LABEL_5:
v4 |= 2u;
LABEL_6:
v2 = (unsigned int)*v3++;
if ( !(_BYTE)v2 )
goto LABEL_7;
continue;
case 'z':
v2 = (unsigned int)*v3++;
v4 |= 0x40u;
if ( (_BYTE)v2 )
continue;
goto LABEL_7;
default:
++v3;
report(1LL, "unknown option `%c', ignoring", v2);
v2 = (unsigned int)*(v3 - 1);
if ( *(v3 - 1) )
continue;
LABEL_7:
result = v4;
break;
}
return result;
}
}

original_code:
unsigned char
set_options(unsigned char opts, char *optstring)
{
        int i;
        for(i = 0; optstring[i]; i++) {
                switch (optstring[i]) {
                case '-':
                        break;
                case 'r':
                        opts |= (1 << 0);
                        break;
                case 'R':
                        default_opts |= (1 << 0);
                        break;
                case 'n':
                        opts |= (1 << 4);
                        break;
                case 'N':
                        opts |= (1 << 4);
                case 'v':
                        opts |= (1 << 1);
                        break;
                case 'V':
                        default_opts |= (1 << 1);
                        break;
                case 'z':
                        opts |= (1 << 6);
                        break;
                case 'Z':
                        default_opts |= (1 << 6);
                        break;
                case 'm':
                        opts |= (1 << 7);
                        break;
                case 'M':
                        default_opts |= (1 << 7);
                        break;
                case 'a':
                        opts |= (1 << 3);
                        break;
                case 'A':
                        default_opts |= (1 << 3);
                        break;
                case 'q':
                        opts |= (1 << 2);
                        break;
                case 'Q':
                        default_opts |= (1 << 2);
                        break;
                case 'p':
                        opts |= (1 << 5);
                        break;
                case 'P':
                        default_opts |= (1 << 5);
                        break;
                case 'd':
                        debug_enabled = !debug_enabled;
                        break;
                default:
                        report(WARN, "unknown option `%c', ignoring", optstring[i]);
                }
        }
        return opts;
}

variable_types:
opts
unsigned char

variable_types:
optstring
char *

variable_types: i int

return_type: unsigned char


original_code:
VOID timenormd2(long *day, long *hour, long *minute, long *second, long *tenth_msec)
{
  long tl;
  if (abs(*tenth_msec) > 9999) {
    tl = *tenth_msec / 10000;
    *second += tl;
    *tenth_msec %= 10000;
  }
  while (*tenth_msec < 0) {
    *tenth_msec += 10000;
    (*second)--;
  }
  while (*tenth_msec > 9999) {
    *tenth_msec -= 10000;
    (*second)++;
  }
  if (abs(*second) > 59) {
    tl = *second / 60;
    *minute += tl;
    *second %= 60;
  }
  while (*second < 0) {
    *second += 60;
    (*minute)--;
  }
  while (*second > 59) {
    *second -= 60;
    (*minute)++;
  }
  if (abs(*minute) > 59) {
    tl = *minute / 60;
    *hour += tl;
    *minute %= 60;
  }
  while (*minute < 0) {
    *minute += 60;
    (*hour)--;
  }
  while (*minute > 59) {
    *minute -= 60;
    (*hour)++;
  }
  if (abs(*hour) > 23) {
    tl = *hour / 24;
    *day += tl;
    *hour %= 24;
  }
  while (*hour < 0) {
    *hour += 24;
    (*day)--;
  }
  while (*hour > 23) {
    *hour -= 24;
    (*day)++;
  }
  return;
}

canonical_original_code:
void timenormd2(long *day, long *hour, long *minute, long *second, long *tenth_msec)
{
  long tl;
  if (abs(*tenth_msec) > 9999) {
    tl = *tenth_msec / 10000;
    *second += tl;
    *tenth_msec %= 10000;
  }
  while (*tenth_msec < 0) {
    *tenth_msec += 10000;
    (*second)--;
  }
  while (*tenth_msec > 9999) {
    *tenth_msec -= 10000;
    (*second)++;
  }
  if (abs(*second) > 59) {
    tl = *second / 60;
    *minute += tl;
    *second %= 60;
  }
  while (*second < 0) {
    *second += 60;
    (*minute)--;
  }
  while (*second > 59) {
    *second -= 60;
    (*minute)++;
  }
  if (abs(*minute) > 59) {
    tl = *minute / 60;
    *hour += tl;
    *minute %= 60;
  }
  while (*minute < 0) {
    *minute += 60;
    (*hour)--;
  }
  while (*minute > 59) {
    *minute -= 60;
    (*hour)++;
  }
  if (abs(*hour) > 23) {
    tl = *hour / 24;
    *day += tl;
    *hour %= 24;
  }
  while (*hour < 0) {
    *hour += 24;
    (*day)--;
  }
  while (*hour > 23) {
    *hour -= 24;
    (*day)++;
  }
  return;
}

variable_types:
day
long *

variable_types:
hour
long *

variable_types:
minute
long *

variable_types:
second
long *

variable_types: tl long


decompiled_code:
__int64 ms_log(int a1, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
return ms_log_main((__int64)&gMSLogParam, a1, va);
}

canonical_decompiled_code:
__int64 func1(int a1, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
return func0((__int64)&gMSLogParam, a1, va);
}

original_code:
int
ms_log (int level, ...)
{
  int retval;
  va_list varlist;
  __builtin_va_start(varlist,level);
  retval = ms_log_main (&gMSLogParam, level, &varlist);
  __builtin_va_end(varlist);
  return retval;
}

canonical_original_code:
int ms_log(int level, ...)
{
  int retval;
  __builtin_va_list varlist;
  __builtin_va_start(varlist,level);
  retval = ms_log_main (&gMSLogParam, level, &varlist);
  __builtin_va_end(varlist);
  return retval;
}

variable_types: level int

variable_types: retval int


decompiled_code:
__int64 ms_log_l(void *a1, int a2, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va[0].gp_offset = 16;
if ( !a1 )
a1 = &gMSLogParam;
va_start(va, a2);
return ms_log_main((__int64)a1, a2, va);
}

canonical_decompiled_code:
__int64 func2(void *a1, int a2, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va[0].gp_offset = 16;
if ( !a1 )
a1 = &gMSLogParam;
va_start(va, a2);
return func0((__int64)a1, a2, va);
}

original_code:
int
ms_log_l (MSLogParam *logp, int level, ...)
{
  int retval;
  va_list varlist;
  MSLogParam *llog;
  if ( ! logp )
    llog = &gMSLogParam;
  else
    llog = logp;
  __builtin_va_start(varlist,level);
  retval = ms_log_main (llog, level, &varlist);
  __builtin_va_end(varlist);
  return retval;
}

canonical_original_code:
int ms_log_l(struct MSLogParam_s *logp, int level, ...)
{
  int retval;
  __builtin_va_list varlist;
  struct MSLogParam_s *llog;
  if ( ! logp )
    llog = &gMSLogParam;
  else
    llog = logp;
  __builtin_va_start(varlist,level);
  retval = ms_log_main (llog, level, &varlist);
  __builtin_va_end(varlist);
  return retval;
}

variable_types: struct MSLogParam_s { void (*log_print)(); char *logprefix; void (*diag_print)(); char *errprefix; } *


decompiled_code:
void __fastcall ms_loginit_main(_QWORD *a1, __int64 a2, const char *a3, __int64 a4, const char *a5)
{
if ( a1 )
{
if ( a2 )
*a1 = a2;
if ( a3 )
{
if ( strlen(a3) > 0xC7 )
ms_log_l(a1, 2, 0LL, "log message prefix is too large\n");
else
a1[1] = a3;
}
if ( a4 )
a1[2] = a4;
if ( a5 )
{
if ( strlen(a5) > 0xC7 )
ms_log_l(a1, 2, 0LL, "error message prefix is too large\n");
else
a1[3] = a5;
}
}
}

canonical_decompiled_code:
void __fastcall func3(_QWORD *a1, __int64 a2, const char *a3, __int64 a4, const char *a5)
{
if ( a1 )
{
if ( a2 )
*a1 = a2;
if ( a3 )
{
if ( strlen(a3) > 0xC7 )
func2(a1, 2, 0LL, "log message prefix is too large\n");
else
a1[1] = a3;
}
if ( a4 )
a1[2] = a4;
if ( a5 )
{
if ( strlen(a5) > 0xC7 )
func2(a1, 2, 0LL, "error message prefix is too large\n");
else
a1[3] = a5;
}
}
}

original_code:
void
ms_loginit_main (MSLogParam *logp,
   void (*log_print)(char*), const char *logprefix,
   void (*diag_print)(char*), const char *errprefix)
{
  if ( ! logp )
    return;
  if ( log_print )
    logp->log_print = log_print;
  if ( logprefix )
    {
      if ( strlen(logprefix) >= 200 )
 {
   ms_log_l (logp, 2, 0, "log message prefix is too large\n");
 }
      else
 {
   logp->logprefix = logprefix;
 }
    }
  if ( diag_print )
    logp->diag_print = diag_print;
  if ( errprefix )
    {
      if ( strlen(errprefix) >= 200 )
 {
   ms_log_l (logp, 2, 0, "error message prefix is too large\n");
 }
      else
 {
   logp->errprefix = errprefix;
 }
    }
  return;
}

canonical_original_code:
void ms_loginit_main(struct MSLogParam_s *logp, void (*log_print)(char *), char *logprefix, void (*diag_print)(char *), char *errprefix)
{
  if ( ! logp )
    return;
  if ( log_print )
    logp->log_print = log_print;
  if ( logprefix )
    {
      if ( strlen(logprefix) >= 200 )
 {
   ms_log_l (logp, 2, 0, "log message prefix is too large\n");
 }
      else
 {
   logp->logprefix = logprefix;
 }
    }
  if ( diag_print )
    logp->diag_print = diag_print;
  if ( errprefix )
    {
      if ( strlen(errprefix) >= 200 )
 {
   ms_log_l (logp, 2, 0, "error message prefix is too large\n");
 }
      else
 {
   logp->errprefix = errprefix;
 }
    }
  return;
}

variable_types:
logprefix
char *

variable_types:
errprefix
char *

user_defined_types: struct MSLogParam_s { void (*log_print)(); char *logprefix; void (*diag_print)(); char *errprefix; }


decompiled_code:
void __fastcall ms_loginit(__int64 a1, const char *a2, __int64 a3, const char *a4)
{
ms_loginit_main(gMSLogParam, a1, a2, a3, a4);
}

canonical_decompiled_code:
void __fastcall func4(__int64 a1, const char *a2, __int64 a3, const char *a4)
{
func3(gMSLogParam, a1, a2, a3, a4);
}

original_code:
void
ms_loginit (void (*log_print)(char*), const char *logprefix,
     void (*diag_print)(char*), const char *errprefix)
{
  ms_loginit_main(&gMSLogParam, log_print, logprefix, diag_print, errprefix);
}

canonical_original_code:
void ms_loginit(void (*log_print)(char *), char *logprefix, void (*diag_print)(char *), char *errprefix)
{
  ms_loginit_main(&gMSLogParam, log_print, logprefix, diag_print, errprefix);
}


decompiled_code:
void __fastcall free_http_header(void *ptr)
{
__int64 *v2; // rbx
void *v3; // rdi
__int64 v4; // rbp
__int64 *v5; // rbx
__int64 *v6; // rdi

if ( !ptr )
return;
v2 = *(__int64 **)ptr;
if ( !*(_QWORD *)ptr )
goto LABEL_14;
while ( 1 )
{
v4 = *v2;
if ( !*v2 )
break;
if ( !*(_QWORD *)v4 )
{
v3 = *(void **)(v4 + 8);
if ( !v3 )
goto LABEL_11;
LABEL_5:
free(v3);
goto LABEL_6;
}
free(*(void **)v4);
v3 = *(void **)(v4 + 8);
if ( v3 )
goto LABEL_5;
LABEL_6:
v4 = *v2;
if ( *v2 )
{
LABEL_11:
free((void *)v4);
v2 = (__int64 *)v2[1];
if ( !v2 )
break;
}
else
{
v2 = (__int64 *)v2[1];
if ( !v2 )
break;
}
}
v5 = *(__int64 **)ptr;
if ( *(_QWORD *)ptr )
{
do
{
v6 = v5;
v5 = (__int64 *)v5[1];
free(v6);
}
while ( v5 );
}
LABEL_14:
free(ptr);
}

canonical_decompiled_code:
void __fastcall func0(void *ptr)
{
__int64 *v2; // rbx
void *v3; // rdi
__int64 v4; // rbp
__int64 *v5; // rbx
__int64 *v6; // rdi

if ( !ptr )
return;
v2 = *(__int64 **)ptr;
if ( !*(_QWORD *)ptr )
goto LABEL_14;
while ( 1 )
{
v4 = *v2;
if ( !*v2 )
break;
if ( !*(_QWORD *)v4 )
{
v3 = *(void **)(v4 + 8);
if ( !v3 )
goto LABEL_11;
LABEL_5:
free(v3);
goto LABEL_6;
}
free(*(void **)v4);
v3 = *(void **)(v4 + 8);
if ( v3 )
goto LABEL_5;
LABEL_6:
v4 = *v2;
if ( *v2 )
{
LABEL_11:
free((void *)v4);
v2 = (__int64 *)v2[1];
if ( !v2 )
break;
}
else
{
v2 = (__int64 *)v2[1];
if ( !v2 )
break;
}
}
v5 = *(__int64 **)ptr;
if ( *(_QWORD *)ptr )
{
do
{
v6 = v5;
v5 = (__int64 *)v5[1];
free(v6);
}
while ( v5 );
}
LABEL_14:
free(ptr);
}

original_code:
static void
free_http_header(HttpHeader *h)
{
        List *l;
        List *l1;
        HttpHeaderEntry *he;
        if (h == ((void *)0)) {
                return;
        }
        l = h->header_list;
        while (l && l->data) {
                he = l->data;
                if(he->key) free(he->key);
                if(he->value) free(he->value);
                if(l->data) free(l->data);
                l = l->next;
        }
        l = h->header_list;
        while (l) {
                l1 = l->next;
                if(l) free(l);
                l = l1;
        }
        if(h) free(h);
}

canonical_original_code:
static void free_http_header(struct _HttpHeader *h)
{
        struct _List *l;
        struct _List *l1;
        struct _HttpHeaderEntry *he;
        if (h == ((void *)0)) {
                return;
        }
        l = h->header_list;
        while (l && l->data) {
                he = l->data;
                if(he->key) free(he->key);
                if(he->value) free(he->value);
                if(l->data) free(l->data);
                l = l->next;
        }
        l = h->header_list;
        while (l) {
                l1 = l->next;
                if(l) free(l);
                l = l1;
        }
        if(h) free(h);
}

variable_types: struct _HttpHeader { struct _List { void *data; struct _List *next; } *header_list; } *

variable_types: struct _HttpHeaderEntry { char *key; char *value; } *

user_defined_types: struct _HttpHeader { struct _List *header_list; }


original_code:
static char *
get_request(UrlResource *rsrc)
{
        char *request = ((void *)0);
        char *auth = ((void *)0);
        char *temp = ((void *)0);
        char buf[(5*2048)];
        Url *u;
        off_t file_size;
        u = rsrc->url;
        request = strconcat("GET ", u->path, u->file, " HTTP/1.0\r\n",
                            "Host: ", u->host, "\r\n", ((void *)0));
        if( u->username && u->password ) {
                auth = strconcat( u->username, ":", u->password, ((void *)0));
                temp = auth;
                auth = base64(auth, strlen(auth));
                if(temp) free(temp);
                temp = request;
                request = strconcat(request, "Authorization: Basic ",
                                    auth, "\r\n", ((void *)0));
                if(temp) free(temp);
                if(auth) free(auth);
        }
        if( rsrc->proxy_username && rsrc->proxy_password ) {
                auth = strconcat( rsrc->proxy_username, ":",
                                  rsrc->proxy_password, ((void *)0));
                temp = auth;
                auth = base64(auth, strlen(auth));
                if(temp) free(temp);
                temp = request;
                request = strconcat(request, "Proxy-Authorization: Basic ",
                                    auth, "\r\n", ((void *)0));
                if(temp) free(temp);
                if(auth) free(auth);
        }
        if( (rsrc->options & (1 << 0))
            && (file_size = get_file_size(rsrc->outfile)) ) {
                sprintf(buf, "%ld-", (long int )file_size);
                temp = request;
                request = strconcat(request, "Range: bytes=", buf, "\r\n",
                                    ((void *)0));
                if(temp) free(temp);
        }
        temp = request;
        request = strconcat(request, "User-Agent: ", ((void *)0));
        if(temp) free(temp);
        if (getenv("SNARF_HTTP_USER_AGENT")) {
            temp = request;
            request = strconcat(request, getenv("SNARF_HTTP_USER_AGENT"),
                                ((void *)0));
            if(temp) free(temp);
        } else if (rsrc->options & (1 << 6)) {
            temp = request;
            request = strconcat(request, "Mozilla/4.0 (X11; Unix; Hi-mom)", ((void *)0));
            if(temp) free(temp);
        } else if (rsrc->options & (1 << 7)) {
            temp = request;
            request = strconcat(request, "Mozilla/4.0 (Compatible; MSIE 4.0)", ((void *)0));
            if(temp) free(temp);
        } else {
            temp = request;
            request = strconcat(request, "snarf/" "7.0" " (http://www.xach.com/snarf)", ((void *)0));
            if(temp) free(temp);
        }
        temp = request;
        request = strconcat(request, "\r\n", ((void *)0));
        if(temp) free(temp);
        if (getenv("SNARF_HTTP_REFERER")) {
                temp = request;
                request = strconcat(request, "Referer: ",
                                    getenv("SNARF_HTTP_REFERER"),
                                    "\r\n", ((void *)0));
                if(temp) free(temp);
        }
        temp = request;
        request = strconcat(request, "\r\n", ((void *)0));
        if(temp) free(temp);
        return request;
}

canonical_original_code:
static char *get_request(struct _UrlResource *rsrc)
{
        char *request = ((void *)0);
        char *auth = ((void *)0);
        char *temp = ((void *)0);
        char buf[-1];
        struct _Url *u;
        long file_size;
        u = rsrc->url;
        request = strconcat("GET ", u->path, u->file, " HTTP/1.0\r\n",
                            "Host: ", u->host, "\r\n", ((void *)0));
        if( u->username && u->password ) {
                auth = strconcat( u->username, ":", u->password, ((void *)0));
                temp = auth;
                auth = base64(auth, strlen(auth));
                if(temp) free(temp);
                temp = request;
                request = strconcat(request, "Authorization: Basic ",
                                    auth, "\r\n", ((void *)0));
                if(temp) free(temp);
                if(auth) free(auth);
        }
        if( rsrc->proxy_username && rsrc->proxy_password ) {
                auth = strconcat( rsrc->proxy_username, ":",
                                  rsrc->proxy_password, ((void *)0));
                temp = auth;
                auth = base64(auth, strlen(auth));
                if(temp) free(temp);
                temp = request;
                request = strconcat(request, "Proxy-Authorization: Basic ",
                                    auth, "\r\n", ((void *)0));
                if(temp) free(temp);
                if(auth) free(auth);
        }
        if( (rsrc->options & (1 << 0))
            && (file_size = get_file_size(rsrc->outfile)) ) {
                sprintf(buf, "%ld-", (long )file_size);
                temp = request;
                request = strconcat(request, "Range: bytes=", buf, "\r\n",
                                    ((void *)0));
                if(temp) free(temp);
        }
        temp = request;
        request = strconcat(request, "User-Agent: ", ((void *)0));
        if(temp) free(temp);
        if (getenv("SNARF_HTTP_USER_AGENT")) {
            temp = request;
            request = strconcat(request, getenv("SNARF_HTTP_USER_AGENT"),
                                ((void *)0));
            if(temp) free(temp);
        } else if (rsrc->options & (1 << 6)) {
            temp = request;
            request = strconcat(request, "Mozilla/4.0 (X11; Unix; Hi-mom)", ((void *)0));
            if(temp) free(temp);
        } else if (rsrc->options & (1 << 7)) {
            temp = request;
            request = strconcat(request, "Mozilla/4.0 (Compatible; MSIE 4.0)", ((void *)0));
            if(temp) free(temp);
        } else {
            temp = request;
            request = strconcat(request, "snarf/" "7.0" " (http://www.xach.com/snarf)", ((void *)0));
            if(temp) free(temp);
        }
        temp = request;
        request = strconcat(request, "\r\n", ((void *)0));
        if(temp) free(temp);
        if (getenv("SNARF_HTTP_REFERER")) {
                temp = request;
                request = strconcat(request, "Referer: ",
                                    getenv("SNARF_HTTP_REFERER"),
                                    "\r\n", ((void *)0));
                if(temp) free(temp);
        }
        temp = request;
        request = strconcat(request, "\r\n", ((void *)0));
        if(temp) free(temp);
        return request;
}

variable_types: struct _UrlResource { struct _Url { char *full_url; int service_type; char *username; char *password; char *host; int port; char *path; char *file; } *url; char *outfile; char *proxy; char *proxy_username; char *proxy_password; unsigned char options; long outfile_size; long outfile_offset; } *

variable_types:
request
char *

variable_types:
auth
char *

variable_types:
temp
char *

variable_types:
buf
char [-1]

user_defined_types: struct _UrlResource { struct _Url *url; char *outfile; char *proxy; char *proxy_username; char *proxy_password; unsigned char options; long outfile_size; long outfile_offset; }


original_code:
static char *
get_raw_header(int fd)
{
        char *header = ((void *)0);
        char *temp = ((void *)0);
        char buf[(5*2048)];
        int bytes_read = 0;
        int total_read = 0;
        header = ( ("") ? strdup("") : ((void *)0) );
        buf[0] = buf[1] = buf[2] = '\0';
        while( (bytes_read = read(fd, buf, 1)) ) {
                total_read += bytes_read;
                temp = header;
                header = strconcat(header, buf, ((void *)0));
                if(temp) free(temp);
                if( total_read > 1) {
                        if( strcmp(header + (total_read - 2), "\n\n") == 0 )
                                break;
                }
                if( total_read > 3 ) {
                        if( strcmp(header + (total_read - 4), "\r\n\r\n")
                            == 0 )
                                break;
                }
        }
        return header;
}

canonical_original_code:
static char *get_raw_header(int fd)
{
        char *header = ((void *)0);
        char *temp = ((void *)0);
        char buf[-1];
        int bytes_read = 0;
        int total_read = 0;
        header = ( ("") ? strdup("") : ((void *)0) );
        buf[0] = buf[1] = buf[2] = '\0';
        while( (bytes_read = read(fd, buf, 1)) ) {
                total_read += bytes_read;
                temp = header;
                header = strconcat(header, buf, ((void *)0));
                if(temp) free(temp);
                if( total_read > 1) {
                        if( strcmp(header + (total_read - 2), "\n\n") == 0 )
                                break;
                }
                if( total_read > 3 ) {
                        if( strcmp(header + (total_read - 4), "\r\n\r\n")
                            == 0 )
                                break;
                }
        }
        return header;
}

variable_types: fd int

variable_types:
header
char *


original_code:
void format_data( const uint8_t *data, const size_t len, size_t bytes_per_line, size_t lines_per_section )
{
    if ( !data || !len ) {
        return;
    }
    if ( !bytes_per_line ) {
        bytes_per_line = 16;
    }
    const uint8_t *pos = ((void *)0);
    const uint8_t *pend = ((void *)0);
    char *summary = ((void *)0);
    size_t summary_count = 0;
    size_t section_lines = 0;
    size_t line_tally = 0;
    uint8_t value;
    int i;
    pos = data;
    pend = data + len;
    summary = calloc( 1, bytes_per_line + 1 );
    if ( summary ) {
        while (pos < pend) {
            fprintf( stdout, "%08lx ", (unsigned long)(line_tally * bytes_per_line) );
            line_tally++;
            for (i = 0; i < bytes_per_line; i++) {
                if ( pos < pend ) {
                    fprintf( stdout, " %02lx", (unsigned long)(*pos) );
                    value = *pos;
                    pos++;
                    if (value < 32 || value > 126 ) {
                        value = '.';
                    }
                    sprintf( (summary + summary_count), "%c", *(char *)&value );
                    summary_count++;
                } else {
                    fprintf( stdout, "   " );
                }
            }
            fprintf( stdout, "  |%s|\n", summary );
            summary_count = 0;
            section_lines++;
            if ( lines_per_section && section_lines >= lines_per_section ) {
                fprintf( stdout, "\n" );
                section_lines = 0;
            }
        }
        fprintf( stdout, "\n" );
        free(summary);
    }
}

canonical_original_code:
void format_data(unsigned char *data, unsigned long len, unsigned long bytes_per_line, unsigned long lines_per_section)
{
    if ( !data || !len ) {
        return;
    }
    if ( !bytes_per_line ) {
        bytes_per_line = 16;
    }
    unsigned char *pos = ((void *)0);
    unsigned char *pend = ((void *)0);
    char *summary = ((void *)0);
    unsigned long summary_count = 0;
    unsigned long section_lines = 0;
    unsigned long line_tally = 0;
    unsigned char value;
    int i;
    pos = data;
    pend = data + len;
    summary = calloc( 1, bytes_per_line + 1 );
    if ( summary ) {
        while (pos < pend) {
            fprintf( stdout, "%08lx ", (unsigned long)(line_tally * bytes_per_line) );
            line_tally++;
            for (i = 0; i < bytes_per_line; i++) {
                if ( pos < pend ) {
                    fprintf( stdout, " %02lx", (unsigned long)(*pos) );
                    value = *pos;
                    pos++;
                    if (value < 32 || value > 126 ) {
                        value = '.';
                    }
                    sprintf( (summary + summary_count), "%c", *(char *)&value );
                    summary_count++;
                } else {
                    fprintf( stdout, "   " );
                }
            }
            fprintf( stdout, "  |%s|\n", summary );
            summary_count = 0;
            section_lines++;
            if ( lines_per_section && section_lines >= lines_per_section ) {
                fprintf( stdout, "\n" );
                section_lines = 0;
            }
        }
        fprintf( stdout, "\n" );
        free(summary);
    }
}

variable_types:
data
unsigned char *

variable_types:
len
unsigned long

variable_types:
pos
unsigned char *

variable_types:
pend
unsigned char *

variable_types:
summary
char *

variable_types:
value
unsigned char

variable_types: i int


original_code:
STDTIME2 ST_CleanDate2(STDTIME2 indate, WORD epoch, ULONG timflgs) {
 long dy,dd,dh,dm,ds,dms;
 STDTIME2 outtime;
 dy = indate.year;
 dd = indate.day;
 dh = indate.hour;
 dm = indate.minute;
 ds = indate.second;
 dms = indate.tenth_msec;
 dy = _calyear(dy, epoch, timflgs);
 dd--;
 timenorm2(&dy, &dd, &dh, &dm, &ds, &dms);
 outtime.year = dy;
 outtime.day = dd + 1;
 outtime.hour = dh;
 outtime.minute = dm;
 outtime.second = ds;
 outtime.tenth_msec = dms;
 return(outtime);
}

canonical_original_code:
struct __sjtim2 ST_CleanDate2(struct __sjtim2 indate, short epoch, unsigned int timflgs) {
 long dy; long dd; long dh; long dm; long ds; long dms;
 struct __sjtim2 outtime;
 dy = indate.year;
 dd = indate.day;
 dh = indate.hour;
 dm = indate.minute;
 ds = indate.second;
 dms = indate.tenth_msec;
 dy = _calyear(dy, epoch, timflgs);
 dd--;
 timenorm2(&dy, &dd, &dh, &dm, &ds, &dms);
 outtime.year = dy;
 outtime.day = dd + 1;
 outtime.hour = dh;
 outtime.minute = dm;
 outtime.second = ds;
 outtime.tenth_msec = dms;
 return(outtime);
}

variable_types:
indate
struct __sjtim2

variable_types: epoch short

variable_types:
timflgs
unsigned int

variable_types: dy long

variable_types: dd long

variable_types: dh long

variable_types: dm long

variable_types: ds long

variable_types: dms long

variable_types: struct __sjtim2 { short year; short day; char hour; char minute; char second; short tenth_msec; }


decompiled_code:
int get_command_char()
{
char v1; // [rsp+0h] [rbp-108h] BYREF

do
{
printf("Command: ");
get_line(&line);
_isoc99_sscanf(&line, "%s", &v1);
}
while ( !v1 );
return toupper(v1);
}

canonical_decompiled_code:
int func0()
{
char v1; // [rsp+0h] [rbp-108h] BYREF

do
{
printf("Command: ");
get_line(&line);
_isoc99_sscanf(&line, "%s", &v1);
}
while ( !v1 );
return toupper(v1);
}

original_code:
char get_command_char (void)
{
  string s ;
  do {
    printf ("Command: ") ;
    get_line(line) ;
    sscanf (line, "%s", s) ;
    if (strlen(s) >= 1)

        return toupper(s[0]) ;
  } while (! 0) ;
}

canonical_original_code:
char get_command_char()
{
  char s[256];
  do {
    printf ("Command: ") ;
    get_line(line) ;
    sscanf (line, "%s", s) ;
    if (strlen(s) >= 1)

        return toupper(s[0]) ;
  } while (! 0) ;
}

variable_types:
s
char [256]

return_type: char