// File: /workspace/github_cpp_makefile_star2_O1/jswaff[P]prophet3/src/think.cpp
// Function: iterate(position*, bool)
move_line iterate(position *pos,bool test_suite_mode) {

    move_line pv; pv.n=0;

    if (!test_suite_mode && !pondering && pos->move_counter <= 30) {
        move book_mv = probe_book(pos);
        if (book_mv) {
            printd("# book move\n");
            pv.mv[0] = book_mv;
            pv.n = 1;
            return pv;
        }
    }

    // if just one legal move don't bother searching
    move *endp = gen_legal_moves(search_moves,pos,true,true);
    int num_mvs = num_moves(search_moves,endp,true,true);
    printd("# position has %d move(s)\n",num_mvs);
    if (!test_suite_mode && num_mvs==1) {
        best_at_top(search_moves,endp);
        pv.mv[0] = search_moves[0];
        pv.n = 1;
        return pv;
    }

    clear_hash_table(&htbl);
    clear_hash_table(&phtbl);
    set_start_time(milli_timer());
    if (test_suite_mode) {
        set_stop_time(get_start_time() + max_time);
    } else {
        calculate_search_times();
    }

    search_stats stats = build_search_stats_dto();

    int depth = 0;
    bool stop_searching = false;

    int32 score = 0;
    do {
        ++depth;

        // aspiration windows
        int32 alpha_bound = -INF;
        int32 beta_bound = INF;
        if (depth > 2) {
            alpha_bound = score - (pawn_val / 3);
            beta_bound = score + (pawn_val / 3);
        }

        score=search(pos,&pv,alpha_bound,beta_bound,depth,&stats,search_moves,search_undos,post);

        if ((score <= alpha_bound || score >= beta_bound) && !is_abort_search()) {
            printd("# research depth %d!  alpha=%d, beta=%d, score=%d\n",depth,alpha_bound,beta_bound,score);
            score=search(pos,&pv,-INF,INF,depth,&stats,search_moves,search_undos,post);
        }

        assert(pv.n > 0);
        if (is_abort_search()) {
            break;
        }

        if (post) {
            print_thinking_output(&pv,depth,score,get_start_time(),stats.nodes);
        }
        memcpy(&stats.last_pv,&pv,sizeof(move_line));

        //char first_line_text[100];
        //line_to_str(&stats.first_line_searched,first_line_text);
        //print("first line: %s\n",first_line_text);

        // if this is a mate score we can stop
        if (abs(score) > CHECKMATE-500) {
            printd("# stopping iterative search because mate found.\n");
            stop_searching = true;
        }

        if (max_depth > 0 && depth >= max_depth) {
            printd("# stopping iterative search on depth.\n");
            stop_searching = true;
        }
        int32 elapsed_time = milli_timer() - get_start_time();
        if (!pondering && !test_suite_mode && elapsed_time > (max_time / 2)) {
            printd("# stopping iterative search because half time expired.\n");
            stop_searching = true;
        }
    } while (!stop_searching);

    assert(pv.n > 0);
    assert(is_line_valid(&pv,pos));

    print_search_summary(depth,&stats);


    return pv;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x6a8, %rsp            # imm = 0x6A8
movl %edx, %r12d
movq %rsi, %r15
movq %rdi, %rbx
movl $0x0, (%rdi)
testl %edx, %edx
jne 0xd7a6
cmpb $0x0, 0x86f1f2(%rip)    # 0x87c965
jne 0xd7a6
cmpl $0x1e, 0x144(%r15)
ja 0xd7a6
movq %r15, %rdi
callq 0x4612
testq %rax, %rax
je 0xd7a6
movq %rax, %r14
leaq 0x1f1de(%rip), %rdi     # 0x2c974
xorl %eax, %eax
callq 0x652d
movq %r14, 0x8(%rbx)
jmp 0xd858
leaq 0x86f3c3(%rip), %r13    # 0x87cb70
movq %r13, %rdi
movq %r15, %rsi
movl $0x1, %edx
movl $0x1, %ecx
callq 0x7840
movq %rax, %r14
movq %r13, %rdi
movq %rax, %rsi
movl $0x1, %edx
movl $0x1, %ecx
callq 0xe7c9
movl %eax, %ebp
leaq 0x1f19e(%rip), %rdi     # 0x2c981
movl %eax, %esi
xorl %eax, %eax
callq 0x652d
cmpl $0x1, %ebp
setne %al
orb %r12b, %al
je 0xd83e
leaq 0xea6f2(%rip), %rdi     # 0xf7ef0
callq 0x6207
leaq 0xea716(%rip), %rdi     # 0xf7f20
callq 0x6207
callq 0xdf58
movl %eax, %edi
callq 0xb2cd
testb %r12b, %r12b
movq %r15, 0x10(%rsp)
movl %r12d, 0x1c(%rsp)
je 0xd863
callq 0xb2c6
addl 0x86f12b(%rip), %eax    # 0x87c960
movl %eax, %edi
callq 0xb2d4
jmp 0xd868
leaq 0x86f32b(%rip), %rdi    # 0x87cb70
movq %r14, %rsi
callq 0x9496
movq 0x86f31c(%rip), %rax    # 0x87cb70
movq %rax, 0x8(%rbx)
movl $0x1, (%rbx)
jmp 0xda91
callq 0xd343
leaq 0x50(%rsp), %rdi
leaq 0x378(%rsp), %r14
xorps %xmm0, %xmm0
movups %xmm0, -0x14(%rdi)
movaps %xmm0, -0x20(%rdi)
movaps %xmm0, -0x30(%rdi)
movl $0x0, 0xc(%rsp)
movl $0x324, %edx            # imm = 0x324
xorl %esi, %esi
callq 0x20c0
movl $0x328, %edx            # imm = 0x328
movq %r14, %rdi
xorl %esi, %esi
callq 0x20c0
xorl %r15d, %r15d
xorl %ebp, %ebp
movl %r15d, %r12d
leal 0x1(%r12), %r15d
leal -0x21(%rbp), %r13d
addl $0x21, %ebp
cmpl $0x2, %r12d
movl $0xfffcf2c0, %eax       # imm = 0xFFFCF2C0
cmovbl %eax, %r13d
movl %ebp, %r14d
movl $0x30d40, %eax          # imm = 0x30D40
cmovbl %eax, %r14d
movzbl 0x348c9(%rip), %eax     # 0x421a4
subq $0x8, %rsp
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
movl %r13d, %edx
movl %r14d, %ecx
movl %r15d, %r8d
leaq 0x28(%rsp), %r9
pushq %rax
leaq 0x878eb3(%rip), %rax    # 0x8867b0
pushq %rax
leaq 0x86f26b(%rip), %rax    # 0x87cb70
pushq %rax
callq 0xb398
addq $0x20, %rsp
movl %eax, %ebp
cmpl %r13d, %eax
jle 0xd91b
cmpl %r14d, %ebp
jl 0xd97f
callq 0xb2b7
testb %al, %al
jne 0xd97f
leaq 0x1f071(%rip), %rdi     # 0x2c99c
movl %r15d, %esi
movl %r13d, %edx
movl %r14d, %ecx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x652d
movzbl 0x3485f(%rip), %eax     # 0x421a4
subq $0x8, %rsp
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
movl $0xfffcf2c0, %edx       # imm = 0xFFFCF2C0
movl $0x30d40, %ecx          # imm = 0x30D40
movl %r15d, %r8d
leaq 0x28(%rsp), %r9
pushq %rax
leaq 0x878e45(%rip), %rax    # 0x8867b0
pushq %rax
leaq 0x86f1fd(%rip), %rax    # 0x87cb70
pushq %rax
callq 0xb398
addq $0x20, %rsp
movl %eax, %ebp
cmpl $0x0, (%rbx)
jle 0xdaa6
callq 0xb2b7
movl %eax, %r13d
testb %al, %al
jne 0xda5b
cmpb $0x1, 0x34805(%rip)     # 0x421a4
jne 0xd9ba
callq 0xb2c6
movq 0x38(%rsp), %r8
movq %rbx, %rdi
movl %r15d, %esi
movl %ebp, %edx
movl %eax, %ecx
callq 0xeb12
movl $0x328, %edx            # imm = 0x328
leaq 0x48(%rsp), %rdi
movq %rbx, %rsi
callq 0x2100
movl %ebp, %eax
negl %eax
cmovsl %ebp, %eax
cmpl $0xfe0c, %eax           # imm = 0xFE0C
jb 0xd9ee
leaq 0x1f01b(%rip), %rdi     # 0x2c9fc
xorl %eax, %eax
callq 0x652d
movb $0x1, %al
movl %eax, 0xc(%rsp)
movl 0x86ef68(%rip), %eax    # 0x87c95c
testl %eax, %eax
setle %cl
cmpl %eax, %r15d
setl %al
orb %cl, %al
jne 0xda17
leaq 0x1f023(%rip), %rdi     # 0x2ca2d
xorl %eax, %eax
callq 0x652d
movb $0x1, %al
movl %eax, 0xc(%rsp)
callq 0xdf58
movl %eax, %r14d
callq 0xb2c6
movb 0x86ef3b(%rip), %cl     # 0x87c965
orb 0x1c(%rsp), %cl
jne 0xda5b
subl %eax, %r14d
movl 0x86ef27(%rip), %eax    # 0x87c960
movl %eax, %ecx
shrl $0x1f, %ecx
addl %eax, %ecx
sarl %ecx
cmpl %ecx, %r14d
jle 0xda5b
leaq 0x1f006(%rip), %rdi     # 0x2ca54
xorl %eax, %eax
callq 0x652d
movb $0x1, %al
movl %eax, 0xc(%rsp)
testb %r13b, %r13b
jne 0xda6b
testb $0x1, 0xc(%rsp)
je 0xd8ac
cmpl $0x0, (%rbx)
jle 0xdac5
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
callq 0xe603
testb %al, %al
je 0xdae4
incl %r12d
leaq 0x20(%rsp), %rsi
movl %r12d, %edi
callq 0xdb49
movq %rbx, %rax
addq $0x6a8, %rsp            # imm = 0x6A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1ef22(%rip), %rdi     # 0x2c9cf
leaq 0x1ed4c(%rip), %rsi     # 0x2c800
leaq 0x1ef1d(%rip), %rcx     # 0x2c9d8
movl $0xdd, %edx
callq 0x20d0
leaq 0x1ef03(%rip), %rdi     # 0x2c9cf
leaq 0x1ed2d(%rip), %rsi     # 0x2c800
leaq 0x1eefe(%rip), %rcx     # 0x2c9d8
movl $0xfc, %edx
callq 0x20d0
leaq 0x1efa1(%rip), %rdi     # 0x2ca8c
leaq 0x1ed0e(%rip), %rsi     # 0x2c800
leaq 0x1eedf(%rip), %rcx     # 0x2c9d8
movl $0xfd, %edx
callq 0x20d0