#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 *);
int 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");
    return 1;
  } else if (!strcmp("XenVMMXenVMM", signature)) {
    printf("Xen HVM guest.\n");
    return 1;
  } else if (!strcmp("Microsoft Hv", signature)) {
    printf("Microsoft Hv guest.\n");
    return 1;
  } else if (!strcmp("VMwareVMware", signature)) {
    printf("VMWare guest.\n");
    return 1;
  } else {
    printf("No KVM or Xen HVM\n");
    return 0;
  }
  return 0;
}


x86 assembly (clang, -O0):
<check_for_hvm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    $0x40000000,%edi
lea    -0x8(%rbp),%rsi
lea    -0xc(%rbp),%rdx
lea    -0x10(%rbp),%rcx
lea    -0x14(%rbp),%r8
call   1050 <cpuid@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x21(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x1d(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x19(%rbp)
movb   $0x0,-0x15(%rbp)
lea    -0x21(%rbp),%rsi
lea    0xe8d(%rip),%rdi
call   1040 <strcmp@plt>
cmp    $0x0,%eax
jne    1199 <check_for_hvm+0x69>
lea    0xe82(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    123f <check_for_hvm+0x10f>
lea    -0x21(%rbp),%rsi
lea    0xe72(%rip),%rdi
call   1040 <strcmp@plt>
cmp    $0x0,%eax
jne    11ca <check_for_hvm+0x9a>
lea    0xe6a(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    123f <check_for_hvm+0x10f>
lea    -0x21(%rbp),%rsi
lea    0xe5e(%rip),%rdi
call   1040 <strcmp@plt>
cmp    $0x0,%eax
jne    11fb <check_for_hvm+0xcb>
lea    0xe56(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    123f <check_for_hvm+0x10f>
lea    -0x21(%rbp),%rsi
lea    0xe4f(%rip),%rdi
call   1040 <strcmp@plt>
cmp    $0x0,%eax
jne    122c <check_for_hvm+0xfc>
lea    0xe47(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    123f <check_for_hvm+0x10f>
lea    0xe3e(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_for_hvm>:
push   %rbx
sub    $0x20,%rsp
lea    0x1c(%rsp),%rsi
lea    0x18(%rsp),%rdx
lea    0x14(%rsp),%rcx
lea    0x10(%rsp),%r8
mov    $0x40000000,%edi
call   1040 <cpuid@plt>
mov    0x18(%rsp),%eax
mov    %eax,(%rsp)
mov    0x14(%rsp),%eax
mov    %eax,0x4(%rsp)
mov    0x10(%rsp),%eax
mov    %eax,0x8(%rsp)
movb   $0x0,0xc(%rsp)
movabs $0x564b4d564b4d564b,%rcx
4d 4b 56
xor    (%rsp),%rcx
movzwl %ax,%eax
xor    $0x4d,%rax
mov    $0x1,%ebx
or     %rcx,%rax
je     11fb <check_for_hvm+0xdb>
movabs $0x65584d4d566e6558,%rax
4d 58 65
xor    (%rsp),%rax
movabs $0x4d4d566e65584d,%rcx
xor    0x5(%rsp),%rcx
or     %rax,%rcx
je     1204 <check_for_hvm+0xe4>
movabs $0x666f736f7263694d,%rax
73 6f 66
xor    (%rsp),%rax
movabs $0x76482074666f73,%rcx
xor    0x5(%rsp),%rcx
or     %rax,%rcx
je     120d <check_for_hvm+0xed>
movabs $0x4d56657261774d56,%rax
65 56 4d
xor    (%rsp),%rax
movabs $0x657261774d5665,%rcx
xor    0x5(%rsp),%rcx
xor    %ebx,%ebx
or     %rax,%rcx
sete   %bl
lea    0xe71(%rip),%rax
lea    0xe78(%rip),%rdi
cmove  %rax,%rdi
jmp    1214 <check_for_hvm+0xf4>
lea    0xe2f(%rip),%rdi
jmp    1214 <check_for_hvm+0xf4>
lea    0xe31(%rip),%rdi
jmp    1214 <check_for_hvm+0xf4>
lea    0xe37(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
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 i32 @check_for_hvm() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [13 x i8]
%7 = call i32 @cpuid(i32 1073741824, i32* %2, i32* %3, i32* %4, i32* %5)
%8 = load i32, i32* %3
%9 = getelementptr inbounds [13 x i8], [13 x i8]* %6, i64 0, i64 0
%10 = getelementptr inbounds i8, i8* %9, i64 0
%11 = bitcast i8* %10 to i32*
store i32 %8, i32* %11
%12 = load i32, i32* %4
%13 = getelementptr inbounds [13 x i8], [13 x i8]* %6, i64 0, i64 0
%14 = getelementptr inbounds i8, i8* %13, i64 4
%15 = bitcast i8* %14 to i32*
store i32 %12, i32* %15
%16 = load i32, i32* %5
%17 = getelementptr inbounds [13 x i8], [13 x i8]* %6, i64 0, i64 0
%18 = getelementptr inbounds i8, i8* %17, i64 8
%19 = bitcast i8* %18 to i32*
store i32 %16, i32* %19
%20 = getelementptr inbounds [13 x i8], [13 x i8]* %6, i64 0, i64 12
store i8 0, i8* %20
%21 = getelementptr inbounds [13 x i8], [13 x i8]* %6, i64 0, i64 0
%22 = call i32 @strcmp(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %21)
%23 = icmp ne i32 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %0
%25 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %46
26: ; preds = %0
%27 = getelementptr inbounds [13 x i8], [13 x i8]* %6, i64 0, i64 0
%28 = call i32 @strcmp(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %27)
%29 = icmp ne i32 %28, 0
br i1 %29, label %32, label %30
30: ; preds = %26
%31 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %46
32: ; preds = %26
%33 = getelementptr inbounds [13 x i8], [13 x i8]* %6, i64 0, i64 0
%34 = call i32 @strcmp(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* %33)
%35 = icmp ne i32 %34, 0
br i1 %35, label %38, label %36
36: ; preds = %32
%37 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %1
br label %46
38: ; preds = %32
%39 = getelementptr inbounds [13 x i8], [13 x i8]* %6, i64 0, i64 0
%40 = call i32 @strcmp(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i8* %39)
%41 = icmp ne i32 %40, 0
br i1 %41, label %44, label %42
42: ; preds = %38
%43 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0))
store i32 1, i32* %1
br label %46
44: ; preds = %38
%45 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.8, i64 0, i64 0))
store i32 0, i32* %1
br label %46
46: ; preds = %44, %42, %36, %30, %24
%47 = load i32, i32* %1
ret i32 %47
}

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 i32 @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 %34, 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 %34, 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 %34, 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)
%33 = zext i1 %31 to i32
br label %34
34: ; preds = %29, %26, %23, %0
%35 = 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 ]
%36 = phi i32 [ 1, %0 ], [ 1, %23 ], [ 1, %26 ], [ %33, %29 ]
%37 = call i32 @puts(i8* dereferenceable(1) %35)
ret i32 %36
}