#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;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ ENOENT;
int /*<<< orphan*/ O_RDONLY;
int close(int);
scalar_t__ errno;
int /*<<< orphan*/ error(int /*<<< orphan*/ *);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ sscanf(char *, char *, int *);
char *strstr(char *, char *);
int memusage(pid_t pid) {
  char a[8192], *p, *q;
  int data, stack;
  int n, v, fd;
  p = a;
  sprintf(p, "/proc/%d/status", pid);
  fd = open(p, O_RDONLY);
  if (fd < 0) {
    if (errno == ENOENT) {
      return 0;
    } else {
      error(NULL);
    }
  }
  do
    n = read(fd, p, 8192);
  while ((n < 0) && (errno == EINTR));
  if (n < 0)
    error(NULL);
  do
    v = close(fd);
  while ((v < 0) && (errno == EINTR));
  if (v < 0)
    error(NULL);
  data = stack = 0;
  q = strstr(p, "VmData:");
  if (q != NULL) {
    sscanf(q, "%*s %d", &data);
    q = strstr(q, "VmStk:");
    if (q != NULL)
      sscanf(q, "%*s %d\n", &stack);
  }
  return (data + stack);
}


x86 assembly (clang, -O0):
<memusage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2040,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x2010(%rbp),%rax
mov    %rax,-0x2018(%rbp)
mov    -0x2018(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe63(%rip),%rsi
call   1080 <sprintf@plt>
mov    -0x2018(%rbp),%rdi
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x2034(%rbp)
cmpl   $0x0,-0x2034(%rbp)
jge    11fe <memusage+0x8e>
mov    0x2e07(%rip),%rax
mov    (%rax),%rax
mov    0x2de5(%rip),%rcx
cmp    (%rcx),%rax
jne    11f0 <memusage+0x80>
movl   $0x0,-0x4(%rbp)
jmp    138e <memusage+0x21e>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <error@plt>
jmp    11fe <memusage+0x8e>
jmp    1203 <memusage+0x93>
mov    -0x2034(%rbp),%edi
mov    -0x2018(%rbp),%rsi
mov    $0x2000,%edx
call   1040 <read@plt>
mov    %eax,-0x202c(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x202c(%rbp)
mov    %al,-0x2035(%rbp)
jge    1252 <memusage+0xe2>
mov    0x2d9c(%rip),%rax
mov    (%rax),%rax
mov    0x2d8a(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x2035(%rbp)
mov    -0x2035(%rbp),%al
test   $0x1,%al
jne    1203 <memusage+0x93>
cmpl   $0x0,-0x202c(%rbp)
jge    1276 <memusage+0x106>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <error@plt>
jmp    127b <memusage+0x10b>
mov    -0x2034(%rbp),%edi
call   1030 <close@plt>
mov    %eax,-0x2030(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x2030(%rbp)
mov    %al,-0x2036(%rbp)
jge    12be <memusage+0x14e>
mov    0x2d30(%rip),%rax
mov    (%rax),%rax
mov    0x2d1e(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x2036(%rbp)
mov    -0x2036(%rbp),%al
test   $0x1,%al
jne    127b <memusage+0x10b>
cmpl   $0x0,-0x2030(%rbp)
jge    12e2 <memusage+0x172>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <error@plt>
movl   $0x0,-0x2028(%rbp)
movl   $0x0,-0x2024(%rbp)
mov    -0x2018(%rbp),%rdi
lea    0xd0c(%rip),%rsi
call   1090 <strstr@plt>
mov    %rax,-0x2020(%rbp)
cmpq   $0x0,-0x2020(%rbp)
je     137f <memusage+0x20f>
mov    -0x2020(%rbp),%rdi
lea    0xcec(%rip),%rsi
lea    -0x2024(%rbp),%rdx
call   1050 <sscanf@plt>
mov    -0x2020(%rbp),%rdi
lea    0xcd9(%rip),%rsi
call   1090 <strstr@plt>
mov    %rax,-0x2020(%rbp)
cmpq   $0x0,-0x2020(%rbp)
je     137a <memusage+0x20a>
mov    -0x2020(%rbp),%rdi
lea    0xcb8(%rip),%rsi
lea    -0x2028(%rbp),%rdx
call   1050 <sscanf@plt>
jmp    137f <memusage+0x20f>
mov    -0x2024(%rbp),%eax
add    -0x2028(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<memusage>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x2018,%rsp
mov    %edi,%edx
lea    0xe7a(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1080 <sprintf@plt>
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11cd <memusage+0x5d>
mov    0x2e27(%rip),%rax
mov    (%rax),%rax
mov    0x2e05(%rip),%rcx
xor    %ebp,%ebp
cmp    (%rcx),%rax
je     129e <memusage+0x12e>
xor    %edi,%edi
call   1060 <error@plt>
lea    0x10(%rsp),%r14
mov    0x2dff(%rip),%r15
mov    0x2df0(%rip),%rbp
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x2000,%edx
call   1040 <read@plt>
test   %eax,%eax
jns    11fc <memusage+0x8c>
mov    0x0(%rbp),%rcx
cmp    %rcx,(%r15)
je     11e0 <memusage+0x70>
test   %eax,%eax
jns    1210 <memusage+0xa0>
xor    %edi,%edi
call   1060 <error@plt>
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1030 <close@plt>
test   %eax,%eax
jns    1224 <memusage+0xb4>
mov    0x0(%rbp),%rcx
cmp    %rcx,(%r15)
je     1210 <memusage+0xa0>
test   %eax,%eax
jns    122f <memusage+0xbf>
xor    %edi,%edi
call   1060 <error@plt>
movl   $0x0,0x8(%rsp)
movl   $0x0,0xc(%rsp)
lea    0xdca(%rip),%rsi
lea    0x10(%rsp),%rdi
call   1090 <strstr@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     129a <memusage+0x12a>
mov    %rax,%rbx
lea    0xdb7(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rax,%rdi
call   1050 <sscanf@plt>
lea    0xdaa(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strstr@plt>
test   %rax,%rax
je     129a <memusage+0x12a>
lea    0xd9d(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %rax,%rdi
call   1050 <sscanf@plt>
mov    0x8(%rsp),%ebp
add    0xc(%rsp),%ebp
mov    %ebp,%eax
add    $0x2018,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"/proc/%d/status\00"
@O_RDONLY = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@EINTR = global i64 0
@.str.1 = [8 x i8] c"VmData:\00"
@.str.2 = [7 x i8] c"%*s %d\00"
@.str.3 = [7 x i8] c"VmStk:\00"
@.str.4 = [8 x i8] c"%*s %d\0A\00"
define i32 @memusage(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [8192 x i8]
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %3
%12 = getelementptr inbounds [8192 x i8], [8192 x i8]* %4, i64 0, i64 0
store i8* %12, i8** %5
%13 = load i8*, i8** %5
%14 = load i32, i32* %3
%15 = call i32 @sprintf(i8* %13, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = load i8*, i8** %5
%17 = load i32, i32* @O_RDONLY
%18 = call i32 @open(i8* %16, i32 %17)
store i32 %18, i32* %11
%19 = load i32, i32* %11
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %29
21: ; preds = %1
%22 = load i64, i64* @errno
%23 = load i64, i64* @ENOENT
%24 = icmp eq i64 %22, %23
br i1 %24, label %25, label %26
25: ; preds = %21
store i32 0, i32* %2
br label %86
26: ; preds = %21
%27 = call i32 @error(i32* null)
br label %28
28: ; preds = %26
br label %29
29: ; preds = %28, %1
br label %30
30: ; preds = %41, %29
%31 = load i32, i32* %11
%32 = load i8*, i8** %5
%33 = call i32 @read(i32 %31, i8* %32, i32 8192)
store i32 %33, i32* %9
br label %34
34: ; preds = %30
%35 = load i32, i32* %9
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %34
%38 = load i64, i64* @errno
%39 = load i64, i64* @EINTR
%40 = icmp eq i64 %38, %39
br label %41
41: ; preds = %37, %34
%42 = phi i1 [ false, %34 ], [ %40, %37 ]
br i1 %42, label %30, label %43
43: ; preds = %41
%44 = load i32, i32* %9
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %43
%47 = call i32 @error(i32* null)
br label %48
48: ; preds = %46, %43
br label %49
49: ; preds = %59, %48
%50 = load i32, i32* %11
%51 = call i32 @close(i32 %50)
store i32 %51, i32* %10
br label %52
52: ; preds = %49
%53 = load i32, i32* %10
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %59
55: ; preds = %52
%56 = load i64, i64* @errno
%57 = load i64, i64* @EINTR
%58 = icmp eq i64 %56, %57
br label %59
59: ; preds = %55, %52
%60 = phi i1 [ false, %52 ], [ %58, %55 ]
br i1 %60, label %49, label %61
61: ; preds = %59
%62 = load i32, i32* %10
%63 = icmp slt i32 %62, 0
br i1 %63, label %64, label %66
64: ; preds = %61
%65 = call i32 @error(i32* null)
br label %66
66: ; preds = %64, %61
store i32 0, i32* %8
store i32 0, i32* %7
%67 = load i8*, i8** %5
%68 = call i8* @strstr(i8* %67, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i8* %68, i8** %6
%69 = load i8*, i8** %6
%70 = icmp ne i8* %69, null
br i1 %70, label %71, label %82
71: ; preds = %66
%72 = load i8*, i8** %6
%73 = call i32 @sscanf(i8* %72, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32* %7)
%74 = load i8*, i8** %6
%75 = call i8* @strstr(i8* %74, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
store i8* %75, i8** %6
%76 = load i8*, i8** %6
%77 = icmp ne i8* %76, null
br i1 %77, label %78, label %81
78: ; preds = %71
%79 = load i8*, i8** %6
%80 = call i32 @sscanf(i8* %79, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32* %8)
br label %81
81: ; preds = %78, %71
br label %82
82: ; preds = %81, %66
%83 = load i32, i32* %7
%84 = load i32, i32* %8
%85 = add nsw i32 %83, %84
store i32 %85, i32* %2
br label %86
86: ; preds = %82, %25
%87 = load i32, i32* %2
ret i32 %87
}

LLVM IR (-O2):
@.str = [16 x i8] c"/proc/%d/status\00"
@O_RDONLY = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@EINTR = global i64 0
@.str.1 = [8 x i8] c"VmData:\00"
@.str.2 = [7 x i8] c"%*s %d\00"
@.str.3 = [7 x i8] c"VmStk:\00"
@.str.4 = [8 x i8] c"%*s %d\0A\00"
define i32 @memusage(i32 %0) {
%2 = alloca [8192 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = getelementptr inbounds [8192 x i8], [8192 x i8]* %2, i64 0, i64 0
%6 = bitcast i32* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %0)
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* %5, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %1
%13 = load i64, i64* @errno
%14 = load i64, i64* @ENOENT
%15 = icmp eq i64 %13, %14
br i1 %15, label %54, label %16
16: ; preds = %12
%17 = tail call i32 @error(i32* null)
br label %18
18: ; preds = %16, %1
br label %19
19: ; preds = %18, %19
%20 = call i32 @read(i32 %10, i8* %5, i32 8192)
%21 = icmp slt i32 %20, 0
%22 = load i64, i64* @errno
%23 = load i64, i64* @EINTR
%24 = icmp eq i64 %22, %23
%25 = select i1 %21, i1 %24, i1 false
br i1 %25, label %19, label %26
26: ; preds = %19
br i1 %21, label %27, label %29
27: ; preds = %26
%28 = tail call i32 @error(i32* null)
br label %29
29: ; preds = %27, %26
br label %30
30: ; preds = %29, %30
%31 = tail call i32 @close(i32 %10)
%32 = icmp slt i32 %31, 0
%33 = load i64, i64* @errno
%34 = load i64, i64* @EINTR
%35 = icmp eq i64 %33, %34
%36 = select i1 %32, i1 %35, i1 false
br i1 %36, label %30, label %37
37: ; preds = %30
br i1 %32, label %38, label %40
38: ; preds = %37
%39 = tail call i32 @error(i32* null)
br label %40
40: ; preds = %38, %37
store i32 0, i32* %4
store i32 0, i32* %3
%41 = call i8* @strstr(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%42 = icmp eq i8* %41, null
br i1 %42, label %50, label %43
43: ; preds = %40
%44 = call i32 @sscanf(i8* %41, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%45 = call i8* @strstr(i8* %41, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%46 = icmp eq i8* %45, null
br i1 %46, label %50, label %47
47: ; preds = %43
%48 = call i32 @sscanf(i8* %45, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32* %4)
%49 = load i32, i32* %4
br label %50
50: ; preds = %43, %47, %40
%51 = phi i32 [ 0, %43 ], [ %49, %47 ], [ 0, %40 ]
%52 = load i32, i32* %3
%53 = add nsw i32 %51, %52
br label %54
54: ; preds = %12, %50
%55 = phi i32 [ %53, %50 ], [ 0, %12 ]
ret i32 %55
}