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

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int proc_ls(char **accept, bool verbose) {
  DIR *dir = opendir("/proc");
  struct dirent *ent;
  if (dir != NULL) {
    int ls_buf_len = 256;
    char *ls[ls_buf_len];
    int ls_i = 0;
    while ((ent = readdir(dir)) != NULL) {
      if (ls_i < ls_buf_len) {
        ls[ls_i] = (ent->d_name);
        if (verbose) {
          printf("Received %s\n.", ent->d_name);
        }
      }
    }
    closedir(dir);
    accept = ls;
    return 0;
  } else {
    perror("Unable to open '/proc' directory.");
    return -1;
  }
}


x86 assembly (clang, -O0):
<proc_ls>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    0xe9a(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1230 <proc_ls+0xe0>
movl   $0x100,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
lea    0xf(,%rax,8),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     120c <proc_ls+0xbc>
mov    -0x44(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    1207 <proc_ls+0xb7>
mov    -0x50(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    (%rcx),%rdx
movslq -0x44(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
cmpl   $0x0,-0x14(%rbp)
je     1202 <proc_ls+0xb2>
mov    -0x28(%rbp),%rax
mov    (%rax),%rsi
lea    0xe09(%rip),%rdi
call   1040 <printf@plt>
jmp    1207 <proc_ls+0xb7>
jmp    11af <proc_ls+0x5f>
mov    -0x20(%rbp),%rdi
call   1050 <closedir@plt>
mov    -0x50(%rbp),%rax
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x4(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
jmp    1243 <proc_ls+0xf3>
lea    0xddd(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<proc_ls>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebp
lea    0xea0(%rip),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     11c6 <proc_ls+0x76>
mov    %rax,%rbx
mov    %rsp,%r15
mov    %rax,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     11ae <proc_ls+0x5e>
lea    0xe82(%rip),%r14
jmp    119d <proc_ls+0x4d>
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     11ae <proc_ls+0x5e>
test   %ebp,%ebp
je     1190 <proc_ls+0x40>
mov    (%rax),%rsi
mov    %r14,%rdi
call   1040 <printf@plt>
jmp    1190 <proc_ls+0x40>
mov    %rbx,%rdi
call   1050 <closedir@plt>
xor    %eax,%eax
mov    %r15,%rsp
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe47(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%eax
jmp    11bb <proc_ls+0x6b>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [6 x i8] c"/proc\00"
@.str.1 = [14 x i8] c"Received %s\0A.\00"
@.str.2 = [34 x i8] c"Unable to open '/proc' directory.\00"
define i32 @proc_ls(i8** %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i32*
%7 = alloca %struct.dirent*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
store i8** %0, i8*** %4
store i32 %1, i32* %5
%12 = call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %6
%13 = load i32*, i32** %6
%14 = icmp ne i32* %13, null
br i1 %14, label %15, label %48
15: ; preds = %2
store i32 256, i32* %8
%16 = load i32, i32* %8
%17 = zext i32 %16 to i64
%18 = call i8* @llvm.stacksave()
store i8* %18, i8** %9
%19 = alloca i8*, i64 %17
store i64 %17, i64* %10
store i32 0, i32* %11
br label %20
20: ; preds = %43, %15
%21 = load i32*, i32** %6
%22 = call %struct.dirent* @readdir(i32* %21)
store %struct.dirent* %22, %struct.dirent** %7
%23 = icmp ne %struct.dirent* %22, null
br i1 %23, label %24, label %44
24: ; preds = %20
%25 = load i32, i32* %11
%26 = load i32, i32* %8
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %43
28: ; preds = %24
%29 = load %struct.dirent*, %struct.dirent** %7
%30 = getelementptr inbounds %struct.dirent, %struct.dirent* %29, i32 0, i32 0
%31 = load i8*, i8** %30
%32 = load i32, i32* %11
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8*, i8** %19, i64 %33
store i8* %31, i8** %34
%35 = load i32, i32* %5
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %42
37: ; preds = %28
%38 = load %struct.dirent*, %struct.dirent** %7
%39 = getelementptr inbounds %struct.dirent, %struct.dirent* %38, i32 0, i32 0
%40 = load i8*, i8** %39
%41 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %40)
br label %42
42: ; preds = %37, %28
br label %43
43: ; preds = %42, %24
br label %20
44: ; preds = %20
%45 = load i32*, i32** %6
%46 = call i32 @closedir(i32* %45)
store i8** %19, i8*** %4
store i32 0, i32* %3
%47 = load i8*, i8** %9
br label %50
48: ; preds = %2
%49 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %50
50: ; preds = %48, %44
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [6 x i8] c"/proc\00"
@.str.1 = [14 x i8] c"Received %s\0A.\00"
@.str.2 = [34 x i8] c"Unable to open '/proc' directory.\00"
define i32 @proc_ls(i8** nocapture readnone %0, i32 %1) {
%3 = tail call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %22, label %5
5: ; preds = %2
%6 = tail call i8* @llvm.stacksave()
%7 = tail call %struct.dirent* @readdir(i32* %3)
%8 = icmp eq %struct.dirent* %7, null
br i1 %8, label %20, label %9
9: ; preds = %5
%10 = icmp eq i32 %1, 0
br label %11
11: ; preds = %9, %17
%12 = phi %struct.dirent* [ %7, %9 ], [ %18, %17 ]
br i1 %10, label %17, label %13
13: ; preds = %11
%14 = getelementptr inbounds %struct.dirent, %struct.dirent* %12, i64 0, i32 0
%15 = load i8*, i8** %14
%16 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %15)
br label %17
17: ; preds = %11, %13
%18 = tail call %struct.dirent* @readdir(i32* %3)
%19 = icmp eq %struct.dirent* %18, null
br i1 %19, label %20, label %11
20: ; preds = %17, %5
%21 = tail call i32 @closedir(i32* %3)
tail call void @llvm.stackrestore(i8* %6)
br label %24
22: ; preds = %2
%23 = tail call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
br label %24
24: ; preds = %22, %20
%25 = phi i32 [ 0, %20 ], [ -1, %22 ]
ret i32 %25
}