#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 PATH_MAX;
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ sscanf(char *, char *, unsigned int *, unsigned int *,
                          char *, char *);
int /*<<< orphan*/ *strstr(char *, char *);
int get_range(unsigned int *start, unsigned int *end) {
  char line[4096];
  char filename[PATH_MAX];
  char flags[32];
  FILE *fp;
  int ret;
  ret = -1;
  fp = fopen("/proc/self/maps", "r");
  if (fp == NULL)
    err(1, "fopen(\"/proc/self/maps\")");
  while (fgets(line, sizeof(line), fp) != NULL) {
    sscanf(line, "%x-%x %s %*x %*x:%*x %*Lu %s", start, end, flags, filename);
    if (strstr(flags, "r-xp") == NULL)
      continue;
    if (strstr(filename, "/libc.so") == NULL)
      continue;
    ret = 0;
    break;
  }
  fclose(fp);
  return ret;
}


x86 assembly (clang, -O0):
<get_range>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1060,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e76(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x1018(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x1058(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x1020(%rbp)
movl   $0xffffffff,-0x104c(%rbp)
ff ff ff
lea    0xe47(%rip),%rdi
lea    0xe50(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x1048(%rbp)
cmpq   $0x0,-0x1048(%rbp)
jne    11eb <get_range+0x8b>
mov    $0x1,%edi
lea    0xe2c(%rip),%rsi
call   1050 <err@plt>
jmp    11f0 <get_range+0x90>
lea    -0x1010(%rbp),%rdi
mov    -0x1048(%rbp),%rdx
mov    $0x1000,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     1289 <get_range+0x129>
mov    -0x1058(%rbp),%r9
lea    -0x1010(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    -0x1040(%rbp),%r8
lea    0xdf5(%rip),%rsi
call   1060 <sscanf@plt>
lea    -0x1040(%rbp),%rdi
lea    0xdff(%rip),%rsi
call   1080 <strstr@plt>
cmp    $0x0,%rax
jne    125d <get_range+0xfd>
jmp    11f0 <get_range+0x90>
mov    -0x1058(%rbp),%rdi
lea    0xde2(%rip),%rsi
call   1080 <strstr@plt>
cmp    $0x0,%rax
jne    127f <get_range+0x11f>
jmp    11f0 <get_range+0x90>
movl   $0x0,-0x104c(%rbp)
mov    -0x1048(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x104c(%rbp),%eax
mov    -0x1018(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_range>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1038,%rsp
mov    %rsi,-0x38(%rbp)
mov    %rdi,%r15
mov    %rsp,%r14
mov    0x2e6b(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe61(%rip),%rdi
lea    0xe6a(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%r12
test   %rax,%rax
jne    11c4 <get_range+0x64>
lea    0xe58(%rip),%rsi
mov    $0x1,%edi
call   1050 <err@plt>
lea    -0x1060(%rbp),%rdi
mov    $0x1000,%esi
mov    %r12,%rdx
call   1040 <fgets@plt>
mov    $0xffffffff,%r13d
test   %rax,%rax
je     126c <get_range+0x10c>
mov    %r14,-0x30(%rbp)
lea    -0x1060(%rbp),%r13
lea    -0x60(%rbp),%r14
jmp    1215 <get_range+0xb5>
nopl   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    $0x1000,%esi
mov    %r12,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     1262 <get_range+0x102>
mov    %r13,%rdi
lea    0xe0c(%rip),%rsi
mov    %r15,%rdx
mov    -0x38(%rbp),%rcx
mov    %r14,%r8
mov    %rbx,%r9
call   1060 <sscanf@plt>
mov    %r14,%rdi
lea    0xe0d(%rip),%rsi
call   1080 <strstr@plt>
test   %rax,%rax
je     1200 <get_range+0xa0>
mov    %rbx,%rdi
lea    0xdfe(%rip),%rsi
call   1080 <strstr@plt>
test   %rax,%rax
je     1200 <get_range+0xa0>
xor    %r13d,%r13d
mov    -0x30(%rbp),%r14
jmp    126c <get_range+0x10c>
mov    -0x30(%rbp),%r14
mov    $0xffffffff,%r13d
mov    %r12,%rdi
call   1030 <fclose@plt>
mov    %r14,%rsp
mov    %r13d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [16 x i8] c"/proc/self/maps\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [25 x i8] c"fopen(\22/proc/self/maps\22)\00"
@.str.3 = [29 x i8] c"%x-%x %s %*x %*x:%*x %*Lu %s\00"
@.str.4 = [5 x i8] c"r-xp\00"
@.str.5 = [9 x i8] c"/libc.so\00"
define i32 @get_range(i32* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca [4096 x i8]
%6 = alloca i8*
%7 = alloca i64
%8 = alloca [32 x i8]
%9 = alloca i32*
%10 = alloca i32
store i32* %0, i32** %3
store i32* %1, i32** %4
%11 = load i32, i32* @PATH_MAX
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %6
%14 = alloca i8, i64 %12
store i64 %12, i64* %7
store i32 -1, i32* %10
%15 = 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* %15, i32** %9
%16 = load i32*, i32** %9
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = call i32 @err(i32 1, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %18, %2
br label %21
21: ; preds = %39, %35, %20
%22 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%23 = load i32*, i32** %9
%24 = call i32* @fgets(i8* %22, i32 4096, i32* %23)
%25 = icmp ne i32* %24, null
br i1 %25, label %26, label %41
26: ; preds = %21
%27 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%28 = load i32*, i32** %3
%29 = load i32*, i32** %4
%30 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%31 = call i32 @sscanf(i8* %27, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32* %28, i32* %29, i8* %30, i8* %14)
%32 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%33 = call i32* @strstr(i8* %32, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%34 = icmp eq i32* %33, null
br i1 %34, label %35, label %36
35: ; preds = %26
br label %21
36: ; preds = %26
%37 = call i32* @strstr(i8* %14, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
%38 = icmp eq i32* %37, null
br i1 %38, label %39, label %40
39: ; preds = %36
br label %21
40: ; preds = %36
store i32 0, i32* %10
br label %41
41: ; preds = %40, %21
%42 = load i32*, i32** %9
%43 = call i32 @fclose(i32* %42)
%44 = load i32, i32* %10
%45 = load i8*, i8** %6
ret i32 %44
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [16 x i8] c"/proc/self/maps\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [25 x i8] c"fopen(\22/proc/self/maps\22)\00"
@.str.3 = [29 x i8] c"%x-%x %s %*x %*x:%*x %*Lu %s\00"
@.str.4 = [5 x i8] c"r-xp\00"
@.str.5 = [9 x i8] c"/libc.so\00"
define i32 @get_range(i32* %0, i32* %1) {
%3 = alloca [4096 x i8]
%4 = alloca [32 x i8]
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%6 = load i32, i32* @PATH_MAX
%7 = zext i32 %6 to i64
%8 = tail call i8* @llvm.stacksave()
%9 = alloca i8, i64 %7
%10 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%11 = 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))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %15
15: ; preds = %13, %2
%16 = call i32* @fgets(i8* %5, i32 4096, i32* %11)
%17 = icmp eq i32* %16, null
br i1 %17, label %28, label %18
18: ; preds = %15, %22
%19 = call i32 @sscanf(i8* %5, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32* %0, i32* %1, i8* %10, i8* %9)
%20 = call i32* @strstr(i8* %10, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %25
22: ; preds = %18, %25
%23 = call i32* @fgets(i8* %5, i32 4096, i32* %11)
%24 = icmp eq i32* %23, null
br i1 %24, label %28, label %18
25: ; preds = %18
%26 = call i32* @strstr(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
%27 = icmp eq i32* %26, null
br i1 %27, label %22, label %28
28: ; preds = %22, %25, %15
%29 = phi i32 [ -1, %15 ], [ 0, %25 ], [ -1, %22 ]
%30 = call i32 @fclose(i32* %11)
ret i32 %29
}