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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *);
char *strstr(char *, char *);
scalar_t__ strtoul(char *, int /*<<< orphan*/ *, int /*<<< orphan*/);
unsigned calc_allocsize(const unsigned leave_free) {
  unsigned memfree = 0, buffers = 0, cached = 0;
  FILE *meminfo = fopen("/proc/meminfo", "r");
  if (meminfo) {
    char line[128];
    while (fgets(line, sizeof(line), meminfo)) {
      if (line == strstr(line, "MemFree:")) {
        memfree = (unsigned)strtoul(line + 9, NULL, 0);
      } else if (line == strstr(line, "Cached:")) {
        cached = (unsigned)strtoul(line + 8, NULL, 0);
      } else if (line == strstr(line, "Buffers:")) {
        buffers = (unsigned)strtoul(line + 9, NULL, 0);
      } else if (line == strstr(line, "SwapTotal:") &&
                 ((unsigned)strtoul(line + 11, NULL, 0) > 0)) {
        printf("Warning: Swap detected!\n");
        printf("This might (or might not, depending on the case) cause\n");
        printf("difference in behavior.\n");
      }
    }
  } else {
    printf("Error getting memory statistics!\n");
    return 0;
  }
  fclose(meminfo);
  if ((leave_free >> 20) > (memfree + buffers + cached) / 1024) {
    return 0;
  } else
    return (memfree + buffers + cached) / 1024 - (leave_free >> 20);
}


x86 assembly (clang, -O0):
<calc_allocsize>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
lea    0xe76(%rip),%rdi
lea    0xe7d(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     134f <calc_allocsize+0x1ef>
jmp    11aa <calc_allocsize+0x4a>
lea    -0xa0(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x80,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     134a <calc_allocsize+0x1ea>
lea    -0xa0(%rbp),%rax
mov    %rax,-0xa8(%rbp)
lea    -0xa0(%rbp),%rdi
lea    0xe2b(%rip),%rsi
call   1080 <strstr@plt>
mov    %rax,%rcx
mov    -0xa8(%rbp),%rax
cmp    %rcx,%rax
jne    121b <calc_allocsize+0xbb>
lea    -0xa0(%rbp),%rdi
add    $0x9,%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1070 <strtoul@plt>
mov    %eax,-0xc(%rbp)
jmp    1345 <calc_allocsize+0x1e5>
lea    -0xa0(%rbp),%rax
mov    %rax,-0xb0(%rbp)
lea    -0xa0(%rbp),%rdi
lea    0xde2(%rip),%rsi
call   1080 <strstr@plt>
mov    %rax,%rcx
mov    -0xb0(%rbp),%rax
cmp    %rcx,%rax
jne    126d <calc_allocsize+0x10d>
lea    -0xa0(%rbp),%rdi
add    $0x8,%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1070 <strtoul@plt>
mov    %eax,-0x14(%rbp)
jmp    1340 <calc_allocsize+0x1e0>
lea    -0xa0(%rbp),%rax
mov    %rax,-0xb8(%rbp)
lea    -0xa0(%rbp),%rdi
lea    0xd98(%rip),%rsi
call   1080 <strstr@plt>
mov    %rax,%rcx
mov    -0xb8(%rbp),%rax
cmp    %rcx,%rax
jne    12bf <calc_allocsize+0x15f>
lea    -0xa0(%rbp),%rdi
add    $0x9,%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1070 <strtoul@plt>
mov    %eax,-0x10(%rbp)
jmp    133b <calc_allocsize+0x1db>
lea    -0xa0(%rbp),%rax
mov    %rax,-0xc0(%rbp)
lea    -0xa0(%rbp),%rdi
lea    0xd4f(%rip),%rsi
call   1080 <strstr@plt>
mov    %rax,%rcx
mov    -0xc0(%rbp),%rax
cmp    %rcx,%rax
jne    1336 <calc_allocsize+0x1d6>
lea    -0xa0(%rbp),%rdi
add    $0xb,%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1070 <strtoul@plt>
cmp    $0x0,%eax
jbe    1336 <calc_allocsize+0x1d6>
lea    0xd1c(%rip),%rdi
call   1040 <printf@plt>
lea    0xd29(%rip),%rdi
call   1040 <printf@plt>
lea    0xd55(%rip),%rdi
call   1040 <printf@plt>
jmp    133b <calc_allocsize+0x1db>
jmp    1340 <calc_allocsize+0x1e0>
jmp    1345 <calc_allocsize+0x1e5>
jmp    11aa <calc_allocsize+0x4a>
jmp    1367 <calc_allocsize+0x207>
lea    0xd49(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    13ad <calc_allocsize+0x24d>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x8(%rbp),%eax
shr    $0x14,%eax
mov    -0xc(%rbp),%ecx
add    -0x10(%rbp),%ecx
add    -0x14(%rbp),%ecx
shr    $0xa,%ecx
cmp    %ecx,%eax
jbe    1396 <calc_allocsize+0x236>
movl   $0x0,-0x4(%rbp)
jmp    13ad <calc_allocsize+0x24d>
mov    -0xc(%rbp),%eax
add    -0x10(%rbp),%eax
add    -0x14(%rbp),%eax
shr    $0xa,%eax
mov    -0x8(%rbp),%ecx
shr    $0x14,%ecx
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calc_allocsize>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
mov    %edi,%ebp
lea    0xe86(%rip),%rdi
lea    0xe8d(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     12f5 <calc_allocsize+0x195>
mov    %rax,%rbx
mov    %ebp,0xc(%rsp)
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x80,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
mov    $0x0,%r15d
mov    $0x0,%ecx
mov    %rcx,(%rsp)
mov    $0x0,%r13d
test   %rax,%rax
je     12cd <calc_allocsize+0x16d>
xor    %r13d,%r13d
lea    0xe3d(%rip),%r14
lea    0xe3f(%rip),%r12
xor    %eax,%eax
mov    %rax,(%rsp)
xor    %r15d,%r15d
jmp    121a <calc_allocsize+0xba>
cs nopw 0x0(%rax,%rax,1)
nop
lea    0x19(%rsp),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <strtoul@plt>
mov    %rax,%r15
mov    %rbp,%rdi
mov    $0x80,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     12cd <calc_allocsize+0x16d>
mov    %rbp,%rdi
mov    %r14,%rsi
call   1080 <strstr@plt>
cmp    %rax,%rbp
je     11f0 <calc_allocsize+0x90>
mov    %rbp,%rdi
mov    %r12,%rsi
call   1080 <strstr@plt>
cmp    %rax,%rbp
je     12a0 <calc_allocsize+0x140>
mov    %rbp,%rdi
lea    0xddd(%rip),%rsi
call   1080 <strstr@plt>
cmp    %rax,%rbp
je     12b6 <calc_allocsize+0x156>
mov    %rbp,%rdi
lea    0xdd2(%rip),%rsi
call   1080 <strstr@plt>
cmp    %rax,%rbp
jne    1201 <calc_allocsize+0xa1>
lea    0x1b(%rsp),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <strtoul@plt>
test   %eax,%eax
je     1201 <calc_allocsize+0xa1>
lea    0xddb(%rip),%rdi
call   1030 <puts@plt>
lea    0xde7(%rip),%rdi
call   1030 <puts@plt>
lea    0xe12(%rip),%rdi
call   1030 <puts@plt>
jmp    1201 <calc_allocsize+0xa1>
nopl   (%rax)
lea    0x18(%rsp),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <strtoul@plt>
mov    %rax,%r13
jmp    1201 <calc_allocsize+0xa1>
lea    0x19(%rsp),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <strtoul@plt>
mov    %rax,(%rsp)
jmp    1201 <calc_allocsize+0xa1>
add    (%rsp),%r15d
add    %r13d,%r15d
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0xc(%rsp),%eax
shr    $0x14,%eax
shr    $0xa,%r15d
sub    %eax,%r15d
mov    $0x0,%eax
cmovb  %eax,%r15d
jmp    1304 <calc_allocsize+0x1a4>
lea    0xd39(%rip),%rdi
call   1030 <puts@plt>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x98,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/meminfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [9 x i8] c"MemFree:\00"
@.str.3 = [8 x i8] c"Cached:\00"
@.str.4 = [9 x i8] c"Buffers:\00"
@.str.5 = [11 x i8] c"SwapTotal:\00"
@.str.6 = [25 x i8] c"Warning: Swap detected!\0A\00"
@.str.7 = [56 x i8] c"This might (or might not, depending on the case) cause\0A\00"
@.str.8 = [25 x i8] c"difference in behavior.\0A\00"
@.str.9 = [34 x i8] c"Error getting memory statistics!\0A\00"
define i32 @calc_allocsize(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca [128 x i8]
store i32 %0, i32* %3
store i32 0, i32* %4
store i32 0, i32* %5
store i32 0, i32* %6
%9 = 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* %9, i32** %7
%10 = load i32*, i32** %7
%11 = icmp ne i32* %10, null
br i1 %11, label %12, label %68
12: ; preds = %1
br label %13
13: ; preds = %66, %12
%14 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%15 = load i32*, i32** %7
%16 = call i64 @fgets(i8* %14, i32 128, i32* %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %67
18: ; preds = %13
%19 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%20 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%21 = call i8* @strstr(i8* %20, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%22 = icmp eq i8* %19, %21
br i1 %22, label %23, label %28
23: ; preds = %18
%24 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%25 = getelementptr inbounds i8, i8* %24, i64 9
%26 = call i64 @strtoul(i8* %25, i32* null, i32 0)
%27 = trunc i64 %26 to i32
store i32 %27, i32* %4
br label %66
28: ; preds = %18
%29 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%30 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%31 = call i8* @strstr(i8* %30, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%32 = icmp eq i8* %29, %31
br i1 %32, label %33, label %38
33: ; preds = %28
%34 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%35 = getelementptr inbounds i8, i8* %34, i64 8
%36 = call i64 @strtoul(i8* %35, i32* null, i32 0)
%37 = trunc i64 %36 to i32
store i32 %37, i32* %6
br label %65
38: ; preds = %28
%39 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%40 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%41 = call i8* @strstr(i8* %40, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%42 = icmp eq i8* %39, %41
br i1 %42, label %43, label %48
43: ; preds = %38
%44 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%45 = getelementptr inbounds i8, i8* %44, i64 9
%46 = call i64 @strtoul(i8* %45, i32* null, i32 0)
%47 = trunc i64 %46 to i32
store i32 %47, i32* %5
br label %64
48: ; preds = %38
%49 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%50 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%51 = call i8* @strstr(i8* %50, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
%52 = icmp eq i8* %49, %51
br i1 %52, label %53, label %63
53: ; preds = %48
%54 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%55 = getelementptr inbounds i8, i8* %54, i64 11
%56 = call i64 @strtoul(i8* %55, i32* null, i32 0)
%57 = trunc i64 %56 to i32
%58 = icmp ugt i32 %57, 0
br i1 %58, label %59, label %63
59: ; preds = %53
%60 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0))
%61 = call i32 @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.7, i64 0, i64 0))
%62 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0))
br label %63
63: ; preds = %59, %53, %48
br label %64
64: ; preds = %63, %43
br label %65
65: ; preds = %64, %33
br label %66
66: ; preds = %65, %23
br label %13
67: ; preds = %13
br label %70
68: ; preds = %1
%69 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.9, i64 0, i64 0))
store i32 0, i32* %2
br label %93
70: ; preds = %67
%71 = load i32*, i32** %7
%72 = call i32 @fclose(i32* %71)
%73 = load i32, i32* %3
%74 = lshr i32 %73, 20
%75 = load i32, i32* %4
%76 = load i32, i32* %5
%77 = add i32 %75, %76
%78 = load i32, i32* %6
%79 = add i32 %77, %78
%80 = udiv i32 %79, 1024
%81 = icmp ugt i32 %74, %80
br i1 %81, label %82, label %83
82: ; preds = %70
store i32 0, i32* %2
br label %93
83: ; preds = %70
%84 = load i32, i32* %4
%85 = load i32, i32* %5
%86 = add i32 %84, %85
%87 = load i32, i32* %6
%88 = add i32 %86, %87
%89 = udiv i32 %88, 1024
%90 = load i32, i32* %3
%91 = lshr i32 %90, 20
%92 = sub i32 %89, %91
store i32 %92, i32* %2
br label %93
93: ; preds = %83, %82, %68
%94 = load i32, i32* %2
ret i32 %94
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/meminfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [9 x i8] c"MemFree:\00"
@.str.3 = [8 x i8] c"Cached:\00"
@.str.4 = [9 x i8] c"Buffers:\00"
@.str.5 = [11 x i8] c"SwapTotal:\00"
@str = [33 x i8] c"Error getting memory statistics!\00"
@str.10 = [24 x i8] c"Warning: Swap detected!\00"
@str.11 = [55 x i8] c"This might (or might not, depending on the case) cause\00"
@str.12 = [24 x i8] c"difference in behavior.\00"
define i32 @calc_allocsize(i32 %0) {
%2 = alloca [128 x i8]
%3 = 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))
%4 = icmp eq i32* %3, null
br i1 %4, label %61, label %5
5: ; preds = %1
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%7 = call i64 @fgets(i8* %6, i32 128, i32* %3)
%8 = icmp eq i64 %7, 0
br i1 %8, label %51, label %9
9: ; preds = %5
%10 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 11
%11 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 9
%12 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 8
br label %13
13: ; preds = %9, %45
%14 = phi i32 [ 0, %9 ], [ %48, %45 ]
%15 = phi i32 [ 0, %9 ], [ %47, %45 ]
%16 = phi i32 [ 0, %9 ], [ %46, %45 ]
%17 = call i8* @strstr(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%18 = icmp eq i8* %6, %17
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = call i64 @strtoul(i8* nocapture %11, i32* null, i32 0)
%21 = trunc i64 %20 to i32
br label %45
22: ; preds = %13
%23 = call i8* @strstr(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%24 = icmp eq i8* %6, %23
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = call i64 @strtoul(i8* nocapture %12, i32* null, i32 0)
%27 = trunc i64 %26 to i32
br label %45
28: ; preds = %22
%29 = call i8* @strstr(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%30 = icmp eq i8* %6, %29
br i1 %30, label %31, label %34
31: ; preds = %28
%32 = call i64 @strtoul(i8* nocapture %11, i32* null, i32 0)
%33 = trunc i64 %32 to i32
br label %45
34: ; preds = %28
%35 = call i8* @strstr(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
%36 = icmp eq i8* %6, %35
br i1 %36, label %37, label %45
37: ; preds = %34
%38 = call i64 @strtoul(i8* nocapture %10, i32* null, i32 0)
%39 = trunc i64 %38 to i32
%40 = icmp eq i32 %39, 0
br i1 %40, label %45, label %41
41: ; preds = %37
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.10, i64 0, i64 0))
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @str.11, i64 0, i64 0))
%44 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.12, i64 0, i64 0))
br label %45
45: ; preds = %25, %34, %37, %41, %31, %19
%46 = phi i32 [ %21, %19 ], [ %16, %25 ], [ %16, %31 ], [ %16, %41 ], [ %16, %37 ], [ %16, %34 ]
%47 = phi i32 [ %15, %19 ], [ %15, %25 ], [ %33, %31 ], [ %15, %41 ], [ %15, %37 ], [ %15, %34 ]
%48 = phi i32 [ %14, %19 ], [ %27, %25 ], [ %14, %31 ], [ %14, %41 ], [ %14, %37 ], [ %14, %34 ]
%49 = call i64 @fgets(i8* %6, i32 128, i32* %3)
%50 = icmp eq i64 %49, 0
br i1 %50, label %51, label %13
51: ; preds = %45, %5
%52 = phi i32 [ 0, %5 ], [ %46, %45 ]
%53 = phi i32 [ 0, %5 ], [ %47, %45 ]
%54 = phi i32 [ 0, %5 ], [ %48, %45 ]
%55 = call i32 @fclose(i32* %3)
%56 = lshr i32 %0, 20
%57 = add i32 %53, %52
%58 = add i32 %57, %54
%59 = lshr i32 %58, 10
%60 = call i32 @llvm.usub.sat.i32(i32 %59, i32 %56)
br label %63
61: ; preds = %1
%62 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str, i64 0, i64 0))
br label %63
63: ; preds = %51, %61
%64 = phi i32 [ 0, %61 ], [ %60, %51 ]
ret i32 %64
}