// File: 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:
subq $0x1b8, %rsp            # imm = 0x1B8
movq 0x1e0(%rsp), %rax
movl 0x1d8(%rsp), %eax
movq 0x1d0(%rsp), %rax
movq 0x1c8(%rsp), %rax
movq 0x1c0(%rsp), %rax
movq %rdi, 0x1b0(%rsp)
movq %rsi, 0x1a8(%rsp)
movsd %xmm0, 0x1a0(%rsp)
movq %rdx, 0x198(%rsp)
movq %rcx, 0x190(%rsp)
movq %r8, 0x188(%rsp)
movq %r9, 0x180(%rsp)
movq 0x1b0(%rsp), %rax
movq %rax, 0x60(%rsp)
leaq 0x160(%rsp), %rdi
callq 0xe0d0
movq 0x1e0(%rsp), %rsi
leaq 0x150(%rsp), %rdi
callq 0xe720
movq 0xa1bb(%rip), %rax      # 0x20fb8
movq (%rax), %rax
movq 0xa1b1(%rip), %rdi      # 0x20fb8
addq -0x18(%rax), %rdi
movl $0xa, %esi
callq 0xee20
movq 0x1d0(%rsp), %rax
movl $0x0, (%rax)
movq 0x198(%rsp), %rdi
callq 0xe120
movq %rax, 0x148(%rsp)
movq 0x1a8(%rsp), %rdi
callq 0xe7f0
movq %rax, 0x140(%rsp)
movq 0x1a8(%rsp), %rdi
callq 0xf1b0
movq 0x60(%rsp), %rdi
movq %rax, 0x138(%rsp)
movq $0x0, 0x98(%rdi)
addq $0xa0, %rdi
movq 0x138(%rsp), %rsi
callq 0xef00
movq $0x0, 0x130(%rsp)
movq 0x130(%rsp), %rax
cmpq 0x138(%rsp), %rax
jae 0x16ee8
movq 0x60(%rsp), %rdi
movq 0x130(%rsp), %rax
movq %rax, 0x58(%rsp)
addq $0xa0, %rdi
movq 0x130(%rsp), %rsi
callq 0xe2d0
movq 0x58(%rsp), %rcx
movq %rcx, (%rax)
movq 0x130(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x130(%rsp)
jmp 0x16e92
movq 0x60(%rsp), %rsi
movq 0x148(%rsp), %rdi
addq $0x18, %rsi
movq 0x190(%rsp), %rdx
movq 0x188(%rsp), %rax
movsd (%rax), %xmm0
movq 0x1a8(%rsp), %rcx
callq 0xe410
movq 0x60(%rsp), %rdx
movq 0x148(%rsp), %rdi
movq %rdx, %rsi
addq $0x18, %rsi
movq %rdx, %rcx
addq $0x78, %rcx
movq 0x1a8(%rsp), %r8
movq (%rdi), %rax
callq *0x10(%rax)
movq 0x60(%rsp), %rdi
addq $0x48, %rdi
movq 0x140(%rsp), %rsi
xorps %xmm0, %xmm0
movsd %xmm0, 0x128(%rsp)
leaq 0x128(%rsp), %rdx
callq 0xe460
movq 0x60(%rsp), %rdi
addq $0x60, %rdi
movq 0x138(%rsp), %rsi
xorps %xmm0, %xmm0
movsd %xmm0, 0x120(%rsp)
leaq 0x120(%rsp), %rdx
callq 0xe460
movq 0x60(%rsp), %rdi
movsd 0x51ac(%rip), %xmm0     # 0x1c150
movsd %xmm0, 0x90(%rdi)
movq 0x188(%rsp), %rsi
movq 0x148(%rsp), %rdx
movq 0x1a8(%rsp), %rcx
callq 0xf7e0
movq 0x60(%rsp), %rsi
movq 0x148(%rsp), %rdi
movq %rsi, %rdx
addq $0x78, %rdx
movq 0x1a8(%rsp), %rcx
movq (%rdi), %rax
callq *0x8(%rax)
movsd %xmm0, 0x118(%rsp)
movsd 0x1a0(%rsp), %xmm0
movsd %xmm0, 0x50(%rsp)
movq 0x190(%rsp), %rdi
movq 0x138(%rsp), %rsi
callq 0xf330
movaps %xmm0, %xmm1
movsd 0x50(%rsp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x110(%rsp)
movsd 0x118(%rsp), %xmm0
addsd 0x110(%rsp), %xmm0
movq 0x1c0(%rsp), %rax
movsd %xmm0, (%rax)
movl $0x0, 0x10c(%rsp)
movl 0x10c(%rsp), %eax
cmpl 0x1d8(%rsp), %eax
jge 0x1781b
movq 0x60(%rsp), %rdi
movq 0x1d0(%rsp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movq 0x1c0(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x100(%rsp)
movq 0x1a8(%rsp), %rsi
callq 0xe0f0
movq 0x60(%rsp), %rdi
movq 0x1a8(%rsp), %rsi
movsd 0x1a0(%rsp), %xmm0
movsd 0x90(%rdi), %xmm1
callq 0xe7a0
movq 0x60(%rsp), %rdi
movsd %xmm0, 0xf8(%rsp)
movsd 0xf8(%rsp), %xmm0
movsd 0x90(%rdi), %xmm1
callq 0xe490
movq 0x60(%rsp), %rsi
movq 0x148(%rsp), %rdi
addq $0x48, %rsi
movq 0x1a8(%rsp), %rdx
movq (%rdi), %rax
movsd 0x5044(%rip), %xmm0     # 0x1c150
callq *(%rax)
movsd %xmm0, 0xf0(%rsp)
movq 0x1c0(%rsp), %rax
movsd (%rax), %xmm0
subsd 0xf0(%rsp), %xmm0
movq 0x1c8(%rsp), %rax
movsd %xmm0, (%rax)
movq 0x190(%rsp), %rdi
movq 0x138(%rsp), %rsi
callq 0xec10
movq %rax, %rcx
movq 0x60(%rsp), %rax
shlq %rcx
movq %rcx, 0x98(%rax)
cmpq $0x64, 0x98(%rax)
jae 0x17179
movq 0x60(%rsp), %rax
movq $0x64, 0x98(%rax)
movq 0x60(%rsp), %rdi
movq 0x98(%rdi), %rax
movq %rax, 0x48(%rsp)
addq $0xa0, %rdi
callq 0xf160
movq %rax, %rcx
movq 0x48(%rsp), %rax
cmpq %rcx, %rax
jbe 0x171c3
movq 0x60(%rsp), %rdi
addq $0xa0, %rdi
callq 0xf160
movq %rax, %rcx
movq 0x60(%rsp), %rax
movq %rcx, 0x98(%rax)
movq 0x60(%rsp), %rdi
movq 0x1a8(%rsp), %rsi
movsd 0x1a0(%rsp), %xmm0
movq 0x190(%rsp), %rdx
movq 0x98(%rdi), %rcx
callq 0xf540
movq 0x1c8(%rsp), %rax
movsd 0x4f63(%rip), %xmm0     # 0x1c160
mulsd (%rax), %xmm0
movq 0x148(%rsp), %rax
divsd 0x8(%rax), %xmm0
callq 0xe900
movq 0x60(%rsp), %rsi
movsd %xmm0, 0xe8(%rsp)
addq $0xb8, %rsi
movsd 0xe8(%rsp), %xmm0
leaq 0xd8(%rsp), %rdi
callq 0xebe0
movq 0x60(%rsp), %rdi
movq %rdi, %rax
addq $0xa0, %rax
movq %rax, 0x38(%rsp)
addq $0xa0, %rdi
callq 0xe100
movq 0x60(%rsp), %rdi
movq %rax, 0xc0(%rsp)
addq $0xa0, %rdi
callq 0xf890
movq %rax, 0xb8(%rsp)
movq 0xd8(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq 0xe0(%rsp), %rax
movq %rax, 0xb0(%rsp)
movq 0xc0(%rsp), %rdi
movq 0xb8(%rsp), %rsi
movq 0xa8(%rsp), %rdx
movsd 0xb0(%rsp), %xmm0
callq 0xe390
movq %rax, 0xc8(%rsp)
leaq 0xd0(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0xe210
movq 0x60(%rsp), %rdi
addq $0xa0, %rdi
callq 0xf890
movq %rax, 0x98(%rsp)
leaq 0xa0(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0xe210
movq 0x38(%rsp), %rdi
movq 0xd0(%rsp), %rsi
movq 0xa0(%rsp), %rdx
callq 0xf4e0
movq 0x60(%rsp), %rdi
movq %rax, 0x90(%rsp)
movq 0x98(%rdi), %rax
movq %rax, 0x40(%rsp)
addq $0xa0, %rdi
callq 0xf160
movq %rax, %rcx
movq 0x40(%rsp), %rax
cmpq %rcx, %rax
jbe 0x1737b
movq 0x60(%rsp), %rdi
addq $0xa0, %rdi
callq 0xf160
movq %rax, %rcx
movq 0x60(%rsp), %rax
movq %rcx, 0x98(%rax)
movq 0x60(%rsp), %rdi
movsd 0x4e70(%rip), %xmm0     # 0x1c1f8
movsd %xmm0, 0x88(%rsp)
callq 0xf7c0
movq 0x60(%rsp), %rdi
movq 0x1c0(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x80(%rsp)
movq 0x190(%rsp), %rsi
movq 0x188(%rsp), %rdx
movsd 0x1a0(%rsp), %xmm0
movq 0x148(%rsp), %rcx
movq 0x1a8(%rsp), %r8
callq 0xf900
movq 0x60(%rsp), %rsi
movsd %xmm0, 0x90(%rsi)
movq 0x148(%rsp), %rdi
movq %rsi, %rdx
addq $0x78, %rdx
movq 0x1a8(%rsp), %rcx
movq (%rdi), %rax
callq *0x8(%rax)
movsd %xmm0, 0x118(%rsp)
movsd 0x1a0(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
movq 0x190(%rsp), %rdi
movq 0x138(%rsp), %rsi
callq 0xf330
movq 0x60(%rsp), %rsi
movaps %xmm0, %xmm1
movsd 0x30(%rsp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x110(%rsp)
movsd 0x118(%rsp), %xmm0
addsd 0x110(%rsp), %xmm0
movq 0x1c0(%rsp), %rax
movsd %xmm0, (%rax)
movq 0x148(%rsp), %rdi
movq 0x1a8(%rsp), %rdx
movsd 0x90(%rsi), %xmm0
movq (%rdi), %rax
callq *(%rax)
movsd %xmm0, 0x78(%rsp)
movq 0x1c0(%rsp), %rax
movsd (%rax), %xmm0
subsd 0x78(%rsp), %xmm0
movsd %xmm0, 0x70(%rsp)
movsd 0x70(%rsp), %xmm1
movsd 0x88(%rsp), %xmm0
movq 0x1c0(%rsp), %rax
movsd (%rax), %xmm2
subsd 0xf0(%rsp), %xmm2
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x174d9
jmp 0x17527
movq 0x60(%rsp), %rax
movsd 0x70(%rsp), %xmm1
movsd 0x78(%rsp), %xmm0
movaps 0x4b4f(%rip), %xmm2     # 0x1c040
pand %xmm2, %xmm0
divsd %xmm0, %xmm1
movsd 0xd0(%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x17509
jmp 0x17527
movq 0x1c0(%rsp), %rax
movsd (%rax), %xmm0
ucomisd 0x80(%rsp), %xmm0
jb 0x17522
jmp 0x17527
jmp 0x17396
movq 0x60(%rsp), %rsi
movq 0x148(%rsp), %rdi
movq %rsi, %rdx
addq $0x78, %rdx
movq 0x1a8(%rsp), %rcx
movq (%rdi), %rax
callq *0x8(%rax)
movsd %xmm0, 0x118(%rsp)
movsd 0x1a0(%rsp), %xmm0
movsd %xmm0, 0x28(%rsp)
movq 0x190(%rsp), %rdi
movq 0x138(%rsp), %rsi
callq 0xf330
movaps %xmm0, %xmm1
movsd 0x28(%rsp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x110(%rsp)
movsd 0x118(%rsp), %xmm0
addsd 0x110(%rsp), %xmm0
movq 0x1c0(%rsp), %rax
movsd %xmm0, (%rax)
movq 0x1c0(%rsp), %rax
movsd (%rax), %xmm0
subsd 0xf0(%rsp), %xmm0
movq 0x1c8(%rsp), %rax
movsd %xmm0, (%rax)
leaq 0x160(%rsp), %rdi
callq 0xe240
movsd %xmm0, 0x68(%rsp)
leaq 0x160(%rsp), %rdi
callq 0xf5d0
movq 0x60(%rsp), %rax
testb $0x1, 0xd8(%rax)
je 0x176c6
movq 0x99b4(%rip), %rdi      # 0x20fb8
leaq 0x4c26(%rip), %rsi      # 0x1c231
callq 0xeb20
movq %rax, %rdi
movq 0x1d0(%rsp), %rax
movl (%rax), %esi
callq 0xf520
movq %rax, %rdi
leaq 0x4c0c(%rip), %rsi      # 0x1c238
callq 0xeb20
movq %rax, %rdi
movsd 0x68(%rsp), %xmm0
callq 0xf800
movq %rax, %rdi
leaq 0x4bf7(%rip), %rsi      # 0x1c240
callq 0xeb20
movq %rax, %rdi
movq 0x1c0(%rsp), %rax
movsd (%rax), %xmm0
callq 0xf800
movq %rax, %rdi
leaq 0x4be1(%rip), %rsi      # 0x1c24d
callq 0xeb20
movq %rax, %rdi
movq 0x1c8(%rsp), %rax
movsd (%rax), %xmm0
callq 0xf800
movq %rax, %rdi
leaq 0x4bcd(%rip), %rsi      # 0x1c25c
callq 0xeb20
movq 0x60(%rsp), %rdi
movq %rax, 0x20(%rsp)
addq $0xa0, %rdi
callq 0xf160
movq 0x20(%rsp), %rdi
movq %rax, %rsi
callq 0xe270
movq %rax, %rdi
movq 0x98df(%rip), %rsi      # 0x20fa0
callq 0xec40
movsd 0x68(%rsp), %xmm0
movq 0x1c0(%rsp), %rax
movsd (%rax), %xmm1
leaq 0x150(%rsp), %rdi
callq 0xf8d0
leaq 0x160(%rsp), %rdi
callq 0xe3e0
movq 0x60(%rsp), %rax
movq 0x1c8(%rsp), %rcx
movsd (%rcx), %xmm1
movsd 0xf0(%rsp), %xmm0
movaps 0x492d(%rip), %xmm2     # 0x1c040
pand %xmm2, %xmm0
divsd %xmm0, %xmm1
movsd 0xd0(%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x17773
movq 0x60(%rsp), %rax
movsd 0x68(%rsp), %xmm0
ucomisd 0xe0(%rax), %xmm0
jbe 0x17773
movq 0x180(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x180(%rsp), %rdi
callq 0xe480
movq 0x18(%rsp), %rdi
movq %rax, %rsi
leaq 0x4b06(%rip), %rdx      # 0x1c26d
movb $0x0, %al
callq 0xf360
jmp 0x1781b
movq 0x1c0(%rsp), %rax
movsd (%rax), %xmm0
ucomisd 0x100(%rsp), %xmm0
jb 0x177bc
movq 0x180(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x180(%rsp), %rdi
callq 0xe480
movq 0x10(%rsp), %rdi
movq %rax, %rsi
leaq 0x4ad5(%rip), %rdx      # 0x1c288
movb $0x0, %al
callq 0xf360
jmp 0x1781b
movq 0x60(%rsp), %rax
movsd 0x68(%rsp), %xmm0
ucomisd 0xe8(%rax), %xmm0
jbe 0x17803
movq 0x180(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x180(%rsp), %rdi
callq 0xe480
movq 0x8(%rsp), %rdi
movq %rax, %rsi
leaq 0x4aa8(%rip), %rdx      # 0x1c2a2
movb $0x0, %al
callq 0xf360
jmp 0x1781b
jmp 0x17805
movl 0x10c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x10c(%rsp)
jmp 0x17057
addq $0x1b8, %rsp            # imm = 0x1B8
retq
nopw %cs:(%rax,%rax)
nopl (%rax)