server

decompiled_code:
__int64 __fastcall server(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5)
{
unsigned __int16 (__fastcall *v5)(_QWORD); // rbp
unsigned __int16 v6; // ax
_QWORD *v7; // rbp
int v8; // eax
__int64 v9; // r14
unsigned __int16 i; // bp
__int64 v11; // rax
const char *v12; // rax
char *v13; // r13
int v14; // eax
__int64 result; // rax
const char *v16; // rax
int v17; // eax
int *v18; // rax
char *v19; // rax
int v20; // edi
char *v21; // rax
__int64 v22; // rbp
int *v23; // rax
char *v24; // rax
int *v25; // rax
char *v26; // rax
__int64 v27[2]; // [rsp+0h] [rbp-58h] BYREF
__int64 v28[9]; // [rsp+10h] [rbp-48h] BYREF

v27[0] = (__int64)command_delegate_filter;
v28[1] = db_driver_delegate_filter;
v27[1] = 0LL;
v28[0] = (__int64)command_delegate_filter;
v28[2] = 0LL;
command_delegate_init(a1, a2, a3, a4, a5);
v5 = (unsigned __int16 (__fastcall *)(_QWORD))db_driver_initialize;
v6 = delegate_get_count();
if ( !v5(v6) )
return lo(2LL, "server: error initializing database driver");
if ( (unsigned int)delegate_connect() == -1 )
{
v18 = _errno_location();
v19 = strerror(*v18);
return lo(2LL, "server: error connecting to a delegate: %s", v19);
}
while ( 2 )
{
if ( (unsigned __int16)db_driver_done() )
{
delegate_disconnect();
result = lo(0LL, "server: finished work on fd %d", (unsigned int)a1);
}
else
{
while ( (unsigned __int16)db_driver_expect_commands() )
{
v7 = (_QWORD *)packet_new();
if ( !v7 )
{
lo(2LL, "server: out of memory!");
return delegate_disconnect();
}
lo(0LL, "server: waiting for next command...");
if ( (unsigned int)read_command(a1, v7, db_driver_get_packet) == -1 )
{
v20 = *_errno_location();
if ( v20 == 104 || v20 == 115 )
{
lo(0LL, "server: client went away");
}
else
{
v21 = strerror(v20);
lo(2LL, "server: error reading command: %s", v21);
}
goto LABEL_32;
}
command_delegate_all();
v8 = db_driver_command(v7);
if ( v8 == 1 )
{
v16 = (const char *)db_driver_table_extract(v7);
v13 = (char *)v16;
if ( !v16 )
{
lo(2LL, "server: error extracting table");
goto LABEL_32;
}
lo(2LL, "server: table '%s'", v16);
v17 = sql_get_table_type(v13);
if ( !v17 )
goto LABEL_38;
if ( v17 == 1 )
{
command_delegate_random_partition();
free(v13);
goto LABEL_10;
}
}
else
{
if ( v8 == 2 )
{
lo(2LL, "server: got unsupported command");
goto LABEL_32;
}
if ( v8 )
goto LABEL_10;
v12 = (const char *)db_driver_sql_extract(v7);
v13 = (char *)v12;
if ( !v12 )
{
lo(2LL, "server: error extracting SQL");
LABEL_32:
packet_delete(v7);
return delegate_disconnect();
}
lo(0LL, "server: query '%s'", v12);
v14 = sql_get_type(v13);
if ( !v14 )
{
LABEL_38:
command_delegate_master();
free(v13);
goto LABEL_10;
}
if ( v14 == 1 && !sql_get_map_keys(v13) )
{
lo(1LL, "server: XX: not doing the right thing");
command_delegate_all();
}
}
free(v13);
LABEL_10:
lo(0LL, "server: delegating command...");
if ( !(unsigned int)delegate_put(v27, db_driver_put_packet, db_driver_rewrite_command, v7) )
{
lo(2LL, "server: error delegating command");
packet_delete(v7);
return delegate_disconnect();
}
packet_delete(v7);
}
db_driver_command_done(v27);
while ( (unsigned __int16)db_driver_expect_replies() )
{
lo(0LL, "server: waiting for reply...");
v9 = delegate_get(v28, db_driver_get_packet);
if ( !v9 )
{
lo(2LL, "server: error getting delegate replies");
return delegate_disconnect();
}
for ( i = 0; (unsigned __int16)delegate_get_count() > i; ++i )
{
v11 = packet_set_get(v9, i);
if ( v11 )
{
if ( *(_DWORD *)(v11 + 12) )
db_driver_reply(i, v11);
}
}
if ( !(unsigned __int16)db_driver_got_error() )
{
v7 = (_QWORD *)db_driver_reduce_replies(v9);
lo(0LL, "server: returning reply...");
if ( (unsigned int)send_reply(a1, (__int64)v7, db_driver_put_packet) == -1 )
{
v23 = _errno_location();
v24 = strerror(*v23);
lo(2LL, "server: error sending reply: %s", v24);
packet_set_delete(v9);
goto LABEL_32;
}
packet_delete(v7);
}
packet_set_delete(v9);
}
if ( !(unsigned __int16)db_driver_got_error() )
{
LABEL_49:
lo(0LL, "server: done with this conversation.");
continue;
}
v22 = db_driver_error_packet();
if ( (unsigned int)send_reply(a1, v22, db_driver_put_packet) != -1 )
{
packet_delete(v22);
goto LABEL_49;
}
v25 = _errno_location();
v26 = strerror(*v25);
lo(2LL, "server: error sending reply: %s", v26);
result = packet_delete(v22);
}
return result;
}
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5)
{
unsigned __int16 (__fastcall *v5)(_QWORD); // rbp
unsigned __int16 v6; // ax
_QWORD *v7; // rbp
int v8; // eax
__int64 v9; // r14
unsigned __int16 i; // bp
__int64 v11; // rax
const char *v12; // rax
char *v13; // r13
int v14; // eax
__int64 result; // rax
const char *v16; // rax
int v17; // eax
int *v18; // rax
char *v19; // rax
int v20; // edi
char *v21; // rax
__int64 v22; // rbp
int *v23; // rax
char *v24; // rax
int *v25; // rax
char *v26; // rax
__int64 v27[2]; // [rsp+0h] [rbp-58h] BYREF
__int64 v28[9]; // [rsp+10h] [rbp-48h] BYREF

v27[0] = (__int64)command_delegate_filter;
v28[1] = db_driver_delegate_filter;
v27[1] = 0LL;
v28[0] = (__int64)command_delegate_filter;
v28[2] = 0LL;
func4(a1, a2, a3, a4, a5);
v5 = (unsigned __int16 (__fastcall *)(_QWORD))db_driver_initialize;
v6 = delegate_get_count();
if ( !v5(v6) )
return lo(2LL, "server: error initializing database driver");
if ( (unsigned int)delegate_connect() == -1 )
{
v18 = _errno_location();
v19 = strerror(*v18);
return lo(2LL, "server: error connecting to a delegate: %s", v19);
}
while ( 2 )
{
if ( (unsigned __int16)db_driver_done() )
{
delegate_disconnect();
result = lo(0LL, "server: finished work on fd %d", (unsigned int)a1);
}
else
{
while ( (unsigned __int16)db_driver_expect_commands() )
{
v7 = (_QWORD *)packet_new();
if ( !v7 )
{
lo(2LL, "server: out of memory!");
return delegate_disconnect();
}
lo(0LL, "server: waiting for next command...");
if ( (unsigned int)func1(a1, v7, db_driver_get_packet) == -1 )
{
v20 = *_errno_location();
if ( v20 == 104 || v20 == 115 )
{
lo(0LL, "server: client went away");
}
else
{
v21 = strerror(v20);
lo(2LL, "server: error reading command: %s", v21);
}
goto LABEL_32;
}
func3();
v8 = db_driver_command(v7);
if ( v8 == 1 )
{
v16 = (const char *)db_driver_table_extract(v7);
v13 = (char *)v16;
if ( !v16 )
{
lo(2LL, "server: error extracting table");
goto LABEL_32;
}
lo(2LL, "server: table '%s'", v16);
v17 = sql_get_table_type(v13);
if ( !v17 )
goto LABEL_38;
if ( v17 == 1 )
{
func6();
free(v13);
goto LABEL_10;
}
}
else
{
if ( v8 == 2 )
{
lo(2LL, "server: got unsupported command");
goto LABEL_32;
}
if ( v8 )
goto LABEL_10;
v12 = (const char *)db_driver_sql_extract(v7);
v13 = (char *)v12;
if ( !v12 )
{
lo(2LL, "server: error extracting SQL");
LABEL_32:
packet_delete(v7);
return delegate_disconnect();
}
lo(0LL, "server: query '%s'", v12);
v14 = sql_get_type(v13);
if ( !v14 )
{
LABEL_38:
func5();
free(v13);
goto LABEL_10;
}
if ( v14 == 1 && !sql_get_map_keys(v13) )
{
lo(1LL, "server: XX: not doing the right thing");
func3();
}
}
free(v13);
LABEL_10:
lo(0LL, "server: delegating command...");
if ( !(unsigned int)delegate_put(v27, db_driver_put_packet, db_driver_rewrite_command, v7) )
{
lo(2LL, "server: error delegating command");
packet_delete(v7);
return delegate_disconnect();
}
packet_delete(v7);
}
db_driver_command_done(v27);
while ( (unsigned __int16)db_driver_expect_replies() )
{
lo(0LL, "server: waiting for reply...");
v9 = delegate_get(v28, db_driver_get_packet);
if ( !v9 )
{
lo(2LL, "server: error getting delegate replies");
return delegate_disconnect();
}
for ( i = 0; (unsigned __int16)delegate_get_count() > i; ++i )
{
v11 = packet_set_get(v9, i);
if ( v11 )
{
if ( *(_DWORD *)(v11 + 12) )
db_driver_reply(i, v11);
}
}
if ( !(unsigned __int16)db_driver_got_error() )
{
v7 = (_QWORD *)db_driver_reduce_replies(v9);
lo(0LL, "server: returning reply...");
if ( (unsigned int)func0(a1, (__int64)v7, db_driver_put_packet) == -1 )
{
v23 = _errno_location();
v24 = strerror(*v23);
lo(2LL, "server: error sending reply: %s", v24);
packet_set_delete(v9);
goto LABEL_32;
}
packet_delete(v7);
}
packet_set_delete(v9);
}
if ( !(unsigned __int16)db_driver_got_error() )
{
LABEL_49:
lo(0LL, "server: done with this conversation.");
continue;
}
v22 = db_driver_error_packet();
if ( (unsigned int)func0(a1, v22, db_driver_put_packet) != -1 )
{
packet_delete(v22);
goto LABEL_49;
}
v25 = _errno_location();
v26 = strerror(*v25);
lo(2LL, "server: error sending reply: %s", v26);
result = packet_delete(v22);
}
return result;
}
}

original_code:
void server(int fd, struct sockaddr_in *addr)
{
    delegate_filter put_filters[] = { command_delegate_filter, 0 };
    delegate_filter get_filters[] =
        { command_delegate_filter, db_driver_delegate_filter, 0 };
    command_delegate_init();
    if (!db_driver_initialize(delegate_get_count())) {
        lo(LOG_ERROR, "server: error initializing database driver");
        return;
    }
    if (delegate_connect() == -1) {
        lo(LOG_ERROR, "server: error connecting to a delegate: %s",
           strerror((*__errno_location ())));
        return;
    }
    while (!db_driver_done()) {
        while (db_driver_expect_commands()) {
            packet *in_command = packet_new();
            if (!in_command) {
                lo(LOG_ERROR, "server: out of memory!");
                delegate_disconnect();
                return;
            }
            lo(LOG_DEBUG, "server: waiting for next command...");
            if (read_command(fd, in_command, db_driver_get_packet) == -1) {
                if (((*__errno_location ()) != 104) && ((*__errno_location ()) != 115)) {
                    lo(LOG_ERROR, "server: error reading command: %s",
                       strerror((*__errno_location ())));
                } else {
                    lo(LOG_DEBUG, "server: client went away");
                }
                packet_delete(in_command);
                delegate_disconnect();
                return;
            }
            command_delegate_all();
            switch (db_driver_command(in_command)) {
            case DB_DRIVER_COMMAND_TYPE_SQL:
                {
                    char *sql = db_driver_sql_extract(in_command);
                    if (!sql) {
                        lo(LOG_ERROR, "server: error extracting SQL");
                        packet_delete(in_command);
                        delegate_disconnect();
                        return;
                    }
                    lo(LOG_DEBUG, "server: query '%s'", sql);
                    switch (sql_get_type(sql)) {
                    case SQL_TYPE_MASTER:
                        command_delegate_master();
                        break;
                    case SQL_TYPE_PARTITIONED:
                        {
                            long *foo = sql_get_map_keys(sql);
                            if (foo) {
                            } else {
                                lo(LOG_INFO,
                                   "server: XX: not doing the right thing");
                                command_delegate_all();
                            }
                            break;
                        }
                    }
                    free(sql);
                    break;
                }
            case DB_DRIVER_COMMAND_TYPE_TABLE_META:
                {
                    char *table = db_driver_table_extract(in_command);
                    if (!table) {
                        lo(LOG_ERROR, "server: error extracting table");
                        packet_delete(in_command);
                        delegate_disconnect();
                        return;
                    }
                    lo(LOG_ERROR, "server: table '%s'", table);
                    switch (sql_get_table_type(table)) {
                    case SQL_TABLE_TYPE_MASTER:
                        command_delegate_master();
                        break;
                    case SQL_TABLE_TYPE_PARTITIONED:
                        command_delegate_random_partition();
                        break;
                    }
                    free(table);
                    break;
                }
            case DB_DRIVER_COMMAND_TYPE_UNSUPPORTED:
                {
                    lo(LOG_ERROR, "server: got unsupported command");
                    packet_delete(in_command);
                    delegate_disconnect();
                    return;
                }
            case DB_DRIVER_COMMAND_TYPE_OTHER:
                break;
            };
            lo(LOG_DEBUG, "server: delegating command...");
            if (!delegate_put(put_filters, db_driver_put_packet,
                              db_driver_rewrite_command, in_command)) {
                lo(LOG_ERROR, "server: error delegating command");
                packet_delete(in_command);
                delegate_disconnect();
                return;
            }
            packet_delete(in_command);
        }
        db_driver_command_done(put_filters);
        while (db_driver_expect_replies()) {
            lo(LOG_DEBUG, "server: waiting for reply...");
            packet_set *replies = delegate_get(get_filters,
                                               db_driver_get_packet);
            if (!replies) {
                lo(LOG_ERROR, "server: error getting delegate replies");
                delegate_disconnect();
                return;
            }
            for (delegate_id i = 0; i < delegate_get_count(); ++i) {
                packet *p = packet_set_get(replies, i);
                if ((p) && (p->size)) {
                    db_driver_reply(i, p);
                }
            }
            if (!db_driver_got_error()) {
                packet *final_reply = db_driver_reduce_replies(replies);
                lo(LOG_DEBUG, "server: returning reply...");
                if (send_reply(fd, final_reply, db_driver_put_packet) == -1) {
                    lo(LOG_ERROR, "server: error sending reply: %s",
                       strerror((*__errno_location ())));
                    packet_set_delete(replies);
                    packet_delete(final_reply);
                    delegate_disconnect();
                    return;
                }
                packet_delete(final_reply);
            }
            packet_set_delete(replies);
        }
        if (db_driver_got_error()) {
            packet *error = db_driver_error_packet();
            if (send_reply(fd, error, db_driver_put_packet) == -1) {
                lo(LOG_ERROR, "server: error sending reply: %s",
                   strerror((*__errno_location ())));
                packet_delete(error);
                return;
            }
            packet_delete(error);
        }
        lo(LOG_DEBUG, "server: done with this conversation.");
    }
    delegate_disconnect();
    lo(LOG_DEBUG, "server: finished work on fd %d", fd);
    return;
}

canonical_original_code:
void server(int fd, struct sockaddr_in *addr)
{
    enum delegate_filter_result (*put_filters[])(unsigned short) = { command_delegate_filter, 0 };
    enum delegate_filter_result (*get_filters[])(unsigned short) = { command_delegate_filter, db_driver_delegate_filter, 0 };
    command_delegate_init();
    if (!db_driver_initialize(delegate_get_count())) {
        lo(LOG_ERROR, "server: error initializing database driver");
        return;
    }
    if (delegate_connect() == -1) {
        lo(LOG_ERROR, "server: error connecting to a delegate: %s",
           strerror((*__errno_location ())));
        return;
    }
    while (!db_driver_done()) {
        while (db_driver_expect_commands()) {
            struct packet *in_command = packet_new();
            if (!in_command) {
                lo(LOG_ERROR, "server: out of memory!");
                delegate_disconnect();
                return;
            }
            lo(LOG_DEBUG, "server: waiting for next command...");
            if (read_command(fd, in_command, db_driver_get_packet) == -1) {
                if (((*__errno_location ()) != 104) && ((*__errno_location ()) != 115)) {
                    lo(LOG_ERROR, "server: error reading command: %s",
                       strerror((*__errno_location ())));
                } else {
                    lo(LOG_DEBUG, "server: client went away");
                }
                packet_delete(in_command);
                delegate_disconnect();
                return;
            }
            command_delegate_all();
            switch (db_driver_command(in_command)) {
            case DB_DRIVER_COMMAND_TYPE_SQL:
                {
                    char *sql = db_driver_sql_extract(in_command);
                    if (!sql) {
                        lo(LOG_ERROR, "server: error extracting SQL");
                        packet_delete(in_command);
                        delegate_disconnect();
                        return;
                    }
                    lo(LOG_DEBUG, "server: query '%s'", sql);
                    switch (sql_get_type(sql)) {
                    case SQL_TYPE_MASTER:
                        command_delegate_master();
                        break;
                    case SQL_TYPE_PARTITIONED:
                        {
                            long *foo = sql_get_map_keys(sql);
                            if (foo) {
                            } else {
                                lo(LOG_INFO,
                                   "server: XX: not doing the right thing");
                                command_delegate_all();
                            }
                            break;
                        }
                    }
                    free(sql);
                    break;
                }
            case DB_DRIVER_COMMAND_TYPE_TABLE_META:
                {
                    char *table = db_driver_table_extract(in_command);
                    if (!table) {
                        lo(LOG_ERROR, "server: error extracting table");
                        packet_delete(in_command);
                        delegate_disconnect();
                        return;
                    }
                    lo(LOG_ERROR, "server: table '%s'", table);
                    switch (sql_get_table_type(table)) {
                    case SQL_TABLE_TYPE_MASTER:
                        command_delegate_master();
                        break;
                    case SQL_TABLE_TYPE_PARTITIONED:
                        command_delegate_random_partition();
                        break;
                    }
                    free(table);
                    break;
                }
            case DB_DRIVER_COMMAND_TYPE_UNSUPPORTED:
                {
                    lo(LOG_ERROR, "server: got unsupported command");
                    packet_delete(in_command);
                    delegate_disconnect();
                    return;
                }
            case DB_DRIVER_COMMAND_TYPE_OTHER:
                break;
            };
            lo(LOG_DEBUG, "server: delegating command...");
            if (!delegate_put(put_filters, db_driver_put_packet,
                              db_driver_rewrite_command, in_command)) {
                lo(LOG_ERROR, "server: error delegating command");
                packet_delete(in_command);
                delegate_disconnect();
                return;
            }
            packet_delete(in_command);
        }
        db_driver_command_done(put_filters);
        while (db_driver_expect_replies()) {
            lo(LOG_DEBUG, "server: waiting for reply...");
            struct packet_set *replies = delegate_get(get_filters,
                                               db_driver_get_packet);
            if (!replies) {
                lo(LOG_ERROR, "server: error getting delegate replies");
                delegate_disconnect();
                return;
            }
            for (unsigned short i = 0; i < delegate_get_count(); ++i) {
                struct packet *p = packet_set_get(replies, i);
                if ((p) && (p->size)) {
                    db_driver_reply(i, p);
                }
            }
            if (!db_driver_got_error()) {
                struct packet *final_reply = db_driver_reduce_replies(replies);
                lo(LOG_DEBUG, "server: returning reply...");
                if (send_reply(fd, final_reply, db_driver_put_packet) == -1) {
                    lo(LOG_ERROR, "server: error sending reply: %s",
                       strerror((*__errno_location ())));
                    packet_set_delete(replies);
                    packet_delete(final_reply);
                    delegate_disconnect();
                    return;
                }
                packet_delete(final_reply);
            }
            packet_set_delete(replies);
        }
        if (db_driver_got_error()) {
            struct packet *error = db_driver_error_packet();
            if (send_reply(fd, error, db_driver_put_packet) == -1) {
                lo(LOG_ERROR, "server: error sending reply: %s",
                   strerror((*__errno_location ())));
                packet_delete(error);
                return;
            }
            packet_delete(error);
        }
        lo(LOG_DEBUG, "server: done with this conversation.");
    }
    delegate_disconnect();
    lo(LOG_DEBUG, "server: finished work on fd %d", fd);
    return;
}

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

variable_types: enum delegate_filter_result { DELEGATE_FILTER_USE=0, DELEGATE_FILTER_DONT_USE=1 } (*[])(unsigned short)

variable_types:
sql
char *

variable_types:
foo
long *

variable_types:
table
char *

variable_types: struct packet_set { struct packet { char *bytes; int allocated; int size; } *packets; int count; } *

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

user_defined_types: struct packet_set { struct packet *packets; int count; }


repo: dlowe/pdb

decompiled_code:
void concurrency_setup()
{
;
}

canonical_decompiled_code:
void func0()
{
;
}

original_code:
void concurrency_setup(void)
{
}

canonical_original_code:
void concurrency_setup()
{
}

return_type: void


decompiled_code:
__pid_t concurrency_teardown()
{
int *v0; // rbx
__pid_t result; // eax
__WAIT_STATUS stat_loc; // [rsp+Ch] [rbp-Ch] BYREF

v0 = _errno_location();
do
{
while ( 1 )
{
result = wait3((__WAIT_STATUS)&stat_loc, 0, 0LL);
if ( result <= 0 )
break;
lo(0LL, "concurrency_teardown: joined pid %d", (unsigned int)result);
}
}
while ( !result || *v0 != 10 );
return result;
}

canonical_decompiled_code:
__pid_t func1()
{
int *v0; // rbx
__pid_t result; // eax
__WAIT_STATUS stat_loc; // [rsp+Ch] [rbp-Ch] BYREF

v0 = _errno_location();
do
{
while ( 1 )
{
result = wait3((__WAIT_STATUS)&stat_loc, 0, 0LL);
if ( result <= 0 )
break;
lo(0LL, "concurrency_teardown: joined pid %d", (unsigned int)result);
}
}
while ( !result || *v0 != 10 );
return result;
}

original_code:
void concurrency_teardown(void)
{
    int status;
    pid_t pid;
    do {
        pid = wait3(&status, 0, 0);
        if (pid > 0) {
            lo(LOG_DEBUG, "concurrency_teardown: joined pid %d", pid);
        }
    } while (pid > -1 || (*__errno_location ()) != 10);
}

canonical_original_code:
void concurrency_teardown()
{
    int status;
    int pid;
    do {
        pid = wait3(&status, 0, 0);
        if (pid > 0) {
            lo(LOG_DEBUG, "concurrency_teardown: joined pid %d", pid);
        }
    } while (pid > -1 || (*__errno_location ()) != 10);
}

variable_types: status int

variable_types: pid int


decompiled_code:
__pid_t __fastcall concurrency_handle_connection(int fd, __int64 a2, void (__fastcall *a3)(_QWORD, __int64))
{
__pid_t result; // eax

result = fork();
if ( result != -1 )
{
if ( !result )
{
_sysv_signal(15, 0LL);
log_reopen();
lo(0LL, "concurrency_handle_connection: handling connection on fd %d", (unsigned int)fd);
a3((unsigned int)fd, a2);
shutdown(fd, 2);
close(fd);
lo(0LL, "concurrency_handle_connection: finished work on fd %d", (unsigned int)fd);
exit(0);
}
close(fd);
result = 0;
}
return result;
}

canonical_decompiled_code:
__pid_t __fastcall func2(int fd, __int64 a2, void (__fastcall *a3)(_QWORD, __int64))
{
__pid_t result; // eax

result = fork();
if ( result != -1 )
{
if ( !result )
{
_sysv_signal(15, 0LL);
log_reopen();
lo(0LL, "concurrency_handle_connection: handling connection on fd %d", (unsigned int)fd);
a3((unsigned int)fd, a2);
shutdown(fd, 2);
close(fd);
lo(0LL, "concurrency_handle_connection: finished work on fd %d", (unsigned int)fd);
exit(0);
}
close(fd);
result = 0;
}
return result;
}

original_code:
int concurrency_handle_connection(int connection_fd,
                                  struct sockaddr_in *connection_addr,
                                  void (*handler) (int, struct sockaddr_in *))
{
    pid_t child_pid = fork();
    switch (child_pid) {
    case -1:
        return -1;
    case 0:
        signal(15, ((__sighandler_t) 0));
        log_reopen();
        lo(LOG_DEBUG, "concurrency_handle_connection: handling connection on "
           "fd %d", connection_fd);
        handler(connection_fd, connection_addr);
        shutdown(connection_fd, SHUT_RDWR);
        close(connection_fd);
        lo(LOG_DEBUG, "concurrency_handle_connection: finished work on fd %d",
           connection_fd);
        exit(0);
    }
    close(connection_fd);
    return 0;
}

canonical_original_code:
int concurrency_handle_connection(int connection_fd, struct sockaddr_in *connection_addr, void (*handler)(int, struct sockaddr_in *))
{
    int child_pid = fork();
    switch (child_pid) {
    case -1:
        return -1;
    case 0:
        signal(15, ((void (*)(int)) 0));
        log_reopen();
        lo(LOG_DEBUG, "concurrency_handle_connection: handling connection on "
           "fd %d", connection_fd);
        handler(connection_fd, connection_addr);
        shutdown(connection_fd, SHUT_RDWR);
        close(connection_fd);
        lo(LOG_DEBUG, "concurrency_handle_connection: finished work on fd %d",
           connection_fd);
        exit(0);
    }
    close(connection_fd);
    return 0;
}

variable_types:
handler
void (*)(int, struct sockaddr_in *)

return_type: int


decompiled_code:
__pid_t concurrency_join_finished()
{
__pid_t result; // eax
__WAIT_STATUS stat_loc; // [rsp+Ch] [rbp-Ch] BYREF

while ( 1 )
{
result = wait3((__WAIT_STATUS)&stat_loc, 1, 0LL);
if ( result <= 0 )
break;
lo(0LL, "concurrency_join_finished: joined pid %d", (unsigned int)result);
}
return result;
}

canonical_decompiled_code:
__pid_t func3()
{
__pid_t result; // eax
__WAIT_STATUS stat_loc; // [rsp+Ch] [rbp-Ch] BYREF

while ( 1 )
{
result = wait3((__WAIT_STATUS)&stat_loc, 1, 0LL);
if ( result <= 0 )
break;
lo(0LL, "concurrency_join_finished: joined pid %d", (unsigned int)result);
}
return result;
}

original_code:
void concurrency_join_finished(void)
{
    int status;
    pid_t pid;
    do {
        pid = wait3(&status, 1, 0);
        if (pid > 0) {
            lo(LOG_DEBUG, "concurrency_join_finished: joined pid %d", pid);
        }
    } while (pid > 0);
}

canonical_original_code:
void concurrency_join_finished()
{
    int status;
    int pid;
    do {
        pid = wait3(&status, 1, 0);
        if (pid > 0) {
            lo(LOG_DEBUG, "concurrency_join_finished: joined pid %d", pid);
        }
    } while (pid > 0);
}


decompiled_code:
void __fastcall packet_initialize(_QWORD *a1)
{
*a1 = 0LL;
a1[1] = 0LL;
}

canonical_decompiled_code:
void __fastcall func0(_QWORD *a1)
{
*a1 = 0LL;
a1[1] = 0LL;
}

original_code:
static void packet_initialize(packet * p)
{
    p->bytes = 0;
    p->allocated = 0;
    p->size = 0;
}

canonical_original_code:
static void packet_initialize(struct packet *p)
{
    p->bytes = 0;
    p->allocated = 0;
    p->size = 0;
}

variable_types: struct packet { char *bytes; int allocated; int size; } *


decompiled_code:
void __fastcall packet_free_bytes(void **a1)
{
void *v2; // rdi

v2 = *a1;
if ( v2 )
{
free(v2);
packet_initialize(a1);
}
}

canonical_decompiled_code:
void __fastcall func1(void **a1)
{
void *v2; // rdi

v2 = *a1;
if ( v2 )
{
free(v2);
func0(a1);
}
}

original_code:
static void packet_free_bytes(packet * p)
{
    if (p->bytes) {
        free(p->bytes);
        packet_initialize(p);
    }
}

canonical_original_code:
static void packet_free_bytes(struct packet *p)
{
    if (p->bytes) {
        free(p->bytes);
        packet_initialize(p);
    }
}

user_defined_types: struct packet { char *bytes; int allocated; int size; }


decompiled_code:
void packet_new()
{
_QWORD *v0; // rax

v0 = malloc(0x10uLL);
if ( v0 )
packet_initialize(v0);
}

canonical_decompiled_code:
void func2()
{
_QWORD *v0; // rax

v0 = malloc(0x10uLL);
if ( v0 )
func0(v0);
}

original_code:
packet *packet_new(void)
{
    packet *p = malloc(sizeof(packet));
    if (!p) {
        return 0;
    }
    packet_initialize(p);
    return p;
}

canonical_original_code:
struct packet *packet_new()
{
    struct packet *p = malloc(sizeof(packet));
    if (!p) {
        return 0;
    }
    packet_initialize(p);
    return p;
}

return_type: struct packet { char *bytes; int allocated; int size; } *


decompiled_code:
void __fastcall packet_delete(void *ptr)
{
if ( ptr )
{
packet_free_bytes((void **)ptr);
free(ptr);
}
}

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

original_code:
void packet_delete(packet * p)
{
    if (p) {
        packet_free_bytes(p);
        free(p);
    }
}

canonical_original_code:
void packet_delete(struct packet *p)
{
    if (p) {
        packet_free_bytes(p);
        free(p);
    }
}


decompiled_code:
__int64 __fastcall packet_set_get(_QWORD *a1, unsigned __int16 a2)
{
return *a1 + 16LL * a2;
}

canonical_decompiled_code:
__int64 __fastcall func4(_QWORD *a1, unsigned __int16 a2)
{
return *a1 + 16LL * a2;
}

original_code:
packet *packet_set_get(packet_set * p, delegate_id index)
{
    return &p->packets[index];
}

canonical_original_code:
struct packet *packet_set_get(struct packet_set *p, unsigned short index)
{
    return &p->packets[index];
}

variable_types: struct packet_set { struct packet *packets; int count; } *

variable_types:
index
unsigned short


decompiled_code:
void __fastcall packet_set_delete(void *ptr)
{
int v2; // eax
__int64 v3; // rbx
__int64 v4; // rdi

if ( ptr )
{
v2 = *((_DWORD *)ptr + 2);
if ( v2 )
{
if ( v2 > 0 )
{
v3 = 0LL;
do
{
v4 = v3++;
packet_free_bytes((void **)(*(_QWORD *)ptr + 16 * v4));
}
while ( *((_DWORD *)ptr + 2) > (int)v3 );
}
free(*(void **)ptr);
}
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func5(void *ptr)
{
int v2; // eax
__int64 v3; // rbx
__int64 v4; // rdi

if ( ptr )
{
v2 = *((_DWORD *)ptr + 2);
if ( v2 )
{
if ( v2 > 0 )
{
v3 = 0LL;
do
{
v4 = v3++;
func1((void **)(*(_QWORD *)ptr + 16 * v4));
}
while ( *((_DWORD *)ptr + 2) > (int)v3 );
}
free(*(void **)ptr);
}
free(ptr);
}
}

original_code:
void packet_set_delete(packet_set * p)
{
    if (p) {
        if (p->count) {
            for (int i = 0; i < p->count; ++i) {
                packet_free_bytes(&p->packets[i]);
            }
            free(p->packets);
            p->count = 0;
        }
        free(p);
    }
}

canonical_original_code:
void packet_set_delete(struct packet_set *p)
{
    if (p) {
        if (p->count) {
            for (int i = 0; i < p->count; ++i) {
                packet_free_bytes(&p->packets[i]);
            }
            free(p->packets);
            p->count = 0;
        }
        free(p);
    }
}

variable_types: i int


decompiled_code:
void map_shutdown()
{
;
}

canonical_decompiled_code:
void func1()
{
;
}

original_code:
static void map_shutdown(void)
{
}

canonical_original_code:
static void map_shutdown()
{
}


decompiled_code:
__int64 __fastcall delegate_filter_reduce(unsigned int (__fastcall **a1)(_QWORD), unsigned __int16 a2)
{
unsigned int (__fastcall *v2)(_QWORD); // rax
unsigned int (__fastcall **v3)(_QWORD); // rbx

if ( !a1 )
return 0LL;
v2 = *a1;
if ( !*a1 )
return 0LL;
v3 = a1 + 1;
do
{
if ( v2(a2) )
return 1LL;
v2 = *v3++;
}
while ( v2 );
return 0LL;
}

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

if ( !a1 )
return 0LL;
v2 = *a1;
if ( !*a1 )
return 0LL;
v3 = a1 + 1;
do
{
if ( v2(a2) )
return 1LL;
v2 = *v3++;
}
while ( v2 );
return 0LL;
}

original_code:
delegate_filter_result delegate_filter_reduce(delegate_filter * filters,
                                              delegate_id id)
{
    delegate_filter_result r = DELEGATE_FILTER_USE;
    if (filters != ((void *)0)) {
        int n = 0;
        while (filters[n] != ((void *)0)) {
            if (filters[n] (id)) {
                r = DELEGATE_FILTER_DONT_USE;
                break;
            }
            ++n;
        }
    }
    return r;
}

canonical_original_code:
enum delegate_filter_result delegate_filter_reduce(enum delegate_filter_result (**filters)(unsigned short), unsigned short id)
{
    enum delegate_filter_result r = DELEGATE_FILTER_USE;
    if (filters != ((void *)0)) {
        int n = 0;
        while (filters[n] != ((void *)0)) {
            if (filters[n] (id)) {
                r = DELEGATE_FILTER_DONT_USE;
                break;
            }
            ++n;
        }
    }
    return r;
}

variable_types: enum delegate_filter_result { DELEGATE_FILTER_USE=0, DELEGATE_FILTER_DONT_USE=1 } (**)(unsigned short)

variable_types:
id
unsigned short

variable_types: n int


repo: dlutxx/apue

decompiled_code:
void __fastcall __noreturn err_sys(const char *a1)
{
perror(a1);
exit(127);
}

canonical_decompiled_code:
void __fastcall __noreturn func0(const char *a1)
{
perror(a1);
exit(127);
}

original_code:
void err_sys(char* s)
{
    perror(s);
    exit(127);
}

canonical_original_code:
void err_sys(char *s)
{
    perror(s);
    exit(127);
}

variable_types:
s
char *


xlog

decompiled_code:
int xlog(char *format, ...)
{
FILE *v2; // rdi
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

v2 = fp;
if ( !fp )
{
fp = fopen("/var/tmp/xlog.log", "a+");
v2 = fp;
if ( !fp )
exit(120);
}
va_start(arg, format);
vfprintf(v2, format, arg);
fputc(10, fp);
return fflush(fp);
}

canonical_decompiled_code:
int func1(char *format, ...)
{
FILE *v2; // rdi
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

v2 = fp;
if ( !fp )
{
fp = fopen("/var/tmp/xlog.log", "a+");
v2 = fp;
if ( !fp )
exit(120);
}
va_start(arg, format);
vfprintf(v2, format, arg);
fputc(10, fp);
return fflush(fp);
}

original_code:
void xlog(char* fmt, ...)
{
    if (((void *)0) == fp && (fp=fopen("/var/tmp/xlog.log", "a+")) == ((void *)0))
        exit(120);
    va_list args;
    __builtin_va_start(args,fmt);
    vfprintf(fp, fmt, args);
    fprintf(fp, "\n");
    __builtin_va_end(args);
    fflush(fp);
}

canonical_original_code:
void xlog(char *fmt, ...)
{
    if (((void *)0) == fp && (fp=fopen("/var/tmp/xlog.log", "a+")) == ((void *)0))
        exit(120);
    __builtin_va_list args;
    __builtin_va_start(args,fmt);
    vfprintf(fp, fmt, args);
    fprintf(fp, "\n");
    __builtin_va_end(args);
    fflush(fp);
}

variable_types:
fmt
char *


pr

decompiled_code:
int pr(const char *a1, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a1);
return vprintf(a1, arg);
}

canonical_decompiled_code:
int func2(const char *a1, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a1);
return vprintf(a1, arg);
}

original_code:
void pr(char* fmt, ...)
{
    va_list args;
    __builtin_va_start(args,fmt);
    vprintf(fmt, args);
    __builtin_va_end(args);
}

canonical_original_code:
void pr(char *fmt, ...)
{
    __builtin_va_list args;
    __builtin_va_start(args,fmt);
    vprintf(fmt, args);
    __builtin_va_end(args);
}


lockfile

decompiled_code:
int __fastcall lockfile(int a1)
{
int v2; // [rsp+0h] [rbp-28h] BYREF
__int64 v3; // [rsp+8h] [rbp-20h]
__int64 v4; // [rsp+10h] [rbp-18h]

v2 = 1;
v3 = 0LL;
v4 = 0LL;
return fcntl(a1, 6, &v2);
}

canonical_decompiled_code:
int __fastcall func3(int a1)
{
int v2; // [rsp+0h] [rbp-28h] BYREF
__int64 v3; // [rsp+8h] [rbp-20h]
__int64 v4; // [rsp+10h] [rbp-18h]

v2 = 1;
v3 = 0LL;
v4 = 0LL;
return fcntl(a1, 6, &v2);
}

original_code:
int lockfile(int fd)
{
    struct flock fl;
    fl.l_type = 1;
    fl.l_start = 0;
    fl.l_whence = 0;
    fl.l_len = 0;
    return fcntl(fd, 6, &fl);
}

variable_types: fd int

variable_types: struct flock { short l_type; short l_whence; long l_start; long l_len; int l_pid; }


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__pid_t v3; // eax
int *v4; // rbp
unsigned int v5; // ebx
__pid_t v6; // eax
int v8; // eax
char *v9; // rbx
int *v10; // rdx
__m128i v11; // xmm0
__int64 v12; // rcx
int v13; // eax
char *v14; // rbx
int *v15; // rdx
__m128i v16; // xmm1
__int64 v17; // rcx
int v18; // eax
char *v19; // rdi
__int64 v20; // rcx
int *v21; // rsi
int v22; // eax
char *v23; // rdi
__int64 v24; // rcx
int *v25; // rsi
int v26[16]; // [rsp+0h] [rbp-908h] BYREF
int v27[44]; // [rsp+40h] [rbp-8C8h] BYREF
char v28[572]; // [rsp+F0h] [rbp-818h] BYREF
__m128i v29; // [rsp+32Ch] [rbp-5DCh]
char v30; // [rsp+33Ch] [rbp-5CCh] BYREF
char v31; // [rsp+358h] [rbp-5B0h] BYREF
char v32[1416]; // [rsp+380h] [rbp-588h] BYREF

v26[0] = 3;
netCreateConnections(v26, argv, envp);
v27[0] = 6;
netCreateLinks(v27);
netSetNetworkTopology(v27);
v3 = fork();
if ( v3 != -1 )
{
if ( !v3 )
{
switchInit(v32, 1LL);
v8 = netSwitchInLink(v27, 1LL, 0LL);
if ( v8 != -1 )
{
v9 = v32;
do
{
v9 += 28;
v10 = &v27[7 * v8];
v11 = _mm_loadu_si128((const __m128i *)(v10 + 1));
v12 = *(_QWORD *)(v10 + 5);
LODWORD(v10) = v10[7];
*((_QWORD *)v9 - 1) = v12;
*(_DWORD *)v9 = (_DWORD)v10;
*(__m128i *)(v9 - 24) = v11;
v8 = netSwitchInLink(v27, 1LL, (unsigned int)(v8 + 1));
}
while ( v8 != -1 );
}
v13 = netSwitchOutLink(v27, 1LL, 0LL);
if ( v13 != -1 )
{
v14 = v32;
do
{
v14 += 28;
v15 = &v27[7 * v13];
v16 = _mm_loadu_si128((const __m128i *)(v15 + 1));
v17 = *(_QWORD *)(v15 + 5);
LODWORD(v15) = v15[7];
*(_QWORD *)(v14 + 76) = v17;
*((_DWORD *)v14 + 21) = (_DWORD)v15;
*(__m128i *)(v14 + 60) = v16;
v13 = netSwitchOutLink(v27, 1LL, (unsigned int)(v13 + 1));
}
while ( v13 != -1 );
}
netCloseSwitchOtherLinks(v27, 1LL);
switchMain(v32);
}
v4 = v26;
v5 = 0;
while ( 1 )
{
v6 = fork();
if ( v6 == -1 )
break;
if ( !v6 )
{
hostInit(v28, v5);
v29 = _mm_loadu_si128((const __m128i *)(v4 + 1));
netCloseConnections(v26, v5);
v18 = netHostOutLink(v27, v5);
v19 = &v31;
v20 = 7LL;
v21 = &v27[7 * v18 + 1];
while ( v20 )
{
*(_DWORD *)v19 = *v21++;
v19 += 4;
--v20;
}
v22 = netHostInLink(v27, v5);
v23 = &v30;
v24 = 7LL;
v25 = &v27[7 * v22 + 1];
while ( v24 )
{
*(_DWORD *)v23 = *v25++;
v23 += 4;
--v24;
}
netCloseHostOtherLinks(v27, v5);
hostMain(v28);
}
++v5;
v4 += 4;
if ( v5 == 3 )
{
netCloseLinks(v27);
netCloseManConnections(v26);
manMain(v26);
return kill(0, 9);
}
}
}
return puts("Error: the fork() failed");
}

original_code:
void main()
{
hostState hstate;
switchState sstate;
linkArrayType linkArray;
manLinkArrayType manLinkArray;
pid_t pid;
int physid;
int switchid;
int i;
int k;
manLinkArray.numlinks = 3;
netCreateConnections(& manLinkArray);
linkArray.numlinks = 3*2;
netCreateLinks(& linkArray);
netSetNetworkTopology(& linkArray);
for(switchid=1;switchid<1 +1;switchid++){
 pid = fork();
 if (pid == -1) {
  printf("Error:  the fork() failed\n");
  return;
 }
 else if(pid==0){
  switchInit(&sstate,switchid);
  i = 0;
  k = netSwitchInLink(&linkArray,switchid,0);
  while(k != -1){
   sstate.linkin[i] = linkArray.link[k];
   i++;
   k = netSwitchInLink(&linkArray,switchid,k+1);
  }
  i = 0;
  k = netSwitchOutLink(&linkArray,switchid,0);
  while(k != -1){
   sstate.linkout[i] = linkArray.link[k];
   i++;
   k = netSwitchOutLink(&linkArray,switchid,k+1);
  }
  netCloseSwitchOtherLinks(&linkArray,switchid);
  switchMain(&sstate);
 }
}
for (physid = 0; physid < 3; physid++) {
   pid = fork();
   if (pid == -1) {
      printf("Error:  the fork() failed\n");
      return;
   }
   else if (pid == 0) {
      hostInit(&hstate, physid);
      hstate.manLink = manLinkArray.link[physid];
      netCloseConnections(& manLinkArray, physid);
      k = netHostOutLink(&linkArray, physid);
      hstate.linkout = linkArray.link[k];
      k = netHostInLink(&linkArray, physid);
      hstate.linkin = linkArray.link[k];
      netCloseHostOtherLinks(& linkArray, physid);
      hostMain(&hstate);
   }
}
netCloseLinks(&linkArray);
netCloseManConnections(&manLinkArray);
manMain(& manLinkArray);
kill(0, 9);
}

canonical_original_code:
void main()
{
struct hostState hstate;
struct switchState sstate;
struct linkArrayType linkArray;
struct manLinkArrayType manLinkArray;
int pid;
int physid;
int switchid;
int i;
int k;
manLinkArray.numlinks = 3;
netCreateConnections(& manLinkArray);
linkArray.numlinks = 3*2;
netCreateLinks(& linkArray);
netSetNetworkTopology(& linkArray);
for(switchid=1;switchid<1 +1;switchid++){
 pid = fork();
 if (pid == -1) {
  printf("Error:  the fork() failed\n");
  return;
 }
 else if(pid==0){
  switchInit(&sstate,switchid);
  i = 0;
  k = netSwitchInLink(&linkArray,switchid,0);
  while(k != -1){
   sstate.linkin[i] = linkArray.link[k];
   i++;
   k = netSwitchInLink(&linkArray,switchid,k+1);
  }
  i = 0;
  k = netSwitchOutLink(&linkArray,switchid,0);
  while(k != -1){
   sstate.linkout[i] = linkArray.link[k];
   i++;
   k = netSwitchOutLink(&linkArray,switchid,k+1);
  }
  netCloseSwitchOtherLinks(&linkArray,switchid);
  switchMain(&sstate);
 }
}
for (physid = 0; physid < 3; physid++) {
   pid = fork();
   if (pid == -1) {
      printf("Error:  the fork() failed\n");
      return;
   }
   else if (pid == 0) {
      hostInit(&hstate, physid);
      hstate.manLink = manLinkArray.link[physid];
      netCloseConnections(& manLinkArray, physid);
      k = netHostOutLink(&linkArray, physid);
      hstate.linkout = linkArray.link[k];
      k = netHostInLink(&linkArray, physid);
      hstate.linkin = linkArray.link[k];
      netCloseHostOtherLinks(& linkArray, physid);
      hostMain(&hstate);
   }
}
netCloseLinks(&linkArray);
netCloseManConnections(&manLinkArray);
manMain(& manLinkArray);
kill(0, 9);
}

variable_types: struct hostState { int physid; char maindir[100]; int maindirvalid; int netaddr; int nbraddr; struct packetBuffer { int srcaddr; int dstaddr; int length; char payload[-1]; int valid; int new; int linkIndex; } sendPacketBuff; struct packetBuffer rcvPacketBuff; struct managerLink { int toHost[2]; int fromHost[2]; } manLink; struct LinkInfo { int linkID; enum LinkType { UNIPIPE=0 } linkType; struct UniPipeInfo { enum UniPipeType { BLOCKING=0, NONBLOCKING=1 } pipeType; int fd[2]; int physIdSrc; int physIdDst; } uniPipeInfo; } linkin; struct LinkInfo linkout; int rcvflag; }

variable_types: struct switchState { int switchid; struct LinkInfo { int linkID; enum LinkType { UNIPIPE=0 } linkType; struct UniPipeInfo { enum UniPipeType { BLOCKING=0, NONBLOCKING=1 } pipeType; int fd[2]; int physIdSrc; int physIdDst; } uniPipeInfo; } linkin[3]; struct LinkInfo linkout[3]; struct Queue { struct Node { struct packetBuffer { int srcaddr; int dstaddr; int length; char payload[-1]; int valid; int new; int linkIndex; } data; struct Node *next; } *head; struct Node *tail; } packetQueue; struct TableEntry { int valid; int dstAddr; int linkout; } fTable[100]; }

variable_types: struct linkArrayType { int numlinks; struct LinkInfo { int linkID; enum LinkType { UNIPIPE=0 } linkType; struct UniPipeInfo { enum UniPipeType { BLOCKING=0, NONBLOCKING=1 } pipeType; int fd[2]; int physIdSrc; int physIdDst; } uniPipeInfo; } link[-1]; }

variable_types: struct manLinkArrayType { int numlinks; struct managerLink { int toHost[2]; int fromHost[2]; } link[3]; }

variable_types: physid int

variable_types: switchid int

variable_types: k int

user_defined_types: struct hostState { int physid; char maindir[100]; int maindirvalid; int netaddr; int nbraddr; struct packetBuffer sendPacketBuff; struct packetBuffer rcvPacketBuff; struct managerLink manLink; struct LinkInfo linkin; struct LinkInfo linkout; int rcvflag; }

user_defined_types: struct switchState { int switchid; struct LinkInfo linkin[3]; struct LinkInfo linkout[3]; struct Queue packetQueue; struct TableEntry fTable[100]; }

user_defined_types: struct linkArrayType { int numlinks; struct LinkInfo link[-1]; }

user_defined_types: struct manLinkArrayType { int numlinks; struct managerLink link[3]; }

user_defined_types: struct LinkInfo { int linkID; enum LinkType linkType; struct UniPipeInfo uniPipeInfo; }

user_defined_types: struct Queue { struct Node *head; struct Node *tail; }

user_defined_types: struct UniPipeInfo { enum UniPipeType pipeType; int fd[2]; int physIdSrc; int physIdDst; }

user_defined_types: struct Node { struct packetBuffer data; struct Node *next; }


netCreateConnections

decompiled_code:
int __fastcall netCreateConnections(int *a1)
{
int result; // eax
int v3; // ebp
int *v4; // rbx
int v5; // edi
unsigned int v6; // eax
unsigned int v7; // eax
unsigned int v8; // eax
unsigned int v9; // eax

result = *a1;
if ( *a1 > 0 )
{
v3 = 0;
v4 = a1 + 1;
while ( pipe(v4) >= 0 && pipe(v4 + 2) >= 0 )
{
v5 = *v4;
++v3;
v4 += 4;
v6 = fcntl(v5, 3);
BYTE1(v6) |= 8u;
fcntl(*(v4 - 4), 4, v6);
v7 = fcntl(*(v4 - 3), 3);
BYTE1(v7) |= 8u;
fcntl(*(v4 - 3), 4, v7);
v8 = fcntl(*(v4 - 2), 3);
BYTE1(v8) |= 8u;
fcntl(*(v4 - 2), 4, v8);
v9 = fcntl(*(v4 - 1), 3);
BYTE1(v9) |= 8u;
result = fcntl(*(v4 - 1), 4, v9);
if ( *a1 <= v3 )
return result;
}
result = puts("Creating pipe failed");
}
return result;
}

canonical_decompiled_code:
int __fastcall func0(int *a1)
{
int result; // eax
int v3; // ebp
int *v4; // rbx
int v5; // edi
unsigned int v6; // eax
unsigned int v7; // eax
unsigned int v8; // eax
unsigned int v9; // eax

result = *a1;
if ( *a1 > 0 )
{
v3 = 0;
v4 = a1 + 1;
while ( pipe(v4) >= 0 && pipe(v4 + 2) >= 0 )
{
v5 = *v4;
++v3;
v4 += 4;
v6 = fcntl(v5, 3);
BYTE1(v6) |= 8u;
fcntl(*(v4 - 4), 4, v6);
v7 = fcntl(*(v4 - 3), 3);
BYTE1(v7) |= 8u;
fcntl(*(v4 - 3), 4, v7);
v8 = fcntl(*(v4 - 2), 3);
BYTE1(v8) |= 8u;
fcntl(*(v4 - 2), 4, v8);
v9 = fcntl(*(v4 - 1), 3);
BYTE1(v9) |= 8u;
result = fcntl(*(v4 - 1), 4, v9);
if ( *a1 <= v3 )
return result;
}
result = puts("Creating pipe failed");
}
return result;
}

original_code:
void netCreateConnections(manLinkArrayType * manLinkArray)
{
int i;
int pflag;
for (i=0; i<manLinkArray->numlinks; i++) {
   if (pipe(manLinkArray->link[i].toHost) < 0) {
      printf("Creating pipe failed\n");
      return;
   }
   if (pipe(manLinkArray->link[i].fromHost) < 0) {
      printf("Creating pipe failed\n");
      return;
   }
   pflag = fcntl(manLinkArray->link[i].toHost[0],3);
   fcntl(manLinkArray->link[i].toHost[0],4, pflag | 04000);
   pflag = fcntl(manLinkArray->link[i].toHost[1],3);
   fcntl(manLinkArray->link[i].toHost[1],4, pflag | 04000);
   pflag = fcntl(manLinkArray->link[i].fromHost[0],3);
   fcntl(manLinkArray->link[i].fromHost[0],4, pflag | 04000);
   pflag = fcntl(manLinkArray->link[i].fromHost[1],3);
   fcntl(manLinkArray->link[i].fromHost[1],4, pflag | 04000);
}
}

canonical_original_code:
void netCreateConnections(struct manLinkArrayType *manLinkArray)
{
int i;
int pflag;
for (i=0; i<manLinkArray->numlinks; i++) {
   if (pipe(manLinkArray->link[i].toHost) < 0) {
      printf("Creating pipe failed\n");
      return;
   }
   if (pipe(manLinkArray->link[i].fromHost) < 0) {
      printf("Creating pipe failed\n");
      return;
   }
   pflag = fcntl(manLinkArray->link[i].toHost[0],3);
   fcntl(manLinkArray->link[i].toHost[0],4, pflag | 04000);
   pflag = fcntl(manLinkArray->link[i].toHost[1],3);
   fcntl(manLinkArray->link[i].toHost[1],4, pflag | 04000);
   pflag = fcntl(manLinkArray->link[i].fromHost[0],3);
   fcntl(manLinkArray->link[i].fromHost[0],4, pflag | 04000);
   pflag = fcntl(manLinkArray->link[i].fromHost[1],3);
   fcntl(manLinkArray->link[i].fromHost[1],4, pflag | 04000);
}
}

variable_types: struct manLinkArrayType { int numlinks; struct managerLink { int toHost[2]; int fromHost[2]; } link[3]; } *

variable_types: pflag int


netCreateLinks

decompiled_code:
__int64 __fastcall netCreateLinks(_DWORD *a1)
{
__int64 result; // rax
int v3; // ebp
_DWORD *v4; // rbx
_DWORD *v5; // rdi

result = (unsigned int)*a1;
if ( (int)result > 0 )
{
v3 = 0;
v4 = a1 + 1;
do
{
*v4 = v3;
v5 = v4;
++v3;
v4 += 7;
*(v4 - 6) = 0;
*(v4 - 5) = 1;
result = linkCreate(v5);
}
while ( *a1 > v3 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(_DWORD *a1)
{
__int64 result; // rax
int v3; // ebp
_DWORD *v4; // rbx
_DWORD *v5; // rdi

result = (unsigned int)*a1;
if ( (int)result > 0 )
{
v3 = 0;
v4 = a1 + 1;
do
{
*v4 = v3;
v5 = v4;
++v3;
v4 += 7;
*(v4 - 6) = 0;
*(v4 - 5) = 1;
result = linkCreate(v5);
}
while ( *a1 > v3 );
}
return result;
}

original_code:
void netCreateLinks(linkArrayType * linkArray)
{
int i;
for (i=0; i<linkArray->numlinks; i++) {
   linkArray->link[i].linkID = i;
   linkArray->link[i].linkType = UNIPIPE;
   linkArray->link[i].uniPipeInfo.pipeType = NONBLOCKING;
   linkCreate(&(linkArray->link[i]));
}
}

canonical_original_code:
void netCreateLinks(struct linkArrayType *linkArray)
{
int i;
for (i=0; i<linkArray->numlinks; i++) {
   linkArray->link[i].linkID = i;
   linkArray->link[i].linkType = UNIPIPE;
   linkArray->link[i].uniPipeInfo.pipeType = NONBLOCKING;
   linkCreate(&(linkArray->link[i]));
}
}

variable_types: struct linkArrayType { int numlinks; struct LinkInfo { int linkID; enum LinkType { UNIPIPE=0 } linkType; struct UniPipeInfo { enum UniPipeType { BLOCKING=0, NONBLOCKING=1 } pipeType; int fd[2]; int physIdSrc; int physIdDst; } uniPipeInfo; } link[-1]; } *

variable_types: i int


netCloseConnections

decompiled_code:
int __fastcall netCloseConnections(int *a1, int a2)
{
int *v2; // rbp
int v3; // ebx
int *v4; // r12

if ( *a1 > 0 )
{
v2 = a1 + 1;
v3 = 0;
do
{
if ( a2 != v3 )
{
close(*v2);
close(v2[1]);
close(v2[2]);
close(v2[3]);
}
++v3;
v2 += 4;
}
while ( *a1 > v3 );
}
v4 = &a1[4 * a2];
close(v4[3]);
return close(v4[2]);
}

original_code:
void netCloseConnections(manLinkArrayType * manLinkArray, int hostid)
{
int i;
for (i=0; i<manLinkArray->numlinks; i++) {
   if (i != hostid) {
      close(manLinkArray->link[i].toHost[0]);
      close(manLinkArray->link[i].toHost[1]);
      close(manLinkArray->link[i].fromHost[0]);
      close(manLinkArray->link[i].fromHost[1]);
   }
}
close(manLinkArray->link[hostid].fromHost[0]);
close(manLinkArray->link[hostid].toHost[1]);
}

canonical_original_code:
void netCloseConnections(struct manLinkArrayType *manLinkArray, int hostid)
{
int i;
for (i=0; i<manLinkArray->numlinks; i++) {
   if (i != hostid) {
      close(manLinkArray->link[i].toHost[0]);
      close(manLinkArray->link[i].toHost[1]);
      close(manLinkArray->link[i].fromHost[0]);
      close(manLinkArray->link[i].fromHost[1]);
   }
}
close(manLinkArray->link[hostid].fromHost[0]);
close(manLinkArray->link[hostid].toHost[1]);
}

variable_types: hostid int

user_defined_types: struct managerLink { int toHost[2]; int fromHost[2]; }


netHostOutLink

decompiled_code:
__int64 __fastcall netHostOutLink(unsigned int *a1, int a2)
{
unsigned int v2; // ebx
_DWORD *v3; // rdi
unsigned int v4; // er8
unsigned int v5; // eax

v2 = *a1;
if ( (int)*a1 > 0 )
{
v3 = a1 + 6;
v4 = v2;
v5 = 0;
do
{
if ( *v3 == a2 )
v4 = v5;
++v5;
v3 += 7;
}
while ( v2 != v5 );
if ( v4 != v2 )
return v4;
}
puts("Error: Can't find outgoing link for host");
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int *a1, int a2)
{
unsigned int v2; // ebx
_DWORD *v3; // rdi
unsigned int v4; // er8
unsigned int v5; // eax

v2 = *a1;
if ( (int)*a1 > 0 )
{
v3 = a1 + 6;
v4 = v2;
v5 = 0;
do
{
if ( *v3 == a2 )
v4 = v5;
++v5;
v3 += 7;
}
while ( v2 != v5 );
if ( v4 != v2 )
return v4;
}
puts("Error: Can't find outgoing link for host");
return v2;
}

original_code:
int netHostOutLink(linkArrayType * linkArray, int hostid)
{
int i;
int index;
index = linkArray->numlinks;
for (i=0; i<linkArray->numlinks; i++) {
   if (linkArray->link[i].uniPipeInfo.physIdSrc == hostid)
      index = i;
}
if (index == linkArray->numlinks)
   printf("Error:  Can't find outgoing link for host\n");
return index;
}

canonical_original_code:
int netHostOutLink(struct linkArrayType *linkArray, int hostid)
{
int i;
int index;
index = linkArray->numlinks;
for (i=0; i<linkArray->numlinks; i++) {
   if (linkArray->link[i].uniPipeInfo.physIdSrc == hostid)
      index = i;
}
if (index == linkArray->numlinks)
   printf("Error:  Can't find outgoing link for host\n");
return index;
}

variable_types: index int

user_defined_types: enum UniPipeType { BLOCKING=0, NONBLOCKING=1 }


netHostInLink

decompiled_code:
__int64 __fastcall netHostInLink(unsigned int *a1, int a2)
{
unsigned int v2; // ebx
_DWORD *v3; // rdi
unsigned int v4; // er8
unsigned int v5; // eax

v2 = *a1;
if ( (int)*a1 > 0 )
{
v3 = a1 + 7;
v4 = v2;
v5 = 0;
do
{
if ( *v3 == a2 )
v4 = v5;
++v5;
v3 += 7;
}
while ( v2 != v5 );
if ( v4 != v2 )
return v4;
}
puts("Error: Can't find outgoing link for host");
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int *a1, int a2)
{
unsigned int v2; // ebx
_DWORD *v3; // rdi
unsigned int v4; // er8
unsigned int v5; // eax

v2 = *a1;
if ( (int)*a1 > 0 )
{
v3 = a1 + 7;
v4 = v2;
v5 = 0;
do
{
if ( *v3 == a2 )
v4 = v5;
++v5;
v3 += 7;
}
while ( v2 != v5 );
if ( v4 != v2 )
return v4;
}
puts("Error: Can't find outgoing link for host");
return v2;
}

original_code:
int netHostInLink(linkArrayType * linkArray, int hostid)
{
int i;
int index;
index = linkArray->numlinks;
for (i=0; i<linkArray->numlinks; i++) {
   if (linkArray->link[i].uniPipeInfo.physIdDst == hostid) index = i;
}
if (index == linkArray->numlinks)
   printf("Error:  Can't find outgoing link for host\n");
return index;
}

canonical_original_code:
int netHostInLink(struct linkArrayType *linkArray, int hostid)
{
int i;
int index;
index = linkArray->numlinks;
for (i=0; i<linkArray->numlinks; i++) {
   if (linkArray->link[i].uniPipeInfo.physIdDst == hostid) index = i;
}
if (index == linkArray->numlinks)
   printf("Error:  Can't find outgoing link for host\n");
return index;
}


netCloseHostOtherLinks

decompiled_code:
int __fastcall netCloseHostOtherLinks(int *a1, int a2)
{
int result; // eax
int v3; // ebp
int *v4; // rbx

result = *a1;
if ( *a1 > 0 )
{
v3 = 0;
v4 = a1 + 4;
do
{
if ( v4[2] != a2 )
result = close(v4[1]);
if ( v4[3] != a2 )
result = close(*v4);
++v3;
v4 += 7;
}
while ( *a1 > v3 );
}
return result;
}

canonical_decompiled_code:
int __fastcall func6(int *a1, int a2)
{
int result; // eax
int v3; // ebp
int *v4; // rbx

result = *a1;
if ( *a1 > 0 )
{
v3 = 0;
v4 = a1 + 4;
do
{
if ( v4[2] != a2 )
result = close(v4[1]);
if ( v4[3] != a2 )
result = close(*v4);
++v3;
v4 += 7;
}
while ( *a1 > v3 );
}
return result;
}

original_code:
void netCloseHostOtherLinks(linkArrayType * linkArray, int hostid)
{
int i;
for (i=0; i<linkArray->numlinks; i++) {
   if (linkArray->link[i].uniPipeInfo.physIdSrc != hostid)
      close(linkArray->link[i].uniPipeInfo.fd[1]);
   if (linkArray->link[i].uniPipeInfo.physIdDst != hostid)
      close(linkArray->link[i].uniPipeInfo.fd[0]);
}
}

canonical_original_code:
void netCloseHostOtherLinks(struct linkArrayType *linkArray, int hostid)
{
int i;
for (i=0; i<linkArray->numlinks; i++) {
   if (linkArray->link[i].uniPipeInfo.physIdSrc != hostid)
      close(linkArray->link[i].uniPipeInfo.fd[1]);
   if (linkArray->link[i].uniPipeInfo.physIdDst != hostid)
      close(linkArray->link[i].uniPipeInfo.fd[0]);
}
}


netSwitchOutLink

decompiled_code:
__int64 __fastcall netSwitchOutLink(_DWORD *a1, int a2, unsigned int a3)
{
__int64 result; // rax
int v4; // esi
_DWORD *i; // rdx

result = a3;
if ( (signed int)a3 >= *a1 )
return 0xFFFFFFFFLL;
v4 = -a2;
for ( i = &a1[7 * a3]; i[6] != v4; i += 7 )
{
result = (unsigned int)(result + 1);
if ( (_DWORD)result == *a1 )
return 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(_DWORD *a1, int a2, unsigned int a3)
{
__int64 result; // rax
int v4; // esi
_DWORD *i; // rdx

result = a3;
if ( (signed int)a3 >= *a1 )
return 0xFFFFFFFFLL;
v4 = -a2;
for ( i = &a1[7 * a3]; i[6] != v4; i += 7 )
{
result = (unsigned int)(result + 1);
if ( (_DWORD)result == *a1 )
return 0xFFFFFFFFLL;
}
return result;
}

original_code:
int netSwitchOutLink(linkArrayType * linkArray, int switchid,int start)
{
int i;
for (i=start; i<linkArray->numlinks; i++)
    if (linkArray->link[i].uniPipeInfo.physIdSrc == -switchid)
  return i;
return -1;
}

canonical_original_code:
int netSwitchOutLink(struct linkArrayType *linkArray, int switchid, int start)
{
int i;
for (i=start; i<linkArray->numlinks; i++)
    if (linkArray->link[i].uniPipeInfo.physIdSrc == -switchid)
  return i;
return -1;
}

variable_types: start int


netSwitchInLink

decompiled_code:
__int64 __fastcall netSwitchInLink(_DWORD *a1, int a2, unsigned int a3)
{
__int64 result; // rax
int v4; // esi
_DWORD *i; // rdx

result = a3;
if ( (signed int)a3 >= *a1 )
return 0xFFFFFFFFLL;
v4 = -a2;
for ( i = &a1[7 * a3]; i[7] != v4; i += 7 )
{
result = (unsigned int)(result + 1);
if ( (_DWORD)result == *a1 )
return 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(_DWORD *a1, int a2, unsigned int a3)
{
__int64 result; // rax
int v4; // esi
_DWORD *i; // rdx

result = a3;
if ( (signed int)a3 >= *a1 )
return 0xFFFFFFFFLL;
v4 = -a2;
for ( i = &a1[7 * a3]; i[7] != v4; i += 7 )
{
result = (unsigned int)(result + 1);
if ( (_DWORD)result == *a1 )
return 0xFFFFFFFFLL;
}
return result;
}

original_code:
int netSwitchInLink(linkArrayType * linkArray, int switchid,int start)
{
int i;
for (i=start; i<linkArray->numlinks; i++)
 if (linkArray->link[i].uniPipeInfo.physIdDst == -switchid)
  return i;
return -1;
}

canonical_original_code:
int netSwitchInLink(struct linkArrayType *linkArray, int switchid, int start)
{
int i;
for (i=start; i<linkArray->numlinks; i++)
 if (linkArray->link[i].uniPipeInfo.physIdDst == -switchid)
  return i;
return -1;
}


netCloseSwitchOtherLinks

decompiled_code:
int __fastcall netCloseSwitchOtherLinks(int *a1, int a2)
{
int result; // eax
int v3; // er12
int v4; // ebp
int *v5; // rbx

result = *a1;
if ( *a1 > 0 )
{
v3 = -a2;
v4 = 0;
v5 = a1 + 4;
do
{
if ( v5[2] != v3 )
result = close(v5[1]);
if ( v5[3] != v3 )
result = close(*v5);
++v4;
v5 += 7;
}
while ( *a1 > v4 );
}
return result;
}

canonical_decompiled_code:
int __fastcall func9(int *a1, int a2)
{
int result; // eax
int v3; // er12
int v4; // ebp
int *v5; // rbx

result = *a1;
if ( *a1 > 0 )
{
v3 = -a2;
v4 = 0;
v5 = a1 + 4;
do
{
if ( v5[2] != v3 )
result = close(v5[1]);
if ( v5[3] != v3 )
result = close(*v5);
++v4;
v5 += 7;
}
while ( *a1 > v4 );
}
return result;
}

original_code:
void netCloseSwitchOtherLinks(linkArrayType * linkArray, int switchid)
{
int i;
for (i=0; i<linkArray->numlinks; i++) {
   if (linkArray->link[i].uniPipeInfo.physIdSrc != -switchid)
      close(linkArray->link[i].uniPipeInfo.fd[1]);
   if (linkArray->link[i].uniPipeInfo.physIdDst != -switchid)
      close(linkArray->link[i].uniPipeInfo.fd[0]);
}
}

canonical_original_code:
void netCloseSwitchOtherLinks(struct linkArrayType *linkArray, int switchid)
{
int i;
for (i=0; i<linkArray->numlinks; i++) {
   if (linkArray->link[i].uniPipeInfo.physIdSrc != -switchid)
      close(linkArray->link[i].uniPipeInfo.fd[1]);
   if (linkArray->link[i].uniPipeInfo.physIdDst != -switchid)
      close(linkArray->link[i].uniPipeInfo.fd[0]);
}
}


netCloseLinks

decompiled_code:
__int64 __fastcall netCloseLinks(_DWORD *a1)
{
__int64 result; // rax
_DWORD *v3; // rbp
int v4; // ebx
_DWORD *v5; // rdi

result = (unsigned int)*a1;
if ( (int)result > 0 )
{
v3 = a1 + 1;
v4 = 0;
do
{
v5 = v3;
++v4;
v3 += 7;
result = linkClear(v5);
}
while ( *a1 > v4 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(_DWORD *a1)
{
__int64 result; // rax
_DWORD *v3; // rbp
int v4; // ebx
_DWORD *v5; // rdi

result = (unsigned int)*a1;
if ( (int)result > 0 )
{
v3 = a1 + 1;
v4 = 0;
do
{
v5 = v3;
++v4;
v3 += 7;
result = linkClear(v5);
}
while ( *a1 > v4 );
}
return result;
}

original_code:
void netCloseLinks(linkArrayType * linkArray)
{
int i;
for (i=0; i<linkArray->numlinks; i++)
   linkClear(&(linkArray->link[i]));
}

canonical_original_code:
void netCloseLinks(struct linkArrayType *linkArray)
{
int i;
for (i=0; i<linkArray->numlinks; i++)
   linkClear(&(linkArray->link[i]));
}


netCloseManConnections

decompiled_code:
int __fastcall netCloseManConnections(int *a1)
{
int result; // eax
__int64 v2; // rbp
__int64 v3; // r12

result = *a1;
if ( *a1 > 0 )
{
v2 = 0LL;
do
{
v3 = v2++;
v3 *= 16LL;
close(*(int *)((char *)a1 + v3 + 4));
result = close(*(int *)((char *)a1 + v3 + 16));
}
while ( *a1 > (int)v2 );
}
return result;
}

canonical_decompiled_code:
int __fastcall func11(int *a1)
{
int result; // eax
__int64 v2; // rbp
__int64 v3; // r12

result = *a1;
if ( *a1 > 0 )
{
v2 = 0LL;
do
{
v3 = v2++;
v3 *= 16LL;
close(*(int *)((char *)a1 + v3 + 4));
result = close(*(int *)((char *)a1 + v3 + 16));
}
while ( *a1 > (int)v2 );
}
return result;
}

original_code:
void netCloseManConnections(manLinkArrayType * manLinkArray)
{
int i;
for (i=0; i<manLinkArray->numlinks; i++) {
   close(manLinkArray->link[i].toHost[0]);
   close(manLinkArray->link[i].fromHost[1]);
}
}

canonical_original_code:
void netCloseManConnections(struct manLinkArrayType *manLinkArray)
{
int i;
for (i=0; i<manLinkArray->numlinks; i++) {
   close(manLinkArray->link[i].toHost[0]);
   close(manLinkArray->link[i].fromHost[1]);
}
}


appendWithSpace

decompiled_code:
char *__fastcall appendWithSpace(const char *a1, const char *a2)
{
size_t v2; // rax

v2 = strlen(a1);
a1[v2] = 32;
return strcpy((char *)&a1[v2 + 1], a2);
}

canonical_decompiled_code:
char *__fastcall func0(const char *a1, const char *a2)
{
size_t v2; // rax

v2 = strlen(a1);
a1[v2] = 32;
return strcpy((char *)&a1[v2 + 1], a2);
}

original_code:
void appendWithSpace(char c1[], char c2[])
{
strcat(c1," ");
strcat(c1,c2);
}


asciiValue

decompiled_code:
__int64 __fastcall asciiValue(char a1)
{
unsigned __int8 v1; // di
__int64 result; // rax

v1 = a1 - 48;
if ( v1 > 9u )
result = 0xFFFFFFFFLL;
else
result = (unsigned int)(char)v1;
return result;
}

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

v1 = a1 - 48;
if ( v1 > 9u )
result = 0xFFFFFFFFLL;
else
result = (unsigned int)(char)v1;
return result;
}

original_code:
int asciiValue(char c)
{
int value;
if (c < '0' || c > '9') return -1;
value = c - '0';
return value;
}

variable_types: c char

variable_types: value int


decompiled_code:
int __fastcall int2Ascii(char *a1, int a2)
{
int v3; // edi
char *v4; // r8
__int64 v5; // r10
unsigned int v6; // edx
int v7; // ecx
char v8; // dl
char *v9; // rax
char *v10; // r10
char *v11; // rcx
char v13[1016]; // [rsp+0h] [rbp-3F8h] BYREF

if ( a2 < 0 )
{
*a1 = 0;
LODWORD(v9) = puts("Error: negative value passed to int2Ascii");
}
else
{
v3 = 0;
v4 = v13;
do
{
v5 = v3;
++v4;
++v3;
v6 = a2 % 0xAu;
v7 = a2;
a2 /= 0xAu;
v8 = v6 + 48;
*(v4 - 1) = v8;
}
while ( v7 > 9 );
v9 = a1;
v10 = &a1[v5];
v11 = &v13[v3];
while ( 1 )
{
*v9 = v8;
--v11;
if ( v9 == v10 )
break;
v8 = *(v11 - 1);
++v9;
}
a1[v3] = 0;
}
return (int)v9;
}

original_code:
void int2Ascii(char c[], int value)
{
char reverse[1000];
int i, j, k;
int remainder;
if (value < 0) {
   c[0] = '\0';
   printf("Error:  negative value passed to int2Ascii\n");
   return;
}
k = 0;
remainder = value;
do {
   reverse[k] = (remainder % 10) + '0';
   remainder = remainder/10;
   k++;
} while(remainder > 0);
for (i=0; i<k; i++) c[i] = reverse[k-i-1];
c[k] = '\0';
}

canonical_original_code:
void int2Ascii(char c[], int value)
{
char reverse[1000];
int i; int j; int k;
int remainder;
if (value < 0) {
   c[0] = '\0';
   printf("Error:  negative value passed to int2Ascii\n");
   return;
}
k = 0;
remainder = value;
do {
   reverse[k] = (remainder % 10) + '0';
   remainder = remainder/10;
   k++;
} while(remainder > 0);
for (i=0; i<k; i++) c[i] = reverse[k-i-1];
c[k] = '\0';
}

variable_types:
c
char []

variable_types:
reverse
char [1000]

variable_types: j int

variable_types: remainder int


decompiled_code:
__int64 __fastcall ascii2Int(__int64 a1)
{
__int64 v2; // rcx
char v3; // si
__int64 v4; // rcx
__int64 v5; // rsi
char *v6; // r8
int v7; // ebx
int v8; // eax
int v9; // edx
__int64 v10; // r10
int v11; // er11
char v12; // r9
unsigned int v13; // er8
char v14; // di

v2 = 0LL;
while ( 1 )
{
v3 = *(_BYTE *)(a1 + v2);
if ( (unsigned int)asciiValue(v3) != -1 )
break;
v2 = v4 + 1;
if ( !v3 )
return 0xFFFFFFFFLL;
}
if ( !v3 )
return 0xFFFFFFFFLL;
v5 = (int)v4;
v6 = (char *)(a1 + (int)v4 + 1);
do
{
v7 = v4;
v8 = asciiValue(*v6);
}
while ( v12 && v8 != -1 );
v13 = 0;
if ( v11 < (int)v4 )
{
while ( 1 )
{
v13 = v9 + 10 * v13;
if ( v7 <= (int)v5 )
break;
v14 = *(_BYTE *)(v10 + v5++ + 1);
v9 = asciiValue(v14);
}
}
return v13;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
__int64 v2; // rcx
char v3; // si
__int64 v4; // rcx
__int64 v5; // rsi
char *v6; // r8
int v7; // ebx
int v8; // eax
int v9; // edx
__int64 v10; // r10
int v11; // er11
char v12; // r9
unsigned int v13; // er8
char v14; // di

v2 = 0LL;
while ( 1 )
{
v3 = *(_BYTE *)(a1 + v2);
if ( (unsigned int)func1(v3) != -1 )
break;
v2 = v4 + 1;
if ( !v3 )
return 0xFFFFFFFFLL;
}
if ( !v3 )
return 0xFFFFFFFFLL;
v5 = (int)v4;
v6 = (char *)(a1 + (int)v4 + 1);
do
{
v7 = v4;
v8 = func1(*v6);
}
while ( v12 && v8 != -1 );
v13 = 0;
if ( v11 < (int)v4 )
{
while ( 1 )
{
v13 = v9 + 10 * v13;
if ( v7 <= (int)v5 )
break;
v14 = *(_BYTE *)(v10 + v5++ + 1);
v9 = func1(v14);
}
}
return v13;
}

original_code:
int ascii2Int(char c[])
{
int first, last;
int value;
first = 0;
while (asciiValue(c[first]) == -1 && c[first] != '\0') first++;
if (c[first] == '\0') return -1;
last = first;
while (asciiValue(c[last]) != -1 && c[last] != '\0') last++;
last--;
value = 0;
for(value = 0; first <= last; first++)
   value = 10* value + asciiValue(c[first]);
return value;
}

canonical_original_code:
int ascii2Int(char c[])
{
int first; int last;
int value;
first = 0;
while (asciiValue(c[first]) == -1 && c[first] != '\0') first++;
if (c[first] == '\0') return -1;
last = first;
while (asciiValue(c[last]) != -1 && c[last] != '\0') last++;
last--;
value = 0;
for(value = 0; first <= last; first++)
   value = 10* value + asciiValue(c[first]);
return value;
}

variable_types: first int

variable_types: last int


original_code:
int point2Word(char c[], int k)
{
int i;
int count;
count = 0;
if (k<1) return -1;
for (i=0; c[i]==' '; i++);
if (c[i] == '\0') return -1;
else count=1;
while (count < k) {
   for (; c[i] != ' ' && c[i] != '\0'; i++);
   if (c[i] == '\0') return -1;
   for (; c[i]==' '; i++);
   if (c[i] == '\0') return -1;
   else count++;
}
return i;
}

variable_types: count int


manReplyReceive

decompiled_code:
__int64 __fastcall manReplyReceive(__int64 a1, void *a2)
{
int v2; // eax
__int64 v3; // rdx
__int64 result; // rax

v2 = read(*(_DWORD *)(a1 + 8), a2, 0xFAuLL);
v3 = v2;
result = (unsigned int)(v2 + 1);
*((_BYTE *)a2 + v3) = 0;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, void *a2)
{
int v2; // eax
__int64 v3; // rdx
__int64 result; // rax

v2 = read(*(_DWORD *)(a1 + 8), a2, 0xFAuLL);
v3 = v2;
result = (unsigned int)(v2 + 1);
*((_BYTE *)a2 + v3) = 0;
return result;
}

original_code:
int manReplyReceive(managerLink * manLink, char reply[])
{
int n;
n = read(manLink->fromHost[0],reply,250);
reply[n] = '\0';
return n+1;
}

canonical_original_code:
int manReplyReceive(struct managerLink *manLink, char reply[])
{
int n;
n = read(manLink->fromHost[0],reply,250);
reply[n] = '\0';
return n+1;
}

variable_types: struct managerLink { int toHost[2]; int fromHost[2]; } *

variable_types:
reply
char []

variable_types: n int


manCommandSend

decompiled_code:
ssize_t __fastcall manCommandSend(__int64 a1, const char *a2)
{
size_t v2; // rax

v2 = strlen(a2);
return write(*(_DWORD *)(a1 + 4), a2, v2);
}

canonical_decompiled_code:
ssize_t __fastcall func1(__int64 a1, const char *a2)
{
size_t v2; // rax

v2 = strlen(a2);
return write(*(_DWORD *)(a1 + 4), a2, v2);
}

original_code:
void manCommandSend(managerLink * manLink, char command[])
{
int i;
i = write(manLink->toHost[1],command,strlen(command));
}

canonical_original_code:
void manCommandSend(struct managerLink *manLink, char command[])
{
int i;
i = write(manLink->toHost[1],command,strlen(command));
}

variable_types:
command
char []


manDisplayReplyMsg

decompiled_code:
int __fastcall manDisplayReplyMsg(__int64 a1)
{
int v2; // eax
int v3; // edi
char *v4; // rbx

v2 = point2Word(a1, 2LL);
v3 = *(char *)(a1 + v2);
if ( *(_BYTE *)(a1 + v2) )
{
v4 = (char *)(a1 + v2 + 1);
do
{
putchar(v3);
v3 = *v4++;
}
while ( (_BYTE)v3 );
}
putchar(10);
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func2(__int64 a1)
{
int v2; // eax
int v3; // edi
char *v4; // rbx

v2 = point2Word(a1, 2LL);
v3 = *(char *)(a1 + v2);
if ( *(_BYTE *)(a1 + v2) )
{
v4 = (char *)(a1 + v2 + 1);
do
{
putchar(v3);
v3 = *v4++;
}
while ( (_BYTE)v3 );
}
putchar(10);
return putchar(10);
}

original_code:
void manDisplayReplyMsg(char replymsg[])
{
int i;
i = point2Word(replymsg, 2);
for(; replymsg[i] != '\0'; i++) putchar(replymsg[i]);
putchar('\n');
putchar('\n');
}

variable_types:
replymsg
char []

variable_types: i int


manGetUserCommand

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

while ( 1 )
{
printf("Commands (Current host ID = %d):\n", a1);
puts(" (d) Display host's state");
puts(" (s) Set host's network address");
puts(" (m) Set host's main directory");
puts(" (f) Clear host's receive packet buffer");
puts(" (r) Download host's receive packet buffer into a file");
puts(" (u) Upload file into host's send packet buffer");
puts(" (t) Transmit packet from the host's send packet buffer");
puts(" (h) Display all hosts");
puts(" (c) Change host");
puts(" (q) Quit");
printf(" Enter Command: ");
do
{
do
LODWORD(result) = getchar();
while ( (_BYTE)result == 32 );
}
while ( (_BYTE)result == 10 );
if ( (_BYTE)result == 100
|| (_BYTE)result == 115
|| (_BYTE)result == 109
|| (_BYTE)result == 102
|| (_BYTE)result == 114
|| (_BYTE)result == 117
|| (_BYTE)result == 116
|| (_BYTE)result == 113
|| (_BYTE)result == 104
|| (_BYTE)result == 99 )
{
break;
}
printf("Invalid command, you entered %c\n", (unsigned int)(char)result);
putchar(10);
}
return (unsigned int)result;
}

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

while ( 1 )
{
printf("Commands (Current host ID = %d):\n", a1);
puts(" (d) Display host's state");
puts(" (s) Set host's network address");
puts(" (m) Set host's main directory");
puts(" (f) Clear host's receive packet buffer");
puts(" (r) Download host's receive packet buffer into a file");
puts(" (u) Upload file into host's send packet buffer");
puts(" (t) Transmit packet from the host's send packet buffer");
puts(" (h) Display all hosts");
puts(" (c) Change host");
puts(" (q) Quit");
printf(" Enter Command: ");
do
{
do
LODWORD(result) = getchar();
while ( (_BYTE)result == 32 );
}
while ( (_BYTE)result == 10 );
if ( (_BYTE)result == 100
|| (_BYTE)result == 115
|| (_BYTE)result == 109
|| (_BYTE)result == 102
|| (_BYTE)result == 114
|| (_BYTE)result == 117
|| (_BYTE)result == 116
|| (_BYTE)result == 113
|| (_BYTE)result == 104
|| (_BYTE)result == 99 )
{
break;
}
printf("Invalid command, you entered %c\n", (unsigned int)(char)result);
putchar(10);
}
return (unsigned int)result;
}

original_code:
char manGetUserCommand(int chost)
{
char cmd;
while(1) {
   printf("Commands (Current host ID = %d):\n", chost);
   printf("   (d) Display host's state\n");
   printf("   (s) Set host's network address\n");
   printf("   (m) Set host's main directory\n");
   printf("   (f) Clear host's receive packet buffer\n");
   printf("   (r) Download host's receive packet buffer into a file\n");
   printf("   (u) Upload file into host's send packet buffer\n");
   printf("   (t) Transmit packet from the host's send packet buffer\n");
   printf("   (h) Display all hosts\n");
   printf("   (c) Change host\n");
   printf("   (q) Quit\n");
   printf("   Enter Command: ");
   do {
      cmd = getchar();
   } while(cmd == ' ' || cmd == '\n');
   if (cmd == 'd') return cmd;
   else if (cmd == 's') return cmd;
   else if (cmd == 'm') return cmd;
   else if (cmd == 'f') return cmd;
   else if (cmd == 'r') return cmd;
   else if (cmd == 'u') return cmd;
   else if (cmd == 't') return cmd;
   else if (cmd == 'q') return cmd;
   else if (cmd == 'h') return cmd;
   else if (cmd == 'c') return cmd;
   else printf("Invalid command, you entered %c\n", cmd);
   printf("\n");
}
}

variable_types: chost int

variable_types: cmd char

return_type: char


manUploadPacket

decompiled_code:
ssize_t __fastcall manUploadPacket(__int64 a1)
{
_BYTE v2[1008]; // [rsp+0h] [rbp-7E8h] BYREF
char v3[1016]; // [rsp+3F0h] [rbp-3F8h] BYREF

printf("Enter the name of the file in the host's directory: ");
_isoc99_scanf("%s", v2);
v3[0] = 0;
appendWithSpace(v3, "UploadPacket");
appendWithSpace(v3, v2);
return manCommandSend(a1, v3);
}

canonical_decompiled_code:
ssize_t __fastcall func4(__int64 a1)
{
_BYTE v2[1008]; // [rsp+0h] [rbp-7E8h] BYREF
char v3[1016]; // [rsp+3F0h] [rbp-3F8h] BYREF

printf("Enter the name of the file in the host's directory: ");
_isoc99_scanf("%s", v2);
v3[0] = 0;
appendWithSpace(v3, "UploadPacket");
appendWithSpace(v3, v2);
return func1(a1, v3);
}

original_code:
void manUploadPacket(managerLink * manLink)
{
char fname[1000];
char command[1000];
printf("Enter the name of the file in the host's directory: ");
scanf("%s",fname);
command[0] = '\0';
appendWithSpace(command, "UploadPacket");
appendWithSpace(command, fname);
manCommandSend(manLink, command);
}

canonical_original_code:
void manUploadPacket(struct managerLink *manLink)
{
char fname[1000];
char command[1000];
printf("Enter the name of the file in the host's directory: ");
scanf("%s",fname);
command[0] = '\0';
appendWithSpace(command, "UploadPacket");
appendWithSpace(command, fname);
manCommandSend(manLink, command);
}

variable_types:
fname
char [1000]

variable_types:
command
char [1000]


manTransmitPacket

decompiled_code:
__int64 __fastcall manTransmitPacket(__int64 a1)
{
__int64 result; // rax
int v2; // [rsp+Ch] [rbp-7ECh] BYREF
char v3[1008]; // [rsp+10h] [rbp-7E8h] BYREF
char v4[1016]; // [rsp+400h] [rbp-3F8h] BYREF

printf("Enter the destination's network address: ");
_isoc99_scanf("%d", &v2);
if ( v2 < 0 )
{
puts("Aborted: address should be positive");
result = 0xFFFFFFFFLL;
}
else if ( v2 > 10000 )
{
puts("The address too big. Try again.");
result = 0xFFFFFFFFLL;
}
else
{
v3[0] = 0;
appendWithSpace(v3, "TransmitPacket");
int2Ascii(v4, (unsigned int)v2);
appendWithSpace(v3, v4);
manCommandSend(a1, v3);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
__int64 result; // rax
int v2; // [rsp+Ch] [rbp-7ECh] BYREF
char v3[1008]; // [rsp+10h] [rbp-7E8h] BYREF
char v4[1016]; // [rsp+400h] [rbp-3F8h] BYREF

printf("Enter the destination's network address: ");
_isoc99_scanf("%d", &v2);
if ( v2 < 0 )
{
puts("Aborted: address should be positive");
result = 0xFFFFFFFFLL;
}
else if ( v2 > 10000 )
{
puts("The address too big. Try again.");
result = 0xFFFFFFFFLL;
}
else
{
v3[0] = 0;
appendWithSpace(v3, "TransmitPacket");
int2Ascii(v4, (unsigned int)v2);
appendWithSpace(v3, v4);
func1(a1, v3);
result = 0LL;
}
return result;
}

original_code:
int manTransmitPacket(managerLink * manLink)
{
char command[1000];
char word[1000];
int netaddr;
printf("Enter the destination's network address: ");
scanf("%d", &netaddr);
if (netaddr < 0) {
   printf("Aborted: address should be positive\n");
   return -1;
}
else if (netaddr > 10000) {
   printf("The address too big.  Try again.\n");
   return -1;
}
command[0] = '\0';
appendWithSpace(command, "TransmitPacket");
int2Ascii(word, netaddr);
appendWithSpace(command, word);
manCommandSend(manLink, command);
return 0;
}

canonical_original_code:
int manTransmitPacket(struct managerLink *manLink)
{
char command[1000];
char word[1000];
int netaddr;
printf("Enter the destination's network address: ");
scanf("%d", &netaddr);
if (netaddr < 0) {
   printf("Aborted: address should be positive\n");
   return -1;
}
else if (netaddr > 10000) {
   printf("The address too big.  Try again.\n");
   return -1;
}
command[0] = '\0';
appendWithSpace(command, "TransmitPacket");
int2Ascii(word, netaddr);
appendWithSpace(command, word);
manCommandSend(manLink, command);
return 0;
}

variable_types:
word
char [1000]

variable_types: netaddr int


manDownloadPacket

decompiled_code:
ssize_t __fastcall manDownloadPacket(__int64 a1)
{
_BYTE v2[1008]; // [rsp+0h] [rbp-7E8h] BYREF
char v3[1016]; // [rsp+3F0h] [rbp-3F8h] BYREF

printf("Enter the file name: ");
_isoc99_scanf("%s", v2);
v3[0] = 0;
appendWithSpace(v3, "DownloadPacket");
appendWithSpace(v3, v2);
return manCommandSend(a1, v3);
}

canonical_decompiled_code:
ssize_t __fastcall func6(__int64 a1)
{
_BYTE v2[1008]; // [rsp+0h] [rbp-7E8h] BYREF
char v3[1016]; // [rsp+3F0h] [rbp-3F8h] BYREF

printf("Enter the file name: ");
_isoc99_scanf("%s", v2);
v3[0] = 0;
appendWithSpace(v3, "DownloadPacket");
appendWithSpace(v3, v2);
return func1(a1, v3);
}

original_code:
void manDownloadPacket(managerLink * manLink)
{
char fname[1000];
char command[1000];
printf("Enter the file name: ");
scanf("%s",fname);
command[0] = '\0';
appendWithSpace(command, "DownloadPacket");
appendWithSpace(command, fname);
manCommandSend(manLink, command);
}

canonical_original_code:
void manDownloadPacket(struct managerLink *manLink)
{
char fname[1000];
char command[1000];
printf("Enter the file name: ");
scanf("%s",fname);
command[0] = '\0';
appendWithSpace(command, "DownloadPacket");
appendWithSpace(command, fname);
manCommandSend(manLink, command);
}


manClearRcvFlg

decompiled_code:
ssize_t __fastcall manClearRcvFlg(__int64 a1)
{
char v2[120]; // [rsp+0h] [rbp-78h] BYREF

v2[0] = 0;
appendWithSpace(v2, "ClearRcvFlg");
return manCommandSend(a1, v2);
}

canonical_decompiled_code:
ssize_t __fastcall func7(__int64 a1)
{
char v2[120]; // [rsp+0h] [rbp-78h] BYREF

v2[0] = 0;
appendWithSpace(v2, "ClearRcvFlg");
return func1(a1, v2);
}

original_code:
void manClearRcvFlg(managerLink * manLink)
{
char command[100];
command[0] = '\0';
appendWithSpace(command, "ClearRcvFlg");
manCommandSend(manLink, command);
}

canonical_original_code:
void manClearRcvFlg(struct managerLink *manLink)
{
char command[100];
command[0] = '\0';
appendWithSpace(command, "ClearRcvFlg");
manCommandSend(manLink, command);
}

variable_types:
command
char [100]


manSetMainDir

decompiled_code:
ssize_t __fastcall manSetMainDir(__int64 a1)
{
_BYTE v2[1008]; // [rsp+0h] [rbp-7E8h] BYREF
char v3[1016]; // [rsp+3F0h] [rbp-3F8h] BYREF

printf("Enter new directory: ");
_isoc99_scanf("%s", v2);
v3[0] = 0;
appendWithSpace(v3, "SetMainDir");
appendWithSpace(v3, v2);
return manCommandSend(a1, v3);
}

canonical_decompiled_code:
ssize_t __fastcall func8(__int64 a1)
{
_BYTE v2[1008]; // [rsp+0h] [rbp-7E8h] BYREF
char v3[1016]; // [rsp+3F0h] [rbp-3F8h] BYREF

printf("Enter new directory: ");
_isoc99_scanf("%s", v2);
v3[0] = 0;
appendWithSpace(v3, "SetMainDir");
appendWithSpace(v3, v2);
return func1(a1, v3);
}

original_code:
void manSetMainDir(managerLink * manLink)
{
char dirname[1000];
char command[1000];
printf("Enter new directory: ");
scanf("%s",dirname);
command[0] = '\0';
appendWithSpace(command, "SetMainDir");
appendWithSpace(command, dirname);
manCommandSend(manLink, command);
}

canonical_original_code:
void manSetMainDir(struct managerLink *manLink)
{
char dirname[1000];
char command[1000];
printf("Enter new directory: ");
scanf("%s",dirname);
command[0] = '\0';
appendWithSpace(command, "SetMainDir");
appendWithSpace(command, dirname);
manCommandSend(manLink, command);
}

variable_types:
dirname
char [1000]


manSetNetAddr

decompiled_code:
ssize_t __fastcall manSetNetAddr(__int64 a1)
{
int v2; // [rsp+Ch] [rbp-7ECh] BYREF
char v3[1008]; // [rsp+10h] [rbp-7E8h] BYREF
char v4[1016]; // [rsp+400h] [rbp-3F8h] BYREF

while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
printf("Enter network address: ");
_isoc99_scanf("%d", &v2);
if ( v2 >= 0 )
break;
puts("Address must be postive. Try again");
}
if ( v2 <= 10000 )
break;
puts("The address is too big. Try again");
}
if ( v2 != 1000 )
break;
puts("Address 1000 is reserved for BROADCAST. Try again");
}
v3[0] = 0;
appendWithSpace(v3, "SetNetAddr");
int2Ascii(v4, (unsigned int)v2);
appendWithSpace(v3, v4);
return manCommandSend(a1, v3);
}

canonical_decompiled_code:
ssize_t __fastcall func9(__int64 a1)
{
int v2; // [rsp+Ch] [rbp-7ECh] BYREF
char v3[1008]; // [rsp+10h] [rbp-7E8h] BYREF
char v4[1016]; // [rsp+400h] [rbp-3F8h] BYREF

while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
printf("Enter network address: ");
_isoc99_scanf("%d", &v2);
if ( v2 >= 0 )
break;
puts("Address must be postive. Try again");
}
if ( v2 <= 10000 )
break;
puts("The address is too big. Try again");
}
if ( v2 != 1000 )
break;
puts("Address 1000 is reserved for BROADCAST. Try again");
}
v3[0] = 0;
appendWithSpace(v3, "SetNetAddr");
int2Ascii(v4, (unsigned int)v2);
appendWithSpace(v3, v4);
return func1(a1, v3);
}

original_code:
void manSetNetAddr(managerLink * manLink)
{
int netaddr;
char command[1000];
char addr[1000];
while(1) {
   printf("Enter network address: ");
   scanf("%d",&netaddr);
   if (netaddr <0)
      printf("Address must be postive. Try again\n");
   else if (netaddr > 10000)
      printf("The address is too big. Try again\n");
   else if (netaddr == 1000)
      printf("Address 1000 is reserved for BROADCAST. Try again\n");
   else break;
}
command[0] = '\0';
appendWithSpace(command, "SetNetAddr");
int2Ascii(addr,netaddr);
appendWithSpace(command, addr);
manCommandSend(manLink, command);
}

canonical_original_code:
void manSetNetAddr(struct managerLink *manLink)
{
int netaddr;
char command[1000];
char addr[1000];
while(1) {
   printf("Enter network address: ");
   scanf("%d",&netaddr);
   if (netaddr <0)
      printf("Address must be postive. Try again\n");
   else if (netaddr > 10000)
      printf("The address is too big. Try again\n");
   else if (netaddr == 1000)
      printf("Address 1000 is reserved for BROADCAST. Try again\n");
   else break;
}
command[0] = '\0';
appendWithSpace(command, "SetNetAddr");
int2Ascii(addr,netaddr);
appendWithSpace(command, addr);
manCommandSend(manLink, command);
}

variable_types:
addr
char [1000]


manGetHostState

decompiled_code:
ssize_t __fastcall manGetHostState(__int64 a1)
{
char v2[1016]; // [rsp+0h] [rbp-3F8h] BYREF

v2[0] = 0;
appendWithSpace(v2, "GetHostState");
return manCommandSend(a1, v2);
}

canonical_decompiled_code:
ssize_t __fastcall func10(__int64 a1)
{
char v2[1016]; // [rsp+0h] [rbp-3F8h] BYREF

v2[0] = 0;
appendWithSpace(v2, "GetHostState");
return func1(a1, v2);
}

original_code:
void manGetHostState(managerLink * manLink)
{
char command[1000];
command[0] = '\0';
appendWithSpace(command, "GetHostState");
manCommandSend(manLink, command);
}

canonical_original_code:
void manGetHostState(struct managerLink *manLink)
{
char command[1000];
command[0] = '\0';
appendWithSpace(command, "GetHostState");
manCommandSend(manLink, command);
}


manDisplayHostState

decompiled_code:
int __fastcall manDisplayHostState(__int64 a1)
{
char v2[1016]; // [rsp+0h] [rbp-3F8h] BYREF

findWord(v2, a1, 2LL);
printf("Host physical ID: %s\n", v2);
findWord(v2, a1, 3LL);
printf("Directory: %s\n", v2);
findWord(v2, a1, 4LL);
printf("Network address: %s\n", v2);
findWord(v2, a1, 5LL);
printf("Neighbor address: %s\n", v2);
findWord(v2, a1, 6LL);
printf("New packet received flag: %s\n", v2);
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func11(__int64 a1)
{
char v2[1016]; // [rsp+0h] [rbp-3F8h] BYREF

findWord(v2, a1, 2LL);
printf("Host physical ID: %s\n", v2);
findWord(v2, a1, 3LL);
printf("Directory: %s\n", v2);
findWord(v2, a1, 4LL);
printf("Network address: %s\n", v2);
findWord(v2, a1, 5LL);
printf("Neighbor address: %s\n", v2);
findWord(v2, a1, 6LL);
printf("New packet received flag: %s\n", v2);
return putchar(10);
}

original_code:
void manDisplayHostState(char buffer[])
{
char word[1000];
findWord(word, buffer, 2);
printf("Host physical ID: %s\n", word);
findWord(word, buffer, 3);
printf("Directory: %s\n", word);
findWord(word, buffer, 4);
printf("Network address: %s\n", word);
findWord(word, buffer, 5);
printf("Neighbor address: %s\n", word);
findWord(word, buffer, 6);
printf("New packet received flag: %s\n", word);
printf("\n");
}

variable_types:
buffer
char []


manWaitForReply

decompiled_code:
int __fastcall manWaitForReply(__int64 a1)
{
int result; // eax
int v2; // ebx
_BYTE v3[1008]; // [rsp+0h] [rbp-808h] BYREF
__int64 v4[131]; // [rsp+3F0h] [rbp-418h] BYREF

do
{
while ( 1 )
{
usleep(0x2710u);
v2 = manReplyReceive(a1, v3);
findWord(v4, v3, 1LL);
if ( v4[0] != 0x59414C50534944LL )
break;
result = manDisplayReplyMsg((__int64)v3);
if ( v2 > 1 )
return result;
}
result = LODWORD(v4[0]) ^ 0x48746547;
if ( !(v4[0] ^ 0x5374736F48746547LL | v4[1] ^ 0x6B634165746174LL) )
result = manDisplayHostState((__int64)v3);
}
while ( v2 <= 1 );
return result;
}

original_code:
void manWaitForReply(managerLink * manLink, int cmd)
{
char reply[1000];
char word[1000];
int length;
do {
   usleep(10000);
   length = manReplyReceive(manLink, reply);
   findWord(word, reply, 1);
   if (strcmp(word, "DISPLAY")==0) manDisplayReplyMsg(reply);
   else if (strcmp(word, "GetHostStateAck") == 0) manDisplayHostState(reply);
} while(length <= 1);
}

canonical_original_code:
void manWaitForReply(struct managerLink *manLink, int cmd)
{
char reply[1000];
char word[1000];
int length;
do {
   usleep(10000);
   length = manReplyReceive(manLink, reply);
   findWord(word, reply, 1);
   if (strcmp(word, "DISPLAY")==0) manDisplayReplyMsg(reply);
   else if (strcmp(word, "GetHostStateAck") == 0) manDisplayHostState(reply);
} while(length <= 1);
}

variable_types: cmd int

variable_types:
reply
char [1000]

variable_types: length int


manDisplayHosts

decompiled_code:
int __fastcall manDisplayHosts(unsigned int a1, int a2)
{
unsigned int i; // ebx
__int64 v4; // rsi

puts("List of hosts:");
if ( a2 > 0 )
{
for ( i = 0; i != a2; ++i )
{
while ( a1 != i )
{
v4 = i++;
printf(" Host ID = %d\n", v4);
if ( a2 == i )
return putchar(10);
}
printf(" Host ID = %d (connected)\n", a1);
}
}
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func13(unsigned int a1, int a2)
{
unsigned int i; // ebx
__int64 v4; // rsi

puts("List of hosts:");
if ( a2 > 0 )
{
for ( i = 0; i != a2; ++i )
{
while ( a1 != i )
{
v4 = i++;
printf(" Host ID = %d\n", v4);
if ( a2 == i )
return putchar(10);
}
printf(" Host ID = %d (connected)\n", a1);
}
}
return putchar(10);
}

original_code:
void manDisplayHosts(int currhost, int maxHosts)
{
int i;
printf("List of hosts:\n");
for (i=0; i<maxHosts; i++) {
   if (i == currhost) printf("   Host ID = %d (connected)\n",i);
   else printf("   Host ID = %d\n",i);
}
printf("\n");
}

variable_types: currhost int

variable_types: maxHosts int


manChangeHost

decompiled_code:
__int64 __fastcall manChangeHost(int a1)
{
__int64 result; // rax
int v2[7]; // [rsp+Ch] [rbp-1Ch] BYREF

while ( 1 )
{
printf("Enter host ID number (range 0 to %d): ", (unsigned int)(a1 - 1));
_isoc99_scanf("%d", v2);
result = (unsigned int)v2[0];
if ( v2[0] < a1 && v2[0] >= 0 )
break;
puts("Number is out of range, try again\n");
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func14(int a1)
{
__int64 result; // rax
int v2[7]; // [rsp+Ch] [rbp-1Ch] BYREF

while ( 1 )
{
printf("Enter host ID number (range 0 to %d): ", (unsigned int)(a1 - 1));
_isoc99_scanf("%d", v2);
result = (unsigned int)v2[0];
if ( v2[0] < a1 && v2[0] >= 0 )
break;
puts("Number is out of range, try again\n");
}
return result;
}

original_code:
int manChangeHost(int maxHosts)
{
int newnumber;
do {
   printf("Enter host ID number (range 0 to %d): ", maxHosts-1);
   scanf("%d",&newnumber);
   if (newnumber >= 0 && newnumber < maxHosts) break;
   else printf("Number is out of range, try again\n\n");
} while(1);
return newnumber;
}

variable_types: newnumber int


manMain

decompiled_code:
__int64 __fastcall manMain(int *a1)
{
signed int v1; // ebx
__int64 result; // rax

v1 = 0;
LABEL_2:
while ( 1 )
{
result = manGetUserCommand(v1);
if ( (_BYTE)result == 113 )
return result;
while ( 1 )
{
switch ( (_BYTE)result )
{
case 'd':
manGetHostState((__int64)&a1[4 * v1 + 1]);
manWaitForReply((__int64)&a1[4 * v1 + 1]);
goto LABEL_2;
case 's':
manSetNetAddr((__int64)&a1[4 * v1 + 1]);
manWaitForReply((__int64)&a1[4 * v1 + 1]);
goto LABEL_2;
case 'm':
manSetMainDir((__int64)&a1[4 * v1 + 1]);
manWaitForReply((__int64)&a1[4 * v1 + 1]);
goto LABEL_2;
case 'f':
manClearRcvFlg((__int64)&a1[4 * v1 + 1]);
manWaitForReply((__int64)&a1[4 * v1 + 1]);
goto LABEL_2;
case 'r':
manDownloadPacket((__int64)&a1[4 * v1 + 1]);
manWaitForReply((__int64)&a1[4 * v1 + 1]);
goto LABEL_2;
case 'u':
manUploadPacket((__int64)&a1[4 * v1 + 1]);
manWaitForReply((__int64)&a1[4 * v1 + 1]);
goto LABEL_2;
}
if ( (_BYTE)result == 116 )
break;
if ( (_BYTE)result == 104 )
{
manDisplayHosts(v1, *a1);
goto LABEL_2;
}
if ( (_BYTE)result == 99 )
{
v1 = manChangeHost(*a1);
goto LABEL_2;
}
printf("***Invalid command, you entered %c\n", (unsigned int)(char)result);
result = manGetUserCommand(v1);
if ( (_BYTE)result == 113 )
return result;
}
if ( !(unsigned int)manTransmitPacket((__int64)&a1[4 * v1 + 1]) )
manWaitForReply((__int64)&a1[4 * v1 + 1]);
}
}

canonical_decompiled_code:
__int64 __fastcall func15(int *a1)
{
signed int v1; // ebx
__int64 result; // rax

v1 = 0;
LABEL_2:
while ( 1 )
{
result = func3(v1);
if ( (_BYTE)result == 113 )
return result;
while ( 1 )
{
switch ( (_BYTE)result )
{
case 'd':
func10((__int64)&a1[4 * v1 + 1]);
func12((__int64)&a1[4 * v1 + 1]);
goto LABEL_2;
case 's':
func9((__int64)&a1[4 * v1 + 1]);
func12((__int64)&a1[4 * v1 + 1]);
goto LABEL_2;
case 'm':
func8((__int64)&a1[4 * v1 + 1]);
func12((__int64)&a1[4 * v1 + 1]);
goto LABEL_2;
case 'f':
func7((__int64)&a1[4 * v1 + 1]);
func12((__int64)&a1[4 * v1 + 1]);
goto LABEL_2;
case 'r':
func6((__int64)&a1[4 * v1 + 1]);
func12((__int64)&a1[4 * v1 + 1]);
goto LABEL_2;
case 'u':
func4((__int64)&a1[4 * v1 + 1]);
func12((__int64)&a1[4 * v1 + 1]);
goto LABEL_2;
}
if ( (_BYTE)result == 116 )
break;
if ( (_BYTE)result == 104 )
{
func13(v1, *a1);
goto LABEL_2;
}
if ( (_BYTE)result == 99 )
{
v1 = func14(*a1);
goto LABEL_2;
}
printf("***Invalid command, you entered %c\n", (unsigned int)(char)result);
result = func3(v1);
if ( (_BYTE)result == 113 )
return result;
}
if ( !(unsigned int)func5((__int64)&a1[4 * v1 + 1]) )
func12((__int64)&a1[4 * v1 + 1]);
}
}

original_code:
void manMain(manLinkArrayType * manLinkArray)
{
int currhost;
char cmd;
int k;
currhost = 0;
while(1) {
   cmd = manGetUserCommand(currhost);
   if (cmd == 'q') return;
   else if (cmd == 'd') {
      manGetHostState(&(manLinkArray->link[currhost]));
      manWaitForReply(&(manLinkArray->link[currhost]), cmd);
   }
   else if (cmd == 's') {
      manSetNetAddr(&(manLinkArray->link[currhost]));
      manWaitForReply(&(manLinkArray->link[currhost]), cmd);
   }
   else if (cmd == 'm') {
      manSetMainDir(&(manLinkArray->link[currhost]));
      manWaitForReply(&(manLinkArray->link[currhost]), cmd);
   }
   else if (cmd == 'f') {
      manClearRcvFlg(&(manLinkArray->link[currhost]));
      manWaitForReply(&(manLinkArray->link[currhost]), cmd);
   }
   else if (cmd == 'r') {
      manDownloadPacket(&(manLinkArray->link[currhost]));
      manWaitForReply(&(manLinkArray->link[currhost]), cmd);
   }
   else if (cmd == 'u') {
      manUploadPacket(&(manLinkArray->link[currhost]));
      manWaitForReply(&(manLinkArray->link[currhost]), cmd);
   }
   else if (cmd == 't') {
      k = manTransmitPacket(&(manLinkArray->link[currhost]));
      if (k==0) manWaitForReply(&(manLinkArray->link[currhost]), cmd);
   }
   else if (cmd == 'h')
      manDisplayHosts(currhost, manLinkArray->numlinks);
   else if (cmd == 'c')
      currhost = manChangeHost(manLinkArray->numlinks);
   else printf("***Invalid command, you entered %c\n", cmd);
}
printf("\n");
}

canonical_original_code:
void manMain(struct manLinkArrayType *manLinkArray)
{
int currhost;
char cmd;
int k;
currhost = 0;
while(1) {
   cmd = manGetUserCommand(currhost);
   if (cmd == 'q') return;
   else if (cmd == 'd') {
      manGetHostState(&(manLinkArray->link[currhost]));
      manWaitForReply(&(manLinkArray->link[currhost]), cmd);
   }
   else if (cmd == 's') {
      manSetNetAddr(&(manLinkArray->link[currhost]));
      manWaitForReply(&(manLinkArray->link[currhost]), cmd);
   }
   else if (cmd == 'm') {
      manSetMainDir(&(manLinkArray->link[currhost]));
      manWaitForReply(&(manLinkArray->link[currhost]), cmd);
   }
   else if (cmd == 'f') {
      manClearRcvFlg(&(manLinkArray->link[currhost]));
      manWaitForReply(&(manLinkArray->link[currhost]), cmd);
   }
   else if (cmd == 'r') {
      manDownloadPacket(&(manLinkArray->link[currhost]));
      manWaitForReply(&(manLinkArray->link[currhost]), cmd);
   }
   else if (cmd == 'u') {
      manUploadPacket(&(manLinkArray->link[currhost]));
      manWaitForReply(&(manLinkArray->link[currhost]), cmd);
   }
   else if (cmd == 't') {
      k = manTransmitPacket(&(manLinkArray->link[currhost]));
      if (k==0) manWaitForReply(&(manLinkArray->link[currhost]), cmd);
   }
   else if (cmd == 'h')
      manDisplayHosts(currhost, manLinkArray->numlinks);
   else if (cmd == 'c')
      currhost = manChangeHost(manLinkArray->numlinks);
   else printf("***Invalid command, you entered %c\n", cmd);
}
printf("\n");
}

variable_types: struct manLinkArrayType { int numlinks; struct managerLink { int toHost[2]; int fromHost[2]; } link[3]; } *

variable_types: k int


switchMain

original_code:
void switchMain(switchState* sstate)
{
 packetBuffer tmpbuff;
 int i,j;
 int received;
 while(1){
  for(i=0;i<3;i++){
   linkReceive(&(sstate->linkin[i]),&tmpbuff);
   if(tmpbuff.valid && tmpbuff.new){
    tmpbuff.linkIndex = i;
    for(j=0;j<100 &&
     !(sstate->fTable[j].valid==1 &&
      sstate->fTable[j].dstAddr == tmpbuff.srcaddr);j++);
    if(j<100)
     sstate->fTable[j].linkout = i;
    else
     for(j=0;j<100 && sstate->fTable[j].valid==1;j++);
      if(j!=100){
       sstate->fTable[j].valid = 1;
       sstate->fTable[j].dstAddr = tmpbuff.srcaddr;
       sstate->fTable[j].linkout = i;
      }
    in(&(sstate->packetQueue),tmpbuff);
    printf("Switch %d Forwarding Table:\n",sstate->switchid);
    printf("D L\n");
    for(j=0;j<100;j++){
     if(sstate->fTable[j].valid)
     printf("%d %d\n",sstate->fTable[j].dstAddr,sstate->fTable[j].linkout);
    }
   }
  }
  if(!isEmpty(&(sstate->packetQueue))){
   tmpbuff = out(&(sstate->packetQueue));
   for(i=0;i<100 &&
    !(sstate->fTable[i].valid &&
     sstate->fTable[i].dstAddr == tmpbuff.dstaddr);i++);
   if(i<100)
    linkSend(&(sstate->linkout[i]),&tmpbuff);
   else{
    for(i=0;i<3;i++)
     if(i!=tmpbuff.linkIndex)
      linkSend(&(sstate->linkout[i]),&tmpbuff);
   }
  }
  usleep(10000);
 }
}

canonical_original_code:
void switchMain(struct switchState *sstate)
{
 struct packetBuffer tmpbuff;
 int i; int j;
 int received;
 while(1){
  for(i=0;i<3;i++){
   linkReceive(&(sstate->linkin[i]),&tmpbuff);
   if(tmpbuff.valid && tmpbuff.new){
    tmpbuff.linkIndex = i;
    for(j=0;j<100 &&
     !(sstate->fTable[j].valid==1 &&
      sstate->fTable[j].dstAddr == tmpbuff.srcaddr);j++);
    if(j<100)
     sstate->fTable[j].linkout = i;
    else
     for(j=0;j<100 && sstate->fTable[j].valid==1;j++);
      if(j!=100){
       sstate->fTable[j].valid = 1;
       sstate->fTable[j].dstAddr = tmpbuff.srcaddr;
       sstate->fTable[j].linkout = i;
      }
    in(&(sstate->packetQueue),tmpbuff);
    printf("Switch %d Forwarding Table:\n",sstate->switchid);
    printf("D L\n");
    for(j=0;j<100;j++){
     if(sstate->fTable[j].valid)
     printf("%d %d\n",sstate->fTable[j].dstAddr,sstate->fTable[j].linkout);
    }
   }
  }
  if(!isEmpty(&(sstate->packetQueue))){
   tmpbuff = out(&(sstate->packetQueue));
   for(i=0;i<100 &&
    !(sstate->fTable[i].valid &&
     sstate->fTable[i].dstAddr == tmpbuff.dstaddr);i++);
   if(i<100)
    linkSend(&(sstate->linkout[i]),&tmpbuff);
   else{
    for(i=0;i<3;i++)
     if(i!=tmpbuff.linkIndex)
      linkSend(&(sstate->linkout[i]),&tmpbuff);
   }
  }
  usleep(10000);
 }
}

variable_types: struct switchState { int switchid; struct LinkInfo { int linkID; enum LinkType { UNIPIPE=0 } linkType; struct UniPipeInfo { enum UniPipeType { BLOCKING=0, NONBLOCKING=1 } pipeType; int fd[2]; int physIdSrc; int physIdDst; } uniPipeInfo; } linkin[3]; struct LinkInfo linkout[3]; struct Queue { struct Node { struct packetBuffer { int srcaddr; int dstaddr; int length; char payload[-1]; int valid; int new; int linkIndex; } data; struct Node *next; } *head; struct Node *tail; } packetQueue; struct TableEntry { int valid; int dstAddr; int linkout; } fTable[100]; } *

variable_types: i int

variable_types: j int

variable_types: received int


isEmpty

decompiled_code:
_BOOL8 __fastcall isEmpty(_QWORD *a1)
{
return *a1 == 0LL;
}

canonical_decompiled_code:
_BOOL8 __fastcall func1(_QWORD *a1)
{
return *a1 == 0LL;
}

original_code:
int isEmpty(Queue* q)
{
 return(q->head==((void *)0));
}

canonical_original_code:
int isEmpty(struct Queue *q)
{
 return(q->head==((void *)0));
}

variable_types: struct Queue { struct Node { struct packetBuffer { int srcaddr; int dstaddr; int length; char payload[-1]; int valid; int new; int linkIndex; } data; struct Node *next; } *head; struct Node *tail; } *


newNode

decompiled_code:
void *newNode()
{
return malloc(0xF0uLL);
}

canonical_decompiled_code:
void *func0()
{
return malloc(0xF0uLL);
}

original_code:
Node* newNode()
{
 return (Node*)malloc(sizeof(Node));
}

canonical_original_code:
struct Node *newNode()
{
 return (struct Node *)malloc(sizeof(Node));
}

return_type: struct Node { struct packetBuffer { int srcaddr; int dstaddr; int length; char payload[-1]; int valid; int new; int linkIndex; } data; struct Node *next; } *


deleteNode

decompiled_code:
// attributes: thunk
void deleteNode(void *ptr)
{
free(ptr);
}

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

original_code:
void deleteNode(Node* nodeptr)
{
 free(nodeptr);
}

canonical_original_code:
void deleteNode(struct Node *nodeptr)
{
 free(nodeptr);
}

variable_types: struct Node { struct packetBuffer { int srcaddr; int dstaddr; int length; char payload[-1]; int valid; int new; int linkIndex; } data; struct Node *next; } *


repo: dmazzoni/ElaboratoIPC

decompiled_code:
key_t __fastcall generate_key(int proj_id)
{
key_t result; // eax

result = ftok("main.c", proj_id);
if ( result == -1 )
{
write_to_fd(2LL, "ftok failed\n");
exit(1);
}
return result;
}

canonical_decompiled_code:
key_t __fastcall func0(int proj_id)
{
key_t result; // eax

result = ftok("main.c", proj_id);
if ( result == -1 )
{
write_to_fd(2LL, "ftok failed\n");
exit(1);
}
return result;
}

original_code:
static int generate_key(int seed) {
 int key = ftok("main.c", seed);
 if (key == -1) {
  write_to_fd(2, "ftok failed\n");
  exit(1);
 }
 return key;
}

variable_types: seed int

variable_types: key int