#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 */

/* Variables and functions */
int /*<<< orphan*/ KVMIO;
int O_CLOEXEC;
int O_RDWR;
int /*<<< orphan*/ _IO(int /*<<< orphan*/, int);
int /*<<< orphan*/ exit(int);
int ioctl(int, int /*<<< orphan*/, int /*<<< orphan*/ *);
int open(char *, int);
int /*<<< orphan*/ printf(char *);
int main() {
  int kvm, ret;
  kvm = open("/dev/kvm", O_RDWR | O_CLOEXEC);
  if (kvm == -1) {
    printf("ERROR: KVM is not loaded!\n");
    exit(1);
  }
  ret = ioctl(kvm, _IO(KVMIO, 0xe4), NULL);
  if (ret == -1) {
    printf("ERROR: KVM-PT is not loaded!\n");
    exit(2);
  }
  if (ret == -2) {
    printf("ERROR: Intel PT is not supported on this CPU!\n");
    exit(3);
  }
  printf("KVM-PT is ready!\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
mov    0x2e59(%rip),%rax
or     (%rax),%esi
lea    0xe88(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11a0 <main+0x50>
lea    0xe78(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
mov    $0xe4,%esi
call   1040 <_IO@plt>
mov    -0x10(%rbp),%edi
mov    %eax,%esi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <ioctl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11ea <main+0x9a>
lea    0xe49(%rip),%rdi
call   1030 <printf@plt>
mov    $0x2,%edi
call   1070 <exit@plt>
cmpl   $0xfffffffe,-0xc(%rbp)
jne    120a <main+0xba>
lea    0xe47(%rip),%rdi
call   1030 <printf@plt>
mov    $0x3,%edi
call   1070 <exit@plt>
lea    0xe60(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e90(%rip),%rax
mov    0x2e69(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe96(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11ae <main+0x5e>
mov    %eax,%ebx
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
mov    $0xe4,%esi
call   1040 <_IO@plt>
mov    %ebx,%edi
mov    %eax,%esi
xor    %edx,%edx
call   1050 <ioctl@plt>
cmp    $0xfffffffe,%eax
je     11c4 <main+0x74>
cmp    $0xffffffff,%eax
je     11da <main+0x8a>
lea    0xe64(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xeb0(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xe4f(%rip),%rdi
call   1030 <puts@plt>
mov    $0x3,%edi
call   1070 <exit@plt>
lea    0xe67(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/kvm\00"
@O_RDWR = global i32 0
@O_CLOEXEC = global i32 0
@.str.1 = [27 x i8] c"ERROR: KVM is not loaded!\0A\00"
@KVMIO = global i32 0
@.str.2 = [30 x i8] c"ERROR: KVM-PT is not loaded!\0A\00"
@.str.3 = [47 x i8] c"ERROR: Intel PT is not supported on this CPU!\0A\00"
@.str.4 = [18 x i8] c"KVM-PT is ready!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_CLOEXEC
%6 = or i32 %4, %5
%7 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = load i32, i32* %2
%15 = load i32, i32* @KVMIO
%16 = call i32 @_IO(i32 %15, i32 228)
%17 = call i32 @ioctl(i32 %14, i32 %16, i32* null)
store i32 %17, i32* %3
%18 = load i32, i32* %3
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %13
%21 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 @exit(i32 2)
unreachable
23: ; preds = %13
%24 = load i32, i32* %3
%25 = icmp eq i32 %24, -2
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0))
%28 = call i32 @exit(i32 3)
unreachable
29: ; preds = %23
%30 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/kvm\00"
@O_RDWR = global i32 0
@O_CLOEXEC = global i32 0
@KVMIO = global i32 0
@str = [17 x i8] c"KVM-PT is ready!\00"
@str.5 = [46 x i8] c"ERROR: Intel PT is not supported on this CPU!\00"
@str.6 = [29 x i8] c"ERROR: KVM-PT is not loaded!\00"
@str.7 = [26 x i8] c"ERROR: KVM is not loaded!\00"
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = load i32, i32* @O_CLOEXEC
%3 = or i32 %2, %1
%4 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.7, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = load i32, i32* @KVMIO
%11 = tail call i32 @_IO(i32 %10, i32 228)
%12 = tail call i32 @ioctl(i32 %4, i32 %11, i32* null)
switch i32 %12, label %19 [
i32 -1, label %13
i32 -2, label %16
]
13: ; preds = %9
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.6, i64 0, i64 0))
%15 = tail call i32 @exit(i32 2)
unreachable
16: ; preds = %9
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str.5, i64 0, i64 0))
%18 = tail call i32 @exit(i32 3)
unreachable
19: ; preds = %9
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
ret i32 0
}