// File: /workspace/github_cpp_makefile_star2_O1/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 0x1f715(%rip), %rdi     # 0x2c5fd
movq %r14, %rsi
xorl %eax, %eax
callq 0x640d
leaq 0x1f720(%rip), %rdi     # 0x2c619
movl %ebx, %esi
xorl %eax, %eax
callq 0x640d
leaq 0x2b2a0(%rip), %rsi     # 0x381a9
movq %r14, %rdi
callq 0x21d0
testq %rax, %rax
je 0xd1f2
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 0xd1d2
imull $0x3e8, %ebx, %eax      # imm = 0x3E8
movl %eax, -0x38(%rbp)
leaq 0x1f7f6(%rip), %r12     # 0x2c745
movl $0x0, -0x34(%rbp)
xorl %r14d, %r14d
movq %r13, -0x40(%rbp)
movq -0x30(%rbp), %rsi
leaq 0x1f6ea(%rip), %rdi     # 0x2c652
xorl %eax, %eax
callq 0x640d
movq -0x30(%rbp), %r15
movq %r15, %rdi
movl $0x20, %esi
callq 0x2070
testq %rax, %rax
je 0xd217
incq %rax
movq %rax, %rdi
movl $0x20, %esi
callq 0x2070
testq %rax, %rax
je 0xd220
incq %rax
movq %rax, %rdi
movl $0x20, %esi
callq 0x2070
testq %rax, %rax
je 0xd229
incq %rax
movq %rax, %rdi
movl $0x20, %esi
callq 0x2070
testq %rax, %rax
je 0xd232
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 0x1f6c2(%rip), %rdi     # 0x2c6d6
movq %r15, %rsi
xorl %eax, %eax
callq 0x640d
leaq -0x248(%rbp), %r13
movq %r13, %rdi
movq %r15, %rsi
callq 0xaa74
movq %r13, %rdi
callq 0x4b7c
leaq 0x1(%rbx), %r13
movq %r13, %rdi
movl $0x3b, %esi
callq 0x2070
testq %rax, %rax
je 0xd23b
cmpb $0x62, (%r13)
jne 0xd20e
cmpb $0x6d, 0x2(%rbx)
jne 0xd20e
cmpb $0x20, 0x3(%rbx)
jne 0xd20e
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 0x1f663(%rip), %rdi     # 0x2c710
movq %r13, %rsi
xorl %eax, %eax
callq 0x640d
leaq -0x248(%rbp), %rdi
movq %r13, %rsi
leaq -0x80(%rbp), %rdx
callq 0xd25c
testl %eax, %eax
je 0xd244
movl %eax, %ebx
leaq 0x1f65b(%rip), %rdi     # 0x2c736
xorl %eax, %eax
callq 0x640d
movl %ebx, %r15d
testl %ebx, %ebx
jle 0xd108
xorl %r13d, %r13d
movq %r12, %rdi
xorl %eax, %eax
callq 0x640d
movq -0x80(%rbp,%r13,8), %rdi
callq 0xe8b3
incq %r13
cmpq %r13, %r15
jne 0xd0ec
leaq 0x1d751(%rip), %rdi     # 0x2a860
xorl %eax, %eax
callq 0x640d
leaq 0xeadd3(%rip), %rdi     # 0xf7ef0
callq 0x6207
leaq 0xeadf7(%rip), %rdi     # 0xf7f20
callq 0x6207
xorl %edi, %edi
callq 0xd32b
xorl %edi, %edi
callq 0xd337
movl -0x38(%rbp), %edi
callq 0xd305
leaq -0x570(%rbp), %rdi
leaq -0x248(%rbp), %rsi
movl $0x1, %edx
callq 0xd748
testl %ebx, %ebx
movq -0x40(%rbp), %r13
jle 0xd17c
movq -0x568(%rbp), %rax
xorl %ecx, %ecx
cmpq %rax, -0x80(%rbp,%rcx,8)
je 0xd18c
incq %rcx
cmpq %rcx, %r15
jne 0xd16d
leaq 0x1f5d1(%rip), %rdi     # 0x2c754
xorl %eax, %eax
callq 0x640d
jmp 0xd19d
leaq 0x1f5b4(%rip), %rdi     # 0x2c747
xorl %eax, %eax
callq 0x640d
incl -0x34(%rbp)
incl %r14d
leaq 0x1f5bb(%rip), %rdi     # 0x2c762
movl -0x34(%rbp), %esi
movl %r14d, %edx
xorl %eax, %eax
callq 0x640d
movq -0x48(%rbp), %rsp
leaq -0x30(%rbp), %rdi
leaq -0x50(%rbp), %rsi
movq %r13, %rdx
callq 0x2210
cmpq $-0x1, %rax
jne 0xcf5d
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 0x1f439(%rip), %rdi     # 0x2c632
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x640d
leaq 0x1f4e7(%rip), %rdi     # 0x2c6fc
jmp 0xd24b
leaq 0x1f439(%rip), %rdi     # 0x2c657
jmp 0xd24b
leaq 0x1f44d(%rip), %rdi     # 0x2c674
jmp 0xd24b
leaq 0x1f461(%rip), %rdi     # 0x2c691
jmp 0xd24b
leaq 0x1f47d(%rip), %rdi     # 0x2c6b6
jmp 0xd24b
leaq 0x1f49e(%rip), %rdi     # 0x2c6e0
jmp 0xd24b
leaq 0x1f4d1(%rip), %rdi     # 0x2c71c
xorl %eax, %eax
callq 0x2330
movl $0x1, %edi
callq 0x21f0