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

/* Variables and functions */
int /*<<< orphan*/ __NR_perf_event_open;
char *format_raw_event(struct perf_event_attr *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ resolve_event(char *, struct perf_event_attr *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcmp(char *, char *);
scalar_t__ syscall(int /*<<< orphan*/, struct perf_event_attr *,
                   int /*<<< orphan*/, int, int, int /*<<< orphan*/);
int main(int ac, char **av) {
  int test = 0;
  int ret = 0;
  while (*++av) {
    if (!strcmp(*av, "--test")) {
      test = 1;
      continue;
    }
    struct perf_event_attr attr;
    if (resolve_event(*av, &attr) < 0) {
      fprintf(stderr, "Cannot resolve %s\n", *av);
      ret = 1;
      continue;
    }
    char *ev = format_raw_event(&attr, *av);
    printf("%s\n", ev);
    free(ev);
    if (test) {
      if (syscall(__NR_perf_event_open, &attr, 0, -1, -1, 0) < 0)
        perror("perf_event_open");
    }
  }
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x10(%rbp)
cmpq   $0x0,0x8(%rax)
je     12a0 <main+0x120>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xe34(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    11e6 <main+0x66>
movl   $0x1,-0x14(%rbp)
jmp    11a4 <main+0x24>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    -0x20(%rbp),%rsi
call   1040 <resolve_event@plt>
cmp    $0x0,%rax
jge    1228 <main+0xa8>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdf0(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x18(%rbp)
jmp    11a4 <main+0x24>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    -0x20(%rbp),%rdi
call   1090 <format_raw_event@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
lea    0xdcf(%rip),%rdi
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x0,-0x14(%rbp)
je     129b <main+0x11b>
mov    0x2d72(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
xor    %r9d,%r9d
mov    $0xffffffff,%r8d
mov    %r9d,%edx
mov    %r8d,%ecx
call   1080 <syscall@plt>
cmp    $0x0,%rax
jge    1296 <main+0x116>
lea    0xd89(%rip),%rdi
call   10a0 <perror@plt>
jmp    129b <main+0x11b>
jmp    11a4 <main+0x24>
mov    -0x18(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x8(%rsi),%r12
test   %r12,%r12
je     1291 <main+0x111>
mov    %rsi,%rbx
add    $0x8,%rbx
mov    $0x1,%r13b
movl   $0x0,0xc(%rsp)
lea    0xe4c(%rip),%r14
lea    0x10(%rsp),%r15
add    $0x8,%rbx
jmp    11f1 <main+0x71>
nop
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbx),%rdx
lea    0xe33(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,0xc(%rsp)
mov    (%rbx),%r12
add    $0x8,%rbx
test   %r12,%r12
je     1299 <main+0x119>
mov    %r12,%rdi
mov    %r14,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     1280 <main+0x100>
mov    %r12,%rdi
mov    %r15,%rsi
call   1040 <resolve_event@plt>
test   %rax,%rax
js     11c0 <main+0x40>
mov    -0x8(%rbx),%rsi
mov    %r15,%rdi
call   1090 <format_raw_event@plt>
mov    %rax,%rbp
lea    0xdec(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
test   $0x1,%r13b
jne    11e1 <main+0x61>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
mov    %r15,%rsi
xor    %edx,%edx
mov    $0xffffffff,%ecx
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1080 <syscall@plt>
test   %rax,%rax
jns    11e1 <main+0x61>
lea    0xda9(%rip),%rdi
call   10a0 <perror@plt>
jmp    11e1 <main+0x61>
nopl   0x0(%rax,%rax,1)
mov    (%rbx),%r12
xor    %r13d,%r13d
test   %r12,%r12
jne    11b9 <main+0x39>
jmp    1299 <main+0x119>
movl   $0x0,0xc(%rsp)
mov    0xc(%rsp),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.perf_event_attr = type { i32 }
@.str = [7 x i8] c"--test\00"
@stderr = global i32 0
@.str.1 = [19 x i8] c"Cannot resolve %s\0A\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@__NR_perf_event_open = global i32 0
@.str.3 = [16 x i8] c"perf_event_open\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.perf_event_attr
%9 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32 0, i32* %7
br label %10
10: ; preds = %48, %26, %20, %2
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i32 1
store i8** %12, i8*** %5
%13 = load i8*, i8** %12
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %49
15: ; preds = %10
%16 = load i8**, i8*** %5
%17 = load i8*, i8** %16
%18 = call i32 @strcmp(i8* %17, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%19 = icmp ne i32 %18, 0
br i1 %19, label %21, label %20
20: ; preds = %15
store i32 1, i32* %6
br label %10
21: ; preds = %15
%22 = load i8**, i8*** %5
%23 = load i8*, i8** %22
%24 = call i64 @resolve_event(i8* %23, %struct.perf_event_attr* %8)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %21
%27 = load i32, i32* @stderr
%28 = load i8**, i8*** %5
%29 = load i8*, i8** %28
%30 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %29)
store i32 1, i32* %7
br label %10
31: ; preds = %21
%32 = load i8**, i8*** %5
%33 = load i8*, i8** %32
%34 = call i8* @format_raw_event(%struct.perf_event_attr* %8, i8* %33)
store i8* %34, i8** %9
%35 = load i8*, i8** %9
%36 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %35)
%37 = load i8*, i8** %9
%38 = call i32 @free(i8* %37)
%39 = load i32, i32* %6
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %48
41: ; preds = %31
%42 = load i32, i32* @__NR_perf_event_open
%43 = call i64 @syscall(i32 %42, %struct.perf_event_attr* %8, i32 0, i32 -1, i32 -1, i32 0)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %41
%46 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %47
47: ; preds = %45, %41
br label %48
48: ; preds = %47, %31
br label %10
49: ; preds = %10
%50 = load i32, i32* %7
ret i32 %50
}

LLVM IR (-O2):
%struct.perf_event_attr = type { i32 }
@.str = [7 x i8] c"--test\00"
@stderr = global i32 0
@.str.1 = [19 x i8] c"Cannot resolve %s\0A\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@__NR_perf_event_open = global i32 0
@.str.3 = [16 x i8] c"perf_event_open\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.perf_event_attr
%4 = getelementptr inbounds i8*, i8** %1, i64 1
%5 = load i8*, i8** %4
%6 = icmp eq i8* %5, null
br i1 %6, label %47, label %7
7: ; preds = %2
%8 = bitcast %struct.perf_event_attr* %3 to i8*
br label %13
9: ; preds = %18
%10 = getelementptr inbounds i8*, i8** %20, i64 1
%11 = load i8*, i8** %10
%12 = icmp eq i8* %11, null
br i1 %12, label %47, label %13
13: ; preds = %7, %9
%14 = phi i8* [ %5, %7 ], [ %11, %9 ]
%15 = phi i8** [ %4, %7 ], [ %10, %9 ]
%16 = phi i32 [ 0, %7 ], [ %21, %9 ]
%17 = phi i1 [ true, %7 ], [ false, %9 ]
br label %18
18: ; preds = %13, %42
%19 = phi i8* [ %14, %13 ], [ %45, %42 ]
%20 = phi i8** [ %15, %13 ], [ %44, %42 ]
%21 = phi i32 [ %16, %13 ], [ %43, %42 ]
%22 = call i32 @strcmp(i8* %19, i8* dereferenceable(7) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%23 = icmp eq i32 %22, 0
br i1 %23, label %9, label %24
24: ; preds = %18
%25 = call i64 @resolve_event(i8* %19, %struct.perf_event_attr* %3)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %24
%28 = load i32, i32* @stderr
%29 = load i8*, i8** %20
%30 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %29)
br label %42
31: ; preds = %24
%32 = load i8*, i8** %20
%33 = call i8* @format_raw_event(%struct.perf_event_attr* %3, i8* %32)
%34 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %33)
%35 = call i32 @free(i8* %33)
br i1 %17, label %42, label %36
36: ; preds = %31
%37 = load i32, i32* @__NR_perf_event_open
%38 = call i64 @syscall(i32 %37, %struct.perf_event_attr* %3, i32 0, i32 -1, i32 -1, i32 0)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %42
42: ; preds = %31, %40, %36, %27
%43 = phi i32 [ 1, %27 ], [ %21, %36 ], [ %21, %40 ], [ %21, %31 ]
%44 = getelementptr inbounds i8*, i8** %20, i64 1
%45 = load i8*, i8** %44
%46 = icmp eq i8* %45, null
br i1 %46, label %47, label %18
47: ; preds = %9, %42, %2
%48 = phi i32 [ 0, %2 ], [ %43, %42 ], [ %21, %9 ]
ret i32 %48
}