// File: 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
subq $0x5b0, %rsp            # imm = 0x5B0
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rsi
leaq 0x25b63(%rip), %rdi     # 0x385b0
movb $0x0, %al
callq 0x8c60
movl -0xc(%rbp), %esi
leaq 0x25b6e(%rip), %rdi     # 0x385cc
movb $0x0, %al
callq 0x8c60
movq -0x8(%rbp), %rdi
leaq 0x3200e(%rip), %rsi     # 0x44a7e
callq 0x21d0
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x12a93
leaq 0x25b5e(%rip), %rdi     # 0x385e5
movb $0x0, %al
callq 0x8c60
jmp 0x12ee1
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movq -0x18(%rbp), %rdx
leaq -0x20(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x2210
movq %rax, -0x30(%rbp)
cmpq $-0x1, %rax
je 0x12ecf
movq -0x20(%rbp), %rsi
leaq 0x25b2a(%rip), %rdi     # 0x38605
movb $0x0, %al
callq 0x8c60
movq -0x20(%rbp), %rdi
movl $0x20, %esi
callq 0x2070
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x12b13
leaq 0x25b08(%rip), %rdi     # 0x3860a
movb $0x0, %al
callq 0x8f00
movl $0x1, %edi
callq 0x21f0
movq -0x40(%rbp), %rdi
addq $0x1, %rdi
movl $0x20, %esi
callq 0x2070
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x12b48
leaq 0x25af0(%rip), %rdi     # 0x38627
movb $0x0, %al
callq 0x8f00
movl $0x1, %edi
callq 0x21f0
movq -0x40(%rbp), %rdi
addq $0x1, %rdi
movl $0x20, %esi
callq 0x2070
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x12b7d
leaq 0x25ad8(%rip), %rdi     # 0x38644
movb $0x0, %al
callq 0x8f00
movl $0x1, %edi
callq 0x21f0
movq -0x40(%rbp), %rdi
addq $0x1, %rdi
movl $0x20, %esi
callq 0x2070
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x12bb2
leaq 0x25ac8(%rip), %rdi     # 0x38669
movb $0x0, %al
callq 0x8f00
movl $0x1, %edi
callq 0x21f0
movl -0x40(%rbp), %eax
movl -0x20(%rbp), %ecx
subl %ecx, %eax
incl %eax
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %eax
movq %rsp, %rcx
movq %rcx, -0x50(%rbp)
movq %rax, %rcx
addq $0xf, %rcx
andq $-0x10, %rcx
movq %rsp, %rdi
subq %rcx, %rdi
movq %rdi, -0x5a0(%rbp)
movq %rdi, %rsp
movq %rax, -0x58(%rbp)
movq -0x20(%rbp), %rsi
movslq -0x44(%rbp), %rdx
callq 0x20b0
movq -0x5a0(%rbp), %rsi
movl -0x44(%rbp), %eax
subl $0x1, %eax
cltq
movb $0x0, (%rsi,%rax)
leaq 0x25a7a(%rip), %rdi     # 0x38689
movb $0x0, %al
callq 0x8c60
movq -0x5a0(%rbp), %rsi
leaq -0x220(%rbp), %rdi
callq 0xf3e0
leaq -0x220(%rbp), %rdi
callq 0x64e0
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
movl $0x3b, %esi
callq 0x2070
movq %rax, -0x228(%rbp)
cmpq $0x0, -0x228(%rbp)
jne 0x12c78
leaq 0x25a2c(%rip), %rdi     # 0x38693
movb $0x0, %al
callq 0x8f00
movl $0x1, %edi
callq 0x21f0
movq -0x40(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x62, %eax
jne 0x12c9e
movq -0x40(%rbp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x6d, %eax
jne 0x12c9e
movq -0x40(%rbp), %rax
movsbl 0x2(%rax), %eax
cmpl $0x20, %eax
je 0x12cb6
leaq 0x25a0a(%rip), %rdi     # 0x386af
movb $0x0, %al
callq 0x8f00
movl $0x1, %edi
callq 0x21f0
movq -0x40(%rbp), %rax
addq $0x3, %rax
movq %rax, -0x40(%rbp)
movl -0x228(%rbp), %eax
movl -0x40(%rbp), %ecx
subl %ecx, %eax
incl %eax
movl %eax, -0x22c(%rbp)
movl -0x22c(%rbp), %eax
movq %rax, %rcx
addq $0xf, %rcx
andq $-0x10, %rcx
movq %rsp, %rdi
subq %rcx, %rdi
movq %rdi, -0x5a8(%rbp)
movq %rdi, %rsp
movq %rax, -0x238(%rbp)
movq -0x40(%rbp), %rsi
movslq -0x22c(%rbp), %rdx
callq 0x20b0
movq -0x5a8(%rbp), %rsi
movl -0x22c(%rbp), %eax
subl $0x1, %eax
cltq
movb $0x0, (%rsi,%rax)
leaq 0x25999(%rip), %rdi     # 0x386c3
movb $0x0, %al
callq 0x8c60
movq -0x5a8(%rbp), %rsi
leaq -0x260(%rbp), %rdx
leaq -0x220(%rbp), %rdi
callq 0x12ef0
movl %eax, -0x264(%rbp)
cmpl $0x0, -0x264(%rbp)
jne 0x12d72
leaq 0x2596e(%rip), %rdi     # 0x386cf
movb $0x0, %al
callq 0x8f00
movl $0x1, %edi
callq 0x21f0
leaq 0x25970(%rip), %rdi     # 0x386e9
movb $0x0, %al
callq 0x8c60
movl $0x0, -0x268(%rbp)
movl -0x268(%rbp), %eax
cmpl -0x264(%rbp), %eax
jge 0x12dcb
leaq 0x25959(%rip), %rdi     # 0x386f8
movb $0x0, %al
callq 0x8c60
movslq -0x268(%rbp), %rax
movq -0x260(%rbp,%rax,8), %rdi
callq 0x14ed0
movl -0x268(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x268(%rbp)
jmp 0x12d8a
leaq 0x23ab3(%rip), %rdi     # 0x36885
movb $0x0, %al
callq 0x8c60
leaq 0xf02d0(%rip), %rdi     # 0x1030b0
callq 0x88c0
leaq 0xf02f4(%rip), %rdi     # 0x1030e0
callq 0x88c0
xorl %edi, %edi
callq 0x130b0
xorl %edi, %edi
callq 0x130e0
imull $0x3e8, -0xc(%rbp), %edi # imm = 0x3E8
callq 0x13030
leaq -0x590(%rbp), %rdi
leaq -0x220(%rbp), %rsi
movl $0x1, %edx
callq 0x13560
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
movb $0x0, -0x591(%rbp)
movl $0x0, -0x598(%rbp)
movl -0x598(%rbp), %eax
cmpl -0x264(%rbp), %eax
jge 0x12e7f
movslq -0x598(%rbp), %rax
movq -0x260(%rbp,%rax,8), %rax
cmpq -0x588(%rbp), %rax
jne 0x12e6c
movb $0x1, -0x591(%rbp)
jmp 0x12e7f
jmp 0x12e6e
movl -0x598(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x598(%rbp)
jmp 0x12e3d
testb $0x1, -0x591(%rbp)
je 0x12ea1
leaq 0x2586b(%rip), %rdi     # 0x386fa
movb $0x0, %al
callq 0x8c60
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x12eaf
leaq 0x2585f(%rip), %rdi     # 0x38707
movb $0x0, %al
callq 0x8c60
movl -0x34(%rbp), %esi
movl -0x38(%rbp), %edx
leaq 0x25859(%rip), %rdi     # 0x38715
movb $0x0, %al
callq 0x8c60
movq -0x50(%rbp), %rax
movq %rax, %rsp
jmp 0x12ab1
movq -0x20(%rbp), %rdi
callq 0x21e0
movq -0x18(%rbp), %rdi
callq 0x2170
movq %rbp, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)