// File: /workspace/github_cpp_makefile_star2_O3/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 0x25a9
callq 0x2244
jmp 0x25de
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x1a99(%rip), %rsi      # 0x4052
callq 0x2120
testq %rax, %rax
jne 0x25e8
movq 0x3a0e(%rip), %rax      # 0x5fd8
movq (%rax), %rdi
leaq 0x1a80(%rip), %rsi      # 0x4054
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2160
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x26ad
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 0x2150
cmpq $-0x1, %rax
jne 0x26bc
movq 0x39b7(%rip), %rbx      # 0x5fd8
movq (%rbx), %rdi
leaq 0x1a58(%rip), %rsi      # 0x4083
movq %r12, %rdx
xorl %eax, %eax
callq 0x2160
movq (%rbx), %rdi
movl -0x2c(%rbp), %edx
leaq 0x1a54(%rip), %rsi      # 0x4096
xorl %eax, %eax
callq 0x2160
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 0x267f
movq $0x0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%r15)
jmp 0x268a
movq %r15, %rdi
movq %rbx, %rdx
callq 0x3048
movl -0x2c(%rbp), %eax
incq %r14
cmpq %rax, %r14
jb 0x265c
movq %r12, %rdi
callq 0x20f0
movq -0x38(%rbp), %rdi
testq %rdi, %rdi
je 0x26ab
callq 0x2130
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 0x26e1
movq -0x38(%rbp), %rcx
cmpb $0xd, -0x2(%rcx,%rax)
jne 0x26eb
addq %rcx, %rax
addq $-0x2, %rax
jmp 0x26fe
cmpq $0x1, %rax
jne 0x2701
movq -0x38(%rbp), %rcx
addq %rcx, %rax
decq %rax
movzbl (%rax), %ecx
cmpl $0xd, %ecx
je 0x26fe
cmpl $0xa, %ecx
jne 0x2701
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 0x31cf
movl %eax, %ebx
testl %eax, %eax
jne 0x2771
movq %r15, %rdi
movq %r13, %rsi
callq 0x2c14
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x2746
movq -0x50(%rbp), %rsi
subq %rdi, %rsi
callq 0x2110
testl %ebx, %ebx
jne 0x261a
incq %r12
leaq -0x38(%rbp), %rdi
leaq -0x70(%rbp), %rsi
movq -0x40(%rbp), %rdx
callq 0x2150
cmpq $-0x1, %rax
jne 0x26c7
jmp 0x261a
leaq -0x38(%rbp), %rdi
callq 0x2220
jmp 0x2731
movq %rax, %rbx
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x2794
movq -0x50(%rbp), %rsi
subq %rdi, %rsi
callq 0x2110
movq %rbx, %rdi
callq 0x21d0