#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*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  DIR *dp;
  struct dirent *dirp;
  if (argc != 2) {
    printf("a single argument (the directory name) is required");
    return 1;
  }
  if ((dp = opendir(argv[1])) == NULL) {
    printf("can't open %s", argv[1]);
    return 2;
  }
  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     117a <main+0x3a>
lea    0xe99(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11fd <main+0xbd>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11b7 <main+0x77>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe8f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    11fd <main+0xbd>
jmp    11bc <main+0x7c>
mov    -0x18(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11ed <main+0xad>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    0xe60(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11bc <main+0x7c>
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    1187 <main+0x37>
mov    %rsi,%r14
mov    0x8(%rsi),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     11a8 <main+0x58>
mov    %rax,%rbx
nopl   (%rax)
mov    %rbx,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     119c <main+0x4c>
mov    (%rax),%rdi
call   1030 <puts@plt>
jmp    1170 <main+0x20>
lea    0xe72(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%eax
jmp    11bf <main+0x6f>
mov    %rbx,%rdi
call   1060 <closedir@plt>
xor    %eax,%eax
jmp    11bf <main+0x6f>
mov    0x8(%r14),%rsi
lea    0xe80(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x2,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [51 x i8] c"a single argument (the directory name) is required\00"
@.str.1 = [14 x i8] c"can't open %s\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0))
br label %26
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32* @opendir(i8* %8)
%10 = icmp eq i32* %9, null
br i1 %10, label %14, label %11
11: ; preds = %6
%12 = tail call %struct.dirent* @readdir(i32* %9)
%13 = icmp eq %struct.dirent* %12, null
br i1 %13, label %24, label %17
14: ; preds = %6
%15 = load i8*, i8** %7
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %15)
br label %26
17: ; preds = %11, %17
%18 = phi %struct.dirent* [ %22, %17 ], [ %12, %11 ]
%19 = getelementptr inbounds %struct.dirent, %struct.dirent* %18, i64 0, i32 0
%20 = load i8*, i8** %19
%21 = tail call i32 @puts(i8* dereferenceable(1) %20)
%22 = tail call %struct.dirent* @readdir(i32* %9)
%23 = icmp eq %struct.dirent* %22, null
br i1 %23, label %24, label %17
24: ; preds = %17, %11
%25 = tail call i32 @closedir(i32* %9)
br label %26
26: ; preds = %24, %14, %4
%27 = phi i32 [ 1, %4 ], [ 2, %14 ], [ 0, %24 ]
ret i32 %27
}