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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, long);
scalar_t__ readdir_r(int /*<<< orphan*/ *, struct dirent *, struct dirent **);
int main(void) {
  DIR *ds = opendir(".");
  if (ds == NULL) {
    perror("opendir");
    exit(EXIT_FAILURE);
  }
  struct dirent *result;
  struct dirent currentEntry;
  while (1) {
    if (readdir_r(ds, &currentEntry, &result) != 0) {
      perror("readdir_r");
      exit(EXIT_FAILURE);
    }
    if (result == NULL)
      break;
    printf("%s is at inode %ld\n", currentEntry.d_name,
           (long)currentEntry.d_ino);
  }
  closedir(ds);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe8a(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a4 <main+0x44>
lea    0xe71(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    11a9 <main+0x49>
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
lea    -0x18(%rbp),%rdx
call   1030 <readdir_r@plt>
cmp    $0x0,%rax
je     11de <main+0x7e>
lea    0xe3f(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jne    11ee <main+0x8e>
jmp    1207 <main+0xa7>
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0xe17(%rip),%rdi
call   1050 <printf@plt>
jmp    11a9 <main+0x49>
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    0xe8e(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     120c <main+0xac>
mov    %rax,%rbx
lea    0x8(%rsp),%rsi
mov    %rsp,%rdx
mov    %rax,%rdi
call   1030 <readdir_r@plt>
test   %rax,%rax
jne    11dc <main+0x7c>
lea    0xe75(%rip),%r14
lea    0x8(%rsp),%r15
mov    %rsp,%r12
nopw   0x0(%rax,%rax,1)
cmpq   $0x0,(%rsp)
je     11f6 <main+0x96>
mov    0x8(%rsp),%rsi
mov    0x10(%rsp),%rdx
mov    %r14,%rdi
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1030 <readdir_r@plt>
test   %rax,%rax
je     11b0 <main+0x50>
lea    0xe27(%rip),%rdi
call   1070 <perror@plt>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    %rbx,%rdi
call   1060 <closedir@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xdef(%rip),%rdi
jmp    11e3 <main+0x83>

LLVM IR (-O0):
%struct.dirent = type { i8*, i64 }
@.str = [2 x i8] c".\00"
@.str.1 = [8 x i8] c"opendir\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [10 x i8] c"readdir_r\00"
@.str.3 = [20 x i8] c"%s is at inode %ld\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca %struct.dirent*
%4 = alloca %struct.dirent
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
%6 = load i32*, i32** %2
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = call i32 @exit(i32 %10)
unreachable
12: ; preds = %0
br label %13
13: ; preds = %12, %25
%14 = load i32*, i32** %2
%15 = call i64 @readdir_r(i32* %14, %struct.dirent* %4, %struct.dirent** %3)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %13
%18 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %13
%22 = load %struct.dirent*, %struct.dirent** %3
%23 = icmp eq %struct.dirent* %22, null
br i1 %23, label %24, label %25
24: ; preds = %21
br label %31
25: ; preds = %21
%26 = getelementptr inbounds %struct.dirent, %struct.dirent* %4, i32 0, i32 0
%27 = load i8*, i8** %26
%28 = getelementptr inbounds %struct.dirent, %struct.dirent* %4, i32 0, i32 1
%29 = load i64, i64* %28
%30 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %27, i64 %29)
br label %13
31: ; preds = %24
%32 = load i32*, i32** %2
%33 = call i32 @closedir(i32* %32)
%34 = load i32, i32* %1
ret i32 %34
}

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