#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 fabs(double);
double sqrt(double);
double find_scale(double *r, double b, double rhoc, double initial_scale, int n,
                  int p) {
  double loss_rho(double *r, double scale, int n, int p, double rhoc);
  int max_it = 200, it = 0;
  double e = 1, scale;
  while ((++it < max_it) && (fabs(e) > 1e-20)) {
    scale = initial_scale * sqrt(loss_rho(r, initial_scale, n, p, rhoc) / b);
    e = fabs(scale / initial_scale - 1);
    initial_scale = scale;
  };
  return (scale);
}


x86 assembly (clang, -O0):
<find_scale>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
movsd  %xmm2,-0x20(%rbp)
mov    %esi,-0x24(%rbp)
mov    %edx,-0x28(%rbp)
movl   $0xc8,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
movsd  0xe99(%rip),%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x30(%rbp),%ecx
add    $0x1,%ecx
mov    %ecx,-0x30(%rbp)
xor    %eax,%eax
cmp    -0x2c(%rbp),%ecx
mov    %al,-0x41(%rbp)
jge    11a3 <find_scale+0x73>
movsd  -0x38(%rbp),%xmm0
call   1030 <fabs@plt>
cvtsi2sd %eax,%xmm0
movsd  0xe6f(%rip),%xmm1
ucomisd %xmm1,%xmm0
seta   %al
mov    %al,-0x41(%rbp)
mov    -0x41(%rbp),%al
test   $0x1,%al
jne    11b3 <find_scale+0x83>
jmp    1224 <find_scale+0xf4>
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x50(%rbp)
mov    -0x8(%rbp),%rdi
movsd  -0x20(%rbp),%xmm0
mov    -0x24(%rbp),%esi
mov    -0x28(%rbp),%edx
movsd  -0x18(%rbp),%xmm1
call   1040 <loss_rho@plt>
divsd  -0x10(%rbp),%xmm0
call   1050 <sqrt@plt>
movaps %xmm0,%xmm1
movsd  -0x50(%rbp),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x40(%rbp),%xmm0
divsd  -0x20(%rbp),%xmm0
movsd  0xdfd(%rip),%xmm1
subsd  %xmm1,%xmm0
call   1030 <fabs@plt>
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
jmp    116c <find_scale+0x3c>
movsd  -0x40(%rbp),%xmm0
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_scale>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %edx,%r14d
mov    %esi,%r15d
movsd  %xmm2,0x8(%rsp)
movsd  %xmm1,0x20(%rsp)
movsd  %xmm0,0x18(%rsp)
mov    %rdi,%rbx
movsd  0xea3(%rip),%xmm0
call   1030 <fabs@plt>
test   %eax,%eax
jle    11f7 <find_scale+0xc7>
mov    $0xc7,%ebp
movsd  0x8(%rsp),%xmm0
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
movsd  %xmm0,0x8(%rsp)
mov    %r15d,%esi
mov    %r14d,%edx
movsd  0x20(%rsp),%xmm1
call   1040 <loss_rho@plt>
divsd  0x18(%rsp),%xmm0
ucomisd 0xe68(%rip),%xmm0
jb     11b0 <find_scale+0x80>
sqrtsd %xmm0,%xmm0
jmp    11b5 <find_scale+0x85>
call   1050 <sqrt@plt>
movsd  0x8(%rsp),%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,0x10(%rsp)
divsd  %xmm1,%xmm0
addsd  0xe37(%rip),%xmm0
call   1030 <fabs@plt>
add    $0xffffffff,%ebp
je     11f9 <find_scale+0xc9>
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
call   1030 <fabs@plt>
movsd  0x10(%rsp),%xmm1
movapd %xmm1,%xmm0
test   %eax,%eax
jg     1180 <find_scale+0x50>
jmp    11ff <find_scale+0xcf>
jmp    11ff <find_scale+0xcf>
movsd  0x10(%rsp),%xmm1
movapd %xmm1,%xmm0
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define double @find_scale(double* %0, double %1, double %2, double %3, i32 %4, i32 %5) {
%7 = alloca double*
%8 = alloca double
%9 = alloca double
%10 = alloca double
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca double
%16 = alloca double
store double* %0, double** %7
store double %1, double* %8
store double %2, double* %9
store double %3, double* %10
store i32 %4, i32* %11
store i32 %5, i32* %12
store i32 200, i32* %13
store i32 0, i32* %14
store double 1.000000e+00, double* %15
br label %17
17: ; preds = %29, %6
%18 = load i32, i32* %14
%19 = add nsw i32 %18, 1
store i32 %19, i32* %14
%20 = load i32, i32* %13
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %27
22: ; preds = %17
%23 = load double, double* %15
%24 = call i32 @fabs(double %23)
%25 = sitofp i32 %24 to double
%26 = fcmp ogt double %25, 0x3BC79CA10C924223
br label %27
27: ; preds = %22, %17
%28 = phi i1 [ false, %17 ], [ %26, %22 ]
br i1 %28, label %29, label %48
29: ; preds = %27
%30 = load double, double* %10
%31 = load double*, double** %7
%32 = load double, double* %10
%33 = load i32, i32* %11
%34 = load i32, i32* %12
%35 = load double, double* %9
%36 = call double @loss_rho(double* %31, double %32, i32 %33, i32 %34, double %35)
%37 = load double, double* %8
%38 = fdiv double %36, %37
%39 = call double @sqrt(double %38)
%40 = fmul double %30, %39
store double %40, double* %16
%41 = load double, double* %16
%42 = load double, double* %10
%43 = fdiv double %41, %42
%44 = fsub double %43, 1.000000e+00
%45 = call i32 @fabs(double %44)
%46 = sitofp i32 %45 to double
store double %46, double* %15
%47 = load double, double* %16
store double %47, double* %10
br label %17
48: ; preds = %27
%49 = load double, double* %16
ret double %49
}

LLVM IR (-O2):
define double @find_scale(double* %0, double %1, double %2, double %3, i32 %4, i32 %5) {
%7 = tail call i32 @fabs(double 1.000000e+00)
%8 = icmp sgt i32 %7, 0
br i1 %8, label %13, label %25
9: ; preds = %13
%10 = sitofp i32 %22 to double
%11 = tail call i32 @fabs(double %10)
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %25
13: ; preds = %6, %9
%14 = phi double [ %19, %9 ], [ %3, %6 ]
%15 = phi i32 [ %23, %9 ], [ 1, %6 ]
%16 = tail call double @loss_rho(double* %0, double %14, i32 %4, i32 %5, double %2)
%17 = fdiv double %16, %1
%18 = tail call double @sqrt(double %17)
%19 = fmul double %14, %18
%20 = fdiv double %19, %14
%21 = fadd double %20, -1.000000e+00
%22 = tail call i32 @fabs(double %21)
%23 = add nuw nsw i32 %15, 1
%24 = icmp eq i32 %23, 200
br i1 %24, label %25, label %9
25: ; preds = %9, %13, %6
%26 = phi double [ undef, %6 ], [ %19, %13 ], [ %19, %9 ]
ret double %26
}