#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 */
int MAXPATHLEN;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ getcwd(char *, int);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ puts(int /*<<< orphan*/);
struct dirent *readdir(int /*<<< orphan*/ *);
int main(void) {
  char path[MAXPATHLEN];
  getcwd(path, MAXPATHLEN);
  printf("wd: %s\n", path);
  DIR *d = opendir(path);
  struct dirent *dir;
  if (d) {
    while ((dir = readdir(d)) != NULL) {
      puts(dir->d_name);
    }
    closedir(d);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x30(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x18(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
call   1060 <getcwd@plt>
mov    -0x30(%rbp),%rsi
lea    0xe4c(%rip),%rdi
call   1050 <printf@plt>
mov    -0x30(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1206 <main+0xa6>
jmp    11d6 <main+0x76>
mov    -0x20(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     11fd <main+0x9d>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1030 <puts@plt>
jmp    11d6 <main+0x76>
mov    -0x20(%rbp),%rdi
call   1070 <closedir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %rbx
push   %rax
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rbx
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
call   1060 <getcwd@plt>
lea    0xe6e(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     11ce <main+0x6e>
mov    %rax,%rbx
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     11c6 <main+0x66>
mov    (%rax),%edi
call   1030 <puts@plt>
jmp    11b0 <main+0x50>
mov    %rbx,%rdi
call   1070 <closedir@plt>
xor    %eax,%eax
lea    -0x8(%rbp),%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
@MAXPATHLEN = global i32 0
@.str = [8 x i8] c"wd: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i32*
%5 = alloca %struct.dirent*
store i32 0, i32* %1
%6 = load i32, i32* @MAXPATHLEN
%7 = zext i32 %6 to i64
%8 = call i8* @llvm.stacksave()
store i8* %8, i8** %2
%9 = alloca i8, i64 %7
store i64 %7, i64* %3
%10 = load i32, i32* @MAXPATHLEN
%11 = call i32 @getcwd(i8* %9, i32 %10)
%12 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %9)
%13 = call i32* @opendir(i8* %9)
store i32* %13, i32** %4
%14 = load i32*, i32** %4
%15 = icmp ne i32* %14, null
br i1 %15, label %16, label %29
16: ; preds = %0
br label %17
17: ; preds = %21, %16
%18 = load i32*, i32** %4
%19 = call %struct.dirent* @readdir(i32* %18)
store %struct.dirent* %19, %struct.dirent** %5
%20 = icmp ne %struct.dirent* %19, null
br i1 %20, label %21, label %26
21: ; preds = %17
%22 = load %struct.dirent*, %struct.dirent** %5
%23 = getelementptr inbounds %struct.dirent, %struct.dirent* %22, i32 0, i32 0
%24 = load i32, i32* %23
%25 = call i32 @puts(i32 %24)
br label %17
26: ; preds = %17
%27 = load i32*, i32** %4
%28 = call i32 @closedir(i32* %27)
br label %29
29: ; preds = %26, %0
store i32 0, i32* %1
%30 = load i8*, i8** %2
%31 = load i32, i32* %1
ret i32 %31
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@MAXPATHLEN = global i32 0
@.str = [8 x i8] c"wd: %s\0A\00"
define i32 @main() {
%1 = load i32, i32* @MAXPATHLEN
%2 = zext i32 %1 to i64
%3 = alloca i8, i64 %2
%4 = call i32 @getcwd(i8* %3, i32 %1)
%5 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %3)
%6 = call i32* @opendir(i8* %3)
%7 = icmp eq i32* %6, null
br i1 %7, label %20, label %8
8: ; preds = %0
%9 = call %struct.dirent* @readdir(i32* %6)
%10 = icmp eq %struct.dirent* %9, null
br i1 %10, label %18, label %11
11: ; preds = %8, %11
%12 = phi %struct.dirent* [ %16, %11 ], [ %9, %8 ]
%13 = getelementptr inbounds %struct.dirent, %struct.dirent* %12, i64 0, i32 0
%14 = load i32, i32* %13
%15 = call i32 @puts(i32 %14)
%16 = call %struct.dirent* @readdir(i32* %6)
%17 = icmp eq %struct.dirent* %16, null
br i1 %17, label %18, label %11
18: ; preds = %11, %8
%19 = call i32 @closedir(i32* %6)
br label %20
20: ; preds = %18, %0
ret i32 0
}