// File: /workspace/github_c_makefile_star2_O1/icarroll[P]clunk/thudlib.c
// Function: nextdwarfplay
struct move nextdwarfplay(struct thudboard * board, struct genstate * ctx)
{
    if (ctx->resume) goto * ctx->resume;

    ctx->move.isdwarfmove = true;

    // generate attacks
    ctx->move.numcapts = 1;
    ctx->move.from = initpos;
    while (true)
    {
        ctx->move.from = nextpos(board->dwarfs, ctx->move.from);
        if (ctx->move.from.x == -1) break;

        for (ctx->dir = 0; ctx->dir < NUMDIRS; ++ctx->dir)
        {
            ctx->dx = DX[ctx->dir];
            ctx->dy = DY[ctx->dir];

            for (ctx->dist = 1; ctx->dist < SIZE ; ++ctx->dist)
            {
                ctx->move.to.x = ctx->move.from.x + ctx->dx * ctx->dist;
                ctx->move.to.y = ctx->move.from.y + ctx->dy * ctx->dist;
                if (! inbounds(ctx->move.to)) break;

                if (dwarfat(board, ctx->move.to)
                    || blockat(board, ctx->move.to)) break;

                struct coord check;
                check.x = ctx->move.from.x - ctx->dx * (ctx->dist-1);
                check.y = ctx->move.from.y - ctx->dy * (ctx->dist-1);
                if (! inbounds(check) || ! dwarfat(board, check)) break;

                if (trollat(board, ctx->move.to))
                {
                    ctx->move.capts[0] = ctx->move.to;

                    ctx->resume = && dwarfresume1;
                    return ctx->move;
dwarfresume1:
                    break;
                }
            }
        }
    }

    // generate moves
    ctx->move.numcapts = 0;
    ctx->move.from = initpos;
    while (true)
    {
        ctx->move.from = nextpos(board->dwarfs, ctx->move.from);
        if (ctx->move.from.x == -1) break;

        for (ctx->dir = 0; ctx->dir < NUMDIRS; ++ctx->dir)
        {
            ctx->dx = DX[ctx->dir];
            ctx->dy = DY[ctx->dir];

            for (ctx->dist = 1; ctx->dist < SIZE ; ++ctx->dist)
            {
                ctx->move.to.x = ctx->move.from.x + ctx->dx * ctx->dist;
                ctx->move.to.y = ctx->move.from.y + ctx->dy * ctx->dist;
                if (! inbounds(ctx->move.to)) break;

                if (occupied(board, ctx->move.to)) break;

                ctx->resume = && dwarfresume2;
                return ctx->move;
dwarfresume2:
                continue;
            }
        }
    }

    ctx->resume = NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
movq (%rdx), %rax
testq %rax, %rax
je 0x29b4
jmpq *%rax
incl 0x14(%r14)
jmp 0x2b57
movb $0x1, 0x18(%r14)
movl $0x1, 0x2c(%r14)
movq 0x5758(%rip), %rax      # 0x8120
movq %rax, 0x1c(%r14)
leaq 0x2(%r15), %rdi
movq 0x1c(%r14), %rsi
callq 0x2ec4
movq %rax, 0x1c(%r14)
cmpl $-0x1, %eax
je 0x29ec
movl $0x0, 0x10(%r14)
jmp 0x2a04
movl $0x0, 0x2c(%r14)
movq 0x5725(%rip), %rax      # 0x8120
movq %rax, 0x1c(%r14)
jmp 0x2af0
movslq 0x10(%r14), %rax
cmpq $0x7, %rax
jg 0x29cc
leaq 0x571b(%rip), %rcx      # 0x8130
movl (%rcx,%rax,4), %edi
movl %edi, 0x8(%r14)
leaq 0x572d(%rip), %rcx      # 0x8150
movslq (%rcx,%rax,4), %r8
movl %r8d, 0xc(%r14)
movl 0x1c(%r14), %eax
movslq 0x20(%r14), %r9
leal (%r9,%r8), %r10d
leal (%rdi,%rax), %r11d
movq $-0xe, %rsi
movl %r11d, 0x24(%r14)
movl %r10d, 0x28(%r14)
movq 0x24(%r14), %rdx
testq %rdx, %rdx
js 0x2abb
cmpl $0xe, %edx
ja 0x2abb
movq %rdx, %r12
shrq $0x20, %r12
cmpl $0xe, %r12d
jg 0x2abb
movl $0x4000, %ebp           # imm = 0x4000
movl %edx, %ecx
shrl %cl, %ebp
testw %bp, 0x2(%r15,%r12,2)
jne 0x2abb
testw %bp, 0x3e(%r15,%r12,2)
jne 0x2abb
cmpl $0xe, %eax
ja 0x2abb
cmpq $0xe, %r9
ja 0x2abb
movl $0x4000, %r13d          # imm = 0x4000
movl %eax, %ecx
shrl %cl, %r13d
testw %r13w, 0x2(%r15,%r9,2)
je 0x2abb
testw %bp, 0x20(%r15,%r12,2)
jne 0x2acb
addl %r8d, %r10d
addl %edi, %r11d
subl %edi, %eax
subq %r8, %r9
incq %rsi
jne 0x2a42
movl $0xf, %esi
jmp 0x2abe
addl $0xf, %esi
movl %esi, 0x14(%r14)
incl 0x10(%r14)
jmp 0x2a04
addl $0xf, %esi
movl %esi, 0x14(%r14)
leaq 0x18(%r14), %rsi
movq %rdx, 0x30(%r14)
leaq -0x1f(%rip), %rax       # 0x2ac2
movq %rax, (%r14)
movl $0x16, %ecx
movq %rbx, %rdi
rep  movsl (%rsi), %es:(%rdi)
jmp 0x2b17
leaq 0x2(%r15), %rdi
movq 0x1c(%r14), %rsi
callq 0x2ec4
movq %rax, 0x1c(%r14)
cmpl $-0x1, %eax
je 0x2b10
movl $0x0, 0x10(%r14)
jmp 0x2b29
movq $0x0, (%r14)
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movslq 0x10(%r14), %rax
cmpq $0x7, %rax
jg 0x2af0
leaq 0x55f6(%rip), %rcx      # 0x8130
movl (%rcx,%rax,4), %ecx
movl %ecx, 0x8(%r14)
leaq 0x5608(%rip), %rcx      # 0x8150
movl (%rcx,%rax,4), %eax
movl %eax, 0xc(%r14)
movl $0x1, 0x14(%r14)
movl 0x14(%r14), %eax
cmpl $0xe, %eax
jg 0x2bc4
movl 0x8(%r14), %ecx
imull %eax, %ecx
addl 0x1c(%r14), %ecx
movl %ecx, 0x24(%r14)
imull 0xc(%r14), %eax
addl 0x20(%r14), %eax
movl %eax, 0x28(%r14)
movq 0x24(%r14), %rcx
cmpl $0xf, %ecx
setb %al
testq %rcx, %rcx
setns %dl
andb %al, %dl
movq %rcx, %rax
shrq $0x20, %rax
cmpl $0xf, %eax
setl %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0x2bc4
movl $0x4000, %edx           # imm = 0x4000
shrl %cl, %edx
testw %dx, 0x2(%r15,%rax,2)
jne 0x2bc4
testw %dx, 0x20(%r15,%rax,2)
jne 0x2bc4
testw %dx, 0x3e(%r15,%rax,2)
je 0x2bcd
incl 0x10(%r14)
jmp 0x2b29
leaq -0x229(%rip), %rax      # 0x29ab
movq %rax, (%r14)
addq $0x18, %r14
movl $0x16, %ecx
movq %rbx, %rdi
movq %r14, %rsi
jmp 0x2aec