decompiled_code:
__int64 __fastcall bts_open(__int64 a1, unsigned int *a2)
{
bool v2; // zf
_QWORD *v3; // r13
unsigned int *v6; // r12
int v7; // er14
const char *v8; // rax
int v9; // eax
_QWORD *v10; // rdi
__int64 v11; // rsi
__int64 v12; // rdx
const char *v13; // rax
int v14; // er13
__int64 v15; // rdi
__int64 v16; // rdx
int v17; // er14
__int64 result; // rax
int v19; // edi
__int64 v20[6]; // [rsp+8h] [rbp-30h] BYREF

v2 = *((_QWORD *)a2 + 2) == 0LL;
v3 = *(_QWORD **)(a1 + 64);
v20[0] = 0LL;
if ( !v2 || *v3 != a1 )
bts_open_cold();
v6 = (unsigned int *)calloc(1uLL, 0x10uLL);
if ( !v6 )
return 0xFFFFFFFFLL;
v20[0] = ndn_charbuf_create();
if ( !v20[0] )
goto LABEL_11;
v7 = ndn_charbuf_append_charbuf(v20[0], v3[1]);
if ( (int)(ndn_charbuf_putf(v20[0], "/%u", *a2) | v7) < 0 )
goto LABEL_11;
v8 = (const char *)ndn_charbuf_as_string(v20[0]);
v9 = open(v8, 66, 416LL);
v6[2] = v9;
if ( v9 < 0 )
goto LABEL_11;
if ( *a2 <= *(_DWORD *)(a1 + 56) )
goto LABEL_10;
v10 = (_QWORD *)v20[0];
v11 = v3[1];
*(_DWORD *)(a1 + 56) = *a2;
*v10 = 0LL;
ndn_charbuf_append_charbuf(v10, v11);
ndn_charbuf_putf(v20[0], "/maxnodeid", v12);
v13 = (const char *)ndn_charbuf_as_string(v20[0]);
v14 = open(v13, 578, 416LL);
if ( v14 < 0 )
goto LABEL_11;
v15 = v20[0];
v16 = *a2;
*(_QWORD *)v20[0] = 0LL;
ndn_charbuf_putf(v15, "%u", v16);
v17 = write(v14, *(const void **)(v20[0] + 16), *(_QWORD *)v20[0]);
close(v14);
if ( v17 == *(_QWORD *)v20[0] )
{
LABEL_10:
ndn_charbuf_destroy(v20);
++*(_DWORD *)(a1 + 60);
result = v6[2];
*(_QWORD *)v6 = a2;
*((_QWORD *)a2 + 2) = v6;
}
else
{
LABEL_11:
ndn_charbuf_destroy(v20);
v19 = v6[2];
if ( v19 >= 0 )
close(v19);
free(v6);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, unsigned int *a2)
{
bool v2; // zf
_QWORD *v3; // r13
unsigned int *v6; // r12
int v7; // er14
const char *v8; // rax
int v9; // eax
_QWORD *v10; // rdi
__int64 v11; // rsi
__int64 v12; // rdx
const char *v13; // rax
int v14; // er13
__int64 v15; // rdi
__int64 v16; // rdx
int v17; // er14
__int64 result; // rax
int v19; // edi
__int64 v20[6]; // [rsp+8h] [rbp-30h] BYREF

v2 = *((_QWORD *)a2 + 2) == 0LL;
v3 = *(_QWORD **)(a1 + 64);
v20[0] = 0LL;
if ( !v2 || *v3 != a1 )
func8();
v6 = (unsigned int *)calloc(1uLL, 0x10uLL);
if ( !v6 )
return 0xFFFFFFFFLL;
v20[0] = ndn_charbuf_create();
if ( !v20[0] )
goto LABEL_11;
v7 = ndn_charbuf_append_charbuf(v20[0], v3[1]);
if ( (int)(ndn_charbuf_putf(v20[0], "/%u", *a2) | v7) < 0 )
goto LABEL_11;
v8 = (const char *)ndn_charbuf_as_string(v20[0]);
v9 = open(v8, 66, 416LL);
v6[2] = v9;
if ( v9 < 0 )
goto LABEL_11;
if ( *a2 <= *(_DWORD *)(a1 + 56) )
goto LABEL_10;
v10 = (_QWORD *)v20[0];
v11 = v3[1];
*(_DWORD *)(a1 + 56) = *a2;
*v10 = 0LL;
ndn_charbuf_append_charbuf(v10, v11);
ndn_charbuf_putf(v20[0], "/maxnodeid", v12);
v13 = (const char *)ndn_charbuf_as_string(v20[0]);
v14 = open(v13, 578, 416LL);
if ( v14 < 0 )
goto LABEL_11;
v15 = v20[0];
v16 = *a2;
*(_QWORD *)v20[0] = 0LL;
ndn_charbuf_putf(v15, "%u", v16);
v17 = write(v14, *(const void **)(v20[0] + 16), *(_QWORD *)v20[0]);
close(v14);
if ( v17 == *(_QWORD *)v20[0] )
{
LABEL_10:
ndn_charbuf_destroy(v20);
++*(_DWORD *)(a1 + 60);
result = v6[2];
*(_QWORD *)v6 = a2;
*((_QWORD *)a2 + 2) = v6;
}
else
{
LABEL_11:
ndn_charbuf_destroy(v20);
v19 = v6[2];
if ( v19 >= 0 )
close(v19);
free(v6);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
static int
bts_open(struct ndn_btree_io *io, struct ndn_btree_node *node)
{
    struct bts_node_state *nd = ((void *)0);
    struct ndn_charbuf *temp = ((void *)0);
    struct bts_data *md = io->data;
    int res, l;
    if (node->iodata != ((void *)0) || io != md->io) abort();
    nd = calloc(1, sizeof(*nd));
    if (nd == ((void *)0))
        return(-1);
    temp = ndn_charbuf_create();
    if (temp == ((void *)0))
        goto Bail;
    res = ndn_charbuf_append_charbuf(temp, md->dirpath);
    res |= ndn_charbuf_putf(temp, "/%u", (unsigned)node->nodeid);
    if (res < 0)
        goto Bail;
    nd->fd = open(ndn_charbuf_as_string(temp),
               (02 | 0100),
               0640);
    if (nd->fd < 0)
        goto Bail;
    if (node->nodeid > io->maxnodeid) {
        io->maxnodeid = node->nodeid;
        temp->length = 0;
        ndn_charbuf_append_charbuf(temp, md->dirpath);
        ndn_charbuf_putf(temp, "/maxnodeid");
        res = open(ndn_charbuf_as_string(temp),
                   (02 | 0100 | 01000),
                   0640);
        if (res < 0)
            goto Bail;
        temp->length = 0;
        ndn_charbuf_putf(temp, "%u", (unsigned)node->nodeid);
        l = write(res, temp->buf, temp->length);
        close(res);
        if (l != temp->length)
            goto Bail;
    }
    ndn_charbuf_destroy(&temp);
    io->openfds++;
    nd->node = node;
    node->iodata = nd;
    return(nd->fd);
Bail:
    ndn_charbuf_destroy(&temp);
    if (nd->fd >= 0)
        close(nd->fd);
    free(nd);
    return(-1);
}

canonical_original_code:
static int bts_open(struct ndn_btree_io *io, struct ndn_btree_node *node)
{
    struct bts_node_state *nd = ((void *)0);
    struct ndn_charbuf *temp = ((void *)0);
    struct bts_data *md = io->data;
    int res; int l;
    if (node->iodata != ((void *)0) || io != md->io) abort();
    nd = calloc(1, sizeof(*nd));
    if (nd == ((void *)0))
        return(-1);
    temp = ndn_charbuf_create();
    if (temp == ((void *)0))
        goto Bail;
    res = ndn_charbuf_append_charbuf(temp, md->dirpath);
    res |= ndn_charbuf_putf(temp, "/%u", (unsigned int)node->nodeid);
    if (res < 0)
        goto Bail;
    nd->fd = open(ndn_charbuf_as_string(temp),
               (02 | 0100),
               0640);
    if (nd->fd < 0)
        goto Bail;
    if (node->nodeid > io->maxnodeid) {
        io->maxnodeid = node->nodeid;
        temp->length = 0;
        ndn_charbuf_append_charbuf(temp, md->dirpath);
        ndn_charbuf_putf(temp, "/maxnodeid");
        res = open(ndn_charbuf_as_string(temp),
                   (02 | 0100 | 01000),
                   0640);
        if (res < 0)
            goto Bail;
        temp->length = 0;
        ndn_charbuf_putf(temp, "%u", (unsigned int)node->nodeid);
        l = write(res, temp->buf, temp->length);
        close(res);
        if (l != temp->length)
            goto Bail;
    }
    ndn_charbuf_destroy(&temp);
    io->openfds++;
    nd->node = node;
    node->iodata = nd;
    return(nd->fd);
Bail:
    ndn_charbuf_destroy(&temp);
    if (nd->fd >= 0)
        close(nd->fd);
    free(nd);
    return(-1);
}

variable_types: struct bts_data { struct ndn_btree_io { char clue[16]; int (*btopen)(struct ndn_btree_io *, struct ndn_btree_node { unsigned int nodeid; struct ndn_charbuf { unsigned long length; unsigned long limit; unsigned char *buf; } *buf; void *iodata; unsigned int parent; unsigned int clean; unsigned int freelow; unsigned int corrupt; unsigned int activity; } *); int (*btread)(struct ndn_btree_io *, struct ndn_btree_node *, unsigned int); int (*btwrite)(struct ndn_btree_io *, struct ndn_btree_node *); int (*btclose)(struct ndn_btree_io *, struct ndn_btree_node *); int (*btdestroy)(struct ndn_btree_io **); unsigned int maxnodeid; int openfds; void *data; } *io; struct ndn_charbuf *dirpath; int lfd; } *

variable_types: l int

user_defined_types: struct bts_data { struct ndn_btree_io *io; struct ndn_charbuf *dirpath; int lfd; }


repo: danihegglin/ChronosAndroid

original_code:
static int
bts_read(struct ndn_btree_io *io, struct ndn_btree_node *node, unsigned limit)
{
    struct bts_node_state *nd = node->iodata;
    ssize_t sres;
    off_t offset;
    off_t clean = 0;
    if (nd == ((void *)0) || nd->node != node) abort();
    offset = lseek(nd->fd, 0, 2);
    if (offset == (off_t)-1)
        return(-1);
    if (offset < limit)
        limit = offset;
    if (node->clean > 0 && node->clean <= node->buf->length)
        clean = node->clean;
    offset = lseek(nd->fd, clean, 0);
    if (offset == (off_t)-1)
        return(-1);
    if (offset != clean)
        abort();
    node->buf->length = clean;
    sres = read(nd->fd, ndn_charbuf_reserve(node->buf, limit - clean), limit - clean);
    if (sres < 0)
        return(-1);
    if (sres != limit - clean) {
        abort();
    }
    if (sres + node->buf->length > node->buf->limit) {
        abort();
    }
    node->buf->length += sres;
    return(0);
}

canonical_original_code:
static int bts_read(struct ndn_btree_io *io, struct ndn_btree_node *node, unsigned int limit)
{
    struct bts_node_state *nd = node->iodata;
    long sres;
    long offset;
    long clean = 0;
    if (nd == ((void *)0) || nd->node != node) abort();
    offset = lseek(nd->fd, 0, 2);
    if (offset == (off_t)-1)
        return(-1);
    if (offset < limit)
        limit = offset;
    if (node->clean > 0 && node->clean <= node->buf->length)
        clean = node->clean;
    offset = lseek(nd->fd, clean, 0);
    if (offset == (off_t)-1)
        return(-1);
    if (offset != clean)
        abort();
    node->buf->length = clean;
    sres = read(nd->fd, ndn_charbuf_reserve(node->buf, limit - clean), limit - clean);
    if (sres < 0)
        return(-1);
    if (sres != limit - clean) {
        abort();
    }
    if (sres + node->buf->length > node->buf->limit) {
        abort();
    }
    node->buf->length += sres;
    return(0);
}

variable_types: struct ndn_btree_io { char clue[16]; int (*btopen)(struct ndn_btree_io *, struct ndn_btree_node { unsigned int nodeid; struct ndn_charbuf { unsigned long length; unsigned long limit; unsigned char *buf; } *buf; void *iodata; unsigned int parent; unsigned int clean; unsigned int freelow; unsigned int corrupt; unsigned int activity; } *); int (*btread)(struct ndn_btree_io *, struct ndn_btree_node *, unsigned int); int (*btwrite)(struct ndn_btree_io *, struct ndn_btree_node *); int (*btclose)(struct ndn_btree_io *, struct ndn_btree_node *); int (*btdestroy)(struct ndn_btree_io **); unsigned int maxnodeid; int openfds; void *data; } *

variable_types:
node
struct ndn_btree_node *

variable_types:
limit
unsigned int

variable_types: struct bts_node_state { struct ndn_btree_node { unsigned int nodeid; struct ndn_charbuf { unsigned long length; unsigned long limit; unsigned char *buf; } *buf; void *iodata; unsigned int parent; unsigned int clean; unsigned int freelow; unsigned int corrupt; unsigned int activity; } *node; int fd; } *

variable_types: sres long

variable_types: offset long

variable_types: clean long

return_type: int

user_defined_types: struct ndn_btree_io { char clue[16]; int (*btopen)(struct ndn_btree_io *, struct ndn_btree_node *); int (*btread)(struct ndn_btree_io *, struct ndn_btree_node *, unsigned int); int (*btwrite)(struct ndn_btree_io *, struct ndn_btree_node *); int (*btclose)(struct ndn_btree_io *, struct ndn_btree_node *); int (*btdestroy)(struct ndn_btree_io **); unsigned int maxnodeid; int openfds; void *data; }

user_defined_types: struct bts_node_state { struct ndn_btree_node *node; int fd; }

user_defined_types: struct ndn_btree_node { unsigned int nodeid; struct ndn_charbuf *buf; void *iodata; unsigned int parent; unsigned int clean; unsigned int freelow; unsigned int corrupt; unsigned int activity; }


decompiled_code:
__int64 __fastcall bts_destroy(void **a1)
{
void *v1; // rax
unsigned int v2; // er12
void **v3; // rbp

v1 = *a1;
if ( !*a1 )
return 0LL;
if ( *((__int64 (__fastcall **)())v1 + 6) != bts_destroy
|| (v2 = bts_remove_lockfile_isra_0(*((_QWORD *)v1 + 8)), v3 = (void **)*((_QWORD *)*a1 + 8), *a1 != *v3) )
{
bts_destroy_cold();
}
ndn_charbuf_destroy(v3 + 1);
free(v3);
*((_QWORD *)*a1 + 8) = 0LL;
free(*a1);
*a1 = 0LL;
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func5(void **a1)
{
void *v1; // rax
unsigned int v2; // er12
void **v3; // rbp

v1 = *a1;
if ( !*a1 )
return 0LL;
if ( *((__int64 (__fastcall **)())v1 + 6) != bts_destroy
|| (v2 = func4(*((_QWORD *)v1 + 8)), v3 = (void **)*((_QWORD *)*a1 + 8), *a1 != *v3) )
{
func10();
}
ndn_charbuf_destroy(v3 + 1);
free(v3);
*((_QWORD *)*a1 + 8) = 0LL;
free(*a1);
*a1 = 0LL;
return v2;
}

original_code:
static int
bts_destroy(struct ndn_btree_io **pio)
{
    int res;
    struct bts_data *md = ((void *)0);
    if (*pio == ((void *)0))
        return(0);
    if ((*pio)->btdestroy != &bts_destroy)
        abort();
    res = bts_remove_lockfile(*pio);
    md = (*pio)->data;
    if (md->io != *pio) abort();
    ndn_charbuf_destroy(&md->dirpath);
    free(md);
    (*pio)->data = ((void *)0);
    free(*pio);
    *pio = ((void *)0);
    return(res);
}

variable_types: struct ndn_btree_io { char clue[16]; int (*btopen)(struct ndn_btree_io *, struct ndn_btree_node { unsigned int nodeid; struct ndn_charbuf { unsigned long length; unsigned long limit; unsigned char *buf; } *buf; void *iodata; unsigned int parent; unsigned int clean; unsigned int freelow; unsigned int corrupt; unsigned int activity; } *); int (*btread)(struct ndn_btree_io *, struct ndn_btree_node *, unsigned int); int (*btwrite)(struct ndn_btree_io *, struct ndn_btree_node *); int (*btclose)(struct ndn_btree_io *, struct ndn_btree_node *); int (*btdestroy)(struct ndn_btree_io **); unsigned int maxnodeid; int openfds; void *data; } **

variable_types: res int


decompiled_code:
__int64 __fastcall ndn_interest_lifetime(__int64 a1, __int64 a2)
{
int v2; // eax
__int64 v3; // rbx
__int64 v4; // r8
__int64 result; // rax
__int64 v6; // [rsp-48h] [rbp-48h] BYREF

v2 = *(unsigned __int16 *)(a2 + 70);
if ( *(unsigned __int16 *)(a2 + 72) == v2 )
return 0x4000LL;
v3 = ndn_buf_decoder_start(&v6, a1 + (unsigned __int16)v2);
v4 = ndn_parse_optional_tagged_binary_number(v3, 48LL, 1LL, 7LL, 0x4000LL);
result = *(int *)(v3 + 8);
if ( (int)result >= 0 )
result = v4;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
int v2; // eax
__int64 v3; // rbx
__int64 v4; // r8
__int64 result; // rax
__int64 v6; // [rsp-48h] [rbp-48h] BYREF

v2 = *(unsigned __int16 *)(a2 + 70);
if ( *(unsigned __int16 *)(a2 + 72) == v2 )
return 0x4000LL;
v3 = ndn_buf_decoder_start(&v6, a1 + (unsigned __int16)v2);
v4 = ndn_parse_optional_tagged_binary_number(v3, 48LL, 1LL, 7LL, 0x4000LL);
result = *(int *)(v3 + 8);
if ( (int)result >= 0 )
result = v4;
return result;
}

original_code:
intmax_t
ndn_interest_lifetime(const unsigned char *msg,
                      const struct ndn_parsed_interest *pi)
{
    struct ndn_buf_decoder decoder;
    struct ndn_buf_decoder *d = ((void *)0);
    unsigned start = pi->offset[NDN_PI_B_InterestLifetime];
    size_t size = pi->offset[NDN_PI_E_InterestLifetime] - start;
    uintmax_t val;
    if (size == 0)
        return(4 << 12);
    d = ndn_buf_decoder_start(&decoder, msg + start, size);
    val = ndn_parse_optional_tagged_binary_number(d, NDN_DTAG_InterestLifetime,
   1, 7, 4 << 12);
    if (d->decoder.state < 0)
        return (d->decoder.state);
    return(val);
}

canonical_original_code:
long ndn_interest_lifetime(unsigned char *msg, struct ndn_parsed_interest *pi)
{
    struct ndn_buf_decoder decoder;
    struct ndn_buf_decoder *d = ((void *)0);
    unsigned int start = pi->offset[NDN_PI_B_InterestLifetime];
    unsigned long size = pi->offset[NDN_PI_E_InterestLifetime] - start;
    unsigned long val;
    if (size == 0)
        return(4 << 12);
    d = ndn_buf_decoder_start(&decoder, msg + start, size);
    val = ndn_parse_optional_tagged_binary_number(d, NDN_DTAG_InterestLifetime,
   1, 7, 4 << 12);
    if (d->decoder.state < 0)
        return (d->decoder.state);
    return(val);
}

variable_types:
msg
unsigned char *

variable_types: struct ndn_parsed_interest { int magic; int prefix_comps; int min_suffix_comps; int max_suffix_comps; int orderpref; int answerfrom; int scope; unsigned short offset[-1]; } *

variable_types: struct ndn_buf_decoder { struct ndn_skeleton_decoder { long index; int state; int nest; unsigned long numval; unsigned long token_index; unsigned long element_index; } decoder; unsigned char *buf; unsigned long size; } *

variable_types:
start
unsigned int

variable_types:
size
unsigned long

variable_types:
val
unsigned long

return_type: long

user_defined_types: struct ndn_buf_decoder { struct ndn_skeleton_decoder decoder; unsigned char *buf; unsigned long size; }


decompiled_code:
__int64 __fastcall ndn_interest_lifetime_seconds(__int64 a1, __int64 a2)
{
__int64 result; // rax

result = ndn_interest_lifetime(a1, a2);
if ( result >= 0 )
result = (unsigned int)(result >> 12);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2)
{
__int64 result; // rax

result = func0(a1, a2);
if ( result >= 0 )
result = (unsigned int)(result >> 12);
return result;
}

original_code:
int
ndn_interest_lifetime_seconds(const unsigned char *msg,
                              const struct ndn_parsed_interest *pi)
{
    intmax_t val = ndn_interest_lifetime(msg, pi);
    if (val < 0)
        return(val);
    return(val >> 12);
}

canonical_original_code:
int ndn_interest_lifetime_seconds(unsigned char *msg, struct ndn_parsed_interest *pi)
{
    long val = ndn_interest_lifetime(msg, pi);
    if (val < 0)
        return(val);
    return(val >> 12);
}

variable_types: val long

user_defined_types: struct ndn_parsed_interest { int magic; int prefix_comps; int min_suffix_comps; int max_suffix_comps; int orderpref; int answerfrom; int scope; unsigned short offset[-1]; }


original_code:
ssize_t
ndn_skeleton_decode(struct ndn_skeleton_decoder *d,
                    const unsigned char *p, size_t n)
{
    enum ndn_decoder_state state = d->state;
    int tagstate = 0;
    size_t numval = d->numval;
    ssize_t i = 0;
    unsigned char c;
    size_t chunk;
    int pause = 0;
    if (d->state >= 0) {
        pause = d->state & (1 << 15);
        tagstate = (d->state >> 8) & 3;
        state = d->state & 0xFF;
    }
    while (i < n) {
        switch (state) {
            case NDN_DSTATE_INITIAL:
            case NDN_DSTATE_NEWTOKEN:
                d->token_index = i + d->index;
                if (tagstate > 1 && tagstate-- == 2) {
                    ((void)0);
                }
                if (p[i] == ((unsigned char)(0))) {
                    i++;
                    if (d->nest <= 0 || tagstate > 1) {
                        state = NDN_DSTATE_ERR_NEST;
                        break;
                    }
                    if (tagstate == 1) {
                        tagstate = 0;
                        ((void)0);
                    }
                    else {
                        ((void)0);
                    }
                    d->nest -= 1;
                    if (d->nest == 0) {
                        state = NDN_DSTATE_INITIAL;
                        n = i;
                    }
                    if (pause) {
                        state |= (((int)NDN_NO_TOKEN) << 16);
                        n = i;
                    }
                    break;
                }
                numval = 0;
                state = NDN_DSTATE_NUMVAL;
            case NDN_DSTATE_NUMVAL:
                c = p[i++];
                if ((c & ((unsigned char)(1 << 7))) == ((unsigned char)(0))) {
                    if (numval > ((~(size_t)0U) >> (7 + 3)))
                        state = NDN_DSTATE_ERR_OVERFLOW;
                    numval = (numval << 7) + (c & 127);
                }
                else {
                    numval = (numval << (7-3)) +
                             ((c >> 3) & ((1 << (7-3)) - 1));
                    c &= ((1 << 3) - 1);
                    switch (c) {
                        case NDN_EXT:
                            if (tagstate == 1) {
                                tagstate = 0;
                                ((void)0);
                            }
                            d->nest += 1;
                            d->element_index = d->token_index;
                            state = NDN_DSTATE_NEWTOKEN;
                            break;
                        case NDN_DTAG:
                            if (tagstate == 1) {
                                tagstate = 0;
                                ((void)0);
                            }
                            d->nest += 1;
                            d->element_index = d->token_index;
                            ((void)0);
                            tagstate = 1;
                            state = NDN_DSTATE_NEWTOKEN;
                            break;
                        case NDN_BLOB:
                            if (tagstate == 1) {
                                tagstate = 0;
                                ((void)0);
                            }
                            state = NDN_DSTATE_BLOB;
                            if (numval == 0)
                                state = NDN_DSTATE_NEWTOKEN;
                            break;
                        case NDN_UDATA:
                            if (tagstate == 1) {
                                tagstate = 0;
                                ((void)0);
                            }
                            state = NDN_DSTATE_UDATA;
                            if (numval == 0)
                                state = NDN_DSTATE_NEWTOKEN;
                            break;
                        case NDN_DATTR:
                            if (tagstate != 1) {
                                state = NDN_DSTATE_ERR_ATTR;
                                break;
                            }
                            tagstate = 3;
                            state = NDN_DSTATE_NEWTOKEN;
                            break;
                        case NDN_ATTR:
                            if (tagstate != 1) {
                                state = NDN_DSTATE_ERR_ATTR;
                                break;
                            }
                            numval += 1;
                            state = NDN_DSTATE_ATTRNAME;
                            break;
                        case NDN_TAG:
                            if (tagstate == 1) {
                                tagstate = 0;
                                ((void)0);
                            }
                            numval += 1;
                            d->nest += 1;
                            d->element_index = d->token_index;
                            state = NDN_DSTATE_TAGNAME;
                            break;
                        default:
                            state = NDN_DSTATE_ERR_CODING;
                    }
                    if (pause) {
                        state |= (c << 16);
                        n = i;
                    }
                }
                break;
            case NDN_DSTATE_TAGNAME:
                chunk = n - i;
                if (chunk > numval)
                    chunk = numval;
                if (chunk == 0) {
                    state = NDN_DSTATE_ERR_BUG;
                    break;
                }
                numval -= chunk;
                i += chunk;
                if (numval == 0) {
                    if (d->nest == 0) {
                        state = NDN_DSTATE_ERR_NEST;
                        break;
                    }
                    ((void)0);
                    tagstate = 1;
                    state = NDN_DSTATE_NEWTOKEN;
                }
                break;
            case NDN_DSTATE_ATTRNAME:
                chunk = n - i;
                if (chunk > numval)
                    chunk = numval;
                if (chunk == 0) {
                    state = NDN_DSTATE_ERR_BUG;
                    break;
                }
                numval -= chunk;
                i += chunk;
                if (numval == 0) {
                    if (d->nest == 0) {
                        state = NDN_DSTATE_ERR_ATTR;
                        break;
                    }
                    ((void)0);
                    tagstate = 3;
                    state = NDN_DSTATE_NEWTOKEN;
                }
                break;
            case NDN_DSTATE_UDATA:
            case NDN_DSTATE_BLOB:
                chunk = n - i;
                if (chunk > numval)
                    chunk = numval;
                if (chunk == 0) {
                    state = NDN_DSTATE_ERR_BUG;
                    break;
                }
                numval -= chunk;
                i += chunk;
                if (numval == 0)
                    state = NDN_DSTATE_NEWTOKEN;
                break;
            default:
                n = i;
        }
    }
    if (state < 0)
        tagstate = pause = 0;
    d->state = state | pause | (tagstate << 8);
    d->numval = numval;
    d->index += i;
    return(i);
}

canonical_original_code:
long ndn_skeleton_decode(struct ndn_skeleton_decoder *d, unsigned char *p, unsigned long n)
{
    enum ndn_decoder_state state = d->state;
    int tagstate = 0;
    unsigned long numval = d->numval;
    long i = 0;
    unsigned char c;
    unsigned long chunk;
    int pause = 0;
    if (d->state >= 0) {
        pause = d->state & (1 << 15);
        tagstate = (d->state >> 8) & 3;
        state = d->state & 0xFF;
    }
    while (i < n) {
        switch (state) {
            case NDN_DSTATE_INITIAL:
            case NDN_DSTATE_NEWTOKEN:
                d->token_index = i + d->index;
                if (tagstate > 1 && tagstate-- == 2) {
                    ((void)0);
                }
                if (p[i] == ((unsigned char)(0))) {
                    i++;
                    if (d->nest <= 0 || tagstate > 1) {
                        state = NDN_DSTATE_ERR_NEST;
                        break;
                    }
                    if (tagstate == 1) {
                        tagstate = 0;
                        ((void)0);
                    }
                    else {
                        ((void)0);
                    }
                    d->nest -= 1;
                    if (d->nest == 0) {
                        state = NDN_DSTATE_INITIAL;
                        n = i;
                    }
                    if (pause) {
                        state |= (((int)NDN_NO_TOKEN) << 16);
                        n = i;
                    }
                    break;
                }
                numval = 0;
                state = NDN_DSTATE_NUMVAL;
            case NDN_DSTATE_NUMVAL:
                c = p[i++];
                if ((c & ((unsigned char)(1 << 7))) == ((unsigned char)(0))) {
                    if (numval > ((~(unsigned long)0U) >> (7 + 3)))
                        state = NDN_DSTATE_ERR_OVERFLOW;
                    numval = (numval << 7) + (c & 127);
                }
                else {
                    numval = (numval << (7-3)) +
                             ((c >> 3) & ((1 << (7-3)) - 1));
                    c &= ((1 << 3) - 1);
                    switch (c) {
                        case NDN_EXT:
                            if (tagstate == 1) {
                                tagstate = 0;
                                ((void)0);
                            }
                            d->nest += 1;
                            d->element_index = d->token_index;
                            state = NDN_DSTATE_NEWTOKEN;
                            break;
                        case NDN_DTAG:
                            if (tagstate == 1) {
                                tagstate = 0;
                                ((void)0);
                            }
                            d->nest += 1;
                            d->element_index = d->token_index;
                            ((void)0);
                            tagstate = 1;
                            state = NDN_DSTATE_NEWTOKEN;
                            break;
                        case NDN_BLOB:
                            if (tagstate == 1) {
                                tagstate = 0;
                                ((void)0);
                            }
                            state = NDN_DSTATE_BLOB;
                            if (numval == 0)
                                state = NDN_DSTATE_NEWTOKEN;
                            break;
                        case NDN_UDATA:
                            if (tagstate == 1) {
                                tagstate = 0;
                                ((void)0);
                            }
                            state = NDN_DSTATE_UDATA;
                            if (numval == 0)
                                state = NDN_DSTATE_NEWTOKEN;
                            break;
                        case NDN_DATTR:
                            if (tagstate != 1) {
                                state = NDN_DSTATE_ERR_ATTR;
                                break;
                            }
                            tagstate = 3;
                            state = NDN_DSTATE_NEWTOKEN;
                            break;
                        case NDN_ATTR:
                            if (tagstate != 1) {
                                state = NDN_DSTATE_ERR_ATTR;
                                break;
                            }
                            numval += 1;
                            state = NDN_DSTATE_ATTRNAME;
                            break;
                        case NDN_TAG:
                            if (tagstate == 1) {
                                tagstate = 0;
                                ((void)0);
                            }
                            numval += 1;
                            d->nest += 1;
                            d->element_index = d->token_index;
                            state = NDN_DSTATE_TAGNAME;
                            break;
                        default:
                            state = NDN_DSTATE_ERR_CODING;
                    }
                    if (pause) {
                        state |= (c << 16);
                        n = i;
                    }
                }
                break;
            case NDN_DSTATE_TAGNAME:
                chunk = n - i;
                if (chunk > numval)
                    chunk = numval;
                if (chunk == 0) {
                    state = NDN_DSTATE_ERR_BUG;
                    break;
                }
                numval -= chunk;
                i += chunk;
                if (numval == 0) {
                    if (d->nest == 0) {
                        state = NDN_DSTATE_ERR_NEST;
                        break;
                    }
                    ((void)0);
                    tagstate = 1;
                    state = NDN_DSTATE_NEWTOKEN;
                }
                break;
            case NDN_DSTATE_ATTRNAME:
                chunk = n - i;
                if (chunk > numval)
                    chunk = numval;
                if (chunk == 0) {
                    state = NDN_DSTATE_ERR_BUG;
                    break;
                }
                numval -= chunk;
                i += chunk;
                if (numval == 0) {
                    if (d->nest == 0) {
                        state = NDN_DSTATE_ERR_ATTR;
                        break;
                    }
                    ((void)0);
                    tagstate = 3;
                    state = NDN_DSTATE_NEWTOKEN;
                }
                break;
            case NDN_DSTATE_UDATA:
            case NDN_DSTATE_BLOB:
                chunk = n - i;
                if (chunk > numval)
                    chunk = numval;
                if (chunk == 0) {
                    state = NDN_DSTATE_ERR_BUG;
                    break;
                }
                numval -= chunk;
                i += chunk;
                if (numval == 0)
                    state = NDN_DSTATE_NEWTOKEN;
                break;
            default:
                n = i;
        }
    }
    if (state < 0)
        tagstate = pause = 0;
    d->state = state | pause | (tagstate << 8);
    d->numval = numval;
    d->index += i;
    return(i);
}

variable_types: struct ndn_skeleton_decoder { long index; int state; int nest; unsigned long numval; unsigned long token_index; unsigned long element_index; } *

variable_types:
p
unsigned char *

variable_types:
n
unsigned long

variable_types: enum ndn_decoder_state { NDN_DSTATE_INITIAL=0, NDN_DSTATE_NEWTOKEN=1, NDN_DSTATE_NUMVAL=2, NDN_DSTATE_UDATA=3, NDN_DSTATE_TAGNAME=4, NDN_DSTATE_ATTRNAME=5, NDN_DSTATE_BLOB=6, NDN_DSTATE_ERR_OVERFLOW=-1, NDN_DSTATE_ERR_ATTR=-2, NDN_DSTATE_ERR_CODING=-3, NDN_DSTATE_ERR_NEST=-4, NDN_DSTATE_ERR_BUG=-5 }

variable_types: tagstate int

variable_types:
numval
unsigned long

variable_types: i long

variable_types:
c
unsigned char

variable_types:
chunk
unsigned long

variable_types: pause int


decompiled_code:
__int64 __fastcall ndn_decimal_seqfunc(__int64 a1, __int64 a2, _QWORD *a3)
{
if ( *a3 )
_assert_fail("resultbuf->length == 0", "ndn_bulkdata.c", 0x3Eu, "ndn_decimal_seqfunc");
return ndn_charbuf_putf(a3, "%ju", a1);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2, _QWORD *a3)
{
if ( *a3 )
_assert_fail("resultbuf->length == 0", "ndn_bulkdata.c", 0x3Eu, "ndn_decimal_seqfunc");
return ndn_charbuf_putf(a3, "%ju", a1);
}

original_code:
void
ndn_decimal_seqfunc(uintmax_t x, void *param, struct ndn_charbuf *resultbuf)
{
    (void)param;
    ((void) sizeof ((resultbuf->length == 0) ? 1 : 0), __extension__ ({ if (resultbuf->length == 0) ; else __assert_fail ("resultbuf->length == 0", "ndn_bulkdata.c", 62, __extension__ __PRETTY_FUNCTION__); }));
    ndn_charbuf_putf(resultbuf, "%ju", x);
}

canonical_original_code:
void ndn_decimal_seqfunc(unsigned long x, void *param, struct ndn_charbuf *resultbuf)
{
    (void)param;
    ((void) sizeof ((resultbuf->length == 0) ? 1 : 0), __extension__ ({ if (resultbuf->length == 0) ; else __assert_fail ("resultbuf->length == 0", "ndn_bulkdata.c", 62, __extension__ __PRETTY_FUNCTION__); }));
    ndn_charbuf_putf(resultbuf, "%ju", x);
}

variable_types:
x
unsigned long

variable_types:
param
void *

variable_types: struct ndn_charbuf { unsigned long length; unsigned long limit; unsigned char *buf; } *

return_type: void


decompiled_code:
__int64 __fastcall ndn_charbuf_append_sockaddr(__int64 *a1, unsigned __int16 *a2)
{
__int64 v2; // r14
const void *v4; // r15
void *v5; // r13
unsigned int v6; // er12
char *v7; // rax
const char *v8; // rdi

v2 = *a1;
if ( !a2 )
return (unsigned int)-1;
if ( *a2 != 2 )
{
if ( *a2 == 10 )
{
v4 = a2 + 4;
v5 = &LC1;
v6 = (unsigned __int16)_bswap_16(a2[1]);
ndn_charbuf_append_string(a1, &LC2);
goto LABEL_5;
}
return (unsigned int)-1;
}
v4 = a2 + 2;
v5 = &LC0;
v6 = (unsigned __int16)_bswap_16(a2[1]);
LABEL_5:
v7 = (char *)ndn_charbuf_reserve(a1, 80LL);
v8 = inet_ntop(*a2, v4, v7, 0x50u);
if ( v8 )
{
*a1 += strlen(v8);
ndn_charbuf_append_string(a1, v5);
}
else
{
*a1 = v2;
v6 = -1;
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 *a1, unsigned __int16 *a2)
{
__int64 v2; // r14
const void *v4; // r15
void *v5; // r13
unsigned int v6; // er12
char *v7; // rax
const char *v8; // rdi

v2 = *a1;
if ( !a2 )
return (unsigned int)-1;
if ( *a2 != 2 )
{
if ( *a2 == 10 )
{
v4 = a2 + 4;
v5 = &LC1;
v6 = (unsigned __int16)func0(a2[1]);
ndn_charbuf_append_string(a1, &LC2);
goto LABEL_5;
}
return (unsigned int)-1;
}
v4 = a2 + 2;
v5 = &LC0;
v6 = (unsigned __int16)func0(a2[1]);
LABEL_5:
v7 = (char *)ndn_charbuf_reserve(a1, 80LL);
v8 = inet_ntop(*a2, v4, v7, 0x50u);
if ( v8 )
{
*a1 += strlen(v8);
ndn_charbuf_append_string(a1, v5);
}
else
{
*a1 = v2;
v6 = -1;
}
return v6;
}

original_code:
int
ndn_charbuf_append_sockaddr(struct ndn_charbuf *c, const struct sockaddr *sa)
{
    const unsigned char *rawaddr = ((void *)0);
    const char *s = ((void *)0);
    const char *closer = "";
    const struct sockaddr_in *addr4 = ((void *)0);
    const struct sockaddr_in6 *addr6 = ((void *)0);
    size_t savlen = c->length;
    socklen_t sz = 80;
    int port = 0;
    if (sa == ((void *)0))
        return(-1);
    switch (sa->sa_family) {
        case 2:
            addr4 = (struct sockaddr_in *)sa;
            rawaddr = (const unsigned char *)&addr4->sin_addr.s_addr;
            port = htons(addr4->sin_port);break;
        case 10:
            addr6 = (struct sockaddr_in6 *)sa;
            rawaddr = (const unsigned char *)&addr6->sin6_addr;
            port = htons(addr6->sin6_port);
            ndn_charbuf_append_string(c, "[");
            closer = "]";
            break;
        default:
            return(-1);
    }
    s = inet_ntop(sa->sa_family, rawaddr,
                  (void *)ndn_charbuf_reserve(c, sz), sz);
    if (s == ((void *)0)) {
        c->length = savlen;
        return(-1);
    }
    c->length += strlen(s);
    ndn_charbuf_append_string(c, closer);
    return(port);
}

canonical_original_code:
int ndn_charbuf_append_sockaddr(struct ndn_charbuf *c, struct sockaddr *sa)
{
    unsigned char *rawaddr = ((void *)0);
    char *s = ((void *)0);
    char *closer = "";
    struct sockaddr_in *addr4 = ((void *)0);
    struct sockaddr_in6 *addr6 = ((void *)0);
    unsigned long savlen = c->length;
    unsigned int sz = 80;
    int port = 0;
    if (sa == ((void *)0))
        return(-1);
    switch (sa->sa_family) {
        case 2:
            addr4 = (struct sockaddr_in *)sa;
            rawaddr = (unsigned char *)&addr4->sin_addr.s_addr;
            port = htons(addr4->sin_port);break;
        case 10:
            addr6 = (struct sockaddr_in6 *)sa;
            rawaddr = (unsigned char *)&addr6->sin6_addr;
            port = htons(addr6->sin6_port);
            ndn_charbuf_append_string(c, "[");
            closer = "]";
            break;
        default:
            return(-1);
    }
    s = inet_ntop(sa->sa_family, rawaddr,
                  (void *)ndn_charbuf_reserve(c, sz), sz);
    if (s == ((void *)0)) {
        c->length = savlen;
        return(-1);
    }
    c->length += strlen(s);
    ndn_charbuf_append_string(c, closer);
    return(port);
}

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

variable_types:
rawaddr
unsigned char *

variable_types:
s
char *

variable_types:
closer
char *

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

variable_types: struct sockaddr_in6 { unsigned short sin6_family; unsigned short sin6_port; unsigned int sin6_flowinfo; struct in6_addr { union { unsigned char __u6_addr8[16]; unsigned short __u6_addr16[8]; unsigned int __u6_addr32[4]; } __in6_u; } sin6_addr; unsigned int sin6_scope_id; } *

variable_types:
savlen
unsigned long

variable_types:
sz
unsigned int

variable_types: port int

user_defined_types: struct ndn_charbuf { unsigned long length; unsigned long limit; unsigned char *buf; }

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 sockaddr_in6 { unsigned short sin6_family; unsigned short sin6_port; unsigned int sin6_flowinfo; struct in6_addr sin6_addr; unsigned int sin6_scope_id; }


main

canonical_name: main

repo: dankamongmen/dellicacy

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned __int64 v3; // r13
int v4; // er12
int v6; // eax
int v7; // ebp
int v8; // er12
int *v9; // rax
char *v10; // rax
char *endptr[6]; // [rsp+8h] [rbp-30h] BYREF

v3 = 0LL;
while ( 1 )
{
v4 = getopt(argc, (char *const *)argv, "r:");
if ( v4 == -1 )
break;
if ( v4 == 114 && !v3 )
{
v3 = strtoull(optarg, endptr, 0);
if ( v3 - 1 <= 0xFFFFFFFFFFFFFFFDLL )
continue;
}
goto LABEL_6;
}
if ( argv[optind] )
{
LABEL_6:
usage();
return 1;
}
if ( v3 )
printf("Ratelimited to %llu message per second\n", v3);
else
puts("No ratelimit in effect");
printf("Serving %u %s\n", 10LL, "children");
if ( (unsigned int)spawn_hungry_children_constprop_0(endptr) )
return 1;
v6 = open("/dev/urandom", 0);
v7 = v6;
if ( v6 < 0 )
{
v9 = __errno_location();
v10 = strerror(*v9);
fprintf(stderr, "Coudln't open /dev/urandom for input (%s?)\n", v10);
}
else
{
v8 = feed_hungry_children_constprop_0((unsigned int)v6, endptr[0], v3);
close(v7);
v4 = v8 != 0;
free(endptr[0]);
}
return v4;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned __int64 v3; // r13
int v4; // er12
int v6; // eax
int v7; // ebp
int v8; // er12
int *v9; // rax
char *v10; // rax
char *endptr[6]; // [rsp+8h] [rbp-30h] BYREF

v3 = 0LL;
while ( 1 )
{
v4 = getopt(argc, (char *const *)argv, "r:");
if ( v4 == -1 )
break;
if ( v4 == 114 && !v3 )
{
v3 = strtoull(optarg, endptr, 0);
if ( v3 - 1 <= 0xFFFFFFFFFFFFFFFDLL )
continue;
}
goto LABEL_6;
}
if ( argv[optind] )
{
LABEL_6:
func7();
return 1;
}
if ( v3 )
printf("Ratelimited to %llu message per second\n", v3);
else
puts("No ratelimit in effect");
printf("Serving %u %s\n", 10LL, "children");
if ( (unsigned int)func9(endptr) )
return 1;
v6 = open("/dev/urandom", 0);
v7 = v6;
if ( v6 < 0 )
{
v9 = __errno_location();
v10 = strerror(*v9);
fprintf(stderr, "Coudln't open /dev/urandom for input (%s?)\n", v10);
}
else
{
v8 = func12((unsigned int)v6, endptr[0], v3);
close(v7);
v4 = v8 != 0;
free(endptr[0]);
}
return v4;
}

original_code:
int main(int argc,char **argv){
 unsigned mouths = 10;
 unsigned long long rate = 0;
 int c,*pipes,rnd;
 while((c = getopt(argc,argv,"r:")) != -1){
  switch(c){
   case 'r':{
    char *e;
    if(rate){
     usage();
     return 1;
    }
    if((rate = strtoull(optarg,&e,0)) == 0 || rate == (0x7fffffffffffffffLL * 2ULL + 1ULL)){
     usage();
     return 1;
    }
    break;}
   case '?':
   default:
    usage();
    return 1;
  }
 }
 if(argv[optind]){
  usage();
  return 1;
 }
 if(rate){
  printf("Ratelimited to %llu message per second\n",rate);
 }else{
  printf("No ratelimit in effect\n");
 }
 printf("Serving %u %s\n",mouths,mouths == 1 ? "child" : "children");
 if(spawn_hungry_children(mouths,&pipes)){
  return 1;
 }
 if((rnd = open("/dev/urandom",00)) < 0){
  fprintf(stderr,"Coudln't open /dev/urandom for input (%s?)\n",
    strerror((*__errno_location ())));
  return -1;
 }
 if(feed_hungry_children(rnd,mouths,pipes,rate)){
  close(rnd);
  free(pipes);
  return 1;
 }
 close(rnd);
 free(pipes);
 return 0;
}

canonical_original_code:
int main(int argc, char **argv){
 unsigned int mouths = 10;
 unsigned long long rate = 0;
 int c; int *pipes; int rnd;
 while((c = getopt(argc,argv,"r:")) != -1){
  switch(c){
   case 'r':{
    char *e;
    if(rate){
     usage();
     return 1;
    }
    if((rate = strtoull(optarg,&e,0)) == 0 || rate == (0x7fffffffffffffffLL * 2ULL + 1ULL)){
     usage();
     return 1;
    }
    break;}
   case '?':
   default:
    usage();
    return 1;
  }
 }
 if(argv[optind]){
  usage();
  return 1;
 }
 if(rate){
  printf("Ratelimited to %llu message per second\n",rate);
 }else{
  printf("No ratelimit in effect\n");
 }
 printf("Serving %u %s\n",mouths,mouths == 1 ? "child" : "children");
 if(spawn_hungry_children(mouths,&pipes)){
  return 1;
 }
 if((rnd = open("/dev/urandom",00)) < 0){
  fprintf(stderr,"Coudln't open /dev/urandom for input (%s?)\n",
    strerror((*__errno_location ())));
  return -1;
 }
 if(feed_hungry_children(rnd,mouths,pipes,rate)){
  close(rnd);
  free(pipes);
  return 1;
 }
 close(rnd);
 free(pipes);
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
mouths
unsigned int

variable_types:
rate
unsigned long long

variable_types: c int

variable_types:
pipes
int *

variable_types: rnd int

variable_types:
e
char *


usage

decompiled_code:
size_t usage()
{
return fwrite("usage: ipc [ -r maxrate/sec ]\n", 1uLL, 0x1EuLL, stderr);
}

canonical_decompiled_code:
size_t func7()
{
return fwrite("usage: ipc [ -r maxrate/sec ]\n", 1uLL, 0x1EuLL, stderr);
}

original_code:
static void
usage(void){
 fprintf(stderr,"usage: ipc [ -r maxrate/sec ]\n");
}

canonical_original_code:
static void usage(){
 fprintf(stderr,"usage: ipc [ -r maxrate/sec ]\n");
}


main

repo: danking/test-harness

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v4; // [rsp+8h] [rbp-10h] BYREF

yyparse(&v4, argv, envp);
printf("%s", v4);
return 0;
}

original_code:
int
main (void)
{
  char* result;
  yyparse(&result);
  printf("%s", result);
  return 0;
}

canonical_original_code:
int main()
{
  char *result;
  yyparse(&result);
  printf("%s", result);
  return 0;
}

variable_types:
result
char *


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
float v3; // xmm1_4
int v4; // ebx
double v5; // xmm0_8

v3 = 0.0;
if ( *(float *)&buf_len <= 0.0 )
return 0;
v4 = 0;
do
{
if ( (v4 & 7) == 0 )
putchar(10);
v5 = sin((float)((float)((float)(v3 * *(float *)&span) / *(float *)&buf_len) + *(float *)&w0));
printf("%5d", (unsigned int)(offset + (int)((double)peak * v5)));
if ( (float)(*(float *)&buf_len - 1.0) > v3 )
printf(", ");
else
putchar(10);
v3 = (float)++v4;
}
while ( *(float *)&buf_len > (float)v4 );
return 0;
}

original_code:
int main()
{
  int i;
  for (i = 0; i < buf_len; i++) {
    if (i % 8 == 0)
      printf("\n");
    printf("%5d", offset + (int)(peak * sin(w0 + span * i / buf_len)));
    if (i < buf_len - 1)
      printf(", ");
    else
      printf("\n");
  }
  return 0;
}

variable_types: i int


handler

repo: danluu/setjmp-longjmp-ucontext-snippets

decompiled_code:
void __noreturn handler()
{
printf("stack overflow %d\n", (unsigned int)i_0);
longjmp(try, ++i_0);
}

canonical_decompiled_code:
void __noreturn func7()
{
printf("stack overflow %d\n", (unsigned int)i_0);
longjmp(try, ++i_0);
}

original_code:
void handler(int sig) {
  static int i = 0;
  printf("stack overflow %d\n", i);
  longjmp(try, ++i);
  ((void) sizeof ((0) ? 1 : 0), __extension__ ({ if (0) ; else __assert_fail ("0", "sigaltstack.c", 17, __extension__ __PRETTY_FUNCTION__); }));
}

variable_types: sig int


recurse

decompiled_code:
// attributes: thunk
void __noreturn recurse()
{
while ( 1 )
;
}

canonical_decompiled_code:
void __noreturn func8()
{
while ( 1 )
;
}

original_code:
unsigned recurse(unsigned x) {
    return recurse(x)+1;
}

canonical_original_code:
unsigned int recurse(unsigned int x) {
    return recurse(x)+1;
}

variable_types:
x
unsigned int

return_type: unsigned int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
struct sigaltstack v4; // [rsp+0h] [rbp-C8h] BYREF
struct sigaction v5; // [rsp+20h] [rbp-A8h] BYREF

v4.ss_sp = malloc(0x10000uLL);
memset(&v5.sa_mask, 0, 0x90uLL);
*(_QWORD *)&v4.ss_flags = 0LL;
v4.ss_size = 0x2000LL;
v5.sa_handler = (__sighandler_t)handler;
v5.sa_flags = 0x8000000;
sigaltstack(&v4, 0LL);
sigfillset(&v5.sa_mask);
sigaction(14, &v5, 0LL);
raise(14);
return 0;
}

original_code:
int main() {
  char* stack;
  stack = malloc(sizeof(stack) * 8192);
  stack_t ss = {
    .ss_size = 8192,
    .ss_sp = stack,
  };
  struct sigaction sa = {
    .__sigaction_handler.sa_handler = handler,
    .sa_flags = 0x08000000
  };
  sigaltstack(&ss, 0);
  sigfillset(&sa.sa_mask);
  sigaction(14, &sa, 0);
  raise(14);
  return 0;
  if (_setjmp (try) < 3) {
    recurse(0);
  } else {
    printf("caught exception!\n");
  }
  return 0;
}

canonical_original_code:
int main() {
  char *stack;
  stack = malloc(sizeof(stack) * 8192);
  struct stack_t ss = {
    .ss_size = 8192,
    .ss_sp = stack,
  };
  struct sigaction sa = {
    .__sigaction_handler.sa_handler = handler,
    .sa_flags = 0x08000000
  };
  sigaltstack(&ss, 0);
  sigfillset(&sa.sa_mask);
  sigaction(14, &sa, 0);
  raise(14);
  return 0;
  if (_setjmp (try) < 3) {
    recurse(0);
  } else {
    printf("caught exception!\n");
  }
  return 0;
}

variable_types:
stack
char *

variable_types: struct stack_t { void *ss_sp; int ss_flags; unsigned long ss_size; }

variable_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval { int sival_int; void *sival_ptr; } si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; } *, void *); } __sigaction_handler; struct __sigset_t { unsigned long __val[-1]; } sa_mask; int sa_flags; void (*sa_restorer)(); }

user_defined_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t *, void *); } __sigaction_handler; struct __sigset_t sa_mask; int sa_flags; void (*sa_restorer)(); }

user_defined_types: struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; }


repo: danni/docker-nss

decompiled_code:
__int64 __fastcall lookup_container_ip(__int64 a1, struct in_addr *a2)
{
unsigned int v2; // er12
int v4; // [rsp+4h] [rbp-54h]
const char *v5; // [rsp+8h] [rbp-50h]
const char *v6; // [rsp+28h] [rbp-30h]

v6 = (const char *)g_strdup();
*strrchr(v6, 46) = 0;
v2 = g_spawn_sync();
if ( v2 )
{
v2 = 0;
if ( !v4 )
{
g_strchug();
v5 = (const char *)g_strchomp();
v2 = inet_aton(v5, a2);
}
}
g_free();
g_free();
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, struct in_addr *a2)
{
unsigned int v2; // er12
int v4; // [rsp+4h] [rbp-54h]
const char *v5; // [rsp+8h] [rbp-50h]
const char *v6; // [rsp+28h] [rbp-30h]

v6 = (const char *)g_strdup();
*strrchr(v6, 46) = 0;
v2 = g_spawn_sync();
if ( v2 )
{
v2 = 0;
if ( !v4 )
{
g_strchug();
v5 = (const char *)g_strchomp();
v2 = inet_aton(v5, a2);
}
}
g_free();
g_free();
return v2;
}

original_code:
static gboolean
lookup_container_ip (const char *name,
        struct in_addr *addr)
{
    char *stdout_s, *name_s;
    int exit_status;
    gboolean success = (0);
    name_s = g_strdup (name);
    *strrchr(name_s, '.') = '\0';
    char *argv[] = {
        "docker",
        "inspect",
        "-format={{.NetworkSettings.IPAddress}}",
        name_s,
        ((void *)0),
    };
    if (!g_spawn_sync(((void *)0),
               argv,
               ((void *)0),
               G_SPAWN_STDERR_TO_DEV_NULL | G_SPAWN_SEARCH_PATH,
               ((void *)0), ((void *)0),
               &stdout_s, ((void *)0),
               &exit_status,
               ((void *)0)))
      {
          goto finally;
      }
    if (exit_status != 0)
      {
        goto finally;
      }
    stdout_s = g_strchomp (g_strchug (stdout_s));
    success = inet_aton (stdout_s, addr);
finally:
    g_free (name_s);
    g_free (stdout_s);
    return success;
}

canonical_original_code:
static int lookup_container_ip(char *name, struct in_addr *addr)
{
    char *stdout_s; char *name_s;
    int exit_status;
    int success = (0);
    name_s = g_strdup (name);
    *strrchr(name_s, '.') = '\0';
    char *argv[] = {
        "docker",
        "inspect",
        "-format={{.NetworkSettings.IPAddress}}",
        name_s,
        ((void *)0),
    };
    if (!g_spawn_sync(((void *)0),
               argv,
               ((void *)0),
               G_SPAWN_STDERR_TO_DEV_NULL | G_SPAWN_SEARCH_PATH,
               ((void *)0), ((void *)0),
               &stdout_s, ((void *)0),
               &exit_status,
               ((void *)0)))
      {
          goto finally;
      }
    if (exit_status != 0)
      {
        goto finally;
      }
    stdout_s = g_strchomp (g_strchug (stdout_s));
    success = inet_aton (stdout_s, addr);
finally:
    g_free (name_s);
    g_free (stdout_s);
    return success;
}

variable_types:
name
char *

variable_types: struct in_addr { unsigned int s_addr; } *

variable_types: success int

variable_types:
argv
char *[]


decompiled_code:
ssize_t __fastcall escribir_bloque(int a1, int a2, const void *a3)
{
return pwrite64(a1, a3, 0x1000uLL, a2 << 12);
}

canonical_decompiled_code:
ssize_t __fastcall func7(int a1, int a2, const void *a3)
{
return pwrite64(a1, a3, 0x1000uLL, a2 << 12);
}

original_code:
int escribir_bloque(int fd, int lba, void *buffer)
{
    return pwrite(fd, buffer, 4096, lba*4096);
}

variable_types: fd int

variable_types: lba int

variable_types:
buffer
void *


decompiled_code:
ssize_t __fastcall leer_bloque(int a1, int a2, void *a3)
{
return pread64(a1, a3, 0x1000uLL, a2 << 12);
}

canonical_decompiled_code:
ssize_t __fastcall func8(int a1, int a2, void *a3)
{
return pread64(a1, a3, 0x1000uLL, a2 << 12);
}

original_code:
int leer_bloque(int fd, int lba, void *buffer)
{
    return pread(fd, buffer, 4096, lba*4096);
}


getSize

decompiled_code:
__int64 __fastcall getSize(char *a1)
{
__int64 v1; // rbx
__int32_t v2; // eax
__int64 result; // rax
char *endptr; // [rsp+8h] [rbp-10h] BYREF

endptr = a1;
v1 = strtol(a1, &endptr, 0);
v2 = (*__ctype_toupper_loc())[*endptr];
switch ( v2 )
{
case 'G':
return v1 << 30;
case 'M':
descripcion = 77;
result = v1 << 20;
break;
case 'K':
descripcion = 75;
result = v1 << 10;
break;
default:
result = -1LL;
break;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(char *a1)
{
__int64 v1; // rbx
__int32_t v2; // eax
__int64 result; // rax
char *endptr; // [rsp+8h] [rbp-10h] BYREF

endptr = a1;
v1 = strtol(a1, &endptr, 0);
v2 = (*__ctype_toupper_loc())[*endptr];
switch ( v2 )
{
case 'G':
return v1 << 30;
case 'M':
descripcion = 77;
result = v1 << 20;
break;
case 'K':
descripcion = 75;
result = v1 << 10;
break;
default:
result = -1LL;
break;
}
return result;
}

original_code:
unsigned long getSize(char *s)
{
    unsigned long valor = strtol(s, &s, 0);
    if (toupper(*s) == 'G') {
        valor *= (1024 * 1024 * 1024);
        return valor;
    }
    if (toupper(*s) == 'M'){
        valor *= (1024 * 1024);
        if (valor % 128 == 0){
            descripcion = 'M';
            return valor;
        }
    }
    if (toupper(*s) == 'K'){
        valor *= 1024;
        if (valor % 128 == 0){
            descripcion = 'K';
            return valor;
        }
    }
    return -1;
}

variable_types:
s
char *

variable_types:
valor
unsigned long

return_type: unsigned long


mensaje

decompiled_code:
void __noreturn mensaje()
{
puts("uso: mkfs [--crear <tamano (g|m|k)>] <image-file>");
exit(1);
}

canonical_decompiled_code:
void __noreturn func10()
{
puts("uso: mkfs [--crear <tamano (g|m|k)>] <image-file>");
exit(1);
}

original_code:
void mensaje()
{
    printf("uso: mkfs [--crear <tamano (g|m|k)>] <image-file>\n");
    exit(1);
}


decompiled_code:
__int64 __fastcall cantidad_bloques_bits(unsigned __int64 a1, char a2)
{
__int64 result; // rax
unsigned __int64 v3; // rdi

if ( a2 == 71 || a2 == 77 )
return (unsigned int)(a1 >> 27);
result = 0xFFFFFFFFLL;
v3 = a1 << 10 >> 27;
if ( a2 == 75 )
result = (unsigned int)v3;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(unsigned __int64 a1, char a2)
{
__int64 result; // rax
unsigned __int64 v3; // rdi

if ( a2 == 71 || a2 == 77 )
return (unsigned int)(a1 >> 27);
result = 0xFFFFFFFFLL;
v3 = a1 << 10 >> 27;
if ( a2 == 75 )
result = (unsigned int)v3;
return result;
}

original_code:
int cantidad_bloques_bits(unsigned long size, char tipo)
{
    unsigned long total_bloques;
    unsigned long tmp;
    int r;
    if (tipo == 'G'){
        tmp = size / 4096;
        tmp = tmp / 8;
        total_bloques = tmp / 4096;
        r = total_bloques;
        return r;
    } else if (tipo == 'M'){
        tmp = size / 4096;
        tmp = tmp / 8;
        total_bloques = tmp / 4096;
        r = total_bloques;
        return r;
    } else if(tipo =='K'){
        total_bloques = size * 1024;
        tmp = total_bloques / 4096;
        tmp = tmp / 8;
        total_bloques = tmp / 4096;
        r = total_bloques;
        return r;
    }
    return -1;
}

variable_types:
size
unsigned long

variable_types: tipo char

variable_types:
tmp
unsigned long

variable_types: r int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned __int64 v3; // r13
int v4; // eax
const char *v5; // rbp
_DWORD *v6; // r12
unsigned int v7; // ebp
__off_t v8; // r14
__int64 v9; // rsi
int v10; // eax
int v11; // ebx
unsigned int v12; // er13
int v13; // edi
unsigned int v14; // er10
char *v15; // rax
unsigned int v16; // ecx
int v17; // esi
__uid_t v18; // er14
__gid_t v19; // ebx
struct passwd *v20; // rax
const char **v21; // r15
size_t v22; // rax
char *v23; // rax
int v24; // eax
_DWORD *v25; // rax
int v27; // ebx
const char *src; // [rsp+8h] [rbp-10E0h]
time_t timer; // [rsp+18h] [rbp-10D0h] BYREF
struct stat64 stat_buf; // [rsp+20h] [rbp-10C8h] BYREF
char v31[4096]; // [rsp+B0h] [rbp-1038h] BYREF
char v32; // [rsp+10B0h] [rbp-38h] BYREF

v3 = 0LL;
while ( 1 )
{
v4 = getopt_long_only(argc, (char *const *)argv, "", &opciones_0, 0LL);
if ( v4 == -1 )
break;
if ( v4 == 99 )
v3 = getSize(optarg);
else
printf("Opcion erronea!!");
}
printf("Size del disco es de: %lu\n", v3);
if ( v3 == -1LL )
{
perror("Tamano invalido para formatear, numero debe ser multiplo de 128\n");
exit(1);
}
if ( _bss_start >= argc )
mensaje();
v5 = argv[_bss_start];
v6 = calloc(0x1000uLL, 1uLL);
if ( v3 )
{
LODWORD(v8) = v3 >> 12;
v7 = open64(v5, 577);
if ( (v7 & 0x80000000) != 0 )
{
perror("Error al crear el archivo");
exit(1);
}
v27 = 0;
if ( (int)(v3 >> 12) > 0 )
{
do
{
if ( write(v7, v6, 0x1000uLL) < 0 )
{
perror("Error al escribir al archivo!!\n");
exit(1);
}
++v27;
}
while ( (_DWORD)v8 != v27 );
}
lseek64(v7, 0LL, 0);
}
else
{
if ( open64(v5, 1, 511LL) < 0 || (int)fstat64(0, &stat_buf) < 0 )
{
perror("Error al abrir el archivo!!\n");
exit(1);
}
v7 = 0;
v8 = stat_buf.st_size / 4096;
}
v9 = (unsigned int)descripcion;
v6[1] = v8;
*v6 = 257;
v6[2] = 4096;
v6[4] = 1;
v10 = cantidad_bloques_bits(v3, v9, v6);
v6[3] = v10;
v11 = v10;
v12 = v10 + 1;
v6[6] = v10 + 1;
v6[5] = v8 - v10 - 2;
escribir_bloque(v7, 0LL);
memset(v31, 0xFFu, sizeof(v31));
if ( v11 > 0 )
{
v13 = 0;
v14 = 1;
LABEL_14:
v15 = v31;
while ( !*v15 )
{
LABEL_15:
if ( &v32 == ++v15 )
{
++v14;
goto LABEL_14;
}
}
v16 = 0;
while ( v11 + 2 > v13 )
{
v17 = (unsigned __int8)*v15;
if ( _bittest(&v17, v16) )
{
++v13;
*v15 &= ~(1 << v16);
}
if ( ++v16 == 7 )
goto LABEL_15;
}
escribir_bloque(v7, v14);
}
v18 = getuid();
if ( v18 == -1 )
perror("getuid() error.");
v19 = getgid();
if ( v19 == -1 )
perror("getgid() error.");
timer = time(0LL);
v20 = getpwuid(v18);
*(_WORD *)v6 = 2;
v21 = (const char **)&v20->pw_name;
src = v20->pw_name;
v22 = strlen(v20->pw_name);
memcpy(v6 + 6, src, v22 + 1);
printf("creador = %s\n", *v21);
*((_WORD *)v6 + 1) = v18;
printf("uid = %d\n", v18);
*((_WORD *)v6 + 2) = v19;
printf("gid = %d\n", v19);
*((_WORD *)v6 + 3) = 16895;
printf("Permisos para / = %d\n", 16895LL);
v6[3] = timer;
v23 = ctime(&timer);
printf("fcreacion = %s\n", v23);
v24 = timer;
v6[5] = 0;
v6[4] = v24;
v6[2] = v24;
v25 = (_DWORD *)((char *)v6 + 122);
do
{
v25 += 16;
*((_WORD *)v25 - 32) = 0;
}
while ( (_DWORD *)((char *)v6 + 4154) != v25 );
escribir_bloque(v7, v12);
close(v7);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned __int64 v3; // r13
int v4; // eax
const char *v5; // rbp
_DWORD *v6; // r12
unsigned int v7; // ebp
__off_t v8; // r14
__int64 v9; // rsi
int v10; // eax
int v11; // ebx
unsigned int v12; // er13
int v13; // edi
unsigned int v14; // er10
char *v15; // rax
unsigned int v16; // ecx
int v17; // esi
__uid_t v18; // er14
__gid_t v19; // ebx
struct passwd *v20; // rax
const char **v21; // r15
size_t v22; // rax
char *v23; // rax
int v24; // eax
_DWORD *v25; // rax
int v27; // ebx
const char *src; // [rsp+8h] [rbp-10E0h]
time_t timer; // [rsp+18h] [rbp-10D0h] BYREF
struct stat64 stat_buf; // [rsp+20h] [rbp-10C8h] BYREF
char v31[4096]; // [rsp+B0h] [rbp-1038h] BYREF
char v32; // [rsp+10B0h] [rbp-38h] BYREF

v3 = 0LL;
while ( 1 )
{
v4 = getopt_long_only(argc, (char *const *)argv, "", &opciones_0, 0LL);
if ( v4 == -1 )
break;
if ( v4 == 99 )
v3 = func9(optarg);
else
printf("Opcion erronea!!");
}
printf("Size del disco es de: %lu\n", v3);
if ( v3 == -1LL )
{
perror("Tamano invalido para formatear, numero debe ser multiplo de 128\n");
exit(1);
}
if ( _bss_start >= argc )
func10();
v5 = argv[_bss_start];
v6 = calloc(0x1000uLL, 1uLL);
if ( v3 )
{
LODWORD(v8) = v3 >> 12;
v7 = open64(v5, 577);
if ( (v7 & 0x80000000) != 0 )
{
perror("Error al crear el archivo");
exit(1);
}
v27 = 0;
if ( (int)(v3 >> 12) > 0 )
{
do
{
if ( write(v7, v6, 0x1000uLL) < 0 )
{
perror("Error al escribir al archivo!!\n");
exit(1);
}
++v27;
}
while ( (_DWORD)v8 != v27 );
}
lseek64(v7, 0LL, 0);
}
else
{
if ( open64(v5, 1, 511LL) < 0 || (int)func15(0, &stat_buf) < 0 )
{
perror("Error al abrir el archivo!!\n");
exit(1);
}
v7 = 0;
v8 = stat_buf.st_size / 4096;
}
v9 = (unsigned int)descripcion;
v6[1] = v8;
*v6 = 257;
v6[2] = 4096;
v6[4] = 1;
v10 = func11(v3, v9, v6);
v6[3] = v10;
v11 = v10;
v12 = v10 + 1;
v6[6] = v10 + 1;
v6[5] = v8 - v10 - 2;
func7(v7, 0LL);
memset(v31, 0xFFu, sizeof(v31));
if ( v11 > 0 )
{
v13 = 0;
v14 = 1;
LABEL_14:
v15 = v31;
while ( !*v15 )
{
LABEL_15:
if ( &v32 == ++v15 )
{
++v14;
goto LABEL_14;
}
}
v16 = 0;
while ( v11 + 2 > v13 )
{
v17 = (unsigned __int8)*v15;
if ( _bittest(&v17, v16) )
{
++v13;
*v15 &= ~(1 << v16);
}
if ( ++v16 == 7 )
goto LABEL_15;
}
func7(v7, v14);
}
v18 = getuid();
if ( v18 == -1 )
perror("getuid() error.");
v19 = getgid();
if ( v19 == -1 )
perror("getgid() error.");
timer = time(0LL);
v20 = getpwuid(v18);
*(_WORD *)v6 = 2;
v21 = (const char **)&v20->pw_name;
src = v20->pw_name;
v22 = strlen(v20->pw_name);
memcpy(v6 + 6, src, v22 + 1);
printf("creador = %s\n", *v21);
*((_WORD *)v6 + 1) = v18;
printf("uid = %d\n", v18);
*((_WORD *)v6 + 2) = v19;
printf("gid = %d\n", v19);
*((_WORD *)v6 + 3) = 16895;
printf("Permisos para / = %d\n", 16895LL);
v6[3] = timer;
v23 = ctime(&timer);
printf("fcreacion = %s\n", v23);
v24 = timer;
v6[5] = 0;
v6[4] = v24;
v6[2] = v24;
v25 = (_DWORD *)((char *)v6 + 122);
do
{
v25 += 16;
*((_WORD *)v25 - 32) = 0;
}
while ( (_DWORD *)((char *)v6 + 4154) != v25 );
func7(v7, v12);
close(v7);
return 0;
}

original_code:
int main(int argc, char **argv)
{
    static struct option opciones[] =
    {
        {"crear", 1, ((void *)0), 'c'},
        {0, 0, 0, 0}
    };
    int c;
    unsigned long size_disco = 0;
    while ((c = getopt_long_only(argc, argv, "", opciones, ((void *)0))) != -1) {
        switch(c) {
            case 'c': size_disco = getSize(optarg);
                        break;
            default: printf("Opcion erronea!!");
        }
    }
    printf("Size del disco es de: %lu\n", size_disco);
    if (size_disco == -1) {
        perror("Tamano invalido para formatear, numero debe ser multiplo de 128\n");
        exit(1);
    }
    if (optind >= argc) {
        mensaje();
    }
    char *path = argv[optind];
    int i, fd, numero_bloques;
    struct stat sb;
    void *buffer = calloc(4096, 1);
    if (size_disco == 0) {
        if ((fd = open(path, 01, 0777) < 0) || (fstat(fd, &sb) < 0)) {
            perror("Error al abrir el archivo!!\n"),
            exit(1);
        }
        numero_bloques = sb.st_size / 4096;
    } else {
        numero_bloques = size_disco / 4096;
        if ((fd = open(path, 01 | 0100 | 01000)) < 0) {
              perror("Error al crear el archivo");
              exit(1);
        }
        for (i = 0; i < numero_bloques; i++) {
            if (write(fd, buffer, 4096) < 0) {
                perror("Error al escribir al archivo!!\n");
                exit(1);
            }
        }
        lseek(fd, 0, 0);
     }
    struct Super_Block *sp = buffer;
    sp->magic_number = 0x101;
    sp->size_bloques = 4096;
    sp->total_bloques = numero_bloques;
    sp->primerbloque_mapabits = 1;
    int size_bloquesmapa = cantidad_bloques_bits(size_disco, descripcion);
    sp->sizebloques_mapabits = size_bloquesmapa;
    sp->bloque_root = size_bloquesmapa + 1;
    sp->bloques_libres = numero_bloques - size_bloquesmapa - 2;
    escribir_bloque(fd, 0, buffer);
    unsigned char arr[4096];
    void *bitsmapa = malloc(4096);
    int j, k, cont = 0;
    int escribirmapabits = 1 + 1 + size_bloquesmapa;
    for (j = 0; j < 4096; j++)
        arr[j] = 255;
    for (i = 1; i < i + size_bloquesmapa; i++) {
        _Bool flag = 0;
        for (j = 0;j < 4096; j++) {
            if (arr[j] != 0) {
                for (k = 0;k < 7; k++) {
                    if (cont < escribirmapabits) {
                        if(arr[j] & (1 << k)) {
                            arr[j]=arr[j]&~(1<<k);
                            cont++;
                        }
                    } else {
                        bitsmapa = &arr;
                        escribir_bloque(fd,i,bitsmapa);
                        flag = 1;
                    }
                    if (flag)
                        break;
                }
            }
            if (flag)
                break;
        }
        if (flag)
            break;
    }
    struct Directory *rootDir = buffer;
    uid_t uid;
    if ((uid = getuid()) == -1) perror("getuid() error.");
    gid_t gid;
    if ((gid = getgid()) == -1) perror("getgid() error.");
    time_t tiempo = time(((void *)0));
    struct passwd *pws = getpwuid(uid);
    rootDir->info.tipo_bloque = 2;
    memcpy(rootDir->creador, pws->pw_name, strlen(pws->pw_name) + 1); printf("creador = %s\n", pws->pw_name);
    rootDir->info.uid = uid; printf("uid = %d\n", uid);
    rootDir->info.gid = gid; printf("gid = %d\n", gid);
    rootDir->info.mode = 0040000 | (0400|0200|0100) | ((0400|0200|0100) >> 3) | (((0400|0200|0100) >> 3) >> 3); printf("Permisos para / = %d\n", 0040000 | (0400|0200|0100) | ((0400|0200|0100) >> 3) | (((0400|0200|0100) >> 3) >> 3));
    rootDir->info.fcreacion = tiempo; printf("fcreacion = %s\n", ctime(&tiempo));
    rootDir->info.fmodificacion = tiempo;
    rootDir->info.facceso = tiempo;
    rootDir->cantidad_elementos = 0;
    for (k = 0; k < ((4096 - 64) / 64); k++)
        rootDir->directory_Entries[k].tipo_bloque = 0;
    escribir_bloque(fd, 1 + size_bloquesmapa, rootDir);
    close(fd);
    return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
    static struct option opciones[] = {
        {"crear", 1, ((void *)0), 'c'},
        {0, 0, 0, 0}
    };
    int c;
    unsigned long size_disco = 0;
    while ((c = getopt_long_only(argc, argv, "", opciones, ((void *)0))) != -1) {
        switch(c) {
            case 'c': size_disco = getSize(optarg);
                        break;
            default: printf("Opcion erronea!!");
        }
    }
    printf("Size del disco es de: %lu\n", size_disco);
    if (size_disco == -1) {
        perror("Tamano invalido para formatear, numero debe ser multiplo de 128\n");
        exit(1);
    }
    if (optind >= argc) {
        mensaje();
    }
    char *path = argv[optind];
    int i; int fd; int numero_bloques;
    struct stat sb;
    void *buffer = calloc(4096, 1);
    if (size_disco == 0) {
        if ((fd = open(path, 01, 0777) < 0) || (fstat(fd, &sb) < 0)) {
            perror("Error al abrir el archivo!!\n"),
            exit(1);
        }
        numero_bloques = sb.st_size / 4096;
    } else {
        numero_bloques = size_disco / 4096;
        if ((fd = open(path, 01 | 0100 | 01000)) < 0) {
              perror("Error al crear el archivo");
              exit(1);
        }
        for (i = 0; i < numero_bloques; i++) {
            if (write(fd, buffer, 4096) < 0) {
                perror("Error al escribir al archivo!!\n");
                exit(1);
            }
        }
        lseek(fd, 0, 0);
     }
    struct Super_Block *sp = buffer;
    sp->magic_number = 0x101;
    sp->size_bloques = 4096;
    sp->total_bloques = numero_bloques;
    sp->primerbloque_mapabits = 1;
    int size_bloquesmapa = cantidad_bloques_bits(size_disco, descripcion);
    sp->sizebloques_mapabits = size_bloquesmapa;
    sp->bloque_root = size_bloquesmapa + 1;
    sp->bloques_libres = numero_bloques - size_bloquesmapa - 2;
    escribir_bloque(fd, 0, buffer);
    unsigned char arr[4096];
    void *bitsmapa = malloc(4096);
    int j; int k; int cont = 0;
    int escribirmapabits = 1 + 1 + size_bloquesmapa;
    for (j = 0; j < 4096; j++)
        arr[j] = 255;
    for (i = 1; i < i + size_bloquesmapa; i++) {
        _Bool flag = 0;
        for (j = 0;j < 4096; j++) {
            if (arr[j] != 0) {
                for (k = 0;k < 7; k++) {
                    if (cont < escribirmapabits) {
                        if(arr[j] & (1 << k)) {
                            arr[j]=arr[j]&~(1<<k);
                            cont++;
                        }
                    } else {
                        bitsmapa = &arr;
                        escribir_bloque(fd,i,bitsmapa);
                        flag = 1;
                    }
                    if (flag)
                        break;
                }
            }
            if (flag)
                break;
        }
        if (flag)
            break;
    }
    struct Directory *rootDir = buffer;
    unsigned int uid;
    if ((uid = getuid()) == -1) perror("getuid() error.");
    unsigned int gid;
    if ((gid = getgid()) == -1) perror("getgid() error.");
    long tiempo = time(((void *)0));
    struct passwd *pws = getpwuid(uid);
    rootDir->info.tipo_bloque = 2;
    memcpy(rootDir->creador, pws->pw_name, strlen(pws->pw_name) + 1); printf("creador = %s\n", pws->pw_name);
    rootDir->info.uid = uid; printf("uid = %d\n", uid);
    rootDir->info.gid = gid; printf("gid = %d\n", gid);
    rootDir->info.mode = 0040000 | (0400|0200|0100) | ((0400|0200|0100) >> 3) | (((0400|0200|0100) >> 3) >> 3); printf("Permisos para / = %d\n", 0040000 | (0400|0200|0100) | ((0400|0200|0100) >> 3) | (((0400|0200|0100) >> 3) >> 3));
    rootDir->info.fcreacion = tiempo; printf("fcreacion = %s\n", ctime(&tiempo));
    rootDir->info.fmodificacion = tiempo;
    rootDir->info.facceso = tiempo;
    rootDir->cantidad_elementos = 0;
    for (k = 0; k < ((4096 - 64) / 64); k++)
        rootDir->directory_Entries[k].tipo_bloque = 0;
    escribir_bloque(fd, 1 + size_bloquesmapa, rootDir);
    close(fd);
    return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: struct option { char *name; int has_arg; int *flag; int val; } []

variable_types: c int

variable_types:
path
char *

variable_types: i int

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; long st_atime; unsigned long st_atimensec; long st_mtime; unsigned long st_mtimensec; long st_ctime; unsigned long st_ctimensec; long __glibc_reserved[3]; }

variable_types: struct Super_Block { int magic_number; int total_bloques; int size_bloques; int sizebloques_mapabits; int primerbloque_mapabits; int bloques_libres; int bloque_root; int padding[-1]; } *

variable_types:
arr
unsigned char [4096]

variable_types:
bitsmapa
void *

variable_types: j int

variable_types: k int

variable_types: cont int

variable_types: escribirmapabits int

variable_types: struct Directory { struct inodo { unsigned short tipo_bloque; unsigned short uid; unsigned short gid; unsigned short mode; unsigned int facceso; unsigned int fcreacion; unsigned int fmodificacion; } info; unsigned int cantidad_elementos; char creador[40]; struct Directory_Entry { char nombre[58]; unsigned short tipo_bloque; unsigned int apuntador; } directory_Entries[-1]; } *

variable_types:
uid
unsigned int

variable_types:
gid
unsigned int

variable_types: tiempo long

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

user_defined_types: struct Directory { struct inodo info; unsigned int cantidad_elementos; char creador[40]; struct Directory_Entry directory_Entries[-1]; }


alocarBloque

decompiled_code:
__int64 __fastcall alocarBloque(int a1, _DWORD *a2)
{
unsigned int v2; // er14
unsigned int v3; // er12
void *v4; // r13
int v5; // ebx
int v6; // er15
unsigned __int8 *v7; // rax
unsigned __int8 v8; // dl
int v9; // ecx
unsigned int v10; // er12
int v12; // [rsp+8h] [rbp-40h]

v2 = a2[4];
v3 = a2[2];
printf("primer blq = %d\n", v2);
v12 = a2[3];
v4 = calloc(v3, 1uLL);
v5 = v2;
if ( v2 + v12 > v2 )
{
while ( 2 )
{
v6 = 0;
leer_bloque(a1, v5, v4);
v7 = (unsigned __int8 *)v4;
do
{
v8 = *v7;
if ( *v7 )
{
v9 = v8;
v10 = 0;
while ( !_bittest(&v9, v10) )
{
if ( ++v10 == 7 )
goto LABEL_3;
}
*v7 = __ROL4__(-2, v10) & v8;
printf("bit #%d\n", v10);
escribir_bloque(a1, v5, v4);
return (v12 + 8) * (v5 - v2) + v10 + 8 * v6;
}
LABEL_3:
++v6;
++v7;
}
while ( v6 != 4096 );
if ( v2 + v12 > ++v5 )
continue;
break;
}
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func12(int a1, _DWORD *a2)
{
unsigned int v2; // er14
unsigned int v3; // er12
void *v4; // r13
int v5; // ebx
int v6; // er15
unsigned __int8 *v7; // rax
unsigned __int8 v8; // dl
int v9; // ecx
unsigned int v10; // er12
int v12; // [rsp+8h] [rbp-40h]

v2 = a2[4];
v3 = a2[2];
printf("primer blq = %d\n", v2);
v12 = a2[3];
v4 = calloc(v3, 1uLL);
v5 = v2;
if ( v2 + v12 > v2 )
{
while ( 2 )
{
v6 = 0;
func8(a1, v5, v4);
v7 = (unsigned __int8 *)v4;
do
{
v8 = *v7;
if ( *v7 )
{
v9 = v8;
v10 = 0;
while ( !_bittest(&v9, v10) )
{
if ( ++v10 == 7 )
goto LABEL_3;
}
*v7 = __ROL4__(-2, v10) & v8;
printf("bit #%d\n", v10);
func7(a1, v5, v4);
return (v12 + 8) * (v5 - v2) + v10 + 8 * v6;
}
LABEL_3:
++v6;
++v7;
}
while ( v6 != 4096 );
if ( v2 + v12 > ++v5 )
continue;
break;
}
}
return 0LL;
}

original_code:
unsigned int alocarBloque(int fd, struct Super_Block *superBlock)
 {
     unsigned int size_bloques = superBlock->size_bloques;
     unsigned int inicio_bitmap = superBlock->primerbloque_mapabits;
     printf("primer blq = %d\n", inicio_bitmap);
     unsigned int size_bitmap = superBlock->sizebloques_mapabits;
     unsigned int i;
     unsigned char *buffer = (unsigned char*)calloc(size_bloques, 1);
     for(i = inicio_bitmap; i < (inicio_bitmap + size_bitmap); i++){
         leer_bloque(fd, i, buffer);
         unsigned int j;
         for(j = 0; j < 4096; j++){
             if (buffer[j] != 0){
                 int x;
                 for(x = 0; x < 7; x++){
                     if (buffer[j] & (1 << x)){
                         buffer[j] &= ~(1 << x);
                         printf("bit #%d\n", x);
                         escribir_bloque(fd, i, buffer);
                         return (i-inicio_bitmap) * (8 + size_bitmap) + (j * 8) + x;
                     }
                 }
             }
         }
     }
     return 0;
 }

variable_types: fd int

variable_types:
i
unsigned int

variable_types:
buffer
unsigned char *

variable_types:
j
unsigned int

variable_types: x int

user_defined_types: struct Super_Block { int magic_number; int total_bloques; int size_bloques; int sizebloques_mapabits; int primerbloque_mapabits; int bloques_libres; int bloque_root; int padding[-1]; }


repo: dannoy/tools

decompiled_code:
__int64 next_element()
{
return __isoc99_scanf();
}

canonical_decompiled_code:
__int64 func7()
{
return __isoc99_scanf();
}

original_code:
int next_element(int *value)
{
    return scanf("%d", value);
}

variable_types:
value
int *


decompiled_code:
__int64 __fastcall print_value(unsigned int a1)
{
fprintf(_bss_start, "%d\n", a1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned int a1)
{
fprintf(_bss_start, "%d\n", a1);
return 0LL;
}

original_code:
int print_value(int value)
{
    fprintf(stdout, "%d\n", value);
    return 0;
}

variable_types: value int


decompiled_code:
_BOOL8 __fastcall int_smaller(int a1, int a2)
{
return a1 < a2;
}

canonical_decompiled_code:
_BOOL8 __fastcall func9(int a1, int a2)
{
return a1 < a2;
}

original_code:
static int int_smaller(int a, int b)
{
    return a < b ? 1 : 0;
}

variable_types: a int

variable_types: b int


decompiled_code:
__int64 __fastcall heap_init(__int64 a1, int a2)
{
void *v2; // rax
__int64 result; // rax

if ( !a1 )
__assert_fail("h != (void *)0", "heap.c", 0x20u, "heap_init");
v2 = malloc(4LL * (a2 + 1));
*(_DWORD *)(a1 + 8) = 0;
*(_QWORD *)a1 = v2;
result = 0LL;
*(_DWORD *)(a1 + 12) = a2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, int a2)
{
void *v2; // rax
__int64 result; // rax

if ( !a1 )
__assert_fail("h != (void *)0", "heap.c", 0x20u, "heap_init");
v2 = malloc(4LL * (a2 + 1));
*(_DWORD *)(a1 + 8) = 0;
*(_QWORD *)a1 = v2;
result = 0LL;
*(_DWORD *)(a1 + 12) = a2;
return result;
}

original_code:
static int heap_init(struct heap *h, int cap)
{
    ((void) sizeof ((h != (void *)0) ? 1 : 0), __extension__ ({ if (h != (void *)0) ; else __assert_fail ("h != (void *)0", "heap.c", 32, __extension__ __PRETTY_FUNCTION__); }));
    h->data = malloc((cap + 1) * sizeof(int));
    h->size = 0;
    h->cap = cap;
    return 0;
}

variable_types: struct heap { int *data; int size; int cap; } *

variable_types: cap int


decompiled_code:
__int64 __fastcall heap_fini(void **a1)
{
void *v1; // rdi

if ( !a1 )
return 0LL;
v1 = *a1;
if ( !v1 )
return 0LL;
free(v1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func11(void **a1)
{
void *v1; // rdi

if ( !a1 )
return 0LL;
v1 = *a1;
if ( !v1 )
return 0LL;
free(v1);
return 0LL;
}

original_code:
static int heap_fini(struct heap *h)
{
    if(h && h->data) free(h->data);
    return 0;
}

user_defined_types: struct heap { int *data; int size; int cap; }


decompiled_code:
// attributes: thunk
__int64 __fastcall min_heap_init(__int64 a1, int a2)
{
return heap_init(a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1, int a2)
{
return func10(a1, a2);
}

original_code:
int min_heap_init(struct heap *h, int cap)
{
    return heap_init(h, cap);
}


decompiled_code:
// attributes: thunk
__int64 __fastcall min_heap_fini(void **a1)
{
return heap_fini(a1);
}

canonical_decompiled_code:
__int64 __fastcall func14(void **a1)
{
return func11(a1);
}

original_code:
int min_heap_fini(struct heap *h)
{
    return heap_fini(h);
}


decompiled_code:
__int64 __fastcall min_heap_replace_root(__int64 a1, int a2)
{
*(_DWORD *)(*(_QWORD *)a1 + 4LL) = a2;
return heapify_constprop_0(a1);
}

canonical_decompiled_code:
__int64 __fastcall func16(__int64 a1, int a2)
{
*(_DWORD *)(*(_QWORD *)a1 + 4LL) = a2;
return func12(a1);
}

original_code:
int min_heap_replace_root(struct heap *h, int value)
{
    h->data[1] = value;
    return heapify(h, 1, int_smaller);
}


decompiled_code:
__int64 __fastcall min_heap_root(int *a1, _DWORD *a2)
{
if ( a1[2] <= 0 )
return 0xFFFFFFFFLL;
*a2 = *(_DWORD *)(*(_QWORD *)a1 + 4LL);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func17(int *a1, _DWORD *a2)
{
if ( a1[2] <= 0 )
return 0xFFFFFFFFLL;
*a2 = *(_DWORD *)(*(_QWORD *)a1 + 4LL);
return 0LL;
}

original_code:
int min_heap_root(struct heap *h, int *value)
{
    if(h->size > 0) {
        *value = h->data[1];
        return 0;
    }
    return -1;
}


decompiled_code:
__int64 __fastcall min_heap_iterate(__int64 a1, int (__fastcall *a2)(_QWORD))
{
int v2; // ebp
__int64 v3; // rbx

if ( *(int *)(a1 + 8) <= 0 )
return 0LL;
v2 = 1;
v3 = 4LL;
do
{
if ( a2(*(unsigned int *)(*(_QWORD *)a1 + v3)) < 0 )
return 0xFFFFFFFFLL;
++v2;
v3 += 4LL;
}
while ( *(_DWORD *)(a1 + 8) >= v2 );
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func18(__int64 a1, int (__fastcall *a2)(_QWORD))
{
int v2; // ebp
__int64 v3; // rbx

if ( *(int *)(a1 + 8) <= 0 )
return 0LL;
v2 = 1;
v3 = 4LL;
do
{
if ( a2(*(unsigned int *)(*(_QWORD *)a1 + v3)) < 0 )
return 0xFFFFFFFFLL;
++v2;
v3 += 4LL;
}
while ( *(_DWORD *)(a1 + 8) >= v2 );
return 0LL;
}

original_code:
int min_heap_iterate(struct heap *h, int (*iter)(int value))
{
    int i = 1;
    for(i = 1; i <= h->size; ++i) {
        if(iter(h->data[i]) < 0) return -1;
    }
    return 0;
}

variable_types:
iter
int (*)(int value)

variable_types: i int


decompiled_code:
// attributes: thunk
__int64 __fastcall max_heap_init(__int64 a1, int a2)
{
return min_heap_init(a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func19(__int64 a1, int a2)
{
return func13(a1, a2);
}

original_code:
int max_heap_init(struct heap *h, int cap)
{
    return heap_init(h, cap);
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
unsigned int v5; // [rsp+8h] [rbp-20h] BYREF
int v6; // [rsp+Ch] [rbp-1Ch] BYREF
char v7[24]; // [rsp+10h] [rbp-18h] BYREF

v3 = 1000;
min_heap_init(v7, 1000LL, envp);
do
{
if ( (unsigned int)next_element(&v5) == -1 )
return -1;
min_heap_insert(v7, v5);
--v3;
}
while ( v3 );
while ( (unsigned int)next_element(&v5) != -1 )
{
if ( (int)min_heap_root(v7, &v6) < 0 )
return -1;
if ( (int)v5 > v6 )
min_heap_replace_root(v7);
}
min_heap_fini(v7);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
unsigned int v5; // [rsp+8h] [rbp-20h] BYREF
int v6; // [rsp+Ch] [rbp-1Ch] BYREF
char v7[24]; // [rsp+10h] [rbp-18h] BYREF

v3 = 1000;
func13(v7, 1000LL, envp);
do
{
if ( (unsigned int)func7(&v5) == -1 )
return -1;
func15(v7, v5);
--v3;
}
while ( v3 );
while ( (unsigned int)func7(&v5) != -1 )
{
if ( (int)func17(v7, &v6) < 0 )
return -1;
if ( (int)v5 > v6 )
func16(v7);
}
func14(v7);
return 0;
}

original_code:
int main()
{
    int i = 0, value, root;
    struct heap h;
    min_heap_init(&h, 1000);
    for(i = 0; i < 1000; ++i) {
        if((-1) == next_element(&value)) {
            return -1;
        }
        min_heap_insert(&h, value);
    }
    while((-1) != next_element(&value)) {
        if(min_heap_root(&h, &root) < 0) {
            return -1;
        }
        else if(value > root) {
            min_heap_replace_root(&h, value);
        }
    }
    min_heap_fini(&h);
    return 0;
}

canonical_original_code:
int main()
{
    int i = 0; int value; int root;
    struct heap h;
    min_heap_init(&h, 1000);
    for(i = 0; i < 1000; ++i) {
        if((-1) == next_element(&value)) {
            return -1;
        }
        min_heap_insert(&h, value);
    }
    while((-1) != next_element(&value)) {
        if(min_heap_root(&h, &root) < 0) {
            return -1;
        }
        else if(value > root) {
            min_heap_replace_root(&h, value);
        }
    }
    min_heap_fini(&h);
    return 0;
}

variable_types: root int

variable_types: struct heap { int *data; int size; int cap; }


fatal

decompiled_code:
void __noreturn fatal(__int64 a1, int a2, const char *a3, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

va_start(arg, a3);
fwrite("[FWU] ", 1uLL, 6uLL, stderr);
vfprintf(stderr, a3, arg);
exit(a2);
}

canonical_decompiled_code:
void __noreturn func7(__int64 a1, int a2, const char *a3, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

va_start(arg, a3);
fwrite("[FWU] ", 1uLL, 6uLL, stderr);
vfprintf(stderr, a3, arg);
exit(a2);
}

original_code:
void fatal(struct ctx *u, int errnum, char *fmt, ...)
{
    va_list ap;
    __builtin_va_start(ap,fmt);
    fprintf(stderr, "[FWU] ");
    vfprintf(stderr, fmt, ap);
    __builtin_va_end(ap);
    exit(errnum);
}

canonical_original_code:
void fatal(struct ctx *u, int errnum, char *fmt, ...)
{
    __builtin_va_list ap;
    __builtin_va_start(ap,fmt);
    fprintf(stderr, "[FWU] ");
    vfprintf(stderr, fmt, ap);
    __builtin_va_end(ap);
    exit(errnum);
}

variable_types:
u
struct ctx {  } *

variable_types: errnum int

variable_types:
fmt
char *


decompiled_code:
__int64 __fastcall elf_get_file_size(__int64 a1, unsigned __int64 a2)
{
__int64 v3; // r13
__int64 v4; // rbp
unsigned int *v5; // rax
unsigned __int64 v6; // r9
int v7; // er12
__int64 v8; // r10
int v9; // edx
int v10; // er13

if ( a2 <= 0x34 )
__assert_fail("size > sizeof(Elf32_Ehdr)", "elf_size.c", 0x38u, "elf_get_file_size");
v3 = *(unsigned int *)(a1 + 32);
v4 = *(unsigned __int16 *)(a1 + 48);
v5 = (unsigned int *)(a1 + v3);
v6 = a1 + v3 + 40 * v4;
v7 = *(_DWORD *)(v6 - 20) + *(_DWORD *)(v6 - 24);
v8 = *(unsigned int *)(a1 + v3 + 40LL * *(unsigned __int16 *)(a1 + 50) + 16);
if ( a1 + v3 >= v6 )
goto LABEL_8;
v9 = 0;
do
{
if ( !memcmp((const void *)(a1 + v8 + *v5), ".debug", 6uLL) )
v9 = 1;
v5 += 10;
}
while ( v6 > (unsigned __int64)v5 );
if ( !v9 )
{
LABEL_8:
v10 = *(unsigned __int16 *)(a1 + 46) * (unsigned __int16)v4 + v3;
if ( v7 < v10 )
v7 = v10;
}
return (unsigned int)v7;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, unsigned __int64 a2)
{
__int64 v3; // r13
__int64 v4; // rbp
unsigned int *v5; // rax
unsigned __int64 v6; // r9
int v7; // er12
__int64 v8; // r10
int v9; // edx
int v10; // er13

if ( a2 <= 0x34 )
__assert_fail("size > sizeof(Elf32_Ehdr)", "elf_size.c", 0x38u, "elf_get_file_size");
v3 = *(unsigned int *)(a1 + 32);
v4 = *(unsigned __int16 *)(a1 + 48);
v5 = (unsigned int *)(a1 + v3);
v6 = a1 + v3 + 40 * v4;
v7 = *(_DWORD *)(v6 - 20) + *(_DWORD *)(v6 - 24);
v8 = *(unsigned int *)(a1 + v3 + 40LL * *(unsigned __int16 *)(a1 + 50) + 16);
if ( a1 + v3 >= v6 )
goto LABEL_8;
v9 = 0;
do
{
if ( !memcmp((const void *)(a1 + v8 + *v5), ".debug", 6uLL) )
v9 = 1;
v5 += 10;
}
while ( v6 > (unsigned __int64)v5 );
if ( !v9 )
{
LABEL_8:
v10 = *(unsigned __int16 *)(a1 + 46) * (unsigned __int16)v4 + v3;
if ( v7 < v10 )
v7 = v10;
}
return (unsigned int)v7;
}

original_code:
int elf_get_file_size(char *start_addr, size_t size)
{
    int fsize = 0, fsize2 = 0;
    Elf32_Ehdr *eh = (Elf32_Ehdr *)start_addr;
    Elf32_Shdr *sh= ((void *)0);
    Elf32_Shdr *sh_start = ((void *)0) ,*sh_end = ((void *)0);
    Elf32_Shdr *sh_shstrtab = ((void *)0);
    char *shstrtab = ((void *)0);
    int is_debug_elf = 0;
    ((void) sizeof ((size > sizeof(Elf32_Ehdr)) ? 1 : 0), __extension__ ({ if (size > sizeof(Elf32_Ehdr)) ; else __assert_fail ("size > sizeof(Elf32_Ehdr)", "elf_size.c", 56, __extension__ __PRETTY_FUNCTION__); }));
    sh_start = (Elf32_Shdr *)(start_addr + eh->e_shoff);
    sh_end = sh_start + eh->e_shnum;
    sh_shstrtab = sh_start + eh->e_shstrndx;
    shstrtab = start_addr + sh_shstrtab->sh_offset;
    for(sh = sh_start; sh < sh_end; ++sh) {
        if(0 == strncmp(&shstrtab[sh->sh_name], ".debug", strlen(".debug"))) {
            is_debug_elf = 1;
        }
    }
 sh = sh_end - 1;
 fsize = sh->sh_offset + sh->sh_size;
    if(is_debug_elf) {
  return fsize;
    }
    else {
        fsize2 = eh->e_shoff + eh->e_shnum * eh->e_shentsize;
  fsize = fsize > fsize2 ? fsize : fsize2;
    }
    return fsize;
}

canonical_original_code:
int elf_get_file_size(char *start_addr, unsigned long size)
{
    int fsize = 0; int fsize2 = 0;
    struct Elf32_Ehdr *eh = (Elf32_Ehdr *)start_addr;
    struct Elf32_Shdr *sh = ((void *)0);
    struct Elf32_Shdr *sh_start = ((void *)0); struct Elf32_Shdr *sh_end = ((void *)0);
    struct Elf32_Shdr *sh_shstrtab = ((void *)0);
    char *shstrtab = ((void *)0);
    int is_debug_elf = 0;
    ((void) sizeof ((size > sizeof(Elf32_Ehdr)) ? 1 : 0), __extension__ ({ if (size > sizeof(Elf32_Ehdr)) ; else __assert_fail ("size > sizeof(Elf32_Ehdr)", "elf_size.c", 56, __extension__ __PRETTY_FUNCTION__); }));
    sh_start = (struct Elf32_Shdr *)(start_addr + eh->e_shoff);
    sh_end = sh_start + eh->e_shnum;
    sh_shstrtab = sh_start + eh->e_shstrndx;
    shstrtab = start_addr + sh_shstrtab->sh_offset;
    for(sh = sh_start; sh < sh_end; ++sh) {
        if(0 == strncmp(&shstrtab[sh->sh_name], ".debug", strlen(".debug"))) {
            is_debug_elf = 1;
        }
    }
 sh = sh_end - 1;
 fsize = sh->sh_offset + sh->sh_size;
    if(is_debug_elf) {
  return fsize;
    }
    else {
        fsize2 = eh->e_shoff + eh->e_shnum * eh->e_shentsize;
  fsize = fsize > fsize2 ? fsize : fsize2;
    }
    return fsize;
}

variable_types:
size
unsigned long

variable_types: fsize int

variable_types: struct Elf32_Ehdr { unsigned char e_ident[-1]; unsigned short e_type; unsigned short e_machine; unsigned int e_version; unsigned int e_entry; unsigned int e_phoff; unsigned int e_shoff; unsigned int e_flags; unsigned short e_ehsize; unsigned short e_phentsize; unsigned short e_phnum; unsigned short e_shentsize; unsigned short e_shnum; unsigned short e_shstrndx; } *

variable_types: struct Elf32_Shdr { unsigned int sh_name; unsigned int sh_type; unsigned int sh_flags; unsigned int sh_addr; unsigned int sh_offset; unsigned int sh_size; unsigned int sh_link; unsigned int sh_info; unsigned int sh_addralign; unsigned int sh_entsize; } *

variable_types:
shstrtab
char *


decompiled_code:
__int64 __fastcall elf_read_elf_info(const char *a1)
{
int v1; // eax
int v2; // ebx
int v3; // er12
void *v4; // rax
void *v5; // rbp
__off_t v6; // r14
unsigned int v7; // er13
struct stat v9; // [rsp+0h] [rbp-B8h] BYREF

v1 = open(a1, 0);
if ( v1 < 0 )
fatal(0LL, 1, "Cannot open %s\n", a1);
v2 = v1;
v3 = fstat(v1, &v9);
if ( v3 < 0 )
fatal(0LL, 1, "Cannot stat %s\n", a1);
v4 = mmap(0LL, v9.st_size, 1, 2, v2, 0LL);
v5 = v4;
if ( v4 == (void *)-1LL )
fatal(0LL, 1, "Cannot mmap %s\n", a1);
v6 = v9.st_size;
v7 = elf_get_file_size((__int64)v4, v9.st_size);
munmap(v5, v6);
fprintf(stderr, "[DBG] elf file size %d\n", v7);
return (unsigned int)v3;
}

canonical_decompiled_code:
__int64 __fastcall func9(const char *a1)
{
int v1; // eax
int v2; // ebx
int v3; // er12
void *v4; // rax
void *v5; // rbp
__off_t v6; // r14
unsigned int v7; // er13
struct stat v9; // [rsp+0h] [rbp-B8h] BYREF

v1 = open(a1, 0);
if ( v1 < 0 )
func7(0LL, 1, "Cannot open %s\n", a1);
v2 = v1;
v3 = func12(v1, &v9);
if ( v3 < 0 )
func7(0LL, 1, "Cannot stat %s\n", a1);
v4 = mmap(0LL, v9.st_size, 1, 2, v2, 0LL);
v5 = v4;
if ( v4 == (void *)-1LL )
func7(0LL, 1, "Cannot mmap %s\n", a1);
v6 = v9.st_size;
v7 = func8((__int64)v4, v9.st_size);
munmap(v5, v6);
fprintf(stderr, "[DBG] elf file size %d\n", v7);
return (unsigned int)v3;
}

original_code:
int elf_read_elf_info(char *file)
{
    int ret = 0;
    int fsize = 0;
    int fd = -1;
    void *faddr = ((void *)0);
    struct stat st;
    char *elf_info = ((void *)0);
    if((fd = open(file, 00)) < 0) {
        fatal(((void *)0), ELF_ERR, "Cannot open %s\n", file);
    }
    else if((ret = fstat(fd, &st)) < 0) {
        fatal(((void *)0), ELF_ERR, "Cannot stat %s\n", file);
    }
    else if((faddr = mmap(((void *)0), st.st_size,
                0x1, 0x02,
                fd, 0)) == ((void *) -1)) {
        fatal(((void *)0), ELF_ERR, "Cannot mmap %s\n", file);
    }
    fsize = elf_get_file_size((char *)faddr, st.st_size);
    munmap(faddr, st.st_size);
 fprintf(stderr, "[DBG] ""elf file size %d\n", fsize);
    return ret;
}

variable_types:
file
char *

variable_types: ret int

variable_types: fd int

variable_types:
faddr
void *

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

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


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
elf_read_elf_info(*argv);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
func9(*argv);
return 0;
}

original_code:
int main(int argc, char *argv[])
{
 int ret = 0;
 elf_read_elf_info(argv[0]);
 return ret;
}

variable_types: argc int

variable_types:
argv
char *[]


original_code:
int kmp_table(char *W, int T[], int SIZE)
{
    int proper_suffix_length = 0;
    int T_idx = 2;
    ((void) sizeof ((SIZE >= 2) ? 1 : 0), __extension__ ({ if (SIZE >= 2) ; else __assert_fail ("SIZE >= 2", "kmp.c", 15, __extension__ __PRETTY_FUNCTION__); }));
    T[0] = -1;
    T[1] = 0;
    while(T_idx < SIZE) {
        if(W[T_idx - 1] == W[proper_suffix_length]) {
            ++proper_suffix_length;
            T[T_idx] = proper_suffix_length;
            ++T_idx;
        }
        else if(proper_suffix_length > 0) {
            proper_suffix_length = T[proper_suffix_length];
        }
        else{
            T[T_idx] = 0;
            ++T_idx;
        }
    }
    void dump(){
        int i = 0;
        printf("T:\n");
        for(i = 0; i < SIZE; ++i) {
            printf("%d ", T[i]);
        }
        printf("\n");
    }
    return 0;
}

variable_types:
W
char *

variable_types:
T
int []

variable_types: SIZE int

variable_types: i int


decompiled_code:
__int64 __fastcall kmp_search(const char *a1, const char *a2)
{
int v2; // er13
int v3; // er14
void *v4; // rsp
unsigned int v5; // ecx
unsigned int v6; // er8
int v7; // eax
unsigned int v8; // ecx
__int64 v10; // [rsp+0h] [rbp-40h] BYREF
size_t v11; // [rsp+8h] [rbp-38h]

v11 = strlen(a1);
v2 = v11;
v3 = strlen(a2);
v4 = alloca(4LL * v3);
kmp_table((__int64)a2, &v10, v3);
if ( (int)v11 > 0 )
{
v5 = 0;
v6 = 0;
v7 = 0;
while ( 1 )
{
if ( a1[v5] == a2[v7] )
{
if ( v3 == ++v7 )
return v6;
}
else
{
v7 = *((_DWORD *)&v10 + v7);
v8 = v5 - v7;
if ( v7 < 0 )
v7 = 0;
v6 = v8;
}
v5 = v7 + v6;
if ( (int)(v7 + v6) >= v2 )
return v6;
}
}
return 0;
}

canonical_decompiled_code:
__int64 __fastcall func8(const char *a1, const char *a2)
{
int v2; // er13
int v3; // er14
void *v4; // rsp
unsigned int v5; // ecx
unsigned int v6; // er8
int v7; // eax
unsigned int v8; // ecx
__int64 v10; // [rsp+0h] [rbp-40h] BYREF
size_t v11; // [rsp+8h] [rbp-38h]

v11 = strlen(a1);
v2 = v11;
v3 = strlen(a2);
v4 = alloca(4LL * v3);
func7((__int64)a2, &v10, v3);
if ( (int)v11 > 0 )
{
v5 = 0;
v6 = 0;
v7 = 0;
while ( 1 )
{
if ( a1[v5] == a2[v7] )
{
if ( v3 == ++v7 )
return v6;
}
else
{
v7 = *((_DWORD *)&v10 + v7);
v8 = v5 - v7;
if ( v7 < 0 )
v7 = 0;
v6 = v8;
}
v5 = v7 + v6;
if ( (int)(v7 + v6) >= v2 )
return v6;
}
}
return 0;
}

original_code:
int kmp_search(char *S, char *W)
{
    int S_length = strlen(S);
    int W_length = strlen(W);
    int T[W_length];
    int m = 0, i = 0;
    kmp_table(W, T, W_length);
    while(m + i < S_length) {
        if(S[m + i] == W[i]) {
            if(++i == W_length) {
                return m;
            }
            continue;
        }
        m = m + i - T[i];
        if(T[i] > -1) {
            i = T[i];
        }
        else {
            i = 0;
        }
    }
    return m;
}

canonical_original_code:
int kmp_search(char *S, char *W)
{
    int S_length = strlen(S);
    int W_length = strlen(W);
    int T[-1];
    int m = 0; int i = 0;
    kmp_table(W, T, W_length);
    while(m + i < S_length) {
        if(S[m + i] == W[i]) {
            if(++i == W_length) {
                return m;
            }
            continue;
        }
        m = m + i - T[i];
        if(T[i] > -1) {
            i = T[i];
        }
        else {
            i = 0;
        }
    }
    return m;
}

variable_types:
S
char *

variable_types:
T
int [-1]

variable_types: m int


decompiled_code:
__int64 __fastcall kmp_table_myself(__int64 a1, _QWORD *a2, int a3)
{
__int64 v4; // r8
__int64 v5; // rax
int v6; // edx
unsigned int v7; // er8
unsigned int *v8; // rbx
__int64 v9; // rbp

if ( a3 <= 1 )
__assert_fail("SIZE >= 2", "kmp.c", 0x57u, "kmp_table_myself");
*a2 = 0xFFFFFFFFLL;
if ( a3 == 2 )
{
v7 = -1;
}
else
{
v4 = a3;
v5 = 2LL;
v6 = 0;
do
{
while ( *(_BYTE *)(a1 + v5 - 1) != *(_BYTE *)(a1 + v6) )
{
*((_DWORD *)a2 + v5++) = 0;
v6 = 0;
if ( v4 == v5 )
goto LABEL_7;
}
*((_DWORD *)a2 + v5++) = ++v6;
}
while ( v4 != v5 );
LABEL_7:
v7 = *(_DWORD *)a2;
}
v8 = (unsigned int *)a2 + 1;
v9 = (__int64)a2 + 4 * (unsigned int)(a3 - 1) + 4;
while ( 1 )
{
printf("%d ", v7);
if ( v8 == (unsigned int *)v9 )
break;
v7 = *v8++;
}
putchar(10);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, _QWORD *a2, int a3)
{
__int64 v4; // r8
__int64 v5; // rax
int v6; // edx
unsigned int v7; // er8
unsigned int *v8; // rbx
__int64 v9; // rbp

if ( a3 <= 1 )
__assert_fail("SIZE >= 2", "kmp.c", 0x57u, "kmp_table_myself");
*a2 = 0xFFFFFFFFLL;
if ( a3 == 2 )
{
v7 = -1;
}
else
{
v4 = a3;
v5 = 2LL;
v6 = 0;
do
{
while ( *(_BYTE *)(a1 + v5 - 1) != *(_BYTE *)(a1 + v6) )
{
*((_DWORD *)a2 + v5++) = 0;
v6 = 0;
if ( v4 == v5 )
goto LABEL_7;
}
*((_DWORD *)a2 + v5++) = ++v6;
}
while ( v4 != v5 );
LABEL_7:
v7 = *(_DWORD *)a2;
}
v8 = (unsigned int *)a2 + 1;
v9 = (__int64)a2 + 4 * (unsigned int)(a3 - 1) + 4;
while ( 1 )
{
printf("%d ", v7);
if ( v8 == (unsigned int *)v9 )
break;
v7 = *v8++;
}
putchar(10);
return 0LL;
}

original_code:
int kmp_table_myself(char *W, int T[], int SIZE)
{
    int proper_suffix_length = 0;
    int T_idx = 2;
    ((void) sizeof ((SIZE >= 2) ? 1 : 0), __extension__ ({ if (SIZE >= 2) ; else __assert_fail ("SIZE >= 2", "kmp.c", 87, __extension__ __PRETTY_FUNCTION__); }));
    T[0] = -1;
    T[1] = 0;
    while(T_idx < SIZE) {
        if(W[T_idx - 1] == W[proper_suffix_length]) {
            ++proper_suffix_length;
            T[T_idx] = proper_suffix_length;
            ++T_idx;
        }
        else {
            proper_suffix_length = 0;
            T[T_idx] = 0;
            ++T_idx;
        }
    }
    {
        int i = 0;
        for(i = 0; i < SIZE; ++i) {
            printf("%d ", T[i]);
        }
        printf("\n");
    }
    return 0;
}


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

v3 = kmp_search("abc abcdab abcdabcdabde", "abcdabd", envp);
printf("pos %d\n", v3);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax

v3 = func8("abc abcdab abcdabcdabde", "abcdabd", envp);
printf("pos %d\n", v3);
return 0;
}

original_code:
int main()
{
    int pos = -1;
    pos = kmp_search("abc abcdab abcdabcdabde", "abcdabd");
    printf("pos %d\n",pos);
    return 0;
}

variable_types: pos int