decompiled_code:
void __fastcall clear_evtchn(unsigned int a1)
{
synch_clear_bit(a1, (volatile signed __int32 *)(hypervisor_shared_info + 2048LL));
}

canonical_decompiled_code:
void __fastcall func7(unsigned int a1)
{
func1(a1, (volatile signed __int32 *)(hypervisor_shared_info + 2048LL));
}

original_code:
static void clear_evtchn(uint32_t port)
{
    shared_info_t *s = hypervisor_shared_info;
    synch_clear_bit(port, &s->evtchn_pending[0]);
}

canonical_original_code:
static void clear_evtchn(unsigned int port)
{
    struct shared_info *s = hypervisor_shared_info;
    synch_clear_bit(port, &s->evtchn_pending[0]);
}

variable_types:
port
unsigned int

variable_types: struct shared_info { struct vcpu_info { unsigned char evtchn_upcall_pending; unsigned char evtchn_upcall_mask; unsigned long evtchn_pending_sel; struct arch_vcpu_info { unsigned long cr2; unsigned long pad; } arch; struct vcpu_time_info { unsigned int version; unsigned int pad0; unsigned long tsc_timestamp; unsigned long system_time; unsigned int tsc_to_system_mul; char tsc_shift; char pad1[3]; } time; } vcpu_info[32]; unsigned long evtchn_pending[-1]; unsigned long evtchn_mask[-1]; unsigned int wc_version; unsigned int wc_sec; unsigned int wc_nsec; unsigned int wc_sec_hi; struct arch_shared_info { unsigned long max_pfn; unsigned long pfn_to_mfn_frame_list_list; unsigned long nmi_reason; unsigned long p2m_cr3; unsigned long p2m_vaddr; unsigned long p2m_generation; } arch; } *

user_defined_types: struct shared_info { struct vcpu_info vcpu_info[32]; unsigned long evtchn_pending[-1]; unsigned long evtchn_mask[-1]; unsigned int wc_version; unsigned int wc_sec; unsigned int wc_nsec; unsigned int wc_sec_hi; struct arch_shared_info arch; }

user_defined_types: struct vcpu_info { unsigned char evtchn_upcall_pending; unsigned char evtchn_upcall_mask; unsigned long evtchn_pending_sel; struct arch_vcpu_info arch; struct vcpu_time_info time; }


repo: FurryFuttock/micro-pv

decompiled_code:
void __fastcall mask_evtchn(unsigned int a1)
{
synch_set_bit(a1, (volatile signed __int32 *)(hypervisor_shared_info + 2560LL));
}

canonical_decompiled_code:
void __fastcall func8(unsigned int a1)
{
func0(a1, (volatile signed __int32 *)(hypervisor_shared_info + 2560LL));
}

original_code:
static void mask_evtchn(uint32_t port)
{
    shared_info_t *s = hypervisor_shared_info;
    synch_set_bit(port, &s->evtchn_mask[0]);
}

canonical_original_code:
static void mask_evtchn(unsigned int port)
{
    struct shared_info *s = hypervisor_shared_info;
    synch_set_bit(port, &s->evtchn_mask[0]);
}

return_type: void

user_defined_types: struct arch_shared_info { unsigned long max_pfn; unsigned long pfn_to_mfn_frame_list_list; unsigned long nmi_reason; unsigned long p2m_cr3; unsigned long p2m_vaddr; unsigned long p2m_generation; }

user_defined_types: struct arch_vcpu_info { unsigned long cr2; unsigned long pad; }

user_defined_types: struct vcpu_time_info { unsigned int version; unsigned int pad0; unsigned long tsc_timestamp; unsigned long system_time; unsigned int tsc_to_system_mul; char tsc_shift; char pad1[3]; }


decompiled_code:
__int64 __fastcall default_handler(unsigned int a1)
{
return micropv_printk("src/xenevents.c", 97LL, "[Port %d] - event received", a1);
}

canonical_decompiled_code:
__int64 __fastcall func9(unsigned int a1)
{
return micropv_printk("src/xenevents.c", 97LL, "[Port %d] - event received", a1);
}

original_code:
static void default_handler(evtchn_port_t port, struct pt_regs *regs, void *ignore)
{
    micropv_printk("src/xenevents.c", 97,"[Port %d] - event received", port);
}

canonical_original_code:
static void default_handler(unsigned int port, struct pt_regs *regs, void *ignore)
{
    micropv_printk("src/xenevents.c", 97,"[Port %d] - event received", port);
}

variable_types: struct pt_regs { unsigned long r15; unsigned long r14; unsigned long r13; unsigned long r12; unsigned long bp; unsigned long bx; unsigned long r11; unsigned long r10; unsigned long r9; unsigned long r8; unsigned long ax; unsigned long cx; unsigned long dx; unsigned long si; unsigned long di; unsigned long orig_ax; unsigned long ip; unsigned long cs; unsigned long flags; unsigned long sp; unsigned long ss; } *

variable_types:
ignore
void *


decompiled_code:
__int64 __fastcall bind_event_handler(unsigned int a1, __int64 a2, __int64 a3)
{
__int64 v4; // rax
__int64 result; // rax

if ( a1 > 0x3FF )
{
micropv_printk("src/xenevents.c", 105LL, "ERROR: Invalid port %i", a1);
result = 0xFFFFFFFFLL;
}
else
{
if ( (__int64 (__fastcall *)(unsigned int))ev_actions[3 * a1] != default_handler )
micropv_printk("src/xenevents.c", 110LL, "WARN: Handler for port %d already registered, replacing", a1);
v4 = 3LL * a1;
qword_7C8[v4] = a3;
_mm_sfence();
ev_actions[v4] = a2;
result = a1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func14(unsigned int a1, __int64 a2, __int64 a3)
{
__int64 v4; // rax
__int64 result; // rax

if ( a1 > 0x3FF )
{
micropv_printk("src/xenevents.c", 105LL, "ERROR: Invalid port %i", a1);
result = 0xFFFFFFFFLL;
}
else
{
if ( (__int64 (__fastcall *)(unsigned int))ev_actions[3 * a1] != default_handler )
micropv_printk("src/xenevents.c", 110LL, "WARN: Handler for port %d already registered, replacing", a1);
v4 = 3LL * a1;
qword_7C8[v4] = a3;
_mm_sfence();
ev_actions[v4] = a2;
result = a1;
}
return result;
}

original_code:
evtchn_port_t bind_event_handler(evtchn_port_t port, evtchn_handler_t handler, void *data)
{
    if ((port < 0) || (port >= (1024)))
    {
        micropv_printk("src/xenevents.c", 105,"ERROR: Invalid port %i", port);
        return -1;
    }
    if (ev_actions[port].handler != default_handler)
        micropv_printk("src/xenevents.c", 110,"WARN: Handler for port %d already registered, replacing", port);
    ev_actions[port].data = data;
    __asm__ __volatile__ ("sfence" ::: "memory");
    ev_actions[port].handler = handler;
    return port;
}

canonical_original_code:
unsigned int bind_event_handler(unsigned int port, void (*handler)(unsigned int port, struct pt_regs *register_file, void *context), void *data)
{
    if ((port < 0) || (port >= (1024)))
    {
        micropv_printk("src/xenevents.c", 105,"ERROR: Invalid port %i", port);
        return -1;
    }
    if (ev_actions[port].handler != default_handler)
        micropv_printk("src/xenevents.c", 110,"WARN: Handler for port %d already registered, replacing", port);
    ev_actions[port].data = data;
    __asm__ __volatile__ ("sfence" ::: "memory");
    ev_actions[port].handler = handler;
    return port;
}

variable_types: void (*)(unsigned int port, struct pt_regs { unsigned long r15; unsigned long r14; unsigned long r13; unsigned long r12; unsigned long bp; unsigned long bx; unsigned long r11; unsigned long r10; unsigned long r9; unsigned long r8; unsigned long ax; unsigned long cx; unsigned long dx; unsigned long si; unsigned long di; unsigned long orig_ax; unsigned long ip; unsigned long cs; unsigned long flags; unsigned long sp; unsigned long ss; } *register_file, void *context)

variable_types:
data
void *

return_type: unsigned int


decompiled_code:
__int64 bind_virq()
{
unsigned int v0; // eax
__int64 v1; // rdx
__int64 v2; // rcx
__int64 v3; // r9
__int64 result; // rax
unsigned int v5; // [rsp+Ch] [rbp-Ch]

v0 = HYPERVISOR_event_channel_op(1);
if ( v0 )
{
micropv_printk("src/xenevents.c", 144LL, "Failed to bind virtual IRQ %d with rc=%d", v2, v0);
result = 0xFFFFFFFFLL;
}
else
{
bind_event_handler(v5, v3, v1);
result = v5;
}
return result;
}

canonical_decompiled_code:
__int64 func16()
{
unsigned int v0; // eax
__int64 v1; // rdx
__int64 v2; // rcx
__int64 v3; // r9
__int64 result; // rax
unsigned int v5; // [rsp+Ch] [rbp-Ch]

v0 = func6(1);
if ( v0 )
{
micropv_printk("src/xenevents.c", 144LL, "Failed to bind virtual IRQ %d with rc=%d", v2, v0);
result = 0xFFFFFFFFLL;
}
else
{
func14(v5, v3, v1);
result = v5;
}
return result;
}

original_code:
evtchn_port_t bind_virq(uint32_t virq, evtchn_handler_t handler, void *data)
{
    evtchn_bind_virq_t op;
    int rc;
    op.virq = virq;
    op.vcpu = 0;
    if ((rc = HYPERVISOR_event_channel_op(1, &op)) != 0)
    {
        micropv_printk("src/xenevents.c", 144,"Failed to bind virtual IRQ %d with rc=%d", virq, rc);
        return -1;
    }
    bind_event_handler(op.port, handler, data);
    return op.port;
}

canonical_original_code:
unsigned int bind_virq(unsigned int virq, void (*handler)(unsigned int port, struct pt_regs *register_file, void *context), void *data)
{
    struct evtchn_bind_virq op;
    int rc;
    op.virq = virq;
    op.vcpu = 0;
    if ((rc = HYPERVISOR_event_channel_op(1, &op)) != 0)
    {
        micropv_printk("src/xenevents.c", 144,"Failed to bind virtual IRQ %d with rc=%d", virq, rc);
        return -1;
    }
    bind_event_handler(op.port, handler, data);
    return op.port;
}

variable_types:
virq
unsigned int

variable_types: struct evtchn_bind_virq { unsigned int virq; unsigned int vcpu; unsigned int port; }

variable_types: rc int

user_defined_types: struct pt_regs { unsigned long r15; unsigned long r14; unsigned long r13; unsigned long r12; unsigned long bp; unsigned long bx; unsigned long r11; unsigned long r10; unsigned long r9; unsigned long r8; unsigned long ax; unsigned long cx; unsigned long dx; unsigned long si; unsigned long di; unsigned long orig_ax; unsigned long ip; unsigned long cs; unsigned long flags; unsigned long sp; unsigned long ss; }


decompiled_code:
__int64 force_evtchn_callback()
{
_BYTE *v0; // rbx
char v1; // bp
__int64 result; // rax

v0 = (_BYTE *)hypervisor_shared_info;
v1 = *(_BYTE *)(hypervisor_shared_info + 1LL);
if ( *hypervisor_shared_info )
{
do
{
v0[1] = 1;
result = do_hypervisor_callback(0LL);
v0[1] = v1;
}
while ( *v0 );
}
return result;
}

canonical_decompiled_code:
__int64 func18()
{
_BYTE *v0; // rbx
char v1; // bp
__int64 result; // rax

v0 = (_BYTE *)hypervisor_shared_info;
v1 = *(_BYTE *)(hypervisor_shared_info + 1LL);
if ( *hypervisor_shared_info )
{
do
{
v0[1] = 1;
result = func17(0LL);
v0[1] = v1;
}
while ( *v0 );
}
return result;
}

original_code:
static void force_evtchn_callback(void)
{
    int save;
    vcpu_info_t *vcpu;
    vcpu = &hypervisor_shared_info->vcpu_info[0];
    save = vcpu->evtchn_upcall_mask;
    while (vcpu->evtchn_upcall_pending)
    {
        vcpu->evtchn_upcall_mask = 1;
        __asm__ __volatile__("": : :"memory");
        do_hypervisor_callback(((void *)0));
        __asm__ __volatile__("": : :"memory");
        vcpu->evtchn_upcall_mask = save;
        __asm__ __volatile__("": : :"memory");
    };
}

canonical_original_code:
static void force_evtchn_callback()
{
    int save;
    struct vcpu_info *vcpu;
    vcpu = &hypervisor_shared_info->vcpu_info[0];
    save = vcpu->evtchn_upcall_mask;
    while (vcpu->evtchn_upcall_pending)
    {
        vcpu->evtchn_upcall_mask = 1;
        __asm__ __volatile__("": : :"memory");
        do_hypervisor_callback(((void *)0));
        __asm__ __volatile__("": : :"memory");
        vcpu->evtchn_upcall_mask = save;
        __asm__ __volatile__("": : :"memory");
    };
}

variable_types: save int

variable_types: struct vcpu_info { unsigned char evtchn_upcall_pending; unsigned char evtchn_upcall_mask; unsigned long evtchn_pending_sel; struct arch_vcpu_info { unsigned long cr2; unsigned long pad; } arch; struct vcpu_time_info { unsigned int version; unsigned int pad0; unsigned long tsc_timestamp; unsigned long system_time; unsigned int tsc_to_system_mul; char tsc_shift; char pad1[3]; } time; } *


decompiled_code:
__int64 __fastcall unmask_evtchn(unsigned int a1)
{
__int64 v1; // rbp
__int64 result; // rax
bool v3; // zf

v1 = hypervisor_shared_info;
micropv_printk("src/xenevents.c", 168LL, "unmask port %d", a1);
synch_clear_bit(a1, (volatile signed __int32 *)(v1 + 2560));
result = synch_var_test_bit(a1, (const signed __int32 *)(v1 + 2048));
if ( (_DWORD)result )
{
result = synch_test_and_set_bit(a1 >> 6, (volatile signed __int32 *)(v1 + 8));
if ( !(_DWORD)result )
{
v3 = *(_BYTE *)(v1 + 1) == 0;
*(_BYTE *)v1 = 1;
if ( v3 )
result = force_evtchn_callback();
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func19(unsigned int a1)
{
__int64 v1; // rbp
__int64 result; // rax
bool v3; // zf

v1 = hypervisor_shared_info;
micropv_printk("src/xenevents.c", 168LL, "unmask port %d", a1);
func1(a1, (volatile signed __int32 *)(v1 + 2560));
result = func3(a1, (const signed __int32 *)(v1 + 2048));
if ( (_DWORD)result )
{
result = func4(a1 >> 6, (volatile signed __int32 *)(v1 + 8));
if ( !(_DWORD)result )
{
v3 = *(_BYTE *)(v1 + 1) == 0;
*(_BYTE *)v1 = 1;
if ( v3 )
result = func18();
}
}
return result;
}

original_code:
static void unmask_evtchn(uint32_t port)
{
    shared_info_t *s = hypervisor_shared_info;
    vcpu_info_t *vcpu_info = &s->vcpu_info[0];
    micropv_printk("src/xenevents.c", 168,"unmask port %d", port);
    synch_clear_bit(port, &s->evtchn_mask[0]);
    if ((__builtin_constant_p(port) ? synch_const_test_bit((port),(&s->evtchn_pending[0])) : synch_var_test_bit((port),(&s->evtchn_pending[0]))) &&
        !synch_test_and_set_bit(port / (sizeof(unsigned long) * 8), &vcpu_info->evtchn_pending_sel))
    {
        vcpu_info->evtchn_upcall_pending = 1;
        if (!vcpu_info->evtchn_upcall_mask)
            force_evtchn_callback();
    }
}

canonical_original_code:
static void unmask_evtchn(unsigned int port)
{
    struct shared_info *s = hypervisor_shared_info;
    struct vcpu_info *vcpu_info = &s->vcpu_info[0];
    micropv_printk("src/xenevents.c", 168,"unmask port %d", port);
    synch_clear_bit(port, &s->evtchn_mask[0]);
    if ((__builtin_constant_p(port) ? synch_const_test_bit((port),(&s->evtchn_pending[0])) : synch_var_test_bit((port),(&s->evtchn_pending[0]))) &&
        !synch_test_and_set_bit(port / (sizeof(unsigned long) * 8), &vcpu_info->evtchn_pending_sel))
    {
        vcpu_info->evtchn_upcall_pending = 1;
        if (!vcpu_info->evtchn_upcall_mask)
            force_evtchn_callback();
    }
}


decompiled_code:
__int64 xenevents_init()
{
__int64 *v0; // rcx
unsigned int i; // edx

v0 = ev_actions;
for ( i = 0; i != 1024; mask_evtchn(i) )
*v0 = (__int64)default_handler;
return HYPERVISOR_set_callbacks_constprop_0_isra_0((__int64)&hypervisor_callback, (__int64)&failsafe_callback);
}

canonical_decompiled_code:
__int64 func20()
{
__int64 *v0; // rcx
unsigned int i; // edx

v0 = ev_actions;
for ( i = 0; i != 1024; func8(i) )
*v0 = (__int64)default_handler;
return func11((__int64)&hypervisor_callback, (__int64)&failsafe_callback);
}

original_code:
void xenevents_init(void)
{
    for (unsigned int i = 0; i < (1024); i++)
    {
        ev_actions[i].handler = default_handler;
        mask_evtchn(i);
    }
    HYPERVISOR_set_callbacks((unsigned long)hypervisor_callback, (unsigned long)failsafe_callback, 0);
}

canonical_original_code:
void xenevents_init()
{
    for (unsigned int i = 0; i < (1024); i++)
    {
        ev_actions[i].handler = default_handler;
        mask_evtchn(i);
    }
    HYPERVISOR_set_callbacks((unsigned long)hypervisor_callback, (unsigned long)failsafe_callback, 0);
}

variable_types:
i
unsigned int


decompiled_code:
__int64 __fastcall xenevents_bind_virq(__int64 a1, __int64 a2)
{
unsigned int v2; // eax
unsigned int v3; // er12

v2 = bind_virq(a1, a2, 0LL);
v3 = v2;
if ( v2 == -1 )
micropv_printk("src/xenevents.c", 263LL, "Error initialising VIRQ %i", (unsigned int)a1);
else
unmask_evtchn(v2);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func21(__int64 a1, __int64 a2)
{
unsigned int v2; // eax
unsigned int v3; // er12

v2 = func16(a1, a2, 0LL);
v3 = v2;
if ( v2 == -1 )
micropv_printk("src/xenevents.c", 263LL, "Error initialising VIRQ %i", (unsigned int)a1);
else
func19(v2);
return v3;
}

original_code:
evtchn_port_t xenevents_bind_virq(int virq, evtchn_handler_t handler)
{
    evtchn_port_t port = bind_virq(virq, handler, ((void *)0));
    if (port == -1)
    {
        micropv_printk("src/xenevents.c", 263,"Error initialising VIRQ %i", virq);
        return -1;
    }
    unmask_evtchn(port);
    return port;
}

canonical_original_code:
unsigned int xenevents_bind_virq(int virq, void (*handler)(unsigned int port, struct pt_regs *register_file, void *context))
{
    unsigned int port = bind_virq(virq, handler, ((void *)0));
    if (port == -1)
    {
        micropv_printk("src/xenevents.c", 263,"Error initialising VIRQ %i", virq);
        return -1;
    }
    unmask_evtchn(port);
    return port;
}

variable_types: virq int


decompiled_code:
__int64 __fastcall xenevents_bind_handler(unsigned int a1, __int64 a2)
{
unsigned int v2; // eax
unsigned int v3; // er12

v2 = bind_event_handler(a1, a2, 0LL);
v3 = v2;
if ( v2 == -1 )
micropv_printk("src/xenevents.c", 275LL, "Error initialising channel %i", a1);
else
unmask_evtchn(v2);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func22(unsigned int a1, __int64 a2)
{
unsigned int v2; // eax
unsigned int v3; // er12

v2 = func14(a1, a2, 0LL);
v3 = v2;
if ( v2 == -1 )
micropv_printk("src/xenevents.c", 275LL, "Error initialising channel %i", a1);
else
func19(v2);
return v3;
}

original_code:
evtchn_port_t xenevents_bind_handler(int channel, evtchn_handler_t handler)
{
    evtchn_port_t port = bind_event_handler(channel, handler, ((void *)0));
    if (-1 == port)
    {
        micropv_printk("src/xenevents.c", 275,"Error initialising channel %i", channel);
        return -1;
    }
    unmask_evtchn(port);
    return port;
}

canonical_original_code:
unsigned int xenevents_bind_handler(int channel, void (*handler)(unsigned int port, struct pt_regs *register_file, void *context))
{
    unsigned int port = bind_event_handler(channel, handler, ((void *)0));
    if (-1 == port)
    {
        micropv_printk("src/xenevents.c", 275,"Error initialising channel %i", channel);
        return -1;
    }
    unmask_evtchn(port);
    return port;
}

variable_types: channel int


decompiled_code:
__int64 xenevents_bind_interdomain_channel()
{
unsigned int v0; // eax
_DWORD *v1; // rdx
unsigned int v2; // er12
int v4; // [rsp+Ch] [rbp-Ch]

v0 = HYPERVISOR_event_channel_op(0);
v2 = v0;
if ( v0 )
micropv_printk("src/xenevents.c", 297LL, "ERROR: xenevents_alloc_channel failed with rc=%d", v0);
else
*v1 = v4;
return v2;
}

canonical_decompiled_code:
__int64 func24()
{
unsigned int v0; // eax
_DWORD *v1; // rdx
unsigned int v2; // er12
int v4; // [rsp+Ch] [rbp-Ch]

v0 = func6(0);
v2 = v0;
if ( v0 )
micropv_printk("src/xenevents.c", 297LL, "ERROR: xenevents_alloc_channel failed with rc=%d", v0);
else
*v1 = v4;
return v2;
}

original_code:
evtchn_port_t xenevents_bind_interdomain_channel(int remote_dom, int remote_port, int *local_port)
{
    int rc;
    evtchn_bind_interdomain_t op;
    op.remote_dom = remote_dom;
    op.remote_port = remote_port;
    rc = HYPERVISOR_event_channel_op(0, &op);
    if (rc)
        micropv_printk("src/xenevents.c", 297,"ERROR: xenevents_alloc_channel failed with rc=%d", rc);
    else
        *local_port = op.local_port;
    return rc;
}

canonical_original_code:
unsigned int xenevents_bind_interdomain_channel(int remote_dom, int remote_port, int *local_port)
{
    int rc;
    struct evtchn_bind_interdomain op;
    op.remote_dom = remote_dom;
    op.remote_port = remote_port;
    rc = HYPERVISOR_event_channel_op(0, &op);
    if (rc)
        micropv_printk("src/xenevents.c", 297,"ERROR: xenevents_alloc_channel failed with rc=%d", rc);
    else
        *local_port = op.local_port;
    return rc;
}

variable_types: struct evtchn_bind_interdomain { unsigned short remote_dom; unsigned int remote_port; unsigned int local_port; }


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

if ( a1 > 0x3FF )
{
micropv_printk("src/xenevents.c", 339LL, "ERROR: Invalid port %i", a1);
result = 0xFFFFFFFFLL;
}
else
{
result = HYPERVISOR_event_channel_op(4);
if ( (_DWORD)result )
{
micropv_printk("src/xenevents.c", 351LL, "Failed to send event %i", (unsigned int)result);
result = 0xFFFFFFFFLL;
}
}
return result;
}

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

if ( a1 > 0x3FF )
{
micropv_printk("src/xenevents.c", 339LL, "ERROR: Invalid port %i", a1);
result = 0xFFFFFFFFLL;
}
else
{
result = func6(4);
if ( (_DWORD)result )
{
micropv_printk("src/xenevents.c", 351LL, "Failed to send event %i", (unsigned int)result);
result = 0xFFFFFFFFLL;
}
}
return result;
}

original_code:
int micropv_fire_event(uint32_t event_port)
{
    if ((event_port < 0) || (event_port >= (1024)))
    {
        micropv_printk("src/xenevents.c", 339,"ERROR: Invalid port %i", event_port);
        return -1;
    }
    evtchn_send_t op =
    {
        .port = event_port
    };
    int rc;
    if ((rc = HYPERVISOR_event_channel_op(4, &op)) != 0)
    {
        micropv_printk("src/xenevents.c", 351,"Failed to send event %i", rc);
        return -1;
    }
    return 0;
}

canonical_original_code:
int micropv_fire_event(unsigned int event_port)
{
    if ((event_port < 0) || (event_port >= (1024)))
    {
        micropv_printk("src/xenevents.c", 339,"ERROR: Invalid port %i", event_port);
        return -1;
    }
    struct evtchn_send op = {
        .port = event_port
    };
    int rc;
    if ((rc = HYPERVISOR_event_channel_op(4, &op)) != 0)
    {
        micropv_printk("src/xenevents.c", 351,"Failed to send event %i", rc);
        return -1;
    }
    return 0;
}

variable_types: struct evtchn_send { unsigned int port; }

return_type: int


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

result = hypervisor_shared_info;
*(_BYTE *)(hypervisor_shared_info + 1LL) = 1;
return result;
}

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

result = hypervisor_shared_info;
*(_BYTE *)(hypervisor_shared_info + 1LL) = 1;
return result;
}

original_code:
void micropv_interrupt_disable(void)
{
    __asm__ __volatile__("": : :"memory");
    hypervisor_shared_info->vcpu_info[0].evtchn_upcall_mask = 1;
    __asm__ __volatile__("": : :"memory");
}

canonical_original_code:
void micropv_interrupt_disable()
{
    __asm__ __volatile__("": : :"memory");
    hypervisor_shared_info->vcpu_info[0].evtchn_upcall_mask = 1;
    __asm__ __volatile__("": : :"memory");
}


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

*(_BYTE *)(hypervisor_shared_info + 1LL) = 0;
result = hypervisor_shared_info;
if ( *hypervisor_shared_info )
result = HYPERVISOR_xen_version_constprop_0_isra_0();
return result;
}

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

*(_BYTE *)(hypervisor_shared_info + 1LL) = 0;
result = hypervisor_shared_info;
if ( *hypervisor_shared_info )
result = func12();
return result;
}

original_code:
void micropv_interrupt_enable(void)
{
    __asm__ __volatile__("": : :"memory");
    hypervisor_shared_info->vcpu_info[0].evtchn_upcall_mask = 0;
    __asm__ __volatile__("": : :"memory");
    if (hypervisor_shared_info->vcpu_info[0].evtchn_upcall_pending)
        HYPERVISOR_xen_version(0, ((void *)0));
}

canonical_original_code:
void micropv_interrupt_enable()
{
    __asm__ __volatile__("": : :"memory");
    hypervisor_shared_info->vcpu_info[0].evtchn_upcall_mask = 0;
    __asm__ __volatile__("": : :"memory");
    if (hypervisor_shared_info->vcpu_info[0].evtchn_upcall_pending)
        HYPERVISOR_xen_version(0, ((void *)0));
}


decompiled_code:
__int64 xenevents_alloc_channel()
{
unsigned int v0; // eax
_DWORD *v1; // rdx
unsigned int v2; // er12
int v4; // [rsp+Ch] [rbp-Ch]

v0 = HYPERVISOR_event_channel_op(6);
v2 = v0;
if ( v0 )
micropv_printk("src/xenevents.c", 387LL, "ERROR: xenevents_alloc_channel failed with rc=%d", v0);
else
*v1 = v4;
return v2;
}

canonical_decompiled_code:
__int64 func28()
{
unsigned int v0; // eax
_DWORD *v1; // rdx
unsigned int v2; // er12
int v4; // [rsp+Ch] [rbp-Ch]

v0 = func6(6);
v2 = v0;
if ( v0 )
micropv_printk("src/xenevents.c", 387LL, "ERROR: xenevents_alloc_channel failed with rc=%d", v0);
else
*v1 = v4;
return v2;
}

original_code:
int xenevents_alloc_channel(int remote_dom, int *port)
{
    int rc;
    evtchn_alloc_unbound_t op;
    op.dom = 0x7FF0U;
    op.remote_dom = remote_dom;
    rc = HYPERVISOR_event_channel_op(6, &op);
    if (rc)
        micropv_printk("src/xenevents.c", 387,"ERROR: xenevents_alloc_channel failed with rc=%d", rc);
    else
        *port = op.port;
    return rc;
}

canonical_original_code:
int xenevents_alloc_channel(int remote_dom, int *port)
{
    int rc;
    struct evtchn_alloc_unbound op;
    op.dom = 0x7FF0U;
    op.remote_dom = remote_dom;
    rc = HYPERVISOR_event_channel_op(6, &op);
    if (rc)
        micropv_printk("src/xenevents.c", 387,"ERROR: xenevents_alloc_channel failed with rc=%d", rc);
    else
        *port = op.port;
    return rc;
}

variable_types:
port
int *

variable_types: struct evtchn_alloc_unbound { unsigned short dom; unsigned int port; }


decompiled_code:
__int64 __fastcall xenevents_create_event(unsigned int *a1, __int64 a2)
{
unsigned int v2; // er12
unsigned int v4; // [rsp+8h] [rbp-20h] BYREF
unsigned int v5[7]; // [rsp+Ch] [rbp-1Ch] BYREF

if ( (unsigned int)xenevents_alloc_channel(32752LL, &v4) )
{
micropv_printk("src/xenevents.c", 308LL, "Failed to create event channel");
v2 = -1;
}
else
{
v2 = 0;
if ( (unsigned int)xenevents_bind_interdomain_channel(32752LL, v4, v5) )
{
micropv_printk("src/xenevents.c", 315LL, "Failed to bind to interdomain channel");
v2 = -1;
}
else if ( (unsigned int)xenevents_bind_handler(v5[0], a2) == -1 )
{
micropv_printk("src/xenevents.c", 323LL, "Failed to bind event");
v2 = -1;
}
else
{
*a1 = v4;
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func29(unsigned int *a1, __int64 a2)
{
unsigned int v2; // er12
unsigned int v4; // [rsp+8h] [rbp-20h] BYREF
unsigned int v5[7]; // [rsp+Ch] [rbp-1Ch] BYREF

if ( (unsigned int)func28(32752LL, &v4) )
{
micropv_printk("src/xenevents.c", 308LL, "Failed to create event channel");
v2 = -1;
}
else
{
v2 = 0;
if ( (unsigned int)func24(32752LL, v4, v5) )
{
micropv_printk("src/xenevents.c", 315LL, "Failed to bind to interdomain channel");
v2 = -1;
}
else if ( (unsigned int)func22(v5[0], a2) == -1 )
{
micropv_printk("src/xenevents.c", 323LL, "Failed to bind event");
v2 = -1;
}
else
{
*a1 = v4;
}
}
return v2;
}

original_code:
int xenevents_create_event(evtchn_port_t *event_port, evtchn_handler_t event_handler)
{
    int remote_port;
    if (xenevents_alloc_channel(0x7FF0U, &remote_port))
    {
        micropv_printk("src/xenevents.c", 308,"Failed to create event channel");
        return -1;
    }
    int local_port;
    if (xenevents_bind_interdomain_channel(0x7FF0U, remote_port, &local_port))
    {
        micropv_printk("src/xenevents.c", 315,"Failed to bind to interdomain channel");
        return -1;
    }
    int local_channel;
    local_channel = xenevents_bind_handler(local_port, event_handler);
    if (-1 == local_channel)
    {
        micropv_printk("src/xenevents.c", 323,"Failed to bind event");
        return -1;
    }
    *event_port = remote_port;
    return 0;
}

canonical_original_code:
int xenevents_create_event(unsigned int *event_port, void (*event_handler)(unsigned int port, struct pt_regs *register_file, void *context))
{
    int remote_port;
    if (xenevents_alloc_channel(0x7FF0U, &remote_port))
    {
        micropv_printk("src/xenevents.c", 308,"Failed to create event channel");
        return -1;
    }
    int local_port;
    if (xenevents_bind_interdomain_channel(0x7FF0U, remote_port, &local_port))
    {
        micropv_printk("src/xenevents.c", 315,"Failed to bind to interdomain channel");
        return -1;
    }
    int local_channel;
    local_channel = xenevents_bind_handler(local_port, event_handler);
    if (-1 == local_channel)
    {
        micropv_printk("src/xenevents.c", 323,"Failed to bind event");
        return -1;
    }
    *event_port = remote_port;
    return 0;
}


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

max_pfn = hypervisor_start_info[4];
result = hypervisor_start_info[13];
phys_to_machine_mapping = result;
return result;
}

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

max_pfn = hypervisor_start_info[4];
result = hypervisor_start_info[13];
phys_to_machine_mapping = result;
return result;
}

original_code:
void xenmmu_init(void)
{
    max_pfn = hypervisor_start_info.nr_pages;
    phys_to_machine_mapping = (uint64_t *)hypervisor_start_info.mfn_list;
}

canonical_original_code:
void xenmmu_init()
{
    max_pfn = hypervisor_start_info.nr_pages;
    phys_to_machine_mapping = (unsigned long *)hypervisor_start_info.mfn_list;
}


decompiled_code:
__int64 __fastcall micropv_remap_page(__int64 a1, __int64 a2, __int64 a3, int a4)
{
unsigned __int64 v5; // r9
int v6; // er10
int v7; // ebp
__int64 v8; // r11
__int64 v9; // rcx
__int64 v10; // r13
__int64 v11; // rcx
int v12; // er10
unsigned int v13; // er12
__int64 result; // rax

v5 = (unsigned __int64)(a3 + 4095) >> 12;
v6 = 0;
v7 = v5;
v8 = a4 == 0 ? 39 : 37;
if ( (int)v5 > 0 )
{
while ( 1 )
{
v9 = v6 << 12;
v10 = v9 + a2;
v13 = HYPERVISOR_update_va_mapping_constprop_0(a1 + v9, v8 | (v9 + a2));
if ( v13 )
break;
v6 = v12 + 1;
if ( v7 == v6 )
goto LABEL_6;
}
micropv_printk("src/xenmmu.c", 90LL, "FAIL mapping physical address 0x%lx to machine address 0x%lx", v11, v10);
micropv_printk("src/xenmmu.c", 91LL, "HYPERVISOR_update_va_mapping returns %i", v13);
result = 0LL;
}
else
{
LABEL_6:
micropv_printk(
"src/xenmmu.c",
95LL,
"Physical address 0x%lx mapped to machine address 0x%lx for a length of %i pages",
a1,
a2,
v5);
result = a1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2, __int64 a3, int a4)
{
unsigned __int64 v5; // r9
int v6; // er10
int v7; // ebp
__int64 v8; // r11
__int64 v9; // rcx
__int64 v10; // r13
__int64 v11; // rcx
int v12; // er10
unsigned int v13; // er12
__int64 result; // rax

v5 = (unsigned __int64)(a3 + 4095) >> 12;
v6 = 0;
v7 = v5;
v8 = a4 == 0 ? 39 : 37;
if ( (int)v5 > 0 )
{
while ( 1 )
{
v9 = v6 << 12;
v10 = v9 + a2;
v13 = func0(a1 + v9, v8 | (v9 + a2));
if ( v13 )
break;
v6 = v12 + 1;
if ( v7 == v6 )
goto LABEL_6;
}
micropv_printk("src/xenmmu.c", 90LL, "FAIL mapping physical address 0x%lx to machine address 0x%lx", v11, v10);
micropv_printk("src/xenmmu.c", 91LL, "HYPERVISOR_update_va_mapping returns %i", v13);
result = 0LL;
}
else
{
LABEL_6:
micropv_printk(
"src/xenmmu.c",
95LL,
"Physical address 0x%lx mapped to machine address 0x%lx for a length of %i pages",
a1,
a2,
v5);
result = a1;
}
return result;
}

original_code:
void *micropv_remap_page(uint64_t physical_address, uint64_t machine_address, size_t size, int readonly)
{
    int pte_type = readonly ? (0x001ULL|0x020ULL|0x004ULL) : (0x001ULL|0x002ULL|0x020ULL|0x004ULL);
    int pages = (size + (1UL << 12) - 1) >> 12;
    int page;
    for (page = 0; page < pages; page++)
    {
        int rc = HYPERVISOR_update_va_mapping(physical_address + (page << 12), ((pte_t) { ((machine_address + (page << 12)) | pte_type) } ), (2UL<<0));
        if (rc)
        {
            micropv_printk("src/xenmmu.c", 90,"FAIL mapping physical address 0x%lx to machine address 0x%lx", physical_address + (page << 12), machine_address + (page << 12));
            micropv_printk("src/xenmmu.c", 91,"HYPERVISOR_update_va_mapping returns %i", rc);
            return ((void *)0);
        }
    }
    micropv_printk("src/xenmmu.c", 95,"Physical address 0x%lx mapped to machine address 0x%lx for a length of %i pages", physical_address, machine_address, pages);
    return (void*)physical_address;
}

canonical_original_code:
void *micropv_remap_page(unsigned long physical_address, unsigned long machine_address, unsigned long size, int readonly)
{
    int pte_type = readonly ? (0x001ULL|0x020ULL|0x004ULL) : (0x001ULL|0x002ULL|0x020ULL|0x004ULL);
    int pages = (size + (1UL << 12) - 1) >> 12;
    int page;
    for (page = 0; page < pages; page++)
    {
        int rc = HYPERVISOR_update_va_mapping(physical_address + (page << 12), ((pte_t) { ((machine_address + (page << 12)) | pte_type) } ), (2UL<<0));
        if (rc)
        {
            micropv_printk("src/xenmmu.c", 90,"FAIL mapping physical address 0x%lx to machine address 0x%lx", physical_address + (page << 12), machine_address + (page << 12));
            micropv_printk("src/xenmmu.c", 91,"HYPERVISOR_update_va_mapping returns %i", rc);
            return ((void *)0);
        }
    }
    micropv_printk("src/xenmmu.c", 95,"Physical address 0x%lx mapped to machine address 0x%lx for a length of %i pages", physical_address, machine_address, pages);
    return (void *)physical_address;
}

variable_types:
size
unsigned long

variable_types: readonly int

variable_types: pages int

variable_types: page int

return_type: void *


original_code:
void *xenmmu_map_frames(uint64_t mfn[], size_t mfn_size, int readonly)
{
    uint64_t physical_address = max_pfn << 12;
    void *physical_ptr = (void *)physical_address;
    for (int fn = 0; fn < mfn_size; fn++, max_pfn++, physical_address += (1UL << 12))
    {
        micropv_remap_page(physical_address, mfn[fn] << 12, (1UL << 12), readonly);
    }
    return physical_ptr;
}

canonical_original_code:
void *xenmmu_map_frames(unsigned long mfn[], unsigned long mfn_size, int readonly)
{
    unsigned long physical_address = max_pfn << 12;
    void *physical_ptr = (void *)physical_address;
    for (int fn = 0; fn < mfn_size; fn++, max_pfn++, physical_address += (1UL << 12))
    {
        micropv_remap_page(physical_address, mfn[fn] << 12, (1UL << 12), readonly);
    }
    return physical_ptr;
}

variable_types:
mfn
unsigned long []

variable_types: fn int


decompiled_code:
__int64 __fastcall micropv_virtual_to_machine_address(unsigned __int64 a1)
{
return a1 & 0xFFF | (*(_QWORD *)(phys_to_machine_mapping + 8 * (a1 >> 12)) << 12);
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned __int64 a1)
{
return a1 & 0xFFF | (*(_QWORD *)(phys_to_machine_mapping + 8 * (a1 >> 12)) << 12);
}

original_code:
uint64_t micropv_virtual_to_machine_address(uint64_t virtual_address)
{
    uint64_t offset = virtual_address & ((1 << 12) - 1);
    uint64_t physical_address = ((unsigned long)(virtual_address)-((unsigned long)0));
    uint64_t physical_frame_number = ((physical_address) >> 12);
    uint64_t machine_frame_number = (phys_to_machine_mapping[(physical_frame_number)]);
    return (machine_frame_number << 12) | offset;
}

canonical_original_code:
unsigned long micropv_virtual_to_machine_address(unsigned long virtual_address)
{
    unsigned long offset = virtual_address & ((1 << 12) - 1);
    unsigned long physical_address = ((unsigned long)(virtual_address)-((unsigned long)0));
    unsigned long physical_frame_number = ((physical_address) >> 12);
    unsigned long machine_frame_number = (phys_to_machine_mapping[(physical_frame_number)]);
    return (machine_frame_number << 12) | offset;
}

variable_types:
offset
unsigned long

return_type: unsigned long


decompiled_code:
__int64 __fastcall micropv_machine_to_virtual_address(unsigned __int64 a1)
{
return a1 & 0xFFF | (*(_QWORD *)(8 * (a1 >> 12) - 0x800000000000LL) << 12);
}

original_code:
uint64_t micropv_machine_to_virtual_address(uint64_t machine_address)
{
    uint64_t offset = machine_address & ((1 << 12) - 1);
    uint64_t machine_frame_number = machine_address >> 12;
    uint64_t physical_frame_number = (((unsigned long *)0xFFFF800000000000UL)[(machine_frame_number)]);
    uint64_t physical_address = physical_frame_number << 12;
    uint64_t virtual_address = (uint64_t)((void *)((unsigned long)(physical_address)+((unsigned long)0)));
    return virtual_address | offset;
}

canonical_original_code:
unsigned long micropv_machine_to_virtual_address(unsigned long machine_address)
{
    unsigned long offset = machine_address & ((1 << 12) - 1);
    unsigned long machine_frame_number = machine_address >> 12;
    unsigned long physical_frame_number = (((unsigned long *)0xFFFF800000000000UL)[(machine_frame_number)]);
    unsigned long physical_address = physical_frame_number << 12;
    unsigned long virtual_address = (uint64_t)((void *)((unsigned long)(physical_address)+((unsigned long)0)));
    return virtual_address | offset;
}


decompiled_code:
__int64 xenconsole_interface()
{
return *(_QWORD *)(8LL * hypervisor_start_info[9] - 0x800000000000LL) << 12;
}

canonical_decompiled_code:
__int64 func0()
{
return *(_QWORD *)(8LL * hypervisor_start_info[9] - 0x800000000000LL) << 12;
}

original_code:
static inline struct xencons_interface *xenconsole_interface(void)
{
    return (((void *)((unsigned long)((((unsigned long *)0xFFFF800000000000UL)[(hypervisor_start_info.console.domU.mfn)]) << 12)+((unsigned long)0))));
}

canonical_original_code:
static inline struct xencons_interface *xenconsole_interface()
{
    return (((void *)((unsigned long)((((unsigned long *)0xFFFF800000000000UL)[(hypervisor_start_info.console.domU.mfn)]) << 12)+((unsigned long)0))));
}

return_type: struct xencons_interface { char in[1024]; char out[2048]; unsigned int in_cons; unsigned int out_cons; } *


decompiled_code:
__int64 xenconsole_event()
{
return hypervisor_start_info[20];
}

canonical_decompiled_code:
__int64 func1()
{
return hypervisor_start_info[20];
}

original_code:
static inline evtchn_port_t xenconsole_event(void)
{
    return hypervisor_start_info.console.domU.evtchn;
}

canonical_original_code:
static inline unsigned int xenconsole_event()
{
    return hypervisor_start_info.console.domU.evtchn;
}


original_code:
static int xenconsole_ring_send_no_notify(struct xencons_interface *ring, const char *data, unsigned len)
{
    int sent = 0;
    XENCONS_RING_IDX cons, prod;
    cons = ring->out_cons;
    prod = ring->out_prod;
    __asm__ __volatile__ ("mfence":::"memory");
    while ((sent < len) && ((prod - cons) < sizeof(ring->out)))
            ring->out[((prod++) & (sizeof(ring->out)-1))] = data[sent++];
    __asm__ __volatile__ ("sfence" ::: "memory");
    ring->out_prod = prod;
    return sent;
}

canonical_original_code:
static int xenconsole_ring_send_no_notify(struct xencons_interface *ring, char *data, unsigned int len)
{
    int sent = 0;
    unsigned int cons; unsigned int prod;
    cons = ring->out_cons;
    prod = ring->out_prod;
    __asm__ __volatile__ ("mfence":::"memory");
    while ((sent < len) && ((prod - cons) < sizeof(ring->out)))
            ring->out[((prod++) & (sizeof(ring->out)-1))] = data[sent++];
    __asm__ __volatile__ ("sfence" ::: "memory");
    ring->out_prod = prod;
    return sent;
}

variable_types: struct xencons_interface { char in[1024]; char out[2048]; unsigned int in_cons; unsigned int out_cons; } *

variable_types:
data
char *

variable_types:
len
unsigned int

variable_types: sent int

variable_types:
cons
unsigned int

variable_types:
prod
unsigned int


decompiled_code:
void xenconsole_event_handler()
{
;
}

canonical_decompiled_code:
void func3()
{
;
}

original_code:
static void xenconsole_event_handler(evtchn_port_t port, struct pt_regs *regs, void *data)
{
}

canonical_original_code:
static void xenconsole_event_handler(unsigned int port, struct pt_regs *regs, void *data)
{
}

variable_types:
port
unsigned int

variable_types: struct pt_regs { unsigned long r15; unsigned long r14; unsigned long r13; unsigned long r12; unsigned long bp; unsigned long bx; unsigned long r11; unsigned long r10; unsigned long r9; unsigned long r8; unsigned long ax; unsigned long cx; unsigned long dx; unsigned long si; unsigned long di; unsigned long orig_ax; unsigned long ip; unsigned long cs; unsigned long flags; unsigned long sp; unsigned long ss; } *

variable_types:
data
void *


decompiled_code:
__int64 __fastcall xenconsole_ring_send(__int64 a1, int a2, __int64 a3, unsigned int a4)
{
unsigned int v4; // er12

v4 = 0;
do
{
v4 += xenconsole_ring_send_no_notify(a1, a3, a4);
xenevents_notify_remote_via_evtchn_isra_0(a2);
}
while ( v4 < a4 );
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, int a2, __int64 a3, unsigned int a4)
{
unsigned int v4; // er12

v4 = 0;
do
{
v4 += func2(a1, a3, a4);
func5(a2);
}
while ( v4 < a4 );
return v4;
}

original_code:
static int xenconsole_ring_send(struct xencons_interface *ring, evtchn_port_t port, const char *data, unsigned len)
{
    int sent = 0;
    do
    {
        sent += xenconsole_ring_send_no_notify(ring, data, len);
        xenevents_notify_remote_via_evtchn(port);
    }
    while (sent < len);
    return sent;
}

canonical_original_code:
static int xenconsole_ring_send(struct xencons_interface *ring, unsigned int port, char *data, unsigned int len)
{
    int sent = 0;
    do
    {
        sent += xenconsole_ring_send_no_notify(ring, data, len);
        xenevents_notify_remote_via_evtchn(port);
    }
    while (sent < len);
    return sent;
}

user_defined_types: struct xencons_interface { char in[1024]; char out[2048]; unsigned int in_cons; unsigned int out_cons; }


decompiled_code:
__int64 __fastcall micropv_console_write(__int64 a1, __int64 a2)
{
unsigned int v2; // eax
int v4; // esi
__int64 v5; // rdx
__int64 v6; // rax
unsigned int v7; // ecx
__int64 v8; // r8

v2 = xenconsole_event();
v4 = v2;
v6 = xenconsole_interface(a1, v2, v5, a2, a1);
return xenconsole_ring_send(v6, v4, v8, v7);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int64 a2)
{
unsigned int v2; // eax
int v4; // esi
__int64 v5; // rdx
__int64 v6; // rax
unsigned int v7; // ecx
__int64 v8; // r8

v2 = func1();
v4 = v2;
v6 = func0(a1, v2, v5, a2, a1);
return func6(v6, v4, v8, v7);
}

original_code:
int micropv_console_write(const void *ptr, size_t len)
{
    return xenconsole_ring_send(xenconsole_interface(), xenconsole_event(), ptr, len);
}

canonical_original_code:
int micropv_console_write(void *ptr, unsigned long len)
{
    return xenconsole_ring_send(xenconsole_interface(), xenconsole_event(), ptr, len);
}

variable_types:
ptr
void *

variable_types:
len
unsigned long


decompiled_code:
__int64 __fastcall micropv_console_write_available(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5)
{
__int64 v5; // rax

v5 = xenconsole_interface(a1, a2, a3, a4, a5);
return (*(_DWORD *)(v5 + 3084) - 1 - *(_DWORD *)(v5 + 3080)) & 0x7FF;
}

original_code:
int micropv_console_write_available()
{
    struct xencons_interface *ring = xenconsole_interface();
    return (ring->out_prod - ring->out_cons - 1) & (sizeof(ring->out) - 1);
}


decompiled_code:
__int64 xenconsole_printf(__int64 a1, ...)
{
int v1; // eax
int v2; // er12
signed __int64 v3; // rsi
void *v4; // rsp
__int64 v6; // [rsp+0h] [rbp-F0h] BYREF
gcc_va_list va; // [rsp+8h] [rbp-E8h] BYREF

va_start(va, a1);
v1 = pvsnprintf(0LL, 0LL, a1, va);
v2 = v1;
if ( v1 > 0 )
{
v3 = v1 + 1;
v4 = alloca(v3);
va_end(va);
va_start(va, a1);
v2 = pvsnprintf(&v6, v3, a1, va);
micropv_console_write((__int64)&v6, v2);
}
return (unsigned int)v2;
}

canonical_decompiled_code:
__int64 func9(__int64 a1, ...)
{
int v1; // eax
int v2; // er12
signed __int64 v3; // rsi
void *v4; // rsp
__int64 v6; // [rsp+0h] [rbp-F0h] BYREF
gcc_va_list va; // [rsp+8h] [rbp-E8h] BYREF

va_start(va, a1);
v1 = pvsnprintf(0LL, 0LL, a1, va);
v2 = v1;
if ( v1 > 0 )
{
v3 = v1 + 1;
v4 = alloca(v3);
va_end(va);
va_start(va, a1);
v2 = pvsnprintf(&v6, v3, a1, va);
func7((__int64)&v6, v2);
}
return (unsigned int)v2;
}

original_code:
int xenconsole_printf(const char *format, ...)
{
    va_list args;
    int length;
    __builtin_va_start(args,format);
    length = pvsnprintf(((void *)0), 0, format, args);
    __builtin_va_end(args);
    if (length > 0)
    {
        char message[length + 1];
        __builtin_va_start(args,format);
        length = pvsnprintf(message, length + 1, format, args);
        __builtin_va_end(args);
        micropv_console_write(message, length);
    }
    return length;
}

canonical_original_code:
int xenconsole_printf(char *format, ...)
{
    __builtin_va_list args;
    int length;
    __builtin_va_start(args,format);
    length = pvsnprintf(((void *)0), 0, format, args);
    __builtin_va_end(args);
    if (length > 0)
    {
        char message[-1];
        __builtin_va_start(args,format);
        length = pvsnprintf(message, length + 1, format, args);
        __builtin_va_end(args);
        micropv_console_write(message, length);
    }
    return length;
}

variable_types:
format
char *

variable_types: length int

variable_types:
message
char [-1]


original_code:
int micropv_console_read(void *ptr, size_t len)
{
    int received = 0;
    XENCONS_RING_IDX cons, prod;
    struct xencons_interface *ring = xenconsole_interface();
    cons = ring->in_cons;
    prod = ring->in_prod;
    __asm__ __volatile__ ("mfence":::"memory");
    while ((received < len) && ((cons - prod) & (sizeof(ring->in) - 1)))
        ((char *)ptr)[received++] = ring->in[((cons++) & (sizeof(ring->in)-1))];
    __asm__ __volatile__ ("sfence" ::: "memory");
    ring->in_cons = cons;
    return received;
}

canonical_original_code:
int micropv_console_read(void *ptr, unsigned long len)
{
    int received = 0;
    unsigned int cons; unsigned int prod;
    struct xencons_interface *ring = xenconsole_interface();
    cons = ring->in_cons;
    prod = ring->in_prod;
    __asm__ __volatile__ ("mfence":::"memory");
    while ((received < len) && ((cons - prod) & (sizeof(ring->in) - 1)))
        ((char *)ptr)[received++] = ring->in[((cons++) & (sizeof(ring->in)-1))];
    __asm__ __volatile__ ("sfence" ::: "memory");
    ring->in_cons = cons;
    return received;
}

variable_types: received int


decompiled_code:
__int64 __fastcall micropv_console_read_available(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5)
{
__int64 v5; // rax

v5 = xenconsole_interface(a1, a2, a3, a4, a5);
return (*(_WORD *)(v5 + 3072) - *(_WORD *)(v5 + 3076)) & 0x3FF;
}

original_code:
int micropv_console_read_available()
{
    struct xencons_interface *ring = xenconsole_interface();
    return (ring->in_cons - ring->in_prod) & (sizeof(ring->in) - 1);
}


decompiled_code:
__int64 xenconsole_init()
{
unsigned int v0; // er12
unsigned int v1; // eax
int v3; // eax

v0 = 0;
v1 = xenconsole_event();
if ( !v1 )
return v0;
if ( (unsigned int)xenevents_bind_handler(v1, xenconsole_event_handler) == -1 )
{
micropv_printk("src/xenconsole.c", 204LL, "XEN console channel bind failed");
return (unsigned int)-1;
}
v3 = xenconsole_event();
xenevents_notify_remote_via_evtchn_isra_0(v3);
return 0LL;
}

canonical_decompiled_code:
__int64 func12()
{
unsigned int v0; // er12
unsigned int v1; // eax
int v3; // eax

v0 = 0;
v1 = func1();
if ( !v1 )
return v0;
if ( (unsigned int)xenevents_bind_handler(v1, xenconsole_event_handler) == -1 )
{
micropv_printk("src/xenconsole.c", 204LL, "XEN console channel bind failed");
return (unsigned int)-1;
}
v3 = func1();
func5(v3);
return 0LL;
}

original_code:
int xenconsole_init(void)
{
    if (!xenconsole_event())
        return 0;
    port = xenevents_bind_handler(xenconsole_event(), xenconsole_event_handler);
    if (port == -1)
    {
        micropv_printk("src/xenconsole.c", 204,"XEN console channel bind failed");
        return -1;
    }
    xenevents_notify_remote_via_evtchn(xenconsole_event());
    return 0;
}

canonical_original_code:
int xenconsole_init()
{
    if (!xenconsole_event())
        return 0;
    port = xenevents_bind_handler(xenconsole_event(), xenconsole_event_handler);
    if (port == -1)
    {
        micropv_printk("src/xenconsole.c", 204,"XEN console channel bind failed");
        return -1;
    }
    xenevents_notify_remote_via_evtchn(xenconsole_event());
    return 0;
}


decompiled_code:
__int64 scheduler_timer_dummy()
{
return 10000000LL;
}

original_code:
static uint64_t scheduler_timer_dummy(struct pt_regs *regs, uint64_t deadline)
{
    return (((10L) * 1000UL) * 1000UL);
}

canonical_original_code:
static unsigned long scheduler_timer_dummy(struct pt_regs *regs, unsigned long deadline)
{
    return (((10L) * 1000UL) * 1000UL);
}

variable_types:
deadline
unsigned long


decompiled_code:
void scheduler_yield_dummy()
{
;
}

canonical_decompiled_code:
void func1()
{
;
}

original_code:
static void scheduler_yield_dummy(struct pt_regs *regs)
{
}


decompiled_code:
__int64 __fastcall timer_handler(__int64 a1, __int64 a2)
{
__int64 v3; // rbp
__int64 v4; // rdi
unsigned __int64 v5; // rax
__int64 (__fastcall *v6)(); // rax
__int64 v7; // r13
__int64 v8; // r12
__int64 result; // rax
__int64 v10; // rsi
__int64 v11; // rdi

v3 = timer_deadline;
v4 = timer_deadline;
do
{
timer_deadline = timer_period + v4;
v5 = micropv_time_monotonic_clock();
v4 = timer_deadline;
}
while ( v5 > timer_deadline );
xentime_set_next_event();
xentime_update();
v6 = micropv_scheduler_timer_callback;
v7 = *(_QWORD *)(a2 + 152);
v8 = *(_QWORD *)(a2 + 160);
if ( !micropv_scheduler_timer_callback )
v6 = scheduler_timer_dummy;
result = ((__int64 (__fastcall *)(__int64, __int64))v6)(a2, v3);
v10 = *(_QWORD *)(a2 + 152);
v11 = *(_QWORD *)(a2 + 160);
timer_period = result;
if ( v10 != v7 || v11 != v8 )
{
HYPERVISOR_stack_switch_isra_0(v11);
result = HYPERVISOR_fpu_taskswitch_constprop_0_isra_0();
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2)
{
__int64 v3; // rbp
__int64 v4; // rdi
unsigned __int64 v5; // rax
__int64 (__fastcall *v6)(); // rax
__int64 v7; // r13
__int64 v8; // r12
__int64 result; // rax
__int64 v10; // rsi
__int64 v11; // rdi

v3 = timer_deadline;
v4 = timer_deadline;
do
{
timer_deadline = timer_period + v4;
v5 = micropv_time_monotonic_clock();
v4 = timer_deadline;
}
while ( v5 > timer_deadline );
xentime_set_next_event();
xentime_update();
v6 = micropv_scheduler_timer_callback;
v7 = *(_QWORD *)(a2 + 152);
v8 = *(_QWORD *)(a2 + 160);
if ( !micropv_scheduler_timer_callback )
v6 = scheduler_timer_dummy;
result = ((__int64 (__fastcall *)(__int64, __int64))v6)(a2, v3);
v10 = *(_QWORD *)(a2 + 152);
v11 = *(_QWORD *)(a2 + 160);
timer_period = result;
if ( v10 != v7 || v11 != v8 )
{
func4(v11);
result = func2();
}
return result;
}

original_code:
static void timer_handler(evtchn_port_t ev, struct pt_regs *regs, void *ign)
{
    uint64_t deadline = timer_deadline;
    do timer_deadline += timer_period;
    while (timer_deadline < micropv_time_monotonic_clock());
    xentime_set_next_event(timer_deadline);
    xentime_update();
    unsigned long sp = regs->sp;
    unsigned long ss = regs->ss;
    timer_period = (micropv_scheduler_timer_callback ? micropv_scheduler_timer_callback : scheduler_timer_dummy)(regs, deadline);
    if ((sp != regs->sp) || (ss != regs->ss))
    {
        HYPERVISOR_stack_switch(regs->ss, regs->sp);
        HYPERVISOR_fpu_taskswitch(1);
    }
}

canonical_original_code:
static void timer_handler(unsigned int ev, struct pt_regs *regs, void *ign)
{
    unsigned long deadline = timer_deadline;
    do timer_deadline += timer_period;
    while (timer_deadline < micropv_time_monotonic_clock());
    xentime_set_next_event(timer_deadline);
    xentime_update();
    unsigned long sp = regs->sp;
    unsigned long ss = regs->ss;
    timer_period = (micropv_scheduler_timer_callback ? micropv_scheduler_timer_callback : scheduler_timer_dummy)(regs, deadline);
    if ((sp != regs->sp) || (ss != regs->ss))
    {
        HYPERVISOR_stack_switch(regs->ss, regs->sp);
        HYPERVISOR_fpu_taskswitch(1);
    }
}

variable_types:
ev
unsigned int

variable_types:
ign
void *

variable_types:
sp
unsigned long

variable_types:
ss
unsigned long


decompiled_code:
__int64 __fastcall yield_handler(__int64 a1, __int64 a2)
{
__int64 result; // rax
__int64 v3; // rbp
__int64 v4; // r12
__int64 v5; // rdi

result = (unsigned int)in_yield;
if ( in_yield )
{
v3 = *(_QWORD *)(a2 + 152);
v4 = *(_QWORD *)(a2 + 160);
in_yield = 0;
result = micropv_scheduler_yield_callback(a2);
v5 = *(_QWORD *)(a2 + 160);
if ( *(_QWORD *)(a2 + 152) != v3 || v5 != v4 )
{
HYPERVISOR_stack_switch_isra_0(v5);
result = HYPERVISOR_fpu_taskswitch_constprop_0_isra_0();
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, __int64 a2)
{
__int64 result; // rax
__int64 v3; // rbp
__int64 v4; // r12
__int64 v5; // rdi

result = (unsigned int)in_yield;
if ( in_yield )
{
v3 = *(_QWORD *)(a2 + 152);
v4 = *(_QWORD *)(a2 + 160);
in_yield = 0;
result = micropv_scheduler_yield_callback(a2);
v5 = *(_QWORD *)(a2 + 160);
if ( *(_QWORD *)(a2 + 152) != v3 || v5 != v4 )
{
func4(v5);
result = func2();
}
}
return result;
}

original_code:
static void yield_handler(uint32_t port, struct pt_regs *regs, void *context)
{
    if (in_yield)
    {
        in_yield = 0;
        unsigned long sp = regs->sp;
        unsigned long ss = regs->ss;
        micropv_scheduler_yield_callback(regs);
        if ((sp != regs->sp) || (ss != regs->ss))
        {
            HYPERVISOR_stack_switch(regs->ss, regs->sp);
            HYPERVISOR_fpu_taskswitch(1);
        }
    }
}

canonical_original_code:
static void yield_handler(unsigned int port, struct pt_regs *regs, void *context)
{
    if (in_yield)
    {
        in_yield = 0;
        unsigned long sp = regs->sp;
        unsigned long ss = regs->ss;
        micropv_scheduler_yield_callback(regs);
        if ((sp != regs->sp) || (ss != regs->ss))
        {
            HYPERVISOR_stack_switch(regs->ss, regs->sp);
            HYPERVISOR_fpu_taskswitch(1);
        }
    }
}

variable_types:
context
void *


decompiled_code:
void micropv_scheduler_yield()
{
if ( yield_port != -1 )
{
in_yield = 1;
if ( (unsigned int)micropv_fire_event() )
micropv_printk("src/xenschedule.c", 174LL, "Error firing context_switch_event");
}
}

canonical_decompiled_code:
void func8()
{
if ( yield_port != -1 )
{
in_yield = 1;
if ( (unsigned int)micropv_fire_event() )
micropv_printk("src/xenschedule.c", 174LL, "Error firing context_switch_event");
}
}

original_code:
void micropv_scheduler_yield(void)
{
    if (-1 != yield_port)
    {
        in_yield = 1;
        if (micropv_fire_event(yield_port))
            micropv_printk("src/xenschedule.c", 174,"Error firing context_switch_event");
    }
}

canonical_original_code:
void micropv_scheduler_yield()
{
    if (-1 != yield_port)
    {
        in_yield = 1;
        if (micropv_fire_event(yield_port))
            micropv_printk("src/xenschedule.c", 174,"Error firing context_switch_event");
    }
}


decompiled_code:
__int64 micropv_scheduler_block()
{
return HYPERVISOR_sched_op_isra_0(1);
}

canonical_decompiled_code:
__int64 func9()
{
return func3(1);
}

original_code:
void micropv_scheduler_block(void)
{
    HYPERVISOR_sched_op(1, 0);
}

canonical_original_code:
void micropv_scheduler_block()
{
    HYPERVISOR_sched_op(1, 0);
}


decompiled_code:
__int64 xenscheduler_init()
{
xentime_stop_periodic();
xenevents_bind_virq(0LL, timer_handler);
xenevents_create_event(&yield_port, yield_handler);
timer_deadline = timer_period + micropv_time_monotonic_clock();
return xentime_set_next_event(timer_deadline);
}

canonical_decompiled_code:
__int64 func10()
{
xentime_stop_periodic();
xenevents_bind_virq(0LL, timer_handler);
xenevents_create_event(&yield_port, yield_handler);
timer_deadline = timer_period + micropv_time_monotonic_clock();
return xentime_set_next_event(timer_deadline);
}

original_code:
void xenscheduler_init(void)
{
    xentime_stop_periodic();
    timer_port = xenevents_bind_virq(0, &timer_handler);
    xenevents_create_event(&yield_port, yield_handler);
    timer_deadline = micropv_time_monotonic_clock() + timer_period;
    xentime_set_next_event(timer_deadline);
}

canonical_original_code:
void xenscheduler_init()
{
    xentime_stop_periodic();
    timer_port = xenevents_bind_virq(0, &timer_handler);
    xenevents_create_event(&yield_port, yield_handler);
    timer_deadline = micropv_time_monotonic_clock() + timer_period;
    xentime_set_next_event(timer_deadline);
}


decompiled_code:
void __noreturn micropv_exit()
{
micropv_printk("src/xenschedule.c", 201LL, "micropv_exit called!");
while ( 1 )
HYPERVISOR_sched_op_isra_0(2);
}

canonical_decompiled_code:
void __noreturn func11()
{
micropv_printk("src/xenschedule.c", 201LL, "micropv_exit called!");
while ( 1 )
func3(2);
}

original_code:
void micropv_exit(void)
{
    micropv_printk("src/xenschedule.c", 201,"micropv_exit called!");
    for (;;)
    {
        struct sched_shutdown sched_shutdown = { .reason = 1 };
        HYPERVISOR_sched_op(2, &sched_shutdown);
    }
}

canonical_original_code:
void micropv_exit()
{
    micropv_printk("src/xenschedule.c", 201,"micropv_exit called!");
    for (;;)
    {
        struct sched_shutdown sched_shutdown = { .reason = 1 };
        HYPERVISOR_sched_op(2, &sched_shutdown);
    }
}

variable_types: struct sched_shutdown { unsigned int reason; }


strlen

decompiled_code:
__int64 __fastcall strlen(_BYTE *a1)
{
__int64 result; // rax

result = 0LL;
if ( *a1 )
{
do
++result;
while ( a1[result] );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(_BYTE *a1)
{
__int64 result; // rax

result = 0LL;
if ( *a1 )
{
do
++result;
while ( a1[result] );
}
return result;
}

original_code:
size_t strlen(const char *s)
{
    size_t c;
    for (c = 0; *s++; c++) ;
    return c;
}

canonical_original_code:
unsigned long strlen(char *s)
{
    unsigned long c;
    for (c = 0; *s++; c++) ;
    return c;
}

variable_types:
s
char *

variable_types:
c
unsigned long


decompiled_code:
int __fastcall start_kernel(__int64 a1)
{
const char **v1; // rdx

micropv_printk("src/kernel.c", 65LL, "micro_pv started");
hypervisor_start(a1);
micropv_printk("src/kernel.c", 71LL, "calling main");
return main((int)"src/kernel.c", (const char **)0x47, v1);
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1)
{
const char **v1; // rdx

micropv_printk("src/kernel.c", 65LL, "micro_pv started");
hypervisor_start(a1);
micropv_printk("src/kernel.c", 71LL, "calling main");
return main((int)"src/kernel.c", (const char **)0x47, v1);
}

original_code:
void start_kernel(start_info_t *si)
{
    micropv_printk("src/kernel.c", 65,"micro_pv started");
    hypervisor_start(si);
    micropv_printk("src/kernel.c", 71,"calling main");
    main();
}

canonical_original_code:
void start_kernel(struct start_info *si)
{
    micropv_printk("src/kernel.c", 65,"micro_pv started");
    hypervisor_start(si);
    micropv_printk("src/kernel.c", 71,"calling main");
    main();
}

variable_types: struct start_info { char magic[32]; unsigned long nr_pages; unsigned long shared_info; unsigned int flags; unsigned long store_mfn; unsigned int store_evtchn; union { struct { unsigned long mfn; unsigned int evtchn; } domU; struct { unsigned int info_off; unsigned int info_size; } dom0; } console; unsigned long pt_base; unsigned long nr_pt_frames; unsigned long mfn_list; unsigned long mod_start; unsigned long mod_len; char cmd_line[1024]; unsigned long first_p2m_pfn; unsigned long nr_p2m_frames; } *


decompiled_code:
__int64 hypervisor_setup_xen_features()
{
__int64 result; // rax
int v1; // esi
__int64 i; // rcx
__int64 v3; // [rsp+8h] [rbp-8h] BYREF

v3 = 0LL;
result = HYPERVISOR_xen_version_constprop_0((__int64)&v3);
if ( (int)result >= 0 )
{
v1 = HIDWORD(v3);
for ( i = 0LL; i != 32; ++i )
{
result = (unsigned int)(1 << i);
xen_features[i] = (v1 & (unsigned int)result) != 0;
}
}
return result;
}

canonical_decompiled_code:
__int64 func1()
{
__int64 result; // rax
int v1; // esi
__int64 i; // rcx
__int64 v3; // [rsp+8h] [rbp-8h] BYREF

v3 = 0LL;
result = func0((__int64)&v3);
if ( (int)result >= 0 )
{
v1 = HIDWORD(v3);
for ( i = 0LL; i != 32; ++i )
{
result = (unsigned int)(1 << i);
xen_features[i] = (v1 & (unsigned int)result) != 0;
}
}
return result;
}

original_code:
static void hypervisor_setup_xen_features(void)
{
    xen_feature_info_t fi;
    int i, j;
    for (i = 0; i < 1; i++)
    {
        fi.submap = 0;
        fi.submap_idx = i;
        if (HYPERVISOR_xen_version(6, &fi) < 0)
            break;
        for (j = 0; j < 32; j++) xen_features[i * 32 + j] = !!(fi.submap & 1 << j);
    }
}

canonical_original_code:
static void hypervisor_setup_xen_features()
{
    struct xen_feature_info fi;
    int i; int j;
    for (i = 0; i < 1; i++)
    {
        fi.submap = 0;
        fi.submap_idx = i;
        if (HYPERVISOR_xen_version(6, &fi) < 0)
            break;
        for (j = 0; j < 32; j++) xen_features[i * 32 + j] = !!(fi.submap & 1 << j);
    }
}

variable_types: struct xen_feature_info { unsigned int submap_idx; unsigned int submap; }

variable_types: i int

variable_types: j int


original_code:
static void console_printkv(const char *file, long line, const char *format, va_list args)
{
    va_list args0;;
    int message_length;
    __builtin_va_copy(args0,args);
    message_length = pvsnprintf(((void *)0), 0, format, args);
    if (message_length > 0)
    {
        char header[strlen(file)+ 30];
        struct timeval tv;
        micropv_time_gettimeofday(&tv, ((void *)0));
        uint64_t millisecond = tv.tv_usec / 1000;
        uint64_t second = tv.tv_sec % 60; tv.tv_sec /= 60;
        uint64_t minute = tv.tv_sec % 60; tv.tv_sec /= 60;
        uint64_t hour = tv.tv_sec % 24;
        int header_length = psnprintf(header, sizeof(header), "%02lu:%02lu:%02lu.%03lu %s@%.5li: ", hour, minute, second, millisecond, file, line);
        char message[message_length + 1];
        message_length = pvsnprintf(message, message_length + 1, format, args0);
        HYPERVISOR_console_io(0, header_length, header);
        HYPERVISOR_console_io(0, message_length, message);
        if ((header_length + message_length) < 80) {
            static char lf = '\n';
            HYPERVISOR_console_io(0, 1, &lf);
        }
    }
    __builtin_va_end(args0);
}

canonical_original_code:
static void console_printkv(char *file, long line, char *format, __builtin_va_list args)
{
    __builtin_va_list args0;;
    int message_length;
    __builtin_va_copy(args0,args);
    message_length = pvsnprintf(((void *)0), 0, format, args);
    if (message_length > 0)
    {
        char header[-1];
        struct timeval tv;
        micropv_time_gettimeofday(&tv, ((void *)0));
        unsigned long millisecond = tv.tv_usec / 1000;
        unsigned long second = tv.tv_sec % 60; tv.tv_sec /= 60;
        unsigned long minute = tv.tv_sec % 60; tv.tv_sec /= 60;
        unsigned long hour = tv.tv_sec % 24;
        int header_length = psnprintf(header, sizeof(header), "%02lu:%02lu:%02lu.%03lu %s@%.5li: ", hour, minute, second, millisecond, file, line);
        char message[-1];
        message_length = pvsnprintf(message, message_length + 1, format, args0);
        HYPERVISOR_console_io(0, header_length, header);
        HYPERVISOR_console_io(0, message_length, message);
        if ((header_length + message_length) < 80) {
            static char lf = '\n';
            HYPERVISOR_console_io(0, 1, &lf);
        }
    }
    __builtin_va_end(args0);
}

variable_types:
file
char *

variable_types: line long

variable_types:
format
char *

variable_types:
header
char [-1]

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

variable_types:
millisecond
unsigned long

variable_types:
second
unsigned long

variable_types:
minute
unsigned long

variable_types:
hour
unsigned long

variable_types: lf char


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

va_start(va, a3);
return micropv_printkv(a1, a2, a3, va);
}

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

va_start(va, a3);
return micropv_printkv(a1, a2, a3, va);
}

original_code:
void micropv_printk(const char *file, long line, const char *format, ...)
{
    va_list args;
    __builtin_va_start(args,format);
    micropv_printkv(file, line, format, args);
    __builtin_va_end(args);
}

canonical_original_code:
void micropv_printk(char *file, long line, char *format, ...)
{
    __builtin_va_list args;
    __builtin_va_start(args,format);
    micropv_printkv(file, line, format, args);
    __builtin_va_end(args);
}


original_code:
void micropv_printk_binary(const char *file, long line, const char *buffer, size_t buffer_len)
{
    const int entries_per_line = 0x10;
    const int entries_per_line_mask = entries_per_line - 1;
    const int line_length = 3 + (entries_per_line * 2) + (entries_per_line / 4) + 5 + entries_per_line + 2;
    char debug_buffer[line_length];
    static const char hex[] = "0123456789abcdef";
    int i = 0;
    while (i < buffer_len)
    {
        int output_index = 0;
        debug_buffer[output_index++] = hex[(i >> 4) & 0xf];
        debug_buffer[output_index++] = hex[i & 0xf];
        debug_buffer[output_index++] = ' ';
        do
        {
            debug_buffer[output_index++] = hex[(buffer[i] >> 4) & 0xf];
            debug_buffer[output_index++] = hex[buffer[i] & 0xf];
            if ((i & 3) == 3)
                debug_buffer[output_index++] = ' ';
            i++;
        } while ((i & entries_per_line_mask) && (i < buffer_len));
        while (i & entries_per_line_mask)
        {
            debug_buffer[output_index++] = ' ';
            debug_buffer[output_index++] = ' ';
            if ((i & 3) == 3)
                debug_buffer[output_index++] = ' ';
            i++;
        }
        debug_buffer[output_index++] = '.';
        debug_buffer[output_index++] = '.';
        debug_buffer[output_index++] = '.';
        debug_buffer[output_index++] = ' ';
        debug_buffer[output_index++] = '[';
        i -= entries_per_line;
        do
        {
            debug_buffer[output_index++] = (buffer[i] >= ' ') ? buffer[i] : '.';
            i++;
        } while ((i & entries_per_line_mask) && (i < buffer_len));
        debug_buffer[output_index++] = ']';
        debug_buffer[output_index++] = 0;
        micropv_printk(file, line, "%.*s", output_index, debug_buffer);
    }
}

canonical_original_code:
void micropv_printk_binary(char *file, long line, char *buffer, unsigned long buffer_len)
{
    int entries_per_line = 0x10;
    int entries_per_line_mask = entries_per_line - 1;
    int line_length = 3 + (entries_per_line * 2) + (entries_per_line / 4) + 5 + entries_per_line + 2;
    char debug_buffer[-1];
    static char hex[] = "0123456789abcdef";
    int i = 0;
    while (i < buffer_len)
    {
        int output_index = 0;
        debug_buffer[output_index++] = hex[(i >> 4) & 0xf];
        debug_buffer[output_index++] = hex[i & 0xf];
        debug_buffer[output_index++] = ' ';
        do
        {
            debug_buffer[output_index++] = hex[(buffer[i] >> 4) & 0xf];
            debug_buffer[output_index++] = hex[buffer[i] & 0xf];
            if ((i & 3) == 3)
                debug_buffer[output_index++] = ' ';
            i++;
        } while ((i & entries_per_line_mask) && (i < buffer_len));
        while (i & entries_per_line_mask)
        {
            debug_buffer[output_index++] = ' ';
            debug_buffer[output_index++] = ' ';
            if ((i & 3) == 3)
                debug_buffer[output_index++] = ' ';
            i++;
        }
        debug_buffer[output_index++] = '.';
        debug_buffer[output_index++] = '.';
        debug_buffer[output_index++] = '.';
        debug_buffer[output_index++] = ' ';
        debug_buffer[output_index++] = '[';
        i -= entries_per_line;
        do
        {
            debug_buffer[output_index++] = (buffer[i] >= ' ') ? buffer[i] : '.';
            i++;
        } while ((i & entries_per_line_mask) && (i < buffer_len));
        debug_buffer[output_index++] = ']';
        debug_buffer[output_index++] = 0;
        micropv_printk(file, line, "%.*s", output_index, debug_buffer);
    }
}

variable_types:
buffer
char *

variable_types:
hex
char []


decompiled_code:
__int64 __fastcall hypervisor_start(_QWORD *a1)
{
micropv_printk((__int64)"src/hypervisor.c", 216LL, (__int64)"Allocated memory pages : %lu", a1[4]);
micropv_printk((__int64)"src/hypervisor.c", 217LL, (__int64)"Allocated memory MB : %lu", a1[4] >> 8);
micropv_printk((__int64)"src/hypervisor.c", 218LL, (__int64)"Machine address of shared memory : 0x%lx", a1[5]);
__asm { fninit }
_mm_setcsr(0x1F80u);
qmemcpy(hypervisor_start_info, a1, sizeof(hypervisor_start_info));
hypervisor_setup_xen_features();
xentraps_init();
xenmmu_init();
hypervisor_shared_info = micropv_remap_page(&shared_info, hypervisor_start_info[5], 4096LL, 0LL);
if ( !hypervisor_shared_info )
{
micropv_printk(
(__int64)"src/hypervisor.c",
242LL,
(__int64)"ASSERTION FAILED: %s at %s:%d.",
"!(hypervisor_shared_info == ((void *)0))",
"src/hypervisor.c",
242LL);
micropv_exit();
}
xenevents_init();
xenconsole_init();
xentime_init();
xenstore_init();
xengnttab_init();
return xenscheduler_init();
}

canonical_decompiled_code:
__int64 __fastcall func6(_QWORD *a1)
{
func4((__int64)"src/hypervisor.c", 216LL, (__int64)"Allocated memory pages : %lu", a1[4]);
func4((__int64)"src/hypervisor.c", 217LL, (__int64)"Allocated memory MB : %lu", a1[4] >> 8);
func4((__int64)"src/hypervisor.c", 218LL, (__int64)"Machine address of shared memory : 0x%lx", a1[5]);
__asm { fninit }
_mm_setcsr(0x1F80u);
qmemcpy(hypervisor_start_info, a1, sizeof(hypervisor_start_info));
func1();
xentraps_init();
xenmmu_init();
hypervisor_shared_info = micropv_remap_page(&shared_info, hypervisor_start_info[5], 4096LL, 0LL);
if ( !hypervisor_shared_info )
{
func4(
(__int64)"src/hypervisor.c",
242LL,
(__int64)"ASSERTION FAILED: %s at %s:%d.",
"!(hypervisor_shared_info == ((void *)0))",
"src/hypervisor.c",
242LL);
micropv_exit();
}
xenevents_init();
xenconsole_init();
xentime_init();
xenstore_init();
xengnttab_init();
return xenscheduler_init();
}

original_code:
void hypervisor_start(start_info_t *si)
{
    micropv_printk("src/hypervisor.c", 216,"Allocated memory pages           : %lu", si->nr_pages);
    micropv_printk("src/hypervisor.c", 217,"Allocated memory MB              : %lu", si->nr_pages >> (20 - 12));
    micropv_printk("src/hypervisor.c", 218,"Machine address of shared memory : 0x%lx", si->shared_info);
    __asm__ volatile(" fninit");
    unsigned long status = 0x1f80;
    __asm__ volatile("ldmxcsr %0" : : "m" (status));
    memcpy(&hypervisor_start_info, si, sizeof(hypervisor_start_info));
    hypervisor_setup_xen_features();
    xentraps_init();
    xenmmu_init();
    hypervisor_shared_info = micropv_remap_page((unsigned long)&shared_info, hypervisor_start_info.shared_info, sizeof(shared_info), 0);
    do { if (!(!(hypervisor_shared_info == ((void *)0)))) { micropv_printk("src/hypervisor.c", 242,"ASSERTION FAILED: %s at %s:%d.", "!(hypervisor_shared_info == ((void *)0))" , "src/hypervisor.c", 242); micropv_exit(); } } while(0);
    xenevents_init();
    xenconsole_init();
    xentime_init();
    xenstore_init();
    xengnttab_init();
    xenscheduler_init();
}

canonical_original_code:
void hypervisor_start(struct start_info *si)
{
    micropv_printk("src/hypervisor.c", 216,"Allocated memory pages           : %lu", si->nr_pages);
    micropv_printk("src/hypervisor.c", 217,"Allocated memory MB              : %lu", si->nr_pages >> (20 - 12));
    micropv_printk("src/hypervisor.c", 218,"Machine address of shared memory : 0x%lx", si->shared_info);
    __asm__ volatile(" fninit");
    unsigned long status = 0x1f80;
    __asm__ volatile("ldmxcsr %0" : : "m" (status));
    memcpy(&hypervisor_start_info, si, sizeof(hypervisor_start_info));
    hypervisor_setup_xen_features();
    xentraps_init();
    xenmmu_init();
    hypervisor_shared_info = micropv_remap_page((unsigned long)&shared_info, hypervisor_start_info.shared_info, sizeof(shared_info), 0);
    do { if (!(!(hypervisor_shared_info == ((void *)0)))) { micropv_printk("src/hypervisor.c", 242,"ASSERTION FAILED: %s at %s:%d.", "!(hypervisor_shared_info == ((void *)0))" , "src/hypervisor.c", 242); micropv_exit(); } } while(0);
    xenevents_init();
    xenconsole_init();
    xentime_init();
    xenstore_init();
    xengnttab_init();
    xenscheduler_init();
}

variable_types:
status
unsigned long

user_defined_types: struct start_info { char magic[32]; unsigned long nr_pages; unsigned long shared_info; unsigned int flags; unsigned long store_mfn; unsigned int store_evtchn; union { struct { unsigned long mfn; unsigned int evtchn; } domU; struct { unsigned int info_off; unsigned int info_size; } dom0; } console; unsigned long pt_base; unsigned long nr_pt_frames; unsigned long mfn_list; unsigned long mod_start; unsigned long mod_len; char cmd_line[1024]; unsigned long first_p2m_pfn; unsigned long nr_p2m_frames; }


main

canonical_name: main

repo: Fydot/easyproxy

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v4; // er13
int v5; // er12
socklen_t addr_len; // [rsp+Ch] [rbp-34h] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-30h] BYREF
struct sockaddr v9; // [rsp+20h] [rbp-20h] BYREF

read_args(argc, argv, envp);
*(_DWORD *)&addr.sa_data[10] = 0;
addr.sa_family = 2;
*(_QWORD *)&addr.sa_data[2] = (unsigned int)_bswap_32_constprop_0();
*(_WORD *)addr.sa_data = proxy_port;
v3 = socket(2, 1, 0);
v4 = v3;
if ( v3 < 0 )
{
fwrite("Failed to create server socket\r\n", 1uLL, 0x20uLL, stderr);
}
else if ( bind(v3, &addr, 0x10u) < 0 )
{
fwrite("faild to bind server socket to specified port\r\n", 1uLL, 0x2FuLL, stderr);
}
else
{
listen(v4, 25);
while ( 1 )
{
while ( 1 )
{
addr_len = 16;
v5 = accept(v4, &v9, &addr_len);
if ( v5 < 0 )
break;
if ( !fork() )
goto LABEL_8;
}
if ( *__errno_location() != 4 )
{
puts("failed to accept connection");
if ( !fork() )
break;
}
}
LABEL_8:
close(v4);
onproxy((unsigned int)v5);
}
return 1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v4; // er13
int v5; // er12
socklen_t addr_len; // [rsp+Ch] [rbp-34h] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-30h] BYREF
struct sockaddr v9; // [rsp+20h] [rbp-20h] BYREF

func10(argc, argv, envp);
*(_DWORD *)&addr.sa_data[10] = 0;
addr.sa_family = 2;
*(_QWORD *)&addr.sa_data[2] = (unsigned int)func9();
*(_WORD *)addr.sa_data = proxy_port;
v3 = socket(2, 1, 0);
v4 = v3;
if ( v3 < 0 )
{
fwrite("Failed to create server socket\r\n", 1uLL, 0x20uLL, stderr);
}
else if ( bind(v3, &addr, 0x10u) < 0 )
{
fwrite("faild to bind server socket to specified port\r\n", 1uLL, 0x2FuLL, stderr);
}
else
{
listen(v4, 25);
while ( 1 )
{
while ( 1 )
{
addr_len = 16;
v5 = accept(v4, &v9, &addr_len);
if ( v5 < 0 )
break;
if ( !fork() )
goto LABEL_8;
}
if ( *__errno_location() != 4 )
{
puts("failed to accept connection");
if ( !fork() )
break;
}
}
LABEL_8:
close(v4);
func11((unsigned int)v5);
}
return 1;
}

original_code:
int main(int argc, char* args[]) {
  struct sockaddr_in servaddr, cliaddr;
  int clilen;
  int childpid;
  int sockfd, newsockfd;
  read_args(argc, args);
  bzero((char *) &servaddr, sizeof(servaddr));
  servaddr.sin_family = 2;
  servaddr.sin_addr.s_addr = htonl(((in_addr_t) 0x00000000));
  servaddr.sin_port = proxy_port;
  if ((sockfd = socket(2,SOCK_STREAM,0)) < 0) {
    fputs("Failed to create server socket\r\n",stderr);
    return 1;
  }
  if (bind(sockfd,(struct sockaddr *) &servaddr,sizeof(servaddr)) < 0) {
    fputs("faild to bind server socket to specified port\r\n",stderr);
    return 1;
  }
  listen(sockfd,25);
  while (1) {
    clilen = sizeof(cliaddr);
    newsockfd = accept(sockfd, (struct sockaddr *) &cliaddr, (socklen_t*)&(clilen));
    if (newsockfd < 0 && (*__errno_location ()) == 4) {
      continue;
    }
    else if (newsockfd < 0) {
      puts("failed to accept connection");
    }
    if ((childpid = fork()) == 0) {
      close(sockfd);
      onproxy(newsockfd);
      return 1;
    }
  }
  return 0;
}

canonical_original_code:
int main(int argc, char *args[]) {
  struct sockaddr_in servaddr; struct sockaddr_in cliaddr;
  int clilen;
  int childpid;
  int sockfd; int newsockfd;
  read_args(argc, args);
  bzero((char *) &servaddr, sizeof(servaddr));
  servaddr.sin_family = 2;
  servaddr.sin_addr.s_addr = htonl(((unsigned int) 0x00000000));
  servaddr.sin_port = proxy_port;
  if ((sockfd = socket(2,SOCK_STREAM,0)) < 0) {
    fputs("Failed to create server socket\r\n",stderr);
    return 1;
  }
  if (bind(sockfd,(struct sockaddr *) &servaddr,sizeof(servaddr)) < 0) {
    fputs("faild to bind server socket to specified port\r\n",stderr);
    return 1;
  }
  listen(sockfd,25);
  while (1) {
    clilen = sizeof(cliaddr);
    newsockfd = accept(sockfd, (struct sockaddr *) &cliaddr, (unsigned int *)&(clilen));
    if (newsockfd < 0 && (*__errno_location ()) == 4) {
      continue;
    }
    else if (newsockfd < 0) {
      puts("failed to accept connection");
    }
    if ((childpid = fork()) == 0) {
      close(sockfd);
      onproxy(newsockfd);
      return 1;
    }
  }
  return 0;
}

variable_types: argc int

variable_types:
args
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: clilen int

variable_types: childpid int

variable_types: sockfd int

variable_types: newsockfd int

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 { unsigned short sa_family; char sa_data[14]; }


decompiled_code:
__int16 __fastcall read_args(__int64 a1, const char **a2)
{
__int64 v2; // rbx
size_t v3; // r12
const unsigned __int16 **v4; // rax
__int64 v5; // rdx
__int16 v6; // ax
unsigned __int16 v7; // ax
in_addr_t v8; // eax
struct servent *v9; // rax
int v10; // eax
struct hostent *v11; // rax
int v12; // eax
char dest[32]; // [rsp+0h] [rbp-90h] BYREF
char cp[64]; // [rsp+20h] [rbp-70h] BYREF
char name[48]; // [rsp+60h] [rbp-30h] BYREF

strcpy(dest, a2[1]);
strcpy(cp, a2[2]);
v2 = 0LL;
strcpy(name, a2[3]);
v3 = strlen(dest);
while ( v3 != v2 )
{
v4 = __ctype_b_loc();
v5 = dest[v2++];
if ( ((*v4)[v5] & 0x800) == 0 )
{
LOWORD(v10) = puts("Invalid Args!");
return v10;
}
}
v6 = atoi(dest);
v7 = _bswap_16(v6);
server_host = 0LL;
proxy_port = v7;
server_host.sa_family = 2;
v8 = inet_addr(cp);
if ( v8 == 0xFFFFFFFFLL )
{
v11 = gethostbyname(cp);
if ( !v11 )
{
LOWORD(v10) = printf("%s: Unknown Host\n", cp);
return v10;
}
memmove(&server_host.sa_data[2], *(const void **)v11->h_addr_list, v11->h_length);
}
else
{
*(_QWORD *)&server_host.sa_data[2] = v8;
}
v9 = getservbyname(name, "tcp");
if ( v9 )
{
v10 = v9->s_port;
*(_WORD *)server_host.sa_data = v10;
}
else
{
v12 = atoi(name);
if ( v12 <= 0 )
{
LOWORD(v10) = printf("%s: Invalid Service Name or Port\n", name);
}
else
{
LOWORD(v10) = _bswap_16(v12);
*(_WORD *)server_host.sa_data = v10;
}
}
return v10;
}

canonical_decompiled_code:
__int16 __fastcall func10(__int64 a1, const char **a2)
{
__int64 v2; // rbx
size_t v3; // r12
const unsigned __int16 **v4; // rax
__int64 v5; // rdx
__int16 v6; // ax
unsigned __int16 v7; // ax
in_addr_t v8; // eax
struct servent *v9; // rax
int v10; // eax
struct hostent *v11; // rax
int v12; // eax
char dest[32]; // [rsp+0h] [rbp-90h] BYREF
char cp[64]; // [rsp+20h] [rbp-70h] BYREF
char name[48]; // [rsp+60h] [rbp-30h] BYREF

strcpy(dest, a2[1]);
strcpy(cp, a2[2]);
v2 = 0LL;
strcpy(name, a2[3]);
v3 = strlen(dest);
while ( v3 != v2 )
{
v4 = __ctype_b_loc();
v5 = dest[v2++];
if ( ((*v4)[v5] & 0x800) == 0 )
{
LOWORD(v10) = puts("Invalid Args!");
return v10;
}
}
v6 = atoi(dest);
v7 = func8(v6);
server_host = 0LL;
proxy_port = v7;
server_host.sa_family = 2;
v8 = inet_addr(cp);
if ( v8 == 0xFFFFFFFFLL )
{
v11 = gethostbyname(cp);
if ( !v11 )
{
LOWORD(v10) = printf("%s: Unknown Host\n", cp);
return v10;
}
memmove(&server_host.sa_data[2], *(const void **)v11->h_addr_list, v11->h_length);
}
else
{
*(_QWORD *)&server_host.sa_data[2] = v8;
}
v9 = getservbyname(name, "tcp");
if ( v9 )
{
v10 = v9->s_port;
*(_WORD *)server_host.sa_data = v10;
}
else
{
v12 = atoi(name);
if ( v12 <= 0 )
{
LOWORD(v10) = printf("%s: Invalid Service Name or Port\n", name);
}
else
{
LOWORD(v10) = func8(v12);
*(_WORD *)server_host.sa_data = v10;
}
}
return v10;
}

original_code:
void read_args(int argc, char* args[]) {
  struct hostent *hostp;
  struct servent *servp;
  unsigned long inaddr;
  struct {
    char proxy_port[32];
    char service_host[64];
    char service_port[32];
  } pargs;
  strcpy(pargs.proxy_port, args[1]);
  strcpy(pargs.service_host, args[2]);
  strcpy(pargs.service_port, args[3]);
  int i;
  for (i = 0; i < strlen(pargs.proxy_port); ++i) {
    if (!((*__ctype_b_loc ())[(int) ((pargs.proxy_port[i]))] & (unsigned short int) _ISdigit)) {
      break;
    }
  }
  if (strlen(pargs.proxy_port) == i) {
    proxy_port = htons(atoi(pargs.proxy_port));
  }
  else {
    printf("Invalid Args!\n");
    return;
  }
  bzero(&server_host, sizeof(server_host));
  server_host.sin_family = 2;
  if ((inaddr = inet_addr(pargs.service_host)) != ((in_addr_t) 0xffffffff))
    bcopy(&inaddr,&server_host.sin_addr,sizeof(inaddr));
  else if ((hostp = gethostbyname(pargs.service_host)) != ((void *)0))
    bcopy(hostp->h_addr_list[0],&server_host.sin_addr,hostp->h_length);
  else {
    printf("%s: Unknown Host\n",pargs.service_host);
    return;
  }
  if ((servp = getservbyname(pargs.service_port, "tcp")) != ((void *)0))
    server_host.sin_port = servp->s_port;
  else if (atoi(pargs.service_port) > 0)
    server_host.sin_port = htons(atoi(pargs.service_port));
  else {
    printf("%s: Invalid Service Name or Port\n", pargs.service_port);
    return;
  }
}

canonical_original_code:
void read_args(int argc, char *args[]) {
  struct hostent *hostp;
  struct servent *servp;
  unsigned long inaddr;
  struct { char proxy_port[32]; char service_host[64]; char service_port[32]; } pargs;
  strcpy(pargs.proxy_port, args[1]);
  strcpy(pargs.service_host, args[2]);
  strcpy(pargs.service_port, args[3]);
  int i;
  for (i = 0; i < strlen(pargs.proxy_port); ++i) {
    if (!((*__ctype_b_loc ())[(int) ((pargs.proxy_port[i]))] & (unsigned short) _ISdigit)) {
      break;
    }
  }
  if (strlen(pargs.proxy_port) == i) {
    proxy_port = htons(atoi(pargs.proxy_port));
  }
  else {
    printf("Invalid Args!\n");
    return;
  }
  bzero(&server_host, sizeof(server_host));
  server_host.sin_family = 2;
  if ((inaddr = inet_addr(pargs.service_host)) != ((unsigned int) 0xffffffff))
    bcopy(&inaddr,&server_host.sin_addr,sizeof(inaddr));
  else if ((hostp = gethostbyname(pargs.service_host)) != ((void *)0))
    bcopy(hostp->h_addr_list[0],&server_host.sin_addr,hostp->h_length);
  else {
    printf("%s: Unknown Host\n",pargs.service_host);
    return;
  }
  if ((servp = getservbyname(pargs.service_port, "tcp")) != ((void *)0))
    server_host.sin_port = servp->s_port;
  else if (atoi(pargs.service_port) > 0)
    server_host.sin_port = htons(atoi(pargs.service_port));
  else {
    printf("%s: Invalid Service Name or Port\n", pargs.service_port);
    return;
  }
}

variable_types: struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; } *

variable_types: struct servent { char *s_name; char **s_aliases; int s_port; char *s_proto; } *

variable_types:
inaddr
unsigned long

variable_types: i int


onproxy

decompiled_code:
int __fastcall onproxy(int a1)
{
int v1; // eax
__int64 v2; // rbx
int v3; // er12
__int64 v4; // r15
__int64 v5; // r12
__int64 v6; // r14
int v7; // eax
int v8; // eax
size_t v9; // rax
int fd; // [rsp+Ch] [rbp-10B4h]
fd_set readfds; // [rsp+10h] [rbp-10B0h] BYREF
char buf[4144]; // [rsp+90h] [rbp-1030h] BYREF

fd = socket(2, 1, 0);
if ( fd < 0 )
puts("failed to create socket to host");
v1 = connect(fd, &server_host, 0x10u);
if ( v1 == 101 )
goto LABEL_18;
if ( v1 > 101 )
{
if ( (unsigned int)(v1 - 110) > 1 )
goto LABEL_20;
LABEL_18:
v9 = strlen(buf);
strcpy(&buf[v9], "\r\n");
write(a1, buf, v9 + 2);
return close(a1);
}
if ( v1 )
LABEL_20:
puts("failed to connect to host");
v2 = a1 / 64;
v3 = fd + 63;
v4 = 1LL << (a1 % 64);
if ( fd >= 0 )
v3 = fd;
v5 = v3 >> 6;
v6 = 1LL << (fd % 64);
while ( 1 )
{
do
{
while ( 1 )
{
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[v2] |= v4;
readfds.fds_bits[v5] |= v6;
if ( select(1024, &readfds, 0LL, 0LL, 0LL) < 0 )
puts("select failed");
memset(buf, 0, 0x1000uLL);
if ( (readfds.fds_bits[v2] & v4) != 0 )
break;
if ( (readfds.fds_bits[v5] & v6) != 0 )
goto LABEL_15;
}
v7 = read(a1, buf, 0x1000uLL);
if ( v7 <= 0 )
return close(fd);
write(fd, buf, v7);
}
while ( (readfds.fds_bits[v5] & v6) == 0 );
LABEL_15:
v8 = read(fd, buf, 0x1000uLL);
if ( v8 <= 0 )
return close(fd);
write(a1, buf, v8);
}
}

canonical_decompiled_code:
int __fastcall func11(int a1)
{
int v1; // eax
__int64 v2; // rbx
int v3; // er12
__int64 v4; // r15
__int64 v5; // r12
__int64 v6; // r14
int v7; // eax
int v8; // eax
size_t v9; // rax
int fd; // [rsp+Ch] [rbp-10B4h]
fd_set readfds; // [rsp+10h] [rbp-10B0h] BYREF
char buf[4144]; // [rsp+90h] [rbp-1030h] BYREF

fd = socket(2, 1, 0);
if ( fd < 0 )
puts("failed to create socket to host");
v1 = connect(fd, &server_host, 0x10u);
if ( v1 == 101 )
goto LABEL_18;
if ( v1 > 101 )
{
if ( (unsigned int)(v1 - 110) > 1 )
goto LABEL_20;
LABEL_18:
v9 = strlen(buf);
strcpy(&buf[v9], "\r\n");
write(a1, buf, v9 + 2);
return close(a1);
}
if ( v1 )
LABEL_20:
puts("failed to connect to host");
v2 = a1 / 64;
v3 = fd + 63;
v4 = 1LL << (a1 % 64);
if ( fd >= 0 )
v3 = fd;
v5 = v3 >> 6;
v6 = 1LL << (fd % 64);
while ( 1 )
{
do
{
while ( 1 )
{
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[v2] |= v4;
readfds.fds_bits[v5] |= v6;
if ( select(1024, &readfds, 0LL, 0LL, 0LL) < 0 )
puts("select failed");
memset(buf, 0, 0x1000uLL);
if ( (readfds.fds_bits[v2] & v4) != 0 )
break;
if ( (readfds.fds_bits[v5] & v6) != 0 )
goto LABEL_15;
}
v7 = read(a1, buf, 0x1000uLL);
if ( v7 <= 0 )
return close(fd);
write(fd, buf, v7);
}
while ( (readfds.fds_bits[v5] & v6) == 0 );
LABEL_15:
v8 = read(fd, buf, 0x1000uLL);
if ( v8 <= 0 )
return close(fd);
write(a1, buf, v8);
}
}

original_code:
void onproxy (int usersockfd) {
  int isosockfd;
  fd_set rdfdset;
  int connstat;
  int iolen;
  char buf[4096];
  if ((isosockfd = socket(2,SOCK_STREAM,0)) < 0) puts("failed to create socket to host");
  connstat = connect(isosockfd,(struct sockaddr *) &server_host, sizeof(server_host));
  switch (connstat) {
    case 0:
      break;
    case 101:
    case 110:
    case 111:
      strcat(buf,"\r\n");
      write(usersockfd,buf,strlen(buf));
      close(usersockfd);
      return;
      break;
    default:
      puts("failed to connect to host");
  }
  while (1) {
    do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&rdfdset)->__fds_bits)[0]) : "memory"); } while (0);
    ((void) (((&rdfdset)->__fds_bits)[((usersockfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((usersockfd) % (8 * (int) sizeof (__fd_mask)))))));
    ((void) (((&rdfdset)->__fds_bits)[((isosockfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((isosockfd) % (8 * (int) sizeof (__fd_mask)))))));
    if (select(1024,&rdfdset,((void *)0),((void *)0),((void *)0)) < 0) {
      puts("select failed");
    }
    bzero(&buf, sizeof(buf));
    if (((((&rdfdset)->__fds_bits)[((usersockfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((usersockfd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
      if ((iolen = read(usersockfd,buf,sizeof(buf))) <= 0) break;
      write(isosockfd,buf,iolen);
    }
    if (((((&rdfdset)->__fds_bits)[((isosockfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((isosockfd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
      if ((iolen = read(isosockfd,buf,sizeof(buf))) <= 0) break;
      write(usersockfd,buf,iolen);
    }
  }
  close(isosockfd);
}

canonical_original_code:
void onproxy(int usersockfd) {
  int isosockfd;
  struct fd_set rdfdset;
  int connstat;
  int iolen;
  char buf[4096];
  if ((isosockfd = socket(2,SOCK_STREAM,0)) < 0) puts("failed to create socket to host");
  connstat = connect(isosockfd,(struct sockaddr *) &server_host, sizeof(server_host));
  switch (connstat) {
    case 0:
      break;
    case 101:
    case 110:
    case 111:
      strcat(buf,"\r\n");
      write(usersockfd,buf,strlen(buf));
      close(usersockfd);
      return;
      break;
    default:
      puts("failed to connect to host");
  }
  while (1) {
    do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&rdfdset)->__fds_bits)[0]) : "memory"); } while (0);
    ((void) (((&rdfdset)->__fds_bits)[((usersockfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((usersockfd) % (8 * (int) sizeof (__fd_mask)))))));
    ((void) (((&rdfdset)->__fds_bits)[((isosockfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((isosockfd) % (8 * (int) sizeof (__fd_mask)))))));
    if (select(1024,&rdfdset,((void *)0),((void *)0),((void *)0)) < 0) {
      puts("select failed");
    }
    bzero(&buf, sizeof(buf));
    if (((((&rdfdset)->__fds_bits)[((usersockfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((usersockfd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
      if ((iolen = read(usersockfd,buf,sizeof(buf))) <= 0) break;
      write(isosockfd,buf,iolen);
    }
    if (((((&rdfdset)->__fds_bits)[((isosockfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((isosockfd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
      if ((iolen = read(isosockfd,buf,sizeof(buf))) <= 0) break;
      write(usersockfd,buf,iolen);
    }
  }
  close(isosockfd);
}

variable_types: usersockfd int

variable_types: isosockfd int

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

variable_types: connstat int

variable_types: iolen int

variable_types:
buf
char [4096]


repo: GBert/openwrt-files

decompiled_code:
const char *ftdi_bb_error()
{
return "Can't open FTDI I/O";
}

canonical_decompiled_code:
const char *func0()
{
return "Can't open FTDI I/O";
}

original_code:
char *
ftdi_bb_error(void)
{
 return "Can't open FTDI I/O";
}

return_type: char *


decompiled_code:
int __fastcall ftbi_bb_dumpbuffer(__int64 a1, unsigned int a2)
{
int result; // eax
int v3; // ebx
int v4; // er14
int v5; // edx
__int16 v6; // cx

result = printf(
"%s VALUE=%08ld NBITS=%d MSB=%d IX=%d\n",
dir_2[*(_BYTE *)a1 & 1],
*(_QWORD *)(a1 + 2),
*(unsigned __int8 *)(a1 + 1),
(unsigned __int8)*(&p + 3145),
a2);
if ( a2 )
{
v3 = 4;
do
{
v4 = v3 - 4;
printf("[%02X]", (unsigned int)(v3 - 4));
do
{
v5 = (unsigned __int8)buffer[v4];
if ( *(_BYTE *)a1 )
v6 = *((_WORD *)&p + 1567);
else
v6 = *((_WORD *)&p + 1566);
++v4;
printf(
" %c%c",
((v5 >> *((_WORD *)&p + 1565)) & 1) == 0 ? 95 : 67,
((unsigned __int8)(1 << v6) & (unsigned __int8)v5) == 0 ? 95 : 72);
}
while ( v3 != v4 );
v3 += 4;
result = putchar(10);
}
while ( ((a2 - 1) & 0xFFFFFFFC) + 8 != v3 );
}
return result;
}

original_code:
static void
ftbi_bb_dumpbuffer(struct ftdi_bb_shift *shift, uint32_t ix)
{
 uint8_t c, d, bit;
 static char read[] = "READ", write[] = "WRITE", *dir[] = {write, read};
 printf("%s VALUE=%08" "l" "d" " NBITS=%d MSB=%d IX=%d\n",
  dir[shift->dir & 1], shift->bits, shift->nbits, p.msb_first, ix);
 for (uint32_t i = 0; i < ix; i += (4)) {
  printf("[%02X]", i);
  for (uint32_t j = 0; j < (4); ++j) {
   if (buffer[i + j] & (1 << p.pgc))
    c = 'C';
   else
    c = '_';
   if (shift->dir)
    bit = (1 << p.pgdi);
   else
    bit = (1 << p.pgdo);
   if (buffer[i + j] & bit)
    d = 'H';
   else
    d = '_';
   printf(" %c%c", c, d);
  }
  printf("\n");
 }
}

canonical_original_code:
static void ftbi_bb_dumpbuffer(struct ftdi_bb_shift *shift, unsigned int ix)
{
 unsigned char c; unsigned char d; unsigned char bit;
 static char read[] = "READ"; static char write[] = "WRITE"; static char *dir[] = {write, read};
 printf("%s VALUE=%08" "l" "d" " NBITS=%d MSB=%d IX=%d\n",
  dir[shift->dir & 1], shift->bits, shift->nbits, p.msb_first, ix);
 for (unsigned int i = 0; i < ix; i += (4)) {
  printf("[%02X]", i);
  for (unsigned int j = 0; j < (4); ++j) {
   if (buffer[i + j] & (1 << p.pgc))
    c = 'C';
   else
    c = '_';
   if (shift->dir)
    bit = (1 << p.pgdi);
   else
    bit = (1 << p.pgdo);
   if (buffer[i + j] & bit)
    d = 'H';
   else
    d = '_';
   printf(" %c%c", c, d);
  }
  printf("\n");
 }
}

variable_types: struct ftdi_bb_shift { unsigned char dir; unsigned char nbits; unsigned long bits; } *

variable_types:
ix
unsigned int

variable_types:
c
unsigned char

variable_types:
d
unsigned char

variable_types:
bit
unsigned char

variable_types:
read
char []

variable_types:
write
char []

variable_types:
dir
char *[]

variable_types:
i
unsigned int

variable_types:
j
unsigned int


decompiled_code:
__int64 ftdi_bb_backend()
{
*((_QWORD *)&p + 395) = &ftdi_bb_ops;
return (unsigned __int8)ftdi_bb_ops;
}

canonical_decompiled_code:
__int64 func2()
{
*((_QWORD *)&p + 395) = &ftdi_bb_ops;
return (unsigned __int8)ftdi_bb_ops;
}

original_code:
uint8_t
ftdi_bb_backend(void)
{
 p.io = &ftdi_bb_ops;
 return p.io->type;
}

canonical_original_code:
unsigned char ftdi_bb_backend()
{
 p.io = &ftdi_bb_ops;
 return p.io->type;
}

return_type: unsigned char


decompiled_code:
__int64 __fastcall ftdi_bb_bitmode(unsigned __int8 a1)
{
__int64 v1; // rsi
int v2; // ebx

if ( ddr == a1 )
return 0LL;
v1 = a1;
v2 = 11;
ddr = a1;
while ( (int)ftdi_set_bitmode(&handle, v1, 4LL) < 0 )
{
if ( !--v2 )
return 0xFFFFFFFFLL;
io_usleep(10000LL);
v1 = (unsigned __int8)ddr;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned __int8 a1)
{
__int64 v1; // rsi
int v2; // ebx

if ( ddr == a1 )
return 0LL;
v1 = a1;
v2 = 11;
ddr = a1;
while ( (int)ftdi_set_bitmode(&handle, v1, 4LL) < 0 )
{
if ( !--v2 )
return 0xFFFFFFFFLL;
io_usleep(10000LL);
v1 = (unsigned __int8)ddr;
}
return 0LL;
}

original_code:
int
ftdi_bb_bitmode(uint8_t new_ddr)
{
 if (ddr != new_ddr) {
  int rc, retry = 10;
  ddr = new_ddr;
  while ((rc = ftdi_set_bitmode(&handle, ddr, BITMODE_SYNCBB)) < 0 && retry--)
   io_usleep(10000);
  if (rc < 0) {
   ;
   return -1;
  }
 }
 return 0;
}

canonical_original_code:
int ftdi_bb_bitmode(unsigned char new_ddr)
{
 if (ddr != new_ddr) {
  int rc; int retry = 10;
  ddr = new_ddr;
  while ((rc = ftdi_set_bitmode(&handle, ddr, BITMODE_SYNCBB)) < 0 && retry--)
   io_usleep(10000);
  if (rc < 0) {
   ;
   return -1;
  }
 }
 return 0;
}

variable_types: rc int

variable_types: retry int


decompiled_code:
__int64 __fastcall ftdi_bb_io(_BYTE *a1)
{
int v2; // edi
unsigned __int8 v3; // dl
__int64 result; // rax
char v5[9]; // [rsp+Fh] [rbp-9h] BYREF

v2 = 1 << a1[1];
if ( *a1 )
{
v3 = ~(_BYTE)v2 & ddr;
}
else
{
v3 = v2 | ddr;
if ( a1[2] )
latch |= v2;
else
latch &= ~(_BYTE)v2;
}
if ( (int)ftdi_bb_bitmode(v3) < 0
|| (int)ftdi_write_data(&handle, &latch, 1LL) < 0
|| (int)ftdi_read_data(&handle, v5, 1LL) < 0 )
{
return 0xFFFFFFFFLL;
}
result = 0LL;
if ( *a1 )
{
a1[2] = (int)(unsigned __int8)v5[0] >> a1[1];
a1[2] &= 1u;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(_BYTE *a1)
{
int v2; // edi
unsigned __int8 v3; // dl
__int64 result; // rax
char v5[9]; // [rsp+Fh] [rbp-9h] BYREF

v2 = 1 << a1[1];
if ( *a1 )
{
v3 = ~(_BYTE)v2 & ddr;
}
else
{
v3 = v2 | ddr;
if ( a1[2] )
latch |= v2;
else
latch &= ~(_BYTE)v2;
}
if ( (int)func3(v3) < 0
|| (int)ftdi_write_data(&handle, &latch, 1LL) < 0
|| (int)ftdi_read_data(&handle, v5, 1LL) < 0 )
{
return 0xFFFFFFFFLL;
}
result = 0LL;
if ( *a1 )
{
a1[2] = (int)(unsigned __int8)v5[0] >> a1[1];
a1[2] &= 1u;
}
return result;
}

original_code:
int
ftdi_bb_io(struct ftdi_bb_io *io)
{
 uint8_t new_ddr = ddr, port;
 if (io->dir) {
  new_ddr &= ~(1 << io->pin);
 } else {
  new_ddr |= (1 << io->pin);
  if (io->bit)
   latch |= (1 << io->pin);
  else
   latch &= ~(1 << io->pin);
 }
 if (ftdi_bb_bitmode(new_ddr) < 0)
  return -1;
 if (ftdi_write_data(&handle, &latch, 1) < 0) {
  ;
  return -1;
 }
 if (ftdi_read_data(&handle, &port, 1) < 0) {
  ;
  return -1;
 }
 if (io->dir)
  io->bit = (port & (1 << io->pin)) != 0;
 return 0;
}

canonical_original_code:
int ftdi_bb_io(struct ftdi_bb_io *io)
{
 unsigned char new_ddr = ddr; unsigned char port;
 if (io->dir) {
  new_ddr &= ~(1 << io->pin);
 } else {
  new_ddr |= (1 << io->pin);
  if (io->bit)
   latch |= (1 << io->pin);
  else
   latch &= ~(1 << io->pin);
 }
 if (ftdi_bb_bitmode(new_ddr) < 0)
  return -1;
 if (ftdi_write_data(&handle, &latch, 1) < 0) {
  ;
  return -1;
 }
 if (ftdi_read_data(&handle, &port, 1) < 0) {
  ;
  return -1;
 }
 if (io->dir)
  io->bit = (port & (1 << io->pin)) != 0;
 return 0;
}

variable_types: struct ftdi_bb_io { unsigned char dir; unsigned char pin; unsigned char bit; } *

variable_types:
port
unsigned char