#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 pid_t;
typedef int /*<<< orphan*/ buffer;
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*/ sprintf(char *, char *, int);
int /*<<< orphan*/ sscanf(char *, char *, size_t *, size_t *, char *, size_t *,
                          unsigned int *, unsigned int *, size_t *, char *);
scalar_t__ strcmp(char *, char *);
int get_heap_range(pid_t pid, void **addr_start, void **addr_end) {
  int ret = -1;
  char buffer[2048], filename[1024], flags[32];
  sprintf(filename, "/proc/%u/maps", pid);
  FILE *file = fopen(filename, "r");
  if (!file)
    return -1;
  size_t offset, inode;
  unsigned dev_major, dev_minor;
  while (fgets(buffer, sizeof(buffer), file)) {
    sscanf(buffer, "%zx-%zx %31s %zx %x:%x %zu %1023s", (size_t *)addr_start,
           (size_t *)addr_end, flags, &offset, &dev_major, &dev_minor, &inode,
           filename);
    if (strcmp(filename, "[heap]") == 0) {
      ret = 0;
      break;
    }
  }
  fclose(file);
  return ret;
}


x86 assembly (clang, -O0):
<get_heap_range>:
push   %rbp
mov    %rsp,%rbp
push   %rbx
sub    $0xc88,%rsp
mov    %edi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0xffffffff,-0x24(%rbp)
lea    -0xc30(%rbp),%rdi
mov    -0x10(%rbp),%edx
lea    0xe71(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0xc30(%rbp),%rdi
lea    0xe6c(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0xc58(%rbp)
cmpq   $0x0,-0xc58(%rbp)
jne    11c8 <get_heap_range+0x68>
movl   $0xffffffff,-0xc(%rbp)
jmp    1287 <get_heap_range+0x127>
jmp    11cd <get_heap_range+0x6d>
lea    -0x830(%rbp),%rdi
mov    -0xc58(%rbp),%rdx
mov    $0x800,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     1275 <get_heap_range+0x115>
lea    -0x830(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    -0xc50(%rbp),%r8
lea    -0xc30(%rbp),%rax
lea    0xdfd(%rip),%rsi
lea    -0xc60(%rbp),%r9
lea    -0xc6c(%rbp),%rbx
lea    -0xc70(%rbp),%r11
lea    -0xc68(%rbp),%r10
mov    %rbx,(%rsp)
mov    %r11,0x8(%rsp)
mov    %r10,0x10(%rsp)
mov    %rax,0x18(%rsp)
call   1060 <sscanf@plt>
lea    -0xc30(%rbp),%rdi
lea    0xddd(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    1270 <get_heap_range+0x110>
movl   $0x0,-0x24(%rbp)
jmp    1275 <get_heap_range+0x115>
jmp    11cd <get_heap_range+0x6d>
mov    -0xc58(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0xc88,%rsp
pop    %rbx
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_heap_range>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xc48,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%edx
lea    0xe80(%rip),%rsi
lea    0x40(%rsp),%r12
mov    %r12,%rdi
call   1080 <sprintf@plt>
lea    0xe7a(%rip),%rsi
mov    %r12,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     1220 <get_heap_range+0xc0>
mov    %rax,%rbx
lea    0x440(%rsp),%r13
lea    0xe7f(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r13,%rdi
mov    $0x800,%esi
mov    %rbx,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     1227 <get_heap_range+0xc7>
mov    %r13,%rdi
lea    0xe31(%rip),%rsi
mov    %r15,%rdx
mov    %r14,%rcx
lea    0x20(%rsp),%r8
lea    0x18(%rsp),%r9
push   %r12
lea    0x18(%rsp),%rax
push   %rax
lea    0x18(%rsp),%rax
push   %rax
lea    0x24(%rsp),%rax
push   %rax
call   1060 <sscanf@plt>
add    $0x20,%rsp
mov    %r12,%rdi
mov    %rbp,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
jne    11c0 <get_heap_range+0x60>
xor    %ebp,%ebp
jmp    122c <get_heap_range+0xcc>
mov    $0xffffffff,%ebp
jmp    1234 <get_heap_range+0xd4>
mov    $0xffffffff,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %ebp,%eax
add    $0xc48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/%u/maps\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [34 x i8] c"%zx-%zx %31s %zx %x:%x %zu %1023s\00"
@.str.3 = [7 x i8] c"[heap]\00"
define i32 @get_heap_range(i32 %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca i32
%9 = alloca [2048 x i8]
%10 = alloca [1024 x i8]
%11 = alloca [32 x i8]
%12 = alloca i32*
%13 = alloca i64
%14 = alloca i64
%15 = alloca i32
%16 = alloca i32
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
store i32 -1, i32* %8
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%18 = load i32, i32* %5
%19 = call i32 @sprintf(i8* %17, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %21, i32** %12
%22 = load i32*, i32** %12
%23 = icmp ne i32* %22, null
br i1 %23, label %25, label %24
24: ; preds = %3
store i32 -1, i32* %4
br label %49
25: ; preds = %3
br label %26
26: ; preds = %44, %25
%27 = getelementptr inbounds [2048 x i8], [2048 x i8]* %9, i64 0, i64 0
%28 = load i32*, i32** %12
%29 = call i64 @fgets(i8* %27, i32 2048, i32* %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %45
31: ; preds = %26
%32 = getelementptr inbounds [2048 x i8], [2048 x i8]* %9, i64 0, i64 0
%33 = load i8**, i8*** %6
%34 = bitcast i8** %33 to i64*
%35 = load i8**, i8*** %7
%36 = bitcast i8** %35 to i64*
%37 = getelementptr inbounds [32 x i8], [32 x i8]* %11, i64 0, i64 0
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%39 = call i32 @sscanf(i8* %32, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i64* %34, i64* %36, i8* %37, i64* %13, i32* %15, i32* %16, i64* %14, i8* %38)
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%41 = call i64 @strcmp(i8* %40, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %31
store i32 0, i32* %8
br label %45
44: ; preds = %31
br label %26
45: ; preds = %43, %26
%46 = load i32*, i32** %12
%47 = call i32 @fclose(i32* %46)
%48 = load i32, i32* %8
store i32 %48, i32* %4
br label %49
49: ; preds = %45, %24
%50 = load i32, i32* %4
ret i32 %50
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/%u/maps\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [34 x i8] c"%zx-%zx %31s %zx %x:%x %zu %1023s\00"
@.str.3 = [7 x i8] c"[heap]\00"
define i32 @get_heap_range(i32 %0, i8** %1, i8** %2) {
%4 = alloca [2048 x i8]
%5 = alloca [1024 x i8]
%6 = alloca [32 x i8]
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
%11 = getelementptr inbounds [2048 x i8], [2048 x i8]* %4, i64 0, i64 0
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%13 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%14 = call i32 @sprintf(i8* %12, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %0)
%15 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%16 = icmp eq i32* %15, null
br i1 %16, label %34, label %17
17: ; preds = %3
%18 = bitcast i64* %7 to i8*
%19 = bitcast i64* %8 to i8*
%20 = bitcast i32* %9 to i8*
%21 = bitcast i32* %10 to i8*
%22 = bitcast i8** %1 to i64*
%23 = bitcast i8** %2 to i64*
br label %24
24: ; preds = %27, %17
%25 = call i64 @fgets(i8* %11, i32 2048, i32* %15)
%26 = icmp eq i64 %25, 0
br i1 %26, label %31, label %27
27: ; preds = %24
%28 = call i32 @sscanf(i8* %11, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i64* %22, i64* %23, i8* %13, i64* %7, i32* %9, i32* %10, i64* %8, i8* %12)
%29 = call i64 @strcmp(i8* %12, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %24
31: ; preds = %27, %24
%32 = phi i32 [ -1, %24 ], [ 0, %27 ]
%33 = call i32 @fclose(i32* %15)
br label %34
34: ; preds = %3, %31
%35 = phi i32 [ %32, %31 ], [ -1, %3 ]
ret i32 %35
}