// File: /workspace/github_cpp_makefile_star2_O3/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 $0x698, %rsp            # imm = 0x698
movl %edx, %r12d
movq %rsi, %r15
movq %rdi, %rbx
movl $0x0, (%rdi)
testl %edx, %edx
jne 0xd771
cmpb $0x0, 0x86f227(%rip)    # 0x87c965
jne 0xd771
cmpl $0x1e, 0x144(%r15)
ja 0xd771
movq %r15, %rdi
callq 0x465c
testq %rax, %rax
je 0xd771
movq %rax, %r14
leaq 0x1f293(%rip), %rdi     # 0x2c9f4
xorl %eax, %eax
callq 0x65d9
movq %r14, 0x8(%rbx)
jmp 0xd822
leaq 0x86f3f8(%rip), %r13    # 0x87cb70
movq %r13, %rdi
movq %r15, %rsi
movl $0x1, %edx
movl $0x1, %ecx
callq 0x78e0
movq %rax, %r14
movq %r13, %rdi
movq %rax, %rsi
movl $0x1, %edx
movl $0x1, %ecx
callq 0xe70f
movl %eax, %ebp
leaq 0x1f253(%rip), %rdi     # 0x2ca01
movl %eax, %esi
xorl %eax, %eax
callq 0x65d9
cmpl $0x1, %ebp
setne %al
orb %r12b, %al
je 0xd808
leaq 0xea727(%rip), %rdi     # 0xf7ef0
callq 0x62b6
leaq 0xea74b(%rip), %rdi     # 0xf7f20
callq 0x62b6
callq 0xdf08
movl %eax, %edi
callq 0xb2c9
testb %r12b, %r12b
movq %r15, (%rsp)
movl %r12d, 0xc(%rsp)
je 0xd82d
callq 0xb2c2
addl 0x86f161(%rip), %eax    # 0x87c960
movl %eax, %edi
callq 0xb2d0
jmp 0xd832
leaq 0x86f361(%rip), %rdi    # 0x87cb70
movq %r14, %rsi
callq 0x954b
movq 0x86f352(%rip), %rax    # 0x87cb70
movq %rax, 0x8(%rbx)
movl $0x1, (%rbx)
jmp 0xda3f
callq 0xd333
leaq 0x40(%rsp), %rdi
leaq 0x368(%rsp), %r14
xorps %xmm0, %xmm0
movups %xmm0, -0x14(%rdi)
movaps %xmm0, -0x20(%rdi)
movaps %xmm0, -0x30(%rdi)
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 %r13d, %r13d
movl %r15d, %r12d
leal 0x1(%r12), %r15d
leal -0x21(%r13), %ebp
addl $0x21, %r13d
cmpl $0x2, %r12d
movl $0xfffcf2c0, %eax       # imm = 0xFFFCF2C0
cmovbl %eax, %ebp
movl %r13d, %r14d
movl $0x30d40, %eax          # imm = 0x30D40
cmovbl %eax, %r14d
movzbl 0x34906(%rip), %eax     # 0x421a4
subq $0x8, %rsp
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
movl %ebp, %edx
movl %r14d, %ecx
movl %r15d, %r8d
leaq 0x18(%rsp), %r9
pushq %rax
leaq 0x878ef1(%rip), %rax    # 0x8867b0
pushq %rax
leaq 0x86f2a9(%rip), %rax    # 0x87cb70
pushq %rax
callq 0xb394
addq $0x20, %rsp
movl %eax, %r13d
cmpl %ebp, %eax
jle 0xd8dd
cmpl %r14d, %r13d
jl 0xd941
callq 0xb2b3
testb %al, %al
jne 0xd941
leaq 0x1f12f(%rip), %rdi     # 0x2ca1c
movl %r15d, %esi
movl %ebp, %edx
movl %r14d, %ecx
movl %r13d, %r8d
xorl %eax, %eax
callq 0x65d9
movzbl 0x3489e(%rip), %eax     # 0x421a4
subq $0x8, %rsp
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
movl $0xfffcf2c0, %edx       # imm = 0xFFFCF2C0
movl $0x30d40, %ecx          # imm = 0x30D40
movl %r15d, %r8d
leaq 0x18(%rsp), %r9
pushq %rax
leaq 0x878e84(%rip), %rax    # 0x8867b0
pushq %rax
leaq 0x86f23c(%rip), %rax    # 0x87cb70
pushq %rax
callq 0xb394
addq $0x20, %rsp
movl %eax, %r13d
cmpl $0x0, (%rbx)
jle 0xda54
callq 0xb2b3
testb %al, %al
jne 0xda1a
cmpb $0x1, 0x34846(%rip)     # 0x421a4
jne 0xd97a
callq 0xb2c2
movq 0x28(%rsp), %r8
movq %rbx, %rdi
movl %r15d, %esi
movl %r13d, %edx
movl %eax, %ecx
callq 0xea5e
movl $0x328, %edx            # imm = 0x328
leaq 0x38(%rsp), %rdi
movq %rbx, %rsi
callq 0x2100
movl %r13d, %eax
negl %eax
cmovsl %r13d, %eax
cmpl $0xfe0c, %eax           # imm = 0xFE0C
setae %r14b
jb 0xd9ae
leaq 0x1f0d5(%rip), %rdi     # 0x2ca7c
xorl %eax, %eax
callq 0x65d9
movl 0x86efa8(%rip), %eax    # 0x87c95c
testl %eax, %eax
setle %cl
cmpl %eax, %r15d
setl %al
orb %cl, %al
jne 0xd9d4
leaq 0x1f0e3(%rip), %rdi     # 0x2caad
xorl %eax, %eax
callq 0x65d9
movb $0x1, %r14b
callq 0xdf08
movl %eax, %ebp
callq 0xb2c2
movb 0x86ef7f(%rip), %cl     # 0x87c965
orb 0xc(%rsp), %cl
jne 0xda01
subl %eax, %ebp
movl 0x86ef6c(%rip), %eax    # 0x87c960
movl %eax, %ecx
shrl $0x1f, %ecx
addl %eax, %ecx
sarl %ecx
cmpl %ecx, %ebp
jg 0xda0c
testb %r14b, %r14b
je 0xd86f
jmp 0xda1a
leaq 0x1f0c1(%rip), %rdi     # 0x2cad4
xorl %eax, %eax
callq 0x65d9
cmpl $0x0, (%rbx)
jle 0xda73
movq %rbx, %rdi
movq (%rsp), %rsi
callq 0xe594
testb %al, %al
je 0xda92
incl %r12d
leaq 0x10(%rsp), %rsi
movl %r12d, %edi
callq 0xdaf7
movq %rbx, %rax
addq $0x698, %rsp            # imm = 0x698
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1eff4(%rip), %rdi     # 0x2ca4f
leaq 0x1ee1e(%rip), %rsi     # 0x2c880
leaq 0x1efef(%rip), %rcx     # 0x2ca58
movl $0xdd, %edx
callq 0x20d0
leaq 0x1efd5(%rip), %rdi     # 0x2ca4f
leaq 0x1edff(%rip), %rsi     # 0x2c880
leaq 0x1efd0(%rip), %rcx     # 0x2ca58
movl $0xfc, %edx
callq 0x20d0
leaq 0x1f073(%rip), %rdi     # 0x2cb0c
leaq 0x1ede0(%rip), %rsi     # 0x2c880
leaq 0x1efb1(%rip), %rcx     # 0x2ca58
movl $0xfd, %edx
callq 0x20d0