#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, unsigned long);
int read(int, char *, int);
int /*<<< orphan*/ sscanf(char *, char *, unsigned long *);
int show_mem_usage(void) {
  int fd, ret;
  char buf[40];
  unsigned long mem_usage;
  fd = open("/proc/"
            "my-proc-entry",
            O_RDONLY);
  if (fd < 0) {
    perror("open "
           "/proc/"
           "my-proc-entry");
    ret = fd;
    goto out;
  }
  ret = read(fd, buf, sizeof buf);
  if (ret < 0)
    goto no_read;
  sscanf(buf, "%lu", &mem_usage);
  buf[ret] = 0;
  printf("Memory usage: %lu\n", mem_usage);
  ret = mem_usage;
no_read:
  close(fd);
out:
  return ret;
}


x86 assembly (clang, -O0):
<show_mem_usage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
lea    0xe8d(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    11a1 <show_mem_usage+0x41>
lea    0xe6f(%rip),%rdi
call   1080 <perror@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x8(%rbp)
jmp    1200 <show_mem_usage+0xa0>
mov    -0x4(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x28,%edx
call   1050 <read@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11c4 <show_mem_usage+0x64>
jmp    11f8 <show_mem_usage+0x98>
lea    -0x30(%rbp),%rdi
lea    0xe4a(%rip),%rsi
lea    -0x38(%rbp),%rdx
call   1060 <sscanf@plt>
movslq -0x8(%rbp),%rax
movb   $0x0,-0x30(%rbp,%rax,1)
mov    -0x38(%rbp),%rsi
lea    0xe31(%rip),%rdi
call   1030 <printf@plt>
mov    -0x38(%rbp),%rax
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<show_mem_usage>:
push   %rbp
push   %rbx
sub    $0x38,%rsp
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
lea    0xe8f(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11dc <show_mem_usage+0x7c>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x28,%edx
call   1050 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11ca <show_mem_usage+0x6a>
lea    0xe7a(%rip),%rsi
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rdx
call   1060 <sscanf@plt>
mov    %ebp,%eax
movb   $0x0,0x10(%rsp,%rax,1)
mov    0x8(%rsp),%rsi
lea    0xe5c(%rip),%rdi
call   1030 <printf@plt>
mov    0x8(%rsp),%ebp
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe1d(%rip),%rdi
call   1080 <perror@plt>
jmp    11d3 <show_mem_usage+0x73>

LLVM IR (-O0):
@.str = [20 x i8] c"/proc/my-proc-entry\00"
@O_RDONLY = global i32 0
@.str.1 = [25 x i8] c"open /proc/my-proc-entry\00"
@.str.2 = [4 x i8] c"%lu\00"
@.str.3 = [19 x i8] c"Memory usage: %lu\0A\00"
define i32 @show_mem_usage() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [40 x i8]
%4 = alloca i64
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %1
%7 = load i32, i32* %1
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%11 = load i32, i32* %1
store i32 %11, i32* %2
br label %32
12: ; preds = %0
%13 = load i32, i32* %1
%14 = getelementptr inbounds [40 x i8], [40 x i8]* %3, i64 0, i64 0
%15 = call i32 @read(i32 %13, i8* %14, i32 40)
store i32 %15, i32* %2
%16 = load i32, i32* %2
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %12
br label %29
19: ; preds = %12
%20 = getelementptr inbounds [40 x i8], [40 x i8]* %3, i64 0, i64 0
%21 = call i32 @sscanf(i8* %20, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64* %4)
%22 = load i32, i32* %2
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds [40 x i8], [40 x i8]* %3, i64 0, i64 %23
store i8 0, i8* %24
%25 = load i64, i64* %4
%26 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i64 %25)
%27 = load i64, i64* %4
%28 = trunc i64 %27 to i32
store i32 %28, i32* %2
br label %29
29: ; preds = %19, %18
%30 = load i32, i32* %1
%31 = call i32 @close(i32 %30)
br label %32
32: ; preds = %29, %9
%33 = load i32, i32* %2
ret i32 %33
}

LLVM IR (-O2):
@.str = [20 x i8] c"/proc/my-proc-entry\00"
@O_RDONLY = global i32 0
@.str.1 = [25 x i8] c"open /proc/my-proc-entry\00"
@.str.2 = [4 x i8] c"%lu\00"
@.str.3 = [19 x i8] c"Memory usage: %lu\0A\00"
define i32 @show_mem_usage() {
%1 = alloca [40 x i8]
%2 = alloca i64
%3 = getelementptr inbounds [40 x i8], [40 x i8]* %1, i64 0, i64 0
%4 = bitcast i64* %2 to i8*
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %24
10: ; preds = %0
%11 = call i32 @read(i32 %6, i8* %3, i32 40)
%12 = icmp slt i32 %11, 0
br i1 %12, label %21, label %13
13: ; preds = %10
%14 = call i32 @sscanf(i8* %3, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64* %2)
%15 = zext i32 %11 to i64
%16 = getelementptr inbounds [40 x i8], [40 x i8]* %1, i64 0, i64 %15
store i8 0, i8* %16
%17 = load i64, i64* %2
%18 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i64 %17)
%19 = load i64, i64* %2
%20 = trunc i64 %19 to i32
br label %21
21: ; preds = %10, %13
%22 = phi i32 [ %11, %10 ], [ %20, %13 ]
%23 = call i32 @close(i32 %6)
br label %24
24: ; preds = %21, %8
%25 = phi i32 [ %6, %8 ], [ %22, %21 ]
ret i32 %25
}