// File: /workspace/github_cpp_makefile_star2_O2/jswaff[P]prophet3/src/perft.cpp
// Function: perft_test_slow(int)
void perft_test_slow(int32 depth) {
    print("perft_test_slow, depth=%d\n",depth);
    if (depth < 1 || depth > 6) {
        error("valid perft_test_slow depths are 1-6.\n");
        return;
    }

    static const char filename[] = "../data/perft-random.epd";
    FILE *file = fopen (filename,"r");
    if (file == NULL) {
        perror (filename); // why didn't the file open?
        return;
    }

    char id_line[255],epd_line[255],node_count_line[6][255];

    position pos;
    uint64 total_nps = 0;
    int32 num_probs = 0;
    while (get_line(id_line,"id ",file) && get_line(epd_line,"epd ",file)
        && get_line(node_count_line[0],"perft 1",file) && get_line(node_count_line[1],"perft 2",file)
        && get_line(node_count_line[2],"perft 3",file) && get_line(node_count_line[3],"perft 4",file)
        && get_line(node_count_line[4],"perft 5",file) && get_line(node_count_line[5],"perft 6",file)
        && num_probs < 100)
    {
        print("======================================================================\n");
        print("%s\n",id_line+3);
        print("%s\n",epd_line+4);
        print("======================================================================\n");

        // remove newline from epd
        char *p=strchr(epd_line,'\n');
        if (p != NULL) *p = '\0';

        set_pos(&pos,epd_line+4);
        draw_board(&pos);
        uint64 expected = strtoull(node_count_line[depth-1]+8,NULL,10);

        int32 start_time = milli_timer();
        uint64 actual = perft(&pos,depth);
        int32 elapsed = milli_timer() - start_time + 1; // div by 0

        print("\nexpected (%d): %llu, actual: %llu\n",depth,expected,actual);
        if (actual != expected) {
            exit(1);
        }

        uint64 nps = actual * 1000 / elapsed;
        total_nps += nps;
        print("nps: %lu\n\n",nps);

        num_probs++;
    }
    fclose (file);

    print("average NPS: %lu\n",total_nps / num_probs);
    print("... perft_test_slow complete!\n");
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x9f8, %rsp            # imm = 0x9F8
movl %edi, %ebx
leaq 0x1fe11(%rip), %rdi     # 0x28fc2
movl %ebx, %esi
xorl %eax, %eax
callq 0x5fdd
movq %rbx, 0x8(%rsp)
leal -0x7(%rbx), %eax
cmpl $-0x7, %eax
ja 0x91e6
leaq 0x1fe0f(%rip), %rdi     # 0x28fdd
xorl %eax, %eax
addq $0x9f8, %rsp            # imm = 0x9F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x622a
leaq 0x20463(%rip), %rdi     # 0x29650
leaq 0x2d0f9(%rip), %rsi     # 0x362ed
callq 0x21d0
testq %rax, %rax
je 0x948d
movq %rax, %r14
leaq 0x34(%rsp), %rbx
movq 0x8(%rsp), %rax
decl %eax
imulq $0xff, %rax, %rax
addq %rsp, %rax
addq $0x238, %rax            # imm = 0x238
movq %rax, 0x18(%rsp)
leaq 0x30(%rsp), %r15
pushq $0xa
popq %rbp
xorl %r12d, %r12d
xorl %r13d, %r13d
movq %r14, 0x10(%rsp)
leaq 0x130(%rsp), %rdi
leaq 0x1fdbc(%rip), %rsi     # 0x29004
movq %r14, %rdx
callq 0x914c
testb %al, %al
je 0x944c
movq %r15, %rdi
leaq 0x1fda6(%rip), %rsi     # 0x29008
movq %r14, %rdx
callq 0x914c
testb %al, %al
je 0x944c
leaq 0x230(%rsp), %rdi
leaq 0x1fd8c(%rip), %rsi     # 0x2900d
movq %r14, %rdx
callq 0x914c
testb %al, %al
je 0x944c
leaq 0x32f(%rsp), %rdi
leaq 0x1fd75(%rip), %rsi     # 0x29015
movq %r14, %rdx
callq 0x914c
testb %al, %al
je 0x944c
leaq 0x42e(%rsp), %rdi
leaq 0x1fd5e(%rip), %rsi     # 0x2901d
movq %r14, %rdx
callq 0x914c
testb %al, %al
je 0x944c
leaq 0x52d(%rsp), %rdi
leaq 0x1fd47(%rip), %rsi     # 0x29025
movq %r14, %rdx
callq 0x914c
testb %al, %al
je 0x944c
leaq 0x62c(%rsp), %rdi
leaq 0x1fd30(%rip), %rsi     # 0x2902d
movq %r14, %rdx
callq 0x914c
testb %al, %al
je 0x944c
leaq 0x72b(%rsp), %rdi
leaq 0x1fd19(%rip), %rsi     # 0x29035
movq %r14, %rdx
callq 0x914c
testb %al, %al
je 0x944c
cmpl $0x63, %r12d
ja 0x944c
movq %r13, 0x20(%rsp)
movq %r12, 0x28(%rsp)
leaq 0x1fcf6(%rip), %r14     # 0x2903d
movq %r14, %rdi
xorl %eax, %eax
callq 0x5fdd
leaq 0x22147(%rip), %r12     # 0x2b49f
movq %r12, %rdi
leaq 0x133(%rsp), %rsi
xorl %eax, %eax
callq 0x5fdd
movq %r12, %rdi
movq %rbx, %rsi
xorl %eax, %eax
callq 0x5fdd
movq %r14, %rdi
xorl %eax, %eax
callq 0x5fdd
movq %r15, %rdi
movl %ebp, %esi
callq 0x2070
testq %rax, %rax
je 0x9393
movb $0x0, (%rax)
leaq 0x830(%rsp), %r15
movq %r15, %rdi
movq %rbx, %r14
movq %rbx, %rsi
callq 0xa1b8
movq %r15, %rdi
callq 0x4938
movq 0x18(%rsp), %rdi
xorl %esi, %esi
movl %ebp, %edx
callq 0x2240
movq %rax, %r13
callq 0xd470
movl %eax, %r12d
movq %r15, %rdi
movq 0x8(%rsp), %rbx
movl %ebx, %esi
callq 0x902c
movq %rax, %r15
callq 0xd470
movl %eax, %ebp
leaq 0x1fc9b(%rip), %rdi     # 0x29085
movl %ebx, %esi
movq %r13, %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0x5fdd
cmpq %r13, %r15
jne 0x94aa
subl %r12d, %ebp
incl %ebp
imulq $0x3e8, %r13, %rax      # imm = 0x3E8
movslq %ebp, %rcx
xorl %edx, %edx
divq %rcx
movq 0x20(%rsp), %r13
addq %rax, %r13
leaq 0x201fd(%rip), %rdi     # 0x29622
movq %rax, %rsi
xorl %eax, %eax
callq 0x5fdd
movq 0x28(%rsp), %r12
incq %r12
movq %r14, %rbx
movq 0x10(%rsp), %r14
leaq 0x30(%rsp), %r15
pushq $0xa
popq %rbp
jmp 0x9239
movq %r14, %rdi
callq 0x2160
movq %r13, %rax
xorl %edx, %edx
divq %r12
leaq 0x1fc46(%rip), %rdi     # 0x290a9
movq %rax, %rsi
xorl %eax, %eax
callq 0x5fdd
leaq 0x1fc47(%rip), %rdi     # 0x290bb
xorl %eax, %eax
callq 0x5fdd
addq $0x9f8, %rsp            # imm = 0x9F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x201bc(%rip), %rdi     # 0x29650
addq $0x9f8, %rsp            # imm = 0x9F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2100
pushq $0x1
popq %rdi
callq 0x21f0