#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 uint32_t;

/* Variables and functions */
int /*<<< orphan*/ cpuid(int, int *, int);
int /*<<< orphan*/ strcmp(char *, char *);
int check_for_xen(int pv_context) {
  uint32_t regs[4];
  char signature[13];
  uint32_t base;
  for (base = 0x40000000; base < 0x40010000; base += 0x100) {
    cpuid(base, regs, pv_context);
    *(uint32_t *)(signature + 0) = regs[1];
    *(uint32_t *)(signature + 4) = regs[2];
    *(uint32_t *)(signature + 8) = regs[3];
    signature[12] = '\0';
    if (!strcmp("XenVMMXenVMM", signature) && (regs[0] >= (base + 2)))
      goto found;
  }
  return 0;
found:
  cpuid(base + 1, regs, pv_context);
  return regs[0];
}


x86 assembly (clang, -O0):
<check_for_xen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x40000000,-0x34(%rbp)
cmpl   $0x40010000,-0x34(%rbp)
jge    11a8 <check_for_xen+0x88>
mov    -0x34(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0x8(%rbp),%edx
call   1040 <cpuid@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x2d(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x29(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x25(%rbp)
movb   $0x0,-0x21(%rbp)
lea    -0x2d(%rbp),%rsi
lea    0xe91(%rip),%rdi
call   1030 <strcmp@plt>
cmp    $0x0,%eax
jne    1193 <check_for_xen+0x73>
mov    -0x20(%rbp),%eax
mov    -0x34(%rbp),%ecx
add    $0x2,%ecx
cmp    %ecx,%eax
jl     1193 <check_for_xen+0x73>
jmp    11b4 <check_for_xen+0x94>
jmp    1198 <check_for_xen+0x78>
mov    -0x34(%rbp),%eax
add    $0x100,%eax
mov    %eax,-0x34(%rbp)
jmp    1132 <check_for_xen+0x12>
movl   $0x0,-0x4(%rbp)
jmp    11cc <check_for_xen+0xac>
mov    -0x34(%rbp),%edi
add    $0x1,%edi
lea    -0x20(%rbp),%rsi
mov    -0x8(%rbp),%edx
call   1040 <cpuid@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_for_xen>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edi,%r15d
mov    $0x40000002,%ebx
lea    0x10(%rsp),%r14
movabs $0x65584d4d566e6558,%r12
4d 58 65
movabs $0x4d4d566e65584d,%r13
jmp    115e <check_for_xen+0x4e>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x100,%ebx
cmp    $0x4000ff00,%ebp
jae    11b6 <check_for_xen+0xa6>
lea    -0x2(%rbx),%ebp
mov    %ebp,%edi
mov    %r14,%rsi
mov    %r15d,%edx
call   1030 <cpuid@plt>
mov    0x14(%rsp),%eax
mov    %eax,(%rsp)
mov    0x18(%rsp),%rax
mov    %rax,0x4(%rsp)
movb   $0x0,0xc(%rsp)
mov    (%rsp),%rax
xor    %r12,%rax
mov    0x5(%rsp),%rcx
xor    %r13,%rcx
or     %rax,%rcx
jne    1150 <check_for_xen+0x40>
cmp    %ebx,0x10(%rsp)
jl     1150 <check_for_xen+0x40>
add    $0xffffffff,%ebx
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    %r15d,%edx
call   1030 <cpuid@plt>
mov    0x10(%rsp),%eax
jmp    11b8 <check_for_xen+0xa8>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"XenVMMXenVMM\00"
define i32 @check_for_xen(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [4 x i32]
%5 = alloca [13 x i8]
%6 = alloca i32
store i32 %0, i32* %3
store i32 1073741824, i32* %6
br label %7
7: ; preds = %42, %1
%8 = load i32, i32* %6
%9 = icmp slt i32 %8, 1073807360
br i1 %9, label %10, label %45
10: ; preds = %7
%11 = load i32, i32* %6
%12 = getelementptr inbounds [4 x i32], [4 x i32]* %4, i64 0, i64 0
%13 = load i32, i32* %3
%14 = call i32 @cpuid(i32 %11, i32* %12, i32 %13)
%15 = getelementptr inbounds [4 x i32], [4 x i32]* %4, i64 0, i64 1
%16 = load i32, i32* %15
%17 = getelementptr inbounds [13 x i8], [13 x i8]* %5, i64 0, i64 0
%18 = getelementptr inbounds i8, i8* %17, i64 0
%19 = bitcast i8* %18 to i32*
store i32 %16, i32* %19
%20 = getelementptr inbounds [4 x i32], [4 x i32]* %4, i64 0, i64 2
%21 = load i32, i32* %20
%22 = getelementptr inbounds [13 x i8], [13 x i8]* %5, i64 0, i64 0
%23 = getelementptr inbounds i8, i8* %22, i64 4
%24 = bitcast i8* %23 to i32*
store i32 %21, i32* %24
%25 = getelementptr inbounds [4 x i32], [4 x i32]* %4, i64 0, i64 3
%26 = load i32, i32* %25
%27 = getelementptr inbounds [13 x i8], [13 x i8]* %5, i64 0, i64 0
%28 = getelementptr inbounds i8, i8* %27, i64 8
%29 = bitcast i8* %28 to i32*
store i32 %26, i32* %29
%30 = getelementptr inbounds [13 x i8], [13 x i8]* %5, i64 0, i64 12
store i8 0, i8* %30
%31 = getelementptr inbounds [13 x i8], [13 x i8]* %5, i64 0, i64 0
%32 = call i32 @strcmp(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %31)
%33 = icmp ne i32 %32, 0
br i1 %33, label %41, label %34
34: ; preds = %10
%35 = getelementptr inbounds [4 x i32], [4 x i32]* %4, i64 0, i64 0
%36 = load i32, i32* %35
%37 = load i32, i32* %6
%38 = add nsw i32 %37, 2
%39 = icmp sge i32 %36, %38
br i1 %39, label %40, label %41
40: ; preds = %34
br label %46
41: ; preds = %34, %10
br label %42
42: ; preds = %41
%43 = load i32, i32* %6
%44 = add nsw i32 %43, 256
store i32 %44, i32* %6
br label %7
45: ; preds = %7
store i32 0, i32* %2
br label %54
46: ; preds = %40
%47 = load i32, i32* %6
%48 = add nsw i32 %47, 1
%49 = getelementptr inbounds [4 x i32], [4 x i32]* %4, i64 0, i64 0
%50 = load i32, i32* %3
%51 = call i32 @cpuid(i32 %48, i32* %49, i32 %50)
%52 = getelementptr inbounds [4 x i32], [4 x i32]* %4, i64 0, i64 0
%53 = load i32, i32* %52
store i32 %53, i32* %2
br label %54
54: ; preds = %46, %45
%55 = load i32, i32* %2
ret i32 %55
}

LLVM IR (-O2):
@.str = [13 x i8] c"XenVMMXenVMM\00"
define i32 @check_for_xen(i32 %0) {
%2 = alloca [4 x i32]
%3 = alloca [13 x i8]
%4 = bitcast [4 x i32]* %2 to i8*
%5 = getelementptr inbounds [13 x i8], [13 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 0
%7 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 1
%8 = bitcast [13 x i8]* %3 to i32*
%9 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 2
%10 = getelementptr inbounds [13 x i8], [13 x i8]* %3, i64 0, i64 4
%11 = bitcast i8* %10 to i32*
%12 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 3
%13 = getelementptr inbounds [13 x i8], [13 x i8]* %3, i64 0, i64 8
%14 = bitcast i8* %13 to i32*
%15 = getelementptr inbounds [13 x i8], [13 x i8]* %3, i64 0, i64 12
br label %16
16: ; preds = %1, %28
%17 = phi i32 [ 1073741824, %1 ], [ %29, %28 ]
%18 = call i32 @cpuid(i32 %17, i32* %6, i32 %0)
%19 = load i32, i32* %7
store i32 %19, i32* %8
%20 = load i32, i32* %9
store i32 %20, i32* %11
%21 = load i32, i32* %12
store i32 %21, i32* %14
store i8 0, i8* %15
%22 = call i32 @bcmp(i8* dereferenceable(13) getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* dereferenceable(13) %5, i64 13)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %16
%25 = load i32, i32* %6
%26 = or i32 %17, 2
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %31
28: ; preds = %16, %24
%29 = add nuw nsw i32 %17, 256
%30 = icmp ult i32 %17, 1073807104
br i1 %30, label %16, label %35
31: ; preds = %24
%32 = or i32 %17, 1
%33 = call i32 @cpuid(i32 %32, i32* %6, i32 %0)
%34 = load i32, i32* %6
br label %35
35: ; preds = %28, %31
%36 = phi i32 [ %34, %31 ], [ 0, %28 ]
ret i32 %36
}