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

/* Variables and functions */
unsigned long get_kernel_mem_total();
long long getpagesize();
double pages_to_gb(int);
int parse_memsize(char const *);
int /*<<< orphan*/ printf(char *, ...);
int calc_all_pages_except(const char *arg) {
  unsigned long kmem_kb;
  int kmem_pages;
  int except_pages;
  except_pages = parse_memsize(arg);
  if (except_pages < 0)
    return -1;
  kmem_kb = get_kernel_mem_total();
  if (kmem_kb == (unsigned long)-1)
    return -1;
  kmem_pages = (int)(((long long)kmem_kb) * 1024 / getpagesize());
  printf("MemTotal: %lu kB -> %d p  -> %.3f G\n", kmem_kb, kmem_pages,
         pages_to_gb(kmem_pages));
  printf("Except pages: %s -> %d p  -> %.3f G\n", arg, except_pages,
         pages_to_gb(except_pages));
  printf("Result: %d p -> %.3f G\n", kmem_pages - except_pages,
         pages_to_gb(kmem_pages - except_pages));
  return kmem_pages - except_pages;
}


x86 assembly (clang, -O0):
<calc_all_pages_except>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <parse_memsize@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    117e <calc_all_pages_except+0x2e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1246 <calc_all_pages_except+0xf6>
mov    $0x0,%al
call   1030 <get_kernel_mem_total@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0xffffffffffffffff,-0x18(%rbp)
jne    11a0 <calc_all_pages_except+0x50>
movl   $0xffffffff,-0x4(%rbp)
jmp    1246 <calc_all_pages_except+0xf6>
mov    -0x18(%rbp),%rax
shl    $0xa,%rax
mov    %rax,-0x48(%rbp)
mov    $0x0,%al
call   1070 <getpagesize@plt>
mov    %rax,%rcx
mov    -0x48(%rbp),%rax
cqto
idiv   %rcx
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x1c(%rbp),%edi
call   1060 <pages_to_gb@plt>
mov    -0x40(%rbp),%rsi
mov    -0x34(%rbp),%edx
lea    0xe1a(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%edi
call   1060 <pages_to_gb@plt>
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%edx
lea    0xe14(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
mov    -0x1c(%rbp),%eax
sub    -0x20(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
sub    -0x20(%rbp),%edi
call   1060 <pages_to_gb@plt>
mov    -0x24(%rbp),%esi
lea    0xe14(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
mov    -0x1c(%rbp),%eax
sub    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calc_all_pages_except>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1040 <parse_memsize@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     11fa <calc_all_pages_except+0xaa>
mov    %eax,%ebp
xor    %eax,%eax
call   1030 <get_kernel_mem_total@plt>
cmp    $0xffffffffffffffff,%rax
je     11fa <calc_all_pages_except+0xaa>
mov    %rax,%r15
mov    %rax,%rbx
shl    $0xa,%rbx
xor    %eax,%eax
call   1070 <getpagesize@plt>
mov    %rax,%rcx
or     %rbx,%rax
shr    $0x20,%rax
je     11a5 <calc_all_pages_except+0x55>
mov    %rbx,%rax
cqto
idiv   %rcx
mov    %rax,%rbx
jmp    11ad <calc_all_pages_except+0x5d>
mov    %ebx,%eax
xor    %edx,%edx
div    %ecx
mov    %eax,%ebx
mov    %ebx,%edi
call   1060 <pages_to_gb@plt>
lea    0xe45(%rip),%rdi
mov    %r15,%rsi
mov    %ebx,%edx
mov    $0x1,%al
call   1050 <printf@plt>
mov    %ebp,%edi
call   1060 <pages_to_gb@plt>
lea    0xe50(%rip),%rdi
mov    %r14,%rsi
mov    %ebp,%edx
mov    $0x1,%al
call   1050 <printf@plt>
sub    %ebp,%ebx
mov    %ebx,%edi
call   1060 <pages_to_gb@plt>
lea    0xe59(%rip),%rdi
mov    %ebx,%esi
mov    $0x1,%al
call   1050 <printf@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [37 x i8] c"MemTotal: %lu kB -> %d p -> %.3f G\0A\00"
@.str.1 = [37 x i8] c"Except pages: %s -> %d p -> %.3f G\0A\00"
@.str.2 = [24 x i8] c"Result: %d p -> %.3f G\0A\00"
define i32 @calc_all_pages_except(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i32 @parse_memsize(i8* %7)
store i32 %8, i32* %6
%9 = load i32, i32* %6
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 -1, i32* %2
br label %44
12: ; preds = %1
%13 = call i64 (...) @get_kernel_mem_total()
store i64 %13, i64* %4
%14 = load i64, i64* %4
%15 = icmp eq i64 %14, -1
br i1 %15, label %16, label %17
16: ; preds = %12
store i32 -1, i32* %2
br label %44
17: ; preds = %12
%18 = load i64, i64* %4
%19 = mul nsw i64 %18, 1024
%20 = call i64 (...) @getpagesize()
%21 = sdiv i64 %19, %20
%22 = trunc i64 %21 to i32
store i32 %22, i32* %5
%23 = load i64, i64* %4
%24 = load i32, i32* %5
%25 = load i32, i32* %5
%26 = call double @pages_to_gb(i32 %25)
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i64 %23, i32 %24, double %26)
%28 = load i8*, i8** %3
%29 = load i32, i32* %6
%30 = load i32, i32* %6
%31 = call double @pages_to_gb(i32 %30)
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %28, i32 %29, double %31)
%33 = load i32, i32* %5
%34 = load i32, i32* %6
%35 = sub nsw i32 %33, %34
%36 = load i32, i32* %5
%37 = load i32, i32* %6
%38 = sub nsw i32 %36, %37
%39 = call double @pages_to_gb(i32 %38)
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %35, double %39)
%41 = load i32, i32* %5
%42 = load i32, i32* %6
%43 = sub nsw i32 %41, %42
store i32 %43, i32* %2
br label %44
44: ; preds = %17, %16, %11
%45 = load i32, i32* %2
ret i32 %45
}

LLVM IR (-O2):
@.str = [37 x i8] c"MemTotal: %lu kB -> %d p -> %.3f G\0A\00"
@.str.1 = [37 x i8] c"Except pages: %s -> %d p -> %.3f G\0A\00"
@.str.2 = [24 x i8] c"Result: %d p -> %.3f G\0A\00"
define i32 @calc_all_pages_except(i8* %0) {
%2 = tail call i32 @parse_memsize(i8* %0)
%3 = icmp slt i32 %2, 0
br i1 %3, label %19, label %4
4: ; preds = %1
%5 = tail call i64 (...) @get_kernel_mem_total()
%6 = icmp eq i64 %5, -1
br i1 %6, label %19, label %7
7: ; preds = %4
%8 = shl nsw i64 %5, 10
%9 = tail call i64 (...) @getpagesize()
%10 = sdiv i64 %8, %9
%11 = trunc i64 %10 to i32
%12 = tail call double @pages_to_gb(i32 %11)
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i64 %5, i32 %11, double %12)
%14 = tail call double @pages_to_gb(i32 %2)
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %2, double %14)
%16 = sub nsw i32 %11, %2
%17 = tail call double @pages_to_gb(i32 %16)
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %16, double %17)
br label %19
19: ; preds = %4, %1, %7
%20 = phi i32 [ %16, %7 ], [ -1, %1 ], [ -1, %4 ]
ret i32 %20
}