decompiled_code:
__int64 __fastcall validate_paxos_msg(__int64 a1, __int64 a2)
{
__int64 v3; // rsi
__int64 v4; // rsi
unsigned __int16 v5; // ax
__int64 result; // rax
unsigned __int16 v7; // ax

v3 = *(_QWORD *)a1 + 16LL;
if ( v3 != a2 )
{
printf("Invalid message, declared size:%lu received size:%u\n", v3, a2);
return 0xFFFFFFFFLL;
}
v4 = *(unsigned int *)(a1 + 8);
if ( (unsigned int)v4 > 0x20 )
{
if ( (unsigned int)(v4 - 64) <= 1 )
return 0LL;
goto LABEL_8;
}
if ( !(_DWORD)v4 )
{
LABEL_8:
printf("Unknow paxos message type:%d\n", v4);
return 0xFFFFFFFFLL;
}
switch ( (int)v4 )
{
case 1:
case 4:
v7 = *(_WORD *)(a1 + 14);
if ( v7 <= 9u )
return 0LL;
printf("Invalida proposer id:%d\n", (unsigned int)(__int16)v7);
result = 0xFFFFFFFFLL;
break;
case 2:
case 8:
v5 = *(_WORD *)(a1 + 12);
if ( v5 <= 2u )
return 0LL;
printf("Invalida acceptor id:%d\n", (unsigned int)(__int16)v5);
result = 0xFFFFFFFFLL;
break;
case 16:
case 32:
return 0LL;
default:
goto LABEL_8;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2)
{
__int64 v3; // rsi
__int64 v4; // rsi
unsigned __int16 v5; // ax
__int64 result; // rax
unsigned __int16 v7; // ax

v3 = *(_QWORD *)a1 + 16LL;
if ( v3 != a2 )
{
printf("Invalid message, declared size:%lu received size:%u\n", v3, a2);
return 0xFFFFFFFFLL;
}
v4 = *(unsigned int *)(a1 + 8);
if ( (unsigned int)v4 > 0x20 )
{
if ( (unsigned int)(v4 - 64) <= 1 )
return 0LL;
goto LABEL_8;
}
if ( !(_DWORD)v4 )
{
LABEL_8:
printf("Unknow paxos message type:%d\n", v4);
return 0xFFFFFFFFLL;
}
switch ( (int)v4 )
{
case 1:
case 4:
v7 = *(_WORD *)(a1 + 14);
if ( v7 <= 9u )
return 0LL;
printf("Invalida proposer id:%d\n", (unsigned int)(__int16)v7);
result = 0xFFFFFFFFLL;
break;
case 2:
case 8:
v5 = *(_WORD *)(a1 + 12);
if ( v5 <= 2u )
return 0LL;
printf("Invalida acceptor id:%d\n", (unsigned int)(__int16)v5);
result = 0xFFFFFFFFLL;
break;
case 16:
case 32:
return 0LL;
default:
goto LABEL_8;
}
return result;
}

original_code:
static int validate_paxos_msg(paxos_msg * m, size_t msg_size) {
    size_t expected_size = sizeof(paxos_msg);
    if (m->data_size + sizeof(paxos_msg) != msg_size) {
        printf("Invalid message, declared size:%lu received size:%u\n", (m->data_size + sizeof(paxos_msg)), (unsigned int)msg_size);
        return -1;
    }
    switch(m->type) {
        case prepare_reqs: {
            prepare_req_batch * prb = (prepare_req_batch *)m->data;
            if(prb->proposer_id < 0 || prb->proposer_id >= 10) {
                printf("Invalida proposer id:%d\n", prb->proposer_id);
                return -1;
            }
            expected_size += (sizeof(prepare_req_batch) + (prb->count*sizeof(prepare_req)));
        }
        break;
        case prepare_acks: {
            prepare_ack_batch * pab = (prepare_ack_batch *)m->data;
            if(pab->acceptor_id < 0 || pab->acceptor_id >= 3) {
                printf("Invalida acceptor id:%d\n", pab->acceptor_id);
                return -1;
            }
            expected_size += (prepare_ack_batch_size_calc(pab));
        }
        break;
        case accept_reqs: {
            accept_req_batch * arb = (accept_req_batch *)m->data;
            if(arb->proposer_id < 0 || arb->proposer_id >= 10) {
                printf("Invalida proposer id:%d\n", arb->proposer_id);
                return -1;
            }
            expected_size += (accept_req_batch_size_calc(arb));
        }
        break;
        case accept_acks: {
            accept_ack_batch * aab = (accept_ack_batch *)m->data;
            if(aab->acceptor_id < 0 || aab->acceptor_id >= 3) {
                printf("Invalida acceptor id:%d\n", aab->acceptor_id);
                return -1;
            }
            expected_size += (accept_ack_batch_size_calc(aab));
        }
        break;
        case repeat_reqs: {
            repeat_req_batch * rrb = (repeat_req_batch *)m->data;
            expected_size += (sizeof(repeat_req_batch) + (sizeof(iid_t) * rrb->count));
        }
        break;
        case submit: {
            expected_size += m->data_size;
        }
        break;
        case alive_ping: {
            expected_size += sizeof(alive_ping_msg);
        }
        break;
        case leader_announce: {
            expected_size += sizeof(leader_announce_msg);
        }
        break;
        default: {
            printf("Unknow paxos message type:%d\n", m->type);
            return -1;
        }
        if(msg_size != expected_size) {
            printf("Invalid size for msg_type:%d declared:%u received:%u\n", m->type, (unsigned int)expected_size, (unsigned int)msg_size);
        }
    }
    return 0;
}

canonical_original_code:
static int validate_paxos_msg(struct paxos_msg_t *m, unsigned long msg_size) {
    unsigned long expected_size = sizeof(paxos_msg);
    if (m->data_size + sizeof(paxos_msg) != msg_size) {
        printf("Invalid message, declared size:%lu received size:%u\n", (m->data_size + sizeof(paxos_msg)), (unsigned int)msg_size);
        return -1;
    }
    switch(m->type) {
        case prepare_reqs: {
            struct prepare_req_batch_t *prb = (prepare_req_batch *)m->data;
            if(prb->proposer_id < 0 || prb->proposer_id >= 10) {
                printf("Invalida proposer id:%d\n", prb->proposer_id);
                return -1;
            }
            expected_size += (sizeof(prepare_req_batch) + (prb->count*sizeof(prepare_req)));
        }
        break;
        case prepare_acks: {
            struct prepare_ack_batch_t *pab = (prepare_ack_batch *)m->data;
            if(pab->acceptor_id < 0 || pab->acceptor_id >= 3) {
                printf("Invalida acceptor id:%d\n", pab->acceptor_id);
                return -1;
            }
            expected_size += (prepare_ack_batch_size_calc(pab));
        }
        break;
        case accept_reqs: {
            struct accept_req_batch_t *arb = (accept_req_batch *)m->data;
            if(arb->proposer_id < 0 || arb->proposer_id >= 10) {
                printf("Invalida proposer id:%d\n", arb->proposer_id);
                return -1;
            }
            expected_size += (accept_req_batch_size_calc(arb));
        }
        break;
        case accept_acks: {
            struct accept_ack_batch_t *aab = (accept_ack_batch *)m->data;
            if(aab->acceptor_id < 0 || aab->acceptor_id >= 3) {
                printf("Invalida acceptor id:%d\n", aab->acceptor_id);
                return -1;
            }
            expected_size += (accept_ack_batch_size_calc(aab));
        }
        break;
        case repeat_reqs: {
            struct repeat_req_batch_t *rrb = (repeat_req_batch *)m->data;
            expected_size += (sizeof(repeat_req_batch) + (sizeof(iid_t) * rrb->count));
        }
        break;
        case submit: {
            expected_size += m->data_size;
        }
        break;
        case alive_ping: {
            expected_size += sizeof(alive_ping_msg);
        }
        break;
        case leader_announce: {
            expected_size += sizeof(leader_announce_msg);
        }
        break;
        default: {
            printf("Unknow paxos message type:%d\n", m->type);
            return -1;
        }
        if(msg_size != expected_size) {
            printf("Invalid size for msg_type:%d declared:%u received:%u\n", m->type, (unsigned int)expected_size, (unsigned int)msg_size);
        }
    }
    return 0;
}

variable_types: struct prepare_req_batch_t { short count; short proposer_id; struct prepare_req_t { unsigned int iid; unsigned int ballot; } prepares[]; } *

variable_types: struct repeat_req_batch_t { short count; unsigned int requests[]; } *

user_defined_types: struct prepare_req_batch_t { short count; short proposer_id; struct prepare_req_t prepares[]; }

user_defined_types: struct prepare_ack_batch_t { short acceptor_id; short count; char data[]; }

user_defined_types: struct accept_req_batch_t { short count; short proposer_id; char data[]; }

user_defined_types: enum pax_msg_code_e { prepare_reqs=1, prepare_acks=2, accept_reqs=4, accept_acks=8, repeat_reqs=16, submit=32, leader_announce=64, alive_ping=65 }


repo: dgomezferro/libpaxos

original_code:
void print_paxos_msg(paxos_msg * msg) {
    printf("[msg=%d size:%lu+%lu ",
        msg->type, sizeof(paxos_msg), msg->data_size);
    int i;
    size_t offset = 0;
    switch(msg->type) {
        case prepare_reqs: {
            prepare_req_batch * prb = (prepare_req_batch *)msg->data;
            printf("(prepare request batch)\n");
            printf(" sender proposer:%d, count:%d\n",
                prb->proposer_id, prb->count);
            prepare_req * pr;
            for(i = 0; i < prb->count; i++) {
                pr = (prepare_req *) &prb->prepares[i];
                printf("\n (%d) iid:%lu bal:%u ",
                    (int)i, pr->iid, pr->ballot);
            }
        }
        break;
        case prepare_acks: {
            prepare_ack_batch * pab = (prepare_ack_batch *)msg->data;
            printf("(prepare acknowledgement batch)\n");
            printf(" sender acceptor:%d, count:%d\n",
                pab->acceptor_id, pab->count);
            prepare_ack * pa;
            for(i = 0; i < pab->count; i++) {
                pa = (prepare_ack *) &pab->data[offset];
                printf("\n (%p)(%d) iid:%lu bal:%u vbal:%u val_size:%lu",
                    (void*)pa, (int)i, pa->iid, pa->ballot,
                    pa->value_ballot, pa->value_size);
                offset += (pa->value_size + sizeof(prepare_ack));
            }
        }
        break;
        case accept_reqs: {
            accept_req_batch * arb = (accept_req_batch *)msg->data;
            printf("(accept request batch)\n");
            printf(" sender proposer:%d, count:%d\n",
                arb->proposer_id, arb->count);
            accept_req * ar;
            for(i = 0; i < arb->count; i++) {
                ar = (accept_req *) &arb->data[offset];
                printf("\n (%d) iid:%lu bal:%u val_size:%lu",
                    (int)i, ar->iid, ar->ballot, ar->value_size);
                offset += (ar->value_size + sizeof(accept_req));
            }
        }
        break;
        case accept_acks: {
            accept_ack_batch * aab = (accept_ack_batch *)msg->data;
            printf("(accept acknowledgement batch)\n");
            printf(" sender acceptor:%d, count:%d\n",
                aab->acceptor_id, aab->count);
            accept_ack * aa;
            for(i = 0; i < aab->count; i++) {
                aa = (accept_ack *) &aab->data[offset];
                printf("\n (%d) iid:%lu bal:%u vbal:%u val_size:%lu",
                    (int)i, aa->iid, aa->ballot,
                    aa->value_ballot, aa->value_size);
                offset += (aa->value_size + sizeof(accept_ack));
            }
        }
        break;
        case repeat_reqs: {
            repeat_req_batch * rrb = (repeat_req_batch *)msg->data;
            printf("(repeat request batch)\n");
            printf(" count:%d\n", rrb->count);
            for(i = 0; i < rrb->count; i++) {
                printf("\n (%d) iid:%lu ",
                    (int)i, rrb->requests[i]);
            }
        }
        break;
        default: {
            printf("Unknow paxos message type:%d\n", msg->type);
        }
    }
    printf("]\n");
}

canonical_original_code:
void print_paxos_msg(struct paxos_msg_t *msg) {
    printf("[msg=%d size:%lu+%lu ",
        msg->type, sizeof(paxos_msg), msg->data_size);
    int i;
    unsigned long offset = 0;
    switch(msg->type) {
        case prepare_reqs: {
            struct prepare_req_batch_t *prb = (prepare_req_batch *)msg->data;
            printf("(prepare request batch)\n");
            printf(" sender proposer:%d, count:%d\n",
                prb->proposer_id, prb->count);
            struct prepare_req_t *pr;
            for(i = 0; i < prb->count; i++) {
                pr = (struct prepare_req_t *) &prb->prepares[i];
                printf("\n (%d) iid:%lu bal:%u ",
                    (int)i, pr->iid, pr->ballot);
            }
        }
        break;
        case prepare_acks: {
            struct prepare_ack_batch_t *pab = (prepare_ack_batch *)msg->data;
            printf("(prepare acknowledgement batch)\n");
            printf(" sender acceptor:%d, count:%d\n",
                pab->acceptor_id, pab->count);
            struct prepare_ack_t *pa;
            for(i = 0; i < pab->count; i++) {
                pa = (struct prepare_ack_t *) &pab->data[offset];
                printf("\n (%p)(%d) iid:%lu bal:%u vbal:%u val_size:%lu",
                    (void *)pa, (int)i, pa->iid, pa->ballot,
                    pa->value_ballot, pa->value_size);
                offset += (pa->value_size + sizeof(prepare_ack));
            }
        }
        break;
        case accept_reqs: {
            struct accept_req_batch_t *arb = (accept_req_batch *)msg->data;
            printf("(accept request batch)\n");
            printf(" sender proposer:%d, count:%d\n",
                arb->proposer_id, arb->count);
            struct accept_req_t *ar;
            for(i = 0; i < arb->count; i++) {
                ar = (struct accept_req_t *) &arb->data[offset];
                printf("\n (%d) iid:%lu bal:%u val_size:%lu",
                    (int)i, ar->iid, ar->ballot, ar->value_size);
                offset += (ar->value_size + sizeof(accept_req));
            }
        }
        break;
        case accept_acks: {
            struct accept_ack_batch_t *aab = (accept_ack_batch *)msg->data;
            printf("(accept acknowledgement batch)\n");
            printf(" sender acceptor:%d, count:%d\n",
                aab->acceptor_id, aab->count);
            struct accept_ack_t *aa;
            for(i = 0; i < aab->count; i++) {
                aa = (struct accept_ack_t *) &aab->data[offset];
                printf("\n (%d) iid:%lu bal:%u vbal:%u val_size:%lu",
                    (int)i, aa->iid, aa->ballot,
                    aa->value_ballot, aa->value_size);
                offset += (aa->value_size + sizeof(accept_ack));
            }
        }
        break;
        case repeat_reqs: {
            struct repeat_req_batch_t *rrb = (repeat_req_batch *)msg->data;
            printf("(repeat request batch)\n");
            printf(" count:%d\n", rrb->count);
            for(i = 0; i < rrb->count; i++) {
                printf("\n (%d) iid:%lu ",
                    (int)i, rrb->requests[i]);
            }
        }
        break;
        default: {
            printf("Unknow paxos message type:%d\n", msg->type);
        }
    }
    printf("]\n");
}

variable_types: struct paxos_msg_t { unsigned long data_size; enum pax_msg_code_e { prepare_reqs=1, prepare_acks=2, accept_reqs=4, accept_acks=8, repeat_reqs=16, submit=32, leader_announce=64, alive_ping=65 } type; char data[]; } *

variable_types: i int

variable_types:
offset
unsigned long

variable_types: struct prepare_req_t { unsigned int iid; unsigned int ballot; } *

variable_types: struct prepare_ack_batch_t { short acceptor_id; short count; char data[]; } *

variable_types: struct prepare_ack_t { unsigned int iid; unsigned int ballot; unsigned int value_ballot; unsigned long value_size; char value[]; } *

variable_types: struct accept_req_batch_t { short count; short proposer_id; char data[]; } *

variable_types: struct accept_req_t { unsigned int iid; unsigned int ballot; unsigned long value_size; char value[]; } *

variable_types: struct accept_ack_batch_t { short acceptor_id; short count; char data[]; } *

variable_types: struct accept_ack_t { unsigned int iid; unsigned int ballot; unsigned int value_ballot; short is_final; unsigned long value_size; char value[]; } *

return_type: void

user_defined_types: struct paxos_msg_t { unsigned long data_size; enum pax_msg_code_e type; char data[]; }

user_defined_types: struct repeat_req_batch_t { short count; unsigned int requests[]; }


decompiled_code:
__int64 __fastcall udp_unicast_receiver_blocking_new(char *cp, __int16 a2)
{
__int64 v2; // r12
int v3; // eax
in_addr_t v4; // eax
__int16 v5; // ax
int v6; // edi
socklen_t v7; // edx
__int64 result; // rax
int optval[11]; // [rsp+Ch] [rbp-2Ch] BYREF

v2 = paxos_normal_malloc(7520LL);
v3 = socket(2, 2, 0);
*(_DWORD *)v2 = v3;
if ( v3 < 0 )
{
perror("receiver socket");
result = 0LL;
}
else
{
optval[0] = 1;
if ( setsockopt(v3, 1, 2, optval, 4u) )
{
perror("setsockopt, setting SO_REUSEADDR");
result = 0LL;
}
else
{
v4 = inet_addr(cp);
*(_DWORD *)(v2 + 8) = v4;
if ( v4 == -1 )
{
v2 = 0LL;
puts("Error setting receiver->addr");
}
else
{
*(_WORD *)(v2 + 4) = 2;
v5 = _bswap_16(a2);
v6 = *(_DWORD *)v2;
*(_WORD *)(v2 + 6) = v5;
if ( bind(v6, (const struct sockaddr *)(v2 + 4), v7) )
{
v2 = 0LL;
perror("bind");
}
}
result = v2;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(char *cp, __int16 a2)
{
__int64 v2; // r12
int v3; // eax
in_addr_t v4; // eax
__int16 v5; // ax
int v6; // edi
socklen_t v7; // edx
__int64 result; // rax
int optval[11]; // [rsp+Ch] [rbp-2Ch] BYREF

v2 = paxos_normal_malloc(7520LL);
v3 = socket(2, 2, 0);
*(_DWORD *)v2 = v3;
if ( v3 < 0 )
{
perror("receiver socket");
result = 0LL;
}
else
{
optval[0] = 1;
if ( setsockopt(v3, 1, 2, optval, 4u) )
{
perror("setsockopt, setting SO_REUSEADDR");
result = 0LL;
}
else
{
v4 = inet_addr(cp);
*(_DWORD *)(v2 + 8) = v4;
if ( v4 == -1 )
{
v2 = 0LL;
puts("Error setting receiver->addr");
}
else
{
*(_WORD *)(v2 + 4) = 2;
v5 = func0(a2);
v6 = *(_DWORD *)v2;
*(_WORD *)(v2 + 6) = v5;
if ( bind(v6, (const struct sockaddr *)(v2 + 4), v7) )
{
v2 = 0LL;
perror("bind");
}
}
result = v2;
}
}
return result;
}

original_code:
udp_receiver * udp_unicast_receiver_blocking_new(char* address_string, int port) {
    udp_receiver * rec = paxos_normal_malloc(sizeof(udp_receiver));;
    rec->sock = socket(2, SOCK_DGRAM, 0);
    if (rec->sock < 0) {
        perror("receiver socket");
        return ((void *)0);
    }
    int activate = 1;
    if (setsockopt(rec->sock, 1, 2, &activate, sizeof(int)) != 0) {
        perror("setsockopt, setting SO_REUSEADDR");
        return ((void *)0);
    }
    struct sockaddr_in * addr_p = &rec->addr;
    addr_p->sin_addr.s_addr = inet_addr(address_string);
    if (addr_p->sin_addr.s_addr == ((in_addr_t) 0xffffffff)) {
        printf("Error setting receiver->addr\n");
        return ((void *)0);
    }
    addr_p->sin_family = 2;
    addr_p->sin_port = htons((uint16_t)port);
    if (bind(rec->sock, (struct sockaddr *) &rec->addr, sizeof(struct sockaddr_in)) != 0) {
        perror("bind");
        return ((void *)0);
    }
    return rec;
}

canonical_original_code:
struct udp_receiver_t *udp_unicast_receiver_blocking_new(char *address_string, int port) {
    struct udp_receiver_t *rec = paxos_normal_malloc(sizeof(udp_receiver));;
    rec->sock = socket(2, SOCK_DGRAM, 0);
    if (rec->sock < 0) {
        perror("receiver socket");
        return ((void *)0);
    }
    int activate = 1;
    if (setsockopt(rec->sock, 1, 2, &activate, sizeof(int)) != 0) {
        perror("setsockopt, setting SO_REUSEADDR");
        return ((void *)0);
    }
    struct sockaddr_in *addr_p = &rec->addr;
    addr_p->sin_addr.s_addr = inet_addr(address_string);
    if (addr_p->sin_addr.s_addr == ((unsigned int) 0xffffffff)) {
        printf("Error setting receiver->addr\n");
        return ((void *)0);
    }
    addr_p->sin_family = 2;
    addr_p->sin_port = htons((unsigned short)port);
    if (bind(rec->sock, (struct sockaddr *) &rec->addr, sizeof(struct sockaddr_in)) != 0) {
        perror("bind");
        return ((void *)0);
    }
    return rec;
}

variable_types: port int

variable_types: struct udp_receiver_t { int sock; 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]; } addr; char recv_buffer[7500]; } *

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

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

user_defined_types: struct udp_receiver_t { int sock; struct sockaddr_in addr; char recv_buffer[7500]; }

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


decompiled_code:
__int64 __fastcall udp_receiver_blocking_new(__int16 a1)
{
__int64 v1; // r12
int v2; // eax
__int16 v3; // ax
int v4; // edi
socklen_t v5; // edx
__int64 result; // rax
int optval; // [rsp+4h] [rbp-24h] BYREF
__int64 v8; // [rsp+8h] [rbp-20h]

v8 = 0LL;
v1 = paxos_normal_malloc(7520LL);
v2 = socket(2, 2, 0);
*(_DWORD *)v1 = v2;
if ( v2 < 0 )
{
perror("receiver socket");
result = 0LL;
}
else
{
optval = 1;
if ( setsockopt(v2, 1, 2, &optval, 4u) )
{
perror("setsockopt, setting SO_REUSEADDR");
result = 0LL;
}
else
{
*(_DWORD *)(v1 + 8) = 0;
*(_WORD *)(v1 + 4) = 2;
v3 = _bswap_16(a1);
v4 = *(_DWORD *)v1;
*(_WORD *)(v1 + 6) = v3;
if ( bind(v4, (const struct sockaddr *)(v1 + 4), v5) )
{
v1 = 0LL;
perror("bind");
}
result = v1;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int16 a1)
{
__int64 v1; // r12
int v2; // eax
__int16 v3; // ax
int v4; // edi
socklen_t v5; // edx
__int64 result; // rax
int optval; // [rsp+4h] [rbp-24h] BYREF
__int64 v8; // [rsp+8h] [rbp-20h]

v8 = 0LL;
v1 = paxos_normal_malloc(7520LL);
v2 = socket(2, 2, 0);
*(_DWORD *)v1 = v2;
if ( v2 < 0 )
{
perror("receiver socket");
result = 0LL;
}
else
{
optval = 1;
if ( setsockopt(v2, 1, 2, &optval, 4u) )
{
perror("setsockopt, setting SO_REUSEADDR");
result = 0LL;
}
else
{
*(_DWORD *)(v1 + 8) = 0;
*(_WORD *)(v1 + 4) = 2;
v3 = func0(a1);
v4 = *(_DWORD *)v1;
*(_WORD *)(v1 + 6) = v3;
if ( bind(v4, (const struct sockaddr *)(v1 + 4), v5) )
{
v1 = 0LL;
perror("bind");
}
result = v1;
}
}
return result;
}

original_code:
udp_receiver * udp_receiver_blocking_new(int port) {
    udp_receiver * rec = paxos_normal_malloc(sizeof(udp_receiver));;
    struct ip_mreq mreq;
    memset(&mreq, '\0', sizeof(struct ip_mreq));
    rec->sock = socket(2, SOCK_DGRAM, 0);
    if (rec->sock < 0) {
        perror("receiver socket");
        return ((void *)0);
    }
    int activate = 1;
    if (setsockopt(rec->sock, 1, 2, &activate, sizeof(int)) != 0) {
        perror("setsockopt, setting SO_REUSEADDR");
        return ((void *)0);
    }
    struct sockaddr_in * addr_p = &rec->addr;
    addr_p->sin_addr.s_addr = ((in_addr_t) 0x00000000);
    if (addr_p->sin_addr.s_addr == ((in_addr_t) 0xffffffff)) {
        printf("Error setting receiver->addr\n");
        return ((void *)0);
    }
    addr_p->sin_family = 2;
    addr_p->sin_port = htons((uint16_t)port);
    if (bind(rec->sock, (struct sockaddr *) &rec->addr, sizeof(struct sockaddr_in)) != 0) {
        perror("bind");
        return ((void *)0);
    }
    return rec;
}

canonical_original_code:
struct udp_receiver_t *udp_receiver_blocking_new(int port) {
    struct udp_receiver_t *rec = paxos_normal_malloc(sizeof(udp_receiver));;
    struct ip_mreq mreq;
    memset(&mreq, '\0', sizeof(struct ip_mreq));
    rec->sock = socket(2, SOCK_DGRAM, 0);
    if (rec->sock < 0) {
        perror("receiver socket");
        return ((void *)0);
    }
    int activate = 1;
    if (setsockopt(rec->sock, 1, 2, &activate, sizeof(int)) != 0) {
        perror("setsockopt, setting SO_REUSEADDR");
        return ((void *)0);
    }
    struct sockaddr_in *addr_p = &rec->addr;
    addr_p->sin_addr.s_addr = ((unsigned int) 0x00000000);
    if (addr_p->sin_addr.s_addr == ((unsigned int) 0xffffffff)) {
        printf("Error setting receiver->addr\n");
        return ((void *)0);
    }
    addr_p->sin_family = 2;
    addr_p->sin_port = htons((unsigned short)port);
    if (bind(rec->sock, (struct sockaddr *) &rec->addr, sizeof(struct sockaddr_in)) != 0) {
        perror("bind");
        return ((void *)0);
    }
    return rec;
}

variable_types: struct ip_mreq { struct in_addr { unsigned int s_addr; } imr_multiaddr; struct in_addr imr_interface; }

return_type: struct udp_receiver_t { int sock; 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]; } addr; char recv_buffer[7500]; } *

user_defined_types: struct ip_mreq { struct in_addr imr_multiaddr; struct in_addr imr_interface; }


decompiled_code:
int *__fastcall udp_unicast_receiver_new(char *a1, __int16 a2)
{
int *v2; // rax
int *v3; // r12
int v4; // eax

v2 = (int *)udp_unicast_receiver_blocking_new(a1, a2);
v3 = v2;
if ( !v2 )
return v3;
v4 = fcntl(*v2, 3);
if ( v4 >= 0 )
{
BYTE1(v4) |= 8u;
if ( fcntl(*v3, 4, (unsigned int)v4) < 0 )
{
v3 = 0LL;
perror("fcntl2");
}
return v3;
}
perror("fcntl1");
return 0LL;
}

canonical_decompiled_code:
int *__fastcall func8(char *a1, __int16 a2)
{
int *v2; // rax
int *v3; // r12
int v4; // eax

v2 = (int *)func6(a1, a2);
v3 = v2;
if ( !v2 )
return v3;
v4 = fcntl(*v2, 3);
if ( v4 >= 0 )
{
BYTE1(v4) |= 8u;
if ( fcntl(*v3, 4, (unsigned int)v4) < 0 )
{
v3 = 0LL;
perror("fcntl2");
}
return v3;
}
perror("fcntl1");
return 0LL;
}

original_code:
udp_receiver * udp_unicast_receiver_new(char* address_string, int port) {
    udp_receiver * rec;
    rec = udp_unicast_receiver_blocking_new(address_string, port);
    if(rec == ((void *)0)) {
        return ((void *)0);
    }
    int flag = fcntl(rec->sock, 3);
    if(flag < 0) {
        perror("fcntl1");
        return ((void *)0);
    }
    flag |= 04000;
    if(fcntl(rec->sock, 4, flag) < 0) {
        perror("fcntl2");
        return ((void *)0);
    }
    if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Socket %d created for address %s:%d (receive mode)\n", rec->sock, address_string, port) ;};
    return rec;
}

canonical_original_code:
struct udp_receiver_t *udp_unicast_receiver_new(char *address_string, int port) {
    struct udp_receiver_t *rec;
    rec = udp_unicast_receiver_blocking_new(address_string, port);
    if(rec == ((void *)0)) {
        return ((void *)0);
    }
    int flag = fcntl(rec->sock, 3);
    if(flag < 0) {
        perror("fcntl1");
        return ((void *)0);
    }
    flag |= 04000;
    if(fcntl(rec->sock, 4, flag) < 0) {
        perror("fcntl2");
        return ((void *)0);
    }
    if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Socket %d created for address %s:%d (receive mode)\n", rec->sock, address_string, port) ;};
    return rec;
}

variable_types: flag int


decompiled_code:
int *__fastcall udp_receiver_new(__int16 a1)
{
int *v1; // rax
int *v2; // r12
int v3; // eax

v1 = (int *)udp_receiver_blocking_new(a1);
v2 = v1;
if ( !v1 )
return v2;
v3 = fcntl(*v1, 3);
if ( v3 >= 0 )
{
BYTE1(v3) |= 8u;
if ( fcntl(*v2, 4, (unsigned int)v3) < 0 )
{
v2 = 0LL;
perror("fcntl2");
}
return v2;
}
perror("fcntl1");
return 0LL;
}

canonical_decompiled_code:
int *__fastcall func9(__int16 a1)
{
int *v1; // rax
int *v2; // r12
int v3; // eax

v1 = (int *)func7(a1);
v2 = v1;
if ( !v1 )
return v2;
v3 = fcntl(*v1, 3);
if ( v3 >= 0 )
{
BYTE1(v3) |= 8u;
if ( fcntl(*v2, 4, (unsigned int)v3) < 0 )
{
v2 = 0LL;
perror("fcntl2");
}
return v2;
}
perror("fcntl1");
return 0LL;
}

original_code:
udp_receiver * udp_receiver_new(int port) {
    udp_receiver * rec;
    rec = udp_receiver_blocking_new(port);
    if(rec == ((void *)0)) {
        return ((void *)0);
    }
    int flag = fcntl(rec->sock, 3);
    if(flag < 0) {
        perror("fcntl1");
        return ((void *)0);
    }
    flag |= 04000;
    if(fcntl(rec->sock, 4, flag) < 0) {
        perror("fcntl2");
        return ((void *)0);
    }
    if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Socket %d created for address %d (receive mode)\n", rec->sock, port) ;};
    return rec;
}

canonical_original_code:
struct udp_receiver_t *udp_receiver_new(int port) {
    struct udp_receiver_t *rec;
    rec = udp_receiver_blocking_new(port);
    if(rec == ((void *)0)) {
        return ((void *)0);
    }
    int flag = fcntl(rec->sock, 3);
    if(flag < 0) {
        perror("fcntl1");
        return ((void *)0);
    }
    flag |= 04000;
    if(fcntl(rec->sock, 4, flag) < 0) {
        perror("fcntl2");
        return ((void *)0);
    }
    if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Socket %d created for address %d (receive mode)\n", rec->sock, port) ;};
    return rec;
}


decompiled_code:
__int64 __fastcall udp_receiver_destroy(void *ptr)
{
unsigned int v1; // er12

if ( close(*(_DWORD *)ptr) )
{
v1 = -1;
puts("Error closing socket");
perror("close");
}
else
{
v1 = 0;
}
free(ptr);
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func10(void *ptr)
{
unsigned int v1; // er12

if ( close(*(_DWORD *)ptr) )
{
v1 = -1;
puts("Error closing socket");
perror("close");
}
else
{
v1 = 0;
}
free(ptr);
return v1;
}

original_code:
int udp_receiver_destroy(udp_receiver * rec) {
    int ret = 0;
    if (close(rec->sock) != 0) {
        printf("Error closing socket\n");
        perror("close");
        ret = -1;
    }
    if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Socket %d closed\n", rec->sock) ;};
    free(rec);
    return ret;
}

canonical_original_code:
int udp_receiver_destroy(struct udp_receiver_t *rec) {
    int ret = 0;
    if (close(rec->sock) != 0) {
        printf("Error closing socket\n");
        perror("close");
        ret = -1;
    }
    if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Socket %d closed\n", rec->sock) ;};
    free(rec);
    return ret;
}

variable_types: ret int

return_type: int


decompiled_code:
__int64 __fastcall udp_read_next_message(int *a1)
{
struct sockaddr *v1; // r8
__int64 v2; // rbp
void *v3; // rsi
int v4; // edi
int v5; // eax
socklen_t addr_len[3]; // [rsp+Ch] [rbp-Ch] BYREF

v1 = (struct sockaddr *)(a1 + 1);
v2 = (__int64)(a1 + 5);
v3 = a1 + 5;
v4 = *a1;
addr_len[0] = 16;
v5 = recvfrom(v4, v3, 0x1D4CuLL, 256, v1, addr_len);
if ( v5 >= 0 )
return validate_paxos_msg(v2, v5);
perror("recvfrom");
sleep(1u);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func11(int *a1)
{
struct sockaddr *v1; // r8
__int64 v2; // rbp
void *v3; // rsi
int v4; // edi
int v5; // eax
socklen_t addr_len[3]; // [rsp+Ch] [rbp-Ch] BYREF

v1 = (struct sockaddr *)(a1 + 1);
v2 = (__int64)(a1 + 5);
v3 = a1 + 5;
v4 = *a1;
addr_len[0] = 16;
v5 = recvfrom(v4, v3, 0x1D4CuLL, 256, v1, addr_len);
if ( v5 >= 0 )
return func4(v2, v5);
perror("recvfrom");
sleep(1u);
return 0xFFFFFFFFLL;
}

original_code:
int udp_read_next_message(udp_receiver * recv_info) {
    socklen_t addrlen = sizeof(struct sockaddr);
    int msg_size = recvfrom(recv_info->sock,
        recv_info->recv_buffer,
        7500,
        MSG_WAITALL,
        (struct sockaddr *)&recv_info->addr,
        &addrlen);
    if (msg_size < 0) {
        perror("recvfrom");
        sleep(1);
        return -1;
    }
    return validate_paxos_msg((paxos_msg*)recv_info->recv_buffer, msg_size);
}

canonical_original_code:
int udp_read_next_message(struct udp_receiver_t *recv_info) {
    unsigned int addrlen = sizeof(struct sockaddr);
    int msg_size = recvfrom(recv_info->sock,
        recv_info->recv_buffer,
        7500,
        MSG_WAITALL,
        (struct sockaddr *)&recv_info->addr,
        &addrlen);
    if (msg_size < 0) {
        perror("recvfrom");
        sleep(1);
        return -1;
    }
    return validate_paxos_msg((struct paxos_msg_t *)recv_info->recv_buffer, msg_size);
}

variable_types:
addrlen
unsigned int


decompiled_code:
int __fastcall sendbuf_clear(__int64 a1, __int64 a2, __int16 a3)
{
int result; // eax

*(_DWORD *)(a1 + 204) = 0;
*(_DWORD *)(a1 + 216) = a2;
*(_QWORD *)(a1 + 208) = 0LL;
result = a2;
switch ( (int)a2 )
{
case 1:
case 4:
*(_WORD *)(a1 + 222) = a3;
*(_QWORD *)(a1 + 208) = 4LL;
*(_WORD *)(a1 + 220) = 0;
break;
case 2:
case 8:
*(_WORD *)(a1 + 220) = a3;
*(_QWORD *)(a1 + 208) = 4LL;
*(_WORD *)(a1 + 222) = 0;
break;
case 16:
*(_QWORD *)(a1 + 208) = 4LL;
result = 0;
*(_WORD *)(a1 + 220) = 0;
break;
case 32:
return result;
default:
result = printf("Invalid message type %d for sendbuf_clear!\n", a2);
break;
}
return result;
}

canonical_decompiled_code:
int __fastcall func1(__int64 a1, __int64 a2, __int16 a3)
{
int result; // eax

*(_DWORD *)(a1 + 204) = 0;
*(_DWORD *)(a1 + 216) = a2;
*(_QWORD *)(a1 + 208) = 0LL;
result = a2;
switch ( (int)a2 )
{
case 1:
case 4:
*(_WORD *)(a1 + 222) = a3;
*(_QWORD *)(a1 + 208) = 4LL;
*(_WORD *)(a1 + 220) = 0;
break;
case 2:
case 8:
*(_WORD *)(a1 + 220) = a3;
*(_QWORD *)(a1 + 208) = 4LL;
*(_WORD *)(a1 + 222) = 0;
break;
case 16:
*(_QWORD *)(a1 + 208) = 4LL;
result = 0;
*(_WORD *)(a1 + 220) = 0;
break;
case 32:
return result;
default:
result = printf("Invalid message type %d for sendbuf_clear!\n", a2);
break;
}
return result;
}

original_code:
void sendbuf_clear(udp_send_buffer * sb, paxos_msg_code type, short int sender_id) {
    sb->dirty = 0;
    paxos_msg * m = (paxos_msg *) &sb->buffer;
    m->type = type;
    m->data_size = 0;
    switch(type) {
        case prepare_reqs: {
            m->data_size = sizeof(prepare_req_batch);
            prepare_req_batch * prb = (prepare_req_batch *)&m->data;
            prb->count = 0;
            prb->proposer_id = sender_id;
        } break;
        case accept_reqs: {
            m->data_size = sizeof(accept_req_batch);
            accept_req_batch * arb = (accept_req_batch *)&m->data;
            arb->count = 0;
            arb->proposer_id = sender_id;
        } break;
        case prepare_acks: {
            m->data_size = sizeof(prepare_ack_batch);
            prepare_ack_batch * pab = (prepare_ack_batch *)&m->data;
            pab->count = 0;
            pab->acceptor_id = sender_id;
        } break;
        case accept_acks: {
            m->data_size += sizeof(accept_ack_batch);
            accept_ack_batch * aab = (accept_ack_batch *)&m->data;
            aab->count = 0;
            aab->acceptor_id = sender_id;
        } break;
        case repeat_reqs: {
            m->data_size += sizeof(repeat_req_batch);
            repeat_req_batch * rrb = (repeat_req_batch *)&m->data;
            rrb->count = 0;
        } break;
        case submit: {
            m->data_size += 0;
        } break;
        default: {
            printf("Invalid message type %d for sendbuf_clear!\n",
                type);
        }
    }
}

canonical_original_code:
void sendbuf_clear(struct udp_send_buffer_t *sb, enum pax_msg_code_e type, short sender_id) {
    sb->dirty = 0;
    struct paxos_msg_t *m = (paxos_msg *) &sb->buffer;
    m->type = type;
    m->data_size = 0;
    switch(type) {
        case prepare_reqs: {
            m->data_size = sizeof(prepare_req_batch);
            struct prepare_req_batch_t *prb = (prepare_req_batch *)&m->data;
            prb->count = 0;
            prb->proposer_id = sender_id;
        } break;
        case accept_reqs: {
            m->data_size = sizeof(accept_req_batch);
            struct accept_req_batch_t *arb = (accept_req_batch *)&m->data;
            arb->count = 0;
            arb->proposer_id = sender_id;
        } break;
        case prepare_acks: {
            m->data_size = sizeof(prepare_ack_batch);
            struct prepare_ack_batch_t *pab = (prepare_ack_batch *)&m->data;
            pab->count = 0;
            pab->acceptor_id = sender_id;
        } break;
        case accept_acks: {
            m->data_size += sizeof(accept_ack_batch);
            struct accept_ack_batch_t *aab = (accept_ack_batch *)&m->data;
            aab->count = 0;
            aab->acceptor_id = sender_id;
        } break;
        case repeat_reqs: {
            m->data_size += sizeof(repeat_req_batch);
            struct repeat_req_batch_t *rrb = (repeat_req_batch *)&m->data;
            rrb->count = 0;
        } break;
        case submit: {
            m->data_size += 0;
        } break;
        default: {
            printf("Invalid message type %d for sendbuf_clear!\n",
                type);
        }
    }
}

variable_types: struct udp_send_buffer_t { int sock[10]; 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]; } addr[10]; int sockets; int dirty; char buffer[7500]; } *

variable_types: struct paxos_msg_t { unsigned long data_size; enum pax_msg_code_e { prepare_reqs=1, prepare_acks=2, accept_reqs=4, accept_acks=8, repeat_reqs=16, submit=32, leader_announce=64, alive_ping=65 } type; char data[]; } *

variable_types: struct prepare_req_batch_t { short count; short proposer_id; struct prepare_req_t { unsigned int iid; unsigned int ballot; } prepares[]; } *

variable_types: struct repeat_req_batch_t { short count; unsigned int requests[]; } *

user_defined_types: struct udp_send_buffer_t { int sock[10]; struct sockaddr_in addr[10]; int sockets; int dirty; char buffer[7500]; }

user_defined_types: struct accept_ack_batch_t { short acceptor_id; short count; char data[]; }


decompiled_code:
void *__fastcall sendbuf_add_submit_val(__int64 a1, const void *a2, size_t a3)
{
_DWORD *v3; // rdi

if ( *(_DWORD *)(a1 + 216) != 32 )
_assert_fail("m->type == submit", "udp_sendbuf.c", 0xD3u, "sendbuf_add_submit_val");
*(_QWORD *)(a1 + 208) += a3;
v3 = (_DWORD *)(a1 + 220);
*(v3 - 4) = 1;
return memcpy(v3, a2, a3);
}

canonical_decompiled_code:
void *__fastcall func2(__int64 a1, const void *a2, size_t a3)
{
_DWORD *v3; // rdi

if ( *(_DWORD *)(a1 + 216) != 32 )
_assert_fail("m->type == submit", "udp_sendbuf.c", 0xD3u, "sendbuf_add_submit_val");
*(_QWORD *)(a1 + 208) += a3;
v3 = (_DWORD *)(a1 + 220);
*(v3 - 4) = 1;
return memcpy(v3, a2, a3);
}

original_code:
void sendbuf_add_submit_val(udp_send_buffer * sb, char * value, size_t val_size) {
    paxos_msg * m = (paxos_msg *) &sb->buffer;
    ((void) sizeof ((m->type == submit) ? 1 : 0), __extension__ ({ if (m->type == submit) ; else __assert_fail ("m->type == submit", "udp_sendbuf.c", 211, __extension__ __PRETTY_FUNCTION__); }));
    sb->dirty = 1;
    m->data_size += val_size;
    memcpy(m->data, value, val_size);
}

canonical_original_code:
void sendbuf_add_submit_val(struct udp_send_buffer_t *sb, char *value, unsigned long val_size) {
    struct paxos_msg_t *m = (paxos_msg *) &sb->buffer;
    ((void) sizeof ((m->type == submit) ? 1 : 0), __extension__ ({ if (m->type == submit) ; else __assert_fail ("m->type == submit", "udp_sendbuf.c", 211, __extension__ __PRETTY_FUNCTION__); }));
    sb->dirty = 1;
    m->data_size += val_size;
    memcpy(m->data, value, val_size);
}

variable_types:
value
char *


decompiled_code:
void __fastcall sendbuf_flush(_DWORD *a1)
{
const struct sockaddr *v1; // r13
__int64 v2; // rbp
int v3; // eax

if ( a1[51] && (int)a1[50] > 0 )
{
v1 = (const struct sockaddr *)(a1 + 10);
v2 = 0LL;
do
{
v3 = sendto(a1[v2], a1 + 52, *((_QWORD *)a1 + 26) + 16LL, 0, v1, 0x10u);
if ( a1[52] + 16 != v3 || v3 == -1 )
perror("failed to send message");
++v2;
++v1;
}
while ( a1[50] > (int)v2 );
}
}

canonical_decompiled_code:
void __fastcall func3(_DWORD *a1)
{
const struct sockaddr *v1; // r13
__int64 v2; // rbp
int v3; // eax

if ( a1[51] && (int)a1[50] > 0 )
{
v1 = (const struct sockaddr *)(a1 + 10);
v2 = 0LL;
do
{
v3 = sendto(a1[v2], a1 + 52, *((_QWORD *)a1 + 26) + 16LL, 0, v1, 0x10u);
if ( a1[52] + 16 != v3 || v3 == -1 )
perror("failed to send message");
++v2;
++v1;
}
while ( a1[50] > (int)v2 );
}
}

original_code:
void sendbuf_flush(udp_send_buffer * sb) {
    int cnt, i;
    if(!sb->dirty) {
        return;
    }
    paxos_msg * m = (paxos_msg *) &sb->buffer;
    for(i = 0; i < sb->sockets; ++i) {
  cnt = sendto(sb->sock[i],
   sb->buffer,
   (m->data_size + sizeof(paxos_msg)),
   0,
   (struct sockaddr *)&sb->addr[i],
   sizeof(struct sockaddr_in));
  if (cnt != (int)(m->data_size + sizeof(paxos_msg)) || cnt == -1) {
   perror("failed to send message");
  }
    }
    if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Sent message of size %lu\n", (m->data_size + sizeof(paxos_msg))) ;};
}

canonical_original_code:
void sendbuf_flush(struct udp_send_buffer_t *sb) {
    int cnt; int i;
    if(!sb->dirty) {
        return;
    }
    struct paxos_msg_t *m = (paxos_msg *) &sb->buffer;
    for(i = 0; i < sb->sockets; ++i) {
  cnt = sendto(sb->sock[i],
   sb->buffer,
   (m->data_size + sizeof(paxos_msg)),
   0,
   (struct sockaddr *)&sb->addr[i],
   sizeof(struct sockaddr_in));
  if (cnt != (int)(m->data_size + sizeof(paxos_msg)) || cnt == -1) {
   perror("failed to send message");
  }
    }
    if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Sent message of size %lu\n", (m->data_size + sizeof(paxos_msg))) ;};
}

variable_types: cnt int

variable_types: i int


original_code:
void sendbuf_add_prepare_req(udp_send_buffer * sb, iid_t iid, ballot_t ballot) {
    paxos_msg * m = (paxos_msg *) &sb->buffer;
    ((void) sizeof ((m->type == prepare_reqs) ? 1 : 0), __extension__ ({ if (m->type == prepare_reqs) ; else __assert_fail ("m->type == prepare_reqs", "udp_sendbuf.c", 81, __extension__ __PRETTY_FUNCTION__); }));
    prepare_req_batch * prb = (prepare_req_batch *)&m->data;
    if((m->data_size + sizeof(paxos_msg)) + sizeof(prepare_req) >= 7500) {
        sendbuf_flush(sb);
        sendbuf_clear(sb, m->type, prb->proposer_id);
    }
    prepare_req * pr = (prepare_req *)&prb->prepares[prb->count];
    pr->iid = iid;
    pr->ballot = ballot;
    prb->count += 1;
    sb->dirty = 1;
    m->data_size += sizeof(prepare_req);
}

canonical_original_code:
void sendbuf_add_prepare_req(struct udp_send_buffer_t *sb, unsigned int iid, unsigned int ballot) {
    struct paxos_msg_t *m = (paxos_msg *) &sb->buffer;
    ((void) sizeof ((m->type == prepare_reqs) ? 1 : 0), __extension__ ({ if (m->type == prepare_reqs) ; else __assert_fail ("m->type == prepare_reqs", "udp_sendbuf.c", 81, __extension__ __PRETTY_FUNCTION__); }));
    struct prepare_req_batch_t *prb = (prepare_req_batch *)&m->data;
    if((m->data_size + sizeof(paxos_msg)) + sizeof(prepare_req) >= 7500) {
        sendbuf_flush(sb);
        sendbuf_clear(sb, m->type, prb->proposer_id);
    }
    struct prepare_req_t *pr = (prepare_req *)&prb->prepares[prb->count];
    pr->iid = iid;
    pr->ballot = ballot;
    prb->count += 1;
    sb->dirty = 1;
    m->data_size += sizeof(prepare_req);
}

variable_types:
iid
unsigned int

variable_types:
ballot
unsigned int

variable_types: struct prepare_req_t { unsigned int iid; unsigned int ballot; } *


decompiled_code:
void *__fastcall sendbuf_add_prepare_ack(__int64 a1, __int64 a2)
{
size_t v2; // rdx
__int64 v3; // rax
size_t v5; // r12
_DWORD *v6; // rdi
int v7; // eax
void *result; // rax

if ( *(_DWORD *)(a1 + 216) != 2 )
_assert_fail("m->type == prepare_acks", "udp_sendbuf.c", 0x69u, "sendbuf_add_prepare_ack");
v2 = *(_QWORD *)(a2 + 16);
v3 = *(_QWORD *)(a1 + 208);
v5 = v2 + 24;
if ( v2 + v3 + 40 > 0x1D4B )
{
stablestorage_tx_end();
sendbuf_flush((_DWORD *)a1);
sendbuf_clear(a1, *(unsigned int *)(a1 + 216), *(_WORD *)(a1 + 220));
stablestorage_tx_begin();
v3 = *(_QWORD *)(a1 + 208);
v2 = *(_QWORD *)(a2 + 16);
}
v6 = (_DWORD *)(a1 + v3 + 244);
*(v6 - 6) = *(_DWORD *)a2;
*(v6 - 5) = *(_DWORD *)(a2 + 4);
v7 = *(_DWORD *)(a2 + 8);
*((_QWORD *)v6 - 1) = v2;
*(v6 - 4) = v7;
result = memcpy(v6, (const void *)(a2 + 24), v2);
*(_QWORD *)(a1 + 208) += v5;
++*(_WORD *)(a1 + 222);
*(_DWORD *)(a1 + 204) = 1;
return result;
}

original_code:
void sendbuf_add_prepare_ack(udp_send_buffer * sb, acceptor_record * rec) {
    paxos_msg * m = (paxos_msg *) &sb->buffer;
    ((void) sizeof ((m->type == prepare_acks) ? 1 : 0), __extension__ ({ if (m->type == prepare_acks) ; else __assert_fail ("m->type == prepare_acks", "udp_sendbuf.c", 105, __extension__ __PRETTY_FUNCTION__); }));
    prepare_ack_batch * pab = (prepare_ack_batch *)&m->data;
    size_t pa_size = (sizeof(prepare_ack) + rec->value_size);
    if((m->data_size + sizeof(paxos_msg)) + pa_size >= 7500) {
        stablestorage_tx_end();
        sendbuf_flush(sb);
        sendbuf_clear(sb, m->type, pab->acceptor_id);
        stablestorage_tx_begin();
    }
    prepare_ack * pa = (prepare_ack *)&m->data[m->data_size];
    pa->iid = rec->iid;
    pa->ballot = rec->ballot;
    pa->value_ballot = rec->value_ballot;
    pa->value_size = rec->value_size;
    memcpy(pa->value, rec->value, rec->value_size);
    sb->dirty = 1;
    m->data_size += pa_size;
    pab->count += 1;
}

canonical_original_code:
void sendbuf_add_prepare_ack(struct udp_send_buffer_t *sb, struct accept_ack_t *rec) {
    struct paxos_msg_t *m = (paxos_msg *) &sb->buffer;
    ((void) sizeof ((m->type == prepare_acks) ? 1 : 0), __extension__ ({ if (m->type == prepare_acks) ; else __assert_fail ("m->type == prepare_acks", "udp_sendbuf.c", 105, __extension__ __PRETTY_FUNCTION__); }));
    struct prepare_ack_batch_t *pab = (prepare_ack_batch *)&m->data;
    unsigned long pa_size = (sizeof(prepare_ack) + rec->value_size);
    if((m->data_size + sizeof(paxos_msg)) + pa_size >= 7500) {
        stablestorage_tx_end();
        sendbuf_flush(sb);
        sendbuf_clear(sb, m->type, pab->acceptor_id);
        stablestorage_tx_begin();
    }
    struct prepare_ack_t *pa = (prepare_ack *)&m->data[m->data_size];
    pa->iid = rec->iid;
    pa->ballot = rec->ballot;
    pa->value_ballot = rec->value_ballot;
    pa->value_size = rec->value_size;
    memcpy(pa->value, rec->value, rec->value_size);
    sb->dirty = 1;
    m->data_size += pa_size;
    pab->count += 1;
}

variable_types: struct accept_ack_t { unsigned int iid; unsigned int ballot; unsigned int value_ballot; short is_final; unsigned long value_size; char value[]; } *

variable_types: struct prepare_ack_batch_t { short acceptor_id; short count; char data[]; } *

variable_types: struct prepare_ack_t { unsigned int iid; unsigned int ballot; unsigned int value_ballot; unsigned long value_size; char value[]; } *


decompiled_code:
void *__fastcall sendbuf_add_accept_req(__int64 a1, int a2, int a3, const void *a4, size_t a5)
{
__int64 v5; // rax
size_t v10; // rbp
_DWORD *v11; // rdi
void *result; // rax

if ( *(_DWORD *)(a1 + 216) != 4 )
_assert_fail("m->type == accept_reqs", "udp_sendbuf.c", 0x87u, "sendbuf_add_accept_req");
v5 = *(_QWORD *)(a1 + 208);
v10 = a5 + 16;
if ( v5 + a5 + 32 > 0x1D4B )
{
sendbuf_flush((_DWORD *)a1);
sendbuf_clear(a1, *(unsigned int *)(a1 + 216), *(_WORD *)(a1 + 222));
v5 = *(_QWORD *)(a1 + 208);
}
*(_DWORD *)(a1 + v5 + 220) = a2;
v11 = (_DWORD *)(a1 + v5 + 236);
*(v11 - 3) = a3;
*((_QWORD *)v11 - 1) = a5;
result = memcpy(v11, a4, a5);
*(_QWORD *)(a1 + 208) += v10;
++*(_WORD *)(a1 + 220);
*(_DWORD *)(a1 + 204) = 1;
return result;
}

original_code:
void sendbuf_add_accept_req(udp_send_buffer * sb, iid_t iid, ballot_t ballot, char * value, size_t val_size) {
    paxos_msg * m = (paxos_msg *) &sb->buffer;
    ((void) sizeof ((m->type == accept_reqs) ? 1 : 0), __extension__ ({ if (m->type == accept_reqs) ; else __assert_fail ("m->type == accept_reqs", "udp_sendbuf.c", 135, __extension__ __PRETTY_FUNCTION__); }));
    accept_req_batch * arb = (accept_req_batch *)&m->data;
    size_t ar_size = sizeof(accept_req) + val_size;
    if((m->data_size + sizeof(paxos_msg)) + ar_size >= 7500) {
        sendbuf_flush(sb);
        sendbuf_clear(sb, m->type, arb->proposer_id);
    }
    accept_req * ar = (accept_req *)&m->data[m->data_size];
    ar->iid = iid;
    ar->ballot = ballot;
    ar->value_size = val_size;
    memcpy(ar->value, value, val_size);
    sb->dirty = 1;
    m->data_size += ar_size;
    arb->count += 1;
}

canonical_original_code:
void sendbuf_add_accept_req(struct udp_send_buffer_t *sb, unsigned int iid, unsigned int ballot, char *value, unsigned long val_size) {
    struct paxos_msg_t *m = (paxos_msg *) &sb->buffer;
    ((void) sizeof ((m->type == accept_reqs) ? 1 : 0), __extension__ ({ if (m->type == accept_reqs) ; else __assert_fail ("m->type == accept_reqs", "udp_sendbuf.c", 135, __extension__ __PRETTY_FUNCTION__); }));
    struct accept_req_batch_t *arb = (accept_req_batch *)&m->data;
    unsigned long ar_size = sizeof(accept_req) + val_size;
    if((m->data_size + sizeof(paxos_msg)) + ar_size >= 7500) {
        sendbuf_flush(sb);
        sendbuf_clear(sb, m->type, arb->proposer_id);
    }
    struct accept_req_t *ar = (accept_req *)&m->data[m->data_size];
    ar->iid = iid;
    ar->ballot = ballot;
    ar->value_size = val_size;
    memcpy(ar->value, value, val_size);
    sb->dirty = 1;
    m->data_size += ar_size;
    arb->count += 1;
}

variable_types: struct accept_req_batch_t { short count; short proposer_id; char data[]; } *

variable_types: struct accept_req_t { unsigned int iid; unsigned int ballot; unsigned long value_size; char value[]; } *


decompiled_code:
void *__fastcall sendbuf_add_accept_ack(__int64 a1, _QWORD *a2)
{
__int64 v2; // rdx
__int64 v3; // rax
size_t v4; // r12
void *result; // rax

if ( *(_DWORD *)(a1 + 216) != 8 )
_assert_fail("m->type == accept_acks", "udp_sendbuf.c", 0xA4u, "sendbuf_add_accept_ack");
v2 = a2[2];
v3 = *(_QWORD *)(a1 + 208);
v4 = v2 + 24;
if ( (unsigned __int64)(v2 + v3 + 40) > 0x1D4B )
{
stablestorage_tx_end();
sendbuf_flush((_DWORD *)a1);
sendbuf_clear(a1, *(unsigned int *)(a1 + 216), *(_WORD *)(a1 + 220));
stablestorage_tx_begin();
v3 = *(_QWORD *)(a1 + 208);
}
result = memcpy((void *)(a1 + v3 + 220), a2, v4);
*(_QWORD *)(a1 + 208) += v4;
++*(_WORD *)(a1 + 222);
*(_DWORD *)(a1 + 204) = 1;
return result;
}

canonical_decompiled_code:
void *__fastcall func7(__int64 a1, _QWORD *a2)
{
__int64 v2; // rdx
__int64 v3; // rax
size_t v4; // r12
void *result; // rax

if ( *(_DWORD *)(a1 + 216) != 8 )
_assert_fail("m->type == accept_acks", "udp_sendbuf.c", 0xA4u, "sendbuf_add_accept_ack");
v2 = a2[2];
v3 = *(_QWORD *)(a1 + 208);
v4 = v2 + 24;
if ( (unsigned __int64)(v2 + v3 + 40) > 0x1D4B )
{
stablestorage_tx_end();
func3((_DWORD *)a1);
func1(a1, *(unsigned int *)(a1 + 216), *(_WORD *)(a1 + 220));
stablestorage_tx_begin();
v3 = *(_QWORD *)(a1 + 208);
}
result = memcpy((void *)(a1 + v3 + 220), a2, v4);
*(_QWORD *)(a1 + 208) += v4;
++*(_WORD *)(a1 + 222);
*(_DWORD *)(a1 + 204) = 1;
return result;
}

original_code:
void sendbuf_add_accept_ack(udp_send_buffer * sb, acceptor_record * rec) {
    paxos_msg * m = (paxos_msg *) &sb->buffer;
    ((void) sizeof ((m->type == accept_acks) ? 1 : 0), __extension__ ({ if (m->type == accept_acks) ; else __assert_fail ("m->type == accept_acks", "udp_sendbuf.c", 164, __extension__ __PRETTY_FUNCTION__); }));
    accept_ack_batch * aab = (accept_ack_batch *)&m->data;
    size_t aa_size = (rec->value_size + sizeof(accept_ack));
    if((m->data_size + sizeof(paxos_msg)) + aa_size >= 7500) {
        stablestorage_tx_end();
        sendbuf_flush(sb);
        sendbuf_clear(sb, m->type, aab->acceptor_id);
        stablestorage_tx_begin();
    }
    accept_ack * aa = (accept_ack *)&m->data[m->data_size];
    memcpy(aa, rec, aa_size);
    sb->dirty = 1;
    m->data_size += aa_size;
    aab->count += 1;
}

canonical_original_code:
void sendbuf_add_accept_ack(struct udp_send_buffer_t *sb, struct accept_ack_t *rec) {
    struct paxos_msg_t *m = (paxos_msg *) &sb->buffer;
    ((void) sizeof ((m->type == accept_acks) ? 1 : 0), __extension__ ({ if (m->type == accept_acks) ; else __assert_fail ("m->type == accept_acks", "udp_sendbuf.c", 164, __extension__ __PRETTY_FUNCTION__); }));
    struct accept_ack_batch_t *aab = (accept_ack_batch *)&m->data;
    unsigned long aa_size = (rec->value_size + sizeof(accept_ack));
    if((m->data_size + sizeof(paxos_msg)) + aa_size >= 7500) {
        stablestorage_tx_end();
        sendbuf_flush(sb);
        sendbuf_clear(sb, m->type, aab->acceptor_id);
        stablestorage_tx_begin();
    }
    struct accept_ack_t *aa = (accept_ack *)&m->data[m->data_size];
    memcpy(aa, rec, aa_size);
    sb->dirty = 1;
    m->data_size += aa_size;
    aab->count += 1;
}

variable_types: struct accept_ack_batch_t { short acceptor_id; short count; char data[]; } *

user_defined_types: struct accept_ack_t { unsigned int iid; unsigned int ballot; unsigned int value_ballot; short is_final; unsigned long value_size; char value[]; }


decompiled_code:
__int64 __fastcall sendbuf_add_repeat_req(__int64 a1, int a2)
{
__int64 v2; // rax
__int64 v3; // rdx
__int64 result; // rax

if ( *(_DWORD *)(a1 + 216) != 16 )
_assert_fail("m->type == repeat_reqs", "udp_sendbuf.c", 0xC0u, "sendbuf_add_repeat_req");
v2 = *(_QWORD *)(a1 + 208);
if ( (unsigned __int64)(v2 + 20) > 0x1D4B )
{
sendbuf_flush((_DWORD *)a1);
sendbuf_clear(a1, *(unsigned int *)(a1 + 216), -1);
v2 = *(_QWORD *)(a1 + 208);
}
v3 = *(__int16 *)(a1 + 220);
*(_DWORD *)(a1 + 204) = 1;
*(_QWORD *)(a1 + 208) = v2 + 4;
*(_DWORD *)(a1 + 4 * v3 + 224) = a2;
result = (unsigned int)(v3 + 1);
*(_WORD *)(a1 + 220) = v3 + 1;
return result;
}

original_code:
void sendbuf_add_repeat_req(udp_send_buffer * sb, iid_t iid) {
    paxos_msg * m = (paxos_msg *) &sb->buffer;
    ((void) sizeof ((m->type == repeat_reqs) ? 1 : 0), __extension__ ({ if (m->type == repeat_reqs) ; else __assert_fail ("m->type == repeat_reqs", "udp_sendbuf.c", 192, __extension__ __PRETTY_FUNCTION__); }));
    if((m->data_size + sizeof(paxos_msg)) + sizeof(iid_t) >= 7500) {
        sendbuf_flush(sb);
        sendbuf_clear(sb, m->type, -1);
    }
    sb->dirty = 1;
    m->data_size += sizeof(iid_t);
    repeat_req_batch * rrb = (repeat_req_batch *)&m->data;
    rrb->requests[rrb->count] = iid;
    rrb->count += 1;
}

canonical_original_code:
void sendbuf_add_repeat_req(struct udp_send_buffer_t *sb, unsigned int iid) {
    struct paxos_msg_t *m = (paxos_msg *) &sb->buffer;
    ((void) sizeof ((m->type == repeat_reqs) ? 1 : 0), __extension__ ({ if (m->type == repeat_reqs) ; else __assert_fail ("m->type == repeat_reqs", "udp_sendbuf.c", 192, __extension__ __PRETTY_FUNCTION__); }));
    if((m->data_size + sizeof(paxos_msg)) + sizeof(iid_t) >= 7500) {
        sendbuf_flush(sb);
        sendbuf_clear(sb, m->type, -1);
    }
    sb->dirty = 1;
    m->data_size += sizeof(iid_t);
    struct repeat_req_batch_t *rrb = (repeat_req_batch *)&m->data;
    rrb->requests[rrb->count] = iid;
    rrb->count += 1;
}


original_code:
void sendbuf_send_ping(udp_send_buffer * sb, short int proposer_id, long unsigned int sequence_number) {
    paxos_msg * m = (paxos_msg *) &sb->buffer;
    sb->dirty = 1;
    m->type = alive_ping;
    m->data_size = sizeof(alive_ping_msg);
    alive_ping_msg * ap = (alive_ping_msg *) m->data;
    ap->proposer_id = proposer_id;
    ap->sequence_number = sequence_number;
    sendbuf_flush(sb);
}

canonical_original_code:
void sendbuf_send_ping(struct udp_send_buffer_t *sb, short proposer_id, unsigned long sequence_number) {
    struct paxos_msg_t *m = (paxos_msg *) &sb->buffer;
    sb->dirty = 1;
    m->type = alive_ping;
    m->data_size = sizeof(alive_ping_msg);
    struct alive_ping_msg_t *ap = (alive_ping_msg *) m->data;
    ap->proposer_id = proposer_id;
    ap->sequence_number = sequence_number;
    sendbuf_flush(sb);
}

variable_types: struct alive_ping_msg_t { short proposer_id; unsigned long sequence_number; } *


original_code:
void sendbuf_send_leader_announce(udp_send_buffer * sb, short int leader_id) {
    paxos_msg * m = (paxos_msg *) &sb->buffer;
    sb->dirty = 1;
    m->type = leader_announce;
    m->data_size = sizeof(leader_announce_msg);
    leader_announce_msg * la = (leader_announce_msg *) m->data;
    la->current_leader = leader_id;
    sendbuf_flush(sb);
}

canonical_original_code:
void sendbuf_send_leader_announce(struct udp_send_buffer_t *sb, short leader_id) {
    struct paxos_msg_t *m = (paxos_msg *) &sb->buffer;
    sb->dirty = 1;
    m->type = leader_announce;
    m->data_size = sizeof(leader_announce_msg);
    struct leader_announce_msg_t *la = (leader_announce_msg *) m->data;
    la->current_leader = leader_id;
    sendbuf_flush(sb);
}

variable_types: struct udp_send_buffer_t { int sock[10]; 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]; } addr[10]; int sockets; int dirty; char buffer[7500]; } *

variable_types: struct leader_announce_msg_t { short current_leader; } *


decompiled_code:
int oracle_lock()
{
return pthread_mutex_lock(&oracle_mutex);
}

canonical_decompiled_code:
int func0()
{
return pthread_mutex_lock(&oracle_mutex);
}

original_code:
void oracle_lock() {
    pthread_mutex_lock(&oracle_mutex);
}


decompiled_code:
int oracle_unlock()
{
return pthread_mutex_unlock(&oracle_mutex);
}

canonical_decompiled_code:
int func1()
{
return pthread_mutex_unlock(&oracle_mutex);
}

original_code:
void oracle_unlock() {
    pthread_mutex_unlock(&oracle_mutex);
}


decompiled_code:
void __fastcall __noreturn broadcast_current_leader(void *a1)
{
__int16 v1; // bx

while ( 1 )
{
oracle_lock();
v1 = current_leader;
oracle_unlock();
sendbuf_send_leader_announce(to_proposers, (unsigned int)v1);
sleep(announce_interval);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func2(void *a1)
{
__int16 v1; // bx

while ( 1 )
{
func0();
v1 = current_leader;
func1();
sendbuf_send_leader_announce(to_proposers, (unsigned int)v1);
sleep(announce_interval);
}
}

original_code:
void * broadcast_current_leader(void * arg) {
    arg = arg;
    while(1) {
        oracle_lock();
        short int leader = current_leader;
        oracle_unlock();
        sendbuf_send_leader_announce(to_proposers, leader);
        sleep(announce_interval);
    }
    return ((void *)0);
}

canonical_original_code:
void *broadcast_current_leader(void *arg) {
    arg = arg;
    while(1) {
        oracle_lock();
        short leader = current_leader;
        oracle_unlock();
        sendbuf_send_leader_announce(to_proposers, leader);
        sleep(announce_interval);
    }
    return ((void *)0);
}

variable_types:
arg
void *

variable_types: leader short

return_type: void *


decompiled_code:
void __fastcall __noreturn get_user_input(void *a1)
{
unsigned int v1[3]; // [rsp+Ch] [rbp-Ch] BYREF

printf("To force a leader, enter the proposer id [0...%d]\n", 9LL);
while ( 1 )
{
while ( 1 )
{
putchar(62);
if ( (unsigned int)_isoc99_scanf("%d", v1) == 1 )
break;
puts("Not a valid proposer ID");
}
if ( v1[0] > 9 )
{
printf("Proposer IDs are in the range 0...%d\n", 9LL);
}
else
{
oracle_lock();
if ( current_leader == v1[0] )
{
printf("Proposer %d is already leader\n", (unsigned int)current_leader);
}
else
{
printf("Leader is now proposer %d\n", v1[0]);
current_leader = v1[0];
}
oracle_unlock();
}
}
}

canonical_decompiled_code:
void __fastcall __noreturn func3(void *a1)
{
unsigned int v1[3]; // [rsp+Ch] [rbp-Ch] BYREF

printf("To force a leader, enter the proposer id [0...%d]\n", 9LL);
while ( 1 )
{
while ( 1 )
{
putchar(62);
if ( (unsigned int)_isoc99_scanf("%d", v1) == 1 )
break;
puts("Not a valid proposer ID");
}
if ( v1[0] > 9 )
{
printf("Proposer IDs are in the range 0...%d\n", 9LL);
}
else
{
func0();
if ( current_leader == v1[0] )
{
printf("Proposer %d is already leader\n", (unsigned int)current_leader);
}
else
{
printf("Leader is now proposer %d\n", v1[0]);
current_leader = v1[0];
}
func1();
}
}
}

original_code:
void * get_user_input(void * arg) {
    arg = arg;
    int proposer_id;
    printf("To force a leader, enter the proposer id [0...%d]\n",
        10 -1);
    while(1) {
        printf(">");
        if(scanf("%d", &proposer_id) != 1) {
            printf("Not a valid proposer ID\n");
            continue;
        }
        if(proposer_id < 0 || proposer_id >= 10) {
            printf("Proposer IDs are in the range 0...%d\n",
                10 -1);
            continue;
        }
        oracle_lock();
        if(proposer_id == current_leader) {
            printf("Proposer %d is already leader\n", proposer_id);
            oracle_unlock();
            continue;
        }
        printf("Leader is now proposer %d\n", proposer_id);
        current_leader = proposer_id;
        oracle_unlock();
    }
    return ((void *)0);
}

canonical_original_code:
void *get_user_input(void *arg) {
    arg = arg;
    int proposer_id;
    printf("To force a leader, enter the proposer id [0...%d]\n",
        10 -1);
    while(1) {
        printf(">");
        if(scanf("%d", &proposer_id) != 1) {
            printf("Not a valid proposer ID\n");
            continue;
        }
        if(proposer_id < 0 || proposer_id >= 10) {
            printf("Proposer IDs are in the range 0...%d\n",
                10 -1);
            continue;
        }
        oracle_lock();
        if(proposer_id == current_leader) {
            printf("Proposer %d is already leader\n", proposer_id);
            oracle_unlock();
            continue;
        }
        printf("Leader is now proposer %d\n", proposer_id);
        current_leader = proposer_id;
        oracle_unlock();
    }
    return ((void *)0);
}


decompiled_code:
void init_oracle_state()
{
;
}

canonical_decompiled_code:
void func4()
{
;
}

original_code:
void init_oracle_state() {
}

canonical_original_code:
void init_oracle_state() {
}


decompiled_code:
int __fastcall update_oracle_state(__int16 a1, __int64 a2)
{
return printf("Got a ping n:%lu from proposer %d \n", a2, (unsigned int)a1);
}

canonical_decompiled_code:
int __fastcall func5(__int16 a1, __int64 a2)
{
return printf("Got a ping n:%lu from proposer %d \n", a2, (unsigned int)a1);
}

original_code:
void update_oracle_state(short int proposer_id, long unsigned int seq_num, struct timeval * current_time) {
    current_time = current_time;
    printf("Got a ping n:%lu from proposer %d \n", seq_num, proposer_id);
}

canonical_original_code:
void update_oracle_state(short proposer_id, unsigned long seq_num, struct timeval *current_time) {
    current_time = current_time;
    printf("Got a ping n:%lu from proposer %d \n", seq_num, proposer_id);
}

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


decompiled_code:
void __fastcall __noreturn get_proposer_pings(void *a1)
{
__int64 v1; // rbx
__int64 v2; // rsi
struct timeval v3; // [rsp+0h] [rbp-18h] BYREF

while ( 1 )
{
while ( (int)udp_read_next_message(for_oracle) < 0 )
LABEL_4:
puts("Dropping invalid proposer message");
while ( 1 )
{
v1 = for_oracle;
v2 = *(unsigned int *)(for_oracle + 28);
if ( (_DWORD)v2 == 65 )
break;
printf("Unknow msg type %d received by oracle\n", v2);
if ( (int)udp_read_next_message(for_oracle) < 0 )
goto LABEL_4;
}
gettimeofday(&v3, 0LL);
update_oracle_state(*(_WORD *)(v1 + 32), *(_QWORD *)(v1 + 40));
}
}

canonical_decompiled_code:
void __fastcall __noreturn func6(void *a1)
{
__int64 v1; // rbx
__int64 v2; // rsi
struct timeval v3; // [rsp+0h] [rbp-18h] BYREF

while ( 1 )
{
while ( (int)udp_read_next_message(for_oracle) < 0 )
LABEL_4:
puts("Dropping invalid proposer message");
while ( 1 )
{
v1 = for_oracle;
v2 = *(unsigned int *)(for_oracle + 28);
if ( (_DWORD)v2 == 65 )
break;
printf("Unknow msg type %d received by oracle\n", v2);
if ( (int)udp_read_next_message(for_oracle) < 0 )
goto LABEL_4;
}
gettimeofday(&v3, 0LL);
func5(*(_WORD *)(v1 + 32), *(_QWORD *)(v1 + 40));
}
}

original_code:
void * get_proposer_pings(void * arg) {
    arg = arg;
    struct timeval current_time;
    while(1) {
        int valid = udp_read_next_message(for_oracle);
        if (valid < 0) {
            printf("Dropping invalid proposer message\n");
            continue;
        }
        paxos_msg * msg = (paxos_msg*) &for_oracle->recv_buffer;
        switch(msg->type) {
            case alive_ping: {
                alive_ping_msg * ap = (alive_ping_msg *) msg->data;
                gettimeofday(&current_time, ((void *)0));
                update_oracle_state(ap->proposer_id, ap->sequence_number, &current_time);
            }
            break;
            default: {
                printf("Unknow msg type %d received by oracle\n", msg->type);
            }
        }
    }
    return ((void *)0);
}

canonical_original_code:
void *get_proposer_pings(void *arg) {
    arg = arg;
    struct timeval current_time;
    while(1) {
        int valid = udp_read_next_message(for_oracle);
        if (valid < 0) {
            printf("Dropping invalid proposer message\n");
            continue;
        }
        struct paxos_msg_t *msg = (paxos_msg*) &for_oracle->recv_buffer;
        switch(msg->type) {
            case alive_ping: {
                struct alive_ping_msg_t *ap = (alive_ping_msg *) msg->data;
                gettimeofday(&current_time, ((void *)0));
                update_oracle_state(ap->proposer_id, ap->sequence_number, &current_time);
            }
            break;
            default: {
                printf("Unknow msg type %d received by oracle\n", msg->type);
            }
        }
    }
    return ((void *)0);
}

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

variable_types: valid int

variable_types: struct paxos_msg_t { unsigned long data_size; enum pax_msg_code_e { prepare_reqs=1, prepare_acks=2, accept_reqs=4, accept_acks=8, repeat_reqs=16, submit=32, leader_announce=64, alive_ping=65 } type; char data[]; } *

user_defined_types: struct alive_ping_msg_t { short proposer_id; unsigned long sequence_number; }


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
to_proposers = udp_sendbuf_new(1LL, "239.4.0.1", 6005LL);
if ( to_proposers )
{
for_oracle = udp_receiver_blocking_new(6006LL);
if ( for_oracle )
{
oracle_lock();
pthread_create(&announce_thread, 0LL, (void *(*)(void *))broadcast_current_leader, 0LL);
pthread_create(&get_pings_thread, 0LL, (void *(*)(void *))get_proposer_pings, 0LL);
pthread_create(&user_input_thread, 0LL, (void *(*)(void *))get_user_input, 0LL);
oracle_unlock();
while ( 1 )
sleep(0xAu);
}
puts("Error creating proposers->oracle network receiver");
}
else
{
puts("Error creating oracle->proposers network sender");
}
return -1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
to_proposers = udp_sendbuf_new(1LL, "239.4.0.1", 6005LL);
if ( to_proposers )
{
for_oracle = udp_receiver_blocking_new(6006LL);
if ( for_oracle )
{
func0();
pthread_create(&announce_thread, 0LL, (void *(*)(void *))broadcast_current_leader, 0LL);
pthread_create(&get_pings_thread, 0LL, (void *(*)(void *))get_proposer_pings, 0LL);
pthread_create(&user_input_thread, 0LL, (void *(*)(void *))get_user_input, 0LL);
func1();
while ( 1 )
sleep(0xAu);
}
puts("Error creating proposers->oracle network receiver");
}
else
{
puts("Error creating oracle->proposers network sender");
}
return -1;
}

original_code:
int main (int argc, char const *argv[])
{
    argc = argc;
    argv = argv;
    to_proposers = udp_sendbuf_new(1, "239.4.0.1", 6005);
    if(to_proposers == ((void *)0)) {
        printf("Error creating oracle->proposers network sender\n");
        return -1;
    }
    for_oracle = udp_receiver_blocking_new(6006);
    if (for_oracle == ((void *)0)) {
        printf("Error creating proposers->oracle network receiver\n");
        return -1;
    }
    init_oracle_state();
    oracle_lock();
    pthread_create(&announce_thread, ((void *)0), broadcast_current_leader, ((void *)0));
    pthread_create(&get_pings_thread, ((void *)0), get_proposer_pings, ((void *)0));
    pthread_create(&user_input_thread, ((void *)0), get_user_input, ((void *)0));
    oracle_unlock();
    while(1) {
        sleep(10);
    }
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    argc = argc;
    argv = argv;
    to_proposers = udp_sendbuf_new(1, "239.4.0.1", 6005);
    if(to_proposers == ((void *)0)) {
        printf("Error creating oracle->proposers network sender\n");
        return -1;
    }
    for_oracle = udp_receiver_blocking_new(6006);
    if (for_oracle == ((void *)0)) {
        printf("Error creating proposers->oracle network receiver\n");
        return -1;
    }
    init_oracle_state();
    oracle_lock();
    pthread_create(&announce_thread, ((void *)0), broadcast_current_leader, ((void *)0));
    pthread_create(&get_pings_thread, ((void *)0), get_proposer_pings, ((void *)0));
    pthread_create(&user_input_thread, ((void *)0), get_user_input, ((void *)0));
    oracle_unlock();
    while(1) {
        sleep(10);
    }
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]


original_code:
paxos_submit_handle * pax_submit_handle_init() {
    paxos_submit_handle * psh = malloc(sizeof(paxos_submit_handle));
    if(psh == ((void *)0)) {
        return ((void *)0);
    }
    psh->sendbuf = udp_sendbuf_new(1, "10.78.36.211", 6004);
    if(psh->sendbuf == ((void *)0)) {
        return ((void *)0);
    }
    return psh;
}

canonical_original_code:
struct paxos_submit_handle_t *pax_submit_handle_init() {
    struct paxos_submit_handle_t *psh = malloc(sizeof(paxos_submit_handle));
    if(psh == ((void *)0)) {
        return ((void *)0);
    }
    psh->sendbuf = udp_sendbuf_new(1, "10.78.36.211", 6004);
    if(psh->sendbuf == ((void *)0)) {
        return ((void *)0);
    }
    return psh;
}

variable_types: struct paxos_submit_handle_t { void *sendbuf; } *


decompiled_code:
__int64 __fastcall pax_submit_nonblock(_QWORD *a1, __int64 a2, __int64 a3)
{
__int64 v4; // rbp

v4 = *a1;
sendbuf_clear(*a1, 32LL, 0LL);
sendbuf_add_submit_val(v4, a2, a3);
sendbuf_flush(v4);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(_QWORD *a1, __int64 a2, __int64 a3)
{
__int64 v4; // rbp

v4 = *a1;
sendbuf_clear(*a1, 32LL, 0LL);
sendbuf_add_submit_val(v4, a2, a3);
sendbuf_flush(v4);
return 0LL;
}

original_code:
int pax_submit_nonblock(paxos_submit_handle * h, char * value, size_t val_size) {
    udp_send_buffer* sb = (udp_send_buffer*)h->sendbuf;
    sendbuf_clear(sb, submit, 0);
    sendbuf_add_submit_val(sb, value, val_size);
    sendbuf_flush(sb);
    return 0;
}

canonical_original_code:
int pax_submit_nonblock(struct paxos_submit_handle_t *h, char *value, unsigned long val_size) {
    struct udp_send_buffer_t *sb = (udp_send_buffer*)h->sendbuf;
    sendbuf_clear(sb, submit, 0);
    sendbuf_add_submit_val(sb, value, val_size);
    sendbuf_flush(sb);
    return 0;
}

variable_types:
value
char *

user_defined_types: struct paxos_submit_handle_t { void *sendbuf; }


decompiled_code:
FILE *open_debug_file()
{
FILE *result; // rax

result = debug_f;
if ( !debug_f )
{
result = fopen("malloc_debug_trace.txt", "w");
debug_f = result;
}
return result;
}

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

result = debug_f;
if ( !debug_f )
{
result = fopen("malloc_debug_trace.txt", "w");
debug_f = result;
}
return result;
}

original_code:
int open_debug_file() {
    if (debug_f == ((void *)0)) {
        debug_f = fopen("malloc_debug_trace.txt", "w");
    }
    return (int)debug_f;
}


decompiled_code:
__int64 close_debug_file()
{
if ( debug_f )
fclose(debug_f);
return 0LL;
}

canonical_decompiled_code:
__int64 func1()
{
if ( debug_f )
fclose(debug_f);
return 0LL;
}

original_code:
int close_debug_file() {
    if (debug_f != ((void *)0)) {
        fclose(debug_f);
    }
    return 0;
}


decompiled_code:
const void *__fastcall paxos_debug_malloc(size_t a1, const char *a2, unsigned int a3)
{
unsigned int v4; // ebx
void *v5; // rax
FILE *v6; // rdi
const void *v7; // r12

v4 = a1;
v5 = malloc(a1);
if ( !v5 )
{
printf("Malloc failed in %s line %d\n", a2, a3);
exit(1);
}
v6 = debug_f;
v7 = v5;
if ( !debug_f )
{
open_debug_file();
v6 = debug_f;
}
fprintf(v6, "malloc %p %s %d %d\n", v7, a2, a3, v4);
return v7;
}

canonical_decompiled_code:
const void *__fastcall func2(size_t a1, const char *a2, unsigned int a3)
{
unsigned int v4; // ebx
void *v5; // rax
FILE *v6; // rdi
const void *v7; // r12

v4 = a1;
v5 = malloc(a1);
if ( !v5 )
{
printf("Malloc failed in %s line %d\n", a2, a3);
exit(1);
}
v6 = debug_f;
v7 = v5;
if ( !debug_f )
{
func0();
v6 = debug_f;
}
fprintf(v6, "malloc %p %s %d %d\n", v7, a2, a3, v4);
return v7;
}

original_code:
void * paxos_debug_malloc(size_t size, char* file, int line) {
    void * p = malloc(size);
    if (p == ((void *)0)) {
        printf("Malloc failed in %s line %d\n", file, line);
        exit(1);
    }
    if (debug_f == ((void *)0)) {
        open_debug_file();
    }
    fprintf(debug_f, "malloc %p %s %d %d\n",p, file, line, (int)size);
    return p;
}

canonical_original_code:
void *paxos_debug_malloc(unsigned long size, char *file, int line) {
    void *p = malloc(size);
    if (p == ((void *)0)) {
        printf("Malloc failed in %s line %d\n", file, line);
        exit(1);
    }
    if (debug_f == ((void *)0)) {
        open_debug_file();
    }
    fprintf(debug_f, "malloc %p %s %d %d\n",p, file, line, (int)size);
    return p;
}

variable_types:
size
unsigned long

variable_types:
file
char *

variable_types: line int

variable_types:
p
void *


decompiled_code:
void __fastcall paxos_debug_free(void *ptr, const char *a2, unsigned int a3)
{
fprintf(debug_f, "free %p %s %d %d\n", ptr, a2, a3, 0xFFFFFFFFLL);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func3(void *ptr, const char *a2, unsigned int a3)
{
fprintf(debug_f, "free %p %s %d %d\n", ptr, a2, a3, 0xFFFFFFFFLL);
free(ptr);
}

original_code:
void paxos_debug_free(void* p, char* file, int line) {
    fprintf(debug_f, "free   %p %s %d %d\n",p, file, line, -1 );
    free(p);
}

canonical_original_code:
void paxos_debug_free(void *p, char *file, int line) {
    fprintf(debug_f, "free   %p %s %d %d\n",p, file, line, -1 );
    free(p);
}


decompiled_code:
void *__fastcall paxos_normal_malloc(size_t a1)
{
void *result; // rax

result = malloc(a1);
if ( !result )
{
puts("Malloc failed, out of memory!!!");
exit(1);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func4(size_t a1)
{
void *result; // rax

result = malloc(a1);
if ( !result )
{
puts("Malloc failed, out of memory!!!");
exit(1);
}
return result;
}

original_code:
void* paxos_normal_malloc(size_t size) {
    void * p = malloc(size);
    if (p == ((void *)0)) {
        printf("Malloc failed, out of memory!!!\n");
        exit(1);
    }
    return p;
}

canonical_original_code:
void *paxos_normal_malloc(unsigned long size) {
    void *p = malloc(size);
    if (p == ((void *)0)) {
        printf("Malloc failed, out of memory!!!\n");
        exit(1);
    }
    return p;
}


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

result = 33LL;
if ( (unsigned __int8)(a1 - 33) <= 0x5Du )
result = a1;
return result;
}

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

result = 33LL;
if ( (unsigned __int8)(a1 - 33) <= 0x5Du )
result = a1;
return result;
}

original_code:
static char as_char(char c) {
    if(c < 33 || c > 126) {
        return '!';
    } else {
        return c;
    }
}

variable_types: c char

return_type: char


decompiled_code:
int __fastcall my_deliver_fun(char *a1, unsigned int a2, unsigned int a3, unsigned int a4, unsigned int a5)
{
char v6; // al
__int64 v7; // rdx
__int64 v8; // rcx

printf("Paxos instance %lu closed by ballot %u\n", a3, a4);
printf("Value (by proposer:%d, size: %d) ->", a5, a2);
as_char(a1[2]);
as_char(a1[1]);
v6 = as_char(*a1);
return printf("[%c][%c][%c][...]\n", (unsigned int)v6, v7, v8);
}

canonical_decompiled_code:
int __fastcall func1(char *a1, unsigned int a2, unsigned int a3, unsigned int a4, unsigned int a5)
{
char v6; // al
__int64 v7; // rdx
__int64 v8; // rcx

printf("Paxos instance %lu closed by ballot %u\n", a3, a4);
printf("Value (by proposer:%d, size: %d) ->", a5, a2);
func0(a1[2]);
func0(a1[1]);
v6 = func0(*a1);
return printf("[%c][%c][%c][...]\n", (unsigned int)v6, v7, v8);
}

original_code:
void my_deliver_fun(char* value, size_t value_size, iid_t iid, ballot_t ballot, int proposer) {
    printf("Paxos instance %lu closed by ballot %u\n", iid, ballot);
    printf("Value (by proposer:%d, size: %d) ->", proposer, (int)value_size);
    printf("[%c][%c][%c][...]\n", as_char(value[0]), as_char(value[1]), as_char(value[2]));
}

canonical_original_code:
void my_deliver_fun(char *value, unsigned long value_size, unsigned int iid, unsigned int ballot, int proposer) {
    printf("Paxos instance %lu closed by ballot %u\n", iid, ballot);
    printf("Value (by proposer:%d, size: %d) ->", proposer, (int)value_size);
    printf("[%c][%c][%c][...]\n", as_char(value[0]), as_char(value[1]), as_char(value[2]));
}

variable_types:
iid
unsigned int

variable_types:
ballot
unsigned int

variable_types: proposer int


decompiled_code:
void __fastcall __noreturn handle_cltr_c(unsigned int a1)
{
printf("Caught signal %d\n", a1);
exit(0);
}

canonical_decompiled_code:
void __fastcall __noreturn func2(unsigned int a1)
{
printf("Caught signal %d\n", a1);
exit(0);
}

original_code:
void handle_cltr_c (int sig) {
 printf("Caught signal %d\n", sig);
    exit(0);
}

canonical_original_code:
void handle_cltr_c(int sig) {
 printf("Caught signal %d\n", sig);
    exit(0);
}

variable_types: sig int


decompiled_code:
__int64 my_custom_init()
{
puts(">>> This is invoked by another thread in libevent");
puts(">>> After the normal learner initialization");
return 0LL;
}

canonical_decompiled_code:
__int64 func3()
{
puts(">>> This is invoked by another thread in libevent");
puts(">>> After the normal learner initialization");
return 0LL;
}

original_code:
int my_custom_init() {
    printf(">>> This is invoked by another thread in libevent\n");
    printf(">>> After the normal learner initialization\n");
    return 0;
}


decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
signal(2, (__sighandler_t)handle_cltr_c);
if ( !(unsigned int)learner_init(my_deliver_fun, my_custom_init) )
{
while ( 1 )
sleep(1u);
}
puts("Could not start the learner!");
exit(1);
}

original_code:
int main (int argc, char const *argv[]) {
    signal(2, handle_cltr_c);
    if (learner_init(my_deliver_fun, my_custom_init) != 0) {
        printf("Could not start the learner!\n");
        exit(1);
    }
    while(1) {
        sleep(1);
    }
    argc = argc;
    argv = argv;
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
    signal(2, handle_cltr_c);
    if (learner_init(my_deliver_fun, my_custom_init) != 0) {
        printf("Could not start the learner!\n");
        exit(1);
    }
    while(1) {
        sleep(1);
    }
    argc = argc;
    argv = argv;
    return 0;
}


decompiled_code:
int stablestorage_do_recovery()
{
int result; // eax

result = puts("Acceptor in recovery mode");
do_recovery = 1;
return result;
}

canonical_decompiled_code:
int func0()
{
int result; // eax

result = puts("Acceptor in recovery mode");
do_recovery = 1;
return result;
}

original_code:
void stablestorage_do_recovery() {
    printf("Acceptor in recovery mode\n");
    do_recovery = 1;
}


decompiled_code:
__int64 __fastcall bdb_init_tx_handle(unsigned int a1)
{
unsigned int v1; // eax
unsigned int v2; // eax
unsigned int v3; // eax
__int64 result; // rax
const char *v5; // rax
const char *v6; // rax
const char *v7; // rax
const char *v8; // rax

v1 = db_env_create(&dbenv, 0LL);
if ( v1 )
{
v7 = (const char *)db_strerror(v1);
printf("DB_ENV creation failed: %s\n", v7);
result = 0xFFFFFFFFLL;
}
else
{
v2 = (*(__int64 (__fastcall **)(__int64, _QWORD, __int64))(dbenv + 1728))(dbenv, a1, 1LL);
if ( v2 )
{
v6 = (const char *)db_strerror(v2);
printf("DB_ENV set_flags failed: %s\n", v6);
result = 0xFFFFFFFFLL;
}
else
{
(*(void (__fastcall **)(__int64, struct _IO_FILE *))(dbenv + 1696))(dbenv, stdout);
v3 = (*(__int64 (__fastcall **)(__int64, _QWORD, __int64, __int64))(dbenv + 1632))(dbenv, 0LL, 0x400000LL, 1LL);
if ( v3 )
{
v8 = (const char *)db_strerror(v3);
printf("DB_ENV set_cachesize failed: %s\n", v8);
result = 0xFFFFFFFFLL;
}
else
{
result = (*(__int64 (__fastcall **)(__int64, char *, __int64, _QWORD))(dbenv + 1328))(
dbenv,
db_env_path,
75267LL,
0LL);
if ( (_DWORD)result )
{
v5 = (const char *)db_strerror((unsigned int)result);
printf("DB_ENV open failed: %s\n", v5);
result = 0xFFFFFFFFLL;
}
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1)
{
unsigned int v1; // eax
unsigned int v2; // eax
unsigned int v3; // eax
__int64 result; // rax
const char *v5; // rax
const char *v6; // rax
const char *v7; // rax
const char *v8; // rax

v1 = db_env_create(&dbenv, 0LL);
if ( v1 )
{
v7 = (const char *)db_strerror(v1);
printf("DB_ENV creation failed: %s\n", v7);
result = 0xFFFFFFFFLL;
}
else
{
v2 = (*(__int64 (__fastcall **)(__int64, _QWORD, __int64))(dbenv + 1728))(dbenv, a1, 1LL);
if ( v2 )
{
v6 = (const char *)db_strerror(v2);
printf("DB_ENV set_flags failed: %s\n", v6);
result = 0xFFFFFFFFLL;
}
else
{
(*(void (__fastcall **)(__int64, struct _IO_FILE *))(dbenv + 1696))(dbenv, stdout);
v3 = (*(__int64 (__fastcall **)(__int64, _QWORD, __int64, __int64))(dbenv + 1632))(dbenv, 0LL, 0x400000LL, 1LL);
if ( v3 )
{
v8 = (const char *)db_strerror(v3);
printf("DB_ENV set_cachesize failed: %s\n", v8);
result = 0xFFFFFFFFLL;
}
else
{
result = (*(__int64 (__fastcall **)(__int64, char *, __int64, _QWORD))(dbenv + 1328))(
dbenv,
db_env_path,
75267LL,
0LL);
if ( (_DWORD)result )
{
v5 = (const char *)db_strerror((unsigned int)result);
printf("DB_ENV open failed: %s\n", v5);
result = 0xFFFFFFFFLL;
}
}
}
}
return result;
}

original_code:
int bdb_init_tx_handle(int tx_mode) {
    int result;
    result = db_env_create(&dbenv, 0);
    if (result != 0) {
        printf("DB_ENV creation failed: %s\n", db_strerror(result));
        return -1;
    }
    result = dbenv->set_flags(dbenv, tx_mode, 1);
    if (result != 0) {
        printf("DB_ENV set_flags failed: %s\n", db_strerror(result));
        return -1;
    }
    dbenv->set_errfile(dbenv, stdout);
    result = dbenv->set_cachesize(dbenv, (0), (4*1024*1024), 1);
    if (result != 0) {
        printf("DB_ENV set_cachesize failed: %s\n", db_strerror(result));
        return -1;
    }
    int flags;
    flags =
        0x00000001 |
        0x00000002 |
        0x00000200 |
        0x00002000 |
        0x00010000 |
        0x00000400;
    result = dbenv->open(dbenv,
        db_env_path,
        flags,
        0);
    if (result != 0) {
        printf("DB_ENV open failed: %s\n", db_strerror(result));
        return -1;
    }
    return 0;
}

variable_types: result int

variable_types: flags int


decompiled_code:
__int64 __fastcall bdb_init_db(__int64 a1)
{
unsigned int v1; // eax
unsigned int v2; // eax
__int64 result; // rax
const char *v4; // rax
const char *v5; // rax
const char *v6; // rax

v1 = db_create(&dbp, dbenv, 0LL);
if ( v1 )
{
v4 = (const char *)db_strerror(v1);
printf("db_create failed: %s\n", v4);
result = 0xFFFFFFFFLL;
}
else
{
v2 = (*(__int64 (__fastcall **)(__int64, _QWORD, __int64, __int64))(dbp + 1096))(dbp, 0LL, 0x400000LL, 1LL);
if ( v2 )
{
v5 = (const char *)db_strerror(v2);
printf("DBP set_cachesize failed: %s\n", v5);
result = 0xFFFFFFFFLL;
}
else
{
result = (*(__int64 (__fastcall **)(__int64, __int64, __int64, _QWORD, __int64, __int64, _QWORD))(dbp + 1008))(
dbp,
txn,
a1,
0LL,
3LL,
1LL,
0LL);
if ( (_DWORD)result )
{
v6 = (const char *)db_strerror((unsigned int)result);
printf("DB open failed: %s\n", v6);
result = 0xFFFFFFFFLL;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
unsigned int v1; // eax
unsigned int v2; // eax
__int64 result; // rax
const char *v4; // rax
const char *v5; // rax
const char *v6; // rax

v1 = db_create(&dbp, dbenv, 0LL);
if ( v1 )
{
v4 = (const char *)db_strerror(v1);
printf("db_create failed: %s\n", v4);
result = 0xFFFFFFFFLL;
}
else
{
v2 = (*(__int64 (__fastcall **)(__int64, _QWORD, __int64, __int64))(dbp + 1096))(dbp, 0LL, 0x400000LL, 1LL);
if ( v2 )
{
v5 = (const char *)db_strerror(v2);
printf("DBP set_cachesize failed: %s\n", v5);
result = 0xFFFFFFFFLL;
}
else
{
result = (*(__int64 (__fastcall **)(__int64, __int64, __int64, _QWORD, __int64, __int64, _QWORD))(dbp + 1008))(
dbp,
txn,
a1,
0LL,
3LL,
1LL,
0LL);
if ( (_DWORD)result )
{
v6 = (const char *)db_strerror((unsigned int)result);
printf("DB open failed: %s\n", v6);
result = 0xFFFFFFFFLL;
}
}
}
return result;
}

original_code:
int bdb_init_db(char * db_path) {
    int result;
    result = db_create(&dbp, dbenv, 0);
    if (result != 0) {
        printf("db_create failed: %s\n", db_strerror(result));
        return -1;
    }
    if(0 == 0 || 0 == 20) {
        result = dbp->set_cachesize(dbp, (0), (4*1024*1024), 1);
        if (result != 0) {
            printf("DBP set_cachesize failed: %s\n", db_strerror(result));
            return -1;
        }
    }
    int flags =
        0x00000001;
    stablestorage_tx_begin();
    result = dbp->open(dbp,
        txn,
        db_path,
        ((void *)0),
        DB_RECNO,
        flags,
        0);
    stablestorage_tx_end();
    if(result != 0) {
        printf("DB open failed: %s\n", db_strerror(result));
        return -1;
    }
    return 0;
}

canonical_original_code:
int bdb_init_db(char *db_path) {
    int result;
    result = db_create(&dbp, dbenv, 0);
    if (result != 0) {
        printf("db_create failed: %s\n", db_strerror(result));
        return -1;
    }
    if(0 == 0 || 0 == 20) {
        result = dbp->set_cachesize(dbp, (0), (4*1024*1024), 1);
        if (result != 0) {
            printf("DBP set_cachesize failed: %s\n", db_strerror(result));
            return -1;
        }
    }
    int flags = 0x00000001;
    stablestorage_tx_begin();
    result = dbp->open(dbp,
        txn,
        db_path,
        ((void *)0),
        DB_RECNO,
        flags,
        0);
    stablestorage_tx_end();
    if(result != 0) {
        printf("DB open failed: %s\n", db_strerror(result));
        return -1;
    }
    return 0;
}


decompiled_code:
__int64 __fastcall stablestorage_init(unsigned int a1)
{
int v1; // er12
int v2; // eax
__int64 result; // rax
int *v4; // rax
char *v5; // rax
int *v6; // rax
char *v7; // rax
struct stat v8; // [rsp+0h] [rbp-2F8h] BYREF
char s[616]; // [rsp+90h] [rbp-268h] BYREF

sprintf(db_env_path, "/tmp/acceptor_%d", a1);
sprintf(db_filename, "acc_db_%d.bdb", a1);
sprintf(db_file_path, "%s/%s", db_env_path, db_filename);
v1 = stat(db_env_path, &v8);
v2 = stat(db_file_path, &v8);
if ( do_recovery )
{
if ( v2 | v1 )
{
puts("Error: Acceptor recovery failed!");
printf("The file:%s does not exist\n", db_file_path);
return 0xFFFFFFFFLL;
}
}
else if ( v1 )
{
if ( mkdir(db_env_path, 0x1C0u) )
{
v4 = _errno_location();
v5 = strerror(*v4);
printf("Failed to create env dir %s: %s\n", db_env_path, v5);
return 0xFFFFFFFFLL;
}
}
else
{
sprintf(s, "rm -r %s", db_env_path);
if ( system(s) || mkdir(db_env_path, 0x1C0u) )
{
v6 = _errno_location();
v7 = strerror(*v6);
printf("Failed to recreate empty env dir %s: %s\n", db_env_path, v7);
}
}
printf("Durability mode is: ");
puts("no durability!");
result = bdb_init_db((__int64)db_file_path);
if ( (_DWORD)result )
{
puts("Failed to open DB file");
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int a1)
{
int v1; // er12
int v2; // eax
__int64 result; // rax
int *v4; // rax
char *v5; // rax
int *v6; // rax
char *v7; // rax
struct stat v8; // [rsp+0h] [rbp-2F8h] BYREF
char s[616]; // [rsp+90h] [rbp-268h] BYREF

sprintf(db_env_path, "/tmp/acceptor_%d", a1);
sprintf(db_filename, "acc_db_%d.bdb", a1);
sprintf(db_file_path, "%s/%s", db_env_path, db_filename);
v1 = stat(db_env_path, &v8);
v2 = stat(db_file_path, &v8);
if ( do_recovery )
{
if ( v2 | v1 )
{
puts("Error: Acceptor recovery failed!");
printf("The file:%s does not exist\n", db_file_path);
return 0xFFFFFFFFLL;
}
}
else if ( v1 )
{
if ( mkdir(db_env_path, 0x1C0u) )
{
v4 = _errno_location();
v5 = strerror(*v4);
printf("Failed to create env dir %s: %s\n", db_env_path, v5);
return 0xFFFFFFFFLL;
}
}
else
{
sprintf(s, "rm -r %s", db_env_path);
if ( system(s) || mkdir(db_env_path, 0x1C0u) )
{
v6 = _errno_location();
v7 = strerror(*v6);
printf("Failed to recreate empty env dir %s: %s\n", db_env_path, v7);
}
}
printf("Durability mode is: ");
puts("no durability!");
result = func2((__int64)db_file_path);
if ( (_DWORD)result )
{
puts("Failed to open DB file");
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int stablestorage_init(int acceptor_id) {
    sprintf(db_env_path, "/tmp/acceptor_%d", acceptor_id);
    sprintf(db_filename, "acc_db_%d.bdb", acceptor_id);
    sprintf(db_file_path, "%s/%s", db_env_path, db_filename);
    if(0 >= 1) { printf("[%s] ", __func__) ; printf ("Opening db file %s/%s\n", db_env_path, db_filename) ;};
    struct stat sb;
    int dir_exists = (stat(db_env_path, &sb) == 0);
    int db_exists = (stat(db_file_path, &sb) == 0);
    if(do_recovery && (!dir_exists || !db_exists)) {
        printf("Error: Acceptor recovery failed!\n");
        printf("The file:%s does not exist\n", db_file_path);
        return -1;
    }
    if(!dir_exists && (mkdir(db_env_path, (0400|0200|0100)) != 0)) {
        printf("Failed to create env dir %s: %s\n", db_env_path, strerror((*__errno_location ())));
        return -1;
    }
    if(!do_recovery && dir_exists) {
        char rm_command[600];
        sprintf(rm_command, "rm -r %s", db_env_path);
        if((system(rm_command) != 0) ||
            (mkdir(db_env_path, (0400|0200|0100)) != 0)) {
            printf("Failed to recreate empty env dir %s: %s\n", db_env_path, strerror((*__errno_location ())));
        }
    }
    int ret = 0;
    char * db_file = db_filename;
    printf("Durability mode is: ");
    switch(0) {
        case 0: {
            printf("no durability!\n");
            db_file = db_file_path;
        }
        break;
        case 10: {
            printf("transactional, no durability!\n");
            ret = bdb_init_tx_handle(0x00000008);
        }
        break;
        case 11: {
            printf("transactional, DB_TXN_NOSYNC\n");
            ret = bdb_init_tx_handle(0x00000001);
        }
        break;
        case 12: {
            printf("transactional, DB_TXN_WRITE_NOSYNC\n");
            ret = bdb_init_tx_handle(0x00000020);
        }
        break;
        case 13: {
            printf("transactional, durable\n");
            ret = bdb_init_tx_handle(0);
        }
        break;
        case 20: {
            printf("manual db flush\n");
            db_file = db_file_path;
        }
        break;
        default: {
            printf("Unknow durability mode %d!\n", 0);
            return -1;
        }
    }
    if(ret != 0) {
        printf("Failed to open DB handle\n");
    }
    if(bdb_init_db(db_file) != 0) {
        printf("Failed to open DB file\n");
        return -1;
    }
    return 0;
}

canonical_original_code:
int stablestorage_init(int acceptor_id) {
    sprintf(db_env_path, "/tmp/acceptor_%d", acceptor_id);
    sprintf(db_filename, "acc_db_%d.bdb", acceptor_id);
    sprintf(db_file_path, "%s/%s", db_env_path, db_filename);
    if(0 >= 1) { printf("[%s] ", __func__) ; printf ("Opening db file %s/%s\n", db_env_path, db_filename) ;};
    struct stat sb;
    int dir_exists = (stat(db_env_path, &sb) == 0);
    int db_exists = (stat(db_file_path, &sb) == 0);
    if(do_recovery && (!dir_exists || !db_exists)) {
        printf("Error: Acceptor recovery failed!\n");
        printf("The file:%s does not exist\n", db_file_path);
        return -1;
    }
    if(!dir_exists && (mkdir(db_env_path, (0400|0200|0100)) != 0)) {
        printf("Failed to create env dir %s: %s\n", db_env_path, strerror((*__errno_location ())));
        return -1;
    }
    if(!do_recovery && dir_exists) {
        char rm_command[600];
        sprintf(rm_command, "rm -r %s", db_env_path);
        if((system(rm_command) != 0) ||
            (mkdir(db_env_path, (0400|0200|0100)) != 0)) {
            printf("Failed to recreate empty env dir %s: %s\n", db_env_path, strerror((*__errno_location ())));
        }
    }
    int ret = 0;
    char *db_file = db_filename;
    printf("Durability mode is: ");
    switch(0) {
        case 0: {
            printf("no durability!\n");
            db_file = db_file_path;
        }
        break;
        case 10: {
            printf("transactional, no durability!\n");
            ret = bdb_init_tx_handle(0x00000008);
        }
        break;
        case 11: {
            printf("transactional, DB_TXN_NOSYNC\n");
            ret = bdb_init_tx_handle(0x00000001);
        }
        break;
        case 12: {
            printf("transactional, DB_TXN_WRITE_NOSYNC\n");
            ret = bdb_init_tx_handle(0x00000020);
        }
        break;
        case 13: {
            printf("transactional, durable\n");
            ret = bdb_init_tx_handle(0);
        }
        break;
        case 20: {
            printf("manual db flush\n");
            db_file = db_file_path;
        }
        break;
        default: {
            printf("Unknow durability mode %d!\n", 0);
            return -1;
        }
    }
    if(ret != 0) {
        printf("Failed to open DB handle\n");
    }
    if(bdb_init_db(db_file) != 0) {
        printf("Failed to open DB file\n");
        return -1;
    }
    return 0;
}

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec { long tv_sec; long tv_nsec; } st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }

variable_types: ret int

user_defined_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }


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

result = (*(__int64 (__fastcall **)(__int64, _QWORD))(dbp + 568))(dbp, 0LL);
if ( (_DWORD)result )
{
puts("DB_ENV close failed");
result = 0xFFFFFFFFLL;
}
return result;
}

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

result = (*(__int64 (__fastcall **)(__int64, _QWORD))(dbp + 568))(dbp, 0LL);
if ( (_DWORD)result )
{
puts("DB_ENV close failed");
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int stablestorage_shutdown() {
    int result = 0;
    if(dbp->close(dbp, 0) != 0) {
        printf("DB_ENV close failed\n");
        result = -1;
    }
    switch(0) {
        case 0:
        case 20:
        break;
        case 10:
        case 11:
        case 12:
        case 13: {
            if(dbenv->close(dbenv, 0) != 0) {
                printf("DB close failed\n");
                result = -1;
            }
        }
        break;
        default: {
            printf("Unknow durability mode %d!\n", 0);
            return -1;
        }
    }
    if(0 >= 1) { printf("[%s] ", __func__) ; printf ("DB close completed\n") ;};
    return result;
}


decompiled_code:
void stablestorage_tx_begin()
{
;
}

canonical_decompiled_code:
void func5()
{
;
}

original_code:
void
stablestorage_tx_begin() {
    if(0 == 0 || 0 == 20) {
        return;
    }
    int result;
    result = dbenv->txn_begin(dbenv, ((void *)0), &txn, 0);
    ((void) sizeof ((result == 0) ? 1 : 0), __extension__ ({ if (result == 0) ; else __assert_fail ("result == 0", "acceptor_stable_storage.c", 300, __extension__ __PRETTY_FUNCTION__); }));
}


decompiled_code:
void stablestorage_tx_end()
{
;
}

canonical_decompiled_code:
void func6()
{
;
}

original_code:
void
stablestorage_tx_end() {
    int result;
    if(0 == 0) {
        return;
    }
    if (0 == 20) {
        result = dbp->sync(dbp, 0);
        ((void) sizeof ((result == 0) ? 1 : 0), __extension__ ({ if (result == 0) ; else __assert_fail ("result == 0", "acceptor_stable_storage.c", 313, __extension__ __PRETTY_FUNCTION__); }));
        return;
    }
    result = txn->commit(txn, 0);
    ((void) sizeof ((result == 0) ? 1 : 0), __extension__ ({ if (result == 0) ; else __assert_fail ("result == 0", "acceptor_stable_storage.c", 320, __extension__ __PRETTY_FUNCTION__); }));
}