#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 /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
char *append_file(char const *, char const *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ *opendir(char const *);
int /*<<< orphan*/ 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 *);
void 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;
  ret = stat(sys_dir, &st);
  if (ret < 0 || !S_ISDIR(st.st_mode))
    return;
  dir = opendir(sys_dir);
  if (!dir)
    return;
  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;
    pevent_parse_event(pevent, buf, len, system);
    free(buf);
  free_format:
    free(format);
  free_event:
    free(event);
  }
  closedir(dir);
}


x86 assembly (clang, -O0):
<load_events>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   10a0 <stat@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jl     11e6 <load_events+0x46>
mov    -0x28(%rbp),%edi
call   1070 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    11eb <load_events+0x4b>
jmp    1338 <load_events+0x198>
mov    -0x18(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1208 <load_events+0x68>
jmp    1338 <load_events+0x198>
jmp    120d <load_events+0x6d>
mov    -0x30(%rbp),%rdi
call   10c0 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     132f <load_events+0x18f>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
lea    0xdc7(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     1263 <load_events+0xc3>
mov    -0x40(%rbp),%rdi
lea    0xdac(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    1268 <load_events+0xc8>
jmp    120d <load_events+0x6d>
mov    -0x18(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1060 <append_file@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   10a0 <stat@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jl     12a4 <load_events+0x104>
mov    -0x28(%rbp),%edi
call   1070 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    12a9 <load_events+0x109>
jmp    1321 <load_events+0x181>
mov    -0x48(%rbp),%rdi
lea    0xd4f(%rip),%rsi
call   1060 <append_file@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   10a0 <stat@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    12dc <load_events+0x13c>
jmp    1318 <load_events+0x178>
mov    -0x50(%rbp),%rdi
lea    -0x58(%rbp),%rsi
call   10b0 <read_file@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    12fb <load_events+0x15b>
jmp    1318 <load_events+0x178>
mov    -0x8(%rbp),%rdi
mov    -0x58(%rbp),%rsi
mov    -0x34(%rbp),%edx
mov    -0x10(%rbp),%rcx
call   1040 <pevent_parse_event@plt>
mov    -0x58(%rbp),%rdi
call   1030 <free@plt>
mov    -0x50(%rbp),%rdi
call   1030 <free@plt>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
jmp    120d <load_events+0x6d>
mov    -0x30(%rbp),%rdi
call   1080 <closedir@plt>
add    $0x60,%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,%r12
mov    %rsi,%r14
mov    %rdi,%rbp
lea    0x8(%rsp),%rsi
mov    %rdx,%rdi
call   10a0 <stat@plt>
test   %eax,%eax
js     1301 <load_events+0x161>
mov    0x8(%rsp),%edi
call   1070 <S_ISDIR@plt>
test   %eax,%eax
je     1301 <load_events+0x161>
mov    %r12,%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     1301 <load_events+0x161>
mov    %rax,%r13
mov    %rbp,0x18(%rsp)
mov    %r14,0x20(%rsp)
mov    %rax,%rdi
call   10c0 <readdir@plt>
test   %rax,%rax
je     12f9 <load_events+0x159>
lea    0xdee(%rip),%rbx
lea    0xde6(%rip),%r14
jmp    1243 <load_events+0xa3>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%r14
mov    %rbp,%rbx
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r13,%rdi
call   10c0 <readdir@plt>
test   %rax,%rax
je     12f9 <load_events+0x159>
mov    (%rax),%rbp
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1232 <load_events+0x92>
mov    %rbp,%rdi
mov    %r14,%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1232 <load_events+0x92>
mov    %r12,%rdi
mov    %rbp,%rsi
call   1060 <append_file@plt>
mov    %rax,%r15
mov    %rax,%rdi
lea    0x8(%rsp),%rsi
call   10a0 <stat@plt>
test   %eax,%eax
js     122a <load_events+0x8a>
mov    0x8(%rsp),%edi
call   1070 <S_ISDIR@plt>
test   %eax,%eax
je     122a <load_events+0x8a>
mov    %rbx,%rbp
mov    %r14,%rbx
mov    %r15,%rdi
lea    0xd61(%rip),%rsi
call   1060 <append_file@plt>
mov    %rax,%r14
mov    %rax,%rdi
lea    0x8(%rsp),%rsi
call   10a0 <stat@plt>
test   %eax,%eax
js     121c <load_events+0x7c>
mov    %r14,%rdi
lea    0x10(%rsp),%rsi
call   10b0 <read_file@plt>
test   %eax,%eax
js     121c <load_events+0x7c>
mov    0x10(%rsp),%rsi
mov    0x18(%rsp),%rdi
mov    %eax,%edx
mov    0x20(%rsp),%rcx
call   1040 <pevent_parse_event@plt>
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
jmp    121c <load_events+0x7c>
mov    %r13,%rdi
call   1080 <closedir@plt>
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 }
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [7 x i8] c"format\00"
define void @load_events(%struct.pevent* %0, i8* %1, i8* %2) {
%4 = alloca %struct.pevent*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca %struct.dirent*
%8 = alloca %struct.stat
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i8*
%15 = alloca i8*
store %struct.pevent* %0, %struct.pevent** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
store i32 0, i32* %10
%16 = load i8*, i8** %6
%17 = call i32 @stat(i8* %16, %struct.stat* %8)
store i32 %17, i32* %11
%18 = load i32, i32* %11
%19 = icmp slt i32 %18, 0
br i1 %19, label %25, label %20
20: ; preds = %3
%21 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%22 = load i32, i32* %21
%23 = call i32 @S_ISDIR(i32 %22)
%24 = icmp ne i32 %23, 0
br i1 %24, label %26, label %25
25: ; preds = %20, %3
br label %94
26: ; preds = %20
%27 = load i8*, i8** %6
%28 = call i32* @opendir(i8* %27)
store i32* %28, i32** %9
%29 = load i32*, i32** %9
%30 = icmp ne i32* %29, null
br i1 %30, label %32, label %31
31: ; preds = %26
br label %94
32: ; preds = %26
br label %33
33: ; preds = %88, %48, %32
%34 = load i32*, i32** %9
%35 = call %struct.dirent* @readdir(i32* %34)
store %struct.dirent* %35, %struct.dirent** %7
%36 = icmp ne %struct.dirent* %35, null
br i1 %36, label %37, label %91
37: ; preds = %33
%38 = load %struct.dirent*, %struct.dirent** %7
%39 = getelementptr inbounds %struct.dirent, %struct.dirent* %38, i32 0, i32 0
%40 = load i8*, i8** %39
store i8* %40, i8** %12
%41 = load i8*, i8** %12
%42 = call i64 @strcmp(i8* %41, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%43 = icmp eq i64 %42, 0
br i1 %43, label %48, label %44
44: ; preds = %37
%45 = load i8*, i8** %12
%46 = call i64 @strcmp(i8* %45, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %44, %37
br label %33
49: ; preds = %44
%50 = load i8*, i8** %6
%51 = load i8*, i8** %12
%52 = call i8* @append_file(i8* %50, i8* %51)
store i8* %52, i8** %13
%53 = load i8*, i8** %13
%54 = call i32 @stat(i8* %53, %struct.stat* %8)
store i32 %54, i32* %11
%55 = load i32, i32* %11
%56 = icmp slt i32 %55, 0
br i1 %56, label %62, label %57
57: ; preds = %49
%58 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%59 = load i32, i32* %58
%60 = call i32 @S_ISDIR(i32 %59)
%61 = icmp ne i32 %60, 0
br i1 %61, label %63, label %62
62: ; preds = %57, %49
br label %88
63: ; preds = %57
%64 = load i8*, i8** %13
%65 = call i8* @append_file(i8* %64, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i8* %65, i8** %14
%66 = load i8*, i8** %14
%67 = call i32 @stat(i8* %66, %struct.stat* %8)
store i32 %67, i32* %11
%68 = load i32, i32* %11
%69 = icmp slt i32 %68, 0
br i1 %69, label %70, label %71
70: ; preds = %63
br label %85
71: ; preds = %63
%72 = load i8*, i8** %14
%73 = call i32 @read_file(i8* %72, i8** %15)
store i32 %73, i32* %10
%74 = load i32, i32* %10
%75 = icmp slt i32 %74, 0
br i1 %75, label %76, label %77
76: ; preds = %71
br label %85
77: ; preds = %71
%78 = load %struct.pevent*, %struct.pevent** %4
%79 = load i8*, i8** %15
%80 = load i32, i32* %10
%81 = load i8*, i8** %5
%82 = call i32 @pevent_parse_event(%struct.pevent* %78, i8* %79, i32 %80, i8* %81)
%83 = load i8*, i8** %15
%84 = call i32 @free(i8* %83)
br label %85
85: ; preds = %77, %76, %70
%86 = load i8*, i8** %14
%87 = call i32 @free(i8* %86)
br label %88
88: ; preds = %85, %62
%89 = load i8*, i8** %13
%90 = call i32 @free(i8* %89)
br label %33
91: ; preds = %33
%92 = load i32*, i32** %9
%93 = call i32 @closedir(i32* %92)
br label %94
94: ; preds = %91, %31, %25
ret void
}

LLVM IR (-O2):
%struct.pevent = type { i32 }
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [7 x i8] c"format\00"
define void @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 %60, 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 %60, label %14
14: ; preds = %9
%15 = tail call i32* @opendir(i8* %2)
%16 = icmp eq i32* %15, null
br i1 %16, label %60, label %17
17: ; preds = %14
%18 = call %struct.dirent* @readdir(i32* %15)
%19 = icmp eq %struct.dirent* %18, null
br i1 %19, label %58, label %20
20: ; preds = %17
%21 = bitcast i8** %5 to i8*
br label %22
22: ; preds = %20, %55
%23 = phi %struct.dirent* [ %18, %20 ], [ %56, %55 ]
%24 = getelementptr inbounds %struct.dirent, %struct.dirent* %23, i64 0, i32 0
%25 = load i8*, i8** %24
%26 = call i64 @strcmp(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
br i1 %27, label %55, label %28
28: ; preds = %22
%29 = call i64 @strcmp(i8* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%30 = icmp eq i64 %29, 0
br i1 %30, label %55, label %31
31: ; preds = %28
%32 = call i8* @append_file(i8* %2, i8* %25)
%33 = call i32 @stat(i8* %32, %struct.stat* %4)
%34 = icmp slt i32 %33, 0
br i1 %34, label %53, label %35
35: ; preds = %31
%36 = load i32, i32* %10
%37 = call i32 @S_ISDIR(i32 %36)
%38 = icmp eq i32 %37, 0
br i1 %38, label %53, label %39
39: ; preds = %35
%40 = call i8* @append_file(i8* %32, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%41 = call i32 @stat(i8* %40, %struct.stat* %4)
%42 = icmp slt i32 %41, 0
br i1 %42, label %51, label %43
43: ; preds = %39
%44 = call i32 @read_file(i8* %40, i8** %5)
%45 = icmp slt i32 %44, 0
br i1 %45, label %51, label %46
46: ; preds = %43
%47 = load i8*, i8** %5
%48 = call i32 @pevent_parse_event(%struct.pevent* %0, i8* %47, i32 %44, i8* %1)
%49 = load i8*, i8** %5
%50 = call i32 @free(i8* %49)
br label %51
51: ; preds = %43, %39, %46
%52 = call i32 @free(i8* %40)
br label %53
53: ; preds = %31, %35, %51
%54 = call i32 @free(i8* %32)
br label %55
55: ; preds = %22, %28, %53
%56 = call %struct.dirent* @readdir(i32* %15)
%57 = icmp eq %struct.dirent* %56, null
br i1 %57, label %58, label %22
58: ; preds = %55, %17
%59 = call i32 @closedir(i32* %15)
br label %60
60: ; preds = %14, %3, %9, %58
ret void
}