#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*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int sscanf(char *, char *, unsigned long *, unsigned long *, unsigned long *,
           unsigned long *, char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
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: %s\n",
            strerror(errno));
    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    0xe86(%rip),%rdi
lea    0xe8f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d3 <read_maps+0x73>
mov    0x2e44(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1050(%rbp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x1050(%rbp),%edi
mov    %rax,%rdx
lea    0xe50(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e2 <read_maps+0x182>
jmp    11d8 <read_maps+0x78>
lea    -0x1020(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x1000,%esi
call   1040 <fgets@plt>
mov    %rax,-0x1028(%rbp)
cmpq   $0x0,-0x1028(%rbp)
jne    1207 <read_maps+0xa7>
jmp    12d2 <read_maps+0x172>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x1020(%rbp),%rdi
mov    -0x18(%rbp),%rax
lea    0xe1b(%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     1265 <read_maps+0x105>
cmpl   $0x5,-0x104c(%rbp)
jle    1296 <read_maps+0x136>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
lea    -0x1020(%rbp),%rdx
lea    0xddd(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e2 <read_maps+0x182>
mov    -0x1030(%rbp),%rax
cmp    -0x10(%rbp),%rax
ja     12cd <read_maps+0x16d>
mov    -0x10(%rbp),%rax
cmp    -0x1038(%rbp),%rax
jae    12cd <read_maps+0x16d>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e2 <read_maps+0x182>
jmp    11d8 <read_maps+0x78>
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    0xe82(%rip),%rdi
lea    0xe8b(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1228 <read_maps+0xc8>
mov    %rax,%r14
lea    0x30(%rsp),%rdi
mov    $0x1000,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     1257 <read_maps+0xf7>
lea    0x30(%rsp),%r13
lea    0x28(%rsp),%rbp
lea    0x20(%rsp),%r12
jmp    11e5 <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     1257 <read_maps+0xf7>
movb   $0x0,(%rbx)
mov    %rbx,(%rsp)
mov    %r13,%rdi
lea    0xe45(%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    1263 <read_maps+0x103>
cmp    %r15,0x18(%rsp)
ja     11d0 <read_maps+0x70>
cmp    %r15,0x10(%rsp)
jbe    11d0 <read_maps+0x70>
mov    $0x1,%ebx
jmp    1282 <read_maps+0x122>
mov    0x2db1(%rip),%rax
mov    (%rax),%ebx
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdcc(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    128a <read_maps+0x12a>
mov    %r14,%rdi
call   1030 <fclose@plt>
xor    %ebx,%ebx
jmp    128a <read_maps+0x12a>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
lea    0xde6(%rip),%rsi
lea    0x30(%rsp),%rdx
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 = [41 x i8] c"ERR: Failed to open /proc/self/maps: %s\0A\00"
@errno = global i32 0
@.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 %22, label %17
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = load i32, i32* @errno
%20 = call i8* @strerror(i32 %19)
%21 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i8* %20)
store i32 -1, i32* %3
br label %62
22: ; preds = %2
br label %23
23: ; preds = %22, %58
%24 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%25 = load i32*, i32** %6
%26 = call i8* @fgets(i8* %24, i32 4096, i32* %25)
store i8* %26, i8** %8
%27 = load i8*, i8** %8
%28 = icmp ne i8* %27, null
br i1 %28, label %30, label %29
29: ; preds = %23
br label %59
30: ; preds = %23
%31 = load i8*, i8** %5
%32 = getelementptr inbounds i8, i8* %31, i64 0
store i8 0, i8* %32
%33 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%34 = load i8*, i8** %5
%35 = call i32 @sscanf(i8* %33, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i64* %9, i64* %10, i64* %11, i64* %12, i8* %34)
store i32 %35, i32* %13
%36 = load i32, i32* %13
%37 = icmp slt i32 %36, 4
br i1 %37, label %41, label %38
38: ; preds = %30
%39 = load i32, i32* %13
%40 = icmp sgt i32 %39, 5
br i1 %40, label %41, label %47
41: ; preds = %38, %30
%42 = load i32, i32* @stderr
%43 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%44 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0), i8* %43)
%45 = load i32*, i32** %6
%46 = call i32 @fclose(i32* %45)
store i32 -1, i32* %3
br label %62
47: ; preds = %38
%48 = load i64, i64* %9
%49 = load i64, i64* %4
%50 = icmp ule i64 %48, %49
br i1 %50, label %51, label %58
51: ; preds = %47
%52 = load i64, i64* %4
%53 = load i64, i64* %10
%54 = icmp ult i64 %52, %53
br i1 %54, label %55, label %58
55: ; preds = %51
%56 = load i32*, i32** %6
%57 = call i32 @fclose(i32* %56)
store i32 1, i32* %3
br label %62
58: ; preds = %51, %47
br label %23
59: ; preds = %29
%60 = load i32*, i32** %6
%61 = call i32 @fclose(i32* %60)
store i32 0, i32* %3
br label %62
62: ; preds = %59, %55, %41, %17
%63 = load i32, i32* %3
ret i32 %63
}

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 = [41 x i8] c"ERR: Failed to open /proc/self/maps: %s\0A\00"
@errno = global i32 0
@.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 %42, label %23
18: ; preds = %2
%19 = load i32, i32* @stderr
%20 = load i32, i32* @errno
%21 = tail call i8* @strerror(i32 %20)
%22 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i8* %21)
br label %44
23: ; preds = %11, %39
store i8 0, i8* %1
%24 = 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)
%25 = add i32 %24, -6
%26 = icmp ult i32 %25, -2
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0), i8* %8)
br label %36
30: ; preds = %23
%31 = load i64, i64* %4
%32 = icmp ule i64 %31, %0
%33 = load i64, i64* %5
%34 = icmp ugt i64 %33, %0
%35 = select i1 %32, i1 %34, i1 false
br i1 %35, label %36, label %39
36: ; preds = %30, %27
%37 = phi i32 [ -1, %27 ], [ 1, %30 ]
%38 = call i32 @fclose(i32* %9)
br label %44
39: ; preds = %30
%40 = call i8* @fgets(i8* %8, i32 4096, i32* %9)
%41 = icmp eq i8* %40, null
br i1 %41, label %42, label %23
42: ; preds = %39, %11
%43 = call i32 @fclose(i32* %9)
br label %44
44: ; preds = %36, %42, %18
%45 = phi i32 [ 0, %42 ], [ -1, %18 ], [ %37, %36 ]
ret i32 %45
}