#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*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ puts(int /*<<< orphan*/);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int main(void) {
  DIR *dir;
  struct dirent *ent;
  if (!(dir = opendir("."))) {
    fprintf(stderr, "Err.(%s) opendir() failed\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  errno = 0;
  while ((ent = readdir(dir))) {
    puts(ent->d_name);
    errno = 0;
  }
  if (errno) {
    fprintf(stderr, "Err.(%s) readdir() failed\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  closedir(dir);
  return (EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe7a(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11d4 <main+0x64>
mov    0x2e40(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e2c(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xe41(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    0x2dfd(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1217 <main+0xa7>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1030 <puts@plt>
mov    0x2dcd(%rip),%rax
movq   $0x0,(%rax)
jmp    11e2 <main+0x72>
mov    0x2dba(%rip),%rax
cmpq   $0x0,(%rax)
je     1263 <main+0xf3>
mov    0x2db1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d9d(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%rdx
lea    0xdcd(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1050 <closedir@plt>
mov    0x2d55(%rip),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
lea    0xe85(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     11ce <main+0x5e>
mov    %rax,%rbx
mov    0x2e49(%rip),%r14
nop
movq   $0x0,(%r14)
mov    %rbx,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     11ad <main+0x3d>
mov    (%rax),%edi
call   1030 <puts@plt>
jmp    1190 <main+0x20>
mov    (%r14),%rdi
test   %rdi,%rdi
jne    11ef <main+0x7f>
mov    %rbx,%rdi
call   1050 <closedir@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2e0b(%rip),%rax
mov    (%rax),%ebx
mov    0x2dfa(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
lea    0xe15(%rip),%rsi
jmp    1204 <main+0x94>
mov    0x2dea(%rip),%rax
mov    (%rax),%ebx
call   1090 <strerror@plt>
lea    0xe19(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [2 x i8] c".\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"Err.(%s) opendir() failed\0A\00"
@errno = global i64 0
@EXIT_FAILURE = global i32 0
@.str.2 = [27 x i8] c"Err.(%s) readdir() failed\0A\00"
@EXIT_SUCCESS = global i32 0
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 %13, label %6
6: ; preds = %0
%7 = load i32, i32* @stderr
%8 = load i64, i64* @errno
%9 = call i8* @strerror(i64 %8)
%10 = call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %9)
%11 = load i32, i32* @EXIT_FAILURE
%12 = call i32 @exit(i32 %11)
unreachable
13: ; preds = %0
store i64 0, i64* @errno
br label %14
14: ; preds = %18, %13
%15 = load i32*, i32** %2
%16 = call %struct.dirent* @readdir(i32* %15)
store %struct.dirent* %16, %struct.dirent** %3
%17 = icmp ne %struct.dirent* %16, null
br i1 %17, label %18, label %23
18: ; preds = %14
%19 = load %struct.dirent*, %struct.dirent** %3
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %19, i32 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @puts(i32 %21)
store i64 0, i64* @errno
br label %14
23: ; preds = %14
%24 = load i64, i64* @errno
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %33
26: ; preds = %23
%27 = load i32, i32* @stderr
%28 = load i64, i64* @errno
%29 = call i8* @strerror(i64 %28)
%30 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %29)
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %23
%34 = load i32*, i32** %2
%35 = call i32 @closedir(i32* %34)
%36 = load i32, i32* @EXIT_SUCCESS
ret i32 %36
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [2 x i8] c".\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"Err.(%s) opendir() failed\0A\00"
@errno = global i64 0
@EXIT_FAILURE = global i32 0
@.str.2 = [27 x i8] c"Err.(%s) readdir() failed\0A\00"
@EXIT_SUCCESS = global i32 0
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 %20, label %13
6: ; preds = %0
%7 = load i32, i32* @stderr
%8 = load i64, i64* @errno
%9 = tail call i8* @strerror(i64 %8)
%10 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %9)
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %3, %13
%14 = phi %struct.dirent* [ %18, %13 ], [ %4, %3 ]
%15 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 0
%16 = load i32, i32* %15
%17 = tail call i32 @puts(i32 %16)
store i64 0, i64* @errno
%18 = tail call %struct.dirent* @readdir(i32* %1)
%19 = icmp eq %struct.dirent* %18, null
br i1 %19, label %20, label %13
20: ; preds = %13, %3
%21 = load i64, i64* @errno
%22 = icmp eq i64 %21, 0
br i1 %22, label %29, label %23
23: ; preds = %20
%24 = load i32, i32* @stderr
%25 = tail call i8* @strerror(i64 %21)
%26 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %25)
%27 = load i32, i32* @EXIT_FAILURE
%28 = tail call i32 @exit(i32 %27)
unreachable
29: ; preds = %20
%30 = tail call i32 @closedir(i32* %1)
%31 = load i32, i32* @EXIT_SUCCESS
ret i32 %31
}