decompiled_code:
__int64 __fastcall ndmp_accept_notify(__int64 a1)
{
u_int v1; // edx
int v3; // [rsp+0h] [rbp-78h] BYREF
__int16 v4; // [rsp+4h] [rbp-74h]
void *v5; // [rsp+8h] [rbp-70h]
__int64 v6[4]; // [rsp+10h] [rbp-68h] BYREF
int v7; // [rsp+30h] [rbp-48h]
XDR xdrs; // [rsp+40h] [rbp-38h] BYREF

v4 = 3;
v3 = 0;
v5 = &unk_768;
v6[0] = (int)get_next_seq_number(0LL);
v6[3] = 0LL;
v6[1] = time(0LL);
v6[2] = 0x50200000000LL;
v7 = 0;
*(_DWORD *)(a1 + 4132) = xdr_sizeof((xdrproc_t)&xdr_ndmp_header, v6);
v1 = xdr_sizeof((xdrproc_t)&xdr_ndmp_notify_connected_request, &v3) + *(_DWORD *)(a1 + 4132);
*(_DWORD *)(a1 + 4132) = v1;
xdrmem_create(&xdrs, (const caddr_t)(a1 + 2084), v1, XDR_ENCODE);
xdr_ndmp_header(&xdrs, v6);
xdr_ndmp_notify_connected_request(&xdrs, &v3);
return write_client_log(*(_QWORD *)(a1 + 4), *(_QWORD *)(a1 + 12), "Sent CONNECT_NOTIFY to client");
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
u_int v1; // edx
int v3; // [rsp+0h] [rbp-78h] BYREF
__int16 v4; // [rsp+4h] [rbp-74h]
void *v5; // [rsp+8h] [rbp-70h]
__int64 v6[4]; // [rsp+10h] [rbp-68h] BYREF
int v7; // [rsp+30h] [rbp-48h]
XDR xdrs; // [rsp+40h] [rbp-38h] BYREF

v4 = 3;
v3 = 0;
v5 = &unk_768;
v6[0] = (int)func2(0LL);
v6[3] = 0LL;
v6[1] = time(0LL);
v6[2] = 0x50200000000LL;
v7 = 0;
*(_DWORD *)(a1 + 4132) = xdr_sizeof((xdrproc_t)&xdr_ndmp_header, v6);
v1 = xdr_sizeof((xdrproc_t)&xdr_ndmp_notify_connected_request, &v3) + *(_DWORD *)(a1 + 4132);
*(_DWORD *)(a1 + 4132) = v1;
xdrmem_create(&xdrs, (const caddr_t)(a1 + 2084), v1, XDR_ENCODE);
xdr_ndmp_header(&xdrs, v6);
xdr_ndmp_notify_connected_request(&xdrs, &v3);
return write_client_log(*(_QWORD *)(a1 + 4), *(_QWORD *)(a1 + 12), "Sent CONNECT_NOTIFY to client");
}

original_code:
void ndmp_accept_notify(struct client_txn* txn, struct ndmp_header header, XDR* request_stream)
{
        struct ndmp_notify_connected_request reply;
        struct ndmp_header reply_header;
        XDR reply_stream;
        reply.reason = NDMP_CONNECTED;
        reply.protocol_version = 3;
        reply.text_reason = "";
        reply_header.sequence = get_next_seq_number();
        reply_header.time_stamp = (u_long) time(((void *)0));
        reply_header.message_type = NDMP_MESSAGE_REQUEST;
        reply_header.message = NDMP_NOTIFY_CONNECTED;
        reply_header.reply_sequence = 0;
        reply_header.error = NDMP_NO_ERR;
        txn->reply.length = xdr_sizeof((xdrproc_t)
                                       xdr_ndmp_header,
                                       &reply_header);
        txn->reply.length += xdr_sizeof((xdrproc_t)
                                        xdr_ndmp_notify_connected_request,
                                        &reply);
        xdrmem_create(&reply_stream, txn->reply.message, txn->reply.length,
                      XDR_ENCODE);
        xdr_ndmp_header(&reply_stream, &reply_header);
        xdr_ndmp_notify_connected_request(&reply_stream, &reply);
        write_client_log(txn->client_session.client_info.client, "Sent CONNECT_NOTIFY to client");
}

canonical_original_code:
void ndmp_accept_notify(struct client_txn *txn, struct ndmp_header header, struct XDR *request_stream)
{
        struct ndmp_notify_connected_request reply;
        struct ndmp_header reply_header;
        struct XDR reply_stream;
        reply.reason = NDMP_CONNECTED;
        reply.protocol_version = 3;
        reply.text_reason = "";
        reply_header.sequence = get_next_seq_number();
        reply_header.time_stamp = (unsigned long) time(((void *)0));
        reply_header.message_type = NDMP_MESSAGE_REQUEST;
        reply_header.message = NDMP_NOTIFY_CONNECTED;
        reply_header.reply_sequence = 0;
        reply_header.error = NDMP_NO_ERR;
        txn->reply.length = xdr_sizeof((int (*)(struct XDR *, void *, ...))
                                       xdr_ndmp_header,
                                       &reply_header);
        txn->reply.length += xdr_sizeof((int (*)(struct XDR *, void *, ...))
                                        xdr_ndmp_notify_connected_request,
                                        &reply);
        xdrmem_create(&reply_stream, txn->reply.message, txn->reply.length,
                      XDR_ENCODE);
        xdr_ndmp_header(&reply_stream, &reply_header);
        xdr_ndmp_notify_connected_request(&reply_stream, &reply);
        write_client_log(txn->client_session.client_info.client, "Sent CONNECT_NOTIFY to client");
}

variable_types: struct ndmp_header { unsigned long sequence; unsigned long time_stamp; enum ndmp_header_message_type { NDMP_MESSAGE_REQUEST=0, NDMP_MESSAGE_REPLY=1 } message_type; enum ndmp_message { NDMP_CONNECT_OPEN=2304, NDMP_CONNECT_CLIENT_AUTH=2305, NDMP_CONNECT_CLOSE=2306, NDMP_CONNECT_SERVER_AUTH=2307, NDMP_CONFIG_GET_HOST_INFO=256, NDMP_CONFIG_GET_CONNECTION_TYPE=258, NDMP_CONFIG_GET_AUTH_ATTR=259, NDMP_CONFIG_GET_BUTYPE_INFO=260, NDMP_CONFIG_GET_FS_INFO=261, NDMP_CONFIG_GET_TAPE_INFO=262, NDMP_CONFIG_GET_SCSI_INFO=263, NDMP_CONFIG_GET_SERVER_INFO=264, NDMP_SCSI_OPEN=512, NDMP_SCSI_CLOSE=513, NDMP_SCSI_GET_STATE=514, NDMP_SCSI_SET_TARGET=515, NDMP_SCSI_RESET_DEVICE=516, NDMP_SCSI_RESET_BUS=517, NDMP_SCSI_EXECUTE_CDB=518, NDMP_TAPE_OPEN=768, NDMP_TAPE_CLOSE=769, NDMP_TAPE_GET_STATE=770, NDMP_TAPE_MTIO=771, NDMP_TAPE_WRITE=772, NDMP_TAPE_READ=773, NDMP_TAPE_EXECUTE_CDB=775, NDMP_DATA_GET_STATE=1024, NDMP_DATA_START_BACKUP=1025, NDMP_DATA_START_RECOVER=1026, NDMP_DATA_ABORT=1027, NDMP_DATA_GET_ENV=1028, NDMP_DATA_STOP=1031, NDMP_DATA_LISTEN=1033, NDMP_DATA_CONNECT=1034, NDMP_NOTIFY_DATA_HALTED=1281, NDMP_NOTIFY_CONNECTED=1282, NDMP_NOTIFY_MOVER_HALTED=1283, NDMP_NOTIFY_MOVER_PAUSED=1284, NDMP_NOTIFY_DATA_READ=1285, NDMP_LOG_FILE=1538, NDMP_LOG_MESSAGE=1539, NDMP_FH_ADD_FILE=1795, NDMP_FH_ADD_DIR=1796, NDMP_FH_ADD_NODE=1797, NDMP_MOVER_GET_STATE=2560, NDMP_MOVER_LISTEN=2561, NDMP_MOVER_CONTINUE=2562, NDMP_MOVER_ABORT=2563, NDMP_MOVER_STOP=2564, NDMP_MOVER_SET_WINDOW=2565, NDMP_MOVER_READ=2566, NDMP_MOVER_CLOSE=2567, NDMP_MOVER_SET_RECORD_SIZE=2568, NDMP_MOVER_CONNECT=2569, NDMP_VENDORS_BASE=61440, NDMP_RESERVED_BASE=65280 } message; unsigned long reply_sequence; enum ndmp_error { NDMP_NO_ERR=0, NDMP_NOT_SUPPORTED_ERR=1, NDMP_DEVICE_BUSY_ERR=2, NDMP_DEVICE_OPENED_ERR=3, NDMP_NOT_AUTHORIZED_ERR=4, NDMP_PERMISSION_ERR=5, NDMP_DEV_NOT_OPEN_ERR=6, NDMP_IO_ERR=7, NDMP_TIMEOUT_ERR=8, NDMP_ILLEGAL_ARGS_ERR=9, NDMP_NO_TAPE_LOADED_ERR=10, NDMP_WRITE_PROTECT_ERR=11, NDMP_EOF_ERR=12, NDMP_EOM_ERR=13, NDMP_FILE_NOT_FOUND_ERR=14, NDMP_BAD_FILE_ERR=15, NDMP_NO_DEVICE_ERR=16, NDMP_NO_BUS_ERR=17, NDMP_XDR_DECODE_ERR=18, NDMP_ILLEGAL_STATE_ERR=19, NDMP_UNDEFINED_ERR=20, NDMP_XDR_ENCODE_ERR=21, NDMP_NO_MEM_ERR=22, NDMP_CONNECT_ERR=23 } error; }

variable_types: struct XDR { enum xdr_op { XDR_ENCODE=0, XDR_DECODE=1, XDR_FREE=2 } x_op; struct xdr_ops { int (*x_getlong)(struct XDR *__xdrs, long *__lp); int (*x_putlong)(struct XDR *__xdrs, long *__lp); int (*x_getbytes)(struct XDR *__xdrs, char *__addr, unsigned int __len); int (*x_putbytes)(struct XDR *__xdrs, char *__addr, unsigned int __len); unsigned int (*x_getpostn)(struct XDR *__xdrs); int (*x_setpostn)(struct XDR *__xdrs, unsigned int __pos); int *(*x_inline)(struct XDR *__xdrs, unsigned int __len); void (*x_destroy)(struct XDR *__xdrs); int (*x_getint32)(struct XDR *__xdrs, int *__ip); int (*x_putint32)(struct XDR *__xdrs, int *__ip); } *x_ops; char *x_public; char *x_private; char *x_base; unsigned int x_handy; } *

variable_types: struct ndmp_notify_connected_request { enum ndmp_connect_reason { NDMP_CONNECTED=0, NDMP_SHUTDOWN=1, NDMP_REFUSED=2 } reason; unsigned short protocol_version; char *text_reason; }

user_defined_types: struct ndmp_header { unsigned long sequence; unsigned long time_stamp; enum ndmp_header_message_type message_type; enum ndmp_message message; unsigned long reply_sequence; enum ndmp_error error; }

user_defined_types: struct ndmp_notify_connected_request { enum ndmp_connect_reason reason; unsigned short protocol_version; char *text_reason; }

user_defined_types: struct XDR { enum xdr_op x_op; struct xdr_ops *x_ops; char *x_public; char *x_private; char *x_base; unsigned int x_handy; }


repo: shishirng/ndmp

decompiled_code:
__int64 __fastcall set_header(__int64 a1, int a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, __int64 a7, int a8, __int64 a9)
{
time_t v9; // rax
__int64 result; // rax

*(_QWORD *)a1 = (int)get_next_seq_number(a1);
v9 = time(0LL);
*(_DWORD *)(a1 + 32) = a2;
*(_QWORD *)(a1 + 8) = v9;
*(_DWORD *)(a1 + 16) = 1;
*(_DWORD *)(a1 + 20) = HIDWORD(a9);
result = a7;
*(_QWORD *)(a1 + 24) = a7;
return result;
}

original_code:
void set_header(ndmp_header req, ndmp_header *reply, ndmp_error err)
{
        reply->sequence = get_next_seq_number();
        reply->time_stamp = (u_long) time(((void *)0));
        reply->message_type = NDMP_MESSAGE_REPLY;
        reply->message = req.message;
        reply->reply_sequence = req.sequence;
        reply->error = err;
}

canonical_original_code:
void set_header(struct ndmp_header req, struct ndmp_header *reply, enum ndmp_error err)
{
        reply->sequence = get_next_seq_number();
        reply->time_stamp = (unsigned long) time(((void *)0));
        reply->message_type = NDMP_MESSAGE_REPLY;
        reply->message = req.message;
        reply->reply_sequence = req.sequence;
        reply->error = err;
}

variable_types:
reply
struct ndmp_header *

variable_types: enum ndmp_error { NDMP_NO_ERR=0, NDMP_NOT_SUPPORTED_ERR=1, NDMP_DEVICE_BUSY_ERR=2, NDMP_DEVICE_OPENED_ERR=3, NDMP_NOT_AUTHORIZED_ERR=4, NDMP_PERMISSION_ERR=5, NDMP_DEV_NOT_OPEN_ERR=6, NDMP_IO_ERR=7, NDMP_TIMEOUT_ERR=8, NDMP_ILLEGAL_ARGS_ERR=9, NDMP_NO_TAPE_LOADED_ERR=10, NDMP_WRITE_PROTECT_ERR=11, NDMP_EOF_ERR=12, NDMP_EOM_ERR=13, NDMP_FILE_NOT_FOUND_ERR=14, NDMP_BAD_FILE_ERR=15, NDMP_NO_DEVICE_ERR=16, NDMP_NO_BUS_ERR=17, NDMP_XDR_DECODE_ERR=18, NDMP_ILLEGAL_STATE_ERR=19, NDMP_UNDEFINED_ERR=20, NDMP_XDR_ENCODE_ERR=21, NDMP_NO_MEM_ERR=22, NDMP_CONNECT_ERR=23 }

return_type: void

user_defined_types: enum ndmp_header_message_type { NDMP_MESSAGE_REQUEST=0, NDMP_MESSAGE_REPLY=1 }

user_defined_types: enum ndmp_message { NDMP_CONNECT_OPEN=2304, NDMP_CONNECT_CLIENT_AUTH=2305, NDMP_CONNECT_CLOSE=2306, NDMP_CONNECT_SERVER_AUTH=2307, NDMP_CONFIG_GET_HOST_INFO=256, NDMP_CONFIG_GET_CONNECTION_TYPE=258, NDMP_CONFIG_GET_AUTH_ATTR=259, NDMP_CONFIG_GET_BUTYPE_INFO=260, NDMP_CONFIG_GET_FS_INFO=261, NDMP_CONFIG_GET_TAPE_INFO=262, NDMP_CONFIG_GET_SCSI_INFO=263, NDMP_CONFIG_GET_SERVER_INFO=264, NDMP_SCSI_OPEN=512, NDMP_SCSI_CLOSE=513, NDMP_SCSI_GET_STATE=514, NDMP_SCSI_SET_TARGET=515, NDMP_SCSI_RESET_DEVICE=516, NDMP_SCSI_RESET_BUS=517, NDMP_SCSI_EXECUTE_CDB=518, NDMP_TAPE_OPEN=768, NDMP_TAPE_CLOSE=769, NDMP_TAPE_GET_STATE=770, NDMP_TAPE_MTIO=771, NDMP_TAPE_WRITE=772, NDMP_TAPE_READ=773, NDMP_TAPE_EXECUTE_CDB=775, NDMP_DATA_GET_STATE=1024, NDMP_DATA_START_BACKUP=1025, NDMP_DATA_START_RECOVER=1026, NDMP_DATA_ABORT=1027, NDMP_DATA_GET_ENV=1028, NDMP_DATA_STOP=1031, NDMP_DATA_LISTEN=1033, NDMP_DATA_CONNECT=1034, NDMP_NOTIFY_DATA_HALTED=1281, NDMP_NOTIFY_CONNECTED=1282, NDMP_NOTIFY_MOVER_HALTED=1283, NDMP_NOTIFY_MOVER_PAUSED=1284, NDMP_NOTIFY_DATA_READ=1285, NDMP_LOG_FILE=1538, NDMP_LOG_MESSAGE=1539, NDMP_FH_ADD_FILE=1795, NDMP_FH_ADD_DIR=1796, NDMP_FH_ADD_NODE=1797, NDMP_MOVER_GET_STATE=2560, NDMP_MOVER_LISTEN=2561, NDMP_MOVER_CONTINUE=2562, NDMP_MOVER_ABORT=2563, NDMP_MOVER_STOP=2564, NDMP_MOVER_SET_WINDOW=2565, NDMP_MOVER_READ=2566, NDMP_MOVER_CLOSE=2567, NDMP_MOVER_SET_RECORD_SIZE=2568, NDMP_MOVER_CONNECT=2569, NDMP_VENDORS_BASE=61440, NDMP_RESERVED_BASE=65280 }


decompiled_code:
bool_t __fastcall ndmp_error_message(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, __int64 a7, __int64 a8, __int64 a9)
{
u_int v9; // eax
char v11[48]; // [rsp+8h] [rbp-68h] BYREF
XDR xdrs; // [rsp+38h] [rbp-38h] BYREF

set_header((__int64)v11, 1, a3, a4, a5, a6, a7, a8, a9);
v9 = xdr_sizeof((xdrproc_t)&xdr_ndmp_header, v11);
*(_DWORD *)(a1 + 4132) = v9;
xdrmem_create(&xdrs, (const caddr_t)(a1 + 2084), v9, XDR_ENCODE);
return xdr_ndmp_header(&xdrs, v11);
}

canonical_decompiled_code:
bool_t __fastcall func5(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, __int64 a7, __int64 a8, __int64 a9)
{
u_int v9; // eax
char v11[48]; // [rsp+8h] [rbp-68h] BYREF
XDR xdrs; // [rsp+38h] [rbp-38h] BYREF

func4((__int64)v11, 1, a3, a4, a5, a6, a7, a8, a9);
v9 = xdr_sizeof((xdrproc_t)&xdr_ndmp_header, v11);
*(_DWORD *)(a1 + 4132) = v9;
xdrmem_create(&xdrs, (const caddr_t)(a1 + 2084), v9, XDR_ENCODE);
return xdr_ndmp_header(&xdrs, v11);
}

original_code:
void ndmp_error_message(struct client_txn *txn,
                        struct ndmp_header header,
                        XDR* requeststream)
{
        struct ndmp_header reply_header;
        XDR reply_stream;
        set_header(header, &reply_header, NDMP_NOT_SUPPORTED_ERR);
        txn->reply.length = xdr_sizeof((xdrproc_t)
                                       xdr_ndmp_header,
                                       &reply_header);
        xdrmem_create(&reply_stream, txn->reply.message, txn->reply.length,
                      XDR_ENCODE);
        xdr_ndmp_header(&reply_stream, &reply_header);
}

canonical_original_code:
void ndmp_error_message(struct client_txn *txn, struct ndmp_header header, struct XDR *requeststream)
{
        struct ndmp_header reply_header;
        struct XDR reply_stream;
        set_header(header, &reply_header, NDMP_NOT_SUPPORTED_ERR);
        txn->reply.length = xdr_sizeof((int (*)(struct XDR *, void *, ...))
                                       xdr_ndmp_header,
                                       &reply_header);
        xdrmem_create(&reply_stream, txn->reply.message, txn->reply.length,
                      XDR_ENCODE);
        xdr_ndmp_header(&reply_stream, &reply_header);
}

variable_types: struct client_txn { struct session { int session_id; struct client_endpoint { 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]; } client; int fd; } client_info; } client_session; struct comm_message { int is_tcp_connect; char message[2048]; int length; } request; struct comm_message reply; } *

variable_types: struct XDR { enum xdr_op { XDR_ENCODE=0, XDR_DECODE=1, XDR_FREE=2 } x_op; struct xdr_ops { int (*x_getlong)(struct XDR *__xdrs, long *__lp); int (*x_putlong)(struct XDR *__xdrs, long *__lp); int (*x_getbytes)(struct XDR *__xdrs, char *__addr, unsigned int __len); int (*x_putbytes)(struct XDR *__xdrs, char *__addr, unsigned int __len); unsigned int (*x_getpostn)(struct XDR *__xdrs); int (*x_setpostn)(struct XDR *__xdrs, unsigned int __pos); int *(*x_inline)(struct XDR *__xdrs, unsigned int __len); void (*x_destroy)(struct XDR *__xdrs); int (*x_getint32)(struct XDR *__xdrs, int *__ip); int (*x_putint32)(struct XDR *__xdrs, int *__ip); } *x_ops; char *x_public; char *x_private; char *x_base; unsigned int x_handy; }

user_defined_types: struct client_txn { struct session client_session; struct comm_message request; struct comm_message reply; }

user_defined_types: enum ndmp_error { NDMP_NO_ERR=0, NDMP_NOT_SUPPORTED_ERR=1, NDMP_DEVICE_BUSY_ERR=2, NDMP_DEVICE_OPENED_ERR=3, NDMP_NOT_AUTHORIZED_ERR=4, NDMP_PERMISSION_ERR=5, NDMP_DEV_NOT_OPEN_ERR=6, NDMP_IO_ERR=7, NDMP_TIMEOUT_ERR=8, NDMP_ILLEGAL_ARGS_ERR=9, NDMP_NO_TAPE_LOADED_ERR=10, NDMP_WRITE_PROTECT_ERR=11, NDMP_EOF_ERR=12, NDMP_EOM_ERR=13, NDMP_FILE_NOT_FOUND_ERR=14, NDMP_BAD_FILE_ERR=15, NDMP_NO_DEVICE_ERR=16, NDMP_NO_BUS_ERR=17, NDMP_XDR_DECODE_ERR=18, NDMP_ILLEGAL_STATE_ERR=19, NDMP_UNDEFINED_ERR=20, NDMP_XDR_ENCODE_ERR=21, NDMP_NO_MEM_ERR=22, NDMP_CONNECT_ERR=23 }

user_defined_types: struct session { int session_id; struct client_endpoint client_info; }

user_defined_types: struct client_endpoint { struct sockaddr_in client; int fd; }

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


decompiled_code:
__int64 get_session_queue()
{
__int64 v0; // rbp

v0 = get_lock();
enter_critical_section(v0);
if ( !session_info_queue_0 )
session_info_queue_0 = init_queue();
exit_critical_section(v0);
return session_info_queue_0;
}

canonical_decompiled_code:
__int64 func6()
{
__int64 v0; // rbp

v0 = get_lock();
enter_critical_section(v0);
if ( !session_info_queue_0 )
session_info_queue_0 = init_queue();
exit_critical_section(v0);
return session_info_queue_0;
}

original_code:
struct queue_hdr* get_session_queue()
{
        static struct queue_hdr *session_info_queue = ((void *)0);
        struct lock *s_lock = get_lock();
        enter_critical_section(s_lock);
        if (session_info_queue == ((void *)0)) {
                session_info_queue = init_queue();
        }
        exit_critical_section(s_lock);
        return session_info_queue;
}

canonical_original_code:
struct queue_hdr *get_session_queue()
{
        static struct queue_hdr *session_info_queue = ((void *)0);
        struct lock *s_lock = get_lock();
        enter_critical_section(s_lock);
        if (session_info_queue == ((void *)0)) {
                session_info_queue = init_queue();
        }
        exit_critical_section(s_lock);
        return session_info_queue;
}

variable_types: struct queue_hdr { struct queue_node { void *elem; struct queue_node *next; } *first; struct queue_node *last; struct lock { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; } *lock; int num_elems; } *

user_defined_types: struct queue_hdr { struct queue_node *first; struct queue_node *last; struct lock *lock; int num_elems; }

user_defined_types: struct lock { union pthread_mutex_t mutex; }

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

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


decompiled_code:
__int64 __fastcall add_job_to_session(int *a1)
{
_DWORD *v1; // rax

v1 = get_session_info(*a1);
return enqueue(*((_QWORD *)v1 + 3), a1);
}

canonical_decompiled_code:
__int64 __fastcall func7(int *a1)
{
_DWORD *v1; // rax

v1 = get_session_info(*a1);
return enqueue(*((_QWORD *)v1 + 3), a1);
}

original_code:
void add_job_to_session(struct client_txn *txn)
{
        struct ndmp_session_info *session;
        session = get_session_info(txn->client_session.session_id);
        enqueue(session->jobs, txn);
}

variable_types: struct ndmp_session_info { int session_id; enum ndmp_state { IDLE=0, LISTEN=1, CONNECTED=2, ACTIVE=3, HALTED=4 } connection_state; enum ndmp_state { IDLE=0, LISTEN=1, CONNECTED=2, ACTIVE=3, HALTED=4 } data_state; enum ndmp_state { IDLE=0, LISTEN=1, CONNECTED=2, ACTIVE=3, HALTED=4 } mover_state; struct lock { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; } *s_lock; struct queue_hdr { struct queue_node { void *elem; struct queue_node *next; } *first; struct queue_node *last; struct lock *lock; int num_elems; } *jobs; int is_terminated; } *

user_defined_types: struct ndmp_session_info { int session_id; enum ndmp_state connection_state; enum ndmp_state data_state; enum ndmp_state mover_state; struct lock *s_lock; struct queue_hdr *jobs; int is_terminated; }

user_defined_types: struct comm_message { int is_tcp_connect; char message[2048]; int length; }


decompiled_code:
__int64 __fastcall cleanup_session(int *a1)
{
_DWORD *v1; // rbp
unsigned int v2; // er12
__int64 v4; // rax

v1 = get_session_info(*a1);
remove_elem(*((_QWORD *)v1 + 3), a1, job_cmp);
v2 = v1[8];
if ( v2 )
{
v2 = 1;
if ( !(unsigned int)num_elems(*((_QWORD *)v1 + 3)) )
{
v4 = get_session_queue();
remove_elem(v4, v1, session_info_cmp);
free(v1);
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func8(int *a1)
{
_DWORD *v1; // rbp
unsigned int v2; // er12
__int64 v4; // rax

v1 = get_session_info(*a1);
remove_elem(*((_QWORD *)v1 + 3), a1, job_cmp);
v2 = v1[8];
if ( v2 )
{
v2 = 1;
if ( !(unsigned int)num_elems(*((_QWORD *)v1 + 3)) )
{
v4 = func6();
remove_elem(v4, v1, session_info_cmp);
free(v1);
}
}
return v2;
}

original_code:
int cleanup_session(struct client_txn *txn)
{
        struct ndmp_session_info *session;
        struct queue_hdr *session_queue;
        session = get_session_info(txn->client_session.session_id);
        remove_elem(session->jobs, txn, job_cmp);
        if (session->is_terminated) {
                if (num_elems(session->jobs) == 0) {
                        session_queue = get_session_queue();
                        remove_elem(session_queue,
                                    &session->session_id,
                                    session_info_cmp);
                        free(session);
                }
                return 1;
        }
        return 0;
}

return_type: int

user_defined_types: enum ndmp_state { IDLE=0, LISTEN=1, CONNECTED=2, ACTIVE=3, HALTED=4 }

user_defined_types: struct queue_node { void *elem; struct queue_node *next; }

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


decompiled_code:
void __fastcall xdr_decode_encode(void *ptr)
{
__int64 v1; // rbx
__int64 (__fastcall *v2)(); // rax
int v3; // er12
_DWORD *v4; // r13
int v5; // [rsp+8h] [rbp-C0h] BYREF
char addr[4]; // [rsp+Ch] [rbp-BCh] BYREF
__int64 v7; // [rsp+10h] [rbp-B8h] BYREF
int v8; // [rsp+24h] [rbp-A4h]
XDR xdrs; // [rsp+40h] [rbp-88h] BYREF
XDR v10; // [rsp+70h] [rbp-58h] BYREF

v1 = comm_context();
write_client_log(*(_QWORD *)((char *)ptr + 4), *(_QWORD *)((char *)ptr + 12), "Received request from client");
if ( *((_DWORD *)ptr + 6) == 1 )
{
v8 = 61441;
}
else
{
xdrmem_create(&xdrs, (const caddr_t)ptr + 32, *((_DWORD *)ptr + 519) - 4, XDR_DECODE);
xdr_ndmp_header(&xdrs, &v7);
write_client_log(
*(_QWORD *)((char *)ptr + 4),
*(_QWORD *)((char *)ptr + 12),
"Decoded NDMP header from client request");
}
add_job_to_session((int *)ptr);
write_client_log(
*(_QWORD *)((char *)ptr + 4),
*(_QWORD *)((char *)ptr + 12),
"Dispatched client request to appropriate message handler");
v2 = ndmp_dispatch(v8);
((void (__fastcall *)(void *, XDR *))v2)(ptr, &xdrs);
if ( (unsigned int)cleanup_session((int *)ptr) != 1 && (v3 = *((_DWORD *)ptr + 1033)) != 0 )
{
v4 = malloc(v3 + 4);
memcpy(v4 + 1, (char *)ptr + 2084, v3);
v5 = v3 | 0x80000000;
xdrmem_create(&v10, addr, 4u, XDR_ENCODE);
xdr_int(&v10, &v5);
*v4 = *(_DWORD *)addr;
memcpy((char *)ptr + 2084, v4, *((_DWORD *)ptr + 1033) + 4);
*((_DWORD *)ptr + 1033) += 4;
enqueue(*(_QWORD *)(v1 + 40), ptr);
free(v4);
}
else
{
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func9(void *ptr)
{
__int64 v1; // rbx
__int64 (__fastcall *v2)(); // rax
int v3; // er12
_DWORD *v4; // r13
int v5; // [rsp+8h] [rbp-C0h] BYREF
char addr[4]; // [rsp+Ch] [rbp-BCh] BYREF
__int64 v7; // [rsp+10h] [rbp-B8h] BYREF
int v8; // [rsp+24h] [rbp-A4h]
XDR xdrs; // [rsp+40h] [rbp-88h] BYREF
XDR v10; // [rsp+70h] [rbp-58h] BYREF

v1 = comm_context();
write_client_log(*(_QWORD *)((char *)ptr + 4), *(_QWORD *)((char *)ptr + 12), "Received request from client");
if ( *((_DWORD *)ptr + 6) == 1 )
{
v8 = 61441;
}
else
{
xdrmem_create(&xdrs, (const caddr_t)ptr + 32, *((_DWORD *)ptr + 519) - 4, XDR_DECODE);
xdr_ndmp_header(&xdrs, &v7);
write_client_log(
*(_QWORD *)((char *)ptr + 4),
*(_QWORD *)((char *)ptr + 12),
"Decoded NDMP header from client request");
}
func7((int *)ptr);
write_client_log(
*(_QWORD *)((char *)ptr + 4),
*(_QWORD *)((char *)ptr + 12),
"Dispatched client request to appropriate message handler");
v2 = ndmp_dispatch(v8);
((void (__fastcall *)(void *, XDR *))v2)(ptr, &xdrs);
if ( (unsigned int)func8((int *)ptr) != 1 && (v3 = *((_DWORD *)ptr + 1033)) != 0 )
{
v4 = malloc(v3 + 4);
memcpy(v4 + 1, (char *)ptr + 2084, v3);
v5 = v3 | 0x80000000;
xdrmem_create(&v10, addr, 4u, XDR_ENCODE);
xdr_int(&v10, &v5);
*v4 = *(_DWORD *)addr;
memcpy((char *)ptr + 2084, v4, *((_DWORD *)ptr + 1033) + 4);
*((_DWORD *)ptr + 1033) += 4;
enqueue(*(_QWORD *)(v1 + 40), ptr);
free(v4);
}
else
{
free(ptr);
}
}

original_code:
void xdr_decode_encode(struct client_txn *txn)
{
        XDR request_stream, stream_len;
        int message_type;
        struct ndmp_header header;
        char *buf, mesg_len[4];
        int len;
        struct comm_context *ctx = comm_context();
        write_client_log(txn->client_session.client_info.client, "Received request from client");
        if (txn->request.is_tcp_connect == 1) {
                header.message = 0xf001;
        }
        else {
                xdrmem_create(&request_stream, txn->request.message+4, txn->request.length-4,
                        XDR_DECODE);
                xdr_ndmp_header(&request_stream, &header);
                write_client_log(txn->client_session.client_info.client, "Decoded NDMP header from client request");
        }
        add_job_to_session(txn);
        write_client_log(txn->client_session.client_info.client, "Dispatched client request to appropriate message handler");
        ndmp_dispatch(header.message)(txn, header, &request_stream);
        if (cleanup_session(txn) == 1 || txn->reply.length == 0) {
                free(txn);
        }
        else {
                buf = (char *)malloc(txn->reply.length+4);
                memcpy(buf+4,txn->reply.message,txn->reply.length);
                len = txn->reply.length | (1<<31);
                xdrmem_create(&stream_len,mesg_len,4, XDR_ENCODE);
                xdr_int(&stream_len, &len);
                memcpy(buf,mesg_len,4);
                memcpy(txn->reply.message, buf, txn->reply.length+4);
                txn->reply.length +=4;
                enqueue(ctx->reply_jobs, txn);
                free(buf);
        }
}

canonical_original_code:
void xdr_decode_encode(struct client_txn *txn)
{
        struct XDR request_stream; struct XDR stream_len;
        int message_type;
        struct ndmp_header header;
        char *buf; char mesg_len[4];
        int len;
        struct comm_context *ctx = comm_context();
        write_client_log(txn->client_session.client_info.client, "Received request from client");
        if (txn->request.is_tcp_connect == 1) {
                header.message = 0xf001;
        }
        else {
                xdrmem_create(&request_stream, txn->request.message+4, txn->request.length-4,
                        XDR_DECODE);
                xdr_ndmp_header(&request_stream, &header);
                write_client_log(txn->client_session.client_info.client, "Decoded NDMP header from client request");
        }
        add_job_to_session(txn);
        write_client_log(txn->client_session.client_info.client, "Dispatched client request to appropriate message handler");
        ndmp_dispatch(header.message)(txn, header, &request_stream);
        if (cleanup_session(txn) == 1 || txn->reply.length == 0) {
                free(txn);
        }
        else {
                buf = (char *)malloc(txn->reply.length+4);
                memcpy(buf+4,txn->reply.message,txn->reply.length);
                len = txn->reply.length | (1<<31);
                xdrmem_create(&stream_len,mesg_len,4, XDR_ENCODE);
                xdr_int(&stream_len, &len);
                memcpy(buf,mesg_len,4);
                memcpy(txn->reply.message, buf, txn->reply.length+4);
                txn->reply.length +=4;
                enqueue(ctx->reply_jobs, txn);
                free(buf);
        }
}

variable_types:
buf
char *

variable_types: len int

variable_types: struct comm_context { void (*marshal_unmarshal)(struct client_txn { struct session { int session_id; struct client_endpoint { 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]; } client; int fd; } client_info; } client_session; struct comm_message { int is_tcp_connect; char message[2048]; int length; } request; struct comm_message reply; } *); int (*cleanup_session)(struct client_txn *); void (*terminate_session)(int session_id); struct queue_hdr { struct queue_node { void *elem; struct queue_node *next; } *first; struct queue_node *last; struct lock { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; } *lock; int num_elems; } *sessions; struct queue_hdr *request_jobs; struct queue_hdr *reply_jobs; int maxfds; } *

user_defined_types: struct XDR { enum xdr_op x_op; struct xdr_ops *x_ops; char *x_public; char *x_private; char *x_base; unsigned int x_handy; }

user_defined_types: struct ndmp_header { unsigned long sequence; unsigned long time_stamp; enum ndmp_header_message_type message_type; enum ndmp_message message; unsigned long reply_sequence; enum ndmp_error error; }

user_defined_types: enum xdr_op { XDR_ENCODE=0, XDR_DECODE=1, XDR_FREE=2 }

user_defined_types: struct comm_context { void (*marshal_unmarshal)(struct client_txn *); int (*cleanup_session)(struct client_txn *); void (*terminate_session)(int session_id); struct queue_hdr *sessions; struct queue_hdr *request_jobs; struct queue_hdr *reply_jobs; int maxfds; }

user_defined_types: struct xdr_ops { int (*x_getlong)(struct XDR *__xdrs, long *__lp); int (*x_putlong)(struct XDR *__xdrs, long *__lp); int (*x_getbytes)(struct XDR *__xdrs, char *__addr, unsigned int __len); int (*x_putbytes)(struct XDR *__xdrs, char *__addr, unsigned int __len); unsigned int (*x_getpostn)(struct XDR *__xdrs); int (*x_setpostn)(struct XDR *__xdrs, unsigned int __pos); int *(*x_inline)(struct XDR *__xdrs, unsigned int __len); void (*x_destroy)(struct XDR *__xdrs); int (*x_getint32)(struct XDR *__xdrs, int *__ip); int (*x_putint32)(struct XDR *__xdrs, int *__ip); }


decompiled_code:
int __fastcall dump_hex(__int64 a1, unsigned int a2, unsigned int a3)
{
unsigned int v4; // ebp
unsigned int v5; // ebx
__int64 v6; // rax
unsigned int v7; // er13
__int64 v8; // rax

v4 = a2;
v5 = a2 + 8;
do
{
while ( a3 <= v4 )
{
++v4;
printf(" ");
if ( v5 == v4 )
goto LABEL_5;
}
v6 = (int)v4++;
printf("%02x ", *(unsigned __int8 *)(a1 + v6));
}
while ( v5 != v4 );
LABEL_5:
v7 = a2 + 16;
putchar(32);
do
{
while ( v5 < a3 )
{
v8 = (int)v5++;
printf("%02x ", *(unsigned __int8 *)(a1 + v8));
if ( v7 == v5 )
return putchar(32);
}
++v5;
printf(" ");
}
while ( v7 != v5 );
return putchar(32);
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1, unsigned int a2, unsigned int a3)
{
unsigned int v4; // ebp
unsigned int v5; // ebx
__int64 v6; // rax
unsigned int v7; // er13
__int64 v8; // rax

v4 = a2;
v5 = a2 + 8;
do
{
while ( a3 <= v4 )
{
++v4;
printf(" ");
if ( v5 == v4 )
goto LABEL_5;
}
v6 = (int)v4++;
printf("%02x ", *(unsigned __int8 *)(a1 + v6));
}
while ( v5 != v4 );
LABEL_5:
v7 = a2 + 16;
putchar(32);
do
{
while ( v5 < a3 )
{
v8 = (int)v5++;
printf("%02x ", *(unsigned __int8 *)(a1 + v8));
if ( v7 == v5 )
return putchar(32);
}
++v5;
printf(" ");
}
while ( v7 != v5 );
return putchar(32);
}

original_code:
void dump_hex(unsigned char *buf, unsigned int whence, unsigned int size)
{
        int i, j, offset;
        for (i = 0; i < 2; i++) {
                for (j = 0; j < 8; j++) {
                        offset = whence + i * 8 + j;
                        if (offset < size) {
                                printf("%02x ", buf[offset]);
                        } else
                                printf("   ");
                }
                putchar(' ');
        }
}

canonical_original_code:
void dump_hex(unsigned char *buf, unsigned int whence, unsigned int size)
{
        int i; int j; int offset;
        for (i = 0; i < 2; i++) {
                for (j = 0; j < 8; j++) {
                        offset = whence + i * 8 + j;
                        if (offset < size) {
                                printf("%02x ", buf[offset]);
                        } else
                                printf("   ");
                }
                putchar(' ');
        }
}

variable_types:
buf
unsigned char *

variable_types:
whence
unsigned int

variable_types:
size
unsigned int

variable_types: i int

variable_types: j int

variable_types: offset int


decompiled_code:
int __fastcall dump_ascii(__int64 a1, unsigned int a2, unsigned int a3)
{
unsigned int v4; // ebx
char v5; // bp

v4 = a2;
putchar(124);
do
{
if ( v4 < a3 )
{
v5 = *(_BYTE *)(a1 + v4);
if ( ((*_ctype_b_loc())[v5] & 0x4000) == 0 )
v5 = 46;
putchar(v5);
}
++v4;
}
while ( a2 + 16 != v4 );
return puts("|");
}

canonical_decompiled_code:
int __fastcall func1(__int64 a1, unsigned int a2, unsigned int a3)
{
unsigned int v4; // ebx
char v5; // bp

v4 = a2;
putchar(124);
do
{
if ( v4 < a3 )
{
v5 = *(_BYTE *)(a1 + v4);
if ( ((*_ctype_b_loc())[v5] & 0x4000) == 0 )
v5 = 46;
putchar(v5);
}
++v4;
}
while ( a2 + 16 != v4 );
return puts("|");
}

original_code:
void dump_ascii(unsigned char *buf, unsigned int whence, unsigned int size)
{
        char c;
        int i;
        printf("|");
        for (i = 0; i < 16; i++) {
                if (i + whence < size) {
                        c = buf[i + whence];
                        putchar(((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISprint) ? c : '.');
                }
        }
        printf("|\n");
}

canonical_original_code:
void dump_ascii(unsigned char *buf, unsigned int whence, unsigned int size)
{
        char c;
        int i;
        printf("|");
        for (i = 0; i < 16; i++) {
                if (i + whence < size) {
                        c = buf[i + whence];
                        putchar(((*__ctype_b_loc ())[(int) ((c))] & (unsigned short) _ISprint) ? c : '.');
                }
        }
        printf("|\n");
}

variable_types: c char


hexdump

decompiled_code:
int __fastcall hexdump(__int64 a1, unsigned int a2)
{
unsigned int i; // ebx
int result; // eax

if ( a2 )
{
for ( i = 0; i < a2; i += 16 )
{
printf("%04x ", i);
dump_hex(a1, i, a2);
result = dump_ascii(a1, i, a2);
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func2(__int64 a1, unsigned int a2)
{
unsigned int i; // ebx
int result; // eax

if ( a2 )
{
for ( i = 0; i < a2; i += 16 )
{
printf("%04x ", i);
func0(a1, i, a2);
result = func1(a1, i, a2);
}
}
return result;
}

original_code:
void hexdump(void *buffer, unsigned int size)
{
        unsigned char *buf = (unsigned char*) buffer;
        int i, j;
        for (i = 0; i < size; i += 16) {
                printf("%04x  ", i);
                dump_hex(buf, i, size);
                dump_ascii(buf, i, size);
        }
}

canonical_original_code:
void hexdump(void *buffer, unsigned int size)
{
        unsigned char *buf = (unsigned char*) buffer;
        int i; int j;
        for (i = 0; i < size; i += 16) {
                printf("%04x  ", i);
                dump_hex(buf, i, size);
                dump_ascii(buf, i, size);
        }
}

variable_types:
buffer
void *


decompiled_code:
__int64 __fastcall add_a_job(__int64 a1, int a2)
{
_DWORD *v2; // rax

v2 = malloc(4uLL);
*v2 = a2;
return enqueue(a1, v2);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, int a2)
{
_DWORD *v2; // rax

v2 = malloc(4uLL);
*v2 = a2;
return enqueue(a1, v2);
}

original_code:
void add_a_job(struct queue_hdr *job_queue, int i)
{
 int *jobid = (int *) malloc(sizeof(int));
 *jobid = i;
 enqueue(job_queue, jobid);
}

variable_types:
jobid
int *


decompiled_code:
// attributes: thunk
__int64 get_job_from_queue()
{
return dequeue();
}

canonical_decompiled_code:
__int64 func2()
{
return dequeue();
}

original_code:
int *get_job_from_queue(struct queue_hdr *job_queue)
{
 return dequeue(job_queue);
}

return_type: int *


decompiled_code:
void __fastcall check_get_from_queue(__int64 a1, int a2)
{
_DWORD *v2; // r8
int v3; // eax
int v4[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v4[0] = 0;
if ( a2 > 0 )
{
do
{
v2 = (_DWORD *)get_elem(a1, v4, queue_cmp);
v3 = v4[0];
if ( *v2 != v4[0] )
_assert_fail("i == *(int *)get_elem(queue,&i,queue_cmp)", "testq.c", 0x50u, "check_get_from_queue");
++v4[0];
}
while ( v3 + 1 < a2 );
}
}

canonical_decompiled_code:
void __fastcall func5(__int64 a1, int a2)
{
_DWORD *v2; // r8
int v3; // eax
int v4[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v4[0] = 0;
if ( a2 > 0 )
{
do
{
v2 = (_DWORD *)get_elem(a1, v4, queue_cmp);
v3 = v4[0];
if ( *v2 != v4[0] )
_assert_fail("i == *(int *)get_elem(queue,&i,queue_cmp)", "testq.c", 0x50u, "check_get_from_queue");
++v4[0];
}
while ( v3 + 1 < a2 );
}
}

original_code:
void check_get_from_queue(struct queue_hdr *queue, int n)
{
 int i;
 for (i=0; i<n; i++)
  ((void) sizeof ((i == *(int *)get_elem(queue,&i,queue_cmp)) ? 1 : 0), __extension__ ({ if (i == *(int *)get_elem(queue,&i,queue_cmp)) ; else __assert_fail ("i == *(int *)get_elem(queue,&i,queue_cmp)", "testq.c", 80, __extension__ __PRETTY_FUNCTION__); }));
}

variable_types: n int


decompiled_code:
__int64 __fastcall check_remove_from_queue(__int64 a1, int a2)
{
__int64 result; // rax
int v3[7]; // [rsp+Ch] [rbp-1Ch] BYREF

result = set_up_job_queue(a1, a2);
v3[0] = 0;
if ( a2 > 0 )
{
do
{
remove_elem(a1, v3, queue_cmp);
if ( get_elem(a1, v3, queue_cmp) )
_assert_fail("get_elem(queue,&i,queue_cmp) == NULL", "testq.c", 0x59u, "check_remove_from_queue");
result = (unsigned int)(v3[0] + 1);
v3[0] = result;
}
while ( (int)result < a2 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, int a2)
{
__int64 result; // rax
int v3[7]; // [rsp+Ch] [rbp-1Ch] BYREF

result = func3(a1, a2);
v3[0] = 0;
if ( a2 > 0 )
{
do
{
remove_elem(a1, v3, queue_cmp);
if ( get_elem(a1, v3, queue_cmp) )
_assert_fail("get_elem(queue,&i,queue_cmp) == NULL", "testq.c", 0x59u, "check_remove_from_queue");
result = (unsigned int)(v3[0] + 1);
v3[0] = result;
}
while ( (int)result < a2 );
}
return result;
}

original_code:
void check_remove_from_queue(struct queue_hdr *queue, int n)
{
 int i;
 set_up_job_queue(queue,n);
 for (i=0; i<n; i++) {
  remove_elem(queue,&i,queue_cmp);
  ((void) sizeof ((get_elem(queue,&i,queue_cmp) == ((void *)0)) ? 1 : 0), __extension__ ({ if (get_elem(queue,&i,queue_cmp) == ((void *)0)) ; else __assert_fail ("get_elem(queue,&i,queue_cmp) == NULL", "testq.c", 89, __extension__ __PRETTY_FUNCTION__); }));
 }
}


decompiled_code:
_BOOL8 __fastcall session_comparator(_DWORD *a1, _DWORD *a2)
{
return *a1 == *a2;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(_DWORD *a1, _DWORD *a2)
{
return *a1 == *a2;
}

original_code:
int session_comparator(void *target, void *elem)
{
        int target_id;
        int elem_id;
        target_id = ((struct session *) target)->session_id;
        elem_id = ((struct session *) elem)->session_id;
        return elem_id == target_id;
}

variable_types:
target
void *

variable_types:
elem
void *

user_defined_types: struct session { int session_id; struct client_endpoint client_info; }


decompiled_code:
void __fastcall sig_handler(int a1)
{
if ( a1 == 2 )
{
puts("\nReceived SIGINT, closing server");
write_server_log("Closed NDMP server\n");
exit(0);
}
}

canonical_decompiled_code:
void __fastcall func1(int a1)
{
if ( a1 == 2 )
{
puts("\nReceived SIGINT, closing server");
write_server_log("Closed NDMP server\n");
exit(0);
}
}

original_code:
void sig_handler(int signo)
{
       if (signo == 2){
                printf("\nReceived SIGINT, closing server\n");
                write_server_log("Closed NDMP server\n");
                exit(0);
       }
}

variable_types: signo int


decompiled_code:
__int64 comm_context()
{
__int64 result; // rax
__int64 v1; // rbx
__int64 v2; // rbx

result = retval_0;
if ( !retval_0 )
{
retval_0 = (__int64)calloc(1uLL, 0x38uLL);
*(_QWORD *)(retval_0 + 24) = init_queue();
v1 = retval_0;
*(_QWORD *)(v1 + 32) = init_queue();
v2 = retval_0;
*(_QWORD *)(v2 + 40) = init_queue();
result = retval_0;
}
return result;
}

canonical_decompiled_code:
__int64 func3()
{
__int64 result; // rax
__int64 v1; // rbx
__int64 v2; // rbx

result = retval_0;
if ( !retval_0 )
{
retval_0 = (__int64)calloc(1uLL, 0x38uLL);
*(_QWORD *)(retval_0 + 24) = init_queue();
v1 = retval_0;
*(_QWORD *)(v1 + 32) = init_queue();
v2 = retval_0;
*(_QWORD *)(v2 + 40) = init_queue();
result = retval_0;
}
return result;
}

original_code:
struct comm_context* comm_context()
{
        static struct comm_context *retval = ((void *)0);
        if (retval == ((void *)0) ) {
                retval = calloc(1, sizeof(struct comm_context));
                retval->sessions = init_queue();
                retval->request_jobs = init_queue();
                retval->reply_jobs = init_queue();
        }
        return retval;
}

canonical_original_code:
struct comm_context *comm_context()
{
        static struct comm_context *retval = ((void *)0);
        if (retval == ((void *)0) ) {
                retval = calloc(1, sizeof(struct comm_context));
                retval->sessions = init_queue();
                retval->request_jobs = init_queue();
                retval->reply_jobs = init_queue();
        }
        return retval;
}

return_type: struct comm_context { void (*marshal_unmarshal)(struct client_txn { struct session { int session_id; struct client_endpoint { 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]; } client; int fd; } client_info; } client_session; struct comm_message { int is_tcp_connect; char message[2048]; int length; } request; struct comm_message reply; } *); int (*cleanup_session)(struct client_txn *); void (*terminate_session)(int session_id); struct queue_hdr { struct queue_node { void *elem; struct queue_node *next; } *first; struct queue_node *last; struct lock { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; } *lock; int num_elems; } *sessions; struct queue_hdr *request_jobs; struct queue_hdr *reply_jobs; int maxfds; } *

user_defined_types: struct client_txn { struct session client_session; struct comm_message request; struct comm_message reply; }

user_defined_types: struct client_endpoint { struct sockaddr_in client; int fd; }


original_code:
void set_fd_flags(fd_set *fds, int listener, struct comm_context *ctx)
{
        struct session **active_sessions;
        int n, i;
        do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((fds)->__fds_bits)[0]) : "memory"); } while (0);
        ((void) (((fds)->__fds_bits)[((listener) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((listener) % (8 * (int) sizeof (__fd_mask)))))));
        active_sessions = (struct session **) get_all_elems(ctx->sessions);
        n = num_elems(ctx->sessions);
        for (i = 0; i < n; i++) {
                ((void) (((fds)->__fds_bits)[((active_sessions[i]->session_id) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((active_sessions[i]->session_id) % (8 * (int) sizeof (__fd_mask)))))));
        }
}

canonical_original_code:
void set_fd_flags(struct fd_set *fds, int listener, struct comm_context *ctx)
{
        struct session **active_sessions;
        int n; int i;
        do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((fds)->__fds_bits)[0]) : "memory"); } while (0);
        ((void) (((fds)->__fds_bits)[((listener) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((listener) % (8 * (int) sizeof (__fd_mask)))))));
        active_sessions = (struct session **) get_all_elems(ctx->sessions);
        n = num_elems(ctx->sessions);
        for (i = 0; i < n; i++) {
                ((void) (((fds)->__fds_bits)[((active_sessions[i]->session_id) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((active_sessions[i]->session_id) % (8 * (int) sizeof (__fd_mask)))))));
        }
}

variable_types: struct fd_set { long __fds_bits[-1]; } *

variable_types: listener int

variable_types: struct session { int session_id; struct client_endpoint { 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]; } client; int fd; } client_info; } **

user_defined_types: struct queue_hdr { struct queue_node *first; struct queue_node *last; struct lock *lock; int num_elems; }

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


decompiled_code:
__int64 __fastcall accept_all_connections(int a1, __int64 a2, __int64 a3)
{
int v4; // ebx
__int64 result; // rax
char *v6; // rax
__m128i v7; // xmm0
__int64 v8; // rdi
const __m128i *v9; // r12
unsigned __int16 v10; // bx
char *v11; // rax
__m128i *v12; // rax
__int64 v13; // rdi
__m128i *v14; // rsi
__int64 v15; // rax
struct in_addr in[4]; // [rsp+0h] [rbp-48h] BYREF
int v17; // [rsp+10h] [rbp-38h]

accept_connection((__m128i *)in, a1);
v4 = v17;
result = 0LL;
if ( v17 != -1 )
{
v6 = (char *)malloc(0x18uLL);
v7 = _mm_loadu_si128((const __m128i *)in);
v8 = *(_QWORD *)(a3 + 24);
*(_DWORD *)v6 = v4;
v9 = (const __m128i *)v6;
*(__m128i *)(v6 + 4) = v7;
*((_DWORD *)v6 + 5) = v17;
enqueue(v8, v6);
*(_QWORD *)(a2 + 8LL * (v4 / 64)) |= 1LL << (v4 % 64);
if ( *(_DWORD *)(a3 + 48) < v4 )
*(_DWORD *)(a3 + 48) = v4;
write_client_log(*(_QWORD *)&in[0].s_addr, *(_QWORD *)&in[2].s_addr, "Client connection accepted");
write_server_log("Client connection accepted");
v10 = _bswap_16(SHIWORD(in[0].s_addr));
v11 = inet_ntoa(in[1]);
printf("Connection request from %s at %d\n", v11, v10);
v12 = (__m128i *)malloc(0x1028uLL);
v13 = *(_QWORD *)(a3 + 32);
v14 = v12;
*v12 = _mm_loadu_si128(v9);
v15 = v9[1].m128i_i64[0];
v14[1].m128i_i32[2] = 1;
v14[1].m128i_i64[0] = v15;
enqueue(v13, v14);
result = 1LL;
}
return result;
}

original_code:
int accept_all_connections(int listener, fd_set *read_socks,
                struct comm_context *ctx)
{
        struct client_endpoint endpoint;
        struct session* new_session;
        struct client_txn* txn;
        int num_new_sessions = 0;
        endpoint = accept_connection(listener);
        if (endpoint.fd != -1) {
                new_session = (struct session*) malloc(sizeof(struct session));
                new_session->session_id = endpoint.fd;
                new_session->client_info = endpoint;
                enqueue(ctx->sessions, new_session);
                ((void) (((read_socks)->__fds_bits)[((endpoint.fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((endpoint.fd) % (8 * (int) sizeof (__fd_mask)))))));
                if (endpoint.fd > ctx->maxfds) {
                        ctx->maxfds = endpoint.fd;
                }
                write_client_log(endpoint.client, "Client connection accepted");
                write_server_log("Client connection accepted");
                printf("Connection request from %s at %d\n",
                                inet_ntoa(endpoint.client.sin_addr),
                                ntohs(endpoint.client.sin_port));
                txn = (struct client_txn *) malloc(sizeof(struct client_txn));
                txn->client_session = *new_session;
                txn->request.is_tcp_connect = 1;
                enqueue(ctx->request_jobs, txn);
                num_new_sessions++;
        }
        return num_new_sessions;
}

canonical_original_code:
int accept_all_connections(int listener, struct fd_set *read_socks, struct comm_context *ctx)
{
        struct client_endpoint endpoint;
        struct session *new_session;
        struct client_txn *txn;
        int num_new_sessions = 0;
        endpoint = accept_connection(listener);
        if (endpoint.fd != -1) {
                new_session = (struct session *) malloc(sizeof(struct session));
                new_session->session_id = endpoint.fd;
                new_session->client_info = endpoint;
                enqueue(ctx->sessions, new_session);
                ((void) (((read_socks)->__fds_bits)[((endpoint.fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((endpoint.fd) % (8 * (int) sizeof (__fd_mask)))))));
                if (endpoint.fd > ctx->maxfds) {
                        ctx->maxfds = endpoint.fd;
                }
                write_client_log(endpoint.client, "Client connection accepted");
                write_server_log("Client connection accepted");
                printf("Connection request from %s at %d\n",
                                inet_ntoa(endpoint.client.sin_addr),
                                ntohs(endpoint.client.sin_port));
                txn = (struct client_txn *) malloc(sizeof(struct client_txn));
                txn->client_session = *new_session;
                txn->request.is_tcp_connect = 1;
                enqueue(ctx->request_jobs, txn);
                num_new_sessions++;
        }
        return num_new_sessions;
}

variable_types: struct comm_context { void (*marshal_unmarshal)(struct client_txn { struct session { int session_id; struct client_endpoint { 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]; } client; int fd; } client_info; } client_session; struct comm_message { int is_tcp_connect; char message[2048]; int length; } request; struct comm_message reply; } *); int (*cleanup_session)(struct client_txn *); void (*terminate_session)(int session_id); struct queue_hdr { struct queue_node { void *elem; struct queue_node *next; } *first; struct queue_node *last; struct lock { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; } *lock; int num_elems; } *sessions; struct queue_hdr *request_jobs; struct queue_hdr *reply_jobs; int maxfds; } *

variable_types: struct session { int session_id; struct client_endpoint { 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]; } client; int fd; } client_info; } *

user_defined_types: struct lock { union pthread_mutex_t mutex; }

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


decompiled_code:
int __fastcall handle_a_client_request(int fd, __int64 a2)
{
_QWORD *v2; // rbp
const __m128i *v3; // rax
__int64 v4; // rax
int v5; // eax
int result; // eax
_BYTE v7[48]; // [rsp+0h] [rbp-38h] BYREF

*(_DWORD *)v7 = fd;
v2 = malloc(0x1028uLL);
v3 = (const __m128i *)get_elem(*(_QWORD *)(a2 + 24), v7, session_comparator);
*(__m128i *)v7 = _mm_loadu_si128(v3);
v4 = v3[1].m128i_i64[0];
*(_OWORD *)v2 = *(_OWORD *)v7;
*(_QWORD *)&v7[16] = v4;
v2[2] = v4;
v5 = recv(fd, (char *)v2 + 28, 0x800uLL, 0);
*((_DWORD *)v2 + 6) = 0;
*((_DWORD *)v2 + 519) = v5;
if ( v5 )
{
enqueue(*(_QWORD *)(a2 + 32), v2);
write_server_log("Created a new job on socket at server");
result = printf("\tcreated a new job on %d [%d bytes]\n", (unsigned int)fd, *((unsigned int *)v2 + 519));
}
else
{
close(fd);
remove_elem(*(_QWORD *)(a2 + 24), v7, session_comparator);
(*(void (__fastcall **)(_QWORD))(a2 + 16))((unsigned int)fd);
free(v2);
write_client_log(*(_QWORD *)&v7[4], *(_QWORD *)&v7[12], "Connection closed by client\n");
result = puts("Detected socket closure");
}
return result;
}

canonical_decompiled_code:
int __fastcall func6(int fd, __int64 a2)
{
_QWORD *v2; // rbp
const __m128i *v3; // rax
__int64 v4; // rax
int v5; // eax
int result; // eax
_BYTE v7[48]; // [rsp+0h] [rbp-38h] BYREF

*(_DWORD *)v7 = fd;
v2 = malloc(0x1028uLL);
v3 = (const __m128i *)get_elem(*(_QWORD *)(a2 + 24), v7, session_comparator);
*(__m128i *)v7 = _mm_loadu_si128(v3);
v4 = v3[1].m128i_i64[0];
*(_OWORD *)v2 = *(_OWORD *)v7;
*(_QWORD *)&v7[16] = v4;
v2[2] = v4;
v5 = recv(fd, (char *)v2 + 28, 0x800uLL, 0);
*((_DWORD *)v2 + 6) = 0;
*((_DWORD *)v2 + 519) = v5;
if ( v5 )
{
enqueue(*(_QWORD *)(a2 + 32), v2);
write_server_log("Created a new job on socket at server");
result = printf("\tcreated a new job on %d [%d bytes]\n", (unsigned int)fd, *((unsigned int *)v2 + 519));
}
else
{
close(fd);
remove_elem(*(_QWORD *)(a2 + 24), v7, session_comparator);
(*(void (__fastcall **)(_QWORD))(a2 + 16))((unsigned int)fd);
free(v2);
write_client_log(*(_QWORD *)&v7[4], *(_QWORD *)&v7[12], "Connection closed by client\n");
result = puts("Detected socket closure");
}
return result;
}

original_code:
void handle_a_client_request(int fd, struct comm_context *ctx)
{
        struct session tmp;
        tmp.session_id = fd;
        struct client_txn* txn;
        int n;
        txn = (struct client_txn *) malloc(sizeof(struct client_txn));
        tmp = *(struct session *) get_elem(ctx->sessions, &tmp,
                session_comparator);
        txn->client_session = tmp;
        txn->request.length = recv(fd, txn->request.message, 2048,
                        0);
        txn->request.is_tcp_connect = 0;
        if (txn->request.length == 0) {
                close(fd);
                remove_elem(ctx->sessions, &tmp, session_comparator);
                ctx->terminate_session(fd);
                free(txn);
                write_client_log(tmp.client_info.client, "Connection closed by client\n");
                printf("Detected socket closure\n");
                return;
        }
        enqueue(ctx->request_jobs, txn);
        write_server_log("Created a new job on socket at server");
        printf("\tcreated a new job on %d [%d bytes]\n", fd,
                        txn->request.length);
}

canonical_original_code:
void handle_a_client_request(int fd, struct comm_context *ctx)
{
        struct session tmp;
        tmp.session_id = fd;
        struct client_txn *txn;
        int n;
        txn = (struct client_txn *) malloc(sizeof(struct client_txn));
        tmp = *(struct session *) get_elem(ctx->sessions, &tmp,
                session_comparator);
        txn->client_session = tmp;
        txn->request.length = recv(fd, txn->request.message, 2048,
                        0);
        txn->request.is_tcp_connect = 0;
        if (txn->request.length == 0) {
                close(fd);
                remove_elem(ctx->sessions, &tmp, session_comparator);
                ctx->terminate_session(fd);
                free(txn);
                write_client_log(tmp.client_info.client, "Connection closed by client\n");
                printf("Detected socket closure\n");
                return;
        }
        enqueue(ctx->request_jobs, txn);
        write_server_log("Created a new job on socket at server");
        printf("\tcreated a new job on %d [%d bytes]\n", fd,
                        txn->request.length);
}

variable_types: fd int

variable_types: struct client_txn { struct session { int session_id; struct client_endpoint { 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]; } client; int fd; } client_info; } client_session; struct comm_message { int is_tcp_connect; char message[2048]; int length; } request; struct comm_message reply; } *

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


decompiled_code:
int __fastcall handle_client_requests(__int64 a1, __int64 a2)
{
__int64 v2; // rax
int v3; // er12
int v5; // edi

LODWORD(v2) = *(_DWORD *)(a2 + 48);
if ( (int)v2 >= 0 )
{
v3 = 0;
do
{
while ( 1 )
{
printf("Checking for event on sock %d\n", (unsigned int)v3);
v2 = *(_QWORD *)(a1 + 8LL * (v3 >> 6)) & (1LL << v3);
if ( v2 )
break;
if ( *(_DWORD *)(a2 + 48) < ++v3 )
return v2;
}
v5 = v3++;
LODWORD(v2) = handle_a_client_request(v5, a2);
}
while ( *(_DWORD *)(a2 + 48) >= v3 );
}
return v2;
}

canonical_decompiled_code:
int __fastcall func7(__int64 a1, __int64 a2)
{
__int64 v2; // rax
int v3; // er12
int v5; // edi

LODWORD(v2) = *(_DWORD *)(a2 + 48);
if ( (int)v2 >= 0 )
{
v3 = 0;
do
{
while ( 1 )
{
printf("Checking for event on sock %d\n", (unsigned int)v3);
v2 = *(_QWORD *)(a1 + 8LL * (v3 >> 6)) & (1LL << v3);
if ( v2 )
break;
if ( *(_DWORD *)(a2 + 48) < ++v3 )
return v2;
}
v5 = v3++;
LODWORD(v2) = func6(v5, a2);
}
while ( *(_DWORD *)(a2 + 48) >= v3 );
}
return v2;
}

original_code:
void handle_client_requests(fd_set *read_socks, struct comm_context *ctx)
{
        int i;
        for (i = 0; i <= ctx->maxfds; i++) {
                printf("Checking for event on sock %d\n", i);
                if (((((read_socks)->__fds_bits)[((i) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((i) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
                        handle_a_client_request(i, ctx);
                }
        }
}

canonical_original_code:
void handle_client_requests(struct fd_set *read_socks, struct comm_context *ctx)
{
        int i;
        for (i = 0; i <= ctx->maxfds; i++) {
                printf("Checking for event on sock %d\n", i);
                if (((((read_socks)->__fds_bits)[((i) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((i) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
                        handle_a_client_request(i, ctx);
                }
        }
}

variable_types: i int

user_defined_types: struct comm_message { int is_tcp_connect; char message[2048]; int length; }


errExit

decompiled_code:
void __fastcall __noreturn errExit(const char *a1)
{
fprintf(stderr, "%s\n", a1);
exit(-1);
}

canonical_decompiled_code:
void __fastcall __noreturn func8(const char *a1)
{
fprintf(stderr, "%s\n", a1);
exit(-1);
}

original_code:
int errExit(char *s)
{
        fprintf(stderr, "%s\n", s);
        exit(-1);
}

variable_types:
s
char *


decompiled_code:
__int64 __fastcall create_listener(__int16 a1)
{
int v1; // edx
unsigned int v2; // er12
int optval; // [rsp+Ch] [rbp-1Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-18h] BYREF

optval = 1;
addr.sa_family = 2;
*(_DWORD *)&addr.sa_data[2] = 0;
*(_WORD *)addr.sa_data = _bswap_16(a1);
v2 = socket(2, 1, v1);
setsockopt(v2, 1, 2, &optval, 4u);
if ( bind(v2, &addr, 0x10u) == -1 )
errExit("Error:bind");
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int16 a1)
{
int v1; // edx
unsigned int v2; // er12
int optval; // [rsp+Ch] [rbp-1Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-18h] BYREF

optval = 1;
addr.sa_family = 2;
*(_DWORD *)&addr.sa_data[2] = 0;
*(_WORD *)addr.sa_data = func2(a1);
v2 = socket(2, 1, v1);
setsockopt(v2, 1, 2, &optval, 4u);
if ( bind(v2, &addr, 0x10u) == -1 )
func8("Error:bind");
return v2;
}

original_code:
int create_listener(int port) {
        int retval;
        struct sockaddr_in listener;
        int reuse = 1;
        listener.sin_family = 2;
        listener.sin_addr.s_addr = ((in_addr_t) 0x00000000);
        listener.sin_port = htons(port);
        retval = socket(2, SOCK_STREAM, 0);
        setsockopt(retval, 1, 2, &reuse, sizeof(int));
        if (bind(retval, (struct sockaddr *) &listener,
                        sizeof(struct sockaddr_in)) == -1)
                errExit("Error:bind");
        return retval;
}

canonical_original_code:
int create_listener(int port) {
        int retval;
        struct sockaddr_in listener;
        int reuse = 1;
        listener.sin_family = 2;
        listener.sin_addr.s_addr = ((unsigned int) 0x00000000);
        listener.sin_port = htons(port);
        retval = socket(2, SOCK_STREAM, 0);
        setsockopt(retval, 1, 2, &reuse, sizeof(int));
        if (bind(retval, (struct sockaddr *) &listener,
                        sizeof(struct sockaddr_in)) == -1)
                errExit("Error:bind");
        return retval;
}

variable_types: port int

variable_types: retval int

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

variable_types: reuse int

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


decompiled_code:
void __fastcall __noreturn comm_listen(__int64 a1)
{
int v1; // er13
int v2; // er14
__int64 v3; // r14
__int64 v4; // r15
int v5; // ebx
unsigned int v6; // eax
char v7[32]; // [rsp+0h] [rbp-D8h] BYREF
fd_set readfds; // [rsp+20h] [rbp-B8h] BYREF

create_listener(10000);
write_server_log("Started NDMP server");
memset(&readfds, 0, sizeof(readfds));
v1 = create_listener(10000);
write_server_log("Created listener");
v2 = v1 + 63;
listen(v1, 512);
sprintf(v7, "Listening on port %d", 10000LL);
write_server_log(v7);
printf("Listening on port %d....\n", 10000LL);
*(_DWORD *)(a1 + 48) = v1;
if ( v1 >= 0 )
v2 = v1;
v3 = v2 >> 6;
v4 = 1LL << (v1 % 64);
readfds.fds_bits[v3] |= v4;
create_worker_threads(1LL);
write_server_log("Created worker threads");
signal(2, sig_handler);
while ( 1 )
{
do
{
puts("Wait on select ");
write_server_log("Wait on select");
set_fd_flags(&readfds, v1, a1);
v5 = select(*(_DWORD *)(a1 + 48) + 1, &readfds, 0LL, 0LL, 0LL);
puts("Woke up from select ");
write_server_log("Woke up from select");
}
while ( v5 == -1 );
if ( (readfds.fds_bits[v3] & v4) != 0 )
{
v6 = accept_all_connections(v1, (__int64)&readfds, a1);
printf("%d new connection(s) \n", v6);
write_server_log("Detected 1 new connection");
}
else
{
handle_client_requests((__int64)&readfds, a1);
write_server_log("Received client request");
}
}
}

canonical_decompiled_code:
void __fastcall __noreturn func10(__int64 a1)
{
int v1; // er13
int v2; // er14
__int64 v3; // r14
__int64 v4; // r15
int v5; // ebx
unsigned int v6; // eax
char v7[32]; // [rsp+0h] [rbp-D8h] BYREF
fd_set readfds; // [rsp+20h] [rbp-B8h] BYREF

func9(10000);
write_server_log("Started NDMP server");
memset(&readfds, 0, sizeof(readfds));
v1 = func9(10000);
write_server_log("Created listener");
v2 = v1 + 63;
listen(v1, 512);
sprintf(v7, "Listening on port %d", 10000LL);
write_server_log(v7);
printf("Listening on port %d....\n", 10000LL);
*(_DWORD *)(a1 + 48) = v1;
if ( v1 >= 0 )
v2 = v1;
v3 = v2 >> 6;
v4 = 1LL << (v1 % 64);
readfds.fds_bits[v3] |= v4;
create_worker_threads(1LL);
write_server_log("Created worker threads");
signal(2, sig_handler);
while ( 1 )
{
do
{
puts("Wait on select ");
write_server_log("Wait on select");
func4(&readfds, v1, a1);
v5 = select(*(_DWORD *)(a1 + 48) + 1, &readfds, 0LL, 0LL, 0LL);
puts("Woke up from select ");
write_server_log("Woke up from select");
}
while ( v5 == -1 );
if ( (readfds.fds_bits[v3] & v4) != 0 )
{
v6 = func5(v1, (__int64)&readfds, a1);
printf("%d new connection(s) \n", v6);
write_server_log("Detected 1 new connection");
}
else
{
func7((__int64)&readfds, a1);
write_server_log("Received client request");
}
}
}

original_code:
void comm_listen(struct comm_context *ctx)
{
        fd_set read_socks;
        int listener = create_listener(10000);
        struct client_endpoint endpoint;
        struct client_endpoint *temp;
        int ready;
        int new_connections;
        char message[32];
        write_server_log("Started NDMP server");
        do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&read_socks)->__fds_bits)[0]) : "memory"); } while (0);
        listener = create_listener(10000);
        write_server_log("Created listener");
        listen(listener, 512);
        sprintf(message, "Listening on port %d", 10000 );
        write_server_log(message);
        printf("Listening on port %d....\n", 10000);
        ((void) (((&read_socks)->__fds_bits)[((listener) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((listener) % (8 * (int) sizeof (__fd_mask)))))));
        ctx->maxfds = listener;
        create_worker_threads(1);
        write_server_log("Created worker threads");
        signal(2, sig_handler);
        for (;;) {
                printf("Wait on select \n");
                write_server_log("Wait on select");
                set_fd_flags(&read_socks, listener, ctx);
                ready = select((ctx->maxfds) + 1, &read_socks, ((void *)0), ((void *)0),
                                ((void *)0) );
                printf("Woke up from select \n");
                write_server_log("Woke up from select");
                if (ready != -1) {
                        if (((((&read_socks)->__fds_bits)[((listener) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((listener) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
                                new_connections = accept_all_connections(
                                        listener, &read_socks, ctx);
                                printf("%d new connection(s) \n",
                                                new_connections);
                                write_server_log("Detected 1 new connection");
                                } else {
                                handle_client_requests(&read_socks, ctx);
                                write_server_log("Received client request");
                                }
                }
        }
}

canonical_original_code:
void comm_listen(struct comm_context *ctx)
{
        struct fd_set read_socks;
        int listener = create_listener(10000);
        struct client_endpoint endpoint;
        struct client_endpoint *temp;
        int ready;
        int new_connections;
        char message[32];
        write_server_log("Started NDMP server");
        do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&read_socks)->__fds_bits)[0]) : "memory"); } while (0);
        listener = create_listener(10000);
        write_server_log("Created listener");
        listen(listener, 512);
        sprintf(message, "Listening on port %d", 10000 );
        write_server_log(message);
        printf("Listening on port %d....\n", 10000);
        ((void) (((&read_socks)->__fds_bits)[((listener) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((listener) % (8 * (int) sizeof (__fd_mask)))))));
        ctx->maxfds = listener;
        create_worker_threads(1);
        write_server_log("Created worker threads");
        signal(2, sig_handler);
        for (;;) {
                printf("Wait on select \n");
                write_server_log("Wait on select");
                set_fd_flags(&read_socks, listener, ctx);
                ready = select((ctx->maxfds) + 1, &read_socks, ((void *)0), ((void *)0),
                                ((void *)0) );
                printf("Woke up from select \n");
                write_server_log("Woke up from select");
                if (ready != -1) {
                        if (((((&read_socks)->__fds_bits)[((listener) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((listener) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
                                new_connections = accept_all_connections(
                                        listener, &read_socks, ctx);
                                printf("%d new connection(s) \n",
                                                new_connections);
                                write_server_log("Detected 1 new connection");
                                } else {
                                handle_client_requests(&read_socks, ctx);
                                write_server_log("Received client request");
                                }
                }
        }
}

variable_types: struct client_endpoint { 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]; } client; int fd; } *

variable_types: ready int

variable_types:
message
char [32]

user_defined_types: struct queue_node { void *elem; struct queue_node *next; }


decompiled_code:
pthread_mutex_t *get_lock()
{
pthread_mutex_t *v0; // r12

v0 = (pthread_mutex_t *)malloc(0x28uLL);
pthread_mutex_init(v0, 0LL);
return v0;
}

canonical_decompiled_code:
pthread_mutex_t *func0()
{
pthread_mutex_t *v0; // r12

v0 = (pthread_mutex_t *)malloc(0x28uLL);
pthread_mutex_init(v0, 0LL);
return v0;
}

original_code:
struct lock* get_lock()
{
        struct lock* retval;
        pthread_mutexattr_t attr;
        retval = (struct lock *) malloc(sizeof(struct lock));
        pthread_mutex_init(&retval->mutex, ((void *)0) );
        return retval;
}

canonical_original_code:
struct lock *get_lock()
{
        struct lock *retval;
        union pthread_mutexattr_t attr;
        retval = (struct lock *) malloc(sizeof(struct lock));
        pthread_mutex_init(&retval->mutex, ((void *)0) );
        return retval;
}

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

variable_types: union pthread_mutexattr_t { char __size[4]; int __align; }


decompiled_code:
// attributes: thunk
int enter_critical_section(pthread_mutex_t *mutex)
{
return pthread_mutex_lock(mutex);
}

canonical_decompiled_code:
int func1(pthread_mutex_t *mutex)
{
return pthread_mutex_lock(mutex);
}

original_code:
void enter_critical_section(struct lock* a_lock)
{
        pthread_mutex_lock(&a_lock->mutex);
}

canonical_original_code:
void enter_critical_section(struct lock *a_lock)
{
        pthread_mutex_lock(&a_lock->mutex);
}

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


decompiled_code:
// attributes: thunk
int exit_critical_section(pthread_mutex_t *mutex)
{
return pthread_mutex_unlock(mutex);
}

canonical_decompiled_code:
int func2(pthread_mutex_t *mutex)
{
return pthread_mutex_unlock(mutex);
}

original_code:
void exit_critical_section(struct lock* a_lock)
{
        pthread_mutex_unlock(&a_lock->mutex);
}

canonical_original_code:
void exit_critical_section(struct lock *a_lock)
{
        pthread_mutex_unlock(&a_lock->mutex);
}


decompiled_code:
__int64 __fastcall process_job(__int64 a1)
{
return hexdump(a1 + 28, *(unsigned int *)(a1 + 2076));
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
return hexdump(a1 + 28, *(unsigned int *)(a1 + 2076));
}

original_code:
void process_job(struct client_txn *job)
{
 hexdump(job->request.message, job->request.length);
}


decompiled_code:
void __fastcall __noreturn get_jobs(void *a1)
{
while ( dequeue(a1) )
{
enter_critical_section(num_lock);
++queue_size;
exit_critical_section(num_lock);
}
pthread_exit(0LL);
}

canonical_decompiled_code:
void __fastcall __noreturn func1(void *a1)
{
while ( dequeue(a1) )
{
enter_critical_section(num_lock);
++queue_size;
exit_critical_section(num_lock);
}
pthread_exit(0LL);
}

original_code:
void *get_jobs(void *queue)
{
 struct queue_hdr *job_queue = (struct queue_hdr *) queue;
 while (dequeue(job_queue) != ((void *)0)) {
  enter_critical_section(num_lock);
  queue_size++;
  exit_critical_section(num_lock);
 }
 pthread_exit(((void *)0));
}

variable_types:
queue
void *

variable_types: struct queue_hdr { struct queue_node { void *elem; struct queue_node *next; } *first; struct queue_node *last; struct lock { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; } *lock; int num_elems; } *

return_type: void *


decompiled_code:
__int64 __fastcall get_and_remove_jobs(void *a1)
{
int v1; // ebx
__int64 v2; // r12
int v4[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v1 = *((_DWORD *)a1 + 2);
v2 = *(_QWORD *)a1;
for ( v4[0] = 0; v4[0] < v1; ++v4[0] )
{
if ( get_elem(v2, v4, queue_cmp) )
{
remove_elem(v2, v4, queue_cmp);
if ( get_elem(v2, v4, queue_cmp) )
_assert_fail("get_elem(job_queue,&i,queue_cmp) == NULL", "mtestq.c", 0x73u, "get_and_remove_jobs");
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func2(void *a1)
{
int v1; // ebx
__int64 v2; // r12
int v4[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v1 = *((_DWORD *)a1 + 2);
v2 = *(_QWORD *)a1;
for ( v4[0] = 0; v4[0] < v1; ++v4[0] )
{
if ( get_elem(v2, v4, queue_cmp) )
{
remove_elem(v2, v4, queue_cmp);
if ( get_elem(v2, v4, queue_cmp) )
_assert_fail("get_elem(job_queue,&i,queue_cmp) == NULL", "mtestq.c", 0x73u, "get_and_remove_jobs");
}
}
return v2;
}

original_code:
void *get_and_remove_jobs(void *args)
{
 struct queue_hdr *job_queue = ((struct thread_args *) args)->queue;
 int i, n;
 n = ((struct thread_args *) args)->num_jobs;
 for (i = 0; i < n; i++) {
  if (get_elem(job_queue, &i, queue_cmp) != ((void *)0)) {
   remove_elem(job_queue, &i, queue_cmp);
   ((void) sizeof ((get_elem(job_queue,&i,queue_cmp) == ((void *)0)) ? 1 : 0), __extension__ ({ if (get_elem(job_queue,&i,queue_cmp) == ((void *)0)) ; else __assert_fail ("get_elem(job_queue,&i,queue_cmp) == NULL", "mtestq.c", 115, __extension__ __PRETTY_FUNCTION__); }));
  }
 }
 return job_queue;
}

canonical_original_code:
void *get_and_remove_jobs(void *args)
{
 struct queue_hdr *job_queue = ((struct thread_args *) args)->queue;
 int i; int n;
 n = ((struct thread_args *) args)->num_jobs;
 for (i = 0; i < n; i++) {
  if (get_elem(job_queue, &i, queue_cmp) != ((void *)0)) {
   remove_elem(job_queue, &i, queue_cmp);
   ((void) sizeof ((get_elem(job_queue,&i,queue_cmp) == ((void *)0)) ? 1 : 0), __extension__ ({ if (get_elem(job_queue,&i,queue_cmp) == ((void *)0)) ; else __assert_fail ("get_elem(job_queue,&i,queue_cmp) == NULL", "mtestq.c", 115, __extension__ __PRETTY_FUNCTION__); }));
  }
 }
 return job_queue;
}

variable_types:
args
void *

variable_types: n int

user_defined_types: struct thread_args { struct queue_hdr *queue; int num_jobs; }


decompiled_code:
__int64 __fastcall add_a_job(__int64 a1, int a2)
{
_DWORD *v2; // rax

v2 = malloc(4uLL);
*v2 = a2;
return enqueue(a1, v2);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, int a2)
{
_DWORD *v2; // rax

v2 = malloc(4uLL);
*v2 = a2;
return enqueue(a1, v2);
}

original_code:
void add_a_job(struct queue_hdr *job_queue, int i)
{
 int *jobid = (int *) malloc(sizeof(int));
 *jobid = i;
 enqueue(job_queue, jobid);
}

variable_types:
jobid
int *


decompiled_code:
// attributes: thunk
__int64 __fastcall get_job_from_queue(__int64 a1)
{
return dequeue(a1);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
return dequeue(a1);
}

original_code:
int *get_job_from_queue(struct queue_hdr *job_queue)
{
 return dequeue(job_queue);
}


decompiled_code:
__int64 __fastcall check_job_removal(__int64 a1, int a2, int a3)
{
pthread_t *v5; // rbx
pthread_t *v6; // r12
pthread_t *v7; // rbp
pthread_t v8; // rdi
__int64 result; // rax
__int64 v10; // [rsp+0h] [rbp-38h] BYREF
int v11; // [rsp+8h] [rbp-30h]

v10 = a1;
v11 = a2;
v5 = (pthread_t *)malloc(8LL * a3);
if ( (unsigned int)num_elems(a1) != a2 )
_assert_fail("num_elems(queue) == num_jobs", "mtestq.c", 0x52u, "check_job_removal");
if ( a3 > 0 )
{
v6 = &v5[a3];
v7 = v5;
do
pthread_create(v7++, 0LL, (void *(*)(void *))get_and_remove_jobs, &v10);
while ( v7 != v6 );
do
{
v8 = *v5++;
pthread_join(v8, 0LL);
}
while ( v5 != v6 );
}
result = num_elems(a1);
if ( (_DWORD)result )
_assert_fail("num_elems(queue) == 0", "mtestq.c", 0x5Bu, "check_job_removal");
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, int a2, int a3)
{
pthread_t *v5; // rbx
pthread_t *v6; // r12
pthread_t *v7; // rbp
pthread_t v8; // rdi
__int64 result; // rax
__int64 v10; // [rsp+0h] [rbp-38h] BYREF
int v11; // [rsp+8h] [rbp-30h]

v10 = a1;
v11 = a2;
v5 = (pthread_t *)malloc(8LL * a3);
if ( (unsigned int)num_elems(a1) != a2 )
_assert_fail("num_elems(queue) == num_jobs", "mtestq.c", 0x52u, "check_job_removal");
if ( a3 > 0 )
{
v6 = &v5[a3];
v7 = v5;
do
pthread_create(v7++, 0LL, (void *(*)(void *))get_and_remove_jobs, &v10);
while ( v7 != v6 );
do
{
v8 = *v5++;
pthread_join(v8, 0LL);
}
while ( v5 != v6 );
}
result = num_elems(a1);
if ( (_DWORD)result )
_assert_fail("num_elems(queue) == 0", "mtestq.c", 0x5Bu, "check_job_removal");
return result;
}

original_code:
void check_job_removal(struct queue_hdr *queue, int num_jobs, int num_threads)
{
 int i;
 struct thread_args args;
 args.queue = queue;
 args.num_jobs = num_jobs;
 pthread_t *threads = (pthread_t *) malloc(
   num_threads * sizeof(pthread_t));
 ((void) sizeof ((num_elems(queue) == num_jobs) ? 1 : 0), __extension__ ({ if (num_elems(queue) == num_jobs) ; else __assert_fail ("num_elems(queue) == num_jobs", "mtestq.c", 82, __extension__ __PRETTY_FUNCTION__); }));
 for (i = 0; i < num_threads; i++) {
  pthread_create(&threads[i], ((void *)0), get_and_remove_jobs,
    (void *) &args);
 }
 for (i = 0; i < num_threads; i++) {
  pthread_join(threads[i], ((void *)0));
 }
 ((void) sizeof ((num_elems(queue) == 0) ? 1 : 0), __extension__ ({ if (num_elems(queue) == 0) ; else __assert_fail ("num_elems(queue) == 0", "mtestq.c", 91, __extension__ __PRETTY_FUNCTION__); }));
}

canonical_original_code:
void check_job_removal(struct queue_hdr *queue, int num_jobs, int num_threads)
{
 int i;
 struct thread_args args;
 args.queue = queue;
 args.num_jobs = num_jobs;
 unsigned long *threads = (pthread_t *) malloc(
   num_threads * sizeof(pthread_t));
 ((void) sizeof ((num_elems(queue) == num_jobs) ? 1 : 0), __extension__ ({ if (num_elems(queue) == num_jobs) ; else __assert_fail ("num_elems(queue) == num_jobs", "mtestq.c", 82, __extension__ __PRETTY_FUNCTION__); }));
 for (i = 0; i < num_threads; i++) {
  pthread_create(&threads[i], ((void *)0), get_and_remove_jobs,
    (void *) &args);
 }
 for (i = 0; i < num_threads; i++) {
  pthread_join(threads[i], ((void *)0));
 }
 ((void) sizeof ((num_elems(queue) == 0) ? 1 : 0), __extension__ ({ if (num_elems(queue) == 0) ; else __assert_fail ("num_elems(queue) == 0", "mtestq.c", 91, __extension__ __PRETTY_FUNCTION__); }));
}

variable_types: struct thread_args { struct queue_hdr { struct queue_node { void *elem; struct queue_node *next; } *first; struct queue_node *last; struct lock { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; } *lock; int num_elems; } *queue; int num_jobs; }

variable_types:
threads
unsigned long *


decompiled_code:
char *__fastcall get_timestamp(char *dest)
{
struct tm *v1; // rax
time_t timer; // [rsp+8h] [rbp-A0h] BYREF
struct timeval tv; // [rsp+10h] [rbp-98h] BYREF
char s[64]; // [rsp+20h] [rbp-88h] BYREF
char src[72]; // [rsp+60h] [rbp-48h] BYREF

gettimeofday(&tv, 0LL);
timer = tv.tv_sec;
v1 = localtime(&timer);
strftime(s, 0x40uLL, "%Y-%m-%d %H:%M:%S", v1);
snprintf(src, 0x40uLL, "%s.%06d", s, tv.tv_usec);
return strcpy(dest, src);
}

canonical_decompiled_code:
char *__fastcall func1(char *dest)
{
struct tm *v1; // rax
time_t timer; // [rsp+8h] [rbp-A0h] BYREF
struct timeval tv; // [rsp+10h] [rbp-98h] BYREF
char s[64]; // [rsp+20h] [rbp-88h] BYREF
char src[72]; // [rsp+60h] [rbp-48h] BYREF

gettimeofday(&tv, 0LL);
timer = tv.tv_sec;
v1 = localtime(&timer);
strftime(s, 0x40uLL, "%Y-%m-%d %H:%M:%S", v1);
snprintf(src, 0x40uLL, "%s.%06d", s, tv.tv_usec);
return strcpy(dest, src);
}

original_code:
void get_timestamp(char timebuf[])
{
 struct timeval tv;
 time_t nowtime;
 struct tm *nowtm;
 char tmbuf[64], buf[64];
 gettimeofday(&tv, ((void *)0));
 nowtime = tv.tv_sec;
 nowtm = localtime(&nowtime);
 strftime(tmbuf, sizeof tmbuf, "%Y-%m-%d %H:%M:%S", nowtm);
 snprintf(buf, sizeof buf, "%s.%06d", tmbuf, tv.tv_usec);
 strcpy(timebuf, buf);
}

canonical_original_code:
void get_timestamp(char timebuf[])
{
 struct timeval tv;
 long nowtime;
 struct tm *nowtm;
 char tmbuf[64]; char buf[64];
 gettimeofday(&tv, ((void *)0));
 nowtime = tv.tv_sec;
 nowtm = localtime(&nowtime);
 strftime(tmbuf, sizeof tmbuf, "%Y-%m-%d %H:%M:%S", nowtm);
 snprintf(buf, sizeof buf, "%s.%06d", tmbuf, tv.tv_usec);
 strcpy(timebuf, buf);
}

variable_types:
timebuf
char []

variable_types: struct timeval { long tv_sec; long tv_usec; }

variable_types: nowtime long

variable_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; } *

variable_types:
tmbuf
char [64]

variable_types:
buf
char [64]


decompiled_code:
int __fastcall write_client_log(__int64 a1, __int64 a2, const char *a3)
{
char *v4; // rax
int v5; // ebp
unsigned __int16 v6; // bx
char *v7; // rax
unsigned int v8; // eax
char v10[64]; // [rsp+0h] [rbp-1A8h] BYREF
char dest[64]; // [rsp+40h] [rbp-168h] BYREF
char s[296]; // [rsp+80h] [rbp-128h] BYREF

v4 = inet_ntoa(*(struct in_addr *)((char *)&a1 + 4));
sprintf(v10, "/ndmp_server/log/%s.txt", v4);
v5 = open(v10, 1089);
get_timestamp(dest);
v6 = _bswap_16(SWORD1(a1));
v7 = inet_ntoa(*(struct in_addr *)((char *)&a1 + 4));
v8 = sprintf(s, "[%s] [Client (%s:%d)]: %s\n", dest, v7, v6, a3);
write(v5, s, v8);
return close(v5);
}

canonical_decompiled_code:
int __fastcall func2(__int64 a1, __int64 a2, const char *a3)
{
char *v4; // rax
int v5; // ebp
unsigned __int16 v6; // bx
char *v7; // rax
unsigned int v8; // eax
char v10[64]; // [rsp+0h] [rbp-1A8h] BYREF
char dest[64]; // [rsp+40h] [rbp-168h] BYREF
char s[296]; // [rsp+80h] [rbp-128h] BYREF

v4 = inet_ntoa(*(struct in_addr *)((char *)&a1 + 4));
sprintf(v10, "/ndmp_server/log/%s.txt", v4);
v5 = open(v10, 1089);
func1(dest);
v6 = func0(SWORD1(a1));
v7 = inet_ntoa(*(struct in_addr *)((char *)&a1 + 4));
v8 = sprintf(s, "[%s] [Client (%s:%d)]: %s\n", dest, v7, v6, a3);
write(v5, s, v8);
return close(v5);
}

original_code:
void write_client_log(struct sockaddr_in socket, char* message)
{
 char log_file_name[64];
 char write_buf[256];
 int log_file_len, write_buf_len, log_fd;
 char timestamp[64];
 char filename[64];
 log_file_len = sprintf(log_file_name, "/ndmp_server/log/%s.txt", inet_ntoa(socket.sin_addr));
 log_fd = open(log_file_name, 01 | 0100 | 02000);
 get_timestamp(timestamp);
 write_buf_len = sprintf(write_buf, "[%s] [Client (%s:%d)]: %s\n",
           timestamp, inet_ntoa(socket.sin_addr),
                      ntohs(socket.sin_port), message);
 write(log_fd, write_buf, write_buf_len);
 close(log_fd);
}

canonical_original_code:
void write_client_log(struct sockaddr_in socket, char *message)
{
 char log_file_name[64];
 char write_buf[256];
 int log_file_len; int write_buf_len; int log_fd;
 char timestamp[64];
 char filename[64];
 log_file_len = sprintf(log_file_name, "/ndmp_server/log/%s.txt", inet_ntoa(socket.sin_addr));
 log_fd = open(log_file_name, 01 | 0100 | 02000);
 get_timestamp(timestamp);
 write_buf_len = sprintf(write_buf, "[%s] [Client (%s:%d)]: %s\n",
           timestamp, inet_ntoa(socket.sin_addr),
                      ntohs(socket.sin_port), message);
 write(log_fd, write_buf, write_buf_len);
 close(log_fd);
}

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:
message
char *

variable_types:
timestamp
char [64]

variable_types:
filename
char [64]


decompiled_code:
int __fastcall write_server_log(const char *a1)
{
int v1; // ebp
unsigned int v2; // eax
__m128i v4; // [rsp+0h] [rbp-198h] BYREF
char v5[16]; // [rsp+10h] [rbp-188h] BYREF
char dest[64]; // [rsp+40h] [rbp-158h] BYREF
char s[280]; // [rsp+80h] [rbp-118h] BYREF

strcpy(v5, "/server.txt");
v4 = _mm_load_si128((const __m128i *)&LC5);
v1 = open(v4.m128i_i8, 1089);
get_timestamp(dest);
v2 = sprintf(s, "[%s] [Server]: %s\n", dest, a1);
write(v1, s, v2);
return close(v1);
}

canonical_decompiled_code:
int __fastcall func3(const char *a1)
{
int v1; // ebp
unsigned int v2; // eax
__m128i v4; // [rsp+0h] [rbp-198h] BYREF
char v5[16]; // [rsp+10h] [rbp-188h] BYREF
char dest[64]; // [rsp+40h] [rbp-158h] BYREF
char s[280]; // [rsp+80h] [rbp-118h] BYREF

strcpy(v5, "/server.txt");
v4 = _mm_load_si128((const __m128i *)&LC5);
v1 = open(v4.m128i_i8, 1089);
func1(dest);
v2 = sprintf(s, "[%s] [Server]: %s\n", dest, a1);
write(v1, s, v2);
return close(v1);
}

original_code:
void write_server_log(char* message)
{
 char log_file_name[64];
 char write_buf[256];
 int log_file_len, write_buf_len, log_fd;
 char timestamp[64];
 log_file_len = sprintf(log_file_name, "/ndmp_server/log/server.txt");
 log_fd = open(log_file_name, 01 | 0100 | 02000);
 get_timestamp(timestamp);
 write_buf_len = sprintf(write_buf, "[%s] [Server]: %s\n",
           timestamp, message);
 write(log_fd, write_buf, write_buf_len);
 close(log_fd);
}

canonical_original_code:
void write_server_log(char *message)
{
 char log_file_name[64];
 char write_buf[256];
 int log_file_len; int write_buf_len; int log_fd;
 char timestamp[64];
 log_file_len = sprintf(log_file_name, "/ndmp_server/log/server.txt");
 log_fd = open(log_file_name, 01 | 0100 | 02000);
 get_timestamp(timestamp);
 write_buf_len = sprintf(write_buf, "[%s] [Server]: %s\n",
           timestamp, message);
 write(log_fd, write_buf, write_buf_len);
 close(log_fd);
}


decompiled_code:
__int64 __fastcall ndmp_connect_open(unsigned int *a1, __int64 a2)
{
__int64 v2; // rbp
__int64 v3; // rsi
u_int v4; // edx
unsigned __int16 v6; // [rsp+Ah] [rbp-7Eh] BYREF
unsigned int v7; // [rsp+Ch] [rbp-7Ch] BYREF
char v8[48]; // [rsp+10h] [rbp-78h] BYREF
XDR xdrs; // [rsp+40h] [rbp-48h] BYREF

xdr_ndmp_connect_open_request(a2, &v6);
v2 = get_session_info(*a1);
enter_critical_section(*(_QWORD *)(v2 + 16));
if ( *(_DWORD *)(v2 + 4) == 1 )
{
if ( (unsigned __int16)(v6 - 1) <= 2u )
{
v7 = 0;
v3 = 0LL;
}
else if ( v6 > 3u )
{
v7 = 9;
v3 = 9LL;
}
else
{
v3 = v7;
}
}
else
{
v7 = 19;
v3 = 19LL;
}
set_header(v8, v3);
a1[1033] = xdr_sizeof((xdrproc_t)&xdr_ndmp_header, v8);
v4 = xdr_sizeof((xdrproc_t)&xdr_ndmp_connect_open_reply, &v7) + a1[1033];
a1[1033] = v4;
xdrmem_create(&xdrs, (const caddr_t)a1 + 2084, v4, XDR_ENCODE);
xdr_ndmp_header(&xdrs, v8);
if ( v7 )
{
a1[1033] -= xdr_sizeof((xdrproc_t)&xdr_ndmp_connect_open_reply, &v7);
}
else
{
xdr_ndmp_connect_open_reply(&xdrs, &v7);
*(_DWORD *)(v2 + 4) = 2;
}
write_client_log(*(_QWORD *)(a1 + 1), *(_QWORD *)(a1 + 3), "Sent CONNECT_OPEN to client");
return exit_critical_section(*(_QWORD *)(v2 + 16));
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int *a1, __int64 a2)
{
__int64 v2; // rbp
__int64 v3; // rsi
u_int v4; // edx
unsigned __int16 v6; // [rsp+Ah] [rbp-7Eh] BYREF
unsigned int v7; // [rsp+Ch] [rbp-7Ch] BYREF
char v8[48]; // [rsp+10h] [rbp-78h] BYREF
XDR xdrs; // [rsp+40h] [rbp-48h] BYREF

xdr_ndmp_connect_open_request(a2, &v6);
v2 = get_session_info(*a1);
enter_critical_section(*(_QWORD *)(v2 + 16));
if ( *(_DWORD *)(v2 + 4) == 1 )
{
if ( (unsigned __int16)(v6 - 1) <= 2u )
{
v7 = 0;
v3 = 0LL;
}
else if ( v6 > 3u )
{
v7 = 9;
v3 = 9LL;
}
else
{
v3 = v7;
}
}
else
{
v7 = 19;
v3 = 19LL;
}
set_header(v8, v3);
a1[1033] = xdr_sizeof((xdrproc_t)&xdr_ndmp_header, v8);
v4 = xdr_sizeof((xdrproc_t)&xdr_ndmp_connect_open_reply, &v7) + a1[1033];
a1[1033] = v4;
xdrmem_create(&xdrs, (const caddr_t)a1 + 2084, v4, XDR_ENCODE);
xdr_ndmp_header(&xdrs, v8);
if ( v7 )
{
a1[1033] -= xdr_sizeof((xdrproc_t)&xdr_ndmp_connect_open_reply, &v7);
}
else
{
xdr_ndmp_connect_open_reply(&xdrs, &v7);
*(_DWORD *)(v2 + 4) = 2;
}
write_client_log(*(_QWORD *)(a1 + 1), *(_QWORD *)(a1 + 3), "Sent CONNECT_OPEN to client");
return exit_critical_section(*(_QWORD *)(v2 + 16));
}

original_code:
void ndmp_connect_open(struct client_txn *txn, struct ndmp_header header, XDR* request_stream)
{
        struct ndmp_connect_open_request request;
        struct ndmp_header reply_header;
        struct ndmp_connect_open_reply reply;
        struct ndmp_session_info *session_info;
        XDR reply_stream;
        xdr_ndmp_connect_open_request(request_stream, &request);
        session_info = get_session_info(txn->client_session.session_id);
        enter_critical_section(session_info->s_lock);
        if (session_info->connection_state != LISTEN)
                reply.error = NDMP_ILLEGAL_STATE_ERR;
        else if (request.protocol_version >= 1 && request.protocol_version <= 3)
                reply.error = NDMP_NO_ERR;
        else if (request.protocol_version > 3)
                reply.error = NDMP_ILLEGAL_ARGS_ERR;
        set_header(header, &reply_header, reply.error);
        txn->reply.length = xdr_sizeof((xdrproc_t)
                                       xdr_ndmp_header, &reply_header);
        txn->reply.length += xdr_sizeof((xdrproc_t)
                                        xdr_ndmp_connect_open_reply, &reply);
        xdrmem_create(&reply_stream,
                      txn->reply.message, txn->reply.length,XDR_ENCODE);
        xdr_ndmp_header(&reply_stream, &reply_header);
        if (reply.error == NDMP_NO_ERR) {
                xdr_ndmp_connect_open_reply(&reply_stream, &reply);
                session_info->connection_state = CONNECTED;
        }
        else
                txn->reply.length -= xdr_sizeof((xdrproc_t)
                                        xdr_ndmp_connect_open_reply, &reply);
        write_client_log(txn->client_session.client_info.client, "Sent CONNECT_OPEN to client");
        exit_critical_section(session_info->s_lock);
}

canonical_original_code:
void ndmp_connect_open(struct client_txn *txn, struct ndmp_header header, struct XDR *request_stream)
{
        struct ndmp_connect_open_request request;
        struct ndmp_header reply_header;
        struct ndmp_connect_open_reply reply;
        struct ndmp_session_info *session_info;
        struct XDR reply_stream;
        xdr_ndmp_connect_open_request(request_stream, &request);
        session_info = get_session_info(txn->client_session.session_id);
        enter_critical_section(session_info->s_lock);
        if (session_info->connection_state != LISTEN)
                reply.error = NDMP_ILLEGAL_STATE_ERR;
        else if (request.protocol_version >= 1 && request.protocol_version <= 3)
                reply.error = NDMP_NO_ERR;
        else if (request.protocol_version > 3)
                reply.error = NDMP_ILLEGAL_ARGS_ERR;
        set_header(header, &reply_header, reply.error);
        txn->reply.length = xdr_sizeof((int (*)(struct XDR *, void *, ...))
                                       xdr_ndmp_header, &reply_header);
        txn->reply.length += xdr_sizeof((int (*)(struct XDR *, void *, ...))
                                        xdr_ndmp_connect_open_reply, &reply);
        xdrmem_create(&reply_stream,
                      txn->reply.message, txn->reply.length,XDR_ENCODE);
        xdr_ndmp_header(&reply_stream, &reply_header);
        if (reply.error == NDMP_NO_ERR) {
                xdr_ndmp_connect_open_reply(&reply_stream, &reply);
                session_info->connection_state = CONNECTED;
        }
        else
                txn->reply.length -= xdr_sizeof((int (*)(struct XDR *, void *, ...))
                                        xdr_ndmp_connect_open_reply, &reply);
        write_client_log(txn->client_session.client_info.client, "Sent CONNECT_OPEN to client");
        exit_critical_section(session_info->s_lock);
}

variable_types: struct client_txn { struct session { int session_id; struct client_endpoint { 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]; } client; int fd; } client_info; } client_session; struct comm_message { int is_tcp_connect; char message[2048]; int length; } request; struct comm_message reply; } *

variable_types: struct ndmp_header { unsigned long sequence; unsigned long time_stamp; enum ndmp_header_message_type { NDMP_MESSAGE_REQUEST=0, NDMP_MESSAGE_REPLY=1 } message_type; enum ndmp_message { NDMP_CONNECT_OPEN=2304, NDMP_CONNECT_CLIENT_AUTH=2305, NDMP_CONNECT_CLOSE=2306, NDMP_CONNECT_SERVER_AUTH=2307, NDMP_CONFIG_GET_HOST_INFO=256, NDMP_CONFIG_GET_CONNECTION_TYPE=258, NDMP_CONFIG_GET_AUTH_ATTR=259, NDMP_CONFIG_GET_BUTYPE_INFO=260, NDMP_CONFIG_GET_FS_INFO=261, NDMP_CONFIG_GET_TAPE_INFO=262, NDMP_CONFIG_GET_SCSI_INFO=263, NDMP_CONFIG_GET_SERVER_INFO=264, NDMP_SCSI_OPEN=512, NDMP_SCSI_CLOSE=513, NDMP_SCSI_GET_STATE=514, NDMP_SCSI_SET_TARGET=515, NDMP_SCSI_RESET_DEVICE=516, NDMP_SCSI_RESET_BUS=517, NDMP_SCSI_EXECUTE_CDB=518, NDMP_TAPE_OPEN=768, NDMP_TAPE_CLOSE=769, NDMP_TAPE_GET_STATE=770, NDMP_TAPE_MTIO=771, NDMP_TAPE_WRITE=772, NDMP_TAPE_READ=773, NDMP_TAPE_EXECUTE_CDB=775, NDMP_DATA_GET_STATE=1024, NDMP_DATA_START_BACKUP=1025, NDMP_DATA_START_RECOVER=1026, NDMP_DATA_ABORT=1027, NDMP_DATA_GET_ENV=1028, NDMP_DATA_STOP=1031, NDMP_DATA_LISTEN=1033, NDMP_DATA_CONNECT=1034, NDMP_NOTIFY_DATA_HALTED=1281, NDMP_NOTIFY_CONNECTED=1282, NDMP_NOTIFY_MOVER_HALTED=1283, NDMP_NOTIFY_MOVER_PAUSED=1284, NDMP_NOTIFY_DATA_READ=1285, NDMP_LOG_FILE=1538, NDMP_LOG_MESSAGE=1539, NDMP_FH_ADD_FILE=1795, NDMP_FH_ADD_DIR=1796, NDMP_FH_ADD_NODE=1797, NDMP_MOVER_GET_STATE=2560, NDMP_MOVER_LISTEN=2561, NDMP_MOVER_CONTINUE=2562, NDMP_MOVER_ABORT=2563, NDMP_MOVER_STOP=2564, NDMP_MOVER_SET_WINDOW=2565, NDMP_MOVER_READ=2566, NDMP_MOVER_CLOSE=2567, NDMP_MOVER_SET_RECORD_SIZE=2568, NDMP_MOVER_CONNECT=2569, NDMP_VENDORS_BASE=61440, NDMP_RESERVED_BASE=65280 } message; unsigned long reply_sequence; enum ndmp_error { NDMP_NO_ERR=0, NDMP_NOT_SUPPORTED_ERR=1, NDMP_DEVICE_BUSY_ERR=2, NDMP_DEVICE_OPENED_ERR=3, NDMP_NOT_AUTHORIZED_ERR=4, NDMP_PERMISSION_ERR=5, NDMP_DEV_NOT_OPEN_ERR=6, NDMP_IO_ERR=7, NDMP_TIMEOUT_ERR=8, NDMP_ILLEGAL_ARGS_ERR=9, NDMP_NO_TAPE_LOADED_ERR=10, NDMP_WRITE_PROTECT_ERR=11, NDMP_EOF_ERR=12, NDMP_EOM_ERR=13, NDMP_FILE_NOT_FOUND_ERR=14, NDMP_BAD_FILE_ERR=15, NDMP_NO_DEVICE_ERR=16, NDMP_NO_BUS_ERR=17, NDMP_XDR_DECODE_ERR=18, NDMP_ILLEGAL_STATE_ERR=19, NDMP_UNDEFINED_ERR=20, NDMP_XDR_ENCODE_ERR=21, NDMP_NO_MEM_ERR=22, NDMP_CONNECT_ERR=23 } error; }

variable_types: struct XDR { enum xdr_op { XDR_ENCODE=0, XDR_DECODE=1, XDR_FREE=2 } x_op; struct xdr_ops { int (*x_getlong)(struct XDR *__xdrs, long *__lp); int (*x_putlong)(struct XDR *__xdrs, long *__lp); int (*x_getbytes)(struct XDR *__xdrs, char *__addr, unsigned int __len); int (*x_putbytes)(struct XDR *__xdrs, char *__addr, unsigned int __len); unsigned int (*x_getpostn)(struct XDR *__xdrs); int (*x_setpostn)(struct XDR *__xdrs, unsigned int __pos); int *(*x_inline)(struct XDR *__xdrs, unsigned int __len); void (*x_destroy)(struct XDR *__xdrs); int (*x_getint32)(struct XDR *__xdrs, int *__ip); int (*x_putint32)(struct XDR *__xdrs, int *__ip); } *x_ops; char *x_public; char *x_private; char *x_base; unsigned int x_handy; } *

variable_types: struct ndmp_connect_open_request { unsigned short protocol_version; }

variable_types: struct ndmp_connect_open_reply { enum ndmp_error { NDMP_NO_ERR=0, NDMP_NOT_SUPPORTED_ERR=1, NDMP_DEVICE_BUSY_ERR=2, NDMP_DEVICE_OPENED_ERR=3, NDMP_NOT_AUTHORIZED_ERR=4, NDMP_PERMISSION_ERR=5, NDMP_DEV_NOT_OPEN_ERR=6, NDMP_IO_ERR=7, NDMP_TIMEOUT_ERR=8, NDMP_ILLEGAL_ARGS_ERR=9, NDMP_NO_TAPE_LOADED_ERR=10, NDMP_WRITE_PROTECT_ERR=11, NDMP_EOF_ERR=12, NDMP_EOM_ERR=13, NDMP_FILE_NOT_FOUND_ERR=14, NDMP_BAD_FILE_ERR=15, NDMP_NO_DEVICE_ERR=16, NDMP_NO_BUS_ERR=17, NDMP_XDR_DECODE_ERR=18, NDMP_ILLEGAL_STATE_ERR=19, NDMP_UNDEFINED_ERR=20, NDMP_XDR_ENCODE_ERR=21, NDMP_NO_MEM_ERR=22, NDMP_CONNECT_ERR=23 } error; }

variable_types: struct ndmp_session_info { int session_id; enum ndmp_state { IDLE=0, LISTEN=1, CONNECTED=2, ACTIVE=3, HALTED=4 } connection_state; enum ndmp_state { IDLE=0, LISTEN=1, CONNECTED=2, ACTIVE=3, HALTED=4 } data_state; enum ndmp_state { IDLE=0, LISTEN=1, CONNECTED=2, ACTIVE=3, HALTED=4 } mover_state; struct lock { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; } *s_lock; struct queue_hdr { struct queue_node { void *elem; struct queue_node *next; } *first; struct queue_node *last; struct lock *lock; int num_elems; } *jobs; int is_terminated; } *

user_defined_types: struct ndmp_header { unsigned long sequence; unsigned long time_stamp; enum ndmp_header_message_type message_type; enum ndmp_message message; unsigned long reply_sequence; enum ndmp_error error; }

user_defined_types: struct ndmp_connect_open_reply { enum ndmp_error error; }

user_defined_types: struct XDR { enum xdr_op x_op; struct xdr_ops *x_ops; char *x_public; char *x_private; char *x_base; unsigned int x_handy; }

user_defined_types: struct ndmp_session_info { int session_id; enum ndmp_state connection_state; enum ndmp_state data_state; enum ndmp_state mover_state; struct lock *s_lock; struct queue_hdr *jobs; int is_terminated; }