#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 scalar_t__ ssize_t;
typedef int /*<<< orphan*/ read_buf;

/* Variables and functions */
int /*<<< orphan*/ die(char *);
int /*<<< orphan*/ err(int, char *);
int hax_read(char *);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ read(int, char *, int);
int sscanf(char *, char *, unsigned long long *, ...);
char *strchr(char *, char);
char *strstr(char *, char *);
void get_libc_stack(unsigned long long *libc, unsigned long long *stack) {
  int fd = hax_read("/proc/self/maps");
  char read_buf[8 * 4096];
  ssize_t cnt = read(fd, read_buf, sizeof(read_buf));
  if (cnt < 0) {
    err(1, "read");
  }
  printf("read maps: '''%s'''\n", read_buf);
  char *libc_off = strstr(read_buf, "/libc");
  while (*libc_off != '\n') {
    libc_off--;
  }
  if (sscanf(libc_off, "%llx ", libc) != 1) {
    err(1, "scanf");
  }
  char *stack_off = read_buf;
  for (char *stack_off = read_buf; *stack_off;
       stack_off = strchr(stack_off, '\n') + 1) {
    unsigned long long start, end;
    if (sscanf(stack_off, "%llx-%llx", &start, &end) != 2) {
      err(1, "scanf");
    }
    if (end - start == 0x9000) {
      *stack = start;
      return;
    }
  }
  die("stack mapping not found");
}


x86 assembly (clang, -O0):
<get_libc_stack>:
push   %rbp
mov    %rsp,%rbp
sub    $0x8050,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe66(%rip),%rdi
call   1080 <hax_read@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x8020(%rbp),%rsi
mov    $0x8000,%edx
call   1050 <read@plt>
mov    %rax,-0x8028(%rbp)
cmpq   $0x0,-0x8028(%rbp)
jge    11dc <get_libc_stack+0x5c>
mov    $0x1,%edi
lea    0xe39(%rip),%rsi
call   1060 <err@plt>
lea    -0x8020(%rbp),%rsi
lea    0xe2b(%rip),%rdi
call   1040 <printf@plt>
lea    -0x8020(%rbp),%rdi
lea    0xe2d(%rip),%rsi
call   10a0 <strstr@plt>
mov    %rax,-0x8030(%rbp)
mov    -0x8030(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
je     1233 <get_libc_stack+0xb3>
mov    -0x8030(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x8030(%rbp)
jmp    1209 <get_libc_stack+0x89>
mov    -0x8030(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xdeb(%rip),%rsi
mov    $0x0,%al
call   1070 <sscanf@plt>
cmp    $0x1,%eax
je     1266 <get_libc_stack+0xe6>
mov    $0x1,%edi
lea    0xdd5(%rip),%rsi
call   1060 <err@plt>
lea    -0x8020(%rbp),%rax
mov    %rax,-0x8038(%rbp)
lea    -0x8020(%rbp),%rax
mov    %rax,-0x8040(%rbp)
mov    -0x8040(%rbp),%rax
cmpb   $0x0,(%rax)
je     1322 <get_libc_stack+0x1a2>
mov    -0x8040(%rbp),%rdi
lea    0xd9c(%rip),%rsi
lea    -0x8048(%rbp),%rdx
lea    -0x8050(%rbp),%rcx
mov    $0x0,%al
call   1070 <sscanf@plt>
cmp    $0x2,%eax
je     12cf <get_libc_stack+0x14f>
mov    $0x1,%edi
lea    0xd6c(%rip),%rsi
call   1060 <err@plt>
mov    -0x8050(%rbp),%rax
sub    -0x8048(%rbp),%rax
cmp    $0x9000,%rax
jne    12fc <get_libc_stack+0x17c>
mov    -0x8048(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
jmp    132e <get_libc_stack+0x1ae>
jmp    1301 <get_libc_stack+0x181>
mov    -0x8040(%rbp),%rdi
mov    $0xa,%esi
call   1030 <strchr@plt>
add    $0x1,%rax
mov    %rax,-0x8040(%rbp)
jmp    1282 <get_libc_stack+0x102>
lea    0xd1d(%rip),%rdi
call   1090 <die@plt>
add    $0x8050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_libc_stack>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x8018,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe62(%rip),%rdi
call   1080 <hax_read@plt>
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0x8000,%edx
call   1050 <read@plt>
test   %rax,%rax
jns    11ca <get_libc_stack+0x4a>
lea    0xe50(%rip),%rsi
mov    $0x1,%edi
call   1060 <err@plt>
lea    0xe44(%rip),%rdi
lea    0x10(%rsp),%rbx
mov    %rbx,%rsi
call   1040 <printf@plt>
lea    0xe45(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <strstr@plt>
add    $0x1,%rax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmpb   $0xa,-0x1(%rax)
lea    -0x1(%rax),%rax
jne    1200 <get_libc_stack+0x80>
lea    0xe1f(%rip),%rsi
mov    %rax,%rdi
mov    %r15,%rdx
xor    %eax,%eax
call   1070 <sscanf@plt>
cmp    $0x1,%eax
je     1234 <get_libc_stack+0xb4>
lea    0xe0c(%rip),%rsi
mov    $0x1,%edi
call   1060 <err@plt>
cmpb   $0x0,0x10(%rsp)
je     12b1 <get_libc_stack+0x131>
lea    0xdfa(%rip),%r15
lea    0x8(%rsp),%r12
mov    %rsp,%r13
lea    0xde5(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
mov    %r13,%rcx
xor    %eax,%eax
call   1070 <sscanf@plt>
cmp    $0x2,%eax
je     1285 <get_libc_stack+0x105>
mov    $0x1,%edi
mov    %rbp,%rsi
call   1060 <err@plt>
mov    (%rsp),%rcx
mov    0x8(%rsp),%rax
sub    %rax,%rcx
cmp    $0x9000,%rcx
je     12bf <get_libc_stack+0x13f>
mov    %rbx,%rdi
mov    $0xa,%esi
call   1030 <strchr@plt>
lea    0x1(%rax),%rbx
cmpb   $0x0,0x1(%rax)
jne    1260 <get_libc_stack+0xe0>
lea    0xd8e(%rip),%rdi
call   1090 <die@plt>
jmp    12c2 <get_libc_stack+0x142>
mov    %rax,(%r14)
add    $0x8018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"/proc/self/maps\00"
@.str.1 = [5 x i8] c"read\00"
@.str.2 = [21 x i8] c"read maps: '''%s'''\0A\00"
@.str.3 = [6 x i8] c"/libc\00"
@.str.4 = [6 x i8] c"%llx \00"
@.str.5 = [6 x i8] c"scanf\00"
@.str.6 = [10 x i8] c"%llx-%llx\00"
@.str.7 = [24 x i8] c"stack mapping not found\00"
define void @get_libc_stack(i64* %0, i64* %1) {
%3 = alloca i64*
%4 = alloca i64*
%5 = alloca i32
%6 = alloca [32768 x i8]
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i64
store i64* %0, i64** %3
store i64* %1, i64** %4
%13 = call i32 @hax_read(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i32 %13, i32* %5
%14 = load i32, i32* %5
%15 = getelementptr inbounds [32768 x i8], [32768 x i8]* %6, i64 0, i64 0
%16 = call i64 @read(i32 %14, i8* %15, i32 32768)
store i64 %16, i64* %7
%17 = load i64, i64* %7
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %19, %2
%22 = getelementptr inbounds [32768 x i8], [32768 x i8]* %6, i64 0, i64 0
%23 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %22)
%24 = getelementptr inbounds [32768 x i8], [32768 x i8]* %6, i64 0, i64 0
%25 = call i8* @strstr(i8* %24, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
store i8* %25, i8** %8
br label %26
26: ; preds = %31, %21
%27 = load i8*, i8** %8
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp ne i32 %29, 10
br i1 %30, label %31, label %34
31: ; preds = %26
%32 = load i8*, i8** %8
%33 = getelementptr inbounds i8, i8* %32, i32 -1
store i8* %33, i8** %8
br label %26
34: ; preds = %26
%35 = load i8*, i8** %8
%36 = load i64*, i64** %3
%37 = call i32 (i8*, i8*, i64*, ...) @sscanf(i8* %35, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i64* %36)
%38 = icmp ne i32 %37, 1
br i1 %38, label %39, label %41
39: ; preds = %34
%40 = call i32 @err(i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %41
41: ; preds = %39, %34
%42 = getelementptr inbounds [32768 x i8], [32768 x i8]* %6, i64 0, i64 0
store i8* %42, i8** %9
%43 = getelementptr inbounds [32768 x i8], [32768 x i8]* %6, i64 0, i64 0
store i8* %43, i8** %10
br label %44
44: ; preds = %63, %41
%45 = load i8*, i8** %10
%46 = load i8, i8* %45
%47 = icmp ne i8 %46, 0
br i1 %47, label %48, label %67
48: ; preds = %44
%49 = load i8*, i8** %10
%50 = call i32 (i8*, i8*, i64*, ...) @sscanf(i8* %49, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i64* %11, i64* %12)
%51 = icmp ne i32 %50, 2
br i1 %51, label %52, label %54
52: ; preds = %48
%53 = call i32 @err(i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %54
54: ; preds = %52, %48
%55 = load i64, i64* %12
%56 = load i64, i64* %11
%57 = sub i64 %55, %56
%58 = icmp eq i64 %57, 36864
br i1 %58, label %59, label %62
59: ; preds = %54
%60 = load i64, i64* %11
%61 = load i64*, i64** %4
store i64 %60, i64* %61
br label %69
62: ; preds = %54
br label %63
63: ; preds = %62
%64 = load i8*, i8** %10
%65 = call i8* @strchr(i8* %64, i8 signext 10)
%66 = getelementptr inbounds i8, i8* %65, i64 1
store i8* %66, i8** %10
br label %44
67: ; preds = %44
%68 = call i32 @die(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0))
br label %69
69: ; preds = %67, %59
ret void
}

LLVM IR (-O2):
@.str = [16 x i8] c"/proc/self/maps\00"
@.str.1 = [5 x i8] c"read\00"
@.str.2 = [21 x i8] c"read maps: '''%s'''\0A\00"
@.str.3 = [6 x i8] c"/libc\00"
@.str.4 = [6 x i8] c"%llx \00"
@.str.5 = [6 x i8] c"scanf\00"
@.str.6 = [10 x i8] c"%llx-%llx\00"
@.str.7 = [24 x i8] c"stack mapping not found\00"
define void @get_libc_stack(i64* %0, i64* nocapture writeonly %1) {
%3 = alloca [32768 x i8]
%4 = alloca i64
%5 = alloca i64
%6 = tail call i32 @hax_read(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%7 = getelementptr inbounds [32768 x i8], [32768 x i8]* %3, i64 0, i64 0
%8 = call i64 @read(i32 %6, i8* %7, i32 32768)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %10, %2
%13 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %7)
%14 = call i8* @strstr(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %15
15: ; preds = %15, %12
%16 = phi i8* [ %14, %12 ], [ %19, %15 ]
%17 = load i8, i8* %16
%18 = icmp eq i8 %17, 10
%19 = getelementptr inbounds i8, i8* %16, i64 -1
br i1 %18, label %20, label %15
20: ; preds = %15
%21 = call i32 (i8*, i8*, i64*, ...) @sscanf(i8* %16, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i64* %0)
%22 = icmp eq i32 %21, 1
br i1 %22, label %25, label %23
23: ; preds = %20
%24 = call i32 @err(i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %25
25: ; preds = %23, %20
%26 = load i8, i8* %7
%27 = icmp eq i8 %26, 0
br i1 %27, label %48, label %28
28: ; preds = %25
%29 = bitcast i64* %4 to i8*
%30 = bitcast i64* %5 to i8*
br label %31
31: ; preds = %28, %43
%32 = phi i8* [ %7, %28 ], [ %45, %43 ]
%33 = call i32 (i8*, i8*, i64*, ...) @sscanf(i8* %32, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i64* %4, i64* %5)
%34 = icmp eq i32 %33, 2
br i1 %34, label %37, label %35
35: ; preds = %31
%36 = call i32 @err(i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %37
37: ; preds = %35, %31
%38 = load i64, i64* %5
%39 = load i64, i64* %4
%40 = sub i64 %38, %39
%41 = icmp eq i64 %40, 36864
br i1 %41, label %42, label %43
42: ; preds = %37
store i64 %39, i64* %1
br label %50
43: ; preds = %37
%44 = call i8* @strchr(i8* %32, i8 signext 10)
%45 = getelementptr inbounds i8, i8* %44, i64 1
%46 = load i8, i8* %45
%47 = icmp eq i8 %46, 0
br i1 %47, label %48, label %31
48: ; preds = %43, %25
%49 = call i32 @die(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0))
br label %50
50: ; preds = %42, %48
ret void
}