// File: kobanium[P]Ray/src/learn/FactorizationMachines.cpp
// Function: UpdateSecondTermAdam(btfm_t*, double, double, double, int)
static void
UpdateSecondTermAdam( btfm_t *feature, const double alpha, const double lambda, const double minV, const int id )
{
  const double beta = 0.9;
  const double gamma = 0.999;
  const double epsilon = 1e-6;

  for (int f = 0; f < FM_DIMENSION; f++) {
    const double v = feature->v[f];
    const double grad = feature->grad_v[id][f];
    const double gradSq = grad * grad;
    const double weight_decay = v - 1.0;
    const double first_moment_v = beta * feature->first_moment_v[f] + (1.0 - beta) * grad;
    const double second_moment_v = gamma * feature->second_moment_v[f] + (1.0 - gamma) * gradSq;
    feature->first_moment_v[f] = first_moment_v;
    feature->second_moment_v[f] = second_moment_v;

    const double first_hat = first_moment_v / (1.0 - beta);
    const double second_hat = second_moment_v / (1.0 - gamma);
    const double newParam = v + alpha * (first_hat / (sqrt(second_hat) + epsilon) - lambda * weight_decay);

    feature->v[f] = std::max(minV, newParam);
    feature->grad_v[id][f] = 0.0;
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movsd %xmm0, -0x10(%rbp)
movsd %xmm1, -0x18(%rbp)
movsd %xmm2, -0x20(%rbp)
movl %esi, -0x24(%rbp)
movsd 0x4ed3f(%rip), %xmm0    # 0xadf68
movsd %xmm0, -0x30(%rbp)
movsd 0x4ed2a(%rip), %xmm0    # 0xadf60
movsd %xmm0, -0x38(%rbp)
movsd 0x4ed05(%rip), %xmm0    # 0xadf48
movsd %xmm0, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
cmpl $0x5, -0x44(%rbp)
jge 0x5f45a
movq -0x8(%rbp), %rax
movslq -0x44(%rbp), %rcx
movsd 0x8(%rax,%rcx,8), %xmm0
movsd %xmm0, -0x50(%rbp)
movq -0x8(%rbp), %rax
addq $0x70, %rax
movslq -0x24(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movslq -0x44(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x58(%rbp)
movsd -0x58(%rbp), %xmm0
mulsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x60(%rbp)
movsd -0x50(%rbp), %xmm0
movsd 0x4cd5f(%rip), %xmm1    # 0xac008
subsd %xmm1, %xmm0
movsd %xmm0, -0x68(%rbp)
movq -0x8(%rbp), %rax
movslq -0x44(%rbp), %rcx
movsd 0x1c0(%rax,%rcx,8), %xmm0
movsd 0x4ec8d(%rip), %xmm1    # 0xadf58
mulsd -0x58(%rbp), %xmm1
movsd 0x4ec90(%rip), %xmm2    # 0xadf68
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x70(%rbp)
movq -0x8(%rbp), %rax
movslq -0x44(%rbp), %rcx
movsd 0x1e8(%rax,%rcx,8), %xmm0
movsd 0x4ec52(%rip), %xmm1    # 0xadf50
mulsd -0x60(%rbp), %xmm1
movsd 0x4ec55(%rip), %xmm2    # 0xadf60
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x78(%rbp)
movsd -0x70(%rbp), %xmm0
movq -0x8(%rbp), %rax
movslq -0x44(%rbp), %rcx
movsd %xmm0, 0x1c0(%rax,%rcx,8)
movsd -0x78(%rbp), %xmm0
movq -0x8(%rbp), %rax
movslq -0x44(%rbp), %rcx
movsd %xmm0, 0x1e8(%rax,%rcx,8)
movsd -0x70(%rbp), %xmm0
movsd 0x4ec07(%rip), %xmm1    # 0xadf58
divsd %xmm1, %xmm0
movsd %xmm0, -0x80(%rbp)
movsd -0x78(%rbp), %xmm0
movsd 0x4ebe9(%rip), %xmm1    # 0xadf50
divsd %xmm1, %xmm0
movsd %xmm0, -0x88(%rbp)
movsd -0x50(%rbp), %xmm0
movsd %xmm0, -0xa0(%rbp)
movsd -0x10(%rbp), %xmm0
movsd %xmm0, -0x98(%rbp)
movsd -0x80(%rbp), %xmm0
movsd %xmm0, -0xa8(%rbp)
movsd -0x88(%rbp), %xmm0
callq 0x7400
movsd -0xa8(%rbp), %xmm3
movsd -0xa0(%rbp), %xmm1
movaps %xmm0, %xmm2
movsd -0x98(%rbp), %xmm0
movsd 0x4eb7e(%rip), %xmm4    # 0xadf48
addsd %xmm4, %xmm2
divsd %xmm2, %xmm3
movsd -0x18(%rbp), %xmm2
movsd -0x68(%rbp), %xmm4
movq %xmm2, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm2
mulsd %xmm4, %xmm2
addsd %xmm3, %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x90(%rbp)
leaq -0x20(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x697e0
movsd (%rax), %xmm0
movq -0x8(%rbp), %rax
movslq -0x44(%rbp), %rcx
movsd %xmm0, 0x8(%rax,%rcx,8)
movq -0x8(%rbp), %rax
addq $0x70, %rax
movslq -0x24(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movslq -0x44(%rbp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x5f24f
addq $0xb0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)