// File: /workspace/github_cpp_makefile_star2_O2/jswaff[P]prophet3/src/suites.cpp
// Function: test_suite(char*, int)
void test_suite(char *tsfile,int32 tstime) {
    print("\nprocessing test suite: %s\n",tsfile);
    print("seconds per problem: %d\n",tstime);

    FILE *f = fopen(tsfile,"r");
    if (!f) {
        print("couldn't open test suite file.\n");
        return;
    }

    char *line = 0;
    size_t len = 0;
    ssize_t read;
    int32 num_correct = 0, num_total = 0;

    while((read = getline(&line,&len,f)) != -1) {
        print("\n\n%s",line);

        // this is EPD format, but the first four fields are the same
        // as FEN so we can use our FEN parser.
        char *p = strchr(line,' '); // pieces
        if (!p) { error("parse error getting pieces.\n"); exit(1); }
        p = strchr(p+1,' ');
        if (!p) { error("parse error getting player.\n"); exit(1); }
        p = strchr(p+1,' ');
        if (!p) { error("parse error getting casting rights.\n"); exit(1); }
        p = strchr(p+1,' ');
        if (!p) { error("parse error getting ep square.\n"); exit(1); }

        // create the FEN to give to the parser
        int fen_length = p-line+1;
        char fen[fen_length];
        strncpy(fen,line,fen_length);
        fen[fen_length-1] = 0;
        print("\nfen: %s\n",fen);

        position pos;
        set_pos(&pos,fen);
        draw_board(&pos);

        // now parse the correct move(s)
        p++; // advance past space
        char *p2 = strchr(p,';');
        if (!p2) { error("parse error getting moves.\n"); exit(1); }
        if (*p != 'b' || *(p+1) != 'm' || *(p+2) != ' ') { error("expected bm marker\n"); exit(1); }
        p += 3;
        int moves_length = p2 - p + 1;
        char str_moves[moves_length];
        strncpy(str_moves,p,moves_length);
        str_moves[moves_length-1] = 0;
        print("\nmoves: %s\n",str_moves);

        move ts_moves[5];
        int32 n = get_ts_moves(&pos,str_moves,ts_moves);
        if (n==0) {
            error("couldn't parse any moves!");
            exit(1);
        } else {
            print("parsed moves:\n");
            for (int i=0;i<n;i++) {
                print("\t");
                print_move(ts_moves[i]);
            }
            print("\n");
        }

        clear_hash_table(&htbl);
        clear_hash_table(&phtbl);

        set_abort_iterator(false);
        set_ponder_mode(false);
        set_max_time(tstime*1000);
        move_line pv = iterate(&pos,true);
        num_total++;

        // did we get it right?
        bool correct = false;
        for (int i=0;i<n;i++) {
            if (ts_moves[i]==pv.mv[0]) {
                correct = true;
                break;
            }
        }
        if (correct) {
            print("\nCorrect! - ");
            num_correct++;
        } else {
            print("\nIncorrect - ");
        }
        print("current score: %d / %d\n",num_correct,num_total);
    }

    free(line);
    fclose(f);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x548, %rsp            # imm = 0x548
movl %esi, %ebx
movq %rdi, %r14
leaq 0x1e135(%rip), %rdi     # 0x2a62d
movq %r14, %rsi
xorl %eax, %eax
callq 0x5fdd
leaq 0x1e140(%rip), %rdi     # 0x2a649
movl %ebx, %esi
xorl %eax, %eax
callq 0x5fdd
leaq 0x29dd4(%rip), %rsi     # 0x362ed
movq %r14, %rdi
callq 0x21d0
movq %rax, -0x48(%rbp)
testq %rax, %rax
je 0xc7fd
leaq -0x38(%rbp), %rax
andq $0x0, (%rax)
leaq -0x58(%rbp), %rax
andq $0x0, (%rax)
imull $0x3e8, %ebx, %eax      # imm = 0x3E8
movl %eax, -0x3c(%rbp)
pushq $0x20
popq %rbx
leaq 0x1e224(%rip), %r15     # 0x2a775
xorl %r12d, %r12d
xorl %r13d, %r13d
leaq -0x38(%rbp), %rdi
leaq -0x58(%rbp), %rsi
movq -0x48(%rbp), %rdx
callq 0x2210
movq -0x38(%rbp), %rsi
cmpq $-0x1, %rax
je 0xc7dd
leaq 0x1e105(%rip), %rdi     # 0x2a682
xorl %eax, %eax
callq 0x5fdd
movq -0x38(%rbp), %r14
movq %r14, %rdi
movl %ebx, %esi
callq 0x2070
testq %rax, %rax
je 0xc834
incq %rax
movq %rax, %rdi
movl %ebx, %esi
callq 0x2070
testq %rax, %rax
je 0xc83d
movl %r12d, -0x40(%rbp)
incq %rax
movq %rax, %rdi
movl %ebx, %esi
callq 0x2070
testq %rax, %rax
je 0xc846
movl %r13d, -0x2c(%rbp)
incq %rax
movq %rax, %rdi
movl %ebx, %esi
callq 0x2070
testq %rax, %rax
je 0xc84f
movq %rax, %r12
movq %rsp, -0x50(%rbp)
movq %rax, %r13
subq %r14, %r13
leal 0x1(%r13), %eax
leaq 0xf(%rax), %rcx
andq $-0x10, %rcx
movq %rsp, %rbx
subq %rcx, %rbx
movq %rbx, %rsp
movq -0x38(%rbp), %rsi
movslq %eax, %rdx
movq %rbx, %rdi
callq 0x20b0
movslq %r13d, %rax
movb $0x0, (%rax,%rbx)
leaq 0x1e0e2(%rip), %rdi     # 0x2a706
movq %rbx, %rsi
xorl %eax, %eax
callq 0x5fdd
leaq -0x248(%rbp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0xa1b8
movq %r14, %rdi
callq 0x4938
leaq 0x1(%r12), %rbx
movq %rbx, %rdi
pushq $0x3b
popq %rsi
callq 0x2070
testq %rax, %rax
je 0xc822
cmpb $0x62, (%rbx)
jne 0xc819
cmpb $0x6d, 0x2(%r12)
jne 0xc819
cmpb $0x20, 0x3(%r12)
jne 0xc819
movq %rax, %r14
addq $0x4, %r12
subq %r12, %r14
leal 0x1(%r14), %eax
leaq 0xf(%rax), %rcx
andq $-0x10, %rcx
movq %rsp, %rbx
subq %rcx, %rbx
movq %rbx, %rsp
movslq %eax, %rdx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x20b0
movslq %r14d, %rax
movb $0x0, (%rax,%rbx)
leaq 0x1e083(%rip), %rdi     # 0x2a740
movq %rbx, %rsi
xorl %eax, %eax
callq 0x5fdd
leaq -0x248(%rbp), %rdi
movq %rbx, %rsi
leaq -0x80(%rbp), %rdx
callq 0xc865
testl %eax, %eax
je 0xc82b
movl %eax, %r13d
leaq 0x1e07a(%rip), %rdi     # 0x2a766
xorl %eax, %eax
callq 0x5fdd
testl %r13d, %r13d
movl $0x0, %eax
cmovlel %eax, %r13d
xorl %ebx, %ebx
movl -0x40(%rbp), %r12d
cmpq %rbx, %r13
je 0xc723
movq %r15, %rdi
xorl %eax, %eax
callq 0x5fdd
movq -0x80(%rbp,%rbx,8), %rdi
callq 0xdc25
incq %rbx
jmp 0xc705
leaq 0x1c166(%rip), %rdi     # 0x28890
xorl %eax, %eax
callq 0x5fdd
leaq 0xee7b8(%rip), %rdi     # 0xfaef0
callq 0x5dd9
leaq 0xee7dc(%rip), %rdi     # 0xfaf20
callq 0x5dd9
xorl %edi, %edi
callq 0xc917
xorl %edi, %edi
callq 0xc923
movl -0x3c(%rbp), %edi
callq 0xc8f1
leaq -0x570(%rbp), %rdi
leaq -0x248(%rbp), %rsi
pushq $0x1
popq %rdx
callq 0xcd14
incl %r12d
movq -0x568(%rbp), %rax
xorl %ecx, %ecx
pushq $0x20
popq %rbx
cmpq %rcx, %r13
je 0xc7ae
leaq 0x1(%rcx), %rdx
cmpq %rax, -0x80(%rbp,%rcx,8)
movq %rdx, %rcx
jne 0xc784
leaq 0x1dfd9(%rip), %rdi     # 0x2a777
xorl %eax, %eax
callq 0x5fdd
movl -0x2c(%rbp), %r13d
incl %r13d
jmp 0xc7c0
leaq 0x1dfcf(%rip), %rdi     # 0x2a784
xorl %eax, %eax
callq 0x5fdd
movl -0x2c(%rbp), %r13d
leaq 0x1dfcb(%rip), %rdi     # 0x2a792
movl %r13d, %esi
movl %r12d, %edx
xorl %eax, %eax
callq 0x5fdd
movq -0x50(%rbp), %rsp
jmp 0xc557
movq %rsi, %rdi
callq 0x21e0
movq -0x48(%rbp), %rdi
callq 0x2160
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1de5e(%rip), %rdi     # 0x2a662
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5fdd
leaq 0x1df0c(%rip), %rdi     # 0x2a72c
jmp 0xc856
leaq 0x1dee7(%rip), %rdi     # 0x2a710
jmp 0xc856
leaq 0x1df1a(%rip), %rdi     # 0x2a74c
jmp 0xc856
leaq 0x1de4c(%rip), %rdi     # 0x2a687
jmp 0xc856
leaq 0x1de60(%rip), %rdi     # 0x2a6a4
jmp 0xc856
leaq 0x1de74(%rip), %rdi     # 0x2a6c1
jmp 0xc856
leaq 0x1de90(%rip), %rdi     # 0x2a6e6
xorl %eax, %eax
callq 0x622a
pushq $0x1
popq %rdi
callq 0x21f0