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

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ getcwd(char *, int);
int /*<<< orphan*/ *opendir(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int executeLsCommand() {
  char cwd[1024];
  DIR *dp;
  dp = opendir(getcwd(cwd, sizeof(cwd)));
  struct dirent *sd;
  while ((sd = readdir(dp)) != NULL) {
    printf("%s\t", sd->d_name);
  }
  printf("\n");
  closedir(dp);
  return 0;
}


x86 assembly (clang, -O0):
<executeLsCommand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
lea    -0x400(%rbp),%rdi
mov    $0x400,%esi
call   1050 <getcwd@plt>
mov    %eax,%edi
call   1030 <opendir@plt>
mov    %rax,-0x408(%rbp)
mov    -0x408(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x410(%rbp)
cmp    $0x0,%rax
je     11b4 <executeLsCommand+0x64>
mov    -0x410(%rbp),%rax
mov    (%rax),%rsi
lea    0xe58(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    117a <executeLsCommand+0x2a>
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x408(%rbp),%rdi
call   1060 <closedir@plt>
xor    %eax,%eax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<executeLsCommand>:
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rsp,%rdi
mov    $0x400,%esi
call   1060 <getcwd@plt>
mov    %eax,%edi
call   1040 <opendir@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     11ba <executeLsCommand+0x5a>
lea    0xe6b(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%rax),%rsi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
jne    11a0 <executeLsCommand+0x40>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %rbx,%rdi
call   1070 <closedir@plt>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
ret

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

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