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

/* Variables and functions */
int /*<<< orphan*/ ENOSYS;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int open(char *, int /*<<< orphan*/);
size_t read(int, char *, int);
char *strstr(char *, char *);
long physpages() {
  int fd = open("/proc/meminfo", O_RDONLY);
  char buf[2048];
  size_t l;
  if (fd == -1) {
    errno = ENOSYS;
    return -1;
  }
  l = read(fd, buf, sizeof(buf));
  if (l != (size_t)-1) {
    char *c;
    buf[l] = 0;
    c = strstr(buf, "\nMemTotal:");
    if (c) {
      c += 10;
      while (*c == ' ' || *c == '\t')
        ++c;
      l = 0;
      while (*c >= '0' && *c <= '9') {
        l = l * 10 + *c - '0';
        ++c;
      }
    }
  }
  close(fd);
  return l * 1024;
}


x86 assembly (clang, -O0):
<physpages>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    0x2e96(%rip),%rax
mov    (%rax),%esi
lea    0xea5(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    118c <physpages+0x4c>
mov    0x2e6c(%rip),%rax
mov    (%rax),%ecx
mov    0x2e53(%rip),%rax
mov    %ecx,(%rax)
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1301 <physpages+0x1c1>
mov    -0xc(%rbp),%edi
lea    -0x810(%rbp),%rsi
mov    $0x800,%edx
call   1040 <read@plt>
mov    %rax,-0x818(%rbp)
cmpq   $0xffffffffffffffff,-0x818(%rbp)
ff
je     12ea <physpages+0x1aa>
mov    -0x818(%rbp),%rax
movb   $0x0,-0x810(%rbp,%rax,1)
lea    -0x810(%rbp),%rdi
lea    0xe3c(%rip),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x820(%rbp)
cmpq   $0x0,-0x820(%rbp)
je     12e5 <physpages+0x1a5>
mov    -0x820(%rbp),%rax
add    $0xa,%rax
mov    %rax,-0x820(%rbp)
mov    -0x820(%rbp),%rax
movsbl (%rax),%ecx
mov    $0x1,%al
cmp    $0x20,%ecx
mov    %al,-0x821(%rbp)
je     122f <physpages+0xef>
mov    -0x820(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x9,%eax
sete   %al
mov    %al,-0x821(%rbp)
mov    -0x821(%rbp),%al
test   $0x1,%al
jne    1242 <physpages+0x102>
jmp    1259 <physpages+0x119>
mov    -0x820(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x820(%rbp)
jmp    11fe <physpages+0xbe>
movq   $0x0,-0x818(%rbp)
mov    -0x820(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x30,%ecx
mov    %al,-0x822(%rbp)
jl     1295 <physpages+0x155>
mov    -0x820(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x39,%eax
setle  %al
mov    %al,-0x822(%rbp)
mov    -0x822(%rbp),%al
test   $0x1,%al
jne    12a8 <physpages+0x168>
jmp    12e0 <physpages+0x1a0>
imul   $0xa,-0x818(%rbp),%rax
0a
mov    -0x820(%rbp),%rcx
movsbq (%rcx),%rcx
add    %rcx,%rax
sub    $0x30,%rax
mov    %rax,-0x818(%rbp)
mov    -0x820(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x820(%rbp)
jmp    1264 <physpages+0x124>
jmp    12e5 <physpages+0x1a5>
jmp    12ea <physpages+0x1aa>
mov    -0xc(%rbp),%edi
call   1030 <close@plt>
mov    -0x818(%rbp),%rax
shl    $0xa,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<physpages>:
push   %rbp
push   %rbx
sub    $0x808,%rsp
mov    0x2e98(%rip),%rax
mov    (%rax),%esi
lea    0xea7(%rip),%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11e0 <physpages+0xa0>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x800,%edx
call   1040 <read@plt>
cmp    $0xffffffffffffffff,%rax
je     11fb <physpages+0xbb>
mov    %rax,%rbx
movb   $0x0,(%rsp,%rax,1)
lea    0xe82(%rip),%rsi
mov    %rsp,%rdi
call   1060 <strstr@plt>
test   %rax,%rax
je     1202 <physpages+0xc2>
add    $0xb,%rax
jmp    11a4 <physpages+0x64>
nop
add    $0x1,%rax
movzbl -0x1(%rax),%ecx
cmp    $0x9,%cl
je     11a0 <physpages+0x60>
cmp    $0x20,%cl
je     11a0 <physpages+0x60>
lea    -0x30(%rcx),%edx
xor    %ebx,%ebx
cmp    $0x9,%dl
ja     1202 <physpages+0xc2>
nopl   0x0(%rax)
lea    (%rbx,%rbx,4),%rdx
movzbl %cl,%ecx
lea    (%rcx,%rdx,2),%rbx
add    $0xffffffffffffffd0,%rbx
movzbl (%rax),%ecx
lea    -0x30(%rcx),%edx
add    $0x1,%rax
cmp    $0xa,%dl
jb     11c0 <physpages+0x80>
jmp    1202 <physpages+0xc2>
mov    0x2df9(%rip),%rax
mov    (%rax),%eax
mov    0x2de0(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffffffffffff,%rbx
jmp    120d <physpages+0xcd>
mov    $0xffffffffffffffff,%rbx
mov    %ebp,%edi
call   1030 <close@plt>
shl    $0xa,%rbx
mov    %rbx,%rax
add    $0x808,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/meminfo\00"
@O_RDONLY = global i32 0
@ENOSYS = global i32 0
@errno = global i32 0
@.str.1 = [11 x i8] c"\0AMemTotal:\00"
define i64 @physpages() {
%1 = alloca i64
%2 = alloca i32
%3 = alloca [2048 x i8]
%4 = alloca i64
%5 = alloca i8*
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = load i32, i32* @ENOSYS
store i32 %11, i32* @errno
store i64 -1, i64* %1
br label %73
12: ; preds = %0
%13 = load i32, i32* %2
%14 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 0
%15 = call i64 @read(i32 %13, i8* %14, i32 2048)
store i64 %15, i64* %4
%16 = load i64, i64* %4
%17 = icmp ne i64 %16, -1
br i1 %17, label %18, label %68
18: ; preds = %12
%19 = load i64, i64* %4
%20 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 %19
store i8 0, i8* %20
%21 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 0
%22 = call i8* @strstr(i8* %21, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
store i8* %22, i8** %5
%23 = load i8*, i8** %5
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %67
25: ; preds = %18
%26 = load i8*, i8** %5
%27 = getelementptr inbounds i8, i8* %26, i64 10
store i8* %27, i8** %5
br label %28
28: ; preds = %40, %25
%29 = load i8*, i8** %5
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp eq i32 %31, 32
br i1 %32, label %38, label %33
33: ; preds = %28
%34 = load i8*, i8** %5
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp eq i32 %36, 9
br label %38
38: ; preds = %33, %28
%39 = phi i1 [ true, %28 ], [ %37, %33 ]
br i1 %39, label %40, label %43
40: ; preds = %38
%41 = load i8*, i8** %5
%42 = getelementptr inbounds i8, i8* %41, i32 1
store i8* %42, i8** %5
br label %28
43: ; preds = %38
store i64 0, i64* %4
br label %44
44: ; preds = %56, %43
%45 = load i8*, i8** %5
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = icmp sge i32 %47, 48
br i1 %48, label %49, label %54
49: ; preds = %44
%50 = load i8*, i8** %5
%51 = load i8, i8* %50
%52 = sext i8 %51 to i32
%53 = icmp sle i32 %52, 57
br label %54
54: ; preds = %49, %44
%55 = phi i1 [ false, %44 ], [ %53, %49 ]
br i1 %55, label %56, label %66
56: ; preds = %54
%57 = load i64, i64* %4
%58 = mul i64 %57, 10
%59 = load i8*, i8** %5
%60 = load i8, i8* %59
%61 = sext i8 %60 to i64
%62 = add i64 %58, %61
%63 = sub i64 %62, 48
store i64 %63, i64* %4
%64 = load i8*, i8** %5
%65 = getelementptr inbounds i8, i8* %64, i32 1
store i8* %65, i8** %5
br label %44
66: ; preds = %54
br label %67
67: ; preds = %66, %18
br label %68
68: ; preds = %67, %12
%69 = load i32, i32* %2
%70 = call i32 @close(i32 %69)
%71 = load i64, i64* %4
%72 = mul i64 %71, 1024
store i64 %72, i64* %1
br label %73
73: ; preds = %68, %10
%74 = load i64, i64* %1
ret i64 %74
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/meminfo\00"
@O_RDONLY = global i32 0
@ENOSYS = global i32 0
@errno = global i32 0
@.str.1 = [11 x i8] c"\0AMemTotal:\00"
define i64 @physpages() {
%1 = alloca [2048 x i8]
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = getelementptr inbounds [2048 x i8], [2048 x i8]* %1, i64 0, i64 0
%5 = icmp eq i32 %3, -1
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = load i32, i32* @ENOSYS
store i32 %7, i32* @errno
br label %41
8: ; preds = %0
%9 = call i64 @read(i32 %3, i8* %4, i32 2048)
%10 = icmp eq i64 %9, -1
br i1 %10, label %37, label %11
11: ; preds = %8
%12 = getelementptr inbounds [2048 x i8], [2048 x i8]* %1, i64 0, i64 %9
store i8 0, i8* %12
%13 = call i8* @strstr(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i8* %13, null
br i1 %14, label %37, label %15
15: ; preds = %11
%16 = getelementptr inbounds i8, i8* %13, i64 10
br label %17
17: ; preds = %23, %15
%18 = phi i8* [ %16, %15 ], [ %24, %23 ]
%19 = load i8, i8* %18
switch i8 %19, label %20 [
i8 32, label %23
i8 9, label %23
]
20: ; preds = %17
%21 = add i8 %19, -48
%22 = icmp ult i8 %21, 10
br i1 %22, label %25, label %37
23: ; preds = %17, %17
%24 = getelementptr inbounds i8, i8* %18, i64 1
br label %17
25: ; preds = %20, %25
%26 = phi i8 [ %34, %25 ], [ %19, %20 ]
%27 = phi i8* [ %33, %25 ], [ %18, %20 ]
%28 = phi i64 [ %32, %25 ], [ 0, %20 ]
%29 = mul i64 %28, 10
%30 = zext i8 %26 to i64
%31 = add i64 %29, -48
%32 = add i64 %31, %30
%33 = getelementptr inbounds i8, i8* %27, i64 1
%34 = load i8, i8* %33
%35 = add i8 %34, -48
%36 = icmp ult i8 %35, 10
br i1 %36, label %25, label %37
37: ; preds = %25, %20, %11, %8
%38 = phi i64 [ -1, %8 ], [ %9, %11 ], [ 0, %20 ], [ %32, %25 ]
%39 = call i32 @close(i32 %3)
%40 = shl i64 %38, 10
br label %41
41: ; preds = %37, %6
%42 = phi i64 [ -1, %6 ], [ %40, %37 ]
ret i64 %42
}