#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 check_pages(double);
int /*<<< orphan*/ error(char *, ...);
double getpagesize();
int percentage_of_total_pages(double, char const *);
int sscanf(char const *, char *, double *, ...);
int parse_n_pages(const char *arg) {
  int n_pages = -1;
  double d;
  char multiplier = '\0';
  if (2 == sscanf(arg, "%lf%c", &d, &multiplier)) {
    switch (multiplier) {
    case 'p':
      n_pages = check_pages(d);
      break;
    case 'g':
    case 'G':
      d *= 1024;
    case 'm':
    case 'M':
      d *= 1024;
    case 'k':
    case 'K':
      d *= 1024;
      n_pages = (int)(d / getpagesize());
      break;
    case '%':
      n_pages = percentage_of_total_pages(d, arg);
      break;
    default:
      error("Unknown multiplier %c\n", multiplier);
    }
  } else if (1 == sscanf(arg, "%lf", &d)) {
    n_pages = check_pages(d);
  } else {
    error("Bad amount '%s'\n", arg);
  }
  return n_pages;
}


x86 assembly (clang, -O0):
<parse_n_pages>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
movb   $0x0,-0x19(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xfc6(%rip),%rsi
lea    -0x18(%rbp),%rdx
lea    -0x19(%rbp),%rcx
mov    $0x0,%al
call   1050 <sscanf@plt>
mov    %eax,%ecx
mov    $0x2,%eax
cmp    %ecx,%eax
jne    1258 <parse_n_pages+0x108>
movsbl -0x19(%rbp),%eax
add    $0xffffffdb,%eax
mov    %eax,%ecx
mov    %rcx,-0x28(%rbp)
sub    $0x4b,%eax
ja     1241 <parse_n_pages+0xf1>
mov    -0x28(%rbp),%rax
lea    0xe57(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movsd  -0x18(%rbp),%xmm0
call   1040 <check_pages@plt>
mov    %eax,-0xc(%rbp)
jmp    1253 <parse_n_pages+0x103>
movsd  0xe2c(%rip),%xmm0
mulsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  0xe1a(%rip),%xmm0
mulsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  0xe08(%rip),%xmm0
mulsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    $0x0,%al
call   1070 <getpagesize@plt>
movaps %xmm0,%xmm1
movsd  -0x30(%rbp),%xmm0
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0xc(%rbp)
jmp    1253 <parse_n_pages+0x103>
movsd  -0x18(%rbp),%xmm0
mov    -0x8(%rbp),%rdi
call   1030 <percentage_of_total_pages@plt>
mov    %eax,-0xc(%rbp)
jmp    1253 <parse_n_pages+0x103>
movsbl -0x19(%rbp),%esi
lea    0xef2(%rip),%rdi
mov    $0x0,%al
call   1060 <error@plt>
jmp    12a6 <parse_n_pages+0x156>
mov    -0x8(%rbp),%rdi
lea    0xef2(%rip),%rsi
lea    -0x18(%rbp),%rdx
mov    $0x0,%al
call   1050 <sscanf@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
jne    128f <parse_n_pages+0x13f>
movsd  -0x18(%rbp),%xmm0
call   1040 <check_pages@plt>
mov    %eax,-0xc(%rbp)
jmp    12a1 <parse_n_pages+0x151>
mov    -0x8(%rbp),%rsi
lea    0xebf(%rip),%rdi
mov    $0x0,%al
call   1060 <error@plt>
jmp    12a6 <parse_n_pages+0x156>
mov    -0xc(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_n_pages>:
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%rbx
movb   $0x0,0xf(%rsp)
lea    0xfd4(%rip),%rsi
lea    0x10(%rsp),%rdx
lea    0xf(%rsp),%rcx
xor    %eax,%eax
call   1050 <sscanf@plt>
cmp    $0x2,%eax
jne    11f4 <parse_n_pages+0xa4>
movsbl 0xf(%rsp),%esi
lea    -0x25(%rsi),%eax
cmp    $0x4b,%eax
ja     1248 <parse_n_pages+0xf8>
lea    0xe76(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movsd  0x10(%rsp),%xmm0
mulsd  0xe57(%rip),%xmm0
movsd  %xmm0,0x10(%rsp)
movsd  0x10(%rsp),%xmm0
mulsd  0xe43(%rip),%xmm0
movsd  %xmm0,0x10(%rsp)
movsd  0x10(%rsp),%xmm0
mulsd  0xe2f(%rip),%xmm0
movsd  %xmm0,0x18(%rsp)
movsd  %xmm0,0x10(%rsp)
xor    %eax,%eax
call   1070 <getpagesize@plt>
movsd  0x18(%rsp),%xmm1
divsd  %xmm0,%xmm1
cvttsd2si %xmm1,%eax
jmp    1232 <parse_n_pages+0xe2>
lea    0xf5a(%rip),%rsi
lea    0x10(%rsp),%rdx
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <sscanf@plt>
cmp    $0x1,%eax
jne    121c <parse_n_pages+0xcc>
movsd  0x10(%rsp),%xmm0
call   1040 <check_pages@plt>
jmp    1232 <parse_n_pages+0xe2>
lea    0xf36(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <error@plt>
mov    $0xffffffff,%eax
add    $0x20,%rsp
pop    %rbx
ret
movsd  0x10(%rsp),%xmm0
mov    %rbx,%rdi
call   1030 <percentage_of_total_pages@plt>
jmp    1232 <parse_n_pages+0xe2>
lea    0xeef(%rip),%rdi
xor    %eax,%eax
call   1060 <error@plt>
jmp    122d <parse_n_pages+0xdd>

LLVM IR (-O0):
@.str = [6 x i8] c"%lf%c\00"
@.str.1 = [23 x i8] c"Unknown multiplier %c\0A\00"
@.str.2 = [4 x i8] c"%lf\00"
@.str.3 = [17 x i8] c"Bad amount '%s'\0A\00"
define i32 @parse_n_pages(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca double
%5 = alloca i8
store i8* %0, i8** %2
store i32 -1, i32* %3
store i8 0, i8* %5
%6 = load i8*, i8** %2
%7 = call i32 (i8*, i8*, double*, ...) @sscanf(i8* %6, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), double* %4, i8* %5)
%8 = icmp eq i32 2, %7
br i1 %8, label %9, label %37
9: ; preds = %1
%10 = load i8, i8* %5
%11 = sext i8 %10 to i32
switch i32 %11, label %32 [
i32 112, label %12
i32 103, label %15
i32 71, label %15
i32 109, label %18
i32 77, label %18
i32 107, label %21
i32 75, label %21
i32 37, label %28
]
12: ; preds = %9
%13 = load double, double* %4
%14 = call i32 @check_pages(double %13)
store i32 %14, i32* %3
br label %36
15: ; preds = %9, %9
%16 = load double, double* %4
%17 = fmul double %16, 1.024000e+03
store double %17, double* %4
br label %18
18: ; preds = %9, %9, %15
%19 = load double, double* %4
%20 = fmul double %19, 1.024000e+03
store double %20, double* %4
br label %21
21: ; preds = %9, %9, %18
%22 = load double, double* %4
%23 = fmul double %22, 1.024000e+03
store double %23, double* %4
%24 = load double, double* %4
%25 = call double (...) @getpagesize()
%26 = fdiv double %24, %25
%27 = fptosi double %26 to i32
store i32 %27, i32* %3
br label %36
28: ; preds = %9
%29 = load double, double* %4
%30 = load i8*, i8** %2
%31 = call i32 @percentage_of_total_pages(double %29, i8* %30)
store i32 %31, i32* %3
br label %36
32: ; preds = %9
%33 = load i8, i8* %5
%34 = sext i8 %33 to i32
%35 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %34)
br label %36
36: ; preds = %32, %28, %21, %12
br label %48
37: ; preds = %1
%38 = load i8*, i8** %2
%39 = call i32 (i8*, i8*, double*, ...) @sscanf(i8* %38, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), double* %4)
%40 = icmp eq i32 1, %39
br i1 %40, label %41, label %44
41: ; preds = %37
%42 = load double, double* %4
%43 = call i32 @check_pages(double %42)
store i32 %43, i32* %3
br label %47
44: ; preds = %37
%45 = load i8*, i8** %2
%46 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %45)
br label %47
47: ; preds = %44, %41
br label %48
48: ; preds = %47, %36
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
@.str = [6 x i8] c"%lf%c\00"
@.str.1 = [23 x i8] c"Unknown multiplier %c\0A\00"
@.str.2 = [4 x i8] c"%lf\00"
@.str.3 = [17 x i8] c"Bad amount '%s'\0A\00"
define i32 @parse_n_pages(i8* %0) {
%2 = alloca double
%3 = alloca i8
%4 = bitcast double* %2 to i8*
store i8 0, i8* %3
%5 = call i32 (i8*, i8*, double*, ...) @sscanf(i8* %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), double* %2, i8* %3)
%6 = icmp eq i32 %5, 2
br i1 %6, label %7, label %30
7: ; preds = %1
%8 = load i8, i8* %3
%9 = sext i8 %8 to i32
switch i32 %9, label %28 [
i32 112, label %10
i32 103, label %13
i32 71, label %13
i32 109, label %16
i32 77, label %16
i32 107, label %19
i32 75, label %19
i32 37, label %25
]
10: ; preds = %7
%11 = load double, double* %2
%12 = call i32 @check_pages(double %11)
br label %38
13: ; preds = %7, %7
%14 = load double, double* %2
%15 = fmul double %14, 1.024000e+03
store double %15, double* %2
br label %16
16: ; preds = %7, %7, %13
%17 = load double, double* %2
%18 = fmul double %17, 1.024000e+03
store double %18, double* %2
br label %19
19: ; preds = %7, %7, %16
%20 = load double, double* %2
%21 = fmul double %20, 1.024000e+03
store double %21, double* %2
%22 = call double (...) @getpagesize()
%23 = fdiv double %21, %22
%24 = fptosi double %23 to i32
br label %38
25: ; preds = %7
%26 = load double, double* %2
%27 = call i32 @percentage_of_total_pages(double %26, i8* %0)
br label %38
28: ; preds = %7
%29 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %9)
br label %38
30: ; preds = %1
%31 = call i32 (i8*, i8*, double*, ...) @sscanf(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), double* %2)
%32 = icmp eq i32 %31, 1
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = load double, double* %2
%35 = call i32 @check_pages(double %34)
br label %38
36: ; preds = %30
%37 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %38
38: ; preds = %33, %36, %10, %19, %25, %28
%39 = phi i32 [ -1, %28 ], [ %27, %25 ], [ %24, %19 ], [ %12, %10 ], [ %35, %33 ], [ -1, %36 ]
ret i32 %39
}