#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 */
char *format_raw_event(struct perf_event_attr *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(char *);
scalar_t__ perf_event_open(struct perf_event_attr *, int /*<<< orphan*/, int,
                           int, int /*<<< orphan*/);
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 *);
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 (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     1295 <main+0x115>
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   1080 <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     1290 <main+0x110>
lea    -0x20(%rbp),%rdi
xor    %r8d,%r8d
mov    $0xffffffff,%ecx
mov    %r8d,%esi
mov    %ecx,%edx
call   10a0 <perf_event_open@plt>
cmp    $0x0,%rax
jge    128b <main+0x10b>
lea    0xd94(%rip),%rdi
call   1090 <perror@plt>
jmp    1290 <main+0x110>
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     1281 <main+0x101>
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     1289 <main+0x109>
mov    %r12,%rdi
mov    %r14,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     1270 <main+0xf0>
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   1080 <format_raw_event@plt>
mov    %rax,%rbp
lea    0xdf0(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
test   $0x1,%r13b
jne    11e1 <main+0x61>
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xffffffff,%edx
mov    $0xffffffff,%ecx
xor    %r8d,%r8d
call   10a0 <perf_event_open@plt>
test   %rax,%rax
jns    11e1 <main+0x61>
lea    0xdbb(%rip),%rdi
call   1090 <perror@plt>
jmp    11e1 <main+0x61>
nopl   0x0(%rax)
mov    (%rbx),%r12
xor    %r13d,%r13d
test   %r12,%r12
jne    11b9 <main+0x39>
jmp    1289 <main+0x109>
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"
@.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 = %47, %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 %48
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 %47
41: ; preds = %31
%42 = call i64 @perf_event_open(%struct.perf_event_attr* %8, i32 0, i32 -1, i32 -1, i32 0)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %41
%45 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %46
46: ; preds = %44, %41
br label %47
47: ; preds = %46, %31
br label %10
48: ; preds = %10
%49 = load i32, i32* %7
ret i32 %49
}

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"
@.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 %46, 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 %46, 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, %41
%19 = phi i8* [ %14, %13 ], [ %44, %41 ]
%20 = phi i8** [ %15, %13 ], [ %43, %41 ]
%21 = phi i32 [ %16, %13 ], [ %42, %41 ]
%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 %41
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 %41, label %36
36: ; preds = %31
%37 = call i64 @perf_event_open(%struct.perf_event_attr* %3, i32 0, i32 -1, i32 -1, i32 0)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %41
41: ; preds = %31, %39, %36, %27
%42 = phi i32 [ 1, %27 ], [ %21, %36 ], [ %21, %39 ], [ %21, %31 ]
%43 = getelementptr inbounds i8*, i8** %20, i64 1
%44 = load i8*, i8** %43
%45 = icmp eq i8* %44, null
br i1 %45, label %46, label %18
46: ; preds = %9, %41, %2
%47 = phi i32 [ 0, %2 ], [ %42, %41 ], [ %21, %9 ]
ret i32 %47
}