#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 */
int /*<<< orphan*/ error(char *, double);
double mb_error_to_string(int);
int mb_query_total();
int /*<<< orphan*/ mb_register(int /*<<< orphan*/);
int /*<<< orphan*/ mb_terminate();
double pages_to_megabytes(int);
int /*<<< orphan*/ printf(char *, char const *, int, int, double);
int percentage_of_total_pages(double d, const char *arg) {
  int n_pages;
  int total_pages;
  if (d < 0.0 || d > 100.0) {
    error("Percentage %g is out of range (0,100)\n", d);
  }
  mb_register(0);
  total_pages = mb_query_total();
  mb_terminate();
  if (total_pages < 0) {
    error("mb_query_total() said %s\n", mb_error_to_string(total_pages));
    ;
  }
  n_pages = (int)(d * total_pages / 100.0);
  printf("%s of total %d pages is %d pages (%g M)\n", arg, total_pages, n_pages,
         pages_to_megabytes(n_pages));
  return n_pages;
}


x86 assembly (clang, -O0):
<percentage_of_total_pages>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x8(%rbp)
mov    %rdi,-0x10(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x8(%rbp),%xmm0
ja     11a6 <percentage_of_total_pages+0x36>
movsd  -0x8(%rbp),%xmm0
movsd  0xe64(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    11b7 <percentage_of_total_pages+0x47>
movsd  -0x8(%rbp),%xmm0
lea    0xe56(%rip),%rdi
call   1070 <error@plt>
xor    %edi,%edi
call   1080 <mb_register@plt>
mov    $0x0,%al
call   1040 <mb_query_total@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1060 <mb_terminate@plt>
cmpl   $0x0,-0x18(%rbp)
jge    11ed <percentage_of_total_pages+0x7d>
mov    -0x18(%rbp),%edi
call   1090 <mb_error_to_string@plt>
lea    0xe47(%rip),%rdi
call   1070 <error@plt>
movsd  -0x8(%rbp),%xmm0
cvtsi2sdl -0x18(%rbp),%xmm1
mulsd  %xmm1,%xmm0
movsd  0xdfd(%rip),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <pages_to_megabytes@plt>
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%edx
mov    -0x1c(%rbp),%ecx
lea    0xe0e(%rip),%rdi
call   1030 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<percentage_of_total_pages>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
xorpd  %xmm1,%xmm1
ucomisd %xmm0,%xmm1
movsd  %xmm0,0x8(%rsp)
ja     1195 <percentage_of_total_pages+0x25>
ucomisd 0xe6d(%rip),%xmm0
jbe    11a1 <percentage_of_total_pages+0x31>
lea    0xe6c(%rip),%rdi
call   1070 <error@plt>
xor    %edi,%edi
call   1080 <mb_register@plt>
xor    %eax,%eax
call   1040 <mb_query_total@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1060 <mb_terminate@plt>
test   %ebp,%ebp
jns    11cf <percentage_of_total_pages+0x5f>
mov    %ebp,%edi
call   1090 <mb_error_to_string@plt>
lea    0xe65(%rip),%rdi
call   1070 <error@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %ebp,%xmm0
mulsd  0x8(%rsp),%xmm0
divsd  0xe1c(%rip),%xmm0
cvttsd2si %xmm0,%ebx
mov    %ebx,%edi
call   1050 <pages_to_megabytes@plt>
lea    0xe53(%rip),%rdi
mov    %r14,%rsi
mov    %ebp,%edx
mov    %ebx,%ecx
call   1030 <printf@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [39 x i8] c"Percentage %g is out of range (0,100)\0A\00"
@.str.1 = [26 x i8] c"mb_query_total() said %s\0A\00"
@.str.2 = [41 x i8] c"%s of total %d pages is %d pages (%g M)\0A\00"
define i32 @percentage_of_total_pages(double %0, i8* %1) {
%3 = alloca double
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store double %0, double* %3
store i8* %1, i8** %4
%7 = load double, double* %3
%8 = fcmp olt double %7, 0.000000e+00
br i1 %8, label %12, label %9
9: ; preds = %2
%10 = load double, double* %3
%11 = fcmp ogt double %10, 1.000000e+02
br i1 %11, label %12, label %15
12: ; preds = %9, %2
%13 = load double, double* %3
%14 = call i32 @error(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), double %13)
br label %15
15: ; preds = %12, %9
%16 = call i32 @mb_register(i32 0)
%17 = call i32 (...) @mb_query_total()
store i32 %17, i32* %6
%18 = call i32 (...) @mb_terminate()
%19 = load i32, i32* %6
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %15
%22 = load i32, i32* %6
%23 = call double @mb_error_to_string(i32 %22)
%24 = call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), double %23)
br label %25
25: ; preds = %21, %15
%26 = load double, double* %3
%27 = load i32, i32* %6
%28 = sitofp i32 %27 to double
%29 = fmul double %26, %28
%30 = fdiv double %29, 1.000000e+02
%31 = fptosi double %30 to i32
store i32 %31, i32* %5
%32 = load i8*, i8** %4
%33 = load i32, i32* %6
%34 = load i32, i32* %5
%35 = load i32, i32* %5
%36 = call double @pages_to_megabytes(i32 %35)
%37 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i8* %32, i32 %33, i32 %34, double %36)
%38 = load i32, i32* %5
ret i32 %38
}

LLVM IR (-O2):
@.str = [39 x i8] c"Percentage %g is out of range (0,100)\0A\00"
@.str.1 = [26 x i8] c"mb_query_total() said %s\0A\00"
@.str.2 = [41 x i8] c"%s of total %d pages is %d pages (%g M)\0A\00"
define i32 @percentage_of_total_pages(double %0, i8* %1) {
%3 = fcmp olt double %0, 0.000000e+00
%4 = fcmp ogt double %0, 1.000000e+02
%5 = or i1 %3, %4
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @error(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), double %0)
br label %8
8: ; preds = %2, %6
%9 = tail call i32 @mb_register(i32 0)
%10 = tail call i32 (...) @mb_query_total()
%11 = tail call i32 (...) @mb_terminate()
%12 = icmp slt i32 %10, 0
br i1 %12, label %13, label %16
13: ; preds = %8
%14 = tail call double @mb_error_to_string(i32 %10)
%15 = tail call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), double %14)
br label %16
16: ; preds = %13, %8
%17 = sitofp i32 %10 to double
%18 = fmul double %17, %0
%19 = fdiv double %18, 1.000000e+02
%20 = fptosi double %19 to i32
%21 = tail call double @pages_to_megabytes(i32 %20)
%22 = tail call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i8* %1, i32 %10, i32 %20, double %21)
ret i32 %20
}