// File: /workspace/github_cpp_makefile_star2_O2/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 0x2519
movq 0x2acd(%rip), %rax      # 0x4fd8
movq (%rax), %rdi
leaq 0xb2f(%rip), %rsi       # 0x3044
xorl %edx, %edx
jmp 0x2545
movq %rdi, %r14
leaq 0xb33(%rip), %rsi       # 0x3056
callq 0x2150
movq %rax, -0x48(%rbp)
testq %rax, %rax
jne 0x2554
movq 0x2aa0(%rip), %rax      # 0x4fd8
movq (%rax), %rdi
leaq 0xb16(%rip), %rsi       # 0x3058
movq %r14, %rdx
xorl %eax, %eax
callq 0x2180
pushq $-0x1
popq %rax
jmp 0x2729
leaq -0x30(%rbp), %rax
andq $0x0, (%rax)
leaq -0x50(%rbp), %rax
andq $0x0, (%rax)
leaq -0x34(%rbp), %rax
andl $0x0, (%rax)
xorl %r14d, %r14d
xorl %ebx, %ebx
xorl %r13d, %r13d
xorl %r15d, %r15d
leaq -0x30(%rbp), %rdi
leaq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
callq 0x2170
cmpq $-0x1, %rax
je 0x26bb
cmpq $0x2, %rax
jl 0x25ac
movq -0x30(%rbp), %rdx
cmpb $0xd, -0x2(%rdx,%rax)
jne 0x25b6
leaq (%rdx,%rax), %rcx
addq $-0x2, %rcx
jmp 0x25cc
cmpq $0x1, %rax
jne 0x25cf
movq -0x30(%rbp), %rdx
leaq (%rdx,%rax), %rcx
decq %rcx
movzbl -0x1(%rdx,%rax), %eax
cmpl $0xd, %eax
je 0x25cc
cmpl $0xa, %eax
jne 0x25cf
movb $0x0, (%rcx)
leaq -0x68(%rbp), %rax
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq -0x30(%rbp), %rdi
leaq -0x70(%rbp), %rsi
leaq -0x34(%rbp), %rdx
callq 0x2a84
movl %eax, %r12d
testl %eax, %eax
jne 0x269d
leaq -0x70(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x2a40
movapd %xmm0, %xmm1
movsd -0x70(%rbp), %xmm0
xorpd %xmm3, %xmm3
ucomisd %xmm3, %xmm0
setae %dl
ucomisd %xmm3, %xmm1
setae %al
ucomisd %xmm0, %xmm3
seta %cl
ucomisd %xmm1, %xmm3
seta %dil
movapd %xmm1, %xmm2
mulsd %xmm0, %xmm2
ucomisd %xmm3, %xmm2
leaq 0xa4b(%rip), %rsi       # 0x3091
ja 0x264f
leaq 0xa48(%rip), %rsi       # 0x3097
movl %eax, %r8d
andb %dl, %r8b
movzbl %r8b, %r8d
addl %r8d, %r14d
movl %edi, %r8d
andb %cl, %r8b
movzbl %r8b, %r8d
addl %r8d, %ebx
andb %dl, %dil
movzbl %dil, %edx
addl %edx, %r13d
andb %cl, %al
movzbl %al, %eax
addl %eax, %r15d
leaq 0xa05(%rip), %rdi       # 0x3087
movb $0x2, %al
callq 0x2030
leaq -0x68(%rbp), %rdi
callq 0x2824
testl %r12d, %r12d
je 0x2576
jmp 0x26bb
movq 0x2934(%rip), %rax      # 0x4fd8
movq (%rax), %rdi
movq -0x30(%rbp), %rdx
leaq 0x9c0(%rip), %rsi       # 0x3072
xorl %eax, %eax
callq 0x2180
jmp 0x2689
movq 0x2916(%rip), %r12      # 0x4fd8
movq (%r12), %rdi
cvtsi2sd %r14d, %xmm1
leal (%r14,%r13), %eax
cvtsi2sd %eax, %xmm2
movapd %xmm1, %xmm0
divsd %xmm2, %xmm0
addl %r15d, %r14d
cvtsi2sd %r14d, %xmm2
divsd %xmm2, %xmm1
leaq 0x9af(%rip), %rsi       # 0x309d
movb $0x2, %al
callq 0x2180
movq (%r12), %rdi
cvtsi2sd %ebx, %xmm1
addl %ebx, %r13d
cvtsi2sd %r13d, %xmm2
movapd %xmm1, %xmm0
divsd %xmm2, %xmm0
addl %ebx, %r15d
cvtsi2sd %r15d, %xmm2
divsd %xmm2, %xmm1
leaq 0x999(%rip), %rsi       # 0x30b9
movb $0x2, %al
callq 0x2180
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x273a
movq %rax, %rbx
leaq -0x68(%rbp), %rdi
callq 0x2824
movq %rbx, %rdi
callq 0x21e0