main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rax
int v4; // ebx
const char *v5; // rax
const char *v6; // rax
const char *v7; // rax
const char *v8; // rax
const char *v9; // rax
const char *v10; // rax
const char *v11; // rax
const char *v12; // rax
const char *v13; // rdi
const char *v14; // rdi
int result; // eax
unsigned int v16; // ebx
const char *v17; // rax
const char *v18; // rax
const char *v19; // rdi
unsigned int v20; // eax
unsigned int v21; // ebp
int v22; // ebp
unsigned int v23; // eax
const char *v24; // rax
unsigned int v25; // eax
const char *v26; // rax
unsigned int v27; // eax
const char *v28; // rax
unsigned int v29; // eax
const char *v30; // rax
unsigned int v31; // eax
const char *v32; // rax
unsigned int v33; // eax
const char *v34; // rax
unsigned int v35; // eax
const char *v36; // rax
unsigned int v37; // eax
const char *v38; // rax
const char *v39; // rax
__int64 v40; // rax
int v41; // edx
const char *v42; // rax
const char *v43; // rax
const char *v44; // rax
const char *v45; // rdi
const char *v46; // rdi
const char *v47; // rdi
const char *v48; // rax
const char *v49; // rax
const char *v50; // rax
const char *v51; // rax
const char *v52; // rax
const char *v53; // rax
unsigned int v54; // eax
unsigned int v55; // eax
unsigned int v56; // eax
unsigned int v57; // eax
unsigned int v58; // eax
unsigned int v59; // eax
unsigned int v60; // eax
const char *v61; // rdi
const char *v62; // rax
int v63; // eax
unsigned __int16 v64; // [rsp+1Eh] [rbp-3Ah]
__int64 v65; // [rsp+20h] [rbp-38h] BYREF
__int64 v66; // [rsp+28h] [rbp-30h] BYREF
unsigned int v67; // [rsp+30h] [rbp-28h] BYREF
unsigned int v68; // [rsp+34h] [rbp-24h]
unsigned int v69; // [rsp+38h] [rbp-20h]
unsigned int v70; // [rsp+3Ch] [rbp-1Ch]

cls(argc, argv, envp);
puts("Welcome to VMXOS ");
puts(vmxos_version);
puts(" build ");
v3 = (const char *)dec2string((unsigned int)vmxos_build);
puts(v3);
puts("\n");
puts("Inizialising the kernel modules\n\n");
disable_A20();
v4 = check_A20();
puts("A20 : ");
if ( v4 )
{
puts("enabled\n");
}
else
{
puts("not enabled -> ");
puts("enabling A20 gate... ");
enable_A20();
if ( !(unsigned int)check_A20() )
return puts("failed\n");
puts("successed\n");
}
cpuid(&v67);
puts("CPUID : EAX -> ");
v5 = (const char *)hex2string(v67);
puts(v5);
puts(" = ");
v6 = (const char *)bin2string(v67);
puts(v6);
puts("\n");
puts(" : EBX -> ");
v7 = (const char *)hex2string(v68);
puts(v7);
puts(" = ");
v8 = (const char *)bin2string(v68);
puts(v8);
puts("\n");
puts(" : ECX -> ");
v9 = (const char *)hex2string(v69);
puts(v9);
puts(" = ");
v10 = (const char *)bin2string(v69);
puts(v10);
puts("\n");
puts(" : EDX -> ");
v11 = (const char *)hex2string(v70);
puts(v11);
puts(" = ");
v12 = (const char *)bin2string(v70);
puts(v12);
puts("\n");
puts("VMX : ");
v13 = "not supported\n";
if ( (v69 & 0x20) != 0 )
v13 = "supported\n";
puts(v13);
puts("MSR RW: ");
v14 = "not supported\n";
if ( (v70 & 0x20) != 0 )
v14 = "supported\n";
result = puts(v14);
if ( (v69 & 0x20) != 0 && (v70 & 0x20) != 0 )
{
v16 = vmx_read_eflags();
puts("EFLAGS: ");
v17 = (const char *)hex2string(v16);
puts(v17);
puts(" = ");
v18 = (const char *)bin2string(v16);
puts(v18);
puts("\n");
puts("VM86 Mode is ");
v19 = "on\n";
if ( (v16 & 0x20000) == 0 )
v19 = "off\n";
puts(v19);
v20 = vmx_read_cr4();
BYTE1(v20) |= 0x20u;
v21 = v20;
puts("Enabling CR4.VME ");
vmx_write_cr4(v21);
puts("[done]\n");
v22 = vmx_read_cr0();
puts("Enabling CR0.PE ");
vmx_write_cr0(v22 | 1u);
puts("[done]\n");
puts("Enabling CR0.NE ");
vmx_write_cr0(v22 | 0x21u);
puts("[done]\n");
puts("Enabling paging ");
enable_paging();
puts("[done]\n");
puts("CR0 : ");
v23 = vmx_read_cr0();
v24 = (const char *)hex2string(v23);
puts(v24);
puts(" = ");
v25 = vmx_read_cr0();
v26 = (const char *)bin2string(v25);
puts(v26);
puts("\n");
puts("CR2 : ");
v27 = vmx_read_cr2();
v28 = (const char *)hex2string(v27);
puts(v28);
puts(" = ");
v29 = vmx_read_cr2();
v30 = (const char *)bin2string(v29);
puts(v30);
puts("\n");
puts("CR3 : ");
v31 = vmx_read_cr3();
v32 = (const char *)hex2string(v31);
puts(v32);
puts(" = ");
v33 = vmx_read_cr3();
v34 = (const char *)bin2string(v33);
puts(v34);
puts("\n");
puts("CR4 : ");
v35 = vmx_read_cr4();
v36 = (const char *)hex2string(v35);
puts(v36);
puts(" = ");
v37 = vmx_read_cr4();
v38 = (const char *)bin2string(v37);
puts(v38);
puts("\n");
remap_pic(32LL, 40LL);
mask_irq(0LL);
puts("Inizializing interrupt handlers ");
init_exceptions();
puts("[done]\n");
sti();
v65 = 0LL;
puts("Reading MSR 0xA3");
vmx_read_msr(58LL, &v65);
puts(" -> ");
v39 = (const char *)bin2string((unsigned int)v65);
puts(v39);
puts("\n");
if ( (v65 & 1) != 0 )
{
puts("MSR 0x3A already locked\n");
}
else
{
puts("Locking MSR...\n");
v65 |= 1uLL;
vmx_write_msr(58LL, &v65);
puts("MSR 0x3A locked -> ");
v62 = (const char *)bin2string((unsigned int)v65);
puts(v62);
puts("\n");
}
vmx_read_msr(1152LL, &v65);
v40 = 647168LL;
vmxon_ptr = 647168LL;
vmcs_ptr = 651264LL;
do
{
*(_DWORD *)v40 = 0;
v40 += 4LL;
}
while ( v40 != 651264 );
v41 = v65;
MEMORY[0x9E000] = v65;
do
{
*(_DWORD *)v40 = 0;
v40 += 4LL;
}
while ( v40 != 655360 );
MEMORY[0x9F000] = v41;
puts("VMX revID: ");
v42 = (const char *)hex2string(MEMORY[0x9F000]);
puts(v42);
puts(" - ");
puts("vmx region at: ");
v43 = (const char *)hex2string(vmxon_ptr);
puts(v43);
puts(" - ");
puts("vmcs region at: ");
v44 = (const char *)hex2string(vmcs_ptr);
puts(v44);
puts("\n");
puts("Entering VMX mode ");
v45 = "failed\n";
if ( (unsigned int)vmx_vmxon() )
v45 = "success\n";
puts(v45);
puts("Clearing VMCS region ");
v46 = "failed\n";
if ( (unsigned int)vmx_vmclear() )
v46 = "success\n";
puts(v46);
puts("Loading VMCS pointer ");
v47 = "failed\n";
if ( (unsigned int)vmx_vmptrld() )
v47 = "success\n";
puts(v47);
break_point();
sgdt();
puts("GDTR base : ");
v48 = (const char *)hex2string(*(unsigned int *)((char *)&gdtr + 2));
puts(v48);
puts("\n");
puts("GDTR limit: ");
v49 = (const char *)hex2string(gdtr);
puts(v49);
puts("\n");
puts("IDTR base : ");
v50 = (const char *)hex2string(*(unsigned int *)((char *)&idtr + 2));
puts(v50);
puts("\n");
puts("IDTR limit: ");
v51 = (const char *)hex2string(idtr);
puts(v51);
puts("\n");
v66 = 0LL;
vmx_vmwrite(3072LL, (unsigned __int16)__ES__);
vmx_vmwrite(3074LL, (unsigned __int16)__CS__);
vmx_vmwrite(3076LL, (unsigned __int16)__SS__);
vmx_vmwrite(3078LL, (unsigned __int16)__DS__);
vmx_vmwrite(3080LL, (unsigned __int16)__FS__);
vmx_vmwrite(3082LL, (unsigned __int16)__GS__);
vmx_vmwrite(3084LL, 24LL);
vmx_vmwrite(10240LL, 0xFFFFFFFFLL);
vmx_vmwrite(10241LL, 0xFFFFFFFFLL);
vmx_read_msr(473LL, &v66);
vmx_vmwrite(10242LL, (unsigned int)v66);
vmx_vmwrite(10243LL, HIDWORD(v66));
puts("Guest_IA32_DEBUGCTL_full: ");
v52 = (const char *)hex2string((unsigned int)v66);
puts(v52);
puts("\n");
puts("Guest_IA32_DEBUGCTL_high: ");
v53 = (const char *)hex2string(HIDWORD(v66));
puts(v53);
puts("\n");
vmx_vmwrite(0x4000LL, 31LL);
vmx_vmwrite(16386LL, 67234290LL);
vmx_vmwrite(16396LL, 224767LL);
vmx_vmwrite(16402LL, 4607LL);
vmx_vmwrite(16398LL, 0LL);
vmx_vmwrite(16400LL, 0LL);
vmx_vmwrite(16404LL, 0LL);
vmx_vmwrite(16406LL, 0LL);
vmx_vmwrite(8208LL, 0LL);
vmx_vmwrite(8209LL, 0LL);
vmx_vmwrite(16390LL, 0LL);
vmx_vmwrite(16392LL, 0LL);
vmx_vmwrite(16394LL, 0LL);
vmx_vmwrite(24584LL, 0LL);
vmx_vmwrite(24586LL, 0LL);
vmx_vmwrite(24588LL, 0LL);
vmx_vmwrite(24590LL, 0LL);
vmx_vmwrite(2048LL, (unsigned __int16)__ES__);
vmx_vmwrite(2050LL, (unsigned __int16)__CS__);
vmx_vmwrite(2052LL, (unsigned __int16)__SS__);
vmx_vmwrite(2054LL, (unsigned __int16)__DS__);
vmx_vmwrite(2056LL, (unsigned __int16)__FS__);
vmx_vmwrite(2058LL, (unsigned __int16)__GS__);
__asm { sldt [rsp+58h+var_3A] }
vmx_vmwrite(2060LL, v64);
vmx_vmwrite(2062LL, 24LL);
vmx_vmwrite(18434LL, 0xFFFFFFFFLL);
vmx_vmwrite(18432LL, 0xFFFFFFFFLL);
vmx_vmwrite(18436LL, 0xFFFFFFFFLL);
vmx_vmwrite(18438LL, 0xFFFFFFFFLL);
vmx_vmwrite(18440LL, 0xFFFFFFFFLL);
vmx_vmwrite(18442LL, 0xFFFFFFFFLL);
vmx_vmwrite(18444LL, 0xFFFFLL);
vmx_vmwrite(18446LL, 255LL);
vmx_vmwrite(18448LL, gdtr);
vmx_vmwrite(18450LL, idtr);
vmx_vmwrite(26650LL, 1024LL);
vmx_vmwrite(18468LL, 0LL);
vmx_vmwrite(18470LL, 0LL);
vmx_vmwrite(18454LL, 49307LL);
vmx_vmwrite(18452LL, 49299LL);
vmx_vmwrite(18456LL, 49299LL);
vmx_vmwrite(18458LL, 49299LL);
vmx_vmwrite(18460LL, 49299LL);
vmx_vmwrite(18462LL, 49299LL);
vmx_vmwrite(18464LL, 0x10000LL);
vmx_vmwrite(18466LL, 139LL);
vmx_read_msr(372LL, &v66);
vmx_vmwrite(18474LL, (unsigned int)v66);
v54 = vmx_read_cr0();
vmx_vmwrite(26624LL, v54);
v55 = vmx_read_cr3();
vmx_vmwrite(26626LL, v55);
v56 = vmx_read_cr4();
vmx_vmwrite(26628LL, v56);
vmx_vmwrite(26630LL, 0LL);
vmx_vmwrite(26632LL, 0LL);
vmx_vmwrite(26634LL, 0LL);
vmx_vmwrite(26636LL, 0LL);
vmx_vmwrite(26638LL, 0LL);
vmx_vmwrite(26640LL, 0LL);
vmx_vmwrite(26642LL, 0LL);
vmx_vmwrite(26644LL, 0LL);
vmx_vmwrite(26646LL, *(unsigned int *)((char *)&gdtr + 2));
vmx_vmwrite(26648LL, *(unsigned int *)((char *)&idtr + 2));
v57 = vmx_read_eflags();
vmx_vmwrite(26656LL, v57);
vmx_read_msr(373LL, &v66);
vmx_vmwrite(26660LL, (unsigned int)v66);
vmx_read_msr(374LL, &v66);
vmx_vmwrite(26662LL, (unsigned int)v66);
v58 = vmx_read_cr0();
vmx_vmwrite(27648LL, v58);
v59 = vmx_read_cr3();
vmx_vmwrite(27650LL, v59);
v60 = vmx_read_cr4();
vmx_vmwrite(27652LL, v60);
vmx_vmwrite(27654LL, 0LL);
vmx_vmwrite(27656LL, 0LL);
vmx_vmwrite(27658LL, 0LL);
vmx_vmwrite(27660LL, *(unsigned int *)((char *)&gdtr + 2));
vmx_vmwrite(27662LL, *(unsigned int *)((char *)&idtr + 2));
vmx_vmwrite(26652LL, 589824LL);
vmx_vmwrite(26654LL, (unsigned int)guest_entry);
vmx_vmwrite(27668LL, 589824LL);
vmx_vmwrite(27670LL, (unsigned int)host_entry);
if ( !(unsigned int)vmx_vmlaunch() )
{
puts("IMPOSSIBILE LANCIARE LA VM\nError #");
v63 = vmx_vmread(17408LL);
puts((const char *)vmx_instruction_errors[v63]);
puts("\n");
}
puts("Leaving VMX mode ");
v61 = "success\n";
if ( !(unsigned int)vmx_vmxoff() )
v61 = "failed\n";
puts(v61);
puts("Type something: ");
while ( 1 )
;
}
return result;
}

original_code:
void main()
{
 CPUID_t cpu;
 CR4_t cr4;
 CR0_t cr0;
 EFLAGS_t eflags;
 unsigned int a20;
 cls();
 puts("Welcome to VMXOS "); puts(vmxos_version); puts(" build "); puts(dec2string(vmxos_build)); puts("\n");
 puts("Inizialising the kernel modules\n\n");
 disable_A20();
 a20 = check_A20();
 puts("A20   : "); puts(a20 ? "enabled\n" : "not enabled -> ");
 if (!a20)
 {
  puts("enabling A20 gate... ");
  enable_A20();
  if (check_A20())
  {
   puts("successed\n");
  }
  else
  {
   puts("failed\n");
   return;
  }
 }
 cpuid(&cpu);
 puts("CPUID : EAX -> "); puts(hex2string(cpu.eax)); puts(" = "); puts(bin2string(cpu.eax)); puts("\n");
 puts("      : EBX -> "); puts(hex2string(cpu.ebx)); puts(" = "); puts(bin2string(cpu.ebx)); puts("\n");
 puts("      : ECX -> "); puts(hex2string(*(unsigned int *) &cpu.ecx)); puts(" = "); puts(bin2string(*(unsigned int *) &cpu.ecx)); puts("\n");
 puts("      : EDX -> "); puts(hex2string(*(unsigned int *) &cpu.edx)); puts(" = "); puts(bin2string(*(unsigned int *) &cpu.edx)); puts("\n");
 puts("VMX   : "); puts(cpu.ecx.b05_VMX ? "supported\n" : "not supported\n");
 puts("MSR RW: "); puts(cpu.edx.b05_MSR ? "supported\n" : "not supported\n");
 if (!cpu.ecx.b05_VMX || !cpu.edx.b05_MSR) return;
 *(unsigned int *) &eflags = vmx_read_eflags();
 puts("EFLAGS: "); puts(hex2string(*(unsigned int *) &eflags)); puts(" = "); puts(bin2string(*(unsigned int *) &eflags)); puts("\n");
 puts("VM86 Mode is "); puts(eflags.b17_VM ? "on\n" : "off\n");
 *(unsigned int *) &cr4 = vmx_read_cr4();
 cr4.b13_VMXE = 1;
 puts("Enabling CR4.VME "); vmx_write_cr4(*(unsigned int *) &cr4); puts("[done]\n");
 *(unsigned int *) &cr0 = vmx_read_cr0();
 cr0.b00_PE = 1; puts("Enabling CR0.PE "); vmx_write_cr0(*(unsigned int *) &cr0); puts("[done]\n");
 cr0.b05_NE = 1; puts("Enabling CR0.NE "); vmx_write_cr0(*(unsigned int *) &cr0); puts("[done]\n");
 puts("Enabling paging "); enable_paging(); puts("[done]\n");
 puts("CR0   : "); puts(hex2string(vmx_read_cr0())); puts(" = "); puts(bin2string(vmx_read_cr0())); puts("\n");
 puts("CR2   : "); puts(hex2string(vmx_read_cr2())); puts(" = "); puts(bin2string(vmx_read_cr2())); puts("\n");
 puts("CR3   : "); puts(hex2string(vmx_read_cr3())); puts(" = "); puts(bin2string(vmx_read_cr3())); puts("\n");
 puts("CR4   : "); puts(hex2string(vmx_read_cr4())); puts(" = "); puts(bin2string(vmx_read_cr4())); puts("\n");
 remap_pic(0x20, 0x28);
 mask_irq(0);
 puts("Inizializing interrupt handlers "); init_exceptions(); puts("[done]\n");
 sti();
 unsigned long long nl = 0;
 puts("Reading MSR 0xA3");
 vmx_read_msr(0x3A, &nl);
 puts(" -> "); puts(bin2string(nl)); puts("\n");
 if (!(nl & 1))
 {
  puts("Locking MSR...\n");
  nl |= 1;
  vmx_write_msr(0x3A, &nl);
  puts("MSR 0x3A locked -> "); puts(bin2string(nl)); puts("\n");
 }
 else
 {
  puts("MSR 0x3A already locked\n");
 }
 vmx_read_msr(0x480, &nl);
 vmxon_ptr = 0x9E000;
 vmcs_ptr = 0x9F000;
 int i;
 unsigned int* reg;
 reg = (unsigned int*)vmxon_ptr;
 for(i = 0; i < 1024; i++) reg[i] = 0;
 reg[0] = nl;
 reg = (unsigned int*)vmcs_ptr;
 for(i = 0; i < 1024; i++) reg[i] = 0;
 reg[0] = nl;
 puts("VMX revID: "); puts(hex2string(*reg));
 puts(" - "); puts("vmx region at: "); puts(hex2string(vmxon_ptr));
 puts(" - "); puts("vmcs region at: "); puts(hex2string(vmcs_ptr)); puts("\n");
 puts("Entering VMX mode "); puts(vmx_vmxon() ? "success\n" : "failed\n");
 puts("Clearing VMCS region "); puts(vmx_vmclear() ? "success\n" : "failed\n");
 puts("Loading VMCS pointer "); puts(vmx_vmptrld() ? "success\n" : "failed\n");
 break_point();
 sgdt();
 puts("GDTR base : "); puts(hex2string(gdtr.base)); puts("\n");
 puts("GDTR limit: "); puts(hex2string(gdtr.limit)); puts("\n");
 puts("IDTR base : "); puts(hex2string(idtr.base)); puts("\n");
 puts("IDTR limit: "); puts(hex2string(idtr.limit)); puts("\n");
 unsigned char u8 = 0;
 unsigned short u16 = 0;
 unsigned int u32 = 0;
 unsigned long long u64 = 0;
 asm("mov %%es, %0" : "=m" (u16)); vmx_vmwrite(0x00000C00, u16);
 asm("mov %%cs, %0" : "=m" (u16)); vmx_vmwrite(0x00000C02, u16);
 asm("mov %%ss, %0" : "=m" (u16)); vmx_vmwrite(0x00000C04, u16);
 asm("mov %%ds, %0" : "=m" (u16)); vmx_vmwrite(0x00000C06, u16);
 asm("mov %%fs, %0" : "=m" (u16)); vmx_vmwrite(0x00000C08, u16);
 asm("mov %%gs, %0" : "=m" (u16)); vmx_vmwrite(0x00000C0A, u16);
                                  vmx_vmwrite(0x00000C0C, 0x18);
 vmx_vmwrite(0x00002800, 0xFFFFFFFF);
 vmx_vmwrite(0x00002801, 0xFFFFFFFF);
 vmx_read_msr(0x000001D9, &u64);
 vmx_vmwrite(0x00002802, u64);
 vmx_vmwrite(0x00002803, u64>>32);
 puts("Guest_IA32_DEBUGCTL_full: "); puts(hex2string(u64)); puts("\n");
 puts("Guest_IA32_DEBUGCTL_high: "); puts(hex2string(u64>>32)); puts("\n");
 vmx_vmwrite(0x00004000, 0x1f);
 vmx_vmwrite(0x00004002, 0x0401E9F2);
 vmx_vmwrite(0x0000400C, 0x36dff);
 vmx_vmwrite(0x00004012, 0x11ff);
 vmx_vmwrite(0x0000400E, 0);
 vmx_vmwrite(0x00004010, 0);
 vmx_vmwrite(0x00004014, 0);
 vmx_vmwrite(0x00004016, 0);
 vmx_vmwrite(0x00002010, 0);
 vmx_vmwrite(0x00002011, 0);
 vmx_vmwrite(0x00004006, 0);
 vmx_vmwrite(0x00004008, 0);
 vmx_vmwrite(0x0000400A, 0);
 vmx_vmwrite(0x00006008, 0);
 vmx_vmwrite(0x0000600A, 0);
 vmx_vmwrite(0x0000600C, 0);
 vmx_vmwrite(0x0000600E, 0);
 asm("mov %%es, %0" : "=m" (u16)); vmx_vmwrite(0x00000800, u16);
 asm("mov %%cs, %0" : "=m" (u16)); vmx_vmwrite(0x00000802, u16);
 asm("mov %%ss, %0" : "=m" (u16)); vmx_vmwrite(0x00000804, u16);
 asm("mov %%ds, %0" : "=m" (u16)); vmx_vmwrite(0x00000806, u16);
 asm("mov %%fs, %0" : "=m" (u16)); vmx_vmwrite(0x00000808, u16);
 asm("mov %%gs, %0" : "=m" (u16)); vmx_vmwrite(0x0000080A, u16);
 asm("sldt %0" : "=m" (u16)); vmx_vmwrite(0x0000080C, u16);
                                  vmx_vmwrite(0x0000080E, 0x18);
 vmx_vmwrite(0x00004802, 0XFFFFFFFF);
 vmx_vmwrite(0x00004800, 0XFFFFFFFF);
 vmx_vmwrite(0x00004804, 0XFFFFFFFF);
 vmx_vmwrite(0x00004806, 0XFFFFFFFF);
 vmx_vmwrite(0x00004808, 0XFFFFFFFF);
 vmx_vmwrite(0x0000480A, 0XFFFFFFFF);
 vmx_vmwrite(0x0000480C, 0xFFFF);
 vmx_vmwrite(0x0000480E, 0xFF);
 vmx_vmwrite(0x00004810, gdtr.limit);
 vmx_vmwrite(0x00004812, idtr.limit);
 vmx_vmwrite(0x0000681A, 0x400);
 vmx_vmwrite(0x00004824, 0);
 vmx_vmwrite(0x00004826, 0);
 vmx_vmwrite(0x00004816, 0xC09B);
 vmx_vmwrite(0x00004814, 0xC093);
 vmx_vmwrite(0x00004818, 0xC093);
 vmx_vmwrite(0x0000481A, 0xC093);
 vmx_vmwrite(0x0000481C, 0xC093);
 vmx_vmwrite(0x0000481E, 0xC093);
 vmx_vmwrite(0x00004820, 0x00010000);
 vmx_vmwrite(0x00004822, 0x8b);
 vmx_read_msr(0x00000174, &u64);
 vmx_vmwrite(0x0000482A, u64);
 vmx_vmwrite(0x00006800, vmx_read_cr0());
 vmx_vmwrite(0x00006802, vmx_read_cr3());
 vmx_vmwrite(0x00006804, vmx_read_cr4());
 vmx_vmwrite(0x00006806, 0);
 vmx_vmwrite(0x00006808, 0);
 vmx_vmwrite(0x0000680A, 0);
 vmx_vmwrite(0x0000680C, 0);
 vmx_vmwrite(0x0000680E, 0);
 vmx_vmwrite(0x00006810, 0);
 vmx_vmwrite(0x00006812, 0);
 vmx_vmwrite(0x00006814, 0);
 vmx_vmwrite(0x00006816, gdtr.base);
 vmx_vmwrite(0x00006818, idtr.base);
 *(unsigned int *) &eflags = vmx_read_eflags();
 vmx_vmwrite(0x00006820, *(unsigned int *) &eflags);
 vmx_read_msr(0x00000175, &u64);
 vmx_vmwrite(0x00006824, u64);
 vmx_read_msr(0x00000176, &u64);
 vmx_vmwrite(0x00006826, u64);
 vmx_vmwrite(0x00006C00, vmx_read_cr0());
 vmx_vmwrite(0x00006C02, vmx_read_cr3());
 vmx_vmwrite(0x00006C04, vmx_read_cr4());
 vmx_vmwrite(0x00006C06, 0);
 vmx_vmwrite(0x00006C08, 0);
 vmx_vmwrite(0x00006C0A, 0);
 vmx_vmwrite(0x00006C0C, gdtr.base);
 vmx_vmwrite(0x00006C0E, idtr.base);
 vmx_vmwrite(0x0000681C, 0x90000);
 vmx_vmwrite(0x0000681E, guest_entry);
 vmx_vmwrite(0x00006C14, 0x90000);
 vmx_vmwrite(0x00006C16, host_entry);
 if (!vmx_vmlaunch())
 {
  puts("IMPOSSIBILE LANCIARE LA VM\nError #");
  puts(vmx_instruction_errors[vmx_vmread(0x00004400)]);
  puts("\n");
 }
 puts("Leaving VMX mode "); puts(vmx_vmxoff() ? "success\n" : "failed\n");
 puts("Type something: ");
 while(1);
}

canonical_original_code:
void main()
{
 struct CPUID_t cpu;
 struct CR4_t cr4;
 struct CR0_t cr0;
 struct EFLAGS_t eflags;
 unsigned int a20;
 cls();
 puts("Welcome to VMXOS "); puts(vmxos_version); puts(" build "); puts(dec2string(vmxos_build)); puts("\n");
 puts("Inizialising the kernel modules\n\n");
 disable_A20();
 a20 = check_A20();
 puts("A20   : "); puts(a20 ? "enabled\n" : "not enabled -> ");
 if (!a20)
 {
  puts("enabling A20 gate... ");
  enable_A20();
  if (check_A20())
  {
   puts("successed\n");
  }
  else
  {
   puts("failed\n");
   return;
  }
 }
 cpuid(&cpu);
 puts("CPUID : EAX -> "); puts(hex2string(cpu.eax)); puts(" = "); puts(bin2string(cpu.eax)); puts("\n");
 puts("      : EBX -> "); puts(hex2string(cpu.ebx)); puts(" = "); puts(bin2string(cpu.ebx)); puts("\n");
 puts("      : ECX -> "); puts(hex2string(*(unsigned int *) &cpu.ecx)); puts(" = "); puts(bin2string(*(unsigned int *) &cpu.ecx)); puts("\n");
 puts("      : EDX -> "); puts(hex2string(*(unsigned int *) &cpu.edx)); puts(" = "); puts(bin2string(*(unsigned int *) &cpu.edx)); puts("\n");
 puts("VMX   : "); puts(cpu.ecx.b05_VMX ? "supported\n" : "not supported\n");
 puts("MSR RW: "); puts(cpu.edx.b05_MSR ? "supported\n" : "not supported\n");
 if (!cpu.ecx.b05_VMX || !cpu.edx.b05_MSR) return;
 *(unsigned int *) &eflags = vmx_read_eflags();
 puts("EFLAGS: "); puts(hex2string(*(unsigned int *) &eflags)); puts(" = "); puts(bin2string(*(unsigned int *) &eflags)); puts("\n");
 puts("VM86 Mode is "); puts(eflags.b17_VM ? "on\n" : "off\n");
 *(unsigned int *) &cr4 = vmx_read_cr4();
 cr4.b13_VMXE = 1;
 puts("Enabling CR4.VME "); vmx_write_cr4(*(unsigned int *) &cr4); puts("[done]\n");
 *(unsigned int *) &cr0 = vmx_read_cr0();
 cr0.b00_PE = 1; puts("Enabling CR0.PE "); vmx_write_cr0(*(unsigned int *) &cr0); puts("[done]\n");
 cr0.b05_NE = 1; puts("Enabling CR0.NE "); vmx_write_cr0(*(unsigned int *) &cr0); puts("[done]\n");
 puts("Enabling paging "); enable_paging(); puts("[done]\n");
 puts("CR0   : "); puts(hex2string(vmx_read_cr0())); puts(" = "); puts(bin2string(vmx_read_cr0())); puts("\n");
 puts("CR2   : "); puts(hex2string(vmx_read_cr2())); puts(" = "); puts(bin2string(vmx_read_cr2())); puts("\n");
 puts("CR3   : "); puts(hex2string(vmx_read_cr3())); puts(" = "); puts(bin2string(vmx_read_cr3())); puts("\n");
 puts("CR4   : "); puts(hex2string(vmx_read_cr4())); puts(" = "); puts(bin2string(vmx_read_cr4())); puts("\n");
 remap_pic(0x20, 0x28);
 mask_irq(0);
 puts("Inizializing interrupt handlers "); init_exceptions(); puts("[done]\n");
 sti();
 unsigned long long nl = 0;
 puts("Reading MSR 0xA3");
 vmx_read_msr(0x3A, &nl);
 puts(" -> "); puts(bin2string(nl)); puts("\n");
 if (!(nl & 1))
 {
  puts("Locking MSR...\n");
  nl |= 1;
  vmx_write_msr(0x3A, &nl);
  puts("MSR 0x3A locked -> "); puts(bin2string(nl)); puts("\n");
 }
 else
 {
  puts("MSR 0x3A already locked\n");
 }
 vmx_read_msr(0x480, &nl);
 vmxon_ptr = 0x9E000;
 vmcs_ptr = 0x9F000;
 int i;
 unsigned int *reg;
 reg = (unsigned int *)vmxon_ptr;
 for(i = 0; i < 1024; i++) reg[i] = 0;
 reg[0] = nl;
 reg = (unsigned int *)vmcs_ptr;
 for(i = 0; i < 1024; i++) reg[i] = 0;
 reg[0] = nl;
 puts("VMX revID: "); puts(hex2string(*reg));
 puts(" - "); puts("vmx region at: "); puts(hex2string(vmxon_ptr));
 puts(" - "); puts("vmcs region at: "); puts(hex2string(vmcs_ptr)); puts("\n");
 puts("Entering VMX mode "); puts(vmx_vmxon() ? "success\n" : "failed\n");
 puts("Clearing VMCS region "); puts(vmx_vmclear() ? "success\n" : "failed\n");
 puts("Loading VMCS pointer "); puts(vmx_vmptrld() ? "success\n" : "failed\n");
 break_point();
 sgdt();
 puts("GDTR base : "); puts(hex2string(gdtr.base)); puts("\n");
 puts("GDTR limit: "); puts(hex2string(gdtr.limit)); puts("\n");
 puts("IDTR base : "); puts(hex2string(idtr.base)); puts("\n");
 puts("IDTR limit: "); puts(hex2string(idtr.limit)); puts("\n");
 unsigned char u8 = 0;
 unsigned short u16 = 0;
 unsigned int u32 = 0;
 unsigned long long u64 = 0;
 asm("mov %%es, %0" : "=m" (u16)); vmx_vmwrite(0x00000C00, u16);
 asm("mov %%cs, %0" : "=m" (u16)); vmx_vmwrite(0x00000C02, u16);
 asm("mov %%ss, %0" : "=m" (u16)); vmx_vmwrite(0x00000C04, u16);
 asm("mov %%ds, %0" : "=m" (u16)); vmx_vmwrite(0x00000C06, u16);
 asm("mov %%fs, %0" : "=m" (u16)); vmx_vmwrite(0x00000C08, u16);
 asm("mov %%gs, %0" : "=m" (u16)); vmx_vmwrite(0x00000C0A, u16);
                                  vmx_vmwrite(0x00000C0C, 0x18);
 vmx_vmwrite(0x00002800, 0xFFFFFFFF);
 vmx_vmwrite(0x00002801, 0xFFFFFFFF);
 vmx_read_msr(0x000001D9, &u64);
 vmx_vmwrite(0x00002802, u64);
 vmx_vmwrite(0x00002803, u64>>32);
 puts("Guest_IA32_DEBUGCTL_full: "); puts(hex2string(u64)); puts("\n");
 puts("Guest_IA32_DEBUGCTL_high: "); puts(hex2string(u64>>32)); puts("\n");
 vmx_vmwrite(0x00004000, 0x1f);
 vmx_vmwrite(0x00004002, 0x0401E9F2);
 vmx_vmwrite(0x0000400C, 0x36dff);
 vmx_vmwrite(0x00004012, 0x11ff);
 vmx_vmwrite(0x0000400E, 0);
 vmx_vmwrite(0x00004010, 0);
 vmx_vmwrite(0x00004014, 0);
 vmx_vmwrite(0x00004016, 0);
 vmx_vmwrite(0x00002010, 0);
 vmx_vmwrite(0x00002011, 0);
 vmx_vmwrite(0x00004006, 0);
 vmx_vmwrite(0x00004008, 0);
 vmx_vmwrite(0x0000400A, 0);
 vmx_vmwrite(0x00006008, 0);
 vmx_vmwrite(0x0000600A, 0);
 vmx_vmwrite(0x0000600C, 0);
 vmx_vmwrite(0x0000600E, 0);
 asm("mov %%es, %0" : "=m" (u16)); vmx_vmwrite(0x00000800, u16);
 asm("mov %%cs, %0" : "=m" (u16)); vmx_vmwrite(0x00000802, u16);
 asm("mov %%ss, %0" : "=m" (u16)); vmx_vmwrite(0x00000804, u16);
 asm("mov %%ds, %0" : "=m" (u16)); vmx_vmwrite(0x00000806, u16);
 asm("mov %%fs, %0" : "=m" (u16)); vmx_vmwrite(0x00000808, u16);
 asm("mov %%gs, %0" : "=m" (u16)); vmx_vmwrite(0x0000080A, u16);
 asm("sldt %0" : "=m" (u16)); vmx_vmwrite(0x0000080C, u16);
                                  vmx_vmwrite(0x0000080E, 0x18);
 vmx_vmwrite(0x00004802, 0XFFFFFFFF);
 vmx_vmwrite(0x00004800, 0XFFFFFFFF);
 vmx_vmwrite(0x00004804, 0XFFFFFFFF);
 vmx_vmwrite(0x00004806, 0XFFFFFFFF);
 vmx_vmwrite(0x00004808, 0XFFFFFFFF);
 vmx_vmwrite(0x0000480A, 0XFFFFFFFF);
 vmx_vmwrite(0x0000480C, 0xFFFF);
 vmx_vmwrite(0x0000480E, 0xFF);
 vmx_vmwrite(0x00004810, gdtr.limit);
 vmx_vmwrite(0x00004812, idtr.limit);
 vmx_vmwrite(0x0000681A, 0x400);
 vmx_vmwrite(0x00004824, 0);
 vmx_vmwrite(0x00004826, 0);
 vmx_vmwrite(0x00004816, 0xC09B);
 vmx_vmwrite(0x00004814, 0xC093);
 vmx_vmwrite(0x00004818, 0xC093);
 vmx_vmwrite(0x0000481A, 0xC093);
 vmx_vmwrite(0x0000481C, 0xC093);
 vmx_vmwrite(0x0000481E, 0xC093);
 vmx_vmwrite(0x00004820, 0x00010000);
 vmx_vmwrite(0x00004822, 0x8b);
 vmx_read_msr(0x00000174, &u64);
 vmx_vmwrite(0x0000482A, u64);
 vmx_vmwrite(0x00006800, vmx_read_cr0());
 vmx_vmwrite(0x00006802, vmx_read_cr3());
 vmx_vmwrite(0x00006804, vmx_read_cr4());
 vmx_vmwrite(0x00006806, 0);
 vmx_vmwrite(0x00006808, 0);
 vmx_vmwrite(0x0000680A, 0);
 vmx_vmwrite(0x0000680C, 0);
 vmx_vmwrite(0x0000680E, 0);
 vmx_vmwrite(0x00006810, 0);
 vmx_vmwrite(0x00006812, 0);
 vmx_vmwrite(0x00006814, 0);
 vmx_vmwrite(0x00006816, gdtr.base);
 vmx_vmwrite(0x00006818, idtr.base);
 *(unsigned int *) &eflags = vmx_read_eflags();
 vmx_vmwrite(0x00006820, *(unsigned int *) &eflags);
 vmx_read_msr(0x00000175, &u64);
 vmx_vmwrite(0x00006824, u64);
 vmx_read_msr(0x00000176, &u64);
 vmx_vmwrite(0x00006826, u64);
 vmx_vmwrite(0x00006C00, vmx_read_cr0());
 vmx_vmwrite(0x00006C02, vmx_read_cr3());
 vmx_vmwrite(0x00006C04, vmx_read_cr4());
 vmx_vmwrite(0x00006C06, 0);
 vmx_vmwrite(0x00006C08, 0);
 vmx_vmwrite(0x00006C0A, 0);
 vmx_vmwrite(0x00006C0C, gdtr.base);
 vmx_vmwrite(0x00006C0E, idtr.base);
 vmx_vmwrite(0x0000681C, 0x90000);
 vmx_vmwrite(0x0000681E, guest_entry);
 vmx_vmwrite(0x00006C14, 0x90000);
 vmx_vmwrite(0x00006C16, host_entry);
 if (!vmx_vmlaunch())
 {
  puts("IMPOSSIBILE LANCIARE LA VM\nError #");
  puts(vmx_instruction_errors[vmx_vmread(0x00004400)]);
  puts("\n");
 }
 puts("Leaving VMX mode "); puts(vmx_vmxoff() ? "success\n" : "failed\n");
 puts("Type something: ");
 while(1);
}

variable_types: struct CPUID_t { unsigned int eax; unsigned int ebx; struct CPUID_ECX_t { unsigned int b00_SSE3; unsigned int b01_PCLMUL; unsigned int b02_DTES64; unsigned int b03_MON; unsigned int b04_DSCPL; unsigned int b05_VMX; unsigned int b06_SMX; unsigned int b07_EST; unsigned int b08_TM2; unsigned int b09_SSSE3; unsigned int b10_CID; unsigned int b11_res; unsigned int b12_FMA; unsigned int b13_CX16; unsigned int b14_ETPRD; unsigned int b15_PDCM; unsigned int b16_res; unsigned int b17_res; unsigned int b18_DCA; unsigned int b19_SSE4_1; unsigned int b20_SSE4_2; unsigned int b21_x2APIC; unsigned int b22_MOVBE; unsigned int b23_POPCNT; unsigned int b24_res; unsigned int b25_AES; unsigned int b26_XSAVE; unsigned int b27_OSXSAVE; unsigned int b28_AVX; unsigned int b29_res; unsigned int b30_res; unsigned int b31_res; } ecx; struct CPUID_EDX_t { unsigned int b00_FPU; unsigned int b01_VME; unsigned int b02_DE; unsigned int b03_PSE; unsigned int b04_TSC; unsigned int b05_MSR; unsigned int b06_PAE; unsigned int b07_MCE; unsigned int b08_CX8; unsigned int b09_APIC; unsigned int b10_res; unsigned int b11_SEP; unsigned int b12_MTRR; unsigned int b13_PGE; unsigned int b14_MCA; unsigned int b15_CMOV; unsigned int b16_PAT; unsigned int b17_PSE36; unsigned int b18_PSN; unsigned int b19_CLFL; unsigned int b20_res; unsigned int b21_DTES; unsigned int b22_ACPI; unsigned int b23_MMX; unsigned int b24_FXSR; unsigned int b25_SSE; unsigned int b26_SSE2; unsigned int b27_SS; unsigned int b28_HTT; unsigned int b29_TM1; unsigned int b30_IA_64; unsigned int b31_PBE; } edx; }

variable_types: struct CR4_t { unsigned int b00_VME; unsigned int b01_PVI; unsigned int b02_TSD; unsigned int b03_DE; unsigned int b04_PSE; unsigned int b05_PAE; unsigned int b06_MCE; unsigned int b07_PGE; unsigned int b08_PCE; unsigned int b09_OSFXSR; unsigned int b10_OSXMMEXCPT; unsigned int b11_res; unsigned int b12_res; unsigned int b13_VMXE; unsigned int b14_SMXE; unsigned int b1531_res; }

variable_types: struct CR0_t { unsigned int b00_PE; unsigned int b01_MP; unsigned int b02_EM; unsigned int b03_TS; unsigned int b04_ET; unsigned int b05_NE; unsigned int b0615_res; unsigned int b16_WP; unsigned int b17_res; unsigned int b18_AM; unsigned int b1928_res; unsigned int b29_NW; unsigned int b30_CD; unsigned int b31_PG; }

variable_types: struct EFLAGS_t { unsigned int b00_CF; unsigned int b01_one; unsigned int b02_PF; unsigned int b03_zero; unsigned int b04_AF; unsigned int b05_zero; unsigned int b06_ZF; unsigned int b07_SF; unsigned int b08_TF; unsigned int b09_IF; unsigned int b10_DF; unsigned int b11_OF; unsigned int b12_OPPL; unsigned int b14_NT; unsigned int b15_zero; unsigned int b16_RF; unsigned int b17_VM; unsigned int b18_AC; unsigned int b19_VIF; unsigned int b20_VIP; unsigned int b21_ID; unsigned int b22_zero; }

variable_types:
nl
unsigned long long

variable_types: i int

variable_types:
reg
unsigned int *

user_defined_types: struct CPUID_t { unsigned int eax; unsigned int ebx; struct CPUID_ECX_t ecx; struct CPUID_EDX_t edx; }


original_code:
static int16_t DS18X20_raw_to_decicelsius(uint8_t familycode, uint8_t sp[]) {
  uint16_t measure;
  uint8_t negative;
  int16_t decicelsius;
  uint16_t fract;
  measure = sp[0] | (sp[1] << 8);
  if(familycode == 0x10) {
    measure &= (uint16_t)0xfffe;
    measure <<= 3;
    measure += (16 - sp[6]) - 4;
  }
  if(measure & 0x8000) {
    negative = 1;
    measure ^= 0xffff;
    measure++;
  }
  else negative = 0;
  if(familycode == 0x28 || familycode == 0x22) {
    switch(sp[4] & ((1<<6)|(1<<5))) {
      case 0:
        measure &= ~(((1<<0)|(1<<1)|(1<<2)));
        break;
      case (1<<5):
        measure &= ~(((1<<0)|(1<<1)));
        break;
      case (1<<6):
        measure &= ~(((1<<0)));
        break;
      default: break;
    }
  }
  decicelsius = (measure >> 4);
  decicelsius *= 10;
  fract = (measure & 0x000F) * 640;
  if(!negative) fract += 512;
  fract /= 1024;
  decicelsius += fract;
  if (negative) decicelsius = -decicelsius;
  if ( decicelsius < -550 || decicelsius > 1250) return 2000;
  else return decicelsius;
}

canonical_original_code:
static short DS18X20_raw_to_decicelsius(unsigned char familycode, unsigned char sp[]) {
  unsigned short measure;
  unsigned char negative;
  short decicelsius;
  unsigned short fract;
  measure = sp[0] | (sp[1] << 8);
  if(familycode == 0x10) {
    measure &= (unsigned short)0xfffe;
    measure <<= 3;
    measure += (16 - sp[6]) - 4;
  }
  if(measure & 0x8000) {
    negative = 1;
    measure ^= 0xffff;
    measure++;
  }
  else negative = 0;
  if(familycode == 0x28 || familycode == 0x22) {
    switch(sp[4] & ((1<<6)|(1<<5))) {
      case 0:
        measure &= ~(((1<<0)|(1<<1)|(1<<2)));
        break;
      case (1<<5):
        measure &= ~(((1<<0)|(1<<1)));
        break;
      case (1<<6):
        measure &= ~(((1<<0)));
        break;
      default: break;
    }
  }
  decicelsius = (measure >> 4);
  decicelsius *= 10;
  fract = (measure & 0x000F) * 640;
  if(!negative) fract += 512;
  fract /= 1024;
  decicelsius += fract;
  if (negative) decicelsius = -decicelsius;
  if ( decicelsius < -550 || decicelsius > 1250) return 2000;
  else return decicelsius;
}

variable_types:
familycode
unsigned char

variable_types:
sp
unsigned char []

variable_types:
measure
unsigned short

variable_types:
negative
unsigned char

variable_types: decicelsius short

variable_types:
fract
unsigned short

return_type: short


decompiled_code:
__int64 __fastcall DS18X20_raw_to_maxres(char a1, unsigned __int16 *a2)
{
unsigned int v2; // eax
char v3; // cl
__int64 result; // rax
char v5; // dl

v2 = *a2;
if ( a1 == 16 )
{
v2 = ((8 * v2) & 0xFFFFFFF0) - *((unsigned __int8 *)a2 + 6);
LOWORD(v2) = v2 + 12;
if ( (v2 & 0x8000u) == 0 )
return 10000 * ((unsigned __int16)v2 >> 4) + 625 * (v2 & 0xF);
}
else
{
v3 = 0;
if ( (v2 & 0x8000u) == 0 )
goto LABEL_3;
}
v2 = -v2;
v3 = 1;
LABEL_3:
if ( a1 == 40 || a1 == 34 )
{
v5 = a2[2] & 0x60;
switch ( v5 )
{
case 32:
LOWORD(v2) = v2 & 0xFFFC;
break;
case 64:
LOWORD(v2) = v2 & 0xFFFE;
break;
case 0:
LOWORD(v2) = v2 & 0xFFF8;
break;
}
}
result = 10000 * ((unsigned __int16)v2 >> 4) + 625 * (v2 & 0xF);
if ( v3 )
result = (unsigned int)-(int)result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(char a1, unsigned __int16 *a2)
{
unsigned int v2; // eax
char v3; // cl
__int64 result; // rax
char v5; // dl

v2 = *a2;
if ( a1 == 16 )
{
v2 = ((8 * v2) & 0xFFFFFFF0) - *((unsigned __int8 *)a2 + 6);
LOWORD(v2) = v2 + 12;
if ( (v2 & 0x8000u) == 0 )
return 10000 * ((unsigned __int16)v2 >> 4) + 625 * (v2 & 0xF);
}
else
{
v3 = 0;
if ( (v2 & 0x8000u) == 0 )
goto LABEL_3;
}
v2 = -v2;
v3 = 1;
LABEL_3:
if ( a1 == 40 || a1 == 34 )
{
v5 = a2[2] & 0x60;
switch ( v5 )
{
case 32:
LOWORD(v2) = v2 & 0xFFFC;
break;
case 64:
LOWORD(v2) = v2 & 0xFFFE;
break;
case 0:
LOWORD(v2) = v2 & 0xFFF8;
break;
}
}
result = 10000 * ((unsigned __int16)v2 >> 4) + 625 * (v2 & 0xF);
if ( v3 )
result = (unsigned int)-(int)result;
return result;
}

original_code:
static int32_t DS18X20_raw_to_maxres( uint8_t familycode, uint8_t sp[]) {
  uint16_t measure;
  uint8_t negative;
  int32_t temperaturevalue;
  measure = sp[0] | (sp[1] << 8);
  if(familycode == 0x10) {
    measure &= (uint16_t)0xfffe;
    measure <<= 3;
    measure += ( 16 - sp[6] ) - 4;
  }
  if(measure & 0x8000) {
    negative = 1;
    measure ^= 0xffff;
    measure++;
  }
  else negative = 0;
  if(familycode == 0x28 || familycode == 0x22) {
    switch(sp[4] & ((1<<6)|(1<<5))) {
      case 0:
        measure &= ~(((1<<0)|(1<<1)|(1<<2)));
        break;
      case (1<<5):
        measure &= ~(((1<<0)|(1<<1)));
        break;
      case (1<<6):
        measure &= ~(((1<<0)));
        break;
      default: break;
    }
  }
  temperaturevalue = (measure >> 4);
  temperaturevalue *= 10000;
  temperaturevalue +=( measure & 0x000F ) * 625;
  if(negative) temperaturevalue = -temperaturevalue;
  return temperaturevalue;
}

canonical_original_code:
static int DS18X20_raw_to_maxres(unsigned char familycode, unsigned char sp[]) {
  unsigned short measure;
  unsigned char negative;
  int temperaturevalue;
  measure = sp[0] | (sp[1] << 8);
  if(familycode == 0x10) {
    measure &= (unsigned short)0xfffe;
    measure <<= 3;
    measure += ( 16 - sp[6] ) - 4;
  }
  if(measure & 0x8000) {
    negative = 1;
    measure ^= 0xffff;
    measure++;
  }
  else negative = 0;
  if(familycode == 0x28 || familycode == 0x22) {
    switch(sp[4] & ((1<<6)|(1<<5))) {
      case 0:
        measure &= ~(((1<<0)|(1<<1)|(1<<2)));
        break;
      case (1<<5):
        measure &= ~(((1<<0)|(1<<1)));
        break;
      case (1<<6):
        measure &= ~(((1<<0)));
        break;
      default: break;
    }
  }
  temperaturevalue = (measure >> 4);
  temperaturevalue *= 10000;
  temperaturevalue +=( measure & 0x000F ) * 625;
  if(negative) temperaturevalue = -temperaturevalue;
  return temperaturevalue;
}

variable_types: temperaturevalue int

return_type: int


decompiled_code:
__int64 __fastcall read_scratchpad(__int64 a1, _BYTE *a2, char a3)
{
_BYTE *v4; // rbx
__int64 v5; // rbp
__int64 result; // rax

ow_command(190LL, a1);
if ( a3 )
{
v4 = a2;
v5 = (__int64)&a2[(unsigned __int8)(a3 - 1) + 1];
do
*v4++ = ow_byte_rd();
while ( v4 != (_BYTE *)v5 );
}
result = crc8(a2, 9LL);
if ( (_BYTE)result )
result = 3LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, _BYTE *a2, char a3)
{
_BYTE *v4; // rbx
__int64 v5; // rbp
__int64 result; // rax

ow_command(190LL, a1);
if ( a3 )
{
v4 = a2;
v5 = (__int64)&a2[(unsigned __int8)(a3 - 1) + 1];
do
*v4++ = ow_byte_rd();
while ( v4 != (_BYTE *)v5 );
}
result = crc8(a2, 9LL);
if ( (_BYTE)result )
result = 3LL;
return result;
}

original_code:
static uint8_t read_scratchpad(uint8_t id[], uint8_t sp[], uint8_t n) {
  uint8_t i;
  uint8_t ret;
  ow_command( 0xBE, id );
  for(i = 0; i < n; i++) sp[i] = ow_byte_rd();
  if (crc8(&sp[0], 9)) ret = 0x03;
  else ret = 0x00;
  return ret;
}

canonical_original_code:
static unsigned char read_scratchpad(unsigned char id[], unsigned char sp[], unsigned char n) {
  unsigned char i;
  unsigned char ret;
  ow_command( 0xBE, id );
  for(i = 0; i < n; i++) sp[i] = ow_byte_rd();
  if (crc8(&sp[0], 9)) ret = 0x03;
  else ret = 0x00;
  return ret;
}

variable_types:
id
unsigned char []

variable_types:
n
unsigned char

variable_types:
i
unsigned char

variable_types:
ret
unsigned char

return_type: unsigned char


decompiled_code:
__int64 __fastcall DS18X20_find_sensor(unsigned __int8 *a1, unsigned __int8 *a2)
{
__int64 v2; // rbp
unsigned __int8 v3; // al
unsigned __int64 v4; // rdx

v2 = 0x10400010000LL;
v3 = *a1;
while ( 1 )
{
v3 = ow_rom_search(v3, a2);
*a1 = v3;
if ( (unsigned __int8)(v3 - 1) > 0xFCu )
break;
v4 = *a2;
if ( (unsigned __int8)v4 <= 0x28u && _bittest64(&v2, v4) )
return 0LL;
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned __int8 *a1, unsigned __int8 *a2)
{
__int64 v2; // rbp
unsigned __int8 v3; // al
unsigned __int64 v4; // rdx

v2 = 0x10400010000LL;
v3 = *a1;
while ( 1 )
{
v3 = ow_rom_search(v3, a2);
*a1 = v3;
if ( (unsigned __int8)(v3 - 1) > 0xFCu )
break;
v4 = *a2;
if ( (unsigned __int8)v4 <= 0x28u && _bittest64(&v2, v4) )
return 0LL;
}
return 1LL;
}

original_code:
uint8_t DS18X20_find_sensor(uint8_t *diff, uint8_t id[]) {
  uint8_t go;
  uint8_t ret;
  ret = 0x00;
  go = 1;
  do {
    *diff = ow_rom_search(*diff, &id[0]);
    if(*diff == 0xFF || *diff == 0xFE || *diff == 0x00) {
      go = 0;
      ret = 0x01;
    }
    else if(id[0] == 0x28 || id[0] == 0x10 || id[0] == 0x22) go = 0;
  } while(go);
  return ret;
}

canonical_original_code:
unsigned char DS18X20_find_sensor(unsigned char *diff, unsigned char id[]) {
  unsigned char go;
  unsigned char ret;
  ret = 0x00;
  go = 1;
  do {
    *diff = ow_rom_search(*diff, &id[0]);
    if(*diff == 0xFF || *diff == 0xFE || *diff == 0x00) {
      go = 0;
      ret = 0x01;
    }
    else if(id[0] == 0x28 || id[0] == 0x10 || id[0] == 0x22) go = 0;
  } while(go);
  return ret;
}

variable_types:
diff
unsigned char *

variable_types:
go
unsigned char


decompiled_code:
bool __fastcall DS18X20_get_power_status(__int64 a1)
{
char v1; // bl

ow_reset();
ow_command(180LL, a1);
v1 = ow_bit(1LL);
ow_reset();
return v1 != 0;
}

canonical_decompiled_code:
bool __fastcall func4(__int64 a1)
{
char v1; // bl

ow_reset();
ow_command(180LL, a1);
v1 = ow_bit(1LL);
ow_reset();
return v1 != 0;
}

original_code:
uint8_t DS18X20_get_power_status(uint8_t id[]) {
  uint8_t pstat;
  ow_reset();
  ow_command(0xB4, id);
  pstat = ow_bit(1);
  ow_reset();
  return (pstat) ? 0x01 : 0x00;
}

canonical_original_code:
unsigned char DS18X20_get_power_status(unsigned char id[]) {
  unsigned char pstat;
  ow_reset();
  ow_command(0xB4, id);
  pstat = ow_bit(1);
  ow_reset();
  return (pstat) ? 0x01 : 0x00;
}

variable_types:
pstat
unsigned char


decompiled_code:
__int64 __fastcall DS18X20_start_meas(__int64 a1, __int64 a2)
{
ow_reset();
ow_command(68LL, a2);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2)
{
ow_reset();
ow_command(68LL, a2);
return 0LL;
}

original_code:
uint8_t DS18X20_start_meas(uint8_t with_power_extern, uint8_t id[]) {
  ow_reset();
  ow_command(0x44, id);
  return 0x00;
}

canonical_original_code:
unsigned char DS18X20_start_meas(unsigned char with_power_extern, unsigned char id[]) {
  ow_reset();
  ow_command(0x44, id);
  return 0x00;
}


decompiled_code:
bool DS18X20_conversion_in_progress()
{
return (unsigned __int8)ow_bit(1LL) == 0;
}

canonical_decompiled_code:
bool func6()
{
return (unsigned __int8)ow_bit(1LL) == 0;
}

original_code:
uint8_t DS18X20_conversion_in_progress(void) {
  return ow_bit(1) ? 0x00 : 0x01;
}

canonical_original_code:
unsigned char DS18X20_conversion_in_progress() {
  return ow_bit(1) ? 0x00 : 0x01;
}


decompiled_code:
__int64 __fastcall DS18X20_read_decicelsius(char *a1, _WORD *a2)
{
unsigned int v2; // er8
unsigned __int16 v4[16]; // [rsp+7h] [rbp-21h] BYREF

ow_reset();
v2 = read_scratchpad((__int64)a1, v4, 9);
if ( !(_BYTE)v2 )
*a2 = DS18X20_raw_to_decicelsius(*a1, v4);
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func7(char *a1, _WORD *a2)
{
unsigned int v2; // er8
unsigned __int16 v4[16]; // [rsp+7h] [rbp-21h] BYREF

ow_reset();
v2 = func2((__int64)a1, v4, 9);
if ( !(_BYTE)v2 )
*a2 = func0(*a1, v4);
return v2;
}

original_code:
uint8_t DS18X20_read_decicelsius(uint8_t id[], int16_t *decicelsius) {
  uint8_t sp[9];
  uint8_t ret;
  ow_reset();
  ret = read_scratchpad(id, sp, 9);
  if(ret == 0x00) *decicelsius = DS18X20_raw_to_decicelsius(id[0], sp);
  return ret;
}

canonical_original_code:
unsigned char DS18X20_read_decicelsius(unsigned char id[], short *decicelsius) {
  unsigned char sp[9];
  unsigned char ret;
  ow_reset();
  ret = read_scratchpad(id, sp, 9);
  if(ret == 0x00) *decicelsius = DS18X20_raw_to_decicelsius(id[0], sp);
  return ret;
}

variable_types:
decicelsius
short *

variable_types:
sp
unsigned char [9]


decompiled_code:
__int64 __fastcall DS18X20_read_decicelsius_single(char a1, _WORD *a2)
{
unsigned int v2; // er8
unsigned __int16 v4[16]; // [rsp+7h] [rbp-21h] BYREF

v2 = read_scratchpad(0LL, v4, 9);
if ( !(_BYTE)v2 )
*a2 = DS18X20_raw_to_decicelsius(a1, v4);
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func8(char a1, _WORD *a2)
{
unsigned int v2; // er8
unsigned __int16 v4[16]; // [rsp+7h] [rbp-21h] BYREF

v2 = func2(0LL, v4, 9);
if ( !(_BYTE)v2 )
*a2 = func0(a1, v4);
return v2;
}

original_code:
uint8_t DS18X20_read_decicelsius_single(uint8_t familycode, int16_t *decicelsius) {
  uint8_t sp[9];
  uint8_t ret;
  ret = read_scratchpad(((void *)0), sp, 9);
  if(ret == 0x00) *decicelsius = DS18X20_raw_to_decicelsius(familycode, sp);
  return ret;
}

canonical_original_code:
unsigned char DS18X20_read_decicelsius_single(unsigned char familycode, short *decicelsius) {
  unsigned char sp[9];
  unsigned char ret;
  ret = read_scratchpad(((void *)0), sp, 9);
  if(ret == 0x00) *decicelsius = DS18X20_raw_to_decicelsius(familycode, sp);
  return ret;
}


decompiled_code:
__int64 __fastcall DS18X20_read_maxres(char *a1, _DWORD *a2)
{
unsigned int v2; // er8
unsigned __int16 v4[16]; // [rsp+7h] [rbp-21h] BYREF

ow_reset();
v2 = read_scratchpad((__int64)a1, v4, 9);
if ( !(_BYTE)v2 )
*a2 = DS18X20_raw_to_maxres(*a1, v4);
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func9(char *a1, _DWORD *a2)
{
unsigned int v2; // er8
unsigned __int16 v4[16]; // [rsp+7h] [rbp-21h] BYREF

ow_reset();
v2 = func2((__int64)a1, v4, 9);
if ( !(_BYTE)v2 )
*a2 = func1(*a1, v4);
return v2;
}

original_code:
uint8_t DS18X20_read_maxres(uint8_t id[], int32_t *temperaturevalue) {
  uint8_t sp[9];
  uint8_t ret;
  ow_reset();
  ret = read_scratchpad(id, sp, 9);
  if(ret == 0x00) *temperaturevalue = DS18X20_raw_to_maxres(id[0], sp);
  return ret;
}

canonical_original_code:
unsigned char DS18X20_read_maxres(unsigned char id[], int *temperaturevalue) {
  unsigned char sp[9];
  unsigned char ret;
  ow_reset();
  ret = read_scratchpad(id, sp, 9);
  if(ret == 0x00) *temperaturevalue = DS18X20_raw_to_maxres(id[0], sp);
  return ret;
}

variable_types:
temperaturevalue
int *


decompiled_code:
__int64 __fastcall DS18X20_read_maxres_single(char a1, _DWORD *a2)
{
unsigned int v2; // er8
unsigned __int16 v4[16]; // [rsp+7h] [rbp-21h] BYREF

v2 = read_scratchpad(0LL, v4, 9);
if ( !(_BYTE)v2 )
*a2 = DS18X20_raw_to_maxres(a1, v4);
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func10(char a1, _DWORD *a2)
{
unsigned int v2; // er8
unsigned __int16 v4[16]; // [rsp+7h] [rbp-21h] BYREF

v2 = func2(0LL, v4, 9);
if ( !(_BYTE)v2 )
*a2 = func1(a1, v4);
return v2;
}

original_code:
uint8_t DS18X20_read_maxres_single(uint8_t familycode, int32_t *temperaturevalue) {
  uint8_t sp[9];
  uint8_t ret;
  ret = read_scratchpad( ((void *)0), sp, 9 );
  if(ret == 0x00) *temperaturevalue = DS18X20_raw_to_maxres(familycode, sp);
  return ret;
}

canonical_original_code:
unsigned char DS18X20_read_maxres_single(unsigned char familycode, int *temperaturevalue) {
  unsigned char sp[9];
  unsigned char ret;
  ret = read_scratchpad( ((void *)0), sp, 9 );
  if(ret == 0x00) *temperaturevalue = DS18X20_raw_to_maxres(familycode, sp);
  return ret;
}

variable_types:
familycode
unsigned char


original_code:
uint8_t DS18X20_format_from_maxres(int32_t temperaturevalue, char str[], uint8_t n) {
  uint8_t sign = 0;
  char temp[10];
  int8_t temp_loc = 0;
  uint8_t str_loc = 0;
  ldiv_t ldt;
  uint8_t ret;
  if(n >= (9 + 1) && temperaturevalue > -1000000L && temperaturevalue < 10000000L) {
    if(temperaturevalue < 0) {
      sign = 1;
      temperaturevalue = -temperaturevalue;
    }
    do {
      ldt = ldiv(temperaturevalue, 10);
      temp[temp_loc++] = ldt.rem + '0';
      temperaturevalue = ldt.quot;
    } while(temperaturevalue > 0);
    if(sign) temp[temp_loc] = '-';
    else temp[temp_loc] = '+';
    while(temp_loc >= 0) {
      str[str_loc++] = temp[(uint8_t)temp_loc--];
      if(temp_loc == 3) str[str_loc++] = '.';
    }
    str[str_loc] = '\0';
    ret = 0x00;
  }
  else ret = 0x01;
  return ret;
}

canonical_original_code:
unsigned char DS18X20_format_from_maxres(int temperaturevalue, char str[], unsigned char n) {
  unsigned char sign = 0;
  char temp[10];
  char temp_loc = 0;
  unsigned char str_loc = 0;
  struct ldiv_t ldt;
  unsigned char ret;
  if(n >= (9 + 1) && temperaturevalue > -1000000L && temperaturevalue < 10000000L) {
    if(temperaturevalue < 0) {
      sign = 1;
      temperaturevalue = -temperaturevalue;
    }
    do {
      ldt = ldiv(temperaturevalue, 10);
      temp[temp_loc++] = ldt.rem + '0';
      temperaturevalue = ldt.quot;
    } while(temperaturevalue > 0);
    if(sign) temp[temp_loc] = '-';
    else temp[temp_loc] = '+';
    while(temp_loc >= 0) {
      str[str_loc++] = temp[(unsigned char)temp_loc--];
      if(temp_loc == 3) str[str_loc++] = '.';
    }
    str[str_loc] = '\0';
    ret = 0x00;
  }
  else ret = 0x01;
  return ret;
}

variable_types:
str
char []

variable_types:
sign
unsigned char

variable_types:
temp
char [10]

variable_types:
ldt
struct ldiv_t { long quot; long rem; }


decompiled_code:
__int64 __fastcall DS18X20_write_scratchpad(_BYTE *a1, unsigned __int8 a2, unsigned __int8 a3, unsigned __int8 a4)
{
ow_reset();
ow_command(78LL, a1);
ow_byte_wr(a2);
ow_byte_wr(a3);
if ( *a1 != 40 && *a1 != 34 )
return 0LL;
ow_byte_wr(a4);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func12(_BYTE *a1, unsigned __int8 a2, unsigned __int8 a3, unsigned __int8 a4)
{
ow_reset();
ow_command(78LL, a1);
ow_byte_wr(a2);
ow_byte_wr(a3);
if ( *a1 != 40 && *a1 != 34 )
return 0LL;
ow_byte_wr(a4);
return 0LL;
}

original_code:
uint8_t DS18X20_write_scratchpad(uint8_t id[], uint8_t th, uint8_t tl, uint8_t conf) {
  ow_reset();
  ow_command(0x4E, id);
  ow_byte_wr(th);
  ow_byte_wr(tl);
  if(id[0] == 0x28 || id[0] == 0x22) ow_byte_wr(conf);
  return 0x00;
}

canonical_original_code:
unsigned char DS18X20_write_scratchpad(unsigned char id[], unsigned char th, unsigned char tl, unsigned char conf) {
  ow_reset();
  ow_command(0x4E, id);
  ow_byte_wr(th);
  ow_byte_wr(tl);
  if(id[0] == 0x28 || id[0] == 0x22) ow_byte_wr(conf);
  return 0x00;
}

variable_types:
th
unsigned char

variable_types:
tl
unsigned char

variable_types:
conf
unsigned char


decompiled_code:
__int64 __fastcall DS18X20_read_scratchpad(__int64 a1, _BYTE *a2, char a3)
{
ow_reset();
return read_scratchpad(a1, a2, a3);
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1, _BYTE *a2, char a3)
{
ow_reset();
return func2(a1, a2, a3);
}

original_code:
uint8_t DS18X20_read_scratchpad(uint8_t id[], uint8_t sp[], uint8_t n) {
  ow_reset();
  return read_scratchpad(id, sp, n);
}

canonical_original_code:
unsigned char DS18X20_read_scratchpad(unsigned char id[], unsigned char sp[], unsigned char n) {
  ow_reset();
  return read_scratchpad(id, sp, n);
}

variable_types:
id
unsigned char []

variable_types:
sp
unsigned char []

variable_types:
n
unsigned char


decompiled_code:
__int64 __fastcall DS18X20_scratchpad_to_eeprom(__int64 a1, __int64 a2)
{
ow_reset();
ow_command(72LL, a2);
delay_ms(10LL);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func14(__int64 a1, __int64 a2)
{
ow_reset();
ow_command(72LL, a2);
delay_ms(10LL);
return 0LL;
}

original_code:
uint8_t DS18X20_scratchpad_to_eeprom(uint8_t with_power_extern, uint8_t id[]) {
  ow_reset();
  ow_command(0x48, id);
  delay_ms(10);
  return 0x00;
}

canonical_original_code:
unsigned char DS18X20_scratchpad_to_eeprom(unsigned char with_power_extern, unsigned char id[]) {
  ow_reset();
  ow_command(0x48, id);
  delay_ms(10);
  return 0x00;
}


decompiled_code:
char __fastcall DS18X20_eeprom_to_scratchpad(__int64 a1)
{
char v1; // bl
char result; // al

v1 = -2;
ow_reset();
ow_command(184LL, a1);
while ( 1 )
{
result = ow_bit(1LL);
if ( result )
break;
if ( --v1 == -1 )
return result;
}
return v1 == 0;
}

canonical_decompiled_code:
char __fastcall func15(__int64 a1)
{
char v1; // bl
char result; // al

v1 = -2;
ow_reset();
ow_command(184LL, a1);
while ( 1 )
{
result = ow_bit(1LL);
if ( result )
break;
if ( --v1 == -1 )
return result;
}
return v1 == 0;
}

original_code:
uint8_t DS18X20_eeprom_to_scratchpad(uint8_t id[]) {
  uint8_t ret;
  uint8_t retry_count = 255;
  ow_reset();
  ow_command(0xB8, id);
  while(retry_count-- && !(ow_bit(1)));
  if(retry_count) ret = 0x00;
  else ret = 0x01;
  return ret;
}

canonical_original_code:
unsigned char DS18X20_eeprom_to_scratchpad(unsigned char id[]) {
  unsigned char ret;
  unsigned char retry_count = 255;
  ow_reset();
  ow_command(0xB8, id);
  while(retry_count-- && !(ow_bit(1)));
  if(retry_count) ret = 0x00;
  else ret = 0x01;
  return ret;
}

variable_types:
ret
unsigned char


decompiled_code:
const char *__fastcall get_type_by_id(char a1)
{
unsigned __int8 v1; // di
const char *result; // rax

v1 = a1 - 16;
result = "WTF?!";
if ( v1 <= 0x18u )
result = CSWTCH_13[v1];
return result;
}

canonical_decompiled_code:
const char *__fastcall func0(char a1)
{
unsigned __int8 v1; // di
const char *result; // rax

v1 = a1 - 16;
result = "WTF?!";
if ( v1 <= 0x18u )
result = CSWTCH_13[v1];
return result;
}

original_code:
char *get_type_by_id(uint8_t id) {
  switch (id) {
    case 0x10: return "DS18S20";
    case 0x28: return "DS18B20";
    case 0x22 : return "DS1822";
    default: return "WTF?!";
  }
}

canonical_original_code:
char *get_type_by_id(unsigned char id) {
  switch (id) {
    case 0x10: return "DS18S20";
    case 0x28: return "DS18B20";
    case 0x22 : return "DS1822";
    default: return "WTF?!";
  }
}

variable_types:
id
unsigned char

return_type: char *


canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int result; // eax
int v4; // ebx
const char *v5; // rax
__int64 v6; // rcx
__int64 v7; // r8
__int64 v8; // rcx
char v9; // [rsp+Dh] [rbp-1Bh] BYREF
__int16 v10[13]; // [rsp+Eh] [rbp-1Ah] BYREF

if ( argc == 2 )
{
v4 = ow_init(argv[1], argv, envp);
if ( (_BYTE)v4 )
{
puts("Bus INIT failed. Check COM port.\n");
result = 1;
}
else
{
v9 = -1;
while ( 1 )
{
DS18X20_find_sensor(&v9, &id);
if ( v9 == -1 )
{
puts("All sensors are offline now.\n");
ow_finit();
return 1;
}
if ( v9 == -2 )
break;
++v4;
v5 = get_type_by_id(id);
fprintf(
stdout,
"Bus %s Device %03u Type 0x%02hx (%s) ID %02hx%02hx%02hx%02hx%02hx%02hx CRC 0x%02hx ",
argv[1],
v6,
v7,
v5,
(unsigned __int8)*(&id + 6),
(unsigned __int8)*(&id + 5),
(unsigned __int8)*(&id + 4),
(unsigned __int8)*(&id + 3),
(unsigned __int8)*(&id + 2),
(unsigned __int8)*(&id + 1),
(unsigned __int8)*(&id + 7));
fflush(stdout);
if ( (unsigned __int8)DS18X20_start_meas(1LL, 0LL) )
goto LABEL_5;
while ( (unsigned __int8)DS18X20_conversion_in_progress() == 1 )
delay_ms(100LL);
if ( (unsigned __int8)DS18X20_read_decicelsius(&id, v10) )
{
LABEL_5:
puts("MEASURE FAILED!\n");
}
else
{
if ( v10[0] <= 0 )
v8 = (unsigned int)(-v10[0] % 10);
else
v8 = (unsigned int)(v10[0] % 10);
fprintf(stdout, "TEMP %3d.%01d C\n", (unsigned int)(v10[0] / 10), v8);
}
if ( !v9 )
{
puts("Sensors listed.\n");
ow_finit();
return 0;
}
}
puts("Bus error.\n");
ow_finit();
result = 1;
}
}
else
{
puts("Path to COM port required.\n");
result = 1;
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int result; // eax
int v4; // ebx
const char *v5; // rax
__int64 v6; // rcx
__int64 v7; // r8
__int64 v8; // rcx
char v9; // [rsp+Dh] [rbp-1Bh] BYREF
__int16 v10[13]; // [rsp+Eh] [rbp-1Ah] BYREF

if ( argc == 2 )
{
v4 = ow_init(argv[1], argv, envp);
if ( (_BYTE)v4 )
{
puts("Bus INIT failed. Check COM port.\n");
result = 1;
}
else
{
v9 = -1;
while ( 1 )
{
DS18X20_find_sensor(&v9, &id);
if ( v9 == -1 )
{
puts("All sensors are offline now.\n");
ow_finit();
return 1;
}
if ( v9 == -2 )
break;
++v4;
v5 = func0(id);
fprintf(
stdout,
"Bus %s Device %03u Type 0x%02hx (%s) ID %02hx%02hx%02hx%02hx%02hx%02hx CRC 0x%02hx ",
argv[1],
v6,
v7,
v5,
(unsigned __int8)*(&id + 6),
(unsigned __int8)*(&id + 5),
(unsigned __int8)*(&id + 4),
(unsigned __int8)*(&id + 3),
(unsigned __int8)*(&id + 2),
(unsigned __int8)*(&id + 1),
(unsigned __int8)*(&id + 7));
fflush(stdout);
if ( (unsigned __int8)DS18X20_start_meas(1LL, 0LL) )
goto LABEL_5;
while ( (unsigned __int8)DS18X20_conversion_in_progress() == 1 )
delay_ms(100LL);
if ( (unsigned __int8)DS18X20_read_decicelsius(&id, v10) )
{
LABEL_5:
puts("MEASURE FAILED!\n");
}
else
{
if ( v10[0] <= 0 )
v8 = (unsigned int)(-v10[0] % 10);
else
v8 = (unsigned int)(v10[0] % 10);
fprintf(stdout, "TEMP %3d.%01d C\n", (unsigned int)(v10[0] / 10), v8);
}
if ( !v9 )
{
puts("Sensors listed.\n");
ow_finit();
return 0;
}
}
puts("Bus error.\n");
ow_finit();
result = 1;
}
}
else
{
puts("Path to COM port required.\n");
result = 1;
}
return result;
}

original_code:
int main(int argc, char *argv[]) {
  if (argc != 2) {
    puts("Path to COM port required.\n");
    return 1;
  }
  if (ow_init(argv[1])) {
    puts("Bus INIT failed. Check COM port.\n");
    return 1;
  }
  uint8_t c = 0, diff = 0xFF;
  int16_t temp_dc;
  while (diff != 0x00) {
    DS18X20_find_sensor(&diff, id);
    if (diff == 0xFF) {
      puts("All sensors are offline now.\n");
      ow_finit();
      return 1;
    }
    if (diff == 0xFE) {
      puts("Bus error.\n");
      ow_finit();
      return 1;
    }
    fprintf(stdout, "Bus %s Device %03u Type 0x%02hx (%s) ID %02hx%02hx%02hx%02hx%02hx%02hx CRC 0x%02hx ", argv[1], c, id[0], get_type_by_id(id[0]), id[6], id[5], id[4], id[3], id[2], id[1], id[7]);
    fflush(stdout);
    c ++;
    if (DS18X20_start_meas(0x01, ((void *)0)) == 0x00) {
      while (DS18X20_conversion_in_progress() == 0x01) {
        delay_ms(100);
      }
      if (DS18X20_read_decicelsius(id, &temp_dc) == 0x00) {
        fprintf(stdout, "TEMP %3d.%01d C\n", temp_dc / 10, temp_dc > 0 ? temp_dc % 10 : -temp_dc % 10);
        continue;
      }
    }
    puts("MEASURE FAILED!\n");
  }
  puts("Sensors listed.\n");
  ow_finit();
  return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
  if (argc != 2) {
    puts("Path to COM port required.\n");
    return 1;
  }
  if (ow_init(argv[1])) {
    puts("Bus INIT failed. Check COM port.\n");
    return 1;
  }
  unsigned char c = 0; unsigned char diff = 0xFF;
  short temp_dc;
  while (diff != 0x00) {
    DS18X20_find_sensor(&diff, id);
    if (diff == 0xFF) {
      puts("All sensors are offline now.\n");
      ow_finit();
      return 1;
    }
    if (diff == 0xFE) {
      puts("Bus error.\n");
      ow_finit();
      return 1;
    }
    fprintf(stdout, "Bus %s Device %03u Type 0x%02hx (%s) ID %02hx%02hx%02hx%02hx%02hx%02hx CRC 0x%02hx ", argv[1], c, id[0], get_type_by_id(id[0]), id[6], id[5], id[4], id[3], id[2], id[1], id[7]);
    fflush(stdout);
    c ++;
    if (DS18X20_start_meas(0x01, ((void *)0)) == 0x00) {
      while (DS18X20_conversion_in_progress() == 0x01) {
        delay_ms(100);
      }
      if (DS18X20_read_decicelsius(id, &temp_dc) == 0x00) {
        fprintf(stdout, "TEMP %3d.%01d C\n", temp_dc / 10, temp_dc > 0 ? temp_dc % 10 : -temp_dc % 10);
        continue;
      }
    }
    puts("MEASURE FAILED!\n");
  }
  puts("Sensors listed.\n");
  ow_finit();
  return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
c
unsigned char

variable_types:
diff
unsigned char


decompiled_code:
unsigned __int8 __fastcall crc8(unsigned __int8 *a1, __int16 a2)
{
unsigned __int8 *v2; // r8
__int64 v3; // r9
unsigned __int8 result; // al
unsigned __int8 v5; // cl
char v6; // dl
unsigned __int8 v7; // di

if ( !a2 )
return 0;
v2 = a1;
v3 = (__int64)&a1[(unsigned __int16)(a2 - 1) + 1];
result = 0;
do
{
v5 = *v2;
v6 = 8;
do
{
v7 = result;
result >>= 1;
if ( ((v5 ^ v7) & 1) != 0 )
result ^= 0x8Cu;
v5 >>= 1;
--v6;
}
while ( v6 );
++v2;
}
while ( v2 != (unsigned __int8 *)v3 );
return result;
}

canonical_decompiled_code:
unsigned __int8 __fastcall func0(unsigned __int8 *a1, __int16 a2)
{
unsigned __int8 *v2; // r8
__int64 v3; // r9
unsigned __int8 result; // al
unsigned __int8 v5; // cl
char v6; // dl
unsigned __int8 v7; // di

if ( !a2 )
return 0;
v2 = a1;
v3 = (__int64)&a1[(unsigned __int16)(a2 - 1) + 1];
result = 0;
do
{
v5 = *v2;
v6 = 8;
do
{
v7 = result;
result >>= 1;
if ( ((v5 ^ v7) & 1) != 0 )
result ^= 0x8Cu;
v5 >>= 1;
--v6;
}
while ( v6 );
++v2;
}
while ( v2 != (unsigned __int8 *)v3 );
return result;
}

original_code:
uint8_t crc8(uint8_t *data_in, uint16_t number_of_bytes_to_read) {
  uint8_t crc = 0x00;
  uint16_t loop_count;
  uint8_t bit_counter;
  uint8_t data;
  uint8_t feedback_bit;
  for(loop_count = 0; loop_count != number_of_bytes_to_read; loop_count++) {
    data = data_in[loop_count];
    bit_counter = 8;
    do {
      feedback_bit = (crc ^ data) & 0x01;
      if ( feedback_bit == 0x01 ) crc = crc ^ 0x18;
      crc = (crc >> 1) & 0x7F;
      if ( feedback_bit == 0x01 ) crc = crc | 0x80;
      data = data >> 1;
      bit_counter--;
    } while(bit_counter > 0);
  }
  return crc;
}

canonical_original_code:
unsigned char crc8(unsigned char *data_in, unsigned short number_of_bytes_to_read) {
  unsigned char crc = 0x00;
  unsigned short loop_count;
  unsigned char bit_counter;
  unsigned char data;
  unsigned char feedback_bit;
  for(loop_count = 0; loop_count != number_of_bytes_to_read; loop_count++) {
    data = data_in[loop_count];
    bit_counter = 8;
    do {
      feedback_bit = (crc ^ data) & 0x01;
      if ( feedback_bit == 0x01 ) crc = crc ^ 0x18;
      crc = (crc >> 1) & 0x7F;
      if ( feedback_bit == 0x01 ) crc = crc | 0x80;
      data = data >> 1;
      bit_counter--;
    } while(bit_counter > 0);
  }
  return crc;
}

variable_types:
crc
unsigned char

variable_types:
data
unsigned char


decompiled_code:
int __fastcall delay_ms(unsigned int a1)
{
struct timespec v2; // [rsp+0h] [rbp-18h] BYREF

v2.tv_sec = a1 / 0x3E8;
v2.tv_nsec = 1000000 * (a1 % 0x3E8);
return nanosleep(&v2, 0LL);
}

canonical_decompiled_code:
int __fastcall func1(unsigned int a1)
{
struct timespec v2; // [rsp+0h] [rbp-18h] BYREF

v2.tv_sec = a1 / 0x3E8;
v2.tv_nsec = 1000000 * (a1 % 0x3E8);
return nanosleep(&v2, 0LL);
}

original_code:
void delay_ms(uint32_t ms) {
  struct timespec tv = {
    .tv_sec = ms / 1000,
    .tv_nsec = (ms % 1000) * 1000000,
  };
  nanosleep(&tv, ((void *)0));
}

canonical_original_code:
void delay_ms(unsigned int ms) {
  struct timespec tv = {
    .tv_sec = ms / 1000,
    .tv_nsec = (ms % 1000) * 1000000,
  };
  nanosleep(&tv, ((void *)0));
}

variable_types:
ms
unsigned int

variable_types: struct timespec { long tv_sec; long tv_nsec; }

return_type: void


decompiled_code:
int __fastcall uart_init(const char *a1)
{
int result; // eax

result = open(a1, 2306);
fd = result;
if ( result > 0 )
result = 0;
return result;
}

canonical_decompiled_code:
int __fastcall func0(const char *a1)
{
int result; // eax

result = open(a1, 2306);
fd = result;
if ( result > 0 )
result = 0;
return result;
}

original_code:
int uart_init(char *dev_path) {
  fd = open(dev_path, 02 | 0400 | 04000);
  if(fd < 0) return fd;
  return 0;
}


decompiled_code:
int uart_finit()
{
int result; // eax

if ( fd >= 0 )
result = close(fd);
return result;
}

canonical_decompiled_code:
int func1()
{
int result; // eax

if ( fd >= 0 )
result = close(fd);
return result;
}

original_code:
void uart_finit(void) {
  if(fd < 0) return;
  close(fd);
}

canonical_original_code:
void uart_finit() {
  if(fd < 0) return;
  close(fd);
}


decompiled_code:
void __fastcall uart_setb(unsigned int a1)
{
tcflag_t v1; // eax
struct termios v2; // [rsp+0h] [rbp-48h] BYREF

if ( fd >= 0 )
{
if ( a1 == 57600 )
{
tio_buad_2 = 4097;
v1 = 6321;
goto LABEL_7;
}
if ( a1 > 0xE100 )
{
if ( a1 == 115200 )
{
tio_buad_2 = 4098;
v1 = 6322;
goto LABEL_7;
}
}
else
{
if ( a1 == 19200 )
{
tio_buad_2 = 14;
v1 = 2238;
goto LABEL_7;
}
if ( a1 == 38400 )
{
tio_buad_2 = 15;
v1 = 2239;
LABEL_7:
*(_QWORD *)&v2.c_cc[31] = 0LL;
*(_QWORD *)&v2.c_iflag = 0LL;
*(_OWORD *)&v2.c_line = 0LL;
*(_OWORD *)&v2.c_cc[15] = 0LL;
v2.c_ospeed = 0;
v2.c_cflag = v1;
v2.c_lflag = 128;
tcflush(fd, 0);
tcsetattr(fd, 0, &v2);
return;
}
}
tio_buad_2 = 13;
v1 = 2237;
goto LABEL_7;
}
}

original_code:
void uart_setb(uint32_t baud) {
  if(fd < 0) return;
  static int tio_buad;
  switch(baud) {
    case 9600:
      tio_buad = 0000015;
      break;
    case 19200:
      tio_buad = 0000016;
      break;
    case 38400:
      tio_buad = 0000017;
      break;
    case 57600:
      tio_buad = 0010001;
      break;
    case 115200:
      tio_buad = 0010002;
      break;
    default:
      tio_buad = 0000015;
  }
  struct termios tio = {
    .c_cflag = tio_buad | 0000060 | 0004000 | 0000200,
    .c_iflag = 0,
    .c_oflag = 0,
    .c_lflag = 0000200,
    .c_cc = {0},
  };
  tcflush(fd, 0);
  tcsetattr(fd, 0, &tio);
}

canonical_original_code:
void uart_setb(unsigned int baud) {
  if(fd < 0) return;
  static int tio_buad;
  switch(baud) {
    case 9600:
      tio_buad = 0000015;
      break;
    case 19200:
      tio_buad = 0000016;
      break;
    case 38400:
      tio_buad = 0000017;
      break;
    case 57600:
      tio_buad = 0010001;
      break;
    case 115200:
      tio_buad = 0010002;
      break;
    default:
      tio_buad = 0000015;
  }
  struct termios tio = {
    .c_cflag = tio_buad | 0000060 | 0004000 | 0000200,
    .c_iflag = 0,
    .c_oflag = 0,
    .c_lflag = 0000200,
    .c_cc = {0},
  };
  tcflush(fd, 0);
  tcsetattr(fd, 0, &tio);
}

variable_types:
baud
unsigned int

variable_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }


decompiled_code:
void __fastcall uart_putc(char a1)
{
char buf[12]; // [rsp+Ch] [rbp-Ch] BYREF

buf[0] = a1;
if ( fd >= 0 && write(fd, buf, 1uLL) != 1 )
_assert_fail("ret == 1", "uart_posix.c", 0x60u, "uart_putc");
}

canonical_decompiled_code:
void __fastcall func3(char a1)
{
char buf[12]; // [rsp+Ch] [rbp-Ch] BYREF

buf[0] = a1;
if ( fd >= 0 && write(fd, buf, 1uLL) != 1 )
_assert_fail("ret == 1", "uart_posix.c", 0x60u, "uart_putc");
}

original_code:
void uart_putc(unsigned char c) {
  ssize_t ret;
  if(fd < 0) return;
  ret = write(fd, &c, 1);
  ((void) sizeof ((ret == 1) ? 1 : 0), __extension__ ({ if (ret == 1) ; else __assert_fail ("ret == 1", "uart_posix.c", 96, __extension__ __PRETTY_FUNCTION__); }));
}

canonical_original_code:
void uart_putc(unsigned char c) {
  long ret;
  if(fd < 0) return;
  ret = write(fd, &c, 1);
  ((void) sizeof ((ret == 1) ? 1 : 0), __extension__ ({ if (ret == 1) ; else __assert_fail ("ret == 1", "uart_posix.c", 96, __extension__ __PRETTY_FUNCTION__); }));
}

variable_types: ret long


decompiled_code:
__int64 uart_getc()
{
int v0; // edi

v0 = fd;
if ( fd < 0 )
return 0LL;
while ( !read(v0, &c_0, 1uLL) )
v0 = fd;
return (unsigned __int8)c_0;
}

canonical_decompiled_code:
__int64 func4()
{
int v0; // edi

v0 = fd;
if ( fd < 0 )
return 0LL;
while ( !read(v0, &c_0, 1uLL) )
v0 = fd;
return (unsigned __int8)c_0;
}

original_code:
unsigned char uart_getc(void) {
  if(fd < 0) return 0x00;
  static unsigned char c = 0x00;
  while(!read(fd, &c, 1));
  return c;
}

canonical_original_code:
unsigned char uart_getc() {
  if(fd < 0) return 0x00;
  static unsigned char c = 0x00;
  while(!read(fd, &c, 1));
  return c;
}


repo: dwyer/li

decompiled_code:
void *__fastcall li_allocate(void *ptr, size_t nmemb, size_t size)
{
void *v3; // r12

if ( ptr )
v3 = realloc(ptr, nmemb * size);
else
v3 = calloc(nmemb, size);
if ( !v3 )
li_error_fmt("out of memory");
return v3;
}

canonical_decompiled_code:
void *__fastcall func0(void *ptr, size_t nmemb, size_t size)
{
void *v3; // r12

if ( ptr )
v3 = realloc(ptr, nmemb * size);
else
v3 = calloc(nmemb, size);
if ( !v3 )
li_error_fmt("out of memory");
return v3;
}

original_code:
extern void *li_allocate(void *ptr, size_t count, size_t size)
{
    if (ptr)
        ptr = realloc(ptr, count*size);
    else
        ptr = calloc(count, size);
    if (!ptr)
        li_error_fmt("out of memory");
    return ptr;
}

canonical_original_code:
extern void *li_allocate(void *ptr, unsigned long count, unsigned long size)
{
    if (ptr)
        ptr = realloc(ptr, count*size);
    else
        ptr = calloc(count, size);
    if (!ptr)
        li_error_fmt("out of memory");
    return ptr;
}

variable_types:
ptr
void *

variable_types:
count
unsigned long

variable_types:
size
unsigned long

return_type: void *


decompiled_code:
__int64 __fastcall li_nat_read(__int64 *a1, const char *a2)
{
__int64 v2; // rbx
__int64 v3; // r12
const unsigned __int16 *v4; // rax

v2 = *a2;
if ( !(_BYTE)v2 )
return 0LL;
v3 = 0LL;
v4 = *_ctype_b_loc();
while ( (v4[v2] & 0x800) != 0 )
{
v2 = a2[++v3];
if ( !(_BYTE)v2 )
goto LABEL_6;
}
if ( !v3 )
return v3;
LABEL_6:
*a1 = atol(a2);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 *a1, const char *a2)
{
__int64 v2; // rbx
__int64 v3; // r12
const unsigned __int16 *v4; // rax

v2 = *a2;
if ( !(_BYTE)v2 )
return 0LL;
v3 = 0LL;
v4 = *_ctype_b_loc();
while ( (v4[v2] & 0x800) != 0 )
{
v2 = a2[++v3];
if ( !(_BYTE)v2 )
goto LABEL_6;
}
if ( !v3 )
return v3;
LABEL_6:
*a1 = atol(a2);
return v3;
}

original_code:
extern size_t li_nat_read(li_nat_t *dst, const char *s)
{
    size_t n;
    for (n = 0; s[n] && ((*__ctype_b_loc ())[(int) ((s[n]))] & (unsigned short int) _ISdigit); ++n)
        ;
    if (n)
        dst->data = atol(s);
    return n;
}

canonical_original_code:
extern unsigned long li_nat_read(struct li_nat_t *dst, char *s)
{
    unsigned long n;
    for (n = 0; s[n] && ((*__ctype_b_loc ())[(int) ((s[n]))] & (unsigned short) _ISdigit); ++n)
        ;
    if (n)
        dst->data = atol(s);
    return n;
}

variable_types: struct li_nat_t { unsigned long data; } *

variable_types:
s
char *

variable_types:
n
unsigned long

return_type: unsigned long


decompiled_code:
__int64 __fastcall li_nat_parse(const char *a1)
{
__int64 v2; // [rsp+8h] [rbp-10h] BYREF

if ( !li_nat_read(&v2, a1) )
_assert_fail("li_nat_read(&x, s)", "src/nat.c", 0x14u, "li_nat_parse");
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1)
{
__int64 v2; // [rsp+8h] [rbp-10h] BYREF

if ( !func0(&v2, a1) )
_assert_fail("li_nat_read(&x, s)", "src/nat.c", 0x14u, "li_nat_parse");
return v2;
}

original_code:
extern li_nat_t li_nat_parse(const char *s)
{
    li_nat_t x;
    ((li_nat_read(&x, s)) ? (void) (0) : __assert_fail ("li_nat_read(&x, s)", "src/nat.c", 20, __extension__ __PRETTY_FUNCTION__));
    return x;
}

canonical_original_code:
extern struct li_nat_t li_nat_parse(char *s)
{
    struct li_nat_t x;
    ((li_nat_read(&x, s)) ? (void) (0) : __assert_fail ("li_nat_read(&x, s)", "src/nat.c", 20, __extension__ __PRETTY_FUNCTION__));
    return x;
}

variable_types:
x
struct li_nat_t { unsigned long data; }

return_type: struct li_nat_t { unsigned long data; }


decompiled_code:
unsigned __int64 __fastcall li_nat_with_int(__int64 a1)
{
return abs64(a1);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func2(__int64 a1)
{
return abs64(a1);
}

original_code:
extern li_nat_t li_nat_with_int(li_int_t x)
{
    li_nat_t z;
    z.data = labs(x);
    return z;
}

canonical_original_code:
extern struct li_nat_t li_nat_with_int(long x)
{
    struct li_nat_t z;
    z.data = labs(x);
    return z;
}

variable_types: x long

variable_types:
z
struct li_nat_t { unsigned long data; }


decompiled_code:
double __fastcall li_nat_to_dec(__int64 a1)
{
double result; // xmm0_8

if ( a1 < 0 )
result = (double)(int)(a1 & 1 | ((unsigned __int64)a1 >> 1)) + (double)(int)(a1 & 1 | ((unsigned __int64)a1 >> 1));
else
result = (double)(int)a1;
return result;
}

canonical_decompiled_code:
double __fastcall func3(__int64 a1)
{
double result; // xmm0_8

if ( a1 < 0 )
result = (double)(int)(a1 & 1 | ((unsigned __int64)a1 >> 1)) + (double)(int)(a1 & 1 | ((unsigned __int64)a1 >> 1));
else
result = (double)(int)a1;
return result;
}

original_code:
extern li_dec_t li_nat_to_dec(li_nat_t x)
{
    return (li_dec_t)x.data;
}

canonical_original_code:
extern double li_nat_to_dec(struct li_nat_t x)
{
    return (double)x.data;
}

return_type: double


decompiled_code:
__int64 __fastcall li_nat_to_int(__int64 a1)
{
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
return a1;
}

original_code:
extern li_int_t li_nat_to_int(li_nat_t x)
{
    return (li_int_t)x.data;
}

canonical_original_code:
extern long li_nat_to_int(struct li_nat_t x)
{
    return (long)x.data;
}

return_type: long


decompiled_code:
_BOOL8 __fastcall li_nat_is_zero(__int64 a1)
{
return a1 == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func5(__int64 a1)
{
return a1 == 0;
}

original_code:
extern li_bool_t li_nat_is_zero(li_nat_t x)
{
    return x.data == 0;
}

canonical_original_code:
extern int li_nat_is_zero(struct li_nat_t x)
{
    return x.data == 0;
}


decompiled_code:
__int64 __fastcall li_nat_add(__int64 a1, __int64 a2)
{
return a2 + a1;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, __int64 a2)
{
return a2 + a1;
}

original_code:
extern li_nat_t li_nat_add(li_nat_t x, li_nat_t y)
{
    x.data += y.data;
    return x;
}

canonical_original_code:
extern struct li_nat_t li_nat_add(struct li_nat_t x, struct li_nat_t y)
{
    x.data += y.data;
    return x;
}

variable_types:
x
struct li_nat_t

variable_types:
y
struct li_nat_t


decompiled_code:
__int64 __fastcall li_nat_mul(__int64 a1, __int64 a2)
{
return a1 * a2;
}

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

original_code:
extern li_nat_t li_nat_mul(li_nat_t x, li_nat_t y)
{
    x.data *= y.data;
    return x;
}

canonical_original_code:
extern struct li_nat_t li_nat_mul(struct li_nat_t x, struct li_nat_t y)
{
    x.data *= y.data;
    return x;
}


decompiled_code:
unsigned __int64 __fastcall li_nat_sub(unsigned __int64 a1, unsigned __int64 a2)
{
if ( a1 < a2 )
_assert_fail("x.data >= y.data", "src/nat.c", 0x3Cu, "li_nat_sub");
return a1 - a2;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func8(unsigned __int64 a1, unsigned __int64 a2)
{
if ( a1 < a2 )
_assert_fail("x.data >= y.data", "src/nat.c", 0x3Cu, "li_nat_sub");
return a1 - a2;
}

original_code:
extern li_nat_t li_nat_sub(li_nat_t x, li_nat_t y)
{
    ((x.data >= y.data) ? (void) (0) : __assert_fail ("x.data >= y.data", "src/nat.c", 60, __extension__ __PRETTY_FUNCTION__));
    x.data -= y.data;
    return x;
}

canonical_original_code:
extern struct li_nat_t li_nat_sub(struct li_nat_t x, struct li_nat_t y)
{
    ((x.data >= y.data) ? (void) (0) : __assert_fail ("x.data >= y.data", "src/nat.c", 60, __extension__ __PRETTY_FUNCTION__));
    x.data -= y.data;
    return x;
}


decompiled_code:
unsigned __int64 __fastcall li_nat_div(unsigned __int64 a1, unsigned __int64 a2)
{
if ( !a2 )
_assert_fail("y.data", "src/nat.c", 0x43u, "li_nat_div");
return a1 / a2;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func9(unsigned __int64 a1, unsigned __int64 a2)
{
if ( !a2 )
_assert_fail("y.data", "src/nat.c", 0x43u, "li_nat_div");
return a1 / a2;
}

original_code:
extern li_nat_t li_nat_div(li_nat_t x, li_nat_t y)
{
    ((y.data) ? (void) (0) : __assert_fail ("y.data", "src/nat.c", 67, __extension__ __PRETTY_FUNCTION__));
    x.data /= y.data;
    return x;
}

canonical_original_code:
extern struct li_nat_t li_nat_div(struct li_nat_t x, struct li_nat_t y)
{
    ((y.data) ? (void) (0) : __assert_fail ("y.data", "src/nat.c", 67, __extension__ __PRETTY_FUNCTION__));
    x.data /= y.data;
    return x;
}


decompiled_code:
unsigned __int64 __fastcall li_nat_mod(unsigned __int64 a1, unsigned __int64 a2)
{
if ( !a2 )
_assert_fail("y.data", "src/nat.c", 0x4Au, "li_nat_mod");
return a1 % a2;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func10(unsigned __int64 a1, unsigned __int64 a2)
{
if ( !a2 )
_assert_fail("y.data", "src/nat.c", 0x4Au, "li_nat_mod");
return a1 % a2;
}

original_code:
extern li_nat_t li_nat_mod(li_nat_t x, li_nat_t y)
{
    ((y.data) ? (void) (0) : __assert_fail ("y.data", "src/nat.c", 74, __extension__ __PRETTY_FUNCTION__));
    x.data %= y.data;
    return x;
}

canonical_original_code:
extern struct li_nat_t li_nat_mod(struct li_nat_t x, struct li_nat_t y)
{
    ((y.data) ? (void) (0) : __assert_fail ("y.data", "src/nat.c", 74, __extension__ __PRETTY_FUNCTION__));
    x.data %= y.data;
    return x;
}


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

result = a1 > a2;
if ( a1 < a2 )
result = 0xFFFFFFFFLL;
return result;
}

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

result = a1 > a2;
if ( a1 < a2 )
result = 0xFFFFFFFFLL;
return result;
}

original_code:
extern li_cmp_t li_nat_cmp(li_nat_t x, li_nat_t y)
{
    if (x.data < y.data)
        return LI_CMP_LT;
    else if (x.data > y.data)
        return LI_CMP_GT;
    return LI_CMP_EQ;
}

canonical_original_code:
extern enum li_cmp_t li_nat_cmp(struct li_nat_t x, struct li_nat_t y)
{
    if (x.data < y.data)
        return LI_CMP_LT;
    else if (x.data > y.data)
        return LI_CMP_GT;
    return LI_CMP_EQ;
}

return_type: enum li_cmp_t { LI_CMP_LT=-1, LI_CMP_EQ=0, LI_CMP_GT=1 }


decompiled_code:
unsigned __int64 __fastcall li_nat_gcd(__int64 a1, unsigned __int64 a2)
{
unsigned __int64 v2; // r8

while ( !li_nat_is_zero(a2) )
a2 = li_nat_mod(v2, a2);
return v2;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func12(__int64 a1, unsigned __int64 a2)
{
unsigned __int64 v2; // r8

while ( !func5(a2) )
a2 = func10(v2, a2);
return v2;
}

original_code:
extern li_nat_t li_nat_gcd(li_nat_t x, li_nat_t y)
{
    while (!li_nat_is_zero(y)) {
        li_nat_t z = y;
        y = li_nat_mod(x, y);
        x = z;
    }
    return x;
}

canonical_original_code:
extern struct li_nat_t li_nat_gcd(struct li_nat_t x, struct li_nat_t y)
{
    while (!li_nat_is_zero(y)) {
        struct li_nat_t z = y;
        y = li_nat_mod(x, y);
        x = z;
    }
    return x;
}


decompiled_code:
size_t __fastcall li_chr_decode(int *a1, const char *a2)
{
size_t result; // rax
size_t v5; // rcx
__int64 v6; // rdx
unsigned __int8 v7; // al
size_t v8; // rsi
__int64 v9; // rax
unsigned __int8 v10; // r8
unsigned __int8 v11; // r9
unsigned __int8 v12; // dl
char v13; // di
int v14; // ecx
char v15; // dl

result = strlen(a2);
if ( !result )
{
if ( a1 )
{
*a1 = 65533;
return result;
}
return 0LL;
}
v5 = result;
v6 = *(unsigned __int8 *)a2;
v7 = first[v6];
if ( v7 > 0xEFu )
{
if ( a1 )
{
*a1 = (char)v6 | v7 & 1;
return 1LL;
}
}
else
{
v8 = v7 & 7;
if ( v5 >= v8 )
{
v9 = v7 >> 4;
v10 = accept_ranges[2 * v9];
v11 = accept_ranges[2 * v9 + 1];
if ( a1 )
*a1 = 0;
if ( v8 )
{
result = 0LL;
while ( result )
{
v12 = a2[result];
if ( v12 < v10 || v11 < v12 )
goto LABEL_19;
if ( a1 )
{
v13 = 63;
v14 = *a1 << 6;
*a1 = v14;
v15 = a2[result];
LABEL_12:
*a1 = v14 | (unsigned __int8)(v13 & v15);
}
LABEL_13:
if ( v8 == ++result )
return result;
}
if ( !a1 )
goto LABEL_13;
v13 = byte_346[v8];
v14 = *a1 << 6;
*a1 = v14;
v15 = *a2;
goto LABEL_12;
}
return 0LL;
}
LABEL_19:
if ( a1 )
*a1 = 65533;
}
return 1LL;
}

canonical_decompiled_code:
size_t __fastcall func0(int *a1, const char *a2)
{
size_t result; // rax
size_t v5; // rcx
__int64 v6; // rdx
unsigned __int8 v7; // al
size_t v8; // rsi
__int64 v9; // rax
unsigned __int8 v10; // r8
unsigned __int8 v11; // r9
unsigned __int8 v12; // dl
char v13; // di
int v14; // ecx
char v15; // dl

result = strlen(a2);
if ( !result )
{
if ( a1 )
{
*a1 = 65533;
return result;
}
return 0LL;
}
v5 = result;
v6 = *(unsigned __int8 *)a2;
v7 = first[v6];
if ( v7 > 0xEFu )
{
if ( a1 )
{
*a1 = (char)v6 | v7 & 1;
return 1LL;
}
}
else
{
v8 = v7 & 7;
if ( v5 >= v8 )
{
v9 = v7 >> 4;
v10 = accept_ranges[2 * v9];
v11 = accept_ranges[2 * v9 + 1];
if ( a1 )
*a1 = 0;
if ( v8 )
{
result = 0LL;
while ( result )
{
v12 = a2[result];
if ( v12 < v10 || v11 < v12 )
goto LABEL_19;
if ( a1 )
{
v13 = 63;
v14 = *a1 << 6;
*a1 = v14;
v15 = a2[result];
LABEL_12:
*a1 = v14 | (unsigned __int8)(v13 & v15);
}
LABEL_13:
if ( v8 == ++result )
return result;
}
if ( !a1 )
goto LABEL_13;
v13 = byte_346[v8];
v14 = *a1 << 6;
*a1 = v14;
v15 = *a2;
goto LABEL_12;
}
return 0LL;
}
LABEL_19:
if ( a1 )
*a1 = 65533;
}
return 1LL;
}

original_code:
extern size_t li_chr_decode(li_character_t *chr, const char *s)
{
    size_t sz, i;
    size_t n = strlen(s);
    struct accept_range accept;
    if (n < 1) {
        if (chr)
            *chr = ((li_character_t)0xFFFD);
        return 0;
    }
    do {
        li_byte_t x = first[(li_byte_t)s[0]];
        if (x >= as) {
            li_character_t mask = (li_character_t)x << 31 >> 31;
            if (chr)
                *chr = ((li_character_t)s[0] & ~mask) | (((li_character_t)0xFFFD) & mask);
            return 1;
        }
        sz = x & 7;
        accept = accept_ranges[x >> 4];
    } while (0);
    if (n < sz) {
        if (chr)
            *chr = ((li_character_t)0xFFFD);
        return 1;
    }
    if (chr)
        *chr = 0;
    for (i = 0; i < sz; ++i) {
        if (i && ((li_byte_t)s[i] < accept.lo || accept.hi < (li_byte_t)s[i])) {
            if (chr)
                *chr = ((li_character_t)0xFFFD);
            return 1;
        }
        if (chr) {
            *chr <<= 6;
            *chr |= (li_character_t)((li_byte_t)s[i] & masks[i ? 0 : sz-1]);
        }
    }
    return sz;
}

canonical_original_code:
extern unsigned long li_chr_decode(unsigned int *chr, char *s)
{
    unsigned long sz; unsigned long i;
    unsigned long n = strlen(s);
    struct accept_range accept;
    if (n < 1) {
        if (chr)
            *chr = ((unsigned int)0xFFFD);
        return 0;
    }
    do {
        unsigned char x = first[(li_byte_t)s[0]];
        if (x >= as) {
            unsigned int mask = (li_character_t)x << 31 >> 31;
            if (chr)
                *chr = ((unsigned int)s[0] & ~mask) | (((unsigned int)0xFFFD) & mask);
            return 1;
        }
        sz = x & 7;
        accept = accept_ranges[x >> 4];
    } while (0);
    if (n < sz) {
        if (chr)
            *chr = ((unsigned int)0xFFFD);
        return 1;
    }
    if (chr)
        *chr = 0;
    for (i = 0; i < sz; ++i) {
        if (i && ((unsigned char)s[i] < accept.lo || accept.hi < (unsigned char)s[i])) {
            if (chr)
                *chr = ((unsigned int)0xFFFD);
            return 1;
        }
        if (chr) {
            *chr <<= 6;
            *chr |= (li_character_t)((unsigned char)s[i] & masks[i ? 0 : sz-1]);
        }
    }
    return sz;
}

variable_types:
chr
unsigned int *

variable_types:
sz
unsigned long

variable_types:
i
unsigned long

variable_types: struct accept_range { unsigned char lo; unsigned char hi; }

variable_types:
x
unsigned char

variable_types:
mask
unsigned int


decompiled_code:
__int64 __fastcall li_chr_encode(unsigned int a1, _BYTE *a2, unsigned __int64 a3)
{
unsigned __int64 v4; // rsi
__int64 result; // rax
char v6; // cl
unsigned int v7; // edx

if ( a1 > 0x10FFFF )
{
a1 = 65533;
LABEL_14:
v4 = 3LL;
goto LABEL_6;
}
if ( a1 <= 0x7F )
{
v4 = 1LL;
}
else
{
if ( a1 > 0x7FF )
{
if ( a1 > 0xFFFF )
{
v4 = 4LL;
goto LABEL_6;
}
goto LABEL_14;
}
v4 = 2LL;
}
LABEL_6:
result = 0LL;
if ( v4 <= a3 )
{
v6 = 6 * v4 - 6;
do
{
v7 = a1 >> v6;
if ( result )
a2[result] = v7 & 0x3F | 0x80;
else
*a2 = t[v4] | v7;
++result;
v6 -= 6;
}
while ( v4 != result );
a2[result] = 0;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1, _BYTE *a2, unsigned __int64 a3)
{
unsigned __int64 v4; // rsi
__int64 result; // rax
char v6; // cl
unsigned int v7; // edx

if ( a1 > 0x10FFFF )
{
a1 = 65533;
LABEL_14:
v4 = 3LL;
goto LABEL_6;
}
if ( a1 <= 0x7F )
{
v4 = 1LL;
}
else
{
if ( a1 > 0x7FF )
{
if ( a1 > 0xFFFF )
{
v4 = 4LL;
goto LABEL_6;
}
goto LABEL_14;
}
v4 = 2LL;
}
LABEL_6:
result = 0LL;
if ( v4 <= a3 )
{
v6 = 6 * v4 - 6;
do
{
v7 = a1 >> v6;
if ( result )
a2[result] = v7 & 0x3F | 0x80;
else
*a2 = t[v4] | v7;
++result;
v6 -= 6;
}
while ( v4 != result );
a2[result] = 0;
}
return result;
}

original_code:
extern size_t li_chr_encode(li_character_t chr, char *s, size_t n)
{
    size_t i, sz;
    if (chr > ((li_character_t)0x0010FFFF))
        chr = ((li_character_t)0xFFFD);
    for (sz = 1; sz < 4; ++sz)
        if (chr <= rune_max[sz])
            break;
    if (n < sz)
        return 0;
    for (i = 0; i < sz; ++i) {
        uint8_t b = (li_byte_t)(chr >> ((sz-i-1) * 6));
        if (i)
            s[i] = t[0] | (b & masks[0]);
        else
            s[i] = t[sz] | b;
    }
    s[i] = '\0';
    return sz;
}

canonical_original_code:
extern unsigned long li_chr_encode(unsigned int chr, char *s, unsigned long n)
{
    unsigned long i; unsigned long sz;
    if (chr > ((unsigned int)0x0010FFFF))
        chr = ((unsigned int)0xFFFD);
    for (sz = 1; sz < 4; ++sz)
        if (chr <= rune_max[sz])
            break;
    if (n < sz)
        return 0;
    for (i = 0; i < sz; ++i) {
        unsigned char b = (li_byte_t)(chr >> ((sz-i-1) * 6));
        if (i)
            s[i] = t[0] | (b & masks[0]);
        else
            s[i] = t[sz] | b;
    }
    s[i] = '\0';
    return sz;
}

variable_types:
chr
unsigned int

variable_types:
b
unsigned char


decompiled_code:
__int64 __fastcall li_chr_count(const char *a1)
{
__int64 i; // r12
size_t v3; // rax

for ( i = 0LL; ; ++i )
{
v3 = li_chr_decode(0LL, a1);
if ( !v3 )
break;
a1 += v3;
}
return i;
}

canonical_decompiled_code:
__int64 __fastcall func2(const char *a1)
{
__int64 i; // r12
size_t v3; // rax

for ( i = 0LL; ; ++i )
{
v3 = func0(0LL, a1);
if ( !v3 )
break;
a1 += v3;
}
return i;
}

original_code:
extern size_t li_chr_count(const char *s)
{
    size_t count = 0;
    size_t n;
    while ((n = li_chr_decode(((void *)0), s))) {
        s += n;
        count++;
    }
    return count;
}

canonical_original_code:
extern unsigned long li_chr_count(char *s)
{
    unsigned long count = 0;
    unsigned long n;
    while ((n = li_chr_decode(((void *)0), s))) {
        s += n;
        count++;
    }
    return count;
}

variable_types:
count
unsigned long


yyerror

decompiled_code:
__int64 __fastcall yyerror(__int64 a1)
{
__int64 v1; // r12
__int64 v2; // rax

v1 = li_num_with_int(yylineno);
v2 = li_string_make(a1);
return li_error_fmt("read: ~a: line ~a", v2, v1);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
__int64 v1; // r12
__int64 v2; // rax

v1 = li_num_with_int(yylineno);
v2 = li_string_make(a1);
return li_error_fmt("read: ~a: line ~a", v2, v1);
}

original_code:
static void yyerror(char *s)
{
    li_error_fmt("read: ~a: line ~a", li_string_make(s), li_num_with_int(yylineno));
}


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

result = li_cdr(stack);
stack = result;
return result;
}

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

result = li_cdr(stack);
stack = result;
return result;
}

original_code:
extern void li_stack_trace_pop(void)
{
    stack = li_cdr(stack);
}

canonical_original_code:
extern void li_stack_trace_pop()
{
    stack = li_cdr(stack);
}


decompiled_code:
void li_stack_trace_clear()
{
stack = 0LL;
}

canonical_decompiled_code:
void func4()
{
stack = 0LL;
}

original_code:
extern void li_stack_trace_clear(void)
{
    stack = ((void *)0);
}

canonical_original_code:
extern void li_stack_trace_clear()
{
    stack = ((void *)0);
}


repo: dylan-evans/objspace

decompiled_code:
void map_hash_set()
{
;
}

canonical_decompiled_code:
void func0()
{
;
}

original_code:
int map_hash_set(Object *self, Key *key, Object *value)
{
}

canonical_original_code:
int map_hash_set(struct _object *self, struct _key *key, struct _object *value)
{
}

variable_types: struct _object { int type; struct _object *container; struct _engine { int (*set)(struct _object *, struct _key { char *str; unsigned int index; } *, struct _object *); struct _object *(*get)(struct _object *, struct _key *); int (*destroy)(struct _object *, struct _key *); struct _iter { struct _object *object; int index; } *(*iter)(struct _object *); struct _object *(*next)(struct _iter *); struct _lock { int id; struct _object *object; } *(*lock)(struct _object *, int); int (*unlock)(struct _lock *); int (*length)(struct _object *); char *(*string)(struct _object *); } *engine; } *

variable_types:
key
struct _key *

variable_types:
value
struct _object *

user_defined_types: struct _object { int type; struct _object *container; struct _engine *engine; }

user_defined_types: struct _engine { int (*set)(struct _object *, struct _key *, struct _object *); struct _object *(*get)(struct _object *, struct _key *); int (*destroy)(struct _object *, struct _key *); struct _iter *(*iter)(struct _object *); struct _object *(*next)(struct _iter *); struct _lock *(*lock)(struct _object *, int); int (*unlock)(struct _lock *); int (*length)(struct _object *); char *(*string)(struct _object *); }


decompiled_code:
void map_hash_get()
{
;
}

canonical_decompiled_code:
void func1()
{
;
}

original_code:
Object *map_hash_get(Object *self, Key *key)
{
}

canonical_original_code:
struct _object *map_hash_get(struct _object *self, struct _key *key)
{
}

variable_types:
self
struct _object *

return_type: struct _object { int type; struct _object *container; struct _engine { int (*set)(struct _object *, struct _key { char *str; unsigned int index; } *, struct _object *); struct _object *(*get)(struct _object *, struct _key *); int (*destroy)(struct _object *, struct _key *); struct _iter { struct _object *object; int index; } *(*iter)(struct _object *); struct _object *(*next)(struct _iter *); struct _lock { int id; struct _object *object; } *(*lock)(struct _object *, int); int (*unlock)(struct _lock *); int (*length)(struct _object *); char *(*string)(struct _object *); } *engine; } *


decompiled_code:
void map_hash_destroy()
{
;
}

canonical_decompiled_code:
void func2()
{
;
}

original_code:
int map_hash_destroy(Object *self, Key *key)
{
}

canonical_original_code:
int map_hash_destroy(struct _object *self, struct _key *key)
{
}

user_defined_types: struct _key { char *str; unsigned int index; }

user_defined_types: struct _iter { struct _object *object; int index; }

user_defined_types: struct _lock { int id; struct _object *object; }


decompiled_code:
void map_hash_iter()
{
;
}

canonical_decompiled_code:
void func3()
{
;
}

original_code:
Iter *map_hash_iter(Object *self)
{
}

canonical_original_code:
struct _iter *map_hash_iter(struct _object *self)
{
}

return_type: struct _iter { struct _object { int type; struct _object *container; struct _engine { int (*set)(struct _object *, struct _key { char *str; unsigned int index; } *, struct _object *); struct _object *(*get)(struct _object *, struct _key *); int (*destroy)(struct _object *, struct _key *); struct _iter *(*iter)(struct _object *); struct _object *(*next)(struct _iter *); struct _lock { int id; struct _object *object; } *(*lock)(struct _object *, int); int (*unlock)(struct _lock *); int (*length)(struct _object *); char *(*string)(struct _object *); } *engine; } *object; int index; } *


decompiled_code:
// attributes: thunk
void map_hash_next()
{
map_hash_iter();
}

canonical_decompiled_code:
void func4()
{
func3();
}

original_code:
Object *map_hash_next(Iter *iter)
{
}

canonical_original_code:
struct _object *map_hash_next(struct _iter *iter)
{
}

variable_types:
iter
struct _iter *


decompiled_code:
void map_hash_lock()
{
;
}

canonical_decompiled_code:
void func5()
{
;
}

original_code:
Lock *map_hash_lock(Object *self, int timeout)
{
}

canonical_original_code:
struct _lock *map_hash_lock(struct _object *self, int timeout)
{
}

variable_types: timeout int

return_type: struct _lock { int id; struct _object { int type; struct _object *container; struct _engine { int (*set)(struct _object *, struct _key { char *str; unsigned int index; } *, struct _object *); struct _object *(*get)(struct _object *, struct _key *); int (*destroy)(struct _object *, struct _key *); struct _iter { struct _object *object; int index; } *(*iter)(struct _object *); struct _object *(*next)(struct _iter *); struct _lock *(*lock)(struct _object *, int); int (*unlock)(struct _lock *); int (*length)(struct _object *); char *(*string)(struct _object *); } *engine; } *object; } *


decompiled_code:
_DWORD *map_hash_create()
{
return map_create_with_engine((__int64)&map_engine_0, 33);
}

canonical_decompiled_code:
_DWORD *func6()
{
return map_create_with_engine((__int64)&map_engine_0, 33);
}

original_code:
Object *map_hash_create()
{
    static Engine map_engine = {
        .set = map_hash_set,
        .get = map_hash_get,
        .destroy = map_hash_destroy,
        .iter = map_hash_iter,
        .next = map_hash_next,
        .lock = map_hash_lock
    };
    return map_create_with_engine(&map_engine, 0x21);
}

canonical_original_code:
struct _object *map_hash_create()
{
    static struct _engine map_engine = {
        .set = map_hash_set,
        .get = map_hash_get,
        .destroy = map_hash_destroy,
        .iter = map_hash_iter,
        .next = map_hash_next,
        .lock = map_hash_lock
    };
    return map_create_with_engine(&map_engine, 0x21);
}

variable_types: struct _engine { int (*set)(struct _object { int type; struct _object *container; struct _engine *engine; } *, struct _key { char *str; unsigned int index; } *, struct _object *); struct _object *(*get)(struct _object *, struct _key *); int (*destroy)(struct _object *, struct _key *); struct _iter { struct _object *object; int index; } *(*iter)(struct _object *); struct _object *(*next)(struct _iter *); struct _lock { int id; struct _object *object; } *(*lock)(struct _object *, int); int (*unlock)(struct _lock *); int (*length)(struct _object *); char *(*string)(struct _object *); }


main

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

if ( !(unsigned int)CU_initialize_registry(argc, argv, envp) )
{
if ( (unsigned int)setup_object_tests() && (unsigned int)setup_list_tests() )
{
CU_basic_set_mode(2LL);
CU_basic_run_tests();
putchar(10);
v3 = CU_get_failure_list();
CU_basic_show_failures(v3);
puts("\n");
CU_automated_run_tests();
CU_list_tests_to_file();
}
CU_cleanup_registry();
}
return CU_get_error();
}

original_code:
int main(int argc, char **argv)
{
    int status;
    if(CUE_SUCCESS != CU_initialize_registry())
        return CU_get_error();
    if(!(status = setup_object_tests())
        || !(status = setup_list_tests()))
    {
        CU_cleanup_registry();
        return CU_get_error();
    }
    CU_basic_set_mode(CU_BRM_VERBOSE);
    CU_basic_run_tests();
    printf("\n");
    CU_basic_show_failures(CU_get_failure_list());
    printf("\n\n");
    CU_automated_run_tests();
    CU_list_tests_to_file();
    CU_cleanup_registry();
    return CU_get_error();
}

variable_types: argc int

variable_types:
argv
char **

variable_types: status int


decompiled_code:
char *__fastcall number_string(double *a1)
{
char *v1; // r12
char *result; // rax

v1 = (char *)malloc(0x40uLL);
if ( *(_DWORD *)a1 == 17 )
{
sprintf(v1, "%d", *((unsigned int *)a1 + 6));
result = v1;
}
else if ( *(_DWORD *)a1 == 18 )
{
sprintf(v1, "%f", a1[3]);
result = v1;
}
else
{
free(v1);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
char *__fastcall func0(double *a1)
{
char *v1; // r12
char *result; // rax

v1 = (char *)malloc(0x40uLL);
if ( *(_DWORD *)a1 == 17 )
{
sprintf(v1, "%d", *((unsigned int *)a1 + 6));
result = v1;
}
else if ( *(_DWORD *)a1 == 18 )
{
sprintf(v1, "%f", a1[3]);
result = v1;
}
else
{
free(v1);
result = 0LL;
}
return result;
}

original_code:
char *number_string(Object *obj)
{
    char *buf;
    buf = malloc(64);
    switch(obj->type)
    {
        case 0x11:
            sprintf(buf, "%d", ((Number *)(obj))->value.num_int);
            break;
        case 0x12:
            sprintf(buf, "%f", ((Number *)(obj))->value.num_float);
            break;
        default:
            free(buf);
            return ((void *)0);
    }
    return buf;
}

canonical_original_code:
char *number_string(struct _object *obj)
{
    char *buf;
    buf = malloc(64);
    switch(obj->type)
    {
        case 0x11:
            sprintf(buf, "%d", ((struct Number *)(obj))->value.num_int);
            break;
        case 0x12:
            sprintf(buf, "%f", ((struct Number *)(obj))->value.num_float);
            break;
        default:
            free(buf);
            return ((void *)0);
    }
    return buf;
}

variable_types:
buf
char *

user_defined_types: struct Number { struct _object object; union { int num_int; double num_float; } value; }


decompiled_code:
__int64 __fastcall obj_set(__int64 a1)
{
__int64 (*v1)(void); // rax
__int64 result; // rax

v1 = **(__int64 (***)(void))(a1 + 16);
if ( v1 )
result = v1();
else
result = 0LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
__int64 (*v1)(void); // rax
__int64 result; // rax

v1 = **(__int64 (***)(void))(a1 + 16);
if ( v1 )
result = v1();
else
result = 0LL;
return result;
}

original_code:
int obj_set(Object *self, Key *key, Object *obj)
{
    if(self->engine->set == ((void *)0))
        return 0;
    return self->engine->set(self, key, obj);
}

canonical_original_code:
int obj_set(struct _object *self, struct _key *key, struct _object *obj)
{
    if(self->engine->set == ((void *)0))
        return 0;
    return self->engine->set(self, key, obj);
}

variable_types:
obj
struct _object *


decompiled_code:
__int64 __fastcall obj_get(__int64 a1)
{
__int64 (*v1)(void); // rax
__int64 result; // rax

v1 = *(__int64 (**)(void))(*(_QWORD *)(a1 + 16) + 8LL);
if ( v1 )
result = v1();
else
result = 0LL;
return result;
}

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

v1 = *(__int64 (**)(void))(*(_QWORD *)(a1 + 16) + 8LL);
if ( v1 )
result = v1();
else
result = 0LL;
return result;
}

original_code:
Object *obj_get(Object *self, Key *key)
{
    if(self->engine->get == ((void *)0))
        return ((void *)0);
    return self->engine->get(self, key);
}

canonical_original_code:
struct _object *obj_get(struct _object *self, struct _key *key)
{
    if(self->engine->get == ((void *)0))
        return ((void *)0);
    return self->engine->get(self, key);
}


decompiled_code:
__int64 __fastcall obj_destroy(__int64 a1)
{
__int64 (*v1)(void); // rax
__int64 result; // rax

v1 = *(__int64 (**)(void))(*(_QWORD *)(a1 + 16) + 16LL);
if ( v1 )
result = v1();
else
result = 0LL;
return result;
}

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

v1 = *(__int64 (**)(void))(*(_QWORD *)(a1 + 16) + 16LL);
if ( v1 )
result = v1();
else
result = 0LL;
return result;
}

original_code:
int obj_destroy(Object *self, Key *key)
{
    if(self->engine->destroy == ((void *)0))
        return 0;
    return self->engine->destroy(self, key);
}

canonical_original_code:
int obj_destroy(struct _object *self, struct _key *key)
{
    if(self->engine->destroy == ((void *)0))
        return 0;
    return self->engine->destroy(self, key);
}


decompiled_code:
__int64 __fastcall obj_iter(__int64 a1)
{
__int64 (*v1)(void); // rax
__int64 result; // rax

v1 = *(__int64 (**)(void))(*(_QWORD *)(a1 + 16) + 24LL);
if ( v1 )
result = v1();
else
result = 0LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
__int64 (*v1)(void); // rax
__int64 result; // rax

v1 = *(__int64 (**)(void))(*(_QWORD *)(a1 + 16) + 24LL);
if ( v1 )
result = v1();
else
result = 0LL;
return result;
}

original_code:
Iter *obj_iter(Object *self)
{
    if(self->engine->iter == ((void *)0))
        return ((void *)0);
    return self->engine->iter(self);
}

canonical_original_code:
struct _iter *obj_iter(struct _object *self)
{
    if(self->engine->iter == ((void *)0))
        return ((void *)0);
    return self->engine->iter(self);
}


decompiled_code:
__int64 __fastcall obj_next(__int64 a1)
{
__int64 (*v1)(void); // rax
__int64 result; // rax

v1 = *(__int64 (**)(void))(*(_QWORD *)(*(_QWORD *)a1 + 16LL) + 32LL);
if ( v1 )
result = v1();
else
result = 0LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
__int64 (*v1)(void); // rax
__int64 result; // rax

v1 = *(__int64 (**)(void))(*(_QWORD *)(*(_QWORD *)a1 + 16LL) + 32LL);
if ( v1 )
result = v1();
else
result = 0LL;
return result;
}

original_code:
Object *obj_next(Iter *iter)
{
    if(iter->object->engine->next == ((void *)0))
        return ((void *)0);
    return iter->object->engine->next(iter);
}

canonical_original_code:
struct _object *obj_next(struct _iter *iter)
{
    if(iter->object->engine->next == ((void *)0))
        return ((void *)0);
    return iter->object->engine->next(iter);
}


decompiled_code:
__int64 __fastcall obj_lock(__int64 a1)
{
__int64 (*v1)(void); // rax
__int64 result; // rax

v1 = *(__int64 (**)(void))(*(_QWORD *)(a1 + 16) + 40LL);
if ( v1 )
result = v1();
else
result = 0LL;
return result;
}

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

v1 = *(__int64 (**)(void))(*(_QWORD *)(a1 + 16) + 40LL);
if ( v1 )
result = v1();
else
result = 0LL;
return result;
}

original_code:
Lock *obj_lock(Object *self, int timeout)
{
    if(self->engine->lock == ((void *)0))
        return ((void *)0);
    return self->engine->lock(self, timeout);
}

canonical_original_code:
struct _lock *obj_lock(struct _object *self, int timeout)
{
    if(self->engine->lock == ((void *)0))
        return ((void *)0);
    return self->engine->lock(self, timeout);
}