#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int /*<<< orphan*/ uint32_t;

/* Variables and functions */
int /*<<< orphan*/ cpuid(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                         int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ strcmp(char *, char *);
void check_for_hvm(void) {
  uint32_t eax, ebx, ecx, edx;
  char signature[13];
  cpuid(0x40000000, &eax, &ebx, &ecx, &edx);
  *(uint32_t *)(signature + 0) = ebx;
  *(uint32_t *)(signature + 4) = ecx;
  *(uint32_t *)(signature + 8) = edx;
  signature[12] = '\0';
  if (!strcmp("KVMKVMKVM", signature)) {
    printf("KVM guest.\n");
  } else if (!strcmp("XenVMMXenVMM", signature)) {
    printf("Xen HVM guest.\n");
  } else if (!strcmp("Microsoft Hv", signature)) {
    printf("Microsoft Hv guest.\n");
  } else if (!strcmp("VMwareVMware", signature)) {
    printf("VMWare guest.\n");
  } else {
    printf("No KVM or Xen HVM\n");
  }
}


x86 assembly (clang, -O0):
<check_for_hvm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x40000000,%edi
lea    -0x4(%rbp),%rsi
lea    -0x8(%rbp),%rdx
lea    -0xc(%rbp),%rcx
lea    -0x10(%rbp),%r8
call   1050 <cpuid@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x1d(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x19(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x15(%rbp)
movb   $0x0,-0x11(%rbp)
lea    -0x1d(%rbp),%rsi
lea    0xe8d(%rip),%rdi
call   1040 <strcmp@plt>
cmp    $0x0,%eax
jne    1192 <check_for_hvm+0x62>
lea    0xe82(%rip),%rdi
call   1030 <printf@plt>
jmp    122b <check_for_hvm+0xfb>
lea    -0x1d(%rbp),%rsi
lea    0xe79(%rip),%rdi
call   1040 <strcmp@plt>
cmp    $0x0,%eax
jne    11bc <check_for_hvm+0x8c>
lea    0xe71(%rip),%rdi
call   1030 <printf@plt>
jmp    1226 <check_for_hvm+0xf6>
lea    -0x1d(%rbp),%rsi
lea    0xe6c(%rip),%rdi
call   1040 <strcmp@plt>
cmp    $0x0,%eax
jne    11e6 <check_for_hvm+0xb6>
lea    0xe64(%rip),%rdi
call   1030 <printf@plt>
jmp    1221 <check_for_hvm+0xf1>
lea    -0x1d(%rbp),%rsi
lea    0xe64(%rip),%rdi
call   1040 <strcmp@plt>
cmp    $0x0,%eax
jne    1210 <check_for_hvm+0xe0>
lea    0xe5c(%rip),%rdi
call   1030 <printf@plt>
jmp    121c <check_for_hvm+0xec>
lea    0xe5a(%rip),%rdi
call   1030 <printf@plt>
jmp    1221 <check_for_hvm+0xf1>
jmp    1226 <check_for_hvm+0xf6>
jmp    122b <check_for_hvm+0xfb>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_for_hvm>:
sub    $0x28,%rsp
lea    0x24(%rsp),%rsi
lea    0x20(%rsp),%rdx
lea    0x1c(%rsp),%rcx
lea    0x18(%rsp),%r8
mov    $0x40000000,%edi
call   1040 <cpuid@plt>
mov    0x20(%rsp),%eax
mov    %eax,0x8(%rsp)
mov    0x1c(%rsp),%eax
mov    %eax,0xc(%rsp)
mov    0x18(%rsp),%eax
mov    %eax,0x10(%rsp)
movb   $0x0,0x14(%rsp)
movabs $0x564b4d564b4d564b,%rcx
4d 4b 56
xor    0x8(%rsp),%rcx
movzwl %ax,%eax
xor    $0x4d,%rax
or     %rcx,%rax
je     11f5 <check_for_hvm+0xd5>
movabs $0x65584d4d566e6558,%rax
4d 58 65
xor    0x8(%rsp),%rax
movabs $0x4d4d566e65584d,%rcx
xor    0xd(%rsp),%rcx
or     %rax,%rcx
je     11fe <check_for_hvm+0xde>
movabs $0x666f736f7263694d,%rax
73 6f 66
xor    0x8(%rsp),%rax
movabs $0x76482074666f73,%rcx
xor    0xd(%rsp),%rcx
or     %rax,%rcx
je     1207 <check_for_hvm+0xe7>
movabs $0x4d56657261774d56,%rax
65 56 4d
xor    0x8(%rsp),%rax
movabs $0x657261774d5665,%rcx
xor    0xd(%rsp),%rcx
or     %rax,%rcx
lea    0xe77(%rip),%rax
lea    0xe7e(%rip),%rdi
cmove  %rax,%rdi
jmp    120e <check_for_hvm+0xee>
lea    0xe35(%rip),%rdi
jmp    120e <check_for_hvm+0xee>
lea    0xe37(%rip),%rdi
jmp    120e <check_for_hvm+0xee>
lea    0xe3d(%rip),%rdi
call   1030 <puts@plt>
add    $0x28,%rsp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"KVMKVMKVM\00"
@.str.1 = [12 x i8] c"KVM guest.\0A\00"
@.str.2 = [13 x i8] c"XenVMMXenVMM\00"
@.str.3 = [16 x i8] c"Xen HVM guest.\0A\00"
@.str.4 = [13 x i8] c"Microsoft Hv\00"
@.str.5 = [21 x i8] c"Microsoft Hv guest.\0A\00"
@.str.6 = [13 x i8] c"VMwareVMware\00"
@.str.7 = [15 x i8] c"VMWare guest.\0A\00"
@.str.8 = [19 x i8] c"No KVM or Xen HVM\0A\00"
define void @check_for_hvm() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [13 x i8]
%6 = call i32 @cpuid(i32 1073741824, i32* %1, i32* %2, i32* %3, i32* %4)
%7 = load i32, i32* %2
%8 = getelementptr inbounds [13 x i8], [13 x i8]* %5, i64 0, i64 0
%9 = getelementptr inbounds i8, i8* %8, i64 0
%10 = bitcast i8* %9 to i32*
store i32 %7, i32* %10
%11 = load i32, i32* %3
%12 = getelementptr inbounds [13 x i8], [13 x i8]* %5, i64 0, i64 0
%13 = getelementptr inbounds i8, i8* %12, i64 4
%14 = bitcast i8* %13 to i32*
store i32 %11, i32* %14
%15 = load i32, i32* %4
%16 = getelementptr inbounds [13 x i8], [13 x i8]* %5, i64 0, i64 0
%17 = getelementptr inbounds i8, i8* %16, i64 8
%18 = bitcast i8* %17 to i32*
store i32 %15, i32* %18
%19 = getelementptr inbounds [13 x i8], [13 x i8]* %5, i64 0, i64 12
store i8 0, i8* %19
%20 = getelementptr inbounds [13 x i8], [13 x i8]* %5, i64 0, i64 0
%21 = call i32 @strcmp(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = icmp ne i32 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %0
%24 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %48
25: ; preds = %0
%26 = getelementptr inbounds [13 x i8], [13 x i8]* %5, i64 0, i64 0
%27 = call i32 @strcmp(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %26)
%28 = icmp ne i32 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %25
%30 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %47
31: ; preds = %25
%32 = getelementptr inbounds [13 x i8], [13 x i8]* %5, i64 0, i64 0
%33 = call i32 @strcmp(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* %32)
%34 = icmp ne i32 %33, 0
br i1 %34, label %37, label %35
35: ; preds = %31
%36 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
br label %46
37: ; preds = %31
%38 = getelementptr inbounds [13 x i8], [13 x i8]* %5, i64 0, i64 0
%39 = call i32 @strcmp(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i8* %38)
%40 = icmp ne i32 %39, 0
br i1 %40, label %43, label %41
41: ; preds = %37
%42 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0))
br label %45
43: ; preds = %37
%44 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.8, i64 0, i64 0))
br label %45
45: ; preds = %43, %41
br label %46
46: ; preds = %45, %35
br label %47
47: ; preds = %46, %29
br label %48
48: ; preds = %47, %23
ret void
}

LLVM IR (-O2):
@.str = [10 x i8] c"KVMKVMKVM\00"
@.str.2 = [13 x i8] c"XenVMMXenVMM\00"
@.str.4 = [13 x i8] c"Microsoft Hv\00"
@.str.6 = [13 x i8] c"VMwareVMware\00"
@str = [11 x i8] c"KVM guest.\00"
@str.9 = [15 x i8] c"Xen HVM guest.\00"
@str.10 = [20 x i8] c"Microsoft Hv guest.\00"
@str.11 = [14 x i8] c"VMWare guest.\00"
@str.12 = [18 x i8] c"No KVM or Xen HVM\00"
define void @check_for_hvm() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [13 x i8]
%6 = bitcast i32* %1 to i8*
%7 = bitcast i32* %2 to i8*
%8 = bitcast i32* %3 to i8*
%9 = bitcast i32* %4 to i8*
%10 = getelementptr inbounds [13 x i8], [13 x i8]* %5, i64 0, i64 0
%11 = call i32 @cpuid(i32 1073741824, i32* %1, i32* %2, i32* %3, i32* %4)
%12 = load i32, i32* %2
%13 = bitcast [13 x i8]* %5 to i32*
store i32 %12, i32* %13
%14 = load i32, i32* %3
%15 = getelementptr inbounds [13 x i8], [13 x i8]* %5, i64 0, i64 4
%16 = bitcast i8* %15 to i32*
store i32 %14, i32* %16
%17 = load i32, i32* %4
%18 = getelementptr inbounds [13 x i8], [13 x i8]* %5, i64 0, i64 8
%19 = bitcast i8* %18 to i32*
store i32 %17, i32* %19
%20 = getelementptr inbounds [13 x i8], [13 x i8]* %5, i64 0, i64 12
store i8 0, i8* %20
%21 = call i32 @bcmp(i8* dereferenceable(10) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* dereferenceable(10) %10, i64 10)
%22 = icmp eq i32 %21, 0
br i1 %22, label %33, label %23
23: ; preds = %0
%24 = call i32 @bcmp(i8* dereferenceable(13) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* dereferenceable(13) %10, i64 13)
%25 = icmp eq i32 %24, 0
br i1 %25, label %33, label %26
26: ; preds = %23
%27 = call i32 @bcmp(i8* dereferenceable(13) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* dereferenceable(13) %10, i64 13)
%28 = icmp eq i32 %27, 0
br i1 %28, label %33, label %29
29: ; preds = %26
%30 = call i32 @bcmp(i8* dereferenceable(13) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i8* dereferenceable(13) %10, i64 13)
%31 = icmp eq i32 %30, 0
%32 = select i1 %31, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @str.11, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @str.12, i64 0, i64 0)
br label %33
33: ; preds = %29, %26, %23, %0
%34 = phi i8* [ getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0), %0 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @str.9, i64 0, i64 0), %23 ], [ getelementptr inbounds ([20 x i8], [20 x i8]* @str.10, i64 0, i64 0), %26 ], [ %32, %29 ]
%35 = call i32 @puts(i8* dereferenceable(1) %34)
ret void
}