#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 alhood(double, double, int, int);
double d2_alhood(double, int, int);
double d_alhood(double, double, int, int);
double exp(double);
int fabs(double);
scalar_t__ isnan(double);
double log(double);
int /*<<< orphan*/ printf(char *, double, ...);
double opt_alpha(double ss, int D, int K) {
  double a, log_a, init_a = 100;
  double f, df, d2f;
  int iter = 0;
  log_a = log(init_a);
  do {
    iter++;
    a = exp(log_a);
    if (isnan(a)) {
      init_a = init_a * 10;
      printf("warning : alpha is nan; new init = %5.5f\n", init_a);
      a = init_a;
      log_a = log(a);
    }
    f = alhood(a, ss, D, K);
    df = d_alhood(a, ss, D, K);
    d2f = d2_alhood(a, D, K);
    log_a = log_a - df / (d2f * a + df);
    printf("alpha maximization : %5.5f   %5.5f\n", f, df);
  } while ((fabs(df) > 1e-5) && (iter < 1000));
  return (exp(log_a));
}


x86 assembly (clang, -O0):
<opt_alpha>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movsd  %xmm0,-0x8(%rbp)
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
movsd  0xe65(%rip),%xmm0
movsd  %xmm0,-0x28(%rbp)
movl   $0x0,-0x44(%rbp)
movsd  -0x28(%rbp),%xmm0
call   10a0 <log@plt>
movsd  %xmm0,-0x20(%rbp)
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
movsd  -0x20(%rbp),%xmm0
call   1070 <exp@plt>
movsd  %xmm0,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
call   1090 <isnan@plt>
cmp    $0x0,%rax
je     1220 <opt_alpha+0xa0>
movsd  0xe1e(%rip),%xmm0
mulsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
lea    0xe18(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
call   10a0 <log@plt>
movsd  %xmm0,-0x20(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  -0x8(%rbp),%xmm1
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1050 <alhood@plt>
movsd  %xmm0,-0x30(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  -0x8(%rbp),%xmm1
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1080 <d_alhood@plt>
movsd  %xmm0,-0x38(%rbp)
movsd  -0x18(%rbp),%xmm0
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1030 <d2_alhood@plt>
movsd  %xmm0,-0x40(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  -0x38(%rbp),%xmm1
movsd  -0x40(%rbp),%xmm2
movsd  -0x18(%rbp),%xmm4
movsd  -0x38(%rbp),%xmm3
mulsd  %xmm4,%xmm2
addsd  %xmm3,%xmm2
divsd  %xmm2,%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x30(%rbp),%xmm0
movsd  -0x38(%rbp),%xmm1
lea    0xd9a(%rip),%rdi
mov    $0x2,%al
call   1040 <printf@plt>
movsd  -0x38(%rbp),%xmm0
call   1060 <fabs@plt>
cvtsi2sd %eax,%xmm0
xor    %eax,%eax
movsd  0xd49(%rip),%xmm1
ucomisd %xmm1,%xmm0
mov    %al,-0x45(%rbp)
jbe    12e1 <opt_alpha+0x161>
cmpl   $0x3e8,-0x44(%rbp)
setl   %al
mov    %al,-0x45(%rbp)
mov    -0x45(%rbp),%al
test   $0x1,%al
jne    11b6 <opt_alpha+0x36>
movsd  -0x20(%rbp),%xmm0
call   1070 <exp@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<opt_alpha>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %esi,%r12d
mov    %edi,%ebp
movsd  %xmm0,0x18(%rsp)
xor    %ebx,%ebx
movsd  0xe5f(%rip),%xmm1
movsd  0xe5f(%rip),%xmm0
movsd  %xmm0,0x8(%rsp)
lea    0xe8c(%rip),%r14
lea    0xe5b(%rip),%r15
nopl   (%rax)
movsd  %xmm1,0x10(%rsp)
movaps %xmm1,%xmm0
call   1070 <exp@plt>
movsd  %xmm0,(%rsp)
call   1090 <isnan@plt>
test   %rax,%rax
je     1220 <opt_alpha+0xa0>
movsd  0x8(%rsp),%xmm0
mulsd  0xe25(%rip),%xmm0
movsd  %xmm0,0x8(%rsp)
mov    %r15,%rdi
mov    $0x1,%al
call   1040 <printf@plt>
movsd  0x8(%rsp),%xmm0
call   10a0 <log@plt>
movsd  %xmm0,0x10(%rsp)
movsd  0x8(%rsp),%xmm0
jmp    1225 <opt_alpha+0xa5>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movsd  (%rsp),%xmm0
movsd  %xmm0,(%rsp)
movsd  0x18(%rsp),%xmm1
mov    %ebp,%edi
mov    %r12d,%esi
call   1050 <alhood@plt>
movsd  %xmm0,0x28(%rsp)
movsd  (%rsp),%xmm0
movsd  0x18(%rsp),%xmm1
mov    %ebp,%edi
mov    %r12d,%esi
call   1080 <d_alhood@plt>
movsd  %xmm0,0x20(%rsp)
movsd  (%rsp),%xmm0
mov    %ebp,%edi
mov    %r12d,%esi
call   1030 <d2_alhood@plt>
mulsd  (%rsp),%xmm0
movsd  0x20(%rsp),%xmm1
addsd  %xmm1,%xmm0
movapd %xmm1,%xmm2
divsd  %xmm0,%xmm2
movsd  0x10(%rsp),%xmm0
subsd  %xmm2,%xmm0
movsd  %xmm0,0x10(%rsp)
mov    %r14,%rdi
movsd  0x28(%rsp),%xmm0
mov    $0x2,%al
call   1040 <printf@plt>
movsd  0x20(%rsp),%xmm0
call   1060 <fabs@plt>
test   %eax,%eax
movsd  0x10(%rsp),%xmm1
jle    12c7 <opt_alpha+0x147>
lea    0x1(%rbx),%eax
cmp    $0x3e7,%ebx
mov    %eax,%ebx
jb     11c0 <opt_alpha+0x40>
movaps %xmm1,%xmm0
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <exp@plt>

LLVM IR (-O0):
@.str = [42 x i8] c"warning : alpha is nan; new init = %5.5f\0A\00"
@.str.1 = [36 x i8] c"alpha maximization : %5.5f %5.5f\0A\00"
define double @opt_alpha(double %0, i32 %1, i32 %2) {
%4 = alloca double
%5 = alloca i32
%6 = alloca i32
%7 = alloca double
%8 = alloca double
%9 = alloca double
%10 = alloca double
%11 = alloca double
%12 = alloca double
%13 = alloca i32
store double %0, double* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store double 1.000000e+02, double* %9
store i32 0, i32* %13
%14 = load double, double* %9
%15 = call double @log(double %14)
store double %15, double* %8
br label %16
16: ; preds = %66, %3
%17 = load i32, i32* %13
%18 = add nsw i32 %17, 1
store i32 %18, i32* %13
%19 = load double, double* %8
%20 = call double @exp(double %19)
store double %20, double* %7
%21 = load double, double* %7
%22 = call i64 @isnan(double %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %32
24: ; preds = %16
%25 = load double, double* %9
%26 = fmul double %25, 1.000000e+01
store double %26, double* %9
%27 = load double, double* %9
%28 = call i32 (i8*, double, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), double %27)
%29 = load double, double* %9
store double %29, double* %7
%30 = load double, double* %7
%31 = call double @log(double %30)
store double %31, double* %8
br label %32
32: ; preds = %24, %16
%33 = load double, double* %7
%34 = load double, double* %4
%35 = load i32, i32* %5
%36 = load i32, i32* %6
%37 = call double @alhood(double %33, double %34, i32 %35, i32 %36)
store double %37, double* %10
%38 = load double, double* %7
%39 = load double, double* %4
%40 = load i32, i32* %5
%41 = load i32, i32* %6
%42 = call double @d_alhood(double %38, double %39, i32 %40, i32 %41)
store double %42, double* %11
%43 = load double, double* %7
%44 = load i32, i32* %5
%45 = load i32, i32* %6
%46 = call double @d2_alhood(double %43, i32 %44, i32 %45)
store double %46, double* %12
%47 = load double, double* %8
%48 = load double, double* %11
%49 = load double, double* %12
%50 = load double, double* %7
%51 = load double, double* %11
%52 = call double @llvm.fmuladd.f64(double %49, double %50, double %51)
%53 = fdiv double %48, %52
%54 = fsub double %47, %53
store double %54, double* %8
%55 = load double, double* %10
%56 = load double, double* %11
%57 = call i32 (i8*, double, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), double %55, double %56)
br label %58
58: ; preds = %32
%59 = load double, double* %11
%60 = call i32 @fabs(double %59)
%61 = sitofp i32 %60 to double
%62 = fcmp ogt double %61, 1.000000e-05
br i1 %62, label %63, label %66
63: ; preds = %58
%64 = load i32, i32* %13
%65 = icmp slt i32 %64, 1000
br label %66
66: ; preds = %63, %58
%67 = phi i1 [ false, %58 ], [ %65, %63 ]
br i1 %67, label %16, label %68
68: ; preds = %66
%69 = load double, double* %8
%70 = call double @exp(double %69)
ret double %70
}

LLVM IR (-O2):
@.str = [42 x i8] c"warning : alpha is nan; new init = %5.5f\0A\00"
@.str.1 = [36 x i8] c"alpha maximization : %5.5f %5.5f\0A\00"
define double @opt_alpha(double %0, i32 %1, i32 %2) {
br label %4
4: ; preds = %16, %3
%5 = phi double [ 1.000000e+02, %3 ], [ %17, %16 ]
%6 = phi double [ 0x40126BB1BBB55516, %3 ], [ %25, %16 ]
%7 = phi i32 [ 0, %3 ], [ %8, %16 ]
%8 = add nuw nsw i32 %7, 1
%9 = tail call double @exp(double %6)
%10 = tail call i64 @isnan(double %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %16, label %12
12: ; preds = %4
%13 = fmul double %5, 1.000000e+01
%14 = tail call i32 (i8*, double, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), double %13)
%15 = tail call double @log(double %13)
br label %16
16: ; preds = %12, %4
%17 = phi double [ %13, %12 ], [ %5, %4 ]
%18 = phi double [ %15, %12 ], [ %6, %4 ]
%19 = phi double [ %13, %12 ], [ %9, %4 ]
%20 = tail call double @alhood(double %19, double %0, i32 %1, i32 %2)
%21 = tail call double @d_alhood(double %19, double %0, i32 %1, i32 %2)
%22 = tail call double @d2_alhood(double %19, i32 %1, i32 %2)
%23 = tail call double @llvm.fmuladd.f64(double %22, double %19, double %21)
%24 = fdiv double %21, %23
%25 = fsub double %18, %24
%26 = tail call i32 (i8*, double, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), double %20, double %21)
%27 = tail call i32 @fabs(double %21)
%28 = icmp sgt i32 %27, 0
%29 = icmp ult i32 %7, 999
%30 = select i1 %28, i1 %29, i1 false
br i1 %30, label %4, label %31
31: ; preds = %16
%32 = tail call double @exp(double %25)
ret double %32
}