#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*/ closedir(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ puts(int /*<<< orphan*/);
struct dirent *readdir(int /*<<< orphan*/ *);
int main(void) {
  DIR *dir;
  struct dirent *ent;
  if (!(dir = opendir("."))) {
    perror("opendir");
    return 1;
  }
  errno = 0;
  while ((ent = readdir(dir))) {
    puts(ent->d_name);
    errno = 0;
  }
  if (errno) {
    perror("readdir");
    return 1;
  }
  closedir(dir);
  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)
cmp    $0x0,%rax
jne    1191 <main+0x41>
lea    0xe82(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    120d <main+0xbd>
mov    0x2e48(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11d4 <main+0x84>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1030 <puts@plt>
mov    0x2e18(%rip),%rax
movq   $0x0,(%rax)
jmp    119f <main+0x4f>
mov    0x2e05(%rip),%rax
cmpq   $0x0,(%rax)
je     11fd <main+0xad>
lea    0xe1e(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    120d <main+0xbd>
mov    -0x10(%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
lea    0xea5(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     11a5 <main+0x55>
mov    %rax,%rbx
mov    0x2e71(%rip),%r14
nop
movq   $0x0,(%r14)
mov    %rbx,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     118d <main+0x3d>
mov    (%rax),%edi
call   1030 <puts@plt>
jmp    1170 <main+0x20>
cmpq   $0x0,(%r14)
jne    11ae <main+0x5e>
mov    %rbx,%rdi
call   1050 <closedir@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe56(%rip),%rdi
jmp    11b5 <main+0x65>
lea    0xe55(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%eax
jmp    119d <main+0x4d>

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

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