original_code:
static void timer_tick(struct em8051 *aCPU)
{
    uint8_t increment;
    uint16_t v;
    if ((aCPU->mSFR[REG_TMOD] & (TMODMASK_M0_0 | TMODMASK_M1_0)) == (TMODMASK_M0_0 | TMODMASK_M1_0))
    {
        increment = 0;
        if (!(aCPU->mSFR[REG_TMOD] & TMODMASK_GATE_0) &&
            (aCPU->mSFR[REG_TCON] & TCONMASK_TR0))
        {
            if (aCPU->mSFR[REG_TMOD] & TMODMASK_CT_0)
            {
                increment = 0;
            }
            else
            {
                increment = 1;
            }
        }
        if (increment)
        {
            v = aCPU->mSFR[REG_TL0];
            v++;
            aCPU->mSFR[REG_TL0] = v & 0xff;
            if (v > 0xff)
            {
                aCPU->mSFR[REG_TCON] |= TCONMASK_TF0;
            }
        }
        increment = 0;
        if (!(aCPU->mSFR[REG_TMOD] & TMODMASK_GATE_1) &&
            (aCPU->mSFR[REG_TCON] & TCONMASK_TR1))
        {
            if (aCPU->mSFR[REG_TMOD] & TMODMASK_CT_1)
            {
                increment = 0;
            }
            else
            {
                increment = 1;
            }
        }
        if (increment)
        {
            v = aCPU->mSFR[REG_TH0];
            v++;
            aCPU->mSFR[REG_TH0] = v & 0xff;
            if (v > 0xff)
            {
                aCPU->mSFR[REG_TCON] |= TCONMASK_TF1;
            }
        }
    }
    {
        increment = 0;
        if (!(aCPU->mSFR[REG_TMOD] & TMODMASK_GATE_0) &&
            (aCPU->mSFR[REG_TCON] & TCONMASK_TR0))
        {
            if (aCPU->mSFR[REG_TMOD] & TMODMASK_CT_0)
            {
                increment = 0;
            }
            else
            {
                increment = 1;
            }
        }
        if (increment)
        {
            switch (aCPU->mSFR[REG_TMOD] & (TMODMASK_M0_0 | TMODMASK_M1_0))
            {
            case 0:
                v = aCPU->mSFR[REG_TL0] & 0x1f;
                v++;
                aCPU->mSFR[REG_TL0] = (aCPU->mSFR[REG_TL0] & ~0x1f) | (v & 0x1f);
                if (v > 0x1f)
                {
                    v = aCPU->mSFR[REG_TH0];
                    v++;
                    aCPU->mSFR[REG_TH0] = v & 0xff;
                    if (v > 0xff)
                    {
                        aCPU->mSFR[REG_TCON] |= TCONMASK_TF0;
                    }
                }
                break;
            case TMODMASK_M0_0:
                v = aCPU->mSFR[REG_TL0];
                v++;
                aCPU->mSFR[REG_TL0] = v & 0xff;
                if (v > 0xff)
                {
                    v = aCPU->mSFR[REG_TH0];
                    v++;
                    aCPU->mSFR[REG_TH0] = v & 0xff;
                    if (v > 0xff)
                    {
                        aCPU->mSFR[REG_TCON] |= TCONMASK_TF0;
                    }
                }
                break;
            case TMODMASK_M1_0:
                v = aCPU->mSFR[REG_TL0];
                v++;
                aCPU->mSFR[REG_TL0] = v & 0xff;
                if (v > 0xff)
                {
                    aCPU->mSFR[REG_TL0] = aCPU->mSFR[REG_TH0];
                    aCPU->mSFR[REG_TCON] |= TCONMASK_TF0;
                }
                break;
            default:
                break;
            }
        }
    }
    {
        increment = 0;
        if (!(aCPU->mSFR[REG_TMOD] & TMODMASK_GATE_1) &&
            (aCPU->mSFR[REG_TCON] & TCONMASK_TR1))
        {
            if (aCPU->mSFR[REG_TMOD] & TMODMASK_CT_1)
            {
                increment = 0;
            }
            else
            {
                increment = 1;
            }
        }
        if (increment)
        {
            switch (aCPU->mSFR[REG_TMOD] & (TMODMASK_M0_1 | TMODMASK_M1_1))
            {
            case 0:
                v = aCPU->mSFR[REG_TL1] & 0x1f;
                v++;
                aCPU->mSFR[REG_TL1] = (aCPU->mSFR[REG_TL1] & ~0x1f) | (v & 0x1f);
                if (v > 0x1f)
                {
                    v = aCPU->mSFR[REG_TH1];
                    v++;
                    aCPU->mSFR[REG_TH1] = v & 0xff;
                    if (v > 0xff)
                    {
                    if (!((aCPU->mSFR[REG_TMOD] & (TMODMASK_M0_0 | TMODMASK_M1_0) ) == (TMODMASK_M0_0 | TMODMASK_M1_0)))
                        aCPU->mSFR[REG_TCON] |= TCONMASK_TF1;
                    }
                }
                break;
            case TMODMASK_M0_1:
                v = aCPU->mSFR[REG_TL1];
                v++;
                aCPU->mSFR[REG_TL1] = v & 0xff;
                if (v > 0xff)
                {
                    v = aCPU->mSFR[REG_TH1];
                    v++;
                    aCPU->mSFR[REG_TH1] = v & 0xff;
                    if (v > 0xff)
                    {
                        if (!((aCPU->mSFR[REG_TMOD] & (TMODMASK_M0_0 | TMODMASK_M1_0)) == (TMODMASK_M0_0 | TMODMASK_M1_0)))
                            aCPU->mSFR[REG_TCON] |= TCONMASK_TF1;
                    }
                }
                break;
            case TMODMASK_M1_1:
                v = aCPU->mSFR[REG_TL1];
                v++;
                aCPU->mSFR[REG_TL1] = v & 0xff;
                if (v > 0xff)
                {
                    aCPU->mSFR[REG_TL1] = aCPU->mSFR[REG_TH1];
                    if (!((aCPU->mSFR[REG_TMOD] & (TMODMASK_M0_0 | TMODMASK_M1_0) ) == (TMODMASK_M0_0 | TMODMASK_M1_0)))
                        aCPU->mSFR[REG_TCON] |= TCONMASK_TF1;
                }
                break;
            default:
                break;
            }
            if (aCPU->mSFR[REG_TCON] & TCONMASK_TF1) {
                if (aCPU->mSFR[REG_SCON] & SCONMASK_SM1) {
                    serial_tx(aCPU);
      aCPU->mSFR[REG_TCON] &= ~TCONMASK_TF1;
                }
            }
        }
    }
}

canonical_original_code:
static void timer_tick(struct em8051 *aCPU)
{
    unsigned char increment;
    unsigned short v;
    if ((aCPU->mSFR[REG_TMOD] & (TMODMASK_M0_0 | TMODMASK_M1_0)) == (TMODMASK_M0_0 | TMODMASK_M1_0))
    {
        increment = 0;
        if (!(aCPU->mSFR[REG_TMOD] & TMODMASK_GATE_0) &&
            (aCPU->mSFR[REG_TCON] & TCONMASK_TR0))
        {
            if (aCPU->mSFR[REG_TMOD] & TMODMASK_CT_0)
            {
                increment = 0;
            }
            else
            {
                increment = 1;
            }
        }
        if (increment)
        {
            v = aCPU->mSFR[REG_TL0];
            v++;
            aCPU->mSFR[REG_TL0] = v & 0xff;
            if (v > 0xff)
            {
                aCPU->mSFR[REG_TCON] |= TCONMASK_TF0;
            }
        }
        increment = 0;
        if (!(aCPU->mSFR[REG_TMOD] & TMODMASK_GATE_1) &&
            (aCPU->mSFR[REG_TCON] & TCONMASK_TR1))
        {
            if (aCPU->mSFR[REG_TMOD] & TMODMASK_CT_1)
            {
                increment = 0;
            }
            else
            {
                increment = 1;
            }
        }
        if (increment)
        {
            v = aCPU->mSFR[REG_TH0];
            v++;
            aCPU->mSFR[REG_TH0] = v & 0xff;
            if (v > 0xff)
            {
                aCPU->mSFR[REG_TCON] |= TCONMASK_TF1;
            }
        }
    }
    {
        increment = 0;
        if (!(aCPU->mSFR[REG_TMOD] & TMODMASK_GATE_0) &&
            (aCPU->mSFR[REG_TCON] & TCONMASK_TR0))
        {
            if (aCPU->mSFR[REG_TMOD] & TMODMASK_CT_0)
            {
                increment = 0;
            }
            else
            {
                increment = 1;
            }
        }
        if (increment)
        {
            switch (aCPU->mSFR[REG_TMOD] & (TMODMASK_M0_0 | TMODMASK_M1_0))
            {
            case 0:
                v = aCPU->mSFR[REG_TL0] & 0x1f;
                v++;
                aCPU->mSFR[REG_TL0] = (aCPU->mSFR[REG_TL0] & ~0x1f) | (v & 0x1f);
                if (v > 0x1f)
                {
                    v = aCPU->mSFR[REG_TH0];
                    v++;
                    aCPU->mSFR[REG_TH0] = v & 0xff;
                    if (v > 0xff)
                    {
                        aCPU->mSFR[REG_TCON] |= TCONMASK_TF0;
                    }
                }
                break;
            case TMODMASK_M0_0:
                v = aCPU->mSFR[REG_TL0];
                v++;
                aCPU->mSFR[REG_TL0] = v & 0xff;
                if (v > 0xff)
                {
                    v = aCPU->mSFR[REG_TH0];
                    v++;
                    aCPU->mSFR[REG_TH0] = v & 0xff;
                    if (v > 0xff)
                    {
                        aCPU->mSFR[REG_TCON] |= TCONMASK_TF0;
                    }
                }
                break;
            case TMODMASK_M1_0:
                v = aCPU->mSFR[REG_TL0];
                v++;
                aCPU->mSFR[REG_TL0] = v & 0xff;
                if (v > 0xff)
                {
                    aCPU->mSFR[REG_TL0] = aCPU->mSFR[REG_TH0];
                    aCPU->mSFR[REG_TCON] |= TCONMASK_TF0;
                }
                break;
            default:
                break;
            }
        }
    }
    {
        increment = 0;
        if (!(aCPU->mSFR[REG_TMOD] & TMODMASK_GATE_1) &&
            (aCPU->mSFR[REG_TCON] & TCONMASK_TR1))
        {
            if (aCPU->mSFR[REG_TMOD] & TMODMASK_CT_1)
            {
                increment = 0;
            }
            else
            {
                increment = 1;
            }
        }
        if (increment)
        {
            switch (aCPU->mSFR[REG_TMOD] & (TMODMASK_M0_1 | TMODMASK_M1_1))
            {
            case 0:
                v = aCPU->mSFR[REG_TL1] & 0x1f;
                v++;
                aCPU->mSFR[REG_TL1] = (aCPU->mSFR[REG_TL1] & ~0x1f) | (v & 0x1f);
                if (v > 0x1f)
                {
                    v = aCPU->mSFR[REG_TH1];
                    v++;
                    aCPU->mSFR[REG_TH1] = v & 0xff;
                    if (v > 0xff)
                    {
                    if (!((aCPU->mSFR[REG_TMOD] & (TMODMASK_M0_0 | TMODMASK_M1_0) ) == (TMODMASK_M0_0 | TMODMASK_M1_0)))
                        aCPU->mSFR[REG_TCON] |= TCONMASK_TF1;
                    }
                }
                break;
            case TMODMASK_M0_1:
                v = aCPU->mSFR[REG_TL1];
                v++;
                aCPU->mSFR[REG_TL1] = v & 0xff;
                if (v > 0xff)
                {
                    v = aCPU->mSFR[REG_TH1];
                    v++;
                    aCPU->mSFR[REG_TH1] = v & 0xff;
                    if (v > 0xff)
                    {
                        if (!((aCPU->mSFR[REG_TMOD] & (TMODMASK_M0_0 | TMODMASK_M1_0)) == (TMODMASK_M0_0 | TMODMASK_M1_0)))
                            aCPU->mSFR[REG_TCON] |= TCONMASK_TF1;
                    }
                }
                break;
            case TMODMASK_M1_1:
                v = aCPU->mSFR[REG_TL1];
                v++;
                aCPU->mSFR[REG_TL1] = v & 0xff;
                if (v > 0xff)
                {
                    aCPU->mSFR[REG_TL1] = aCPU->mSFR[REG_TH1];
                    if (!((aCPU->mSFR[REG_TMOD] & (TMODMASK_M0_0 | TMODMASK_M1_0) ) == (TMODMASK_M0_0 | TMODMASK_M1_0)))
                        aCPU->mSFR[REG_TCON] |= TCONMASK_TF1;
                }
                break;
            default:
                break;
            }
            if (aCPU->mSFR[REG_TCON] & TCONMASK_TF1) {
                if (aCPU->mSFR[REG_SCON] & SCONMASK_SM1) {
                    serial_tx(aCPU);
      aCPU->mSFR[REG_TCON] &= ~TCONMASK_TF1;
                }
            }
        }
    }
}

variable_types:
increment
unsigned char

variable_types:
v
unsigned short


original_code:
void handle_interrupts(struct em8051 *aCPU)
{
    int16_t dest_ip = -1;
    uint8_t hi = 0;
    uint8_t lo = 0;
    if (aCPU->mInterruptActive > 1)
        return;
    if (aCPU->mSFR[REG_IE] & IEMASK_EA)
    {
        if (aCPU->mSFR[REG_IE] & IEMASK_EX0 && aCPU->mSFR[REG_TCON] & TCONMASK_IE0)
        {
            dest_ip = ISR_INT0;
            if (aCPU->mSFR[REG_IP] & IPMASK_PX0)
                hi = 1;
            lo = 1;
        }
        if (aCPU->mSFR[REG_IE] & IEMASK_ET0 && aCPU->mSFR[REG_TCON] & TCONMASK_TF0 && !hi)
        {
            if (!lo)
            {
                dest_ip = ISR_TF0;
                lo = 1;
            }
            if (aCPU->mSFR[REG_IP] & IPMASK_PT0)
            {
                hi = 1;
                dest_ip = ISR_TF0;
            }
        }
        if (aCPU->mSFR[REG_IE] & IEMASK_EX1 && aCPU->mSFR[REG_TCON] & TCONMASK_IE1 && !hi)
        {
            if (!lo)
            {
                dest_ip = ISR_INT1;
                lo = 1;
            }
            if (aCPU->mSFR[REG_IP] & IPMASK_PX1)
            {
                hi = 1;
                dest_ip = ISR_INT1;
            }
        }
        if (aCPU->mSFR[REG_IE] & IEMASK_ET1 && aCPU->mSFR[REG_TCON] & TCONMASK_TF1 && !hi)
        {
            if (!lo)
            {
                dest_ip = ISR_TF1;
                lo = 1;
            }
            if (aCPU->mSFR[REG_IP] & IPMASK_PT1)
            {
                hi = 1;
                dest_ip = ISR_TF1;
            }
        }
        if (aCPU->mSFR[REG_IE] & IEMASK_ES && aCPU->serial_interrupt_trigger && !hi)
        {
            if (!lo)
            {
                dest_ip = ISR_SR;
                lo = 1;
            }
            if (aCPU->mSFR[REG_IP] & IPMASK_PS)
            {
                hi = 1;
                dest_ip = ISR_SR;
            }
        }
    }
    if (dest_ip == -1)
        return;
    if (aCPU->mInterruptActive == 1 && !hi)
        return;
    aCPU->mSFR[REG_PCON] &= ~0x01;
    push_to_stack(aCPU, aCPU->mPC & 0xff);
    push_to_stack(aCPU, aCPU->mPC >> 8);
    aCPU->mPC = dest_ip;
    aCPU->mTickDelay = 2;
    switch (dest_ip)
    {
    case ISR_TF0:
        aCPU->mSFR[REG_TCON] &= ~TCONMASK_TF0;
        break;
    case ISR_TF1:
        aCPU->mSFR[REG_TCON] &= ~TCONMASK_TF1;
        break;
    case ISR_SR:
        aCPU->serial_interrupt_trigger = 0;
        break;
    }
    if (hi)
    {
        aCPU->mInterruptActive |= 2;
    }
    else
    {
        aCPU->mInterruptActive = 1;
    }
    aCPU->int_a[hi] = aCPU->mSFR[REG_ACC];
    aCPU->int_psw[hi] = aCPU->mSFR[REG_PSW];
    aCPU->int_sp[hi] = aCPU->mSFR[REG_SP];
}

canonical_original_code:
void handle_interrupts(struct em8051 *aCPU)
{
    short dest_ip = -1;
    unsigned char hi = 0;
    unsigned char lo = 0;
    if (aCPU->mInterruptActive > 1)
        return;
    if (aCPU->mSFR[REG_IE] & IEMASK_EA)
    {
        if (aCPU->mSFR[REG_IE] & IEMASK_EX0 && aCPU->mSFR[REG_TCON] & TCONMASK_IE0)
        {
            dest_ip = ISR_INT0;
            if (aCPU->mSFR[REG_IP] & IPMASK_PX0)
                hi = 1;
            lo = 1;
        }
        if (aCPU->mSFR[REG_IE] & IEMASK_ET0 && aCPU->mSFR[REG_TCON] & TCONMASK_TF0 && !hi)
        {
            if (!lo)
            {
                dest_ip = ISR_TF0;
                lo = 1;
            }
            if (aCPU->mSFR[REG_IP] & IPMASK_PT0)
            {
                hi = 1;
                dest_ip = ISR_TF0;
            }
        }
        if (aCPU->mSFR[REG_IE] & IEMASK_EX1 && aCPU->mSFR[REG_TCON] & TCONMASK_IE1 && !hi)
        {
            if (!lo)
            {
                dest_ip = ISR_INT1;
                lo = 1;
            }
            if (aCPU->mSFR[REG_IP] & IPMASK_PX1)
            {
                hi = 1;
                dest_ip = ISR_INT1;
            }
        }
        if (aCPU->mSFR[REG_IE] & IEMASK_ET1 && aCPU->mSFR[REG_TCON] & TCONMASK_TF1 && !hi)
        {
            if (!lo)
            {
                dest_ip = ISR_TF1;
                lo = 1;
            }
            if (aCPU->mSFR[REG_IP] & IPMASK_PT1)
            {
                hi = 1;
                dest_ip = ISR_TF1;
            }
        }
        if (aCPU->mSFR[REG_IE] & IEMASK_ES && aCPU->serial_interrupt_trigger && !hi)
        {
            if (!lo)
            {
                dest_ip = ISR_SR;
                lo = 1;
            }
            if (aCPU->mSFR[REG_IP] & IPMASK_PS)
            {
                hi = 1;
                dest_ip = ISR_SR;
            }
        }
    }
    if (dest_ip == -1)
        return;
    if (aCPU->mInterruptActive == 1 && !hi)
        return;
    aCPU->mSFR[REG_PCON] &= ~0x01;
    push_to_stack(aCPU, aCPU->mPC & 0xff);
    push_to_stack(aCPU, aCPU->mPC >> 8);
    aCPU->mPC = dest_ip;
    aCPU->mTickDelay = 2;
    switch (dest_ip)
    {
    case ISR_TF0:
        aCPU->mSFR[REG_TCON] &= ~TCONMASK_TF0;
        break;
    case ISR_TF1:
        aCPU->mSFR[REG_TCON] &= ~TCONMASK_TF1;
        break;
    case ISR_SR:
        aCPU->serial_interrupt_trigger = 0;
        break;
    }
    if (hi)
    {
        aCPU->mInterruptActive |= 2;
    }
    else
    {
        aCPU->mInterruptActive = 1;
    }
    aCPU->int_a[hi] = aCPU->mSFR[REG_ACC];
    aCPU->int_psw[hi] = aCPU->mSFR[REG_PSW];
    aCPU->int_sp[hi] = aCPU->mSFR[REG_SP];
}

variable_types: struct em8051 { unsigned char *mCodeMem; unsigned short mCodeMemMaxIdx; unsigned char *mExtData; unsigned short mExtDataMaxIdx; unsigned char mLowerData[128]; unsigned char *mUpperData; unsigned char mSFR[128]; unsigned short mPC; unsigned char mTickDelay; unsigned char (*op[256])(struct em8051 *aCPU); unsigned char (*dec[256])(struct em8051 *aCPU, unsigned short aPosition, char *aBuffer); void (*except)(struct em8051 *aCPU, int aCode); unsigned char (*sfrread[128])(struct em8051 *aCPU, unsigned char aRegister); void (*sfrwrite[128])(struct em8051 *aCPU, unsigned char aRegister); unsigned char (*xread)(struct em8051 *aCPU, unsigned short aAddress); void (*xwrite)(struct em8051 *aCPU, unsigned short aAddress, unsigned char aValue); unsigned char mInterruptActive; unsigned char int_a[2]; unsigned char int_psw[2]; unsigned char int_sp[2]; char serial_out[18]; unsigned char serial_out_idx; unsigned char serial_out_remaining_bits; _Bool serial_interrupt_trigger; } *

variable_types:
hi
unsigned char

variable_types:
lo
unsigned char

return_type: void


tick

original_code:
_Bool tick(struct em8051 *aCPU)
{
    uint8_t v;
    _Bool ticked = 0;
    if (aCPU->mTickDelay)
    {
        aCPU->mTickDelay--;
    }
    if (aCPU->mTickDelay == 0 && (aCPU->mSFR[REG_PCON]) & 0x02) {
        aCPU->mTickDelay = 1;
        return 1;
    }
    if (aCPU->mTickDelay == 0)
    {
        handle_interrupts(aCPU);
    }
    if (aCPU->mTickDelay == 0)
    {
        _Bool is_idle = (aCPU->mSFR[REG_PCON]) & 0x01;
        if (is_idle) {
            aCPU->mTickDelay = 1;
        } else {
            aCPU->mTickDelay = aCPU->op[aCPU->mCodeMem[aCPU->mPC & (aCPU->mCodeMemMaxIdx)]](aCPU);
        }
        ticked = 1;
        v = aCPU->mSFR[REG_ACC];
        v ^= v >> 4;
        v &= 0xf;
        v = (0x6996 >> v) & 1;
        aCPU->mSFR[REG_PSW] = (aCPU->mSFR[REG_PSW] & ~PSWMASK_P) | (v * PSWMASK_P);
    }
    timer_tick(aCPU);
    return ticked;
}

canonical_original_code:
_Bool tick(struct em8051 *aCPU)
{
    unsigned char v;
    _Bool ticked = 0;
    if (aCPU->mTickDelay)
    {
        aCPU->mTickDelay--;
    }
    if (aCPU->mTickDelay == 0 && (aCPU->mSFR[REG_PCON]) & 0x02) {
        aCPU->mTickDelay = 1;
        return 1;
    }
    if (aCPU->mTickDelay == 0)
    {
        handle_interrupts(aCPU);
    }
    if (aCPU->mTickDelay == 0)
    {
        _Bool is_idle = (aCPU->mSFR[REG_PCON]) & 0x01;
        if (is_idle) {
            aCPU->mTickDelay = 1;
        } else {
            aCPU->mTickDelay = aCPU->op[aCPU->mCodeMem[aCPU->mPC & (aCPU->mCodeMemMaxIdx)]](aCPU);
        }
        ticked = 1;
        v = aCPU->mSFR[REG_ACC];
        v ^= v >> 4;
        v &= 0xf;
        v = (0x6996 >> v) & 1;
        aCPU->mSFR[REG_PSW] = (aCPU->mSFR[REG_PSW] & ~PSWMASK_P) | (v * PSWMASK_P);
    }
    timer_tick(aCPU);
    return ticked;
}

variable_types:
v
unsigned char

user_defined_types: struct em8051 { unsigned char *mCodeMem; unsigned short mCodeMemMaxIdx; unsigned char *mExtData; unsigned short mExtDataMaxIdx; unsigned char mLowerData[128]; unsigned char *mUpperData; unsigned char mSFR[128]; unsigned short mPC; unsigned char mTickDelay; unsigned char (*op[256])(struct em8051 *aCPU); unsigned char (*dec[256])(struct em8051 *aCPU, unsigned short aPosition, char *aBuffer); void (*except)(struct em8051 *aCPU, int aCode); unsigned char (*sfrread[128])(struct em8051 *aCPU, unsigned char aRegister); void (*sfrwrite[128])(struct em8051 *aCPU, unsigned char aRegister); unsigned char (*xread)(struct em8051 *aCPU, unsigned short aAddress); void (*xwrite)(struct em8051 *aCPU, unsigned short aAddress, unsigned char aValue); unsigned char mInterruptActive; unsigned char int_a[2]; unsigned char int_psw[2]; unsigned char int_sp[2]; char serial_out[18]; unsigned char serial_out_idx; unsigned char serial_out_remaining_bits; _Bool serial_interrupt_trigger; }


decode

decompiled_code:
__int64 __fastcall decode(__int64 a1, unsigned __int16 a2, char *a3)
{
char v3; // al
__int64 result; // rax

v3 = *(_BYTE *)(a1 + 175);
if ( (v3 & 1) != 0 )
{
strcpy(a3, "IDLE");
result = 0LL;
}
else if ( (v3 & 2) != 0 )
{
strcpy(a3, "POWER DOWN");
result = 0LL;
}
else
{
result = (*(__int64 (__fastcall **)(__int64, _QWORD))(a1
+ 8LL
* *(unsigned __int8 *)(*(_QWORD *)a1
+ (unsigned __int16)(*(_WORD *)(a1 + 8) & a2))
+ 2352))(
a1,
a2);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, unsigned __int16 a2, char *a3)
{
char v3; // al
__int64 result; // rax

v3 = *(_BYTE *)(a1 + 175);
if ( (v3 & 1) != 0 )
{
strcpy(a3, "IDLE");
result = 0LL;
}
else if ( (v3 & 2) != 0 )
{
strcpy(a3, "POWER DOWN");
result = 0LL;
}
else
{
result = (*(__int64 (__fastcall **)(__int64, _QWORD))(a1
+ 8LL
* *(unsigned __int8 *)(*(_QWORD *)a1
+ (unsigned __int16)(*(_WORD *)(a1 + 8) & a2))
+ 2352))(
a1,
a2);
}
return result;
}

original_code:
uint8_t decode(struct em8051 *aCPU, uint16_t aPosition, char *aBuffer)
{
    _Bool is_idle = (aCPU->mSFR[REG_PCON]) & 0x01;
    if (is_idle) {
        strcpy(aBuffer, "IDLE");
        return 0;
    }
    _Bool is_powerdown = (aCPU->mSFR[REG_PCON]) & 0x02;
    if (is_powerdown) {
        strcpy(aBuffer, "POWER DOWN");
        return 0;
    }
    return aCPU->dec[aCPU->mCodeMem[aPosition & (aCPU->mCodeMemMaxIdx)]](aCPU, aPosition, aBuffer);
}

canonical_original_code:
unsigned char decode(struct em8051 *aCPU, unsigned short aPosition, char *aBuffer)
{
    _Bool is_idle = (aCPU->mSFR[REG_PCON]) & 0x01;
    if (is_idle) {
        strcpy(aBuffer, "IDLE");
        return 0;
    }
    _Bool is_powerdown = (aCPU->mSFR[REG_PCON]) & 0x02;
    if (is_powerdown) {
        strcpy(aBuffer, "POWER DOWN");
        return 0;
    }
    return aCPU->dec[aCPU->mCodeMem[aPosition & (aCPU->mCodeMemMaxIdx)]](aCPU, aPosition, aBuffer);
}

variable_types:
aPosition
unsigned short

variable_types:
aBuffer
char *

return_type: unsigned char


reset

original_code:
void reset(struct em8051 *aCPU, _Bool aWipe)
{
    if (aWipe)
    {
        memset(aCPU->mCodeMem, 0, aCPU->mCodeMemMaxIdx+1);
        memset(aCPU->mExtData, 0, aCPU->mExtDataMaxIdx+1);
        memset(aCPU->mLowerData, 0, 128);
        if (aCPU->mUpperData)
            memset(aCPU->mUpperData, 0, 128);
    }
    memset(aCPU->mSFR, 0, 128);
    aCPU->mPC = 0;
    aCPU->mTickDelay = 0;
    aCPU->mSFR[REG_SP] = 7;
    aCPU->mSFR[REG_P0] = 0xff;
    aCPU->mSFR[REG_P1] = 0xff;
    aCPU->mSFR[REG_P2] = 0xff;
    aCPU->mSFR[REG_P3] = 0xff;
    if (aWipe)
        aCPU->mSFR[REG_PCON] |= (1<<4);
    if (aWipe)
        aCPU->mSFR[REG_SBUF] = rand();
    disasm_setptrs(aCPU);
    op_setptrs(aCPU);
    aCPU->mInterruptActive = 0;
    aCPU->serial_interrupt_trigger = 0;
    aCPU->serial_out_remaining_bits = 0;
}


decompiled_code:
int wipe_memeditor_view()
{
__int64 i; // rbx
WINDOW *v1; // rdi
int result; // eax

for ( i = 0LL; i != 30; result = delwin(*(WINDOW **)(i * 8 + 2896)) )
{
v1 = (WINDOW *)qword_B88[i];
i += 6LL;
delwin(v1);
}
return result;
}

canonical_decompiled_code:
int func0()
{
__int64 i; // rbx
WINDOW *v1; // rdi
int result; // eax

for ( i = 0LL; i != 30; result = delwin(*(WINDOW **)(i * 8 + 2896)) )
{
v1 = (WINDOW *)qword_B88[i];
i += 6LL;
delwin(v1);
}
return result;
}

original_code:
void wipe_memeditor_view()
{
    int i;
    for (i = 0; i < 5; i++)
    {
        delwin(eds[i].view);
        delwin(eds[i].box);
    }
}

variable_types: i int


decompiled_code:
int __fastcall build_memeditor_view(__int64 a1)
{
WINDOW *v1; // rax
chtype v2; // rsi
chtype v3; // rcx
WINDOW *v4; // rax
chtype v5; // rsi
chtype v6; // rcx
WINDOW *v7; // rax
__int64 v8; // rdx
WINDOW *v9; // rax
chtype v10; // rsi
chtype v11; // rcx
WINDOW *v12; // rax
chtype v13; // rsi
chtype v14; // rcx
__int64 v15; // rax
WINDOW *v16; // rax
chtype v17; // rsi
chtype v18; // rcx
__int64 v19; // rax

werase(stdscr);
dword_B98 = LINES / 3;
v1 = subwin(stdscr, LINES / 3, 40, 0, 0);
v2 = LODWORD(acs_map[60]);
v3 = HIDWORD(acs_map[56]);
eds = v1;
wborder(v1, v2, (unsigned int)v2, v3, (unsigned int)v3, 0LL, 0LL, 0LL, 0LL);
if ( wmove(eds, 0, 2) != -1 )
waddnstr(eds, "Lower", -1);
qword_B88[0] = (__int64)subwin(eds, dword_B98 - 2, 38, 1, 1);
qword_B90 = a1 + 26;
qword_BA4 = 128LL;
dword_BC8 = LINES / 3;
v4 = subwin(stdscr, LINES / 3, 40, dword_B98, 0);
v5 = LODWORD(acs_map[60]);
v6 = HIDWORD(acs_map[56]);
qword_BB0 = v4;
wborder(v4, v5, (unsigned int)v5, v6, (unsigned int)v6, 0LL, 0LL, 0LL, 0LL);
if ( wmove(qword_BB0, 0, 2) != -1 )
waddnstr(qword_BB0, "Upper", -1);
v7 = subwin(qword_BB0, dword_BC8 - 2, 38, dword_B98 + 1, 1);
v8 = *(_QWORD *)(a1 + 160);
dword_BD8 = 128;
qword_BB8 = (__int64)v7;
qword_BC0 = v8;
dword_BF8 = LINES - (dword_B98 + dword_BC8);
dword_BD4 = (v8 != 0) << 7;
v9 = subwin(stdscr, LINES - (dword_B98 + dword_BC8), 40, dword_B98 + dword_BC8, 0);
v10 = LODWORD(acs_map[60]);
v11 = HIDWORD(acs_map[56]);
qword_BE0 = v9;
wborder(v9, v10, (unsigned int)v10, v11, (unsigned int)v11, 0LL, 0LL, 0LL, 0LL);
if ( wmove(qword_BE0, 0, 2) != -1 )
waddnstr(qword_BE0, "SFR", -1);
qword_BE8 = (__int64)subwin(qword_BE0, dword_BF8 - 2, 38, dword_B98 + dword_BC8 + 1, 1);
qword_BF0 = a1 + 168;
qword_C04 = 0x8000000080LL;
dword_C28 = LINES / 2;
v12 = subwin(stdscr, LINES / 2, 40, 0, 40);
v13 = LODWORD(acs_map[60]);
v14 = HIDWORD(acs_map[56]);
qword_C10 = v12;
wborder(v12, v13, (unsigned int)v13, v14, (unsigned int)v14, 0LL, 0LL, 0LL, 0LL);
if ( wmove(qword_C10, 0, 2) != -1 )
waddnstr(qword_C10, "External", -1);
qword_C18 = (__int64)subwin(qword_C10, dword_C28 - 2, 38, 1, 41);
dword_C34 = *(unsigned __int16 *)(a1 + 24) + 1;
v15 = *(_QWORD *)(a1 + 16);
dword_C38 = 0;
qword_C20 = v15;
dword_C58 = LINES / 2;
v16 = subwin(stdscr, dword_C28, 40, dword_C28, 40);
v17 = LODWORD(acs_map[60]);
v18 = HIDWORD(acs_map[56]);
qword_C40 = v16;
wborder(v16, v17, (unsigned int)v17, v18, (unsigned int)v18, 0LL, 0LL, 0LL, 0LL);
if ( wmove(qword_C40, 0, 2) != -1 )
waddnstr(qword_C40, "ROM", -1);
qword_C48 = (__int64)subwin(qword_C40, dword_C58 - 2, 38, dword_C28 + 1, 41);
dword_C64 = *(unsigned __int16 *)(a1 + 8) + 1;
v19 = *(_QWORD *)a1;
dword_C68 = 0;
qword_C50 = v19;
qword_B9C = 0LL;
qword_BCC = 0LL;
qword_BFC = 0LL;
qword_C2C = 0LL;
qword_C5C = 0LL;
return wrefresh(stdscr);
}

canonical_decompiled_code:
int __fastcall func1(__int64 a1)
{
WINDOW *v1; // rax
chtype v2; // rsi
chtype v3; // rcx
WINDOW *v4; // rax
chtype v5; // rsi
chtype v6; // rcx
WINDOW *v7; // rax
__int64 v8; // rdx
WINDOW *v9; // rax
chtype v10; // rsi
chtype v11; // rcx
WINDOW *v12; // rax
chtype v13; // rsi
chtype v14; // rcx
__int64 v15; // rax
WINDOW *v16; // rax
chtype v17; // rsi
chtype v18; // rcx
__int64 v19; // rax

werase(stdscr);
dword_B98 = LINES / 3;
v1 = subwin(stdscr, LINES / 3, 40, 0, 0);
v2 = LODWORD(acs_map[60]);
v3 = HIDWORD(acs_map[56]);
eds = v1;
wborder(v1, v2, (unsigned int)v2, v3, (unsigned int)v3, 0LL, 0LL, 0LL, 0LL);
if ( wmove(eds, 0, 2) != -1 )
waddnstr(eds, "Lower", -1);
qword_B88[0] = (__int64)subwin(eds, dword_B98 - 2, 38, 1, 1);
qword_B90 = a1 + 26;
qword_BA4 = 128LL;
dword_BC8 = LINES / 3;
v4 = subwin(stdscr, LINES / 3, 40, dword_B98, 0);
v5 = LODWORD(acs_map[60]);
v6 = HIDWORD(acs_map[56]);
qword_BB0 = v4;
wborder(v4, v5, (unsigned int)v5, v6, (unsigned int)v6, 0LL, 0LL, 0LL, 0LL);
if ( wmove(qword_BB0, 0, 2) != -1 )
waddnstr(qword_BB0, "Upper", -1);
v7 = subwin(qword_BB0, dword_BC8 - 2, 38, dword_B98 + 1, 1);
v8 = *(_QWORD *)(a1 + 160);
dword_BD8 = 128;
qword_BB8 = (__int64)v7;
qword_BC0 = v8;
dword_BF8 = LINES - (dword_B98 + dword_BC8);
dword_BD4 = (v8 != 0) << 7;
v9 = subwin(stdscr, LINES - (dword_B98 + dword_BC8), 40, dword_B98 + dword_BC8, 0);
v10 = LODWORD(acs_map[60]);
v11 = HIDWORD(acs_map[56]);
qword_BE0 = v9;
wborder(v9, v10, (unsigned int)v10, v11, (unsigned int)v11, 0LL, 0LL, 0LL, 0LL);
if ( wmove(qword_BE0, 0, 2) != -1 )
waddnstr(qword_BE0, "SFR", -1);
qword_BE8 = (__int64)subwin(qword_BE0, dword_BF8 - 2, 38, dword_B98 + dword_BC8 + 1, 1);
qword_BF0 = a1 + 168;
qword_C04 = 0x8000000080LL;
dword_C28 = LINES / 2;
v12 = subwin(stdscr, LINES / 2, 40, 0, 40);
v13 = LODWORD(acs_map[60]);
v14 = HIDWORD(acs_map[56]);
qword_C10 = v12;
wborder(v12, v13, (unsigned int)v13, v14, (unsigned int)v14, 0LL, 0LL, 0LL, 0LL);
if ( wmove(qword_C10, 0, 2) != -1 )
waddnstr(qword_C10, "External", -1);
qword_C18 = (__int64)subwin(qword_C10, dword_C28 - 2, 38, 1, 41);
dword_C34 = *(unsigned __int16 *)(a1 + 24) + 1;
v15 = *(_QWORD *)(a1 + 16);
dword_C38 = 0;
qword_C20 = v15;
dword_C58 = LINES / 2;
v16 = subwin(stdscr, dword_C28, 40, dword_C28, 40);
v17 = LODWORD(acs_map[60]);
v18 = HIDWORD(acs_map[56]);
qword_C40 = v16;
wborder(v16, v17, (unsigned int)v17, v18, (unsigned int)v18, 0LL, 0LL, 0LL, 0LL);
if ( wmove(qword_C40, 0, 2) != -1 )
waddnstr(qword_C40, "ROM", -1);
qword_C48 = (__int64)subwin(qword_C40, dword_C58 - 2, 38, dword_C28 + 1, 41);
dword_C64 = *(unsigned __int16 *)(a1 + 8) + 1;
v19 = *(_QWORD *)a1;
dword_C68 = 0;
qword_C50 = v19;
qword_B9C = 0LL;
qword_BCC = 0LL;
qword_BFC = 0LL;
qword_C2C = 0LL;
qword_C5C = 0LL;
return wrefresh(stdscr);
}

original_code:
void build_memeditor_view(struct em8051 *aCPU)
{
    werase(stdscr);
    eds[0].lines = (LINES / 3);
    eds[0].box = subwin(stdscr, eds[0].lines, 40, 0, 0);
    wborder(eds[0].box, (acs_map[(unsigned char)(('x'))]), (acs_map[(unsigned char)(('x'))]), (acs_map[(unsigned char)(('q'))]), (acs_map[(unsigned char)(('q'))]), 0, 0, 0, 0);
    (wmove((eds[0].box),(0),(2)) == (-1) ? (-1) : waddnstr((eds[0].box),("Lower"),-1));
    eds[0].view = subwin(eds[0].box, eds[0].lines - 2, 38, 1, 1);
    eds[0].maxmem = 128;
    eds[0].memarea = aCPU->mLowerData;
    eds[0].memviewoffset = 0;
    eds[1].lines = (LINES / 3);
    eds[1].box = subwin(stdscr, eds[1].lines, 40, eds[0].lines, 0);
    wborder(eds[1].box, (acs_map[(unsigned char)(('x'))]), (acs_map[(unsigned char)(('x'))]), (acs_map[(unsigned char)(('q'))]), (acs_map[(unsigned char)(('q'))]), 0, 0, 0, 0);
    (wmove((eds[1].box),(0),(2)) == (-1) ? (-1) : waddnstr((eds[1].box),("Upper"),-1));
    eds[1].view = subwin(eds[1].box, eds[1].lines - 2, 38, eds[0].lines + 1, 1);
    if (aCPU->mUpperData)
        eds[1].maxmem = 128;
    else
        eds[1].maxmem = 0;
    eds[1].memarea = aCPU->mUpperData;
    eds[1].memviewoffset = 128;
    eds[2].lines = LINES - (eds[0].lines + eds[1].lines);
    eds[2].box = subwin(stdscr, eds[2].lines, 40, eds[0].lines + eds[1].lines, 0);
    wborder(eds[2].box, (acs_map[(unsigned char)(('x'))]), (acs_map[(unsigned char)(('x'))]), (acs_map[(unsigned char)(('q'))]), (acs_map[(unsigned char)(('q'))]), 0, 0, 0, 0);
    (wmove((eds[2].box),(0),(2)) == (-1) ? (-1) : waddnstr((eds[2].box),("SFR"),-1));
    eds[2].view = subwin(eds[2].box, eds[2].lines - 2, 38, eds[0].lines + eds[1].lines + 1, 1);
    eds[2].maxmem = 128;
    eds[2].memarea = aCPU->mSFR;
    eds[2].memviewoffset = 128;
    eds[3].lines = LINES / 2;
    eds[3].box = subwin(stdscr, eds[3].lines, 40, 0, 40);
    wborder(eds[3].box, (acs_map[(unsigned char)(('x'))]), (acs_map[(unsigned char)(('x'))]), (acs_map[(unsigned char)(('q'))]), (acs_map[(unsigned char)(('q'))]), 0, 0, 0, 0);
    (wmove((eds[3].box),(0),(2)) == (-1) ? (-1) : waddnstr((eds[3].box),("External"),-1));
    eds[3].view = subwin(eds[3].box, eds[3].lines - 2, 38, 1, 41);
    eds[3].maxmem = aCPU->mExtDataMaxIdx+1;
    eds[3].memarea = aCPU->mExtData;
    eds[3].memviewoffset = 0;
    eds[4].lines = LINES / 2;
    eds[4].box = subwin(stdscr, eds[3].lines, 40, eds[3].lines, 40);
    wborder(eds[4].box, (acs_map[(unsigned char)(('x'))]), (acs_map[(unsigned char)(('x'))]), (acs_map[(unsigned char)(('q'))]), (acs_map[(unsigned char)(('q'))]), 0, 0, 0, 0);
    (wmove((eds[4].box),(0),(2)) == (-1) ? (-1) : waddnstr((eds[4].box),("ROM"),-1));
    eds[4].view = subwin(eds[4].box, eds[4].lines - 2, 38, eds[3].lines + 1, 41);
    eds[4].maxmem = aCPU->mCodeMemMaxIdx+1;
    eds[4].memarea = aCPU->mCodeMem;
    eds[4].memviewoffset = 0;
    eds[0].cursorpos = 0;
    eds[1].cursorpos = 0;
    eds[2].cursorpos = 0;
    eds[3].cursorpos = 0;
    eds[4].cursorpos = 0;
    eds[0].memoffset = 0;
    eds[1].memoffset = 0;
    eds[2].memoffset = 0;
    eds[3].memoffset = 0;
    eds[4].memoffset = 0;
    wrefresh(stdscr);
}


decompiled_code:
__int64 __fastcall memeditor_editor_keys(__int64 a1, int a2)
{
__int64 v2; // rcx
__int64 result; // rax
WINDOW **v4; // rsi
int v5; // edx
bool v6; // sf
WINDOW **v7; // rsi
int v8; // er8
int v9; // edx
int v10; // edi
char v11; // al
char *v12; // rdx
char v13; // si

v2 = focus;
if ( a2 <= 102 )
{
if ( a2 > 8 )
{
switch ( a2 )
{
case 9:
goto LABEL_8;
case 48:
case 49:
case 50:
case 51:
case 52:
case 53:
case 54:
case 55:
case 56:
case 57:
v11 = a2 - 48;
break;
case 65:
case 97:
v11 = 10;
break;
case 66:
case 98:
v11 = 11;
break;
case 67:
case 99:
v11 = 12;
break;
case 68:
case 100:
v11 = 13;
break;
case 69:
case 101:
v11 = 14;
break;
case 70:
case 102:
v11 = 15;
break;
default:
goto LABEL_13;
}
v12 = (char *)(qword_B90[6 * focus] + HIDWORD(qword_B9C[6 * focus]) + SLODWORD(qword_B9C[6 * focus]) / 2);
v13 = *v12;
if ( (qword_B9C[6 * focus] & 1) != 0 )
*v12 = v11 | v13 & 0xF0;
else
*v12 = (16 * v11) | v13 & 0xF;
goto LABEL_37;
}
LABEL_13:
result = LODWORD(qword_B9C[6 * v2]);
goto LABEL_14;
}
if ( a2 == 261 )
{
LABEL_37:
result = (unsigned int)(LODWORD(qword_B9C[6 * v2]) + 1);
LODWORD(qword_B9C[6 * v2]) = result;
goto LABEL_14;
}
if ( a2 <= 261 )
{
if ( a2 != 259 )
{
if ( a2 == 260 )
{
result = (unsigned int)(LODWORD(qword_B9C[6 * focus]) - 1);
LODWORD(qword_B9C[6 * focus]) = result;
goto LABEL_14;
}
if ( a2 == 258 )
{
result = (unsigned int)(LODWORD(qword_B9C[6 * focus]) + 16);
LODWORD(qword_B9C[6 * focus]) = result;
goto LABEL_14;
}
goto LABEL_13;
}
result = (unsigned int)(LODWORD(qword_B9C[6 * focus]) - 16);
LODWORD(qword_B9C[6 * focus]) = result;
}
else
{
switch ( a2 )
{
case 339:
result = (unsigned int)(LODWORD(qword_B9C[6 * focus]) - 16 * (*(&dword_B98 + 12 * focus) - 2));
LODWORD(qword_B9C[6 * focus]) = result;
break;
case 367:
LABEL_8:
LODWORD(v2) = focus + 1;
focus = v2;
if ( (_DWORD)v2 == 1 )
{
if ( !*(_QWORD *)(a1 + 160) )
{
focus = 2;
LODWORD(v2) = 2;
}
goto LABEL_12;
}
if ( (_DWORD)v2 == 3 )
{
if ( !*(_QWORD *)(a1 + 16) )
{
focus = 4;
LODWORD(v2) = 4;
}
goto LABEL_12;
}
if ( (_DWORD)v2 != 5 )
{
LABEL_12:
v2 = (int)v2;
goto LABEL_13;
}
focus = 0;
result = LODWORD(qword_B9C[0]);
v2 = 0LL;
break;
case 338:
result = (unsigned int)(LODWORD(qword_B9C[6 * focus]) + 16 * (*(&dword_B98 + 12 * focus) - 2));
LODWORD(qword_B9C[6 * focus]) = result;
break;
default:
goto LABEL_13;
}
}
LABEL_14:
if ( (int)result < 0 )
{
v4 = &eds + 6 * v2;
v5 = *((_DWORD *)v4 + 8);
while ( 1 )
{
v5 -= 8;
if ( v5 < 0 )
break;
v6 = (int)result + 16 < 0;
LODWORD(result) = result + 16;
*((_DWORD *)v4 + 7) = result;
if ( !v6 )
goto LABEL_18;
}
v5 = 0;
LODWORD(qword_B9C[6 * v2]) = 0;
LABEL_18:
HIDWORD(qword_B9C[6 * v2]) = v5;
result = *((unsigned int *)&eds + 12 * v2 + 7);
}
v7 = &eds + 6 * v2;
v8 = 16 * (*((_DWORD *)v7 + 6) - 2);
if ( v8 <= (int)result )
{
v9 = *((_DWORD *)v7 + 8);
v10 = *((_DWORD *)v7 + 9) - 8 * (*((_DWORD *)v7 + 6) - 2);
do
{
while ( 1 )
{
v9 += 8;
if ( v9 <= v10 )
break;
LODWORD(result) = v8 - 1;
v9 = v10;
*((_DWORD *)v7 + 7) = v8 - 1;
if ( !__OFSUB__(v8, (_DWORD)result) && v8 != (_DWORD)result )
goto LABEL_24;
}
LODWORD(result) = result - 16;
*((_DWORD *)v7 + 7) = result;
}
while ( v8 <= (int)result );
LABEL_24:
result = 48 * v2;
HIDWORD(qword_B9C[6 * v2]) = v9;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, int a2)
{
__int64 v2; // rcx
__int64 result; // rax
WINDOW **v4; // rsi
int v5; // edx
bool v6; // sf
WINDOW **v7; // rsi
int v8; // er8
int v9; // edx
int v10; // edi
char v11; // al
char *v12; // rdx
char v13; // si

v2 = focus;
if ( a2 <= 102 )
{
if ( a2 > 8 )
{
switch ( a2 )
{
case 9:
goto LABEL_8;
case 48:
case 49:
case 50:
case 51:
case 52:
case 53:
case 54:
case 55:
case 56:
case 57:
v11 = a2 - 48;
break;
case 65:
case 97:
v11 = 10;
break;
case 66:
case 98:
v11 = 11;
break;
case 67:
case 99:
v11 = 12;
break;
case 68:
case 100:
v11 = 13;
break;
case 69:
case 101:
v11 = 14;
break;
case 70:
case 102:
v11 = 15;
break;
default:
goto LABEL_13;
}
v12 = (char *)(qword_B90[6 * focus] + HIDWORD(qword_B9C[6 * focus]) + SLODWORD(qword_B9C[6 * focus]) / 2);
v13 = *v12;
if ( (qword_B9C[6 * focus] & 1) != 0 )
*v12 = v11 | v13 & 0xF0;
else
*v12 = (16 * v11) | v13 & 0xF;
goto LABEL_37;
}
LABEL_13:
result = LODWORD(qword_B9C[6 * v2]);
goto LABEL_14;
}
if ( a2 == 261 )
{
LABEL_37:
result = (unsigned int)(LODWORD(qword_B9C[6 * v2]) + 1);
LODWORD(qword_B9C[6 * v2]) = result;
goto LABEL_14;
}
if ( a2 <= 261 )
{
if ( a2 != 259 )
{
if ( a2 == 260 )
{
result = (unsigned int)(LODWORD(qword_B9C[6 * focus]) - 1);
LODWORD(qword_B9C[6 * focus]) = result;
goto LABEL_14;
}
if ( a2 == 258 )
{
result = (unsigned int)(LODWORD(qword_B9C[6 * focus]) + 16);
LODWORD(qword_B9C[6 * focus]) = result;
goto LABEL_14;
}
goto LABEL_13;
}
result = (unsigned int)(LODWORD(qword_B9C[6 * focus]) - 16);
LODWORD(qword_B9C[6 * focus]) = result;
}
else
{
switch ( a2 )
{
case 339:
result = (unsigned int)(LODWORD(qword_B9C[6 * focus]) - 16 * (*(&dword_B98 + 12 * focus) - 2));
LODWORD(qword_B9C[6 * focus]) = result;
break;
case 367:
LABEL_8:
LODWORD(v2) = focus + 1;
focus = v2;
if ( (_DWORD)v2 == 1 )
{
if ( !*(_QWORD *)(a1 + 160) )
{
focus = 2;
LODWORD(v2) = 2;
}
goto LABEL_12;
}
if ( (_DWORD)v2 == 3 )
{
if ( !*(_QWORD *)(a1 + 16) )
{
focus = 4;
LODWORD(v2) = 4;
}
goto LABEL_12;
}
if ( (_DWORD)v2 != 5 )
{
LABEL_12:
v2 = (int)v2;
goto LABEL_13;
}
focus = 0;
result = LODWORD(qword_B9C[0]);
v2 = 0LL;
break;
case 338:
result = (unsigned int)(LODWORD(qword_B9C[6 * focus]) + 16 * (*(&dword_B98 + 12 * focus) - 2));
LODWORD(qword_B9C[6 * focus]) = result;
break;
default:
goto LABEL_13;
}
}
LABEL_14:
if ( (int)result < 0 )
{
v4 = &eds + 6 * v2;
v5 = *((_DWORD *)v4 + 8);
while ( 1 )
{
v5 -= 8;
if ( v5 < 0 )
break;
v6 = (int)result + 16 < 0;
LODWORD(result) = result + 16;
*((_DWORD *)v4 + 7) = result;
if ( !v6 )
goto LABEL_18;
}
v5 = 0;
LODWORD(qword_B9C[6 * v2]) = 0;
LABEL_18:
HIDWORD(qword_B9C[6 * v2]) = v5;
result = *((unsigned int *)&eds + 12 * v2 + 7);
}
v7 = &eds + 6 * v2;
v8 = 16 * (*((_DWORD *)v7 + 6) - 2);
if ( v8 <= (int)result )
{
v9 = *((_DWORD *)v7 + 8);
v10 = *((_DWORD *)v7 + 9) - 8 * (*((_DWORD *)v7 + 6) - 2);
do
{
while ( 1 )
{
v9 += 8;
if ( v9 <= v10 )
break;
LODWORD(result) = v8 - 1;
v9 = v10;
*((_DWORD *)v7 + 7) = v8 - 1;
if ( !__OFSUB__(v8, (_DWORD)result) && v8 != (_DWORD)result )
goto LABEL_24;
}
LODWORD(result) = result - 16;
*((_DWORD *)v7 + 7) = result;
}
while ( v8 <= (int)result );
LABEL_24:
result = 48 * v2;
HIDWORD(qword_B9C[6 * v2]) = v9;
}
return result;
}

original_code:
void memeditor_editor_keys(struct em8051 *aCPU, int ch)
{
    int insert_value = -1;
    switch(ch)
    {
    case 0557:
    case '\t':
        focus++;
        if (focus == 1 && aCPU->mUpperData == ((void *)0))
            focus++;
        if (focus == 3 && aCPU->mExtData == ((void *)0))
            focus++;
        if (focus == 5)
            focus = 0;
        break;
    case 0522:
        eds[focus].cursorpos += (eds[focus].lines - 2) * 16;
        break;
    case 0523:
        eds[focus].cursorpos -= (eds[focus].lines - 2) * 16;
        break;
    case 0405:
        eds[focus].cursorpos++;
        break;
    case 0404:
        eds[focus].cursorpos--;
        break;
    case 0403:
        eds[focus].cursorpos-=16;
        break;
    case 0402:
        eds[focus].cursorpos+=16;
        break;
    case '0':
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':
        insert_value = ch - '0';
        break;
    case 'a':
    case 'A':
        insert_value = 0xa;
        break;
    case 'b':
    case 'B':
        insert_value = 0xb;
        break;
    case 'c':
    case 'C':
        insert_value = 0xc;
        break;
    case 'd':
    case 'D':
        insert_value = 0xd;
        break;
    case 'e':
    case 'E':
        insert_value = 0xe;
        break;
    case 'f':
    case 'F':
        insert_value = 0xf;
        break;
    }
    if (insert_value != -1)
    {
        if (eds[focus].cursorpos & 1)
            eds[focus].memarea[eds[focus].memoffset + (eds[focus].cursorpos / 2)] = (eds[focus].memarea[eds[focus].memoffset + (eds[focus].cursorpos / 2)] & 0xf0) | insert_value;
        else
            eds[focus].memarea[eds[focus].memoffset + (eds[focus].cursorpos / 2)] = (eds[focus].memarea[eds[focus].memoffset + (eds[focus].cursorpos / 2)] & 0x0f) | (insert_value << 4);
        eds[focus].cursorpos++;
    }
    while (eds[focus].cursorpos < 0)
    {
        eds[focus].memoffset -= 8;
        if (eds[focus].memoffset < 0)
        {
            eds[focus].memoffset = 0;
            eds[focus].cursorpos = 0;
        }
        else
        {
            eds[focus].cursorpos += 16;
        }
    }
    while (eds[focus].cursorpos > (eds[focus].lines - 2) * 16 - 1)
    {
        eds[focus].memoffset += 8;
        if (eds[focus].memoffset > (eds[focus].maxmem - (eds[focus].lines - 2)*8))
        {
            eds[focus].memoffset = (eds[focus].maxmem - (eds[focus].lines - 2)*8);
            eds[focus].cursorpos = (eds[focus].lines - 2) * 16 - 1;
        }
        else
        {
            eds[focus].cursorpos -= 16;
        }
    }
}

variable_types: ch int


original_code:
void memeditor_update(struct em8051 *aCPU)
{
    int i, j, bytevalue;
    for (i = 0; i < 5; i++)
    {
        werase(eds[i].view);
        if (eds[i].memarea)
        {
            for (j = 0; j < eds[i].lines - 2; j++)
            {
                wprintw(eds[i].view,"%04X %02X %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c%c\n",
                    j*8+eds[i].memoffset+eds[i].memviewoffset,
                    eds[i].memarea[j*8+0+eds[i].memoffset],
                    eds[i].memarea[j*8+1+eds[i].memoffset],
                    eds[i].memarea[j*8+2+eds[i].memoffset],
                    eds[i].memarea[j*8+3+eds[i].memoffset],
                    eds[i].memarea[j*8+4+eds[i].memoffset],
                    eds[i].memarea[j*8+5+eds[i].memoffset],
                    eds[i].memarea[j*8+6+eds[i].memoffset],
                    eds[i].memarea[j*8+7+eds[i].memoffset],
                    (((eds[i].memarea[j*8+0+eds[i].memoffset]) > 31)?(((eds[i].memarea[j*8+0+eds[i].memoffset]) < 127)?(eds[i].memarea[j*8+0+eds[i].memoffset]):'.'):'.'),
                    (((eds[i].memarea[j*8+1+eds[i].memoffset]) > 31)?(((eds[i].memarea[j*8+1+eds[i].memoffset]) < 127)?(eds[i].memarea[j*8+1+eds[i].memoffset]):'.'):'.'),
                    (((eds[i].memarea[j*8+2+eds[i].memoffset]) > 31)?(((eds[i].memarea[j*8+2+eds[i].memoffset]) < 127)?(eds[i].memarea[j*8+2+eds[i].memoffset]):'.'):'.'),
                    (((eds[i].memarea[j*8+3+eds[i].memoffset]) > 31)?(((eds[i].memarea[j*8+3+eds[i].memoffset]) < 127)?(eds[i].memarea[j*8+3+eds[i].memoffset]):'.'):'.'),
                    (((eds[i].memarea[j*8+4+eds[i].memoffset]) > 31)?(((eds[i].memarea[j*8+4+eds[i].memoffset]) < 127)?(eds[i].memarea[j*8+4+eds[i].memoffset]):'.'):'.'),
                    (((eds[i].memarea[j*8+5+eds[i].memoffset]) > 31)?(((eds[i].memarea[j*8+5+eds[i].memoffset]) < 127)?(eds[i].memarea[j*8+5+eds[i].memoffset]):'.'):'.'),
                    (((eds[i].memarea[j*8+6+eds[i].memoffset]) > 31)?(((eds[i].memarea[j*8+6+eds[i].memoffset]) < 127)?(eds[i].memarea[j*8+6+eds[i].memoffset]):'.'):'.'),
                    (((eds[i].memarea[j*8+7+eds[i].memoffset]) > 31)?(((eds[i].memarea[j*8+7+eds[i].memoffset]) < 127)?(eds[i].memarea[j*8+7+eds[i].memoffset]):'.'):'.'));
            }
        }
    }
    bytevalue = eds[focus].memarea[eds[focus].cursorpos / 2 + eds[focus].memoffset];
    wattr_on(eds[focus].view, (attr_t)(((chtype)((1U)) << ((10) + 8))), ((void *)0));
    wmove(eds[focus].view, eds[focus].cursorpos / 16, 5 + ((eds[focus].cursorpos % 16) / 2) * 3 + (eds[focus].cursorpos & 1));
    wprintw(eds[focus].view,"%X", (bytevalue >> (4 * (!(eds[focus].cursorpos & 1)))) & 0xf);
    wattr_off(eds[focus].view, (attr_t)(((chtype)((1U)) << ((10) + 8))), ((void *)0));
    for (i = 0; i < 5; i++)
        wrefresh(eds[i].view);
    wrefresh(stdscr);
}

canonical_original_code:
void memeditor_update(struct em8051 *aCPU)
{
    int i; int j; int bytevalue;
    for (i = 0; i < 5; i++)
    {
        werase(eds[i].view);
        if (eds[i].memarea)
        {
            for (j = 0; j < eds[i].lines - 2; j++)
            {
                wprintw(eds[i].view,"%04X %02X %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c%c\n",
                    j*8+eds[i].memoffset+eds[i].memviewoffset,
                    eds[i].memarea[j*8+0+eds[i].memoffset],
                    eds[i].memarea[j*8+1+eds[i].memoffset],
                    eds[i].memarea[j*8+2+eds[i].memoffset],
                    eds[i].memarea[j*8+3+eds[i].memoffset],
                    eds[i].memarea[j*8+4+eds[i].memoffset],
                    eds[i].memarea[j*8+5+eds[i].memoffset],
                    eds[i].memarea[j*8+6+eds[i].memoffset],
                    eds[i].memarea[j*8+7+eds[i].memoffset],
                    (((eds[i].memarea[j*8+0+eds[i].memoffset]) > 31)?(((eds[i].memarea[j*8+0+eds[i].memoffset]) < 127)?(eds[i].memarea[j*8+0+eds[i].memoffset]):'.'):'.'),
                    (((eds[i].memarea[j*8+1+eds[i].memoffset]) > 31)?(((eds[i].memarea[j*8+1+eds[i].memoffset]) < 127)?(eds[i].memarea[j*8+1+eds[i].memoffset]):'.'):'.'),
                    (((eds[i].memarea[j*8+2+eds[i].memoffset]) > 31)?(((eds[i].memarea[j*8+2+eds[i].memoffset]) < 127)?(eds[i].memarea[j*8+2+eds[i].memoffset]):'.'):'.'),
                    (((eds[i].memarea[j*8+3+eds[i].memoffset]) > 31)?(((eds[i].memarea[j*8+3+eds[i].memoffset]) < 127)?(eds[i].memarea[j*8+3+eds[i].memoffset]):'.'):'.'),
                    (((eds[i].memarea[j*8+4+eds[i].memoffset]) > 31)?(((eds[i].memarea[j*8+4+eds[i].memoffset]) < 127)?(eds[i].memarea[j*8+4+eds[i].memoffset]):'.'):'.'),
                    (((eds[i].memarea[j*8+5+eds[i].memoffset]) > 31)?(((eds[i].memarea[j*8+5+eds[i].memoffset]) < 127)?(eds[i].memarea[j*8+5+eds[i].memoffset]):'.'):'.'),
                    (((eds[i].memarea[j*8+6+eds[i].memoffset]) > 31)?(((eds[i].memarea[j*8+6+eds[i].memoffset]) < 127)?(eds[i].memarea[j*8+6+eds[i].memoffset]):'.'):'.'),
                    (((eds[i].memarea[j*8+7+eds[i].memoffset]) > 31)?(((eds[i].memarea[j*8+7+eds[i].memoffset]) < 127)?(eds[i].memarea[j*8+7+eds[i].memoffset]):'.'):'.'));
            }
        }
    }
    bytevalue = eds[focus].memarea[eds[focus].cursorpos / 2 + eds[focus].memoffset];
    wattr_on(eds[focus].view, (attr_t)(((chtype)((1U)) << ((10) + 8))), ((void *)0));
    wmove(eds[focus].view, eds[focus].cursorpos / 16, 5 + ((eds[focus].cursorpos % 16) / 2) * 3 + (eds[focus].cursorpos & 1));
    wprintw(eds[focus].view,"%X", (bytevalue >> (4 * (!(eds[focus].cursorpos & 1)))) & 0xf);
    wattr_off(eds[focus].view, (attr_t)(((chtype)((1U)) << ((10) + 8))), ((void *)0));
    for (i = 0; i < 5; i++)
        wrefresh(eds[i].view);
    wrefresh(stdscr);
}

variable_types: j int

variable_types: bytevalue int


main

canonical_name: main

decompiled_code:
// attributes: thunk
int __cdecl main(int argc, const char **argv, const char **envp)
{
return runTests(argc, argv, envp);
}

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

original_code:
int main (int argc, char* argv[])
{
    (void)argc;
    (void)argv;
    return runTests();
}

canonical_original_code:
int main(int argc, char *argv[])
{
    (void)argc;
    (void)argv;
    return runTests();
}

variable_types: argc int

variable_types:
argv
char *[]

return_type: int


repo: jason-foo/stupid

decompiled_code:
void __fastcall __noreturn dg_echo(int fd, struct sockaddr *addr, socklen_t a3)
{
int v4; // eax
socklen_t addr_len; // [rsp+Ch] [rbp-101Ch] BYREF
char buf[4120]; // [rsp+10h] [rbp-1018h] BYREF

while ( 1 )
{
addr_len = a3;
v4 = recvfrom(fd, buf, 0x1000uLL, 0, addr, &addr_len);
sendto(fd, buf, v4, 0, addr, addr_len);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func9(int fd, struct sockaddr *addr, socklen_t a3)
{
int v4; // eax
socklen_t addr_len; // [rsp+Ch] [rbp-101Ch] BYREF
char buf[4120]; // [rsp+10h] [rbp-1018h] BYREF

while ( 1 )
{
addr_len = a3;
v4 = recvfrom(fd, buf, 0x1000uLL, 0, addr, &addr_len);
sendto(fd, buf, v4, 0, addr, addr_len);
}
}

original_code:
void dg_echo(int sockfd, struct sockaddr *pcliaddr, socklen_t clilen)
{
 int n;
 socklen_t len;
 char mesg[4096];
 for (;;)
 {
  len = clilen;
  n = recvfrom(sockfd, mesg, 4096, 0, pcliaddr, &len);
  sendto(sockfd, mesg, n, 0, pcliaddr, len);
 }
}

canonical_original_code:
void dg_echo(int sockfd, struct sockaddr *pcliaddr, unsigned int clilen)
{
 int n;
 unsigned int len;
 char mesg[4096];
 for (;;)
 {
  len = clilen;
  n = recvfrom(sockfd, mesg, 4096, 0, pcliaddr, &len);
  sendto(sockfd, mesg, n, 0, pcliaddr, len);
 }
}

variable_types: sockfd int

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

variable_types:
clilen
unsigned int

variable_types: n int

variable_types:
len
unsigned int

variable_types:
mesg
char [4096]


decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v4; // ebp
int v5; // edi
socklen_t v6; // edx
struct sockaddr v7; // [rsp+0h] [rbp-28h] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-18h] BYREF

v3 = socket(2, 2, 0);
if ( v3 >= 0 )
{
v4 = v3;
v7.sa_family = 2;
*(_QWORD *)&v7.sa_data[6] = 0LL;
v5 = v3;
*(_QWORD *)&v7.sa_data[2] = (unsigned int)_bswap_32_constprop_0((unsigned int)v3, &v7, 16LL);
*(_WORD *)v7.sa_data = _bswap_16_constprop_0();
if ( bind(v5, &v7, v6) >= 0 )
dg_echo(v4, &addr);
perror("bind");
exit(1);
}
perror("sockfd");
exit(1);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v4; // ebp
int v5; // edi
socklen_t v6; // edx
struct sockaddr v7; // [rsp+0h] [rbp-28h] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-18h] BYREF

v3 = socket(2, 2, 0);
if ( v3 >= 0 )
{
v4 = v3;
v7.sa_family = 2;
*(_QWORD *)&v7.sa_data[6] = 0LL;
v5 = v3;
*(_QWORD *)&v7.sa_data[2] = (unsigned int)func7((unsigned int)v3, &v7, 16LL);
*(_WORD *)v7.sa_data = func8();
if ( bind(v5, &v7, v6) >= 0 )
func9(v4, &addr);
perror("bind");
exit(1);
}
perror("sockfd");
exit(1);
}

original_code:
int main()
{
 int sockfd;
 struct sockaddr_in servaddr, cliaddr;
 if ( (sockfd = socket(2, SOCK_DGRAM, 0)) < 0)
 {
  perror("sockfd");
  exit(1);
 }
 bzero(&servaddr, sizeof(servaddr));
 servaddr.sin_family = 2;
 servaddr.sin_addr.s_addr = htonl(((in_addr_t) 0x00000000));
 servaddr.sin_port = htons(7);
 if ( bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
 {
  perror("bind");
  exit(1);
 }
 dg_echo(sockfd, (struct sockaddr *)&cliaddr, sizeof(cliaddr));
 return 0;
}

canonical_original_code:
int main()
{
 int sockfd;
 struct sockaddr_in servaddr; struct sockaddr_in cliaddr;
 if ( (sockfd = socket(2, SOCK_DGRAM, 0)) < 0)
 {
  perror("sockfd");
  exit(1);
 }
 bzero(&servaddr, sizeof(servaddr));
 servaddr.sin_family = 2;
 servaddr.sin_addr.s_addr = htonl(((unsigned int) 0x00000000));
 servaddr.sin_port = htons(7);
 if ( bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
 {
  perror("bind");
  exit(1);
 }
 dg_echo(sockfd, (struct sockaddr *)&cliaddr, sizeof(cliaddr));
 return 0;
}

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

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

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


decompiled_code:
__int64 sfd_alloc()
{
unsigned int v0; // er12
int *v1; // rax

v0 = 0;
pthread_spin_lock(&sfd_lock);
v1 = sfd_table;
do
{
if ( !*v1 )
{
sfd_table[3 * v0] = 1;
pthread_spin_unlock(&sfd_lock);
return v0;
}
++v0;
v1 += 3;
}
while ( v0 != 256 );
pthread_spin_unlock(&sfd_lock);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func0()
{
unsigned int v0; // er12
int *v1; // rax

v0 = 0;
pthread_spin_lock(&sfd_lock);
v1 = sfd_table;
do
{
if ( !*v1 )
{
sfd_table[3 * v0] = 1;
pthread_spin_unlock(&sfd_lock);
return v0;
}
++v0;
v1 += 3;
}
while ( v0 != 256 );
pthread_spin_unlock(&sfd_lock);
return 0xFFFFFFFFLL;
}

original_code:
static int sfd_alloc()
{
 int i;
 pthread_spin_lock(&sfd_lock);
 for (i = 0; i < 256; i++)
  if (!sfd_table[i].inuse)
   break;
 if (i != 256)
  sfd_table[i].inuse = 1;
 pthread_spin_unlock(&sfd_lock);
 return (i == 256) ? -1 : i;
}


decompiled_code:
void socket_init()
{
int v0; // eax

if ( !init_done )
{
lock_fd = open("/run/stupid.pid", 66, 432LL);
if ( lock_fd < 0 )
_error_sys("error opening lock_file");
if ( !(unsigned int)lock_test((unsigned int)lock_fd, 1LL, 1024LL, 0LL, 1LL) )
_error_sys("stupid server not started");
pthread_spin_init(&sfd_lock, 1);
pid = getpid();
server_fd = socket(1, 2, 0);
if ( server_fd < 0 )
_error_sys("UNIX domain socket: server\n");
client_fd = socket(1, 2, 0);
if ( client_fd < 0 )
_error_sys("UNIX domain socket: client\n");
server.sa_family = 1;
strncpy((char *)(unsigned int)((_DWORD)&server + 2), stupid_server_path, 0x6CuLL);
memset(&client, 0, 0x68uLL);
client.sa_family = 1;
*((_DWORD *)&client + 26) = 0;
*((_WORD *)&client + 54) = 0;
sprintf(stupid_client_path, "/tmp/stupid.%d", (unsigned int)pid);
strncpy((char *)(unsigned int)((_DWORD)&client + 2), stupid_client_path, 0x6CuLL);
v0 = strlen((const char *)(unsigned int)((_DWORD)&client + 2));
if ( bind(client_fd, &client, v0 + 2) < 0 )
_error_sys("bind to client");
init_done = 1;
}
}

canonical_decompiled_code:
void func2()
{
int v0; // eax

if ( !init_done )
{
lock_fd = open("/run/stupid.pid", 66, 432LL);
if ( lock_fd < 0 )
func1("error opening lock_file");
if ( !(unsigned int)lock_test((unsigned int)lock_fd, 1LL, 1024LL, 0LL, 1LL) )
func1("stupid server not started");
pthread_spin_init(&sfd_lock, 1);
pid = getpid();
server_fd = socket(1, 2, 0);
if ( server_fd < 0 )
func1("UNIX domain socket: server\n");
client_fd = socket(1, 2, 0);
if ( client_fd < 0 )
func1("UNIX domain socket: client\n");
server.sa_family = 1;
strncpy((char *)(unsigned int)((_DWORD)&server + 2), stupid_server_path, 0x6CuLL);
memset(&client, 0, 0x68uLL);
client.sa_family = 1;
*((_DWORD *)&client + 26) = 0;
*((_WORD *)&client + 54) = 0;
sprintf(stupid_client_path, "/tmp/stupid.%d", (unsigned int)pid);
strncpy((char *)(unsigned int)((_DWORD)&client + 2), stupid_client_path, 0x6CuLL);
v0 = strlen((const char *)(unsigned int)((_DWORD)&client + 2));
if ( bind(client_fd, &client, v0 + 2) < 0 )
func1("bind to client");
init_done = 1;
}
}

original_code:
void socket_init()
{
 int addr_len;
 char *pidfile = "/run/stupid.pid";
 if (init_done)
  return;
 if ( (lock_fd = open(pidfile, 02 | 0100, (0400 | 0200 | (0400 >> 3) | (0200 >> 3)))) < 0)
  __error_sys("error opening lock_file");
 if (!(!(lock_test(((lock_fd)), 1, (1024), (0), (1)) == 0)))
  __error_sys("stupid server not started");
 pthread_spin_init(&sfd_lock, PTHREAD_PROCESS_SHARED);
 pid = getpid();
 if ( (server_fd = socket(1, SOCK_DGRAM, 0)) < 0)
  __error_sys("UNIX domain socket: server\n");
 if ( (client_fd = socket(1, SOCK_DGRAM, 0)) < 0)
  __error_sys("UNIX domain socket: client\n");
 bzero(&server, sizeof(server));
 server.sun_family = 1;
 strncpy(server.sun_path, stupid_server_path, sizeof(server.sun_path));
 addr_len = ((size_t)(&((struct sockaddr_un *)0)->sun_path)) +
  strlen(server.sun_path);
 bzero(&client, sizeof(client));
 client.sun_family = 1;
 sprintf(stupid_client_path, "/tmp/stupid.%d", pid);
 strncpy(client.sun_path, stupid_client_path, sizeof(client.sun_path));
 addr_len = ((size_t)(&((struct sockaddr_un *)0)->sun_path)) +
  strlen(client.sun_path);
 if (bind(client_fd, (struct sockaddr *) &client, addr_len) < 0)
  __error_sys("bind to client");
 init_done = 1;
}

canonical_original_code:
void socket_init()
{
 int addr_len;
 char *pidfile = "/run/stupid.pid";
 if (init_done)
  return;
 if ( (lock_fd = open(pidfile, 02 | 0100, (0400 | 0200 | (0400 >> 3) | (0200 >> 3)))) < 0)
  __error_sys("error opening lock_file");
 if (!(!(lock_test(((lock_fd)), 1, (1024), (0), (1)) == 0)))
  __error_sys("stupid server not started");
 pthread_spin_init(&sfd_lock, PTHREAD_PROCESS_SHARED);
 pid = getpid();
 if ( (server_fd = socket(1, SOCK_DGRAM, 0)) < 0)
  __error_sys("UNIX domain socket: server\n");
 if ( (client_fd = socket(1, SOCK_DGRAM, 0)) < 0)
  __error_sys("UNIX domain socket: client\n");
 bzero(&server, sizeof(server));
 server.sun_family = 1;
 strncpy(server.sun_path, stupid_server_path, sizeof(server.sun_path));
 addr_len = ((unsigned long)(&((struct sockaddr_un *)0)->sun_path)) +
  strlen(server.sun_path);
 bzero(&client, sizeof(client));
 client.sun_family = 1;
 sprintf(stupid_client_path, "/tmp/stupid.%d", pid);
 strncpy(client.sun_path, stupid_client_path, sizeof(client.sun_path));
 addr_len = ((unsigned long)(&((struct sockaddr_un *)0)->sun_path)) +
  strlen(client.sun_path);
 if (bind(client_fd, (struct sockaddr *) &client, addr_len) < 0)
  __error_sys("bind to client");
 init_done = 1;
}

variable_types:
pidfile
char *

user_defined_types: struct sockaddr_un { unsigned short sun_family; char sun_path[108]; }


decompiled_code:
int __fastcall sfd_release(int a1)
{
pthread_spin_lock(&sfd_lock);
sfd_table[3 * a1] = 0;
return pthread_spin_unlock(&sfd_lock);
}

canonical_decompiled_code:
int __fastcall func3(int a1)
{
pthread_spin_lock(&sfd_lock);
sfd_table[3 * a1] = 0;
return pthread_spin_unlock(&sfd_lock);
}

original_code:
void sfd_release(int index)
{
 pthread_spin_lock(&sfd_lock);
 sfd_table[index].inuse = 0;
 pthread_spin_unlock(&sfd_lock);
}

variable_types: index int


decompiled_code:
ssize_t __fastcall read_from_server(void *buf, int a2)
{
return recv(client_fd, buf, a2, 0);
}

canonical_decompiled_code:
ssize_t __fastcall func4(void *buf, int a2)
{
return recv(client_fd, buf, a2, 0);
}

original_code:
int read_from_server(char *buf, int len)
{
 int tot = 0;
 tot = recv(client_fd, buf, len, 0);
 return tot;
}

variable_types:
buf
char *

variable_types: len int

variable_types: tot int


decompiled_code:
ssize_t __fastcall write_to_server(void *buf, int a2)
{
ssize_t result; // rax

result = sendto(server_fd, buf, a2, 0, &server, 0x6Eu);
if ( (int)result < 0 )
_error_sys("sendto server error\n");
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func5(void *buf, int a2)
{
ssize_t result; // rax

result = sendto(server_fd, buf, a2, 0, &server, 0x6Eu);
if ( (int)result < 0 )
func1("sendto server error\n");
return result;
}

original_code:
int write_to_server(char *buf, int len)
{
 int tot;
 tot = sendto(server_fd, buf, len, 0, (struct sockaddr *) &server,
       sizeof(server));
 if (tot < 0)
  __error_sys("sendto server error\n");
 return tot;
}


decompiled_code:
__int64 __fastcall stupid_socket(int a1, int a2, int a3)
{
unsigned int v4; // er12
int v6; // ebx
char v7[4]; // [rsp+0h] [rbp-688h] BYREF
int v8; // [rsp+4h] [rbp-684h]
int v9; // [rsp+8h] [rbp-680h]
int v10; // [rsp+Ch] [rbp-67Ch]
int v11; // [rsp+10h] [rbp-678h]
char buf[4]; // [rsp+20h] [rbp-668h] BYREF
unsigned int v13; // [rsp+24h] [rbp-664h]

socket_init();
v4 = sfd_alloc();
if ( v4 != -1 )
{
v7[0] = 1;
v9 = a1;
v8 = pid;
v10 = a2;
v11 = a3;
if ( (unsigned int)write_to_server(v7, 20) != 20 )
_error_sys("stupid_socket: write to server fifo error");
read_from_server(buf, 1600);
if ( buf[0] == 2 )
{
v6 = v13;
printf("got sock id: %d\n", v13);
lock_reg((unsigned int)lock_fd, 6LL, 1LL, v6, 0LL, 1LL);
sfd_table[3 * v4 + 2] = v6;
}
else
{
printf("unknown type id %d in response\n", (unsigned __int8)buf[0]);
}
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func6(int a1, int a2, int a3)
{
unsigned int v4; // er12
int v6; // ebx
char v7[4]; // [rsp+0h] [rbp-688h] BYREF
int v8; // [rsp+4h] [rbp-684h]
int v9; // [rsp+8h] [rbp-680h]
int v10; // [rsp+Ch] [rbp-67Ch]
int v11; // [rsp+10h] [rbp-678h]
char buf[4]; // [rsp+20h] [rbp-668h] BYREF
unsigned int v13; // [rsp+24h] [rbp-664h]

func2();
v4 = func0();
if ( v4 != -1 )
{
v7[0] = 1;
v9 = a1;
v8 = pid;
v10 = a2;
v11 = a3;
if ( (unsigned int)func5(v7, 20) != 20 )
func1("stupid_socket: write to server fifo error");
func4(buf, 1600);
if ( buf[0] == 2 )
{
v6 = v13;
printf("got sock id: %d\n", v13);
lock_reg((unsigned int)lock_fd, 6LL, 1LL, v6, 0LL, 1LL);
sfd_table[3 * v4 + 2] = v6;
}
else
{
printf("unknown type id %d in response\n", (unsigned __int8)buf[0]);
}
}
return v4;
}

original_code:
int stupid_socket(int domain, int type, int protocol)
{
 struct request_socket req;
 struct response_socket *resp;
 int sfd;
 int sock_id;
 int res, len;
 char readbuf[1600];
 socket_init();
 if ((sfd = sfd_alloc()) == -1)
  return -1;
 req.type = 1;
 req.pid = pid;
 req.socket.domain = domain;
 req.socket.type = type;
 req.socket.protocol = protocol;
 len = sizeof(req);
 res = write_to_server((char *) &req, len);
 if (len != res)
  __error_sys("stupid_socket: write to server fifo error");
 read_from_server(readbuf, sizeof(readbuf));
 resp = (struct response_socket *) readbuf;
 if (resp->type == 2)
 {
  sock_id = resp->sock_id;
  printf("got sock id: %d\n", sock_id);
  (lock_reg(((lock_fd)), 6, 1, ((sock_id)), (0), (1)));
  sfd_table[sfd].sock_id = sock_id;
 } else {
  printf("unknown type id %d in response\n", resp->type);
 }
 return sfd;
}

canonical_original_code:
int stupid_socket(int domain, int type, int protocol)
{
 struct request_socket req;
 struct response_socket *resp;
 int sfd;
 int sock_id;
 int res; int len;
 char readbuf[1600];
 socket_init();
 if ((sfd = sfd_alloc()) == -1)
  return -1;
 req.type = 1;
 req.pid = pid;
 req.socket.domain = domain;
 req.socket.type = type;
 req.socket.protocol = protocol;
 len = sizeof(req);
 res = write_to_server((char *) &req, len);
 if (len != res)
  __error_sys("stupid_socket: write to server fifo error");
 read_from_server(readbuf, sizeof(readbuf));
 resp = (struct response_socket *) readbuf;
 if (resp->type == 2)
 {
  sock_id = resp->sock_id;
  printf("got sock id: %d\n", sock_id);
  (lock_reg(((lock_fd)), 6, 1, ((sock_id)), (0), (1)));
  sfd_table[sfd].sock_id = sock_id;
 } else {
  printf("unknown type id %d in response\n", resp->type);
 }
 return sfd;
}

variable_types: domain int

variable_types: type int

variable_types: protocol int

variable_types: struct request_socket { unsigned char type; int pid; struct socket_type { int domain; int type; int protocol; } socket; }

variable_types: struct response_socket { unsigned char type; int sock_id; } *

variable_types: sfd int

variable_types: res int

variable_types:
readbuf
char [1600]

user_defined_types: struct request_socket { unsigned char type; int pid; struct socket_type socket; }


decompiled_code:
ssize_t __fastcall stupid_bind(int a1, const __m128i *a2, int a3)
{
__m128i v3; // xmm0
int v4; // eax
char v6[4]; // [rsp+0h] [rbp-28h] BYREF
int v7; // [rsp+4h] [rbp-24h]
__m128i v8; // [rsp+8h] [rbp-20h]
int v9; // [rsp+18h] [rbp-10h]

v3 = _mm_loadu_si128(a2);
v6[0] = 6;
v4 = sfd_table[3 * a1 + 2];
v9 = a3;
v8 = v3;
v7 = v4;
return write_to_server(v6, 28);
}

original_code:
void stupid_bind(int sfd, struct sockaddr_in *servaddr, int addr_len)
{
 struct request_bind req_bind;
 int tot_len;
 req_bind.type = 6;
 req_bind.sock_id = sfd_table[sfd].sock_id;
 req_bind.sockaddr = *servaddr;
 req_bind.len = addr_len;
 tot_len = sizeof(struct request_bind);
 write_to_server((char *)&req_bind, tot_len);
}

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

variable_types: struct request_bind { unsigned char type; int sock_id; 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]; } sockaddr; unsigned int len; }

user_defined_types: struct request_bind { unsigned char type; int sock_id; struct sockaddr_in sockaddr; unsigned int len; }


decompiled_code:
ssize_t __fastcall stupid_sendto(int a1, const void *a2, int a3, const __m128i *a4)
{
int v5; // eax
char v7[4]; // [rsp+0h] [rbp-668h] BYREF
int v8; // [rsp+4h] [rbp-664h]
__m128i v9; // [rsp+8h] [rbp-660h]
int v10; // [rsp+18h] [rbp-650h]
char dest[1612]; // [rsp+1Ch] [rbp-64Ch] BYREF

v5 = sfd_table[3 * a1 + 2];
v10 = a3;
v9 = _mm_loadu_si128(a4);
v7[0] = 3;
v8 = v5;
memcpy(dest, a2, a3);
return write_to_server(v7, a3 + 28);
}

canonical_decompiled_code:
ssize_t __fastcall func8(int a1, const void *a2, int a3, const __m128i *a4)
{
int v5; // eax
char v7[4]; // [rsp+0h] [rbp-668h] BYREF
int v8; // [rsp+4h] [rbp-664h]
__m128i v9; // [rsp+8h] [rbp-660h]
int v10; // [rsp+18h] [rbp-650h]
char dest[1612]; // [rsp+1Ch] [rbp-64Ch] BYREF

v5 = sfd_table[3 * a1 + 2];
v10 = a3;
v9 = _mm_loadu_si128(a4);
v7[0] = 3;
v8 = v5;
memcpy(dest, a2, a3);
return func5(v7, a3 + 28);
}

original_code:
void stupid_sendto(int sfd, char *sendbuff, int len, struct sockaddr_in *servaddr, int addr_len)
{
 struct send_packet packet;
 int tot_len;
 packet.type = 3;
 packet.sock_id = sfd_table[sfd].sock_id;
 packet.len = len;
 packet.destaddr = *servaddr;
 memcpy(packet.data, sendbuff, len);
 tot_len = ((size_t)(&((struct send_packet *)0)->data)) + len;
 write_to_server((char *) &packet, tot_len);
}

canonical_original_code:
void stupid_sendto(int sfd, char *sendbuff, int len, struct sockaddr_in *servaddr, int addr_len)
{
 struct send_packet packet;
 int tot_len;
 packet.type = 3;
 packet.sock_id = sfd_table[sfd].sock_id;
 packet.len = len;
 packet.destaddr = *servaddr;
 memcpy(packet.data, sendbuff, len);
 tot_len = ((unsigned long)(&((struct send_packet *)0)->data)) + len;
 write_to_server((char *) &packet, tot_len);
}

variable_types:
sendbuff
char *

variable_types: struct send_packet { unsigned char type; int sock_id; 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]; } destaddr; unsigned int len; unsigned char data[1600]; }

user_defined_types: struct send_packet { unsigned char type; int sock_id; struct sockaddr_in destaddr; unsigned int len; unsigned char data[1600]; }


decompiled_code:
__int64 __fastcall stupid_recvfrom(int a1, void *a2, int a3, const __m128i *a4)
{
int v4; // eax
__m128i v5; // xmm0
unsigned int v6; // er12
char v8[4]; // [rsp+0h] [rbp-678h] BYREF
int v9; // [rsp+4h] [rbp-674h]
__m128i v10; // [rsp+8h] [rbp-670h]
int v11; // [rsp+18h] [rbp-660h]
char buf[4]; // [rsp+20h] [rbp-658h] BYREF
int v13; // [rsp+24h] [rbp-654h]
char src[1616]; // [rsp+28h] [rbp-650h] BYREF

v4 = sfd_table[3 * a1 + 2];
v5 = _mm_loadu_si128(a4);
v8[0] = 4;
v9 = v4;
v11 = a3;
v10 = v5;
if ( (unsigned int)write_to_server(v8, 28) != 28 )
_error_sys("write to server fifo");
read_from_server(buf, 1600);
if ( buf[0] != 5 )
_error_sys("recvfrom: wrong response type\n");
v6 = v13;
memcpy(a2, src, v13);
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func9(int a1, void *a2, int a3, const __m128i *a4)
{
int v4; // eax
__m128i v5; // xmm0
unsigned int v6; // er12
char v8[4]; // [rsp+0h] [rbp-678h] BYREF
int v9; // [rsp+4h] [rbp-674h]
__m128i v10; // [rsp+8h] [rbp-670h]
int v11; // [rsp+18h] [rbp-660h]
char buf[4]; // [rsp+20h] [rbp-658h] BYREF
int v13; // [rsp+24h] [rbp-654h]
char src[1616]; // [rsp+28h] [rbp-650h] BYREF

v4 = sfd_table[3 * a1 + 2];
v5 = _mm_loadu_si128(a4);
v8[0] = 4;
v9 = v4;
v11 = a3;
v10 = v5;
if ( (unsigned int)func5(v8, 28) != 28 )
func1("write to server fifo");
func4(buf, 1600);
if ( buf[0] != 5 )
func1("recvfrom: wrong response type\n");
v6 = v13;
memcpy(a2, src, v13);
return v6;
}

original_code:
int stupid_recvfrom(int sfd, char *recvbuff, int len, struct sockaddr_in *servaddr, int addr_len)
{
 struct request_packet req_packet;
 struct response_packet *resp_packet;
 int tot_len, res;
 char readbuf[1600];
 req_packet.type = 4;
 req_packet.sock_id = sfd_table[sfd].sock_id;
 req_packet.destaddr = *servaddr;
 req_packet.len = len;
 tot_len = sizeof(struct request_packet);
 res = write_to_server((char *) &req_packet, tot_len);
 if (tot_len != res)
  __error_sys("write to server fifo");
 read_from_server(readbuf, sizeof(readbuf));
 resp_packet = (struct response_packet *) readbuf;
 if (resp_packet->type != 5)
  __error_sys("recvfrom: wrong response type\n");
 else {
  memcpy(recvbuff, (char *)resp_packet->data, resp_packet->len);
  return resp_packet->len;
 }
 return 0;
}

canonical_original_code:
int stupid_recvfrom(int sfd, char *recvbuff, int len, struct sockaddr_in *servaddr, int addr_len)
{
 struct request_packet req_packet;
 struct response_packet *resp_packet;
 int tot_len; int res;
 char readbuf[1600];
 req_packet.type = 4;
 req_packet.sock_id = sfd_table[sfd].sock_id;
 req_packet.destaddr = *servaddr;
 req_packet.len = len;
 tot_len = sizeof(struct request_packet);
 res = write_to_server((char *) &req_packet, tot_len);
 if (tot_len != res)
  __error_sys("write to server fifo");
 read_from_server(readbuf, sizeof(readbuf));
 resp_packet = (struct response_packet *) readbuf;
 if (resp_packet->type != 5)
  __error_sys("recvfrom: wrong response type\n");
 else {
  memcpy(recvbuff, (char *)resp_packet->data, resp_packet->len);
  return resp_packet->len;
 }
 return 0;
}

variable_types:
recvbuff
char *

variable_types: struct request_packet { unsigned char type; int sock_id; 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]; } destaddr; unsigned int len; }

variable_types: struct response_packet { unsigned char type; int len; char data[1600]; } *

user_defined_types: struct request_packet { unsigned char type; int sock_id; struct sockaddr_in destaddr; unsigned int len; }


LongCon

decompiled_code:
__int64 __fastcall LongCon(char *a1, __int64 *a2)
{
__int64 v2; // rax
int v3; // er8
char *endptr; // [rsp+8h] [rbp-10h] BYREF

endptr = a1;
v2 = strtol(a1, &endptr, 0);
if ( *endptr )
v2 = 0LL;
v3 = -(*endptr == 0);
*a2 = v2;
return (unsigned int)(v3 + 1);
}

canonical_decompiled_code:
__int64 __fastcall func1(char *a1, __int64 *a2)
{
__int64 v2; // rax
int v3; // er8
char *endptr; // [rsp+8h] [rbp-10h] BYREF

endptr = a1;
v2 = strtol(a1, &endptr, 0);
if ( *endptr )
v2 = 0LL;
v3 = -(*endptr == 0);
*a2 = v2;
return (unsigned int)(v3 + 1);
}

original_code:
IX LongCon(I1 *str, I4 *i)
{
  I1 *endptr;
  I4 value;
  IX eflag=0;
  endptr = str;
  value = strtol(str, &endptr, 0);
  if(*endptr) {
    eflag = 1;
  }
  if(eflag) {
    *i = 0L;
  } else {
    *i = value;
  }
  return eflag;
}

canonical_original_code:
int LongCon(char *str, long *i)
{
  char *endptr;
  long value;
  int eflag = 0;
  endptr = str;
  value = strtol(str, &endptr, 0);
  if(*endptr) {
    eflag = 1;
  }
  if(eflag) {
    *i = 0L;
  } else {
    *i = value;
  }
  return eflag;
}

variable_types:
str
char *

variable_types:
i
long *

variable_types:
endptr
char *

variable_types: value long

variable_types: eflag int


IntCon

decompiled_code:
__int64 __fastcall IntCon(char *a1, _DWORD *a2)
{
int v2; // eax
_BOOL4 v3; // er8
__int64 result; // rax
__int64 v5; // [rsp+8h] [rbp-10h] BYREF

v2 = LongCon(a1, &v5);
if ( v2 )
{
*a2 = 0;
result = 1LL;
}
else
{
v3 = (unsigned __int64)(v5 + 0x8000) >= 0x10000;
if ( (unsigned __int64)(v5 + 0x8000) <= 0xFFFF )
v2 = v5;
*a2 = v2;
result = v3;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(char *a1, _DWORD *a2)
{
int v2; // eax
_BOOL4 v3; // er8
__int64 result; // rax
__int64 v5; // [rsp+8h] [rbp-10h] BYREF

v2 = func1(a1, &v5);
if ( v2 )
{
*a2 = 0;
result = 1LL;
}
else
{
v3 = (unsigned __int64)(v5 + 0x8000) >= 0x10000;
if ( (unsigned __int64)(v5 + 0x8000) <= 0xFFFF )
v2 = v5;
*a2 = v2;
result = v3;
}
return result;
}

original_code:
IX IntCon(I1 *str, IX *i)
{
  I4 value;
  IX eflag=0;
  if(LongCon(str, &value)) {
    eflag = 1;
  }
  if(value > 0x7fff) {
    eflag = 1;
  }
  if(value < (-0x7fff - 1)) {
    eflag = 1;
  }
  if(eflag) {
    *i = 0;
  } else {
    *i = (IX)value;
  }
  return eflag;
}

canonical_original_code:
int IntCon(char *str, int *i)
{
  long value;
  int eflag = 0;
  if(LongCon(str, &value)) {
    eflag = 1;
  }
  if(value > 0x7fff) {
    eflag = 1;
  }
  if(value < (-0x7fff - 1)) {
    eflag = 1;
  }
  if(eflag) {
    *i = 0;
  } else {
    *i = (int)value;
  }
  return eflag;
}

variable_types:
i
int *


NxtClose

decompiled_code:
void NxtClose()
{
if ( unxt )
{
if ( fclose(unxt) )
error(2, "misc.c", 0xB8u, "Problem while closing _UNXT");
unxt = 0LL;
}
}

canonical_decompiled_code:
void func3()
{
if ( unxt )
{
if ( fclose(unxt) )
func0(2, "misc.c", 0xB8u, "Problem while closing _UNXT");
unxt = 0LL;
}
}

original_code:
void NxtClose(void)
{
  if(_unxt) {
    if(fclose(_unxt)) {
      error(2, "misc.c", 184, "Problem while closing _UNXT");
    }
    _unxt = ((void *)0);
  }
}

canonical_original_code:
void NxtClose()
{
  if(_unxt) {
    if(fclose(_unxt)) {
      error(2, "misc.c", 184, "Problem while closing _UNXT");
    }
    _unxt = ((void *)0);
  }
}


NxtLine

decompiled_code:
const char *__fastcall NxtLine(const char *a1, int a2)
{
__int64 v2; // rbp
int v3; // ebx
int v4; // eax
char *v5; // rbp

v2 = 0LL;
do
{
while ( 1 )
{
v3 = getc(unxt);
if ( v3 == -1 )
return 0LL;
if ( (_DWORD)echo )
putc(v3, ulog);
if ( a2 <= 0 )
break;
if ( v3 != 13 )
{
v4 = v2 + 1;
v5 = (char *)&a1[v2];
if ( a2 == v4 )
{
*v5 = 0;
v2 = a2;
error(3, "misc.c", 0xDBu, "Buffer overflow: %s", a1);
}
else
{
*v5 = v3;
v2 = v4;
}
break;
}
}
}
while ( v3 != 10 );
if ( (_DWORD)v2 )
a1[v2 - 1] = 0;
else
*a1 = 0;
return a1;
}

canonical_decompiled_code:
const char *__fastcall func4(const char *a1, int a2)
{
__int64 v2; // rbp
int v3; // ebx
int v4; // eax
char *v5; // rbp

v2 = 0LL;
do
{
while ( 1 )
{
v3 = getc(unxt);
if ( v3 == -1 )
return 0LL;
if ( (_DWORD)echo )
putc(v3, ulog);
if ( a2 <= 0 )
break;
if ( v3 != 13 )
{
v4 = v2 + 1;
v5 = (char *)&a1[v2];
if ( a2 == v4 )
{
*v5 = 0;
v2 = a2;
func0(3, "misc.c", 0xDBu, "Buffer overflow: %s", a1);
}
else
{
*v5 = v3;
v2 = v4;
}
break;
}
}
}
while ( v3 != 10 );
if ( (_DWORD)v2 )
a1[v2 - 1] = 0;
else
*a1 = 0;
return a1;
}

original_code:
I1 *NxtLine(I1 *str, IX maxlen)
{
  IX c=0;
  IX i=0;
  while(c!='\n')
  {
    c = getc(_unxt);
    if(c==(-1)) {
      return ((void *)0);
    }
    if(_echo) {
      putc(c, _ulog);
    }
    if(maxlen < 1) {
      continue;
    }
    if(c == '\r') {
      continue;
    }
    str[i++] = (I1)c;
    if(i == maxlen)
    {
      str[i-1] = '\0';
      error(3, "misc.c", 219, "Buffer overflow: %s", str);
    }
  }
  if(i) {
    str[i-1] = '\0';
  } else {
    str[0] = '\0';
  }
  return str;
}

canonical_original_code:
char *NxtLine(char *str, int maxlen)
{
  int c = 0;
  int i = 0;
  while(c!='\n')
  {
    c = getc(_unxt);
    if(c==(-1)) {
      return ((void *)0);
    }
    if(_echo) {
      putc(c, _ulog);
    }
    if(maxlen < 1) {
      continue;
    }
    if(c == '\r') {
      continue;
    }
    str[i++] = (char)c;
    if(i == maxlen)
    {
      str[i-1] = '\0';
      error(3, "misc.c", 219, "Buffer overflow: %s", str);
    }
  }
  if(i) {
    str[i-1] = '\0';
  } else {
    str[0] = '\0';
  }
  return str;
}

variable_types: maxlen int

variable_types: c int

variable_types: i int

return_type: char *


NxtWord

decompiled_code:
char *__fastcall NxtWord(char *a1, int a2, int a3)
{
unsigned __int64 v4; // rax
int v5; // eax
int v6; // ebx
__int64 v7; // r15
int v8; // er12
char *result; // rax
__int64 v10; // rdx

LODWORD(v4) = getc(unxt);
if ( a2 <= 0 )
{
if ( (unsigned int)v4 > 0x2C || (v10 = 0x100100000600LL, !_bittest64(&v10, v4)) )
ungetc(v4, unxt);
goto LABEL_5;
}
if ( (_DWORD)v4 != 10 )
{
if ( a2 != 2 )
{
NxtLine(a1, 0);
if ( a2 == 1 )
goto LABEL_5;
goto LABEL_19;
}
NxtLine(a1, a3);
goto LABEL_20;
}
if ( a2 != 1 )
{
if ( a2 != 2 )
{
LABEL_19:
NxtLine(a1, a3);
if ( a2 == 3 )
{
while ( *a1 == 33 )
NxtLine(a1, a3);
}
}
LABEL_20:
ungetc(10, unxt);
return a1;
}
LABEL_5:
while ( 2 )
{
v5 = getc(unxt);
v6 = v5;
if ( v5 == -1 )
return 0LL;
if ( (_DWORD)echo )
putc(v5, ulog);
switch ( v6 )
{
case 0:
case 9:
case 10:
case 13:
case 32:
case 44:
continue;
case 33:
NxtLine(a1, 0);
continue;
case 42:
NxtClose();
result = 0LL;
break;
default:
*a1 = v6;
v7 = 1LL;
while ( 2 )
{
v8 = getc(unxt);
if ( v8 == -1 )
return 0LL;
if ( (_DWORD)echo )
putc(v8, ulog);
switch ( v8 )
{
case 0:
case 13:
continue;
case 9:
case 10:
case 32:
case 44:
a1[(int)v7] = 0;
ungetc(v8, unxt);
result = a1;
break;
default:
if ( a3 == (_DWORD)v7 + 1 )
{
a1[v7] = 0;
error(3, "misc.c", 0x16Bu, "Buffer overflow: %s", a1);
}
else
{
a1[v7] = v8;
}
++v7;
continue;
}
break;
}
break;
}
break;
}
return result;
}

canonical_decompiled_code:
char *__fastcall func5(char *a1, int a2, int a3)
{
unsigned __int64 v4; // rax
int v5; // eax
int v6; // ebx
__int64 v7; // r15
int v8; // er12
char *result; // rax
__int64 v10; // rdx

LODWORD(v4) = getc(unxt);
if ( a2 <= 0 )
{
if ( (unsigned int)v4 > 0x2C || (v10 = 0x100100000600LL, !_bittest64(&v10, v4)) )
ungetc(v4, unxt);
goto LABEL_5;
}
if ( (_DWORD)v4 != 10 )
{
if ( a2 != 2 )
{
func4(a1, 0);
if ( a2 == 1 )
goto LABEL_5;
goto LABEL_19;
}
func4(a1, a3);
goto LABEL_20;
}
if ( a2 != 1 )
{
if ( a2 != 2 )
{
LABEL_19:
func4(a1, a3);
if ( a2 == 3 )
{
while ( *a1 == 33 )
func4(a1, a3);
}
}
LABEL_20:
ungetc(10, unxt);
return a1;
}
LABEL_5:
while ( 2 )
{
v5 = getc(unxt);
v6 = v5;
if ( v5 == -1 )
return 0LL;
if ( (_DWORD)echo )
putc(v5, ulog);
switch ( v6 )
{
case 0:
case 9:
case 10:
case 13:
case 32:
case 44:
continue;
case 33:
func4(a1, 0);
continue;
case 42:
func3();
result = 0LL;
break;
default:
*a1 = v6;
v7 = 1LL;
while ( 2 )
{
v8 = getc(unxt);
if ( v8 == -1 )
return 0LL;
if ( (_DWORD)echo )
putc(v8, ulog);
switch ( v8 )
{
case 0:
case 13:
continue;
case 9:
case 10:
case 32:
case 44:
a1[(int)v7] = 0;
ungetc(v8, unxt);
result = a1;
break;
default:
if ( a3 == (_DWORD)v7 + 1 )
{
a1[v7] = 0;
func0(3, "misc.c", 0x16Bu, "Buffer overflow: %s", a1);
}
else
{
a1[v7] = v8;
}
++v7;
continue;
}
break;
}
break;
}
break;
}
return result;
}

original_code:
I1 *NxtWord(I1 *str, IX flag, IX maxlen)
{
  IX c;
  IX i=0;
  IX done=0;
  c = getc(_unxt);
  if(flag > 0) {
    if(c != '\n') {
      if(flag == 2) {
        NxtLine(str, maxlen);
      } else {
        NxtLine(str, 0);
      }
    }
    if(flag > 1) {
      if(flag > 2) {
        NxtLine(str, maxlen);
        if(flag == 3) {
          while(str[0] == '!') {
            NxtLine(str, maxlen);
          }
        }
      }
      ungetc('\n', _unxt);
      return str;
    }
  }
  else
  {
    if(c == ' ' || c == ',' || c == '\n' || c == '\t') {
      ;
    } else {
      ungetc(c, _unxt);
    }
  }
  while(!done) {
    c = getc(_unxt);
    if(c==(-1)) {
      return ((void *)0);
    }
    if(_echo) {
      putc(c, _ulog);
    }
    switch(c) {
    case ' ':
    case ',':
    case '\n':
    case '\r':
    case '\t':
    case '\0':
      break;
    case '!':
      NxtLine(str, 0);
      break;
    case '*':
      NxtClose();
      return ((void *)0);
    default:
      str[i++] = (I1)c;
      done = 1;
      break;
    }
  }
  done = 0;
  while(!done) {
    c = getc(_unxt);
    if(c==(-1)) {
      return ((void *)0);
    }
    if(_echo) {
      putc(c, _ulog);
    }
    switch(c) {
    case '\n':
    case ' ':
    case ',':
    case '\t':
      str[i] = '\0';
      done = 1;
      break;
    case '\r':
    case '\0':
      break;
    default:
      str[i++] = (I1)c;
      if(i == maxlen) {
        str[i-1] = '\0';
        error(3, "misc.c", 363, "Buffer overflow: %s", str);
      }
      break;
    }
  }
  ungetc(c, _unxt);
  return str;
}

canonical_original_code:
char *NxtWord(char *str, int flag, int maxlen)
{
  int c;
  int i = 0;
  int done = 0;
  c = getc(_unxt);
  if(flag > 0) {
    if(c != '\n') {
      if(flag == 2) {
        NxtLine(str, maxlen);
      } else {
        NxtLine(str, 0);
      }
    }
    if(flag > 1) {
      if(flag > 2) {
        NxtLine(str, maxlen);
        if(flag == 3) {
          while(str[0] == '!') {
            NxtLine(str, maxlen);
          }
        }
      }
      ungetc('\n', _unxt);
      return str;
    }
  }
  else
  {
    if(c == ' ' || c == ',' || c == '\n' || c == '\t') {
      ;
    } else {
      ungetc(c, _unxt);
    }
  }
  while(!done) {
    c = getc(_unxt);
    if(c==(-1)) {
      return ((void *)0);
    }
    if(_echo) {
      putc(c, _ulog);
    }
    switch(c) {
    case ' ':
    case ',':
    case '\n':
    case '\r':
    case '\t':
    case '\0':
      break;
    case '!':
      NxtLine(str, 0);
      break;
    case '*':
      NxtClose();
      return ((void *)0);
    default:
      str[i++] = (char)c;
      done = 1;
      break;
    }
  }
  done = 0;
  while(!done) {
    c = getc(_unxt);
    if(c==(-1)) {
      return ((void *)0);
    }
    if(_echo) {
      putc(c, _ulog);
    }
    switch(c) {
    case '\n':
    case ' ':
    case ',':
    case '\t':
      str[i] = '\0';
      done = 1;
      break;
    case '\r':
    case '\0':
      break;
    default:
      str[i++] = (char)c;
      if(i == maxlen) {
        str[i-1] = '\0';
        error(3, "misc.c", 363, "Buffer overflow: %s", str);
      }
      break;
    }
  }
  ungetc(c, _unxt);
  return str;
}

variable_types: flag int

variable_types: done int


ReadIX

decompiled_code:
__int64 __fastcall ReadIX(int a1)
{
__int64 result; // rax
__int64 v2; // [rsp+8h] [rbp-10h] BYREF

NxtWord((char *)&string, a1, 256);
if ( (unsigned int)LongCon((char *)&string, &v2) || (result = v2, (unsigned __int64)(v2 + 0x80000000LL) > 0xFFFFFFFF) )
{
error(2, "misc.c", 0x1A1u, "Bad integer: %s", (const char *)&string);
result = v2;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(int a1)
{
__int64 result; // rax
__int64 v2; // [rsp+8h] [rbp-10h] BYREF

func5((char *)&string, a1, 256);
if ( (unsigned int)func1((char *)&string, &v2) || (result = v2, (unsigned __int64)(v2 + 0x80000000LL) > 0xFFFFFFFF) )
{
func0(2, "misc.c", 0x1A1u, "Bad integer: %s", (const char *)&string);
result = v2;
}
return result;
}

original_code:
IX ReadIX(IX flag)
{
  I4 value;
  NxtWord(_string, flag, sizeof(_string));
  if(LongCon(_string, &value) || value > 0x7fffffff || value < (-0x7fffffff - 1)) {
    error(2, "misc.c", 417, "Bad integer: %s", _string);
  }
  return (IX)value;
}

canonical_original_code:
int ReadIX(int flag)
{
  long value;
  NxtWord(_string, flag, sizeof(_string));
  if(LongCon(_string, &value) || value > 0x7fffffff || value < (-0x7fffffff - 1)) {
    error(2, "misc.c", 417, "Bad integer: %s", _string);
  }
  return (int)value;
}


DblCon

decompiled_code:
__int64 __fastcall DblCon(char *a1, double *a2)
{
double v2; // xmm0_8
__int64 result; // rax
char *endptr; // [rsp+8h] [rbp-10h] BYREF

endptr = a1;
v2 = strtod(a1, &endptr);
result = 0LL;
if ( *endptr )
{
v2 = 0.0;
result = 1LL;
}
*a2 = v2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(char *a1, double *a2)
{
double v2; // xmm0_8
__int64 result; // rax
char *endptr; // [rsp+8h] [rbp-10h] BYREF

endptr = a1;
v2 = strtod(a1, &endptr);
result = 0LL;
if ( *endptr )
{
v2 = 0.0;
result = 1LL;
}
*a2 = v2;
return result;
}

original_code:
IX DblCon(I1 *str, R8 *f)
{
  I1 *endptr;
  R8 value;
  IX eflag=0;
  endptr = str;
  value = strtod(str, &endptr);
  if(*endptr != '\0') {
    eflag = 1;
  }
  if(eflag) {
    *f = 0.0;
  } else {
    *f = value;
  }
  return eflag;
}

canonical_original_code:
int DblCon(char *str, double *f)
{
  char *endptr;
  double value;
  int eflag = 0;
  endptr = str;
  value = strtod(str, &endptr);
  if(*endptr != '\0') {
    eflag = 1;
  }
  if(eflag) {
    *f = 0.0;
  } else {
    *f = value;
  }
  return eflag;
}

variable_types:
f
double *

variable_types: value double


decompiled_code:
double __fastcall ReadR8(int a1)
{
double v2[2]; // [rsp+8h] [rbp-10h] BYREF

NxtWord((char *)&string, a1, 256);
if ( (unsigned int)DblCon((char *)&string, v2) )
error(2, "misc.c", 0x180u, "%s is not a (valid) number", (const char *)&string);
return v2[0];
}

canonical_decompiled_code:
double __fastcall func8(int a1)
{
double v2[2]; // [rsp+8h] [rbp-10h] BYREF

func5((char *)&string, a1, 256);
if ( (unsigned int)func7((char *)&string, v2) )
func0(2, "misc.c", 0x180u, "%s is not a (valid) number", (const char *)&string);
return v2[0];
}

original_code:
R8 ReadR8(IX flag)
{
  R8 value;
  NxtWord(_string, flag, sizeof(_string));
  if(DblCon(_string, &value)) {
    error(2, "misc.c", 384, "%s is not a (valid) number", _string);
  }
  return value;
}

canonical_original_code:
double ReadR8(int flag)
{
  double value;
  NxtWord(_string, flag, sizeof(_string));
  if(DblCon(_string, &value)) {
    error(2, "misc.c", 384, "%s is not a (valid) number", _string);
  }
  return value;
}

return_type: double


decompiled_code:
float __fastcall ReadR4(int a1)
{
double v2[2]; // [rsp+8h] [rbp-10h] BYREF

NxtWord((char *)&string, a1, 256);
if ( !(unsigned int)DblCon((char *)&string, v2) && v2[0] <= 3.402823466385289e38 && v2[0] >= -3.402823466385289e38 )
return v2[0];
error(2, "misc.c", 0x190u, "Bad float value: %s", (const char *)&string);
return v2[0];
}

canonical_decompiled_code:
float __fastcall func9(int a1)
{
double v2[2]; // [rsp+8h] [rbp-10h] BYREF

func5((char *)&string, a1, 256);
if ( !(unsigned int)func7((char *)&string, v2) && v2[0] <= 3.402823466385289e38 && v2[0] >= -3.402823466385289e38 )
return v2[0];
func0(2, "misc.c", 0x190u, "Bad float value: %s", (const char *)&string);
return v2[0];
}

canonical_original_code:
float ReadR4(int flag)
{
  double value;
  NxtWord(_string, flag, sizeof(_string));
  if(DblCon(_string, &value) || value > 3.40282346638528859811704183484516925e+38F || value < -3.40282346638528859811704183484516925e+38F) {
    error(2, "misc.c", 400, "Bad float value: %s", _string);
  }
  return (float)value;
}

return_type: float


FltCon

variable_types:
f
float *


CPUtime

decompiled_code:
float __fastcall CPUtime(float a1)
{
float v1; // xmm0_4
struct rusage usage; // [rsp+10h] [rbp-98h] BYREF

getrusage(RUSAGE_SELF, &usage);
v1 = (double)SLODWORD(usage.ru_utime.tv_usec) * 0.000001 + (float)SLODWORD(usage.ru_utime.tv_sec);
return v1 - a1;
}

canonical_decompiled_code:
float __fastcall func11(float a1)
{
float v1; // xmm0_4
struct rusage usage; // [rsp+10h] [rbp-98h] BYREF

getrusage(RUSAGE_SELF, &usage);
v1 = (double)SLODWORD(usage.ru_utime.tv_usec) * 0.000001 + (float)SLODWORD(usage.ru_utime.tv_sec);
return v1 - a1;
}

original_code:
R4 CPUtime(R4 t1)
{
  R4 t2;
  struct rusage r2;
  getrusage(RUSAGE_SELF,&r2);
  t2 = (R4)r2.ru_utime.tv_sec + 1.0e-6*r2.ru_utime.tv_usec;
  return (t2-t1);
}

canonical_original_code:
float CPUtime(float t1)
{
  float t2;
  struct rusage r2;
  getrusage(RUSAGE_SELF,&r2);
  t2 = (float)r2.ru_utime.tv_sec + 1.0e-6*r2.ru_utime.tv_usec;
  return (t2-t1);
}

variable_types: struct rusage { struct timeval { long tv_sec; long tv_usec; } ru_utime; struct timeval ru_stime; union { long ru_maxrss; long __ru_maxrss_word; }; union { long ru_ixrss; long __ru_ixrss_word; }; union { long ru_idrss; long __ru_idrss_word; }; union { long ru_isrss; long __ru_isrss_word; }; union { long ru_minflt; long __ru_minflt_word; }; union { long ru_majflt; long __ru_majflt_word; }; union { long ru_nswap; long __ru_nswap_word; }; union { long ru_inblock; long __ru_inblock_word; }; union { long ru_oublock; long __ru_oublock_word; }; union { long ru_msgsnd; long __ru_msgsnd_word; }; union { long ru_msgrcv; long __ru_msgrcv_word; }; union { long ru_nsignals; long __ru_nsignals_word; }; union { long ru_nvcsw; long __ru_nvcsw_word; }; union { long ru_nivcsw; long __ru_nivcsw_word; }; }

user_defined_types: struct rusage { struct timeval ru_utime; struct timeval ru_stime; union { long ru_maxrss; long __ru_maxrss_word; }; union { long ru_ixrss; long __ru_ixrss_word; }; union { long ru_idrss; long __ru_idrss_word; }; union { long ru_isrss; long __ru_isrss_word; }; union { long ru_minflt; long __ru_minflt_word; }; union { long ru_majflt; long __ru_majflt_word; }; union { long ru_nswap; long __ru_nswap_word; }; union { long ru_inblock; long __ru_inblock_word; }; union { long ru_oublock; long __ru_oublock_word; }; union { long ru_msgsnd; long __ru_msgsnd_word; }; union { long ru_msgrcv; long __ru_msgrcv_word; }; union { long ru_nsignals; long __ru_nsignals_word; }; union { long ru_nvcsw; long __ru_nvcsw_word; }; union { long ru_nivcsw; long __ru_nivcsw_word; }; }


cleanStore

original_code:
void cleanStore(TMPSTORE *s, I1 *file, IX line)
{
  TMPBLOCK *next = s->first->next;
  s->first->next = ((void *)0);
  while(next != ((void *)0)) {
    s->last = next->next;
    Fre_E(next->memory, s->Nmax * s->size, file, line);
    Fre_E(next, sizeof(TMPBLOCK), file, line);
    next = s->last;
  }
  s->last = s->first;
  memset(s->first->memory, 0, s->Nmax * s->size);
  s->nblock = 1;
  s->n = 0;
  return;
}

canonical_original_code:
void cleanStore(struct TMPSTORE *s, char *file, int line)
{
  struct TMPBLOCK *next = s->first->next;
  s->first->next = ((void *)0);
  while(next != ((void *)0)) {
    s->last = next->next;
    Fre_E(next->memory, s->Nmax * s->size, file, line);
    Fre_E(next, sizeof(TMPBLOCK), file, line);
    next = s->last;
  }
  s->last = s->first;
  memset(s->first->memory, 0, s->Nmax * s->size);
  s->nblock = 1;
  s->n = 0;
  return;
}

variable_types: struct TMPSTORE { unsigned long size; unsigned long Nmax; unsigned long n; struct TMPBLOCK { struct TMPBLOCK *next; char *memory; } *first; struct TMPBLOCK *last; unsigned long nblock; } *

variable_types:
file
char *

variable_types: line int

user_defined_types: struct TMPSTORE { unsigned long size; unsigned long Nmax; unsigned long n; struct TMPBLOCK *first; struct TMPBLOCK *last; unsigned long nblock; }


deleteStore

decompiled_code:
__int64 __fastcall deleteStore(__int64 a1, __int64 a2, unsigned int a3)
{
cleanStore(a1, a2, a3);
Fre_E(*(_QWORD *)(*(_QWORD *)(a1 + 24) + 8LL), *(_DWORD *)(a1 + 8) * (unsigned int)*(_QWORD *)a1, a2, a3);
Fre_E(*(_QWORD *)(a1 + 24), 16LL, a2, a3);
return Fre_E(a1, 48LL, a2, a3);
}

original_code:
void deleteStore(TMPSTORE *s, I1 *file, IX line)
{
  cleanStore(s, file, line);
  Fre_E(s->first->memory, s->Nmax * s->size, file, line);
  Fre_E(s->first, sizeof(TMPBLOCK), file, line);
  Fre_E(s, sizeof(TMPSTORE), file, line);
  return;
}

canonical_original_code:
void deleteStore(struct TMPSTORE *s, char *file, int line)
{
  cleanStore(s, file, line);
  Fre_E(s->first->memory, s->Nmax * s->size, file, line);
  Fre_E(s->first, sizeof(TMPBLOCK), file, line);
  Fre_E(s, sizeof(TMPSTORE), file, line);
  return;
}

user_defined_types: struct TMPBLOCK { struct TMPBLOCK *next; char *memory; }


SaveVF

decompiled_code:
void __fastcall SaveVF(const char *a1, const char *a2, const char *a3, unsigned int a4, unsigned int a5, __int64 a6, unsigned int a7, __int64 a8, __int64 a9, __int64 a10, __int64 a11)
{
size_t v12; // rax
__int64 v13; // r9
__int64 v14; // [rsp-10h] [rbp-48h]
char s[30]; // [rsp+0h] [rbp-38h] BYREF
__int16 v16; // [rsp+1Eh] [rbp-1Ah]

sprintf(s, "%s %s %d %d %d %d", a2, a3, a4, a5, a6, a7);
v12 = strlen(s);
v13 = v14;
if ( v12 <= 0x1D )
memset(&s[(int)v12], 32, (unsigned int)(29 - v12) + 1LL);
v16 = 10;
if ( a4 )
{
if ( a4 == 1 )
{
v16 = 2573;
SaveF1(a1, s, a7, a8, a9, a10, a11);
}
else
{
error(3, (int)"savevf.c", (const char *)&loc_B0 + 3, "Undefined format: %d", a4, v13);
}
}
else
{
SaveF0(a1, s, a7, a8, a9, a10, a11);
}
}

canonical_decompiled_code:
void __fastcall func3(const char *a1, const char *a2, const char *a3, unsigned int a4, unsigned int a5, __int64 a6, unsigned int a7, __int64 a8, __int64 a9, __int64 a10, __int64 a11)
{
size_t v12; // rax
__int64 v13; // r9
__int64 v14; // [rsp-10h] [rbp-48h]
char s[30]; // [rsp+0h] [rbp-38h] BYREF
__int16 v16; // [rsp+1Eh] [rbp-1Ah]

sprintf(s, "%s %s %d %d %d %d", a2, a3, a4, a5, a6, a7);
v12 = strlen(s);
v13 = v14;
if ( v12 <= 0x1D )
memset(&s[(int)v12], 32, (unsigned int)(29 - v12) + 1LL);
v16 = 10;
if ( a4 )
{
if ( a4 == 1 )
{
v16 = 2573;
func1(a1, s, a7, a8, a9, a10, a11);
}
else
{
error(3, (int)"savevf.c", (const char *)&loc_B0 + 3, "Undefined format: %d", a4, v13);
}
}
else
{
func0(a1, s, a7, a8, a9, a10, a11);
}
}

original_code:
void SaveVF(I1 *fileName, I1 *program, I1 *version,
            IX format, IX encl, IX didemit, IX nSrf,
            R4 *area, R4 *emit, R8 **AF, R4 *vtmp)
{
  I1 header[32];
  IX j;
  sprintf(header, "%s %s %d %d %d %d",
          program, version, format, encl, didemit, nSrf);
  for(j=strlen(header); j<30; j++) {
    header[j] = ' ';
  }
  header[30] = '\n';
  header[31] = '\0';
  if(format == 0) {
    SaveF0(fileName, header, nSrf, area, emit, AF, vtmp);
  } else if(format == 1) {
    header[30] = '\r';
    header[31] = '\n';
    SaveF1(fileName, header, nSrf, area, emit, AF, vtmp);
  } else {
    error(3, "savevf.c", 179, "Undefined format: %d", format);
  }
}

canonical_original_code:
void SaveVF(char *fileName, char *program, char *version, int format, int encl, int didemit, int nSrf, float *area, float *emit, double **AF, float *vtmp)
{
  char header[32];
  int j;
  sprintf(header, "%s %s %d %d %d %d",
          program, version, format, encl, didemit, nSrf);
  for(j=strlen(header); j<30; j++) {
    header[j] = ' ';
  }
  header[30] = '\n';
  header[31] = '\0';
  if(format == 0) {
    SaveF0(fileName, header, nSrf, area, emit, AF, vtmp);
  } else if(format == 1) {
    header[30] = '\r';
    header[31] = '\n';
    SaveF1(fileName, header, nSrf, area, emit, AF, vtmp);
  } else {
    error(3, "savevf.c", 179, "Undefined format: %d", format);
  }
}

variable_types:
fileName
char *

variable_types:
program
char *

variable_types:
version
char *

variable_types: format int

variable_types: encl int

variable_types: didemit int

variable_types: nSrf int

variable_types:
area
float *

variable_types:
emit
float *

variable_types:
AF
double **

variable_types:
vtmp
float *

variable_types:
header
char [32]

variable_types: j int


identicalAlignedParallelRects

repo: jasondegraw/ViewFactors

original_code:
double identicalAlignedParallelRects(double X, double Y)
{
  double X2,Y2,sqrt1pX2,sqrt1pY2;
  X2 = X*X;
  Y2 = Y*Y;
  sqrt1pX2 = sqrt(1.0+X2);
  sqrt1pY2 = sqrt(1.0+Y2);
  return 2.0*( 0.5*log((1.0+X2)*(1.0+Y2)/(1+X2+Y2))
        + X*sqrt1pY2*atan(X/sqrt1pY2)
        + Y*sqrt1pX2*atan(Y/sqrt1pX2)
        - X*atan(X)
        - Y*atan(Y) )/(3.14159265358979323846*X*Y);
}

canonical_original_code:
double identicalAlignedParallelRects(double X, double Y)
{
  double X2; double Y2; double sqrt1pX2; double sqrt1pY2;
  X2 = X*X;
  Y2 = Y*Y;
  sqrt1pX2 = sqrt(1.0+X2);
  sqrt1pY2 = sqrt(1.0+Y2);
  return 2.0*( 0.5*log((1.0+X2)*(1.0+Y2)/(1+X2+Y2))
        + X*sqrt1pY2*atan(X/sqrt1pY2)
        + Y*sqrt1pX2*atan(Y/sqrt1pX2)
        - X*atan(X)
        - Y*atan(Y) )/(3.14159265358979323846*X*Y);
}

variable_types: X double

variable_types: Y double


original_code:
double identicalAlignedParallelRects1(double a, double b, double c)
{
  double X2,Y2,sqrt1pX2,sqrt1pY2;
  double X = a/c;
  double Y = b/c;
  X2 = X*X;
  Y2 = Y*Y;
  sqrt1pX2 = sqrt(1.0+X2);
  sqrt1pY2 = sqrt(1.0+Y2);
  return 2.0*( 0.5*log((1.0+X2)*(1.0+Y2)/(1+X2+Y2))
           + X*sqrt1pY2*atan(X/sqrt1pY2)
           + Y*sqrt1pX2*atan(Y/sqrt1pX2)
           - X*atan(X)
           - Y*atan(Y) )/(3.14159265358979323846*X*Y);
}

canonical_original_code:
double identicalAlignedParallelRects1(double a, double b, double c)
{
  double X2; double Y2; double sqrt1pX2; double sqrt1pY2;
  double X = a/c;
  double Y = b/c;
  X2 = X*X;
  Y2 = Y*Y;
  sqrt1pX2 = sqrt(1.0+X2);
  sqrt1pY2 = sqrt(1.0+Y2);
  return 2.0*( 0.5*log((1.0+X2)*(1.0+Y2)/(1+X2+Y2))
           + X*sqrt1pY2*atan(X/sqrt1pY2)
           + Y*sqrt1pX2*atan(Y/sqrt1pX2)
           - X*atan(X)
           - Y*atan(Y) )/(3.14159265358979323846*X*Y);
}

variable_types: a double

variable_types: b double

variable_types: c double


perpCommonEdgeRects

original_code:
double perpCommonEdgeRects(double H, double W)
{
 double H2 = H*H;
 double W2 = W*W;
 double H2pW2 = H2+W2;
 double H2pW2p1 = H2pW2 + 1;
 double H2p1 = H2+1;
 double W2p1 = W2+1;
 double rootH2pW2 = sqrt(H2pW2);
 return (W*atan(1.0/W)+H*atan(1.0/H)-rootH2pW2*atan(1.0/rootH2pW2)
         + 0.25*log((W2p1*H2p1/H2pW2p1)
              *pow(W2*H2pW2p1/(W2p1*H2pW2),W2)
        *pow(H2*H2pW2p1/(H2p1*H2pW2),H2)))/(3.14159265358979323846*W);
}

variable_types: H double

variable_types: W double


original_code:
double perpCommonEdgeRects1(double w, double h, double l)
{
 double H, W, H2, W2, H2pW2, H2pW2p1, H2p1, W2p1, rootH2pW2;
 if(w*l*h <= 0.0)
  return 0.0;
 H = h/l;
 W = w/l;
 H2 = H*H;
 W2 = W*W;
 H2pW2 = H2+W2;
 H2pW2p1 = H2pW2 + 1;
 H2p1 = H2+1;
 W2p1 = W2+1;
 rootH2pW2 = sqrt(H2pW2);
 return l*l*(W*atan(1.0/W)+H*atan(1.0/H)-rootH2pW2*atan(1.0/rootH2pW2)
             + 0.25*log((W2p1*H2p1/H2pW2p1)
                  *pow(W2*H2pW2p1/(W2p1*H2pW2),W2)
            *pow(H2*H2pW2p1/(H2p1*H2pW2),H2)))/3.14159265358979323846;
}

canonical_original_code:
double perpCommonEdgeRects1(double w, double h, double l)
{
 double H; double W; double H2; double W2; double H2pW2; double H2pW2p1; double H2p1; double W2p1; double rootH2pW2;
 if(w*l*h <= 0.0)
  return 0.0;
 H = h/l;
 W = w/l;
 H2 = H*H;
 W2 = W*W;
 H2pW2 = H2+W2;
 H2pW2p1 = H2pW2 + 1;
 H2p1 = H2+1;
 W2p1 = W2+1;
 rootH2pW2 = sqrt(H2pW2);
 return l*l*(W*atan(1.0/W)+H*atan(1.0/H)-rootH2pW2*atan(1.0/rootH2pW2)
             + 0.25*log((W2p1*H2p1/H2pW2p1)
                  *pow(W2*H2pW2p1/(W2p1*H2pW2),W2)
            *pow(H2*H2pW2p1/(H2p1*H2pW2),H2)))/3.14159265358979323846;
}

variable_types: w double

variable_types: h double

variable_types: l double


diffSphereToAlignedRect

decompiled_code:
double __fastcall diffSphereToAlignedRect(double a1, double a2)
{
double v2; // xmm2_8
double v3; // xmm0_8
double v4; // xmm0_8

v2 = a1 * a2;
v3 = a1 * a1 + 1.0 + a2 * a2;
if ( v3 < 0.0 )
v4 = sqrt(v3);
else
v4 = sqrt(v3);
return atan(v2 / v4) * 0.25 / 3.141592653589793;
}

canonical_decompiled_code:
double __fastcall func4(double a1, double a2)
{
double v2; // xmm2_8
double v3; // xmm0_8
double v4; // xmm0_8

v2 = a1 * a2;
v3 = a1 * a1 + 1.0 + a2 * a2;
if ( v3 < 0.0 )
v4 = sqrt(v3);
else
v4 = sqrt(v3);
return atan(v2 / v4) * 0.25 / 3.141592653589793;
}

original_code:
double diffSphereToAlignedRect(double X, double Y)
{
  return 0.25*atan(X*Y/sqrt(1.0+X*X+Y*Y))/3.14159265358979323846;
}