#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 /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ listfile(int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  DIR *d;
  struct dirent *info;
  if (argc != 2) {
    fprintf(stderr, "usage: listdir dirname\n");
    exit(1);
  }
  chdir(argv[1]);
  d = opendir(".");
  while ((info = readdir(d)) != NULL) {
    listfile(info->d_name);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     119f <main+0x3f>
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
lea    0xe70(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <chdir@plt>
lea    0xe65(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11e3 <main+0x83>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1070 <listfile@plt>
jmp    11bc <main+0x5c>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
cmp    $0x2,%edi
jne    11aa <main+0x4a>
mov    0x8(%rsi),%rdi
call   1040 <chdir@plt>
lea    0xea2(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,%rbx
mov    %rax,%rdi
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
call   1060 <readdir@plt>
test   %rax,%rax
je     11a6 <main+0x46>
mov    (%rax),%edi
call   1070 <listfile@plt>
mov    %rbx,%rdi
jmp    1190 <main+0x30>
xor    %eax,%eax
pop    %rbx
ret
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
lea    0xe46(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i32 }
@stderr = global i32 0
@.str = [24 x i8] c"usage: listdir dirname\0A\00"
@.str.1 = [2 x i8] c".\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca %struct.dirent*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 2
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32 @chdir(i8* %17)
%19 = call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %19, i32** %6
br label %20
20: ; preds = %24, %14
%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 %29
24: ; preds = %20
%25 = load %struct.dirent*, %struct.dirent** %7
%26 = getelementptr inbounds %struct.dirent, %struct.dirent* %25, i32 0, i32 0
%27 = load i32, i32* %26
%28 = call i32 @listfile(i32 %27)
br label %20
29: ; preds = %20
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@stderr = global i32 0
@.str = [24 x i8] c"usage: listdir dirname\0A\00"
@.str.1 = [2 x i8] c".\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %8, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32 @chdir(i8* %10)
%12 = tail call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = tail call %struct.dirent* @readdir(i32* %12)
%14 = icmp eq %struct.dirent* %13, null
br i1 %14, label %22, label %15
15: ; preds = %8, %15
%16 = phi %struct.dirent* [ %20, %15 ], [ %13, %8 ]
%17 = getelementptr inbounds %struct.dirent, %struct.dirent* %16, i64 0, i32 0
%18 = load i32, i32* %17
%19 = tail call i32 @listfile(i32 %18)
%20 = tail call %struct.dirent* @readdir(i32* %12)
%21 = icmp eq %struct.dirent* %20, null
br i1 %21, label %22, label %15
22: ; preds = %15, %8
ret i32 0
}