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

  const double w = feature->w;
  const double grad = feature->grad_w[id];
  const double weight_decay = w - 1.0;
  const double gradSq = grad * grad;

  const double first_moment_w = beta * feature->first_moment_w + (1.0 - beta) * grad;
  const double second_moment_w = gamma * feature->second_moment_w + (1.0 - gamma) * gradSq;
  feature->first_moment_w = first_moment_w;
  feature->second_moment_w = second_moment_w;

  const double first_hat = first_moment_w / (1.0 - beta);
  const double second_hat = second_moment_w / (1.0 - gamma);

  const double newParam = w + alpha * (first_hat / (sqrt(second_hat) + epsilon) - lambda * weight_decay);

  feature->w = std::max(minW, newParam);
  feature->grad_w[id] = 0.0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movsd %xmm0, -0x10(%rbp)
movsd %xmm1, -0x18(%rbp)
movsd %xmm2, -0x20(%rbp)
movl %esi, -0x24(%rbp)
movsd 0x4ef4f(%rip), %xmm0    # 0xadf68
movsd %xmm0, -0x30(%rbp)
movsd 0x4ef3a(%rip), %xmm0    # 0xadf60
movsd %xmm0, -0x38(%rbp)
movsd 0x4ef15(%rip), %xmm0    # 0xadf48
movsd %xmm0, -0x40(%rbp)
movq -0x8(%rbp), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x48(%rbp)
movq -0x8(%rbp), %rax
movslq -0x24(%rbp), %rcx
movsd 0x30(%rax,%rcx,8), %xmm0
movsd %xmm0, -0x50(%rbp)
movsd -0x48(%rbp), %xmm0
movsd 0x4cfa3(%rip), %xmm1    # 0xac008
subsd %xmm1, %xmm0
movsd %xmm0, -0x58(%rbp)
movsd -0x50(%rbp), %xmm0
mulsd -0x50(%rbp), %xmm0
movsd %xmm0, -0x60(%rbp)
movq -0x8(%rbp), %rax
movsd 0x1b0(%rax), %xmm0
movsd 0x4eec7(%rip), %xmm1    # 0xadf58
mulsd -0x50(%rbp), %xmm1
movsd 0x4eeca(%rip), %xmm2    # 0xadf68
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x68(%rbp)
movq -0x8(%rbp), %rax
movsd 0x1b8(%rax), %xmm0
movsd 0x4ee91(%rip), %xmm1    # 0xadf50
mulsd -0x60(%rbp), %xmm1
movsd 0x4ee94(%rip), %xmm2    # 0xadf60
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x70(%rbp)
movsd -0x68(%rbp), %xmm0
movq -0x8(%rbp), %rax
movsd %xmm0, 0x1b0(%rax)
movsd -0x70(%rbp), %xmm0
movq -0x8(%rbp), %rax
movsd %xmm0, 0x1b8(%rax)
movsd -0x68(%rbp), %xmm0
movsd 0x4ee50(%rip), %xmm1    # 0xadf58
divsd %xmm1, %xmm0
movsd %xmm0, -0x78(%rbp)
movsd -0x70(%rbp), %xmm0
movsd 0x4ee32(%rip), %xmm1    # 0xadf50
divsd %xmm1, %xmm0
movsd %xmm0, -0x80(%rbp)
movsd -0x48(%rbp), %xmm0
movsd %xmm0, -0x98(%rbp)
movsd -0x10(%rbp), %xmm0
movsd %xmm0, -0x90(%rbp)
movsd -0x78(%rbp), %xmm0
movsd %xmm0, -0xa0(%rbp)
movsd -0x80(%rbp), %xmm0
callq 0x7400
movsd -0xa0(%rbp), %xmm3
movsd -0x98(%rbp), %xmm1
movaps %xmm0, %xmm2
movsd -0x90(%rbp), %xmm0
movsd 0x4edcd(%rip), %xmm4    # 0xadf48
addsd %xmm4, %xmm2
divsd %xmm2, %xmm3
movsd -0x18(%rbp), %xmm2
movsd -0x58(%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, -0x88(%rbp)
leaq -0x20(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x697e0
movsd (%rax), %xmm0
movq -0x8(%rbp), %rax
movsd %xmm0, (%rax)
movq -0x8(%rbp), %rax
movslq -0x24(%rbp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, 0x30(%rax,%rcx,8)
addq $0xa0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)