#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*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  DIR *dp;
  struct dirent *dirp;
  if (argc != 2) {
    fprintf(stderr, "usage: %s dir_name\n", argv[0]);
    exit(1);
  }
  if ((dp = opendir(argv[1])) == NULL) {
    fprintf(stderr, "can't open '%s'\n", argv[1]);
    exit(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     11a6 <main+0x46>
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe69(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11e8 <main+0x88>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe3b(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    11ed <main+0x8d>
mov    -0x18(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     121c <main+0xbc>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    0xe13(%rip),%rdi
call   1040 <printf@plt>
jmp    11ed <main+0x8d>
mov    -0x18(%rbp),%rdi
call   1050 <closedir@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
cmp    $0x2,%edi
jne    11ca <main+0x6a>
mov    0x8(%r14),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     11df <main+0x7f>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     11b8 <main+0x58>
lea    0xe94(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%rax),%rsi
mov    %r14,%rdi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
jne    11a0 <main+0x40>
mov    %rbx,%rdi
call   1050 <closedir@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xe23(%rip),%rsi
jmp    11f3 <main+0x93>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r14),%rdx
lea    0xe21(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

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

LLVM IR (-O2):
%struct.dirent = type { i8* }
@stderr = global i32 0
@.str = [20 x i8] c"usage: %s dir_name\0A\00"
@.str.1 = [17 x i8] c"can't open '%s'\0A\00"
@.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 %9, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32* @opendir(i8* %11)
%13 = icmp eq i32* %12, null
br i1 %13, label %17, label %14
14: ; preds = %9
%15 = tail call %struct.dirent* @readdir(i32* %12)
%16 = icmp eq %struct.dirent* %15, null
br i1 %16, label %29, label %22
17: ; preds = %9
%18 = load i32, i32* @stderr
%19 = load i8*, i8** %10
%20 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %19)
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %14, %22
%23 = phi %struct.dirent* [ %27, %22 ], [ %15, %14 ]
%24 = getelementptr inbounds %struct.dirent, %struct.dirent* %23, i64 0, i32 0
%25 = load i8*, i8** %24
%26 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %25)
%27 = tail call %struct.dirent* @readdir(i32* %12)
%28 = icmp eq %struct.dirent* %27, null
br i1 %28, label %29, label %22
29: ; preds = %22, %14
%30 = tail call i32 @closedir(i32* %12)
ret i32 0
}