#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*/ str;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fscanf(int /*<<< orphan*/ *, char *, char *, int *);
int /*<<< orphan*/ memset(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ strncmp(char *, char *, int);
int get_ram_size(void) {
  FILE *fp;
  char str[256];
  int val = 0;
  int ret = -1;
  if (!(fp = fopen("/proc/meminfo", "r"))) {
    perror("fopen fails");
    return ret;
  }
  memset(str, 0x00, sizeof(str));
  while ((fscanf(fp, "%s %d", str, &val)) != 0 && ret == -1) {
    if (!(strncmp(str, "MemTotal", 8))) {
      ret = val;
    }
  }
  fclose(fp);
  return ret;
}


x86 assembly (clang, -O0):
<get_ram_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movl   $0x0,-0x114(%rbp)
movl   $0xffffffff,-0x118(%rbp)
ff ff ff
lea    0xe7a(%rip),%rdi
lea    0xe81(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11ba <get_ram_size+0x5a>
lea    0xe69(%rip),%rdi
call   1080 <perror@plt>
mov    -0x118(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1267 <get_ram_size+0x107>
lea    -0x110(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
mov    -0x10(%rbp),%rdi
lea    -0x110(%rbp),%rdx
lea    0xe3d(%rip),%rsi
lea    -0x114(%rbp),%rcx
call   1060 <fscanf@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x119(%rbp)
je     1210 <get_ram_size+0xb0>
cmpl   $0xffffffff,-0x118(%rbp)
sete   %al
mov    %al,-0x119(%rbp)
mov    -0x119(%rbp),%al
test   $0x1,%al
jne    1223 <get_ram_size+0xc3>
jmp    1255 <get_ram_size+0xf5>
lea    -0x110(%rbp),%rdi
lea    0xdf1(%rip),%rsi
mov    $0x8,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    1250 <get_ram_size+0xf0>
mov    -0x114(%rbp),%eax
mov    %eax,-0x118(%rbp)
jmp    11cd <get_ram_size+0x6d>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x118(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_ram_size>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
movl   $0x0,0xc(%rsp)
lea    0xe80(%rip),%rdi
lea    0xe87(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1236 <get_ram_size+0xd6>
mov    %rax,%r15
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
lea    0xe69(%rip),%rsi
lea    0xc(%rsp),%rcx
mov    %r15,%rdi
mov    %rbx,%rdx
call   1060 <fscanf@plt>
test   %rax,%rax
je     1215 <get_ram_size+0xb5>
lea    0xe53(%rip),%r14
lea    0x10(%rsp),%rbx
lea    0xe41(%rip),%r12
lea    0xc(%rsp),%r13
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x8,%edx
call   1030 <strncmp@plt>
neg    %eax
sbb    %ebp,%ebp
or     0xc(%rsp),%ebp
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
mov    %r13,%rcx
call   1060 <fscanf@plt>
test   %rax,%rax
je     121a <get_ram_size+0xba>
cmp    $0xffffffff,%ebp
je     11e0 <get_ram_size+0x80>
jmp    121a <get_ram_size+0xba>
mov    $0xffffffff,%ebp
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %ebp,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdd3(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebp
jmp    1222 <get_ram_size+0xc2>

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/meminfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [12 x i8] c"fopen fails\00"
@.str.3 = [6 x i8] c"%s %d\00"
@.str.4 = [9 x i8] c"MemTotal\00"
define i32 @get_ram_size() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [256 x i8]
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %4
store i32 -1, i32* %5
%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** %2
%7 = icmp ne i32* %6, null
br i1 %7, label %11, label %8
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%10 = load i32, i32* %5
store i32 %10, i32* %1
br label %35
11: ; preds = %0
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%13 = call i32 @memset(i8* %12, i32 0, i32 256)
br label %14
14: ; preds = %30, %11
%15 = load i32*, i32** %2
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%17 = call i64 @fscanf(i32* %15, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %16, i32* %4)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = load i32, i32* %5
%21 = icmp eq i32 %20, -1
br label %22
22: ; preds = %19, %14
%23 = phi i1 [ false, %14 ], [ %21, %19 ]
br i1 %23, label %24, label %31
24: ; preds = %22
%25 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%26 = call i32 @strncmp(i8* %25, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 8)
%27 = icmp ne i32 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %24
%29 = load i32, i32* %4
store i32 %29, i32* %5
br label %30
30: ; preds = %28, %24
br label %14
31: ; preds = %22
%32 = load i32*, i32** %2
%33 = call i32 @fclose(i32* %32)
%34 = load i32, i32* %5
store i32 %34, i32* %1
br label %35
35: ; preds = %31, %8
%36 = load i32, i32* %1
ret i32 %36
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/meminfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [12 x i8] c"fopen fails\00"
@.str.3 = [6 x i8] c"%s %d\00"
@.str.4 = [9 x i8] c"MemTotal\00"
define i32 @get_ram_size() {
%1 = alloca [256 x i8]
%2 = alloca i32
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%4 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%5 = 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))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %25
9: ; preds = %0
%10 = call i32 @memset(i8* %3, i32 0, i32 256)
%11 = call i64 @fscanf(i32* %5, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %3, i32* %2)
%12 = icmp eq i64 %11, 0
br i1 %12, label %22, label %13
13: ; preds = %9, %13
%14 = call i32 @strncmp(i8* %3, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 8)
%15 = icmp eq i32 %14, 0
%16 = load i32, i32* %2
%17 = select i1 %15, i32 %16, i32 -1
%18 = call i64 @fscanf(i32* %5, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %3, i32* %2)
%19 = icmp ne i64 %18, 0
%20 = icmp eq i32 %17, -1
%21 = select i1 %19, i1 %20, i1 false
br i1 %21, label %13, label %22
22: ; preds = %13, %9
%23 = phi i32 [ -1, %9 ], [ %17, %13 ]
%24 = call i32 @fclose(i32* %5)
br label %25
25: ; preds = %22, %7
%26 = phi i32 [ %23, %22 ], [ -1, %7 ]
ret i32 %26
}