// File: /workspace/github_cpp_makefile_star2_O2/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 $0xa8, %rsp
movq %r9, 0x50(%rsp)
movq %r8, 0x20(%rsp)
movq %rcx, %r15
movq %rdx, %rbx
movsd %xmm0, 0x10(%rsp)
movq %rsi, %r12
movq %rdi, %r13
movq 0xf0(%rsp), %rbp
movq 0x100(%rsp), %r14
andq $0x0, 0x98(%rsp)
callq 0x7110
movsd %xmm0, 0x88(%rsp)
movq %r14, 0x78(%rsp)
andl $0x0, 0x80(%rsp)
movb $0x0, 0xa0(%rsp)
movq 0x6196(%rip), %rax      # 0xffc8
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movq $0xa, 0x8(%rax,%rcx)
andl $0x0, (%rbp)
movq %rbx, %rdi
callq 0x70a0
movq %rax, %rbp
movq 0x18(%r12), %rax
movq %rax, 0x8(%rsp)
movq 0x20(%r12), %rbx
andq $0x0, 0x98(%r13)
leaq 0xa0(%r13), %rdi
movq %rdi, 0x68(%rsp)
movq %rbx, %rsi
callq 0x7550
movq 0xa0(%r13), %rax
xorl %ecx, %ecx
cmpq %rcx, %rbx
je 0x9e93
movq %rcx, (%rax,%rcx,8)
incq %rcx
jmp 0x9e85
leaq 0x18(%r13), %rsi
movq %rsi, 0x18(%rsp)
movq 0x20(%rsp), %rax
movsd (%rax), %xmm0
movq %rbp, %rdi
movq %r15, %rdx
movq %r12, %rcx
callq 0x7180
leaq 0x78(%r13), %r14
movq (%rbp), %rax
movq %rbp, %rdi
movq 0x18(%rsp), %rsi
movq %r13, %rdx
movq %r14, %rcx
movq %r12, %r8
callq *0x10(%rax)
leaq 0x48(%r13), %rdi
leaq 0x38(%rsp), %rdx
andq $0x0, (%rdx)
movq %rdi, 0x60(%rsp)
movq 0x8(%rsp), %rsi
callq 0x7830
leaq 0x60(%r13), %rdi
leaq 0x38(%rsp), %rax
andq $0x0, (%rax)
leaq 0x38(%rsp), %rdx
movq %rbx, %rsi
callq 0x7830
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x90(%r13)
movq %r13, %rdi
movq 0x20(%rsp), %rsi
movq %rbp, %rdx
movq %r12, %rcx
callq 0x7860
movq (%rbp), %rax
movq %rbp, %rdi
movq %r13, %rsi
movq %r14, 0x30(%rsp)
movq %r14, %rdx
movq %r12, %rcx
callq *0x8(%rax)
movsd %xmm0, 0x8(%rsp)
movq %r15, %rdi
movq %rbx, %rsi
callq 0x76e0
mulsd 0x10(%rsp), %xmm0
addsd 0x8(%rsp), %xmm0
movq 0xe0(%rsp), %rcx
movsd %xmm0, (%rcx)
leaq 0xb8(%r13), %rax
movq %rax, 0x58(%rsp)
xorl %edx, %edx
movl 0xf8(%rsp), %eax
testl %eax, %eax
cmovlel %edx, %eax
movl %eax, 0x28(%rsp)
cmpl 0x28(%rsp), %edx
je 0xa3cc
movl %edx, 0x2c(%rsp)
movsd %xmm0, 0x70(%rsp)
movq 0xf0(%rsp), %rax
incl (%rax)
movq %r13, %rdi
movq %r12, %rsi
movq %rcx, %r14
callq 0x7080
movsd 0x90(%r13), %xmm1
movq %r13, %rdi
movq %r12, %rsi
movsd 0x10(%rsp), %xmm0
callq 0x72d0
movsd 0x90(%r13), %xmm1
movq %r13, %rdi
callq 0x71b0
movq (%rbp), %rax
movq %rbp, %rdi
movq 0x60(%rsp), %rsi
movq %r12, %rdx
movsd 0x219a(%rip), %xmm0     # 0xc190
callq *(%rax)
movsd (%r14), %xmm1
movapd %xmm0, 0x40(%rsp)
subsd %xmm0, %xmm1
movq 0xe8(%rsp), %rax
movsd %xmm1, (%rax)
movq %r15, %rdi
movq %rbx, %rsi
callq 0x7490
addq %rax, %rax
cmpq $0x65, %rax
pushq $0x64
popq %rcx
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 0x77c0
movq 0xe8(%rsp), %rax
movsd (%rax), %xmm0
addsd %xmm0, %xmm0
divsd 0x8(%rbp), %xmm0
ucomisd 0x20b3(%rip), %xmm0     # 0xc130
jb 0xa085
sqrtsd %xmm0, %xmm0
jmp 0xa08a
callq 0x7320
movq 0xa0(%r13), %rdi
movq 0xa8(%r13), %rsi
movq 0x58(%rsp), %rdx
callq 0x71e0
movq 0xa8(%r13), %rdx
movq 0x68(%rsp), %rdi
movq %rax, %rsi
callq 0x7790
movq 0xa8(%r13), %rax
subq 0xa0(%r13), %rax
sarq $0x3, %rax
cmpq %rax, 0x98(%r13)
jbe 0xa0d8
movq %rax, 0x98(%r13)
movb $0x1, 0xf0(%r13)
andq $0x0, 0xf8(%r13)
movq 0xe0(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x18(%rsp)
movq %r13, %rdi
movq %r15, %rsi
movq 0x20(%rsp), %rdx
movsd 0x10(%rsp), %xmm0
movq %rbp, %rcx
movq %r12, %r8
callq 0x78e0
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 %rbx, %rsi
callq 0x76e0
mulsd 0x10(%rsp), %xmm0
addsd 0x8(%rsp), %xmm0
movq 0xe0(%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 0x40(%rsp), %xmm3
mulsd 0x20a3(%rip), %xmm3     # 0xc238
ucomisd %xmm2, %xmm3
ja 0xa1c8
andpd 0x1ead(%rip), %xmm0     # 0xc050
divsd %xmm0, %xmm2
movsd 0xd0(%r13), %xmm0
ucomisd %xmm2, %xmm0
ja 0xa1c8
ucomisd 0x18(%rsp), %xmm1
movsd %xmm1, 0x18(%rsp)
jb 0xa0fa
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 %rbx, %rsi
callq 0x76e0
mulsd 0x10(%rsp), %xmm0
addsd 0x8(%rsp), %xmm0
movq 0xe0(%rsp), %rax
movsd %xmm0, (%rax)
subsd 0x40(%rsp), %xmm0
movq 0xe8(%rsp), %rax
movsd %xmm0, (%rax)
leaq 0x88(%rsp), %r14
movq %r14, %rdi
callq 0x70c0
movsd %xmm0, 0x8(%rsp)
callq 0x7110
movsd %xmm0, 0x90(%rsp)
movb $0x1, 0xa0(%rsp)
cmpb $0x1, 0xd8(%r13)
jne 0xa30b
movq 0x5d6f(%rip), %rdi      # 0xffc8
leaq 0x1ff1(%rip), %rsi      # 0xc251
callq 0x7410
movq 0xf0(%rsp), %rcx
movl (%rcx), %esi
movq %rax, %rdi
callq 0x77b0
movq %rax, %rdi
leaq 0x1fd7(%rip), %rsi      # 0xc258
callq 0x7410
movq %rax, %rdi
movsd 0x8(%rsp), %xmm0
callq 0x7720
movq %rax, %rdi
leaq 0x1fc2(%rip), %rsi      # 0xc260
callq 0x7410
movq 0xe0(%rsp), %rcx
movsd (%rcx), %xmm0
movq %rax, %rdi
callq 0x7720
movq %rax, %rdi
leaq 0x1fac(%rip), %rsi      # 0xc26d
callq 0x7410
movq 0xe8(%rsp), %rcx
movsd (%rcx), %xmm0
movq %rax, %rdi
callq 0x7720
movq %rax, %rdi
leaq 0x1f98(%rip), %rsi      # 0xc27c
callq 0x7410
movq 0xa8(%r13), %rsi
subq 0xa0(%r13), %rsi
sarq $0x3, %rsi
movq %rax, %rdi
callq 0x72f0
movq %rax, %rdi
callq 0x71f0
movq 0xe0(%rsp), %rax
movsd (%rax), %xmm1
leaq 0x78(%rsp), %rdi
movsd 0x8(%rsp), %xmm0
callq 0x78c0
movq %r14, %rdi
callq 0x7160
movq 0xe8(%rsp), %rax
movsd (%rax), %xmm0
movapd 0x40(%rsp), %xmm1
andpd 0x1d07(%rip), %xmm1     # 0xc050
divsd %xmm1, %xmm0
movsd 0xd0(%r13), %xmm1
ucomisd %xmm0, %xmm1
movsd 0x8(%rsp), %xmm1
jbe 0xa36d
ucomisd 0xe0(%r13), %xmm1
ja 0xa39f
movq 0xe0(%rsp), %rcx
movsd (%rcx), %xmm0
ucomisd 0x70(%rsp), %xmm0
jae 0xa3a8
movl 0x2c(%rsp), %edx
incl %edx
ucomisd 0xe8(%r13), %xmm1
jbe 0x9f88
leaq 0x1f25(%rip), %r14      # 0xc2c2
jmp 0xa3af
leaq 0x1ee7(%rip), %r14      # 0xc28d
jmp 0xa3af
leaq 0x1ef9(%rip), %r14      # 0xc2a8
movq 0x50(%rsp), %rbx
movq %rbx, %rdi
callq 0x71a0
movq %rbx, %rdi
movq %rax, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x7700
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq