#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 stat {
  int st_ino;
};
typedef int ino_t;

/* Variables and functions */
scalar_t__ ENOENT;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int read_maps(unsigned long, char *);
int stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
ino_t get_addr_inode(void *p) {
  char name[256];
  int ret;
  struct stat sb;
  ret = read_maps((unsigned long)p, name);
  if (ret < 0)
    return ret;
  if (ret == 0) {
    fprintf(stderr,
            "ERR: "
            "Couldn't find address %p in /proc/self/maps\n",
            p);
    return -1;
  }
  if (name[0] != '/')
    return 0;
  ret = stat(name, &sb);
  if (ret < 0) {
    if (errno == ENOENT)
      return 0;
    fprintf(stderr,
            "ERR: "
            "stat failed: %s\n",
            strerror(errno));
    return -1;
  }
  return sb.st_ino;
}


x86 assembly (clang, -O0):
<get_addr_inode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x110(%rbp),%rsi
call   1030 <read_maps@plt>
mov    %eax,-0x114(%rbp)
cmpl   $0x0,-0x114(%rbp)
jge    1180 <get_addr_inode+0x40>
mov    -0x114(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1262 <get_addr_inode+0x122>
cmpl   $0x0,-0x114(%rbp)
jne    11b2 <get_addr_inode+0x72>
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe5f(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1262 <get_addr_inode+0x122>
movsbl -0x110(%rbp),%eax
cmp    $0x2f,%eax
je     11ce <get_addr_inode+0x8e>
movl   $0x0,-0x4(%rbp)
jmp    1262 <get_addr_inode+0x122>
lea    -0x110(%rbp),%rdi
lea    -0x118(%rbp),%rsi
call   1050 <stat@plt>
mov    %eax,-0x114(%rbp)
cmpl   $0x0,-0x114(%rbp)
jge    1259 <get_addr_inode+0x119>
mov    0x2ddd(%rip),%rax
mov    (%rax),%rax
mov    0x2dc3(%rip),%rcx
cmp    (%rcx),%rax
jne    121a <get_addr_inode+0xda>
movl   $0x0,-0x4(%rbp)
jmp    1262 <get_addr_inode+0x122>
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x11c(%rbp)
mov    0x2da8(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    -0x11c(%rbp),%edi
mov    %rax,%rdx
lea    0xdea(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1262 <get_addr_inode+0x122>
mov    -0x118(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_addr_inode>:
push   %r14
push   %rbx
sub    $0x118,%rsp
mov    %rdi,%r14
lea    0x10(%rsp),%rsi
call   1030 <read_maps@plt>
test   %eax,%eax
js     117f <get_addr_inode+0x3f>
je     1183 <get_addr_inode+0x43>
xor    %ebx,%ebx
cmpb   $0x2f,0x10(%rsp)
jne    11a0 <get_addr_inode+0x60>
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1050 <stat@plt>
test   %eax,%eax
js     11ad <get_addr_inode+0x6d>
mov    0x8(%rsp),%ebx
jmp    11a0 <get_addr_inode+0x60>
mov    %eax,%ebx
jmp    11a0 <get_addr_inode+0x60>
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
lea    0xe6d(%rip),%rsi
mov    %r14,%rdx
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2e24(%rip),%rax
mov    (%rax),%rdi
mov    0x2e0a(%rip),%rax
cmp    (%rax),%rdi
je     11a0 <get_addr_inode+0x60>
mov    0x2e16(%rip),%rax
mov    (%rax),%ebx
call   1060 <strerror@plt>
lea    0xe5a(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
jmp    1196 <get_addr_inode+0x56>

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [50 x i8] c"ERR: Couldn't find address %p in /proc/self/maps\0A\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.1 = [22 x i8] c"ERR: stat failed: %s\0A\00"
define i32 @get_addr_inode(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [256 x i8]
%5 = alloca i32
%6 = alloca %struct.stat
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = ptrtoint i8* %7 to i64
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%10 = call i32 @read_maps(i64 %8, i8* %9)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = load i32, i32* %5
store i32 %14, i32* %2
br label %46
15: ; preds = %1
%16 = load i32, i32* %5
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = load i8*, i8** %3
%21 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i8* %20)
store i32 -1, i32* %2
br label %46
22: ; preds = %15
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp ne i32 %25, 47
br i1 %26, label %27, label %28
27: ; preds = %22
store i32 0, i32* %2
br label %46
28: ; preds = %22
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%30 = call i32 @stat(i8* %29, %struct.stat* %6)
store i32 %30, i32* %5
%31 = load i32, i32* %5
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %43
33: ; preds = %28
%34 = load i64, i64* @errno
%35 = load i64, i64* @ENOENT
%36 = icmp eq i64 %34, %35
br i1 %36, label %37, label %38
37: ; preds = %33
store i32 0, i32* %2
br label %46
38: ; preds = %33
%39 = load i32, i32* @stderr
%40 = load i64, i64* @errno
%41 = call i8* @strerror(i64 %40)
%42 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %41)
store i32 -1, i32* %2
br label %46
43: ; preds = %28
%44 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%45 = load i32, i32* %44
store i32 %45, i32* %2
br label %46
46: ; preds = %43, %38, %37, %27, %18, %13
%47 = load i32, i32* %2
ret i32 %47
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [50 x i8] c"ERR: Couldn't find address %p in /proc/self/maps\0A\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.1 = [22 x i8] c"ERR: stat failed: %s\0A\00"
define i32 @get_addr_inode(i8* %0) {
%2 = alloca [256 x i8]
%3 = alloca %struct.stat
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%5 = bitcast %struct.stat* %3 to i8*
%6 = ptrtoint i8* %0 to i64
%7 = call i32 @read_maps(i64 %6, i8* %4)
%8 = icmp slt i32 %7, 0
br i1 %8, label %31, label %9
9: ; preds = %1
%10 = icmp eq i32 %7, 0
br i1 %10, label %11, label %14
11: ; preds = %9
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %31
14: ; preds = %9
%15 = load i8, i8* %4
%16 = icmp eq i8 %15, 47
br i1 %16, label %17, label %31
17: ; preds = %14
%18 = call i32 @stat(i8* %4, %struct.stat* %3)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %28
20: ; preds = %17
%21 = load i64, i64* @errno
%22 = load i64, i64* @ENOENT
%23 = icmp eq i64 %21, %22
br i1 %23, label %31, label %24
24: ; preds = %20
%25 = load i32, i32* @stderr
%26 = call i8* @strerror(i64 %21)
%27 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %26)
br label %31
28: ; preds = %17
%29 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%30 = load i32, i32* %29
br label %31
31: ; preds = %20, %14, %1, %28, %24, %11
%32 = phi i32 [ -1, %11 ], [ -1, %24 ], [ %30, %28 ], [ %7, %1 ], [ 0, %14 ], [ 0, %20 ]
ret i32 %32
}