// File: 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
movq %rsp, %rbp
subq $0x700, %rsp            # imm = 0x700
movq %rdi, -0x6d0(%rbp)
movb %dl, %al
movq %rdi, %rcx
movq %rcx, -0x6c8(%rbp)
movq %rsi, -0x8(%rbp)
andb $0x1, %al
movb %al, -0x9(%rbp)
movl $0x0, (%rdi)
testb $0x1, -0x9(%rbp)
jne 0x135e8
movb 0x87458c(%rip), %al     # 0x887b25
testb $0x1, %al
jne 0x135e8
movq -0x8(%rbp), %rax
cmpl $0x1e, 0x144(%rax)
ja 0x135e8
movq -0x8(%rbp), %rdi
callq 0x5c50
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x135e6
leaq 0x2536f(%rip), %rdi     # 0x38934
movb $0x0, %al
callq 0x8db0
movq -0x6d0(%rbp), %rax
movq -0x18(%rbp), %rcx
movq %rcx, 0x8(%rax)
movl $0x1, (%rax)
jmp 0x139fd
jmp 0x135e8
movq -0x8(%rbp), %rsi
leaq 0x87473d(%rip), %rdi    # 0x887d30
movl $0x1, %ecx
movl %ecx, %edx
callq 0xac70
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rsi
leaq 0x874722(%rip), %rdi    # 0x887d30
movl $0x1, %ecx
movl %ecx, %edx
callq 0x14d00
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %esi
leaq 0x2531a(%rip), %rdi     # 0x38941
movb $0x0, %al
callq 0x8db0
testb $0x1, -0x9(%rbp)
jne 0x13667
cmpl $0x1, -0x24(%rbp)
jne 0x13667
movq -0x20(%rbp), %rsi
leaq 0x8746eb(%rip), %rdi    # 0x887d30
callq 0xd4b0
movq -0x6d0(%rbp), %rax
movq 0x8746d8(%rip), %rcx    # 0x887d30
movq %rcx, 0x8(%rax)
movl $0x1, (%rax)
jmp 0x139fd
leaq 0xefa42(%rip), %rdi     # 0x1030b0
callq 0x88c0
leaq 0xefa66(%rip), %rdi     # 0x1030e0
callq 0x88c0
callq 0x14080
movl %eax, %edi
callq 0x10230
testb $0x1, -0x9(%rbp)
je 0x136a5
callq 0x10220
movl %eax, %edi
addl 0x874482(%rip), %edi    # 0x887b20
callq 0x10250
jmp 0x136aa
callq 0x13110
leaq -0x6a8(%rbp), %rdi
callq 0x13a10
movl $0x0, -0x6ac(%rbp)
movb $0x0, -0x6ad(%rbp)
movl $0x0, -0x6b4(%rbp)
movl -0x6ac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6ac(%rbp)
movl $0xfffcf2c0, -0x6b8(%rbp) # imm = 0xFFFCF2C0
movl $0x30d40, -0x6bc(%rbp)  # imm = 0x30D40
cmpl $0x2, -0x6ac(%rbp)
jle 0x1371b
movl -0x6b4(%rbp), %eax
subl $0x21, %eax
movl %eax, -0x6b8(%rbp)
movl -0x6b4(%rbp), %eax
addl $0x21, %eax
movl %eax, -0x6bc(%rbp)
movq -0x6d0(%rbp), %rsi
movq -0x8(%rbp), %rdi
movl -0x6b8(%rbp), %edx
movl -0x6bc(%rbp), %ecx
movl -0x6ac(%rbp), %r8d
movb 0x39a65(%rip), %al      # 0x4d1a4
leaq -0x6a8(%rbp), %r9
leaq 0x8745e3(%rip), %r11    # 0x887d30
leaq 0x87e21c(%rip), %r10    # 0x891970
andb $0x1, %al
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movzbl %al, %eax
movl %eax, 0x10(%rsp)
callq 0x10350
movl %eax, -0x6b4(%rbp)
movl -0x6b4(%rbp), %eax
cmpl -0x6b8(%rbp), %eax
jle 0x13791
movl -0x6b4(%rbp), %eax
cmpl -0x6bc(%rbp), %eax
jl 0x13815
callq 0x101f0
testb $0x1, %al
jne 0x13815
movl -0x6ac(%rbp), %esi
movl -0x6b8(%rbp), %edx
movl -0x6bc(%rbp), %ecx
movl -0x6b4(%rbp), %r8d
leaq 0x251a2(%rip), %rdi     # 0x3895c
movb $0x0, %al
callq 0x8db0
movq -0x6d0(%rbp), %rsi
movq -0x8(%rbp), %rdi
movl -0x6ac(%rbp), %r8d
movb 0x399cb(%rip), %al      # 0x4d1a4
movl $0xfffcf2c0, %edx       # imm = 0xFFFCF2C0
movl $0x30d40, %ecx          # imm = 0x30D40
leaq -0x6a8(%rbp), %r9
leaq 0x87453f(%rip), %r11    # 0x887d30
leaq 0x87e178(%rip), %r10    # 0x891970
andb $0x1, %al
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movzbl %al, %eax
movl %eax, 0x10(%rsp)
callq 0x10350
movl %eax, -0x6b4(%rbp)
movq -0x6d0(%rbp), %rax
cmpl $0x0, (%rax)
jle 0x13823
jmp 0x13842
leaq 0x25165(%rip), %rdi     # 0x3898f
leaq 0x24f8f(%rip), %rsi     # 0x387c0
movl $0xdd, %edx
leaq 0x2515b(%rip), %rcx     # 0x38998
callq 0x20c0
callq 0x101f0
testb $0x1, %al
jne 0x1384d
jmp 0x13852
jmp 0x13987
testb $0x1, 0x3994b(%rip)     # 0x4d1a4
je 0x13899
movl -0x6ac(%rbp), %eax
movl %eax, -0x6d8(%rbp)
movl -0x6b4(%rbp), %eax
movl %eax, -0x6d4(%rbp)
callq 0x10220
movq -0x6d0(%rbp), %rdi
movl -0x6d8(%rbp), %esi
movl -0x6d4(%rbp), %edx
movl %eax, %ecx
movq -0x690(%rbp), %r8
callq 0x152c0
movq -0x6d0(%rbp), %rsi
leaq -0x6a8(%rbp), %rdi
addq $0x28, %rdi
movl $0x328, %edx            # imm = 0x328
callq 0x20f0
movl -0x6b4(%rbp), %eax
movl %eax, %ecx
negl %ecx
cmovnsl %ecx, %eax
cmpl $0xfe0b, %eax           # imm = 0xFE0B
jle 0x138de
leaq 0x250ec(%rip), %rdi     # 0x389bc
movb $0x0, %al
callq 0x8db0
movb $0x1, -0x6ad(%rbp)
cmpl $0x0, 0x874237(%rip)    # 0x887b1c
jle 0x1390a
movl -0x6ac(%rbp), %eax
cmpl 0x874229(%rip), %eax    # 0x887b1c
jl 0x1390a
leaq 0x250f1(%rip), %rdi     # 0x389ed
movb $0x0, %al
callq 0x8db0
movb $0x1, -0x6ad(%rbp)
callq 0x14080
movl %eax, -0x6dc(%rbp)
callq 0x10220
movl %eax, %ecx
movl -0x6dc(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x6c0(%rbp)
movb 0x8741f5(%rip), %al     # 0x887b25
testb $0x1, %al
jne 0x13975
testb $0x1, -0x9(%rbp)
jne 0x13975
movl -0x6c0(%rbp), %eax
movl %eax, -0x6e0(%rbp)
movl 0x8741d4(%rip), %eax    # 0x887b20
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0x6e0(%rbp), %eax
cmpl %ecx, %eax
jle 0x13975
leaq 0x250ad(%rip), %rdi     # 0x38a14
movb $0x0, %al
callq 0x8db0
movb $0x1, -0x6ad(%rbp)
jmp 0x13977
movb -0x6ad(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x136d1
movq -0x6d0(%rbp), %rax
cmpl $0x0, (%rax)
jle 0x13995
jmp 0x139b4
leaq 0x24ff3(%rip), %rdi     # 0x3898f
leaq 0x24e1d(%rip), %rsi     # 0x387c0
movl $0xfc, %edx
leaq 0x24fe9(%rip), %rcx     # 0x38998
callq 0x20c0
movq -0x6d0(%rbp), %rdi
movq -0x8(%rbp), %rsi
callq 0x14af0
testb $0x1, %al
jne 0x139ca
jmp 0x139cc
jmp 0x139eb
leaq 0x25079(%rip), %rdi     # 0x38a4c
leaq 0x24de6(%rip), %rsi     # 0x387c0
movl $0xfd, %edx
leaq 0x24fb2(%rip), %rcx     # 0x38998
callq 0x20c0
movl -0x6ac(%rbp), %edi
leaq -0x6a8(%rbp), %rsi
callq 0x13ab0
movq -0x6c8(%rbp), %rax
addq $0x700, %rsp            # imm = 0x700
popq %rbp
retq
nopl (%rax)