// File: 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
subq $0xb0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0x24a6
movq 0x8b53(%rip), %rax      # 0xafd8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
leaq 0x4b85(%rip), %rsi      # 0x7018
movb $0x0, %al
callq 0x2220
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x270a
movq -0x10(%rbp), %rdi
leaq 0x4b79(%rip), %rsi      # 0x702a
callq 0x21b0
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x24e9
movq 0x8b10(%rip), %rax      # 0xafd8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
leaq 0x4b56(%rip), %rsi      # 0x702c
movb $0x0, %al
callq 0x2220
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x270a
movq $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x28(%rbp), %rdx
leaq -0x30(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x2200
movq %rax, -0x50(%rbp)
cmpq $-0x1, %rax
je 0x2663
cmpq $0x1, -0x50(%rbp)
jle 0x255d
movq -0x30(%rbp), %rax
movq -0x50(%rbp), %rcx
subq $0x2, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xd, %eax
jne 0x255d
movq -0x30(%rbp), %rax
movq -0x50(%rbp), %rcx
subq $0x2, %rcx
movb $0x0, (%rax,%rcx)
jmp 0x25a0
cmpq $0x0, -0x50(%rbp)
jle 0x259e
movq -0x30(%rbp), %rax
movq -0x50(%rbp), %rcx
subq $0x1, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xd, %eax
je 0x258e
movq -0x30(%rbp), %rax
movq -0x50(%rbp), %rcx
subq $0x1, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
jne 0x259e
movq -0x30(%rbp), %rax
movq -0x50(%rbp), %rcx
subq $0x1, %rcx
movb $0x0, (%rax,%rcx)
jmp 0x25a0
leaq -0x70(%rbp), %rdi
movq %rdi, -0xa8(%rbp)
callq 0x2ed0
movq -0xa8(%rbp), %rsi
movq -0x30(%rbp), %rdi
leaq -0x44(%rbp), %rdx
callq 0x5bb0
movl %eax, -0x9c(%rbp)
jmp 0x25cc
movl -0x9c(%rbp), %eax
movl %eax, -0x74(%rbp)
cmpl $0x0, -0x74(%rbp)
je 0x2620
movq 0x89f6(%rip), %rax      # 0xafd8
movq (%rax), %rdi
movq -0x30(%rbp), %rdx
leaq 0x4a56(%rip), %rsi      # 0x7046
movb $0x0, %al
callq 0x2220
movl $0x2, -0x88(%rbp)
jmp 0x2639
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x2f60
jmp 0x2716
movq -0x18(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x2ef0
jmp 0x262f
movl $0x0, -0x88(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x2f60
movl -0x88(%rbp), %eax
testl %eax, %eax
je 0x2650
jmp 0x264e
jmp 0x2663
jmp 0x2652
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x2510
movq 0x896e(%rip), %rax      # 0xafd8
movq (%rax), %rdi
movq -0x40(%rbp), %rdx
leaq 0x49e3(%rip), %rsi      # 0x705b
movb $0x0, %al
callq 0x2220
movq 0x8952(%rip), %rax      # 0xafd8
movq (%rax), %rdi
movl -0x44(%rbp), %edx
leaq 0x49db(%rip), %rsi      # 0x706e
movb $0x0, %al
callq 0x2220
movq $0x0, -0x90(%rbp)
movq -0x90(%rbp), %rax
movl -0x44(%rbp), %ecx
cmpq %rcx, %rax
ja 0x26e3
movq -0x20(%rbp), %rdi
xorps %xmm0, %xmm0
movsd %xmm0, -0x98(%rbp)
leaq -0x98(%rbp), %rsi
callq 0x2f80
movq -0x90(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x90(%rbp)
jmp 0x26a5
cmpq $0x0, -0x28(%rbp)
je 0x26f3
movq -0x28(%rbp), %rdi
callq 0x2140
cmpq $0x0, -0x30(%rbp)
je 0x2703
movq -0x30(%rbp), %rdi
callq 0x21c0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xb0, %rsp
popq %rbp
retq
movq -0x80(%rbp), %rdi
callq 0x2290
nop