decompiled_code:
void __fastcall bzone_sound_update(__int64 a1, __int64 a2, unsigned int a3)
{
int v4; // edi
int v5; // ecx
int v6; // er8
int v7; // er12
int v8; // esi
int v9; // er15
int v10; // er10
int v11; // ebx
int v12; // er9
int v13; // ebp
int v14; // er13
int v15; // xmm8_4
int v16; // xmm9_4
int v17; // er11
_BOOL4 i; // esi
unsigned int v19; // eax
char v20; // dl
int v21; // eax
int v22; // er11
int v23; // er14
char *v24; // rax
int v25; // eax
int v26; // er11
int v27; // edx
int v28; // xmm1_4
int v29; // xmm0_4
int v30; // eax
int v31; // ebp
int v32; // eax
char *v33; // rax
int v34; // eax
int v35; // edx
unsigned int v36; // eax
int v37; // er14
int v38; // eax
int v39; // eax
int v40; // eax
bool v41; // sf
int v42; // er11
int v43; // eax
int v44; // eax
int v45; // edi
__int64 v46; // [rsp+0h] [rbp-A0h]
int v47; // [rsp+Ch] [rbp-94h]
__int64 v48; // [rsp+10h] [rbp-90h]
int v49; // [rsp+18h] [rbp-88h]
int v50; // [rsp+1Ch] [rbp-84h]
char *v51; // [rsp+20h] [rbp-80h]
int v52; // [rsp+28h] [rbp-78h]
int v53; // [rsp+2Ch] [rbp-74h]
__int64 v54; // [rsp+30h] [rbp-70h]
int v55; // [rsp+38h] [rbp-68h]
int v56; // [rsp+3Ch] [rbp-64h]
int v57; // [rsp+40h] [rbp-60h]
int v58; // [rsp+44h] [rbp-5Ch]
int v59; // [rsp+44h] [rbp-5Ch]
char v60; // [rsp+48h] [rbp-58h]
char v61; // [rsp+49h] [rbp-57h]
char v62; // [rsp+4Ah] [rbp-56h]
char v63; // [rsp+4Bh] [rbp-55h]
int v64; // [rsp+4Ch] [rbp-54h]
int v65; // [rsp+50h] [rbp-50h]
int v66; // [rsp+54h] [rbp-4Ch]
int v67; // [rsp+58h] [rbp-48h]
int v68; // [rsp+5Ch] [rbp-44h]
char v69; // [rsp+60h] [rbp-40h]
int v70; // [rsp+60h] [rbp-40h]
char v71; // [rsp+64h] [rbp-3Ch]
char v72; // [rsp+65h] [rbp-3Bh]
char v73; // [rsp+66h] [rbp-3Ah]
char v74; // [rsp+67h] [rbp-39h]
int v75; // [rsp+68h] [rbp-38h]
char v76; // [rsp+6Ch] [rbp-34h]
char v77; // [rsp+6Dh] [rbp-33h]
char v78; // [rsp+6Eh] [rbp-32h]
char v79; // [rsp+6Fh] [rbp-31h]

v46 = a2;
if ( !a3 )
return;
v4 = poly_counter;
v5 = poly_shift;
v6 = explosion_clock;
v7 = explosion_out;
v8 = shell_clock;
v9 = shell_out;
v48 = Machine;
v10 = motor_counter_a;
v52 = latch & 1;
v57 = explosion_amp_counter;
v49 = explosion_amp;
v64 = latch & 2;
v51 = (char *)discharge;
v53 = latch & 4;
v58 = shell_amp_counter;
v50 = shell_amp;
v65 = latch & 8;
v55 = latch & 0x80;
v11 = motor_counter;
v75 = (latch & 0x10) == 0 ? 184 : 240;
v67 = motor_rate_counter;
v56 = motor_rate;
v12 = motor_counter_b;
v76 = 0;
v74 = 0;
v13 = motor_amp_new;
v68 = motor_amp_step;
v71 = 0;
v14 = motor_amp;
v66 = motor_amp_counter;
v73 = 0;
v47 = last_val_0;
v15 = r0_2;
v54 = a2 + 2LL * a3;
v63 = 0;
v16 = r1_1;
v61 = 0;
v60 = 0;
v62 = 0;
v79 = 0;
v78 = 0;
v77 = 0;
v72 = 0;
v69 = 0;
do
{
v4 -= 6000;
if ( v4 <= 0 )
{
v17 = v8;
for ( i = v6; ; i = v21 != 28672 )
{
v19 = v5;
v20 = v5;
v5 *= 2;
v4 += *(_DWORD *)(v48 + 836);
if ( ((v20 & 8) != 0) != (((unsigned __int8)(v19 >> 14) ^ 1) & 1) )
v5 |= 1u;
v21 = v5 & 0x7000;
v6 = v21 != 28672;
if ( v21 != 28672 && !i )
{
v60 = 1;
v7 ^= 1u;
}
v8 = (v5 >> 15) & 1;
if ( v8 && !v17 )
{
v61 = 1;
v9 ^= 1u;
}
if ( v4 > 0 )
break;
v17 = (v5 >> 15) & 1;
}
v62 = 1;
}
if ( v52 )
{
if ( !v7 )
{
v63 = 1;
v22 = 0;
v49 = 0x7FFF;
goto LABEL_19;
}
v63 = 1;
v49 = 0x7FFF;
}
else
{
v22 = 0;
if ( !v7 )
goto LABEL_19;
if ( v49 <= 0 )
{
v33 = &v51[2 * v49];
goto LABEL_78;
}
}
if ( v57 - 35616 < 0 )
{
v42 = *(_DWORD *)(v48 + 836);
v43 = (35616 - v57) / v42 + 1;
v41 = v49 - v43 < 0;
v49 -= v43;
v57 = v43 * v42 + v57 - 35616;
v73 = 1;
if ( v41 )
{
v33 = v51;
v63 = 1;
v49 = 0;
}
else
{
v63 = 1;
v33 = &v51[2 * v49];
}
LABEL_78:
v34 = *(__int16 *)v33;
if ( v64 )
goto LABEL_79;
goto LABEL_83;
}
v57 -= 35616;
v73 = 1;
v34 = *(__int16 *)&v51[2 * v49];
if ( v64 )
{
LABEL_79:
v22 = v34 / 3;
goto LABEL_19;
}
LABEL_83:
v22 = v34 / 4;
LABEL_19:
if ( v53 )
{
v71 = 1;
v50 = 0x7FFF;
if ( !v9 )
goto LABEL_25;
}
else
{
if ( !v9 )
goto LABEL_25;
if ( v50 <= 0 )
{
v25 = *(__int16 *)&v51[2 * v50];
if ( v65 )
goto LABEL_24;
goto LABEL_58;
}
}
v23 = v58 - 75779;
if ( v58 - 75779 < 0 )
{
v39 = 75779 - v58;
v59 = *(_DWORD *)(v48 + 836);
v40 = v39 / v59 + 1;
v41 = v50 - v40 < 0;
v50 -= v40;
v58 = v23 + v40 * v59;
v74 = 1;
if ( v41 )
{
v24 = v51;
v71 = 1;
v50 = 0;
}
else
{
v71 = 1;
v24 = &v51[2 * v50];
}
}
else
{
v58 -= 75779;
v74 = 1;
v24 = &v51[2 * v50];
}
v25 = *(__int16 *)v24;
if ( v65 )
{
LABEL_24:
v22 += v25 / 3;
LABEL_25:
if ( !v55 )
goto LABEL_26;
goto LABEL_59;
}
LABEL_58:
v22 += v25 / 4;
if ( !v55 )
goto LABEL_26;
LABEL_59:
if ( v75 != v56 )
{
v67 -= 224;
v44 = v67;
if ( v67 > 0 )
{
v78 = 1;
}
else
{
v70 = v4;
v45 = v56;
do
{
v44 += *(_DWORD *)(v48 + 836);
v45 = v45 + 2 * (v75 > v45) - 1;
}
while ( v44 <= 0 );
v56 = v45;
v79 = 1;
v4 = v70;
v67 = v44;
v78 = 1;
}
}
v11 -= v56;
if ( v11 <= 0 )
{
while ( 1 )
{
++v10;
v11 += *(_DWORD *)(v48 + 836);
if ( v10 == 16 )
break;
++v12;
v31 = v10 & 8;
if ( v12 != 16 )
goto LABEL_68;
v32 = 0;
v12 = 4;
LABEL_69:
if ( v31 )
{
v28 = LC1;
v29 = LC0;
if ( v10 != 15 )
goto LABEL_71;
}
else
{
v28 = LC0;
v29 = LC1;
if ( v10 != 15 )
goto LABEL_71;
}
*(float *)&v28 = *(float *)&v28 + 0.0000303030303030303;
LABEL_72:
if ( v32 )
{
*(float *)&v28 = *(float *)&v28 + 0.0000303030303030303;
if ( v12 == 15 )
goto LABEL_74;
}
else
{
*(float *)&v29 = *(float *)&v29 + 0.0000303030303030303;
if ( v12 == 15 )
{
LABEL_74:
*(float *)&v28 = *(float *)&v28 + 0.0000303030303030303;
goto LABEL_64;
}
}
*(float *)&v29 = *(float *)&v29 + 0.0000303030303030303;
LABEL_64:
*(float *)&v15 = 1.0 / *(float *)&v29;
*(float *)&v16 = 1.0 / *(float *)&v28;
v13 = (int)(float)((float)(*(float *)&v15 * 32767.0) / (float)(*(float *)&v15 + (float)(1.0 / *(float *)&v28)));
if ( v13 <= v14 )
{
v30 = (int)((double)(v14 - v13) / (*(float *)&v15 * 0.00000047));
if ( v11 > 0 )
{
LABEL_85:
v68 = v30;
v72 = 1;
goto LABEL_86;
}
}
else
{
v30 = (int)((double)(v13 - v14) / (*(float *)&v16 * 0.00000047));
if ( v11 > 0 )
goto LABEL_85;
}
}
if ( ++v12 == 16 )
{
v10 = 6;
v29 = LC1;
v28 = LC0;
v32 = 0;
v12 = 4;
LABEL_71:
*(float *)&v29 = *(float *)&v29 + 0.0000303030303030303;
goto LABEL_72;
}
v31 = 0;
v10 = 6;
LABEL_68:
v32 = v12 & 8;
goto LABEL_69;
}
LABEL_86:
if ( v13 == v14 )
goto LABEL_100;
v35 = v66;
v66 -= v68;
if ( v66 < 0 )
{
v37 = *(_DWORD *)(v48 + 836);
v38 = (v68 - v35) / v37 + 1;
v66 += v38 * v37;
if ( v13 < v14 )
{
v14 -= v38;
v76 = 1;
v77 = 1;
if ( v14 < v13 )
v14 = v13;
goto LABEL_100;
}
v14 += v38;
if ( v14 > v13 )
{
v76 = 1;
v14 = v13;
v77 = 1;
LABEL_100:
v36 = *(__int16 *)&v51[2 * v14] / 3;
goto LABEL_90;
}
v77 = 1;
}
if ( v13 <= v14 )
{
v76 = 1;
goto LABEL_100;
}
v76 = 1;
v36 = (0x7FFF - *(__int16 *)&v51[2 * (0x7FFF - v14)]) / 3u;
LABEL_90:
v69 = 1;
v22 += v36;
LABEL_26:
v46 += 2LL;
v26 = v47 + v22;
v27 = v26 + 2 * v47;
*(_WORD *)(v46 - 2) = v26 / 2;
v47 = v27 / 4;
}
while ( v46 != v54 );
poly_counter = v4;
if ( v69 )
motor_counter = v11;
if ( v72 )
{
motor_counter_a = v10;
motor_counter_b = v12;
motor_amp_new = v13;
r0_2 = v15;
r1_1 = v16;
if ( v77 )
motor_amp = v14;
motor_amp_step = v68;
}
else if ( v77 )
{
motor_amp = v14;
}
if ( v78 )
motor_rate_counter = v67;
if ( v79 )
motor_rate = v56;
if ( v62 )
{
poly_shift = v5;
explosion_clock = v6;
if ( v60 )
explosion_out = v7;
shell_clock = v8;
}
else if ( v60 )
{
explosion_out = v7;
}
if ( v61 )
shell_out = v9;
if ( v63 )
explosion_amp = v49;
if ( v73 )
explosion_amp_counter = v57;
if ( v71 )
shell_amp = v50;
if ( v74 )
shell_amp_counter = v58;
if ( v76 )
motor_amp_counter = v66;
last_val_0 = v27 / 4;
}

canonical_decompiled_code:
void __fastcall func0(__int64 a1, __int64 a2, unsigned int a3)
{
int v4; // edi
int v5; // ecx
int v6; // er8
int v7; // er12
int v8; // esi
int v9; // er15
int v10; // er10
int v11; // ebx
int v12; // er9
int v13; // ebp
int v14; // er13
int v15; // xmm8_4
int v16; // xmm9_4
int v17; // er11
_BOOL4 i; // esi
unsigned int v19; // eax
char v20; // dl
int v21; // eax
int v22; // er11
int v23; // er14
char *v24; // rax
int v25; // eax
int v26; // er11
int v27; // edx
int v28; // xmm1_4
int v29; // xmm0_4
int v30; // eax
int v31; // ebp
int v32; // eax
char *v33; // rax
int v34; // eax
int v35; // edx
unsigned int v36; // eax
int v37; // er14
int v38; // eax
int v39; // eax
int v40; // eax
bool v41; // sf
int v42; // er11
int v43; // eax
int v44; // eax
int v45; // edi
__int64 v46; // [rsp+0h] [rbp-A0h]
int v47; // [rsp+Ch] [rbp-94h]
__int64 v48; // [rsp+10h] [rbp-90h]
int v49; // [rsp+18h] [rbp-88h]
int v50; // [rsp+1Ch] [rbp-84h]
char *v51; // [rsp+20h] [rbp-80h]
int v52; // [rsp+28h] [rbp-78h]
int v53; // [rsp+2Ch] [rbp-74h]
__int64 v54; // [rsp+30h] [rbp-70h]
int v55; // [rsp+38h] [rbp-68h]
int v56; // [rsp+3Ch] [rbp-64h]
int v57; // [rsp+40h] [rbp-60h]
int v58; // [rsp+44h] [rbp-5Ch]
int v59; // [rsp+44h] [rbp-5Ch]
char v60; // [rsp+48h] [rbp-58h]
char v61; // [rsp+49h] [rbp-57h]
char v62; // [rsp+4Ah] [rbp-56h]
char v63; // [rsp+4Bh] [rbp-55h]
int v64; // [rsp+4Ch] [rbp-54h]
int v65; // [rsp+50h] [rbp-50h]
int v66; // [rsp+54h] [rbp-4Ch]
int v67; // [rsp+58h] [rbp-48h]
int v68; // [rsp+5Ch] [rbp-44h]
char v69; // [rsp+60h] [rbp-40h]
int v70; // [rsp+60h] [rbp-40h]
char v71; // [rsp+64h] [rbp-3Ch]
char v72; // [rsp+65h] [rbp-3Bh]
char v73; // [rsp+66h] [rbp-3Ah]
char v74; // [rsp+67h] [rbp-39h]
int v75; // [rsp+68h] [rbp-38h]
char v76; // [rsp+6Ch] [rbp-34h]
char v77; // [rsp+6Dh] [rbp-33h]
char v78; // [rsp+6Eh] [rbp-32h]
char v79; // [rsp+6Fh] [rbp-31h]

v46 = a2;
if ( !a3 )
return;
v4 = poly_counter;
v5 = poly_shift;
v6 = explosion_clock;
v7 = explosion_out;
v8 = shell_clock;
v9 = shell_out;
v48 = Machine;
v10 = motor_counter_a;
v52 = latch & 1;
v57 = explosion_amp_counter;
v49 = explosion_amp;
v64 = latch & 2;
v51 = (char *)discharge;
v53 = latch & 4;
v58 = shell_amp_counter;
v50 = shell_amp;
v65 = latch & 8;
v55 = latch & 0x80;
v11 = motor_counter;
v75 = (latch & 0x10) == 0 ? 184 : 240;
v67 = motor_rate_counter;
v56 = motor_rate;
v12 = motor_counter_b;
v76 = 0;
v74 = 0;
v13 = motor_amp_new;
v68 = motor_amp_step;
v71 = 0;
v14 = motor_amp;
v66 = motor_amp_counter;
v73 = 0;
v47 = last_val_0;
v15 = r0_2;
v54 = a2 + 2LL * a3;
v63 = 0;
v16 = r1_1;
v61 = 0;
v60 = 0;
v62 = 0;
v79 = 0;
v78 = 0;
v77 = 0;
v72 = 0;
v69 = 0;
do
{
v4 -= 6000;
if ( v4 <= 0 )
{
v17 = v8;
for ( i = v6; ; i = v21 != 28672 )
{
v19 = v5;
v20 = v5;
v5 *= 2;
v4 += *(_DWORD *)(v48 + 836);
if ( ((v20 & 8) != 0) != (((unsigned __int8)(v19 >> 14) ^ 1) & 1) )
v5 |= 1u;
v21 = v5 & 0x7000;
v6 = v21 != 28672;
if ( v21 != 28672 && !i )
{
v60 = 1;
v7 ^= 1u;
}
v8 = (v5 >> 15) & 1;
if ( v8 && !v17 )
{
v61 = 1;
v9 ^= 1u;
}
if ( v4 > 0 )
break;
v17 = (v5 >> 15) & 1;
}
v62 = 1;
}
if ( v52 )
{
if ( !v7 )
{
v63 = 1;
v22 = 0;
v49 = 0x7FFF;
goto LABEL_19;
}
v63 = 1;
v49 = 0x7FFF;
}
else
{
v22 = 0;
if ( !v7 )
goto LABEL_19;
if ( v49 <= 0 )
{
v33 = &v51[2 * v49];
goto LABEL_78;
}
}
if ( v57 - 35616 < 0 )
{
v42 = *(_DWORD *)(v48 + 836);
v43 = (35616 - v57) / v42 + 1;
v41 = v49 - v43 < 0;
v49 -= v43;
v57 = v43 * v42 + v57 - 35616;
v73 = 1;
if ( v41 )
{
v33 = v51;
v63 = 1;
v49 = 0;
}
else
{
v63 = 1;
v33 = &v51[2 * v49];
}
LABEL_78:
v34 = *(__int16 *)v33;
if ( v64 )
goto LABEL_79;
goto LABEL_83;
}
v57 -= 35616;
v73 = 1;
v34 = *(__int16 *)&v51[2 * v49];
if ( v64 )
{
LABEL_79:
v22 = v34 / 3;
goto LABEL_19;
}
LABEL_83:
v22 = v34 / 4;
LABEL_19:
if ( v53 )
{
v71 = 1;
v50 = 0x7FFF;
if ( !v9 )
goto LABEL_25;
}
else
{
if ( !v9 )
goto LABEL_25;
if ( v50 <= 0 )
{
v25 = *(__int16 *)&v51[2 * v50];
if ( v65 )
goto LABEL_24;
goto LABEL_58;
}
}
v23 = v58 - 75779;
if ( v58 - 75779 < 0 )
{
v39 = 75779 - v58;
v59 = *(_DWORD *)(v48 + 836);
v40 = v39 / v59 + 1;
v41 = v50 - v40 < 0;
v50 -= v40;
v58 = v23 + v40 * v59;
v74 = 1;
if ( v41 )
{
v24 = v51;
v71 = 1;
v50 = 0;
}
else
{
v71 = 1;
v24 = &v51[2 * v50];
}
}
else
{
v58 -= 75779;
v74 = 1;
v24 = &v51[2 * v50];
}
v25 = *(__int16 *)v24;
if ( v65 )
{
LABEL_24:
v22 += v25 / 3;
LABEL_25:
if ( !v55 )
goto LABEL_26;
goto LABEL_59;
}
LABEL_58:
v22 += v25 / 4;
if ( !v55 )
goto LABEL_26;
LABEL_59:
if ( v75 != v56 )
{
v67 -= 224;
v44 = v67;
if ( v67 > 0 )
{
v78 = 1;
}
else
{
v70 = v4;
v45 = v56;
do
{
v44 += *(_DWORD *)(v48 + 836);
v45 = v45 + 2 * (v75 > v45) - 1;
}
while ( v44 <= 0 );
v56 = v45;
v79 = 1;
v4 = v70;
v67 = v44;
v78 = 1;
}
}
v11 -= v56;
if ( v11 <= 0 )
{
while ( 1 )
{
++v10;
v11 += *(_DWORD *)(v48 + 836);
if ( v10 == 16 )
break;
++v12;
v31 = v10 & 8;
if ( v12 != 16 )
goto LABEL_68;
v32 = 0;
v12 = 4;
LABEL_69:
if ( v31 )
{
v28 = LC1;
v29 = LC0;
if ( v10 != 15 )
goto LABEL_71;
}
else
{
v28 = LC0;
v29 = LC1;
if ( v10 != 15 )
goto LABEL_71;
}
*(float *)&v28 = *(float *)&v28 + 0.0000303030303030303;
LABEL_72:
if ( v32 )
{
*(float *)&v28 = *(float *)&v28 + 0.0000303030303030303;
if ( v12 == 15 )
goto LABEL_74;
}
else
{
*(float *)&v29 = *(float *)&v29 + 0.0000303030303030303;
if ( v12 == 15 )
{
LABEL_74:
*(float *)&v28 = *(float *)&v28 + 0.0000303030303030303;
goto LABEL_64;
}
}
*(float *)&v29 = *(float *)&v29 + 0.0000303030303030303;
LABEL_64:
*(float *)&v15 = 1.0 / *(float *)&v29;
*(float *)&v16 = 1.0 / *(float *)&v28;
v13 = (int)(float)((float)(*(float *)&v15 * 32767.0) / (float)(*(float *)&v15 + (float)(1.0 / *(float *)&v28)));
if ( v13 <= v14 )
{
v30 = (int)((double)(v14 - v13) / (*(float *)&v15 * 0.00000047));
if ( v11 > 0 )
{
LABEL_85:
v68 = v30;
v72 = 1;
goto LABEL_86;
}
}
else
{
v30 = (int)((double)(v13 - v14) / (*(float *)&v16 * 0.00000047));
if ( v11 > 0 )
goto LABEL_85;
}
}
if ( ++v12 == 16 )
{
v10 = 6;
v29 = LC1;
v28 = LC0;
v32 = 0;
v12 = 4;
LABEL_71:
*(float *)&v29 = *(float *)&v29 + 0.0000303030303030303;
goto LABEL_72;
}
v31 = 0;
v10 = 6;
LABEL_68:
v32 = v12 & 8;
goto LABEL_69;
}
LABEL_86:
if ( v13 == v14 )
goto LABEL_100;
v35 = v66;
v66 -= v68;
if ( v66 < 0 )
{
v37 = *(_DWORD *)(v48 + 836);
v38 = (v68 - v35) / v37 + 1;
v66 += v38 * v37;
if ( v13 < v14 )
{
v14 -= v38;
v76 = 1;
v77 = 1;
if ( v14 < v13 )
v14 = v13;
goto LABEL_100;
}
v14 += v38;
if ( v14 > v13 )
{
v76 = 1;
v14 = v13;
v77 = 1;
LABEL_100:
v36 = *(__int16 *)&v51[2 * v14] / 3;
goto LABEL_90;
}
v77 = 1;
}
if ( v13 <= v14 )
{
v76 = 1;
goto LABEL_100;
}
v76 = 1;
v36 = (0x7FFF - *(__int16 *)&v51[2 * (0x7FFF - v14)]) / 3u;
LABEL_90:
v69 = 1;
v22 += v36;
LABEL_26:
v46 += 2LL;
v26 = v47 + v22;
v27 = v26 + 2 * v47;
*(_WORD *)(v46 - 2) = v26 / 2;
v47 = v27 / 4;
}
while ( v46 != v54 );
poly_counter = v4;
if ( v69 )
motor_counter = v11;
if ( v72 )
{
motor_counter_a = v10;
motor_counter_b = v12;
motor_amp_new = v13;
r0_2 = v15;
r1_1 = v16;
if ( v77 )
motor_amp = v14;
motor_amp_step = v68;
}
else if ( v77 )
{
motor_amp = v14;
}
if ( v78 )
motor_rate_counter = v67;
if ( v79 )
motor_rate = v56;
if ( v62 )
{
poly_shift = v5;
explosion_clock = v6;
if ( v60 )
explosion_out = v7;
shell_clock = v8;
}
else if ( v60 )
{
explosion_out = v7;
}
if ( v61 )
shell_out = v9;
if ( v63 )
explosion_amp = v49;
if ( v73 )
explosion_amp_counter = v57;
if ( v71 )
shell_amp = v50;
if ( v74 )
shell_amp_counter = v58;
if ( v76 )
motor_amp_counter = v66;
last_val_0 = v27 / 4;
}

original_code:
static void bzone_sound_update(int param, INT16 *buffer, int length)
{
 while( length-- )
 {
  static int last_val = 0;
  int sum = 0;
  poly_counter -= 6000;
  while( poly_counter <= 0 )
  {
   int clock;
   poly_counter += Machine->sample_rate;
   if( ((poly_shift & 0x0008) == 0) == ((poly_shift & 0x4000) == 0) )
    poly_shift = (poly_shift << 1) | 1;
   else
    poly_shift <<= 1;
   clock = ((poly_shift & 0x7000) == 0x7000) ? 0 : 1;
   if( clock && !explosion_clock )
    explosion_out ^= 1;
   explosion_clock = clock;
   clock = (poly_shift >> 15) & 1;
   if( clock && !shell_clock )
    shell_out ^= 1;
   shell_clock = clock;
  }
  if( latch & 0x01 )
   explosion_amp = 32767;
  if( explosion_out )
  {
   if( explosion_amp > 0 )
   {
    explosion_amp_counter -= (int)(32767 / (0.23*4));
    if( explosion_amp_counter < 0 )
    {
     int n = (-explosion_amp_counter / Machine->sample_rate) + 1;
     explosion_amp_counter += n * Machine->sample_rate;
     if( (explosion_amp -= n) < 0 )
      explosion_amp = 0;
    }
   }
   if( latch & 0x02 )
    sum += (0 ? 0x7fff - discharge[0x7fff-explosion_amp] : discharge[explosion_amp])/3;
   else
    sum += (0 ? 0x7fff - discharge[0x7fff-explosion_amp] : discharge[explosion_amp])/4;
  }
  if( latch & 0x04 )
   shell_amp = 32767;
  if( shell_out )
  {
   if( shell_amp > 0 )
   {
    shell_amp_counter -= (int)(32767 / (0.1081*4));
    if( shell_amp_counter < 0 )
    {
     int n = (-shell_amp_counter / Machine->sample_rate) + 1;
     shell_amp_counter += n * Machine->sample_rate;
     if( (shell_amp -= n) < 0 )
      shell_amp = 0;
    }
   }
   if( latch & 0x08 )
    sum += (0 ? 0x7fff - discharge[0x7fff-shell_amp] : discharge[shell_amp])/3;
   else
    sum += (0 ? 0x7fff - discharge[0x7fff-shell_amp] : discharge[shell_amp])/4;
  }
  if( latch & 0x80 )
  {
   static float r0 = 1.0/1e12, r1 = 1.0/1e12;
   motor_rate_new = (latch & 0x10) ? 240 : 184;
   if( motor_rate != motor_rate_new )
   {
    motor_rate_counter -= (int)((240 - 184) / 0.25);
    while( motor_rate_counter <= 0 )
    {
     motor_rate_counter += Machine->sample_rate;
     motor_rate += (motor_rate < motor_rate_new) ? +1 : -1;
    }
   }
   motor_counter -= motor_rate;
   while( motor_counter <= 0 )
   {
    motor_counter += Machine->sample_rate;
    r0 = 1.0/1e12;
    r1 = 1.0/1e12;
    if( ++motor_counter_a == 16 )
     motor_counter_a = 6;
    if( ++motor_counter_b == 16 )
     motor_counter_b = 4;
    if( motor_counter_a & 8 )
     r1 += 1.0/33000;
    else
     r0 += 1.0/33000;
    if( motor_counter_a == 15 )
     r1 += 1.0/33000;
    else
     r0 += 1.0/33000;
    if( motor_counter_b & 8 )
     r1 += 1.0/33000;
    else
     r0 += 1.0/33000;
    if( motor_counter_b == 15 )
     r1 += 1.0/33000;
    else
     r0 += 1.0/33000;
    r0 = 1.0/r0;
    r1 = 1.0/r1;
    motor_amp_new = (int)(32767 * r0 / (r0 + r1));
    if( motor_amp_new > motor_amp )
     motor_amp_step = (int)((motor_amp_new - motor_amp) / (r1*0.47e-6));
    else
     motor_amp_step = (int)((motor_amp - motor_amp_new) / (r0*0.47e-6));
   }
   if( motor_amp != motor_amp_new )
   {
    motor_amp_counter -= motor_amp_step;
    if( motor_amp_counter < 0 )
    {
     int n = (-motor_amp_counter / Machine->sample_rate) + 1;
     motor_amp_counter += n * Machine->sample_rate;
     if( motor_amp > motor_amp_new )
     {
      motor_amp -= n;
      if( motor_amp < motor_amp_new )
       motor_amp = motor_amp_new;
     }
     else
     {
      motor_amp += n;
      if( motor_amp > motor_amp_new )
       motor_amp = motor_amp_new;
     }
    }
   }
   sum += ((motor_amp<motor_amp_new) ? 0x7fff - discharge[0x7fff-motor_amp] : discharge[motor_amp])/3;
  }
  *buffer++ = (sum + last_val) / 2;
  last_val = (sum + last_val * 3) / 4;
 }
}

canonical_original_code:
static void bzone_sound_update(int param, short *buffer, int length)
{
 while( length-- )
 {
  static int last_val = 0;
  int sum = 0;
  poly_counter -= 6000;
  while( poly_counter <= 0 )
  {
   int clock;
   poly_counter += Machine->sample_rate;
   if( ((poly_shift & 0x0008) == 0) == ((poly_shift & 0x4000) == 0) )
    poly_shift = (poly_shift << 1) | 1;
   else
    poly_shift <<= 1;
   clock = ((poly_shift & 0x7000) == 0x7000) ? 0 : 1;
   if( clock && !explosion_clock )
    explosion_out ^= 1;
   explosion_clock = clock;
   clock = (poly_shift >> 15) & 1;
   if( clock && !shell_clock )
    shell_out ^= 1;
   shell_clock = clock;
  }
  if( latch & 0x01 )
   explosion_amp = 32767;
  if( explosion_out )
  {
   if( explosion_amp > 0 )
   {
    explosion_amp_counter -= (int)(32767 / (0.23*4));
    if( explosion_amp_counter < 0 )
    {
     int n = (-explosion_amp_counter / Machine->sample_rate) + 1;
     explosion_amp_counter += n * Machine->sample_rate;
     if( (explosion_amp -= n) < 0 )
      explosion_amp = 0;
    }
   }
   if( latch & 0x02 )
    sum += (0 ? 0x7fff - discharge[0x7fff-explosion_amp] : discharge[explosion_amp])/3;
   else
    sum += (0 ? 0x7fff - discharge[0x7fff-explosion_amp] : discharge[explosion_amp])/4;
  }
  if( latch & 0x04 )
   shell_amp = 32767;
  if( shell_out )
  {
   if( shell_amp > 0 )
   {
    shell_amp_counter -= (int)(32767 / (0.1081*4));
    if( shell_amp_counter < 0 )
    {
     int n = (-shell_amp_counter / Machine->sample_rate) + 1;
     shell_amp_counter += n * Machine->sample_rate;
     if( (shell_amp -= n) < 0 )
      shell_amp = 0;
    }
   }
   if( latch & 0x08 )
    sum += (0 ? 0x7fff - discharge[0x7fff-shell_amp] : discharge[shell_amp])/3;
   else
    sum += (0 ? 0x7fff - discharge[0x7fff-shell_amp] : discharge[shell_amp])/4;
  }
  if( latch & 0x80 )
  {
   static float r0 = 1.0/1e12; static float r1 = 1.0/1e12;
   motor_rate_new = (latch & 0x10) ? 240 : 184;
   if( motor_rate != motor_rate_new )
   {
    motor_rate_counter -= (int)((240 - 184) / 0.25);
    while( motor_rate_counter <= 0 )
    {
     motor_rate_counter += Machine->sample_rate;
     motor_rate += (motor_rate < motor_rate_new) ? +1 : -1;
    }
   }
   motor_counter -= motor_rate;
   while( motor_counter <= 0 )
   {
    motor_counter += Machine->sample_rate;
    r0 = 1.0/1e12;
    r1 = 1.0/1e12;
    if( ++motor_counter_a == 16 )
     motor_counter_a = 6;
    if( ++motor_counter_b == 16 )
     motor_counter_b = 4;
    if( motor_counter_a & 8 )
     r1 += 1.0/33000;
    else
     r0 += 1.0/33000;
    if( motor_counter_a == 15 )
     r1 += 1.0/33000;
    else
     r0 += 1.0/33000;
    if( motor_counter_b & 8 )
     r1 += 1.0/33000;
    else
     r0 += 1.0/33000;
    if( motor_counter_b == 15 )
     r1 += 1.0/33000;
    else
     r0 += 1.0/33000;
    r0 = 1.0/r0;
    r1 = 1.0/r1;
    motor_amp_new = (int)(32767 * r0 / (r0 + r1));
    if( motor_amp_new > motor_amp )
     motor_amp_step = (int)((motor_amp_new - motor_amp) / (r1*0.47e-6));
    else
     motor_amp_step = (int)((motor_amp - motor_amp_new) / (r0*0.47e-6));
   }
   if( motor_amp != motor_amp_new )
   {
    motor_amp_counter -= motor_amp_step;
    if( motor_amp_counter < 0 )
    {
     int n = (-motor_amp_counter / Machine->sample_rate) + 1;
     motor_amp_counter += n * Machine->sample_rate;
     if( motor_amp > motor_amp_new )
     {
      motor_amp -= n;
      if( motor_amp < motor_amp_new )
       motor_amp = motor_amp_new;
     }
     else
     {
      motor_amp += n;
      if( motor_amp > motor_amp_new )
       motor_amp = motor_amp_new;
     }
    }
   }
   sum += ((motor_amp<motor_amp_new) ? 0x7fff - discharge[0x7fff-motor_amp] : discharge[motor_amp])/3;
  }
  *buffer++ = (sum + last_val) / 2;
  last_val = (sum + last_val * 3) / 4;
 }
}

variable_types: param int

variable_types:
buffer
short *

variable_types: length int

variable_types: sum int

variable_types: clock int


decompiled_code:
__int64 bzone_sh_update()
{
return stream_update((unsigned int)channel, 0LL);
}

canonical_decompiled_code:
__int64 func1()
{
return stream_update((unsigned int)channel, 0LL);
}

original_code:
void bzone_sh_update(void)
{
 stream_update(channel, 0);
}

canonical_original_code:
void bzone_sh_update()
{
 stream_update(channel, 0);
}

return_type: void


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

if ( latch != a2 )
{
stream_update((unsigned int)channel, 0LL);
latch = a2;
result = mixer_sound_enable_global_w(a2 & 0x20);
}
return result;
}

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

if ( latch != a2 )
{
stream_update((unsigned int)channel, 0LL);
latch = a2;
result = mixer_sound_enable_global_w(a2 & 0x20);
}
return result;
}

original_code:
void bzone_sounds_w(offs_t offset,data_t data)
{
 if( data == latch )
  return;
 stream_update(channel, 0);
    latch = data;
    mixer_sound_enable_global_w(latch & 0x20);
}

canonical_original_code:
void bzone_sounds_w(unsigned int offset, unsigned int data)
{
 if( data == latch )
  return;
 stream_update(channel, 0);
    latch = data;
    mixer_sound_enable_global_w(latch & 0x20);
}

variable_types:
offset
unsigned int

variable_types:
data
unsigned int


decompiled_code:
_BOOL8 bzone_sh_start()
{
char *v0; // rax
char *v1; // rbp
int i; // ebx
double v3; // xmm0_8

v0 = (char *)malloc(0x10000uLL);
discharge = v0;
if ( !v0 )
return 1LL;
v1 = v0 + 65534;
for ( i = 0; i != 0x8000; ++i )
{
v1 -= 2;
v3 = (double)i * 0.000244140625;
*((_WORD *)v1 + 1) = (int)(32767.0 / exp(v3));
}
channel = stream_init("Custom", 50LL, *(unsigned int *)(Machine + 836LL), 0LL, bzone_sound_update);
return channel == -1;
}

canonical_decompiled_code:
_BOOL8 func3()
{
char *v0; // rax
char *v1; // rbp
int i; // ebx
double v3; // xmm0_8

v0 = (char *)malloc(0x10000uLL);
discharge = v0;
if ( !v0 )
return 1LL;
v1 = v0 + 65534;
for ( i = 0; i != 0x8000; ++i )
{
v1 -= 2;
v3 = (double)i * 0.000244140625;
*((_WORD *)v1 + 1) = (int)(32767.0 / exp(v3));
}
channel = stream_init("Custom", 50LL, *(unsigned int *)(Machine + 836LL), 0LL, bzone_sound_update);
return channel == -1;
}

original_code:
int bzone_sh_start(const struct MachineSound *msound)
{
    int i;
 discharge = (INT16 *)malloc(32768 * sizeof(INT16));
 if( !discharge )
        return 1;
    for( i = 0; i < 0x8000; i++ )
  discharge[0x7fff-i] = (INT16) (0x7fff/exp(1.0*i/4096));
 channel = stream_init("Custom", 50, Machine->sample_rate, 0, bzone_sound_update);
    if( channel == -1 )
        return 1;
    return 0;
}

canonical_original_code:
int bzone_sh_start(struct MachineSound *msound)
{
    int i;
 discharge = (short *)malloc(32768 * sizeof(INT16));
 if( !discharge )
        return 1;
    for( i = 0; i < 0x8000; i++ )
  discharge[0x7fff-i] = (INT16) (0x7fff/exp(1.0*i/4096));
 channel = stream_init("Custom", 50, Machine->sample_rate, 0, bzone_sound_update);
    if( channel == -1 )
        return 1;
    return 0;
}

variable_types: struct MachineSound { int sound_type; void *sound_interface; } *

variable_types: i int

return_type: int


decompiled_code:
void bzone_sh_stop()
{
if ( discharge )
free(discharge);
discharge = 0LL;
}

canonical_decompiled_code:
void func4()
{
if ( discharge )
free(discharge);
discharge = 0LL;
}

original_code:
void bzone_sh_stop(void)
{
 if( discharge )
  free(discharge);
 discharge = ((void *)0);
}

canonical_original_code:
void bzone_sh_stop()
{
 if( discharge )
  free(discharge);
 discharge = ((void *)0);
}


decompiled_code:
__int64 bzone_IN3_r()
{
unsigned int v0; // ebx

v0 = readinputport(3LL);
return one_joy_trans[readinputport(4LL) & 0x1F] | v0;
}

canonical_decompiled_code:
__int64 func5()
{
unsigned int v0; // ebx

v0 = readinputport(3LL);
return one_joy_trans[readinputport(4LL) & 0x1F] | v0;
}

original_code:
static data_t bzone_IN3_r(offs_t offset)
{
 int res,res1;
 res=readinputport(3);
 res1=readinputport(4);
 res|=one_joy_trans[res1&0x1f];
 return (res);
}

canonical_original_code:
static unsigned int bzone_IN3_r(unsigned int offset)
{
 int res; int res1;
 res=readinputport(3);
 res1=readinputport(4);
 res|=one_joy_trans[res1&0x1f];
 return (res);
}

variable_types: res int

return_type: unsigned int


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

if ( rb_input_select )
result = readinputport(5LL);
else
result = readinputport(6LL);
return result;
}

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

if ( rb_input_select )
result = readinputport(5LL);
else
result = readinputport(6LL);
return result;
}

original_code:
static data_t redbaron_joy_r(offs_t offset)
{
 if (rb_input_select)
  return readinputport (5);
 else
  return readinputport (6);
}

canonical_original_code:
static unsigned int redbaron_joy_r(unsigned int offset)
{
 if (rb_input_select)
  return readinputport (5);
 else
  return readinputport (6);
}


decompiled_code:
__int64 bzone_IN0_r()
{
__int16 v0; // r8
int v1; // ebx
int v2; // eax
int v3; // er8
int v4; // eax
unsigned int v5; // ebx
__int64 result; // rax

v1 = readinputport(0LL);
v0 = cpu_gettotalcycles();
v2 = v1;
LOBYTE(v1) = v1 & 0x7F;
LOBYTE(v2) = v2 | 0x80;
if ( (v0 & 0x100) != 0 )
v1 = v2;
v3 = avgdvg_done();
v4 = v1;
v5 = v1 & 0xFFFFFFBF;
result = v4 | 0x40u;
if ( !v3 )
result = v5;
return result;
}

canonical_decompiled_code:
__int64 func7()
{
__int16 v0; // r8
int v1; // ebx
int v2; // eax
int v3; // er8
int v4; // eax
unsigned int v5; // ebx
__int64 result; // rax

v1 = readinputport(0LL);
v0 = cpu_gettotalcycles();
v2 = v1;
LOBYTE(v1) = v1 & 0x7F;
LOBYTE(v2) = v2 | 0x80;
if ( (v0 & 0x100) != 0 )
v1 = v2;
v3 = avgdvg_done();
v4 = v1;
v5 = v1 & 0xFFFFFFBF;
result = v4 | 0x40u;
if ( !v3 )
result = v5;
return result;
}

original_code:
data_t bzone_IN0_r(offs_t offset)
{
 int res;
 res = readinputport(0);
 if (cpu_gettotalcycles() & 0x100)
  res |= (1<<7);
 else
  res &= ~(1<<7);
 if (avgdvg_done())
  res |= (1<<6);
 else
  res &= ~(1<<6);
 return res;
}

canonical_original_code:
unsigned int bzone_IN0_r(unsigned int offset)
{
 int res;
 res = readinputport(0);
 if (cpu_gettotalcycles() & 0x100)
  res |= (1<<7);
 else
  res &= ~(1<<7);
 if (avgdvg_done())
  res |= (1<<6);
 else
  res &= ~(1<<6);
 return res;
}


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

if ( (readinputport(0LL) & 0x10) != 0 )
result = nmi_interrupt();
else
result = ignore_interrupt();
return result;
}

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

if ( (readinputport(0LL) & 0x10) != 0 )
result = nmi_interrupt();
else
result = ignore_interrupt();
return result;
}

original_code:
static int bzone_interrupt(void)
{
 if (readinputport(0) & 0x10)
  return nmi_interrupt();
 else
  return ignore_interrupt();
}

canonical_original_code:
static int bzone_interrupt()
{
 if (readinputport(0) & 0x10)
  return nmi_interrupt();
 else
  return ignore_interrupt();
}


decompiled_code:
void mnight_init_machine()
{
main_cpu_num = 0;
}

canonical_decompiled_code:
void func2()
{
main_cpu_num = 0;
}

original_code:
void mnight_init_machine(void)
{
 main_cpu_num = 0;
}


decompiled_code:
__int64 mnight_interrupt()
{
return 215LL;
}

canonical_decompiled_code:
__int64 func3()
{
return 215LL;
}

original_code:
int mnight_interrupt(void)
{
 return 0x00d7;
}

canonical_original_code:
int mnight_interrupt()
{
 return 0x00d7;
}


decompiled_code:
__int64 mnight_bankselect_r()
{
return (unsigned int)mnight_bank_latch;
}

canonical_decompiled_code:
__int64 func4()
{
return (unsigned int)mnight_bank_latch;
}

original_code:
data_t mnight_bankselect_r(offs_t offset)
{
 return mnight_bank_latch;
}

canonical_original_code:
unsigned int mnight_bankselect_r(unsigned int offset)
{
 return mnight_bank_latch;
}


decompiled_code:
__int64 mnight_vh_start()
{
_QWORD *v0; // rdx
unsigned __int64 v1; // rdi
__int64 v2; // rcx
int v3; // edx
__int64 v4; // rax
int v5; // edi
__int64 result; // rax

bg_dirtybuffer = malloc(0x400uLL);
if ( !bg_dirtybuffer )
return 1LL;
bitmap_bg = (void *)bitmap_alloc(
(unsigned int)(2 * *(_DWORD *)(*(_QWORD *)(Machine + 824LL) + 664LL)),
(unsigned int)(2 * *(_DWORD *)(*(_QWORD *)(Machine + 824LL) + 668LL)));
if ( bitmap_bg )
{
bitmap_sp = bitmap_alloc(
*(unsigned int *)(*(_QWORD *)(Machine + 824LL) + 664LL),
*(unsigned int *)(*(_QWORD *)(Machine + 824LL) + 668LL));
if ( bitmap_sp )
{
v0 = bg_dirtybuffer;
v1 = (unsigned __int64)bg_dirtybuffer + 8;
*(_QWORD *)bg_dirtybuffer = 0x101010101010101LL;
v0[127] = 0x101010101010101LL;
memset((void *)(v1 & 0xFFFFFFFFFFFFFFF8LL), 1u, 8 * (((unsigned int)v0 - (v1 & 0xFFFFFFF8) + 1024) >> 3));
memset(palette_used_colors, 3, *(unsigned int *)(*(_QWORD *)(Machine + 824LL) + 696LL));
v2 = Machine;
v3 = 0;
v4 = *(_QWORD *)(Machine + 520LL);
if ( *(int *)(v4 + 24) > 0 )
{
do
{
*((_BYTE *)palette_used_colors
+ v3 * *(_DWORD *)(v4 + 12)
+ *(_DWORD *)(*(_QWORD *)(*(_QWORD *)(v2 + 824) + 688LL) + 40LL)
+ 15) = 7;
v5 = v3 * *(_DWORD *)(*(_QWORD *)(Machine + 528LL) + 12LL);
++v3;
*((_BYTE *)palette_used_colors
+ v5
+ *(_DWORD *)(*(_QWORD *)(*(_QWORD *)(Machine + 824LL) + 688LL) + 64LL)
+ 15) = 7;
v2 = Machine;
v4 = *(_QWORD *)(Machine + 520LL);
}
while ( *(_DWORD *)(v4 + 24) > v3 );
}
result = 0LL;
}
else
{
free(bg_dirtybuffer);
free(bitmap_bg);
result = 1LL;
}
}
else
{
free(bg_dirtybuffer);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 func5()
{
_QWORD *v0; // rdx
unsigned __int64 v1; // rdi
__int64 v2; // rcx
int v3; // edx
__int64 v4; // rax
int v5; // edi
__int64 result; // rax

bg_dirtybuffer = malloc(0x400uLL);
if ( !bg_dirtybuffer )
return 1LL;
bitmap_bg = (void *)bitmap_alloc(
(unsigned int)(2 * *(_DWORD *)(*(_QWORD *)(Machine + 824LL) + 664LL)),
(unsigned int)(2 * *(_DWORD *)(*(_QWORD *)(Machine + 824LL) + 668LL)));
if ( bitmap_bg )
{
bitmap_sp = bitmap_alloc(
*(unsigned int *)(*(_QWORD *)(Machine + 824LL) + 664LL),
*(unsigned int *)(*(_QWORD *)(Machine + 824LL) + 668LL));
if ( bitmap_sp )
{
v0 = bg_dirtybuffer;
v1 = (unsigned __int64)bg_dirtybuffer + 8;
*(_QWORD *)bg_dirtybuffer = 0x101010101010101LL;
v0[127] = 0x101010101010101LL;
memset((void *)(v1 & 0xFFFFFFFFFFFFFFF8LL), 1u, 8 * (((unsigned int)v0 - (v1 & 0xFFFFFFF8) + 1024) >> 3));
memset(palette_used_colors, 3, *(unsigned int *)(*(_QWORD *)(Machine + 824LL) + 696LL));
v2 = Machine;
v3 = 0;
v4 = *(_QWORD *)(Machine + 520LL);
if ( *(int *)(v4 + 24) > 0 )
{
do
{
*((_BYTE *)palette_used_colors
+ v3 * *(_DWORD *)(v4 + 12)
+ *(_DWORD *)(*(_QWORD *)(*(_QWORD *)(v2 + 824) + 688LL) + 40LL)
+ 15) = 7;
v5 = v3 * *(_DWORD *)(*(_QWORD *)(Machine + 528LL) + 12LL);
++v3;
*((_BYTE *)palette_used_colors
+ v5
+ *(_DWORD *)(*(_QWORD *)(*(_QWORD *)(Machine + 824LL) + 688LL) + 64LL)
+ 15) = 7;
v2 = Machine;
v4 = *(_QWORD *)(Machine + 520LL);
}
while ( *(_DWORD *)(v4 + 24) > v3 );
}
result = 0LL;
}
else
{
free(bg_dirtybuffer);
free(bitmap_bg);
result = 1LL;
}
}
else
{
free(bg_dirtybuffer);
result = 1LL;
}
return result;
}

original_code:
int mnight_vh_start(void)
{
 int i;
 if ((bg_dirtybuffer = (unsigned char*)malloc(1024)) == 0)
 {
  return 1;
 }
 if ((bitmap_bg = bitmap_alloc (Machine->drv->screen_width*2,Machine->drv->screen_height*2)) == 0)
 {
  free(bg_dirtybuffer);
  return 1;
 }
 if ((bitmap_sp = bitmap_alloc (Machine->drv->screen_width,Machine->drv->screen_height)) == 0)
 {
  free(bg_dirtybuffer);
  free(bitmap_bg);
  return 1;
 }
 memset(bg_dirtybuffer,1,1024);
 memset(palette_used_colors,(1|2),Machine->drv->total_colors * sizeof(unsigned char));
 for (i = 0;i < Machine->gfx[1]->total_colors;i++)
 {
  palette_used_colors[Machine->drv->gfxdecodeinfo[1].color_codes_start + i * Machine->gfx[1]->color_granularity+15] = (4|(1|2));
  palette_used_colors[Machine->drv->gfxdecodeinfo[2].color_codes_start + i * Machine->gfx[2]->color_granularity+15] = (4|(1|2));
 }
 return 0;
}

canonical_original_code:
int mnight_vh_start()
{
 int i;
 if ((bg_dirtybuffer = (unsigned char *)malloc(1024)) == 0)
 {
  return 1;
 }
 if ((bitmap_bg = bitmap_alloc (Machine->drv->screen_width*2,Machine->drv->screen_height*2)) == 0)
 {
  free(bg_dirtybuffer);
  return 1;
 }
 if ((bitmap_sp = bitmap_alloc (Machine->drv->screen_width,Machine->drv->screen_height)) == 0)
 {
  free(bg_dirtybuffer);
  free(bitmap_bg);
  return 1;
 }
 memset(bg_dirtybuffer,1,1024);
 memset(palette_used_colors,(1|2),Machine->drv->total_colors * sizeof(unsigned char));
 for (i = 0;i < Machine->gfx[1]->total_colors;i++)
 {
  palette_used_colors[Machine->drv->gfxdecodeinfo[1].color_codes_start + i * Machine->gfx[1]->color_granularity+15] = (4|(1|2));
  palette_used_colors[Machine->drv->gfxdecodeinfo[2].color_codes_start + i * Machine->gfx[2]->color_granularity+15] = (4|(1|2));
 }
 return 0;
}


decompiled_code:
void mnight_vh_stop()
{
bitmap_free(bitmap_bg);
bitmap_free(bitmap_sp);
free(bg_dirtybuffer);
}

canonical_decompiled_code:
void func6()
{
bitmap_free(bitmap_bg);
bitmap_free(bitmap_sp);
free(bg_dirtybuffer);
}

original_code:
void mnight_vh_stop(void)
{
 bitmap_free(bitmap_bg);
 bitmap_free(bitmap_sp);
 free(bg_dirtybuffer);
}


decompiled_code:
void *__fastcall mnight_background_enable_w(unsigned int a1, int a2)
{
void *result; // rax

if ( bg_enable != a2 )
{
bg_enable = a2;
*(_BYTE *)(mnight_bgenable_ram + a1) = a2;
if ( a2 )
result = memset(bg_dirtybuffer, 1, mnight_backgroundram_size >> 1);
else
result = (void *)fillbitmap(bitmap_bg, palette_transparent_pen, 0LL);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func7(unsigned int a1, int a2)
{
void *result; // rax

if ( bg_enable != a2 )
{
bg_enable = a2;
*(_BYTE *)(mnight_bgenable_ram + a1) = a2;
if ( a2 )
result = memset(bg_dirtybuffer, 1, mnight_backgroundram_size >> 1);
else
result = (void *)fillbitmap(bitmap_bg, palette_transparent_pen, 0LL);
}
return result;
}

original_code:
void mnight_background_enable_w(offs_t offset,data_t data)
{
 if (bg_enable!=data)
 {
  mnight_bgenable_ram[offset] = data;
  bg_enable = data;
  if (bg_enable)
   memset(bg_dirtybuffer, 1, mnight_backgroundram_size / 2);
  else
   fillbitmap(bitmap_bg, palette_transparent_pen,0);
 }
}

canonical_original_code:
void mnight_background_enable_w(unsigned int offset, unsigned int data)
{
 if (bg_enable!=data)
 {
  mnight_bgenable_ram[offset] = data;
  bg_enable = data;
  if (bg_enable)
   memset(bg_dirtybuffer, 1, mnight_backgroundram_size / 2);
  else
   fillbitmap(bitmap_bg, palette_transparent_pen,0);
 }
}


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

if ( (a2 & 1) != sp_overdraw )
{
*(_BYTE *)(mnight_spoverdraw_ram + a1) = a2;
result = fillbitmap(bitmap_sp, 15LL, Machine + 776LL);
sp_overdraw = a2 & 1;
}
return result;
}

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

if ( (a2 & 1) != sp_overdraw )
{
*(_BYTE *)(mnight_spoverdraw_ram + a1) = a2;
result = fillbitmap(bitmap_sp, 15LL, Machine + 776LL);
sp_overdraw = a2 & 1;
}
return result;
}

original_code:
void mnight_sprite_overdraw_w(offs_t offset,data_t data)
{
 if (sp_overdraw != (data&1))
 {
  mnight_spoverdraw_ram[offset] = data;
  fillbitmap(bitmap_sp,15,&Machine->visible_area);
  sp_overdraw = data & 1;
 }
}

canonical_original_code:
void mnight_sprite_overdraw_w(unsigned int offset, unsigned int data)
{
 if (sp_overdraw != (data&1))
 {
  mnight_spoverdraw_ram[offset] = data;
  fillbitmap(bitmap_sp,15,&Machine->visible_area);
  sp_overdraw = data & 1;
 }
}


decompiled_code:
char *__fastcall mnight_bankselect_w(__int64 a1, int a2)
{
char *result; // rax
unsigned int v3; // eax

result = (char *)memory_region((unsigned int)(main_cpu_num + 129));
if ( mnight_bank_latch != a2 )
{
mnight_bank_latch = a2;
cpu_bankbase[1] = &result[0x4000 * (a2 & 7) + 0x10000];
result = (char *)&ophw;
if ( ophw == 1 )
{
ophw = -1;
v3 = cpu_get_pc();
result = (char *)cpu_setOPbase16(v3);
}
}
return result;
}

canonical_decompiled_code:
char *__fastcall func9(__int64 a1, int a2)
{
char *result; // rax
unsigned int v3; // eax

result = (char *)memory_region((unsigned int)(main_cpu_num + 129));
if ( mnight_bank_latch != a2 )
{
mnight_bank_latch = a2;
cpu_bankbase[1] = &result[0x4000 * (a2 & 7) + 0x10000];
result = (char *)&ophw;
if ( ophw == 1 )
{
ophw = -1;
v3 = cpu_get_pc();
result = (char *)cpu_setOPbase16(v3);
}
}
return result;
}

original_code:
void mnight_bankselect_w(offs_t offset,data_t data)
{
 unsigned char *RAM = memory_region(REGION_CPU1+main_cpu_num);
 int bankaddress;
 if ( data != mnight_bank_latch )
 {
  mnight_bank_latch = data;
  bankaddress = 0x10000 + ((data & 0x7) * 0x4000);
  { if (1 >= 1 && 1 <= 16) { cpu_bankbase[1] = (UINT8 *)(&RAM[bankaddress]); if (ophw == 1) { ophw = 0xff; cpu_setOPbase16(cpu_get_pc()); } } };
 }
}

canonical_original_code:
void mnight_bankselect_w(unsigned int offset, unsigned int data)
{
 unsigned char *RAM = memory_region(REGION_CPU1+main_cpu_num);
 int bankaddress;
 if ( data != mnight_bank_latch )
 {
  mnight_bank_latch = data;
  bankaddress = 0x10000 + ((data & 0x7) * 0x4000);
  { if (1 >= 1 && 1 <= 16) { cpu_bankbase[1] = (unsigned char *)(&RAM[bankaddress]); if (ophw == 1) { ophw = 0xff; cpu_setOPbase16(cpu_get_pc()); } } };
 }
}

variable_types:
RAM
unsigned char *

variable_types: bankaddress int


decompiled_code:
void *__fastcall junofrst_bankselect_w(__int64 a1, char a2)
{
void *result; // rax
unsigned int v3; // eax

cpu_bankbase[1] = memory_region(129LL) + (((a2 & 0xFu) + 16) << 12);
result = &ophw;
if ( ophw == 1 )
{
ophw = -1;
v3 = cpu_get_pc();
result = (void *)cpu_setOPbase16(v3);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func1(__int64 a1, char a2)
{
void *result; // rax
unsigned int v3; // eax

cpu_bankbase[1] = memory_region(129LL) + (((a2 & 0xFu) + 16) << 12);
result = &ophw;
if ( ophw == 1 )
{
ophw = -1;
v3 = cpu_get_pc();
result = (void *)cpu_setOPbase16(v3);
}
return result;
}

original_code:
void junofrst_bankselect_w(offs_t offset,data_t data)
{
 int bankaddress;
 unsigned char *RAM = memory_region(REGION_CPU1);
 bankaddress = 0x10000 + (data & 0x0f) * 0x1000;
 { if (1 >= 1 && 1 <= 16) { cpu_bankbase[1] = (UINT8 *)(&RAM[bankaddress]); if (ophw == 1) { ophw = 0xff; cpu_setOPbase16(cpu_get_pc()); } } };
}

canonical_original_code:
void junofrst_bankselect_w(unsigned int offset, unsigned int data)
{
 int bankaddress;
 unsigned char *RAM = memory_region(REGION_CPU1);
 bankaddress = 0x10000 + (data & 0x0f) * 0x1000;
 { if (1 >= 1 && 1 <= 16) { cpu_bankbase[1] = (unsigned char *)(&RAM[bankaddress]); if (ophw == 1) { ophw = 0xff; cpu_setOPbase16(cpu_get_pc()); } } };
}


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

result = (unsigned int)last_0;
if ( !last_0 && a2 == 1 )
{
result = cpu_cause_interrupt(1LL, 255LL);
last_0 = 1;
}
else
{
last_0 = a2;
}
return result;
}

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

result = (unsigned int)last_0;
if ( !last_0 && a2 == 1 )
{
result = cpu_cause_interrupt(1LL, 255LL);
last_0 = 1;
}
else
{
last_0 = a2;
}
return result;
}

original_code:
void junofrst_sh_irqtrigger_w(offs_t offset,data_t data)
{
 static int last;
 if (last == 0 && data == 1)
 {
  cpu_cause_interrupt(1,0xff);
 }
 last = data;
}

canonical_original_code:
void junofrst_sh_irqtrigger_w(unsigned int offset, unsigned int data)
{
 static int last;
 if (last == 0 && data == 1)
 {
  cpu_cause_interrupt(1,0xff);
 }
 last = data;
}

variable_types: last int


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

result = (unsigned int)i8039_irqenable;
if ( i8039_irqenable )
result = cpu_cause_interrupt(2LL, 1LL);
return result;
}

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

result = (unsigned int)i8039_irqenable;
if ( i8039_irqenable )
result = cpu_cause_interrupt(2LL, 1LL);
return result;
}

original_code:
void junofrst_i8039_irq_w(offs_t offset,data_t data)
{
 if (i8039_irqenable)
  cpu_cause_interrupt(2,1);
}

canonical_original_code:
void junofrst_i8039_irq_w(unsigned int offset, unsigned int data)
{
 if (i8039_irqenable)
  cpu_cause_interrupt(2,1);
}


decompiled_code:
// attributes: thunk
__int64 init_junofrst()
{
return konami1_decode();
}

canonical_decompiled_code:
__int64 func4()
{
return konami1_decode();
}

original_code:
static void init_junofrst(void)
{
 konami1_decode();
}

canonical_original_code:
static void init_junofrst()
{
 konami1_decode();
}


decompiled_code:
__int64 __fastcall junofrst_portB_w(__int64 a1, unsigned int a2)
{
unsigned int v2; // ebp
__int64 v4; // rdi
__int64 v5; // r8
__int64 result; // rax

v2 = 0;
do
{
v4 = v2;
v5 = -(a2 & 1) & 0xB798;
if ( (a2 & 2) != 0 )
v5 = (unsigned int)(v5 + 220000);
++v2;
a2 >>= 2;
result = set_RC_filter(v4, 1000LL, 2200LL, 200LL, v5);
}
while ( v2 != 3 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, unsigned int a2)
{
unsigned int v2; // ebp
__int64 v4; // rdi
__int64 v5; // r8
__int64 result; // rax

v2 = 0;
do
{
v4 = v2;
v5 = -(a2 & 1) & 0xB798;
if ( (a2 & 2) != 0 )
v5 = (unsigned int)(v5 + 220000);
++v2;
a2 >>= 2;
result = set_RC_filter(v4, 1000LL, 2200LL, 200LL, v5);
}
while ( v2 != 3 );
return result;
}

original_code:
static void junofrst_portB_w(offs_t offset,data_t data)
{
 int i;
 for (i = 0;i < 3;i++)
 {
  int C;
  C = 0;
  if (data & 1) C += 47000;
  if (data & 2) C += 220000;
  data >>= 2;
  set_RC_filter(i,1000,2200,200,C);
 }
}

canonical_original_code:
static void junofrst_portB_w(unsigned int offset, unsigned int data)
{
 int i;
 for (i = 0;i < 3;i++)
 {
  int C;
  C = 0;
  if (data & 1) C += 47000;
  if (data & 2) C += 220000;
  data >>= 2;
  set_RC_filter(i,1000,2200,200,C);
 }
}

variable_types: C int


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

v0 = cpu_gettotalcycles();
v1 = v0 + 511;
if ( v0 >= 0 )
v1 = v0;
return i8039_status | (v1 >> 5) & 0xF0;
}

canonical_decompiled_code:
__int64 func6()
{
int v0; // edx
unsigned int v1; // eax

v0 = cpu_gettotalcycles();
v1 = v0 + 511;
if ( v0 >= 0 )
v1 = v0;
return i8039_status | (v1 >> 5) & 0xF0;
}

original_code:
static data_t junofrst_portA_r(offs_t offset)
{
 int timer;
 timer = (cpu_gettotalcycles() / (1024/2)) & 0x0f;
 return (timer << 4) | i8039_status;
}

canonical_original_code:
static unsigned int junofrst_portA_r(unsigned int offset)
{
 int timer;
 timer = (cpu_gettotalcycles() / (1024/2)) & 0x0f;
 return (timer << 4) | i8039_status;
}

variable_types: timer int


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

result = (unsigned __int8)a2 & 0x80;
i8039_irqenable = a2 & 0x80;
i8039_status = (a2 >> 4) & 7;
return result;
}

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

result = (unsigned __int8)a2 & 0x80;
i8039_irqenable = a2 & 0x80;
i8039_status = (a2 >> 4) & 7;
return result;
}

original_code:
static void i8039_irqen_and_status_w(offs_t offset,data_t data)
{
 i8039_irqenable = data & 0x80;
 i8039_status = (data & 0x70) >> 4;
}

canonical_original_code:
static void i8039_irqen_and_status_w(unsigned int offset, unsigned int data)
{
 i8039_irqenable = data & 0x80;
 i8039_status = (data & 0x70) >> 4;
}


decompiled_code:
void __fastcall surpratk_videobank_w(__int64 a1, int a2)
{
videobank = a2;
}

canonical_decompiled_code:
void __fastcall func2(__int64 a1, int a2)
{
videobank = a2;
}

original_code:
static void surpratk_videobank_w(offs_t offset,data_t data)
{
 videobank = data;
}

canonical_original_code:
static void surpratk_videobank_w(unsigned int offset, unsigned int data)
{
 videobank = data;
}


decompiled_code:
__int64 surpratk_vh_start()
{
int v0; // er8
__int64 result; // rax

v0 = K052109_vh_start(137LL, 0LL, 1LL, 2LL, 3LL, tile_callback);
result = 1LL;
if ( !v0 )
{
result = K053245_vh_start(138LL, 0LL, 1LL, 2LL, 3LL, sprite_callback);
if ( (_DWORD)result )
{
K052109_vh_stop();
result = 1LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 func3()
{
int v0; // er8
__int64 result; // rax

v0 = K052109_vh_start(137LL, 0LL, 1LL, 2LL, 3LL, tile_callback);
result = 1LL;
if ( !v0 )
{
result = K053245_vh_start(138LL, 0LL, 1LL, 2LL, 3LL, sprite_callback);
if ( (_DWORD)result )
{
K052109_vh_stop();
result = 1LL;
}
}
return result;
}

original_code:
int surpratk_vh_start( void )
{
 if (K052109_vh_start(REGION_GFX1,0,1,2,3,tile_callback))
 {
  return 1;
 }
 if (K053245_vh_start(REGION_GFX2,0,1,2,3,sprite_callback))
 {
  K052109_vh_stop();
  return 1;
 }
 return 0;
}

canonical_original_code:
int surpratk_vh_start()
{
 if (K052109_vh_start(REGION_GFX1,0,1,2,3,tile_callback))
 {
  return 1;
 }
 if (K053245_vh_start(REGION_GFX2,0,1,2,3,sprite_callback))
 {
  K052109_vh_stop();
  return 1;
 }
 return 0;
}


decompiled_code:
__int64 surpratk_vh_stop()
{
K052109_vh_stop();
return K053245_vh_stop();
}

original_code:
void surpratk_vh_stop( void )
{
 K052109_vh_stop();
 K053245_vh_stop();
}

canonical_original_code:
void surpratk_vh_stop()
{
 K052109_vh_stop();
 K053245_vh_stop();
}


decompiled_code:
__int64 init_surpratk()
{
konami_rom_deinterleave_2(137LL);
return konami_rom_deinterleave_2(138LL);
}

canonical_decompiled_code:
__int64 func5()
{
konami_rom_deinterleave_2(137LL);
return konami_rom_deinterleave_2(138LL);
}

original_code:
static void init_surpratk(void)
{
 konami_rom_deinterleave_2(REGION_GFX1);
 konami_rom_deinterleave_2(REGION_GFX2);
}

canonical_original_code:
static void init_surpratk()
{
 konami_rom_deinterleave_2(REGION_GFX1);
 konami_rom_deinterleave_2(REGION_GFX2);
}


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

konami_cpu_setlines_callback = surpratk_banking;
result = memory_region(129LL) + 294912;
paletteram = result;
return result;
}

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

konami_cpu_setlines_callback = surpratk_banking;
result = memory_region(129LL) + 294912;
paletteram = result;
return result;
}

original_code:
static void surpratk_init_machine( void )
{
 konami_cpu_setlines_callback = surpratk_banking;
 paletteram = &memory_region(REGION_CPU1)[0x48000];
}

canonical_original_code:
static void surpratk_init_machine()
{
 konami_cpu_setlines_callback = surpratk_banking;
 paletteram = &memory_region(REGION_CPU1)[0x48000];
}


decompiled_code:
void *__fastcall surpratk_banking(char a1)
{
__int64 v1; // rax
int v2; // edi
void *result; // rax
unsigned int v4; // eax

v1 = memory_region(129LL);
v2 = ((a1 & 0x1F) + 8) << 13;
if ( v2 > 294911 )
v2 -= 0x40000;
cpu_bankbase[1] = v1 + v2;
result = &ophw;
if ( ophw == 1 )
{
ophw = -1;
v4 = cpu_get_pc();
result = (void *)cpu_setOPbase16(v4);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func8(char a1)
{
__int64 v1; // rax
int v2; // edi
void *result; // rax
unsigned int v4; // eax

v1 = memory_region(129LL);
v2 = ((a1 & 0x1F) + 8) << 13;
if ( v2 > 294911 )
v2 -= 0x40000;
cpu_bankbase[1] = v1 + v2;
result = &ophw;
if ( ophw == 1 )
{
ophw = -1;
v4 = cpu_get_pc();
result = (void *)cpu_setOPbase16(v4);
}
return result;
}

original_code:
static void surpratk_banking(int lines)
{
 unsigned char *RAM = memory_region(REGION_CPU1);
 int offs = 0;
 offs = 0x10000 + ((lines & 0x1f) * 0x2000);
 if (offs >= 0x48000) offs -= 0x40000;
 { if (1 >= 1 && 1 <= 16) { cpu_bankbase[1] = (UINT8 *)(&RAM[offs]); if (ophw == 1) { ophw = 0xff; cpu_setOPbase16(cpu_get_pc()); } } };
}

canonical_original_code:
static void surpratk_banking(int lines)
{
 unsigned char *RAM = memory_region(REGION_CPU1);
 int offs = 0;
 offs = 0x10000 + ((lines & 0x1f) * 0x2000);
 if (offs >= 0x48000) offs -= 0x40000;
 { if (1 >= 1 && 1 <= 16) { cpu_bankbase[1] = (unsigned char *)(&RAM[offs]); if (ophw == 1) { ophw = 0xff; cpu_setOPbase16(cpu_get_pc()); } } };
}

variable_types: lines int

variable_types: offs int


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

if ( (unsigned int)K052109_is_IRQ_enabled() )
result = interrupt();
else
result = ignore_interrupt();
return result;
}

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

if ( (unsigned int)K052109_is_IRQ_enabled() )
result = interrupt();
else
result = ignore_interrupt();
return result;
}

original_code:
static int surpratk_interrupt(void)
{
 if (K052109_is_IRQ_enabled()) return interrupt();
 else return ignore_interrupt();
}

canonical_original_code:
static int surpratk_interrupt()
{
 if (K052109_is_IRQ_enabled()) return interrupt();
 else return ignore_interrupt();
}


decompiled_code:
__int64 __fastcall surpratk_5fc0_w(__int64 a1, char a2)
{
coin_counter_w(0LL, a2 & 1);
coin_counter_w(1LL, a2 & 2);
return K052109_set_RMRD_line((a2 & 8) != 0);
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, char a2)
{
coin_counter_w(0LL, a2 & 1);
coin_counter_w(1LL, a2 & 2);
return K052109_set_RMRD_line((a2 & 8) != 0);
}

original_code:
static void surpratk_5fc0_w(offs_t offset,data_t data)
{
 coin_counter_w(0,data & 0x01);
 coin_counter_w(1,data & 0x02);
 K052109_set_RMRD_line( ( data & 0x08 ) ? 1 : 0 );
}

canonical_original_code:
static void surpratk_5fc0_w(unsigned int offset, unsigned int data)
{
 coin_counter_w(0,data & 0x01);
 coin_counter_w(1,data & 0x02);
 K052109_set_RMRD_line( ( data & 0x08 ) ? 1 : 0 );
}


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

bg_colorbase = K053251_get_palette_index(0LL);
sprite_colorbase = K053251_get_palette_index(1LL);
layer_colorbase[0] = K053251_get_palette_index(2LL);
dword_564 = K053251_get_palette_index(4LL);
dword_568 = K053251_get_palette_index(3LL);
K052109_tilemap_update();
palette_init_used_colors();
K053245_mark_sprites_colors();
if ( palette_recalc() )
tilemap_mark_all_pixels_dirty(0LL);
tilemap_render(0LL);
v2 = 0;
v3 = 1;
layerpri = K053251_get_priority(2LL);
v4 = 2;
dword_54C = K053251_get_priority(4LL);
dword_550 = K053251_get_priority(3LL);
sortlayers_constprop_0((int *)&v2);
fillbitmap(priority_bitmap, 0LL, 0LL);
fillbitmap(a1, *(unsigned __int16 *)(*(_QWORD *)(Machine + 792LL) + 32LL * bg_colorbase), Machine + 776LL);
K052109_tilemap_draw(a1, v2, 0x10000LL);
K052109_tilemap_draw(a1, v3, 0x20000LL);
K052109_tilemap_draw(a1, v4, 0x40000LL);
return K053245_sprites_draw(a1);
}

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

bg_colorbase = K053251_get_palette_index(0LL);
sprite_colorbase = K053251_get_palette_index(1LL);
layer_colorbase[0] = K053251_get_palette_index(2LL);
dword_564 = K053251_get_palette_index(4LL);
dword_568 = K053251_get_palette_index(3LL);
K052109_tilemap_update();
palette_init_used_colors();
K053245_mark_sprites_colors();
if ( palette_recalc() )
tilemap_mark_all_pixels_dirty(0LL);
tilemap_render(0LL);
v2 = 0;
v3 = 1;
layerpri = K053251_get_priority(2LL);
v4 = 2;
dword_54C = K053251_get_priority(4LL);
dword_550 = K053251_get_priority(3LL);
func13((int *)&v2);
fillbitmap(priority_bitmap, 0LL, 0LL);
fillbitmap(a1, *(unsigned __int16 *)(*(_QWORD *)(Machine + 792LL) + 32LL * bg_colorbase), Machine + 776LL);
K052109_tilemap_draw(a1, v2, 0x10000LL);
K052109_tilemap_draw(a1, v3, 0x20000LL);
K052109_tilemap_draw(a1, v4, 0x40000LL);
return K053245_sprites_draw(a1);
}

original_code:
void surpratk_vh_screenrefresh(struct osd_bitmap *bitmap,int full_refresh)
{
 int layer[3];
 bg_colorbase = K053251_get_palette_index(K053251_CI0);
 sprite_colorbase = K053251_get_palette_index(K053251_CI1);
 layer_colorbase[0] = K053251_get_palette_index(K053251_CI2);
 layer_colorbase[1] = K053251_get_palette_index(K053251_CI4);
 layer_colorbase[2] = K053251_get_palette_index(K053251_CI3);
 K052109_tilemap_update();
 palette_init_used_colors();
 K053245_mark_sprites_colors();
 if (palette_recalc())
  tilemap_mark_all_pixels_dirty(0);
 tilemap_render(0);
 layer[0] = 0;
 layerpri[0] = K053251_get_priority(K053251_CI2);
 layer[1] = 1;
 layerpri[1] = K053251_get_priority(K053251_CI4);
 layer[2] = 2;
 layerpri[2] = K053251_get_priority(K053251_CI3);
 sortlayers(layer,layerpri);
 fillbitmap(priority_bitmap,0,((void *)0));
 fillbitmap(bitmap,Machine->pens[16 * bg_colorbase],&Machine->visible_area);
 K052109_tilemap_draw(bitmap,layer[0],1<<16);
 K052109_tilemap_draw(bitmap,layer[1],2<<16);
 K052109_tilemap_draw(bitmap,layer[2],4<<16);
 K053245_sprites_draw(bitmap);
}

canonical_original_code:
void surpratk_vh_screenrefresh(struct osd_bitmap *bitmap, int full_refresh)
{
 int layer[3];
 bg_colorbase = K053251_get_palette_index(K053251_CI0);
 sprite_colorbase = K053251_get_palette_index(K053251_CI1);
 layer_colorbase[0] = K053251_get_palette_index(K053251_CI2);
 layer_colorbase[1] = K053251_get_palette_index(K053251_CI4);
 layer_colorbase[2] = K053251_get_palette_index(K053251_CI3);
 K052109_tilemap_update();
 palette_init_used_colors();
 K053245_mark_sprites_colors();
 if (palette_recalc())
  tilemap_mark_all_pixels_dirty(0);
 tilemap_render(0);
 layer[0] = 0;
 layerpri[0] = K053251_get_priority(K053251_CI2);
 layer[1] = 1;
 layerpri[1] = K053251_get_priority(K053251_CI4);
 layer[2] = 2;
 layerpri[2] = K053251_get_priority(K053251_CI3);
 sortlayers(layer,layerpri);
 fillbitmap(priority_bitmap,0,((void *)0));
 fillbitmap(bitmap,Machine->pens[16 * bg_colorbase],&Machine->visible_area);
 K052109_tilemap_draw(bitmap,layer[0],1<<16);
 K052109_tilemap_draw(bitmap,layer[1],2<<16);
 K052109_tilemap_draw(bitmap,layer[2],4<<16);
 K053245_sprites_draw(bitmap);
}

variable_types: struct osd_bitmap { int width; int depth; void *_private; unsigned char **line; } *

variable_types:
layer
int [3]


irqhandler

decompiled_code:
__int64 __fastcall irqhandler(unsigned int a1)
{
return cpu_set_irq_line(0LL, 1LL, a1);
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned int a1)
{
return cpu_set_irq_line(0LL, 1LL, a1);
}

original_code:
static void irqhandler(int linestate)
{
 cpu_set_irq_line(0,1,linestate);
}

variable_types: linestate int


decompiled_code:
__int64 __fastcall timer_callback_2610(int a1)
{
Timer[2 * (a1 & 0x7F) + (a1 >> 7)] = 0LL;
return YM2610TimerOver(a1 & 0x7F);
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1)
{
Timer[2 * (a1 & 0x7F) + (a1 >> 7)] = 0LL;
return YM2610TimerOver(a1 & 0x7F);
}

original_code:
static void timer_callback_2610(int param)
{
 int n=param&0x7f;
 int c=param>>7;
 Timer[n][c] = 0;
 YM2610TimerOver(n,c);
}

canonical_original_code:
static void timer_callback_2610(int param)
{
 int n = param&0x7f;
 int c = param>>7;
 Timer[n][c] = 0;
 YM2610TimerOver(n,c);
}

variable_types: param int

variable_types: n int

variable_types: c int


decompiled_code:
__int64 __fastcall YM2610UpdateRequest(int a1)
{
return stream_update(*((unsigned int *)&stream + a1), 100LL);
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1)
{
return stream_update(*((unsigned int *)&stream + a1), 100LL);
}

original_code:
void YM2610UpdateRequest(int chip)
{
 stream_update(stream[chip],100);
}

variable_types: chip int


decompiled_code:
_BOOL8 __fastcall YM2610_sh_start(__int64 a1)
{
unsigned int v1; // er12
bool v2; // cc
__int64 v5; // rax
__int64 v6; // rdi
__int64 v7; // rbx
__int64 v8; // r14
const char *v9; // rax
const char *v10; // rax
__int64 v11; // rdi
int v12; // eax
unsigned int v13; // [rsp+10h] [rbp-118h]
__int64 v14; // [rsp+10h] [rbp-118h]
int v15[2]; // [rsp+58h] [rbp-D0h] BYREF
_DWORD v16[2]; // [rsp+60h] [rbp-C8h] BYREF
_DWORD v17[2]; // [rsp+68h] [rbp-C0h] BYREF
__int64 v18[2]; // [rsp+70h] [rbp-B8h] BYREF
_QWORD v19[2]; // [rsp+80h] [rbp-A8h] BYREF
_QWORD v20[2]; // [rsp+90h] [rbp-98h] BYREF
char s[40]; // [rsp+A0h] [rbp-88h] BYREF
char v22[96]; // [rsp+C8h] [rbp-60h] BYREF

v1 = *(_DWORD *)(Machine + 836LL);
v2 = **(_DWORD **)(a1 + 8) <= 2;
intf = *(_QWORD *)(a1 + 8);
if ( !v2 || (unsigned int)AY8910_sh_start() )
return 1LL;
FMTimerInit();
v5 = intf;
v6 = *(unsigned int *)intf;
if ( (int)v6 <= 0 )
{
v12 = YM2610Init(v6, *(unsigned int *)(intf + 4), v1, v19, v16, v20, v17, TimerHandler, IRQHandler);
}
else
{
v7 = 0LL;
do
{
v18[0] = (__int64)s;
v8 = (int)v7 + 60LL;
v13 = *(_DWORD *)(v5 + 4 * v8 + 8);
v15[0] = (unsigned __int16)v13;
v9 = (const char *)sound_name(a1);
sprintf(s, "%s #%d Ch%d", v9, (unsigned int)v7, 1LL);
v18[1] = (__int64)v22;
v15[1] = HIWORD(v13);
v10 = (const char *)sound_name(a1);
sprintf(v22, "%s #%d Ch%d", v10, (unsigned int)v7, 2LL);
stream[v7] = stream_init_multi(2LL, v18, v15, v1, (unsigned int)v7, &YM2610UpdateOne);
v19[v7] = memory_region(*(unsigned int *)(intf + 4 * v8));
v16[v7] = memory_region_length(*(unsigned int *)(intf + 4 * v8));
v14 = (int)v7 + 56LL;
v20[v7] = memory_region(*(unsigned int *)(intf + 4 * v14 + 8));
v17[v7] = memory_region_length(*(unsigned int *)(intf + 4 * v14 + 8));
v5 = intf;
++v7;
v11 = *(unsigned int *)intf;
}
while ( (int)v11 > (int)v7 );
v12 = YM2610Init(v11, *(unsigned int *)(intf + 4), v1, v19, v16, v20, v17, TimerHandler, IRQHandler);
}
return v12 != 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func4(__int64 a1)
{
unsigned int v1; // er12
bool v2; // cc
__int64 v5; // rax
__int64 v6; // rdi
__int64 v7; // rbx
__int64 v8; // r14
const char *v9; // rax
const char *v10; // rax
__int64 v11; // rdi
int v12; // eax
unsigned int v13; // [rsp+10h] [rbp-118h]
__int64 v14; // [rsp+10h] [rbp-118h]
int v15[2]; // [rsp+58h] [rbp-D0h] BYREF
_DWORD v16[2]; // [rsp+60h] [rbp-C8h] BYREF
_DWORD v17[2]; // [rsp+68h] [rbp-C0h] BYREF
__int64 v18[2]; // [rsp+70h] [rbp-B8h] BYREF
_QWORD v19[2]; // [rsp+80h] [rbp-A8h] BYREF
_QWORD v20[2]; // [rsp+90h] [rbp-98h] BYREF
char s[40]; // [rsp+A0h] [rbp-88h] BYREF
char v22[96]; // [rsp+C8h] [rbp-60h] BYREF

v1 = *(_DWORD *)(Machine + 836LL);
v2 = **(_DWORD **)(a1 + 8) <= 2;
intf = *(_QWORD *)(a1 + 8);
if ( !v2 || (unsigned int)AY8910_sh_start() )
return 1LL;
func0();
v5 = intf;
v6 = *(unsigned int *)intf;
if ( (int)v6 <= 0 )
{
v12 = YM2610Init(v6, *(unsigned int *)(intf + 4), v1, v19, v16, v20, v17, TimerHandler, IRQHandler);
}
else
{
v7 = 0LL;
do
{
v18[0] = (__int64)s;
v8 = (int)v7 + 60LL;
v13 = *(_DWORD *)(v5 + 4 * v8 + 8);
v15[0] = (unsigned __int16)v13;
v9 = (const char *)sound_name(a1);
sprintf(s, "%s #%d Ch%d", v9, (unsigned int)v7, 1LL);
v18[1] = (__int64)v22;
v15[1] = HIWORD(v13);
v10 = (const char *)sound_name(a1);
sprintf(v22, "%s #%d Ch%d", v10, (unsigned int)v7, 2LL);
stream[v7] = stream_init_multi(2LL, v18, v15, v1, (unsigned int)v7, &YM2610UpdateOne);
v19[v7] = memory_region(*(unsigned int *)(intf + 4 * v8));
v16[v7] = memory_region_length(*(unsigned int *)(intf + 4 * v8));
v14 = (int)v7 + 56LL;
v20[v7] = memory_region(*(unsigned int *)(intf + 4 * v14 + 8));
v17[v7] = memory_region_length(*(unsigned int *)(intf + 4 * v14 + 8));
v5 = intf;
++v7;
v11 = *(unsigned int *)intf;
}
while ( (int)v11 > (int)v7 );
v12 = YM2610Init(v11, *(unsigned int *)(intf + 4), v1, v19, v16, v20, v17, TimerHandler, IRQHandler);
}
return v12 != 0;
}

original_code:
int YM2610_sh_start(const struct MachineSound *msound)
{
 int i,j;
 int rate = Machine->sample_rate;
 char buf[2][40];
 const char *name[2];
 int mixed_vol,vol[2];
 void *pcmbufa[2],*pcmbufb[2];
 int pcmsizea[2],pcmsizeb[2];
 intf = (const struct YM2610interface *)msound->sound_interface;
 if( intf->num > (2) ) return 1;
 if (AY8910_sh_start(msound)) return 1;
 FMTimerInit();
 for (i = 0;i < intf->num;i++)
 {
  mixed_vol = intf->volumeFM[i];
  for (j = 0 ; j < 2 ; j++)
  {
   name[j]=buf[j];
   vol[j] = mixed_vol & 0xffff;
   mixed_vol>>=16;
   sprintf(buf[j],"%s #%d Ch%d",sound_name(msound),i,j+1);
  }
  stream[i] = stream_init_multi(2,name,vol,rate,i,YM2610UpdateOne);
  pcmbufa[i] = (void *)(memory_region(intf->pcmroma[i]));
  pcmsizea[i] = memory_region_length(intf->pcmroma[i]);
  pcmbufb[i] = (void *)(memory_region(intf->pcmromb[i]));
  pcmsizeb[i] = memory_region_length(intf->pcmromb[i]);
 }
 if (YM2610Init(intf->num,intf->baseclock,rate,
             pcmbufa,pcmsizea,pcmbufb,pcmsizeb,
             TimerHandler,IRQHandler) == 0)
  return 0;
 return 1;
}

canonical_original_code:
int YM2610_sh_start(struct MachineSound *msound)
{
 int i; int j;
 int rate = Machine->sample_rate;
 char buf[2][40];
 char *name[2];
 int mixed_vol; int vol[2];
 void *pcmbufa[2]; void *pcmbufb[2];
 int pcmsizea[2]; int pcmsizeb[2];
 intf = (struct YM2610interface *)msound->sound_interface;
 if( intf->num > (2) ) return 1;
 if (AY8910_sh_start(msound)) return 1;
 FMTimerInit();
 for (i = 0;i < intf->num;i++)
 {
  mixed_vol = intf->volumeFM[i];
  for (j = 0 ; j < 2 ; j++)
  {
   name[j]=buf[j];
   vol[j] = mixed_vol & 0xffff;
   mixed_vol>>=16;
   sprintf(buf[j],"%s #%d Ch%d",sound_name(msound),i,j+1);
  }
  stream[i] = stream_init_multi(2,name,vol,rate,i,YM2610UpdateOne);
  pcmbufa[i] = (void *)(memory_region(intf->pcmroma[i]));
  pcmsizea[i] = memory_region_length(intf->pcmroma[i]);
  pcmbufb[i] = (void *)(memory_region(intf->pcmromb[i]));
  pcmsizeb[i] = memory_region_length(intf->pcmromb[i]);
 }
 if (YM2610Init(intf->num,intf->baseclock,rate,
             pcmbufa,pcmsizea,pcmbufb,pcmsizeb,
             TimerHandler,IRQHandler) == 0)
  return 0;
 return 1;
}

variable_types: struct MachineSound { int sound_type; void *sound_interface; } *

variable_types: j int

variable_types: rate int

variable_types:
buf
char [2][40]

variable_types:
name
char *[2]

variable_types:
vol
int [2]

variable_types:
pcmbufa
void *[2]

variable_types:
pcmbufb
void *[2]

variable_types:
pcmsizea
int [2]

variable_types:
pcmsizeb
int [2]

user_defined_types: struct YM2610interface { int num; int baseclock; int volumeSSG[5]; unsigned int (*portAread[5])(unsigned int offset); unsigned int (*portBread[5])(unsigned int offset); void (*portAwrite[5])(unsigned int offset, unsigned int data); void (*portBwrite[5])(unsigned int offset, unsigned int data); void (*handler[5])(int irq); int pcmromb[-1]; int pcmroma[-1]; int volumeFM[-1]; }


decompiled_code:
_BOOL8 __fastcall YM2610B_sh_start(__int64 a1)
{
unsigned int v1; // er12
bool v2; // cc
__int64 v5; // rax
__int64 v6; // rdi
__int64 v7; // rbx
__int64 v8; // r14
const char *v9; // rax
const char *v10; // rax
__int64 v11; // rdi
int v12; // eax
unsigned int v13; // [rsp+10h] [rbp-118h]
__int64 v14; // [rsp+10h] [rbp-118h]
int v15[2]; // [rsp+58h] [rbp-D0h] BYREF
_DWORD v16[2]; // [rsp+60h] [rbp-C8h] BYREF
_DWORD v17[2]; // [rsp+68h] [rbp-C0h] BYREF
__int64 v18[2]; // [rsp+70h] [rbp-B8h] BYREF
_QWORD v19[2]; // [rsp+80h] [rbp-A8h] BYREF
_QWORD v20[2]; // [rsp+90h] [rbp-98h] BYREF
char s[40]; // [rsp+A0h] [rbp-88h] BYREF
char v22[96]; // [rsp+C8h] [rbp-60h] BYREF

v1 = *(_DWORD *)(Machine + 836LL);
v2 = **(_DWORD **)(a1 + 8) <= 2;
intf = *(_QWORD *)(a1 + 8);
if ( !v2 || (unsigned int)AY8910_sh_start() )
return 1LL;
FMTimerInit();
v5 = intf;
v6 = *(unsigned int *)intf;
if ( (int)v6 <= 0 )
{
v12 = YM2610Init(v6, *(unsigned int *)(intf + 4), v1, v19, v16, v20, v17, TimerHandler, IRQHandler);
}
else
{
v7 = 0LL;
do
{
v18[0] = (__int64)s;
v8 = (int)v7 + 60LL;
v13 = *(_DWORD *)(v5 + 4 * v8 + 8);
v15[0] = (unsigned __int16)v13;
v9 = (const char *)sound_name(a1);
sprintf(s, "%s #%d Ch%d", v9, (unsigned int)v7, 1LL);
v18[1] = (__int64)v22;
v15[1] = HIWORD(v13);
v10 = (const char *)sound_name(a1);
sprintf(v22, "%s #%d Ch%d", v10, (unsigned int)v7, 2LL);
stream[v7] = stream_init_multi(2LL, v18, v15, v1, (unsigned int)v7, &YM2610BUpdateOne);
v19[v7] = memory_region(*(unsigned int *)(intf + 4 * v8));
v16[v7] = memory_region_length(*(unsigned int *)(intf + 4 * v8));
v14 = (int)v7 + 56LL;
v20[v7] = memory_region(*(unsigned int *)(intf + 4 * v14 + 8));
v17[v7] = memory_region_length(*(unsigned int *)(intf + 4 * v14 + 8));
v5 = intf;
++v7;
v11 = *(unsigned int *)intf;
}
while ( (int)v11 > (int)v7 );
v12 = YM2610Init(v11, *(unsigned int *)(intf + 4), v1, v19, v16, v20, v17, TimerHandler, IRQHandler);
}
return v12 != 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func5(__int64 a1)
{
unsigned int v1; // er12
bool v2; // cc
__int64 v5; // rax
__int64 v6; // rdi
__int64 v7; // rbx
__int64 v8; // r14
const char *v9; // rax
const char *v10; // rax
__int64 v11; // rdi
int v12; // eax
unsigned int v13; // [rsp+10h] [rbp-118h]
__int64 v14; // [rsp+10h] [rbp-118h]
int v15[2]; // [rsp+58h] [rbp-D0h] BYREF
_DWORD v16[2]; // [rsp+60h] [rbp-C8h] BYREF
_DWORD v17[2]; // [rsp+68h] [rbp-C0h] BYREF
__int64 v18[2]; // [rsp+70h] [rbp-B8h] BYREF
_QWORD v19[2]; // [rsp+80h] [rbp-A8h] BYREF
_QWORD v20[2]; // [rsp+90h] [rbp-98h] BYREF
char s[40]; // [rsp+A0h] [rbp-88h] BYREF
char v22[96]; // [rsp+C8h] [rbp-60h] BYREF

v1 = *(_DWORD *)(Machine + 836LL);
v2 = **(_DWORD **)(a1 + 8) <= 2;
intf = *(_QWORD *)(a1 + 8);
if ( !v2 || (unsigned int)AY8910_sh_start() )
return 1LL;
func0();
v5 = intf;
v6 = *(unsigned int *)intf;
if ( (int)v6 <= 0 )
{
v12 = YM2610Init(v6, *(unsigned int *)(intf + 4), v1, v19, v16, v20, v17, TimerHandler, IRQHandler);
}
else
{
v7 = 0LL;
do
{
v18[0] = (__int64)s;
v8 = (int)v7 + 60LL;
v13 = *(_DWORD *)(v5 + 4 * v8 + 8);
v15[0] = (unsigned __int16)v13;
v9 = (const char *)sound_name(a1);
sprintf(s, "%s #%d Ch%d", v9, (unsigned int)v7, 1LL);
v18[1] = (__int64)v22;
v15[1] = HIWORD(v13);
v10 = (const char *)sound_name(a1);
sprintf(v22, "%s #%d Ch%d", v10, (unsigned int)v7, 2LL);
stream[v7] = stream_init_multi(2LL, v18, v15, v1, (unsigned int)v7, &YM2610BUpdateOne);
v19[v7] = memory_region(*(unsigned int *)(intf + 4 * v8));
v16[v7] = memory_region_length(*(unsigned int *)(intf + 4 * v8));
v14 = (int)v7 + 56LL;
v20[v7] = memory_region(*(unsigned int *)(intf + 4 * v14 + 8));
v17[v7] = memory_region_length(*(unsigned int *)(intf + 4 * v14 + 8));
v5 = intf;
++v7;
v11 = *(unsigned int *)intf;
}
while ( (int)v11 > (int)v7 );
v12 = YM2610Init(v11, *(unsigned int *)(intf + 4), v1, v19, v16, v20, v17, TimerHandler, IRQHandler);
}
return v12 != 0;
}

original_code:
int YM2610B_sh_start(const struct MachineSound *msound)
{
 int i,j;
 int rate = Machine->sample_rate;
 char buf[2][40];
 const char *name[2];
 int mixed_vol,vol[2];
 void *pcmbufa[2],*pcmbufb[2];
 int pcmsizea[2],pcmsizeb[2];
 intf = (const struct YM2610interface *)msound->sound_interface;
 if( intf->num > (2) ) return 1;
 if (AY8910_sh_start(msound)) return 1;
 FMTimerInit();
 for (i = 0;i < intf->num;i++)
 {
  mixed_vol = intf->volumeFM[i];
  for (j = 0 ; j < 2 ; j++)
  {
   name[j]=buf[j];
   vol[j] = mixed_vol & 0xffff;
   mixed_vol>>=16;
   sprintf(buf[j],"%s #%d Ch%d",sound_name(msound),i,j+1);
  }
  stream[i] = stream_init_multi(2,name,vol,rate,i,YM2610BUpdateOne);
  pcmbufa[i] = (void *)(memory_region(intf->pcmroma[i]));
  pcmsizea[i] = memory_region_length(intf->pcmroma[i]);
  pcmbufb[i] = (void *)(memory_region(intf->pcmromb[i]));
  pcmsizeb[i] = memory_region_length(intf->pcmromb[i]);
 }
 if (YM2610Init(intf->num,intf->baseclock,rate,
             pcmbufa,pcmsizea,pcmbufb,pcmsizeb,
             TimerHandler,IRQHandler) == 0)
  return 0;
 return 1;
}

canonical_original_code:
int YM2610B_sh_start(struct MachineSound *msound)
{
 int i; int j;
 int rate = Machine->sample_rate;
 char buf[2][40];
 char *name[2];
 int mixed_vol; int vol[2];
 void *pcmbufa[2]; void *pcmbufb[2];
 int pcmsizea[2]; int pcmsizeb[2];
 intf = (struct YM2610interface *)msound->sound_interface;
 if( intf->num > (2) ) return 1;
 if (AY8910_sh_start(msound)) return 1;
 FMTimerInit();
 for (i = 0;i < intf->num;i++)
 {
  mixed_vol = intf->volumeFM[i];
  for (j = 0 ; j < 2 ; j++)
  {
   name[j]=buf[j];
   vol[j] = mixed_vol & 0xffff;
   mixed_vol>>=16;
   sprintf(buf[j],"%s #%d Ch%d",sound_name(msound),i,j+1);
  }
  stream[i] = stream_init_multi(2,name,vol,rate,i,YM2610BUpdateOne);
  pcmbufa[i] = (void *)(memory_region(intf->pcmroma[i]));
  pcmsizea[i] = memory_region_length(intf->pcmroma[i]);
  pcmbufb[i] = (void *)(memory_region(intf->pcmromb[i]));
  pcmsizeb[i] = memory_region_length(intf->pcmromb[i]);
 }
 if (YM2610Init(intf->num,intf->baseclock,rate,
             pcmbufa,pcmsizea,pcmbufb,pcmsizeb,
             TimerHandler,IRQHandler) == 0)
  return 0;
 return 1;
}

variable_types: i int

user_defined_types: struct MachineSound { int sound_type; void *sound_interface; }


decompiled_code:
// attributes: thunk
__int64 YM2610_sh_stop()
{
return YM2610Shutdown();
}

canonical_decompiled_code:
__int64 func6()
{
return YM2610Shutdown();
}

original_code:
void YM2610_sh_stop(void)
{
 YM2610Shutdown();
}

canonical_original_code:
void YM2610_sh_stop()
{
 YM2610Shutdown();
}


decompiled_code:
__int64 YM2610_sh_reset()
{
__int64 result; // rax
unsigned int v1; // ebx
__int64 v2; // rdi

result = *(unsigned int *)intf;
if ( (int)result > 0 )
{
v1 = 0;
do
{
v2 = v1++;
YM2610ResetChip(v2);
result = intf;
}
while ( *(_DWORD *)intf > (signed int)v1 );
}
return result;
}

canonical_decompiled_code:
__int64 func7()
{
__int64 result; // rax
unsigned int v1; // ebx
__int64 v2; // rdi

result = *(unsigned int *)intf;
if ( (int)result > 0 )
{
v1 = 0;
do
{
v2 = v1++;
YM2610ResetChip(v2);
result = intf;
}
while ( *(_DWORD *)intf > (signed int)v1 );
}
return result;
}

original_code:
void YM2610_sh_reset(void)
{
 int i;
 for (i = 0;i < intf->num;i++)
  YM2610ResetChip(i);
}

canonical_original_code:
void YM2610_sh_reset()
{
 int i;
 for (i = 0;i < intf->num;i++)
  YM2610ResetChip(i);
}


decompiled_code:
__int64 YM2610_status_port_0_A_r()
{
return (unsigned __int8)YM2610Read(0LL, 0LL);
}

canonical_decompiled_code:
__int64 func8()
{
return (unsigned __int8)YM2610Read(0LL, 0LL);
}

original_code:
data_t YM2610_status_port_0_A_r(offs_t offset)
{
 return YM2610Read(0,0);
}

canonical_original_code:
unsigned int YM2610_status_port_0_A_r(unsigned int offset)
{
 return YM2610Read(0,0);
}

variable_types:
offset
unsigned int


decompiled_code:
__int64 YM2610_status_port_0_B_r()
{
return (unsigned __int8)YM2610Read(0LL, 2LL);
}

canonical_decompiled_code:
__int64 func9()
{
return (unsigned __int8)YM2610Read(0LL, 2LL);
}

original_code:
data_t YM2610_status_port_0_B_r(offs_t offset)
{
 return YM2610Read(0,2);
}

canonical_original_code:
unsigned int YM2610_status_port_0_B_r(unsigned int offset)
{
 return YM2610Read(0,2);
}


decompiled_code:
__int64 YM2610_status_port_1_A_r()
{
return (unsigned __int8)YM2610Read(1LL, 0LL);
}

canonical_decompiled_code:
__int64 func10()
{
return (unsigned __int8)YM2610Read(1LL, 0LL);
}

original_code:
data_t YM2610_status_port_1_A_r(offs_t offset) {
 return YM2610Read(1,0);
}

canonical_original_code:
unsigned int YM2610_status_port_1_A_r(unsigned int offset) {
 return YM2610Read(1,0);
}


decompiled_code:
__int64 YM2610_status_port_1_B_r()
{
return (unsigned __int8)YM2610Read(1LL, 2LL);
}

canonical_decompiled_code:
__int64 func11()
{
return (unsigned __int8)YM2610Read(1LL, 2LL);
}

original_code:
data_t YM2610_status_port_1_B_r(offs_t offset) {
 return YM2610Read(1,2);
}

canonical_original_code:
unsigned int YM2610_status_port_1_B_r(unsigned int offset) {
 return YM2610Read(1,2);
}


decompiled_code:
__int64 YM2610_read_port_0_r()
{
return (unsigned __int8)YM2610Read(0LL, 1LL);
}

canonical_decompiled_code:
__int64 func12()
{
return (unsigned __int8)YM2610Read(0LL, 1LL);
}

original_code:
data_t YM2610_read_port_0_r(offs_t offset){
 return YM2610Read(0,1);
}

canonical_original_code:
unsigned int YM2610_read_port_0_r(unsigned int offset){
 return YM2610Read(0,1);
}


decompiled_code:
__int64 YM2610_read_port_1_r()
{
return (unsigned __int8)YM2610Read(1LL, 1LL);
}

canonical_decompiled_code:
__int64 func13()
{
return (unsigned __int8)YM2610Read(1LL, 1LL);
}

original_code:
data_t YM2610_read_port_1_r(offs_t offset){
 return YM2610Read(1,1);
}

canonical_original_code:
unsigned int YM2610_read_port_1_r(unsigned int offset){
 return YM2610Read(1,1);
}


decompiled_code:
__int64 __fastcall YM2610_control_port_0_A_w(__int64 a1, unsigned __int8 a2)
{
return YM2610Write(0LL, 0LL, a2);
}

canonical_decompiled_code:
__int64 __fastcall func14(__int64 a1, unsigned __int8 a2)
{
return YM2610Write(0LL, 0LL, a2);
}

original_code:
void YM2610_control_port_0_A_w(offs_t offset,data_t data)
{
 YM2610Write(0,0,data);
}

canonical_original_code:
void YM2610_control_port_0_A_w(unsigned int offset, unsigned int data)
{
 YM2610Write(0,0,data);
}

variable_types:
data
unsigned int


decompiled_code:
__int64 __fastcall YM2610_control_port_0_B_w(__int64 a1, unsigned __int8 a2)
{
return YM2610Write(0LL, 2LL, a2);
}

canonical_decompiled_code:
__int64 __fastcall func15(__int64 a1, unsigned __int8 a2)
{
return YM2610Write(0LL, 2LL, a2);
}

original_code:
void YM2610_control_port_0_B_w(offs_t offset,data_t data)
{
 YM2610Write(0,2,data);
}

canonical_original_code:
void YM2610_control_port_0_B_w(unsigned int offset, unsigned int data)
{
 YM2610Write(0,2,data);
}


decompiled_code:
__int64 __fastcall YM2610_control_port_1_A_w(__int64 a1, unsigned __int8 a2)
{
return YM2610Write(1LL, 0LL, a2);
}

canonical_decompiled_code:
__int64 __fastcall func16(__int64 a1, unsigned __int8 a2)
{
return YM2610Write(1LL, 0LL, a2);
}

original_code:
void YM2610_control_port_1_A_w(offs_t offset,data_t data){
 YM2610Write(1,0,data);
}

canonical_original_code:
void YM2610_control_port_1_A_w(unsigned int offset, unsigned int data){
 YM2610Write(1,0,data);
}


decompiled_code:
__int64 __fastcall YM2610_control_port_1_B_w(__int64 a1, unsigned __int8 a2)
{
return YM2610Write(1LL, 2LL, a2);
}

canonical_decompiled_code:
__int64 __fastcall func17(__int64 a1, unsigned __int8 a2)
{
return YM2610Write(1LL, 2LL, a2);
}

original_code:
void YM2610_control_port_1_B_w(offs_t offset,data_t data){
 YM2610Write(1,2,data);
}

canonical_original_code:
void YM2610_control_port_1_B_w(unsigned int offset, unsigned int data){
 YM2610Write(1,2,data);
}


decompiled_code:
__int64 __fastcall YM2610_data_port_0_A_w(__int64 a1, unsigned __int8 a2)
{
return YM2610Write(0LL, 1LL, a2);
}

canonical_decompiled_code:
__int64 __fastcall func18(__int64 a1, unsigned __int8 a2)
{
return YM2610Write(0LL, 1LL, a2);
}

original_code:
void YM2610_data_port_0_A_w(offs_t offset,data_t data)
{
 YM2610Write(0,1,data);
}

canonical_original_code:
void YM2610_data_port_0_A_w(unsigned int offset, unsigned int data)
{
 YM2610Write(0,1,data);
}


decompiled_code:
__int64 __fastcall YM2610_data_port_0_B_w(__int64 a1, unsigned __int8 a2)
{
return YM2610Write(0LL, 3LL, a2);
}

canonical_decompiled_code:
__int64 __fastcall func19(__int64 a1, unsigned __int8 a2)
{
return YM2610Write(0LL, 3LL, a2);
}

original_code:
void YM2610_data_port_0_B_w(offs_t offset,data_t data)
{
 YM2610Write(0,3,data);
}

canonical_original_code:
void YM2610_data_port_0_B_w(unsigned int offset, unsigned int data)
{
 YM2610Write(0,3,data);
}


decompiled_code:
__int64 __fastcall YM2610_data_port_1_A_w(__int64 a1, unsigned __int8 a2)
{
return YM2610Write(1LL, 1LL, a2);
}

canonical_decompiled_code:
__int64 __fastcall func20(__int64 a1, unsigned __int8 a2)
{
return YM2610Write(1LL, 1LL, a2);
}

original_code:
void YM2610_data_port_1_A_w(offs_t offset,data_t data){
 YM2610Write(1,1,data);
}

canonical_original_code:
void YM2610_data_port_1_A_w(unsigned int offset, unsigned int data){
 YM2610Write(1,1,data);
}


decompiled_code:
__int64 __fastcall YM2610_data_port_1_B_w(__int64 a1, unsigned __int8 a2)
{
return YM2610Write(1LL, 3LL, a2);
}

canonical_decompiled_code:
__int64 __fastcall func21(__int64 a1, unsigned __int8 a2)
{
return YM2610Write(1LL, 3LL, a2);
}

original_code:
void YM2610_data_port_1_B_w(offs_t offset,data_t data){
 YM2610Write(1,3,data);
}

canonical_original_code:
void YM2610_data_port_1_B_w(unsigned int offset, unsigned int data){
 YM2610Write(1,3,data);
}


FMTimerInit

decompiled_code:
void FMTimerInit()
{
qword_7E8 = 0LL;
Timer = 0LL;
qword_7F8 = 0LL;
qword_7F0 = 0LL;
}

canonical_decompiled_code:
void func0()
{
qword_7E8 = 0LL;
Timer = 0LL;
qword_7F8 = 0LL;
qword_7F0 = 0LL;
}

original_code:
static void FMTimerInit( void )
{
 int i;
 for( i = 0 ; i < (2) ; i++ )
  Timer[i][0] = Timer[i][1] = 0;
}

canonical_original_code:
static void FMTimerInit()
{
 int i;
 for( i = 0 ; i < (2) ; i++ )
  Timer[i][0] = Timer[i][1] = 0;
}


decompiled_code:
__int64 __fastcall snowbros_spriteram_r(unsigned int a1)
{
return *(unsigned __int16 *)(snowbros_spriteram + a1);
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1)
{
return *(unsigned __int16 *)(snowbros_spriteram + a1);
}

original_code:
data_t snowbros_spriteram_r(offs_t offset)
{
 return (*(UINT16 *)(&snowbros_spriteram[offset]));
}

canonical_original_code:
unsigned int snowbros_spriteram_r(unsigned int offset)
{
 return (*(unsigned short *)(&snowbros_spriteram[offset]));
}


decompiled_code:
__int64 __fastcall snowbros_vh_screenrefresh(__int64 a1)
{
__int64 v1; // r14
__int64 v2; // r13
__int64 v3; // rbx
int v4; // esi
__int64 result; // rax
__int16 v6; // cx
__int16 v7; // r8
unsigned int v8; // edx

LODWORD(v1) = 0;
LODWORD(v2) = 0;
v3 = 8LL;
palette_recalc();
fillbitmap(a1, **(unsigned __int16 **)(*(_QWORD *)(Machine + 512LL) + 16LL), Machine + 776LL);
do
{
while ( 1 )
{
result = *(unsigned __int8 *)(snowbros_spriteram + v3);
v6 = *(_WORD *)(snowbros_spriteram + v3 - 2);
v4 = *(unsigned __int8 *)(snowbros_spriteram + v3 + 2);
if ( (v6 & 1) != 0 )
result = *(unsigned __int8 *)(snowbros_spriteram + v3) ^ 0xFFFFFF00;
if ( (v6 & 2) == 0 )
break;
v4 ^= 0xFFFFFF00;
if ( (v6 & 4) != 0 )
goto LABEL_14;
LODWORD(v1) = result;
LODWORD(v2) = v4;
LABEL_4:
v3 += 16LL;
if ( v3 == 7688 )
return result;
}
if ( (v6 & 4) == 0 )
{
v1 = (unsigned int)result;
v2 = *(unsigned __int8 *)(snowbros_spriteram + v3 + 2);
goto LABEL_10;
}
LABEL_14:
v1 = (unsigned int)(result + v1);
v2 = (unsigned int)(v4 + v2);
if ( (int)v1 > 511 )
v1 &= 0x1FFu;
if ( (int)v2 > 511 )
v2 &= 0x1FFu;
LABEL_10:
result = (unsigned int)(v2 - 1);
if ( (unsigned int)result > 0xEE )
goto LABEL_4;
result = (unsigned int)(v1 + 15);
if ( (unsigned int)result > 0x10E )
goto LABEL_4;
v7 = *(_WORD *)(snowbros_spriteram + v3 + 6);
v8 = *(unsigned __int8 *)(snowbros_spriteram + v3 + 4);
v3 += 16LL;
result = drawgfx(
a1,
*(_QWORD *)(Machine + 512LL),
(v7 << 8) & 0xF00 | v8,
(unsigned __int8)v6 >> 4,
(unsigned __int8)v7 & 0x80,
v7 & 0x40,
v1,
v2,
Machine + 776LL,
2LL,
0LL);
}
while ( v3 != 7688 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
__int64 v1; // r14
__int64 v2; // r13
__int64 v3; // rbx
int v4; // esi
__int64 result; // rax
__int16 v6; // cx
__int16 v7; // r8
unsigned int v8; // edx

LODWORD(v1) = 0;
LODWORD(v2) = 0;
v3 = 8LL;
palette_recalc();
fillbitmap(a1, **(unsigned __int16 **)(*(_QWORD *)(Machine + 512LL) + 16LL), Machine + 776LL);
do
{
while ( 1 )
{
result = *(unsigned __int8 *)(snowbros_spriteram + v3);
v6 = *(_WORD *)(snowbros_spriteram + v3 - 2);
v4 = *(unsigned __int8 *)(snowbros_spriteram + v3 + 2);
if ( (v6 & 1) != 0 )
result = *(unsigned __int8 *)(snowbros_spriteram + v3) ^ 0xFFFFFF00;
if ( (v6 & 2) == 0 )
break;
v4 ^= 0xFFFFFF00;
if ( (v6 & 4) != 0 )
goto LABEL_14;
LODWORD(v1) = result;
LODWORD(v2) = v4;
LABEL_4:
v3 += 16LL;
if ( v3 == 7688 )
return result;
}
if ( (v6 & 4) == 0 )
{
v1 = (unsigned int)result;
v2 = *(unsigned __int8 *)(snowbros_spriteram + v3 + 2);
goto LABEL_10;
}
LABEL_14:
v1 = (unsigned int)(result + v1);
v2 = (unsigned int)(v4 + v2);
if ( (int)v1 > 511 )
v1 &= 0x1FFu;
if ( (int)v2 > 511 )
v2 &= 0x1FFu;
LABEL_10:
result = (unsigned int)(v2 - 1);
if ( (unsigned int)result > 0xEE )
goto LABEL_4;
result = (unsigned int)(v1 + 15);
if ( (unsigned int)result > 0x10E )
goto LABEL_4;
v7 = *(_WORD *)(snowbros_spriteram + v3 + 6);
v8 = *(unsigned __int8 *)(snowbros_spriteram + v3 + 4);
v3 += 16LL;
result = drawgfx(
a1,
*(_QWORD *)(Machine + 512LL),
(v7 << 8) & 0xF00 | v8,
(unsigned __int8)v6 >> 4,
(unsigned __int8)v7 & 0x80,
v7 & 0x40,
v1,
v2,
Machine + 776LL,
2LL,
0LL);
}
while ( v3 != 7688 );
return result;
}

original_code:
void snowbros_vh_screenrefresh(struct osd_bitmap *bitmap,int full_refresh)
{
 int x=0,y=0,offs;
 palette_recalc ();
   fillbitmap(bitmap,Machine->gfx[0]->colortable[0],&Machine->visible_area);
 for (offs = 0;offs < 0x1e00; offs += 16)
 {
  int sx = (*(UINT16 *)(&snowbros_spriteram[8+offs])) & 0xff;
  int sy = (*(UINT16 *)(&snowbros_spriteram[0x0a+offs])) & 0xff;
  int tilecolour = (*(UINT16 *)(&snowbros_spriteram[6+offs]));
  if (tilecolour & 1) sx = -1 - (sx ^ 0xff);
  if (tilecolour & 2) sy = -1 - (sy ^ 0xff);
  if (tilecolour & 4)
  {
   x += sx;
   y += sy;
  }
  else
  {
   x = sx;
   y = sy;
  }
  if (x > 511) x &= 0x1ff;
  if (y > 511) y &= 0x1ff;
  if ((x>-16) && (y>0) && (x<256) && (y<240))
  {
   int attr = (*(UINT16 *)(&snowbros_spriteram[0x0e + offs]));
   int tile = ((attr & 0x0f) << 8) + ((*(UINT16 *)(&snowbros_spriteram[0x0c+offs])) & 0xff);
   drawgfx(bitmap,Machine->gfx[0],
     tile,
     (tilecolour & 0xf0) >> 4,
     attr & 0x80, attr & 0x40,
     x,y,
     &Machine->visible_area,TRANSPARENCY_PEN,0);
  }
 }
}

canonical_original_code:
void snowbros_vh_screenrefresh(struct osd_bitmap *bitmap, int full_refresh)
{
 int x = 0; int y = 0; int offs;
 palette_recalc ();
   fillbitmap(bitmap,Machine->gfx[0]->colortable[0],&Machine->visible_area);
 for (offs = 0;offs < 0x1e00; offs += 16)
 {
  int sx = (*(UINT16 *)(&snowbros_spriteram[8+offs])) & 0xff;
  int sy = (*(UINT16 *)(&snowbros_spriteram[0x0a+offs])) & 0xff;
  int tilecolour = (*(UINT16 *)(&snowbros_spriteram[6+offs]));
  if (tilecolour & 1) sx = -1 - (sx ^ 0xff);
  if (tilecolour & 2) sy = -1 - (sy ^ 0xff);
  if (tilecolour & 4)
  {
   x += sx;
   y += sy;
  }
  else
  {
   x = sx;
   y = sy;
  }
  if (x > 511) x &= 0x1ff;
  if (y > 511) y &= 0x1ff;
  if ((x>-16) && (y>0) && (x<256) && (y<240))
  {
   int attr = (*(UINT16 *)(&snowbros_spriteram[0x0e + offs]));
   int tile = ((attr & 0x0f) << 8) + ((*(UINT16 *)(&snowbros_spriteram[0x0c+offs])) & 0xff);
   drawgfx(bitmap,Machine->gfx[0],
     tile,
     (tilecolour & 0xf0) >> 4,
     attr & 0x80, attr & 0x40,
     x,y,
     &Machine->visible_area,TRANSPARENCY_PEN,0);
  }
 }
}

variable_types: struct osd_bitmap { int width; int depth; void *_private; unsigned char **line; } *

variable_types: x int

variable_types: y int

variable_types: offs int

variable_types: sx int

variable_types: sy int

variable_types: tilecolour int

variable_types: attr int

variable_types: tile int


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

if ( *(_DWORD *)(Machine + 836LL) )
result = soundlatch_r();
else
result = 3LL;
return result;
}

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

if ( *(_DWORD *)(Machine + 836LL) )
result = soundlatch_r();
else
result = 3LL;
return result;
}

original_code:
data_t snowbros_68000_sound_r(offs_t offset)
{
 int ret;
 if (Machine->sample_rate != 0)
 {
  ret = soundlatch_r(offset);
 }
 else
 {
  ret = 3;
 }
 return ret;
}

canonical_original_code:
unsigned int snowbros_68000_sound_r(unsigned int offset)
{
 int ret;
 if (Machine->sample_rate != 0)
 {
  ret = soundlatch_r(offset);
 }
 else
 {
  ret = 3;
 }
 return ret;
}

variable_types: ret int


decompiled_code:
__int64 snowbros_interrupt()
{
return (unsigned int)cpu_getiloops() + 2;
}

canonical_decompiled_code:
__int64 func5()
{
return (unsigned int)cpu_getiloops() + 2;
}

original_code:
static int snowbros_interrupt(void)
{
 return cpu_getiloops() + 2;
}

canonical_original_code:
static int snowbros_interrupt()
{
 return cpu_getiloops() + 2;
}


decompiled_code:
__int64 snowbros_68000_sound_w()
{
soundlatch_w();
return cpu_cause_interrupt(1LL, 4294967294LL);
}

canonical_decompiled_code:
__int64 func6()
{
soundlatch_w();
return cpu_cause_interrupt(1LL, 4294967294LL);
}

original_code:
static void snowbros_68000_sound_w(offs_t offset,data_t data)
{
 soundlatch_w(offset,data);
 cpu_cause_interrupt(1,-2);
}

canonical_original_code:
static void snowbros_68000_sound_w(unsigned int offset, unsigned int data)
{
 soundlatch_w(offset,data);
 cpu_cause_interrupt(1,-2);
}


decompiled_code:
__int64 __fastcall snowbros_input_r(int a1)
{
__int64 result; // rax
int v2; // ebx
int v3; // ebx

if ( a1 == 2 )
{
v3 = input_port_1_r(2LL);
result = (v3 << 8) + (unsigned int)input_port_4_r(2LL);
}
else if ( a1 == 4 )
{
result = (unsigned int)input_port_2_r(4LL) << 8;
}
else
{
result = 255LL;
if ( !a1 )
{
v2 = input_port_0_r();
result = (v2 << 8) + (unsigned int)input_port_3_r(0LL);
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1)
{
__int64 result; // rax
int v2; // ebx
int v3; // ebx

if ( a1 == 2 )
{
v3 = input_port_1_r(2LL);
result = (v3 << 8) + (unsigned int)input_port_4_r(2LL);
}
else if ( a1 == 4 )
{
result = (unsigned int)input_port_2_r(4LL) << 8;
}
else
{
result = 255LL;
if ( !a1 )
{
v2 = input_port_0_r();
result = (v2 << 8) + (unsigned int)input_port_3_r(0LL);
}
}
return result;
}

original_code:
static data_t snowbros_input_r(offs_t offset)
{
 int ans = 0xff;
 switch (offset)
 {
  case 0:
   ans = (input_port_0_r (offset) << 8) + (input_port_3_r (offset));
            break;
  case 2:
   ans = (input_port_1_r (offset) << 8) + (input_port_4_r (offset));
            break;
  case 4:
   ans = input_port_2_r (offset) << 8;
            break;
 }
    return ans;
}

canonical_original_code:
static unsigned int snowbros_input_r(unsigned int offset)
{
 int ans = 0xff;
 switch (offset)
 {
  case 0:
   ans = (input_port_0_r (offset) << 8) + (input_port_3_r (offset));
            break;
  case 2:
   ans = (input_port_1_r (offset) << 8) + (input_port_4_r (offset));
            break;
  case 4:
   ans = input_port_2_r (offset) << 8;
            break;
 }
    return ans;
}

variable_types: ans int


irqhandler

decompiled_code:
__int64 __fastcall irqhandler(unsigned int a1)
{
return cpu_set_irq_line(1LL, 0LL, a1);
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int a1)
{
return cpu_set_irq_line(1LL, 0LL, a1);
}

original_code:
static void irqhandler(int linestate)
{
 cpu_set_irq_line(1,0,linestate);
}

variable_types: linestate int


original_code:
void jailbrek_vh_convert_color_prom(unsigned char *palette, unsigned short *colortable,const unsigned char *color_prom) {
 int i;
 for ( i = 0; i < Machine->drv->total_colors; i++ ) {
  int bit0,bit1,bit2,bit3;
  bit0 = (color_prom[0] >> 0) & 0x01;
  bit1 = (color_prom[0] >> 1) & 0x01;
  bit2 = (color_prom[0] >> 2) & 0x01;
  bit3 = (color_prom[0] >> 3) & 0x01;
  *(palette++) = 0x0e * bit0 + 0x1f * bit1 + 0x43 * bit2 + 0x8f * bit3;
  bit0 = (color_prom[0] >> 4) & 0x01;
  bit1 = (color_prom[0] >> 5) & 0x01;
  bit2 = (color_prom[0] >> 6) & 0x01;
  bit3 = (color_prom[0] >> 7) & 0x01;
  *(palette++) = 0x0e * bit0 + 0x1f * bit1 + 0x43 * bit2 + 0x8f * bit3;
  bit0 = (color_prom[Machine->drv->total_colors] >> 0) & 0x01;
  bit1 = (color_prom[Machine->drv->total_colors] >> 1) & 0x01;
  bit2 = (color_prom[Machine->drv->total_colors] >> 2) & 0x01;
  bit3 = (color_prom[Machine->drv->total_colors] >> 3) & 0x01;
  *(palette++) = 0x0e * bit0 + 0x1f * bit1 + 0x43 * bit2 + 0x8f * bit3;
  color_prom++;
 }
 color_prom += Machine->drv->total_colors;
 for ( i = 0; i < (Machine->gfx[0]->total_colors * Machine->gfx[0]->color_granularity); i++ )
  (colortable[Machine->drv->gfxdecodeinfo[0].color_codes_start + i]) = ( *color_prom++ ) + 0x10;
 for ( i = 0; i < (Machine->gfx[1]->total_colors * Machine->gfx[1]->color_granularity); i++ )
  (colortable[Machine->drv->gfxdecodeinfo[1].color_codes_start + i]) = *color_prom++;
}

canonical_original_code:
void jailbrek_vh_convert_color_prom(unsigned char *palette, unsigned short *colortable, unsigned char *color_prom) {
 int i;
 for ( i = 0; i < Machine->drv->total_colors; i++ ) {
  int bit0; int bit1; int bit2; int bit3;
  bit0 = (color_prom[0] >> 0) & 0x01;
  bit1 = (color_prom[0] >> 1) & 0x01;
  bit2 = (color_prom[0] >> 2) & 0x01;
  bit3 = (color_prom[0] >> 3) & 0x01;
  *(palette++) = 0x0e * bit0 + 0x1f * bit1 + 0x43 * bit2 + 0x8f * bit3;
  bit0 = (color_prom[0] >> 4) & 0x01;
  bit1 = (color_prom[0] >> 5) & 0x01;
  bit2 = (color_prom[0] >> 6) & 0x01;
  bit3 = (color_prom[0] >> 7) & 0x01;
  *(palette++) = 0x0e * bit0 + 0x1f * bit1 + 0x43 * bit2 + 0x8f * bit3;
  bit0 = (color_prom[Machine->drv->total_colors] >> 0) & 0x01;
  bit1 = (color_prom[Machine->drv->total_colors] >> 1) & 0x01;
  bit2 = (color_prom[Machine->drv->total_colors] >> 2) & 0x01;
  bit3 = (color_prom[Machine->drv->total_colors] >> 3) & 0x01;
  *(palette++) = 0x0e * bit0 + 0x1f * bit1 + 0x43 * bit2 + 0x8f * bit3;
  color_prom++;
 }
 color_prom += Machine->drv->total_colors;
 for ( i = 0; i < (Machine->gfx[0]->total_colors * Machine->gfx[0]->color_granularity); i++ )
  (colortable[Machine->drv->gfxdecodeinfo[0].color_codes_start + i]) = ( *color_prom++ ) + 0x10;
 for ( i = 0; i < (Machine->gfx[1]->total_colors * Machine->gfx[1]->color_granularity); i++ )
  (colortable[Machine->drv->gfxdecodeinfo[1].color_codes_start + i]) = *color_prom++;
}

variable_types:
palette
unsigned char *

variable_types:
colortable
unsigned short *


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

result = interrupt_control;
*(_BYTE *)interrupt_control = 0;
return result;
}

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

result = interrupt_control;
*(_BYTE *)interrupt_control = 0;
return result;
}

original_code:
static void jailbrek_machine_init( void )
{
 interrupt_control[0] = 0;
}

canonical_original_code:
static void jailbrek_machine_init()
{
 interrupt_control[0] = 0;
}


decompiled_code:
__int64 jailbrek_vh_start()
{
size_t v0; // r12
void *v1; // rax
__int64 v2; // rax
unsigned int v3; // er8

v0 = videoram_size;
v1 = malloc(videoram_size);
dirtybuffer = v1;
if ( !v1 )
return 1;
memset(v1, 1, v0);
v2 = bitmap_alloc(
(unsigned int)(2 * *(_DWORD *)(*(_QWORD *)(Machine + 824LL) + 664LL)),
*(unsigned int *)(*(_QWORD *)(Machine + 824LL) + 668LL));
v3 = 0;
tmpbitmap = v2;
if ( !v2 )
{
free(dirtybuffer);
v3 = 1;
}
return v3;
}

canonical_decompiled_code:
__int64 func2()
{
size_t v0; // r12
void *v1; // rax
__int64 v2; // rax
unsigned int v3; // er8

v0 = videoram_size;
v1 = malloc(videoram_size);
dirtybuffer = v1;
if ( !v1 )
return 1;
memset(v1, 1, v0);
v2 = bitmap_alloc(
(unsigned int)(2 * *(_DWORD *)(*(_QWORD *)(Machine + 824LL) + 664LL)),
*(unsigned int *)(*(_QWORD *)(Machine + 824LL) + 668LL));
v3 = 0;
tmpbitmap = v2;
if ( !v2 )
{
free(dirtybuffer);
v3 = 1;
}
return v3;
}

original_code:
int jailbrek_vh_start( void ) {
 if ( ( dirtybuffer = (unsigned char*)malloc( videoram_size ) ) == 0 )
  return 1;
 memset( dirtybuffer, 1, videoram_size );
 if ( ( tmpbitmap = bitmap_alloc(Machine->drv->screen_width * 2,Machine->drv->screen_height) ) == 0 ) {
  free(dirtybuffer);
  return 1;
 }
 return 0;
}

canonical_original_code:
int jailbrek_vh_start() {
 if ( ( dirtybuffer = (unsigned char *)malloc( videoram_size ) ) == 0 )
  return 1;
 memset( dirtybuffer, 1, videoram_size );
 if ( ( tmpbitmap = bitmap_alloc(Machine->drv->screen_width * 2,Machine->drv->screen_height) ) == 0 ) {
  free(dirtybuffer);
  return 1;
 }
 return 0;
}


decompiled_code:
// attributes: thunk
__int64 init_jailbrek()
{
return konami1_decode();
}

canonical_decompiled_code:
__int64 func5()
{
return konami1_decode();
}

original_code:
static void init_jailbrek(void)
{
 konami1_decode();
}

canonical_original_code:
static void init_jailbrek()
{
 konami1_decode();
}


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

if ( (*(_BYTE *)interrupt_control & 1) != 0 )
result = nmi_interrupt();
else
result = ignore_interrupt();
return result;
}

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

if ( (*(_BYTE *)interrupt_control & 1) != 0 )
result = nmi_interrupt();
else
result = ignore_interrupt();
return result;
}

original_code:
static int jb_interrupt_nmi( void ) {
 if ( interrupt_control[0] & 1 )
  return nmi_interrupt();
 return ignore_interrupt();
}

canonical_original_code:
static int jb_interrupt_nmi() {
 if ( interrupt_control[0] & 1 )
  return nmi_interrupt();
 return ignore_interrupt();
}


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

if ( (*(_BYTE *)interrupt_control & 2) != 0 )
result = interrupt();
else
result = ignore_interrupt();
return result;
}

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

if ( (*(_BYTE *)interrupt_control & 2) != 0 )
result = interrupt();
else
result = ignore_interrupt();
return result;
}

original_code:
static int jb_interrupt( void ) {
 if ( interrupt_control[0] & 2 )
  return interrupt();
 return ignore_interrupt();
}

canonical_original_code:
static int jb_interrupt() {
 if ( interrupt_control[0] & 2 )
  return interrupt();
 return ignore_interrupt();
}


decompiled_code:
__int64 __fastcall jailbrek_speech_w(__int64 a1, unsigned int a2)
{
VLM5030_VCU(a2 & 1);
VLM5030_ST((a2 >> 1) & 1);
return VLM5030_RST((a2 & 4) != 0);
}

original_code:
static void jailbrek_speech_w(offs_t offset,data_t data) {
 VLM5030_VCU( data & 1 );
 VLM5030_ST( ( data >> 1 ) & 1 );
 VLM5030_RST( ( data >> 2 ) & 1 );
}

canonical_original_code:
static void jailbrek_speech_w(unsigned int offset, unsigned int data) {
 VLM5030_VCU( data & 1 );
 VLM5030_ST( ( data >> 1 ) & 1 );
 VLM5030_RST( ( data >> 2 ) & 1 );
}


decompiled_code:
_BOOL8 jailbrek_speech_r()
{
return (unsigned int)VLM5030_BSY() != 0;
}

canonical_decompiled_code:
_BOOL8 func9()
{
return (unsigned int)VLM5030_BSY() != 0;
}

original_code:
static data_t jailbrek_speech_r(offs_t offset) {
 return ( VLM5030_BSY() ? 1 : 0 );
}

canonical_original_code:
static unsigned int jailbrek_speech_r(unsigned int offset) {
 return ( VLM5030_BSY() ? 1 : 0 );
}


decompiled_code:
__int64 jailbrek_vh_stop()
{
free(dirtybuffer);
return bitmap_free(tmpbitmap);
}

canonical_decompiled_code:
__int64 func10()
{
free(dirtybuffer);
return bitmap_free(tmpbitmap);
}

original_code:
void jailbrek_vh_stop( void ) {
 free( dirtybuffer );
 bitmap_free( tmpbitmap );
}

canonical_original_code:
void jailbrek_vh_stop() {
 free( dirtybuffer );
 bitmap_free( tmpbitmap );
}


decompiled_code:
__int64 __fastcall update_pb4(int a1)
{
unsigned int v1; // er8
int v2; // eax
int v3; // ecx
int v5; // eax
int v6; // eax
unsigned int v7; // er8
int v8; // eax

v1 = level_13;
if ( (sound_latch_b & 0x10) != 0 )
{
if ( level_13 > 32766 )
return v1;
v2 = (int)(float)((float)(0x7FFF - level_13) / *(float *)&pb4_charge_time);
v3 = counter_12 - v2;
if ( counter_12 - v2 > 0 )
{
LABEL_4:
counter_12 = v3;
return v1;
}
v8 = (v2 - counter_12) / a1 + 1;
v7 = v8 + level_13;
counter_12 = v8 * a1 + v3;
if ( v8 + level_13 > 0x7FFF )
{
level_13 = 0x7FFF;
return 0x7FFF;
}
}
else
{
if ( level_13 <= 0 )
return v1;
v5 = (int)(float)((float)level_13 / *(float *)&pb4_discharge_time);
v3 = counter_12 - v5;
if ( counter_12 - v5 > 0 )
goto LABEL_4;
v6 = (v5 - counter_12) / a1 + 1;
counter_12 = v3 + v6 * a1;
v7 = level_13 - v6;
if ( level_13 - v6 < 0 )
{
level_13 = 0;
return 0;
}
}
level_13 = v7;
return v7;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1)
{
unsigned int v1; // er8
int v2; // eax
int v3; // ecx
int v5; // eax
int v6; // eax
unsigned int v7; // er8
int v8; // eax

v1 = level_13;
if ( (sound_latch_b & 0x10) != 0 )
{
if ( level_13 > 32766 )
return v1;
v2 = (int)(float)((float)(0x7FFF - level_13) / *(float *)&pb4_charge_time);
v3 = counter_12 - v2;
if ( counter_12 - v2 > 0 )
{
LABEL_4:
counter_12 = v3;
return v1;
}
v8 = (v2 - counter_12) / a1 + 1;
v7 = v8 + level_13;
counter_12 = v8 * a1 + v3;
if ( v8 + level_13 > 0x7FFF )
{
level_13 = 0x7FFF;
return 0x7FFF;
}
}
else
{
if ( level_13 <= 0 )
return v1;
v5 = (int)(float)((float)level_13 / *(float *)&pb4_discharge_time);
v3 = counter_12 - v5;
if ( counter_12 - v5 > 0 )
goto LABEL_4;
v6 = (v5 - counter_12) / a1 + 1;
counter_12 = v3 + v6 * a1;
v7 = level_13 - v6;
if ( level_13 - v6 < 0 )
{
level_13 = 0;
return 0;
}
}
level_13 = v7;
return v7;
}

original_code:
static inline int update_pb4(int samplerate)
{
 static int counter, level;
 if( sound_latch_b & 0x10 )
 {
  if( level < 32767 )
  {
   counter -= (int)((32767 - level) / pb4_charge_time);
   if( counter <= 0 )
   {
    int n = (-counter / samplerate) + 1;
    counter += n * samplerate;
    if( (level += n) > 32767 )
     level = 32767;
   }
  }
 }
 else
 {
  if( level > 0 )
  {
   counter -= (int)((level - 0) / pb4_discharge_time);
   if( counter <= 0 )
   {
    int n = (-counter / samplerate) + 1;
    counter += n * samplerate;
    if( (level -= n) < 0)
     level = 0;
   }
  }
 }
 return level;
}

canonical_original_code:
static inline int update_pb4(int samplerate)
{
 static int counter; static int level;
 if( sound_latch_b & 0x10 )
 {
  if( level < 32767 )
  {
   counter -= (int)((32767 - level) / pb4_charge_time);
   if( counter <= 0 )
   {
    int n = (-counter / samplerate) + 1;
    counter += n * samplerate;
    if( (level += n) > 32767 )
     level = 32767;
   }
  }
 }
 else
 {
  if( level > 0 )
  {
   counter -= (int)((level - 0) / pb4_discharge_time);
   if( counter <= 0 )
   {
    int n = (-counter / samplerate) + 1;
    counter += n * samplerate;
    if( (level -= n) < 0)
     level = 0;
   }
  }
 }
 return level;
}

variable_types: samplerate int

variable_types: counter int

variable_types: level int

variable_types: n int