// File: /workspace/github_cpp_makefile_star2_O1/mattcha[P]sgd-svm/sgd_svm_train.cpp
// Function: load_data(char const*, Dataset&, Weight&)
int load_data(const char* filename,
        Dataset &dataset,
        Weight &weight)
{
    if (filename == NULL) {
        fprintf(stderr, "filename is null\n", filename);
        return -1;
    }

    FILE *fp = fopen(filename, "r");
    if (fp == NULL) {
        fprintf(stderr, "can't open filename [%s]\n", filename);
        return -1;
    }

    char *buff = NULL;
    size_t buff_len = 0;

    size_t lineno = 0;
    uint32_t max_feature = 0;

    for (ssize_t read = 0; 
            (read = getline(&buff, &buff_len, fp)) != -1; 
            ++lineno) {
        if(read > 1 && buff[read-2] == '\r') {
            buff[read-2] = '\0';
        } else if (read > 0 && (buff[read-1] == '\r' || buff[read-1] == '\n')) {
            buff[read-1] = '\0';
        }

        Instance instance;
        int ret = load_line(buff, instance, max_feature);
        if (ret != 0) {
            fprintf(stderr, "load line [%s] fail\n", buff);
            break;
        }
        dataset.ins_vec.push_back(instance);
    }

    fprintf(stderr, "Line number: [%u]\n", lineno);
    fprintf(stderr, "Max feature: [%u]\n", max_feature);

    for (size_t i = 0; i <= max_feature; ++i) {
        weight.w_vec.push_back(0.0);
    }

    if (fp)
        fclose(fp);
    if (buff)
        free(buff);

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
testq %rdi, %rdi
jne 0x25b9
callq 0x2254
jmp 0x25ee
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x1a89(%rip), %rsi      # 0x4052
callq 0x2130
testq %rax, %rax
jne 0x25f8
movq 0x39fe(%rip), %rax      # 0x5fd8
movq (%rax), %rdi
leaq 0x1a70(%rip), %rsi      # 0x4054
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2170
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x26bd
movq %r14, -0x48(%rbp)
xorl %r12d, %r12d
leaq -0x38(%rbp), %rdi
movq %r12, (%rdi)
leaq -0x70(%rbp), %rsi
movq %r12, (%rsi)
movl $0x0, -0x2c(%rbp)
movq %rax, -0x40(%rbp)
movq %rax, %rdx
callq 0x2160
cmpq $-0x1, %rax
jne 0x26cc
movq 0x39a7(%rip), %rbx      # 0x5fd8
movq (%rbx), %rdi
leaq 0x1a48(%rip), %rsi      # 0x4083
movq %r12, %rdx
xorl %eax, %eax
callq 0x2170
movq (%rbx), %rdi
movl -0x2c(%rbp), %edx
leaq 0x1a44(%rip), %rsi      # 0x4096
xorl %eax, %eax
callq 0x2170
movq $-0x1, %r14
leaq -0x68(%rbp), %rbx
movq -0x48(%rbp), %r15
movq -0x40(%rbp), %r12
movq $0x0, -0x68(%rbp)
movq 0x8(%r15), %rsi
cmpq 0x10(%r15), %rsi
je 0x268f
movq $0x0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%r15)
jmp 0x269a
movq %r15, %rdi
movq %rbx, %rdx
callq 0x3090
movl -0x2c(%rbp), %eax
incq %r14
cmpq %rax, %r14
jb 0x266c
movq %r12, %rdi
callq 0x2100
movq -0x38(%rbp), %rdi
testq %rdi, %rdi
je 0x26bb
callq 0x2140
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq -0x60(%rbp), %r14
xorl %r12d, %r12d
leaq -0x68(%rbp), %r13
cmpq $0x2, %rax
jl 0x26f1
movq -0x38(%rbp), %rcx
cmpb $0xd, -0x2(%rcx,%rax)
jne 0x26f1
addq %rcx, %rax
addq $-0x2, %rax
jmp 0x270d
testq %rax, %rax
jle 0x2710
movq -0x38(%rbp), %rcx
addq %rcx, %rax
decq %rax
movzbl (%rax), %ecx
cmpl $0xd, %ecx
je 0x270d
cmpl $0xa, %ecx
jne 0x2710
movb $0x0, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, (%r14)
movq $0x0, 0x10(%r14)
movq -0x38(%rbp), %rdi
movq %r13, %rsi
leaq -0x2c(%rbp), %rdx
callq 0x3217
movl %eax, %ebx
testl %eax, %eax
jne 0x2780
movq %r15, %rdi
movq %r13, %rsi
callq 0x2c32
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x2755
movq -0x50(%rbp), %rsi
subq %rdi, %rsi
callq 0x2120
testl %ebx, %ebx
jne 0x262a
incq %r12
leaq -0x38(%rbp), %rdi
leaq -0x70(%rbp), %rsi
movq -0x40(%rbp), %rdx
callq 0x2160
cmpq $-0x1, %rax
jne 0x26d7
jmp 0x262a
leaq -0x38(%rbp), %rdi
callq 0x2230
jmp 0x2740
movq %rax, %rbx
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x27a3
movq -0x50(%rbp), %rsi
subq %rdi, %rsi
callq 0x2120
movq %rbx, %rdi
callq 0x21e0