#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 {
  int /*<<< orphan*/ d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ close(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ puts(int /*<<< orphan*/);
struct dirent *readdir(int /*<<< orphan*/ *);
int main() {
  DIR *dp;
  struct dirent *ep;
  dp = opendir("./");
  if (dp != NULL) {
    while (ep = readdir(dp)) {
      puts(ep->d_name);
    }
  } else {
    perror("opendir error");
  }
  close(dp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9a(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11ab <main+0x5b>
jmp    117f <main+0x2f>
mov    -0x10(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11a6 <main+0x56>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1030 <puts@plt>
jmp    117f <main+0x2f>
jmp    11b7 <main+0x67>
lea    0xe51(%rip),%rdi
call   1070 <perror@plt>
mov    -0x10(%rbp),%rdi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
lea    0xea8(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1186 <main+0x36>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     1192 <main+0x42>
mov    (%rax),%edi
call   1030 <puts@plt>
jmp    1170 <main+0x20>
lea    0xe76(%rip),%rdi
call   1070 <perror@plt>
mov    %rbx,%rdi
call   1050 <close@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [3 x i8] c"./\00"
@.str.1 = [14 x i8] c"opendir error\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca %struct.dirent*
store i32 0, i32* %1
%4 = call i32* @opendir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %4, i32** %2
%5 = load i32*, i32** %2
%6 = icmp ne i32* %5, null
br i1 %6, label %7, label %18
7: ; preds = %0
br label %8
8: ; preds = %12, %7
%9 = load i32*, i32** %2
%10 = call %struct.dirent* @readdir(i32* %9)
store %struct.dirent* %10, %struct.dirent** %3
%11 = icmp ne %struct.dirent* %10, null
br i1 %11, label %12, label %17
12: ; preds = %8
%13 = load %struct.dirent*, %struct.dirent** %3
%14 = getelementptr inbounds %struct.dirent, %struct.dirent* %13, i32 0, i32 0
%15 = load i32, i32* %14
%16 = call i32 @puts(i32 %15)
br label %8
17: ; preds = %8
br label %20
18: ; preds = %0
%19 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %18, %17
%21 = load i32*, i32** %2
%22 = call i32 @close(i32* %21)
ret i32 0
}

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