#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 BUFSIZ;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
char *strchr(char *, char);
scalar_t__ strncmp(char *, char *, int);
unsigned long long strtoull(char *, int /*<<< orphan*/ *, int);
long long get_mem_size_iomem(void) {
  unsigned long long mem_size = 0, begin, end;
  char *pos;
  char temp_string[BUFSIZ];
  FILE *info;
  if ((info = fopen("/proc/iomem", "r"))) {
    while (fgets(temp_string, BUFSIZ, info)) {
      pos = strchr(temp_string, ':');
      if (pos && (strncmp(pos + 1, " System RAM", 10) == 0 ||
                  strncmp(pos + 1, " ACPI", 5) == 0)) {
        pos = strchr(temp_string, '-');
        if (pos) {
          *pos = 0;
          end = strtoull(pos + 1, NULL, 16);
          begin = strtoull(temp_string, NULL, 16);
          mem_size += (end - begin);
        }
      }
    }
    fclose(info);
  }
  if (mem_size > 0) {
    if (mem_size % (1024 * 1024)) {
      mem_size = (mem_size / (1024 * 1024)) + 1;
    } else {
      mem_size = (mem_size / (1024 * 1024));
    }
    return mem_size;
  } else
    return 0;
}


x86 assembly (clang, -O0):
<get_mem_size_iomem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movq   $0x0,-0x10(%rbp)
mov    0x2e69(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
lea    0xe5d(%rip),%rdi
lea    0xe62(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
je     12bc <get_mem_size_iomem+0x15c>
jmp    11c2 <get_mem_size_iomem+0x62>
mov    -0x50(%rbp),%rdi
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
mov    -0x40(%rbp),%rdx
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     12b3 <get_mem_size_iomem+0x153>
mov    -0x50(%rbp),%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     12ae <get_mem_size_iomem+0x14e>
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
lea    0xe00(%rip),%rsi
mov    $0xa,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     1245 <get_mem_size_iomem+0xe5>
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
lea    0xde9(%rip),%rsi
mov    $0x5,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    12ae <get_mem_size_iomem+0x14e>
mov    -0x50(%rbp),%rdi
mov    $0x2d,%esi
call   1050 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     12a9 <get_mem_size_iomem+0x149>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1070 <strtoull@plt>
mov    -0x50(%rbp),%rdi
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1070 <strtoull@plt>
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
sub    -0x18(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    12ae <get_mem_size_iomem+0x14e>
jmp    11c2 <get_mem_size_iomem+0x62>
mov    -0x40(%rbp),%rdi
call   1040 <fclose@plt>
cmpq   $0x0,-0x10(%rbp)
jbe    1310 <get_mem_size_iomem+0x1b0>
mov    -0x10(%rbp),%rax
and    $0xfffff,%rax
cmp    $0x0,%rax
je     12f0 <get_mem_size_iomem+0x190>
mov    -0x10(%rbp),%rax
shr    $0x14,%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    12fc <get_mem_size_iomem+0x19c>
mov    -0x10(%rbp),%rax
shr    $0x14,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    131f <get_mem_size_iomem+0x1bf>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_mem_size_iomem>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e6b(%rip),%rbx
mov    (%rbx),%eax
mov    %rsp,%r12
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
lea    0xe71(%rip),%rdi
lea    0xe76(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     12aa <get_mem_size_iomem+0x14a>
mov    %rax,%r15
mov    (%rbx),%esi
mov    %r12,%rdi
mov    %rax,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     12a2 <get_mem_size_iomem+0x142>
xor    %r14d,%r14d
jmp    11e6 <get_mem_size_iomem+0x86>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%rbx),%esi
mov    %r12,%rdi
mov    %r15,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     127f <get_mem_size_iomem+0x11f>
mov    %r12,%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     11d0 <get_mem_size_iomem+0x70>
mov    %rax,%r13
add    $0x1,%r13
mov    %r13,%rdi
lea    0xe05(%rip),%rsi
mov    $0xa,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     1231 <get_mem_size_iomem+0xd1>
mov    %r13,%rdi
lea    0xdf8(%rip),%rsi
mov    $0x5,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    11d0 <get_mem_size_iomem+0x70>
mov    %r12,%rdi
mov    $0x2d,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     11d0 <get_mem_size_iomem+0x70>
movb   $0x0,(%rax)
add    $0x1,%rax
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <strtoull@plt>
mov    %rbx,%r13
mov    %rax,%rbx
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <strtoull@plt>
add    %r14,%rbx
sub    %rax,%rbx
mov    %rbx,%r14
mov    %r13,%rbx
jmp    11d0 <get_mem_size_iomem+0x70>
mov    %r15,%rdi
call   1040 <fclose@plt>
test   %r14,%r14
je     12aa <get_mem_size_iomem+0x14a>
mov    %r14d,%eax
and    $0xfffff,%eax
shr    $0x14,%r14
cmp    $0x1,%rax
sbb    $0xffffffffffffffff,%r14
jmp    12ad <get_mem_size_iomem+0x14d>
mov    %r15,%rdi
call   1040 <fclose@plt>
xor    %r14d,%r14d
mov    %r14,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@BUFSIZ = global i32 0
@.str = [12 x i8] c"/proc/iomem\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [12 x i8] c" System RAM\00"
@.str.3 = [6 x i8] c" ACPI\00"
define i64 @get_mem_size_iomem() {
%1 = alloca i64
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i32
store i64 0, i64* %2
%10 = load i32, i32* @BUFSIZ
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %6
%13 = alloca i8, i64 %11
store i64 %11, i64* %7
%14 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %14, i32** %8
%15 = icmp ne i32* %14, null
br i1 %15, label %16, label %56
16: ; preds = %0
br label %17
17: ; preds = %52, %16
%18 = load i32, i32* @BUFSIZ
%19 = load i32*, i32** %8
%20 = call i64 @fgets(i8* %13, i32 %18, i32* %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %53
22: ; preds = %17
%23 = call i8* @strchr(i8* %13, i8 signext 58)
store i8* %23, i8** %5
%24 = load i8*, i8** %5
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %52
26: ; preds = %22
%27 = load i8*, i8** %5
%28 = getelementptr inbounds i8, i8* %27, i64 1
%29 = call i64 @strncmp(i8* %28, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 10)
%30 = icmp eq i64 %29, 0
br i1 %30, label %36, label %31
31: ; preds = %26
%32 = load i8*, i8** %5
%33 = getelementptr inbounds i8, i8* %32, i64 1
%34 = call i64 @strncmp(i8* %33, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 5)
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %52
36: ; preds = %31, %26
%37 = call i8* @strchr(i8* %13, i8 signext 45)
store i8* %37, i8** %5
%38 = load i8*, i8** %5
%39 = icmp ne i8* %38, null
br i1 %39, label %40, label %51
40: ; preds = %36
%41 = load i8*, i8** %5
store i8 0, i8* %41
%42 = load i8*, i8** %5
%43 = getelementptr inbounds i8, i8* %42, i64 1
%44 = call i64 @strtoull(i8* %43, i32* null, i32 16)
store i64 %44, i64* %4
%45 = call i64 @strtoull(i8* %13, i32* null, i32 16)
store i64 %45, i64* %3
%46 = load i64, i64* %4
%47 = load i64, i64* %3
%48 = sub i64 %46, %47
%49 = load i64, i64* %2
%50 = add i64 %49, %48
store i64 %50, i64* %2
br label %51
51: ; preds = %40, %36
br label %52
52: ; preds = %51, %31, %22
br label %17
53: ; preds = %17
%54 = load i32*, i32** %8
%55 = call i32 @fclose(i32* %54)
br label %56
56: ; preds = %53, %0
%57 = load i64, i64* %2
%58 = icmp ugt i64 %57, 0
br i1 %58, label %59, label %72
59: ; preds = %56
%60 = load i64, i64* %2
%61 = urem i64 %60, 1048576
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %67
63: ; preds = %59
%64 = load i64, i64* %2
%65 = udiv i64 %64, 1048576
%66 = add i64 %65, 1
store i64 %66, i64* %2
br label %70
67: ; preds = %59
%68 = load i64, i64* %2
%69 = udiv i64 %68, 1048576
store i64 %69, i64* %2
br label %70
70: ; preds = %67, %63
%71 = load i64, i64* %2
store i64 %71, i64* %1
store i32 1, i32* %9
br label %73
72: ; preds = %56
store i64 0, i64* %1
store i32 1, i32* %9
br label %73
73: ; preds = %72, %70
%74 = load i8*, i8** %6
%75 = load i64, i64* %1
ret i64 %75
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@.str = [12 x i8] c"/proc/iomem\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [12 x i8] c" System RAM\00"
@.str.3 = [6 x i8] c" ACPI\00"
define i64 @get_mem_size_iomem() {
%1 = load i32, i32* @BUFSIZ
%2 = zext i32 %1 to i64
%3 = alloca i8, i64 %2
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %46, label %6
6: ; preds = %0
%7 = load i32, i32* @BUFSIZ
%8 = call i64 @fgets(i8* %3, i32 %7, i32* %4)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = call i32 @fclose(i32* %4)
br label %46
12: ; preds = %6, %32
%13 = phi i64 [ %33, %32 ], [ 0, %6 ]
%14 = call i8* @strchr(i8* %3, i8 signext 58)
%15 = icmp eq i8* %14, null
br i1 %15, label %32, label %16
16: ; preds = %12
%17 = getelementptr inbounds i8, i8* %14, i64 1
%18 = call i64 @strncmp(i8* %17, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 10)
%19 = icmp eq i64 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %16
%21 = call i64 @strncmp(i8* %17, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 5)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %32
23: ; preds = %20, %16
%24 = call i8* @strchr(i8* %3, i8 signext 45)
%25 = icmp eq i8* %24, null
br i1 %25, label %32, label %26
26: ; preds = %23
store i8 0, i8* %24
%27 = getelementptr inbounds i8, i8* %24, i64 1
%28 = call i64 @strtoull(i8* nocapture %27, i32* null, i32 16)
%29 = call i64 @strtoull(i8* nocapture %3, i32* null, i32 16)
%30 = add i64 %28, %13
%31 = sub i64 %30, %29
br label %32
32: ; preds = %23, %26, %20, %12
%33 = phi i64 [ %31, %26 ], [ %13, %23 ], [ %13, %20 ], [ %13, %12 ]
%34 = load i32, i32* @BUFSIZ
%35 = call i64 @fgets(i8* %3, i32 %34, i32* %4)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %12
37: ; preds = %32
%38 = call i32 @fclose(i32* %4)
%39 = icmp eq i64 %33, 0
br i1 %39, label %46, label %40
40: ; preds = %37
%41 = and i64 %33, 1048575
%42 = icmp ne i64 %41, 0
%43 = lshr i64 %33, 20
%44 = zext i1 %42 to i64
%45 = add nuw nsw i64 %43, %44
br label %46
46: ; preds = %10, %0, %37, %40
%47 = phi i64 [ %45, %40 ], [ 0, %37 ], [ 0, %0 ], [ 0, %10 ]
ret i64 %47
}