#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 */
struct trace_seq {
  int dummy;
};
struct pevent_record {
  int dummy;
};
struct event_format {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ find_exit_reason(unsigned long long, unsigned long long);
scalar_t__ pevent_get_field_val(struct trace_seq *, struct event_format *,
                                char *, struct pevent_record *,
                                unsigned long long *, int);
int /*<<< orphan*/ pevent_print_num_field(struct trace_seq *, char *,
                                          struct event_format *, char *,
                                          struct pevent_record *, int);
int /*<<< orphan*/ trace_seq_printf(struct trace_seq *, char *,
                                    unsigned long long, ...);
int kvm_exit_handler(struct trace_seq *s, struct pevent_record *record,
                     struct event_format *event, void *context) {
  unsigned long long isa;
  unsigned long long val;
  unsigned long long info1 = 0, info2 = 0;
  if (pevent_get_field_val(s, event, "exit_reason", record, &val, 1) < 0)
    return -1;
  if (pevent_get_field_val(s, event, "isa", record, &isa, 0) < 0)
    isa = 1;
  trace_seq_printf(s, "reason %s", find_exit_reason(isa, val));
  pevent_print_num_field(s, " rip 0x%lx", event, "guest_rip", record, 1);
  if (pevent_get_field_val(s, event, "info1", record, &info1, 0) >= 0 &&
      pevent_get_field_val(s, event, "info2", record, &info2, 0) >= 0)
    trace_seq_printf(s, " info %llx %llx\n", info1, info2);
  return 0;
}


x86 assembly (clang, -O0):
<kvm_exit_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rcx
lea    0xe85(%rip),%rdx
lea    -0x38(%rbp),%r8
mov    $0x1,%r9d
call   1050 <pevent_get_field_val@plt>
cmp    $0x0,%rax
jge    11a0 <kvm_exit_handler+0x60>
movl   $0xffffffff,-0x4(%rbp)
jmp    1293 <kvm_exit_handler+0x153>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rcx
lea    0xe59(%rip),%rdx
lea    -0x30(%rbp),%r8
xor    %r9d,%r9d
call   1050 <pevent_get_field_val@plt>
cmp    $0x0,%rax
jge    11d1 <kvm_exit_handler+0x91>
movq   $0x1,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1030 <find_exit_reason@plt>
mov    -0x50(%rbp),%rdi
movslq %eax,%rdx
lea    0xe1c(%rip),%rsi
mov    $0x0,%al
call   1040 <trace_seq_printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%r8
lea    0xe0c(%rip),%rsi
lea    0xe10(%rip),%rcx
mov    $0x1,%r9d
call   1060 <pevent_print_num_field@plt>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rcx
lea    0xdfc(%rip),%rdx
lea    -0x40(%rbp),%r8
xor    %r9d,%r9d
call   1050 <pevent_get_field_val@plt>
cmp    $0x0,%rax
jl     128c <kvm_exit_handler+0x14c>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rcx
lea    0xdd9(%rip),%rdx
lea    -0x48(%rbp),%r8
xor    %r9d,%r9d
call   1050 <pevent_get_field_val@plt>
cmp    $0x0,%rax
jl     128c <kvm_exit_handler+0x14c>
mov    -0x10(%rbp),%rdi
mov    -0x40(%rbp),%rdx
mov    -0x48(%rbp),%rcx
lea    0xdb6(%rip),%rsi
mov    $0x0,%al
call   1040 <trace_seq_printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kvm_exit_handler>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
movq   $0x0,0x10(%rsp)
movq   $0x0,0x8(%rsp)
lea    0xe94(%rip),%rdx
lea    0x20(%rsp),%r8
mov    %r15,%rsi
mov    %r14,%rcx
mov    $0x1,%r9d
call   1050 <pevent_get_field_val@plt>
test   %rax,%rax
js     11b0 <kvm_exit_handler+0x70>
lea    0xe7e(%rip),%rdx
lea    0x18(%rsp),%r8
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rcx
xor    %r9d,%r9d
call   1050 <pevent_get_field_val@plt>
test   %rax,%rax
js     11ba <kvm_exit_handler+0x7a>
mov    0x18(%rsp),%rdi
jmp    11c8 <kvm_exit_handler+0x88>
mov    $0xffffffff,%ebp
jmp    126d <kvm_exit_handler+0x12d>
movq   $0x1,0x18(%rsp)
mov    $0x1,%edi
mov    0x20(%rsp),%rsi
call   1030 <find_exit_reason@plt>
movslq %eax,%rdx
lea    0xe34(%rip),%rsi
xor    %ebp,%ebp
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <trace_seq_printf@plt>
lea    0xe2b(%rip),%rsi
lea    0xe2f(%rip),%rcx
mov    %rbx,%rdi
mov    %r15,%rdx
mov    %r14,%r8
mov    $0x1,%r9d
call   1060 <pevent_print_num_field@plt>
lea    0xe1e(%rip),%rdx
lea    0x10(%rsp),%r8
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rcx
xor    %r9d,%r9d
call   1050 <pevent_get_field_val@plt>
test   %rax,%rax
js     126d <kvm_exit_handler+0x12d>
lea    0xe02(%rip),%rdx
lea    0x8(%rsp),%r8
xor    %ebp,%ebp
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rcx
xor    %r9d,%r9d
call   1050 <pevent_get_field_val@plt>
test   %rax,%rax
js     126d <kvm_exit_handler+0x12d>
mov    0x10(%rsp),%rdx
mov    0x8(%rsp),%rcx
lea    0xdda(%rip),%rsi
xor    %ebp,%ebp
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <trace_seq_printf@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.trace_seq = type { i32 }
%struct.pevent_record = type { i32 }
%struct.event_format = type { i32 }
@.str = [12 x i8] c"exit_reason\00"
@.str.1 = [4 x i8] c"isa\00"
@.str.2 = [10 x i8] c"reason %s\00"
@.str.3 = [11 x i8] c" rip 0x%lx\00"
@.str.4 = [10 x i8] c"guest_rip\00"
@.str.5 = [6 x i8] c"info1\00"
@.str.6 = [6 x i8] c"info2\00"
@.str.7 = [17 x i8] c" info %llx %llx\0A\00"
define i32 @kvm_exit_handler(%struct.trace_seq* %0, %struct.pevent_record* %1, %struct.event_format* %2, i8* %3) {
%5 = alloca i32
%6 = alloca %struct.trace_seq*
%7 = alloca %struct.pevent_record*
%8 = alloca %struct.event_format*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
%13 = alloca i64
store %struct.trace_seq* %0, %struct.trace_seq** %6
store %struct.pevent_record* %1, %struct.pevent_record** %7
store %struct.event_format* %2, %struct.event_format** %8
store i8* %3, i8** %9
store i64 0, i64* %12
store i64 0, i64* %13
%14 = load %struct.trace_seq*, %struct.trace_seq** %6
%15 = load %struct.event_format*, %struct.event_format** %8
%16 = load %struct.pevent_record*, %struct.pevent_record** %7
%17 = call i64 @pevent_get_field_val(%struct.trace_seq* %14, %struct.event_format* %15, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), %struct.pevent_record* %16, i64* %11, i32 1)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %4
store i32 -1, i32* %5
br label %55
20: ; preds = %4
%21 = load %struct.trace_seq*, %struct.trace_seq** %6
%22 = load %struct.event_format*, %struct.event_format** %8
%23 = load %struct.pevent_record*, %struct.pevent_record** %7
%24 = call i64 @pevent_get_field_val(%struct.trace_seq* %21, %struct.event_format* %22, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), %struct.pevent_record* %23, i64* %10, i32 0)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %20
store i64 1, i64* %10
br label %27
27: ; preds = %26, %20
%28 = load %struct.trace_seq*, %struct.trace_seq** %6
%29 = load i64, i64* %10
%30 = load i64, i64* %11
%31 = call i32 @find_exit_reason(i64 %29, i64 %30)
%32 = sext i32 %31 to i64
%33 = call i32 (%struct.trace_seq*, i8*, i64, ...) @trace_seq_printf(%struct.trace_seq* %28, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i64 %32)
%34 = load %struct.trace_seq*, %struct.trace_seq** %6
%35 = load %struct.event_format*, %struct.event_format** %8
%36 = load %struct.pevent_record*, %struct.pevent_record** %7
%37 = call i32 @pevent_print_num_field(%struct.trace_seq* %34, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), %struct.event_format* %35, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), %struct.pevent_record* %36, i32 1)
%38 = load %struct.trace_seq*, %struct.trace_seq** %6
%39 = load %struct.event_format*, %struct.event_format** %8
%40 = load %struct.pevent_record*, %struct.pevent_record** %7
%41 = call i64 @pevent_get_field_val(%struct.trace_seq* %38, %struct.event_format* %39, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), %struct.pevent_record* %40, i64* %12, i32 0)
%42 = icmp sge i64 %41, 0
br i1 %42, label %43, label %54
43: ; preds = %27
%44 = load %struct.trace_seq*, %struct.trace_seq** %6
%45 = load %struct.event_format*, %struct.event_format** %8
%46 = load %struct.pevent_record*, %struct.pevent_record** %7
%47 = call i64 @pevent_get_field_val(%struct.trace_seq* %44, %struct.event_format* %45, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), %struct.pevent_record* %46, i64* %13, i32 0)
%48 = icmp sge i64 %47, 0
br i1 %48, label %49, label %54
49: ; preds = %43
%50 = load %struct.trace_seq*, %struct.trace_seq** %6
%51 = load i64, i64* %12
%52 = load i64, i64* %13
%53 = call i32 (%struct.trace_seq*, i8*, i64, ...) @trace_seq_printf(%struct.trace_seq* %50, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0), i64 %51, i64 %52)
br label %54
54: ; preds = %49, %43, %27
store i32 0, i32* %5
br label %55
55: ; preds = %54, %19
%56 = load i32, i32* %5
ret i32 %56
}

LLVM IR (-O2):
%struct.trace_seq = type { i32 }
%struct.pevent_record = type { i32 }
%struct.event_format = type { i32 }
@.str = [12 x i8] c"exit_reason\00"
@.str.1 = [4 x i8] c"isa\00"
@.str.2 = [10 x i8] c"reason %s\00"
@.str.3 = [11 x i8] c" rip 0x%lx\00"
@.str.4 = [10 x i8] c"guest_rip\00"
@.str.5 = [6 x i8] c"info1\00"
@.str.6 = [6 x i8] c"info2\00"
@.str.7 = [17 x i8] c" info %llx %llx\0A\00"
define i32 @kvm_exit_handler(%struct.trace_seq* %0, %struct.pevent_record* %1, %struct.event_format* %2, i8* nocapture readnone %3) {
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = bitcast i64* %5 to i8*
%10 = bitcast i64* %6 to i8*
%11 = bitcast i64* %7 to i8*
store i64 0, i64* %7
%12 = bitcast i64* %8 to i8*
store i64 0, i64* %8
%13 = call i64 @pevent_get_field_val(%struct.trace_seq* %0, %struct.event_format* %2, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), %struct.pevent_record* %1, i64* %6, i32 1)
%14 = icmp slt i64 %13, 0
br i1 %14, label %37, label %15
15: ; preds = %4
%16 = call i64 @pevent_get_field_val(%struct.trace_seq* %0, %struct.event_format* %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), %struct.pevent_record* %1, i64* %5, i32 0)
%17 = icmp slt i64 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %15
%19 = load i64, i64* %5
br label %21
20: ; preds = %15
store i64 1, i64* %5
br label %21
21: ; preds = %18, %20
%22 = phi i64 [ %19, %18 ], [ 1, %20 ]
%23 = load i64, i64* %6
%24 = call i32 @find_exit_reason(i64 %22, i64 %23)
%25 = sext i32 %24 to i64
%26 = call i32 (%struct.trace_seq*, i8*, i64, ...) @trace_seq_printf(%struct.trace_seq* %0, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i64 %25)
%27 = call i32 @pevent_print_num_field(%struct.trace_seq* %0, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), %struct.event_format* %2, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), %struct.pevent_record* %1, i32 1)
%28 = call i64 @pevent_get_field_val(%struct.trace_seq* %0, %struct.event_format* %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), %struct.pevent_record* %1, i64* %7, i32 0)
%29 = icmp sgt i64 %28, -1
br i1 %29, label %30, label %37
30: ; preds = %21
%31 = call i64 @pevent_get_field_val(%struct.trace_seq* %0, %struct.event_format* %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), %struct.pevent_record* %1, i64* %8, i32 0)
%32 = icmp sgt i64 %31, -1
br i1 %32, label %33, label %37
33: ; preds = %30
%34 = load i64, i64* %7
%35 = load i64, i64* %8
%36 = call i32 (%struct.trace_seq*, i8*, i64, ...) @trace_seq_printf(%struct.trace_seq* %0, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0), i64 %34, i64 %35)
br label %37
37: ; preds = %21, %30, %33, %4
%38 = phi i32 [ -1, %4 ], [ 0, %33 ], [ 0, %30 ], [ 0, %21 ]
ret i32 %38
}