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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, scalar_t__);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *inode_to_name(ino_t inode) {
  DIR *dir_ptr;
  struct dirent *dp;
  dir_ptr = opendir(".");
  if (dir_ptr == NULL) {
    perror("opendir() failed");
    exit(EXIT_FAILURE);
  }
  while ((dp = readdir(dir_ptr)) != NULL) {
    if (dp->d_ino == inode) {
      closedir(dir_ptr);
      return dp->d_name;
    }
  }
  fprintf(stderr, "error looking for inode number: %d\n", inode);
  exit(EXIT_FAILURE);
}


x86 assembly (clang, -O0):
<inode_to_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe8d(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a1 <inode_to_name+0x41>
lea    0xe74(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    11a6 <inode_to_name+0x46>
mov    -0x10(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11ea <inode_to_name+0x8a>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
cmp    -0x8(%rbp),%rax
jne    11e5 <inode_to_name+0x85>
mov    -0x10(%rbp),%rdi
call   1040 <closedir@plt>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
add    $0x20,%rsp
pop    %rbp
ret
jmp    11a6 <inode_to_name+0x46>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe15(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<inode_to_name>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
lea    0xe91(%rip),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     11cd <inode_to_name+0x6d>
mov    %rax,%r15
nopl   0x0(%rax)
mov    %r15,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     11a7 <inode_to_name+0x47>
mov    %rax,%rbx
cmp    %r14,(%rax)
jne    1180 <inode_to_name+0x20>
mov    %r15,%rdi
call   1040 <closedir@plt>
mov    0x8(%rbx),%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
lea    0xe5c(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
lea    0xe2e(%rip),%rdi
call   1070 <perror@plt>
jmp    11bf <inode_to_name+0x5f>

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

LLVM IR (-O2):
%struct.dirent = type { i64, i8* }
@.str = [2 x i8] c".\00"
@.str.1 = [17 x i8] c"opendir() failed\00"
@EXIT_FAILURE = global i32 0
@stderr = global i32 0
@.str.2 = [36 x i8] c"error looking for inode number: %d\0A\00"
define i8* @inode_to_name(i64 %0) {
%2 = tail call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %8
4: ; preds = %1
%5 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%6 = load i32, i32* @EXIT_FAILURE
%7 = tail call i32 @exit(i32 %6)
unreachable
8: ; preds = %1, %11
%9 = tail call %struct.dirent* @readdir(i32* %2)
%10 = icmp eq %struct.dirent* %9, null
br i1 %10, label %19, label %11
11: ; preds = %8
%12 = getelementptr inbounds %struct.dirent, %struct.dirent* %9, i64 0, i32 0
%13 = load i64, i64* %12
%14 = icmp eq i64 %13, %0
br i1 %14, label %15, label %8
15: ; preds = %11
%16 = tail call i32 @closedir(i32* %2)
%17 = getelementptr inbounds %struct.dirent, %struct.dirent* %9, i64 0, i32 1
%18 = load i8*, i8** %17
ret i8* %18
19: ; preds = %8
%20 = load i32, i32* @stderr
%21 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i64 %0)
%22 = load i32, i32* @EXIT_FAILURE
%23 = tail call i32 @exit(i32 %22)
unreachable
}