// File: /workspace/github_c_makefile_star2_O2/debbiemarkslab[P]plmc/src/lib/lbfgs.c
// Function: line_search_backtracking
static int line_search_backtracking(
    int n,
    lbfgsfloatval_t *x,
    lbfgsfloatval_t *f,
    lbfgsfloatval_t *g,
    lbfgsfloatval_t *s,
    lbfgsfloatval_t *stp,
    const lbfgsfloatval_t* xp,
    const lbfgsfloatval_t* gp,
    lbfgsfloatval_t *wp,
    callback_data_t *cd,
    const lbfgs_parameter_t *param
    )
{
    int count = 0;
    lbfgsfloatval_t width, dg;
    lbfgsfloatval_t finit, dginit = 0., dgtest;
    const lbfgsfloatval_t dec = 0.5, inc = 2.1;

    /* Check the input parameters for errors. */
    if (*stp <= 0.) {
        return LBFGSERR_INVALIDPARAMETERS;
    }

    /* Compute the initial gradient in the search direction. */
    vecdot(&dginit, g, s, n);

    /* Make sure that s points to a descent direction. */
    if (0 < dginit) {
        return LBFGSERR_INCREASEGRADIENT;
    }

    /* The initial value of the objective function. */
    finit = *f;
    dgtest = param->ftol * dginit;

    for (;;) {
        veccpy(x, xp, n);
        vecadd(x, s, *stp, n);

        /* Evaluate the function and gradient values. */
        *f = cd->proc_evaluate(cd->instance, x, g, cd->n, *stp);

        ++count;

        if (*f > finit + *stp * dgtest) {
            width = dec;
        } else {
            /* The sufficient decrease condition (Armijo condition). */
            if (param->linesearch == LBFGS_LINESEARCH_BACKTRACKING_ARMIJO) {
                /* Exit with the Armijo condition. */
                return count;
	        }

	        /* Check the Wolfe condition. */
	        vecdot(&dg, g, s, n);
	        if (dg < param->wolfe * dginit) {
    		    width = inc;
	        } else {
		        if(param->linesearch == LBFGS_LINESEARCH_BACKTRACKING_WOLFE) {
		            /* Exit with the regular Wolfe condition. */
		            return count;
		        }

		        /* Check the strong Wolfe condition. */
		        if(dg > -param->wolfe * dginit) {
		            width = dec;
		        } else {
		            /* Exit with the strong Wolfe condition. */
		            return count;
		        }
            }
        }

        if (*stp < param->min_step) {
            /* The step is the minimum value. */
            return LBFGSERR_MINIMUMSTEP;
        }
        if (*stp > param->max_step) {
            /* The step is the maximum value. */
            return LBFGSERR_MAXIMUMSTEP;
        }
        if (param->max_linesearch <= count) {
            /* Maximum number of iteration. */
            return LBFGSERR_MAXIMUMLINESEARCH;
        }

        (*stp) *= width;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdx, 0x10(%rsp)
andq $0x0, 0x38(%rsp)
movl $0xfffffc1d, %r12d      # imm = 0xFFFFFC1D
xorpd %xmm0, %xmm0
ucomisd (%r9), %xmm0
jae 0x2c5f
movq %r9, %rbx
movq %r8, %r14
movq %rcx, %r15
movq %rsi, %r13
movl %edi, %ebp
leaq 0x38(%rsp), %r12
movq %r12, %rdi
movq %rcx, %rsi
movq %r8, %rdx
movl %ebp, %ecx
callq 0x2dcf
movsd (%r12), %xmm0
movl $0xfffffc1e, %r12d      # imm = 0xFFFFFC1E
ucomisd 0x9591(%rip), %xmm0     # 0xc070
ja 0x2c5f
movq %r15, 0x18(%rsp)
movq 0xa0(%rsp), %rcx
movq 0x10(%rsp), %rax
movsd (%rax), %xmm1
movsd %xmm1, 0x30(%rsp)
movsd 0x40(%rcx), %xmm1
movsd %xmm0, 0x8(%rsp)
mulsd %xmm0, %xmm1
movsd %xmm1, 0x28(%rsp)
movl $0x0, 0x4(%rsp)
pushq $0x1
popq %r12
movq %r13, %rdi
movq 0x80(%rsp), %rsi
movl %ebp, %edx
callq 0x2d92
movsd (%rbx), %xmm0
movq %r13, %rdi
movq %r14, %r15
movq %r14, %rsi
movl %ebp, %edx
callq 0x2dfb
movq 0x98(%rsp), %rax
movq 0x8(%rax), %rdi
movl (%rax), %ecx
movsd (%rbx), %xmm0
movq %r13, %rsi
movq 0x18(%rsp), %rdx
callq *0x10(%rax)
movq 0x10(%rsp), %rax
movsd %xmm0, (%rax)
incl 0x4(%rsp)
movsd (%rbx), %xmm3
movapd %xmm3, %xmm1
mulsd 0x28(%rsp), %xmm1
addsd 0x30(%rsp), %xmm1
ucomisd %xmm1, %xmm0
movsd 0x94be(%rip), %xmm0     # 0xc050
movq 0xa0(%rsp), %rax
ja 0x2c17
movl 0x24(%rax), %r14d
cmpl $0x1, %r14d
je 0x2c5f
leaq 0x40(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %r15, %rdx
movl %ebp, %ecx
movsd %xmm3, 0x20(%rsp)
callq 0x2dcf
movsd 0x20(%rsp), %xmm3
movq 0xa0(%rsp), %rax
movsd 0x40(%rsp), %xmm1
movsd 0x48(%rax), %xmm2
movsd 0x8(%rsp), %xmm0
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
movsd 0x9495(%rip), %xmm0     # 0xc088
ja 0x2c17
cmpl $0x2, %r14d
je 0x2c5a
xorpd 0x949d(%rip), %xmm2     # 0xc0a0
mulsd 0x8(%rsp), %xmm2
ucomisd %xmm2, %xmm1
movsd 0x943b(%rip), %xmm0     # 0xc050
jbe 0x2c5a
movsd 0x30(%rax), %xmm1
ucomisd %xmm3, %xmm1
ja 0x2c42
ucomisd 0x38(%rax), %xmm3
ja 0x2c4a
cmpl %r12d, 0x28(%rax)
jle 0x2c52
mulsd %xmm0, %xmm3
movsd %xmm3, (%rbx)
incl %r12d
movq %r15, %r14
jmp 0x2b22
movl $0xfffffc18, %r12d      # imm = 0xFFFFFC18
jmp 0x2c5f
movl $0xfffffc19, %r12d      # imm = 0xFFFFFC19
jmp 0x2c5f
movl $0xfffffc1a, %r12d      # imm = 0xFFFFFC1A
jmp 0x2c5f
movl 0x4(%rsp), %r12d
movl %r12d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq