#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fscanf(int /*<<< orphan*/ *, char *, double *);
double residentSetSize(void) {
  FILE *statm = fopen("/proc/self/statm", "r");
  double res;
  if (!statm)
    return -1.0;
  if (fscanf(statm, "%*f %lf", &res)) {
    fclose(statm);
    return -1.0;
  }
  fclose(statm);
  return (res * 4096);
}


x86 assembly (clang, -O0):
<residentSetSize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xed1(%rip),%rdi
lea    0xedb(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    116c <residentSetSize+0x3c>
movsd  0xe9e(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11c0 <residentSetSize+0x90>
mov    -0x10(%rbp),%rdi
lea    0xeac(%rip),%rsi
lea    -0x18(%rbp),%rdx
call   1040 <fscanf@plt>
cmp    $0x0,%rax
je     11a5 <residentSetSize+0x75>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
movsd  0xe65(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11c0 <residentSetSize+0x90>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
movsd  0xe52(%rip),%xmm0
mulsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<residentSetSize>:
push   %r14
push   %rbx
push   %rax
lea    0xed5(%rip),%rdi
lea    0xedf(%rip),%rsi
call   1050 <fopen@plt>
movsd  0xeb1(%rip),%xmm0
test   %rax,%rax
je     118e <residentSetSize+0x5e>
mov    %rax,%rbx
lea    0xec5(%rip),%rsi
mov    %rsp,%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <fclose@plt>
movsd  0xe84(%rip),%xmm0
test   %r14,%r14
jne    118e <residentSetSize+0x5e>
movsd  (%rsp),%xmm0
mulsd  0xe7a(%rip),%xmm0
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [17 x i8] c"/proc/self/statm\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [8 x i8] c"%*f %lf\00"
define double @residentSetSize() {
%1 = alloca double
%2 = alloca i32*
%3 = alloca double
%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** %2
%5 = load i32*, i32** %2
%6 = icmp ne i32* %5, null
br i1 %6, label %8, label %7
7: ; preds = %0
store double -1.000000e+00, double* %1
br label %20
8: ; preds = %0
%9 = load i32*, i32** %2
%10 = call i64 @fscanf(i32* %9, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), double* %3)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = load i32*, i32** %2
%14 = call i32 @fclose(i32* %13)
store double -1.000000e+00, double* %1
br label %20
15: ; preds = %8
%16 = load i32*, i32** %2
%17 = call i32 @fclose(i32* %16)
%18 = load double, double* %3
%19 = fmul double %18, 4.096000e+03
store double %19, double* %1
br label %20
20: ; preds = %15, %12, %7
%21 = load double, double* %1
ret double %21
}

LLVM IR (-O2):
@.str = [17 x i8] c"/proc/self/statm\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [8 x i8] c"%*f %lf\00"
define double @residentSetSize() {
%1 = alloca double
%2 = 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))
%3 = bitcast double* %1 to i8*
%4 = icmp eq i32* %2, null
br i1 %4, label %12, label %5
5: ; preds = %0
%6 = call i64 @fscanf(i32* %2, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), double* %1)
%7 = icmp eq i64 %6, 0
%8 = call i32 @fclose(i32* %2)
br i1 %7, label %9, label %12
9: ; preds = %5
%10 = load double, double* %1
%11 = fmul double %10, 4.096000e+03
br label %12
12: ; preds = %5, %0, %9
%13 = phi double [ %11, %9 ], [ -1.000000e+00, %0 ], [ -1.000000e+00, %5 ]
ret double %13
}