#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 *);
int fprintf(int /*<<< orphan*/ *, char *, char const *, unsigned int);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, unsigned int *);
scalar_t__ getpid();
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int);
int print_memory_usage(FILE *out, const char *prefix) {
  char buf[30] = {};
  int res = 0;
  snprintf(buf, 30, "/proc/%u/statm", (unsigned)getpid());
  FILE *pf = fopen(buf, "r");
  if (pf) {
    unsigned size;
    unsigned resident;
    unsigned share;
    unsigned text;
    unsigned lib;
    unsigned data;
    unsigned dt;
    fscanf(pf, "%u", &size);
    res = fprintf(out, "%s %u MB mem used\n", prefix, size / (1024.0));
  }
  fclose(pf);
  return res;
}


x86 assembly (clang, -O0):
<print_memory_usage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x1e,%edx
call   1060 <memset@plt>
movl   $0x0,-0x34(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x68(%rbp),%rdi
mov    %eax,%ecx
mov    $0x1e,%esi
lea    0xe50(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x30(%rbp),%rdi
lea    0xe4f(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     1222 <print_memory_usage+0xb2>
mov    -0x40(%rbp),%rdi
lea    0xe32(%rip),%rsi
lea    -0x44(%rbp),%rdx
call   1070 <fscanf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x44(%rbp),%eax
cvtsi2sd %rax,%xmm0
movsd  0xdf8(%rip),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rax
mov    %eax,%ecx
lea    0xe02(%rip),%rsi
call   1080 <fprintf@plt>
mov    %eax,-0x34(%rbp)
mov    -0x40(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x34(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_memory_usage>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
xorpd  %xmm0,%xmm0
movupd %xmm0,0x1e(%rsp)
movapd %xmm0,0x10(%rsp)
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe78(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x1e,%esi
mov    %eax,%ecx
call   1050 <snprintf@plt>
lea    0xe6c(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11fc <print_memory_usage+0x9c>
lea    0xe57(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rbx,%rdi
call   1060 <fscanf@plt>
mov    0xc(%rsp),%eax
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
mulsd  0xe1d(%rip),%xmm0
cvttsd2si %xmm0,%rcx
lea    0xe2d(%rip),%rsi
mov    %r15,%rdi
mov    %r14,%rdx
call   1070 <fprintf@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"/proc/%u/statm\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%u\00"
@.str.3 = [19 x i8] c"%s %u MB mem used\0A\00"
define i32 @print_memory_usage(i32* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca [30 x i8]
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32* %0, i32** %3
store i8* %1, i8** %4
%15 = bitcast [30 x i8]* %5 to i8*
store i32 0, i32* %6
%16 = getelementptr inbounds [30 x i8], [30 x i8]* %5, i64 0, i64 0
%17 = call i64 (...) @getpid()
%18 = trunc i64 %17 to i32
%19 = call i32 @snprintf(i8* %16, i32 30, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = getelementptr inbounds [30 x i8], [30 x i8]* %5, i64 0, i64 0
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %21, i32** %7
%22 = load i32*, i32** %7
%23 = icmp ne i32* %22, null
br i1 %23, label %24, label %34
24: ; preds = %2
%25 = load i32*, i32** %7
%26 = call i32 @fscanf(i32* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %8)
%27 = load i32*, i32** %3
%28 = load i8*, i8** %4
%29 = load i32, i32* %8
%30 = uitofp i32 %29 to double
%31 = fdiv double %30, 1.024000e+03
%32 = fptoui double %31 to i32
%33 = call i32 @fprintf(i32* %27, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %28, i32 %32)
store i32 %33, i32* %6
br label %34
34: ; preds = %24, %2
%35 = load i32*, i32** %7
%36 = call i32 @fclose(i32* %35)
%37 = load i32, i32* %6
ret i32 %37
}

LLVM IR (-O2):
@.str = [15 x i8] c"/proc/%u/statm\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%u\00"
@.str.3 = [19 x i8] c"%s %u MB mem used\0A\00"
define i32 @print_memory_usage(i32* nocapture %0, i8* %1) {
%3 = alloca [30 x i8]
%4 = alloca i32
%5 = getelementptr inbounds [30 x i8], [30 x i8]* %3, i64 0, i64 0
%6 = tail call i64 (...) @getpid()
%7 = trunc i64 %6 to i32
%8 = call i32 @snprintf(i8* %5, i32 30, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %19, label %11
11: ; preds = %2
%12 = bitcast i32* %4 to i8*
%13 = call i32 @fscanf(i32* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %4)
%14 = load i32, i32* %4
%15 = uitofp i32 %14 to double
%16 = fmul double %15, 0x3F50000000000000
%17 = fptoui double %16 to i32
%18 = call i32 @fprintf(i32* %0, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %1, i32 %17)
br label %19
19: ; preds = %11, %2
%20 = phi i32 [ %18, %11 ], [ 0, %2 ]
%21 = call i32 @fclose(i32* %9)
ret i32 %20
}