// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xd0, %rsp
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl %r8d, -0x20(%rbp)
movq %r9, -0x28(%rbp)
cmpq $0x0, 0x10(%rbp)
je 0x1d4c
xorl %eax, %eax
movl %eax, %edi
callq 0x11e0
cmpq 0x10(%rbp), %rax
jle 0x1d4c
movq 0x18(%rbp), %rdi
movl $0x1, %esi
callq 0x1150
cmpl $0x1, -0x14(%rbp)
jge 0x1d63
movq -0x10(%rbp), %rdi
callq 0x2580
movl %eax, -0x4(%rbp)
jmp 0x20a8
movb $0x0, -0x31(%rbp)
movl -0x14(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movb (%rax), %al
andb $0x1, %al
movb %al, -0x39(%rbp)
movq -0x10(%rbp), %rdi
callq 0x22f0
movq %rax, -0x50(%rbp)
movq %rdx, -0x48(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x2340
movq %rax, -0x60(%rbp)
movq %rdx, -0x58(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0x64(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x64(%rbp)
movb %al, -0xa1(%rbp)
jle 0x1dc2
cmpl $0x0, -0x60(%rbp)
setg %al
movb %al, -0xa1(%rbp)
movb -0xa1(%rbp), %al
testb $0x1, %al
jne 0x1dd1
jmp 0x2061
leaq -0x60(%rbp), %rdi
callq 0x4b80
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x23f0
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rdi
callq 0x5980
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x70(%rbp)
je 0x1f0d
movq -0x70(%rbp), %rax
movl 0x8(%rax), %eax
cmpl -0x38(%rbp), %eax
jl 0x1f0d
movq -0x70(%rbp), %rax
movl 0x14(%rax), %eax
movq -0x70(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jne 0x1e36
movq -0x70(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, -0x68(%rbp)
jmp 0x1f08
movq -0x70(%rbp), %rax
movl 0x18(%rax), %eax
cmpl -0x1c(%rbp), %eax
jle 0x1e4e
movq -0x70(%rbp), %rax
movl 0x14(%rax), %eax
cmpl -0x20(%rbp), %eax
jl 0x1e5d
movq -0x70(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x68(%rbp)
jmp 0x1f06
movl -0x1c(%rbp), %edi
movq -0x70(%rbp), %rax
movl 0x14(%rax), %esi
callq 0x25e0
movl %eax, -0x74(%rbp)
movl -0x20(%rbp), %edi
movq -0x70(%rbp), %rax
movl 0x18(%rax), %esi
callq 0x2610
movl %eax, -0x78(%rbp)
movq -0x10(%rbp), %rdi
movl -0x38(%rbp), %esi
movl -0x18(%rbp), %edx
movl -0x74(%rbp), %ecx
movl -0x78(%rbp), %r8d
movq 0x10(%rbp), %r10
movq 0x18(%rbp), %rax
xorl %r9d, %r9d
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x1d00
movl %eax, -0x68(%rbp)
movl -0x68(%rbp), %eax
movq -0x70(%rbp), %rcx
cmpl 0x14(%rcx), %eax
jl 0x1ec6
movl -0x68(%rbp), %eax
movq -0x70(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jle 0x1ec8
jmp 0x1f0f
movq -0x70(%rbp), %rax
movl 0x8(%rax), %eax
cmpl -0x38(%rbp), %eax
jne 0x1f04
movl -0x68(%rbp), %ecx
movq -0x70(%rbp), %rax
movl %ecx, 0x10(%rax)
movl -0x68(%rbp), %eax
cmpl -0x78(%rbp), %eax
jge 0x1ef0
movl -0x68(%rbp), %ecx
movq -0x70(%rbp), %rax
movl %ecx, 0x18(%rax)
movl -0x68(%rbp), %eax
cmpl -0x74(%rbp), %eax
jle 0x1f02
movl -0x68(%rbp), %ecx
movq -0x70(%rbp), %rax
movl %ecx, 0x14(%rax)
jmp 0x1f04
jmp 0x1f06
jmp 0x1f08
jmp 0x1fda
jmp 0x1f0f
movq -0x10(%rbp), %rdi
movl -0x38(%rbp), %esi
movl -0x18(%rbp), %edx
movl -0x1c(%rbp), %ecx
movl -0x20(%rbp), %r8d
movq 0x10(%rbp), %r10
movq 0x18(%rbp), %rax
xorl %r9d, %r9d
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x1d00
movl %eax, -0x68(%rbp)
movl -0x68(%rbp), %eax
movl %eax, -0x80(%rbp)
movl %eax, -0x7c(%rbp)
movl -0x68(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x1f56
movl $0x80000000, -0x7c(%rbp) # imm = 0x80000000
jmp 0x1f67
movl -0x68(%rbp), %eax
cmpl -0x20(%rbp), %eax
jle 0x1f65
movl $0x7fffffff, -0x80(%rbp) # imm = 0x7FFFFFFF
jmp 0x1f67
cmpq $0x0, -0x70(%rbp)
jne 0x1fd8
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rax
movq %rax, -0xa0(%rbp)
movl -0x38(%rbp), %eax
movl %eax, -0x98(%rbp)
movl -0x38(%rbp), %eax
movl %eax, -0x94(%rbp)
movl -0x68(%rbp), %eax
movl %eax, -0x90(%rbp)
movl -0x7c(%rbp), %eax
movl %eax, -0x8c(%rbp)
movl -0x80(%rbp), %eax
movl %eax, -0x88(%rbp)
leaq -0xa0(%rbp), %rax
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rcx
movq %rcx, 0x10(%rsp)
movq 0x18(%rax), %rax
movq %rax, 0x18(%rsp)
callq 0x58f0
jmp 0x1fda
testb $0x1, -0x39(%rbp)
je 0x200b
movl -0x68(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0x2009
movl -0x68(%rbp), %eax
movl %eax, -0x20(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x2007
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
movl $0x58, %edx
callq 0x11c0
jmp 0x2009
jmp 0x2036
movl -0x68(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jle 0x2034
movl -0x68(%rbp), %eax
movl %eax, -0x1c(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x2032
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
movl $0x58, %edx
callq 0x11c0
jmp 0x2034
jmp 0x2036
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x24a0
movl -0x1c(%rbp), %eax
cmpl -0x20(%rbp), %eax
jl 0x2051
movb $0x1, -0x31(%rbp)
jmp 0x2061
jmp 0x2053
movl -0x64(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x1da7
leaq -0x60(%rbp), %rdi
callq 0x4a00
leaq -0x50(%rbp), %rdi
callq 0x4f40
movb -0x39(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movb -0x31(%rbp), %cl
andb $0x1, %cl
movzbl %cl, %ecx
xorl %ecx, %eax
cmpl $0x0, %eax
je 0x2096
movl -0x20(%rbp), %eax
movl %eax, -0xa8(%rbp)
jmp 0x209f
movl -0x1c(%rbp), %eax
movl %eax, -0xa8(%rbp)
movl -0xa8(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xd0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)