#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 /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
char *strerror(int /*<<< orphan*/);
int main(int argv, char *argc[]) {
  DIR *dp;
  struct dirent *dirp;
  if (argv != 2) {
    printf("Usage: %s dirname\n", argc[0]);
    return -1;
  }
  if ((dp = opendir(argc[1])) == NULL) {
    printf("opendir error: %s\n", strerror(errno));
    return -1;
  }
  while ((dirp = readdir(dp)) != NULL) {
    printf("%s\n", dirp->d_name);
  }
  closedir(dp);
  return 0;
}


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     118f <main+0x3f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe82(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1217 <main+0xc7>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11d3 <main+0x83>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %rax,%rsi
lea    0xe51(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1217 <main+0xc7>
jmp    11d8 <main+0x88>
mov    -0x18(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1207 <main+0xb7>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    0xe25(%rip),%rdi
call   1040 <printf@plt>
jmp    11d8 <main+0x88>
mov    -0x18(%rbp),%rdi
call   1050 <closedir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    11a4 <main+0x54>
mov    0x8(%rsi),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     11b0 <main+0x60>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     1198 <main+0x48>
lea    0xea4(%rip),%r14
xchg   %ax,%ax
mov    (%rax),%rsi
mov    %r14,%rdi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
jne    1180 <main+0x30>
mov    %rbx,%rdi
call   1050 <closedir@plt>
xor    %eax,%eax
jmp    11d2 <main+0x82>
mov    (%rsi),%rsi
lea    0xe52(%rip),%rdi
jmp    11c8 <main+0x78>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe4e(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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