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

/* Variables and functions */
int BUFSIZ;
scalar_t__ EOF;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ err(int /*<<< orphan*/, char *);
scalar_t__ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
unsigned long read_value(char *);
int /*<<< orphan*/ strncmp(char *, char *, int);
unsigned long read_VmRSS(void) {
  FILE *file;
  unsigned long result;
  char line[BUFSIZ];
  result = 0;
  file = fopen("/proc/self/status", "r");
  if (!file)
    err(EXIT_FAILURE, "fopen");
  while (fgets(line, BUFSIZ, file)) {
    if (!strncmp(line, "VmRSS:", 6)) {
      result = read_value(line);
      break;
    }
  }
  if (fclose(file) == EOF)
    err(EXIT_FAILURE, "fclose");
  return result * 1024L;
}


x86 assembly (clang, -O0):
<read_VmRSS>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e69(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x10(%rbp)
lea    0xe5d(%rip),%rdi
lea    0xe68(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11d3 <read_VmRSS+0x73>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
lea    0xe46(%rip),%rsi
call   1070 <err@plt>
jmp    11d8 <read_VmRSS+0x78>
mov    -0x28(%rbp),%rdi
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%rdx
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     122d <read_VmRSS+0xcd>
mov    -0x28(%rbp),%rdi
lea    0xe17(%rip),%rsi
mov    $0x6,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    1228 <read_VmRSS+0xc8>
mov    -0x28(%rbp),%rdi
call   1050 <read_value@plt>
mov    %rax,-0x10(%rbp)
jmp    122d <read_VmRSS+0xcd>
jmp    11d8 <read_VmRSS+0x78>
mov    -0x8(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2dab(%rip),%rcx
cmp    (%rcx),%rax
jne    125b <read_VmRSS+0xfb>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
lea    0xdcb(%rip),%rsi
call   1070 <err@plt>
mov    -0x10(%rbp),%rax
shl    $0xa,%rax
mov    -0x18(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_VmRSS>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e63(%rip),%r13
mov    0x0(%r13),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe6f(%rip),%rdi
lea    0xe7a(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11ba <read_VmRSS+0x5a>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
lea    0xe5f(%rip),%rsi
call   1070 <err@plt>
xor    %r14d,%r14d
lea    0xe56(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x0(%r13),%esi
mov    %rbx,%rdi
mov    %r15,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     1207 <read_VmRSS+0xa7>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0x6,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
jne    11d0 <read_VmRSS+0x70>
mov    %rbx,%rdi
call   1050 <read_value@plt>
mov    %rax,%r14
shl    $0xa,%r14
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    0x2dd2(%rip),%rcx
cmp    (%rcx),%rax
jne    1230 <read_VmRSS+0xd0>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
lea    0xdf6(%rip),%rsi
call   1070 <err@plt>
mov    %r14,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@BUFSIZ = global i32 0
@.str = [18 x i8] c"/proc/self/status\00"
@.str.1 = [2 x i8] c"r\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [6 x i8] c"fopen\00"
@.str.3 = [7 x i8] c"VmRSS:\00"
@EOF = global i64 0
@.str.4 = [7 x i8] c"fclose\00"
define i64 @read_VmRSS() {
%1 = alloca i32*
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i64
%5 = load i32, i32* @BUFSIZ
%6 = zext i32 %5 to i64
%7 = call i8* @llvm.stacksave()
store i8* %7, i8** %3
%8 = alloca i8, i64 %6
store i64 %6, i64* %4
store i64 0, i64* %2
%9 = call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %1
%10 = load i32*, i32** %1
%11 = icmp ne i32* %10, null
br i1 %11, label %15, label %12
12: ; preds = %0
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @err(i32 %13, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %15
15: ; preds = %12, %0
br label %16
16: ; preds = %26, %15
%17 = load i32, i32* @BUFSIZ
%18 = load i32*, i32** %1
%19 = call i64 @fgets(i8* %8, i32 %17, i32* %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %16
%22 = call i32 @strncmp(i8* %8, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 6)
%23 = icmp ne i32 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %21
%25 = call i64 @read_value(i8* %8)
store i64 %25, i64* %2
br label %27
26: ; preds = %21
br label %16
27: ; preds = %24, %16
%28 = load i32*, i32** %1
%29 = call i64 @fclose(i32* %28)
%30 = load i64, i64* @EOF
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %35
32: ; preds = %27
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @err(i32 %33, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %35
35: ; preds = %32, %27
%36 = load i64, i64* %2
%37 = mul i64 %36, 1024
%38 = load i8*, i8** %3
ret i64 %37
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@.str = [18 x i8] c"/proc/self/status\00"
@.str.1 = [2 x i8] c"r\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [6 x i8] c"fopen\00"
@.str.3 = [7 x i8] c"VmRSS:\00"
@EOF = global i64 0
@.str.4 = [7 x i8] c"fclose\00"
define i64 @read_VmRSS() {
%1 = load i32, i32* @BUFSIZ
%2 = zext i32 %1 to i64
%3 = alloca i8, i64 %2
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @err(i32 %7, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %9
9: ; preds = %6, %0
br label %10
10: ; preds = %9, %14
%11 = load i32, i32* @BUFSIZ
%12 = call i64 @fgets(i8* %3, i32 %11, i32* %4)
%13 = icmp eq i64 %12, 0
br i1 %13, label %20, label %14
14: ; preds = %10
%15 = call i32 @strncmp(i8* %3, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 6)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %10
17: ; preds = %14
%18 = call i64 @read_value(i8* %3)
%19 = shl i64 %18, 10
br label %20
20: ; preds = %10, %17
%21 = phi i64 [ %19, %17 ], [ 0, %10 ]
%22 = call i64 @fclose(i32* %4)
%23 = load i64, i64* @EOF
%24 = icmp eq i64 %22, %23
br i1 %24, label %25, label %28
25: ; preds = %20
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @err(i32 %26, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %28
28: ; preds = %25, %20
ret i64 %21
}