#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 */
struct rusage {
  scalar_t__ ru_isrss;
  scalar_t__ ru_idrss;
};
typedef int /*<<< orphan*/ file_buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ RUSAGE_SELF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ getrusage(int /*<<< orphan*/, struct rusage *);
int /*<<< orphan*/ snprintf(char *, int, char *, int, int, int, int);
int /*<<< orphan*/ sscanf(char *, char *, int *);
void get_memusage(int buflen, char *buf) {
  struct rusage ru;
  int vmdata = 0, vmstk = 0;
  FILE *fp;
  char file_buf[128];
  if ((fp = fopen("/proc/self/status", "r"))) {
    while (fgets(file_buf, sizeof(file_buf), fp)) {
      sscanf(file_buf, "VmData: %d", &vmdata);
      sscanf(file_buf, "VmStk: %d", &vmstk);
    }
    fclose(fp);
  }
  getrusage(RUSAGE_SELF, &ru);
  snprintf(buf, buflen,
           "VmData: %d KB, VmStk: %d KB, "
           "idrss: %d KB, isrss: %d KB",
           vmdata, vmstk, (int)ru.ru_idrss, (int)ru.ru_isrss);
}


x86 assembly (clang, -O0):
<get_memusage>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
lea    0xe79(%rip),%rdi
lea    0xe84(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     1201 <get_memusage+0xa1>
jmp    11a6 <get_memusage+0x46>
lea    -0xb0(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    $0x80,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     11f8 <get_memusage+0x98>
lea    -0xb0(%rbp),%rdi
lea    0xe41(%rip),%rsi
lea    -0x24(%rbp),%rdx
call   1060 <sscanf@plt>
lea    -0xb0(%rbp),%rdi
lea    0xe35(%rip),%rsi
lea    -0x28(%rbp),%rdx
call   1060 <sscanf@plt>
jmp    11a6 <get_memusage+0x46>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1080 <getrusage@plt>
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%esi
mov    -0x24(%rbp),%ecx
mov    -0x28(%rbp),%r8d
mov    -0x18(%rbp),%rax
mov    %eax,%r9d
mov    -0x20(%rbp),%rax
lea    0xdf6(%rip),%rdx
mov    %eax,(%rsp)
call   1040 <snprintf@plt>
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_memusage>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xb8,%rsp
mov    %rsi,%rbp
mov    %edi,%r14d
movl   $0x0,0x10(%rsp)
movl   $0x0,0xc(%rsp)
lea    0xe72(%rip),%rdi
lea    0xe7d(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1233 <get_memusage+0xd3>
mov    %rax,%rbx
mov    %rbp,0x18(%rsp)
mov    %r14d,0x14(%rsp)
lea    0x30(%rsp),%rdi
mov    $0x80,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1221 <get_memusage+0xc1>
lea    0xe46(%rip),%r12
lea    0x30(%rsp),%rbp
lea    0x10(%rsp),%r13
lea    0xe40(%rip),%r14
lea    0xc(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1060 <sscanf@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1060 <sscanf@plt>
mov    %rbp,%rdi
mov    $0x80,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
jne    11f0 <get_memusage+0x90>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x14(%rsp),%r14d
mov    0x18(%rsp),%rbp
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
lea    0x20(%rsp),%rsi
call   1080 <getrusage@plt>
mov    0x10(%rsp),%ecx
mov    0xc(%rsp),%r8d
mov    0x20(%rsp),%eax
mov    0x28(%rsp),%r9d
mov    %eax,(%rsp)
lea    0xdc7(%rip),%rdx
mov    %rbp,%rdi
mov    %r14d,%esi
call   1040 <snprintf@plt>
add    $0xb8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.rusage = type { i64, i64 }
@.str = [18 x i8] c"/proc/self/status\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [11 x i8] c"VmData: %d\00"
@.str.3 = [10 x i8] c"VmStk: %d\00"
@RUSAGE_SELF = global i32 0
@.str.4 = [56 x i8] c"VmData: %d KB, VmStk: %d KB, idrss: %d KB, isrss: %d KB\00"
define void @get_memusage(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.rusage
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca [128 x i8]
store i32 %0, i32* %3
store i8* %1, i8** %4
store i32 0, i32* %6
store i32 0, i32* %7
%10 = call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %10, i32** %8
%11 = icmp ne i32* %10, null
br i1 %11, label %12, label %26
12: ; preds = %2
br label %13
13: ; preds = %18, %12
%14 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%15 = load i32*, i32** %8
%16 = call i64 @fgets(i8* %14, i32 128, i32* %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %13
%19 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%20 = call i32 @sscanf(i8* %19, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32* %6)
%21 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%22 = call i32 @sscanf(i8* %21, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32* %7)
br label %13
23: ; preds = %13
%24 = load i32*, i32** %8
%25 = call i32 @fclose(i32* %24)
br label %26
26: ; preds = %23, %2
%27 = load i32, i32* @RUSAGE_SELF
%28 = call i32 @getrusage(i32 %27, %struct.rusage* %5)
%29 = load i8*, i8** %4
%30 = load i32, i32* %3
%31 = load i32, i32* %6
%32 = load i32, i32* %7
%33 = getelementptr inbounds %struct.rusage, %struct.rusage* %5, i32 0, i32 1
%34 = load i64, i64* %33
%35 = trunc i64 %34 to i32
%36 = getelementptr inbounds %struct.rusage, %struct.rusage* %5, i32 0, i32 0
%37 = load i64, i64* %36
%38 = trunc i64 %37 to i32
%39 = call i32 @snprintf(i8* %29, i32 %30, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.4, i64 0, i64 0), i32 %31, i32 %32, i32 %35, i32 %38)
ret void
}

LLVM IR (-O2):
%struct.rusage = type { i64, i64 }
@.str = [18 x i8] c"/proc/self/status\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [11 x i8] c"VmData: %d\00"
@.str.3 = [10 x i8] c"VmStk: %d\00"
@RUSAGE_SELF = global i32 0
@.str.4 = [56 x i8] c"VmData: %d KB, VmStk: %d KB, idrss: %d KB, isrss: %d KB\00"
define void @get_memusage(i32 %0, i8* %1) {
%3 = alloca %struct.rusage
%4 = alloca i32
%5 = alloca i32
%6 = alloca [128 x i8]
%7 = bitcast %struct.rusage* %3 to i8*
%8 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%9 = bitcast i32* %5 to i8*
store i32 0, i32* %5
%10 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%11 = tail call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %23, label %13
13: ; preds = %2
%14 = call i64 @fgets(i8* %10, i32 128, i32* %11)
%15 = icmp eq i64 %14, 0
br i1 %15, label %21, label %16
16: ; preds = %13, %16
%17 = call i32 @sscanf(i8* %10, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32* %4)
%18 = call i32 @sscanf(i8* %10, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32* %5)
%19 = call i64 @fgets(i8* %10, i32 128, i32* %11)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %16
21: ; preds = %16, %13
%22 = call i32 @fclose(i32* %11)
br label %23
23: ; preds = %21, %2
%24 = load i32, i32* @RUSAGE_SELF
%25 = call i32 @getrusage(i32 %24, %struct.rusage* %3)
%26 = load i32, i32* %4
%27 = load i32, i32* %5
%28 = getelementptr inbounds %struct.rusage, %struct.rusage* %3, i64 0, i32 1
%29 = load i64, i64* %28
%30 = trunc i64 %29 to i32
%31 = getelementptr inbounds %struct.rusage, %struct.rusage* %3, i64 0, i32 0
%32 = load i64, i64* %31
%33 = trunc i64 %32 to i32
%34 = call i32 @snprintf(i8* %1, i32 %0, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.4, i64 0, i64 0), i32 %26, i32 %27, i32 %30, i32 %33)
ret void
}