// File: /workspace/github_c_makefile_star2_O3/icarroll[P]clunk/thudlib.c
// Function: absearch
int absearch(struct thudboard * board, int depth, int width,
             int trmin, int dwmax, struct move * bestmove,
             time_t stoptime, jmp_buf stopsearch)
{
    if (stoptime && time(NULL) > stoptime) longjmp(stopsearch, true);

    if (depth < 1) return evaluate(board);

    struct move * move;
    bool cutoff = false;

    int nextdepth = depth - 1;

    bool isdwarfturn = board->isdwarfturn;

    struct movelist list = allmoves(board);
    struct moveheap queue = heapof(board, & list);

    for (int i = width; i > 0 && queue.used > 0; --i)
    {
        move = pop(& queue);

        domove(board, move);

        int result;

        struct tableentry * entry = ttget(board->hash);
        if (entry && entry->depth >= nextdepth)
        {
            if (entry->min == entry->max) result = entry->min;
            else
            {
                if (entry->max <= trmin || entry->min >= dwmax)
                {
                    result = entry->scoreguess;
                }
                else
                {
                    int windlow = min(trmin, entry->min);
                    int windhigh = max(dwmax, entry->max);
                    result = absearch(board, nextdepth, width,
                                      windlow, windhigh, NULL,
                                      stoptime, stopsearch);

                    if (result < entry->min || result > entry->max)
                    {
                        goto search;
                    }

                    if (entry->depth == nextdepth)
                    {
                        entry->scoreguess = result;
                        if (result < windhigh) entry->max = result;
                        if (result > windlow) entry->min = result;
                    }
                }
            }
        }
        else
        {
search:
            result = absearch(board, nextdepth, width,
                              trmin, dwmax, NULL,
                              stoptime, stopsearch);

            int min, max;
            min = max = result;
            if (result < trmin) min = INT_MIN;
            else if (result > dwmax) max = INT_MAX;

            if (! entry) ttput((struct tableentry)
                    {board->hash, nextdepth, nextdepth, result, min, max});
        }

        // the only problem with not doing negamax
        if (isdwarfturn)
        {
            if (result < dwmax)
            {
                dwmax = result;
                if (bestmove) * bestmove = * move;
            }
        }
        else
        {
            if (result > trmin)
            {
                trmin = result;
                if (bestmove) * bestmove = * move;
            }
        }

        undomove(board, move);

        if (trmin >= dwmax)
        {
            cutoff = true;
            break;
        }
    }

    closeheap(& queue);
    closelist(& list);

    return (isdwarfturn ^ cutoff) ? dwmax : trmin;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %r9, 0x40(%rsp)
movl %r8d, %r14d
movl %ecx, %r12d
movl %edx, %ebx
movq %rdi, %r13
movq 0xc8(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0xc0(%rsp), %rcx
testq %rcx, %rcx
je 0x1b3e
xorl %edi, %edi
movl %esi, %ebp
movq %rcx, %r15
callq 0x11d0
movq %r15, %rcx
movl %ebp, %esi
cmpq %r15, %rax
jg 0x1e41
testl %esi, %esi
jle 0x1b61
movq %rcx, 0x38(%rsp)
movl %esi, 0x14(%rsp)
movb (%r13), %al
movb %al, 0xb(%rsp)
movq %r13, %rdi
cmpb $0x1, %al
jne 0x1b7a
callq 0x21cf
jmp 0x1b7f
movq %r13, %rdi
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x219d
callq 0x24b2
leaq 0x78(%rsp), %rsi
movq %rax, (%rsi)
movq %rdx, 0x8(%rsi)
movq %r13, %rdi
callq 0x2055
movq %rax, 0x48(%rsp)
movq %rdx, 0x50(%rsp)
testl %ebx, %ebx
setg %cl
testl %eax, %eax
setg %al
andb %cl, %al
cmpb $0x1, %al
jne 0x1e0e
decl 0x14(%rsp)
movl %ebx, 0x24(%rsp)
movq %r13, 0x18(%rsp)
leaq 0x48(%rsp), %rdi
callq 0x3bab
movq %r13, %rdi
movq %rax, 0x28(%rsp)
movq %rax, %rsi
callq 0x20ec
movq 0x68(%r13), %rdi
callq 0x4500
testq %rax, %rax
je 0x1c37
movq %rax, %r15
movl %r12d, 0xc(%rsp)
movl %r14d, 0x10(%rsp)
movl 0x14(%rsp), %esi
cmpl %esi, 0x8(%rax)
jge 0x1cc4
movq 0x18(%rsp), %r13
movq %r13, %rdi
movl 0x24(%rsp), %edx
movl 0xc(%rsp), %r12d
movl %r12d, %ecx
movl 0x10(%rsp), %r14d
movl %r14d, %r8d
xorl %r9d, %r9d
pushq 0x30(%rsp)
pushq 0x40(%rsp)
callq 0x1ae9
addq $0x10, %rsp
movl %eax, %ebp
jmp 0x1d8b
movq %r13, %rdi
movl 0x14(%rsp), %r15d
movl %r15d, %esi
movl 0x24(%rsp), %edx
movl %r12d, %ecx
movl %r14d, %r8d
xorl %r9d, %r9d
pushq 0x30(%rsp)
pushq 0x40(%rsp)
callq 0x1ae9
addq $0x10, %rsp
movl %eax, %ebp
cmpl %r14d, %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmovgl %ecx, %eax
cmpl %r12d, %ebp
cmovll %ebp, %eax
movq 0x68(%r13), %rcx
movq %rcx, 0x58(%rsp)
movl %r15d, 0x60(%rsp)
movl %r15d, 0x64(%rsp)
movl %ebp, 0x68(%rsp)
movl %ebp, %ecx
movl $0x80000000, %edx       # imm = 0x80000000
cmovll %edx, %ecx
movl %ecx, 0x6c(%rsp)
movl %eax, 0x70(%rsp)
subq $0x20, %rsp
movups 0x78(%rsp), %xmm0
movups 0x88(%rsp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x44bb
addq $0x20, %rsp
jmp 0x1d8b
movl 0x14(%r15), %r12d
movl 0x18(%r15), %r14d
cmpl %r14d, %r12d
jne 0x1cd9
movl %r12d, %ebp
jmp 0x1d7c
cmpl 0xc(%rsp), %r14d
jle 0x1d78
cmpl 0x10(%rsp), %r12d
jge 0x1d78
movl 0xc(%rsp), %eax
cmpl %r12d, %eax
cmovll %eax, %r12d
movl 0x10(%rsp), %eax
cmpl %r14d, %eax
cmovgl %eax, %r14d
movq 0x18(%rsp), %rdi
movl 0x14(%rsp), %esi
movl 0x24(%rsp), %edx
movl %r12d, %ecx
movl %r14d, %r8d
xorl %r9d, %r9d
pushq 0x30(%rsp)
pushq 0x40(%rsp)
callq 0x1ae9
movl 0x24(%rsp), %esi
addq $0x10, %rsp
cmpl 0x14(%r15), %eax
jl 0x1c00
movl %eax, %ebp
cmpl 0x18(%r15), %eax
jg 0x1c00
cmpl %esi, 0x8(%r15)
jne 0x1d7c
movl %ebp, 0x10(%r15)
cmpl %r14d, %ebp
movq 0x28(%rsp), %rdx
jge 0x1d5e
movl %ebp, 0x18(%r15)
cmpl %r12d, %ebp
movl 0x10(%rsp), %r14d
jle 0x1d6c
movl %ebp, 0x14(%r15)
movl 0xc(%rsp), %r12d
movq 0x18(%rsp), %r13
jmp 0x1d90
movl 0x10(%r15), %ebp
movl 0x10(%rsp), %r14d
movl 0xc(%rsp), %r12d
movq 0x18(%rsp), %r13
movq 0x28(%rsp), %rdx
cmpb $0x0, 0xb(%rsp)
je 0x1db5
cmpq $0x0, 0x40(%rsp)
sete %al
cmpl %r14d, %ebp
cmovll %ebp, %r14d
setge %cl
orb %al, %cl
movl %ebp, %eax
movl %r12d, %ebp
je 0x1dcf
jmp 0x1de4
cmpq $0x0, 0x40(%rsp)
sete %al
cmpl %r12d, %ebp
setle %cl
cmovgl %ebp, %r12d
orb %al, %cl
movl %r14d, %eax
jne 0x1de4
movl $0x16, %ecx
movq 0x40(%rsp), %rdi
movq %rdx, %rsi
rep  movsl (%rsi), %es:(%rdi)
movl %eax, %r14d
movl %ebp, %r12d
movq %r13, %rdi
movq %rdx, %rsi
callq 0x2124
cmpl %r14d, %r12d
jge 0x1e06
cmpl $0x2, %ebx
jl 0x1e06
decl %ebx
cmpl $0x0, 0x48(%rsp)
jg 0x1bbe
cmpl %r14d, %r12d
setge %bl
jmp 0x1e10
xorl %ebx, %ebx
leaq 0x48(%rsp), %rdi
callq 0x3af5
leaq 0x78(%rsp), %rdi
callq 0x3d55
cmpb %bl, 0xb(%rsp)
cmovel %r12d, %r14d
movl %r14d, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x30(%rsp), %rdi
movl $0x1, %esi
callq 0x1140