// File: /workspace/github_c_makefile_star2_O3/enzo1982[P]smooth/libraries/libcpuid/cpuid_main.c
// Function: cpuid_get_hypervisor
hypervisor_vendor_t cpuid_get_hypervisor(struct cpu_raw_data_t* raw, struct cpu_id_t* data)
{
	int i, r;
	uint32_t hypervisor_fn40000000h[NUM_REGS];
	char hypervisor_str[VENDOR_STR_MAX];
	struct cpu_id_t mydata;
	const struct { hypervisor_vendor_t hypervisor; char match[16]; }
	matchtable[NUM_HYPERVISOR_VENDORS] = {
		/* source: https://github.com/a0rtega/pafish/blob/master/pafish/cpu.c */
		{ HYPERVISOR_BHYVE      , "bhyve bhyve\0"   },
		{ HYPERVISOR_HYPERV     , "Microsoft Hv"    },
		{ HYPERVISOR_KVM        , "KVMKVMKVM\0\0\0" },
		{ HYPERVISOR_PARALLELS  , "prl hyperv\0\0"  },
		{ HYPERVISOR_QEMU       , "TCGTCGTCGTCG"    },
		{ HYPERVISOR_VIRTUALBOX , "VBoxVBoxVBox"    },
		{ HYPERVISOR_VMWARE     , "VMwareVMware"    },
		{ HYPERVISOR_XEN        , "XenVMMXenVMM"    },
	};

	if (!data) {
		if ((r = cpu_identify(raw, data)) < 0)
			return HYPERVISOR_UNKNOWN;
		data = &mydata;
	}

	/* Intel and AMD CPUs have reserved bit 31 of ECX of CPUID leaf 0x1 as the hypervisor present bit
	Source: https://kb.vmware.com/s/article/1009458 */
	switch (data->vendor) {
		case VENDOR_AMD:
		case VENDOR_INTEL:
			break;
		default:
			return HYPERVISOR_UNKNOWN;
	}
	if (!data->flags[CPU_FEATURE_HYPERVISOR])
		return HYPERVISOR_NONE;

	/* Intel and AMD have also reserved CPUID leaves 0x40000000 - 0x400000FF for software use.
	Hypervisors can use these leaves to provide an interface to pass information
	from the hypervisor to the guest operating system running inside a virtual machine.
	The hypervisor bit indicates the presence of a hypervisor
	and that it is safe to test these additional software leaves. */
	memset(hypervisor_fn40000000h, 0, sizeof(hypervisor_fn40000000h));
	hypervisor_fn40000000h[EAX] = 0x40000000;
	cpu_exec_cpuid_ext(hypervisor_fn40000000h);

	/* Copy the hypervisor CPUID information leaf */
	memcpy(hypervisor_str + 0, &hypervisor_fn40000000h[1], 4);
	memcpy(hypervisor_str + 4, &hypervisor_fn40000000h[2], 4);
	memcpy(hypervisor_str + 8, &hypervisor_fn40000000h[3], 4);
	hypervisor_str[12] = '\0';

	/* Determine hypervisor */
	for (i = 0; i < NUM_HYPERVISOR_VENDORS; i++)
		if (!strcmp(hypervisor_str, matchtable[i].match))
			return matchtable[i].hypervisor;
	return HYPERVISOR_UNKNOWN;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x48, %rsp
testq %rsi, %rsi
je 0xc9a49
leaq 0x54(%rsi), %rax
addq $0xc9, %rsi
jmp 0xc9a63
leaq 0x20(%rsp), %rdx
xorl %esi, %esi
callq 0xc85c8
testl %eax, %eax
js 0xc9ad7
leaq 0xb(%rsp), %rsi
leaq 0xc(%rsp), %rax
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpl $0x1, (%rax)
ja 0xc9ae2
cmpb $0x0, (%rsi)
je 0xc9ad3
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %r15
movaps %xmm0, (%r15)
movl $0x40000000, (%r15)     # imm = 0x40000000
movq %r15, %rdi
callq 0xccae6
movq 0x4(%r15), %rax
leaq 0x10(%rsp), %r14
movq %rax, (%r14)
movl 0xc(%r15), %eax
movl %eax, 0x8(%r14)
movb $0x0, 0xc(%r14)
xorl %r15d, %r15d
leaq 0x49e10(%rip), %r12     # 0x1138c0
leaq (%r15,%r12), %rsi
addq $0x4, %rsi
movq %r14, %rdi
callq 0x57bc0
testl %eax, %eax
je 0xc9ade
addq $0x14, %r15
cmpq $0xb4, %r15
jne 0xc9ab0
jmp 0xc9ae2
xorl %ebx, %ebx
jmp 0xc9ae2
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0xc9ae2
movl (%r15,%r12), %ebx
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq