#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 EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int main(int argc, char **argv) {
  if (argc == 1) {
    printf("%s: [directory]\n", argv[0]);
    return EXIT_SUCCESS;
  }
  DIR *dp;
  struct dirent *de;
  dp = opendir(argv[1]);
  if (!dp) {
    perror(argv[0]);
    return EXIT_FAILURE;
  }
  while ((de = readdir(dp))) {
    printf("%s\n", de->d_name);
  }
  closedir(dp);
  return EXIT_SUCCESS;
}


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   $0x1,-0x8(%rbp)
jne    1194 <main+0x44>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe82(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1216 <main+0xc6>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11cd <main+0x7d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1070 <perror@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1216 <main+0xc6>
jmp    11d2 <main+0x82>
mov    -0x18(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1201 <main+0xb1>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    0xe1a(%rip),%rdi
call   1040 <printf@plt>
jmp    11d2 <main+0x82>
mov    -0x18(%rbp),%rdi
call   1050 <closedir@plt>
mov    0x2dc7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
cmp    $0x1,%edi
jne    116d <main+0x1d>
mov    (%r14),%rsi
lea    0xe9a(%rip),%rdi
call   1040 <printf@plt>
jmp    11c0 <main+0x70>
mov    0x8(%r14),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     11d1 <main+0x81>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     11b8 <main+0x68>
lea    0xe7f(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%rax),%rsi
mov    %r14,%rdi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
jne    11a0 <main+0x50>
mov    %rbx,%rdi
call   1050 <closedir@plt>
mov    0x2e11(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    (%r14),%rdi
call   1070 <perror@plt>
mov    0x2e00(%rip),%rax
jmp    11c7 <main+0x77>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [17 x i8] c"%s: [directory]\0A\00"
@EXIT_SUCCESS = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [4 x i8] c"%s\0A\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 eq i32 %8, 1
br i1 %9, label %10, label %16
10: ; preds = %2
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 0
%13 = load i8*, i8** %12
%14 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = load i32, i32* @EXIT_SUCCESS
store i32 %15, i32* %3
br label %43
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32* @opendir(i8* %19)
store i32* %20, i32** %6
%21 = load i32*, i32** %6
%22 = icmp ne i32* %21, null
br i1 %22, label %29, label %23
23: ; preds = %16
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 0
%26 = load i8*, i8** %25
%27 = call i32 @perror(i8* %26)
%28 = load i32, i32* @EXIT_FAILURE
store i32 %28, i32* %3
br label %43
29: ; preds = %16
br label %30
30: ; preds = %34, %29
%31 = load i32*, i32** %6
%32 = call %struct.dirent* @readdir(i32* %31)
store %struct.dirent* %32, %struct.dirent** %7
%33 = icmp ne %struct.dirent* %32, null
br i1 %33, label %34, label %39
34: ; preds = %30
%35 = load %struct.dirent*, %struct.dirent** %7
%36 = getelementptr inbounds %struct.dirent, %struct.dirent* %35, i32 0, i32 0
%37 = load i8*, i8** %36
%38 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %37)
br label %30
39: ; preds = %30
%40 = load i32*, i32** %6
%41 = call i32 @closedir(i32* %40)
%42 = load i32, i32* @EXIT_SUCCESS
store i32 %42, i32* %3
br label %43
43: ; preds = %39, %23, %10
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [17 x i8] c"%s: [directory]\0A\00"
@EXIT_SUCCESS = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 1
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %5)
br label %27
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32* @opendir(i8* %9)
%11 = icmp eq i32* %10, null
br i1 %11, label %15, label %12
12: ; preds = %7
%13 = tail call %struct.dirent* @readdir(i32* %10)
%14 = icmp eq %struct.dirent* %13, null
br i1 %14, label %25, label %18
15: ; preds = %7
%16 = load i8*, i8** %1
%17 = tail call i32 @perror(i8* %16)
br label %27
18: ; preds = %12, %18
%19 = phi %struct.dirent* [ %23, %18 ], [ %13, %12 ]
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %19, i64 0, i32 0
%21 = load i8*, i8** %20
%22 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %21)
%23 = tail call %struct.dirent* @readdir(i32* %10)
%24 = icmp eq %struct.dirent* %23, null
br i1 %24, label %25, label %18
25: ; preds = %18, %12
%26 = tail call i32 @closedir(i32* %10)
br label %27
27: ; preds = %15, %25, %4
%28 = phi i32* [ @EXIT_SUCCESS, %4 ], [ @EXIT_SUCCESS, %25 ], [ @EXIT_FAILURE, %15 ]
%29 = load i32, i32* %28
ret i32 %29
}