#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*/ tmp;
typedef size_t ssize_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ abort();
int /*<<< orphan*/ asprintf(char **, char *, int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ ferror(int /*<<< orphan*/ *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ getpid();
char *malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ *popen(char *, char *);
char *realloc(char *, size_t);
char *vmmap() {
  char *cmd = NULL;
  asprintf(&cmd, "vmmap %d", getpid());
  FILE *f = popen(cmd, "r");
  size_t size = 10240;
  char *buf = malloc(size);
  ssize_t off = 0;
  char tmp[1025];
  while (!feof(f) && !ferror(f)) {
    size_t n = fread(tmp, 1, sizeof(tmp), f);
    if (n == 0) {
      break;
    }
    if (n > size - off - 1) {
      size += 10240;
      char *nbuf = realloc(buf, size);
      if (!nbuf) {
        perror("realloc failed");
        abort();
      }
      buf = nbuf;
    }
    memcpy(&buf[off], tmp, n);
    off += n;
    buf[off] = '\0';
  }
  fclose(f);
  return buf;
}


x86 assembly (clang, -O0):
<vmmap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
movq   $0x0,-0x8(%rbp)
mov    $0x0,%al
call   1060 <getpid@plt>
mov    %eax,%edx
lea    -0x8(%rbp),%rdi
lea    0xe19(%rip),%rsi
call   10c0 <asprintf@plt>
mov    -0x8(%rbp),%rdi
lea    0xe12(%rip),%rsi
call   10d0 <popen@plt>
mov    %rax,-0x10(%rbp)
movq   $0x2800,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   10a0 <malloc@plt>
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <feof@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x441(%rbp)
jne    1250 <vmmap+0x90>
mov    -0x10(%rbp),%rdi
call   1040 <ferror@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x441(%rbp)
mov    -0x441(%rbp),%al
test   $0x1,%al
jne    1263 <vmmap+0xa3>
jmp    133c <vmmap+0x17c>
lea    -0x430(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x401,%edx
call   1050 <fread@plt>
mov    %rax,-0x438(%rbp)
cmpq   $0x0,-0x438(%rbp)
jne    1297 <vmmap+0xd7>
jmp    133c <vmmap+0x17c>
mov    -0x438(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    -0x28(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jbe    1301 <vmmap+0x141>
mov    -0x18(%rbp),%rax
add    $0x2800,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10b0 <realloc@plt>
mov    %rax,-0x440(%rbp)
cmpq   $0x0,-0x440(%rbp)
jne    12f6 <vmmap+0x136>
lea    0xd21(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x440(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
add    -0x28(%rbp),%rdi
lea    -0x430(%rbp),%rsi
mov    -0x438(%rbp),%rdx
call   1090 <memcpy@plt>
mov    -0x438(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    121d <vmmap+0x5d>
mov    -0x10(%rbp),%rdi
call   1070 <fclose@plt>
mov    -0x20(%rbp),%rax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vmmap>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
movq   $0x0,0x8(%rsp)
xor    %eax,%eax
call   1060 <getpid@plt>
lea    0xe18(%rip),%rsi
lea    0x8(%rsp),%rdi
mov    %eax,%edx
call   10c0 <asprintf@plt>
mov    0x8(%rsp),%rdi
lea    0xe09(%rip),%rsi
call   10d0 <popen@plt>
mov    %rax,%r14
mov    $0x2800,%r13d
mov    $0x2800,%edi
call   10a0 <malloc@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1080 <feof@plt>
test   %eax,%eax
je     1244 <vmmap+0x84>
mov    %r14,%rdi
call   1070 <fclose@plt>
mov    %r15,%rax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %ebp,%ebp
lea    0x10(%rsp),%r12
jmp    1273 <vmmap+0xb3>
nopl   (%rax)
lea    (%r15,%rbp,1),%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1090 <memcpy@plt>
add    %rbx,%rbp
movb   $0x0,(%r15,%rbp,1)
mov    %r14,%rdi
call   1080 <feof@plt>
test   %eax,%eax
jne    1227 <vmmap+0x67>
mov    %r14,%rdi
call   1040 <ferror@plt>
test   %eax,%eax
jne    1227 <vmmap+0x67>
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x401,%edx
mov    %r14,%rcx
call   1050 <fread@plt>
test   %rax,%rax
je     1227 <vmmap+0x67>
mov    %rax,%rbx
mov    %rbp,%rax
not    %rax
add    %r13,%rax
cmp    %rax,%rbx
jbe    1250 <vmmap+0x90>
add    $0x2800,%r13
mov    %r15,%rdi
mov    %r13,%rsi
call   10b0 <realloc@plt>
mov    %rax,%r15
test   %rax,%rax
jne    1250 <vmmap+0x90>
lea    0xd40(%rip),%rdi
call   10e0 <perror@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"vmmap %d\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [15 x i8] c"realloc failed\00"
define i8* @vmmap() {
%1 = alloca i8*
%2 = alloca i32*
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i64
%6 = alloca [1025 x i8]
%7 = alloca i64
%8 = alloca i8*
store i8* null, i8** %1
%9 = call i32 (...) @getpid()
%10 = call i32 @asprintf(i8** %1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = load i8*, i8** %1
%12 = call i32* @popen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %12, i32** %2
store i64 10240, i64* %3
%13 = load i64, i64* %3
%14 = call i8* @malloc(i64 %13)
store i8* %14, i8** %4
store i64 0, i64* %5
br label %15
15: ; preds = %53, %0
%16 = load i32*, i32** %2
%17 = call i32 @feof(i32* %16)
%18 = icmp ne i32 %17, 0
br i1 %18, label %24, label %19
19: ; preds = %15
%20 = load i32*, i32** %2
%21 = call i32 @ferror(i32* %20)
%22 = icmp ne i32 %21, 0
%23 = xor i1 %22, true
br label %24
24: ; preds = %19, %15
%25 = phi i1 [ false, %15 ], [ %23, %19 ]
br i1 %25, label %26, label %66
26: ; preds = %24
%27 = getelementptr inbounds [1025 x i8], [1025 x i8]* %6, i64 0, i64 0
%28 = load i32*, i32** %2
%29 = call i64 @fread(i8* %27, i32 1, i32 1025, i32* %28)
store i64 %29, i64* %7
%30 = load i64, i64* %7
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %26
br label %66
33: ; preds = %26
%34 = load i64, i64* %7
%35 = load i64, i64* %3
%36 = load i64, i64* %5
%37 = sub i64 %35, %36
%38 = sub i64 %37, 1
%39 = icmp ugt i64 %34, %38
br i1 %39, label %40, label %53
40: ; preds = %33
%41 = load i64, i64* %3
%42 = add i64 %41, 10240
store i64 %42, i64* %3
%43 = load i8*, i8** %4
%44 = load i64, i64* %3
%45 = call i8* @realloc(i8* %43, i64 %44)
store i8* %45, i8** %8
%46 = load i8*, i8** %8
%47 = icmp ne i8* %46, null
br i1 %47, label %51, label %48
48: ; preds = %40
%49 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%50 = call i32 (...) @abort()
unreachable
51: ; preds = %40
%52 = load i8*, i8** %8
store i8* %52, i8** %4
br label %53
53: ; preds = %51, %33
%54 = load i8*, i8** %4
%55 = load i64, i64* %5
%56 = getelementptr inbounds i8, i8* %54, i64 %55
%57 = getelementptr inbounds [1025 x i8], [1025 x i8]* %6, i64 0, i64 0
%58 = load i64, i64* %7
%59 = call i32 @memcpy(i8* %56, i8* %57, i64 %58)
%60 = load i64, i64* %7
%61 = load i64, i64* %5
%62 = add i64 %61, %60
store i64 %62, i64* %5
%63 = load i8*, i8** %4
%64 = load i64, i64* %5
%65 = getelementptr inbounds i8, i8* %63, i64 %64
store i8 0, i8* %65
br label %15
66: ; preds = %32, %24
%67 = load i32*, i32** %2
%68 = call i32 @fclose(i32* %67)
%69 = load i8*, i8** %4
ret i8* %69
}

LLVM IR (-O2):
@.str = [9 x i8] c"vmmap %d\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [15 x i8] c"realloc failed\00"
define i8* @vmmap() {
%1 = alloca i8*
%2 = alloca [1025 x i8]
%3 = bitcast i8** %1 to i8*
store i8* null, i8** %1
%4 = tail call i32 (...) @getpid()
%5 = call i32 @asprintf(i8** %1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = load i8*, i8** %1
%7 = call i32* @popen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = call dereferenceable_or_null(10240) i8* @malloc(i64 10240)
%9 = getelementptr inbounds [1025 x i8], [1025 x i8]* %2, i64 0, i64 0
%10 = call i32 @feof(i32* %7)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %41
12: ; preds = %0, %32
%13 = phi i64 [ %34, %32 ], [ 10240, %0 ]
%14 = phi i8* [ %33, %32 ], [ %8, %0 ]
%15 = phi i64 [ %37, %32 ], [ 0, %0 ]
%16 = call i32 @ferror(i32* %7)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %41
18: ; preds = %12
%19 = call i64 @fread(i8* %9, i32 1, i32 1025, i32* %7)
%20 = icmp eq i64 %19, 0
br i1 %20, label %41, label %21
21: ; preds = %18
%22 = xor i64 %15, -1
%23 = add i64 %13, %22
%24 = icmp ugt i64 %19, %23
br i1 %24, label %25, label %32
25: ; preds = %21
%26 = add i64 %13, 10240
%27 = call i8* @realloc(i8* %14, i64 %26)
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%31 = call i32 (...) @abort()
unreachable
32: ; preds = %21, %25
%33 = phi i8* [ %14, %21 ], [ %27, %25 ]
%34 = phi i64 [ %13, %21 ], [ %26, %25 ]
%35 = getelementptr inbounds i8, i8* %33, i64 %15
%36 = call i32 @memcpy(i8* %35, i8* %9, i64 %19)
%37 = add i64 %19, %15
%38 = getelementptr inbounds i8, i8* %33, i64 %37
store i8 0, i8* %38
%39 = call i32 @feof(i32* %7)
%40 = icmp eq i32 %39, 0
br i1 %40, label %12, label %41
41: ; preds = %12, %32, %18, %0
%42 = phi i8* [ %8, %0 ], [ %14, %18 ], [ %33, %32 ], [ %14, %12 ]
%43 = call i32 @fclose(i32* %7)
ret i8* %42
}