#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*/ closedir(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, unsigned long long, int /*<<< orphan*/);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  DIR *dirp;
  struct dirent *dent;
  if (argc != 2) {
    fprintf(stderr, "Usage: readdir_inode dir\n");
    return 1;
  }
  dirp = opendir(argv[1]);
  if (dirp == NULL) {
    perror("failed to open directory");
    return 2;
  }
  dent = readdir(dirp);
  while (dent != NULL) {
    if (errno != 0) {
      perror("failed to read directory entry");
      return 3;
    }
    printf("%llu %s\n", (unsigned long long)dent->d_ino, dent->d_name);
    dent = readdir(dirp);
  }
  closedir(dirp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11a1 <main+0x41>
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
lea    0xe70(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1252 <main+0xf2>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d5 <main+0x75>
lea    0xe56(%rip),%rdi
call   1080 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1252 <main+0xf2>
mov    -0x18(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1242 <main+0xe2>
mov    0x2de4(%rip),%rax
cmpq   $0x0,(%rax)
je     1216 <main+0xb6>
lea    0xe2e(%rip),%rdi
call   1080 <perror@plt>
movl   $0x3,-0x4(%rbp)
jmp    1252 <main+0xf2>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
lea    0xe27(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x20(%rbp)
jmp    11e2 <main+0x82>
mov    -0x18(%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   %r15
push   %r14
push   %rbx
cmp    $0x2,%edi
jne    11cd <main+0x6d>
mov    0x8(%rsi),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     11ed <main+0x8d>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     11c1 <main+0x61>
mov    0x2e49(%rip),%r15
lea    0xebc(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
cmpq   $0x0,(%r15)
jne    1200 <main+0xa0>
mov    0x8(%rax),%rsi
mov    (%rax),%edx
mov    %r14,%rdi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
jne    11a0 <main+0x40>
mov    %rbx,%rdi
call   1050 <closedir@plt>
xor    %eax,%eax
jmp    11e7 <main+0x87>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
lea    0xe23(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe26(%rip),%rdi
call   1080 <perror@plt>
mov    $0x2,%eax
jmp    11e7 <main+0x87>
lea    0xe2c(%rip),%rdi
call   1080 <perror@plt>
mov    $0x3,%eax
jmp    11e7 <main+0x87>

LLVM IR (-O0):
%struct.dirent = type { i32, i64 }
@stderr = global i32 0
@.str = [26 x i8] c"Usage: readdir_inode dir\0A\00"
@.str.1 = [25 x i8] c"failed to open directory\00"
@errno = global i64 0
@.str.2 = [31 x i8] c"failed to read directory entry\00"
@.str.3 = [9 x i8] c"%llu %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca %struct.dirent*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 2
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %46
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i32* @opendir(i8* %16)
store i32* %17, i32** %6
%18 = load i32*, i32** %6
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %22
20: ; preds = %13
%21 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %3
br label %46
22: ; preds = %13
%23 = load i32*, i32** %6
%24 = call %struct.dirent* @readdir(i32* %23)
store %struct.dirent* %24, %struct.dirent** %7
br label %25
25: ; preds = %33, %22
%26 = load %struct.dirent*, %struct.dirent** %7
%27 = icmp ne %struct.dirent* %26, null
br i1 %27, label %28, label %43
28: ; preds = %25
%29 = load i64, i64* @errno
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
store i32 3, i32* %3
br label %46
33: ; preds = %28
%34 = load %struct.dirent*, %struct.dirent** %7
%35 = getelementptr inbounds %struct.dirent, %struct.dirent* %34, i32 0, i32 1
%36 = load i64, i64* %35
%37 = load %struct.dirent*, %struct.dirent** %7
%38 = getelementptr inbounds %struct.dirent, %struct.dirent* %37, i32 0, i32 0
%39 = load i32, i32* %38
%40 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i64 %36, i32 %39)
%41 = load i32*, i32** %6
%42 = call %struct.dirent* @readdir(i32* %41)
store %struct.dirent* %42, %struct.dirent** %7
br label %25
43: ; preds = %25
%44 = load i32*, i32** %6
%45 = call i32 @closedir(i32* %44)
store i32 0, i32* %3
br label %46
46: ; preds = %43, %31, %20, %10
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
%struct.dirent = type { i32, i64 }
@stderr = global i32 0
@.str = [26 x i8] c"Usage: readdir_inode dir\0A\00"
@.str.1 = [25 x i8] c"failed to open directory\00"
@errno = global i64 0
@.str.2 = [31 x i8] c"failed to read directory entry\00"
@.str.3 = [9 x i8] c"%llu %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %33
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32* @opendir(i8* %9)
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %14
12: ; preds = %7
%13 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %33
14: ; preds = %7
%15 = tail call %struct.dirent* @readdir(i32* %10)
%16 = icmp eq %struct.dirent* %15, null
br i1 %16, label %31, label %17
17: ; preds = %14, %23
%18 = phi %struct.dirent* [ %29, %23 ], [ %15, %14 ]
%19 = load i64, i64* @errno
%20 = icmp eq i64 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %17
%22 = tail call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
br label %33
23: ; preds = %17
%24 = getelementptr inbounds %struct.dirent, %struct.dirent* %18, i64 0, i32 1
%25 = load i64, i64* %24
%26 = getelementptr inbounds %struct.dirent, %struct.dirent* %18, i64 0, i32 0
%27 = load i32, i32* %26
%28 = tail call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i64 %25, i32 %27)
%29 = tail call %struct.dirent* @readdir(i32* %10)
%30 = icmp eq %struct.dirent* %29, null
br i1 %30, label %31, label %17
31: ; preds = %23, %14
%32 = tail call i32 @closedir(i32* %10)
br label %33
33: ; preds = %31, %21, %12, %4
%34 = phi i32 [ 1, %4 ], [ 2, %12 ], [ 3, %21 ], [ 0, %31 ]
ret i32 %34
}