#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_stime;
  int /*<<< orphan*/ ru_utime;
};
typedef int pid_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int PAGE_SIZE;
int /*<<< orphan*/ WNOHANG;
int /*<<< orphan*/ asprintf(char **, char *, int);
int execvp(char *, char **);
int /*<<< orphan*/ fatal(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fork();
int fscanf(int /*<<< orphan*/ *, char *, int *, int *);
int /*<<< orphan*/ printf(char *, unsigned int, ...);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ sleep(int);
float timeval_to_sec(int /*<<< orphan*/ *);
unsigned int umax(unsigned int, int);
int wait4(int, int /*<<< orphan*/ *, int /*<<< orphan*/, struct rusage *);
int writeable_size(char *);
int main(int argc, char *argv[]) {
  if (argc < 2) {
    puts("usage: ptime <program> [arg...]");
    return 1;
  }
  pid_t pid = fork();
  if (!pid)
    return execvp(argv[1], &argv[1]);
  char *statm_name;
  asprintf(&statm_name, "/proc/%d/statm", pid);
  char *maps_name;
  asprintf(&maps_name, "/proc/%d/maps", pid);
  unsigned max_vm = 0, max_rss = 0, max_writeable = 0;
  struct rusage rusage;
  while (true) {
    pid_t p1 = wait4(pid, NULL, WNOHANG, &rusage);
    if (p1 == -1)
      fatal("wait4");
    if (p1 != 0)
      break;
    sleep(1);
    FILE *statm = fopen(statm_name, "r");
    if (statm != NULL) {
      int vm, rss;
      if (fscanf(statm, "%d %d", &vm, &rss) == 2) {
        max_vm = umax(max_vm, vm * PAGE_SIZE);
        max_rss = umax(max_rss, rss * PAGE_SIZE);
      }
      fclose(statm);
    }
    max_writeable = umax(max_writeable, writeable_size(maps_name));
  }
  float cpu_time =
      timeval_to_sec(&rusage.ru_utime) + timeval_to_sec(&rusage.ru_stime);
  printf("cpu = %.2f sec\n", cpu_time);
  printf("virtual = %uk, resident = %uk, writeable = %uk\n", max_vm / 1024,
         max_rss / 1024, max_writeable / 1024);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1218 <main+0x38>
lea    0xdf9(%rip),%rdi
call   1040 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    13ce <main+0x1ee>
mov    $0x0,%al
call   1100 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1249 <main+0x69>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
call   10d0 <execvp@plt>
mov    %eax,-0x4(%rbp)
jmp    13ce <main+0x1ee>
mov    -0x14(%rbp),%edx
lea    -0x20(%rbp),%rdi
lea    0xdc9(%rip),%rsi
call   1090 <asprintf@plt>
mov    -0x14(%rbp),%edx
lea    -0x28(%rbp),%rdi
lea    0xdc5(%rip),%rsi
call   1090 <asprintf@plt>
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d52(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
lea    -0x40(%rbp),%rcx
call   10a0 <wait4@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0xffffffff,-0x44(%rbp)
jne    12b6 <main+0xd6>
lea    0xd8c(%rip),%rdi
call   10e0 <fatal@plt>
cmpl   $0x0,-0x44(%rbp)
je     12c5 <main+0xe5>
jmp    1369 <main+0x189>
mov    $0x1,%edi
call   10f0 <sleep@plt>
mov    -0x20(%rbp),%rdi
lea    0xd69(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     1348 <main+0x168>
mov    -0x50(%rbp),%rdi
lea    0xd4c(%rip),%rsi
lea    -0x54(%rbp),%rdx
lea    -0x58(%rbp),%rcx
call   1070 <fscanf@plt>
cmp    $0x2,%eax
jne    133f <main+0x15f>
mov    -0x2c(%rbp),%edi
mov    -0x54(%rbp),%esi
mov    0x2ccc(%rip),%rax
imul   (%rax),%esi
call   1080 <umax@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x30(%rbp),%edi
mov    -0x58(%rbp),%esi
mov    0x2cb4(%rip),%rax
imul   (%rax),%esi
call   1080 <umax@plt>
mov    %eax,-0x30(%rbp)
mov    -0x50(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x60(%rbp)
mov    -0x28(%rbp),%rdi
call   10b0 <writeable_size@plt>
mov    -0x60(%rbp),%edi
mov    %eax,%esi
call   1080 <umax@plt>
mov    %eax,-0x34(%rbp)
jmp    1284 <main+0xa4>
lea    -0x3c(%rbp),%rdi
call   1030 <timeval_to_sec@plt>
movss  %xmm0,-0x64(%rbp)
lea    -0x40(%rbp),%rdi
call   1030 <timeval_to_sec@plt>
movaps %xmm0,%xmm1
movss  -0x64(%rbp),%xmm0
addss  %xmm1,%xmm0
movss  %xmm0,-0x5c(%rbp)
cvttss2si -0x5c(%rbp),%rax
mov    %eax,%esi
lea    0xcab(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x2c(%rbp),%esi
shr    $0xa,%esi
mov    -0x30(%rbp),%edx
shr    $0xa,%edx
mov    -0x34(%rbp),%ecx
shr    $0xa,%ecx
lea    0xc9b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
cmp    $0x1,%edi
jg     1209 <main+0x29>
lea    0xe06(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%ebx
jmp    1370 <main+0x190>
mov    %rsi,%rbp
xor    %r12d,%r12d
xor    %eax,%eax
call   1100 <fork@plt>
test   %eax,%eax
je     1381 <main+0x1a1>
mov    %eax,%r14d
lea    0xdf8(%rip),%rsi
lea    0x20(%rsp),%rdi
mov    %eax,%edx
call   1090 <asprintf@plt>
lea    0xdf4(%rip),%rsi
lea    0x18(%rsp),%rdi
mov    %r14d,%edx
call   1090 <asprintf@plt>
lea    0x10(%rsp),%rbx
xor    %ebp,%ebp
movl   $0x0,(%rsp)
jmp    127d <main+0x9d>
nopl   0x0(%rax,%rax,1)
mov    %r13,%rdi
call   1050 <fclose@plt>
mov    0x18(%rsp),%rdi
call   10b0 <writeable_size@plt>
mov    %ebp,%edi
mov    %eax,%esi
call   1080 <umax@plt>
mov    %eax,%ebp
mov    0x2d5c(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
xor    %esi,%esi
mov    %rbx,%rcx
call   10a0 <wait4@plt>
test   %eax,%eax
jne    130a <main+0x12a>
mov    $0x1,%edi
call   10f0 <sleep@plt>
mov    0x20(%rsp),%rdi
lea    0xd96(%rip),%rsi
call   10c0 <fopen@plt>
test   %rax,%rax
je     1268 <main+0x88>
mov    %rax,%r13
mov    %rax,%rdi
lea    0xd81(%rip),%rsi
lea    0xc(%rsp),%rdx
lea    0x8(%rsp),%rcx
call   1070 <fscanf@plt>
cmp    $0x2,%eax
jne    1260 <main+0x80>
mov    0x2d09(%rip),%r15
mov    (%r15),%esi
imul   0xc(%rsp),%esi
mov    (%rsp),%edi
call   1080 <umax@plt>
mov    %eax,(%rsp)
mov    (%r15),%esi
imul   0x8(%rsp),%esi
mov    %r12d,%edi
call   1080 <umax@plt>
mov    %eax,%r12d
jmp    1260 <main+0x80>
cmp    $0xffffffff,%eax
jne    131b <main+0x13b>
lea    0xd27(%rip),%rdi
call   10e0 <fatal@plt>
lea    0x14(%rsp),%rdi
call   1030 <timeval_to_sec@plt>
movss  %xmm0,0x4(%rsp)
lea    0x10(%rsp),%rdi
call   1030 <timeval_to_sec@plt>
addss  0x4(%rsp),%xmm0
cvttss2si %xmm0,%rsi
lea    0xd04(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <printf@plt>
mov    (%rsp),%esi
shr    $0xa,%esi
shr    $0xa,%r12d
shr    $0xa,%ebp
lea    0xcf7(%rip),%rdi
mov    %r12d,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rbp),%rdi
add    $0x8,%rbp
mov    %rbp,%rsi
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10d0 <execvp@plt>

LLVM IR (-O0):
%struct.rusage = type { i32, i32 }
@.str = [32 x i8] c"usage: ptime <program> [arg...]\00"
@.str.1 = [15 x i8] c"/proc/%d/statm\00"
@.str.2 = [14 x i8] c"/proc/%d/maps\00"
@WNOHANG = global i32 0
@.str.3 = [6 x i8] c"wait4\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [6 x i8] c"%d %d\00"
@PAGE_SIZE = global i32 0
@.str.6 = [16 x i8] c"cpu = %.2f sec\0A\00"
@.str.7 = [48 x i8] c"virtual = %uk, resident = %uk, writeable = %uk\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.rusage
%13 = alloca i32
%14 = alloca i32*
%15 = alloca i32
%16 = alloca i32
%17 = alloca float
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%18 = load i32, i32* %4
%19 = icmp slt i32 %18, 2
br i1 %19, label %20, label %22
20: ; preds = %2
%21 = call i32 @puts(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %95
22: ; preds = %2
%23 = call i32 (...) @fork()
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = icmp ne i32 %24, 0
br i1 %25, label %33, label %26
26: ; preds = %22
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = call i32 @execvp(i8* %29, i8** %31)
store i32 %32, i32* %3
br label %95
33: ; preds = %22
%34 = load i32, i32* %6
%35 = call i32 @asprintf(i8** %7, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %34)
%36 = load i32, i32* %6
%37 = call i32 @asprintf(i8** %8, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %36)
store i32 0, i32* %9
store i32 0, i32* %10
store i32 0, i32* %11
br label %38
38: ; preds = %33, %74
%39 = load i32, i32* %6
%40 = load i32, i32* @WNOHANG
%41 = call i32 @wait4(i32 %39, i32* null, i32 %40, %struct.rusage* %12)
store i32 %41, i32* %13
%42 = load i32, i32* %13
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %46
44: ; preds = %38
%45 = call i32 @fatal(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %46
46: ; preds = %44, %38
%47 = load i32, i32* %13
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %46
br label %79
50: ; preds = %46
%51 = call i32 @sleep(i32 1)
%52 = load i8*, i8** %7
%53 = call i32* @fopen(i8* %52, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %53, i32** %14
%54 = load i32*, i32** %14
%55 = icmp ne i32* %54, null
br i1 %55, label %56, label %74
56: ; preds = %50
%57 = load i32*, i32** %14
%58 = call i32 @fscanf(i32* %57, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32* %15, i32* %16)
%59 = icmp eq i32 %58, 2
br i1 %59, label %60, label %71
60: ; preds = %56
%61 = load i32, i32* %9
%62 = load i32, i32* %15
%63 = load i32, i32* @PAGE_SIZE
%64 = mul nsw i32 %62, %63
%65 = call i32 @umax(i32 %61, i32 %64)
store i32 %65, i32* %9
%66 = load i32, i32* %10
%67 = load i32, i32* %16
%68 = load i32, i32* @PAGE_SIZE
%69 = mul nsw i32 %67, %68
%70 = call i32 @umax(i32 %66, i32 %69)
store i32 %70, i32* %10
br label %71
71: ; preds = %60, %56
%72 = load i32*, i32** %14
%73 = call i32 @fclose(i32* %72)
br label %74
74: ; preds = %71, %50
%75 = load i32, i32* %11
%76 = load i8*, i8** %8
%77 = call i32 @writeable_size(i8* %76)
%78 = call i32 @umax(i32 %75, i32 %77)
store i32 %78, i32* %11
br label %38
79: ; preds = %49
%80 = getelementptr inbounds %struct.rusage, %struct.rusage* %12, i32 0, i32 1
%81 = call float @timeval_to_sec(i32* %80)
%82 = getelementptr inbounds %struct.rusage, %struct.rusage* %12, i32 0, i32 0
%83 = call float @timeval_to_sec(i32* %82)
%84 = fadd float %81, %83
store float %84, float* %17
%85 = load float, float* %17
%86 = fptoui float %85 to i32
%87 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), i32 %86)
%88 = load i32, i32* %9
%89 = udiv i32 %88, 1024
%90 = load i32, i32* %10
%91 = udiv i32 %90, 1024
%92 = load i32, i32* %11
%93 = udiv i32 %92, 1024
%94 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.7, i64 0, i64 0), i32 %89, i32 %91, i32 %93)
store i32 0, i32* %3
br label %95
95: ; preds = %79, %26, %20
%96 = load i32, i32* %3
ret i32 %96
}

LLVM IR (-O2):
%struct.rusage = type { i32, i32 }
@.str = [32 x i8] c"usage: ptime <program> [arg...]\00"
@.str.1 = [15 x i8] c"/proc/%d/statm\00"
@.str.2 = [14 x i8] c"/proc/%d/maps\00"
@WNOHANG = global i32 0
@.str.3 = [6 x i8] c"wait4\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [6 x i8] c"%d %d\00"
@PAGE_SIZE = global i32 0
@.str.6 = [16 x i8] c"cpu = %.2f sec\0A\00"
@.str.7 = [48 x i8] c"virtual = %uk, resident = %uk, writeable = %uk\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca %struct.rusage
%6 = alloca i32
%7 = alloca i32
%8 = icmp slt i32 %0, 2
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %73
11: ; preds = %2
%12 = tail call i32 (...) @fork()
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = tail call i32 @execvp(i8* %16, i8** %15)
br label %73
18: ; preds = %11
%19 = bitcast i8** %3 to i8*
%20 = call i32 @asprintf(i8** %3, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%21 = bitcast i8** %4 to i8*
%22 = call i32 @asprintf(i8** %4, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %12)
%23 = bitcast %struct.rusage* %5 to i8*
%24 = bitcast i32* %6 to i8*
%25 = bitcast i32* %7 to i8*
br label %26
26: ; preds = %55, %18
%27 = phi i32 [ 0, %18 ], [ %56, %55 ]
%28 = phi i32 [ 0, %18 ], [ %60, %55 ]
%29 = phi i32 [ 0, %18 ], [ %57, %55 ]
%30 = load i32, i32* @WNOHANG
%31 = call i32 @wait4(i32 %12, i32* null, i32 %30, %struct.rusage* %5)
switch i32 %31, label %61 [
i32 -1, label %32
i32 0, label %34
]
32: ; preds = %26
%33 = call i32 @fatal(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %61
34: ; preds = %26
%35 = call i32 @sleep(i32 1)
%36 = load i8*, i8** %3
%37 = call i32* @fopen(i8* %36, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%38 = icmp eq i32* %37, null
br i1 %38, label %55, label %39
39: ; preds = %34
%40 = call i32 @fscanf(i32* %37, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32* %6, i32* %7)
%41 = icmp eq i32 %40, 2
br i1 %41, label %42, label %51
42: ; preds = %39
%43 = load i32, i32* %6
%44 = load i32, i32* @PAGE_SIZE
%45 = mul nsw i32 %44, %43
%46 = call i32 @umax(i32 %29, i32 %45)
%47 = load i32, i32* %7
%48 = load i32, i32* @PAGE_SIZE
%49 = mul nsw i32 %48, %47
%50 = call i32 @umax(i32 %27, i32 %49)
br label %51
51: ; preds = %42, %39
%52 = phi i32 [ %50, %42 ], [ %27, %39 ]
%53 = phi i32 [ %46, %42 ], [ %29, %39 ]
%54 = call i32 @fclose(i32* %37)
br label %55
55: ; preds = %34, %51
%56 = phi i32 [ %52, %51 ], [ %27, %34 ]
%57 = phi i32 [ %53, %51 ], [ %29, %34 ]
%58 = load i8*, i8** %4
%59 = call i32 @writeable_size(i8* %58)
%60 = call i32 @umax(i32 %28, i32 %59)
br label %26
61: ; preds = %26, %32
%62 = getelementptr inbounds %struct.rusage, %struct.rusage* %5, i64 0, i32 1
%63 = call float @timeval_to_sec(i32* %62)
%64 = getelementptr inbounds %struct.rusage, %struct.rusage* %5, i64 0, i32 0
%65 = call float @timeval_to_sec(i32* %64)
%66 = fadd float %63, %65
%67 = fptoui float %66 to i32
%68 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), i32 %67)
%69 = lshr i32 %29, 10
%70 = lshr i32 %27, 10
%71 = lshr i32 %28, 10
%72 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.7, i64 0, i64 0), i32 %69, i32 %70, i32 %71)
br label %73
73: ; preds = %14, %61, %9
%74 = phi i32 [ 1, %9 ], [ 0, %61 ], [ %17, %14 ]
ret i32 %74
}