#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*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int main(int argc, char **argv) {
  struct dirent *direntry;
  DIR *dir = opendir(argv[1]);
  if (dir == NULL) {
    printf("opendir() failed\n");
    return 1;
  }
  while ((direntry = readdir(dir)) != NULL) {
    printf("%s\n", direntry->d_name);
  }
  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)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    117c <main+0x4c>
lea    0xe97(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11b9 <main+0x89>
jmp    1181 <main+0x51>
mov    -0x20(%rbp),%rdi
call   1050 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11b2 <main+0x82>
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
lea    0xe6c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1181 <main+0x51>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    0x8(%rsi),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     1177 <main+0x47>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <readdir@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     1188 <main+0x58>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    (%rax),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1050 <readdir@plt>
test   %rax,%rax
jne    1160 <main+0x30>
jmp    1188 <main+0x58>
lea    0xe82(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [18 x i8] c"opendir() failed\0A\00"
@.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 %struct.dirent*
%7 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i8**, i8*** %5
%9 = getelementptr inbounds i8*, i8** %8, i64 1
%10 = load i8*, i8** %9
%11 = call i32* @opendir(i8* %10)
store i32* %11, i32** %7
%12 = load i32*, i32** %7
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %27
16: ; preds = %2
br label %17
17: ; preds = %21, %16
%18 = load i32*, i32** %7
%19 = call %struct.dirent* @readdir(i32* %18)
store %struct.dirent* %19, %struct.dirent** %6
%20 = icmp ne %struct.dirent* %19, null
br i1 %20, label %21, label %26
21: ; preds = %17
%22 = load %struct.dirent*, %struct.dirent** %6
%23 = getelementptr inbounds %struct.dirent, %struct.dirent* %22, i32 0, i32 0
%24 = load i8*, i8** %23
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %24)
br label %17
26: ; preds = %17
store i32 0, i32* %3
br label %27
27: ; preds = %26, %14
%28 = load i32, i32* %3
ret i32 %28
}

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