#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 /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *, int *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int obterUsoDeMemoria(int pid) {
  int vNULLI;
  FILE *statm;
  char nomeArquivo[100];
  int numeroDePaginasMemoria;
  int memoriaEmKb = 0;
  sprintf(nomeArquivo, "/proc/%d/statm", pid);
  if ((statm = fopen(nomeArquivo, "r")) != NULL) {
    fscanf(statm, "%d\t%d", &vNULLI, &numeroDePaginasMemoria);
    fclose(statm);
    memoriaEmKb = numeroDePaginasMemoria * 4;
  }
  return memoriaEmKb;
}


x86 assembly (clang, -O0):
<obterUsoDeMemoria>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x88(%rbp)
lea    -0x80(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe9a(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x80(%rbp),%rdi
lea    0xe99(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     11bc <obterUsoDeMemoria+0x7c>
mov    -0x10(%rbp),%rdi
lea    0xe7d(%rip),%rsi
lea    -0x8(%rbp),%rdx
lea    -0x84(%rbp),%rcx
call   1040 <fscanf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x84(%rbp),%eax
shl    $0x2,%eax
mov    %eax,-0x88(%rbp)
mov    -0x88(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<obterUsoDeMemoria>:
push   %rbx
sub    $0x80,%rsp
mov    %edi,%edx
lea    0xeaf(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <sprintf@plt>
lea    0xeaa(%rip),%rsi
mov    %rbx,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     119f <obterUsoDeMemoria+0x5f>
mov    %rax,%rbx
lea    0xe95(%rip),%rsi
lea    0xc(%rsp),%rdx
lea    0x8(%rsp),%rcx
mov    %rax,%rdi
call   1040 <fscanf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x8(%rsp),%eax
shl    $0x2,%eax
jmp    11a1 <obterUsoDeMemoria+0x61>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbx
ret

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

LLVM IR (-O2):
@.str = [15 x i8] c"/proc/%d/statm\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"%d\09%d\00"
define i32 @obterUsoDeMemoria(i32 %0) {
%2 = alloca i32
%3 = alloca [100 x i8]
%4 = alloca i32
%5 = bitcast i32* %2 to i8*
%6 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%7 = bitcast i32* %4 to i8*
%8 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %0)
%9 = call i32* @fopen(i8* %6, 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 %16, label %11
11: ; preds = %1
%12 = call i32 @fscanf(i32* %9, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32* %2, i32* %4)
%13 = call i32 @fclose(i32* %9)
%14 = load i32, i32* %4
%15 = shl nsw i32 %14, 2
br label %16
16: ; preds = %11, %1
%17 = phi i32 [ %15, %11 ], [ 0, %1 ]
ret i32 %17
}