#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 */
double floor(double);
double pow(int, unsigned int);
double rq_math_round(double x, unsigned int p) {
  double f;
  double fx;
  f = (p > 0) ? pow(10, p) : 1.0;
  x *= f;
  fx = floor(x);
  x = (x - fx >= 0.5) ? fx + 1.0 : fx;
  x /= f;
  return x;
}


x86 assembly (clang, -O0):
<rq_math_round>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x8(%rbp)
mov    %edi,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jbe    1151 <rq_math_round+0x31>
mov    -0xc(%rbp),%esi
mov    $0xa,%edi
call   1030 <pow@plt>
movsd  %xmm0,-0x28(%rbp)
jmp    1163 <rq_math_round+0x43>
movsd  0xea7(%rip),%xmm0
movsd  %xmm0,-0x28(%rbp)
jmp    1163 <rq_math_round+0x43>
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
mulsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
call   1040 <floor@plt>
movsd  %xmm0,-0x20(%rbp)
movsd  -0x8(%rbp),%xmm0
subsd  -0x20(%rbp),%xmm0
movsd  0xe6b(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     11be <rq_math_round+0x9e>
movsd  0xe51(%rip),%xmm0
addsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
jmp    11c8 <rq_math_round+0xa8>
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x18(%rbp),%xmm1
movsd  -0x8(%rbp),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rq_math_round>:
sub    $0x18,%rsp
movsd  %xmm0,0x8(%rsp)
test   %edi,%edi
je     113c <rq_math_round+0x1c>
mov    %edi,%esi
mov    $0xa,%edi
call   1030 <pow@plt>
jmp    1144 <rq_math_round+0x24>
movsd  0xebc(%rip),%xmm0
movsd  %xmm0,0x10(%rsp)
movsd  0x8(%rsp),%xmm1
mulsd  %xmm0,%xmm1
movsd  %xmm1,0x8(%rsp)
movapd %xmm1,%xmm0
call   1040 <floor@plt>
movsd  0x8(%rsp),%xmm3
subsd  %xmm0,%xmm3
movsd  0xe8b(%rip),%xmm2
addsd  %xmm0,%xmm2
movsd  0xe87(%rip),%xmm1
cmplesd %xmm3,%xmm1
andpd  %xmm1,%xmm2
andnpd %xmm0,%xmm1
orpd   %xmm2,%xmm1
divsd  0x10(%rsp),%xmm1
movapd %xmm1,%xmm0
add    $0x18,%rsp
ret

LLVM IR (-O0):
define double @rq_math_round(double %0, i32 %1) {
%3 = alloca double
%4 = alloca i32
%5 = alloca double
%6 = alloca double
store double %0, double* %3
store i32 %1, i32* %4
%7 = load i32, i32* %4
%8 = icmp ugt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i32, i32* %4
%11 = call double @pow(i32 10, i32 %10)
br label %13
12: ; preds = %2
br label %13
13: ; preds = %12, %9
%14 = phi double [ %11, %9 ], [ 1.000000e+00, %12 ]
store double %14, double* %5
%15 = load double, double* %5
%16 = load double, double* %3
%17 = fmul double %16, %15
store double %17, double* %3
%18 = load double, double* %3
%19 = call double @llvm.floor.f64(double %18)
store double %19, double* %6
%20 = load double, double* %3
%21 = load double, double* %6
%22 = fsub double %20, %21
%23 = fcmp oge double %22, 5.000000e-01
br i1 %23, label %24, label %27
24: ; preds = %13
%25 = load double, double* %6
%26 = fadd double %25, 1.000000e+00
br label %29
27: ; preds = %13
%28 = load double, double* %6
br label %29
29: ; preds = %27, %24
%30 = phi double [ %26, %24 ], [ %28, %27 ]
store double %30, double* %3
%31 = load double, double* %5
%32 = load double, double* %3
%33 = fdiv double %32, %31
store double %33, double* %3
%34 = load double, double* %3
ret double %34
}

LLVM IR (-O2):
define double @rq_math_round(double %0, i32 %1) {
%3 = icmp eq i32 %1, 0
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = tail call double @pow(i32 10, i32 %1)
br label %6
6: ; preds = %2, %4
%7 = phi double [ %5, %4 ], [ 1.000000e+00, %2 ]
%8 = fmul double %7, %0
%9 = tail call double @llvm.floor.f64(double %8)
%10 = fsub double %8, %9
%11 = fcmp oge double %10, 5.000000e-01
%12 = fadd double %9, 1.000000e+00
%13 = select i1 %11, double %12, double %9
%14 = fdiv double %13, %7
ret double %14
}