#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 *, long *);
int /*<<< orphan*/ sprintf(char *, char *, long);
void mem_used_kb(char *buf) {
  FILE *f;
  long tot, fr;
  f = fopen("/proc/meminfo", "r");
  fscanf(f, "%*s %ld %*s\n", &tot);
  fscanf(f, "%*s %ld %*s\n", &fr);
  fclose(f);
  sprintf(buf, "%ld", tot - fr);
}


x86 assembly (clang, -O0):
<mem_used_kb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xead(%rip),%rdi
lea    0xeb4(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea2(%rip),%rsi
lea    -0x18(%rbp),%rdx
call   1040 <fscanf@plt>
mov    -0x10(%rbp),%rdi
lea    0xe8e(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   1040 <fscanf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rdx
sub    -0x20(%rbp),%rdx
lea    0xe76(%rip),%rsi
call   1060 <sprintf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mem_used_kb>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
lea    0xead(%rip),%rdi
lea    0xeb4(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%rbx
lea    0xea7(%rip),%r15
lea    0x8(%rsp),%rdx
mov    %rax,%rdi
mov    %r15,%rsi
call   1040 <fscanf@plt>
mov    %rsp,%rdx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1040 <fscanf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x8(%rsp),%rdx
sub    (%rsp),%rdx
lea    0xe7e(%rip),%rsi
mov    %r14,%rdi
call   1060 <sprintf@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/meminfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [13 x i8] c"%*s %ld %*s\0A\00"
@.str.3 = [4 x i8] c"%ld\00"
define void @mem_used_kb(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i64
store i8* %0, i8** %2
%6 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %3
%7 = load i32*, i32** %3
%8 = call i32 @fscanf(i32* %7, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i64* %4)
%9 = load i32*, i32** %3
%10 = call i32 @fscanf(i32* %9, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i64* %5)
%11 = load i32*, i32** %3
%12 = call i32 @fclose(i32* %11)
%13 = load i8*, i8** %2
%14 = load i64, i64* %4
%15 = load i64, i64* %5
%16 = sub nsw i64 %14, %15
%17 = call i32 @sprintf(i8* %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i64 %16)
ret void
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/meminfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [13 x i8] c"%*s %ld %*s\0A\00"
@.str.3 = [4 x i8] c"%ld\00"
define void @mem_used_kb(i8* nocapture writeonly %0) {
%2 = alloca i64
%3 = alloca i64
%4 = bitcast i64* %2 to i8*
%5 = bitcast i64* %3 to i8*
%6 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = call i32 @fscanf(i32* %6, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i64* %2)
%8 = call i32 @fscanf(i32* %6, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i64* %3)
%9 = call i32 @fclose(i32* %6)
%10 = load i64, i64* %2
%11 = load i64, i64* %3
%12 = sub nsw i64 %10, %11
%13 = call i32 @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i64 %12)
ret void
}