#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*/ load_events(struct pevent *, char const *, char *);
int /*<<< orphan*/ *opendir(char *);
struct pevent *pevent_alloc();
int /*<<< orphan*/ pevent_free(struct pevent *);
int read_header(struct pevent *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int stat(char *, struct stat *);
scalar_t__ strcmp(char const *, char *);
struct pevent *tracecmd_local_events(const char *tracing_dir) {
  struct pevent *pevent = NULL;
  struct dirent *dent;
  char *events_dir;
  struct stat st;
  DIR *dir;
  int ret;
  if (!tracing_dir)
    return NULL;
  events_dir = append_file(tracing_dir, "events");
  if (!events_dir)
    return NULL;
  ret = stat(events_dir, &st);
  if (ret < 0 || !S_ISDIR(st.st_mode))
    goto out_free;
  dir = opendir(events_dir);
  if (!dir)
    goto out_free;
  pevent = pevent_alloc();
  if (!pevent)
    goto out_free;
  ret = read_header(pevent, events_dir);
  if (ret < 0) {
    pevent_free(pevent);
    pevent = NULL;
    goto out_free;
  }
  while ((dent = readdir(dir))) {
    const char *name = dent->d_name;
    char *sys;
    if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0)
      continue;
    sys = append_file(events_dir, name);
    ret = stat(sys, &st);
    if (ret < 0 || !S_ISDIR(st.st_mode)) {
      free(sys);
      continue;
    }
    load_events(pevent, name, sys);
    free(sys);
  }
  closedir(dir);
out_free:
  free(events_dir);
  return pevent;
}


x86 assembly (clang, -O0):
<tracecmd_local_events>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ec <tracecmd_local_events+0x2c>
movq   $0x0,-0x8(%rbp)
jmp    1393 <tracecmd_local_events+0x1d3>
mov    -0x10(%rbp),%rdi
lea    0xe09(%rip),%rsi
call   1070 <append_file@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1218 <tracecmd_local_events+0x58>
movq   $0x0,-0x8(%rbp)
jmp    1393 <tracecmd_local_events+0x1d3>
mov    -0x28(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   10b0 <stat@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jl     1243 <tracecmd_local_events+0x83>
mov    -0x30(%rbp),%edi
call   1080 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    1248 <tracecmd_local_events+0x88>
jmp    1382 <tracecmd_local_events+0x1c2>
mov    -0x28(%rbp),%rdi
call   1060 <opendir@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1265 <tracecmd_local_events+0xa5>
jmp    1382 <tracecmd_local_events+0x1c2>
mov    $0x0,%al
call   1050 <pevent_alloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1280 <tracecmd_local_events+0xc0>
jmp    1382 <tracecmd_local_events+0x1c2>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   10d0 <read_header@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    12b0 <tracecmd_local_events+0xf0>
mov    -0x18(%rbp),%rdi
call   1030 <pevent_free@plt>
movq   $0x0,-0x18(%rbp)
jmp    1382 <tracecmd_local_events+0x1c2>
jmp    12b5 <tracecmd_local_events+0xf5>
mov    -0x38(%rbp),%rdi
call   10c0 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1379 <tracecmd_local_events+0x1b9>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
lea    0xd26(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
je     130b <tracecmd_local_events+0x14b>
mov    -0x48(%rbp),%rdi
lea    0xd0b(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
jne    1310 <tracecmd_local_events+0x150>
jmp    12b5 <tracecmd_local_events+0xf5>
mov    -0x28(%rbp),%rdi
mov    -0x48(%rbp),%rsi
call   1070 <append_file@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   10b0 <stat@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jl     134c <tracecmd_local_events+0x18c>
mov    -0x30(%rbp),%edi
call   1080 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    135a <tracecmd_local_events+0x19a>
mov    -0x50(%rbp),%rdi
call   1040 <free@plt>
jmp    12b5 <tracecmd_local_events+0xf5>
mov    -0x18(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    -0x50(%rbp),%rdx
call   10e0 <load_events@plt>
mov    -0x50(%rbp),%rdi
call   1040 <free@plt>
jmp    12b5 <tracecmd_local_events+0xf5>
mov    -0x38(%rbp),%rdi
call   1090 <closedir@plt>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tracecmd_local_events>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     125f <tracecmd_local_events+0x9f>
lea    0xe25(%rip),%rsi
call   1070 <append_file@plt>
test   %rax,%rax
je     125f <tracecmd_local_events+0x9f>
mov    %rax,%r14
mov    %rsp,%rsi
mov    %rax,%rdi
call   10b0 <stat@plt>
test   %eax,%eax
js     12f7 <tracecmd_local_events+0x137>
mov    (%rsp),%edi
call   1080 <S_ISDIR@plt>
test   %eax,%eax
je     12f7 <tracecmd_local_events+0x137>
mov    %r14,%rdi
call   1060 <opendir@plt>
test   %rax,%rax
je     12f7 <tracecmd_local_events+0x137>
mov    %rax,%r15
xor    %eax,%eax
call   1050 <pevent_alloc@plt>
test   %rax,%rax
je     12f7 <tracecmd_local_events+0x137>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   10d0 <read_header@plt>
test   %eax,%eax
js     12ef <tracecmd_local_events+0x12f>
mov    %r15,%rdi
call   10c0 <readdir@plt>
test   %rax,%rax
je     12e5 <tracecmd_local_events+0x125>
lea    0xdab(%rip),%r12
jmp    1285 <tracecmd_local_events+0xc5>
xor    %ebx,%ebx
jmp    1301 <tracecmd_local_events+0x141>
cs nopw 0x0(%rax,%rax,1)
mov    %r13,%rdi
call   1040 <free@plt>
mov    %r15,%rdi
call   10c0 <readdir@plt>
test   %rax,%rax
je     12e5 <tracecmd_local_events+0x125>
mov    (%rax),%rbp
mov    %rbp,%rdi
mov    %r12,%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     1278 <tracecmd_local_events+0xb8>
mov    %rbp,%rdi
lea    0xd65(%rip),%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     1278 <tracecmd_local_events+0xb8>
mov    %r14,%rdi
mov    %rbp,%rsi
call   1070 <append_file@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    %rsp,%rsi
call   10b0 <stat@plt>
test   %eax,%eax
js     1270 <tracecmd_local_events+0xb0>
mov    (%rsp),%edi
call   1080 <S_ISDIR@plt>
test   %eax,%eax
je     1270 <tracecmd_local_events+0xb0>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r13,%rdx
call   10e0 <load_events@plt>
jmp    1270 <tracecmd_local_events+0xb0>
mov    %r15,%rdi
call   1090 <closedir@plt>
jmp    12f9 <tracecmd_local_events+0x139>
mov    %rbx,%rdi
call   1030 <pevent_free@plt>
xor    %ebx,%ebx
mov    %r14,%rdi
call   1040 <free@plt>
mov    %rbx,%rax
add    $0x8,%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 = [7 x i8] c"events\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
define %struct.pevent* @tracecmd_local_events(i8* %0) {
%2 = alloca %struct.pevent*
%3 = alloca i8*
%4 = alloca %struct.pevent*
%5 = alloca %struct.dirent*
%6 = alloca i8*
%7 = alloca %struct.stat
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
store i8* %0, i8** %3
store %struct.pevent* null, %struct.pevent** %4
%12 = load i8*, i8** %3
%13 = icmp ne i8* %12, null
br i1 %13, label %15, label %14
14: ; preds = %1
store %struct.pevent* null, %struct.pevent** %2
br label %99
15: ; preds = %1
%16 = load i8*, i8** %3
%17 = call i8* @append_file(i8* %16, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i8* %17, i8** %6
%18 = load i8*, i8** %6
%19 = icmp ne i8* %18, null
br i1 %19, label %21, label %20
20: ; preds = %15
store %struct.pevent* null, %struct.pevent** %2
br label %99
21: ; preds = %15
%22 = load i8*, i8** %6
%23 = call i32 @stat(i8* %22, %struct.stat* %7)
store i32 %23, i32* %9
%24 = load i32, i32* %9
%25 = icmp slt i32 %24, 0
br i1 %25, label %31, label %26
26: ; preds = %21
%27 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 @S_ISDIR(i32 %28)
%30 = icmp ne i32 %29, 0
br i1 %30, label %32, label %31
31: ; preds = %26, %21
br label %95
32: ; preds = %26
%33 = load i8*, i8** %6
%34 = call i32* @opendir(i8* %33)
store i32* %34, i32** %8
%35 = load i32*, i32** %8
%36 = icmp ne i32* %35, null
br i1 %36, label %38, label %37
37: ; preds = %32
br label %95
38: ; preds = %32
%39 = call %struct.pevent* (...) @pevent_alloc()
store %struct.pevent* %39, %struct.pevent** %4
%40 = load %struct.pevent*, %struct.pevent** %4
%41 = icmp ne %struct.pevent* %40, null
br i1 %41, label %43, label %42
42: ; preds = %38
br label %95
43: ; preds = %38
%44 = load %struct.pevent*, %struct.pevent** %4
%45 = load i8*, i8** %6
%46 = call i32 @read_header(%struct.pevent* %44, i8* %45)
store i32 %46, i32* %9
%47 = load i32, i32* %9
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %43
%50 = load %struct.pevent*, %struct.pevent** %4
%51 = call i32 @pevent_free(%struct.pevent* %50)
store %struct.pevent* null, %struct.pevent** %4
br label %95
52: ; preds = %43
br label %53
53: ; preds = %85, %82, %68, %52
%54 = load i32*, i32** %8
%55 = call %struct.dirent* @readdir(i32* %54)
store %struct.dirent* %55, %struct.dirent** %5
%56 = icmp ne %struct.dirent* %55, null
br i1 %56, label %57, label %92
57: ; preds = %53
%58 = load %struct.dirent*, %struct.dirent** %5
%59 = getelementptr inbounds %struct.dirent, %struct.dirent* %58, i32 0, i32 0
%60 = load i8*, i8** %59
store i8* %60, i8** %10
%61 = load i8*, i8** %10
%62 = call i64 @strcmp(i8* %61, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%63 = icmp eq i64 %62, 0
br i1 %63, label %68, label %64
64: ; preds = %57
%65 = load i8*, i8** %10
%66 = call i64 @strcmp(i8* %65, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%67 = icmp eq i64 %66, 0
br i1 %67, label %68, label %69
68: ; preds = %64, %57
br label %53
69: ; preds = %64
%70 = load i8*, i8** %6
%71 = load i8*, i8** %10
%72 = call i8* @append_file(i8* %70, i8* %71)
store i8* %72, i8** %11
%73 = load i8*, i8** %11
%74 = call i32 @stat(i8* %73, %struct.stat* %7)
store i32 %74, i32* %9
%75 = load i32, i32* %9
%76 = icmp slt i32 %75, 0
br i1 %76, label %82, label %77
77: ; preds = %69
%78 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%79 = load i32, i32* %78
%80 = call i32 @S_ISDIR(i32 %79)
%81 = icmp ne i32 %80, 0
br i1 %81, label %85, label %82
82: ; preds = %77, %69
%83 = load i8*, i8** %11
%84 = call i32 @free(i8* %83)
br label %53
85: ; preds = %77
%86 = load %struct.pevent*, %struct.pevent** %4
%87 = load i8*, i8** %10
%88 = load i8*, i8** %11
%89 = call i32 @load_events(%struct.pevent* %86, i8* %87, i8* %88)
%90 = load i8*, i8** %11
%91 = call i32 @free(i8* %90)
br label %53
92: ; preds = %53
%93 = load i32*, i32** %8
%94 = call i32 @closedir(i32* %93)
br label %95
95: ; preds = %92, %49, %42, %37, %31
%96 = load i8*, i8** %6
%97 = call i32 @free(i8* %96)
%98 = load %struct.pevent*, %struct.pevent** %4
store %struct.pevent* %98, %struct.pevent** %2
br label %99
99: ; preds = %95, %20, %14
%100 = load %struct.pevent*, %struct.pevent** %2
ret %struct.pevent* %100
}

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