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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
long get_memusage(void) {
  long vmsize;
  pid_t pid;
  char filename[64];
  char buf[4096];
  FILE *stat_file;
  int i, field;
  vmsize = 0;
  pid = getpid();
  snprintf(filename, 64, "/proc/%d/stat", pid);
  filename[63] = '\0';
  stat_file = fopen(filename, "r");
  if (!stat_file) {
    perror(filename);
    exit(EXIT_FAILURE);
  }
  if (!fread(buf, sizeof(char), 4096, stat_file)) {
    perror(filename);
    exit(EXIT_FAILURE);
  }
  field = 0;
  i = 0;
  while (buf[i]) {
    if (field == 22) {
      for (; '0' <= buf[i] && buf[i] <= '9'; i++)
        vmsize = 10 * vmsize + buf[i] - '0';
    }
    while (buf[i] && buf[i] != ' ')
      i++;
    i++;
    field++;
  }
  fclose(stat_file);
  return vmsize;
}


x86 assembly (clang, -O0):
<get_memusage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1070,%rsp
movq   $0x0,-0x8(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0xc(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0xc(%rbp),%ecx
mov    $0x40,%esi
lea    0xe60(%rip),%rdx
call   1060 <snprintf@plt>
movb   $0x0,-0x11(%rbp)
lea    -0x50(%rbp),%rdi
lea    0xe5a(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x1058(%rbp)
cmpq   $0x0,-0x1058(%rbp)
jne    11e5 <get_memusage+0x75>
lea    -0x50(%rbp),%rdi
call   1080 <perror@plt>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
lea    -0x1050(%rbp),%rdi
mov    -0x1058(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1000,%edx
call   1030 <fread@plt>
cmp    $0x0,%eax
jne    1222 <get_memusage+0xb2>
lea    -0x50(%rbp),%rdi
call   1080 <perror@plt>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
movl   $0x0,-0x1060(%rbp)
movl   $0x0,-0x105c(%rbp)
movslq -0x105c(%rbp),%rax
cmpb   $0x0,-0x1050(%rbp,%rax,1)
je     1372 <get_memusage+0x202>
cmpl   $0x16,-0x1060(%rbp)
jne    12e8 <get_memusage+0x178>
jmp    125d <get_memusage+0xed>
movslq -0x105c(%rbp),%rax
movsbl -0x1050(%rbp,%rax,1),%edx
ff
xor    %eax,%eax
mov    $0x30,%ecx
cmp    %edx,%ecx
mov    %al,-0x1061(%rbp)
jg     129c <get_memusage+0x12c>
movslq -0x105c(%rbp),%rax
movsbl -0x1050(%rbp,%rax,1),%eax
ff
cmp    $0x39,%eax
setle  %al
mov    %al,-0x1061(%rbp)
mov    -0x1061(%rbp),%al
test   $0x1,%al
jne    12af <get_memusage+0x13f>
jmp    12e3 <get_memusage+0x173>
imul   $0xa,-0x8(%rbp),%rax
movslq -0x105c(%rbp),%rcx
movsbq -0x1050(%rbp,%rcx,1),%rcx
ff ff
add    %rcx,%rax
sub    $0x30,%rax
mov    %rax,-0x8(%rbp)
mov    -0x105c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x105c(%rbp)
jmp    125d <get_memusage+0xed>
jmp    12e8 <get_memusage+0x178>
jmp    12ed <get_memusage+0x17d>
movslq -0x105c(%rbp),%rax
movsbl -0x1050(%rbp,%rax,1),%ecx
ff
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x1062(%rbp)
je     1328 <get_memusage+0x1b8>
movslq -0x105c(%rbp),%rax
movsbl -0x1050(%rbp,%rax,1),%eax
ff
cmp    $0x20,%eax
setne  %al
mov    %al,-0x1062(%rbp)
mov    -0x1062(%rbp),%al
test   $0x1,%al
jne    133b <get_memusage+0x1cb>
jmp    134f <get_memusage+0x1df>
mov    -0x105c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x105c(%rbp)
jmp    12ed <get_memusage+0x17d>
mov    -0x105c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x105c(%rbp)
mov    -0x1060(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1060(%rbp)
jmp    1236 <get_memusage+0xc6>
mov    -0x1058(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x8(%rbp),%rax
add    $0x1070,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_memusage>:
push   %r14
push   %rbx
sub    $0x1048,%rsp
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe78(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x40,%esi
mov    %eax,%ecx
call   1060 <snprintf@plt>
movb   $0x0,0x3f(%rsp)
lea    0xe68(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     1296 <get_memusage+0x126>
mov    %rax,%r14
lea    0x40(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
test   %eax,%eax
je     1296 <get_memusage+0x126>
mov    0x40(%rsp),%sil
xor    %eax,%eax
test   %sil,%sil
je     127e <get_memusage+0x10e>
lea    0x41(%rsp),%r8
xor    %edx,%edx
xor    %ebx,%ebx
jmp    1210 <get_memusage+0xa0>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x1,%eax
movslq %edx,%rcx
mov    0x40(%rsp,%rcx,1),%sil
test   %sil,%sil
je     1280 <get_memusage+0x110>
cmp    $0x16,%eax
jne    1252 <get_memusage+0xe2>
lea    -0x30(%rsi),%edi
cmp    $0x9,%dil
ja     1252 <get_memusage+0xe2>
movslq %edx,%rdi
add    %r8,%rdi
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
lea    (%rbx,%rbx,4),%rbx
movzbl %sil,%esi
lea    (%rsi,%rbx,2),%rbx
add    $0xffffffffffffffd0,%rbx
movzbl (%rdi),%esi
lea    -0x30(%rsi),%ecx
add    $0x1,%edx
add    $0x1,%rdi
cmp    $0xa,%cl
jb     1230 <get_memusage+0xc0>
movslq %edx,%rcx
lea    0x1(%rcx),%edx
lea    (%rsp,%rcx,1),%rsi
add    $0x40,%rsi
testb  $0xdf,(%rsi)
je     1200 <get_memusage+0x90>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x1,%edx
add    $0x1,%rsi
testb  $0xdf,(%rsi)
jne    1270 <get_memusage+0x100>
jmp    1200 <get_memusage+0x90>
xor    %ebx,%ebx
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    %rbx,%rax
add    $0x1048,%rsp
pop    %rbx
pop    %r14
ret
mov    %rsp,%rdi
call   1080 <perror@plt>
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/%d/stat\00"
@.str.1 = [2 x i8] c"r\00"
@EXIT_FAILURE = global i32 0
define i64 @get_memusage() {
%1 = alloca i64
%2 = alloca i32
%3 = alloca [64 x i8]
%4 = alloca [4096 x i8]
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
store i64 0, i64* %1
%8 = call i32 (...) @getpid()
store i32 %8, i32* %2
%9 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%10 = load i32, i32* %2
%11 = call i32 @snprintf(i8* %9, i32 64, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 63
store i8 0, i8* %12
%13 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %14, i32** %5
%15 = load i32*, i32** %5
%16 = icmp ne i32* %15, null
br i1 %16, label %22, label %17
17: ; preds = %0
%18 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%19 = call i32 @perror(i8* %18)
%20 = load i32, i32* @EXIT_FAILURE
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %0
%23 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%24 = load i32*, i32** %5
%25 = call i32 @fread(i8* %23, i32 1, i32 4096, i32* %24)
%26 = icmp ne i32 %25, 0
br i1 %26, label %32, label %27
27: ; preds = %22
%28 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%29 = call i32 @perror(i8* %28)
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %22
store i32 0, i32* %7
store i32 0, i32* %6
br label %33
33: ; preds = %93, %32
%34 = load i32, i32* %6
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 %35
%37 = load i8, i8* %36
%38 = icmp ne i8 %37, 0
br i1 %38, label %39, label %98
39: ; preds = %33
%40 = load i32, i32* %7
%41 = icmp eq i32 %40, 22
br i1 %41, label %42, label %73
42: ; preds = %39
br label %43
43: ; preds = %69, %42
%44 = load i32, i32* %6
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 %45
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = icmp sle i32 48, %48
br i1 %49, label %50, label %57
50: ; preds = %43
%51 = load i32, i32* %6
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 %52
%54 = load i8, i8* %53
%55 = sext i8 %54 to i32
%56 = icmp sle i32 %55, 57
br label %57
57: ; preds = %50, %43
%58 = phi i1 [ false, %43 ], [ %56, %50 ]
br i1 %58, label %59, label %72
59: ; preds = %57
%60 = load i64, i64* %1
%61 = mul nsw i64 10, %60
%62 = load i32, i32* %6
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 %63
%65 = load i8, i8* %64
%66 = sext i8 %65 to i64
%67 = add nsw i64 %61, %66
%68 = sub nsw i64 %67, 48
store i64 %68, i64* %1
br label %69
69: ; preds = %59
%70 = load i32, i32* %6
%71 = add nsw i32 %70, 1
store i32 %71, i32* %6
br label %43
72: ; preds = %57
br label %73
73: ; preds = %72, %39
br label %74
74: ; preds = %90, %73
%75 = load i32, i32* %6
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 %76
%78 = load i8, i8* %77
%79 = sext i8 %78 to i32
%80 = icmp ne i32 %79, 0
br i1 %80, label %81, label %88
81: ; preds = %74
%82 = load i32, i32* %6
%83 = sext i32 %82 to i64
%84 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 %83
%85 = load i8, i8* %84
%86 = sext i8 %85 to i32
%87 = icmp ne i32 %86, 32
br label %88
88: ; preds = %81, %74
%89 = phi i1 [ false, %74 ], [ %87, %81 ]
br i1 %89, label %90, label %93
90: ; preds = %88
%91 = load i32, i32* %6
%92 = add nsw i32 %91, 1
store i32 %92, i32* %6
br label %74
93: ; preds = %88
%94 = load i32, i32* %6
%95 = add nsw i32 %94, 1
store i32 %95, i32* %6
%96 = load i32, i32* %7
%97 = add nsw i32 %96, 1
store i32 %97, i32* %7
br label %33
98: ; preds = %33
%99 = load i32*, i32** %5
%100 = call i32 @fclose(i32* %99)
%101 = load i64, i64* %1
ret i64 %101
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/%d/stat\00"
@.str.1 = [2 x i8] c"r\00"
@EXIT_FAILURE = global i32 0
define i64 @get_memusage() {
%1 = alloca [64 x i8]
%2 = alloca [4096 x i8]
%3 = getelementptr inbounds [64 x i8], [64 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%5 = tail call i32 (...) @getpid()
%6 = call i32 @snprintf(i8* %3, i32 64, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = getelementptr inbounds [64 x i8], [64 x i8]* %1, i64 0, i64 63
store i8 0, i8* %7
%8 = call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %14
10: ; preds = %0
%11 = call i32 @perror(i8* %3)
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %0
%15 = call i32 @fread(i8* %4, i32 1, i32 4096, i32* %8)
%16 = icmp eq i32 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %14
%18 = load i8, i8* %4
%19 = icmp eq i8 %18, 0
br i1 %19, label %68, label %24
20: ; preds = %14
%21 = call i32 @perror(i8* %3)
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %17, %60
%25 = phi i8 [ %66, %60 ], [ %18, %17 ]
%26 = phi i32 [ %63, %60 ], [ 0, %17 ]
%27 = phi i32 [ %62, %60 ], [ 0, %17 ]
%28 = phi i64 [ %51, %60 ], [ 0, %17 ]
%29 = icmp eq i32 %26, 22
%30 = add i8 %25, -48
%31 = icmp ult i8 %30, 10
%32 = select i1 %29, i1 %31, i1 false
br i1 %32, label %33, label %50
33: ; preds = %24
%34 = sext i32 %27 to i64
br label %35
35: ; preds = %33, %35
%36 = phi i64 [ %34, %33 ], [ %43, %35 ]
%37 = phi i8 [ %25, %33 ], [ %45, %35 ]
%38 = phi i64 [ %28, %33 ], [ %42, %35 ]
%39 = mul nsw i64 %38, 10
%40 = zext i8 %37 to i64
%41 = add i64 %39, -48
%42 = add i64 %41, %40
%43 = add i64 %36, 1
%44 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 %43
%45 = load i8, i8* %44
%46 = add i8 %45, -48
%47 = icmp ult i8 %46, 10
br i1 %47, label %35, label %48
48: ; preds = %35
%49 = trunc i64 %43 to i32
br label %50
50: ; preds = %48, %24
%51 = phi i64 [ %28, %24 ], [ %42, %48 ]
%52 = phi i32 [ %27, %24 ], [ %49, %48 ]
%53 = sext i32 %52 to i64
br label %54
54: ; preds = %58, %50
%55 = phi i64 [ %59, %58 ], [ %53, %50 ]
%56 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 %55
%57 = load i8, i8* %56
switch i8 %57, label %58 [
i8 0, label %60
i8 32, label %60
]
58: ; preds = %54
%59 = add i64 %55, 1
br label %54
60: ; preds = %54, %54
%61 = trunc i64 %55 to i32
%62 = add nsw i32 %61, 1
%63 = add nuw nsw i32 %26, 1
%64 = sext i32 %62 to i64
%65 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 %64
%66 = load i8, i8* %65
%67 = icmp eq i8 %66, 0
br i1 %67, label %68, label %24
68: ; preds = %60, %17
%69 = phi i64 [ 0, %17 ], [ %51, %60 ]
%70 = call i32 @fclose(i32* %8)
ret i64 %69
}