// File: mattcha[P]sgd-svm/sgd_svm_train.cpp
// Function: train(Dataset&, Weight&, Param&)
int train(Dataset &dataset, Weight &weight, Param &param)
{
    uint32_t ins_num = dataset.ins_vec.size();

    for (uint32_t i_iter = 0; i_iter < param.iteration; ++i_iter) {
        param.right = 0;
        param.loss_hinge = 0;
        param.loss_l2 = 0;

        for (uint32_t i_ins = 0; i_ins < ins_num; ++i_ins) {
            ++ param.step;

            int r_idx = rand() % (ins_num - i_ins);
            Instance inst = dataset.ins_vec[i_ins + r_idx];
            dataset.ins_vec[i_ins + r_idx] = dataset.ins_vec[i_ins];
            dataset.ins_vec[i_ins] = inst;

            param.right += step_train(inst, weight, param);
        }
        fprintf(stderr, "iter : %-5u   accurate : %-8f  hinge loss : %-8f   l2 loss : %-8f\n", 
                i_iter, param.right*100.0/ins_num, param.loss_hinge/ins_num, param.loss_l2/ins_num);
    }

    fprintf(stderr, "finish training\n");

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rdi
callq 0x3030
movl %eax, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl (%rcx), %eax
jae 0x2b2a
movq -0x18(%rbp), %rax
movl $0x0, 0x30(%rax)
movq -0x18(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x20(%rax)
movq -0x18(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x28(%rax)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jae 0x2acb
movq -0x18(%rbp), %rax
movl 0x18(%rax), %ecx
incl %ecx
movl %ecx, 0x18(%rax)
callq 0x2040
movl -0x1c(%rbp), %ecx
movl -0x24(%rbp), %edx
subl %edx, %ecx
xorl %edx, %edx
divl %ecx
movl %edx, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movl -0x24(%rbp), %eax
movl -0x28(%rbp), %ecx
addl %ecx, %eax
movl %eax, %esi
callq 0x3050
movq %rax, %rsi
leaq -0x48(%rbp), %rdi
callq 0x3070
movq -0x8(%rbp), %rdi
movl -0x24(%rbp), %eax
movl %eax, %esi
callq 0x3050
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rdi
movl -0x24(%rbp), %eax
movl -0x28(%rbp), %ecx
addl %ecx, %eax
movl %eax, %esi
callq 0x3050
movq -0x60(%rbp), %rsi
movq %rax, %rdi
callq 0x30b0
jmp 0x2a5e
movq -0x8(%rbp), %rdi
movl -0x24(%rbp), %eax
movl %eax, %esi
callq 0x3050
movq %rax, %rdi
leaq -0x48(%rbp), %rsi
callq 0x30b0
jmp 0x2a7a
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
leaq -0x48(%rbp), %rdi
callq 0x2720
movl %eax, -0x64(%rbp)
jmp 0x2a90
movl -0x64(%rbp), %ecx
movq -0x18(%rbp), %rax
addl 0x30(%rax), %ecx
movl %ecx, 0x30(%rax)
leaq -0x48(%rbp), %rdi
callq 0x2f60
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x29e0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x2f60
jmp 0x2b4a
movq 0x8506(%rip), %rax      # 0xafd8
movq (%rax), %rdi
movl -0x20(%rbp), %edx
movq -0x18(%rbp), %rax
movl 0x30(%rax), %ecx
cvtsi2sd %rcx, %xmm0
movsd 0x4524(%rip), %xmm1     # 0x7010
mulsd %xmm1, %xmm0
movl -0x1c(%rbp), %ecx
cvtsi2sd %rcx, %xmm3
divsd %xmm3, %xmm0
movsd 0x20(%rax), %xmm1
movsd 0x28(%rax), %xmm2
divsd %xmm3, %xmm1
divsd %xmm3, %xmm2
leaq 0x456c(%rip), %rsi      # 0x7081
movb $0x3, %al
callq 0x2220
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x29a7
movq 0x84a7(%rip), %rax      # 0xafd8
movq (%rax), %rdi
leaq 0x4589(%rip), %rsi      # 0x70c4
movb $0x0, %al
callq 0x2220
xorl %eax, %eax
addq $0x70, %rsp
popq %rbp
retq
movq -0x50(%rbp), %rdi
callq 0x2290
nopw %cs:(%rax,%rax)