// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rsi, -0x88(%rbp)
movq %rdi, -0x30(%rbp)
cmpl $0x0, (%rdx)
je 0x2a5a
movq %rdx, %r15
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rbx
subq (%rax), %rbx
shrq $0x5, %rbx
leaq 0x20(%rdx), %r14
movl %ebx, %eax
cvtsi2sd %rax, %xmm0
movsd %xmm0, -0x78(%rbp)
movl %ebx, %eax
movq %rax, -0x68(%rbp)
xorl %r12d, %r12d
movq 0x36dd(%rip), %r13      # 0x5fd8
movq %rdx, -0x80(%rbp)
movq %rbx, -0x60(%rbp)
movq %r14, -0x58(%rbp)
xorpd %xmm0, %xmm0
movupd %xmm0, (%r14)
movl $0x0, 0x10(%r14)
xorpd %xmm2, %xmm2
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
testl %ebx, %ebx
je 0x2a28
movq %r12, -0x70(%rbp)
movl $0x8, %r14d
movq -0x68(%rbp), %r12
xorl %ebx, %ebx
incl 0x18(%r15)
callq 0x2030
xorl %edx, %edx
divl %r12d
movl %edx, %r13d
addl %ebx, %r13d
movq -0x30(%rbp), %r15
movq (%r15), %rax
shlq $0x5, %r13
leaq (%rax,%r13), %rsi
addq $0x8, %rsi
movsd -0x8(%rsi), %xmm0
movsd %xmm0, -0x50(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x2c6e
movq (%r15), %rax
leaq (%rax,%r14), %rsi
movsd -0x8(%rax,%r14), %xmm0
leaq (%rax,%r13), %rdi
addq $0x8, %rdi
movq -0x80(%rbp), %r15
movsd %xmm0, -0x8(%rdi)
callq 0x2d10
movq -0x30(%rbp), %rax
movq (%rax), %rdi
movsd -0x50(%rbp), %xmm0
movsd %xmm0, -0x8(%rdi,%r14)
addq %r14, %rdi
leaq -0x48(%rbp), %rsi
callq 0x2d10
leaq -0x50(%rbp), %rdi
movq -0x88(%rbp), %rsi
movq %r15, %rdx
callq 0x279c
addl %eax, 0x30(%r15)
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x29e5
movq -0x38(%rbp), %rsi
subq %rdi, %rsi
callq 0x2110
incq %rbx
addq $0x20, %r14
decq %r12
jne 0x293c
movl 0x30(%r15), %eax
movsd 0x20(%r15), %xmm1
movsd 0x28(%r15), %xmm2
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
mulsd 0x1623(%rip), %xmm0     # 0x4038
movq -0x60(%rbp), %rbx
movq -0x58(%rbp), %r14
movq -0x70(%rbp), %r12
movq 0x35b0(%rip), %r13      # 0x5fd8
movq (%r13), %rdi
movsd -0x78(%rbp), %xmm3
divsd %xmm3, %xmm0
divsd %xmm3, %xmm1
divsd %xmm3, %xmm2
leaq 0x1665(%rip), %rsi      # 0x40a9
movl %r12d, %edx
movb $0x3, %al
callq 0x2160
incl %r12d
cmpl (%r15), %r12d
jb 0x2907
callq 0x2263
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x2a88
movq -0x38(%rbp), %rsi
subq %rdi, %rsi
callq 0x2110
movq %rbx, %rdi
callq 0x21d0