// File: /workspace/github_cpp_makefile_star2_O1/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), %rax
movq 0x38(%rdx), %rcx
cmpq %rax, %rcx
jb 0x8e81
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
cmpl $0x0, (%rdx)
jne 0x8b62
movl $0x1, (%rbx)
cmpq %rax, %rcx
jne 0x8ede
cmpq %rax, 0x30(%rbx)
jne 0x8efd
movq 0x8(%rbx), %rsi
movq %r15, %rdi
callq 0x9052
testb %al, %al
je 0x8b62
addq $0x8, %rbx
movq (%rbx), %rdi
movq %r15, %rsi
callq 0x941f
testb %al, %al
jne 0x8bc4
leaq 0x221f5(%rip), %rdi     # 0x2ad3f
leaq 0x22182(%rip), %rsi     # 0x2acd3
leaq 0x22188(%rip), %rcx     # 0x2ace0
movl $0x1a, %edx
callq 0x20d0
cmpl $0x1, (%rbx)
jne 0x8bcc
movl $0x2, (%rbx)
movq 0x10(%rbx), %r12
movq %r12, %rdi
callq 0x783c
cmpq %rax, %r12
jne 0x8ea0
movq 0x8(%rbx), %r12
movq %r12, %rdi
callq 0x783c
cmpq %rax, %r12
jne 0x8ebf
movq 0x10(%rbx), %rsi
cmpq 0x8(%rbx), %rsi
je 0x8bcc
movq %r15, %rdi
callq 0x9052
testb %al, %al
je 0x8bcc
addq $0x10, %rbx
movq (%rbx), %rdi
movq %r15, %rsi
callq 0x941f
testb %al, %al
je 0x8f98
movq %rbx, (%r14)
jmp 0x8caa
cmpl $0x2, (%rbx)
jne 0x8c57
movl $0x3, (%rbx)
movq 0x28(%rbx), %rdi
cmpq %rdi, 0x38(%rbx)
jne 0x8f1c
cmpq %rdi, 0x30(%rbx)
jne 0x8f3b
movq %r15, %rsi
movl $0x1, %edx
xorl %ecx, %ecx
callq 0x78d0
movq %rax, 0x30(%rbx)
movq 0x28(%rbx), %r12
cmpq %rax, %r12
jae 0x8c57
movq (%r12), %rsi
cmpq 0x8(%rbx), %rsi
je 0x8c1f
cmpq 0x10(%rbx), %rsi
jne 0x8c29
movq $0x0, (%r12)
jmp 0x8c4d
movq %r15, %rdi
callq 0x8a95
leal 0x4e20(%rax), %ecx
cmpl $0x9c41, %ecx           # imm = 0x9C41
jae 0x8e62
movq %r12, %rdi
movl %eax, %esi
callq 0x7818
addq $0x8, %r12
cmpq 0x30(%rbx), %r12
jb 0x8c0f
cmpl $0x3, (%rbx)
jne 0x8cb7
movq 0x30(%rbx), %rsi
movq 0x38(%rbx), %rdi
callq 0x9496
testb %al, %al
je 0x8cb1
movq 0x38(%rbx), %rdi
callq 0x77fd
testl %eax, %eax
jle 0x8cb1
movq 0x38(%rbx), %rax
movq (%rax), %rdi
callq 0x77df
testb %al, %al
jne 0x8c9e
movq 0x38(%rbx), %rax
movq (%rax), %rdi
callq 0x77cb
testl %eax, %eax
je 0x8fb7
movq 0x38(%rbx), %rax
movq %rax, (%r14)
addq $0x8, 0x38(%rbx)
movb $0x1, %al
jmp 0x8e56
movl $0x4, (%rbx)
cmpb $0x0, 0x4(%rbx)
jne 0x8e38
cmpl $0x4, (%rbx)
jne 0x8d42
movl $0x5, (%rbx)
movq 0x18(%rbx), %rsi
cmpq 0x8(%rbx), %rsi
je 0x8d42
cmpq 0x10(%rbx), %rsi
je 0x8d42
movq %r15, %rdi
callq 0x9052
testb %al, %al
je 0x8d42
addq $0x18, %rbx
movq (%rbx), %rdi
movq %r15, %rsi
callq 0x941f
testb %al, %al
je 0x8fd6
movq (%rbx), %rdi
callq 0x77df
testb %al, %al
jne 0x8ff5
movq (%rbx), %r15
movq %r15, %rdi
callq 0x783c
cmpq %rax, %r15
je 0x8bc4
leaq 0x22147(%rip), %rdi     # 0x2ae71
leaq 0x21fa2(%rip), %rsi     # 0x2acd3
leaq 0x21fa8(%rip), %rcx     # 0x2ace0
movl $0x56, %edx
callq 0x20d0
cmpl $0x5, (%rbx)
jne 0x8dcd
movl $0x6, (%rbx)
movq 0x20(%rbx), %rsi
cmpq 0x18(%rbx), %rsi
je 0x8dcd
cmpq 0x8(%rbx), %rsi
je 0x8dcd
cmpq 0x10(%rbx), %rsi
je 0x8dcd
movq %r15, %rdi
callq 0x9052
testb %al, %al
je 0x8dcd
addq $0x20, %rbx
movq (%rbx), %rdi
movq %r15, %rsi
callq 0x941f
testb %al, %al
je 0x9014
movq (%rbx), %rdi
callq 0x77df
testb %al, %al
jne 0x9033
movq (%rbx), %r15
movq %r15, %rdi
callq 0x783c
cmpq %rax, %r15
je 0x8bc4
leaq 0x2211b(%rip), %rdi     # 0x2aed0
leaq 0x21f17(%rip), %rsi     # 0x2acd3
leaq 0x21f1d(%rip), %rcx     # 0x2ace0
movl $0x64, %edx
callq 0x20d0
cmpl $0x6, (%rbx)
jne 0x8e38
movl $0x7, (%rbx)
movq 0x38(%rbx), %rax
cmpq 0x28(%rbx), %rax
jb 0x8f5a
movq 0x30(%rbx), %r12
cmpq %r12, %rax
ja 0x8f79
movq %r12, %rdi
movq %r15, %rsi
xorl %edx, %edx
movl $0x1, %ecx
callq 0x78d0
movq %rax, 0x30(%rbx)
cmpq %rax, %r12
jae 0x8e38
movq (%r12), %rcx
cmpq 0x8(%rbx), %rcx
je 0x8e2a
cmpq 0x10(%rbx), %rcx
je 0x8e2a
cmpq 0x18(%rbx), %rcx
je 0x8e2a
cmpq 0x20(%rbx), %rcx
jne 0x8e32
movq $0x0, (%r12)
addq $0x8, %r12
jmp 0x8e09
movq 0x38(%rbx), %rcx
cmpq 0x30(%rbx), %rcx
setb %al
jae 0x8e56
movq %rcx, (%r14)
addq $0x8, 0x38(%rbx)
movq (%r14), %rcx
cmpq $0x0, (%rcx)
je 0x8e38
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
leaq 0x21f68(%rip), %rdi     # 0x2add1
leaq 0x21e63(%rip), %rsi     # 0x2acd3
leaq 0x21e69(%rip), %rcx     # 0x2ace0
movl $0x3a, %edx
callq 0x20d0
leaq 0x21e30(%rip), %rdi     # 0x2acb8
leaq 0x21e44(%rip), %rsi     # 0x2acd3
leaq 0x21e4a(%rip), %rcx     # 0x2ace0
movl $0x11, %edx
callq 0x20d0
leaq 0x21eb8(%rip), %rdi     # 0x2ad5f
leaq 0x21e25(%rip), %rsi     # 0x2acd3
leaq 0x21e2b(%rip), %rcx     # 0x2ace0
movl $0x23, %edx
callq 0x20d0
leaq 0x21ec3(%rip), %rdi     # 0x2ad89
leaq 0x21e06(%rip), %rsi     # 0x2acd3
leaq 0x21e0c(%rip), %rcx     # 0x2ace0
movl $0x24, %edx
callq 0x20d0
leaq 0x21e2c(%rip), %rdi     # 0x2ad11
leaq 0x21de7(%rip), %rsi     # 0x2acd3
leaq 0x21ded(%rip), %rcx     # 0x2ace0
movl $0x16, %edx
callq 0x20d0
leaq 0x21e26(%rip), %rdi     # 0x2ad2a
leaq 0x21dc8(%rip), %rsi     # 0x2acd3
leaq 0x21dce(%rip), %rcx     # 0x2ace0
movl $0x17, %edx
callq 0x20d0
leaq 0x21dee(%rip), %rdi     # 0x2ad11
leaq 0x21da9(%rip), %rsi     # 0x2acd3
leaq 0x21daf(%rip), %rcx     # 0x2ace0
movl $0x31, %edx
callq 0x20d0
leaq 0x21de8(%rip), %rdi     # 0x2ad2a
leaq 0x21d8a(%rip), %rsi     # 0x2acd3
leaq 0x21d90(%rip), %rcx     # 0x2ace0
movl $0x32, %edx
callq 0x20d0
leaq 0x21d57(%rip), %rdi     # 0x2acb8
leaq 0x21d6b(%rip), %rsi     # 0x2acd3
leaq 0x21d71(%rip), %rcx     # 0x2ace0
movl $0x6d, %edx
callq 0x20d0
leaq 0x21f76(%rip), %rdi     # 0x2aef6
leaq 0x21d4c(%rip), %rsi     # 0x2acd3
leaq 0x21d52(%rip), %rcx     # 0x2ace0
movl $0x6e, %edx
callq 0x20d0
leaq 0x21e10(%rip), %rdi     # 0x2adaf
leaq 0x21d2d(%rip), %rsi     # 0x2acd3
leaq 0x21d33(%rip), %rcx     # 0x2ace0
movl $0x27, %edx
callq 0x20d0
leaq 0x21e35(%rip), %rdi     # 0x2adf3
leaq 0x21d0e(%rip), %rsi     # 0x2acd3
leaq 0x21d14(%rip), %rcx     # 0x2ace0
movl $0x44, %edx
callq 0x20d0
leaq 0x21e5b(%rip), %rdi     # 0x2ae38
leaq 0x21cef(%rip), %rsi     # 0x2acd3
leaq 0x21cf5(%rip), %rcx     # 0x2ace0
movl $0x54, %edx
callq 0x20d0
leaq 0x21e5c(%rip), %rdi     # 0x2ae58
leaq 0x21cd0(%rip), %rsi     # 0x2acd3
leaq 0x21cd6(%rip), %rcx     # 0x2ace0
movl $0x55, %edx
callq 0x20d0
leaq 0x21e7c(%rip), %rdi     # 0x2ae97
leaq 0x21cb1(%rip), %rsi     # 0x2acd3
leaq 0x21cb7(%rip), %rcx     # 0x2ace0
movl $0x62, %edx
callq 0x20d0
leaq 0x21e7d(%rip), %rdi     # 0x2aeb7
leaq 0x21c92(%rip), %rsi     # 0x2acd3
leaq 0x21c98(%rip), %rcx     # 0x2ace0
movl $0x63, %edx
callq 0x20d0