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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, long long, int /*<<< orphan*/);
int readdir_r(int /*<<< orphan*/ *, struct dirent *, struct dirent **);
int main(int argc, char **argv) {
  char *d_to_open = ".";
  DIR *dstream = opendir(d_to_open);
  if (dstream == NULL) {
    perror("opening directory failed");
    exit(2);
  }
  struct dirent d;
  struct dirent *result;
  for (;;) {
    int ret = readdir_r(dstream, &d, &result);
    if (ret != 0) {
      perror("readdir_r error");
      exit(3);
    }
    if (!result) {
      break;
    }
    printf("inode: %lld, name: %s\n", (long long)d.d_ino, d.d_name);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe93(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    119f <main+0x4f>
lea    0xe72(%rip),%rdi
call   1060 <perror@plt>
mov    $0x2,%edi
call   1070 <exit@plt>
jmp    11a4 <main+0x54>
mov    -0x20(%rbp),%rdi
lea    -0x30(%rbp),%rsi
lea    -0x38(%rbp),%rdx
call   1030 <readdir_r@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
je     11d8 <main+0x88>
lea    0xe52(%rip),%rdi
call   1060 <perror@plt>
mov    $0x3,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x38(%rbp)
jne    11e8 <main+0x98>
jmp    1200 <main+0xb0>
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%edx
lea    0xe35(%rip),%rdi
call   1050 <printf@plt>
jmp    11a4 <main+0x54>
xor    %eax,%eax
add    $0x40,%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     11ee <main+0x9e>
mov    %rax,%rbx
lea    0x8(%rsp),%rsi
mov    %rsp,%rdx
mov    %rax,%rdi
call   1030 <readdir_r@plt>
test   %eax,%eax
jne    11ca <main+0x7a>
lea    0xe9d(%rip),%r14
lea    0x8(%rsp),%r15
mov    %rsp,%r12
cs nopw 0x0(%rax,%rax,1)
cmpq   $0x0,(%rsp)
je     11e0 <main+0x90>
mov    0x10(%rsp),%rsi
mov    0x8(%rsp),%edx
mov    %r14,%rdi
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1030 <readdir_r@plt>
test   %eax,%eax
je     11a0 <main+0x50>
lea    0xe4a(%rip),%rdi
call   1060 <perror@plt>
mov    $0x3,%edi
call   1070 <exit@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe0d(%rip),%rdi
call   1060 <perror@plt>
mov    $0x2,%edi
call   1070 <exit@plt>

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

LLVM IR (-O2):
%struct.dirent = type { i32, i64 }
@.str = [2 x i8] c".\00"
@.str.1 = [25 x i8] c"opening directory failed\00"
@.str.2 = [16 x i8] c"readdir_r error\00"
@.str.3 = [23 x i8] c"inode: %lld, name: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.dirent
%4 = alloca %struct.dirent*
%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 %10
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%9 = tail call i32 @exit(i32 2)
unreachable
10: ; preds = %2
%11 = bitcast %struct.dirent* %3 to i8*
%12 = bitcast %struct.dirent** %4 to i8*
%13 = call i32 @readdir_r(i32* %5, %struct.dirent* %3, %struct.dirent** %4)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = getelementptr inbounds %struct.dirent, %struct.dirent* %3, i64 0, i32 1
%17 = getelementptr inbounds %struct.dirent, %struct.dirent* %3, i64 0, i32 0
br label %21
18: ; preds = %24, %10
%19 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%20 = call i32 @exit(i32 3)
unreachable
21: ; preds = %15, %24
%22 = load %struct.dirent*, %struct.dirent** %4
%23 = icmp eq %struct.dirent* %22, null
br i1 %23, label %30, label %24
24: ; preds = %21
%25 = load i64, i64* %16
%26 = load i32, i32* %17
%27 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i64 %25, i32 %26)
%28 = call i32 @readdir_r(i32* %5, %struct.dirent* %3, %struct.dirent** %4)
%29 = icmp eq i32 %28, 0
br i1 %29, label %21, label %18
30: ; preds = %21
ret i32 0
}