#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 */
typedef struct TYPE_4__ TYPE_2__;
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
struct tms {
  int dummy;
};
struct TYPE_4__ {
  scalar_t__ tv_usec;
  scalar_t__ tv_sec;
};
struct TYPE_3__ {
  scalar_t__ tv_usec;
  scalar_t__ tv_sec;
};
struct rusage {
  long ru_maxrss;
  TYPE_2__ ru_stime;
  TYPE_1__ ru_utime;
};
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ RUSAGE_CHILDREN;
int /*<<< orphan*/ RUSAGE_SELF;
int execvp(char *const, char *const *);
int /*<<< orphan*/ fork();
scalar_t__ getrusage(int /*<<< orphan*/, struct rusage *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ strcmp(char *const, char *);
int times(struct tms *);
scalar_t__ waitpid(int /*<<< orphan*/, int /*<<< orphan*/ *,
                   int /*<<< orphan*/);
int main(int argc, char *const argv[]) {
  pid_t pid;
  struct rusage my_rusage, child_rusage;
  struct tms my_tms;
  long maxRSS;
  int i;
  if (argc < 2 || strcmp(argv[1], "-h") == 0 ||
      strcmp(argv[1], "--help") == 0) {
    printf("use: monitorRSS <cmd> [<cmd args>]\n");
    printf("out: <max rss (bytes)>\t<user time (seconds)>\t<system time "
           "(seconds)>\n");
    return 1;
  }
  pid = fork();
  if (!pid) {
    return execvp(argv[1], &(argv[1]));
  }
  if (waitpid(pid, NULL, 0) < 0) {
    printf("error: waitpid() failed\n");
    return -1;
  }
  if (getrusage(RUSAGE_SELF, &my_rusage) < 0) {
    printf("error: getrusage()\n");
    return -1;
  }
  if (getrusage(RUSAGE_CHILDREN, &child_rusage) < 0) {
    printf("error: getrusage()\n");
    return -1;
  }
  if (times(&my_tms) == -1) {
    printf("error: times()\n");
    return -1;
  }
  maxRSS = child_rusage.ru_maxrss - my_rusage.ru_maxrss;
  maxRSS = maxRSS * 1024L;
  printf("%ld\t%ld.%06ld\t%ld.%06ld\n", maxRSS,
         (long int)child_rusage.ru_utime.tv_sec,
         (long int)child_rusage.ru_utime.tv_usec,
         (long int)child_rusage.ru_stime.tv_sec,
         (long int)child_rusage.ru_stime.tv_usec);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     11cf <main+0x5f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe5e(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
je     11cf <main+0x5f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe43(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    11f7 <main+0x87>
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe4a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1333 <main+0x1c3>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1228 <main+0xb8>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
call   1080 <execvp@plt>
mov    %eax,-0x4(%rbp)
jmp    1333 <main+0x1c3>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmp    $0x0,%rax
jge    125a <main+0xea>
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1333 <main+0x1c3>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rsi
call   1070 <getrusage@plt>
cmp    $0x0,%rax
jge    1290 <main+0x120>
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1333 <main+0x1c3>
mov    0x2d49(%rip),%rax
mov    (%rax),%edi
lea    -0x68(%rbp),%rsi
call   1070 <getrusage@plt>
cmp    $0x0,%rax
jge    12c6 <main+0x156>
lea    0xdda(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1333 <main+0x1c3>
lea    -0x70(%rbp),%rdi
call   1060 <times@plt>
cmp    $0xffffffff,%eax
jne    12f2 <main+0x182>
lea    0xdc2(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1333 <main+0x1c3>
mov    -0x68(%rbp),%rax
sub    -0x40(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x78(%rbp),%rax
shl    $0xa,%rax
mov    %rax,-0x78(%rbp)
mov    -0x78(%rbp),%rsi
mov    -0x48(%rbp),%rdx
mov    -0x50(%rbp),%rcx
mov    -0x58(%rbp),%r8
mov    -0x60(%rbp),%r9
lea    0xd8c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x60,%rsp
cmp    $0x2,%edi
jl     1260 <main+0xe0>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
lea    0xe64(%rip),%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     1260 <main+0xe0>
add    $0x8,%rbx
mov    (%rbx),%rdi
lea    0xe4b(%rip),%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     1260 <main+0xe0>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     1285 <main+0x105>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <waitpid@plt>
test   %rax,%rax
js     129e <main+0x11e>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
lea    0x38(%rsp),%rsi
call   1080 <getrusage@plt>
test   %rax,%rax
js     1295 <main+0x115>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
lea    0x10(%rsp),%rsi
call   1080 <getrusage@plt>
test   %rax,%rax
js     1295 <main+0x115>
lea    0x8(%rsp),%rdi
call   1070 <times@plt>
cmp    $0xffffffff,%eax
je     12a7 <main+0x127>
mov    0x10(%rsp),%rsi
mov    0x18(%rsp),%r9
sub    0x38(%rsp),%rsi
shl    $0xa,%rsi
mov    0x30(%rsp),%rdx
mov    0x28(%rsp),%rcx
mov    0x20(%rsp),%r8
lea    0xdb5(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    127d <main+0xfd>
lea    0xdf6(%rip),%rdi
call   1030 <puts@plt>
lea    0xe0d(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x60,%rsp
pop    %rbx
ret
mov    (%rbx),%rdi
mov    %rbx,%rsi
add    $0x60,%rsp
pop    %rbx
jmp    1090 <execvp@plt>
lea    0xd96(%rip),%rdi
jmp    12ae <main+0x12e>
lea    0xda0(%rip),%rdi
jmp    12ae <main+0x12e>
lea    0xd75(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
jmp    127d <main+0xfd>

LLVM IR (-O0):
%struct.rusage = type { i64, %struct.TYPE_4__, %struct.TYPE_3__ }
%struct.TYPE_4__ = type { i64, i64 }
%struct.TYPE_3__ = type { i64, i64 }
%struct.tms = type { i32 }
@.str = [3 x i8] c"-h\00"
@.str.1 = [7 x i8] c"--help\00"
@.str.2 = [36 x i8] c"use: monitorRSS <cmd> [<cmd args>]\0A\00"
@.str.3 = [70 x i8] c"out: <max rss (bytes)>\09<user time (seconds)>\09<system time (seconds)>\0A\00"
@.str.4 = [25 x i8] c"error: waitpid() failed\0A\00"
@RUSAGE_SELF = global i32 0
@.str.5 = [20 x i8] c"error: getrusage()\0A\00"
@RUSAGE_CHILDREN = global i32 0
@.str.6 = [16 x i8] c"error: times()\0A\00"
@.str.7 = [25 x i8] c"%ld\09%ld.%06ld\09%ld.%06ld\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.rusage
%8 = alloca %struct.rusage
%9 = alloca %struct.tms
%10 = alloca i64
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 2
br i1 %13, label %26, label %14
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i64 @strcmp(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%19 = icmp eq i64 %18, 0
br i1 %19, label %26, label %20
20: ; preds = %14
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i64 @strcmp(i8* %23, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %20, %14, %2
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %85
29: ; preds = %20
%30 = call i32 (...) @fork()
store i32 %30, i32* %6
%31 = load i32, i32* %6
%32 = icmp ne i32 %31, 0
br i1 %32, label %40, label %33
33: ; preds = %29
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 1
%36 = load i8*, i8** %35
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 1
%39 = call i32 @execvp(i8* %36, i8** %38)
store i32 %39, i32* %3
br label %85
40: ; preds = %29
%41 = load i32, i32* %6
%42 = call i64 @waitpid(i32 %41, i32* null, i32 0)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %40
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %85
46: ; preds = %40
%47 = load i32, i32* @RUSAGE_SELF
%48 = call i64 @getrusage(i32 %47, %struct.rusage* %7)
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %46
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %3
br label %85
52: ; preds = %46
%53 = load i32, i32* @RUSAGE_CHILDREN
%54 = call i64 @getrusage(i32 %53, %struct.rusage* %8)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %52
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %3
br label %85
58: ; preds = %52
%59 = call i32 @times(%struct.tms* %9)
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %63
61: ; preds = %58
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
store i32 -1, i32* %3
br label %85
63: ; preds = %58
%64 = getelementptr inbounds %struct.rusage, %struct.rusage* %8, i32 0, i32 0
%65 = load i64, i64* %64
%66 = getelementptr inbounds %struct.rusage, %struct.rusage* %7, i32 0, i32 0
%67 = load i64, i64* %66
%68 = sub nsw i64 %65, %67
store i64 %68, i64* %10
%69 = load i64, i64* %10
%70 = mul nsw i64 %69, 1024
store i64 %70, i64* %10
%71 = load i64, i64* %10
%72 = getelementptr inbounds %struct.rusage, %struct.rusage* %8, i32 0, i32 2
%73 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %72, i32 0, i32 1
%74 = load i64, i64* %73
%75 = getelementptr inbounds %struct.rusage, %struct.rusage* %8, i32 0, i32 2
%76 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %75, i32 0, i32 0
%77 = load i64, i64* %76
%78 = getelementptr inbounds %struct.rusage, %struct.rusage* %8, i32 0, i32 1
%79 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %78, i32 0, i32 1
%80 = load i64, i64* %79
%81 = getelementptr inbounds %struct.rusage, %struct.rusage* %8, i32 0, i32 1
%82 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %81, i32 0, i32 0
%83 = load i64, i64* %82
%84 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.7, i64 0, i64 0), i64 %71, i64 %74, i64 %77, i64 %80, i64 %83)
store i32 0, i32* %3
br label %85
85: ; preds = %63, %61, %56, %50, %44, %33, %26
%86 = load i32, i32* %3
ret i32 %86
}

LLVM IR (-O2):
%struct.rusage = type { i64, %struct.TYPE_4__, %struct.TYPE_3__ }
%struct.TYPE_4__ = type { i64, i64 }
%struct.TYPE_3__ = type { i64, i64 }
%struct.tms = type { i32 }
@.str = [3 x i8] c"-h\00"
@.str.1 = [7 x i8] c"--help\00"
@RUSAGE_SELF = global i32 0
@RUSAGE_CHILDREN = global i32 0
@.str.7 = [25 x i8] c"%ld\09%ld.%06ld\09%ld.%06ld\0A\00"
@str = [15 x i8] c"error: times()\00"
@str.9 = [19 x i8] c"error: getrusage()\00"
@str.10 = [24 x i8] c"error: waitpid() failed\00"
@str.11 = [35 x i8] c"use: monitorRSS <cmd> [<cmd args>]\00"
@str.12 = [69 x i8] c"out: <max rss (bytes)>\09<user time (seconds)>\09<system time (seconds)>\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca %struct.rusage
%4 = alloca %struct.rusage
%5 = alloca %struct.tms
%6 = bitcast %struct.rusage* %3 to i8*
%7 = bitcast %struct.rusage* %4 to i8*
%8 = bitcast %struct.tms* %5 to i8*
%9 = icmp slt i32 %0, 2
br i1 %9, label %19, label %10
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i64 @strcmp(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%14 = icmp eq i64 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %10
%16 = load i8*, i8** %11
%17 = tail call i64 @strcmp(i8* %16, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %15, %10, %2
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.11, i64 0, i64 0))
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([69 x i8], [69 x i8]* @str.12, i64 0, i64 0))
br label %66
22: ; preds = %15
%23 = tail call i32 (...) @fork()
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = load i8*, i8** %11
%27 = tail call i32 @execvp(i8* %26, i8** %11)
br label %66
28: ; preds = %22
%29 = tail call i64 @waitpid(i32 %23, i32* null, i32 0)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.10, i64 0, i64 0))
br label %66
33: ; preds = %28
%34 = load i32, i32* @RUSAGE_SELF
%35 = call i64 @getrusage(i32 %34, %struct.rusage* %3)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %33
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.9, i64 0, i64 0))
br label %66
39: ; preds = %33
%40 = load i32, i32* @RUSAGE_CHILDREN
%41 = call i64 @getrusage(i32 %40, %struct.rusage* %4)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %39
%44 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.9, i64 0, i64 0))
br label %66
45: ; preds = %39
%46 = call i32 @times(%struct.tms* %5)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %50
48: ; preds = %45
%49 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %66
50: ; preds = %45
%51 = getelementptr inbounds %struct.rusage, %struct.rusage* %4, i64 0, i32 0
%52 = load i64, i64* %51
%53 = getelementptr inbounds %struct.rusage, %struct.rusage* %3, i64 0, i32 0
%54 = load i64, i64* %53
%55 = sub nsw i64 %52, %54
%56 = shl nsw i64 %55, 10
%57 = getelementptr inbounds %struct.rusage, %struct.rusage* %4, i64 0, i32 2, i32 1
%58 = load i64, i64* %57
%59 = getelementptr inbounds %struct.rusage, %struct.rusage* %4, i64 0, i32 2, i32 0
%60 = load i64, i64* %59
%61 = getelementptr inbounds %struct.rusage, %struct.rusage* %4, i64 0, i32 1, i32 1
%62 = load i64, i64* %61
%63 = getelementptr inbounds %struct.rusage, %struct.rusage* %4, i64 0, i32 1, i32 0
%64 = load i64, i64* %63
%65 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.7, i64 0, i64 0), i64 %56, i64 %58, i64 %60, i64 %62, i64 %64)
br label %66
66: ; preds = %50, %48, %43, %37, %31, %25, %19
%67 = phi i32 [ 1, %19 ], [ -1, %31 ], [ -1, %37 ], [ -1, %43 ], [ -1, %48 ], [ 0, %50 ], [ %27, %25 ]
ret i32 %67
}