#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 scalar_t__ pid_t;
typedef int /*<<< orphan*/ mem_info;
typedef int /*<<< orphan*/ file_name;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int getpagesize();
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int sscanf(char *, char *, int *);
int get_rss(pid_t pid) {
  char file_name[64];
  int fd;
  char mem_info[128];
  int rval;
  int rss;
  snprintf(file_name, sizeof(file_name), "/proc/%d/statm", (int)pid);
  fd = open(file_name, O_RDONLY);
  if (fd == -1)
    return -1;
  rval = read(fd, mem_info, sizeof(mem_info) - 1);
  close(fd);
  if (rval <= 0)
    return -1;
  mem_info[rval] = '\0';
  rval = sscanf(mem_info, "%*d %d", &rss);
  if (rval != 1)
    return -1;
  return rss * getpagesize() / 1024;
}


x86 assembly (clang, -O0):
<get_rss>:
push   %rbp
mov    %rsp,%rbp
sub    $0xf0,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    %eax,%ecx
mov    $0x40,%esi
lea    0xe7b(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x50(%rbp),%rdi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0xffffffff,-0x54(%rbp)
jne    11b5 <get_rss+0x55>
movl   $0xffffffff,-0x4(%rbp)
jmp    1261 <get_rss+0x101>
mov    -0x54(%rbp),%edi
lea    -0xe0(%rbp),%rsi
mov    $0x7f,%edx
call   1050 <read@plt>
mov    %eax,-0xe4(%rbp)
mov    -0x54(%rbp),%edi
call   1040 <close@plt>
cmpl   $0x0,-0xe4(%rbp)
jg     11f0 <get_rss+0x90>
movl   $0xffffffff,-0x4(%rbp)
jmp    1261 <get_rss+0x101>
movslq -0xe4(%rbp),%rax
movb   $0x0,-0xe0(%rbp,%rax,1)
lea    -0xe0(%rbp),%rdi
lea    0xe02(%rip),%rsi
lea    -0xe8(%rbp),%rdx
call   1060 <sscanf@plt>
mov    %eax,-0xe4(%rbp)
cmpl   $0x1,-0xe4(%rbp)
je     1238 <get_rss+0xd8>
movl   $0xffffffff,-0x4(%rbp)
jmp    1261 <get_rss+0x101>
mov    -0xe8(%rbp),%eax
mov    %eax,-0xec(%rbp)
mov    $0x0,%al
call   1080 <getpagesize@plt>
mov    %eax,%ecx
mov    -0xec(%rbp),%eax
imul   %ecx,%eax
mov    $0x400,%ecx
cltd
idiv   %ecx
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xf0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_rss>:
push   %rbp
push   %r14
push   %rbx
sub    $0xd0,%rsp
mov    %rdi,%rcx
lea    0xe8b(%rip),%rdx
lea    0x90(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x40,%esi
call   1030 <snprintf@plt>
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
mov    $0xffffffff,%r14d
cmp    $0xffffffff,%eax
je     1207 <get_rss+0xa7>
mov    %eax,%ebp
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0x7f,%edx
call   1050 <read@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1040 <close@plt>
test   %ebx,%ebx
jle    1207 <get_rss+0xa7>
mov    %ebx,%eax
movb   $0x0,0x10(%rsp,%rax,1)
lea    0xe3b(%rip),%rsi
lea    0x10(%rsp),%rdi
lea    0xc(%rsp),%rdx
call   1060 <sscanf@plt>
cmp    $0x1,%eax
jne    1207 <get_rss+0xa7>
mov    0xc(%rsp),%ebx
xor    %eax,%eax
call   1080 <getpagesize@plt>
imul   %ebx,%eax
lea    0x3ff(%rax),%r14d
test   %eax,%eax
cmovns %eax,%r14d
sar    $0xa,%r14d
mov    %r14d,%eax
add    $0xd0,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"/proc/%d/statm\00"
@O_RDONLY = global i32 0
@.str.1 = [7 x i8] c"%*d %d\00"
define i32 @get_rss(i64 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca [64 x i8]
%5 = alloca i32
%6 = alloca [128 x i8]
%7 = alloca i32
%8 = alloca i32
store i64 %0, i64* %3
%9 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%10 = load i64, i64* %3
%11 = trunc i64 %10 to i32
%12 = call i32 @snprintf(i8* %9, i32 64, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%14 = load i32, i32* @O_RDONLY
%15 = call i32 @open(i8* %13, i32 %14)
store i32 %15, i32* %5
%16 = load i32, i32* %5
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %19
18: ; preds = %1
store i32 -1, i32* %2
br label %42
19: ; preds = %1
%20 = load i32, i32* %5
%21 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%22 = call i32 @read(i32 %20, i8* %21, i32 127)
store i32 %22, i32* %7
%23 = load i32, i32* %5
%24 = call i32 @close(i32 %23)
%25 = load i32, i32* %7
%26 = icmp sle i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %19
store i32 -1, i32* %2
br label %42
28: ; preds = %19
%29 = load i32, i32* %7
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 %30
store i8 0, i8* %31
%32 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%33 = call i32 @sscanf(i8* %32, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32* %8)
store i32 %33, i32* %7
%34 = load i32, i32* %7
%35 = icmp ne i32 %34, 1
br i1 %35, label %36, label %37
36: ; preds = %28
store i32 -1, i32* %2
br label %42
37: ; preds = %28
%38 = load i32, i32* %8
%39 = call i32 (...) @getpagesize()
%40 = mul nsw i32 %38, %39
%41 = sdiv i32 %40, 1024
store i32 %41, i32* %2
br label %42
42: ; preds = %37, %36, %27, %18
%43 = load i32, i32* %2
ret i32 %43
}

LLVM IR (-O2):
@.str = [15 x i8] c"/proc/%d/statm\00"
@O_RDONLY = global i32 0
@.str.1 = [7 x i8] c"%*d %d\00"
define i32 @get_rss(i64 %0) {
%2 = alloca [64 x i8]
%3 = alloca [128 x i8]
%4 = alloca i32
%5 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%7 = bitcast i32* %4 to i8*
%8 = trunc i64 %0 to i32
%9 = call i32 @snprintf(i8* %5, i32 64, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %5, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %27, label %13
13: ; preds = %1
%14 = call i32 @read(i32 %11, i8* %6, i32 127)
%15 = call i32 @close(i32 %11)
%16 = icmp slt i32 %14, 1
br i1 %16, label %27, label %17
17: ; preds = %13
%18 = zext i32 %14 to i64
%19 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 %18
store i8 0, i8* %19
%20 = call i32 @sscanf(i8* %6, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32* %4)
%21 = icmp eq i32 %20, 1
br i1 %21, label %22, label %27
22: ; preds = %17
%23 = load i32, i32* %4
%24 = call i32 (...) @getpagesize()
%25 = mul nsw i32 %24, %23
%26 = sdiv i32 %25, 1024
br label %27
27: ; preds = %17, %13, %1, %22
%28 = phi i32 [ %26, %22 ], [ -1, %1 ], [ -1, %13 ], [ -1, %17 ]
ret i32 %28
}