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

/* Variables and functions */
size_t NAME_MAX;
int PATH_MAX;
int /*<<< orphan*/ bzero(char *, size_t);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ display_attr(char *, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ getcwd(char *, int);
int /*<<< orphan*/ *opendir(int /*<<< orphan*/);
struct dirent *readdir(int /*<<< orphan*/ *);
size_t strlen(char *);
int list_cwd(char *buf, size_t buf_size) {
  DIR *dir = NULL;
  char cwd[PATH_MAX];
  struct dirent *de = NULL;
  bzero(buf, buf_size);
  dir = opendir(getcwd(cwd, PATH_MAX));
  while ((de = readdir(dir)) != NULL) {
    if (buf_size - strlen(buf) < 63 + NAME_MAX)
      return -1;
    display_attr(buf, buf_size, de->d_name);
  }
  closedir(dir);
  return 0;
}


x86 assembly (clang, -O0):
<list_cwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2e61(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1090 <bzero@plt>
mov    -0x48(%rbp),%rdi
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
call   1060 <getcwd@plt>
mov    %eax,%edi
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     125a <list_cwd+0xea>
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x50(%rbp),%rax
sub    %rcx,%rax
mov    0x2db1(%rip),%rcx
mov    (%rcx),%rcx
add    $0x3f,%rcx
cmp    %rcx,%rax
jae    1242 <list_cwd+0xd2>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    1271 <list_cwd+0x101>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x38(%rbp),%rax
mov    (%rax),%edx
call   1030 <display_attr@plt>
jmp    11e6 <list_cwd+0x76>
mov    -0x20(%rbp),%rdi
call   1070 <closedir@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<list_cwd>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
mov    0x2e65(%rip),%r15
mov    (%r15),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
call   1090 <bzero@plt>
mov    (%r15),%esi
mov    %rbx,%rdi
call   1060 <getcwd@plt>
mov    %eax,%edi
call   1040 <opendir@plt>
mov    %rax,%r15
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     1208 <list_cwd+0x98>
mov    %rax,%rbx
mov    0x2e00(%rip),%r13
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %r14,%rcx
sub    %rax,%rcx
mov    0x0(%r13),%rax
add    $0x3f,%rax
cmp    %rax,%rcx
jb     1214 <list_cwd+0xa4>
mov    (%rbx),%edx
mov    %r12,%rdi
mov    %r14,%rsi
call   1030 <display_attr@plt>
mov    %r15,%rdi
call   1080 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11d0 <list_cwd+0x60>
mov    %r15,%rdi
call   1070 <closedir@plt>
xor    %eax,%eax
jmp    1219 <list_cwd+0xa9>
mov    $0xffffffff,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
@PATH_MAX = global i32 0
@NAME_MAX = global i64 0
define i32 @list_cwd(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca %struct.dirent*
%10 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
store i32* null, i32** %6
%11 = load i32, i32* @PATH_MAX
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %7
%14 = alloca i8, i64 %12
store i64 %12, i64* %8
store %struct.dirent* null, %struct.dirent** %9
%15 = load i8*, i8** %4
%16 = load i64, i64* %5
%17 = call i32 @bzero(i8* %15, i64 %16)
%18 = load i32, i32* @PATH_MAX
%19 = call i32 @getcwd(i8* %14, i32 %18)
%20 = call i32* @opendir(i32 %19)
store i32* %20, i32** %6
br label %21
21: ; preds = %34, %2
%22 = load i32*, i32** %6
%23 = call %struct.dirent* @readdir(i32* %22)
store %struct.dirent* %23, %struct.dirent** %9
%24 = icmp ne %struct.dirent* %23, null
br i1 %24, label %25, label %41
25: ; preds = %21
%26 = load i64, i64* %5
%27 = load i8*, i8** %4
%28 = call i64 @strlen(i8* %27)
%29 = sub i64 %26, %28
%30 = load i64, i64* @NAME_MAX
%31 = add i64 63, %30
%32 = icmp ult i64 %29, %31
br i1 %32, label %33, label %34
33: ; preds = %25
store i32 -1, i32* %3
store i32 1, i32* %10
br label %44
34: ; preds = %25
%35 = load i8*, i8** %4
%36 = load i64, i64* %5
%37 = load %struct.dirent*, %struct.dirent** %9
%38 = getelementptr inbounds %struct.dirent, %struct.dirent* %37, i32 0, i32 0
%39 = load i32, i32* %38
%40 = call i32 @display_attr(i8* %35, i64 %36, i32 %39)
br label %21
41: ; preds = %21
%42 = load i32*, i32** %6
%43 = call i32 @closedir(i32* %42)
store i32 0, i32* %3
store i32 1, i32* %10
br label %44
44: ; preds = %41, %33
%45 = load i8*, i8** %7
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@PATH_MAX = global i32 0
@NAME_MAX = global i64 0
define i32 @list_cwd(i8* %0, i64 %1) {
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = tail call i32 @bzero(i8* %0, i64 %1)
%7 = load i32, i32* @PATH_MAX
%8 = call i32 @getcwd(i8* %5, i32 %7)
%9 = call i32* @opendir(i32 %8)
%10 = call %struct.dirent* @readdir(i32* %9)
%11 = icmp eq %struct.dirent* %10, null
br i1 %11, label %25, label %12
12: ; preds = %2, %19
%13 = phi %struct.dirent* [ %23, %19 ], [ %10, %2 ]
%14 = call i64 @strlen(i8* dereferenceable(1) %0)
%15 = sub i64 %1, %14
%16 = load i64, i64* @NAME_MAX
%17 = add i64 %16, 63
%18 = icmp ult i64 %15, %17
br i1 %18, label %27, label %19
19: ; preds = %12
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %13, i64 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @display_attr(i8* %0, i64 %1, i32 %21)
%23 = call %struct.dirent* @readdir(i32* %9)
%24 = icmp eq %struct.dirent* %23, null
br i1 %24, label %25, label %12
25: ; preds = %19, %2
%26 = call i32 @closedir(i32* %9)
br label %27
27: ; preds = %12, %25
%28 = phi i32 [ 0, %25 ], [ -1, %12 ]
ret i32 %28
}