#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 closedir(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int main() {
  DIR *this_dir = opendir(".");
  errno = 0;
  struct dirent *dir_entry = NULL;
  while (1) {
    if (dir_entry = readdir(this_dir)) {
      printf("dir entry: %s \n", dir_entry->d_name);
    } else if (errno == 0) {
      break;
    } else {
      perror("readdir");
    }
  }
  const int result = closedir(this_dir);
  if (result != 0)
    perror("closedir");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9a(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x10(%rbp)
mov    0x2e6a(%rip),%rax
movq   $0x0,(%rax)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11b4 <main+0x64>
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
lea    0xe58(%rip),%rdi
call   1040 <printf@plt>
jmp    11db <main+0x8b>
mov    0x2e25(%rip),%rax
cmpq   $0x0,(%rax)
jne    11ca <main+0x7a>
jmp    11e0 <main+0x90>
lea    0xe41(%rip),%rdi
call   1070 <perror@plt>
jmp    11db <main+0x8b>
jmp    1185 <main+0x35>
mov    -0x10(%rbp),%rdi
call   1050 <closedir@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1202 <main+0xb2>
lea    0xe1d(%rip),%rdi
call   1070 <perror@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
lea    0xea1(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,%rbx
mov    0x2e72(%rip),%r12
movq   $0x0,(%r12)
lea    0xe95(%rip),%r14
lea    0xe7e(%rip),%r15
jmp    119b <main+0x4b>
cs nopw 0x0(%rax,%rax,1)
mov    (%rax),%rsi
mov    %r15,%rdi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
jne    1190 <main+0x40>
cmpq   $0x0,(%r12)
je     11b9 <main+0x69>
mov    %r14,%rdi
call   1070 <perror@plt>
jmp    119b <main+0x4b>
mov    %rbx,%rdi
call   1050 <closedir@plt>
test   %eax,%eax
jne    11d3 <main+0x83>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe40(%rip),%rdi
call   1070 <perror@plt>
jmp    11c5 <main+0x75>

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

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