// File: /workspace/github_cpp_makefile_star2_O2/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 0x8844
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
movl (%rdx), %edx
testl %edx, %edx
jne 0x8516
movl $0x1, (%rbx)
cmpq %rcx, %rax
jne 0x8898
cmpq %rax, 0x30(%rbx)
jne 0x88b4
movq 0x8(%rbx), %rsi
movq %r15, %rdi
callq 0x89da
testb %al, %al
je 0x8514
addq $0x8, %rbx
movq (%rbx), %rdi
movq %r15, %rsi
callq 0x8d73
testb %al, %al
jne 0x8578
leaq 0x20870(%rip), %rdi     # 0x28d6f
leaq 0x207fd(%rip), %rsi     # 0x28d03
leaq 0x20803(%rip), %rcx     # 0x28d10
pushq $0x1a
jmp 0x89d4
movl (%rbx), %edx
cmpl $0x1, %edx
jne 0x8580
movl $0x2, (%rbx)
movq 0x10(%rbx), %r12
movq %r12, %rdi
callq 0x73f4
cmpq %rax, %r12
jne 0x8860
movq 0x8(%rbx), %r12
movq %r12, %rdi
callq 0x73f4
cmpq %rax, %r12
jne 0x887c
movq 0x10(%rbx), %rsi
cmpq 0x8(%rbx), %rsi
je 0x8580
movq %r15, %rdi
callq 0x89da
testb %al, %al
je 0x8580
addq $0x10, %rbx
movq (%rbx), %rdi
movq %r15, %rsi
callq 0x8d73
testb %al, %al
je 0x8940
movq %rbx, (%r14)
jmp 0x8663
movl (%rbx), %eax
cmpl $0x2, %eax
jne 0x860d
movl $0x3, (%rbx)
movq 0x28(%rbx), %rdi
movq 0x38(%rbx), %rax
cmpq %rdi, %rax
jne 0x88d0
cmpq %rax, 0x30(%rbx)
jne 0x88ec
pushq $0x1
popq %rdx
movq %r15, %rsi
xorl %ecx, %ecx
callq 0x7482
movq %rax, 0x30(%rbx)
movq 0x28(%rbx), %r12
cmpq %rax, %r12
jae 0x860b
movq (%r12), %rsi
cmpq 0x8(%rbx), %rsi
je 0x85d6
cmpq 0x10(%rbx), %rsi
jne 0x85dd
andq $0x0, (%r12)
jmp 0x8605
movq %r15, %rdi
callq 0x844b
leal 0x4e20(%rax), %ecx
cmpl $0x9c41, %ecx           # imm = 0x9C41
jae 0x8828
movq %r12, %rdi
movl %eax, %esi
callq 0x73d3
movq 0x30(%rbx), %rax
addq $0x8, %r12
jmp 0x85c1
movl (%rbx), %eax
cmpl $0x3, %eax
jne 0x8667
movq 0x30(%rbx), %rsi
movq 0x38(%rbx), %rdi
callq 0x8de5
testb %al, %al
je 0x8678
movq 0x38(%rbx), %rdi
callq 0x73b8
testl %eax, %eax
jle 0x8678
movq 0x38(%rbx), %rax
movq (%rax), %rdi
callq 0x739a
testb %al, %al
jne 0x8654
movq 0x38(%rbx), %rax
movq (%rax), %rdi
callq 0x7386
testl %eax, %eax
je 0x8959
movq 0x38(%rbx), %rax
movq %rax, (%r14)
addq $0x8, %rax
movq %rax, 0x38(%rbx)
movb $0x1, %al
jmp 0x86a9
cmpb $0x0, 0x4(%rbx)
jne 0x8684
cmpl $0x4, %eax
jne 0x8730
jmp 0x86b5
movl $0x4, (%rbx)
cmpb $0x0, 0x4(%rbx)
je 0x86b5
movq 0x38(%rbx), %rcx
cmpq 0x30(%rbx), %rcx
setb %al
jae 0x86a9
movq %rcx, (%r14)
movq 0x38(%rbx), %rcx
addq $0x8, %rcx
movq %rcx, 0x38(%rbx)
movq (%r14), %rdx
cmpq $0x0, (%rdx)
je 0x8688
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movl $0x5, (%rbx)
movq 0x18(%rbx), %rsi
cmpq 0x8(%rbx), %rsi
je 0x8739
cmpq 0x10(%rbx), %rsi
je 0x8739
movq %r15, %rdi
callq 0x89da
testb %al, %al
je 0x872e
addq $0x18, %rbx
movq (%rbx), %rdi
movq %r15, %rsi
callq 0x8d73
testb %al, %al
je 0x8972
movq (%rbx), %rdi
callq 0x739a
testb %al, %al
jne 0x898b
movq (%rbx), %r15
movq %r15, %rdi
callq 0x73f4
cmpq %rax, %r15
je 0x8578
leaq 0x20788(%rip), %rdi     # 0x28ea1
leaq 0x205e3(%rip), %rsi     # 0x28d03
leaq 0x205e9(%rip), %rcx     # 0x28d10
pushq $0x56
jmp 0x89d4
movl (%rbx), %eax
cmpl $0x5, %eax
jne 0x87ba
movl $0x6, (%rbx)
movq 0x20(%rbx), %rsi
cmpq 0x18(%rbx), %rsi
je 0x87c3
cmpq 0x8(%rbx), %rsi
je 0x87c3
cmpq 0x10(%rbx), %rsi
je 0x87c3
movq %r15, %rdi
callq 0x89da
testb %al, %al
je 0x87b8
addq $0x20, %rbx
movq (%rbx), %rdi
movq %r15, %rsi
callq 0x8d73
testb %al, %al
je 0x89a4
movq (%rbx), %rdi
callq 0x739a
testb %al, %al
jne 0x89bd
movq (%rbx), %r15
movq %r15, %rdi
callq 0x73f4
cmpq %rax, %r15
je 0x8578
leaq 0x2075d(%rip), %rdi     # 0x28f00
leaq 0x20559(%rip), %rsi     # 0x28d03
leaq 0x2055f(%rip), %rcx     # 0x28d10
pushq $0x64
jmp 0x89d4
movl (%rbx), %eax
cmpl $0x6, %eax
jne 0x8684
movl $0x7, (%rbx)
movq 0x38(%rbx), %rax
cmpq 0x28(%rbx), %rax
jb 0x8908
movq 0x30(%rbx), %r12
cmpq %r12, %rax
ja 0x8924
pushq $0x1
popq %rcx
movq %r12, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x7482
movq %rax, 0x30(%rbx)
cmpq %rax, %r12
jae 0x8684
movq (%r12), %rcx
cmpq 0x8(%rbx), %rcx
je 0x881d
cmpq 0x10(%rbx), %rcx
je 0x881d
cmpq 0x18(%rbx), %rcx
je 0x881d
cmpq 0x20(%rbx), %rcx
jne 0x8822
andq $0x0, (%r12)
addq $0x8, %r12
jmp 0x87f8
leaq 0x205d2(%rip), %rdi     # 0x28e01
leaq 0x204cd(%rip), %rsi     # 0x28d03
leaq 0x204d3(%rip), %rcx     # 0x28d10
pushq $0x3a
jmp 0x89d4
leaq 0x2049d(%rip), %rdi     # 0x28ce8
leaq 0x204b1(%rip), %rsi     # 0x28d03
leaq 0x204b7(%rip), %rcx     # 0x28d10
pushq $0x11
jmp 0x89d4
leaq 0x20528(%rip), %rdi     # 0x28d8f
leaq 0x20495(%rip), %rsi     # 0x28d03
leaq 0x2049b(%rip), %rcx     # 0x28d10
pushq $0x23
jmp 0x89d4
leaq 0x20536(%rip), %rdi     # 0x28db9
leaq 0x20479(%rip), %rsi     # 0x28d03
leaq 0x2047f(%rip), %rcx     # 0x28d10
pushq $0x24
jmp 0x89d4
leaq 0x204a2(%rip), %rdi     # 0x28d41
leaq 0x2045d(%rip), %rsi     # 0x28d03
leaq 0x20463(%rip), %rcx     # 0x28d10
pushq $0x16
jmp 0x89d4
leaq 0x2049f(%rip), %rdi     # 0x28d5a
leaq 0x20441(%rip), %rsi     # 0x28d03
leaq 0x20447(%rip), %rcx     # 0x28d10
pushq $0x17
jmp 0x89d4
leaq 0x2046a(%rip), %rdi     # 0x28d41
leaq 0x20425(%rip), %rsi     # 0x28d03
leaq 0x2042b(%rip), %rcx     # 0x28d10
pushq $0x31
jmp 0x89d4
leaq 0x20467(%rip), %rdi     # 0x28d5a
leaq 0x20409(%rip), %rsi     # 0x28d03
leaq 0x2040f(%rip), %rcx     # 0x28d10
pushq $0x32
jmp 0x89d4
leaq 0x203d9(%rip), %rdi     # 0x28ce8
leaq 0x203ed(%rip), %rsi     # 0x28d03
leaq 0x203f3(%rip), %rcx     # 0x28d10
pushq $0x6d
jmp 0x89d4
leaq 0x205fb(%rip), %rdi     # 0x28f26
leaq 0x203d1(%rip), %rsi     # 0x28d03
leaq 0x203d7(%rip), %rcx     # 0x28d10
pushq $0x6e
jmp 0x89d4
leaq 0x20498(%rip), %rdi     # 0x28ddf
leaq 0x203b5(%rip), %rsi     # 0x28d03
leaq 0x203bb(%rip), %rcx     # 0x28d10
pushq $0x27
jmp 0x89d4
leaq 0x204c3(%rip), %rdi     # 0x28e23
leaq 0x2039c(%rip), %rsi     # 0x28d03
leaq 0x203a2(%rip), %rcx     # 0x28d10
pushq $0x44
jmp 0x89d4
leaq 0x204ef(%rip), %rdi     # 0x28e68
leaq 0x20383(%rip), %rsi     # 0x28d03
leaq 0x20389(%rip), %rcx     # 0x28d10
pushq $0x54
jmp 0x89d4
leaq 0x204f6(%rip), %rdi     # 0x28e88
leaq 0x2036a(%rip), %rsi     # 0x28d03
leaq 0x20370(%rip), %rcx     # 0x28d10
pushq $0x55
jmp 0x89d4
leaq 0x2051c(%rip), %rdi     # 0x28ec7
leaq 0x20351(%rip), %rsi     # 0x28d03
leaq 0x20357(%rip), %rcx     # 0x28d10
pushq $0x62
jmp 0x89d4
leaq 0x20523(%rip), %rdi     # 0x28ee7
leaq 0x20338(%rip), %rsi     # 0x28d03
leaq 0x2033e(%rip), %rcx     # 0x28d10
pushq $0x63
popq %rdx
callq 0x20c0