// File: /workspace/github_cpp_makefile_star2_O2/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
andl $0x0, (%rdi)
testl %edx, %edx
jne 0xcd6f
cmpb $0x0, 0x872c29(%rip)    # 0x87f965
jne 0xcd6f
cmpl $0x1e, 0x144(%r15)
ja 0xcd6f
movq %r15, %rdi
callq 0x4439
testq %rax, %rax
je 0xcd6f
movq %rax, %r14
leaq 0x1dc45(%rip), %rdi     # 0x2a9a4
xorl %eax, %eax
callq 0x60fd
movq %r14, 0x8(%rbx)
jmp 0xce18
leaq 0x872dfa(%rip), %r13    # 0x87fb70
pushq $0x1
popq %rbp
movq %r13, %rdi
movq %r15, %rsi
movl %ebp, %edx
movl %ebp, %ecx
callq 0x73f8
movq %rax, %r14
movq %r13, %rdi
movq %rax, %rsi
movl %ebp, %edx
movl %ebp, %ecx
callq 0xdb4d
movl %eax, %ebp
leaq 0x1dc0e(%rip), %rdi     # 0x2a9b1
movl %eax, %esi
xorl %eax, %eax
callq 0x60fd
cmpl $0x1, %ebp
setne %al
orb %r12b, %al
je 0xcdfe
leaq 0xee132(%rip), %rdi     # 0xfaef0
callq 0x5dd9
leaq 0xee156(%rip), %rdi     # 0xfaf20
callq 0x5dd9
callq 0xd470
movl %eax, %edi
callq 0xa95d
testb %r12b, %r12b
movq %r15, 0x8(%rsp)
movl %r12d, 0x14(%rsp)
je 0xce23
callq 0xa956
addl 0x872b6b(%rip), %eax    # 0x87f960
movl %eax, %edi
callq 0xa964
jmp 0xce28
leaq 0x872d6b(%rip), %rdi    # 0x87fb70
movq %r14, %rsi
callq 0x8de5
movq 0x872d5c(%rip), %rax    # 0x87fb70
movq %rax, 0x8(%rbx)
movl $0x1, (%rbx)
jmp 0xd006
callq 0xc92f
leaq 0x18(%rsp), %rdi
callq 0xd078
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 0x3833d(%rip), %eax     # 0x451a4
subq $0x8, %rsp
movq 0x10(%rsp), %rdi
movq %rbx, %rsi
movl %ebp, %edx
movl %r14d, %ecx
movl %r15d, %r8d
leaq 0x20(%rsp), %r9
pushq %rax
leaq 0x87c928(%rip), %rax    # 0x8897b0
pushq %rax
leaq 0x872ce0(%rip), %rax    # 0x87fb70
pushq %rax
callq 0xaa1d
addq $0x20, %rsp
movl %eax, %r13d
cmpl %ebp, %eax
jle 0xcea6
cmpl %r14d, %r13d
jl 0xcf0a
callq 0xa947
testb %al, %al
jne 0xcf0a
leaq 0x1db16(%rip), %rdi     # 0x2a9cc
movl %r15d, %esi
movl %ebp, %edx
movl %r14d, %ecx
movl %r13d, %r8d
xorl %eax, %eax
callq 0x60fd
movzbl 0x382d5(%rip), %eax     # 0x451a4
subq $0x8, %rsp
movq 0x10(%rsp), %rdi
movq %rbx, %rsi
movl $0xfffcf2c0, %edx       # imm = 0xFFFCF2C0
movl $0x30d40, %ecx          # imm = 0x30D40
movl %r15d, %r8d
leaq 0x20(%rsp), %r9
pushq %rax
leaq 0x87c8bb(%rip), %rax    # 0x8897b0
pushq %rax
leaq 0x872c73(%rip), %rax    # 0x87fb70
pushq %rax
callq 0xaa1d
addq $0x20, %rsp
movl %eax, %r13d
cmpl $0x0, (%rbx)
jle 0xd01b
callq 0xa947
testb %al, %al
jne 0xcfe0
cmpb $0x1, 0x3827d(%rip)     # 0x451a4
jne 0xcf43
callq 0xa956
movq 0x30(%rsp), %r8
movq %rbx, %rdi
movl %r15d, %esi
movl %r13d, %edx
movl %eax, %ecx
callq 0xde4e
movl $0x328, %edx            # imm = 0x328
leaq 0x40(%rsp), %rdi
movq %rbx, %rsi
callq 0x20f0
movl %r13d, %eax
negl %eax
cmovsl %r13d, %eax
cmpl $0xfe0c, %eax           # imm = 0xFE0C
setae %r14b
jb 0xcf77
leaq 0x1dabc(%rip), %rdi     # 0x2aa2c
xorl %eax, %eax
callq 0x60fd
movl 0x8729df(%rip), %eax    # 0x87f95c
testl %eax, %eax
setle %cl
cmpl %eax, %r15d
setl %al
orb %cl, %al
jne 0xcf9d
leaq 0x1daca(%rip), %rdi     # 0x2aa5d
xorl %eax, %eax
callq 0x60fd
movb $0x1, %r14b
callq 0xd470
movl %eax, %ebp
callq 0xa956
movb 0x8729b6(%rip), %cl     # 0x87f965
orb 0x14(%rsp), %cl
jne 0xcfc7
subl %eax, %ebp
movl 0x8729a3(%rip), %eax    # 0x87f960
cltd
pushq $0x2
popq %rcx
idivl %ecx
cmpl %eax, %ebp
jg 0xcfd2
testb %r14b, %r14b
je 0xce38
jmp 0xcfe0
leaq 0x1daab(%rip), %rdi     # 0x2aa84
xorl %eax, %eax
callq 0x60fd
cmpl $0x0, (%rbx)
jle 0xd03a
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
callq 0xda1e
testb %al, %al
je 0xd059
incl %r12d
leaq 0x18(%rsp), %rsi
movl %r12d, %edi
callq 0xd0bd
movq %rbx, %rax
addq $0x698, %rsp            # imm = 0x698
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1d9dd(%rip), %rdi     # 0x2a9ff
leaq 0x1d807(%rip), %rsi     # 0x2a830
leaq 0x1d9d8(%rip), %rcx     # 0x2aa08
movl $0xdd, %edx
callq 0x20c0
leaq 0x1d9be(%rip), %rdi     # 0x2a9ff
leaq 0x1d7e8(%rip), %rsi     # 0x2a830
leaq 0x1d9b9(%rip), %rcx     # 0x2aa08
movl $0xfc, %edx
callq 0x20c0
leaq 0x1da5c(%rip), %rdi     # 0x2aabc
leaq 0x1d7c9(%rip), %rsi     # 0x2a830
leaq 0x1d99a(%rip), %rcx     # 0x2aa08
movl $0xfd, %edx
callq 0x20c0