int main(void) {
  unsigned int eax, ebx, ecx, edx;
  char string[13];
  asm volatile("test %1,%1 ; jz 1f ; ud2a ; .ascii \"xen\" ; 1: cpuid"
               : "=b"(*&ebx), "=a"(*&eax), "=c"(*&ecx), "=d"(*&edx)
               : "0"(0x40000000));
  ;
  *(unsigned int *)(string + 0) = ebx;
  *(unsigned int *)(string + 4) = ecx;
  *(unsigned int *)(string + 8) = edx;
  string[12] = 0;
  if (strncmp(string, "XenVMMXenVMM", 12) == 0) {
    printf("xen hvm\n");
  } else if (strncmp(string, "VMwareVMware", 12) == 0) {
    printf("vmware\n");
  } else if (strncmp(string, "KVMKVMKVM", 12) == 0) {
    printf("kvm\n");
  } else
    printf("bare hardware\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %rbx
sub    $0x28,%rsp
movl   $0x0,-0xc(%rbp)
mov    $0x40000000,%ebx
test   %eax,%eax
je     1142 <main+0x22>
ud2
js     11a6 <main+0x86>
outsb  %ds:(%rsi),(%dx)
cpuid
mov    %ebx,-0x14(%rbp)
mov    %eax,-0x10(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x29(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x25(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x21(%rbp)
movb   $0x0,-0x1d(%rbp)
lea    -0x29(%rbp),%rdi
lea    0xe8f(%rip),%rsi
mov    $0xc,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    1197 <main+0x77>
lea    0xe82(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1211 <main+0xf1>
lea    -0x29(%rbp),%rdi
lea    0xe74(%rip),%rsi
mov    $0xc,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    11c8 <main+0xa8>
lea    0xe67(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    120c <main+0xec>
lea    -0x29(%rbp),%rdi
lea    0xe58(%rip),%rsi
mov    $0xc,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    11f9 <main+0xd9>
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1207 <main+0xe7>
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    120c <main+0xec>
jmp    1211 <main+0xf1>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    $0x40000000,%ebx
test   %eax,%eax
je     1123 <main+0x13>
ud2
js     1187 <main+0x77>
outsb  %ds:(%rsi),(%dx)
cpuid
mov    %ebx,(%rsp)
mov    %ecx,0x4(%rsp)
mov    %edx,0x8(%rsp)
movabs $0x65584d4d566e6558,%rax
4d 58 65
xor    (%rsp),%rax
mov    %edx,%ecx
xor    $0x4d4d566e,%rcx
or     %rax,%rcx
je     1198 <main+0x88>
movabs $0x4d56657261774d56,%rax
65 56 4d
xor    (%rsp),%rax
mov    0x8(%rsp),%ecx
xor    $0x65726177,%rcx
or     %rax,%rcx
je     11a1 <main+0x91>
movabs $0x564b4d564b4d564b,%rax
4d 4b 56
xor    (%rsp),%rax
movzwl 0x8(%rsp),%ecx
xor    $0x4d,%rcx
or     %rax,%rcx
lea    0xea7(%rip),%rax
lea    0xe92(%rip),%rdi
cmove  %rax,%rdi
jmp    11a8 <main+0x98>
lea    0xe9e(%rip),%rdi
jmp    11a8 <main+0x98>
lea    0xe8e(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [13 x i8] c"XenVMMXenVMM\00"
@.str.1 = [9 x i8] c"xen hvm\0A\00"
@.str.2 = [13 x i8] c"VMwareVMware\00"
@.str.3 = [8 x i8] c"vmware\0A\00"
@.str.4 = [10 x i8] c"KVMKVMKVM\00"
@.str.5 = [5 x i8] c"kvm\0A\00"
@.str.6 = [15 x i8] c"bare hardware\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [13 x i8]
store i32 0, i32* %1
%7 = call { i32, i32, i32, i32 } asm sideeffect "test $1,$1 ; jz 1f ; ud2a ; .ascii \22xen\22 ; 1: cpuid", "={bx},={ax},={cx},={dx},0,~{dirflag},~{fpsr},~{flags}"(i32 1073741824)
%8 = extractvalue { i32, i32, i32, i32 } %7, 0
%9 = extractvalue { i32, i32, i32, i32 } %7, 1
%10 = extractvalue { i32, i32, i32, i32 } %7, 2
%11 = extractvalue { i32, i32, i32, i32 } %7, 3
store i32 %8, i32* %3
store i32 %9, i32* %2
store i32 %10, i32* %4
store i32 %11, i32* %5
%12 = load i32, i32* %3
%13 = getelementptr inbounds [13 x i8], [13 x i8]* %6, i64 0, i64 0
%14 = getelementptr inbounds i8, i8* %13, i64 0
%15 = bitcast i8* %14 to i32*
store i32 %12, i32* %15
%16 = load i32, i32* %4
%17 = getelementptr inbounds [13 x i8], [13 x i8]* %6, i64 0, i64 0
%18 = getelementptr inbounds i8, i8* %17, i64 4
%19 = bitcast i8* %18 to i32*
store i32 %16, i32* %19
%20 = load i32, i32* %5
%21 = getelementptr inbounds [13 x i8], [13 x i8]* %6, i64 0, i64 0
%22 = getelementptr inbounds i8, i8* %21, i64 8
%23 = bitcast i8* %22 to i32*
store i32 %20, i32* %23
%24 = getelementptr inbounds [13 x i8], [13 x i8]* %6, i64 0, i64 12
store i8 0, i8* %24
%25 = getelementptr inbounds [13 x i8], [13 x i8]* %6, i64 0, i64 0
%26 = call i32 @strncmp(i8* %25, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i64 12)
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %0
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %46
30: ; preds = %0
%31 = getelementptr inbounds [13 x i8], [13 x i8]* %6, i64 0, i64 0
%32 = call i32 @strncmp(i8* %31, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i64 12)
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %30
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %45
36: ; preds = %30
%37 = getelementptr inbounds [13 x i8], [13 x i8]* %6, i64 0, i64 0
%38 = call i32 @strncmp(i8* %37, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i64 12)
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %44
42: ; preds = %36
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
br label %44
44: ; preds = %42, %40
br label %45
45: ; preds = %44, %34
br label %46
46: ; preds = %45, %28
ret i32 0
}

LLVM IR (-O2):
@.str = [13 x i8] c"XenVMMXenVMM\00"
@.str.2 = [13 x i8] c"VMwareVMware\00"
@.str.4 = [10 x i8] c"KVMKVMKVM\00"
@str = [14 x i8] c"bare hardware\00"
@str.7 = [4 x i8] c"kvm\00"
@str.8 = [7 x i8] c"vmware\00"
@str.9 = [8 x i8] c"xen hvm\00"
define i32 @main() {
%1 = alloca [13 x i8]
%2 = getelementptr inbounds [13 x i8], [13 x i8]* %1, i64 0, i64 0
%3 = tail call { i32, i32, i32, i32 } asm sideeffect "test $1,$1 ; jz 1f ; ud2a ; .ascii \22xen\22 ; 1: cpuid", "={bx},={ax},={cx},={dx},0,~{dirflag},~{fpsr},~{flags}"(i32 1073741824)
%4 = extractvalue { i32, i32, i32, i32 } %3, 0
%5 = extractvalue { i32, i32, i32, i32 } %3, 2
%6 = extractvalue { i32, i32, i32, i32 } %3, 3
%7 = bitcast [13 x i8]* %1 to i32*
store i32 %4, i32* %7
%8 = getelementptr inbounds [13 x i8], [13 x i8]* %1, i64 0, i64 4
%9 = bitcast i8* %8 to i32*
store i32 %5, i32* %9
%10 = getelementptr inbounds [13 x i8], [13 x i8]* %1, i64 0, i64 8
%11 = bitcast i8* %10 to i32*
store i32 %6, i32* %11
%12 = call i32 @bcmp(i8* dereferenceable(12) %2, i8* dereferenceable(12) getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i64 12)
%13 = icmp eq i32 %12, 0
br i1 %13, label %21, label %14
14: ; preds = %0
%15 = call i32 @bcmp(i8* dereferenceable(12) %2, i8* dereferenceable(12) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i64 12)
%16 = icmp eq i32 %15, 0
br i1 %16, label %21, label %17
17: ; preds = %14
%18 = call i32 @bcmp(i8* dereferenceable(10) %2, i8* dereferenceable(10) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i64 10)
%19 = icmp eq i32 %18, 0
%20 = select i1 %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @str.7, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0)
br label %21
21: ; preds = %17, %14, %0
%22 = phi i8* [ getelementptr inbounds ([8 x i8], [8 x i8]* @str.9, i64 0, i64 0), %0 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @str.8, i64 0, i64 0), %14 ], [ %20, %17 ]
%23 = tail call i32 @puts(i8* dereferenceable(1) %22)
ret i32 0
}