#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 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 *);
struct dirent *readdir(int /*<<< orphan*/ *);
int stat(char *, struct stat *);
scalar_t__ strcmp(char const *, char *);
char **tracecmd_add_list(char **, char const *, int /*<<< orphan*/);
char **tracecmd_event_systems(const char *tracing_dir) {
  struct dirent *dent;
  char **systems = NULL;
  char *events_dir;
  struct stat st;
  DIR *dir;
  int len = 0;
  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;
  while ((dent = readdir(dir))) {
    const char *name = dent->d_name;
    char *enable;
    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;
    }
    enable = append_file(sys, "enable");
    ret = stat(enable, &st);
    if (ret >= 0)
      systems = tracecmd_add_list(systems, name, len++);
    free(enable);
    free(sys);
  }
  closedir(dir);
out_free:
  free(events_dir);
  return systems;
}


x86 assembly (clang, -O0):
<tracecmd_event_systems>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x3c(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11c3 <tracecmd_event_systems+0x33>
movq   $0x0,-0x8(%rbp)
jmp    1361 <tracecmd_event_systems+0x1d1>
mov    -0x10(%rbp),%rdi
lea    0xe32(%rip),%rsi
call   1050 <append_file@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11ef <tracecmd_event_systems+0x5f>
movq   $0x0,-0x8(%rbp)
jmp    1361 <tracecmd_event_systems+0x1d1>
mov    -0x28(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1090 <stat@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
jl     121a <tracecmd_event_systems+0x8a>
mov    -0x30(%rbp),%edi
call   1060 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    121f <tracecmd_event_systems+0x8f>
jmp    1350 <tracecmd_event_systems+0x1c0>
mov    -0x28(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    123c <tracecmd_event_systems+0xac>
jmp    1350 <tracecmd_event_systems+0x1c0>
jmp    1241 <tracecmd_event_systems+0xb1>
mov    -0x38(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1347 <tracecmd_event_systems+0x1b7>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
lea    0xd9a(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     1297 <tracecmd_event_systems+0x107>
mov    -0x48(%rbp),%rdi
lea    0xd7f(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    129c <tracecmd_event_systems+0x10c>
jmp    1241 <tracecmd_event_systems+0xb1>
mov    -0x28(%rbp),%rdi
mov    -0x48(%rbp),%rsi
call   1050 <append_file@plt>
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1090 <stat@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
jl     12d8 <tracecmd_event_systems+0x148>
mov    -0x30(%rbp),%edi
call   1060 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    12e6 <tracecmd_event_systems+0x156>
mov    -0x58(%rbp),%rdi
call   1030 <free@plt>
jmp    1241 <tracecmd_event_systems+0xb1>
mov    -0x58(%rbp),%rdi
lea    0xd19(%rip),%rsi
call   1050 <append_file@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1090 <stat@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
jl     1330 <tracecmd_event_systems+0x1a0>
mov    -0x20(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    -0x3c(%rbp),%edx
mov    %edx,%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
call   10b0 <tracecmd_add_list@plt>
mov    %rax,-0x20(%rbp)
mov    -0x50(%rbp),%rdi
call   1030 <free@plt>
mov    -0x58(%rbp),%rdi
call   1030 <free@plt>
jmp    1241 <tracecmd_event_systems+0xb1>
mov    -0x38(%rbp),%rdi
call   1070 <closedir@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tracecmd_event_systems>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
test   %rdi,%rdi
je     1220 <tracecmd_event_systems+0x90>
lea    0xe56(%rip),%rsi
call   1050 <append_file@plt>
test   %rax,%rax
je     1220 <tracecmd_event_systems+0x90>
mov    %rax,%r15
lea    0x8(%rsp),%rsi
mov    %rax,%rdi
call   1090 <stat@plt>
test   %eax,%eax
js     12f1 <tracecmd_event_systems+0x161>
mov    0x8(%rsp),%edi
call   1060 <S_ISDIR@plt>
test   %eax,%eax
je     12f1 <tracecmd_event_systems+0x161>
mov    %r15,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     12f1 <tracecmd_event_systems+0x161>
mov    %rax,%r14
mov    %rax,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     12f5 <tracecmd_event_systems+0x165>
xor    %ecx,%ecx
mov    %rcx,0x20(%rsp)
lea    0xdf8(%rip),%rbx
lea    0xdf0(%rip),%r13
xor    %ecx,%ecx
mov    %rcx,0x10(%rsp)
jmp    124f <tracecmd_event_systems+0xbf>
xor    %ebx,%ebx
jmp    1311 <tracecmd_event_systems+0x181>
mov    %r13,%rdi
call   1030 <free@plt>
lea    0xdd1(%rip),%r13
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     12fc <tracecmd_event_systems+0x16c>
mov    (%rax),%rbp
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     123e <tracecmd_event_systems+0xae>
mov    %rbp,%rdi
mov    %r13,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     123e <tracecmd_event_systems+0xae>
mov    %r15,%rdi
mov    %rbp,%rsi
call   1050 <append_file@plt>
mov    %rax,%r12
mov    %rax,%rdi
lea    0x8(%rsp),%rsi
call   1090 <stat@plt>
test   %eax,%eax
js     1236 <tracecmd_event_systems+0xa6>
mov    0x8(%rsp),%edi
call   1060 <S_ISDIR@plt>
test   %eax,%eax
je     1236 <tracecmd_event_systems+0xa6>
mov    %r12,%rdi
lea    0xd62(%rip),%rsi
call   1050 <append_file@plt>
mov    %rax,%r13
mov    %rax,%rdi
lea    0x8(%rsp),%rsi
call   1090 <stat@plt>
test   %eax,%eax
js     1227 <tracecmd_event_systems+0x97>
mov    0x20(%rsp),%rdx
lea    0x1(%rdx),%eax
mov    %eax,0x1c(%rsp)
mov    0x10(%rsp),%rdi
mov    %rbp,%rsi
call   10b0 <tracecmd_add_list@plt>
mov    %rax,0x10(%rsp)
mov    0x1c(%rsp),%eax
mov    %rax,0x20(%rsp)
jmp    1227 <tracecmd_event_systems+0x97>
xor    %ebx,%ebx
jmp    1309 <tracecmd_event_systems+0x179>
xor    %eax,%eax
mov    %rax,0x10(%rsp)
mov    %r14,%rdi
call   1070 <closedir@plt>
mov    0x10(%rsp),%rbx
mov    %r15,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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