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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int sscanf(char *, char *, void **, void **, int *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char const *);
int /*<<< orphan*/ strncmp(char *, char const *, int /*<<< orphan*/);
int find_map(void **start, void **end, const char *name) {
  FILE *maps;
  char line[128];
  int found = 0;
  maps = fopen("/proc/self/maps", "r");
  if (!maps) {
    fprintf(stderr, "cannot open maps\n");
    return -1;
  }
  while (!found && fgets(line, sizeof(line), maps)) {
    int m = -1;
    if (2 != sscanf(line, "%p-%p r-xp %*x %*x:%*x %*u %n", start, end, &m))
      continue;
    if (m < 0)
      continue;
    if (!strncmp(&line[m], name, strlen(name)))
      found = 1;
  }
  fclose(maps);
  return !found;
}


x86 assembly (clang, -O0):
<find_map>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0xb4(%rbp)
lea    0xe68(%rip),%rdi
lea    0xe71(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11d4 <find_map+0x64>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
lea    0xe4f(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12eb <find_map+0x17b>
jmp    11d9 <find_map+0x69>
xor    %eax,%eax
cmpl   $0x0,-0xb4(%rbp)
mov    %al,-0xb9(%rbp)
jne    1210 <find_map+0xa0>
lea    -0xb0(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    $0x80,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0xb9(%rbp)
mov    -0xb9(%rbp),%al
test   $0x1,%al
jne    1223 <find_map+0xb3>
jmp    12ce <find_map+0x15e>
movl   $0xffffffff,-0xb8(%rbp)
ff ff ff
lea    -0xb0(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xde1(%rip),%rsi
lea    -0xb8(%rbp),%r8
call   1080 <sscanf@plt>
mov    %eax,%ecx
mov    $0x2,%eax
cmp    %ecx,%eax
je     1263 <find_map+0xf3>
jmp    11d9 <find_map+0x69>
cmpl   $0x0,-0xb8(%rbp)
jge    1275 <find_map+0x105>
jmp    11d9 <find_map+0x69>
movslq -0xb8(%rbp),%rcx
lea    -0xb0(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0xd0(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0xc8(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0xd0(%rbp),%rdi
mov    -0xc8(%rbp),%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    12c9 <find_map+0x159>
movl   $0x1,-0xb4(%rbp)
jmp    11d9 <find_map+0x69>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
cmpl   $0x0,-0xb4(%rbp)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xd0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_map>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xa8,%rsp
mov    %rdx,0x18(%rsp)
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0xe6d(%rip),%rdi
lea    0xe76(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1267 <find_map+0xf7>
mov    %rax,%r12
lea    0x20(%rsp),%r15
lea    0xe6d(%rip),%r13
mov    %rax,0x10(%rsp)
jmp    11c8 <find_map+0x58>
xchg   %ax,%ax
test   %ecx,%ecx
jne    1253 <find_map+0xe3>
mov    %r15,%rdi
mov    $0x80,%esi
mov    %r12,%rdx
call   1060 <fgets@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1253 <find_map+0xe3>
movl   $0xffffffff,0xc(%rsp)
ff
mov    %r15,%rdi
mov    %r13,%rsi
mov    %r14,%rdx
mov    %rbx,%rcx
lea    0xc(%rsp),%r8
call   1080 <sscanf@plt>
xor    %ecx,%ecx
cmp    $0x2,%eax
jne    11c0 <find_map+0x50>
mov    0xc(%rsp),%eax
test   %eax,%eax
js     11c0 <find_map+0x50>
mov    %r14,%r13
lea    (%rsp,%rax,1),%r14
add    $0x20,%r14
mov    %r15,%r12
mov    0x18(%rsp),%r15
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %r14,%rdi
mov    %r13,%r14
lea    0xdef(%rip),%r13
mov    %r15,%rsi
mov    %r12,%r15
mov    0x10(%rsp),%r12
mov    %eax,%edx
call   1030 <strncmp@plt>
xor    %ecx,%ecx
test   %eax,%eax
sete   %cl
jmp    11c0 <find_map+0x50>
xor    %r14d,%r14d
test   %rbp,%rbp
sete   %r14b
mov    %r12,%rdi
call   1040 <fclose@plt>
jmp    1282 <find_map+0x112>
mov    0x2d72(%rip),%rax
mov    (%rax),%edi
lea    0xd9b(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0xa8,%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 = [18 x i8] c"cannot open maps\0A\00"
@.str.3 = [30 x i8] c"%p-%p r-xp %*x %*x:%*x %*u %n\00"
define i32 @find_map(i8** %0, i8** %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8**
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca [128 x i8]
%10 = alloca i32
%11 = alloca i32
store i8** %0, i8*** %5
store i8** %1, i8*** %6
store i8* %2, i8** %7
store i32 0, i32* %10
%12 = 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* %12, i32** %8
%13 = load i32*, i32** %8
%14 = icmp ne i32* %13, null
br i1 %14, label %18, label %15
15: ; preds = %3
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %58
18: ; preds = %3
br label %19
19: ; preds = %50, %39, %35, %18
%20 = load i32, i32* %10
%21 = icmp ne i32 %20, 0
br i1 %21, label %27, label %22
22: ; preds = %19
%23 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%24 = load i32*, i32** %8
%25 = call i64 @fgets(i8* %23, i32 128, i32* %24)
%26 = icmp ne i64 %25, 0
br label %27
27: ; preds = %22, %19
%28 = phi i1 [ false, %19 ], [ %26, %22 ]
br i1 %28, label %29, label %51
29: ; preds = %27
store i32 -1, i32* %11
%30 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%31 = load i8**, i8*** %5
%32 = load i8**, i8*** %6
%33 = call i32 @sscanf(i8* %30, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8** %31, i8** %32, i32* %11)
%34 = icmp ne i32 2, %33
br i1 %34, label %35, label %36
35: ; preds = %29
br label %19
36: ; preds = %29
%37 = load i32, i32* %11
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %36
br label %19
40: ; preds = %36
%41 = load i32, i32* %11
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 %42
%44 = load i8*, i8** %7
%45 = load i8*, i8** %7
%46 = call i32 @strlen(i8* %45)
%47 = call i32 @strncmp(i8* %43, i8* %44, i32 %46)
%48 = icmp ne i32 %47, 0
br i1 %48, label %50, label %49
49: ; preds = %40
store i32 1, i32* %10
br label %50
50: ; preds = %49, %40
br label %19
51: ; preds = %27
%52 = load i32*, i32** %8
%53 = call i32 @fclose(i32* %52)
%54 = load i32, i32* %10
%55 = icmp ne i32 %54, 0
%56 = xor i1 %55, true
%57 = zext i1 %56 to i32
store i32 %57, i32* %4
br label %58
58: ; preds = %51, %15
%59 = load i32, i32* %4
ret i32 %59
}

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 = [18 x i8] c"cannot open maps\0A\00"
@.str.3 = [30 x i8] c"%p-%p r-xp %*x %*x:%*x %*u %n\00"
define i32 @find_map(i8** %0, i8** %1, i8* %2) {
%4 = alloca [128 x i8]
%5 = alloca i32
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%7 = 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))
%8 = icmp eq i32* %7, null
br i1 %8, label %11, label %9
9: ; preds = %3
%10 = bitcast i32* %5 to i8*
br label %14
11: ; preds = %3
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %36
14: ; preds = %9, %30
%15 = call i64 @fgets(i8* %6, i32 128, i32* %7)
%16 = icmp eq i64 %15, 0
br i1 %16, label %33, label %17
17: ; preds = %14
store i32 -1, i32* %5
%18 = call i32 @sscanf(i8* %6, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8** %0, i8** %1, i32* %5)
%19 = icmp eq i32 %18, 2
br i1 %19, label %20, label %30
20: ; preds = %17
%21 = load i32, i32* %5
%22 = icmp slt i32 %21, 0
br i1 %22, label %30, label %23
23: ; preds = %20
%24 = zext i32 %21 to i64
%25 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 %24
%26 = call i32 @strlen(i8* %2)
%27 = call i32 @strncmp(i8* %25, i8* %2, i32 %26)
%28 = icmp eq i32 %27, 0
%29 = zext i1 %28 to i32
br label %30
30: ; preds = %20, %17, %23
%31 = phi i32 [ %29, %23 ], [ 0, %17 ], [ 0, %20 ]
%32 = icmp eq i32 %31, 0
br i1 %32, label %14, label %33
33: ; preds = %30, %14
%34 = call i32 @fclose(i32* %7)
%35 = zext i1 %16 to i32
br label %36
36: ; preds = %33, %11
%37 = phi i32 [ %35, %33 ], [ -1, %11 ]
ret i32 %37
}