#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*/ uint64_t;
typedef int /*<<< orphan*/ buffer;
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 sscanf(char *, char *, int /*<<< orphan*/ *, int /*<<< orphan*/ *, char *,
           char *);
scalar_t__ strcmp(char *, char *);
char *strrchr(char *, char);
bool find_ld_in_memory(uint64_t *start, uint64_t *end) {
  char execname[PATH_MAX], buffer[1024], prot[5], *p;
  bool found;
  FILE *fp;
  fp = fopen("/proc/self/maps", "r");
  if (fp == NULL)
    err(1, "fopen(\"/proc/self/maps\")");
  found = false;
  while (fgets(buffer, sizeof(buffer), fp) != NULL && !found) {
    if (sscanf(buffer, "%lx-%lx %4s %*x %*x:%*x %*u %s\n", start, end, prot,
               execname) != 4)
      continue;
    if (strcmp(prot, "r-xp") != 0)
      continue;
    p = strrchr(execname, '/');
    p = (p == NULL) ? execname : p + 1;
    if (strcmp(p, "ld-2.19.so") != 0)
      continue;
    found = true;
  }
  fclose(fp);
  return found;
}


x86 assembly (clang, -O0):
<find_ld_in_memory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x460,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e66(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x448(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
lea    0xe47(%rip),%rdi
lea    0xe50(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x440(%rbp)
cmpq   $0x0,-0x440(%rbp)
jne    11eb <find_ld_in_memory+0x7b>
mov    $0x1,%edi
lea    0xe2c(%rip),%rsi
call   1070 <err@plt>
movl   $0x0,-0x434(%rbp)
lea    -0x420(%rbp),%rdi
mov    -0x440(%rbp),%rdx
mov    $0x400,%esi
call   1050 <fgets@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x449(%rbp)
je     1234 <find_ld_in_memory+0xc4>
cmpl   $0x0,-0x434(%rbp)
setne  %al
xor    $0xff,%al
mov    %al,-0x449(%rbp)
mov    -0x449(%rbp),%al
test   $0x1,%al
jne    1247 <find_ld_in_memory+0xd7>
jmp    132a <find_ld_in_memory+0x1ba>
mov    -0x448(%rbp),%r9
lea    -0x420(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    -0x425(%rbp),%r8
lea    0xdc0(%rip),%rsi
call   1080 <sscanf@plt>
cmp    $0x4,%eax
je     127e <find_ld_in_memory+0x10e>
jmp    11f5 <find_ld_in_memory+0x85>
lea    -0x425(%rbp),%rdi
lea    0xdbf(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     12a0 <find_ld_in_memory+0x130>
jmp    11f5 <find_ld_in_memory+0x85>
mov    -0x448(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x430(%rbp)
cmpq   $0x0,-0x430(%rbp)
jne    12d9 <find_ld_in_memory+0x169>
mov    -0x448(%rbp),%rax
mov    %rax,-0x458(%rbp)
jmp    12eb <find_ld_in_memory+0x17b>
mov    -0x430(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x458(%rbp)
mov    -0x458(%rbp),%rax
mov    %rax,-0x430(%rbp)
mov    -0x430(%rbp),%rdi
lea    0xd49(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     131b <find_ld_in_memory+0x1ab>
jmp    11f5 <find_ld_in_memory+0x85>
movl   $0x1,-0x434(%rbp)
jmp    11f5 <find_ld_in_memory+0x85>
mov    -0x440(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x434(%rbp),%eax
mov    -0x18(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_ld_in_memory>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rsi,-0x38(%rbp)
mov    %rdi,-0x30(%rbp)
mov    0x2e5d(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe53(%rip),%rdi
lea    0xe5c(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%r12
test   %rax,%rax
jne    11d2 <find_ld_in_memory+0x62>
lea    0xe4a(%rip),%rsi
mov    $0x1,%edi
call   1070 <err@plt>
lea    -0x440(%rbp),%rdi
mov    $0x400,%esi
mov    %r12,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1290 <find_ld_in_memory+0x120>
lea    -0x440(%rbp),%r13
lea    -0x3d(%rbp),%r14
nopw   0x0(%rax,%rax,1)
mov    %r13,%rdi
lea    0xe21(%rip),%rsi
mov    -0x30(%rbp),%rdx
mov    -0x38(%rbp),%rcx
mov    %r14,%r8
mov    %rbx,%r9
call   1080 <sscanf@plt>
xor    %r15d,%r15d
cmp    $0x4,%eax
jne    1270 <find_ld_in_memory+0x100>
mov    %r14,%rdi
lea    0xe1c(%rip),%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    1270 <find_ld_in_memory+0x100>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
lea    0x1(%rax),%rdi
test   %rax,%rax
cmove  %rbx,%rdi
lea    0xdf8(%rip),%rsi
call   1060 <strcmp@plt>
xor    %r15d,%r15d
test   %rax,%rax
sete   %r15b
nopw   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    $0x400,%esi
mov    %r12,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1293 <find_ld_in_memory+0x123>
test   %r15d,%r15d
je     1200 <find_ld_in_memory+0x90>
jmp    1293 <find_ld_in_memory+0x123>
xor    %r15d,%r15d
mov    %r12,%rdi
call   1030 <fclose@plt>
mov    %r15d,%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 = [32 x i8] c"%lx-%lx %4s %*x %*x:%*x %*u %s\0A\00"
@.str.4 = [5 x i8] c"r-xp\00"
@.str.5 = [11 x i8] c"ld-2.19.so\00"
define i32 @find_ld_in_memory(i32* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca [1024 x i8]
%8 = alloca [5 x i8]
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32*
store i32* %0, i32** %3
store i32* %1, i32** %4
%12 = load i32, i32* @PATH_MAX
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %5
%15 = alloca i8, i64 %13
store i64 %13, i64* %6
%16 = 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* %16, i32** %11
%17 = load i32*, i32** %11
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 @err(i32 1, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %19, %2
store i32 0, i32* %10
br label %22
22: ; preds = %60, %59, %45, %40, %21
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%24 = load i32*, i32** %11
%25 = call i32* @fgets(i8* %23, i32 1024, i32* %24)
%26 = icmp ne i32* %25, null
br i1 %26, label %27, label %31
27: ; preds = %22
%28 = load i32, i32* %10
%29 = icmp ne i32 %28, 0
%30 = xor i1 %29, true
br label %31
31: ; preds = %27, %22
%32 = phi i1 [ false, %22 ], [ %30, %27 ]
br i1 %32, label %33, label %61
33: ; preds = %31
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%35 = load i32*, i32** %3
%36 = load i32*, i32** %4
%37 = getelementptr inbounds [5 x i8], [5 x i8]* %8, i64 0, i64 0
%38 = call i32 @sscanf(i8* %34, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32* %35, i32* %36, i8* %37, i8* %15)
%39 = icmp ne i32 %38, 4
br i1 %39, label %40, label %41
40: ; preds = %33
br label %22
41: ; preds = %33
%42 = getelementptr inbounds [5 x i8], [5 x i8]* %8, i64 0, i64 0
%43 = call i64 @strcmp(i8* %42, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %41
br label %22
46: ; preds = %41
%47 = call i8* @strrchr(i8* %15, i8 signext 47)
store i8* %47, i8** %9
%48 = load i8*, i8** %9
%49 = icmp eq i8* %48, null
br i1 %49, label %50, label %51
50: ; preds = %46
br label %54
51: ; preds = %46
%52 = load i8*, i8** %9
%53 = getelementptr inbounds i8, i8* %52, i64 1
br label %54
54: ; preds = %51, %50
%55 = phi i8* [ %15, %50 ], [ %53, %51 ]
store i8* %55, i8** %9
%56 = load i8*, i8** %9
%57 = call i64 @strcmp(i8* %56, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %60
59: ; preds = %54
br label %22
60: ; preds = %54
store i32 1, i32* %10
br label %22
61: ; preds = %31
%62 = load i32*, i32** %11
%63 = call i32 @fclose(i32* %62)
%64 = load i32, i32* %10
%65 = load i8*, i8** %5
ret i32 %64
}

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 = [32 x i8] c"%lx-%lx %4s %*x %*x:%*x %*u %s\0A\00"
@.str.4 = [5 x i8] c"r-xp\00"
@.str.5 = [11 x i8] c"ld-2.19.so\00"
define i32 @find_ld_in_memory(i32* %0, i32* %1) {
%3 = alloca [1024 x i8]
%4 = alloca [5 x i8]
%5 = load i32, i32* @PATH_MAX
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%9 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 0
%10 = 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))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %14
14: ; preds = %12, %2
%15 = call i32* @fgets(i8* %8, i32 1024, i32* %10)
%16 = icmp eq i32* %15, null
br i1 %16, label %37, label %17
17: ; preds = %14, %20
%18 = call i32 @sscanf(i8* %8, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32* %0, i32* %1, i8* %9, i8* %7)
%19 = icmp eq i32 %18, 4
br i1 %19, label %26, label %20
20: ; preds = %17, %26, %29
%21 = phi i32 [ 0, %17 ], [ 0, %26 ], [ %36, %29 ]
%22 = call i32* @fgets(i8* %8, i32 1024, i32* %10)
%23 = icmp ne i32* %22, null
%24 = icmp eq i32 %21, 0
%25 = select i1 %23, i1 %24, i1 false
br i1 %25, label %17, label %37
26: ; preds = %17
%27 = call i64 @strcmp(i8* %9, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %20
29: ; preds = %26
%30 = call i8* @strrchr(i8* %7, i8 signext 47)
%31 = icmp eq i8* %30, null
%32 = getelementptr inbounds i8, i8* %30, i64 1
%33 = select i1 %31, i8* %7, i8* %32
%34 = call i64 @strcmp(i8* %33, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
%35 = icmp eq i64 %34, 0
%36 = zext i1 %35 to i32
br label %20
37: ; preds = %20, %14
%38 = phi i32 [ 0, %14 ], [ %21, %20 ]
%39 = call i32 @fclose(i32* %10)
ret i32 %38
}