#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 */
struct rusage {
  int /*<<< orphan*/ ru_maxrss;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ RUSAGE_SELF;
int /*<<< orphan*/ exit(int);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int getpid();
scalar_t__ getrusage(int /*<<< orphan*/, struct rusage *);
scalar_t__ isspace(char);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char const *, int /*<<< orphan*/, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
char *strchr(char *, char);
char *strstr(char *, char *);
void report_memory(const char *prefix) {
  struct rusage ru;
  if (getrusage(RUSAGE_SELF, &ru)) {
    perror("getrusage");
    exit(1);
  }
  char statusfilename[256];
  snprintf(statusfilename, 256, "/proc/%d/status", getpid());
  FILE *f = fopen(statusfilename, "r");
  if (!f) {
    perror("fopen");
    exit(1);
  }
  char buf[256] = {'\0'};
  char *bufstart = buf;
  while (fgets(buf, 256, f)) {
    if (strstr(buf, "VmSize") == buf) {
      bufstart = buf + 7;
      while (isspace(*bufstart)) {
        bufstart++;
      }
      char *pos = strchr(bufstart, '\n');
      if (pos) {
        *pos = '\0';
      }
      break;
    }
  }
  printf("%s: max RSS = %ld kB; vm size = %s\n", prefix, ru.ru_maxrss,
         bufstart);
}


x86 assembly (clang, -O0):
<report_memory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   10b0 <getrusage@plt>
cmp    $0x0,%rax
je     1201 <report_memory+0x41>
lea    0xe0e(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    -0x110(%rbp),%rax
mov    %rax,-0x238(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x238(%rbp),%rdi
mov    %eax,%ecx
mov    $0x100,%esi
lea    0xddf(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x110(%rbp),%rdi
lea    0xddc(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x118(%rbp)
cmpq   $0x0,-0x118(%rbp)
jne    126e <report_memory+0xae>
lea    0xdbd(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    -0x220(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1070 <memset@plt>
lea    -0x220(%rbp),%rax
mov    %rax,-0x228(%rbp)
lea    -0x220(%rbp),%rdi
mov    -0x118(%rbp),%rdx
mov    $0x100,%esi
call   1090 <fgets@plt>
cmp    $0x0,%rax
je     1350 <report_memory+0x190>
lea    -0x220(%rbp),%rdi
lea    0xd63(%rip),%rsi
call   10e0 <strstr@plt>
lea    -0x220(%rbp),%rcx
cmp    %rcx,%rax
jne    134b <report_memory+0x18b>
lea    -0x220(%rbp),%rax
add    $0x7,%rax
mov    %rax,-0x228(%rbp)
mov    -0x228(%rbp),%rax
movsbl (%rax),%edi
call   1080 <isspace@plt>
cmp    $0x0,%rax
je     1316 <report_memory+0x156>
mov    -0x228(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x228(%rbp)
jmp    12e6 <report_memory+0x126>
mov    -0x228(%rbp),%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
mov    %rax,-0x230(%rbp)
cmpq   $0x0,-0x230(%rbp)
je     1346 <report_memory+0x186>
mov    -0x230(%rbp),%rax
movb   $0x0,(%rax)
jmp    1350 <report_memory+0x190>
jmp    128f <report_memory+0xcf>
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%edx
mov    -0x228(%rbp),%rcx
lea    0xcc4(%rip),%rdi
call   1050 <printf@plt>
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<report_memory>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x218,%rsp
mov    %rdi,%r14
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   10a0 <getrusage@plt>
test   %rax,%rax
jne    130b <report_memory+0x16b>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe30(%rip),%rdx
lea    0x110(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %eax,%ecx
call   1060 <snprintf@plt>
lea    0xe22(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1314 <report_memory+0x174>
mov    %rax,%r15
xorps  %xmm0,%xmm0
movaps %xmm0,0x100(%rsp)
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
lea    0x10(%rsp),%rbx
mov    $0x69536d56,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %r15,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     12e7 <report_memory+0x147>
mov    0x10(%rsp),%eax
xor    %ebp,%eax
movzwl 0x14(%rsp),%ecx
xor    $0x657a,%ecx
or     %eax,%ecx
jne    1290 <report_memory+0xf0>
lea    0x16(%rsp),%rbx
nop
movsbl 0x1(%rbx),%edi
add    $0x1,%rbx
call   1070 <isspace@plt>
test   %rax,%rax
jne    12c0 <report_memory+0x120>
mov    %rbx,%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     12e7 <report_memory+0x147>
movb   $0x0,(%rax)
mov    0x8(%rsp),%edx
lea    0xd37(%rip),%rdi
mov    %r14,%rsi
mov    %rbx,%rcx
call   1050 <printf@plt>
add    $0x218,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcee(%rip),%rdi
jmp    131b <report_memory+0x17b>
lea    0xd01(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.rusage = type { i32 }
@RUSAGE_SELF = global i32 0
@.str = [10 x i8] c"getrusage\00"
@.str.1 = [16 x i8] c"/proc/%d/status\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [6 x i8] c"fopen\00"
@.str.4 = [7 x i8] c"VmSize\00"
@.str.5 = [36 x i8] c"%s: max RSS = %ld kB; vm size = %s\0A\00"
define void @report_memory(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.rusage
%4 = alloca [256 x i8]
%5 = alloca i32*
%6 = alloca [256 x i8]
%7 = alloca i8*
%8 = alloca i8*
store i8* %0, i8** %2
%9 = load i32, i32* @RUSAGE_SELF
%10 = call i64 @getrusage(i32 %9, %struct.rusage* %3)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %1
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%17 = call i32 (...) @getpid()
%18 = call i32 @snprintf(i8* %16, i32 256, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%20 = call i32* @fopen(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %20, i32** %5
%21 = load i32*, i32** %5
%22 = icmp ne i32* %21, null
br i1 %22, label %26, label %23
23: ; preds = %15
%24 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %15
%27 = bitcast [256 x i8]* %6 to i8*
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
store i8* %28, i8** %7
br label %29
29: ; preds = %58, %26
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%31 = load i32*, i32** %5
%32 = call i64 @fgets(i8* %30, i32 256, i32* %31)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %59
34: ; preds = %29
%35 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%36 = call i8* @strstr(i8* %35, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%37 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%38 = icmp eq i8* %36, %37
br i1 %38, label %39, label %58
39: ; preds = %34
%40 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%41 = getelementptr inbounds i8, i8* %40, i64 7
store i8* %41, i8** %7
br label %42
42: ; preds = %47, %39
%43 = load i8*, i8** %7
%44 = load i8, i8* %43
%45 = call i64 @isspace(i8 signext %44)
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %42
%48 = load i8*, i8** %7
%49 = getelementptr inbounds i8, i8* %48, i32 1
store i8* %49, i8** %7
br label %42
50: ; preds = %42
%51 = load i8*, i8** %7
%52 = call i8* @strchr(i8* %51, i8 signext 10)
store i8* %52, i8** %8
%53 = load i8*, i8** %8
%54 = icmp ne i8* %53, null
br i1 %54, label %55, label %57
55: ; preds = %50
%56 = load i8*, i8** %8
store i8 0, i8* %56
br label %57
57: ; preds = %55, %50
br label %59
58: ; preds = %34
br label %29
59: ; preds = %57, %29
%60 = load i8*, i8** %2
%61 = getelementptr inbounds %struct.rusage, %struct.rusage* %3, i32 0, i32 0
%62 = load i32, i32* %61
%63 = load i8*, i8** %7
%64 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i8* %60, i32 %62, i8* %63)
ret void
}

LLVM IR (-O2):
%struct.rusage = type { i32 }
@RUSAGE_SELF = global i32 0
@.str = [10 x i8] c"getrusage\00"
@.str.1 = [16 x i8] c"/proc/%d/status\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [6 x i8] c"fopen\00"
@.str.4 = [7 x i8] c"VmSize\00"
@.str.5 = [36 x i8] c"%s: max RSS = %ld kB; vm size = %s\0A\00"
define void @report_memory(i8* %0) {
%2 = alloca %struct.rusage
%3 = alloca [256 x i8]
%4 = alloca [256 x i8]
%5 = bitcast %struct.rusage* %2 to i8*
%6 = load i32, i32* @RUSAGE_SELF
%7 = call i64 @getrusage(i32 %6, %struct.rusage* %2)
%8 = icmp eq i64 %7, 0
br i1 %8, label %12, label %9
9: ; preds = %1
%10 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %1
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%14 = call i32 (...) @getpid()
%15 = call i32 @snprintf(i8* %13, i32 256, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %21
18: ; preds = %12
%19 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %12
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
br label %23
23: ; preds = %26, %21
%24 = call i64 @fgets(i8* %22, i32 256, i32* %16)
%25 = icmp eq i64 %24, 0
br i1 %25, label %41, label %26
26: ; preds = %23
%27 = call i32 @bcmp(i8* dereferenceable(6) %22, i8* dereferenceable(6) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i64 6)
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %23
29: ; preds = %26
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 7
br label %31
31: ; preds = %31, %29
%32 = phi i8* [ %30, %29 ], [ %36, %31 ]
%33 = load i8, i8* %32
%34 = call i64 @isspace(i8 signext %33)
%35 = icmp eq i64 %34, 0
%36 = getelementptr inbounds i8, i8* %32, i64 1
br i1 %35, label %37, label %31
37: ; preds = %31
%38 = call i8* @strchr(i8* %32, i8 signext 10)
%39 = icmp eq i8* %38, null
br i1 %39, label %41, label %40
40: ; preds = %37
store i8 0, i8* %38
br label %41
41: ; preds = %23, %37, %40
%42 = phi i8* [ %32, %40 ], [ %32, %37 ], [ %22, %23 ]
%43 = getelementptr inbounds %struct.rusage, %struct.rusage* %2, i64 0, i32 0
%44 = load i32, i32* %43
%45 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i8* %0, i32 %44, i8* %42)
ret void
}