// File: /workspace/github_cpp_makefile_star2_O3/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 0x216ce(%rip), %rdi     # 0x2b012
movl %r15d, %esi
xorl %eax, %eax
callq 0x64b9
leal -0x7(%r15), %eax
cmpl $-0x7, %eax
ja 0x9976
leaq 0x216cf(%rip), %rdi     # 0x2b02d
xorl %eax, %eax
addq $0x9f8, %rsp            # imm = 0x9F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2330
leaq 0x21d23(%rip), %rdi     # 0x2b6a0
leaq 0x2e8a5(%rip), %rsi     # 0x38229
callq 0x21d0
testq %rax, %rax
je 0x9c45
movq %rax, %r12
leaq 0x216b8(%rip), %rsi     # 0x2b054
leaq 0x130(%rsp), %rdi
movq %rax, %rdx
callq 0x98de
xorl %r14d, %r14d
testb %al, %al
je 0x9c01
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 0x21661(%rip), %rsi     # 0x2b058
movq %r12, %rdx
callq 0x98de
testb %al, %al
je 0x9bfc
leaq 0x230(%rsp), %rdi
leaq 0x21647(%rip), %rsi     # 0x2b05d
movq %r12, %rdx
callq 0x98de
testb %al, %al
je 0x9bfc
leaq 0x32f(%rsp), %rdi
leaq 0x21630(%rip), %rsi     # 0x2b065
movq %r12, %rdx
callq 0x98de
testb %al, %al
je 0x9bfc
leaq 0x42e(%rsp), %rdi
leaq 0x21619(%rip), %rsi     # 0x2b06d
movq %r12, %rdx
callq 0x98de
testb %al, %al
je 0x9bfc
leaq 0x52d(%rsp), %rdi
leaq 0x21602(%rip), %rsi     # 0x2b075
movq %r12, %rdx
callq 0x98de
testb %al, %al
je 0x9bfc
leaq 0x62c(%rsp), %rdi
leaq 0x215eb(%rip), %rsi     # 0x2b07d
movq %r12, %rdx
callq 0x98de
testb %al, %al
je 0x9bfc
leaq 0x72b(%rsp), %rdi
leaq 0x215d4(%rip), %rsi     # 0x2b085
movq %r12, %rdx
callq 0x98de
testb %al, %al
je 0x9bfc
cmpl $0x63, %ebp
ja 0x9bfc
movl %ebp, 0xc(%rsp)
movq %r15, 0x28(%rsp)
leaq 0x215b3(%rip), %r15     # 0x2b08d
movq %r15, %rdi
xorl %eax, %eax
callq 0x64b9
leaq 0x23a04(%rip), %r12     # 0x2d4ef
movq %r12, %rdi
leaq 0x133(%rsp), %rsi
xorl %eax, %eax
callq 0x64b9
movq %r12, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x64b9
movq %r15, %rdi
xorl %eax, %eax
callq 0x64b9
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2070
testq %rax, %rax
je 0x9b29
movb $0x0, (%rax)
leaq 0x830(%rsp), %r15
movq %r15, %rdi
movq %r14, %rbx
movq %r14, %rsi
callq 0xaa82
movq %r15, %rdi
callq 0x4bd8
movq 0x18(%rsp), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x2240
movq %rax, %r13
callq 0xdf08
movl %eax, %r12d
movq %r15, %rdi
movq 0x20(%rsp), %r15
movl %r15d, %esi
callq 0x97a0
movq %rax, %r14
callq 0xdf08
movl %eax, %ebp
leaq 0x21551(%rip), %rdi     # 0x2b0d5
movl %r15d, %esi
movq %r13, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x64b9
cmpq %r13, %r14
jne 0x9c62
subl %r12d, %ebp
incl %ebp
imulq $0x3e8, %r13, %rax      # imm = 0x3E8
movslq %ebp, %rcx
xorl %edx, %edx
divq %rcx
movq 0x28(%rsp), %r15
addq %rax, %r15
leaq 0x21ab2(%rip), %rdi     # 0x2b672
movq %rax, %rsi
xorl %eax, %eax
callq 0x64b9
movl 0xc(%rsp), %ebp
incl %ebp
leaq 0x130(%rsp), %rdi
leaq 0x21475(%rip), %rsi     # 0x2b054
movq 0x10(%rsp), %r12
movq %r12, %rdx
callq 0x98de
testb %al, %al
movq %rbx, %r14
leaq 0x30(%rsp), %rbx
jne 0x99ed
movl %ebp, %r14d
jmp 0x9c04
xorl %r15d, %r15d
movq %r12, %rdi
callq 0x2170
movq %r15, %rax
xorl %edx, %edx
divq %r14
leaq 0x214de(%rip), %rdi     # 0x2b0f9
movq %rax, %rsi
xorl %eax, %eax
callq 0x64b9
leaq 0x214df(%rip), %rdi     # 0x2b10b
xorl %eax, %eax
callq 0x64b9
addq $0x9f8, %rsp            # imm = 0x9F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x21a54(%rip), %rdi     # 0x2b6a0
addq $0x9f8, %rsp            # imm = 0x9F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2110
movl $0x1, %edi
callq 0x21f0