#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*/ dmat_waxpby(int const, double, double const *, int,
                               double const *, double *);
int /*<<< orphan*/ dmat_yexpx(int const, double *, double *);
int /*<<< orphan*/ dmat_yinvx(int const, double *, double *);
int /*<<< orphan*/ gradient_hessian_over_v(int const, double *, double *,
                                           double const *, double *, double *);
double logistic_loss(int const, double *);
double logistic_loss2(int const, double *, double *, double *);
void optimize_intercept(double *v, double *z, double *expz, double *expmz,
                        double *z2, const double *b, const double *Aw,
                        const int m) {
  int i, j;
  for (i = 1; i <= 10; i++) {
    double g2, h2, nd2, dv2, s2, phi2;
    dmat_waxpby(m, v[0], b, 1, Aw, z);
    dmat_yexpx(m, z, expz);
    dmat_yinvx(m, expz, expmz);
    gradient_hessian_over_v(m, expz, expmz, b, &g2, &h2);
    nd2 = g2 * g2 / h2 / 2;
    dv2 = -g2 / h2;
    if (nd2 < 1e-12 || i == 10) {
      break;
    }
    s2 = 1.0;
    phi2 = logistic_loss2(m, z, expz, expmz);
    for (j = 1; j < 10; j++) {
      dmat_waxpby(m, s2 * dv2, b, 1, z, z2);
      if (logistic_loss(m, z2) - phi2 <= 0.01 * s2 * dv2 * g2) {
        break;
      }
      s2 *= 0.5;
    }
    v[0] += s2 * dv2;
  }
}


x86 assembly (clang, -O0):
<optimize_intercept>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    0x18(%rbp),%eax
mov    0x10(%rbp),%rax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
movl   $0x1,-0x34(%rbp)
cmpl   $0xa,-0x34(%rbp)
jg     133d <optimize_intercept+0x1dd>
mov    0x18(%rbp),%edi
mov    -0x8(%rbp),%rax
movsd  (%rax),%xmm0
mov    -0x30(%rbp),%rsi
mov    0x10(%rbp),%rcx
mov    -0x10(%rbp),%r8
mov    $0x1,%edx
call   1040 <dmat_waxpby@plt>
mov    0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1060 <dmat_yexpx@plt>
mov    0x18(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1080 <dmat_yinvx@plt>
mov    0x18(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x30(%rbp),%rcx
lea    -0x40(%rbp),%r8
lea    -0x48(%rbp),%r9
call   1050 <gradient_hessian_over_v@plt>
movsd  -0x40(%rbp),%xmm0
mulsd  -0x40(%rbp),%xmm0
divsd  -0x48(%rbp),%xmm0
movsd  0xdfc(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x50(%rbp)
movsd  -0x40(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
divsd  -0x48(%rbp),%xmm0
movsd  %xmm0,-0x58(%rbp)
movsd  0xdbd(%rip),%xmm0
ucomisd -0x50(%rbp),%xmm0
ja     1258 <optimize_intercept+0xf8>
cmpl   $0xa,-0x34(%rbp)
jne    125d <optimize_intercept+0xfd>
jmp    133d <optimize_intercept+0x1dd>
movsd  0xdab(%rip),%xmm0
movsd  %xmm0,-0x60(%rbp)
mov    0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1030 <logistic_loss2@plt>
movsd  %xmm0,-0x68(%rbp)
movl   $0x1,-0x38(%rbp)
cmpl   $0xa,-0x38(%rbp)
jge    1311 <optimize_intercept+0x1b1>
mov    0x18(%rbp),%edi
movsd  -0x60(%rbp),%xmm0
mulsd  -0x58(%rbp),%xmm0
mov    -0x30(%rbp),%rsi
mov    -0x10(%rbp),%rcx
mov    -0x28(%rbp),%r8
mov    $0x1,%edx
call   1040 <dmat_waxpby@plt>
mov    0x18(%rbp),%edi
mov    -0x28(%rbp),%rsi
call   1070 <logistic_loss@plt>
movaps %xmm0,%xmm1
subsd  -0x68(%rbp),%xmm1
movsd  0xd45(%rip),%xmm0
mulsd  -0x60(%rbp),%xmm0
mulsd  -0x58(%rbp),%xmm0
mulsd  -0x40(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jb     12f1 <optimize_intercept+0x191>
jmp    1311 <optimize_intercept+0x1b1>
movsd  0xd27(%rip),%xmm0
mulsd  -0x60(%rbp),%xmm0
movsd  %xmm0,-0x60(%rbp)
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    128a <optimize_intercept+0x12a>
movsd  -0x60(%rbp),%xmm0
movsd  -0x58(%rbp),%xmm2
mov    -0x8(%rbp),%rax
movsd  (%rax),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,(%rax)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    118e <optimize_intercept+0x2e>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<optimize_intercept>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %r9,%r14
mov    %r8,%rbx
mov    %rcx,%r12
mov    %rdx,0x10(%rsp)
mov    %rsi,%r13
mov    0x98(%rsp),%ebp
mov    %rdi,0x30(%rsp)
movsd  (%rdi),%xmm2
mov    $0x1,%r15d
mov    %rcx,0x38(%rsp)
jmp    11c6 <optimize_intercept+0x66>
nopl   0x0(%rax)
mulsd  0x20(%rsp),%xmm2
mov    0x30(%rsp),%rax
addsd  (%rax),%xmm2
movsd  %xmm2,(%rax)
add    $0x1,%r15d
cmp    $0xb,%r15d
mov    0x38(%rsp),%r12
je     1310 <optimize_intercept+0x1b0>
mov    %r15d,0xc(%rsp)
mov    %ebp,%edi
movapd %xmm2,%xmm0
mov    %r14,%rsi
mov    $0x1,%edx
mov    0x90(%rsp),%rcx
mov    %r13,%r8
call   1040 <dmat_waxpby@plt>
mov    %ebp,%edi
mov    %r13,%rsi
mov    0x10(%rsp),%r15
mov    %r15,%rdx
call   1060 <dmat_yexpx@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   1080 <dmat_yinvx@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    0xc(%rsp),%r15d
mov    %r12,%rdx
mov    %r14,%rcx
lea    0x18(%rsp),%r8
lea    0x50(%rsp),%r9
call   1050 <gradient_hessian_over_v@plt>
movsd  0x18(%rsp),%xmm1
movapd %xmm1,%xmm3
mulsd  %xmm1,%xmm1
movsd  0x50(%rsp),%xmm0
divsd  %xmm0,%xmm1
mulsd  0xdb9(%rip),%xmm1
movsd  0xdb9(%rip),%xmm2
ucomisd %xmm1,%xmm2
ja     1310 <optimize_intercept+0x1b0>
cmp    $0xa,%r15d
je     1310 <optimize_intercept+0x1b0>
xorpd  0xdb5(%rip),%xmm3
divsd  %xmm0,%xmm3
movapd %xmm3,0x20(%rsp)
mov    %ebp,%edi
mov    %r13,%rsi
mov    0x10(%rsp),%rdx
mov    %r12,%rcx
call   1030 <logistic_loss2@plt>
movsd  %xmm0,0x40(%rsp)
mov    $0x9,%r12d
movsd  0xd75(%rip),%xmm2
nopl   0x0(%rax,%rax,1)
movsd  %xmm2,0x48(%rsp)
movapd 0x20(%rsp),%xmm0
mulsd  %xmm2,%xmm0
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
mov    %r13,%rcx
mov    %rbx,%r8
call   1040 <dmat_waxpby@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1070 <logistic_loss@plt>
movsd  0x48(%rsp),%xmm2
subsd  0x40(%rsp),%xmm0
movapd %xmm2,%xmm1
mulsd  0xd31(%rip),%xmm1
mulsd  0x20(%rsp),%xmm1
mulsd  0x18(%rsp),%xmm1
ucomisd %xmm0,%xmm1
jae    11a0 <optimize_intercept+0x40>
mulsd  0xcfb(%rip),%xmm2
add    $0xffffffff,%r12d
jne    12a0 <optimize_intercept+0x140>
jmp    11a0 <optimize_intercept+0x40>
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @optimize_intercept(double* %0, double* %1, double* %2, double* %3, double* %4, double* %5, double* %6, i32 %7) {
%9 = alloca double*
%10 = alloca double*
%11 = alloca double*
%12 = alloca double*
%13 = alloca double*
%14 = alloca double*
%15 = alloca double*
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca double
%20 = alloca double
%21 = alloca double
%22 = alloca double
%23 = alloca double
%24 = alloca double
store double* %0, double** %9
store double* %1, double** %10
store double* %2, double** %11
store double* %3, double** %12
store double* %4, double** %13
store double* %5, double** %14
store double* %6, double** %15
store i32 %7, i32* %16
store i32 1, i32* %17
br label %25
25: ; preds = %110, %8
%26 = load i32, i32* %17
%27 = icmp sle i32 %26, 10
br i1 %27, label %28, label %113
28: ; preds = %25
%29 = load i32, i32* %16
%30 = load double*, double** %9
%31 = getelementptr inbounds double, double* %30, i64 0
%32 = load double, double* %31
%33 = load double*, double** %14
%34 = load double*, double** %15
%35 = load double*, double** %10
%36 = call i32 @dmat_waxpby(i32 %29, double %32, double* %33, i32 1, double* %34, double* %35)
%37 = load i32, i32* %16
%38 = load double*, double** %10
%39 = load double*, double** %11
%40 = call i32 @dmat_yexpx(i32 %37, double* %38, double* %39)
%41 = load i32, i32* %16
%42 = load double*, double** %11
%43 = load double*, double** %12
%44 = call i32 @dmat_yinvx(i32 %41, double* %42, double* %43)
%45 = load i32, i32* %16
%46 = load double*, double** %11
%47 = load double*, double** %12
%48 = load double*, double** %14
%49 = call i32 @gradient_hessian_over_v(i32 %45, double* %46, double* %47, double* %48, double* %19, double* %20)
%50 = load double, double* %19
%51 = load double, double* %19
%52 = fmul double %50, %51
%53 = load double, double* %20
%54 = fdiv double %52, %53
%55 = fdiv double %54, 2.000000e+00
store double %55, double* %21
%56 = load double, double* %19
%57 = fneg double %56
%58 = load double, double* %20
%59 = fdiv double %57, %58
store double %59, double* %22
%60 = load double, double* %21
%61 = fcmp olt double %60, 0x3D719799812DEA11
br i1 %61, label %65, label %62
62: ; preds = %28
%63 = load i32, i32* %17
%64 = icmp eq i32 %63, 10
br i1 %64, label %65, label %66
65: ; preds = %62, %28
br label %113
66: ; preds = %62
store double 1.000000e+00, double* %23
%67 = load i32, i32* %16
%68 = load double*, double** %10
%69 = load double*, double** %11
%70 = load double*, double** %12
%71 = call double @logistic_loss2(i32 %67, double* %68, double* %69, double* %70)
store double %71, double* %24
store i32 1, i32* %18
br label %72
72: ; preds = %100, %66
%73 = load i32, i32* %18
%74 = icmp slt i32 %73, 10
br i1 %74, label %75, label %103
75: ; preds = %72
%76 = load i32, i32* %16
%77 = load double, double* %23
%78 = load double, double* %22
%79 = fmul double %77, %78
%80 = load double*, double** %14
%81 = load double*, double** %10
%82 = load double*, double** %13
%83 = call i32 @dmat_waxpby(i32 %76, double %79, double* %80, i32 1, double* %81, double* %82)
%84 = load i32, i32* %16
%85 = load double*, double** %13
%86 = call double @logistic_loss(i32 %84, double* %85)
%87 = load double, double* %24
%88 = fsub double %86, %87
%89 = load double, double* %23
%90 = fmul double 1.000000e-02, %89
%91 = load double, double* %22
%92 = fmul double %90, %91
%93 = load double, double* %19
%94 = fmul double %92, %93
%95 = fcmp ole double %88, %94
br i1 %95, label %96, label %97
96: ; preds = %75
br label %103
97: ; preds = %75
%98 = load double, double* %23
%99 = fmul double %98, 5.000000e-01
store double %99, double* %23
br label %100
100: ; preds = %97
%101 = load i32, i32* %18
%102 = add nsw i32 %101, 1
store i32 %102, i32* %18
br label %72
103: ; preds = %96, %72
%104 = load double, double* %23
%105 = load double, double* %22
%106 = load double*, double** %9
%107 = getelementptr inbounds double, double* %106, i64 0
%108 = load double, double* %107
%109 = call double @llvm.fmuladd.f64(double %104, double %105, double %108)
store double %109, double* %107
br label %110
110: ; preds = %103
%111 = load i32, i32* %17
%112 = add nsw i32 %111, 1
store i32 %112, i32* %17
br label %25
113: ; preds = %65, %25
ret void
}

LLVM IR (-O2):
define void @optimize_intercept(double* nocapture %0, double* %1, double* %2, double* %3, double* %4, double* %5, double* %6, i32 %7) {
%9 = alloca double
%10 = alloca double
%11 = bitcast double* %9 to i8*
%12 = bitcast double* %10 to i8*
%13 = load double, double* %0
br label %14
14: ; preds = %8, %50
%15 = phi double [ %13, %8 ], [ %53, %50 ]
%16 = phi i32 [ 1, %8 ], [ %54, %50 ]
%17 = call i32 @dmat_waxpby(i32 %7, double %15, double* %5, i32 1, double* %6, double* %1)
%18 = call i32 @dmat_yexpx(i32 %7, double* %1, double* %2)
%19 = call i32 @dmat_yinvx(i32 %7, double* %2, double* %3)
%20 = call i32 @gradient_hessian_over_v(i32 %7, double* %2, double* %3, double* %5, double* %9, double* %10)
%21 = load double, double* %9
%22 = fmul double %21, %21
%23 = load double, double* %10
%24 = fdiv double %22, %23
%25 = fmul double %24, 5.000000e-01
%26 = fneg double %21
%27 = fdiv double %26, %23
%28 = fcmp olt double %25, 0x3D719799812DEA11
%29 = icmp eq i32 %16, 10
%30 = select i1 %28, i1 true, i1 %29
br i1 %30, label %49, label %31
31: ; preds = %14
%32 = call double @logistic_loss2(i32 %7, double* %1, double* %2, double* %3)
br label %33
33: ; preds = %31, %45
%34 = phi double [ 1.000000e+00, %31 ], [ %46, %45 ]
%35 = phi i32 [ 1, %31 ], [ %47, %45 ]
%36 = fmul double %27, %34
%37 = call i32 @dmat_waxpby(i32 %7, double %36, double* %5, i32 1, double* %1, double* %4)
%38 = call double @logistic_loss(i32 %7, double* %4)
%39 = fsub double %38, %32
%40 = fmul double %34, 1.000000e-02
%41 = fmul double %27, %40
%42 = load double, double* %9
%43 = fmul double %41, %42
%44 = fcmp ugt double %39, %43
br i1 %44, label %45, label %50
45: ; preds = %33
%46 = fmul double %34, 5.000000e-01
%47 = add nuw nsw i32 %35, 1
%48 = icmp eq i32 %47, 10
br i1 %48, label %50, label %33
49: ; preds = %14
br label %56
50: ; preds = %45, %33
%51 = phi double [ %46, %45 ], [ %34, %33 ]
%52 = load double, double* %0
%53 = call double @llvm.fmuladd.f64(double %51, double %27, double %52)
store double %53, double* %0
%54 = add nuw nsw i32 %16, 1
%55 = icmp eq i32 %54, 11
br i1 %55, label %56, label %14
56: ; preds = %50, %49
ret void
}