#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 /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int sscanf(char *, char *, unsigned long *, unsigned long *, unsigned long *,
           unsigned long *, char *);
int /*<<< orphan*/ stderr;
int read_maps(unsigned long addr, char *buf) {
  FILE *f;
  char line[4096];
  char *tmp;
  f = fopen("/proc/self/maps", "r");
  if (!f) {
    fprintf(stderr, "ERR: "
                    "Failed to open /proc/self/maps\n");
    return -1;
  }
  while (1) {
    unsigned long start, end, off, ino;
    int ret;
    tmp = fgets(line, 4096, f);
    if (!tmp)
      break;
    buf[0] = '\0';
    ret = sscanf(line, "%lx-%lx %*s %lx %*s %ld %255s", &start, &end, &off,
                 &ino, buf);
    if ((ret < 4) || (ret > 5)) {
      fprintf(stderr,
              "ERR: "
              "Couldn't parse /proc/self/maps line: %s\n",
              line);
      fclose(f);
      return -1;
    }
    if ((start <= addr) && (addr < end)) {
      fclose(f);
      return 1;
    }
  }
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<read_maps>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1060,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    0xe96(%rip),%rdi
lea    0xe9f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a8 <read_maps+0x58>
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
lea    0xe7d(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b9 <read_maps+0x169>
jmp    11ad <read_maps+0x5d>
lea    -0x1020(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x1000,%esi
call   1040 <fgets@plt>
mov    %rax,-0x1028(%rbp)
cmpq   $0x0,-0x1028(%rbp)
jne    11dc <read_maps+0x8c>
jmp    12a9 <read_maps+0x159>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x1020(%rbp),%rdi
mov    -0x18(%rbp),%rax
lea    0xe42(%rip),%rsi
lea    -0x1030(%rbp),%rdx
lea    -0x1038(%rbp),%rcx
lea    -0x1040(%rbp),%r8
lea    -0x1048(%rbp),%r9
mov    %rax,(%rsp)
call   1060 <sscanf@plt>
mov    %eax,-0x104c(%rbp)
cmpl   $0x4,-0x104c(%rbp)
jl     123a <read_maps+0xea>
cmpl   $0x5,-0x104c(%rbp)
jle    126d <read_maps+0x11d>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
lea    -0x1020(%rbp),%rdx
lea    0xe04(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b9 <read_maps+0x169>
mov    -0x1030(%rbp),%rax
cmp    -0x10(%rbp),%rax
ja     12a4 <read_maps+0x154>
mov    -0x10(%rbp),%rax
cmp    -0x1038(%rbp),%rax
jae    12a4 <read_maps+0x154>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    12b9 <read_maps+0x169>
jmp    11ad <read_maps+0x5d>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1060,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_maps>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1038,%rsp
mov    %rsi,%rbx
mov    %rdi,%r15
lea    0xe92(%rip),%rdi
lea    0xe9b(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1218 <read_maps+0xc8>
mov    %rax,%r14
lea    0x30(%rsp),%rdi
mov    $0x1000,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     1236 <read_maps+0xe6>
lea    0x30(%rsp),%r13
lea    0x28(%rsp),%rbp
lea    0x20(%rsp),%r12
jmp    11d5 <read_maps+0x85>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r13,%rdi
mov    $0x1000,%esi
mov    %r14,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     1236 <read_maps+0xe6>
movb   $0x0,(%rbx)
mov    %rbx,(%rsp)
mov    %r13,%rdi
lea    0xe51(%rip),%rsi
lea    0x18(%rsp),%rdx
lea    0x10(%rsp),%rcx
mov    %rbp,%r8
mov    %r12,%r9
call   1060 <sscanf@plt>
add    $0xfffffffa,%eax
cmp    $0xfffffffd,%eax
jbe    1242 <read_maps+0xf2>
cmp    %r15,0x18(%rsp)
ja     11c0 <read_maps+0x70>
cmp    %r15,0x10(%rsp)
jbe    11c0 <read_maps+0x70>
mov    $0x1,%ebx
jmp    1263 <read_maps+0x113>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
lea    0xdea(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    126b <read_maps+0x11b>
mov    %r14,%rdi
call   1030 <fclose@plt>
xor    %ebx,%ebx
jmp    126b <read_maps+0x11b>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
lea    0xe03(%rip),%rsi
lea    0x30(%rsp),%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %ebx,%eax
add    $0x1038,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"/proc/self/maps\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [37 x i8] c"ERR: Failed to open /proc/self/maps\0A\00"
@.str.3 = [30 x i8] c"%lx-%lx %*s %lx %*s %ld %255s\00"
@.str.4 = [46 x i8] c"ERR: Couldn't parse /proc/self/maps line: %s\0A\00"
define i32 @read_maps(i64 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca [4096 x i8]
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
%13 = alloca i32
store i64 %0, i64* %4
store i8* %1, i8** %5
%14 = call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %14, i32** %6
%15 = load i32*, i32** %6
%16 = icmp ne i32* %15, null
br i1 %16, label %20, label %17
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %60
20: ; preds = %2
br label %21
21: ; preds = %20, %56
%22 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%23 = load i32*, i32** %6
%24 = call i8* @fgets(i8* %22, i32 4096, i32* %23)
store i8* %24, i8** %8
%25 = load i8*, i8** %8
%26 = icmp ne i8* %25, null
br i1 %26, label %28, label %27
27: ; preds = %21
br label %57
28: ; preds = %21
%29 = load i8*, i8** %5
%30 = getelementptr inbounds i8, i8* %29, i64 0
store i8 0, i8* %30
%31 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%32 = load i8*, i8** %5
%33 = call i32 @sscanf(i8* %31, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i64* %9, i64* %10, i64* %11, i64* %12, i8* %32)
store i32 %33, i32* %13
%34 = load i32, i32* %13
%35 = icmp slt i32 %34, 4
br i1 %35, label %39, label %36
36: ; preds = %28
%37 = load i32, i32* %13
%38 = icmp sgt i32 %37, 5
br i1 %38, label %39, label %45
39: ; preds = %36, %28
%40 = load i32, i32* @stderr
%41 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0), i8* %41)
%43 = load i32*, i32** %6
%44 = call i32 @fclose(i32* %43)
store i32 -1, i32* %3
br label %60
45: ; preds = %36
%46 = load i64, i64* %9
%47 = load i64, i64* %4
%48 = icmp ule i64 %46, %47
br i1 %48, label %49, label %56
49: ; preds = %45
%50 = load i64, i64* %4
%51 = load i64, i64* %10
%52 = icmp ult i64 %50, %51
br i1 %52, label %53, label %56
53: ; preds = %49
%54 = load i32*, i32** %6
%55 = call i32 @fclose(i32* %54)
store i32 1, i32* %3
br label %60
56: ; preds = %49, %45
br label %21
57: ; preds = %27
%58 = load i32*, i32** %6
%59 = call i32 @fclose(i32* %58)
store i32 0, i32* %3
br label %60
60: ; preds = %57, %53, %39, %17
%61 = load i32, i32* %3
ret i32 %61
}

LLVM IR (-O2):
@.str = [16 x i8] c"/proc/self/maps\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [37 x i8] c"ERR: Failed to open /proc/self/maps\0A\00"
@.str.3 = [30 x i8] c"%lx-%lx %*s %lx %*s %ld %255s\00"
@.str.4 = [46 x i8] c"ERR: Couldn't parse /proc/self/maps line: %s\0A\00"
define i32 @read_maps(i64 %0, i8* %1) {
%3 = alloca [4096 x i8]
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%9 = tail call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %18, label %11
11: ; preds = %2
%12 = bitcast i64* %4 to i8*
%13 = bitcast i64* %5 to i8*
%14 = bitcast i64* %6 to i8*
%15 = bitcast i64* %7 to i8*
%16 = call i8* @fgets(i8* %8, i32 4096, i32* %9)
%17 = icmp eq i8* %16, null
br i1 %17, label %40, label %21
18: ; preds = %2
%19 = load i32, i32* @stderr
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
br label %42
21: ; preds = %11, %37
store i8 0, i8* %1
%22 = call i32 @sscanf(i8* %8, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i64* %4, i64* %5, i64* %6, i64* %7, i8* %1)
%23 = add i32 %22, -6
%24 = icmp ult i32 %23, -2
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = load i32, i32* @stderr
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0), i8* %8)
br label %34
28: ; preds = %21
%29 = load i64, i64* %4
%30 = icmp ule i64 %29, %0
%31 = load i64, i64* %5
%32 = icmp ugt i64 %31, %0
%33 = select i1 %30, i1 %32, i1 false
br i1 %33, label %34, label %37
34: ; preds = %28, %25
%35 = phi i32 [ -1, %25 ], [ 1, %28 ]
%36 = call i32 @fclose(i32* %9)
br label %42
37: ; preds = %28
%38 = call i8* @fgets(i8* %8, i32 4096, i32* %9)
%39 = icmp eq i8* %38, null
br i1 %39, label %40, label %21
40: ; preds = %37, %11
%41 = call i32 @fclose(i32* %9)
br label %42
42: ; preds = %34, %40, %18
%43 = phi i32 [ 0, %40 ], [ -1, %18 ], [ %35, %34 ]
ret i32 %43
}