#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 *);
scalar_t__ strstr(char *, char *);
int /*<<< orphan*/ strtok(char *, char *);
uintptr_t strtoll(int /*<<< orphan*/, int /*<<< orphan*/ *, int);
void get_heap_bounds(uintptr_t *st, uintptr_t *en) {
  FILE *map;
  char buf[256];
  if (!(map = fopen("/proc/self/maps", "r")))
    return;
  while (fgets(buf, sizeof(buf), map))
    if (strstr(buf, "[heap]")) {
      *st = strtoll(strtok(buf, "-"), NULL, 16);
      *en = strtoll(strtok(NULL, " "), NULL, 16);
      break;
    }
  fclose(map);
}


x86 assembly (clang, -O0):
<get_heap_bounds>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe86(%rip),%rdi
lea    0xe8f(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1199 <get_heap_bounds+0x39>
jmp    1244 <get_heap_bounds+0xe4>
jmp    119e <get_heap_bounds+0x3e>
lea    -0x120(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x100,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     123b <get_heap_bounds+0xdb>
lea    -0x120(%rbp),%rdi
lea    0xe47(%rip),%rsi
call   1080 <strstr@plt>
cmp    $0x0,%rax
je     1236 <get_heap_bounds+0xd6>
lea    -0x120(%rbp),%rdi
lea    0xe31(%rip),%rsi
call   1070 <strtok@plt>
mov    %eax,%edi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1050 <strtoll@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
xor    %eax,%eax
mov    %eax,%edi
lea    0xe09(%rip),%rsi
call   1070 <strtok@plt>
mov    %eax,%edi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1050 <strtoll@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
jmp    123b <get_heap_bounds+0xdb>
jmp    119e <get_heap_bounds+0x3e>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_heap_bounds>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x100,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe83(%rip),%rdi
lea    0xe8c(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     120c <get_heap_bounds+0xac>
mov    %rax,%r12
mov    %rsp,%rbx
lea    0xe77(%rip),%r13
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %r12,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     1204 <get_heap_bounds+0xa4>
mov    %rbx,%rdi
mov    %r13,%rsi
call   1080 <strstr@plt>
test   %rax,%rax
je     11a0 <get_heap_bounds+0x40>
lea    0xe4d(%rip),%rsi
mov    %rsp,%rdi
call   1070 <strtok@plt>
mov    %eax,%edi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <strtoll@plt>
mov    %rax,(%r15)
lea    0xe2f(%rip),%rsi
xor    %edi,%edi
call   1070 <strtok@plt>
mov    %eax,%edi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <strtoll@plt>
mov    %rax,(%r14)
mov    %r12,%rdi
call   1030 <fclose@plt>
add    $0x100,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [16 x i8] c"/proc/self/maps\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [7 x i8] c"[heap]\00"
@.str.3 = [2 x i8] c"-\00"
@.str.4 = [2 x i8] c" \00"
define void @get_heap_bounds(i64* %0, i64* %1) {
%3 = alloca i64*
%4 = alloca i64*
%5 = alloca i32*
%6 = alloca [256 x i8]
store i64* %0, i64** %3
store i64* %1, i64** %4
%7 = 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* %7, i32** %5
%8 = icmp ne i32* %7, null
br i1 %8, label %10, label %9
9: ; preds = %2
br label %32
10: ; preds = %2
br label %11
11: ; preds = %28, %10
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%13 = load i32*, i32** %5
%14 = call i64 @fgets(i8* %12, i32 256, i32* %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %29
16: ; preds = %11
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%18 = call i64 @strstr(i8* %17, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %28
20: ; preds = %16
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%22 = call i32 @strtok(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%23 = call i64 @strtoll(i32 %22, i32* null, i32 16)
%24 = load i64*, i64** %3
store i64 %23, i64* %24
%25 = call i32 @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%26 = call i64 @strtoll(i32 %25, i32* null, i32 16)
%27 = load i64*, i64** %4
store i64 %26, i64* %27
br label %29
28: ; preds = %16
br label %11
29: ; preds = %20, %11
%30 = load i32*, i32** %5
%31 = call i32 @fclose(i32* %30)
br label %32
32: ; preds = %29, %9
ret void
}

LLVM IR (-O2):
@.str = [16 x i8] c"/proc/self/maps\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [7 x i8] c"[heap]\00"
@.str.3 = [2 x i8] c"-\00"
@.str.4 = [2 x i8] c" \00"
define void @get_heap_bounds(i64* nocapture writeonly %0, i64* nocapture writeonly %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = 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))
%6 = icmp eq i32* %5, null
br i1 %6, label %20, label %7
7: ; preds = %2, %10
%8 = call i64 @fgets(i8* %4, i32 256, i32* %5)
%9 = icmp eq i64 %8, 0
br i1 %9, label %18, label %10
10: ; preds = %7
%11 = call i64 @strstr(i8* %4, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%12 = icmp eq i64 %11, 0
br i1 %12, label %7, label %13
13: ; preds = %10
%14 = call i32 @strtok(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%15 = call i64 @strtoll(i32 %14, i32* null, i32 16)
store i64 %15, i64* %0
%16 = call i32 @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%17 = call i64 @strtoll(i32 %16, i32* null, i32 16)
store i64 %17, i64* %1
br label %18
18: ; preds = %7, %13
%19 = call i32 @fclose(i32* %5)
br label %20
20: ; preds = %2, %18
ret void
}