decompiled_code:
__int64 vgadrv_init()
{
if ( _svgalib_driver_report )
fwrite("Using VGA driver.\n", 1uLL, 0x12uLL, stderr);
_svgalib_driverspecs = _svgalib_vga_driverspecs;
_svgalib_banked_mem_base = 655360LL;
_svgalib_banked_mem_size = 0x10000LL;
return 0LL;
}

canonical_decompiled_code:
__int64 func8()
{
if ( _svgalib_driver_report )
fwrite("Using VGA driver.\n", 1uLL, 0x12uLL, stderr);
_svgalib_driverspecs = _svgalib_vga_driverspecs;
_svgalib_banked_mem_base = 655360LL;
_svgalib_banked_mem_size = 0x10000LL;
return 0LL;
}

original_code:
static int vgadrv_init(int force, int par1, int par2)
{
    if (__svgalib_driver_report)
 fprintf(stderr,"Using VGA driver.\n");
    __svgalib_driverspecs = &__svgalib_vga_driverspecs;
    __svgalib_banked_mem_base=0xa0000;
    __svgalib_banked_mem_size=0x10000;
    return 0;
}

variable_types: force int


decompiled_code:
__int64 vga_test()
{
unsigned __int8 v0; // bl
char v1; // bp
__int64 result; // rax

v0 = _svgalib_port_in(968LL);
_svgalib_delay();
_svgalib_port_out(~v0, 968LL);
_svgalib_delay();
v1 = _svgalib_port_in(968LL);
_svgalib_delay();
_svgalib_port_out(v0, 968LL);
result = 0LL;
if ( ~v0 == v1 )
{
vgadrv_init(0LL, 0LL, 0LL);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 func9()
{
unsigned __int8 v0; // bl
char v1; // bp
__int64 result; // rax

v0 = _svgalib_port_in(968LL);
_svgalib_delay();
_svgalib_port_out(~v0, 968LL);
_svgalib_delay();
v1 = _svgalib_port_in(968LL);
_svgalib_delay();
_svgalib_port_out(v0, 968LL);
result = 0LL;
if ( ~v0 == v1 )
{
func8(0LL, 0LL, 0LL);
result = 1LL;
}
return result;
}

original_code:
static int vga_test(void)
{
    unsigned char save, back;
    save = __svgalib_port_in(0x3C8);
    __svgalib_delay();
    __svgalib_port_out(~save, 0x3C8);
    __svgalib_delay();
    back = __svgalib_port_in(0x3C8);
    __svgalib_delay();
    __svgalib_port_out(save, 0x3C8);
    save = ~save;
    if (back == save) {
 vgadrv_init(0, 0, 0);
 return 1;
    }
    return 0;
}

canonical_original_code:
static int vga_test()
{
    unsigned char save; unsigned char back;
    save = __svgalib_port_in(0x3C8);
    __svgalib_delay();
    __svgalib_port_out(~save, 0x3C8);
    __svgalib_delay();
    back = __svgalib_port_in(0x3C8);
    __svgalib_delay();
    __svgalib_port_out(save, 0x3C8);
    save = ~save;
    if (back == save) {
 vgadrv_init(0, 0, 0);
 return 1;
    }
    return 0;
}

variable_types:
save
unsigned char

variable_types:
back
unsigned char

return_type: int


saveregs

decompiled_code:
__int64 saveregs()
{
return 0LL;
}

canonical_decompiled_code:
__int64 func2()
{
return 0LL;
}

original_code:
static int saveregs(unsigned char regs[])
{
    return 0;
}

variable_types:
regs
unsigned char []


setregs

decompiled_code:
void setregs()
{
;
}

canonical_decompiled_code:
void func3()
{
;
}

original_code:
static void setregs(const unsigned char regs[], int mode)
{
}

canonical_original_code:
static void setregs(unsigned char regs[], int mode)
{
}

variable_types: mode int

return_type: void


setlogicalwidth

decompiled_code:
__int64 __fastcall setlogicalwidth(int a1)
{
return _svgalib_outcrtc(19LL, (unsigned int)(a1 >> 3));
}

canonical_decompiled_code:
__int64 __fastcall func4(int a1)
{
return _svgalib_outcrtc(19LL, (unsigned int)(a1 >> 3));
}

original_code:
static void setlogicalwidth(int width)
{
    __svgalib_outcrtc(0x13, width >> 3);
}

variable_types: width int


setdisplaystart

decompiled_code:
__int64 __fastcall setdisplaystart(int a1)
{
int v1; // ebx
__int64 v2; // rax
int v3; // eax
void (__fastcall *v5)(__int64, _QWORD); // rbp
char v6; // al
void (__fastcall *v7)(__int64, _QWORD); // rbp
char v8; // al

LOWORD(v1) = a1;
v2 = vga_getmodeinfo((unsigned int)lastmode);
if ( !*(_DWORD *)(v2 + 8) )
{
v3 = *(_DWORD *)(v2 + 12);
if ( v3 == 16 )
{
v5 = (void (__fastcall *)(__int64, _QWORD))_svgalib_outatt;
v6 = _svgalib_inatt(51LL);
v5(51LL, a1 & 7 | v6 & 0xF0u);
v1 = a1 >> 3;
}
else if ( v3 == 256 )
{
v7 = (void (__fastcall *)(__int64, _QWORD))_svgalib_outatt;
v8 = _svgalib_inatt(51LL);
v7(51LL, v8 & 0xF0 | (2 * (_BYTE)a1) & 6u);
v1 = a1 >> 2;
}
}
_svgalib_outcrtc(13LL, (unsigned __int8)v1);
return _svgalib_outcrtc(12LL, BYTE1(v1));
}

canonical_decompiled_code:
__int64 __fastcall func5(int a1)
{
int v1; // ebx
__int64 v2; // rax
int v3; // eax
void (__fastcall *v5)(__int64, _QWORD); // rbp
char v6; // al
void (__fastcall *v7)(__int64, _QWORD); // rbp
char v8; // al

LOWORD(v1) = a1;
v2 = vga_getmodeinfo((unsigned int)lastmode);
if ( !*(_DWORD *)(v2 + 8) )
{
v3 = *(_DWORD *)(v2 + 12);
if ( v3 == 16 )
{
v5 = (void (__fastcall *)(__int64, _QWORD))_svgalib_outatt;
v6 = _svgalib_inatt(51LL);
v5(51LL, a1 & 7 | v6 & 0xF0u);
v1 = a1 >> 3;
}
else if ( v3 == 256 )
{
v7 = (void (__fastcall *)(__int64, _QWORD))_svgalib_outatt;
v8 = _svgalib_inatt(51LL);
v7(51LL, v8 & 0xF0 | (2 * (_BYTE)a1) & 6u);
v1 = a1 >> 2;
}
}
_svgalib_outcrtc(13LL, (unsigned __int8)v1);
return _svgalib_outcrtc(12LL, BYTE1(v1));
}

original_code:
static void setdisplaystart(int address)
{
    vga_modeinfo *modeinfo;
    modeinfo = vga_getmodeinfo(lastmode);
    if (modeinfo->bytesperpixel == 0)
 switch (modeinfo->colors) {
 case 16:
            __svgalib_outatt(0x33,(__svgalib_inatt(0x33)&0xf0) | (address & 7));
     address >>= 3;
     break;
 case 256:
            __svgalib_outatt(0x33,(__svgalib_inatt(0x33)&0xf0) | ((address & 3)<<1) );
     address >>= 2;
     break;
 }
    __svgalib_outcrtc(0x0d, address & 0x00ff);
    __svgalib_outcrtc(0x0c, (address & 0xff00) >> 8);
}

canonical_original_code:
static void setdisplaystart(int address)
{
    struct vga_modeinfo *modeinfo;
    modeinfo = vga_getmodeinfo(lastmode);
    if (modeinfo->bytesperpixel == 0)
 switch (modeinfo->colors) {
 case 16:
            __svgalib_outatt(0x33,(__svgalib_inatt(0x33)&0xf0) | (address & 7));
     address >>= 3;
     break;
 case 256:
            __svgalib_outatt(0x33,(__svgalib_inatt(0x33)&0xf0) | ((address & 3)<<1) );
     address >>= 2;
     break;
 }
    __svgalib_outcrtc(0x0d, address & 0x00ff);
    __svgalib_outcrtc(0x0c, (address & 0xff00) >> 8);
}

variable_types: address int

variable_types: struct vga_modeinfo { int width; int height; int bytesperpixel; int colors; int linewidth; int maxlogicalwidth; int startaddressrange; int maxpixels; int haveblit; int flags; int chiptype; int memory; int linewidth_unit; char *linear_aperture; int aperture_size; void (*set_aperture_page)(int page); void *extensions; } *


modeavailable

decompiled_code:
_BOOL8 __fastcall modeavailable(unsigned int a1)
{
return (unsigned __int64)_svgalib_mode_in_table(&vga_modes, a1) > 1;
}

canonical_decompiled_code:
_BOOL8 __fastcall func6(unsigned int a1)
{
return (unsigned __int64)_svgalib_mode_in_table(&vga_modes, a1) > 1;
}

original_code:
static int modeavailable(int mode)
{
    const unsigned char *regs;
    regs = __svgalib_mode_in_table(vga_modes, mode);
    if (regs != ((void *)0) && regs != ((unsigned char *)1))
 return 1;
    return 0;
}

canonical_original_code:
static int modeavailable(int mode)
{
    unsigned char *regs;
    regs = __svgalib_mode_in_table(vga_modes, mode);
    if (regs != ((void *)0) && regs != ((unsigned char *)1))
 return 1;
    return 0;
}

variable_types:
regs
unsigned char *


setmode

decompiled_code:
__int64 __fastcall setmode(unsigned int a1)
{
unsigned int v1; // ebx
unsigned __int64 v3; // rax

v1 = a1;
if ( a1 )
{
v3 = _svgalib_mode_in_table(&vga_modes, a1);
if ( v3 > 1 )
{
lastmode = a1;
_svgalib_setregs(v3);
v1 = 0;
}
else
{
v1 = 1;
}
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned int a1)
{
unsigned int v1; // ebx
unsigned __int64 v3; // rax

v1 = a1;
if ( a1 )
{
v3 = _svgalib_mode_in_table(&vga_modes, a1);
if ( v3 > 1 )
{
lastmode = a1;
_svgalib_setregs(v3);
v1 = 0;
}
else
{
v1 = 1;
}
}
return v1;
}

original_code:
static int setmode(int mode, int prv_mode)
{
    const unsigned char *regs;
    if (mode == 0)
 return 0;
    regs = __svgalib_mode_in_table(vga_modes, mode);
    if (regs == ((void *)0) || regs == ((unsigned char *)1))
 return 1;
    lastmode = mode;
    __svgalib_setregs(regs);
    return 0;
}

canonical_original_code:
static int setmode(int mode, int prv_mode)
{
    unsigned char *regs;
    if (mode == 0)
 return 0;
    regs = __svgalib_mode_in_table(vga_modes, mode);
    if (regs == ((void *)0) || regs == ((unsigned char *)1))
 return 1;
    lastmode = mode;
    __svgalib_setregs(regs);
    return 0;
}


original_code:
static int I2061A_SelectClock(int frequency)
{
    unsigned int m;
    int i;
    double freq, fvco;
    double dev, devx;
    double delta, deltax;
    double f0;
    unsigned int p, q;
    unsigned int bestp = 0, bestq = 0, bestm = 0, besti = 0;
    freq = ((double) frequency) / 1000.0;
    if (freq > I2061A_range[13])
 freq = I2061A_range[13];
    else if (freq < 7.0)
 freq = 7.0;
    delta = 999.0;
    dev = 999.0;
    for (m = 0; m < 8; m++) {
 fvco = freq * (1 << m);
 if (fvco < 50.0 || fvco > 120.0)
     continue;
 f0 = fvco / (14.31818 * 2.0);
 for (q = 14; q <= 71; q++) {
     p = (int) (f0 * q + 0.5);
     if (p < 4 || p > 130)
  continue;
     deltax = (double) (p) / (double) (q) - f0;
     if (deltax < 0)
  deltax = -deltax;
     if (deltax <= delta) {
  for (i = 13; i >= 0; i--)
      if (fvco >= I2061A_range[i])
   break;
  devx = (fvco - (I2061A_range[i] + I2061A_range[i + 1]) / 2) / fvco;
  if (devx < 0)
      devx = -devx;
  if (deltax < delta || devx < dev) {
      delta = deltax;
      dev = devx;
      bestp = p;
      bestq = q;
      bestm = m;
      besti = i;
  }
     }
 }
    }
    return ((((((int) besti << 7) | (bestp - 3)) << 3) | bestm) << 7) | (bestq - 2);
}

canonical_original_code:
static int I2061A_SelectClock(int frequency)
{
    unsigned int m;
    int i;
    double freq; double fvco;
    double dev; double devx;
    double delta; double deltax;
    double f0;
    unsigned int p; unsigned int q;
    unsigned int bestp = 0; unsigned int bestq = 0; unsigned int bestm = 0; unsigned int besti = 0;
    freq = ((double) frequency) / 1000.0;
    if (freq > I2061A_range[13])
 freq = I2061A_range[13];
    else if (freq < 7.0)
 freq = 7.0;
    delta = 999.0;
    dev = 999.0;
    for (m = 0; m < 8; m++) {
 fvco = freq * (1 << m);
 if (fvco < 50.0 || fvco > 120.0)
     continue;
 f0 = fvco / (14.31818 * 2.0);
 for (q = 14; q <= 71; q++) {
     p = (int) (f0 * q + 0.5);
     if (p < 4 || p > 130)
  continue;
     deltax = (double) (p) / (double) (q) - f0;
     if (deltax < 0)
  deltax = -deltax;
     if (deltax <= delta) {
  for (i = 13; i >= 0; i--)
      if (fvco >= I2061A_range[i])
   break;
  devx = (fvco - (I2061A_range[i] + I2061A_range[i + 1]) / 2) / fvco;
  if (devx < 0)
      devx = -devx;
  if (deltax < delta || devx < dev) {
      delta = deltax;
      dev = devx;
      bestp = p;
      bestq = q;
      bestm = m;
      besti = i;
  }
     }
 }
    }
    return ((((((int) besti << 7) | (bestp - 3)) << 3) | bestm) << 7) | (bestq - 2);
}

variable_types: frequency int

variable_types:
m
unsigned int

variable_types: i int

variable_types: freq double

variable_types: fvco double

variable_types: dev double

variable_types: devx double

variable_types: delta double

variable_types: deltax double

variable_types:
p
unsigned int

variable_types:
q
unsigned int

variable_types:
bestp
unsigned int

variable_types:
bestq
unsigned int

variable_types:
bestm
unsigned int

variable_types:
besti
unsigned int


original_code:
static int I2061A_GetClock(int dwv)
{
    int clock_q = (dwv & 0x7f) + 2;
    int clock_m = (dwv >> 7) & 7;
    int clock_p = ((dwv >> 10) & 0x7f) + 3;
    double fvco;
    fvco = (14.31818 * 2.0) / (1 << clock_m);
    return (int) (((fvco * clock_p) / clock_q) * 1000);
}

variable_types: dwv int


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

result = I2061A_SelectClock(a1);
if ( (_DWORD)result )
result = I2061A_GetClock(result);
return result;
}

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

result = func0(a1);
if ( (_DWORD)result )
result = func1(result);
return result;
}

original_code:
static int I2061A_match_programmable_clock(int desiredclock)
{
    int dvw;
    dvw = I2061A_SelectClock(desiredclock);
    if (dvw)
 return I2061A_GetClock(dvw);
    return 0;
}

variable_types: desiredclock int

variable_types: dvw int


decompiled_code:
__int64 __fastcall I2061A_saveState(__int64 a1)
{
void (*v1)(void); // rax
_DWORD *v2; // rbx
__int64 result; // rax

v1 = (void (*)(void))_svgalib_I2061A_clockchip_methods[4];
if ( v1 )
v1();
v2 = (_DWORD *)(SHIDWORD(_svgalib_I2061A_clockchip_methods[7]) + a1);
result = I2061A_SelectClock((int)_svgalib_I2061A_clockchip_methods[7]);
*v2 = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
void (*v1)(void); // rax
_DWORD *v2; // rbx
__int64 result; // rax

v1 = (void (*)(void))_svgalib_I2061A_clockchip_methods[4];
if ( v1 )
v1();
v2 = (_DWORD *)(SHIDWORD(_svgalib_I2061A_clockchip_methods[7]) + a1);
result = func0((int)_svgalib_I2061A_clockchip_methods[7]);
*v2 = result;
return result;
}

original_code:
static void I2061A_saveState(unsigned char *regs)
{
    int *dvwp;
    if (__svgalib_I2061A_clockchip_methods.DAC_saveState)
 __svgalib_I2061A_clockchip_methods.DAC_saveState(regs);
    dvwp = (int *) (regs + __svgalib_I2061A_clockchip_methods.DAC_stateSize);
    *dvwp = I2061A_SelectClock(__svgalib_I2061A_clockchip_methods.TextFrequency);
}

variable_types:
regs
unsigned char *

variable_types:
dvwp
int *


decompiled_code:
__int64 __fastcall I2061A_initializeState(__int64 a1, int a2, __int64 a3, int a4)
{
void (*v5)(void); // rax
_DWORD *v6; // rbp
__int64 result; // rax

v5 = (void (*)(void))_svgalib_I2061A_clockchip_methods[6];
if ( v5 )
v5();
v6 = (_DWORD *)(SHIDWORD(_svgalib_I2061A_clockchip_methods[7]) + a1);
if ( a2 <= 16 )
{
if ( a2 > 8 )
a4 *= 2;
}
else
{
a4 *= 4;
}
result = I2061A_SelectClock(a4);
*v6 = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, int a2, __int64 a3, int a4)
{
void (*v5)(void); // rax
_DWORD *v6; // rbp
__int64 result; // rax

v5 = (void (*)(void))_svgalib_I2061A_clockchip_methods[6];
if ( v5 )
v5();
v6 = (_DWORD *)(SHIDWORD(_svgalib_I2061A_clockchip_methods[7]) + a1);
if ( a2 <= 16 )
{
if ( a2 > 8 )
a4 *= 2;
}
else
{
a4 *= 4;
}
result = func0(a4);
*v6 = result;
return result;
}

original_code:
static void I2061A_initializeState(unsigned char *regs, int bpp, int colormode, int pixelclock)
{
    int *dvwp;
    if (__svgalib_I2061A_clockchip_methods.DAC_initializeState)
 __svgalib_I2061A_clockchip_methods.DAC_initializeState(regs, bpp, colormode, pixelclock);
    dvwp = (int *) (regs + __svgalib_I2061A_clockchip_methods.DAC_stateSize);
    if (bpp > 16)
 pixelclock *= 4;
    else if (bpp > 8)
 pixelclock *= 2;
    *dvwp = I2061A_SelectClock((int) pixelclock);
}

variable_types: bpp int

variable_types: colormode int

variable_types: pixelclock int


decompiled_code:
__int64 __fastcall I2061A_init_clock(unsigned int a1)
{
unsigned __int16 v2; // bp
unsigned __int16 v3; // r15
unsigned int v4; // ebx
unsigned __int8 v5; // al
unsigned __int8 v6; // r14
int v7; // ebp
int v8; // er14
unsigned __int16 v9; // bp
char v10; // al
unsigned int v12; // [rsp+8h] [rbp-60h]
unsigned int v13; // [rsp+Ch] [rbp-5Ch]
unsigned int v14; // [rsp+14h] [rbp-54h]
unsigned __int16 v15; // [rsp+18h] [rbp-50h]
unsigned __int8 v16; // [rsp+1Fh] [rbp-49h]
char v17[2]; // [rsp+2Ch] [rbp-3Ch]
char v18; // [rsp+2Eh] [rbp-3Ah]
unsigned __int8 v19; // [rsp+2Fh] [rbp-39h]

v2 = (_svgalib_port_in(972LL) & 1) == 0 ? 948 : 980;
v3 = _svgalib_port_in(972LL);
v4 = v2;
_svgalib_port_out(66LL, v2);
v14 = (unsigned __int16)(v2 + 1);
v15 = _svgalib_port_in((unsigned __int16)(v2 + 1));
_svgalib_port_outw(256LL, 964LL);
_svgalib_port_out(1LL, 964LL);
v5 = _svgalib_port_in(965LL);
_svgalib_port_out(v5 | 0x20u, 965LL);
_svgalib_port_out(66LL, v2);
_svgalib_port_out(3LL, (unsigned __int16)(v2 + 1));
_svgalib_port_outw(768LL, 964LL);
v6 = v3 & 0xF3;
v18 = v3 & 0xF3;
v19 = v3 & 0xF3 | 8;
v16 = v3 & 0xF3 | 4;
v17[0] = v16;
v17[1] = v3 & 0xF3 | 0xC;
_svgalib_port_out(66LL, v2);
_svgalib_port_in((unsigned __int16)(v2 + 1));
_svgalib_port_outw(256LL, 964LL);
_svgalib_port_out(v3 | 8u, 962LL);
_svgalib_port_in(v2);
_svgalib_port_in(v2);
_svgalib_port_out(v3 | 0xCu, 962LL);
_svgalib_port_in(v2);
_svgalib_port_in(v2);
v7 = 5;
v12 = v19;
do
{
_svgalib_port_out(v19, 962LL);
_svgalib_port_in(v4);
_svgalib_port_in(v4);
v13 = v6 | 0xC;
_svgalib_port_out(v6 | 0xCu, 962LL);
_svgalib_port_in(v4);
_svgalib_port_in(v4);
--v7;
}
while ( v7 );
_svgalib_port_out(v19, 962LL);
_svgalib_port_in(v4);
_svgalib_port_in(v4);
_svgalib_port_out(v6, 962LL);
_svgalib_port_in(v4);
_svgalib_port_in(v4);
_svgalib_port_out(v16, 962LL);
_svgalib_port_in(v4);
_svgalib_port_in(v4);
_svgalib_port_out(v6, 962LL);
_svgalib_port_in(v4);
_svgalib_port_in(v4);
_svgalib_port_out(v16, 962LL);
_svgalib_port_in(v4);
_svgalib_port_in(v4);
v8 = 24;
do
{
v9 = a1 & 1;
a1 >>= 1;
_svgalib_port_out((unsigned __int8)v17[1 - v9], 962LL);
_svgalib_port_in(v4);
_svgalib_port_in(v4);
_svgalib_port_out((unsigned __int8)*(&v18 + 1 - v9), 962LL);
_svgalib_port_in(v4);
_svgalib_port_in(v4);
_svgalib_port_out((unsigned __int8)*(&v18 + v9), 962LL);
_svgalib_port_in(v4);
_svgalib_port_in(v4);
_svgalib_port_out((unsigned __int8)v17[v9], 962LL);
_svgalib_port_in(v4);
_svgalib_port_in(v4);
--v8;
}
while ( v8 );
_svgalib_port_out(v13, 962LL);
_svgalib_port_in(v4);
_svgalib_port_in(v4);
_svgalib_port_out(v12, 962LL);
_svgalib_port_in(v4);
_svgalib_port_in(v4);
_svgalib_port_out(v13, 962LL);
_svgalib_port_in(v4);
_svgalib_port_in(v4);
_svgalib_port_out(1LL, 964LL);
v10 = _svgalib_port_in(965LL);
_svgalib_port_out(v10 & 0xDF, 965LL);
_svgalib_port_out(66LL, v4);
_svgalib_port_out(v15, v14);
_svgalib_port_out(v3, 962LL);
_svgalib_port_outw(768LL, 964LL);
vga_waitretrace();
vga_waitretrace();
vga_waitretrace();
vga_waitretrace();
vga_waitretrace();
vga_waitretrace();
return vga_waitretrace();
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int a1)
{
unsigned __int16 v2; // bp
unsigned __int16 v3; // r15
unsigned int v4; // ebx
unsigned __int8 v5; // al
unsigned __int8 v6; // r14
int v7; // ebp
int v8; // er14
unsigned __int16 v9; // bp
char v10; // al
unsigned int v12; // [rsp+8h] [rbp-60h]
unsigned int v13; // [rsp+Ch] [rbp-5Ch]
unsigned int v14; // [rsp+14h] [rbp-54h]
unsigned __int16 v15; // [rsp+18h] [rbp-50h]
unsigned __int8 v16; // [rsp+1Fh] [rbp-49h]
char v17[2]; // [rsp+2Ch] [rbp-3Ch]
char v18; // [rsp+2Eh] [rbp-3Ah]
unsigned __int8 v19; // [rsp+2Fh] [rbp-39h]

v2 = (_svgalib_port_in(972LL) & 1) == 0 ? 948 : 980;
v3 = _svgalib_port_in(972LL);
v4 = v2;
_svgalib_port_out(66LL, v2);
v14 = (unsigned __int16)(v2 + 1);
v15 = _svgalib_port_in((unsigned __int16)(v2 + 1));
_svgalib_port_outw(256LL, 964LL);
_svgalib_port_out(1LL, 964LL);
v5 = _svgalib_port_in(965LL);
_svgalib_port_out(v5 | 0x20u, 965LL);
_svgalib_port_out(66LL, v2);
_svgalib_port_out(3LL, (unsigned __int16)(v2 + 1));
_svgalib_port_outw(768LL, 964LL);
v6 = v3 & 0xF3;
v18 = v3 & 0xF3;
v19 = v3 & 0xF3 | 8;
v16 = v3 & 0xF3 | 4;
v17[0] = v16;
v17[1] = v3 & 0xF3 | 0xC;
_svgalib_port_out(66LL, v2);
_svgalib_port_in((unsigned __int16)(v2 + 1));
_svgalib_port_outw(256LL, 964LL);
_svgalib_port_out(v3 | 8u, 962LL);
_svgalib_port_in(v2);
_svgalib_port_in(v2);
_svgalib_port_out(v3 | 0xCu, 962LL);
_svgalib_port_in(v2);
_svgalib_port_in(v2);
v7 = 5;
v12 = v19;
do
{
_svgalib_port_out(v19, 962LL);
_svgalib_port_in(v4);
_svgalib_port_in(v4);
v13 = v6 | 0xC;
_svgalib_port_out(v6 | 0xCu, 962LL);
_svgalib_port_in(v4);
_svgalib_port_in(v4);
--v7;
}
while ( v7 );
_svgalib_port_out(v19, 962LL);
_svgalib_port_in(v4);
_svgalib_port_in(v4);
_svgalib_port_out(v6, 962LL);
_svgalib_port_in(v4);
_svgalib_port_in(v4);
_svgalib_port_out(v16, 962LL);
_svgalib_port_in(v4);
_svgalib_port_in(v4);
_svgalib_port_out(v6, 962LL);
_svgalib_port_in(v4);
_svgalib_port_in(v4);
_svgalib_port_out(v16, 962LL);
_svgalib_port_in(v4);
_svgalib_port_in(v4);
v8 = 24;
do
{
v9 = a1 & 1;
a1 >>= 1;
_svgalib_port_out((unsigned __int8)v17[1 - v9], 962LL);
_svgalib_port_in(v4);
_svgalib_port_in(v4);
_svgalib_port_out((unsigned __int8)*(&v18 + 1 - v9), 962LL);
_svgalib_port_in(v4);
_svgalib_port_in(v4);
_svgalib_port_out((unsigned __int8)*(&v18 + v9), 962LL);
_svgalib_port_in(v4);
_svgalib_port_in(v4);
_svgalib_port_out((unsigned __int8)v17[v9], 962LL);
_svgalib_port_in(v4);
_svgalib_port_in(v4);
--v8;
}
while ( v8 );
_svgalib_port_out(v13, 962LL);
_svgalib_port_in(v4);
_svgalib_port_in(v4);
_svgalib_port_out(v12, 962LL);
_svgalib_port_in(v4);
_svgalib_port_in(v4);
_svgalib_port_out(v13, 962LL);
_svgalib_port_in(v4);
_svgalib_port_in(v4);
_svgalib_port_out(1LL, 964LL);
v10 = _svgalib_port_in(965LL);
_svgalib_port_out(v10 & 0xDF, 965LL);
_svgalib_port_out(66LL, v4);
_svgalib_port_out(v15, v14);
_svgalib_port_out(v3, 962LL);
_svgalib_port_outw(768LL, 964LL);
vga_waitretrace();
vga_waitretrace();
vga_waitretrace();
vga_waitretrace();
vga_waitretrace();
vga_waitretrace();
return vga_waitretrace();
}

original_code:
static void I2061A_init_clock(unsigned int setup)
{
    unsigned char nclk[2], clk[2];
    unsigned short restore42;
    unsigned short oldclk;
    unsigned short bitval;
    int i;
    unsigned char c;
    unsigned short crtcaddr = (__svgalib_port_in(0x3CC) & 0x01) ? 0x3D4 : 0x3B4;
    oldclk = __svgalib_port_in(0x3CC);
    __svgalib_port_out(0x42, crtcaddr);
    restore42 = __svgalib_port_in(crtcaddr + 1);
    __svgalib_port_outw(0x0100, 0x3C4);
    __svgalib_port_out(1, 0x3C4);
    c = __svgalib_port_in(0x3C5);
    __svgalib_port_out(0x20 | c, 0x3C5);
    __svgalib_port_out(0x42, crtcaddr);
    __svgalib_port_out(0x03, crtcaddr + 1);
    __svgalib_port_outw(0x0300, 0x3C4);
    nclk[0] = oldclk & 0xF3;
    nclk[1] = nclk[0] | 0x08;
    clk[0] = nclk[0] | 0x04;
    clk[1] = nclk[0] | 0x0C;
    __svgalib_port_out(0x42, crtcaddr);
    (void) __svgalib_port_in(crtcaddr + 1);
    __svgalib_port_outw(0x0100, 0x3C4);
    __svgalib_port_out(oldclk | 0x08, 0x3C2), (void)__svgalib_port_in(crtcaddr), (void)__svgalib_port_in(crtcaddr);
    __svgalib_port_out(oldclk | 0x0C, 0x3C2), (void)__svgalib_port_in(crtcaddr), (void)__svgalib_port_in(crtcaddr);
    for (i = 0; i < 5; i++) {
 __svgalib_port_out(nclk[1], 0x3C2), (void)__svgalib_port_in(crtcaddr), (void)__svgalib_port_in(crtcaddr);
 __svgalib_port_out(clk[1], 0x3C2), (void)__svgalib_port_in(crtcaddr), (void)__svgalib_port_in(crtcaddr);
    }
    __svgalib_port_out(nclk[1], 0x3C2), (void)__svgalib_port_in(crtcaddr), (void)__svgalib_port_in(crtcaddr);
    __svgalib_port_out(nclk[0], 0x3C2), (void)__svgalib_port_in(crtcaddr), (void)__svgalib_port_in(crtcaddr);
    __svgalib_port_out(clk[0], 0x3C2), (void)__svgalib_port_in(crtcaddr), (void)__svgalib_port_in(crtcaddr);
    __svgalib_port_out(nclk[0], 0x3C2), (void)__svgalib_port_in(crtcaddr), (void)__svgalib_port_in(crtcaddr);
    __svgalib_port_out(clk[0], 0x3C2), (void)__svgalib_port_in(crtcaddr), (void)__svgalib_port_in(crtcaddr);
    for (i = 0; i < 24; i++) {
 bitval = setup & 0x01;
 setup >>= 1;
 __svgalib_port_out(clk[1 - bitval], 0x3C2), (void)__svgalib_port_in(crtcaddr), (void)__svgalib_port_in(crtcaddr);
 __svgalib_port_out(nclk[1 - bitval], 0x3C2), (void)__svgalib_port_in(crtcaddr), (void)__svgalib_port_in(crtcaddr);
 __svgalib_port_out(nclk[bitval], 0x3C2), (void)__svgalib_port_in(crtcaddr), (void)__svgalib_port_in(crtcaddr);
 __svgalib_port_out(clk[bitval], 0x3C2), (void)__svgalib_port_in(crtcaddr), (void)__svgalib_port_in(crtcaddr);
    }
    __svgalib_port_out(clk[1], 0x3C2), (void)__svgalib_port_in(crtcaddr), (void)__svgalib_port_in(crtcaddr);
    __svgalib_port_out(nclk[1], 0x3C2), (void)__svgalib_port_in(crtcaddr), (void)__svgalib_port_in(crtcaddr);
    __svgalib_port_out(clk[1], 0x3C2), (void)__svgalib_port_in(crtcaddr), (void)__svgalib_port_in(crtcaddr);
    __svgalib_port_out(1, 0x3C4);
    c = __svgalib_port_in(0x3C5);
    __svgalib_port_out(0xDF & c, 0x3C5);
    __svgalib_port_out(0x42, crtcaddr);
    __svgalib_port_out(restore42, crtcaddr + 1);
    __svgalib_port_out(oldclk, 0x3C2);
    __svgalib_port_outw(0x0300, 0x3C4);
    vga_waitretrace();
    vga_waitretrace();
    vga_waitretrace();
    vga_waitretrace();
    vga_waitretrace();
    vga_waitretrace();
    vga_waitretrace();
}

canonical_original_code:
static void I2061A_init_clock(unsigned int setup)
{
    unsigned char nclk[2]; unsigned char clk[2];
    unsigned short restore42;
    unsigned short oldclk;
    unsigned short bitval;
    int i;
    unsigned char c;
    unsigned short crtcaddr = (__svgalib_port_in(0x3CC) & 0x01) ? 0x3D4 : 0x3B4;
    oldclk = __svgalib_port_in(0x3CC);
    __svgalib_port_out(0x42, crtcaddr);
    restore42 = __svgalib_port_in(crtcaddr + 1);
    __svgalib_port_outw(0x0100, 0x3C4);
    __svgalib_port_out(1, 0x3C4);
    c = __svgalib_port_in(0x3C5);
    __svgalib_port_out(0x20 | c, 0x3C5);
    __svgalib_port_out(0x42, crtcaddr);
    __svgalib_port_out(0x03, crtcaddr + 1);
    __svgalib_port_outw(0x0300, 0x3C4);
    nclk[0] = oldclk & 0xF3;
    nclk[1] = nclk[0] | 0x08;
    clk[0] = nclk[0] | 0x04;
    clk[1] = nclk[0] | 0x0C;
    __svgalib_port_out(0x42, crtcaddr);
    (void) __svgalib_port_in(crtcaddr + 1);
    __svgalib_port_outw(0x0100, 0x3C4);
    __svgalib_port_out(oldclk | 0x08, 0x3C2), (void)__svgalib_port_in(crtcaddr), (void)__svgalib_port_in(crtcaddr);
    __svgalib_port_out(oldclk | 0x0C, 0x3C2), (void)__svgalib_port_in(crtcaddr), (void)__svgalib_port_in(crtcaddr);
    for (i = 0; i < 5; i++) {
 __svgalib_port_out(nclk[1], 0x3C2), (void)__svgalib_port_in(crtcaddr), (void)__svgalib_port_in(crtcaddr);
 __svgalib_port_out(clk[1], 0x3C2), (void)__svgalib_port_in(crtcaddr), (void)__svgalib_port_in(crtcaddr);
    }
    __svgalib_port_out(nclk[1], 0x3C2), (void)__svgalib_port_in(crtcaddr), (void)__svgalib_port_in(crtcaddr);
    __svgalib_port_out(nclk[0], 0x3C2), (void)__svgalib_port_in(crtcaddr), (void)__svgalib_port_in(crtcaddr);
    __svgalib_port_out(clk[0], 0x3C2), (void)__svgalib_port_in(crtcaddr), (void)__svgalib_port_in(crtcaddr);
    __svgalib_port_out(nclk[0], 0x3C2), (void)__svgalib_port_in(crtcaddr), (void)__svgalib_port_in(crtcaddr);
    __svgalib_port_out(clk[0], 0x3C2), (void)__svgalib_port_in(crtcaddr), (void)__svgalib_port_in(crtcaddr);
    for (i = 0; i < 24; i++) {
 bitval = setup & 0x01;
 setup >>= 1;
 __svgalib_port_out(clk[1 - bitval], 0x3C2), (void)__svgalib_port_in(crtcaddr), (void)__svgalib_port_in(crtcaddr);
 __svgalib_port_out(nclk[1 - bitval], 0x3C2), (void)__svgalib_port_in(crtcaddr), (void)__svgalib_port_in(crtcaddr);
 __svgalib_port_out(nclk[bitval], 0x3C2), (void)__svgalib_port_in(crtcaddr), (void)__svgalib_port_in(crtcaddr);
 __svgalib_port_out(clk[bitval], 0x3C2), (void)__svgalib_port_in(crtcaddr), (void)__svgalib_port_in(crtcaddr);
    }
    __svgalib_port_out(clk[1], 0x3C2), (void)__svgalib_port_in(crtcaddr), (void)__svgalib_port_in(crtcaddr);
    __svgalib_port_out(nclk[1], 0x3C2), (void)__svgalib_port_in(crtcaddr), (void)__svgalib_port_in(crtcaddr);
    __svgalib_port_out(clk[1], 0x3C2), (void)__svgalib_port_in(crtcaddr), (void)__svgalib_port_in(crtcaddr);
    __svgalib_port_out(1, 0x3C4);
    c = __svgalib_port_in(0x3C5);
    __svgalib_port_out(0xDF & c, 0x3C5);
    __svgalib_port_out(0x42, crtcaddr);
    __svgalib_port_out(restore42, crtcaddr + 1);
    __svgalib_port_out(oldclk, 0x3C2);
    __svgalib_port_outw(0x0300, 0x3C4);
    vga_waitretrace();
    vga_waitretrace();
    vga_waitretrace();
    vga_waitretrace();
    vga_waitretrace();
    vga_waitretrace();
    vga_waitretrace();
}

variable_types:
setup
unsigned int

variable_types:
nclk
unsigned char [2]

variable_types:
clk
unsigned char [2]

variable_types:
oldclk
unsigned short

variable_types:
bitval
unsigned short

variable_types: i int

variable_types:
c
unsigned char

variable_types:
crtcaddr
unsigned short


decompiled_code:
__int64 __fastcall I2061A_restoreState(__int64 a1)
{
void (*v1)(void); // rax
__int64 result; // rax
int v3; // edi

v1 = (void (*)(void))_svgalib_I2061A_clockchip_methods[5];
if ( v1 )
v1();
result = SHIDWORD(_svgalib_I2061A_clockchip_methods[7]);
v3 = *(_DWORD *)(a1 + result);
if ( v3 )
result = I2061A_init_clock(v3 | 0x400000u);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
void (*v1)(void); // rax
__int64 result; // rax
int v3; // edi

v1 = (void (*)(void))_svgalib_I2061A_clockchip_methods[5];
if ( v1 )
v1();
result = SHIDWORD(_svgalib_I2061A_clockchip_methods[7]);
v3 = *(_DWORD *)(a1 + result);
if ( v3 )
result = func5(v3 | 0x400000u);
return result;
}

original_code:
static void I2061A_restoreState(const unsigned char *regs)
{
    unsigned int clknum = 2;
    int *dvwp;
    if (__svgalib_I2061A_clockchip_methods.DAC_restoreState)
 __svgalib_I2061A_clockchip_methods.DAC_restoreState(regs);
    dvwp = (int *) (regs + __svgalib_I2061A_clockchip_methods.DAC_stateSize);
    if (*dvwp) {
 I2061A_init_clock(((unsigned int) *dvwp) | (((int) clknum) << 21));
    }
}

canonical_original_code:
static void I2061A_restoreState(unsigned char *regs)
{
    unsigned int clknum = 2;
    int *dvwp;
    if (__svgalib_I2061A_clockchip_methods.DAC_restoreState)
 __svgalib_I2061A_clockchip_methods.DAC_restoreState(regs);
    dvwp = (int *) (regs + __svgalib_I2061A_clockchip_methods.DAC_stateSize);
    if (*dvwp) {
 I2061A_init_clock(((unsigned int) *dvwp) | (((int) clknum) << 21));
    }
}

variable_types:
clknum
unsigned int


original_code:
static void apm_getmodeinfo(int mode, vga_modeinfo *modeinfo)
{
    if(modeinfo->colors==16)return;
    modeinfo->maxpixels = apm_memory*1024/modeinfo->bytesperpixel;
    modeinfo->maxlogicalwidth = 4088;
    modeinfo->startaddressrange = apm_memory * 1024 - 1;
    modeinfo->haveblit = 0;
    modeinfo->flags &= ~1;
 if (modeinfo->bytesperpixel >= 1) {
  modeinfo->flags |= 16;
    }
}

canonical_original_code:
static void apm_getmodeinfo(int mode, struct vga_modeinfo *modeinfo)
{
    if(modeinfo->colors==16)return;
    modeinfo->maxpixels = apm_memory*1024/modeinfo->bytesperpixel;
    modeinfo->maxlogicalwidth = 4088;
    modeinfo->startaddressrange = apm_memory * 1024 - 1;
    modeinfo->haveblit = 0;
    modeinfo->flags &= ~1;
 if (modeinfo->bytesperpixel >= 1) {
  modeinfo->flags |= 16;
    }
}

variable_types: mode int

variable_types: struct vga_modeinfo { int width; int height; int bytesperpixel; int colors; int linewidth; int maxlogicalwidth; int startaddressrange; int maxpixels; int haveblit; int flags; int chiptype; int memory; int linewidth_unit; char *linear_aperture; int aperture_size; void (*set_aperture_page)(int page); void *extensions; } *


decompiled_code:
__int64 apm_unlock()
{
void (__fastcall *v0)(__int64, _QWORD); // rbx
char v1; // al

v0 = (void (__fastcall *)(__int64, _QWORD))_svgalib_outcrtc;
v1 = _svgalib_incrtc(17LL);
v0(17LL, v1 & 0x7F);
return _svgalib_outseq(16LL, 18LL);
}

canonical_decompiled_code:
__int64 func1()
{
void (__fastcall *v0)(__int64, _QWORD); // rbx
char v1; // al

v0 = (void (__fastcall *)(__int64, _QWORD))_svgalib_outcrtc;
v1 = _svgalib_incrtc(17LL);
v0(17LL, v1 & 0x7F);
return _svgalib_outseq(16LL, 18LL);
}

original_code:
static void apm_unlock(void)
{
    __svgalib_outcrtc(0x11,__svgalib_incrtc(0x11)&0x7f);
    __svgalib_outseq(0x10, 0x12);
}

canonical_original_code:
static void apm_unlock()
{
    __svgalib_outcrtc(0x11,__svgalib_incrtc(0x11)&0x7f);
    __svgalib_outseq(0x10, 0x12);
}


decompiled_code:
void apm_lock()
{
;
}

canonical_decompiled_code:
void func2()
{
;
}

original_code:
static void apm_lock(void)
{
}

canonical_original_code:
static void apm_lock()
{
}


decompiled_code:
void apm_setrdpage()
{
;
}

original_code:
static void apm_setrdpage(int page)
{
}

canonical_original_code:
static void apm_setrdpage(int page)
{
}

variable_types: page int


decompiled_code:
void apm_setwrpage()
{
;
}

canonical_decompiled_code:
void func4()
{
;
}

original_code:
static void apm_setwrpage(int page)
{
}

canonical_original_code:
static void apm_setwrpage(int page)
{
}


decompiled_code:
void apm_setlogicalwidth()
{
;
}

canonical_decompiled_code:
void func5()
{
;
}

original_code:
static void apm_setlogicalwidth(int width)
{
}

variable_types: width int


decompiled_code:
__int64 __fastcall apm_match_programmable_clock(unsigned int a1)
{
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned int a1)
{
return a1;
}

original_code:
static int apm_match_programmable_clock(int clock)
{
return clock ;
}

variable_types: clock int


decompiled_code:
__int64 __fastcall apm_map_clock(__int64 a1, unsigned int a2)
{
return a2;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, unsigned int a2)
{
return a2;
}

original_code:
static int apm_map_clock(int bpp, int clock)
{
return clock ;
}

variable_types: bpp int


decompiled_code:
__int64 __fastcall apm_map_horizontal_crtc(__int64 a1, __int64 a2, unsigned int a3)
{
return a3;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, __int64 a2, unsigned int a3)
{
return a3;
}

original_code:
static int apm_map_horizontal_crtc(int bpp, int pixelclock, int htiming)
{
return htiming;
}

variable_types: htiming int


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

result = 0LL;
if ( (unsigned int)(a1 - 3) > 1 )
{
if ( a1 )
{
result = (unsigned int)-((unsigned int)(a1 - 1) > 1);
}
else
{
_svgalib_outseq(29LL, 100LL);
result = (unsigned int)_svgalib_port_in((unsigned int)(apm_xbase + 3)) << 24;
}
}
return result;
}

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

result = 0LL;
if ( (unsigned int)(a1 - 3) > 1 )
{
if ( a1 )
{
result = (unsigned int)-((unsigned int)(a1 - 1) > 1);
}
else
{
_svgalib_outseq(29LL, 100LL);
result = (unsigned int)_svgalib_port_in((unsigned int)(apm_xbase + 3)) << 24;
}
}
return result;
}

original_code:
static int apm_linear(int op, int param)
{
 if (op==LINEAR_ENABLE) { return 0;}
 if (op==LINEAR_DISABLE){ return 0;}
 if (op==LINEAR_QUERY_BASE) {
  __svgalib_outseq(0x1d,0x193>>2);
  return __svgalib_port_in(apm_xbase+3)<<24;
 }
 if (op == LINEAR_QUERY_RANGE || op == LINEAR_QUERY_GRANULARITY)
  return 0;
 return -1;
}

variable_types: op int

variable_types: param int


inXR

decompiled_code:
__int64 __fastcall inXR(int a1)
{
_svgalib_outseq(29LL, (unsigned int)(a1 >> 2));
return _svgalib_port_in((unsigned int)apm_xbase);
}

canonical_decompiled_code:
__int64 __fastcall func11(int a1)
{
_svgalib_outseq(29LL, (unsigned int)(a1 >> 2));
return _svgalib_port_in((unsigned int)apm_xbase);
}

original_code:
static unsigned char inXR(int index)
{
    __svgalib_outseq(0x1d,index>>2);
    return __svgalib_port_in(apm_xbase);
}

variable_types: index int

return_type: unsigned char


decompiled_code:
__int64 __fastcall apm_setpage(__int16 a1)
{
return outwXR(192, 16 * a1);
}

canonical_decompiled_code:
__int64 __fastcall func13(__int16 a1)
{
return func12(192, 16 * a1);
}

original_code:
static void apm_setpage(int page)
{
    outwXR(0xc0,page << 4);
}


decompiled_code:
__int64 __fastcall apm_setdisplaystart(int a1)
{
char v1; // al

_svgalib_port_outw(a1 & 0xFF00 | 0xCu, 980LL);
_svgalib_port_outw((unsigned __int16)((_WORD)a1 << 8) | 0xDu, 980LL);
_svgalib_port_out(28LL, 980LL);
v1 = _svgalib_port_in(981LL);
return _svgalib_outcrtc(28LL, (a1 >> 16) & 0xF | v1 & 0xF0u);
}

canonical_decompiled_code:
__int64 __fastcall func14(int a1)
{
char v1; // al

_svgalib_port_outw(a1 & 0xFF00 | 0xCu, 980LL);
_svgalib_port_outw((unsigned __int16)((_WORD)a1 << 8) | 0xDu, 980LL);
_svgalib_port_out(28LL, 980LL);
v1 = _svgalib_port_in(981LL);
return _svgalib_outcrtc(28LL, (a1 >> 16) & 0xF | v1 & 0xF0u);
}

original_code:
static void apm_setdisplaystart(int address)
{ int i;
  __svgalib_port_outw((address & 0x00FF00) | 0x0C, 0x3d4);
  __svgalib_port_outw(((address & 0x00FF) << 8) | 0x0D, 0x3d4);
   __svgalib_port_out(0x1c, 0x3d4);
   i=(__svgalib_port_in(0x3d5)&0xf0)|((address & 0x0f0000) >> 16);
   __svgalib_outcrtc(0x1c,i);
}

variable_types: address int


decompiled_code:
__int64 __fastcall apm_modeavailable(int a1)
{
__int64 result; // rax
void *v2; // rbx
void *v3; // rbp

if ( a1 == 32 || a1 <= 9 || a1 == 145 )
return _svgalib_vga_driverspecs[10]();
result = 0LL;
if ( apm_memory << 10 >= _svgalib_infotable[5 * a1 + 3] * _svgalib_infotable[5 * a1 + 1] )
{
v2 = (void *)_svgalib_createModeInfoStructureForSvgalibMode();
v3 = malloc(0x50uLL);
if ( (unsigned int)_svgalib_getmodetiming(v3, v2, cardspecs) )
{
free(v3);
free(v2);
result = 0LL;
}
else
{
free(v3);
free(v2);
result = 2LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func16(int a1)
{
__int64 result; // rax
void *v2; // rbx
void *v3; // rbp

if ( a1 == 32 || a1 <= 9 || a1 == 145 )
return _svgalib_vga_driverspecs[10]();
result = 0LL;
if ( apm_memory << 10 >= _svgalib_infotable[5 * a1 + 3] * _svgalib_infotable[5 * a1 + 1] )
{
v2 = (void *)_svgalib_createModeInfoStructureForSvgalibMode();
v3 = malloc(0x50uLL);
if ( (unsigned int)_svgalib_getmodetiming(v3, v2, cardspecs) )
{
free(v3);
free(v2);
result = 0LL;
}
else
{
free(v3);
free(v2);
result = 2LL;
}
}
return result;
}

original_code:
static int apm_modeavailable(int mode)
{
    struct vgainfo *info;
    ModeTiming *modetiming;
    ModeInfo *modeinfo;
    if (( ((mode) < 10) || ((mode) == 32) || ( ((mode) >= 145) && ((mode) <= 145) ) ))
 return __svgalib_vga_driverspecs.modeavailable(mode);
    info = &__svgalib_infotable[mode];
    if (apm_memory * 1024 < info->ydim * info->xbytes)
 return 0;
    modeinfo = __svgalib_createModeInfoStructureForSvgalibMode(mode);
    if((modeinfo->bitsPerPixel==24)&&(apm_chiptype==AT24)) {
 free(modeinfo);
 return 0;
    }
    modetiming = malloc(sizeof(ModeTiming));
    if (__svgalib_getmodetiming(modetiming, modeinfo, cardspecs)) {
 free(modetiming);
 free(modeinfo);
 return 0;
    }
    free(modetiming);
    free(modeinfo);
    return 2;
}

canonical_original_code:
static int apm_modeavailable(int mode)
{
    struct vgainfo *info;
    struct ModeTiming *modetiming;
    struct ModeInfo *modeinfo;
    if (( ((mode) < 10) || ((mode) == 32) || ( ((mode) >= 145) && ((mode) <= 145) ) ))
 return __svgalib_vga_driverspecs.modeavailable(mode);
    info = &__svgalib_infotable[mode];
    if (apm_memory * 1024 < info->ydim * info->xbytes)
 return 0;
    modeinfo = __svgalib_createModeInfoStructureForSvgalibMode(mode);
    if((modeinfo->bitsPerPixel==24)&&(apm_chiptype==AT24)) {
 free(modeinfo);
 return 0;
    }
    modetiming = malloc(sizeof(ModeTiming));
    if (__svgalib_getmodetiming(modetiming, modeinfo, cardspecs)) {
 free(modetiming);
 free(modeinfo);
 return 0;
    }
    free(modetiming);
    free(modeinfo);
    return 2;
}

variable_types: struct vgainfo { int xdim; int ydim; int colors; int xbytes; int bytesperpixel; } *

variable_types: struct ModeTiming { int pixelClock; int HDisplay; int HSyncStart; int HSyncEnd; int HTotal; int VDisplay; int VSyncStart; int VSyncEnd; int VTotal; int flags; int programmedClock; int selectedClockNo; int CrtcHDisplay; int CrtcHSyncStart; int CrtcHSyncEnd; int CrtcHTotal; int CrtcVDisplay; int CrtcVSyncStart; int CrtcVSyncEnd; int CrtcVTotal; } *

variable_types: struct ModeInfo { short width; short height; char bytesPerPixel; char bitsPerPixel; char colorBits; char __padding1; char redWeight; char greenWeight; char blueWeight; char __padding2; char redOffset; char blueOffset; char greenOffset; char __padding3; unsigned int redMask; unsigned int blueMask; unsigned int greenMask; int lineWidth; short realWidth; short realHeight; int flags; } *


decompiled_code:
__int64 apm_init()
{
int v0; // edx
__int64 result; // rax
__int64 v2; // rcx
char v3; // al
int v4; // eax
int v5; // ebx
_DWORD *v6; // rax
int v7; // edx
__int64 v8; // [rsp+0h] [rbp-128h] BYREF
int v9; // [rsp+10h] [rbp-118h]
__int64 v10[4]; // [rsp+108h] [rbp-20h] BYREF

v10[0] = 0x20202020202020LL;
apm_unlock();
v0 = _svgalib_pci_find_vendor_vga_pos(4418LL, &v8);
result = 1LL;
if ( v0 )
{
if ( WORD1(v8) == 25636 )
{
strcpy((char *)v10, "AT24");
}
else
{
if ( WORD1(v8) != 25661 )
return 1LL;
v10[0] = 0x44332F35325441LL;
}
_svgalib_linear_mem_base = v9 & 0xFFFFFF00;
_svgalib_mmio_base = _svgalib_linear_mem_base + 16760832LL;
_svgalib_mmio_size = 0x4000LL;
map_mmio();
v2 = _svgalib_mmio_pointer;
v3 = *(_BYTE *)(_svgalib_mmio_pointer + 11481LL);
*(_BYTE *)(_svgalib_mmio_pointer + 11483LL) = *(_BYTE *)(_svgalib_mmio_pointer + 11483LL) & 0xF4 | 0xA;
*(_BYTE *)(v2 + 11481) = v3 & 0xCF | 0x20;
_svgalib_vgammbase = 12288;
_svgalib_mm_io_mapio();
v4 = _svgalib_inseq(32LL);
apm_memory = (v4 << 6) - 34;
if ( _svgalib_driver_report )
fprintf(stderr, "Using Alliance driver, %.7s, %iKB.\n", (const char *)v10, (unsigned int)((v4 << 6) - 34));
v5 = _svgalib_inseq(31LL);
apm_xbase = _svgalib_inseq(30LL) + (v5 << 8);
v6 = malloc(0x48uLL);
cardspecs = (__int64)v6;
v7 = apm_memory;
*v6 = apm_memory;
v6[2] = 175500;
v6[3] = 144000;
v6[4] = 75000;
v6[5] = 94500;
v6[6] = 3;
v6[10] = 4088;
v6[1] = 0;
v6[7] = 0;
*((_QWORD *)v6 + 4) = 0LL;
*((_QWORD *)v6 + 6) = apm_map_clock;
*((_QWORD *)v6 + 8) = apm_map_horizontal_crtc;
*((_QWORD *)v6 + 7) = apm_match_programmable_clock;
_svgalib_driverspecs = _svgalib_apm_driverspecs;
_svgalib_banked_mem_base = 655360LL;
_svgalib_banked_mem_size = 0x10000LL;
_svgalib_linear_mem_size = v7 << 10;
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 func17()
{
int v0; // edx
__int64 result; // rax
__int64 v2; // rcx
char v3; // al
int v4; // eax
int v5; // ebx
_DWORD *v6; // rax
int v7; // edx
__int64 v8; // [rsp+0h] [rbp-128h] BYREF
int v9; // [rsp+10h] [rbp-118h]
__int64 v10[4]; // [rsp+108h] [rbp-20h] BYREF

v10[0] = 0x20202020202020LL;
func1();
v0 = _svgalib_pci_find_vendor_vga_pos(4418LL, &v8);
result = 1LL;
if ( v0 )
{
if ( WORD1(v8) == 25636 )
{
strcpy((char *)v10, "AT24");
}
else
{
if ( WORD1(v8) != 25661 )
return 1LL;
v10[0] = 0x44332F35325441LL;
}
_svgalib_linear_mem_base = v9 & 0xFFFFFF00;
_svgalib_mmio_base = _svgalib_linear_mem_base + 16760832LL;
_svgalib_mmio_size = 0x4000LL;
map_mmio();
v2 = _svgalib_mmio_pointer;
v3 = *(_BYTE *)(_svgalib_mmio_pointer + 11481LL);
*(_BYTE *)(_svgalib_mmio_pointer + 11483LL) = *(_BYTE *)(_svgalib_mmio_pointer + 11483LL) & 0xF4 | 0xA;
*(_BYTE *)(v2 + 11481) = v3 & 0xCF | 0x20;
_svgalib_vgammbase = 12288;
_svgalib_mm_io_mapio();
v4 = _svgalib_inseq(32LL);
apm_memory = (v4 << 6) - 34;
if ( _svgalib_driver_report )
fprintf(stderr, "Using Alliance driver, %.7s, %iKB.\n", (const char *)v10, (unsigned int)((v4 << 6) - 34));
v5 = _svgalib_inseq(31LL);
apm_xbase = _svgalib_inseq(30LL) + (v5 << 8);
v6 = malloc(0x48uLL);
cardspecs = (__int64)v6;
v7 = apm_memory;
*v6 = apm_memory;
v6[2] = 175500;
v6[3] = 144000;
v6[4] = 75000;
v6[5] = 94500;
v6[6] = 3;
v6[10] = 4088;
v6[1] = 0;
v6[7] = 0;
*((_QWORD *)v6 + 4) = 0LL;
*((_QWORD *)v6 + 6) = apm_map_clock;
*((_QWORD *)v6 + 8) = apm_map_horizontal_crtc;
*((_QWORD *)v6 + 7) = apm_match_programmable_clock;
_svgalib_driverspecs = _svgalib_apm_driverspecs;
_svgalib_banked_mem_base = 655360LL;
_svgalib_banked_mem_size = 0x10000LL;
_svgalib_linear_mem_size = v7 << 10;
result = 0LL;
}
return result;
}

original_code:
static int apm_init(int force, int par1, int par2)
{
    char idstring[] = "       ";
    int found=0;
    unsigned int buf[64];
    int db, d9;
    apm_unlock();
    found=__svgalib_pci_find_vendor_vga_pos(0x1142,buf);
    if(!found)return 1;
    switch((buf[0]>>16)&0xffff) {
        case 0x6424: strcpy(idstring,"AT24"); break;
        case 0x643d: strcpy(idstring,"AT25/3D"); break;
        default: return 1; break;
    };
    __svgalib_linear_mem_base=buf[4]&0xffffff00;
    __svgalib_mmio_base=__svgalib_linear_mem_base+0xffc000;
    __svgalib_mmio_size=0x4000;
    map_mmio();
    db=(*(volatile uint8_t *) (__svgalib_mmio_pointer+(0x2cdb)));
    d9=(*(volatile uint8_t *) (__svgalib_mmio_pointer+(0x2cd9)));
    (*(volatile uint8_t *) (__svgalib_mmio_pointer+(0x2cdb)) = ((db & 0xf4) | 0x0a));
    (*(volatile uint8_t *) (__svgalib_mmio_pointer+(0x2cd9)) = ((d9 & 0xcf) | 0x20));
    __svgalib_vgammbase = 0x3000;
    __svgalib_mm_io_mapio();
    apm_memory=__svgalib_inseq(0x20)*64-34;
    if (__svgalib_driver_report) {
 fprintf(stderr,"Using Alliance driver, %.7s, %iKB.\n",idstring, apm_memory);
    }
    apm_xbase= (__svgalib_inseq(0x1f) << 8 ) + __svgalib_inseq(0x1e);
    cardspecs = malloc(sizeof(CardSpecs));
    cardspecs->videoMemory = apm_memory;
    cardspecs->maxPixelClock4bpp = 75000;
    cardspecs->maxPixelClock8bpp = 175500;
    if(apm_chiptype==AT24)cardspecs->maxPixelClock8bpp = 160000;
    cardspecs->maxPixelClock16bpp = 144000;
    cardspecs->maxPixelClock24bpp = 75000;
    cardspecs->maxPixelClock32bpp = 94500;
    cardspecs->flags = 0x1 | 0x2;
    cardspecs->maxHorizontalCrtc = 4088;
    cardspecs->maxPixelClock4bpp = 0;
    cardspecs->nClocks =0;
    cardspecs->clocks = ((void *)0);
    cardspecs->mapClock = apm_map_clock;
    cardspecs->mapHorizontalCrtc = apm_map_horizontal_crtc;
    cardspecs->matchProgrammableClock=apm_match_programmable_clock;
    __svgalib_driverspecs = &__svgalib_apm_driverspecs;
    __svgalib_banked_mem_base=0xa0000;
    __svgalib_banked_mem_size=0x10000;
    __svgalib_linear_mem_size=apm_memory*1024;
    return 0;
}

canonical_original_code:
static int apm_init(int force, int par1, int par2)
{
    char idstring[] = "       ";
    int found = 0;
    unsigned int buf[64];
    int db; int d9;
    apm_unlock();
    found=__svgalib_pci_find_vendor_vga_pos(0x1142,buf);
    if(!found)return 1;
    switch((buf[0]>>16)&0xffff) {
        case 0x6424: strcpy(idstring,"AT24"); break;
        case 0x643d: strcpy(idstring,"AT25/3D"); break;
        default: return 1; break;
    };
    __svgalib_linear_mem_base=buf[4]&0xffffff00;
    __svgalib_mmio_base=__svgalib_linear_mem_base+0xffc000;
    __svgalib_mmio_size=0x4000;
    map_mmio();
    db=(*(unsigned char *) (__svgalib_mmio_pointer+(0x2cdb)));
    d9=(*(unsigned char *) (__svgalib_mmio_pointer+(0x2cd9)));
    (*(unsigned char *) (__svgalib_mmio_pointer+(0x2cdb)) = ((db & 0xf4) | 0x0a));
    (*(unsigned char *) (__svgalib_mmio_pointer+(0x2cd9)) = ((d9 & 0xcf) | 0x20));
    __svgalib_vgammbase = 0x3000;
    __svgalib_mm_io_mapio();
    apm_memory=__svgalib_inseq(0x20)*64-34;
    if (__svgalib_driver_report) {
 fprintf(stderr,"Using Alliance driver, %.7s, %iKB.\n",idstring, apm_memory);
    }
    apm_xbase= (__svgalib_inseq(0x1f) << 8 ) + __svgalib_inseq(0x1e);
    cardspecs = malloc(sizeof(CardSpecs));
    cardspecs->videoMemory = apm_memory;
    cardspecs->maxPixelClock4bpp = 75000;
    cardspecs->maxPixelClock8bpp = 175500;
    if(apm_chiptype==AT24)cardspecs->maxPixelClock8bpp = 160000;
    cardspecs->maxPixelClock16bpp = 144000;
    cardspecs->maxPixelClock24bpp = 75000;
    cardspecs->maxPixelClock32bpp = 94500;
    cardspecs->flags = 0x1 | 0x2;
    cardspecs->maxHorizontalCrtc = 4088;
    cardspecs->maxPixelClock4bpp = 0;
    cardspecs->nClocks =0;
    cardspecs->clocks = ((void *)0);
    cardspecs->mapClock = apm_map_clock;
    cardspecs->mapHorizontalCrtc = apm_map_horizontal_crtc;
    cardspecs->matchProgrammableClock=apm_match_programmable_clock;
    __svgalib_driverspecs = &__svgalib_apm_driverspecs;
    __svgalib_banked_mem_base=0xa0000;
    __svgalib_banked_mem_size=0x10000;
    __svgalib_linear_mem_size=apm_memory*1024;
    return 0;
}

variable_types: force int

variable_types:
idstring
char []

variable_types: found int

variable_types:
buf
unsigned int [64]

variable_types: db int


decompiled_code:
__int64 apm_test()
{
__int64 result; // rax
__int64 v1; // [rsp+0h] [rbp-108h] BYREF

result = _svgalib_pci_find_vendor_vga_pos(4418LL, &v1);
if ( (_DWORD)result )
{
if ( WORD1(v1) == 25636 || (result = 0LL, WORD1(v1) == 25661) )
{
apm_init(0LL, 0LL, 0LL);
result = 1LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 func18()
{
__int64 result; // rax
__int64 v1; // [rsp+0h] [rbp-108h] BYREF

result = _svgalib_pci_find_vendor_vga_pos(4418LL, &v1);
if ( (_DWORD)result )
{
if ( WORD1(v1) == 25636 || (result = 0LL, WORD1(v1) == 25661) )
{
func17(0LL, 0LL, 0LL);
result = 1LL;
}
}
return result;
}

original_code:
static int apm_test(void)
{
   int found=0;
   unsigned int buf[64];
   found=__svgalib_pci_find_vendor_vga_pos(0x1142,buf);
   if(!found)return 0;
   if ((((buf[0]>>16)&0xffff)!=0x6424)&&
       (((buf[0]>>16)&0xffff)!=0x643d)
       ) return 0;
   apm_init(0,0,0);
   return 1;
}

canonical_original_code:
static int apm_test()
{
   int found = 0;
   unsigned int buf[64];
   found=__svgalib_pci_find_vendor_vga_pos(0x1142,buf);
   if(!found)return 0;
   if ((((buf[0]>>16)&0xffff)!=0x6424)&&
       (((buf[0]>>16)&0xffff)!=0x643d)
       ) return 0;
   apm_init(0,0,0);
   return 1;
}


outlXR

decompiled_code:
__int64 __fastcall outlXR(int a1, unsigned int a2)
{
_svgalib_outseq(29LL, (unsigned int)(a1 >> 2));
return _svgalib_port_outl(a2, (unsigned int)apm_xbase);
}

canonical_decompiled_code:
__int64 __fastcall func19(int a1, unsigned int a2)
{
_svgalib_outseq(29LL, (unsigned int)(a1 >> 2));
return _svgalib_port_outl(a2, (unsigned int)apm_xbase);
}

original_code:
static void outlXR(int index,unsigned int d)
{
    __svgalib_outseq(0x1d,index>>2);
    __svgalib_port_outl(d, apm_xbase);
}

canonical_original_code:
static void outlXR(int index, unsigned int d)
{
    __svgalib_outseq(0x1d,index>>2);
    __svgalib_port_outl(d, apm_xbase);
}

variable_types:
d
unsigned int


decompiled_code:
__int64 __fastcall apm_setregs(unsigned __int8 *a1)
{
int v1; // ebp
unsigned int v2; // esi
unsigned int v3; // esi

apm_unlock();
apm_setpage(0);
_svgalib_outseq(27LL, a1[60]);
_svgalib_outseq(28LL, a1[61]);
_svgalib_outcrtc(25LL, a1[62]);
_svgalib_outcrtc(26LL, a1[63]);
_svgalib_outcrtc(27LL, a1[64]);
_svgalib_outcrtc(28LL, a1[65]);
_svgalib_outcrtc(29LL, a1[66]);
_svgalib_outcrtc(30LL, a1[67]);
v1 = (a1[75] << 8) + a1[74] + (a1[76] << 16) + (a1[77] << 24);
v2 = v1;
LOBYTE(v2) = a1[74] & 0x7F;
outlXR(236, v2);
v3 = v1;
LOBYTE(v3) = v1 | 0x80;
outlXR(236, v3);
return outXR(128, a1[68]);
}

canonical_decompiled_code:
__int64 __fastcall func20(unsigned __int8 *a1)
{
int v1; // ebp
unsigned int v2; // esi
unsigned int v3; // esi

func1();
func13(0);
_svgalib_outseq(27LL, a1[60]);
_svgalib_outseq(28LL, a1[61]);
_svgalib_outcrtc(25LL, a1[62]);
_svgalib_outcrtc(26LL, a1[63]);
_svgalib_outcrtc(27LL, a1[64]);
_svgalib_outcrtc(28LL, a1[65]);
_svgalib_outcrtc(29LL, a1[66]);
_svgalib_outcrtc(30LL, a1[67]);
v1 = (a1[75] << 8) + a1[74] + (a1[76] << 16) + (a1[77] << 24);
v2 = v1;
LOBYTE(v2) = a1[74] & 0x7F;
func19(236, v2);
v3 = v1;
LOBYTE(v3) = v1 | 0x80;
func19(236, v3);
return func15(128, a1[68]);
}

variable_types:
regs
unsigned char []

variable_types: mode int

variable_types:
k
unsigned int


inlXR

decompiled_code:
__int64 __fastcall inlXR(int a1)
{
_svgalib_outseq(29LL, (unsigned int)(a1 >> 2));
return _svgalib_port_inl((unsigned int)apm_xbase);
}

canonical_decompiled_code:
__int64 __fastcall func21(int a1)
{
_svgalib_outseq(29LL, (unsigned int)(a1 >> 2));
return _svgalib_port_inl((unsigned int)apm_xbase);
}

original_code:
static unsigned int inlXR(int index)
{
    __svgalib_outseq(0x1d,index>>2);
    return __svgalib_port_inl(apm_xbase);
}

return_type: unsigned int


original_code:
static int apm_saveregs(unsigned char regs[])
{
 unsigned int k;
 unsigned int *p;
 unsigned short *q;
    apm_unlock();
    regs[(60 +2)] = __svgalib_incrtc(0x19);
    regs[(60 +3)] = __svgalib_incrtc(0x1A);
    regs[(60 +4)] = __svgalib_incrtc(0x1B);
    regs[(60 +5)] = __svgalib_incrtc(0x1C);
    regs[(60 +6)] = __svgalib_incrtc(0x1D);
    regs[(60 +7)] = __svgalib_incrtc(0x1E);
    regs[(60 +0)] = __svgalib_inseq(0x1B);
    regs[(60 +1)] = __svgalib_inseq(0x1C);
   regs[(60 +8)] = inXR(0x80) ;
   regs[(60 +9)] = inXR(0xc0) ;
   k = inlXR(0xe8) ;
   regs[(60 +10)] = k&0xff;
   regs[(60 +11)] = (k >> 8)&0xff;
   regs[(60 +12)] = (k >> 16) & 0xff ;
   regs[(60 +13)] = ( k >> 24 ) & 0xff ;
   k = inlXR(0xec) ;
   regs[(60 +14)] = k&0xff;
   regs[(60 +15)] = (k >> 8)&0xff;
   regs[(60 +16)] = (k >> 16) & 0xff ;
   regs[(60 +17)] = ( k >> 24 ) & 0xff ;
   p=(unsigned int *) &regs[(60 +18)];
   *p = inlXR(0xf0);
   p=(unsigned int *) &regs[(60 +22)];
   *p = inlXR(0xf4);
   p=(unsigned int *) &regs[(60 +26)];
   *p = inlXR(0x140);
   q=(unsigned short *) &regs[(60 +30)];
   *q = inlXR(0x144);
   p=(unsigned int *) &regs[(60 +32)];
   *p = inXR(0x148);
   q=(unsigned short *) &regs[(60 +36)];
   *q = inXR(0x14c);
    return (60 + 38) - 60;
}

variable_types:
p
unsigned int *

variable_types:
q
unsigned short *


decompiled_code:
__int64 __fastcall apm_setmode(unsigned int a1)
{
unsigned int v1; // ebp
void *v2; // r12
void *v3; // r13
unsigned __int8 *v4; // r14

if ( a1 == 32 || (int)a1 <= 9 || a1 == 145 )
return ((unsigned int (__fastcall *)(_QWORD))_svgalib_vga_driverspecs[9])(a1);
v1 = 1;
if ( (unsigned int)apm_modeavailable(a1) )
{
v2 = (void *)_svgalib_createModeInfoStructureForSvgalibMode(a1);
v3 = malloc(0x50uLL);
v1 = _svgalib_getmodetiming(v3, v2, cardspecs);
if ( v1 )
{
free(v3);
free(v2);
v1 = 1;
}
else
{
v4 = (unsigned __int8 *)malloc(0x62uLL);
apm_initializemode((__int64)v4, (__int64)v3, (__int64)v2);
free(v3);
_svgalib_setregs(v4);
apm_setregs(v4);
free(v4);
free(v2);
}
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func24(unsigned int a1)
{
unsigned int v1; // ebp
void *v2; // r12
void *v3; // r13
unsigned __int8 *v4; // r14

if ( a1 == 32 || (int)a1 <= 9 || a1 == 145 )
return ((unsigned int (__fastcall *)(_QWORD))_svgalib_vga_driverspecs[9])(a1);
v1 = 1;
if ( (unsigned int)func16(a1) )
{
v2 = (void *)_svgalib_createModeInfoStructureForSvgalibMode(a1);
v3 = malloc(0x50uLL);
v1 = _svgalib_getmodetiming(v3, v2, cardspecs);
if ( v1 )
{
free(v3);
free(v2);
v1 = 1;
}
else
{
v4 = (unsigned __int8 *)malloc(0x62uLL);
func23((__int64)v4, (__int64)v3, (__int64)v2);
free(v3);
_svgalib_setregs(v4);
func20(v4);
free(v4);
free(v2);
}
}
return v1;
}

original_code:
static int apm_setmode(int mode, int prv_mode)
{
    unsigned char *moderegs;
    ModeTiming *modetiming;
    ModeInfo *modeinfo;
    if (( ((mode) < 10) || ((mode) == 32) || ( ((mode) >= 145) && ((mode) <= 145) ) )) {
 return __svgalib_vga_driverspecs.setmode(mode, prv_mode);
    }
    if (!apm_modeavailable(mode))
 return 1;
    modeinfo = __svgalib_createModeInfoStructureForSvgalibMode(mode);
    modetiming = malloc(sizeof(ModeTiming));
    if (__svgalib_getmodetiming(modetiming, modeinfo, cardspecs)) {
 free(modetiming);
 free(modeinfo);
 return 1;
    }
    moderegs = malloc((60 + 38));
    apm_initializemode(moderegs, modetiming, modeinfo, mode);
    free(modetiming);
    __svgalib_setregs(moderegs);
    apm_setregs(moderegs, mode);
    free(moderegs);
    free(modeinfo);
    return 0;
}

canonical_original_code:
static int apm_setmode(int mode, int prv_mode)
{
    unsigned char *moderegs;
    struct ModeTiming *modetiming;
    struct ModeInfo *modeinfo;
    if (( ((mode) < 10) || ((mode) == 32) || ( ((mode) >= 145) && ((mode) <= 145) ) )) {
 return __svgalib_vga_driverspecs.setmode(mode, prv_mode);
    }
    if (!apm_modeavailable(mode))
 return 1;
    modeinfo = __svgalib_createModeInfoStructureForSvgalibMode(mode);
    modetiming = malloc(sizeof(ModeTiming));
    if (__svgalib_getmodetiming(modetiming, modeinfo, cardspecs)) {
 free(modetiming);
 free(modeinfo);
 return 1;
    }
    moderegs = malloc((60 + 38));
    apm_initializemode(moderegs, modetiming, modeinfo, mode);
    free(modetiming);
    __svgalib_setregs(moderegs);
    apm_setregs(moderegs, mode);
    free(moderegs);
    free(modeinfo);
    return 0;
}

variable_types:
moderegs
unsigned char *

user_defined_types: struct ModeTiming { int pixelClock; int HDisplay; int HSyncStart; int HSyncEnd; int HTotal; int VDisplay; int VSyncStart; int VSyncEnd; int VTotal; int flags; int programmedClock; int selectedClockNo; int CrtcHDisplay; int CrtcHSyncStart; int CrtcHSyncEnd; int CrtcHTotal; int CrtcVDisplay; int CrtcVSyncStart; int CrtcVSyncEnd; int CrtcVTotal; }

user_defined_types: struct ModeInfo { short width; short height; char bytesPerPixel; char bitsPerPixel; char colorBits; char __padding1; char redWeight; char greenWeight; char blueWeight; char __padding2; char redOffset; char blueOffset; char greenOffset; char __padding3; unsigned int redMask; unsigned int blueMask; unsigned int greenMask; int lineWidth; short realWidth; short realHeight; int flags; }


variable_types:
clock
unsigned int

variable_types: n int

variable_types: m int

variable_types: l int

variable_types: f int

variable_types: fvco int

variable_types: fout int

variable_types: fmax int

variable_types: fref int

variable_types: c int


decompiled_code:
__int64 __fastcall IBMRGB52x_match_programmable_clock(int a1)
{
int v2; // [rsp+4h] [rbp-Ch] BYREF
int v3; // [rsp+8h] [rbp-8h] BYREF
int v4; // [rsp+Ch] [rbp-4h] BYREF

IBMRGBSetClock(a1, 2LL, IBMRGB52x_dacspeed, 16000, &v4, &v3, &v2);
return (unsigned int)(int)(((double)v4 + 65.0) / (double)v3 / (double)(8 >> v2) * 16000.0);
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
int v2; // [rsp+4h] [rbp-Ch] BYREF
int v3; // [rsp+8h] [rbp-8h] BYREF
int v4; // [rsp+Ch] [rbp-4h] BYREF

func0(a1, 2LL, IBMRGB52x_dacspeed, 16000, &v4, &v3, &v2);
return (unsigned int)(int)(((double)v4 + 65.0) / (double)v3 / (double)(8 >> v2) * 16000.0);
}

original_code:
static int IBMRGB52x_match_programmable_clock(int desiredclock)
{
    int m, n, df;
    IBMRGBSetClock(desiredclock, IBMRGB52x_clk, IBMRGB52x_dacspeed,
     IBMRGB52x_fref, &m, &n, &df);
    return ((m + 65.0) / n) / (8 >> df) * IBMRGB52x_fref;
}

canonical_original_code:
static int IBMRGB52x_match_programmable_clock(int desiredclock)
{
    int m; int n; int df;
    IBMRGBSetClock(desiredclock, IBMRGB52x_clk, IBMRGB52x_dacspeed,
     IBMRGB52x_fref, &m, &n, &df);
    return ((m + 65.0) / n) / (8 >> df) * IBMRGB52x_fref;
}

variable_types: desiredclock int

variable_types: df int


decompiled_code:
__int64 __fastcall IBMRGB52x_map_clock(__int64 a1, unsigned int a2)
{
return a2;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, unsigned int a2)
{
return a2;
}

original_code:
static int IBMRGB52x_map_clock(int bpp, int pixelclock)
{
    return pixelclock;
}

variable_types: bpp int

variable_types: pixelclock int


decompiled_code:
__int64 __fastcall IBMRGB52x_map_horizontal_crtc(__int64 a1, __int64 a2, unsigned int a3)
{
return a3;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2, unsigned int a3)
{
return a3;
}

original_code:
static int IBMRGB52x_map_horizontal_crtc(int bpp, int pixelclock, int htiming)
{
    return htiming;
}

variable_types: htiming int


decompiled_code:
__int64 __fastcall IBMRGB52x_restorestate(__int64 a1)
{
unsigned __int8 v1; // r12
__int64 i; // rbx

_svgalib_port_out(85LL, 980LL);
v1 = _svgalib_port_in(981LL) & 0xFC;
_svgalib_port_out(v1 | 1u, 981LL);
for ( i = 0LL; i != 256; ++i )
{
_svgalib_port_out((unsigned int)i, 968LL);
_svgalib_port_out(*(unsigned __int8 *)(a1 + i), 966LL);
}
_svgalib_outcrtc(34LL, *(unsigned __int8 *)(a1 + 256));
return _svgalib_outcrtc(85LL, v1);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
unsigned __int8 v1; // r12
__int64 i; // rbx

_svgalib_port_out(85LL, 980LL);
v1 = _svgalib_port_in(981LL) & 0xFC;
_svgalib_port_out(v1 | 1u, 981LL);
for ( i = 0LL; i != 256; ++i )
{
_svgalib_port_out((unsigned int)i, 968LL);
_svgalib_port_out(*(unsigned __int8 *)(a1 + i), 966LL);
}
_svgalib_outcrtc(34LL, *(unsigned __int8 *)(a1 + 256));
return _svgalib_outcrtc(85LL, v1);
}

original_code:
static void IBMRGB52x_restorestate(const unsigned char *regs)
{
    int i;
    unsigned char tmp;
    __svgalib_port_out(0x55, 0x3D4);
    tmp = __svgalib_port_in(0x3D5) & 0xFC;
    __svgalib_port_out(tmp | 0x01, 0x3D5);
    for (i = 0; i < 0x100; i++) {
 __svgalib_port_out(i, 0x3C8);
 __svgalib_port_out(regs[i], 0x3C6);
    }
    __svgalib_outcrtc(0x22, regs[0x100]);
    __svgalib_outcrtc(0x55, tmp);
}

canonical_original_code:
static void IBMRGB52x_restorestate(unsigned char *regs)
{
    int i;
    unsigned char tmp;
    __svgalib_port_out(0x55, 0x3D4);
    tmp = __svgalib_port_in(0x3D5) & 0xFC;
    __svgalib_port_out(tmp | 0x01, 0x3D5);
    for (i = 0; i < 0x100; i++) {
 __svgalib_port_out(i, 0x3C8);
 __svgalib_port_out(regs[i], 0x3C6);
    }
    __svgalib_outcrtc(0x22, regs[0x100]);
    __svgalib_outcrtc(0x55, tmp);
}

variable_types:
regs
unsigned char *

variable_types: i int

variable_types:
tmp
unsigned char


decompiled_code:
__int64 __fastcall IBMRGB52x_savestate(__int64 a1)
{
unsigned __int8 v1; // r12
__int64 i; // rbx

_svgalib_port_out(85LL, 980LL);
v1 = _svgalib_port_in(981LL) & 0xFC;
_svgalib_port_out(v1 | 1u, 981LL);
for ( i = 0LL; i != 256; ++i )
{
_svgalib_port_out((unsigned int)i, 968LL);
*(_BYTE *)(a1 + i) = _svgalib_port_in(966LL);
}
*(_BYTE *)(a1 + 256) = _svgalib_incrtc(34LL);
return _svgalib_outcrtc(85LL, v1);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
unsigned __int8 v1; // r12
__int64 i; // rbx

_svgalib_port_out(85LL, 980LL);
v1 = _svgalib_port_in(981LL) & 0xFC;
_svgalib_port_out(v1 | 1u, 981LL);
for ( i = 0LL; i != 256; ++i )
{
_svgalib_port_out((unsigned int)i, 968LL);
*(_BYTE *)(a1 + i) = _svgalib_port_in(966LL);
}
*(_BYTE *)(a1 + 256) = _svgalib_incrtc(34LL);
return _svgalib_outcrtc(85LL, v1);
}

original_code:
static void IBMRGB52x_savestate(unsigned char *regs)
{
    int i;
    unsigned char tmp;
    __svgalib_port_out(0x55, 0x3D4);
    tmp = __svgalib_port_in(0x3D5) & 0xFC;
    __svgalib_port_out(tmp | 0x01, 0x3D5);
    for (i = 0; i < 0x100; i++) {
 __svgalib_port_out(i, 0x3C8);
 regs[i] = __svgalib_port_in(0x3C6);
    }
    regs[0x100] = __svgalib_incrtc(0x22);
    __svgalib_outcrtc(0x55, tmp);
}


decompiled_code:
__int64 IBMRGB52x_probe()
{
unsigned __int8 v0; // al
unsigned int v1; // er14
unsigned __int8 v2; // al
unsigned int v3; // er13
unsigned int v4; // ebx
unsigned __int8 v5; // r15
unsigned __int8 v6; // bp
unsigned int v7; // er12
unsigned __int8 v9; // [rsp+Ch] [rbp-6Ch]
unsigned __int8 v10; // [rsp+10h] [rbp-68h]
unsigned __int8 v11; // [rsp+14h] [rbp-64h]
unsigned __int8 v12; // [rsp+18h] [rbp-60h]
unsigned __int8 v13; // [rsp+1Ch] [rbp-5Ch]
unsigned __int8 v14; // [rsp+20h] [rbp-58h]
unsigned __int8 v15; // [rsp+24h] [rbp-54h]
unsigned __int8 v16; // [rsp+28h] [rbp-50h]
unsigned __int8 v17; // [rsp+2Ch] [rbp-4Ch]
unsigned __int8 v18; // [rsp+30h] [rbp-48h]
unsigned __int8 v19; // [rsp+34h] [rbp-44h]
char v20; // [rsp+3Ch] [rbp-3Ch]

_svgalib_port_out(67LL, 980LL);
v0 = _svgalib_port_in(981LL);
v1 = v0;
_svgalib_port_out(v0 & 0xFD, 981LL);
_svgalib_port_out(85LL, 980LL);
v2 = _svgalib_port_in(981LL);
v3 = v2;
v4 = v2 & 0xFC;
_svgalib_port_out(v2 & 0xFC, 981LL);
v17 = _svgalib_port_in(966LL);
v18 = _svgalib_port_in(967LL);
v5 = _svgalib_port_in(968LL);
_svgalib_port_out(0LL, 967LL);
v14 = _svgalib_port_in(969LL);
v15 = _svgalib_port_in(969LL);
v16 = _svgalib_port_in(969LL);
_svgalib_port_out(1LL, 967LL);
v9 = _svgalib_port_in(969LL);
v10 = _svgalib_port_in(969LL);
v11 = _svgalib_port_in(969LL);
_svgalib_port_out(85LL, 980LL);
_svgalib_port_out(v4 | 1, 981LL);
v12 = _svgalib_port_in(968LL);
v13 = _svgalib_port_in(969LL);
_svgalib_port_out(0LL, 969LL);
_svgalib_port_out(0LL, 968LL);
v19 = _svgalib_port_in(966LL);
_svgalib_port_out(1LL, 968LL);
v6 = _svgalib_port_in(966LL);
v7 = 0;
if ( (unsigned __int8)(v6 - 1) <= 1u )
{
_svgalib_port_out(0LL, 968LL);
_svgalib_port_out(~v19, 966LL);
_svgalib_port_out(1LL, 968LL);
_svgalib_port_out(~v6, 966LL);
_svgalib_port_out(0LL, 968LL);
v20 = _svgalib_port_in(966LL);
_svgalib_port_out(1LL, 968LL);
if ( v6 == (unsigned __int8)_svgalib_port_in(966LL) && v19 == v20 )
{
v7 = v19 | (v6 << 8);
}
else
{
_svgalib_port_out(0LL, 968LL);
_svgalib_port_out(v19, 966LL);
_svgalib_port_out(1LL, 968LL);
_svgalib_port_out(v6, 966LL);
v7 = 0;
}
}
_svgalib_port_out(v12, 968LL);
_svgalib_port_out(v13, 969LL);
_svgalib_port_out(85LL, 980LL);
_svgalib_port_out(v4, 981LL);
_svgalib_port_out(0LL, 968LL);
_svgalib_port_out(v14, 969LL);
_svgalib_port_out(v15, 969LL);
_svgalib_port_out(v16, 969LL);
_svgalib_port_out(1LL, 968LL);
_svgalib_port_out(v9, 969LL);
_svgalib_port_out(v10, 969LL);
_svgalib_port_out(v11, 969LL);
_svgalib_port_out(v17, 966LL);
_svgalib_port_out(v18, 967LL);
_svgalib_port_out(v5, 968LL);
_svgalib_port_out(67LL, 980LL);
_svgalib_port_out(v1, 981LL);
_svgalib_port_out(85LL, 980LL);
_svgalib_port_out(v3, 981LL);
return v7;
}

canonical_decompiled_code:
__int64 func8()
{
unsigned __int8 v0; // al
unsigned int v1; // er14
unsigned __int8 v2; // al
unsigned int v3; // er13
unsigned int v4; // ebx
unsigned __int8 v5; // r15
unsigned __int8 v6; // bp
unsigned int v7; // er12
unsigned __int8 v9; // [rsp+Ch] [rbp-6Ch]
unsigned __int8 v10; // [rsp+10h] [rbp-68h]
unsigned __int8 v11; // [rsp+14h] [rbp-64h]
unsigned __int8 v12; // [rsp+18h] [rbp-60h]
unsigned __int8 v13; // [rsp+1Ch] [rbp-5Ch]
unsigned __int8 v14; // [rsp+20h] [rbp-58h]
unsigned __int8 v15; // [rsp+24h] [rbp-54h]
unsigned __int8 v16; // [rsp+28h] [rbp-50h]
unsigned __int8 v17; // [rsp+2Ch] [rbp-4Ch]
unsigned __int8 v18; // [rsp+30h] [rbp-48h]
unsigned __int8 v19; // [rsp+34h] [rbp-44h]
char v20; // [rsp+3Ch] [rbp-3Ch]

_svgalib_port_out(67LL, 980LL);
v0 = _svgalib_port_in(981LL);
v1 = v0;
_svgalib_port_out(v0 & 0xFD, 981LL);
_svgalib_port_out(85LL, 980LL);
v2 = _svgalib_port_in(981LL);
v3 = v2;
v4 = v2 & 0xFC;
_svgalib_port_out(v2 & 0xFC, 981LL);
v17 = _svgalib_port_in(966LL);
v18 = _svgalib_port_in(967LL);
v5 = _svgalib_port_in(968LL);
_svgalib_port_out(0LL, 967LL);
v14 = _svgalib_port_in(969LL);
v15 = _svgalib_port_in(969LL);
v16 = _svgalib_port_in(969LL);
_svgalib_port_out(1LL, 967LL);
v9 = _svgalib_port_in(969LL);
v10 = _svgalib_port_in(969LL);
v11 = _svgalib_port_in(969LL);
_svgalib_port_out(85LL, 980LL);
_svgalib_port_out(v4 | 1, 981LL);
v12 = _svgalib_port_in(968LL);
v13 = _svgalib_port_in(969LL);
_svgalib_port_out(0LL, 969LL);
_svgalib_port_out(0LL, 968LL);
v19 = _svgalib_port_in(966LL);
_svgalib_port_out(1LL, 968LL);
v6 = _svgalib_port_in(966LL);
v7 = 0;
if ( (unsigned __int8)(v6 - 1) <= 1u )
{
_svgalib_port_out(0LL, 968LL);
_svgalib_port_out(~v19, 966LL);
_svgalib_port_out(1LL, 968LL);
_svgalib_port_out(~v6, 966LL);
_svgalib_port_out(0LL, 968LL);
v20 = _svgalib_port_in(966LL);
_svgalib_port_out(1LL, 968LL);
if ( v6 == (unsigned __int8)_svgalib_port_in(966LL) && v19 == v20 )
{
v7 = v19 | (v6 << 8);
}
else
{
_svgalib_port_out(0LL, 968LL);
_svgalib_port_out(v19, 966LL);
_svgalib_port_out(1LL, 968LL);
_svgalib_port_out(v6, 966LL);
v7 = 0;
}
}
_svgalib_port_out(v12, 968LL);
_svgalib_port_out(v13, 969LL);
_svgalib_port_out(85LL, 980LL);
_svgalib_port_out(v4, 981LL);
_svgalib_port_out(0LL, 968LL);
_svgalib_port_out(v14, 969LL);
_svgalib_port_out(v15, 969LL);
_svgalib_port_out(v16, 969LL);
_svgalib_port_out(1LL, 968LL);
_svgalib_port_out(v9, 969LL);
_svgalib_port_out(v10, 969LL);
_svgalib_port_out(v11, 969LL);
_svgalib_port_out(v17, 966LL);
_svgalib_port_out(v18, 967LL);
_svgalib_port_out(v5, 968LL);
_svgalib_port_out(67LL, 980LL);
_svgalib_port_out(v1, 981LL);
_svgalib_port_out(85LL, 980LL);
_svgalib_port_out(v3, 981LL);
return v7;
}

original_code:
static int IBMRGB52x_probe(void)
{
    unsigned char CR43, CR55, dac[3], lut[6];
    unsigned char ilow, ihigh, id, rev, id2, rev2;
    int i, j;
    int ret = 0;
    __svgalib_port_out(0x43, 0x3D4);
    CR43 = __svgalib_port_in(0x3D5);
    __svgalib_port_out(CR43 & ~0x02, 0x3D5);
    __svgalib_port_out(0x55, 0x3D4);
    CR55 = __svgalib_port_in(0x3D5);
    __svgalib_port_out(CR55 & ~0x03, 0x3D5);
    for (i = 0; i < 3; i++)
 dac[i] = __svgalib_port_in(0x3C6 + i);
    for (i = j = 0; i < 2; i++) {
 __svgalib_port_out(i, 0x3C7);
 lut[j++] = __svgalib_port_in(0x3C9);
 lut[j++] = __svgalib_port_in(0x3C9);
 lut[j++] = __svgalib_port_in(0x3C9);
    }
    __svgalib_port_out(0x55, 0x3D4);
    __svgalib_port_out((CR55 & ~0x03) | 0x01, 0x3D5);
    ilow = __svgalib_port_in(0x3C8);
    ihigh = __svgalib_port_in(0x3C9);
    __svgalib_port_out(0, 0x3C9);
    __svgalib_port_out(0x00, 0x3C8);
    rev = __svgalib_port_in(0x3C6);
    __svgalib_port_out(0x01, 0x3C8);
    id = __svgalib_port_in(0x3C6);
    if (id >= 1 && id <= 2) {
 __svgalib_port_out(0x00, 0x3C8);
 __svgalib_port_out(~rev, 0x3C6);
 __svgalib_port_out(0x01, 0x3C8);
 __svgalib_port_out(~id, 0x3C6);
 __svgalib_port_out(0x00, 0x3C8);
 rev2 = __svgalib_port_in(0x3C6);
 __svgalib_port_out(0x01, 0x3C8);
 id2 = __svgalib_port_in(0x3C6);
 if (id == id2 && rev == rev2) {
     ret = (id << 8) | rev;
 } else {
     __svgalib_port_out(0x00, 0x3C8);
     __svgalib_port_out(rev, 0x3C6);
     __svgalib_port_out(0x01, 0x3C8);
     __svgalib_port_out(id, 0x3C6);
 }
    }
    __svgalib_port_out(ilow, 0x3C8);
    __svgalib_port_out(ihigh, 0x3C9);
    __svgalib_port_out(0x55, 0x3D4);
    __svgalib_port_out(CR55 & ~0x03, 0x3D5);
    for (i = j = 0; i < 2; i++) {
 __svgalib_port_out(i, 0x3C8);
 __svgalib_port_out(lut[j++], 0x3C9);
 __svgalib_port_out(lut[j++], 0x3C9);
 __svgalib_port_out(lut[j++], 0x3C9);
    }
    for (i = 0; i < 3; i++)
 __svgalib_port_out(dac[i], 0x3C6 + i);
    __svgalib_port_out(0x43, 0x3D4);
    __svgalib_port_out(CR43, 0x3D5);
    __svgalib_port_out(0x55, 0x3D4);
    __svgalib_port_out(CR55, 0x3D5);
    return ret;
}

canonical_original_code:
static int IBMRGB52x_probe()
{
    unsigned char CR43; unsigned char CR55; unsigned char dac[3]; unsigned char lut[6];
    unsigned char ilow; unsigned char ihigh; unsigned char id; unsigned char rev; unsigned char id2; unsigned char rev2;
    int i; int j;
    int ret = 0;
    __svgalib_port_out(0x43, 0x3D4);
    CR43 = __svgalib_port_in(0x3D5);
    __svgalib_port_out(CR43 & ~0x02, 0x3D5);
    __svgalib_port_out(0x55, 0x3D4);
    CR55 = __svgalib_port_in(0x3D5);
    __svgalib_port_out(CR55 & ~0x03, 0x3D5);
    for (i = 0; i < 3; i++)
 dac[i] = __svgalib_port_in(0x3C6 + i);
    for (i = j = 0; i < 2; i++) {
 __svgalib_port_out(i, 0x3C7);
 lut[j++] = __svgalib_port_in(0x3C9);
 lut[j++] = __svgalib_port_in(0x3C9);
 lut[j++] = __svgalib_port_in(0x3C9);
    }
    __svgalib_port_out(0x55, 0x3D4);
    __svgalib_port_out((CR55 & ~0x03) | 0x01, 0x3D5);
    ilow = __svgalib_port_in(0x3C8);
    ihigh = __svgalib_port_in(0x3C9);
    __svgalib_port_out(0, 0x3C9);
    __svgalib_port_out(0x00, 0x3C8);
    rev = __svgalib_port_in(0x3C6);
    __svgalib_port_out(0x01, 0x3C8);
    id = __svgalib_port_in(0x3C6);
    if (id >= 1 && id <= 2) {
 __svgalib_port_out(0x00, 0x3C8);
 __svgalib_port_out(~rev, 0x3C6);
 __svgalib_port_out(0x01, 0x3C8);
 __svgalib_port_out(~id, 0x3C6);
 __svgalib_port_out(0x00, 0x3C8);
 rev2 = __svgalib_port_in(0x3C6);
 __svgalib_port_out(0x01, 0x3C8);
 id2 = __svgalib_port_in(0x3C6);
 if (id == id2 && rev == rev2) {
     ret = (id << 8) | rev;
 } else {
     __svgalib_port_out(0x00, 0x3C8);
     __svgalib_port_out(rev, 0x3C6);
     __svgalib_port_out(0x01, 0x3C8);
     __svgalib_port_out(id, 0x3C6);
 }
    }
    __svgalib_port_out(ilow, 0x3C8);
    __svgalib_port_out(ihigh, 0x3C9);
    __svgalib_port_out(0x55, 0x3D4);
    __svgalib_port_out(CR55 & ~0x03, 0x3D5);
    for (i = j = 0; i < 2; i++) {
 __svgalib_port_out(i, 0x3C8);
 __svgalib_port_out(lut[j++], 0x3C9);
 __svgalib_port_out(lut[j++], 0x3C9);
 __svgalib_port_out(lut[j++], 0x3C9);
    }
    for (i = 0; i < 3; i++)
 __svgalib_port_out(dac[i], 0x3C6 + i);
    __svgalib_port_out(0x43, 0x3D4);
    __svgalib_port_out(CR43, 0x3D5);
    __svgalib_port_out(0x55, 0x3D4);
    __svgalib_port_out(CR55, 0x3D5);
    return ret;
}

variable_types:
dac
unsigned char [3]

variable_types:
lut
unsigned char [6]

variable_types:
ilow
unsigned char

variable_types:
ihigh
unsigned char

variable_types:
id
unsigned char

variable_types:
rev
unsigned char

variable_types: j int

variable_types: ret int


decompiled_code:
__int64 IBMRGB52x_init()
{
int v0; // eax
unsigned __int8 v1; // bl
char v2; // al

v0 = IBMRGB52x_probe();
if ( _svgalib_driver_report )
fprintf(
stderr,
"svgalib: Using IBM RGB 52%d PaletteDAC, revision %d.\n",
(unsigned int)(v0 >> 8 == 1) + 4,
(unsigned __int8)v0);
_svgalib_port_out(85LL, 980LL);
v1 = _svgalib_port_in(981LL) & 0xFC;
_svgalib_port_out(v1 | 1u, 981LL);
v2 = _svgalib_port_in(967LL);
_svgalib_port_out(v2 & 0xFE, 967LL);
_svgalib_port_out(0LL, 969LL);
return _svgalib_outcrtc(85LL, v1);
}

canonical_decompiled_code:
__int64 func9()
{
int v0; // eax
unsigned __int8 v1; // bl
char v2; // al

v0 = func8();
if ( _svgalib_driver_report )
fprintf(
stderr,
"svgalib: Using IBM RGB 52%d PaletteDAC, revision %d.\n",
(unsigned int)(v0 >> 8 == 1) + 4,
(unsigned __int8)v0);
_svgalib_port_out(85LL, 980LL);
v1 = _svgalib_port_in(981LL) & 0xFC;
_svgalib_port_out(v1 | 1u, 981LL);
v2 = _svgalib_port_in(967LL);
_svgalib_port_out(v2 & 0xFE, 967LL);
_svgalib_port_out(0LL, 969LL);
return _svgalib_outcrtc(85LL, v1);
}

original_code:
static void IBMRGB52x_init(void)
{
    unsigned char tmp, CR55;
    int idrev;
    idrev = IBMRGB52x_probe();
    if (__svgalib_driver_report)
 fprintf(stderr,"svgalib: Using IBM RGB 52%d PaletteDAC, revision %d.\n",
        (idrev >> 8) == 1 ? 5 : 4,
        idrev & 0xff);
    __svgalib_port_out(0x55, 0x3D4);
    CR55 = __svgalib_port_in(0x3D5) & 0xFC;
    __svgalib_port_out(CR55 | 0x01, 0x3D5);
    tmp = __svgalib_port_in(0x3C7);
    __svgalib_port_out(tmp & ~0x01, 0x3C7);
    __svgalib_port_out(0, 0x3C9);
    __svgalib_outcrtc(0x55, CR55);
}

canonical_original_code:
static void IBMRGB52x_init()
{
    unsigned char tmp; unsigned char CR55;
    int idrev;
    idrev = IBMRGB52x_probe();
    if (__svgalib_driver_report)
 fprintf(stderr,"svgalib: Using IBM RGB 52%d PaletteDAC, revision %d.\n",
        (idrev >> 8) == 1 ? 5 : 4,
        idrev & 0xff);
    __svgalib_port_out(0x55, 0x3D4);
    CR55 = __svgalib_port_in(0x3D5) & 0xFC;
    __svgalib_port_out(CR55 | 0x01, 0x3D5);
    tmp = __svgalib_port_in(0x3C7);
    __svgalib_port_out(tmp & ~0x01, 0x3C7);
    __svgalib_port_out(0, 0x3C9);
    __svgalib_outcrtc(0x55, CR55);
}

variable_types: idrev int


decompiled_code:
int __fastcall check_new_mode(char *s1, char *s2, char *a3, char *a4)
{
char *v4; // rbx
int result; // eax

v4 = s1;
if ( !s1 )
return 1;
while ( 1 )
{
if ( !strcmp(v4, s2) && !strcmp(v4 + 100, a3) )
{
result = strcmp(v4 + 200, a4);
if ( !result )
break;
}
v4 = (char *)*((_QWORD *)v4 + 40);
if ( !v4 )
return 1;
}
return result;
}

canonical_decompiled_code:
int __fastcall func0(char *s1, char *s2, char *a3, char *a4)
{
char *v4; // rbx
int result; // eax

v4 = s1;
if ( !s1 )
return 1;
while ( 1 )
{
if ( !strcmp(v4, s2) && !strcmp(v4 + 100, a3) )
{
result = strcmp(v4 + 200, a4);
if ( !result )
break;
}
v4 = (char *)*((_QWORD *)v4 + 40);
if ( !v4 )
return 1;
}
return result;
}

original_code:
static int check_new_mode(ModeList * p, char *x, char *y, char *c)
{
    while (p != ((void *)0)) {
 if (strcmp(p->x, x) == 0
     && strcmp(p->y, y) == 0
     && strcmp(p->c, c) == 0)
     return (1==0);
 p = p->nxt;
    }
    return (1==1);
}

canonical_original_code:
static int check_new_mode(struct ML *p, char *x, char *y, char *c)
{
    while (p != ((void *)0)) {
 if (strcmp(p->x, x) == 0
     && strcmp(p->y, y) == 0
     && strcmp(p->c, c) == 0)
     return (1==0);
 p = p->nxt;
    }
    return (1==1);
}

variable_types: struct ML { char x[100]; int mnum; int equ; void *regs; struct ML *nxt; } *

variable_types:
x
char *

variable_types:
y
char *

variable_types:
c
char *


decompiled_code:
char *__fastcall mode2name(const char *a1, const char *a2, const char *a3)
{
sprintf(mn_0, "G%sx%sx%s", a1, a2, a3);
return mn_0;
}

canonical_decompiled_code:
char *__fastcall func4(const char *a1, const char *a2, const char *a3)
{
sprintf(mn_0, "G%sx%sx%s", a1, a2, a3);
return mn_0;
}

original_code:
static char *
 mode2name(char *x, char *y, char *c)
{
    static char mn[100];
    sprintf(mn, "G%sx%sx%s", x, y, c);
    return mn;
}

canonical_original_code:
static char *mode2name(char *x, char *y, char *c)
{
    static char mn[100];
    sprintf(mn, "G%sx%sx%s", x, y, c);
    return mn;
}

variable_types:
mn
char [100]

return_type: char *


variable_types:
rp
char *

variable_types:
mnum
int *

variable_types:
p
char *

variable_types: cols int

variable_types: xbytes int


Malloc

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

result = malloc(a1);
if ( !result )
{
fwrite("regextr.c: Can't allocate memory\n", 1uLL, 0x21uLL, stderr);
exit(1);
}
return result;
}

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

result = malloc(a1);
if ( !result )
{
fwrite("regextr.c: Can't allocate memory\n", 1uLL, 0x21uLL, stderr);
exit(1);
}
return result;
}

original_code:
static void *
 Malloc(size_t bytes)
{
    void *res;
    res = (void *) malloc(bytes);
    if (res == ((void *)0)) {
 fprintf(stderr, "regextr.c: Can't allocate memory\n");
 exit(1);
    }
    return res;
}

canonical_original_code:
static void *Malloc(unsigned long bytes)
{
    void *res;
    res = (void *) malloc(bytes);
    if (res == ((void *)0)) {
 fprintf(stderr, "regextr.c: Can't allocate memory\n");
 exit(1);
    }
    return res;
}

variable_types:
bytes
unsigned long

variable_types:
res
void *

return_type: void *


decompiled_code:
char *__fastcall store_equ(char **a1, const char *a2, const char *a3, const char *a4, int a5, const char *a6, char *a7, char *a8)
{
char *i; // rbx
char *v12; // rbx
char *v13; // rbx
char *result; // rax

for ( i = *a1; i; i = (char *)*((_QWORD *)i + 40) )
{
if ( !strcmp(i, a2) && !strcmp(i + 100, a3) && !strcmp(i + 200, a4) )
{
fprintf(stderr, "regextr.c: Duplicate g%sx%sx%s_regs !\n", a2, a3, a4);
exit(1);
}
}
v12 = (char *)Malloc(0x148uLL);
strcpy(v12, a2);
strcpy(v12 + 100, a3);
strcpy(v12 + 200, a4);
*((_DWORD *)v12 + 75) = a5;
*((_DWORD *)v12 + 76) = 1;
*((_QWORD *)v12 + 40) = *a1;
*a1 = v12;
v13 = (char *)Malloc(0x148uLL);
strcpy(v13, a6);
strcpy(v13 + 100, a7);
strcpy(v13 + 200, a8);
*((_DWORD *)v13 + 75) = 0;
*((_DWORD *)v13 + 76) = 0;
*((_QWORD *)v13 + 39) = 0LL;
*((_QWORD *)v13 + 40) = 0LL;
result = *a1;
*((_QWORD *)*a1 + 39) = v13;
return result;
}

canonical_decompiled_code:
char *__fastcall func7(char **a1, const char *a2, const char *a3, const char *a4, int a5, const char *a6, char *a7, char *a8)
{
char *i; // rbx
char *v12; // rbx
char *v13; // rbx
char *result; // rax

for ( i = *a1; i; i = (char *)*((_QWORD *)i + 40) )
{
if ( !strcmp(i, a2) && !strcmp(i + 100, a3) && !strcmp(i + 200, a4) )
{
fprintf(stderr, "regextr.c: Duplicate g%sx%sx%s_regs !\n", a2, a3, a4);
exit(1);
}
}
v12 = (char *)func6(0x148uLL);
strcpy(v12, a2);
strcpy(v12 + 100, a3);
strcpy(v12 + 200, a4);
*((_DWORD *)v12 + 75) = a5;
*((_DWORD *)v12 + 76) = 1;
*((_QWORD *)v12 + 40) = *a1;
*a1 = v12;
v13 = (char *)func6(0x148uLL);
strcpy(v13, a6);
strcpy(v13 + 100, a7);
strcpy(v13 + 200, a8);
*((_DWORD *)v13 + 75) = 0;
*((_DWORD *)v13 + 76) = 0;
*((_QWORD *)v13 + 39) = 0LL;
*((_QWORD *)v13 + 40) = 0LL;
result = *a1;
*((_QWORD *)*a1 + 39) = v13;
return result;
}

original_code:
static void store_equ(ModeList ** root, char *x1, char *y1, char *c1,
        int mnum, char *x2, char *y2, char *c2)
{
    ModeList *p;
    p = *root;
    while (p != ((void *)0)) {
 if (strcmp(p->x, x1) == 0
     && strcmp(p->y, y1) == 0
     && strcmp(p->c, c1) == 0) {
     fprintf(stderr, "regextr.c: Duplicate g%sx%sx%s_regs !\n", x1, y1, c1);
     exit(1);
 }
 p = p->nxt;
    }
    p = (ModeList *) Malloc(sizeof(ModeList));
    strcpy(p->x, x1);
    strcpy(p->y, y1);
    strcpy(p->c, c1);
    p->mnum = mnum;
    p->equ = (1==1);
    p->nxt = *root;
    *root = p;
    p = (ModeList *) Malloc(sizeof(ModeList));
    strcpy(p->x, x2);
    strcpy(p->y, y2);
    strcpy(p->c, c2);
    p->mnum = 0;
    p->equ = (1==0);
    p->regs = ((void *)0);
    p->nxt = ((void *)0);
    (*root)->regs = (void *) p;
}

canonical_original_code:
static void store_equ(struct ML **root, char *x1, char *y1, char *c1, int mnum, char *x2, char *y2, char *c2)
{
    struct ML *p;
    p = *root;
    while (p != ((void *)0)) {
 if (strcmp(p->x, x1) == 0
     && strcmp(p->y, y1) == 0
     && strcmp(p->c, c1) == 0) {
     fprintf(stderr, "regextr.c: Duplicate g%sx%sx%s_regs !\n", x1, y1, c1);
     exit(1);
 }
 p = p->nxt;
    }
    p = (struct ML *) Malloc(sizeof(ModeList));
    strcpy(p->x, x1);
    strcpy(p->y, y1);
    strcpy(p->c, c1);
    p->mnum = mnum;
    p->equ = (1==1);
    p->nxt = *root;
    *root = p;
    p = (struct ML *) Malloc(sizeof(ModeList));
    strcpy(p->x, x2);
    strcpy(p->y, y2);
    strcpy(p->c, c2);
    p->mnum = 0;
    p->equ = (1==0);
    p->regs = ((void *)0);
    p->nxt = ((void *)0);
    (*root)->regs = (void *) p;
}

variable_types: struct ML { char x[100]; int mnum; int equ; void *regs; struct ML *nxt; } **

variable_types: mnum int


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

result = (unsigned int)a2;
if ( a1 == 8 && a2 > 80000 )
return (unsigned int)(a2 / 2);
if ( a1 == 32 )
result = (unsigned int)(2 * a2);
return result;
}

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

result = (unsigned int)a2;
if ( a1 == 8 && a2 > 80000 )
return (unsigned int)(a2 / 2);
if ( a1 == 32 )
result = (unsigned int)(2 * a2);
return result;
}

original_code:
static int att20c498_map_clock(int bpp, int pixelclock)
{
    if (bpp == 8 && pixelclock > 80000)
 return pixelclock / 2;
    if (bpp == 16)
 return pixelclock;
    if (bpp == 32)
 return pixelclock * 2;
    return pixelclock;
}

variable_types: bpp int

variable_types: pixelclock int


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

result = a3;
if ( a1 == 8 && a2 > 80000 )
return (unsigned int)((int)a3 / 2);
if ( a1 == 32 )
result = 2 * a3;
return result;
}

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

result = a3;
if ( a1 == 8 && a2 > 80000 )
return (unsigned int)((int)a3 / 2);
if ( a1 == 32 )
result = 2 * a3;
return result;
}

original_code:
static int att20c498_map_horizontal_crtc(int bpp, int pixelclock, int htiming)
{
    if (bpp == 8 && pixelclock > 80000)
 return htiming / 2;
    if (bpp == 32)
 return htiming * 2;
    return htiming;
}

variable_types: htiming int


decompiled_code:
_DWORD *att20c490_init()
{
_DWORD *result; // rax

result = &_svgalib_driver_report;
if ( _svgalib_driver_report )
result = (_DWORD *)fwrite("svgalib: Using AT&T20C490-compatible truecolor DAC.\n", 1uLL, 0x34uLL, stderr);
return result;
}

canonical_decompiled_code:
_DWORD *func6()
{
_DWORD *result; // rax

result = &_svgalib_driver_report;
if ( _svgalib_driver_report )
result = (_DWORD *)fwrite("svgalib: Using AT&T20C490-compatible truecolor DAC.\n", 1uLL, 0x34uLL, stderr);
return result;
}

original_code:
static void att20c490_init(void)
{
    if (__svgalib_driver_report)
 fprintf(stderr,"svgalib: Using AT&T20C490-compatible truecolor DAC.\n");
}

canonical_original_code:
static void att20c490_init()
{
    if (__svgalib_driver_report)
 fprintf(stderr,"svgalib: Using AT&T20C490-compatible truecolor DAC.\n");
}


decompiled_code:
_DWORD *att20c498_init()
{
_DWORD *result; // rax

result = &_svgalib_driver_report;
if ( _svgalib_driver_report )
result = (_DWORD *)fwrite("svgalib: Using AT&T20C498-compatible DAC, 80 MHz rated.\n", 1uLL, 0x38uLL, stderr);
return result;
}

canonical_decompiled_code:
_DWORD *func7()
{
_DWORD *result; // rax

result = &_svgalib_driver_report;
if ( _svgalib_driver_report )
result = (_DWORD *)fwrite("svgalib: Using AT&T20C498-compatible DAC, 80 MHz rated.\n", 1uLL, 0x38uLL, stderr);
return result;
}

original_code:
static void att20c498_init(void)
{
    if (__svgalib_driver_report)
 fprintf(stderr,"svgalib: Using AT&T20C498-compatible DAC, 80 MHz rated.\n");
}

canonical_original_code:
static void att20c498_init()
{
    if (__svgalib_driver_report)
 fprintf(stderr,"svgalib: Using AT&T20C498-compatible DAC, 80 MHz rated.\n");
}


decompiled_code:
_BOOL8 att20c490_probe()
{
unsigned __int8 v0; // bl
unsigned __int8 v1; // bp
_BOOL4 v2; // er13

ramdac_dactocomm();
v0 = _svgalib_port_in(966LL);
ramdac_dactopel();
v1 = _svgalib_port_in(966LL);
_svgalib_port_out((unsigned __int8)~v0, 966LL);
ramdac_dactocomm();
v2 = 0;
if ( ~v0 != (unsigned __int8)_svgalib_port_in(966LL) && (ramdac_setcomm(0xE0u) & 0xE0) == 0xE0 )
{
if ( (ramdac_setcomm(0x60u) & 0xE0) != 0 )
{
ramdac_setcomm(v0);
v2 = 0;
if ( (unsigned __int8)~v0 == (unsigned int)_svgalib_port_in(966LL) )
v2 = (unsigned __int8)ramdac_setcomm(0xFFu) == 0xFF;
}
else
{
ramdac_setcomm(2u);
v2 = 1;
}
}
ramdac_dactocomm();
_svgalib_port_out(v0, 966LL);
ramdac_dactopel();
_svgalib_port_out(v1, 966LL);
return v2;
}

canonical_decompiled_code:
_BOOL8 func11()
{
unsigned __int8 v0; // bl
unsigned __int8 v1; // bp
_BOOL4 v2; // er13

func8();
v0 = _svgalib_port_in(966LL);
func9();
v1 = _svgalib_port_in(966LL);
_svgalib_port_out((unsigned __int8)~v0, 966LL);
func8();
v2 = 0;
if ( ~v0 != (unsigned __int8)_svgalib_port_in(966LL) && (func10(0xE0u) & 0xE0) == 0xE0 )
{
if ( (func10(0x60u) & 0xE0) != 0 )
{
func10(v0);
v2 = 0;
if ( (unsigned __int8)~v0 == (unsigned int)_svgalib_port_in(966LL) )
v2 = (unsigned __int8)func10(0xFFu) == 0xFF;
}
else
{
func10(2u);
v2 = 1;
}
}
func8();
_svgalib_port_out(v0, 966LL);
func9();
_svgalib_port_out(v1, 966LL);
return v2;
}

original_code:
static int att20c490_probe(void)
{
    unsigned char oldcomm, notcomm, oldpel, v;
    int flag = 0;
    _ramdac_dactocomm();
    oldcomm = __svgalib_port_in(0x3C6);
    _ramdac_dactopel();
    oldpel = __svgalib_port_in(0x3C6);
    notcomm = ~oldcomm;
    __svgalib_port_out(notcomm, 0x3C6);
    _ramdac_dactocomm();
    v = __svgalib_port_in(0x3C6);
    if (v != notcomm) {
 if ((_ramdac_setcomm(0xe0) & 0xe0) == 0xe0) {
     if ((_ramdac_setcomm(0x60) & 0xe0) == 0) {
  if ((_ramdac_setcomm(2) & 2) > 0)
      flag = 1;
  else
      flag = 1;
     } else {
  _ramdac_setcomm(oldcomm);
  if (__svgalib_port_in(0x3C6) == notcomm)
      if (_ramdac_setcomm(0xFF) == 0xFF)
   flag = 1;
     }
 }
    }
    _ramdac_dactocomm();
    __svgalib_port_out(oldcomm, 0x3C6);
    _ramdac_dactopel();
    __svgalib_port_out(oldpel, 0x3C6);
    return flag;
}

canonical_original_code:
static int att20c490_probe()
{
    unsigned char oldcomm; unsigned char notcomm; unsigned char oldpel; unsigned char v;
    int flag = 0;
    _ramdac_dactocomm();
    oldcomm = __svgalib_port_in(0x3C6);
    _ramdac_dactopel();
    oldpel = __svgalib_port_in(0x3C6);
    notcomm = ~oldcomm;
    __svgalib_port_out(notcomm, 0x3C6);
    _ramdac_dactocomm();
    v = __svgalib_port_in(0x3C6);
    if (v != notcomm) {
 if ((_ramdac_setcomm(0xe0) & 0xe0) == 0xe0) {
     if ((_ramdac_setcomm(0x60) & 0xe0) == 0) {
  if ((_ramdac_setcomm(2) & 2) > 0)
      flag = 1;
  else
      flag = 1;
     } else {
  _ramdac_setcomm(oldcomm);
  if (__svgalib_port_in(0x3C6) == notcomm)
      if (_ramdac_setcomm(0xFF) == 0xFF)
   flag = 1;
     }
 }
    }
    _ramdac_dactocomm();
    __svgalib_port_out(oldcomm, 0x3C6);
    _ramdac_dactopel();
    __svgalib_port_out(oldpel, 0x3C6);
    return flag;
}

variable_types:
oldcomm
unsigned char

variable_types:
notcomm
unsigned char

variable_types:
oldpel
unsigned char

variable_types:
v
unsigned char

variable_types: flag int


decompiled_code:
__int64 __fastcall ali_setlogicalwidth(int a1)
{
return _svgalib_port_outw((unsigned int)((a1 >> 3 << 8) + 19), 980LL);
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
return _svgalib_port_outw((unsigned int)((a1 >> 3 << 8) + 19), 980LL);
}

original_code:
static void ali_setlogicalwidth(int width)
{
    __svgalib_port_outw(0x13 + (width >> 3) * 256, 0x3D4);
}

variable_types: width int


decompiled_code:
__int64 __fastcall ali_setdisplaystart(int a1)
{
_svgalib_port_outw((a1 >> 3) & 0xFF00 | 0xCu, _svgalib_CRT_I);
_svgalib_port_outw((unsigned __int16)((unsigned __int16)(a1 >> 3) << 8) | 0xDu, _svgalib_CRT_I);
return _svgalib_port_outw((a1 >> 11) & 0x700 | 0x20u, _svgalib_CRT_I);
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1)
{
_svgalib_port_outw((a1 >> 3) & 0xFF00 | 0xCu, _svgalib_CRT_I);
_svgalib_port_outw((unsigned __int16)((unsigned __int16)(a1 >> 3) << 8) | 0xDu, _svgalib_CRT_I);
return _svgalib_port_outw((a1 >> 11) & 0x700 | 0x20u, _svgalib_CRT_I);
}

original_code:
static void ali_setdisplaystart(int address)
{
    address >>= 3;
    __svgalib_port_outw(0x0c | (address & 0x00ff00), __svgalib_CRT_I);
    __svgalib_port_outw(0x0d | ((address & 0x00ff) << 8), __svgalib_CRT_I);
    __svgalib_port_outw(0x20 | ((address & 0x070000) >> 8), __svgalib_CRT_I);
}

variable_types: address int


decompiled_code:
__int64 __fastcall ali_setpage(int a1)
{
return _svgalib_port_outw((a1 << 8) | (unsigned int)(unsigned __int8)a1, 982LL);
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1)
{
return _svgalib_port_outw((a1 << 8) | (unsigned int)(unsigned __int8)a1, 982LL);
}

original_code:
static void ali_setpage(int page)
{
    __svgalib_port_outw((last_page = page) | (page << 8), 0x3d6);
}

variable_types: page int


decompiled_code:
__int64 __fastcall ali_modeavailable(int a1)
{
__int64 v1; // rax
int v2; // edx
__int64 result; // rax

v1 = _svgalib_mode_in_table(&ali_modes, (unsigned int)a1);
if ( !v1 || a1 == 3 )
return _svgalib_vga_driverspecs[10]((unsigned int)a1);
if ( v1 == 1 || a1 <= 0 )
return 0LL;
v2 = vga_lastmodenumber();
result = 0LL;
if ( v2 >= a1 )
result = 2 * (unsigned int)(ali_memory << 10 >= _svgalib_infotable[5 * a1 + 3] * _svgalib_infotable[5 * a1 + 1]);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(int a1)
{
__int64 v1; // rax
int v2; // edx
__int64 result; // rax

v1 = _svgalib_mode_in_table(&ali_modes, (unsigned int)a1);
if ( !v1 || a1 == 3 )
return _svgalib_vga_driverspecs[10]((unsigned int)a1);
if ( v1 == 1 || a1 <= 0 )
return 0LL;
v2 = vga_lastmodenumber();
result = 0LL;
if ( v2 >= a1 )
result = 2 * (unsigned int)(ali_memory << 10 >= _svgalib_infotable[5 * a1 + 3] * _svgalib_infotable[5 * a1 + 1]);
return result;
}

original_code:
static int ali_modeavailable(int mode)
{
    const unsigned char *regs;
    struct vgainfo *info;
    regs = __svgalib_mode_in_table(ali_modes, mode);
    if (regs == ((void *)0) || mode == 3) {
 return __svgalib_vga_driverspecs.modeavailable(mode);
    }
    if (regs == ((unsigned char *)1) || mode <= 0 || mode > vga_lastmodenumber()) {
 return 0;
    }
    info = &__svgalib_infotable[mode];
    if (ali_memory * 1024 < info->ydim * info->xbytes) {
 return 0;
    }
    return 2;
}

canonical_original_code:
static int ali_modeavailable(int mode)
{
    unsigned char *regs;
    struct vgainfo *info;
    regs = __svgalib_mode_in_table(ali_modes, mode);
    if (regs == ((void *)0) || mode == 3) {
 return __svgalib_vga_driverspecs.modeavailable(mode);
    }
    if (regs == ((unsigned char *)1) || mode <= 0 || mode > vga_lastmodenumber()) {
 return 0;
    }
    info = &__svgalib_infotable[mode];
    if (ali_memory * 1024 < info->ydim * info->xbytes) {
 return 0;
    }
    return 2;
}

variable_types: mode int

variable_types:
regs
unsigned char *

variable_types: struct vgainfo { int xdim; int ydim; int colors; int xbytes; int bytesperpixel; } *


decompiled_code:
__int64 __fastcall ali_setwrpage(__int64 a1)
{
return _svgalib_port_out(a1, 983LL);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
return _svgalib_port_out(a1, 983LL);
}

original_code:
static void ali_setwrpage(int page)
{
    last_page = page;
    __svgalib_port_out(page, 0x3d7);
}


decompiled_code:
__int64 __fastcall ali_setrdpage(__int64 a1)
{
return _svgalib_port_out(a1, 982LL);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
return _svgalib_port_out(a1, 982LL);
}

original_code:
static void ali_setrdpage(int page)
{
    last_page = page;
    __svgalib_port_out(page, 0x3d6);
}


SETB

decompiled_code:
__int64 __fastcall SETB(unsigned int a1, unsigned int a2, unsigned int a3)
{
if ( a2 != -1 )
_svgalib_port_out(a2, a1);
return _svgalib_port_out(a3, a1 + (a2 != -1));
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned int a1, unsigned int a2, unsigned int a3)
{
if ( a2 != -1 )
_svgalib_port_out(a2, a1);
return _svgalib_port_out(a3, a1 + (a2 != -1));
}

original_code:
static void SETB(int port, int idx, int value)
{
    if (idx != -1)
 __svgalib_port_out(idx, port);
    __svgalib_port_out(value, port + (idx != -1));
}

variable_types: port int

variable_types: idx int

variable_types: value int


GETB

decompiled_code:
__int64 __fastcall GETB(unsigned int a1, unsigned int a2)
{
if ( a2 != -1 )
_svgalib_port_out(a2, a1);
return _svgalib_port_in(a1 + (a2 != -1));
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned int a1, unsigned int a2)
{
if ( a2 != -1 )
_svgalib_port_out(a2, a1);
return _svgalib_port_in(a1 + (a2 != -1));
}

original_code:
static int GETB(int port, int idx)
{
    if (idx != -1)
 __svgalib_port_out(idx, port);
    return __svgalib_port_in(port + (idx != -1));
}


decompiled_code:
__int64 ali_memorydetect()
{
int v0; // eax
unsigned int v1; // edx

v0 = GETB(_svgalib_CRT_I, 0x1Eu) & 3;
if ( v0 == 2 )
return 1024;
v1 = 2048;
if ( v0 <= 2 )
{
v1 = 256;
if ( v0 )
v1 = 512;
}
return v1;
}

original_code:
static int ali_memorydetect(void)
{
    int tmp;
    tmp = GETB(__svgalib_CRT_I, 0x1e);
    switch (tmp & 3) {
    case 0:
 return 256;
    case 1:
 return 512;
    case 2:
 return 1024;
    case 3:
 return 2048;
    default:
 fprintf(stderr,"ALI driver: More than 2MB installed. Using 2MB.\n");
 return 2048;
    }
}

canonical_original_code:
static int ali_memorydetect()
{
    int tmp;
    tmp = GETB(__svgalib_CRT_I, 0x1e);
    switch (tmp & 3) {
    case 0:
 return 256;
    case 1:
 return 512;
    case 2:
 return 1024;
    case 3:
 return 2048;
    default:
 fprintf(stderr,"ALI driver: More than 2MB installed. Using 2MB.\n");
 return 2048;
    }
}

variable_types: tmp int


decompiled_code:
__int64 __fastcall ali_init(int a1, __int64 a2, int a3)
{
if ( a1 )
ali_memory = a3;
else
ali_memory = ali_memorydetect();
if ( _svgalib_driver_report )
fprintf(stderr, "Using ALI driver (ALI2301, %dK).\n", (unsigned int)ali_memory);
_svgalib_driverspecs = _svgalib_ali_driverspecs;
_svgalib_infotable[98] = 2048;
_svgalib_banked_mem_base = 655360LL;
_svgalib_banked_mem_size = 0x10000LL;
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func10(int a1, __int64 a2, int a3)
{
if ( a1 )
ali_memory = a3;
else
ali_memory = func9();
if ( _svgalib_driver_report )
fprintf(stderr, "Using ALI driver (ALI2301, %dK).\n", (unsigned int)ali_memory);
_svgalib_driverspecs = _svgalib_ali_driverspecs;
_svgalib_infotable[98] = 2048;
_svgalib_banked_mem_base = 655360LL;
_svgalib_banked_mem_size = 0x10000LL;
return 1LL;
}

original_code:
static int ali_init(int force, int par1, int par2)
{
    if (force) {
 ali_memory = par2;
    } else {
 ali_memory = ali_memorydetect();
    }
    if (__svgalib_driver_report) {
 fprintf(stderr,"Using ALI driver (ALI2301, %dK).\n", ali_memory);
    }
    __svgalib_driverspecs = &__svgalib_ali_driverspecs;
    __svgalib_infotable[19].xbytes = 2048;
    __svgalib_banked_mem_base=0xa0000;
    __svgalib_banked_mem_size=0x10000;
    return 1;
}

variable_types: force int


decompiled_code:
__int64 ali_lock()
{
unsigned int v0; // eax
unsigned __int8 v1; // al

v0 = GETB(_svgalib_CRT_I, 0x11u);
LOBYTE(v0) = v0 | 0x80;
_svgalib_port_out(v0, _svgalib_CRT_D);
v1 = GETB(_svgalib_CRT_I, 0x1Au);
return _svgalib_port_out(v1 & 0xEF, _svgalib_CRT_D);
}

canonical_decompiled_code:
__int64 func11()
{
unsigned int v0; // eax
unsigned __int8 v1; // al

v0 = func8(_svgalib_CRT_I, 0x11u);
LOBYTE(v0) = v0 | 0x80;
_svgalib_port_out(v0, _svgalib_CRT_D);
v1 = func8(_svgalib_CRT_I, 0x1Au);
return _svgalib_port_out(v1 & 0xEF, _svgalib_CRT_D);
}

original_code:
static void ali_lock(void)
{
    int tmp;
    tmp = GETB(__svgalib_CRT_I, 0x11);
    __svgalib_port_out(tmp | 0x80, __svgalib_CRT_D);
    tmp = GETB(__svgalib_CRT_I, 0x1a);
    __svgalib_port_out(tmp & 0xef, __svgalib_CRT_D);
}

canonical_original_code:
static void ali_lock()
{
    int tmp;
    tmp = GETB(__svgalib_CRT_I, 0x11);
    __svgalib_port_out(tmp | 0x80, __svgalib_CRT_D);
    tmp = GETB(__svgalib_CRT_I, 0x1a);
    __svgalib_port_out(tmp & 0xef, __svgalib_CRT_D);
}


decompiled_code:
__int64 ali_unlock()
{
int v0; // eax
char v1; // al

v0 = GETB(_svgalib_CRT_I, 0x1Au);
_svgalib_port_out(v0 | 0x10u, _svgalib_CRT_D);
v1 = GETB(_svgalib_CRT_I, 0x11u);
return _svgalib_port_out(v1 & 0x7F, _svgalib_CRT_D);
}

canonical_decompiled_code:
__int64 func12()
{
int v0; // eax
char v1; // al

v0 = func8(_svgalib_CRT_I, 0x1Au);
_svgalib_port_out(v0 | 0x10u, _svgalib_CRT_D);
v1 = func8(_svgalib_CRT_I, 0x11u);
return _svgalib_port_out(v1 & 0x7F, _svgalib_CRT_D);
}

original_code:
static void ali_unlock(void)
{
    int temp;
    temp = GETB(__svgalib_CRT_I, 0x1a);
    __svgalib_port_out(temp | 0x10, __svgalib_CRT_D);
    temp = GETB(__svgalib_CRT_I, 0x11);
    __svgalib_port_out(temp & 0x7f, __svgalib_CRT_D);
}

canonical_original_code:
static void ali_unlock()
{
    int temp;
    temp = GETB(__svgalib_CRT_I, 0x1a);
    __svgalib_port_out(temp | 0x10, __svgalib_CRT_D);
    temp = GETB(__svgalib_CRT_I, 0x11);
    __svgalib_port_out(temp & 0x7f, __svgalib_CRT_D);
}

variable_types: temp int


cwg

decompiled_code:
_BOOL8 cwg()
{
unsigned int v0; // er13
unsigned int v1; // ebp
char v2; // bl

v0 = _svgalib_port_in(974LL);
v1 = GETB(0x3CEu, 0xBu);
_svgalib_port_out(v1 & 0x30 ^ 0x30, 975LL);
v2 = _svgalib_port_in(975LL);
_svgalib_port_out(v1, 975LL);
_svgalib_port_out(v0, 974LL);
return (~v2 & 0x30) == (v1 & 0x30);
}

canonical_decompiled_code:
_BOOL8 func13()
{
unsigned int v0; // er13
unsigned int v1; // ebp
char v2; // bl

v0 = _svgalib_port_in(974LL);
v1 = func8(0x3CEu, 0xBu);
_svgalib_port_out(v1 & 0x30 ^ 0x30, 975LL);
v2 = _svgalib_port_in(975LL);
_svgalib_port_out(v1, 975LL);
_svgalib_port_out(v0, 974LL);
return (~v2 & 0x30) == (v1 & 0x30);
}

original_code:
static int cwg(void)
{
    int t, t1, t2, t3;
    t = __svgalib_port_in(0x3CE);
    t1 = GETB(0x3CE, 0x0b);
    t2 = t1 & 0x30;
    __svgalib_port_out(t2 ^ 0x30, 0x3CF);
    t3 = ((__svgalib_port_in(0x3CF) & 0x30) ^ 0x30);
    __svgalib_port_out(t1, 0x3CF);
    __svgalib_port_out(t, 0x3CE);
    return (t2 == t3);
}

canonical_original_code:
static int cwg()
{
    int t; int t1; int t2; int t3;
    t = __svgalib_port_in(0x3CE);
    t1 = GETB(0x3CE, 0x0b);
    t2 = t1 & 0x30;
    __svgalib_port_out(t2 ^ 0x30, 0x3CF);
    t3 = ((__svgalib_port_in(0x3CF) & 0x30) ^ 0x30);
    __svgalib_port_out(t1, 0x3CF);
    __svgalib_port_out(t, 0x3CE);
    return (t2 == t3);
}

variable_types: t int