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

/* Variables and functions */
char *append_file(char const *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ pevent_parse_header_page(struct pevent *, char *, int, int);
int read_file(char *, char **);
int stat(char *, struct stat *);
int read_header(struct pevent *pevent, const char *events_dir) {
  struct stat st;
  char *header;
  char *buf;
  int len;
  int ret = -1;
  header = append_file(events_dir, "header_page");
  ret = stat(header, &st);
  if (ret < 0)
    goto out;
  len = read_file(header, &buf);
  if (len < 0)
    goto out;
  pevent_parse_header_page(pevent, buf, len, sizeof(long));
  free(buf);
  ret = 0;
out:
  free(header);
  return ret;
}


x86 assembly (clang, -O0):
<read_header>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe8e(%rip),%rsi
call   1040 <append_file@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <stat@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    119a <read_header+0x4a>
jmp    11de <read_header+0x8e>
mov    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1060 <read_file@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11b9 <read_header+0x69>
jmp    11de <read_header+0x8e>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x2c(%rbp),%edx
mov    $0x8,%ecx
call   1070 <pevent_parse_header_page@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_header>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rax
mov    %rdi,%r14
lea    0xe9b(%rip),%rsi
mov    %rax,%rdi
call   1040 <append_file@plt>
mov    %rax,%rbx
lea    0x8(%rsp),%rsi
mov    %rax,%rdi
call   1050 <stat@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11b0 <read_header+0x60>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1060 <read_file@plt>
test   %eax,%eax
js     11b0 <read_header+0x60>
mov    (%rsp),%rsi
mov    %r14,%rdi
mov    %eax,%edx
mov    $0x8,%ecx
call   1070 <pevent_parse_header_page@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
xor    %ebp,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.pevent = type { i32 }
%struct.stat = type { i32 }
@.str = [12 x i8] c"header_page\00"
define i32 @read_header(%struct.pevent* %0, i8* %1) {
%3 = alloca %struct.pevent*
%4 = alloca i8*
%5 = alloca %struct.stat
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store %struct.pevent* %0, %struct.pevent** %3
store i8* %1, i8** %4
store i32 -1, i32* %9
%10 = load i8*, i8** %4
%11 = call i8* @append_file(i8* %10, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i8* %11, i8** %6
%12 = load i8*, i8** %6
%13 = call i32 @stat(i8* %12, %struct.stat* %5)
store i32 %13, i32* %9
%14 = load i32, i32* %9
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %2
br label %30
17: ; preds = %2
%18 = load i8*, i8** %6
%19 = call i32 @read_file(i8* %18, i8** %7)
store i32 %19, i32* %8
%20 = load i32, i32* %8
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %17
br label %30
23: ; preds = %17
%24 = load %struct.pevent*, %struct.pevent** %3
%25 = load i8*, i8** %7
%26 = load i32, i32* %8
%27 = call i32 @pevent_parse_header_page(%struct.pevent* %24, i8* %25, i32 %26, i32 8)
%28 = load i8*, i8** %7
%29 = call i32 @free(i8* %28)
store i32 0, i32* %9
br label %30
30: ; preds = %23, %22, %16
%31 = load i8*, i8** %6
%32 = call i32 @free(i8* %31)
%33 = load i32, i32* %9
ret i32 %33
}

LLVM IR (-O2):
%struct.pevent = type { i32 }
%struct.stat = type { i32 }
@.str = [12 x i8] c"header_page\00"
define i32 @read_header(%struct.pevent* %0, i8* %1) {
%3 = alloca %struct.stat
%4 = alloca i8*
%5 = bitcast %struct.stat* %3 to i8*
%6 = bitcast i8** %4 to i8*
%7 = tail call i8* @append_file(i8* %1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @stat(i8* %7, %struct.stat* %3)
%9 = icmp slt i32 %8, 0
br i1 %9, label %18, label %10
10: ; preds = %2
%11 = call i32 @read_file(i8* %7, i8** %4)
%12 = icmp slt i32 %11, 0
br i1 %12, label %18, label %13
13: ; preds = %10
%14 = load i8*, i8** %4
%15 = call i32 @pevent_parse_header_page(%struct.pevent* %0, i8* %14, i32 %11, i32 8)
%16 = load i8*, i8** %4
%17 = call i32 @free(i8* %16)
br label %18
18: ; preds = %10, %2, %13
%19 = phi i32 [ %8, %2 ], [ %8, %10 ], [ 0, %13 ]
%20 = call i32 @free(i8* %7)
ret i32 %19
}