#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 {
  scalar_t__ ru_maxrss;
};
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ NAME_MAX;
int /*<<< orphan*/ WUNTRACED;
int /*<<< orphan*/ execl(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, long);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ wait3(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         struct rusage *);
int main(int argc, char *argv[]) {
  int ret;
  pid_t c_pid;
  unsigned long vsize = 0;
  char exefile[NAME_MAX + 3];
  if (argc != 2) {
    perror("argument error! usage: maxmem *FILENAME*\n");
    return 1;
  }
  struct rusage ru;
  switch (c_pid = fork()) {
  case -1:
    perror("fork failed!\n");
    break;
  case 0:
    sprintf(exefile, "./%s", argv[1]);
    execl(exefile, exefile + 2, NULL);
    break;
  default:
    wait3(NULL, WUNTRACED, &ru);
    fprintf(stderr, "Maximum memory usage: %luKB\n", (long int)ru.ru_maxrss);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2e43(%rip),%rax
mov    (%rax),%eax
add    $0x3,%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11d6 <main+0x76>
lea    0xe42(%rip),%rdi
call   1050 <perror@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    1282 <main+0x122>
xor    %eax,%eax
call   1080 <fork@plt>
mov    %eax,-0x18(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x4c(%rbp)
sub    $0xffffffff,%eax
je     1203 <main+0xa3>
jmp    11f3 <main+0x93>
mov    -0x4c(%rbp),%eax
test   %eax,%eax
je     1214 <main+0xb4>
jmp    1245 <main+0xe5>
lea    0xe20(%rip),%rdi
call   1050 <perror@plt>
jmp    1274 <main+0x114>
mov    -0x48(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe11(%rip),%rsi
call   1060 <sprintf@plt>
mov    -0x48(%rbp),%rdi
mov    %rdi,%rsi
add    $0x2,%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1070 <execl@plt>
jmp    1274 <main+0x114>
mov    0x2d94(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x40(%rbp),%rdx
call   1030 <wait3@plt>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
mov    -0x40(%rbp),%rdx
lea    0xdce(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x28(%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   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e56(%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    11fb <main+0x9b>
mov    %rsi,%r14
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     11cf <main+0x6f>
cmp    $0xffffffff,%eax
je     120e <main+0xae>
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
xor    %edi,%edi
call   1030 <wait3@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe75(%rip),%rsi
call   1040 <fprintf@plt>
jmp    11f0 <main+0x90>
mov    0x8(%r14),%rdx
lea    0xe5e(%rip),%rsi
mov    %rbx,%rdi
call   1060 <sprintf@plt>
lea    0x2(%rbx),%rsi
mov    %rbx,%rdi
xor    %edx,%edx
call   1070 <execl@plt>
xor    %eax,%eax
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdfe(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%eax
jmp    11f2 <main+0x92>
lea    0xe15(%rip),%rdi
call   1050 <perror@plt>
jmp    11f0 <main+0x90>

LLVM IR (-O0):
%struct.rusage = type { i64 }
@NAME_MAX = global i32 0
@.str = [42 x i8] c"argument error! usage: maxmem *FILENAME*\0A\00"
@.str.1 = [14 x i8] c"fork failed!\0A\00"
@.str.2 = [5 x i8] c"./%s\00"
@WUNTRACED = global i32 0
@stderr = global i32 0
@.str.3 = [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 i8*
%10 = alloca i64
%11 = alloca i32
%12 = alloca %struct.rusage
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** %9
%17 = alloca i8, i64 %15
store i64 %15, i64* %10
%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))
store i32 1, i32* %3
store i32 1, i32* %11
br label %41
22: ; preds = %2
%23 = call i32 (...) @fork()
store i32 %23, i32* %7
switch i32 %23, label %33 [
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 %40
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 = getelementptr inbounds i8, i8* %17, i64 2
%32 = call i32 @execl(i8* %17, i8* %31, i32* null)
br label %40
33: ; preds = %22
%34 = load i32, i32* @WUNTRACED
%35 = call i32 @wait3(i32* null, i32 %34, %struct.rusage* %12)
%36 = load i32, i32* @stderr
%37 = getelementptr inbounds %struct.rusage, %struct.rusage* %12, i32 0, i32 0
%38 = load i64, i64* %37
%39 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i64 %38)
br label %40
40: ; preds = %33, %26, %24
store i32 0, i32* %3
store i32 1, i32* %11
br label %41
41: ; preds = %40, %20
%42 = load i8*, i8** %9
%43 = load i32, i32* %3
ret i32 %43
}

LLVM IR (-O2):
%struct.rusage = type { i64 }
@NAME_MAX = global i32 0
@.str = [42 x i8] c"argument error! usage: maxmem *FILENAME*\0A\00"
@.str.1 = [14 x i8] c"fork failed!\0A\00"
@.str.2 = [5 x i8] c"./%s\00"
@WUNTRACED = global i32 0
@stderr = global i32 0
@.str.3 = [29 x i8] c"Maximum memory usage: %luKB\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.rusage
%4 = load i32, i32* @NAME_MAX
%5 = add nsw i32 %4, 3
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = icmp eq i32 %0, 2
br i1 %8, label %11, label %9
9: ; preds = %2
%10 = tail call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
br label %30
11: ; preds = %2
%12 = bitcast %struct.rusage* %3 to i8*
%13 = tail call i32 (...) @fork()
switch i32 %13, label %22 [
i32 -1, label %14
i32 0, label %16
]
14: ; preds = %11
%15 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %29
16: ; preds = %11
%17 = getelementptr inbounds i8*, i8** %1, i64 1
%18 = load i8*, i8** %17
%19 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %18)
%20 = getelementptr inbounds i8, i8* %7, i64 2
%21 = call i32 @execl(i8* %7, i8* %20, i32* null)
br label %29
22: ; preds = %11
%23 = load i32, i32* @WUNTRACED
%24 = call i32 @wait3(i32* null, i32 %23, %struct.rusage* %3)
%25 = load i32, i32* @stderr
%26 = getelementptr inbounds %struct.rusage, %struct.rusage* %3, i64 0, i32 0
%27 = load i64, i64* %26
%28 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i64 %27)
br label %29
29: ; preds = %22, %16, %14
br label %30
30: ; preds = %29, %9
%31 = phi i32 [ 1, %9 ], [ 0, %29 ]
ret i32 %31
}