#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 /*<<< orphan*/ _SC_PAGESIZE;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, long *);
scalar_t__ sysconf(int /*<<< orphan*/);
size_t get_rss() {
  long rss = 0;
  FILE *file = NULL;
  if ((file = fopen("/proc/self/statm", "r")) == NULL)
    return 0;
  if (fscanf(file, "%*s%ld", &rss) != 1) {
    fclose(file);
    return 0;
  }
  fclose(file);
  return (size_t)rss * (size_t)sysconf(_SC_PAGESIZE);
}


x86 assembly (clang, -O0):
<get_rss>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
lea    0xea1(%rip),%rdi
lea    0xeab(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1186 <get_rss+0x46>
movq   $0x0,-0x8(%rbp)
jmp    11e7 <get_rss+0xa7>
mov    -0x18(%rbp),%rdi
lea    0xe82(%rip),%rsi
lea    -0x10(%rbp),%rdx
call   1040 <fscanf@plt>
cmp    $0x1,%eax
je     11b9 <get_rss+0x79>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    11e7 <get_rss+0xa7>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
call   1060 <sysconf@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
imul   %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_rss>:
push   %rbp
push   %rbx
push   %rax
movq   $0x0,(%rsp)
lea    0xeae(%rip),%rdi
lea    0xeb8(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     119f <get_rss+0x5f>
mov    %rax,%rbx
lea    0xea6(%rip),%rsi
mov    %rsp,%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
cmp    $0x1,%ebp
jne    119f <get_rss+0x5f>
mov    (%rsp),%rbx
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
call   1060 <sysconf@plt>
imul   %rbx,%rax
jmp    11a1 <get_rss+0x61>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"/proc/self/statm\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [7 x i8] c"%*s%ld\00"
@_SC_PAGESIZE = global i32 0
define i64 @get_rss() {
%1 = alloca i64
%2 = alloca i64
%3 = alloca i32*
store i64 0, i64* %2
store i32* null, i32** %3
%4 = call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %4, i32** %3
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %7
6: ; preds = %0
store i64 0, i64* %1
br label %21
7: ; preds = %0
%8 = load i32*, i32** %3
%9 = call i32 @fscanf(i32* %8, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i64* %2)
%10 = icmp ne i32 %9, 1
br i1 %10, label %11, label %14
11: ; preds = %7
%12 = load i32*, i32** %3
%13 = call i32 @fclose(i32* %12)
store i64 0, i64* %1
br label %21
14: ; preds = %7
%15 = load i32*, i32** %3
%16 = call i32 @fclose(i32* %15)
%17 = load i64, i64* %2
%18 = load i32, i32* @_SC_PAGESIZE
%19 = call i64 @sysconf(i32 %18)
%20 = mul i64 %17, %19
store i64 %20, i64* %1
br label %21
21: ; preds = %14, %11, %6
%22 = load i64, i64* %1
ret i64 %22
}

LLVM IR (-O2):
@.str = [17 x i8] c"/proc/self/statm\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [7 x i8] c"%*s%ld\00"
@_SC_PAGESIZE = global i32 0
define i64 @get_rss() {
%1 = alloca i64
%2 = bitcast i64* %1 to i8*
store i64 0, i64* %1
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %14, label %5
5: ; preds = %0
%6 = call i32 @fscanf(i32* %3, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i64* %1)
%7 = icmp eq i32 %6, 1
%8 = call i32 @fclose(i32* %3)
br i1 %7, label %9, label %14
9: ; preds = %5
%10 = load i64, i64* %1
%11 = load i32, i32* @_SC_PAGESIZE
%12 = call i64 @sysconf(i32 %11)
%13 = mul i64 %12, %10
br label %14
14: ; preds = %5, %0, %9
%15 = phi i64 [ %13, %9 ], [ 0, %0 ], [ 0, %5 ]
ret i64 %15
}