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

/* Variables and functions */
int /*<<< orphan*/ NAME_MAX;
int /*<<< orphan*/ PTRACE_SYSCALL;
int /*<<< orphan*/ PTRACE_TRACEME;
int /*<<< orphan*/ WIFSTOPPED(int);
int /*<<< orphan*/ execl(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, unsigned long);
int /*<<< orphan*/ get_mem(int /*<<< orphan*/, unsigned long *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ ptrace(int /*<<< orphan*/, int /*<<< orphan*/,
                          int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ wait(int *);
int main(int argc, char *argv[]) {
  int ret;
  pid_t c_pid;
  unsigned long vsize = 0, vs;
  int test[100000];
  char exefile[NAME_MAX + 3];
  if (argc != 2)
    perror("argument errro! usage: maxmem *FILENAME*\n");
  switch (c_pid = fork()) {
  case -1:
    perror("fork failed!\n");
    break;
  case 0:
    sprintf(exefile, "./%s", argv[1]);
    ptrace(PTRACE_TRACEME, 0, 0, 0);
    execl(exefile, exefile + 2, NULL);
    break;
  default:
    wait(&ret);
    while (WIFSTOPPED(ret)) {
      get_mem(c_pid, &vs);
      if (vs > vsize)
        vsize = vs;
      if (ptrace(PTRACE_SYSCALL, c_pid, 0, 0) != 0)
        perror("ptrace failed!\n");
      wait(&ret);
    }
    fprintf(stderr, "Maximum memory usage: %luKB\n", vsize / 1024);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x61ad0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2e08(%rip),%rax
mov    (%rax),%eax
add    $0x3,%eax
mov    %rsp,%rcx
mov    %rcx,-0x61ab8(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x61ac8(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x61ac0(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11ff <main+0x6f>
lea    0xe06(%rip),%rdi
call   1070 <perror@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,-0x18(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x61acc(%rbp)
sub    $0xffffffff,%eax
je     1232 <main+0xa2>
jmp    121f <main+0x8f>
mov    -0x61acc(%rbp),%eax
test   %eax,%eax
je     1243 <main+0xb3>
jmp    128e <main+0xfe>
lea    0xdf1(%rip),%rdi
call   1070 <perror@plt>
jmp    131f <main+0x18f>
mov    -0x61ac8(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xddf(%rip),%rsi
call   1080 <sprintf@plt>
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
xor    %ecx,%ecx
mov    %ecx,%esi
mov    %ecx,%edx
call   1060 <ptrace@plt>
mov    -0x61ac8(%rbp),%rdi
mov    %rdi,%rsi
add    $0x2,%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1090 <execl@plt>
jmp    131f <main+0x18f>
lea    -0x14(%rbp),%rdi
call   10a0 <wait@plt>
mov    -0x14(%rbp),%edi
call   1050 <WIFSTOPPED@plt>
cmp    $0x0,%eax
je     1302 <main+0x172>
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1030 <get_mem@plt>
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jbe    12ca <main+0x13a>
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2cf7(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%esi
xor    %ecx,%ecx
mov    %ecx,%edx
call   1060 <ptrace@plt>
cmp    $0x0,%eax
je     12f4 <main+0x164>
lea    0xd4e(%rip),%rdi
call   1070 <perror@plt>
lea    -0x14(%rbp),%rdi
call   10a0 <wait@plt>
jmp    1297 <main+0x107>
mov    0x2cd7(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
shr    $0xa,%rdx
lea    0xd33(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x61ab8(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r15
mov    %rsp,%r12
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
add    $0x3,%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
cmp    $0x2,%edi
jne    12db <main+0x14b>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     126c <main+0xdc>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
je     12ec <main+0x15c>
lea    -0x2c(%rbp),%rdi
call   10a0 <wait@plt>
mov    -0x2c(%rbp),%edi
call   1050 <WIFSTOPPED@plt>
test   %eax,%eax
je     12ad <main+0x11d>
mov    %r12,-0x38(%rbp)
xor    %ebx,%ebx
lea    -0x40(%rbp),%r15
mov    0x2db6(%rip),%r13
lea    -0x2c(%rbp),%r12
jmp    1234 <main+0xa4>
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   10a0 <wait@plt>
mov    -0x2c(%rbp),%edi
call   1050 <WIFSTOPPED@plt>
test   %eax,%eax
je     12a3 <main+0x113>
mov    %r14d,%edi
mov    %r15,%rsi
call   1030 <get_mem@plt>
mov    -0x40(%rbp),%rax
cmp    %rbx,%rax
cmova  %rax,%rbx
mov    0x0(%r13),%edi
mov    %r14d,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1060 <ptrace@plt>
test   %eax,%eax
je     1220 <main+0x90>
lea    0xdd8(%rip),%rdi
call   1070 <perror@plt>
jmp    1220 <main+0x90>
mov    0x8(%r15),%rdx
lea    0xdc1(%rip),%rsi
mov    %rbx,%rdi
call   1080 <sprintf@plt>
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1060 <ptrace@plt>
lea    0x2(%rbx),%rsi
mov    %rbx,%rdi
xor    %edx,%edx
call   1090 <execl@plt>
jmp    12c7 <main+0x137>
shr    $0xa,%rbx
mov    -0x38(%rbp),%r12
jmp    12af <main+0x11f>
xor    %ebx,%ebx
mov    0x2d2a(%rip),%rax
mov    (%rax),%edi
lea    0xd8e(%rip),%rsi
mov    %rbx,%rdx
call   1040 <fprintf@plt>
mov    %r12,%rsp
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd1e(%rip),%rdi
call   1070 <perror@plt>
jmp    11cd <main+0x3d>
lea    0xd37(%rip),%rdi
call   1070 <perror@plt>
jmp    12c7 <main+0x137>

LLVM IR (-O0):
@NAME_MAX = global i32 0
@.str = [42 x i8] c"argument errro! usage: maxmem *FILENAME*\0A\00"
@.str.1 = [14 x i8] c"fork failed!\0A\00"
@.str.2 = [5 x i8] c"./%s\00"
@PTRACE_TRACEME = global i32 0
@PTRACE_SYSCALL = global i32 0
@.str.3 = [16 x i8] c"ptrace failed!\0A\00"
@stderr = global i32 0
@.str.4 = [29 x i8] c"Maximum memory usage: %luKB\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
%9 = alloca i64
%10 = alloca [100000 x i32]
%11 = alloca i8*
%12 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 0, i64* %8
%13 = load i32, i32* @NAME_MAX
%14 = add nsw i32 %13, 3
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %11
%17 = alloca i8, i64 %15
store i64 %15, i64* %12
%18 = load i32, i32* %4
%19 = icmp ne i32 %18, 2
br i1 %19, label %20, label %22
20: ; preds = %2
%21 = call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %20, %2
%23 = call i32 (...) @fork()
store i32 %23, i32* %7
switch i32 %23, label %35 [
i32 -1, label %24
i32 0, label %26
]
24: ; preds = %22
%25 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %63
26: ; preds = %22
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = call i32 @sprintf(i8* %17, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %29)
%31 = load i32, i32* @PTRACE_TRACEME
%32 = call i32 @ptrace(i32 %31, i32 0, i32 0, i32 0)
%33 = getelementptr inbounds i8, i8* %17, i64 2
%34 = call i32 @execl(i8* %17, i8* %33, i32* null)
br label %63
35: ; preds = %22
%36 = call i32 @wait(i32* %6)
br label %37
37: ; preds = %56, %35
%38 = load i32, i32* %6
%39 = call i32 @WIFSTOPPED(i32 %38)
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %58
41: ; preds = %37
%42 = load i32, i32* %7
%43 = call i32 @get_mem(i32 %42, i64* %9)
%44 = load i64, i64* %9
%45 = load i64, i64* %8
%46 = icmp ugt i64 %44, %45
br i1 %46, label %47, label %49
47: ; preds = %41
%48 = load i64, i64* %9
store i64 %48, i64* %8
br label %49
49: ; preds = %47, %41
%50 = load i32, i32* @PTRACE_SYSCALL
%51 = load i32, i32* %7
%52 = call i32 @ptrace(i32 %50, i32 %51, i32 0, i32 0)
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %49
%55 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %56
56: ; preds = %54, %49
%57 = call i32 @wait(i32* %6)
br label %37
58: ; preds = %37
%59 = load i32, i32* @stderr
%60 = load i64, i64* %8
%61 = udiv i64 %60, 1024
%62 = call i32 @fprintf(i32 %59, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i64 %61)
br label %63
63: ; preds = %58, %26, %24
store i32 0, i32* %3
%64 = load i8*, i8** %11
%65 = load i32, i32* %3
ret i32 %65
}

LLVM IR (-O2):
@NAME_MAX = global i32 0
@.str = [42 x i8] c"argument errro! usage: maxmem *FILENAME*\0A\00"
@.str.1 = [14 x i8] c"fork failed!\0A\00"
@.str.2 = [5 x i8] c"./%s\00"
@PTRACE_TRACEME = global i32 0
@PTRACE_SYSCALL = global i32 0
@.str.3 = [16 x i8] c"ptrace failed!\0A\00"
@stderr = global i32 0
@.str.4 = [29 x i8] c"Maximum memory usage: %luKB\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i64
%5 = bitcast i32* %3 to i8*
%6 = bitcast i64* %4 to i8*
%7 = load i32, i32* @NAME_MAX
%8 = add nsw i32 %7, 3
%9 = zext i32 %8 to i64
%10 = tail call i8* @llvm.stacksave()
%11 = alloca i8, i64 %9
%12 = icmp eq i32 %0, 2
br i1 %12, label %15, label %13
13: ; preds = %2
%14 = tail call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %2
%16 = tail call i32 (...) @fork()
switch i32 %16, label %27 [
i32 -1, label %17
i32 0, label %19
]
17: ; preds = %15
%18 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %54
19: ; preds = %15
%20 = getelementptr inbounds i8*, i8** %1, i64 1
%21 = load i8*, i8** %20
%22 = call i32 @sprintf(i8* %11, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %21)
%23 = load i32, i32* @PTRACE_TRACEME
%24 = tail call i32 @ptrace(i32 %23, i32 0, i32 0, i32 0)
%25 = getelementptr inbounds i8, i8* %11, i64 2
%26 = call i32 @execl(i8* %11, i8* %25, i32* null)
br label %54
27: ; preds = %15
%28 = call i32 @wait(i32* %3)
%29 = load i32, i32* %3
%30 = call i32 @WIFSTOPPED(i32 %29)
%31 = icmp eq i32 %30, 0
br i1 %31, label %50, label %32
32: ; preds = %27, %43
%33 = phi i64 [ %37, %43 ], [ 0, %27 ]
%34 = call i32 @get_mem(i32 %16, i64* %4)
%35 = load i64, i64* %4
%36 = icmp ugt i64 %35, %33
%37 = select i1 %36, i64 %35, i64 %33
%38 = load i32, i32* @PTRACE_SYSCALL
%39 = call i32 @ptrace(i32 %38, i32 %16, i32 0, i32 0)
%40 = icmp eq i32 %39, 0
br i1 %40, label %43, label %41
41: ; preds = %32
%42 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %43
43: ; preds = %41, %32
%44 = call i32 @wait(i32* %3)
%45 = load i32, i32* %3
%46 = call i32 @WIFSTOPPED(i32 %45)
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %32
48: ; preds = %43
%49 = lshr i64 %37, 10
br label %50
50: ; preds = %48, %27
%51 = phi i64 [ 0, %27 ], [ %49, %48 ]
%52 = load i32, i32* @stderr
%53 = call i32 @fprintf(i32 %52, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i64 %51)
br label %54
54: ; preds = %50, %19, %17
ret i32 0
}