#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*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ isspace(char);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int);
int /*<<< orphan*/ strlen(char const *);
char *strstr(char *, char const *);
long strtol(char *, char **, int /*<<< orphan*/);
long read_meminfo(const char *tag) {
  int fd;
  char buf[1024];
  int len, readerr;
  char *p, *q;
  long val;
  fd = open("/proc/meminfo", O_RDONLY);
  if (fd < 0) {
    fprintf(stderr,
            "ERR: "
            "Couldn't open /proc/meminfo (%s)\n",
            strerror(errno));
    return -1;
  }
  len = read(fd, buf, sizeof(buf));
  readerr = errno;
  close(fd);
  if (len < 0) {
    fprintf(stderr,
            "ERR: "
            "Error reading /proc/meminfo (%s)\n",
            strerror(readerr));
    return -1;
  }
  if (len == sizeof(buf)) {
    fprintf(stderr, "ERR: "
                    "/proc/meminfo is too large\n");
    return -1;
  }
  buf[len] = '\0';
  p = strstr(buf, tag);
  if (!p)
    return -1;
  p += strlen(tag);
  val = strtol(p, &q, 0);
  if (!isspace(*q)) {
    fprintf(stderr, "ERR: "
                    "Couldn't parse /proc/meminfo value\n");
    return -1;
  }
  return val;
}


x86 assembly (clang, -O0):
<read_meminfo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
lea    0xe51(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1202 <read_meminfo+0x72>
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x444(%rbp)
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x444(%rbp),%edi
mov    %rax,%rdx
lea    0xe20(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1378 <read_meminfo+0x1e8>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,-0x424(%rbp)
mov    0x2dad(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x428(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
cmpl   $0x0,-0x424(%rbp)
jge    127e <read_meminfo+0xee>
mov    0x2d91(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x448(%rbp)
mov    -0x428(%rbp),%edi
call   10a0 <strerror@plt>
mov    -0x448(%rbp),%edi
mov    %rax,%rdx
lea    0xdcb(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1378 <read_meminfo+0x1e8>
movslq -0x424(%rbp),%rax
cmp    $0x400,%rax
jne    12b5 <read_meminfo+0x125>
mov    0x2d40(%rip),%rax
mov    (%rax),%edi
lea    0xdbb(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1378 <read_meminfo+0x1e8>
movslq -0x424(%rbp),%rax
movb   $0x0,-0x420(%rbp,%rax,1)
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10b0 <strstr@plt>
mov    %rax,-0x430(%rbp)
cmpq   $0x0,-0x430(%rbp)
jne    12f6 <read_meminfo+0x166>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1378 <read_meminfo+0x1e8>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x430(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x430(%rbp)
mov    -0x430(%rbp),%rdi
lea    -0x438(%rbp),%rsi
xor    %edx,%edx
call   1080 <strtol@plt>
mov    %rax,-0x440(%rbp)
mov    -0x438(%rbp),%rax
movsbl (%rax),%edi
call   1050 <isspace@plt>
cmp    $0x0,%eax
jne    136d <read_meminfo+0x1dd>
mov    0x2c88(%rip),%rax
mov    (%rax),%edi
lea    0xd24(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1378 <read_meminfo+0x1e8>
mov    -0x440(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_meminfo>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x418,%rsp
mov    %rdi,%r14
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
lea    0xe50(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     1204 <read_meminfo+0x74>
mov    %eax,%ebx
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,%ebp
mov    0x2dfb(%rip),%rax
mov    (%rax),%r15d
mov    %ebx,%edi
call   1040 <close@plt>
test   %ebp,%ebp
js     1224 <read_meminfo+0x94>
cmp    $0x400,%ebp
jne    1260 <read_meminfo+0xd0>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
lea    0xe61(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1248 <read_meminfo+0xb8>
mov    0x2dcd(%rip),%rax
mov    (%rax),%ebx
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdec(%rip),%rsi
jmp    123c <read_meminfo+0xac>
mov    0x2dad(%rip),%rax
mov    (%rax),%ebx
mov    %r15d,%edi
call   10a0 <strerror@plt>
lea    0xdf9(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffffffffffff,%rbx
mov    %rbx,%rax
add    $0x418,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebp,%eax
movb   $0x0,0x10(%rsp,%rax,1)
lea    0x10(%rsp),%rdi
mov    %r14,%rsi
call   10b0 <strstr@plt>
mov    $0xffffffffffffffff,%rbx
test   %rax,%rax
je     124f <read_meminfo+0xbf>
mov    %rax,%r15
mov    %r14,%rdi
call   1030 <strlen@plt>
cltq
add    %rax,%r15
lea    0x8(%rsp),%rsi
mov    %r15,%rdi
xor    %edx,%edx
call   1080 <strtol@plt>
mov    %rax,%rbp
mov    0x8(%rsp),%rax
movsbl (%rax),%edi
call   1050 <isspace@plt>
test   %eax,%eax
je     12b8 <read_meminfo+0x128>
mov    %rbp,%rbx
jmp    124f <read_meminfo+0xbf>
mov    0x2d19(%rip),%rax
mov    (%rax),%edi
lea    0xdb5(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    124f <read_meminfo+0xbf>

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/meminfo\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [39 x i8] c"ERR: Couldn't open /proc/meminfo (%s)\0A\00"
@errno = global i32 0
@.str.2 = [39 x i8] c"ERR: Error reading /proc/meminfo (%s)\0A\00"
@.str.3 = [33 x i8] c"ERR: /proc/meminfo is too large\0A\00"
@.str.4 = [41 x i8] c"ERR: Couldn't parse /proc/meminfo value\0A\00"
define i64 @read_meminfo(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [1024 x i8]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
store i8* %0, i8** %3
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %1
%16 = load i32, i32* @stderr
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %18)
store i64 -1, i64* %2
br label %68
20: ; preds = %1
%21 = load i32, i32* %4
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%23 = call i32 @read(i32 %21, i8* %22, i32 1024)
store i32 %23, i32* %6
%24 = load i32, i32* @errno
store i32 %24, i32* %7
%25 = load i32, i32* %4
%26 = call i32 @close(i32 %25)
%27 = load i32, i32* %6
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %20
%30 = load i32, i32* @stderr
%31 = load i32, i32* %7
%32 = call i8* @strerror(i32 %31)
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %32)
store i64 -1, i64* %2
br label %68
34: ; preds = %20
%35 = load i32, i32* %6
%36 = sext i32 %35 to i64
%37 = icmp eq i64 %36, 1024
br i1 %37, label %38, label %41
38: ; preds = %34
%39 = load i32, i32* @stderr
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
store i64 -1, i64* %2
br label %68
41: ; preds = %34
%42 = load i32, i32* %6
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 %43
store i8 0, i8* %44
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%46 = load i8*, i8** %3
%47 = call i8* @strstr(i8* %45, i8* %46)
store i8* %47, i8** %8
%48 = load i8*, i8** %8
%49 = icmp ne i8* %48, null
br i1 %49, label %51, label %50
50: ; preds = %41
store i64 -1, i64* %2
br label %68
51: ; preds = %41
%52 = load i8*, i8** %3
%53 = call i32 @strlen(i8* %52)
%54 = load i8*, i8** %8
%55 = sext i32 %53 to i64
%56 = getelementptr inbounds i8, i8* %54, i64 %55
store i8* %56, i8** %8
%57 = load i8*, i8** %8
%58 = call i64 @strtol(i8* %57, i8** %9, i32 0)
store i64 %58, i64* %10
%59 = load i8*, i8** %9
%60 = load i8, i8* %59
%61 = call i32 @isspace(i8 signext %60)
%62 = icmp ne i32 %61, 0
br i1 %62, label %66, label %63
63: ; preds = %51
%64 = load i32, i32* @stderr
%65 = call i32 (i32, i8*, ...) @fprintf(i32 %64, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0))
store i64 -1, i64* %2
br label %68
66: ; preds = %51
%67 = load i64, i64* %10
store i64 %67, i64* %2
br label %68
68: ; preds = %66, %63, %50, %38, %29, %15
%69 = load i64, i64* %2
ret i64 %69
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/meminfo\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [39 x i8] c"ERR: Couldn't open /proc/meminfo (%s)\0A\00"
@errno = global i32 0
@.str.2 = [39 x i8] c"ERR: Error reading /proc/meminfo (%s)\0A\00"
@.str.3 = [33 x i8] c"ERR: /proc/meminfo is too large\0A\00"
@.str.4 = [41 x i8] c"ERR: Couldn't parse /proc/meminfo value\0A\00"
define i64 @read_meminfo(i8* %0) {
%2 = alloca [1024 x i8]
%3 = alloca i8*
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%5 = bitcast i8** %3 to i8*
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %14
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = load i32, i32* @errno
%12 = tail call i8* @strerror(i32 %11)
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %12)
br label %45
14: ; preds = %1
%15 = call i32 @read(i32 %7, i8* %4, i32 1024)
%16 = load i32, i32* @errno
%17 = tail call i32 @close(i32 %7)
%18 = icmp slt i32 %15, 0
br i1 %18, label %19, label %23
19: ; preds = %14
%20 = load i32, i32* @stderr
%21 = tail call i8* @strerror(i32 %16)
%22 = tail call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %21)
br label %45
23: ; preds = %14
%24 = icmp eq i32 %15, 1024
br i1 %24, label %25, label %28
25: ; preds = %23
%26 = load i32, i32* @stderr
%27 = tail call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
br label %45
28: ; preds = %23
%29 = zext i32 %15 to i64
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %29
store i8 0, i8* %30
%31 = call i8* @strstr(i8* %4, i8* dereferenceable(1) %0)
%32 = icmp eq i8* %31, null
br i1 %32, label %45, label %33
33: ; preds = %28
%34 = call i32 @strlen(i8* %0)
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %31, i64 %35
%37 = call i64 @strtol(i8* %36, i8** %3, i32 0)
%38 = load i8*, i8** %3
%39 = load i8, i8* %38
%40 = call i32 @isspace(i8 signext %39)
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %33
%43 = load i32, i32* @stderr
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0))
br label %45
45: ; preds = %33, %28, %42, %25, %19, %9
%46 = phi i64 [ -1, %9 ], [ -1, %19 ], [ -1, %25 ], [ -1, %42 ], [ -1, %28 ], [ %37, %33 ]
ret i64 %46
}