// File: /workspace/github_cpp_makefile_star2_O3/tbjohns[P]BlitzL1/src/solver.cpp
// Function: BlitzL1::Solver::solve(BlitzL1::Dataset const*, double, char const*, double*, double&, char*, double&, double&, int&, int, char const*)
void Solver::solve(const Dataset *data,
                     value_t lambda,
                     const char *loss_type,
                     value_t *x,
                     value_t &intercept,
                     char *solution_status,
                     value_t &primal_obj,
                     value_t &duality_gap,
                     int &itr_counter,
                     int max_iter,
                     const char *log_directory)
  {

    // Solves l1-regularized loss minimization problem

    Timer timer;
    Logger logger(log_directory);
    cout.precision(10);

    itr_counter = 0;
    Loss *loss_function = get_loss_function(loss_type);
    size_t n = data->get_n();
    size_t d = data->get_d();
    working_set_size = 0;
    prioritized_features.resize(d);
    for (size_t j = 0; j < d; ++j)
      prioritized_features[j] = j;

    // Initialize dual points:
    loss_function->compute_Ax(Ax, x, intercept, data);
    loss_function->compute_dual_points(Ax, theta, aux_dual, data);

    phi.assign(n, 0.0);
    ATphi.assign(d, 0.0);
    theta_scale = 1.0;

    // Update intercept (if using intercept term):
    update_intercept(intercept, loss_function, data);

    // Initialize objective values:
    value_t primal_loss = loss_function->primal_loss(theta, aux_dual, data);
    value_t l1_penalty = lambda * l1_norm(x, d);
    primal_obj = primal_loss + l1_penalty;

    // Main Blitz loop:
    // while (++itr_counter) {
    for (int iter = 0; iter < max_iter; ++iter)
    {
      ++itr_counter;
      value_t primal_obj_last = primal_obj;

      update_ATtheta(data);

      value_t alpha = compute_alpha(data, lambda, theta_scale);
      update_phi(alpha, theta_scale);

      value_t dual_obj = loss_function->dual_obj(phi, data);
      duality_gap = primal_obj - dual_obj;

      // Determine working set size:
      working_set_size = 2 * l0_norm(x, d);
      if (working_set_size < 100)
        working_set_size = 100;
      if (working_set_size > prioritized_features.size())
        working_set_size = prioritized_features.size();

      prioritize_features(data, lambda, x, working_set_size);

      // Eliminate features:
      value_t thresh = sqrt(2 * duality_gap / loss_function->L);
      IndirectExceedsThreshold exceeds_thresh(feature_priorities, thresh);
      prioritized_features.erase(
          remove_if(prioritized_features.begin(),
                    prioritized_features.end(),
                    exceeds_thresh),
          prioritized_features.end());
      if (working_set_size > prioritized_features.size())
        working_set_size = prioritized_features.size();

      // Solve subproblem:
      value_t epsilon = 0.3;
      reset_prox_newton_variables();
      while (true)
      {
        value_t last_subproblem_obj = primal_obj;
        theta_scale = run_prox_newton_iteration(
            x, intercept, lambda, loss_function, data);

        primal_loss = loss_function->primal_loss(theta, aux_dual, data);
        l1_penalty = lambda * l1_norm(x, d);
        primal_obj = primal_loss + l1_penalty;
        value_t subprob_dual_obj = loss_function->dual_obj(theta, data, theta_scale);

        value_t subprob_duality_gap = primal_obj - subprob_dual_obj;
        if (subprob_duality_gap < epsilon * (primal_obj - dual_obj))
          break;
        if (subprob_duality_gap / fabs(subprob_dual_obj) < tolerance)
          break;
        if (primal_obj >= last_subproblem_obj)
          break;
      }

      primal_loss = loss_function->primal_loss(theta, aux_dual, data);
      l1_penalty = lambda * l1_norm(x, d);
      primal_obj = primal_loss + l1_penalty;
      duality_gap = primal_obj - dual_obj;

      // Print/record results:
      double elapsed_time = timer.elapsed_time();
      timer.pause_timing();
      if (verbose)
        cout << "Iter: " << itr_counter
             << " Time: " << elapsed_time
             << " Objective: " << primal_obj
             << " Duality gap: " << duality_gap
             << " Features left: " << prioritized_features.size() << endl;

      logger.log_point(elapsed_time, primal_obj);
      timer.continue_timing();

      // Test stopping conditions:
      if ((duality_gap / fabs(dual_obj) < tolerance) &&
          (elapsed_time > min_time))
      {
        snprintf(solution_status, strlen(solution_status), "reached stopping tolerance");
        break;
      }

      if (primal_obj >= primal_obj_last)
      {
        snprintf(solution_status, strlen(solution_status), "reached machine precision");
        break;
      }

      if (elapsed_time > max_time)
      {
        snprintf(solution_status, strlen(solution_status), "reached time limit");
        break;
      }
    }
  }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %r9, 0x88(%rsp)
movq %r8, 0x38(%rsp)
movq %rcx, %r15
movq %rdx, %rbp
movsd %xmm0, 0x10(%rsp)
movq %rsi, %r12
movq %rdi, %r13
movq 0x100(%rsp), %rbx
xorl %eax, %eax
movq %rax, 0x60(%rsp)
leaq 0x20(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x5500
xorps %xmm0, %xmm0
cvtsi2sdq (%r14), %xmm0
cvtsi2sdq 0x8(%r14), %xmm1
divsd 0x2064(%rip), %xmm1     # 0xa248
addsd %xmm0, %xmm1
movsd %xmm1, 0x50(%rsp)
movb $0x0, 0x68(%rsp)
movq 0x110(%rsp), %rax
movq %rax, 0xa8(%rsp)
xorl %edx, %edx
movl %edx, 0xb0(%rsp)
movq 0x4db5(%rip), %rax      # 0xcfc8
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movq $0xa, 0x8(%rax,%rcx)
movl %edx, (%rbx)
xorl %r14d, %r14d
movq %rbp, %rdi
callq 0x5090
movq %rax, %rbp
movq 0x18(%r12), %rax
movq %rax, 0x8(%rsp)
movq 0x20(%r12), %rbx
movq %r14, 0x98(%r13)
leaq 0xa0(%r13), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x53f0
movq %rbx, 0x18(%rsp)
testq %rbx, %rbx
je 0x8303
movq (%r14), %rax
movq 0x18(%rsp), %rdx
leaq 0x1(%rdx), %rcx
andq $-0x2, %rcx
decq %rdx
movq %rdx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
movdqa 0x1eb2(%rip), %xmm1     # 0xa140
xorl %edx, %edx
movdqa 0x1eb8(%rip), %xmm2     # 0xa150
pxor %xmm2, %xmm0
pcmpeqd %xmm3, %xmm3
movdqa 0x1fc8(%rip), %xmm4     # 0xa270
movdqa %xmm1, %xmm5
pxor %xmm2, %xmm5
movdqa %xmm5, %xmm6
pcmpgtd %xmm0, %xmm6
pcmpeqd %xmm0, %xmm5
pshufd $0xf5, %xmm5, %xmm7     # xmm7 = xmm5[1,1,3,3]
pand %xmm6, %xmm7
pshufd $0xf5, %xmm6, %xmm5     # xmm5 = xmm6[1,1,3,3]
por %xmm7, %xmm5
movd %xmm5, %esi
notl %esi
testb $0x1, %sil
je 0x82de
movq %rdx, (%rax,%rdx,8)
pxor %xmm3, %xmm5
pextrw $0x4, %xmm5, %esi
testb $0x1, %sil
je 0x82f6
leaq 0x1(%rdx), %rsi
movq %rsi, 0x8(%rax,%rdx,8)
addq $0x2, %rdx
paddq %xmm4, %xmm1
cmpq %rdx, %rcx
jne 0x82a8
leaq 0x18(%r13), %r14
movq 0x38(%rsp), %rbx
movsd (%rbx), %xmm0
movq %rbp, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %r12, %rcx
callq 0x5100
leaq 0x78(%r13), %rcx
movq %rcx, 0x30(%rsp)
movq (%rbp), %rax
movq %rbp, %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %r12, %r8
callq *0x10(%rax)
leaq 0x48(%r13), %rdi
xorl %r14d, %r14d
leaq 0x20(%rsp), %rdx
movq %r14, (%rdx)
movq %rdi, 0x98(%rsp)
movq 0x8(%rsp), %rsi
callq 0x55a0
leaq 0x60(%r13), %rdi
movq $0x0, 0x40(%rsp)
leaq 0x20(%rsp), %rax
movq %r14, (%rax)
leaq 0x20(%rsp), %rdx
movq 0x18(%rsp), %r14
movq %r14, %rsi
callq 0x55a0
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x90(%r13)
movq %r13, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
movq %r12, %rcx
callq 0x55d0
movq (%rbp), %rax
movq %rbp, %rdi
movq %r13, %rsi
movq 0x30(%rsp), %rdx
movq %r12, %rcx
callq *0x8(%rax)
movsd %xmm0, 0x8(%rsp)
movq %r15, %rdi
movq %r14, %rsi
callq 0x54b0
mulsd 0x10(%rsp), %xmm0
addsd 0x8(%rsp), %xmm0
movq 0xf0(%rsp), %rax
movsd %xmm0, (%rax)
cmpl $0x0, 0x108(%rsp)
jle 0x88f2
movq 0xf8(%rsp), %r14
leaq 0xb8(%r13), %rcx
movq %rcx, 0x90(%rsp)
movsd %xmm0, 0xa0(%rsp)
movq 0x100(%rsp), %rax
incl (%rax)
movq %r13, %rdi
movq %r12, %rsi
callq 0x5080
movsd 0x90(%r13), %xmm1
movq %r13, %rdi
movq %r12, %rsi
movsd 0x10(%rsp), %xmm0
callq 0x51e0
movsd 0x90(%r13), %xmm1
movq %r13, %rdi
callq 0x5120
movq (%rbp), %rax
movq %rbp, %rdi
movq 0x98(%rsp), %rsi
movq %r12, %rdx
movsd 0x1d53(%rip), %xmm0     # 0xa1c0
callq *(%rax)
movq 0xf0(%rsp), %rax
movsd (%rax), %xmm1
movapd %xmm0, 0x70(%rsp)
subsd %xmm0, %xmm1
movsd %xmm1, (%r14)
movq %r15, %rdi
movq 0x18(%rsp), %rsi
callq 0x5360
addq %rax, %rax
cmpq $0x65, %rax
movl $0x64, %ecx
cmovbq %rcx, %rax
movq 0xa8(%r13), %rcx
subq 0xa0(%r13), %rcx
sarq $0x3, %rcx
cmpq %rcx, %rax
cmovbq %rax, %rcx
movq %rcx, 0x98(%r13)
movq %r13, %rdi
movq %r12, %rsi
movsd 0x10(%rsp), %xmm0
movq %r15, %rdx
callq 0x5560
movsd (%r14), %xmm0
addsd %xmm0, %xmm0
divsd 0x8(%rbp), %xmm0
ucomisd 0x1c3f(%rip), %xmm0     # 0xa130
jb 0x84f9
sqrtsd %xmm0, %xmm0
jmp 0x84fe
callq 0x5230
movq 0xa0(%r13), %rdi
movq 0xa8(%r13), %rsi
movq 0x90(%rsp), %rdx
callq 0x5150
movq 0xa0(%r13), %rcx
cmpq 0xa8(%r13), %rax
je 0x8530
movq %rax, 0xa8(%r13)
subq %rcx, %rax
sarq $0x3, %rax
cmpq %rax, 0x98(%r13)
jbe 0x8547
movq %rax, 0x98(%r13)
movb $0x1, 0xf0(%r13)
movq $0x0, 0xf8(%r13)
movq 0xf0(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x48(%rsp)
movq %r13, %rdi
movq %r15, %rsi
movq %rbx, %rdx
movsd 0x10(%rsp), %xmm0
movq %rbp, %rcx
movq %r12, %r8
callq 0x5620
movsd %xmm0, 0x90(%r13)
movq (%rbp), %rax
movq %rbp, %rdi
movq %r13, %rsi
movq 0x30(%rsp), %rdx
movq %r12, %rcx
callq *0x8(%rax)
movsd %xmm0, 0x8(%rsp)
movq %r15, %rdi
movq 0x18(%rsp), %rsi
callq 0x54b0
mulsd 0x10(%rsp), %xmm0
addsd 0x8(%rsp), %xmm0
movq 0xf0(%rsp), %r14
movsd %xmm0, (%r14)
movsd 0x90(%r13), %xmm0
movq (%rbp), %rax
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq *(%rax)
movsd (%r14), %xmm1
movapd %xmm1, %xmm2
subsd %xmm0, %xmm2
movapd %xmm1, %xmm3
subsd 0x70(%rsp), %xmm3
mulsd 0x1c61(%rip), %xmm3     # 0xa268
ucomisd %xmm2, %xmm3
ja 0x863a
andpd 0x1a3b(%rip), %xmm0     # 0xa050
divsd %xmm0, %xmm2
movsd 0xd0(%r13), %xmm0
ucomisd %xmm2, %xmm0
ja 0x863a
ucomisd 0x48(%rsp), %xmm1
movsd %xmm1, 0x48(%rsp)
jb 0x856c
movq (%rbp), %rax
movq %rbp, %rdi
movq %r13, %rsi
movq 0x30(%rsp), %rdx
movq %r12, %rcx
callq *0x8(%rax)
movsd %xmm0, 0x8(%rsp)
movq %r15, %rdi
movq 0x18(%rsp), %rsi
callq 0x54b0
mulsd 0x10(%rsp), %xmm0
addsd 0x8(%rsp), %xmm0
movq 0xf0(%rsp), %rax
movsd %xmm0, (%rax)
subsd 0x70(%rsp), %xmm0
movq 0xf8(%rsp), %r14
movsd %xmm0, (%r14)
leaq 0x50(%rsp), %rdi
callq 0x50a0
movsd %xmm0, 0x8(%rsp)
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
callq 0x5500
xorps %xmm0, %xmm0
cvtsi2sdq 0x20(%rsp), %xmm0
xorps %xmm1, %xmm1
cvtsi2sdq 0x28(%rsp), %xmm1
divsd 0x1b83(%rip), %xmm1     # 0xa248
addsd %xmm0, %xmm1
movsd %xmm1, 0x58(%rsp)
movb $0x1, 0x68(%rsp)
cmpb $0x1, 0xd8(%r13)
jne 0x87e3
movl $0x6, %edx
movq 0x48da(%rip), %rbx      # 0xcfc8
movq %rbx, %rdi
leaq 0x1bc9(%rip), %rsi      # 0xa2c1
callq 0x53d0
movq 0x100(%rsp), %rax
movl (%rax), %esi
movq %rbx, %rdi
callq 0x5550
movq %rax, %rbx
movl $0x7, %edx
movq %rax, %rdi
leaq 0x1ba7(%rip), %rsi      # 0xa2c8
callq 0x53d0
movq %rbx, %rdi
movsd 0x8(%rsp), %xmm0
callq 0x54f0
movq %rax, %rbx
movl $0xc, %edx
movq %rax, %rdi
leaq 0x1b8a(%rip), %rsi      # 0xa2d0
callq 0x53d0
movq 0xf0(%rsp), %rax
movsd (%rax), %xmm0
movq %rbx, %rdi
callq 0x54f0
movq %rax, %rbx
movl $0xe, %edx
movq %rax, %rdi
leaq 0x1b6c(%rip), %rsi      # 0xa2dd
callq 0x53d0
movsd (%r14), %xmm0
movq %rbx, %rdi
callq 0x54f0
movq %rax, %rbx
movl $0x10, %edx
movq %rax, %rdi
leaq 0x1b57(%rip), %rsi      # 0xa2ec
callq 0x53d0
movq 0xa8(%r13), %rsi
subq 0xa0(%r13), %rsi
sarq $0x3, %rsi
movq %rbx, %rdi
callq 0x51f0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x52d0
movsbl %al, %esi
movq %rbx, %rdi
movq 0x38(%rsp), %rbx
callq 0x5040
movq %rax, %rdi
callq 0x5200
movq 0xf0(%rsp), %rax
movsd (%rax), %xmm1
leaq 0xa8(%rsp), %rdi
movsd 0x8(%rsp), %xmm0
callq 0x5610
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
callq 0x5500
xorps %xmm0, %xmm0
cvtsi2sdq 0x20(%rsp), %xmm0
xorps %xmm1, %xmm1
cvtsi2sdq 0x28(%rsp), %xmm1
divsd 0x1a1e(%rip), %xmm1     # 0xa248
addsd %xmm0, %xmm1
subsd 0x58(%rsp), %xmm1
addsd 0x60(%rsp), %xmm1
movsd %xmm1, 0x60(%rsp)
movb $0x0, 0x68(%rsp)
movsd (%r14), %xmm0
movapd 0x70(%rsp), %xmm1
andpd 0x17f8(%rip), %xmm1     # 0xa050
divsd %xmm1, %xmm0
movsd 0xd0(%r13), %xmm1
ucomisd %xmm0, %xmm1
movsd 0x8(%rsp), %xmm1
jbe 0x887c
ucomisd 0xe0(%r13), %xmm1
ja 0x88b9
movq 0xf0(%rsp), %rax
movsd (%rax), %xmm0
ucomisd 0xa0(%rsp), %xmm0
jae 0x88c2
ucomisd 0xe8(%r13), %xmm1
ja 0x88cb
movq 0x40(%rsp), %rcx
incl %ecx
movq %rcx, 0x40(%rsp)
cmpl 0x108(%rsp), %ecx
jne 0x840a
jmp 0x88f2
leaq 0x1a3d(%rip), %r14      # 0xa2fd
jmp 0x88d2
leaq 0x1a4f(%rip), %r14      # 0xa318
jmp 0x88d2
leaq 0x1a60(%rip), %r14      # 0xa332
movq 0x88(%rsp), %rbx
movq %rbx, %rdi
callq 0x5110
movq %rbx, %rdi
movq %rax, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x54d0
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq