#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*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, scalar_t__);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strncpy(char *, int /*<<< orphan*/, int);
void inum_to_name(ino_t inode_to_find, char *namebuf, int buflen) {
  DIR *dir_ptr;
  struct dirent *direntp;
  dir_ptr = opendir(".");
  if (dir_ptr == NULL) {
    perror(".");
    exit(1);
  }
  while ((direntp = readdir(dir_ptr)) != NULL) {
    if (direntp->d_ino == inode_to_find) {
      strncpy(namebuf, direntp->d_name, buflen);
      namebuf[buflen - 1] = '\0';
      closedir(dir_ptr);
      return;
    }
  }
  fprintf(stderr, "error looking for inum %d\n", inode_to_find);
  exit(1);
}


x86 assembly (clang, -O0):
<inum_to_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    0xe76(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b4 <inum_to_name+0x44>
lea    0xe5b(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    11b9 <inum_to_name+0x49>
mov    -0x20(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1219 <inum_to_name+0xa9>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
cmp    -0x8(%rbp),%rax
jne    1214 <inum_to_name+0xa4>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%esi
mov    -0x14(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
call   1050 <closedir@plt>
add    $0x30,%rsp
pop    %rbp
ret
jmp    11b9 <inum_to_name+0x49>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdd5(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<inum_to_name>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xe79(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     11fd <inum_to_name+0x8d>
mov    %rax,%rbp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbp,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     11db <inum_to_name+0x6b>
cmp    %rbx,(%rax)
jne    11a0 <inum_to_name+0x30>
mov    0x8(%rax),%esi
mov    %r14,%rdi
mov    %r15d,%edx
call   1030 <strncpy@plt>
movslq %r15d,%rax
movb   $0x0,-0x1(%rax,%r14,1)
mov    %rbp,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <closedir@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
lea    0xe17(%rip),%rsi
mov    %rbx,%rdx
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xdfc(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

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

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