// File: /workspace/github_cpp_makefile_star2_O3/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 0x1f7a5(%rip), %rdi     # 0x2c67d
movq %r14, %rsi
xorl %eax, %eax
callq 0x64b9
leaq 0x1f7b0(%rip), %rdi     # 0x2c699
movl %ebx, %esi
xorl %eax, %eax
callq 0x64b9
leaq 0x2b330(%rip), %rsi     # 0x38229
movq %r14, %rdi
callq 0x21d0
testq %rax, %rax
je 0xd1e2
movq %rax, %r13
xorl %eax, %eax
leaq -0x30(%rbp), %rdi
movq %rax, (%rdi)
leaq -0x50(%rbp), %rsi
movq %rax, (%rsi)
movq %r13, %rdx
callq 0x2210
cmpq $-0x1, %rax
je 0xd1c2
imull $0x3e8, %ebx, %eax      # imm = 0x3E8
movl %eax, -0x38(%rbp)
leaq 0x1f886(%rip), %r12     # 0x2c7c5
movl $0x0, -0x34(%rbp)
xorl %r14d, %r14d
movq %r13, -0x40(%rbp)
movq -0x30(%rbp), %rsi
leaq 0x1f77a(%rip), %rdi     # 0x2c6d2
xorl %eax, %eax
callq 0x64b9
movq -0x30(%rbp), %r15
movq %r15, %rdi
movl $0x20, %esi
callq 0x2070
testq %rax, %rax
je 0xd207
incq %rax
movq %rax, %rdi
movl $0x20, %esi
callq 0x2070
testq %rax, %rax
je 0xd210
incq %rax
movq %rax, %rdi
movl $0x20, %esi
callq 0x2070
testq %rax, %rax
je 0xd219
incq %rax
movq %rax, %rdi
movl $0x20, %esi
callq 0x2070
testq %rax, %rax
je 0xd222
movq %rax, %rbx
movq %rsp, -0x48(%rbp)
movq %rax, %r13
subq %r15, %r13
leal 0x1(%r13), %eax
leaq 0xf(%rax), %rcx
andq $-0x10, %rcx
movq %rsp, %r15
subq %rcx, %r15
movq %r15, %rsp
movq -0x30(%rbp), %rsi
movslq %eax, %rdx
movq %r15, %rdi
callq 0x20b0
movslq %r13d, %rax
movb $0x0, (%rax,%r15)
leaq 0x1f752(%rip), %rdi     # 0x2c756
movq %r15, %rsi
xorl %eax, %eax
callq 0x64b9
leaq -0x248(%rbp), %r13
movq %r13, %rdi
movq %r15, %rsi
callq 0xaa82
movq %r13, %rdi
callq 0x4bd8
leaq 0x1(%rbx), %r13
movq %r13, %rdi
movl $0x3b, %esi
callq 0x2070
testq %rax, %rax
je 0xd22b
cmpb $0x62, (%r13)
jne 0xd1fe
cmpb $0x6d, 0x2(%rbx)
jne 0xd1fe
cmpb $0x20, 0x3(%rbx)
jne 0xd1fe
movq %rax, %r15
addq $0x4, %rbx
subq %rbx, %r15
leal 0x1(%r15), %eax
leaq 0xf(%rax), %rcx
andq $-0x10, %rcx
movq %rsp, %r13
subq %rcx, %r13
movq %r13, %rsp
movslq %eax, %rdx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x20b0
movslq %r15d, %rax
movb $0x0, (%rax,%r13)
leaq 0x1f6f3(%rip), %rdi     # 0x2c790
movq %r13, %rsi
xorl %eax, %eax
callq 0x64b9
leaq -0x248(%rbp), %rdi
movq %r13, %rsi
leaq -0x80(%rbp), %rdx
callq 0xd24c
testl %eax, %eax
je 0xd234
movl %eax, %ebx
leaq 0x1f6eb(%rip), %rdi     # 0x2c7b6
xorl %eax, %eax
callq 0x64b9
movl %ebx, %r15d
testl %ebx, %ebx
jle 0xd0f8
xorl %r13d, %r13d
movq %r12, %rdi
xorl %eax, %eax
callq 0x64b9
movq -0x80(%rbp,%r13,8), %rdi
callq 0xe7f9
incq %r13
cmpq %r13, %r15
jne 0xd0dc
leaq 0x1d7e1(%rip), %rdi     # 0x2a8e0
xorl %eax, %eax
callq 0x64b9
leaq 0xeade3(%rip), %rdi     # 0xf7ef0
callq 0x62b6
leaq 0xeae07(%rip), %rdi     # 0xf7f20
callq 0x62b6
xorl %edi, %edi
callq 0xd31b
xorl %edi, %edi
callq 0xd327
movl -0x38(%rbp), %edi
callq 0xd2f5
leaq -0x570(%rbp), %rdi
leaq -0x248(%rbp), %rsi
movl $0x1, %edx
callq 0xd713
testl %ebx, %ebx
movq -0x40(%rbp), %r13
jle 0xd16c
movq -0x568(%rbp), %rax
xorl %ecx, %ecx
cmpq %rax, -0x80(%rbp,%rcx,8)
je 0xd17c
incq %rcx
cmpq %rcx, %r15
jne 0xd15d
leaq 0x1f661(%rip), %rdi     # 0x2c7d4
xorl %eax, %eax
callq 0x64b9
jmp 0xd18d
leaq 0x1f644(%rip), %rdi     # 0x2c7c7
xorl %eax, %eax
callq 0x64b9
incl -0x34(%rbp)
incl %r14d
leaq 0x1f64b(%rip), %rdi     # 0x2c7e2
movl -0x34(%rbp), %esi
movl %r14d, %edx
xorl %eax, %eax
callq 0x64b9
movq -0x48(%rbp), %rsp
leaq -0x30(%rbp), %rdi
leaq -0x50(%rbp), %rsi
movq %r13, %rdx
callq 0x2210
cmpq $-0x1, %rax
jne 0xcf4d
movq -0x30(%rbp), %rdi
callq 0x21e0
movq %r13, %rdi
callq 0x2170
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1f4c9(%rip), %rdi     # 0x2c6b2
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x64b9
leaq 0x1f577(%rip), %rdi     # 0x2c77c
jmp 0xd23b
leaq 0x1f4c9(%rip), %rdi     # 0x2c6d7
jmp 0xd23b
leaq 0x1f4dd(%rip), %rdi     # 0x2c6f4
jmp 0xd23b
leaq 0x1f4f1(%rip), %rdi     # 0x2c711
jmp 0xd23b
leaq 0x1f50d(%rip), %rdi     # 0x2c736
jmp 0xd23b
leaq 0x1f52e(%rip), %rdi     # 0x2c760
jmp 0xd23b
leaq 0x1f561(%rip), %rdi     # 0x2c79c
xorl %eax, %eax
callq 0x2330
movl $0x1, %edi
callq 0x21f0