#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 asprintf(char **, char *, unsigned int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int fscanf(int /*<<< orphan*/ *, char *, unsigned int *, unsigned int *,
           unsigned int *);
int /*<<< orphan*/ perror(char *);
void pid_getmem(unsigned pid, unsigned *rss, unsigned *shared) {
  char *file;
  if (asprintf(&file, "/proc/%u/statm", pid) == -1) {
    perror("asprintf");
    exit(1);
  }
  FILE *fp = fopen(file, "r");
  if (!fp) {
    free(file);
    return;
  }
  free(file);
  unsigned a, b, c;
  if (3 != fscanf(fp, "%u %u %u", &a, &b, &c)) {
    fclose(fp);
    return;
  }
  *rss += b;
  *shared += c;
  fclose(fp);
}


x86 assembly (clang, -O0):
<pid_getmem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x4(%rbp),%edx
lea    -0x20(%rbp),%rdi
lea    0xe6f(%rip),%rsi
call   1060 <asprintf@plt>
cmp    $0xffffffff,%eax
jne    11b5 <pid_getmem+0x45>
lea    0xe69(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x20(%rbp),%rdi
lea    0xe58(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11e2 <pid_getmem+0x72>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
jmp    1243 <pid_getmem+0xd3>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
lea    0xe24(%rip),%rsi
lea    -0x2c(%rbp),%rdx
lea    -0x30(%rbp),%rcx
lea    -0x34(%rbp),%r8
call   1050 <fscanf@plt>
mov    %eax,%ecx
mov    $0x3,%eax
cmp    %ecx,%eax
je     1224 <pid_getmem+0xb4>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
jmp    1243 <pid_getmem+0xd3>
mov    -0x30(%rbp),%ecx
mov    -0x10(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
mov    -0x34(%rbp),%ecx
mov    -0x18(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pid_getmem>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%edx
lea    0xe78(%rip),%rsi
lea    0x10(%rsp),%rdi
call   1060 <asprintf@plt>
cmp    $0xffffffff,%eax
je     11fd <pid_getmem+0x8d>
mov    0x10(%rsp),%rdi
lea    0xe75(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
test   %rbx,%rbx
je     11f3 <pid_getmem+0x83>
lea    0xe59(%rip),%rsi
lea    0x1c(%rsp),%rdx
lea    0xc(%rsp),%rcx
lea    0x8(%rsp),%r8
mov    %rbx,%rdi
call   1050 <fscanf@plt>
cmp    $0x3,%eax
jne    11eb <pid_getmem+0x7b>
mov    0xc(%rsp),%eax
add    %eax,(%r15)
mov    0x8(%rsp),%eax
add    %eax,(%r14)
mov    %rbx,%rdi
call   1040 <fclose@plt>
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe0b(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"/proc/%u/statm\00"
@.str.1 = [9 x i8] c"asprintf\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [9 x i8] c"%u %u %u\00"
define void @pid_getmem(i32 %0, i32* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %4
store i32* %1, i32** %5
store i32* %2, i32** %6
%12 = load i32, i32* %4
%13 = call i32 @asprintf(i8** %7, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %3
%16 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %3
%19 = load i8*, i8** %7
%20 = call i32* @fopen(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %20, i32** %8
%21 = load i32*, i32** %8
%22 = icmp ne i32* %21, null
br i1 %22, label %26, label %23
23: ; preds = %18
%24 = load i8*, i8** %7
%25 = call i32 @free(i8* %24)
br label %46
26: ; preds = %18
%27 = load i8*, i8** %7
%28 = call i32 @free(i8* %27)
%29 = load i32*, i32** %8
%30 = call i32 @fscanf(i32* %29, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32* %9, i32* %10, i32* %11)
%31 = icmp ne i32 3, %30
br i1 %31, label %32, label %35
32: ; preds = %26
%33 = load i32*, i32** %8
%34 = call i32 @fclose(i32* %33)
br label %46
35: ; preds = %26
%36 = load i32, i32* %10
%37 = load i32*, i32** %5
%38 = load i32, i32* %37
%39 = add i32 %38, %36
store i32 %39, i32* %37
%40 = load i32, i32* %11
%41 = load i32*, i32** %6
%42 = load i32, i32* %41
%43 = add i32 %42, %40
store i32 %43, i32* %41
%44 = load i32*, i32** %8
%45 = call i32 @fclose(i32* %44)
br label %46
46: ; preds = %35, %32, %23
ret void
}

LLVM IR (-O2):
@.str = [15 x i8] c"/proc/%u/statm\00"
@.str.1 = [9 x i8] c"asprintf\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [9 x i8] c"%u %u %u\00"
define void @pid_getmem(i32 %0, i32* nocapture %1, i32* nocapture %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = bitcast i8** %4 to i8*
%9 = call i32 @asprintf(i8** %4, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %0)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %3
%12 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %3
%15 = load i8*, i8** %4
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%17 = icmp eq i32* %16, null
%18 = load i8*, i8** %4
%19 = call i32 @free(i8* %18)
br i1 %17, label %35, label %20
20: ; preds = %14
%21 = bitcast i32* %5 to i8*
%22 = bitcast i32* %6 to i8*
%23 = bitcast i32* %7 to i8*
%24 = call i32 @fscanf(i32* %16, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32* %5, i32* %6, i32* %7)
%25 = icmp eq i32 %24, 3
br i1 %25, label %26, label %33
26: ; preds = %20
%27 = load i32, i32* %6
%28 = load i32, i32* %1
%29 = add i32 %28, %27
store i32 %29, i32* %1
%30 = load i32, i32* %7
%31 = load i32, i32* %2
%32 = add i32 %31, %30
store i32 %32, i32* %2
br label %33
33: ; preds = %20, %26
%34 = call i32 @fclose(i32* %16)
br label %35
35: ; preds = %14, %33
ret void
}