#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 stat {
  int /*<<< orphan*/ st_size;
};
struct pevent {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ die(char *, char const *);
int /*<<< orphan*/ free(char *);
int fstat(int, struct stat *);
char *malloc_or_die(int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ parse_proc_kallsyms(struct pevent *, char *,
                                       int /*<<< orphan*/);
int /*<<< orphan*/ read_file_fd(int, char *, int /*<<< orphan*/);
void add_functions(struct pevent *pevent, const char *file) {
  struct stat st;
  char *buf;
  int ret;
  int fd;
  fd = open(file, O_RDONLY);
  if (fd < 0)
    die("Can't read file %s", file);
  ret = fstat(fd, &st);
  if (ret < 0)
    die("Can't stat file %s", file);
  buf = malloc_or_die(st.st_size);
  read_file_fd(fd, buf, st.st_size);
  close(fd);
  parse_proc_kallsyms(pevent, buf, st.st_size);
  free(buf);
}


x86 assembly (clang, -O0):
<add_functions>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    11bf <add_functions+0x3f>
mov    -0x10(%rbp),%rsi
lea    0xe46(%rip),%rdi
call   1080 <die@plt>
mov    -0x28(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   10a0 <fstat@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11e8 <add_functions+0x68>
mov    -0x10(%rbp),%rsi
lea    0xe30(%rip),%rdi
call   1080 <die@plt>
mov    -0x18(%rbp),%edi
call   1050 <malloc_or_die@plt>
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1060 <read_file_fd@plt>
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1090 <parse_proc_kallsyms@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_functions>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
mov    %eax,%r15d
test   %eax,%eax
jns    11b4 <add_functions+0x34>
lea    0xe54(%rip),%rdi
mov    %rbx,%rsi
call   1080 <die@plt>
mov    %rsp,%rsi
mov    %r15d,%edi
call   10a0 <fstat@plt>
test   %eax,%eax
jns    11d2 <add_functions+0x52>
lea    0xe49(%rip),%rdi
mov    %rbx,%rsi
call   1080 <die@plt>
mov    (%rsp),%ebx
mov    %ebx,%edi
call   1050 <malloc_or_die@plt>
mov    %rax,%rbp
mov    %r15d,%edi
mov    %rax,%rsi
mov    %ebx,%edx
call   1060 <read_file_fd@plt>
mov    %r15d,%edi
call   1040 <close@plt>
mov    %r14,%rdi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1090 <parse_proc_kallsyms@plt>
mov    %rbp,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
%struct.pevent = type { i32 }
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [19 x i8] c"Can't read file %s\00"
@.str.1 = [19 x i8] c"Can't stat file %s\00"
define void @add_functions(%struct.pevent* %0, i8* %1) {
%3 = alloca %struct.pevent*
%4 = alloca i8*
%5 = alloca %struct.stat
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store %struct.pevent* %0, %struct.pevent** %3
store i8* %1, i8** %4
%9 = load i8*, i8** %4
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %8
%12 = load i32, i32* %8
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = call i32 @die(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %15)
br label %17
17: ; preds = %14, %2
%18 = load i32, i32* %8
%19 = call i32 @fstat(i32 %18, %struct.stat* %5)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = load i8*, i8** %4
%24 = call i32 @die(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %23)
br label %25
25: ; preds = %22, %17
%26 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%27 = load i32, i32* %26
%28 = call i8* @malloc_or_die(i32 %27)
store i8* %28, i8** %6
%29 = load i32, i32* %8
%30 = load i8*, i8** %6
%31 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 @read_file_fd(i32 %29, i8* %30, i32 %32)
%34 = load i32, i32* %8
%35 = call i32 @close(i32 %34)
%36 = load %struct.pevent*, %struct.pevent** %3
%37 = load i8*, i8** %6
%38 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%39 = load i32, i32* %38
%40 = call i32 @parse_proc_kallsyms(%struct.pevent* %36, i8* %37, i32 %39)
%41 = load i8*, i8** %6
%42 = call i32 @free(i8* %41)
ret void
}

LLVM IR (-O2):
%struct.pevent = type { i32 }
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [19 x i8] c"Can't read file %s\00"
@.str.1 = [19 x i8] c"Can't stat file %s\00"
define void @add_functions(%struct.pevent* %0, i8* %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* %1, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @die(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %10
10: ; preds = %8, %2
%11 = call i32 @fstat(i32 %6, %struct.stat* %3)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 @die(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %15
15: ; preds = %13, %10
%16 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%17 = load i32, i32* %16
%18 = tail call i8* @malloc_or_die(i32 %17)
%19 = tail call i32 @read_file_fd(i32 %6, i8* %18, i32 %17)
%20 = tail call i32 @close(i32 %6)
%21 = tail call i32 @parse_proc_kallsyms(%struct.pevent* %0, i8* %18, i32 %17)
%22 = tail call i32 @free(i8* %18)
ret void
}