// File: /workspace/github_cpp_makefile_star2_O3/mattcha[P]sgd-svm/sgd_svm_predict.cpp
// Function: predict(char const*, Weight const&)
int predict(const char* filename, const 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;

    int eval_tp = 0;
    int eval_fn = 0;
    int eval_tn = 0;
    int eval_fp = 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;
        }    

        double result = inner_product(instance, weight);
        // output format : predict_result label score 

        if (instance.y_label >= 0 && result >= 0) 
            eval_tp++;
        if (instance.y_label < 0 && result < 0)
            eval_fn++;
        if (instance.y_label >= 0 && result < 0)
            eval_tn++;
        if (instance.y_label < 0 && result >= 0)
            eval_fp++;

        printf("%s\t%f\t%f\n", (result*instance.y_label > 0)?"right":"wrong", 
                instance.y_label, result);
    }

    fprintf(stderr, " 1 recall %f, precision %f\n", 
            eval_tp/(double)(eval_tp+eval_tn), eval_tp/(double)(eval_tp+eval_fp));
    fprintf(stderr, "-1 recall %f, precision %f\n",
            eval_fn/(double)(eval_fn+eval_tn), eval_fn/(double)(eval_fn+eval_fp));

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, -0x40(%rbp)
testq %rdi, %rdi
jne 0x25fe
callq 0x2263
jmp 0x2631
movq %rdi, %r15
leaq 0xa4e(%rip), %rsi       # 0x3056
callq 0x2130
movq %rax, -0x48(%rbp)
testq %rax, %rax
jne 0x263b
movq 0x29bb(%rip), %rax      # 0x4fd8
movq (%rax), %rdi
leaq 0xa31(%rip), %rsi       # 0x3058
movq %r15, %rdx
xorl %eax, %eax
callq 0x2160
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x281c
xorl %r14d, %r14d
leaq -0x30(%rbp), %rax
movq %r14, (%rax)
leaq -0x70(%rbp), %rax
movq %r14, (%rax)
leaq -0x34(%rbp), %rax
movl $0x0, (%rax)
xorl %r15d, %r15d
xorl %r12d, %r12d
xorl %ebx, %ebx
leaq -0x30(%rbp), %rdi
leaq -0x70(%rbp), %rsi
movq -0x48(%rbp), %rdx
callq 0x2150
cmpq $-0x1, %rax
je 0x279c
cmpq $0x2, %rax
jl 0x2694
movq -0x30(%rbp), %rdx
cmpb $0xd, -0x2(%rdx,%rax)
jne 0x269e
leaq (%rdx,%rax), %rcx
addq $-0x2, %rcx
jmp 0x26b4
cmpq $0x1, %rax
jne 0x26b7
movq -0x30(%rbp), %rdx
leaq (%rdx,%rax), %rcx
decq %rcx
movzbl -0x1(%rdx,%rax), %eax
cmpl $0xd, %eax
je 0x26b4
cmpl $0xa, %eax
jne 0x26b7
movb $0x0, (%rcx)
leaq -0x60(%rbp), %rax
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq -0x30(%rbp), %rdi
leaq -0x68(%rbp), %rsi
leaq -0x34(%rbp), %rdx
callq 0x2aa7
movl %eax, %r13d
testl %eax, %eax
jne 0x2791
leaq -0x68(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x2a64
movapd %xmm0, %xmm1
movsd -0x68(%rbp), %xmm0
xorpd %xmm3, %xmm3
ucomisd %xmm3, %xmm0
setae %al
ucomisd %xmm3, %xmm1
setae %cl
movl %ecx, %edx
andb %al, %dl
movzbl %dl, %edx
addl %edx, %r14d
ucomisd %xmm0, %xmm3
seta %dl
ucomisd %xmm1, %xmm3
seta %sil
movl %esi, %edi
andb %dl, %dil
movzbl %dil, %edi
addl %edi, %r15d
andb %al, %sil
movzbl %sil, %eax
addl %eax, %r12d
andb %dl, %cl
movzbl %cl, %eax
addl %eax, %ebx
movapd %xmm1, %xmm2
mulsd %xmm0, %xmm2
ucomisd %xmm3, %xmm2
leaq 0x93f(%rip), %rsi       # 0x3097
leaq 0x932(%rip), %rax       # 0x3091
cmovaq %rax, %rsi
leaq 0x91d(%rip), %rdi       # 0x3087
movb $0x2, %al
callq 0x2030
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x2786
movq -0x50(%rbp), %rsi
subq %rdi, %rsi
callq 0x2110
testl %r13d, %r13d
je 0x265e
jmp 0x279c
leaq -0x30(%rbp), %rdi
callq 0x223f
jmp 0x2771
movq 0x2835(%rip), %r13      # 0x4fd8
movq (%r13), %rdi
xorps %xmm1, %xmm1
cvtsi2sd %r14d, %xmm1
leal (%r12,%r14), %eax
xorps %xmm2, %xmm2
cvtsi2sd %eax, %xmm2
movapd %xmm1, %xmm0
divsd %xmm2, %xmm0
addl %ebx, %r14d
xorps %xmm2, %xmm2
cvtsi2sd %r14d, %xmm2
divsd %xmm2, %xmm1
leaq 0x8c5(%rip), %rsi       # 0x309d
movb $0x2, %al
callq 0x2160
movq (%r13), %rdi
xorps %xmm1, %xmm1
cvtsi2sd %r15d, %xmm1
addl %r15d, %r12d
xorps %xmm2, %xmm2
cvtsi2sd %r12d, %xmm2
movapd %xmm1, %xmm0
divsd %xmm2, %xmm0
addl %r15d, %ebx
xorps %xmm2, %xmm2
cvtsi2sd %ebx, %xmm2
divsd %xmm2, %xmm1
leaq 0x8a6(%rip), %rsi       # 0x30b9
movb $0x2, %al
callq 0x2160
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x282d
movq %rax, %rbx
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x2845
movq -0x50(%rbp), %rsi
subq %rdi, %rsi
callq 0x2110
movq %rbx, %rdi
callq 0x21c0