#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;
  }
  errno = 0;
  dent = readdir(dirp);
  while (dent != NULL) {
    printf("%llu %s\n", (unsigned long long)dent->d_ino, dent->d_name);
    dent = readdir(dirp);
  }
  if (errno != 0) {
    perror("failed to read directory entry");
    return 3;
  }
  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    1260 <main+0x100>
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    1260 <main+0x100>
mov    0x2dfc(%rip),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1227 <main+0xc7>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
lea    0xe23(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x20(%rbp)
jmp    11f0 <main+0x90>
mov    0x2daa(%rip),%rax
cmpq   $0x0,(%rax)
je     1250 <main+0xf0>
lea    0xdfd(%rip),%rdi
call   1080 <perror@plt>
movl   $0x3,-0x4(%rbp)
jmp    1260 <main+0x100>
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    0x2e56(%rip),%r15
movq   $0x0,(%r15)
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     11bb <main+0x5b>
lea    0xe96(%rip),%r14
nopl   (%rax)
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>
cmpq   $0x0,(%r15)
jne    1200 <main+0xa0>
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    0xe35(%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 = [9 x i8] c"%llu %s\0A\00"
@.str.3 = [31 x i8] c"failed to read directory entry\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
store i64 0, i64* @errno
%23 = load i32*, i32** %6
%24 = call %struct.dirent* @readdir(i32* %23)
store %struct.dirent* %24, %struct.dirent** %7
br label %25
25: ; preds = %28, %22
%26 = load %struct.dirent*, %struct.dirent** %7
%27 = icmp ne %struct.dirent* %26, null
br i1 %27, label %28, label %38
28: ; preds = %25
%29 = load %struct.dirent*, %struct.dirent** %7
%30 = getelementptr inbounds %struct.dirent, %struct.dirent* %29, i32 0, i32 1
%31 = load i64, i64* %30
%32 = load %struct.dirent*, %struct.dirent** %7
%33 = getelementptr inbounds %struct.dirent, %struct.dirent* %32, i32 0, i32 0
%34 = load i32, i32* %33
%35 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i64 %31, i32 %34)
%36 = load i32*, i32** %6
%37 = call %struct.dirent* @readdir(i32* %36)
store %struct.dirent* %37, %struct.dirent** %7
br label %25
38: ; preds = %25
%39 = load i64, i64* @errno
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
store i32 3, i32* %3
br label %46
43: ; preds = %38
%44 = load i32*, i32** %6
%45 = call i32 @closedir(i32* %44)
store i32 0, i32* %3
br label %46
46: ; preds = %43, %41, %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 = [9 x i8] c"%llu %s\0A\00"
@.str.3 = [31 x i8] c"failed to read directory entry\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
store i64 0, i64* @errno
%15 = tail call %struct.dirent* @readdir(i32* %10)
%16 = icmp eq %struct.dirent* %15, null
br i1 %16, label %26, label %17
17: ; preds = %14, %17
%18 = phi %struct.dirent* [ %24, %17 ], [ %15, %14 ]
%19 = getelementptr inbounds %struct.dirent, %struct.dirent* %18, i64 0, i32 1
%20 = load i64, i64* %19
%21 = getelementptr inbounds %struct.dirent, %struct.dirent* %18, i64 0, i32 0
%22 = load i32, i32* %21
%23 = tail call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i64 %20, i32 %22)
%24 = tail call %struct.dirent* @readdir(i32* %10)
%25 = icmp eq %struct.dirent* %24, null
br i1 %25, label %26, label %17
26: ; preds = %17, %14
%27 = load i64, i64* @errno
%28 = icmp eq i64 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %26
%30 = tail call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
br label %33
31: ; preds = %26
%32 = tail call i32 @closedir(i32* %10)
br label %33
33: ; preds = %31, %29, %12, %4
%34 = phi i32 [ 1, %4 ], [ 2, %12 ], [ 3, %29 ], [ 0, %31 ]
ret i32 %34
}