// File: /workspace/github_cpp_makefile_star2_O1/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 0x2a78
movq %rdx, %rbx
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rcx
subq (%rax), %rcx
shrq $0x5, %rcx
leaq 0x20(%rdx), %rax
movq %rax, -0x68(%rbp)
movl %ecx, %eax
cvtsi2sd %rax, %xmm0
movsd %xmm0, -0x60(%rbp)
movq %rcx, -0x70(%rbp)
movl %ecx, %eax
movq %rax, -0x58(%rbp)
xorl %r14d, %r14d
movq %rdx, -0x80(%rbp)
movq -0x30(%rbp), %r15
movq %r14, -0x78(%rbp)
movq -0x68(%rbp), %rax
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movl $0x0, 0x10(%rax)
cmpl $0x0, -0x70(%rbp)
je 0x2a1f
movl $0x8, %r13d
movq -0x58(%rbp), %r14
xorl %r12d, %r12d
incl 0x18(%rbx)
callq 0x2030
xorl %edx, %edx
divl %r14d
leal (%r12,%rdx), %ebx
movq (%r15), %rax
shlq $0x5, %rbx
leaq (%rax,%rbx), %rsi
addq $0x8, %rsi
movsd -0x8(%rsi), %xmm0
movsd %xmm0, -0x50(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x2c8c
movq (%r15), %rax
leaq (%rax,%r13), %rsi
movsd -0x8(%rax,%r13), %xmm0
leaq (%rax,%rbx), %rdi
addq $0x8, %rdi
movq -0x80(%rbp), %rbx
movsd %xmm0, -0x8(%rdi)
callq 0x2d2e
movq -0x30(%rbp), %rax
movq (%rax), %rdi
movsd -0x50(%rbp), %xmm0
movsd %xmm0, -0x8(%rdi,%r13)
addq %r13, %rdi
leaq -0x48(%rbp), %rsi
callq 0x2d2e
leaq -0x50(%rbp), %rdi
movq -0x88(%rbp), %rsi
movq %rbx, %rdx
callq 0x27ab
addl %eax, 0x30(%rbx)
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x2a0f
movq -0x38(%rbp), %rsi
subq %rdi, %rsi
callq 0x2120
incq %r12
addq $0x20, %r13
decq %r14
jne 0x296e
movq 0x35b2(%rip), %rax      # 0x5fd8
movq (%rax), %rdi
movl 0x30(%rbx), %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
mulsd 0x15fc(%rip), %xmm0     # 0x4038
movsd -0x60(%rbp), %xmm3
divsd %xmm3, %xmm0
movsd 0x20(%rbx), %xmm1
movsd 0x28(%rbx), %xmm2
divsd %xmm3, %xmm1
divsd %xmm3, %xmm2
leaq 0x164b(%rip), %rsi      # 0x40a9
movq -0x78(%rbp), %r14
movl %r14d, %edx
movb $0x3, %al
callq 0x2170
incl %r14d
cmpl (%rbx), %r14d
jb 0x2940
callq 0x2273
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 0x2aa6
movq -0x38(%rbp), %rsi
subq %rdi, %rsi
callq 0x2120
movq %rbx, %rdi
callq 0x21e0