// File: /workspace/github_cpp_makefile_star2_O2/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 0x23a7
movq 0x3c3f(%rip), %rax      # 0x5fd8
movq (%rax), %rdi
leaq 0x1c9d(%rip), %rsi      # 0x4040
xorl %edx, %edx
jmp 0x23d9
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x1c9b(%rip), %rsi      # 0x4052
callq 0x2120
movq %rax, -0x40(%rbp)
testq %rax, %rax
jne 0x23e8
movq 0x3c0c(%rip), %rax      # 0x5fd8
movq (%rax), %rdi
leaq 0x1c7e(%rip), %rsi      # 0x4054
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2160
pushq $-0x1
popq %rax
jmp 0x2533
movq %r14, -0x48(%rbp)
leaq -0x38(%rbp), %rax
andq $0x0, (%rax)
leaq -0x50(%rbp), %rax
andq $0x0, (%rax)
leaq -0x2c(%rbp), %rax
andl $0x0, (%rax)
leaq -0x68(%rbp), %r13
leaq -0x70(%rbp), %r12
xorl %ebx, %ebx
leaq -0x38(%rbp), %rdi
leaq -0x50(%rbp), %rsi
movq -0x40(%rbp), %rdx
callq 0x2150
cmpq $-0x1, %rax
je 0x24c8
cmpq $0x2, %rax
jl 0x2443
movq -0x38(%rbp), %rdx
cmpb $0xd, -0x2(%rdx,%rax)
jne 0x244d
leaq (%rdx,%rax), %rcx
addq $-0x2, %rcx
jmp 0x2463
cmpq $0x1, %rax
jne 0x2466
movq -0x38(%rbp), %rdx
leaq (%rdx,%rax), %rcx
decq %rcx
movzbl -0x1(%rdx,%rax), %eax
cmpl $0xd, %eax
je 0x2463
cmpl $0xa, %eax
jne 0x2466
movb $0x0, (%rcx)
xorps %xmm0, %xmm0
movups %xmm0, (%r13)
andq $0x0, 0x10(%r13)
movq -0x38(%rbp), %rdi
movq %r12, %rsi
leaq -0x2c(%rbp), %rdx
callq 0x31d4
movl %eax, %r14d
testl %eax, %eax
jne 0x24aa
movq %r15, %rdi
movq %r12, %rsi
callq 0x2a58
movq %r13, %rdi
callq 0x2ae4
testl %r14d, %r14d
jne 0x24c8
incq %rbx
jmp 0x240d
movq 0x3b27(%rip), %rax      # 0x5fd8
movq (%rax), %rdi
movq -0x38(%rbp), %rdx
leaq 0x1baf(%rip), %rsi      # 0x406e
xorl %eax, %eax
callq 0x2160
jmp 0x2495
movq 0x3b09(%rip), %r15      # 0x5fd8
movq (%r15), %rdi
leaq 0x1baa(%rip), %rsi      # 0x4083
xorl %r14d, %r14d
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2160
movq (%r15), %rdi
movl -0x2c(%rbp), %edx
leaq 0x1ba3(%rip), %rsi      # 0x4096
xorl %eax, %eax
callq 0x2160
leaq -0x70(%rbp), %rbx
movq -0x48(%rbp), %r15
movl -0x2c(%rbp), %eax
cmpq %rax, %r14
ja 0x251f
andq $0x0, -0x70(%rbp)
movq %r15, %rdi
movq %rbx, %rsi
callq 0x2ff0
incq %r14
jmp 0x2502
movq -0x40(%rbp), %rdi
callq 0x20f0
movq -0x38(%rbp), %rdi
callq 0x2130
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq %r13, %rdi
callq 0x2ae4
movq %rbx, %rdi
callq 0x21d0