#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*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int readdir_r(int /*<<< orphan*/ *, struct dirent *, struct dirent **);
main() {
  int return_code;
  DIR *dir;
  struct dirent entry;
  struct dirent *result;
  if ((dir = opendir(".")) == NULL)
    perror("opendir() error");
  else {
    for (return_code = readdir_r(dir, &entry, &result);
         result != NULL && return_code == 0;
         return_code = readdir_r(dir, &entry, &result))
      printf("%s\n", entry.d_name);
    if (return_code != 0)
      perror("readdir_r() error");
    closedir(dir);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9a(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    118a <main+0x3a>
lea    0xe82(%rip),%rdi
call   1070 <perror@plt>
jmp    1210 <main+0xc0>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1030 <readdir_r@plt>
mov    %eax,-0x8(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x20(%rbp)
mov    %al,-0x21(%rbp)
je     11b8 <main+0x68>
cmpl   $0x0,-0x8(%rbp)
sete   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    11c8 <main+0x78>
jmp    11f1 <main+0xa1>
mov    -0x18(%rbp),%rsi
lea    0xe3f(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1030 <readdir_r@plt>
mov    %eax,-0x8(%rbp)
jmp    119e <main+0x4e>
cmpl   $0x0,-0x8(%rbp)
je     1207 <main+0xb7>
lea    0xe14(%rip),%rdi
call   1070 <perror@plt>
mov    -0x10(%rbp),%rdi
call   1060 <closedir@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xe9e(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     11f6 <main+0xa6>
mov    %rax,%rbx
lea    0x10(%rsp),%rsi
lea    0x8(%rsp),%rdx
mov    %rax,%rdi
call   1030 <readdir_r@plt>
test   %eax,%eax
sete   %cl
cmpq   $0x0,0x8(%rsp)
je     11dc <main+0x8c>
test   %eax,%eax
jne    11dc <main+0x8c>
lea    0xe75(%rip),%r14
lea    0x10(%rsp),%r15
lea    0x8(%rsp),%r12
nopw   0x0(%rax,%rax,1)
mov    0x10(%rsp),%rsi
mov    %r14,%rdi
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1030 <readdir_r@plt>
test   %eax,%eax
sete   %cl
cmpq   $0x0,0x8(%rsp)
je     11dc <main+0x8c>
test   %eax,%eax
je     11b0 <main+0x60>
test   %cl,%cl
je     1204 <main+0xb4>
mov    %rbx,%rdi
call   1060 <closedir@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe05(%rip),%rdi
call   1070 <perror@plt>
jmp    11e8 <main+0x98>
lea    0xe0b(%rip),%rdi
call   1070 <perror@plt>
jmp    11e0 <main+0x90>

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

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