#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int BUFSIZ;
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
scalar_t__ getline(char **, size_t *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int sscanf(char *, char *, unsigned long *, unsigned long *, char *,
           unsigned long *, char *, unsigned long *, char *);
FILE *seek_to_smaps_entry(unsigned long addr) {
  FILE *file;
  char *line = NULL;
  size_t size = 0;
  unsigned long start, end;
  char perms[5];
  unsigned long offset;
  char dev[32];
  unsigned long inode;
  char path[BUFSIZ];
  file = fopen("/proc/self/smaps", "r");
  if (!file) {
    perror("fopen smaps");
    _exit(1);
  }
  while (getline(&line, &size, file) > 0) {
    if (sscanf(line, "%lx-%lx %s %lx %s %lu %s\n", &start, &end, perms, &offset,
               dev, &inode, path) < 6)
      goto next;
    if (start <= addr && addr < end)
      goto out;
  next:
    free(line);
    line = NULL;
    size = 0;
  }
  fclose(file);
  file = NULL;
out:
  free(line);
  return file;
}


x86 assembly (clang, -O0):
<seek_to_smaps_entry>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x70(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x80(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x78(%rbp)
lea    0xe3e(%rip),%rdi
lea    0xe48(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11f3 <seek_to_smaps_entry+0x83>
lea    0xe2f(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1040 <_exit@plt>
jmp    11f8 <seek_to_smaps_entry+0x88>
mov    -0x10(%rbp),%rdx
lea    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1090 <getline@plt>
cmp    $0x0,%rax
jle    12a7 <seek_to_smaps_entry+0x137>
mov    -0x80(%rbp),%rax
mov    -0x18(%rbp),%rdi
lea    -0x35(%rbp),%r8
lea    -0x60(%rbp),%r11
lea    0xdf5(%rip),%rsi
lea    -0x28(%rbp),%rdx
lea    -0x30(%rbp),%rcx
lea    -0x40(%rbp),%r9
lea    -0x68(%rbp),%r10
sub    $0x20,%rsp
mov    %r11,(%rsp)
mov    %r10,0x8(%rsp)
mov    %rax,0x10(%rsp)
call   1060 <sscanf@plt>
add    $0x20,%rsp
cmp    $0x6,%eax
jge    1263 <seek_to_smaps_entry+0xf3>
jmp    1289 <seek_to_smaps_entry+0x119>
mov    -0x28(%rbp),%rax
cmp    -0x8(%rbp),%rax
ja     1284 <seek_to_smaps_entry+0x114>
mov    -0x8(%rbp),%rax
cmp    -0x30(%rbp),%rax
jae    1284 <seek_to_smaps_entry+0x114>
jmp    12b8 <seek_to_smaps_entry+0x148>
jmp    1289 <seek_to_smaps_entry+0x119>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
jmp    11f8 <seek_to_smaps_entry+0x88>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x10(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
mov    -0x70(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<seek_to_smaps_entry>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
mov    %rdi,%r14
mov    %rsp,-0x40(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r15
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
lea    0xe47(%rip),%rdi
lea    0xe51(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     129f <seek_to_smaps_entry+0x12f>
mov    %rax,%rbx
lea    -0x30(%rbp),%rdi
lea    -0x38(%rbp),%rsi
mov    %rax,%rdx
call   1090 <getline@plt>
test   %rax,%rax
jle    1276 <seek_to_smaps_entry+0x106>
lea    -0x68(%rbp),%r12
lea    -0x38(%rbp),%r13
jmp    122d <seek_to_smaps_entry+0xbd>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
lea    -0x30(%rbp),%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
call   1090 <getline@plt>
test   %rax,%rax
jle    1276 <seek_to_smaps_entry+0x106>
mov    -0x30(%rbp),%rdi
sub    $0x8,%rsp
lea    0xde3(%rip),%rsi
lea    -0x50(%rbp),%rdx
lea    -0x48(%rbp),%rcx
lea    -0x55(%rbp),%r8
mov    %r12,%r9
push   %r15
lea    -0x60(%rbp),%rax
push   %rax
lea    -0x90(%rbp),%rax
push   %rax
call   1060 <sscanf@plt>
add    $0x20,%rsp
cmp    $0x6,%eax
jl     1200 <seek_to_smaps_entry+0x90>
cmp    %r14,-0x50(%rbp)
ja     1200 <seek_to_smaps_entry+0x90>
cmp    %r14,-0x48(%rbp)
jbe    1200 <seek_to_smaps_entry+0x90>
jmp    1280 <seek_to_smaps_entry+0x110>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %ebx,%ebx
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rsp
mov    %rbx,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd6d(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1040 <_exit@plt>

LLVM IR (-O0):
@BUFSIZ = global i32 0
@.str = [17 x i8] c"/proc/self/smaps\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [12 x i8] c"fopen smaps\00"
@.str.3 = [26 x i8] c"%lx-%lx %s %lx %s %lu %s\0A\00"
define i32* @seek_to_smaps_entry(i64 %0) {
%2 = alloca i64
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca [5 x i8]
%9 = alloca i64
%10 = alloca [32 x i8]
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i64
store i64 %0, i64* %2
store i8* null, i8** %4
store i64 0, i64* %5
%14 = load i32, i32* @BUFSIZ
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %12
%17 = alloca i8, i64 %15
store i64 %15, i64* %13
%18 = call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %18, i32** %3
%19 = load i32*, i32** %3
%20 = icmp ne i32* %19, null
br i1 %20, label %24, label %21
21: ; preds = %1
%22 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%23 = call i32 @_exit(i32 1)
unreachable
24: ; preds = %1
br label %25
25: ; preds = %46, %24
%26 = load i32*, i32** %3
%27 = call i64 @getline(i8** %4, i64* %5, i32* %26)
%28 = icmp sgt i64 %27, 0
br i1 %28, label %29, label %49
29: ; preds = %25
%30 = load i8*, i8** %4
%31 = getelementptr inbounds [5 x i8], [5 x i8]* %8, i64 0, i64 0
%32 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
%33 = call i32 @sscanf(i8* %30, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i64* %6, i64* %7, i8* %31, i64* %9, i8* %32, i64* %11, i8* %17)
%34 = icmp slt i32 %33, 6
br i1 %34, label %35, label %36
35: ; preds = %29
br label %46
36: ; preds = %29
%37 = load i64, i64* %6
%38 = load i64, i64* %2
%39 = icmp ule i64 %37, %38
br i1 %39, label %40, label %45
40: ; preds = %36
%41 = load i64, i64* %2
%42 = load i64, i64* %7
%43 = icmp ult i64 %41, %42
br i1 %43, label %44, label %45
44: ; preds = %40
br label %52
45: ; preds = %40, %36
br label %46
46: ; preds = %45, %35
%47 = load i8*, i8** %4
%48 = call i32 @free(i8* %47)
store i8* null, i8** %4
store i64 0, i64* %5
br label %25
49: ; preds = %25
%50 = load i32*, i32** %3
%51 = call i32 @fclose(i32* %50)
store i32* null, i32** %3
br label %52
52: ; preds = %49, %44
%53 = load i8*, i8** %4
%54 = call i32 @free(i8* %53)
%55 = load i32*, i32** %3
%56 = load i8*, i8** %12
ret i32* %55
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@.str = [17 x i8] c"/proc/self/smaps\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [12 x i8] c"fopen smaps\00"
@.str.3 = [26 x i8] c"%lx-%lx %s %lx %s %lu %s\0A\00"
define i32* @seek_to_smaps_entry(i64 %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca [5 x i8]
%7 = alloca i64
%8 = alloca [32 x i8]
%9 = alloca i64
%10 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%11 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%12 = bitcast i64* %4 to i8*
%13 = bitcast i64* %5 to i8*
%14 = getelementptr inbounds [5 x i8], [5 x i8]* %6, i64 0, i64 0
%15 = bitcast i64* %7 to i8*
%16 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%17 = bitcast i64* %9 to i8*
%18 = load i32, i32* @BUFSIZ
%19 = zext i32 %18 to i64
%20 = tail call i8* @llvm.stacksave()
%21 = alloca i8, i64 %19
%22 = tail call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%23 = icmp eq i32* %22, null
br i1 %23, label %27, label %24
24: ; preds = %1
%25 = call i64 @getline(i8** %2, i64* %3, i32* %22)
%26 = icmp sgt i64 %25, 0
br i1 %26, label %30, label %45
27: ; preds = %1
%28 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%29 = tail call i32 @_exit(i32 1)
unreachable
30: ; preds = %24, %40
%31 = load i8*, i8** %2
%32 = call i32 @sscanf(i8* %31, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i64* %4, i64* %5, i8* %14, i64* %7, i8* %16, i64* %9, i8* %21)
%33 = icmp sgt i32 %32, 5
%34 = load i64, i64* %4
%35 = icmp ule i64 %34, %0
%36 = select i1 %33, i1 %35, i1 false
%37 = load i64, i64* %5
%38 = icmp ugt i64 %37, %0
%39 = select i1 %36, i1 %38, i1 false
br i1 %39, label %47, label %40
40: ; preds = %30
%41 = load i8*, i8** %2
%42 = call i32 @free(i8* %41)
store i8* null, i8** %2
store i64 0, i64* %3
%43 = call i64 @getline(i8** %2, i64* %3, i32* %22)
%44 = icmp sgt i64 %43, 0
br i1 %44, label %30, label %45
45: ; preds = %40, %24
%46 = call i32 @fclose(i32* %22)
br label %47
47: ; preds = %30, %45
%48 = phi i32* [ null, %45 ], [ %22, %30 ]
%49 = load i8*, i8** %2
%50 = call i32 @free(i8* %49)
ret i32* %48
}