#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 intel_pt_pkt {
  int dummy;
};

/* Variables and functions */
int intel_pt_get_3byte(unsigned char const *, size_t, struct intel_pt_pkt *);
int intel_pt_get_cbr(unsigned char const *, size_t, struct intel_pt_pkt *);
int intel_pt_get_long_tnt(unsigned char const *, size_t, struct intel_pt_pkt *);
int intel_pt_get_ovf(struct intel_pt_pkt *);
int intel_pt_get_pip(unsigned char const *, size_t, struct intel_pt_pkt *);
int intel_pt_get_psb(unsigned char const *, size_t, struct intel_pt_pkt *);
int intel_pt_get_psbend(struct intel_pt_pkt *);
int intel_pt_get_tma(unsigned char const *, size_t, struct intel_pt_pkt *);
int intel_pt_get_tracestop(struct intel_pt_pkt *);
int intel_pt_get_vmcs(unsigned char const *, size_t, struct intel_pt_pkt *);
int intel_pt_get_ext(const unsigned char *buf, size_t len,
                     struct intel_pt_pkt *packet) {
  if (len < 2) {
    return -1;
  }
  switch (buf[1]) {
  case 0xa3:
    return intel_pt_get_long_tnt(buf, len, packet);
  case 0x43:
    return intel_pt_get_pip(buf, len, packet);
  case 0x83:
    return intel_pt_get_tracestop(packet);
  case 0x03:
    return intel_pt_get_cbr(buf, len, packet);
  case 0xc8:
    return intel_pt_get_vmcs(buf, len, packet);
  case 0xf3:
    return intel_pt_get_ovf(packet);
  case 0x82:
    return intel_pt_get_psb(buf, len, packet);
  case 0x23:
    return intel_pt_get_psbend(packet);
  case 0x73:
    return intel_pt_get_tma(buf, len, packet);
  case 0xC3:
    return intel_pt_get_3byte(buf, len, packet);
  default:
    return -2;
  }
}


x86 assembly (clang, -O0):
<intel_pt_get_ext>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x2,-0x18(%rbp)
jae    11cb <intel_pt_get_ext+0x2b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1372 <intel_pt_get_ext+0x1d2>
mov    -0x10(%rbp),%rax
movzbl 0x1(%rax),%eax
mov    %eax,-0x24(%rbp)
sub    $0x3,%eax
je     12cc <intel_pt_get_ext+0x12c>
jmp    11e4 <intel_pt_get_ext+0x44>
mov    -0x24(%rbp),%eax
sub    $0x23,%eax
je     1328 <intel_pt_get_ext+0x188>
jmp    11f5 <intel_pt_get_ext+0x55>
mov    -0x24(%rbp),%eax
sub    $0x43,%eax
je     12a2 <intel_pt_get_ext+0x102>
jmp    1206 <intel_pt_get_ext+0x66>
mov    -0x24(%rbp),%eax
sub    $0x73,%eax
je     1339 <intel_pt_get_ext+0x199>
jmp    1217 <intel_pt_get_ext+0x77>
mov    -0x24(%rbp),%eax
sub    $0x82,%eax
je     130f <intel_pt_get_ext+0x16f>
jmp    122a <intel_pt_get_ext+0x8a>
mov    -0x24(%rbp),%eax
sub    $0x83,%eax
je     12bb <intel_pt_get_ext+0x11b>
jmp    123d <intel_pt_get_ext+0x9d>
mov    -0x24(%rbp),%eax
sub    $0xa3,%eax
je     1289 <intel_pt_get_ext+0xe9>
jmp    1250 <intel_pt_get_ext+0xb0>
mov    -0x24(%rbp),%eax
sub    $0xc3,%eax
je     1352 <intel_pt_get_ext+0x1b2>
jmp    1263 <intel_pt_get_ext+0xc3>
mov    -0x24(%rbp),%eax
sub    $0xc8,%eax
je     12e5 <intel_pt_get_ext+0x145>
jmp    1276 <intel_pt_get_ext+0xd6>
mov    -0x24(%rbp),%eax
sub    $0xf3,%eax
je     12fe <intel_pt_get_ext+0x15e>
jmp    136b <intel_pt_get_ext+0x1cb>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   10c0 <intel_pt_get_long_tnt@plt>
mov    %eax,-0x4(%rbp)
jmp    1372 <intel_pt_get_ext+0x1d2>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <intel_pt_get_pip@plt>
mov    %eax,-0x4(%rbp)
jmp    1372 <intel_pt_get_ext+0x1d2>
mov    -0x20(%rbp),%rdi
call   10a0 <intel_pt_get_tracestop@plt>
mov    %eax,-0x4(%rbp)
jmp    1372 <intel_pt_get_ext+0x1d2>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1060 <intel_pt_get_cbr@plt>
mov    %eax,-0x4(%rbp)
jmp    1372 <intel_pt_get_ext+0x1d2>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1080 <intel_pt_get_vmcs@plt>
mov    %eax,-0x4(%rbp)
jmp    1372 <intel_pt_get_ext+0x1d2>
mov    -0x20(%rbp),%rdi
call   1040 <intel_pt_get_ovf@plt>
mov    %eax,-0x4(%rbp)
jmp    1372 <intel_pt_get_ext+0x1d2>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1090 <intel_pt_get_psb@plt>
mov    %eax,-0x4(%rbp)
jmp    1372 <intel_pt_get_ext+0x1d2>
mov    -0x20(%rbp),%rdi
call   10b0 <intel_pt_get_psbend@plt>
mov    %eax,-0x4(%rbp)
jmp    1372 <intel_pt_get_ext+0x1d2>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <intel_pt_get_tma@plt>
mov    %eax,-0x4(%rbp)
jmp    1372 <intel_pt_get_ext+0x1d2>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1070 <intel_pt_get_3byte@plt>
mov    %eax,-0x4(%rbp)
jmp    1372 <intel_pt_get_ext+0x1d2>
movl   $0xfffffffe,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<intel_pt_get_ext>:
mov    $0xffffffff,%eax
cmp    $0x2,%rsi
jae    11ac <intel_pt_get_ext+0xc>
ret
mov    0x1(%rdi),%cl
mov    $0xfffffffe,%eax
cmp    $0xf2,%cl
jg     11d2 <intel_pt_get_ext+0x32>
cmp    $0xa2,%cl
jle    11eb <intel_pt_get_ext+0x4b>
cmp    $0xa3,%cl
je     120c <intel_pt_get_ext+0x6c>
cmp    $0xc3,%cl
je     1211 <intel_pt_get_ext+0x71>
cmp    $0xc8,%cl
jne    11ab <intel_pt_get_ext+0xb>
jmp    1080 <intel_pt_get_vmcs@plt>
cmp    $0x22,%cl
jle    11fd <intel_pt_get_ext+0x5d>
cmp    $0x23,%cl
je     1216 <intel_pt_get_ext+0x76>
cmp    $0x43,%cl
je     121e <intel_pt_get_ext+0x7e>
cmp    $0x73,%cl
jne    11ab <intel_pt_get_ext+0xb>
jmp    1030 <intel_pt_get_tma@plt>
cmp    $0x82,%cl
je     1223 <intel_pt_get_ext+0x83>
cmp    $0x83,%cl
jne    11ab <intel_pt_get_ext+0xb>
mov    %rdx,%rdi
jmp    10a0 <intel_pt_get_tracestop@plt>
cmp    $0xf3,%cl
je     1228 <intel_pt_get_ext+0x88>
cmp    $0x3,%cl
jne    11ab <intel_pt_get_ext+0xb>
jmp    1060 <intel_pt_get_cbr@plt>
jmp    10c0 <intel_pt_get_long_tnt@plt>
jmp    1070 <intel_pt_get_3byte@plt>
mov    %rdx,%rdi
jmp    10b0 <intel_pt_get_psbend@plt>
jmp    1050 <intel_pt_get_pip@plt>
jmp    1090 <intel_pt_get_psb@plt>
mov    %rdx,%rdi
jmp    1040 <intel_pt_get_ovf@plt>

LLVM IR (-O0):
%struct.intel_pt_pkt = type { i32 }
define i32 @intel_pt_get_ext(i8* %0, i64 %1, %struct.intel_pt_pkt* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca %struct.intel_pt_pkt*
store i8* %0, i8** %5
store i64 %1, i64* %6
store %struct.intel_pt_pkt* %2, %struct.intel_pt_pkt** %7
%8 = load i64, i64* %6
%9 = icmp ult i64 %8, 2
br i1 %9, label %10, label %11
10: ; preds = %3
store i32 -1, i32* %4
br label %61
11: ; preds = %3
%12 = load i8*, i8** %5
%13 = getelementptr inbounds i8, i8* %12, i64 1
%14 = load i8, i8* %13
%15 = zext i8 %14 to i32
switch i32 %15, label %60 [
i32 163, label %16
i32 67, label %21
i32 131, label %26
i32 3, label %29
i32 200, label %34
i32 243, label %39
i32 130, label %42
i32 35, label %47
i32 115, label %50
i32 195, label %55
]
16: ; preds = %11
%17 = load i8*, i8** %5
%18 = load i64, i64* %6
%19 = load %struct.intel_pt_pkt*, %struct.intel_pt_pkt** %7
%20 = call i32 @intel_pt_get_long_tnt(i8* %17, i64 %18, %struct.intel_pt_pkt* %19)
store i32 %20, i32* %4
br label %61
21: ; preds = %11
%22 = load i8*, i8** %5
%23 = load i64, i64* %6
%24 = load %struct.intel_pt_pkt*, %struct.intel_pt_pkt** %7
%25 = call i32 @intel_pt_get_pip(i8* %22, i64 %23, %struct.intel_pt_pkt* %24)
store i32 %25, i32* %4
br label %61
26: ; preds = %11
%27 = load %struct.intel_pt_pkt*, %struct.intel_pt_pkt** %7
%28 = call i32 @intel_pt_get_tracestop(%struct.intel_pt_pkt* %27)
store i32 %28, i32* %4
br label %61
29: ; preds = %11
%30 = load i8*, i8** %5
%31 = load i64, i64* %6
%32 = load %struct.intel_pt_pkt*, %struct.intel_pt_pkt** %7
%33 = call i32 @intel_pt_get_cbr(i8* %30, i64 %31, %struct.intel_pt_pkt* %32)
store i32 %33, i32* %4
br label %61
34: ; preds = %11
%35 = load i8*, i8** %5
%36 = load i64, i64* %6
%37 = load %struct.intel_pt_pkt*, %struct.intel_pt_pkt** %7
%38 = call i32 @intel_pt_get_vmcs(i8* %35, i64 %36, %struct.intel_pt_pkt* %37)
store i32 %38, i32* %4
br label %61
39: ; preds = %11
%40 = load %struct.intel_pt_pkt*, %struct.intel_pt_pkt** %7
%41 = call i32 @intel_pt_get_ovf(%struct.intel_pt_pkt* %40)
store i32 %41, i32* %4
br label %61
42: ; preds = %11
%43 = load i8*, i8** %5
%44 = load i64, i64* %6
%45 = load %struct.intel_pt_pkt*, %struct.intel_pt_pkt** %7
%46 = call i32 @intel_pt_get_psb(i8* %43, i64 %44, %struct.intel_pt_pkt* %45)
store i32 %46, i32* %4
br label %61
47: ; preds = %11
%48 = load %struct.intel_pt_pkt*, %struct.intel_pt_pkt** %7
%49 = call i32 @intel_pt_get_psbend(%struct.intel_pt_pkt* %48)
store i32 %49, i32* %4
br label %61
50: ; preds = %11
%51 = load i8*, i8** %5
%52 = load i64, i64* %6
%53 = load %struct.intel_pt_pkt*, %struct.intel_pt_pkt** %7
%54 = call i32 @intel_pt_get_tma(i8* %51, i64 %52, %struct.intel_pt_pkt* %53)
store i32 %54, i32* %4
br label %61
55: ; preds = %11
%56 = load i8*, i8** %5
%57 = load i64, i64* %6
%58 = load %struct.intel_pt_pkt*, %struct.intel_pt_pkt** %7
%59 = call i32 @intel_pt_get_3byte(i8* %56, i64 %57, %struct.intel_pt_pkt* %58)
store i32 %59, i32* %4
br label %61
60: ; preds = %11
store i32 -2, i32* %4
br label %61
61: ; preds = %60, %55, %50, %47, %42, %39, %34, %29, %26, %21, %16, %10
%62 = load i32, i32* %4
ret i32 %62
}

LLVM IR (-O2):
%struct.intel_pt_pkt = type { i32 }
define i32 @intel_pt_get_ext(i8* %0, i64 %1, %struct.intel_pt_pkt* %2) {
%4 = icmp ult i64 %1, 2
br i1 %4, label %28, label %5
5: ; preds = %3
%6 = getelementptr inbounds i8, i8* %0, i64 1
%7 = load i8, i8* %6
switch i8 %7, label %28 [
i8 -93, label %8
i8 67, label %10
i8 -125, label %12
i8 3, label %14
i8 -56, label %16
i8 -13, label %18
i8 -126, label %20
i8 35, label %22
i8 115, label %24
i8 -61, label %26
]
8: ; preds = %5
%9 = tail call i32 @intel_pt_get_long_tnt(i8* %0, i64 %1, %struct.intel_pt_pkt* %2)
br label %28
10: ; preds = %5
%11 = tail call i32 @intel_pt_get_pip(i8* %0, i64 %1, %struct.intel_pt_pkt* %2)
br label %28
12: ; preds = %5
%13 = tail call i32 @intel_pt_get_tracestop(%struct.intel_pt_pkt* %2)
br label %28
14: ; preds = %5
%15 = tail call i32 @intel_pt_get_cbr(i8* %0, i64 %1, %struct.intel_pt_pkt* %2)
br label %28
16: ; preds = %5
%17 = tail call i32 @intel_pt_get_vmcs(i8* %0, i64 %1, %struct.intel_pt_pkt* %2)
br label %28
18: ; preds = %5
%19 = tail call i32 @intel_pt_get_ovf(%struct.intel_pt_pkt* %2)
br label %28
20: ; preds = %5
%21 = tail call i32 @intel_pt_get_psb(i8* %0, i64 %1, %struct.intel_pt_pkt* %2)
br label %28
22: ; preds = %5
%23 = tail call i32 @intel_pt_get_psbend(%struct.intel_pt_pkt* %2)
br label %28
24: ; preds = %5
%25 = tail call i32 @intel_pt_get_tma(i8* %0, i64 %1, %struct.intel_pt_pkt* %2)
br label %28
26: ; preds = %5
%27 = tail call i32 @intel_pt_get_3byte(i8* %0, i64 %1, %struct.intel_pt_pkt* %2)
br label %28
28: ; preds = %5, %3, %26, %24, %22, %20, %18, %16, %14, %12, %10, %8
%29 = phi i32 [ %27, %26 ], [ %25, %24 ], [ %23, %22 ], [ %21, %20 ], [ %19, %18 ], [ %17, %16 ], [ %15, %14 ], [ %13, %12 ], [ %11, %10 ], [ %9, %8 ], [ -1, %3 ], [ -2, %5 ]
ret i32 %29
}