#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 type;
  int config;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ eprint(char *, ...);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, int *);
int snprintf(char *, int, char *, char const *, ...);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int perf_event_encode(struct perf_event_attr *attr, const char *pmu,
                      const char *event) {
  FILE *f;
  int eventid;
  int pmutype;
  char buf[PATH_MAX];
  if (snprintf(buf, PATH_MAX, "/sys/bus/event_source/devices/%s/events/%s", pmu,
               event) >= PATH_MAX) {
    eprint("overflow in path name");
    return -1;
  }
  f = fopen(buf, "r");
  if (!f) {
    eprint("Event %s for pmu %s not found (%d:%s)\n", event, pmu, errno,
           strerror(errno));
    return -1;
  }
  if (fscanf(f, "event=0x%x\n", &eventid) != 1) {
    fclose(f);
    eprint("Event file %s has invalid format\n", buf);
    return -1;
  }
  fclose(f);
  if (snprintf(buf, PATH_MAX, "/sys/bus/event_source/devices/%s/type", pmu) >=
      PATH_MAX) {
    eprint("overflow in path name");
    return -1;
  }
  f = fopen(buf, "r");
  if (!f) {
    eprint("Event %s for pmu %s not found (%d:%s)\n", event, pmu, errno,
           strerror(errno));
    return -1;
  }
  if (fscanf(f, "%d\n", &pmutype) != 1) {
    fclose(f);
    eprint("Type file %s has invalid format\n", buf);
    return -1;
  }
  attr->type = pmutype;
  attr->config = eventid;
  return 0;
}


x86 assembly (clang, -O0):
<perf_event_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x50(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x40(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%r8
lea    0xe45(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    0x2e27(%rip),%rcx
cmp    (%rcx),%eax
jl     11f2 <perf_event_encode+0x92>
lea    0xe53(%rip),%rdi
mov    $0x0,%al
call   1080 <eprint@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    13f1 <perf_event_encode+0x291>
mov    -0x50(%rbp),%rdi
lea    0xe44(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    126a <perf_event_encode+0x10a>
mov    -0x20(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    0x2db0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x68(%rbp),%rsi
mov    -0x60(%rbp),%rdx
mov    -0x54(%rbp),%ecx
mov    %eax,%r8d
lea    0xdf3(%rip),%rdi
mov    $0x0,%al
call   1080 <eprint@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    13f1 <perf_event_encode+0x291>
mov    -0x28(%rbp),%rdi
lea    0xdf5(%rip),%rsi
lea    -0x2c(%rbp),%rdx
call   1050 <fscanf@plt>
cmp    $0x1,%eax
je     12b5 <perf_event_encode+0x155>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x50(%rbp),%rsi
lea    0xddb(%rip),%rdi
mov    $0x0,%al
call   1080 <eprint@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    13f1 <perf_event_encode+0x291>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x50(%rbp),%rdi
mov    0x2d27(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rcx
lea    0xdc2(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    0x2d0c(%rip),%rcx
cmp    (%rcx),%eax
jl     130d <perf_event_encode+0x1ad>
lea    0xd38(%rip),%rdi
mov    $0x0,%al
call   1080 <eprint@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    13f1 <perf_event_encode+0x291>
mov    -0x50(%rbp),%rdi
lea    0xd29(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1385 <perf_event_encode+0x225>
mov    -0x20(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    0x2c95(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x6c(%rbp)
mov    0x2c89(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x80(%rbp),%rsi
mov    -0x78(%rbp),%rdx
mov    -0x6c(%rbp),%ecx
mov    %eax,%r8d
lea    0xcd8(%rip),%rdi
mov    $0x0,%al
call   1080 <eprint@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    13f1 <perf_event_encode+0x291>
mov    -0x28(%rbp),%rdi
lea    0xd2e(%rip),%rsi
lea    -0x30(%rbp),%rdx
call   1050 <fscanf@plt>
cmp    $0x1,%eax
je     13d0 <perf_event_encode+0x270>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x50(%rbp),%rsi
lea    0xd0c(%rip),%rdi
mov    $0x0,%al
call   1080 <eprint@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    13f1 <perf_event_encode+0x291>
mov    -0x30(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x2c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<perf_event_encode>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%r13
mov    %rsi,%r15
mov    %rdi,%r12
mov    %rsp,-0x48(%rbp)
mov    0x2e6b(%rip),%r14
mov    (%r14),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    (%r14),%esi
lea    0xe5d(%rip),%rdx
mov    %rbx,%rdi
mov    %r15,%rcx
mov    %r13,%r8
xor    %eax,%eax
call   1040 <snprintf@plt>
cmp    (%r14),%eax
jge    1265 <perf_event_encode+0x105>
mov    %r12,-0x40(%rbp)
mov    %r13,-0x38(%rbp)
lea    0xe76(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     1275 <perf_event_encode+0x115>
mov    %rax,%r13
lea    0xe84(%rip),%rsi
lea    -0x30(%rbp),%rdx
mov    %rax,%rdi
call   1050 <fscanf@plt>
mov    %eax,%r12d
mov    %r13,%rdi
call   1030 <fclose@plt>
cmp    $0x1,%r12d
jne    12a1 <perf_event_encode+0x141>
mov    (%r14),%esi
lea    0xe87(%rip),%rdx
mov    %rbx,%rdi
mov    %r15,%rcx
xor    %eax,%eax
call   1040 <snprintf@plt>
cmp    (%r14),%eax
jge    1265 <perf_event_encode+0x105>
lea    0xe17(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     1275 <perf_event_encode+0x115>
mov    %rax,%r13
lea    0xe7d(%rip),%rsi
lea    -0x2c(%rbp),%rdx
mov    %rax,%rdi
call   1050 <fscanf@plt>
cmp    $0x1,%eax
jne    12ca <perf_event_encode+0x16a>
mov    -0x2c(%rbp),%eax
mov    -0x40(%rbp),%rcx
mov    %eax,(%rcx)
mov    -0x30(%rbp),%eax
mov    %eax,0x4(%rcx)
xor    %eax,%eax
jmp    12b7 <perf_event_encode+0x157>
lea    0xdbf(%rip),%rdi
xor    %eax,%eax
call   1080 <eprint@plt>
jmp    12b2 <perf_event_encode+0x152>
mov    0x2d5c(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1070 <strerror@plt>
lea    0xdb7(%rip),%rdi
mov    -0x38(%rbp),%rsi
mov    %r15,%rdx
mov    %ebx,%ecx
mov    %eax,%r8d
xor    %eax,%eax
call   1080 <eprint@plt>
jmp    12b2 <perf_event_encode+0x152>
lea    0xdce(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1080 <eprint@plt>
mov    $0xffffffff,%eax
mov    -0x48(%rbp),%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r13,%rdi
call   1030 <fclose@plt>
lea    0xde9(%rip),%rdi
jmp    12a8 <perf_event_encode+0x148>

LLVM IR (-O0):
%struct.perf_event_attr = type { i32, i32 }
@PATH_MAX = global i32 0
@.str = [43 x i8] c"/sys/bus/event_source/devices/%s/events/%s\00"
@.str.1 = [22 x i8] c"overflow in path name\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [39 x i8] c"Event %s for pmu %s not found (%d:%s)\0A\00"
@errno = global i32 0
@.str.4 = [12 x i8] c"event=0x%x\0A\00"
@.str.5 = [34 x i8] c"Event file %s has invalid format\0A\00"
@.str.6 = [38 x i8] c"/sys/bus/event_source/devices/%s/type\00"
@.str.7 = [4 x i8] c"%d\0A\00"
@.str.8 = [33 x i8] c"Type file %s has invalid format\0A\00"
define i32 @perf_event_encode(%struct.perf_event_attr* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca %struct.perf_event_attr*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32
store %struct.perf_event_attr* %0, %struct.perf_event_attr** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%14 = load i32, i32* @PATH_MAX
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %11
%17 = alloca i8, i64 %15
store i64 %15, i64* %12
%18 = load i32, i32* @PATH_MAX
%19 = load i8*, i8** %6
%20 = load i8*, i8** %7
%21 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %17, i32 %18, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i8* %19, i8* %20)
%22 = load i32, i32* @PATH_MAX
%23 = icmp sge i32 %21, %22
br i1 %23, label %24, label %26
24: ; preds = %3
%25 = call i32 (i8*, ...) @eprint(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
store i32 1, i32* %13
br label %81
26: ; preds = %3
%27 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %27, i32** %8
%28 = load i32*, i32** %8
%29 = icmp ne i32* %28, null
br i1 %29, label %37, label %30
30: ; preds = %26
%31 = load i8*, i8** %7
%32 = load i8*, i8** %6
%33 = load i32, i32* @errno
%34 = load i32, i32* @errno
%35 = call i32 @strerror(i32 %34)
%36 = call i32 (i8*, ...) @eprint(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i8* %31, i8* %32, i32 %33, i32 %35)
store i32 -1, i32* %4
store i32 1, i32* %13
br label %81
37: ; preds = %26
%38 = load i32*, i32** %8
%39 = call i32 @fscanf(i32* %38, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32* %9)
%40 = icmp ne i32 %39, 1
br i1 %40, label %41, label %45
41: ; preds = %37
%42 = load i32*, i32** %8
%43 = call i32 @fclose(i32* %42)
%44 = call i32 (i8*, ...) @eprint(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i8* %17)
store i32 -1, i32* %4
store i32 1, i32* %13
br label %81
45: ; preds = %37
%46 = load i32*, i32** %8
%47 = call i32 @fclose(i32* %46)
%48 = load i32, i32* @PATH_MAX
%49 = load i8*, i8** %6
%50 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %17, i32 %48, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.6, i64 0, i64 0), i8* %49)
%51 = load i32, i32* @PATH_MAX
%52 = icmp sge i32 %50, %51
br i1 %52, label %53, label %55
53: ; preds = %45
%54 = call i32 (i8*, ...) @eprint(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
store i32 1, i32* %13
br label %81
55: ; preds = %45
%56 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %56, i32** %8
%57 = load i32*, i32** %8
%58 = icmp ne i32* %57, null
br i1 %58, label %66, label %59
59: ; preds = %55
%60 = load i8*, i8** %7
%61 = load i8*, i8** %6
%62 = load i32, i32* @errno
%63 = load i32, i32* @errno
%64 = call i32 @strerror(i32 %63)
%65 = call i32 (i8*, ...) @eprint(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i8* %60, i8* %61, i32 %62, i32 %64)
store i32 -1, i32* %4
store i32 1, i32* %13
br label %81
66: ; preds = %55
%67 = load i32*, i32** %8
%68 = call i32 @fscanf(i32* %67, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i32* %10)
%69 = icmp ne i32 %68, 1
br i1 %69, label %70, label %74
70: ; preds = %66
%71 = load i32*, i32** %8
%72 = call i32 @fclose(i32* %71)
%73 = call i32 (i8*, ...) @eprint(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.8, i64 0, i64 0), i8* %17)
store i32 -1, i32* %4
store i32 1, i32* %13
br label %81
74: ; preds = %66
%75 = load i32, i32* %10
%76 = load %struct.perf_event_attr*, %struct.perf_event_attr** %5
%77 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %76, i32 0, i32 0
store i32 %75, i32* %77
%78 = load i32, i32* %9
%79 = load %struct.perf_event_attr*, %struct.perf_event_attr** %5
%80 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %79, i32 0, i32 1
store i32 %78, i32* %80
store i32 0, i32* %4
store i32 1, i32* %13
br label %81
81: ; preds = %74, %70, %59, %53, %41, %30, %24
%82 = load i8*, i8** %11
%83 = load i32, i32* %4
ret i32 %83
}

LLVM IR (-O2):
%struct.perf_event_attr = type { i32, i32 }
@PATH_MAX = global i32 0
@.str = [43 x i8] c"/sys/bus/event_source/devices/%s/events/%s\00"
@.str.1 = [22 x i8] c"overflow in path name\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [39 x i8] c"Event %s for pmu %s not found (%d:%s)\0A\00"
@errno = global i32 0
@.str.4 = [12 x i8] c"event=0x%x\0A\00"
@.str.5 = [34 x i8] c"Event file %s has invalid format\0A\00"
@.str.6 = [38 x i8] c"/sys/bus/event_source/devices/%s/type\00"
@.str.7 = [4 x i8] c"%d\0A\00"
@.str.8 = [33 x i8] c"Type file %s has invalid format\0A\00"
define i32 @perf_event_encode(%struct.perf_event_attr* nocapture writeonly %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i32* %4 to i8*
%7 = bitcast i32* %5 to i8*
%8 = load i32, i32* @PATH_MAX
%9 = zext i32 %8 to i64
%10 = tail call i8* @llvm.stacksave()
%11 = alloca i8, i64 %9
%12 = load i32, i32* @PATH_MAX
%13 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %11, i32 %12, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %2)
%14 = load i32, i32* @PATH_MAX
%15 = icmp slt i32 %13, %14
br i1 %15, label %18, label %16
16: ; preds = %3
%17 = call i32 (i8*, ...) @eprint(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %56
18: ; preds = %3
%19 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %25
21: ; preds = %18
%22 = load i32, i32* @errno
%23 = call i32 @strerror(i32 %22)
%24 = call i32 (i8*, ...) @eprint(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i8* %2, i8* %1, i32 %22, i32 %23)
br label %56
25: ; preds = %18
%26 = call i32 @fscanf(i32* %19, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32* %4)
%27 = icmp eq i32 %26, 1
%28 = call i32 @fclose(i32* %19)
br i1 %27, label %31, label %29
29: ; preds = %25
%30 = call i32 (i8*, ...) @eprint(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i8* %11)
br label %56
31: ; preds = %25
%32 = load i32, i32* @PATH_MAX
%33 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %11, i32 %32, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.6, i64 0, i64 0), i8* %1)
%34 = load i32, i32* @PATH_MAX
%35 = icmp slt i32 %33, %34
br i1 %35, label %38, label %36
36: ; preds = %31
%37 = call i32 (i8*, ...) @eprint(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %56
38: ; preds = %31
%39 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%40 = icmp eq i32* %39, null
br i1 %40, label %41, label %45
41: ; preds = %38
%42 = load i32, i32* @errno
%43 = call i32 @strerror(i32 %42)
%44 = call i32 (i8*, ...) @eprint(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i8* %2, i8* %1, i32 %42, i32 %43)
br label %56
45: ; preds = %38
%46 = call i32 @fscanf(i32* %39, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i32* %5)
%47 = icmp eq i32 %46, 1
br i1 %47, label %51, label %48
48: ; preds = %45
%49 = call i32 @fclose(i32* %39)
%50 = call i32 (i8*, ...) @eprint(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.8, i64 0, i64 0), i8* %11)
br label %56
51: ; preds = %45
%52 = load i32, i32* %5
%53 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %0, i64 0, i32 0
store i32 %52, i32* %53
%54 = load i32, i32* %4
%55 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %0, i64 0, i32 1
store i32 %54, i32* %55
br label %56
56: ; preds = %51, %48, %41, %36, %29, %21, %16
%57 = phi i32 [ -1, %16 ], [ -1, %29 ], [ -1, %36 ], [ -1, %48 ], [ 0, %51 ], [ -1, %41 ], [ -1, %21 ]
ret i32 %57
}