// File: /workspace/github_cpp_makefile_star2_O1/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, %r15d
leaq 0x216fe(%rip), %rdi     # 0x2af92
movl %r15d, %esi
xorl %eax, %eax
callq 0x640d
leal -0x7(%r15), %eax
cmpl $-0x7, %eax
ja 0x98c6
leaq 0x216ff(%rip), %rdi     # 0x2afad
xorl %eax, %eax
addq $0x9f8, %rsp            # imm = 0x9F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2330
leaq 0x21d53(%rip), %rdi     # 0x2b620
leaq 0x2e8d5(%rip), %rsi     # 0x381a9
callq 0x21d0
testq %rax, %rax
je 0x9b95
movq %rax, %r12
leaq 0x216e8(%rip), %rsi     # 0x2afd4
leaq 0x130(%rsp), %rdi
movq %rax, %rdx
callq 0x982e
xorl %r14d, %r14d
testb %al, %al
je 0x9b51
leaq 0x34(%rsp), %r14
movq %r15, 0x20(%rsp)
leal -0x1(%r15), %eax
movq %rax, %rcx
shlq $0x8, %rcx
subq %rax, %rcx
leaq (%rsp,%rcx), %rax
addq $0x238, %rax            # imm = 0x238
movq %rax, 0x18(%rsp)
leaq 0x30(%rsp), %rbx
xorl %r15d, %r15d
xorl %ebp, %ebp
movq %r12, 0x10(%rsp)
movq %rbx, %rdi
leaq 0x21691(%rip), %rsi     # 0x2afd8
movq %r12, %rdx
callq 0x982e
testb %al, %al
je 0x9b4c
leaq 0x230(%rsp), %rdi
leaq 0x21677(%rip), %rsi     # 0x2afdd
movq %r12, %rdx
callq 0x982e
testb %al, %al
je 0x9b4c
leaq 0x32f(%rsp), %rdi
leaq 0x21660(%rip), %rsi     # 0x2afe5
movq %r12, %rdx
callq 0x982e
testb %al, %al
je 0x9b4c
leaq 0x42e(%rsp), %rdi
leaq 0x21649(%rip), %rsi     # 0x2afed
movq %r12, %rdx
callq 0x982e
testb %al, %al
je 0x9b4c
leaq 0x52d(%rsp), %rdi
leaq 0x21632(%rip), %rsi     # 0x2aff5
movq %r12, %rdx
callq 0x982e
testb %al, %al
je 0x9b4c
leaq 0x62c(%rsp), %rdi
leaq 0x2161b(%rip), %rsi     # 0x2affd
movq %r12, %rdx
callq 0x982e
testb %al, %al
je 0x9b4c
leaq 0x72b(%rsp), %rdi
leaq 0x21604(%rip), %rsi     # 0x2b005
movq %r12, %rdx
callq 0x982e
testb %al, %al
je 0x9b4c
cmpl $0x63, %ebp
ja 0x9b4c
movl %ebp, 0xc(%rsp)
movq %r15, 0x28(%rsp)
leaq 0x215e3(%rip), %r15     # 0x2b00d
movq %r15, %rdi
xorl %eax, %eax
callq 0x640d
leaq 0x23a34(%rip), %r12     # 0x2d46f
movq %r12, %rdi
leaq 0x133(%rsp), %rsi
xorl %eax, %eax
callq 0x640d
movq %r12, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x640d
movq %r15, %rdi
xorl %eax, %eax
callq 0x640d
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2070
testq %rax, %rax
je 0x9a79
movb $0x0, (%rax)
leaq 0x830(%rsp), %r15
movq %r15, %rdi
movq %r14, %rbx
movq %r14, %rsi
callq 0xaa74
movq %r15, %rdi
callq 0x4b7c
movq 0x18(%rsp), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x2240
movq %rax, %r14
callq 0xdf58
movl %eax, %r13d
movq %r15, %rdi
movq 0x20(%rsp), %r15
movl %r15d, %esi
callq 0x96f0
movq %rax, %r12
callq 0xdf58
movl %eax, %ebp
leaq 0x21581(%rip), %rdi     # 0x2b055
movl %r15d, %esi
movq %r14, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x640d
cmpq %r14, %r12
jne 0x9bb2
subl %r13d, %ebp
incl %ebp
imulq $0x3e8, %r12, %rax      # imm = 0x3E8
movslq %ebp, %rcx
xorl %edx, %edx
divq %rcx
movq 0x28(%rsp), %r15
addq %rax, %r15
leaq 0x21ae2(%rip), %rdi     # 0x2b5f2
movq %rax, %rsi
xorl %eax, %eax
callq 0x640d
movl 0xc(%rsp), %ebp
incl %ebp
leaq 0x130(%rsp), %rdi
leaq 0x214a5(%rip), %rsi     # 0x2afd4
movq 0x10(%rsp), %r12
movq %r12, %rdx
callq 0x982e
testb %al, %al
movq %rbx, %r14
leaq 0x30(%rsp), %rbx
jne 0x993d
movl %ebp, %r14d
jmp 0x9b54
xorl %r15d, %r15d
movq %r12, %rdi
callq 0x2170
movq %r15, %rax
xorl %edx, %edx
divq %r14
leaq 0x2150e(%rip), %rdi     # 0x2b079
movq %rax, %rsi
xorl %eax, %eax
callq 0x640d
leaq 0x2150f(%rip), %rdi     # 0x2b08b
xorl %eax, %eax
callq 0x640d
addq $0x9f8, %rsp            # imm = 0x9F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x21a84(%rip), %rdi     # 0x2b620
addq $0x9f8, %rsp            # imm = 0x9F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2110
movl $0x1, %edi
callq 0x21f0