// File: /workspace/github_cpp_makefile_star2_O3/jswaff[P]prophet3/src/next.cpp
// Function: next(position*, unsigned long long**, move_order_dto*)
bool next(position *pos,move **m,move_order_dto *mo) {
    assert(mo->currentp >= mo->startp);

    if (mo->next_stage==PVMOVE) {
        mo->next_stage = HASHMV;

        assert(mo->currentp==mo->startp);
        assert(mo->endp==mo->startp);

        if (good_move(pos,mo->pv_move)) {
            assert(test_good_move(mo->pv_move,pos));
            *m = &mo->pv_move;
            return true;
        }
    }

    if (mo->next_stage==HASHMV) {
        mo->next_stage = GENCAPS;

        assert(mo->hash_move==clear_score(mo->hash_move));
        assert(mo->pv_move==clear_score(mo->pv_move));

        if (mo->hash_move != mo->pv_move && good_move(pos,mo->hash_move)) {
            assert(test_good_move(mo->hash_move,pos));
            *m = &mo->hash_move;
            return true;
        }
    }

    if (mo->next_stage==GENCAPS) {
        mo->next_stage = CAPTURES_PROMOS;


        assert(mo->currentp==mo->startp);
        assert(mo->endp==mo->startp);

        mo->endp = gen_moves(mo->startp,pos,true,false);
        for (move* mp=mo->startp;mp<mo->endp;mp++) {
            if (*mp==mo->pv_move || *mp==mo->hash_move) {
                *mp = 0;
            } else {
                int32 score = mvvlva(pos,*mp);
                assert(score >= -20000 && score <= 20000);
                set_move_score(mp,score);
            }
        }
    }

    // captures promotions
    if (mo->next_stage==CAPTURES_PROMOS) {
        if (best_at_top(mo->currentp,mo->endp)) {
            if (get_move_score(mo->currentp) > 0) { // only caps and promos should have a score >0
                assert(is_capture(*mo->currentp) || get_promopiece(*mo->currentp)!=NO_PIECE);
                *m = mo->currentp;
                mo->currentp++;
                return true;
            }
        }
        mo->next_stage = KILLER1;
    }

    if (mo->caps_only==false) {
        // killer1
        if (mo->next_stage==KILLER1) {
            mo->next_stage = KILLER2;

            if (mo->killer1 != mo->pv_move && mo->killer1 != mo->hash_move
                    && good_move(pos,mo->killer1)) {
                assert(test_good_move(mo->killer1,pos));
                assert(!is_capture(mo->killer1));
                assert(mo->killer1==clear_score(mo->killer1));
                *m = &mo->killer1;
                return true;
            }
        }

        // killer2
        if (mo->next_stage==KILLER2) {
            mo->next_stage = GENNONCAPS;

            if (mo->killer2 != mo->killer1 && mo->killer2 != mo->pv_move
                    && mo->killer2 != mo->hash_move && good_move(pos,mo->killer2)) {
                assert(test_good_move(mo->killer2,pos));
                assert(!is_capture(mo->killer2));
                assert(mo->killer2==clear_score(mo->killer2));
                *m = &mo->killer2;
                return true;
            }
        }

        if (mo->next_stage==GENNONCAPS) {
            mo->next_stage = REMAINING;

            assert(mo->currentp >= mo->startp);
            assert(mo->currentp <= mo->endp);

            move* mystartp = mo->endp;
            mo->endp = gen_moves(mystartp,pos,false,true);
            for (move* mp=mystartp;mp<mo->endp;mp++) {
                if (*mp==mo->pv_move || *mp==mo->hash_move || *mp==mo->killer1 || *mp==mo->killer2) {
                    *mp = 0;
                }
            }
        }
    }

    // play them as they come
    while (mo->currentp < mo->endp){
        *m = mo->currentp;
        mo->currentp++;
        if (**m != 0) {
            return true;
        }
    }

    return false;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
pushq %rax
movq 0x28(%rdx), %rcx
movq 0x38(%rdx), %rax
cmpq %rcx, %rax
jb 0x8f3a
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
movl (%rdx), %edx
testl %edx, %edx
jne 0x8bf9
movl $0x1, (%rbx)
cmpq %rcx, %rax
jne 0x8f97
cmpq %rax, 0x30(%rbx)
jne 0x8fb6
movq 0x8(%rbx), %rsi
movq %r15, %rdi
callq 0x910b
testb %al, %al
je 0x8bf7
addq $0x8, %rbx
movq (%rbx), %rdi
movq %r15, %rsi
callq 0x94d4
testb %al, %al
jne 0x8c5b
leaq 0x221e0(%rip), %rdi     # 0x2adbf
leaq 0x2216d(%rip), %rsi     # 0x2ad53
leaq 0x22173(%rip), %rcx     # 0x2ad60
movl $0x1a, %edx
callq 0x20d0
movl (%rbx), %edx
cmpl $0x1, %edx
jne 0x8c63
movl $0x2, (%rbx)
movq 0x10(%rbx), %r12
movq %r12, %rdi
callq 0x78dd
cmpq %rax, %r12
jne 0x8f59
movq 0x8(%rbx), %r12
movq %r12, %rdi
callq 0x78dd
cmpq %rax, %r12
jne 0x8f78
movq 0x10(%rbx), %rsi
cmpq 0x8(%rbx), %rsi
je 0x8c63
movq %r15, %rdi
callq 0x910b
testb %al, %al
je 0x8c63
addq $0x10, %rbx
movq (%rbx), %rdi
movq %r15, %rsi
callq 0x94d4
testb %al, %al
je 0x9051
movq %rbx, (%r14)
jmp 0x8d4b
movl (%rbx), %eax
cmpl $0x2, %eax
jne 0x8cf5
movl $0x3, (%rbx)
movq 0x28(%rbx), %rdi
movq 0x38(%rbx), %rax
cmpq %rdi, %rax
jne 0x8fd5
cmpq %rax, 0x30(%rbx)
jne 0x8ff4
movq %r15, %rsi
movl $0x1, %edx
xorl %ecx, %ecx
callq 0x7970
movq %rax, 0x30(%rbx)
movq 0x28(%rbx), %r12
cmpq %rax, %r12
jae 0x8cf3
movq (%r12), %rsi
cmpq 0x8(%rbx), %rsi
je 0x8cbb
cmpq 0x10(%rbx), %rsi
jne 0x8cc5
movq $0x0, (%r12)
jmp 0x8ced
movq %r15, %rdi
callq 0x8b29
leal 0x4e20(%rax), %ecx
cmpl $0x9c41, %ecx           # imm = 0x9C41
jae 0x8f1b
movq %r12, %rdi
movl %eax, %esi
callq 0x78bc
movq 0x30(%rbx), %rax
addq $0x8, %r12
jmp 0x8ca6
movl (%rbx), %eax
cmpl $0x3, %eax
jne 0x8d4f
movq 0x30(%rbx), %rsi
movq 0x38(%rbx), %rdi
callq 0x954b
testb %al, %al
je 0x8d60
movq 0x38(%rbx), %rdi
callq 0x78a1
testl %eax, %eax
jle 0x8d60
movq 0x38(%rbx), %rax
movq (%rax), %rdi
callq 0x7883
testb %al, %al
jne 0x8d3c
movq 0x38(%rbx), %rax
movq (%rax), %rdi
callq 0x786f
testl %eax, %eax
je 0x9070
movq 0x38(%rbx), %rax
movq %rax, (%r14)
addq $0x8, %rax
movq %rax, 0x38(%rbx)
movb $0x1, %al
jmp 0x8d91
cmpb $0x0, 0x4(%rbx)
jne 0x8d6c
cmpl $0x4, %eax
jne 0x8e1b
jmp 0x8d9d
movl $0x4, (%rbx)
cmpb $0x0, 0x4(%rbx)
je 0x8d9d
movq 0x38(%rbx), %rcx
cmpq 0x30(%rbx), %rcx
setb %al
jae 0x8d91
movq %rcx, (%r14)
movq 0x38(%rbx), %rcx
addq $0x8, %rcx
movq %rcx, 0x38(%rbx)
movq (%r14), %rdx
cmpq $0x0, (%rdx)
je 0x8d70
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movl $0x5, (%rbx)
movq 0x18(%rbx), %rsi
cmpq 0x8(%rbx), %rsi
je 0x8e24
cmpq 0x10(%rbx), %rsi
je 0x8e24
movq %r15, %rdi
callq 0x910b
testb %al, %al
je 0x8e19
addq $0x18, %rbx
movq (%rbx), %rdi
movq %r15, %rsi
callq 0x94d4
testb %al, %al
je 0x908f
movq (%rbx), %rdi
callq 0x7883
testb %al, %al
jne 0x90ae
movq (%rbx), %r15
movq %r15, %rdi
callq 0x78dd
cmpq %rax, %r15
je 0x8c5b
leaq 0x220f0(%rip), %rdi     # 0x2aef1
leaq 0x21f4b(%rip), %rsi     # 0x2ad53
leaq 0x21f51(%rip), %rcx     # 0x2ad60
movl $0x56, %edx
callq 0x20d0
movl (%rbx), %eax
cmpl $0x5, %eax
jne 0x8ea8
movl $0x6, (%rbx)
movq 0x20(%rbx), %rsi
cmpq 0x18(%rbx), %rsi
je 0x8eb1
cmpq 0x8(%rbx), %rsi
je 0x8eb1
cmpq 0x10(%rbx), %rsi
je 0x8eb1
movq %r15, %rdi
callq 0x910b
testb %al, %al
je 0x8ea6
addq $0x20, %rbx
movq (%rbx), %rdi
movq %r15, %rsi
callq 0x94d4
testb %al, %al
je 0x90cd
movq (%rbx), %rdi
callq 0x7883
testb %al, %al
jne 0x90ec
movq (%rbx), %r15
movq %r15, %rdi
callq 0x78dd
cmpq %rax, %r15
je 0x8c5b
leaq 0x220c2(%rip), %rdi     # 0x2af50
leaq 0x21ebe(%rip), %rsi     # 0x2ad53
leaq 0x21ec4(%rip), %rcx     # 0x2ad60
movl $0x64, %edx
callq 0x20d0
movl (%rbx), %eax
cmpl $0x6, %eax
jne 0x8d6c
movl $0x7, (%rbx)
movq 0x38(%rbx), %rax
cmpq 0x28(%rbx), %rax
jb 0x9013
movq 0x30(%rbx), %r12
cmpq %r12, %rax
ja 0x9032
movq %r12, %rdi
movq %r15, %rsi
xorl %edx, %edx
movl $0x1, %ecx
callq 0x7970
movq %rax, 0x30(%rbx)
cmpq %rax, %r12
jae 0x8d6c
movq (%r12), %rcx
cmpq 0x8(%rbx), %rcx
je 0x8f0d
cmpq 0x10(%rbx), %rcx
je 0x8f0d
cmpq 0x18(%rbx), %rcx
je 0x8f0d
cmpq 0x20(%rbx), %rcx
jne 0x8f15
movq $0x0, (%r12)
addq $0x8, %r12
jmp 0x8ee8
leaq 0x21f2f(%rip), %rdi     # 0x2ae51
leaq 0x21e2a(%rip), %rsi     # 0x2ad53
leaq 0x21e30(%rip), %rcx     # 0x2ad60
movl $0x3a, %edx
callq 0x20d0
leaq 0x21df7(%rip), %rdi     # 0x2ad38
leaq 0x21e0b(%rip), %rsi     # 0x2ad53
leaq 0x21e11(%rip), %rcx     # 0x2ad60
movl $0x11, %edx
callq 0x20d0
leaq 0x21e7f(%rip), %rdi     # 0x2addf
leaq 0x21dec(%rip), %rsi     # 0x2ad53
leaq 0x21df2(%rip), %rcx     # 0x2ad60
movl $0x23, %edx
callq 0x20d0
leaq 0x21e8a(%rip), %rdi     # 0x2ae09
leaq 0x21dcd(%rip), %rsi     # 0x2ad53
leaq 0x21dd3(%rip), %rcx     # 0x2ad60
movl $0x24, %edx
callq 0x20d0
leaq 0x21df3(%rip), %rdi     # 0x2ad91
leaq 0x21dae(%rip), %rsi     # 0x2ad53
leaq 0x21db4(%rip), %rcx     # 0x2ad60
movl $0x16, %edx
callq 0x20d0
leaq 0x21ded(%rip), %rdi     # 0x2adaa
leaq 0x21d8f(%rip), %rsi     # 0x2ad53
leaq 0x21d95(%rip), %rcx     # 0x2ad60
movl $0x17, %edx
callq 0x20d0
leaq 0x21db5(%rip), %rdi     # 0x2ad91
leaq 0x21d70(%rip), %rsi     # 0x2ad53
leaq 0x21d76(%rip), %rcx     # 0x2ad60
movl $0x31, %edx
callq 0x20d0
leaq 0x21daf(%rip), %rdi     # 0x2adaa
leaq 0x21d51(%rip), %rsi     # 0x2ad53
leaq 0x21d57(%rip), %rcx     # 0x2ad60
movl $0x32, %edx
callq 0x20d0
leaq 0x21d1e(%rip), %rdi     # 0x2ad38
leaq 0x21d32(%rip), %rsi     # 0x2ad53
leaq 0x21d38(%rip), %rcx     # 0x2ad60
movl $0x6d, %edx
callq 0x20d0
leaq 0x21f3d(%rip), %rdi     # 0x2af76
leaq 0x21d13(%rip), %rsi     # 0x2ad53
leaq 0x21d19(%rip), %rcx     # 0x2ad60
movl $0x6e, %edx
callq 0x20d0
leaq 0x21dd7(%rip), %rdi     # 0x2ae2f
leaq 0x21cf4(%rip), %rsi     # 0x2ad53
leaq 0x21cfa(%rip), %rcx     # 0x2ad60
movl $0x27, %edx
callq 0x20d0
leaq 0x21dfc(%rip), %rdi     # 0x2ae73
leaq 0x21cd5(%rip), %rsi     # 0x2ad53
leaq 0x21cdb(%rip), %rcx     # 0x2ad60
movl $0x44, %edx
callq 0x20d0
leaq 0x21e22(%rip), %rdi     # 0x2aeb8
leaq 0x21cb6(%rip), %rsi     # 0x2ad53
leaq 0x21cbc(%rip), %rcx     # 0x2ad60
movl $0x54, %edx
callq 0x20d0
leaq 0x21e23(%rip), %rdi     # 0x2aed8
leaq 0x21c97(%rip), %rsi     # 0x2ad53
leaq 0x21c9d(%rip), %rcx     # 0x2ad60
movl $0x55, %edx
callq 0x20d0
leaq 0x21e43(%rip), %rdi     # 0x2af17
leaq 0x21c78(%rip), %rsi     # 0x2ad53
leaq 0x21c7e(%rip), %rcx     # 0x2ad60
movl $0x62, %edx
callq 0x20d0
leaq 0x21e44(%rip), %rdi     # 0x2af37
leaq 0x21c59(%rip), %rsi     # 0x2ad53
leaq 0x21c5f(%rip), %rcx     # 0x2ad60
movl $0x63, %edx
callq 0x20d0