#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_mode;
};
struct pevent {
  int dummy;
};
struct dirent {
  char *d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int EINVAL;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
char *append_file(char const *, char const *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int errno;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ *opendir(char const *);
int pevent_parse_event(struct pevent *, char *, int, char const *);
int read_file(char *, char **);
struct dirent *readdir(int /*<<< orphan*/ *);
int stat(char const *, struct stat *);
scalar_t__ strcmp(char const *, char *);
int load_events(struct pevent *pevent, const char *system,
                const char *sys_dir) {
  struct dirent *dent;
  struct stat st;
  DIR *dir;
  int len = 0;
  int ret = 0, failure = 0;
  ret = stat(sys_dir, &st);
  if (ret < 0 || !S_ISDIR(st.st_mode))
    return EINVAL;
  dir = opendir(sys_dir);
  if (!dir)
    return errno;
  while ((dent = readdir(dir))) {
    const char *name = dent->d_name;
    char *event;
    char *format;
    char *buf;
    if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0)
      continue;
    event = append_file(sys_dir, name);
    ret = stat(event, &st);
    if (ret < 0 || !S_ISDIR(st.st_mode))
      goto free_event;
    format = append_file(event, "format");
    ret = stat(format, &st);
    if (ret < 0)
      goto free_format;
    len = read_file(format, &buf);
    if (len < 0)
      goto free_format;
    ret = pevent_parse_event(pevent, buf, len, system);
    free(buf);
  free_format:
    free(format);
  free_event:
    free(event);
    if (ret)
      failure = ret;
  }
  closedir(dir);
  return failure;
}


x86 assembly (clang, -O0):
<load_events>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x3c(%rbp)
movl   $0x0,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   10a0 <stat@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
jl     11f4 <load_events+0x54>
mov    -0x30(%rbp),%edi
call   1070 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    1205 <load_events+0x65>
mov    0x2ddd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1377 <load_events+0x1d7>
mov    -0x20(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    122e <load_events+0x8e>
mov    0x2dbc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1377 <load_events+0x1d7>
jmp    1233 <load_events+0x93>
mov    -0x38(%rbp),%rdi
call   10c0 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1368 <load_events+0x1c8>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rdi
lea    0xda1(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     1289 <load_events+0xe9>
mov    -0x50(%rbp),%rdi
lea    0xd86(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    128e <load_events+0xee>
jmp    1233 <load_events+0x93>
mov    -0x20(%rbp),%rdi
mov    -0x50(%rbp),%rsi
call   1060 <append_file@plt>
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   10a0 <stat@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
jl     12ca <load_events+0x12a>
mov    -0x30(%rbp),%edi
call   1070 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    12cf <load_events+0x12f>
jmp    134a <load_events+0x1aa>
mov    -0x58(%rbp),%rdi
lea    0xd29(%rip),%rsi
call   1060 <append_file@plt>
mov    %rax,-0x60(%rbp)
mov    -0x60(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   10a0 <stat@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
jge    1302 <load_events+0x162>
jmp    1341 <load_events+0x1a1>
mov    -0x60(%rbp),%rdi
lea    -0x68(%rbp),%rsi
call   10b0 <read_file@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    1321 <load_events+0x181>
jmp    1341 <load_events+0x1a1>
mov    -0x10(%rbp),%rdi
mov    -0x68(%rbp),%rsi
mov    -0x3c(%rbp),%edx
mov    -0x18(%rbp),%rcx
call   1040 <pevent_parse_event@plt>
mov    %eax,-0x40(%rbp)
mov    -0x68(%rbp),%rdi
call   1030 <free@plt>
mov    -0x60(%rbp),%rdi
call   1030 <free@plt>
mov    -0x58(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x0,-0x40(%rbp)
je     1363 <load_events+0x1c3>
mov    -0x40(%rbp),%eax
mov    %eax,-0x44(%rbp)
jmp    1233 <load_events+0x93>
mov    -0x38(%rbp),%rdi
call   1080 <closedir@plt>
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_events>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%r13
mov    %rsi,%r14
mov    %rdi,%rbp
mov    %rsp,%rsi
mov    %rdx,%rdi
call   10a0 <stat@plt>
test   %eax,%eax
js     1305 <load_events+0x165>
mov    (%rsp),%edi
call   1070 <S_ISDIR@plt>
test   %eax,%eax
je     1305 <load_events+0x165>
mov    %rbp,0x10(%rsp)
mov    %r14,0x18(%rsp)
mov    %r13,%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     1318 <load_events+0x178>
mov    %rax,%r14
mov    %rax,%rdi
call   10c0 <readdir@plt>
xor    %r12d,%r12d
test   %rax,%rax
je     130e <load_events+0x16e>
lea    0xdee(%rip),%rbx
mov    %r14,0x20(%rsp)
jmp    124a <load_events+0xaa>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %rbp,%r13
mov    %r15,%rdi
call   1030 <free@plt>
test   %r14d,%r14d
cmovne %r14d,%r12d
mov    0x20(%rsp),%r14
mov    %r14,%rdi
call   10c0 <readdir@plt>
test   %rax,%rax
je     130e <load_events+0x16e>
mov    (%rax),%rbp
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1239 <load_events+0x99>
mov    %rbp,%rdi
lea    0xd99(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1239 <load_events+0x99>
mov    %r13,%rdi
mov    %rbp,%rsi
call   1060 <append_file@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rsp,%rsi
call   10a0 <stat@plt>
mov    %eax,%r14d
test   %eax,%eax
js     1225 <load_events+0x85>
mov    (%rsp),%edi
call   1070 <S_ISDIR@plt>
test   %eax,%eax
je     1225 <load_events+0x85>
mov    %r13,%rbp
mov    %r15,%rdi
lea    0xd59(%rip),%rsi
call   1060 <append_file@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    %rsp,%rsi
call   10a0 <stat@plt>
mov    %eax,%r14d
test   %eax,%eax
js     121a <load_events+0x7a>
mov    %r13,%rdi
lea    0x8(%rsp),%rsi
call   10b0 <read_file@plt>
test   %eax,%eax
js     121a <load_events+0x7a>
mov    0x8(%rsp),%rsi
mov    0x10(%rsp),%rdi
mov    %eax,%edx
mov    0x18(%rsp),%rcx
call   1040 <pevent_parse_event@plt>
mov    %eax,%r14d
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
jmp    121a <load_events+0x7a>
mov    0x2ccc(%rip),%rax
jmp    131f <load_events+0x17f>
mov    %r14,%rdi
call   1080 <closedir@plt>
jmp    1322 <load_events+0x182>
mov    0x2cc1(%rip),%rax
mov    (%rax),%r12d
mov    %r12d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pevent = type { i32 }
%struct.dirent = type { i8* }
%struct.stat = type { i32 }
@EINVAL = global i32 0
@errno = global i32 0
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [7 x i8] c"format\00"
define i32 @load_events(%struct.pevent* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca %struct.pevent*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca %struct.dirent*
%9 = alloca %struct.stat
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i8*
%15 = alloca i8*
%16 = alloca i8*
%17 = alloca i8*
store %struct.pevent* %0, %struct.pevent** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 0, i32* %11
store i32 0, i32* %12
store i32 0, i32* %13
%18 = load i8*, i8** %7
%19 = call i32 @stat(i8* %18, %struct.stat* %9)
store i32 %19, i32* %12
%20 = load i32, i32* %12
%21 = icmp slt i32 %20, 0
br i1 %21, label %27, label %22
22: ; preds = %3
%23 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%24 = load i32, i32* %23
%25 = call i32 @S_ISDIR(i32 %24)
%26 = icmp ne i32 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %22, %3
%28 = load i32, i32* @EINVAL
store i32 %28, i32* %4
br label %104
29: ; preds = %22
%30 = load i8*, i8** %7
%31 = call i32* @opendir(i8* %30)
store i32* %31, i32** %10
%32 = load i32*, i32** %10
%33 = icmp ne i32* %32, null
br i1 %33, label %36, label %34
34: ; preds = %29
%35 = load i32, i32* @errno
store i32 %35, i32* %4
br label %104
36: ; preds = %29
br label %37
37: ; preds = %99, %52, %36
%38 = load i32*, i32** %10
%39 = call %struct.dirent* @readdir(i32* %38)
store %struct.dirent* %39, %struct.dirent** %8
%40 = icmp ne %struct.dirent* %39, null
br i1 %40, label %41, label %100
41: ; preds = %37
%42 = load %struct.dirent*, %struct.dirent** %8
%43 = getelementptr inbounds %struct.dirent, %struct.dirent* %42, i32 0, i32 0
%44 = load i8*, i8** %43
store i8* %44, i8** %14
%45 = load i8*, i8** %14
%46 = call i64 @strcmp(i8* %45, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%47 = icmp eq i64 %46, 0
br i1 %47, label %52, label %48
48: ; preds = %41
%49 = load i8*, i8** %14
%50 = call i64 @strcmp(i8* %49, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%51 = icmp eq i64 %50, 0
br i1 %51, label %52, label %53
52: ; preds = %48, %41
br label %37
53: ; preds = %48
%54 = load i8*, i8** %7
%55 = load i8*, i8** %14
%56 = call i8* @append_file(i8* %54, i8* %55)
store i8* %56, i8** %15
%57 = load i8*, i8** %15
%58 = call i32 @stat(i8* %57, %struct.stat* %9)
store i32 %58, i32* %12
%59 = load i32, i32* %12
%60 = icmp slt i32 %59, 0
br i1 %60, label %66, label %61
61: ; preds = %53
%62 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%63 = load i32, i32* %62
%64 = call i32 @S_ISDIR(i32 %63)
%65 = icmp ne i32 %64, 0
br i1 %65, label %67, label %66
66: ; preds = %61, %53
br label %92
67: ; preds = %61
%68 = load i8*, i8** %15
%69 = call i8* @append_file(i8* %68, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i8* %69, i8** %16
%70 = load i8*, i8** %16
%71 = call i32 @stat(i8* %70, %struct.stat* %9)
store i32 %71, i32* %12
%72 = load i32, i32* %12
%73 = icmp slt i32 %72, 0
br i1 %73, label %74, label %75
74: ; preds = %67
br label %89
75: ; preds = %67
%76 = load i8*, i8** %16
%77 = call i32 @read_file(i8* %76, i8** %17)
store i32 %77, i32* %11
%78 = load i32, i32* %11
%79 = icmp slt i32 %78, 0
br i1 %79, label %80, label %81
80: ; preds = %75
br label %89
81: ; preds = %75
%82 = load %struct.pevent*, %struct.pevent** %5
%83 = load i8*, i8** %17
%84 = load i32, i32* %11
%85 = load i8*, i8** %6
%86 = call i32 @pevent_parse_event(%struct.pevent* %82, i8* %83, i32 %84, i8* %85)
store i32 %86, i32* %12
%87 = load i8*, i8** %17
%88 = call i32 @free(i8* %87)
br label %89
89: ; preds = %81, %80, %74
%90 = load i8*, i8** %16
%91 = call i32 @free(i8* %90)
br label %92
92: ; preds = %89, %66
%93 = load i8*, i8** %15
%94 = call i32 @free(i8* %93)
%95 = load i32, i32* %12
%96 = icmp ne i32 %95, 0
br i1 %96, label %97, label %99
97: ; preds = %92
%98 = load i32, i32* %12
store i32 %98, i32* %13
br label %99
99: ; preds = %97, %92
br label %37
100: ; preds = %37
%101 = load i32*, i32** %10
%102 = call i32 @closedir(i32* %101)
%103 = load i32, i32* %13
store i32 %103, i32* %4
br label %104
104: ; preds = %100, %34, %27
%105 = load i32, i32* %4
ret i32 %105
}

LLVM IR (-O2):
%struct.pevent = type { i32 }
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@EINVAL = global i32 0
@errno = global i32 0
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [7 x i8] c"format\00"
define i32 @load_events(%struct.pevent* %0, i8* %1, i8* %2) {
%4 = alloca %struct.stat
%5 = alloca i8*
%6 = bitcast %struct.stat* %4 to i8*
%7 = call i32 @stat(i8* %2, %struct.stat* %4)
%8 = icmp slt i32 %7, 0
br i1 %8, label %14, label %9
9: ; preds = %3
%10 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%11 = load i32, i32* %10
%12 = tail call i32 @S_ISDIR(i32 %11)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %9, %3
%15 = load i32, i32* @EINVAL
br label %71
16: ; preds = %9
%17 = tail call i32* @opendir(i8* %2)
%18 = icmp eq i32* %17, null
br i1 %18, label %24, label %19
19: ; preds = %16
%20 = call %struct.dirent* @readdir(i32* %17)
%21 = icmp eq %struct.dirent* %20, null
br i1 %21, label %68, label %22
22: ; preds = %19
%23 = bitcast i8** %5 to i8*
br label %26
24: ; preds = %16
%25 = load i32, i32* @errno
br label %71
26: ; preds = %22, %64
%27 = phi %struct.dirent* [ %20, %22 ], [ %66, %64 ]
%28 = phi i32 [ 0, %22 ], [ %65, %64 ]
%29 = getelementptr inbounds %struct.dirent, %struct.dirent* %27, i64 0, i32 0
%30 = load i8*, i8** %29
%31 = call i64 @strcmp(i8* %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%32 = icmp eq i64 %31, 0
br i1 %32, label %64, label %33
33: ; preds = %26
%34 = call i64 @strcmp(i8* %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%35 = icmp eq i64 %34, 0
br i1 %35, label %64, label %36
36: ; preds = %33
%37 = call i8* @append_file(i8* %2, i8* %30)
%38 = call i32 @stat(i8* %37, %struct.stat* %4)
%39 = icmp slt i32 %38, 0
br i1 %39, label %59, label %40
40: ; preds = %36
%41 = load i32, i32* %10
%42 = call i32 @S_ISDIR(i32 %41)
%43 = icmp eq i32 %42, 0
br i1 %43, label %59, label %44
44: ; preds = %40
%45 = call i8* @append_file(i8* %37, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%46 = call i32 @stat(i8* %45, %struct.stat* %4)
%47 = icmp slt i32 %46, 0
br i1 %47, label %56, label %48
48: ; preds = %44
%49 = call i32 @read_file(i8* %45, i8** %5)
%50 = icmp slt i32 %49, 0
br i1 %50, label %56, label %51
51: ; preds = %48
%52 = load i8*, i8** %5
%53 = call i32 @pevent_parse_event(%struct.pevent* %0, i8* %52, i32 %49, i8* %1)
%54 = load i8*, i8** %5
%55 = call i32 @free(i8* %54)
br label %56
56: ; preds = %48, %44, %51
%57 = phi i32 [ %46, %44 ], [ %46, %48 ], [ %53, %51 ]
%58 = call i32 @free(i8* %45)
br label %59
59: ; preds = %36, %40, %56
%60 = phi i32 [ %38, %36 ], [ %57, %56 ], [ %38, %40 ]
%61 = call i32 @free(i8* %37)
%62 = icmp eq i32 %60, 0
%63 = select i1 %62, i32 %28, i32 %60
br label %64
64: ; preds = %26, %33, %59
%65 = phi i32 [ %63, %59 ], [ %28, %33 ], [ %28, %26 ]
%66 = call %struct.dirent* @readdir(i32* %17)
%67 = icmp eq %struct.dirent* %66, null
br i1 %67, label %68, label %26
68: ; preds = %64, %19
%69 = phi i32 [ 0, %19 ], [ %65, %64 ]
%70 = call i32 @closedir(i32* %17)
br label %71
71: ; preds = %68, %24, %14
%72 = phi i32 [ %15, %14 ], [ %69, %68 ], [ %25, %24 ]
ret i32 %72
}