#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*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int main(int argc, char **argv) {
  DIR *dir_p;
  struct dirent *dirent_p;
  if ((dir_p = opendir(".")) == NULL) {
    perror("opendir");
    exit(-1);
  }
  while ((dirent_p = readdir(dir_p)) != NULL) {
    printf("%s\n", dirent_p->d_name);
  }
  closedir(dir_p);
  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)
lea    0xe83(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11a6 <main+0x46>
lea    0xe6b(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
jmp    11ab <main+0x4b>
mov    -0x18(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11da <main+0x7a>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    0xe3a(%rip),%rdi
call   1040 <printf@plt>
jmp    11ab <main+0x4b>
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
lea    0xe95(%rip),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     11ba <main+0x5a>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     11a8 <main+0x48>
lea    0xe7e(%rip),%r14
nopl   0x0(%rax)
mov    (%rax),%rsi
mov    %r14,%rdi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
jne    1190 <main+0x30>
mov    %rbx,%rdi
call   1050 <closedir@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe41(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

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

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