#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
size_t case_get_maps_len();
int case_repeat_read(int, char *, size_t);
int /*<<< orphan*/ close(int);
char *malloc(size_t);
int open(char *, int /*<<< orphan*/);
char *case_get_maps() {
  int f;
  int result;
  static char init_buf[1];
  static char *maps_buf = init_buf;
  static size_t maps_buf_sz = 1;
  size_t maps_size, old_maps_size = 0;
  maps_size = case_get_maps_len();
  if (0 == maps_size)
    return 0;
  do {
    while (maps_size >= maps_buf_sz) {
      while (maps_size >= maps_buf_sz)
        maps_buf_sz *= 2;
      maps_buf = malloc(maps_buf_sz);
      maps_size = case_get_maps_len();
      if (0 == maps_size)
        return 0;
    }
    f = open("/proc/self/maps", O_RDONLY);
    if (-1 == f)
      return 0;
    old_maps_size = maps_size;
    maps_size = 0;
    do {
      result = case_repeat_read(f, maps_buf, maps_buf_sz - 1);
      if (result <= 0)
        return 0;
      maps_size += result;
    } while (result == maps_buf_sz - 1);
    close(f);
  } while (maps_size >= maps_buf_sz || maps_size < old_maps_size);
  maps_buf[maps_size] = '\0';
  return maps_buf;
}


x86 assembly (clang, -O0):
<case_get_maps>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movq   $0x0,-0x20(%rbp)
mov    $0x0,%al
call   1060 <case_get_maps_len@plt>
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    1184 <case_get_maps+0x34>
movq   $0x0,-0x8(%rbp)
jmp    12f4 <case_get_maps+0x1a4>
jmp    1189 <case_get_maps+0x39>
jmp    118e <case_get_maps+0x3e>
mov    -0x18(%rbp),%rax
cmp    0x2eb7(%rip),%rax
jb     1208 <case_get_maps+0xb8>
jmp    11a4 <case_get_maps+0x54>
mov    -0x18(%rbp),%rax
cmp    0x2ea1(%rip),%rax
jb     11cc <case_get_maps+0x7c>
mov    0x2e94(%rip),%rax
shl    $0x1,%rax
mov    %rax,0x2e89(%rip)
jmp    11a4 <case_get_maps+0x54>
mov    0x2e7d(%rip),%rdi
call   1050 <malloc@plt>
mov    %rax,0x2e69(%rip)
mov    $0x0,%al
call   1060 <case_get_maps_len@plt>
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    1203 <case_get_maps+0xb3>
movq   $0x0,-0x8(%rbp)
jmp    12f4 <case_get_maps+0x1a4>
jmp    118e <case_get_maps+0x3e>
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
lea    0xde8(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
mov    $0xffffffff,%eax
cmp    -0xc(%rbp),%eax
jne    123b <case_get_maps+0xeb>
movq   $0x0,-0x8(%rbp)
jmp    12f4 <case_get_maps+0x1a4>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2df3(%rip),%rsi
mov    0x2df4(%rip),%rdx
sub    $0x1,%rdx
call   1030 <case_repeat_read@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jg     127f <case_get_maps+0x12f>
movq   $0x0,-0x8(%rbp)
jmp    12f4 <case_get_maps+0x1a4>
movslq -0x10(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
movslq -0x10(%rbp),%rax
mov    0x2dba(%rip),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
je     124b <case_get_maps+0xfb>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%rcx
mov    $0x1,%al
cmp    0x2d98(%rip),%rcx
mov    %al,-0x21(%rbp)
jae    12cf <case_get_maps+0x17f>
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
setb   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1189 <case_get_maps+0x39>
mov    0x2d67(%rip),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    0x2d58(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<case_get_maps>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %r15d,%r15d
xor    %eax,%eax
call   1060 <case_get_maps_len@plt>
test   %rax,%rax
je     124c <case_get_maps+0xfc>
mov    %rax,%rbx
mov    0x2e70(%rip),%r12
lea    0xe81(%rip),%r14
xor    %r15d,%r15d
mov    %rbx,%rbp
mov    0x2ec4(%rip),%rdi
cmp    %rdi,%rbx
jae    1210 <case_get_maps+0xc0>
mov    (%r12),%esi
mov    %r14,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     124c <case_get_maps+0xfc>
mov    %eax,%r13d
mov    0x2ea0(%rip),%rdx
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x2e81(%rip),%rsi
add    $0xffffffffffffffff,%rdx
mov    %r13d,%edi
call   1030 <case_repeat_read@plt>
test   %eax,%eax
jle    124c <case_get_maps+0xfc>
mov    %eax,%eax
add    %rax,%rbx
mov    0x2e6d(%rip),%rdx
lea    -0x1(%rdx),%rcx
cmp    %rax,%rcx
je     11c0 <case_get_maps+0x70>
mov    %r13d,%edi
call   1040 <close@plt>
cmp    0x2e55(%rip),%rbx
jae    1182 <case_get_maps+0x32>
cmp    %rbp,%rbx
jb     1182 <case_get_maps+0x32>
jmp    1240 <case_get_maps+0xf0>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    %rdi,%rdi
cmp    %rdi,%rbp
jae    1210 <case_get_maps+0xc0>
mov    %rdi,0x2e31(%rip)
call   1050 <malloc@plt>
mov    %rax,0x2e1d(%rip)
xor    %eax,%eax
call   1060 <case_get_maps_len@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1182 <case_get_maps+0x32>
jmp    124c <case_get_maps+0xfc>
mov    0x2e01(%rip),%r15
movb   $0x0,(%r15,%rbx,1)
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@case_get_maps.init_buf = internal global [1 x i8] zeroinitializer
@case_get_maps.maps_buf = internal global i8* getelementptr inbounds ([1 x i8], [1 x i8]* @case_get_maps.init_buf, i32 0, i32 0)
@case_get_maps.maps_buf_sz = internal global i64 1
@.str = [16 x i8] c"/proc/self/maps\00"
@O_RDONLY = global i32 0
define i8* @case_get_maps() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca i32
%4 = alloca i64
%5 = alloca i64
store i64 0, i64* %5
%6 = call i64 (...) @case_get_maps_len()
store i64 %6, i64* %4
%7 = load i64, i64* %4
%8 = icmp eq i64 0, %7
br i1 %8, label %9, label %10
9: ; preds = %0
store i8* null, i8** %1
br label %78
10: ; preds = %0
br label %11
11: ; preds = %71, %10
br label %12
12: ; preds = %31, %11
%13 = load i64, i64* %4
%14 = load i64, i64* @case_get_maps.maps_buf_sz
%15 = icmp uge i64 %13, %14
br i1 %15, label %16, label %32
16: ; preds = %12
br label %17
17: ; preds = %21, %16
%18 = load i64, i64* %4
%19 = load i64, i64* @case_get_maps.maps_buf_sz
%20 = icmp uge i64 %18, %19
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = load i64, i64* @case_get_maps.maps_buf_sz
%23 = mul i64 %22, 2
store i64 %23, i64* @case_get_maps.maps_buf_sz
br label %17
24: ; preds = %17
%25 = load i64, i64* @case_get_maps.maps_buf_sz
%26 = call i8* @malloc(i64 %25)
store i8* %26, i8** @case_get_maps.maps_buf
%27 = call i64 (...) @case_get_maps_len()
store i64 %27, i64* %4
%28 = load i64, i64* %4
%29 = icmp eq i64 0, %28
br i1 %29, label %30, label %31
30: ; preds = %24
store i8* null, i8** %1
br label %78
31: ; preds = %24
br label %12
32: ; preds = %12
%33 = load i32, i32* @O_RDONLY
%34 = call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %33)
store i32 %34, i32* %2
%35 = load i32, i32* %2
%36 = icmp eq i32 -1, %35
br i1 %36, label %37, label %38
37: ; preds = %32
store i8* null, i8** %1
br label %78
38: ; preds = %32
%39 = load i64, i64* %4
store i64 %39, i64* %5
store i64 0, i64* %4
br label %40
40: ; preds = %54, %38
%41 = load i32, i32* %2
%42 = load i8*, i8** @case_get_maps.maps_buf
%43 = load i64, i64* @case_get_maps.maps_buf_sz
%44 = sub i64 %43, 1
%45 = call i32 @case_repeat_read(i32 %41, i8* %42, i64 %44)
store i32 %45, i32* %3
%46 = load i32, i32* %3
%47 = icmp sle i32 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %40
store i8* null, i8** %1
br label %78
49: ; preds = %40
%50 = load i32, i32* %3
%51 = sext i32 %50 to i64
%52 = load i64, i64* %4
%53 = add i64 %52, %51
store i64 %53, i64* %4
br label %54
54: ; preds = %49
%55 = load i32, i32* %3
%56 = sext i32 %55 to i64
%57 = load i64, i64* @case_get_maps.maps_buf_sz
%58 = sub i64 %57, 1
%59 = icmp eq i64 %56, %58
br i1 %59, label %40, label %60
60: ; preds = %54
%61 = load i32, i32* %2
%62 = call i32 @close(i32 %61)
br label %63
63: ; preds = %60
%64 = load i64, i64* %4
%65 = load i64, i64* @case_get_maps.maps_buf_sz
%66 = icmp uge i64 %64, %65
br i1 %66, label %71, label %67
67: ; preds = %63
%68 = load i64, i64* %4
%69 = load i64, i64* %5
%70 = icmp ult i64 %68, %69
br label %71
71: ; preds = %67, %63
%72 = phi i1 [ true, %63 ], [ %70, %67 ]
br i1 %72, label %11, label %73
73: ; preds = %71
%74 = load i8*, i8** @case_get_maps.maps_buf
%75 = load i64, i64* %4
%76 = getelementptr inbounds i8, i8* %74, i64 %75
store i8 0, i8* %76
%77 = load i8*, i8** @case_get_maps.maps_buf
store i8* %77, i8** %1
br label %78
78: ; preds = %73, %48, %37, %30, %9
%79 = load i8*, i8** %1
ret i8* %79
}

LLVM IR (-O2):
@case_get_maps.init_buf = internal global [1 x i8] zeroinitializer
@case_get_maps.maps_buf = internal unnamed_addr global i8* getelementptr inbounds ([1 x i8], [1 x i8]* @case_get_maps.init_buf, i64 0, i64 0)
@case_get_maps.maps_buf_sz = internal unnamed_addr global i64 1
@.str = [16 x i8] c"/proc/self/maps\00"
@O_RDONLY = global i32 0
define i8* @case_get_maps() {
%1 = tail call i64 (...) @case_get_maps_len()
%2 = icmp eq i64 %1, 0
br i1 %2, label %45, label %3
3: ; preds = %0, %15
%4 = phi i64 [ %16, %15 ], [ %1, %0 ]
%5 = load i64, i64* @case_get_maps.maps_buf_sz
%6 = icmp ult i64 %4, %5
br i1 %6, label %17, label %7
7: ; preds = %3, %7
%8 = phi i64 [ %9, %7 ], [ %5, %3 ]
%9 = shl i64 %8, 1
%10 = icmp ult i64 %4, %9
br i1 %10, label %11, label %7
11: ; preds = %7
store i64 %9, i64* @case_get_maps.maps_buf_sz
%12 = tail call i8* @malloc(i64 %9)
store i8* %12, i8** @case_get_maps.maps_buf
%13 = tail call i64 (...) @case_get_maps_len()
%14 = icmp eq i64 %13, 0
br i1 %14, label %45, label %15
15: ; preds = %11, %36
%16 = phi i64 [ %13, %11 ], [ %32, %36 ]
br label %3
17: ; preds = %3
%18 = load i32, i32* @O_RDONLY
%19 = tail call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %45, label %21
21: ; preds = %17
%22 = load i64, i64* @case_get_maps.maps_buf_sz
br label %23
23: ; preds = %21, %30
%24 = phi i64 [ %33, %30 ], [ %22, %21 ]
%25 = phi i64 [ %32, %30 ], [ 0, %21 ]
%26 = load i8*, i8** @case_get_maps.maps_buf
%27 = add i64 %24, -1
%28 = tail call i32 @case_repeat_read(i32 %19, i8* %26, i64 %27)
%29 = icmp slt i32 %28, 1
br i1 %29, label %45, label %30
30: ; preds = %23
%31 = zext i32 %28 to i64
%32 = add i64 %25, %31
%33 = load i64, i64* @case_get_maps.maps_buf_sz
%34 = add i64 %33, -1
%35 = icmp eq i64 %34, %31
br i1 %35, label %23, label %36
36: ; preds = %30
%37 = tail call i32 @close(i32 %19)
%38 = load i64, i64* @case_get_maps.maps_buf_sz
%39 = icmp uge i64 %32, %38
%40 = icmp ult i64 %32, %4
%41 = select i1 %39, i1 true, i1 %40
br i1 %41, label %15, label %42
42: ; preds = %36
%43 = load i8*, i8** @case_get_maps.maps_buf
%44 = getelementptr inbounds i8, i8* %43, i64 %32
store i8 0, i8* %44
br label %45
45: ; preds = %17, %11, %23, %0, %42
%46 = phi i8* [ %43, %42 ], [ null, %0 ], [ null, %23 ], [ null, %11 ], [ null, %17 ]
ret i8* %46
}