#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*/ buf;
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*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int sscanf(char *, char *, unsigned long *, unsigned long *, char *);
scalar_t__ strstr(char *, char *);
int search_proc_maps(char *needle, unsigned long *low, unsigned long *high) {
  unsigned long start, end;
  static char buf[4096];
  char name[128];
  FILE *f;
  int rc;
  f = fopen("/proc/self/maps", "r");
  if (!f) {
    perror("fopen");
    return -1;
  }
  while (fgets(buf, sizeof(buf), f)) {
    rc = sscanf(buf, "%lx-%lx %*c%*c%*c%*c %*x %*d:%*d %*d %127s\n", &start,
                &end, name);
    if (rc == 2)
      continue;
    if (rc != 3) {
      printf("sscanf errored\n");
      rc = -1;
      break;
    }
    if (strstr(name, needle)) {
      *low = start;
      *high = end - 1;
      rc = 0;
      break;
    }
  }
  fclose(f);
  return rc;
}


x86 assembly (clang, -O0):
<search_proc_maps>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    0xe72(%rip),%rdi
lea    0xe7b(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0xb8(%rbp)
cmpq   $0x0,-0xb8(%rbp)
jne    11c7 <search_proc_maps+0x57>
lea    0xe5c(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ad <search_proc_maps+0x13d>
jmp    11cc <search_proc_maps+0x5c>
mov    -0xb8(%rbp),%rdx
lea    0x2e96(%rip),%rdi
mov    $0x1000,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1298 <search_proc_maps+0x128>
lea    -0xb0(%rbp),%r8
lea    0x2e74(%rip),%rdi
lea    0xe15(%rip),%rsi
lea    -0x28(%rbp),%rdx
lea    -0x30(%rbp),%rcx
call   1060 <sscanf@plt>
mov    %eax,-0xbc(%rbp)
cmpl   $0x2,-0xbc(%rbp)
jne    1228 <search_proc_maps+0xb8>
jmp    11cc <search_proc_maps+0x5c>
cmpl   $0x3,-0xbc(%rbp)
je     1250 <search_proc_maps+0xe0>
lea    0xe08(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0xbc(%rbp)
ff ff ff
jmp    1298 <search_proc_maps+0x128>
lea    -0xb0(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1090 <strstr@plt>
cmp    $0x0,%rax
je     1293 <search_proc_maps+0x123>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rcx
sub    $0x1,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0xbc(%rbp)
jmp    1298 <search_proc_maps+0x128>
jmp    11cc <search_proc_maps+0x5c>
mov    -0xb8(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0xbc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<search_proc_maps>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xb8,%rsp
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %rdi,0x18(%rsp)
lea    0xe6d(%rip),%rdi
lea    0xe76(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1291 <search_proc_maps+0x121>
mov    %rax,%r13
lea    0x2ebe(%rip),%rdi
mov    $0x1000,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1262 <search_proc_maps+0xf2>
mov    %rbp,0x8(%rsp)
mov    %rbx,0x10(%rsp)
lea    0x2e97(%rip),%rbx
lea    0x28(%rsp),%r14
lea    0x20(%rsp),%r15
lea    0x30(%rsp),%r12
jmp    1205 <search_proc_maps+0x95>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x1000,%esi
mov    %r13,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1275 <search_proc_maps+0x105>
mov    %rbx,%rdi
lea    0xe09(%rip),%rsi
mov    %r14,%rdx
mov    %r15,%rcx
mov    %r12,%r8
call   1060 <sscanf@plt>
mov    %eax,%ebp
cmp    $0x2,%eax
je     11f0 <search_proc_maps+0x80>
cmp    $0x3,%ebp
jne    1264 <search_proc_maps+0xf4>
mov    %r12,%rdi
mov    0x18(%rsp),%rsi
call   1090 <strstr@plt>
mov    $0x3,%ebp
test   %rax,%rax
je     11f0 <search_proc_maps+0x80>
mov    0x28(%rsp),%rax
mov    0x8(%rsp),%rcx
mov    %rax,(%rcx)
mov    0x20(%rsp),%rax
add    $0xffffffffffffffff,%rax
mov    0x10(%rsp),%rcx
mov    %rax,(%rcx)
xor    %ebp,%ebp
jmp    1275 <search_proc_maps+0x105>
jmp    1275 <search_proc_maps+0x105>
lea    0xdd9(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
mov    %r13,%rdi
call   1040 <fclose@plt>
mov    %ebp,%eax
add    $0xb8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd7a(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebp
jmp    127d <search_proc_maps+0x10d>

LLVM IR (-O0):
@search_proc_maps.buf = internal global [4096 x i8] zeroinitializer
@.str = [16 x i8] c"/proc/self/maps\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"fopen\00"
@.str.3 = [44 x i8] c"%lx-%lx %*c%*c%*c%*c %*x %*d:%*d %*d %127s\0A\00"
@.str.4 = [16 x i8] c"sscanf errored\0A\00"
define i32 @search_proc_maps(i8* %0, i64* %1, i64* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64*
%7 = alloca i64*
%8 = alloca i64
%9 = alloca i64
%10 = alloca [128 x i8]
%11 = alloca i32*
%12 = alloca i32
store i8* %0, i8** %5
store i64* %1, i64** %6
store i64* %2, i64** %7
%13 = 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* %13, i32** %11
%14 = load i32*, i32** %11
%15 = icmp ne i32* %14, null
br i1 %15, label %18, label %16
16: ; preds = %3
%17 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %50
18: ; preds = %3
br label %19
19: ; preds = %45, %28, %18
%20 = load i32*, i32** %11
%21 = call i64 @fgets(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @search_proc_maps.buf, i64 0, i64 0), i32 4096, i32* %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %46
23: ; preds = %19
%24 = getelementptr inbounds [128 x i8], [128 x i8]* %10, i64 0, i64 0
%25 = call i32 @sscanf(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @search_proc_maps.buf, i64 0, i64 0), i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i64* %8, i64* %9, i8* %24)
store i32 %25, i32* %12
%26 = load i32, i32* %12
%27 = icmp eq i32 %26, 2
br i1 %27, label %28, label %29
28: ; preds = %23
br label %19
29: ; preds = %23
%30 = load i32, i32* %12
%31 = icmp ne i32 %30, 3
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %12
br label %46
34: ; preds = %29
%35 = getelementptr inbounds [128 x i8], [128 x i8]* %10, i64 0, i64 0
%36 = load i8*, i8** %5
%37 = call i64 @strstr(i8* %35, i8* %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %45
39: ; preds = %34
%40 = load i64, i64* %8
%41 = load i64*, i64** %6
store i64 %40, i64* %41
%42 = load i64, i64* %9
%43 = sub i64 %42, 1
%44 = load i64*, i64** %7
store i64 %43, i64* %44
store i32 0, i32* %12
br label %46
45: ; preds = %34
br label %19
46: ; preds = %39, %32, %19
%47 = load i32*, i32** %11
%48 = call i32 @fclose(i32* %47)
%49 = load i32, i32* %12
store i32 %49, i32* %4
br label %50
50: ; preds = %46, %16
%51 = load i32, i32* %4
ret i32 %51
}

LLVM IR (-O2):
@search_proc_maps.buf = internal global [4096 x i8] zeroinitializer
@.str = [16 x i8] c"/proc/self/maps\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"fopen\00"
@.str.3 = [44 x i8] c"%lx-%lx %*c%*c%*c%*c %*x %*d:%*d %*d %127s\0A\00"
@str = [15 x i8] c"sscanf errored\00"
define i32 @search_proc_maps(i8* %0, i64* nocapture writeonly %1, i64* nocapture writeonly %2) {
%4 = alloca i64
%5 = alloca i64
%6 = alloca [128 x i8]
%7 = bitcast i64* %4 to i8*
%8 = bitcast i64* %5 to i8*
%9 = getelementptr inbounds [128 x i8], [128 x i8]* %6, 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 %15, label %12
12: ; preds = %3
%13 = call i64 @fgets(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @search_proc_maps.buf, i64 0, i64 0), i32 4096, i32* %10)
%14 = icmp eq i64 %13, 0
br i1 %14, label %32, label %17
15: ; preds = %3
%16 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %35
17: ; preds = %12, %24
%18 = call i32 @sscanf(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @search_proc_maps.buf, i64 0, i64 0), i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i64* %4, i64* %5, i8* %9)
switch i32 %18, label %19 [
i32 2, label %24
i32 3, label %21
]
19: ; preds = %17
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %32
21: ; preds = %17
%22 = call i64 @strstr(i8* %9, i8* %0)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %21, %17
%25 = phi i32 [ %18, %17 ], [ 3, %21 ]
%26 = call i64 @fgets(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @search_proc_maps.buf, i64 0, i64 0), i32 4096, i32* %10)
%27 = icmp eq i64 %26, 0
br i1 %27, label %32, label %17
28: ; preds = %21
%29 = load i64, i64* %4
store i64 %29, i64* %1
%30 = load i64, i64* %5
%31 = add i64 %30, -1
store i64 %31, i64* %2
br label %32
32: ; preds = %24, %12, %28, %19
%33 = phi i32 [ -1, %19 ], [ 0, %28 ], [ undef, %12 ], [ %25, %24 ]
%34 = call i32 @fclose(i32* %10)
br label %35
35: ; preds = %32, %15
%36 = phi i32 [ %33, %32 ], [ -1, %15 ]
ret i32 %36
}