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

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ fail(char *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
char *xstrdup(int /*<<< orphan*/);
char *ino2name(ino_t ino, char *dir) {
  DIR *dp;
  struct dirent *ep;
  char *name;
  dp = opendir("..");
  if (!dp)
    fail("cannot read parent directory");
  for (;;) {
    if (!(ep = readdir(dp)))
      fail("cannot find current directory");
    if (ep->d_ino == ino)
      break;
  }
  name = xstrdup(ep->d_name);
  closedir(dp);
  return name;
}


x86 assembly (clang, -O0):
<ino2name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe99(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1187 <ino2name+0x37>
lea    0xe81(%rip),%rdi
call   1030 <fail@plt>
jmp    118c <ino2name+0x3c>
mov    -0x18(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11af <ino2name+0x5f>
lea    0xe76(%rip),%rdi
call   1030 <fail@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
cmp    -0x8(%rbp),%rax
jne    11c5 <ino2name+0x75>
jmp    11ca <ino2name+0x7a>
jmp    118c <ino2name+0x3c>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%edi
call   1050 <xstrdup@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <closedir@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ino2name>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xe9e(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,%r12
test   %rax,%rax
jne    117b <ino2name+0x2b>
lea    0xe8d(%rip),%rdi
call   1030 <fail@plt>
lea    0xe9e(%rip),%r15
jmp    1195 <ino2name+0x45>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
cmp    %r14,(%rbx)
je     11af <ino2name+0x5f>
mov    %r12,%rdi
call   1070 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1190 <ino2name+0x40>
mov    %r15,%rdi
call   1030 <fail@plt>
jmp    1190 <ino2name+0x40>
mov    0x8(%rbx),%edi
call   1050 <xstrdup@plt>
mov    %rax,%rbx
mov    %r12,%rdi
call   1060 <closedir@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.dirent = type { i64, i32 }
@.str = [3 x i8] c"..\00"
@.str.1 = [29 x i8] c"cannot read parent directory\00"
@.str.2 = [30 x i8] c"cannot find current directory\00"
define i8* @ino2name(i64 %0, i8* %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca %struct.dirent*
%7 = alloca i8*
store i64 %0, i64* %3
store i8* %1, i8** %4
%8 = call i32* @opendir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %5
%9 = load i32*, i32** %5
%10 = icmp ne i32* %9, null
br i1 %10, label %13, label %11
11: ; preds = %2
%12 = call i32 @fail(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %11, %2
br label %14
14: ; preds = %27, %13
%15 = load i32*, i32** %5
%16 = call %struct.dirent* @readdir(i32* %15)
store %struct.dirent* %16, %struct.dirent** %6
%17 = icmp ne %struct.dirent* %16, null
br i1 %17, label %20, label %18
18: ; preds = %14
%19 = call i32 @fail(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %18, %14
%21 = load %struct.dirent*, %struct.dirent** %6
%22 = getelementptr inbounds %struct.dirent, %struct.dirent* %21, i32 0, i32 0
%23 = load i64, i64* %22
%24 = load i64, i64* %3
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %20
br label %28
27: ; preds = %20
br label %14
28: ; preds = %26
%29 = load %struct.dirent*, %struct.dirent** %6
%30 = getelementptr inbounds %struct.dirent, %struct.dirent* %29, i32 0, i32 1
%31 = load i32, i32* %30
%32 = call i8* @xstrdup(i32 %31)
store i8* %32, i8** %7
%33 = load i32*, i32** %5
%34 = call i32 @closedir(i32* %33)
%35 = load i8*, i8** %7
ret i8* %35
}

LLVM IR (-O2):
%struct.dirent = type { i64, i32 }
@.str = [3 x i8] c"..\00"
@.str.1 = [29 x i8] c"cannot read parent directory\00"
@.str.2 = [30 x i8] c"cannot find current directory\00"
define i8* @ino2name(i64 %0, i8* nocapture readnone %1) {
%3 = tail call i32* @opendir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @fail(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
br label %7
7: ; preds = %5, %2
br label %8
8: ; preds = %7, %13
%9 = tail call %struct.dirent* @readdir(i32* %3)
%10 = icmp eq %struct.dirent* %9, null
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = tail call i32 @fail(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %13
13: ; preds = %11, %8
%14 = getelementptr inbounds %struct.dirent, %struct.dirent* %9, i64 0, i32 0
%15 = load i64, i64* %14
%16 = icmp eq i64 %15, %0
br i1 %16, label %17, label %8
17: ; preds = %13
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %9, i64 0, i32 1
%19 = load i32, i32* %18
%20 = tail call i8* @xstrdup(i32 %19)
%21 = tail call i32 @closedir(i32* %3)
ret i8* %20
}