// File: /workspace/github_c_makefile_star2_O2/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
subq $0x18, %rsp
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, 0x8(%rsp)
movq (%rdx), %rax
testq %rax, %rax
je 0x2735
jmpq *%rax
movl 0x14(%r14), %ecx
leal 0x1(%rcx), %eax
movl %eax, 0x14(%r14)
cmpl $0xd, %ecx
jle 0x28f8
jmp 0x293e
movb $0x1, 0x18(%r14)
movl $0x1, 0x2c(%r14)
movq 0x59d7(%rip), %rsi      # 0x8120
movq %rsi, 0x1c(%r14)
leaq 0x2(%r15), %rdi
callq 0x2c4d
movq %rax, 0x1c(%r14)
cmpl $-0x1, %eax
je 0x2768
andl $0x0, 0x10(%r14)
xorl %eax, %eax
jmp 0x277d
andl $0x0, 0x2c(%r14)
movq 0x59ac(%rip), %rsi      # 0x8120
movq %rsi, 0x1c(%r14)
jmp 0x28a1
cmpl $0x8, %eax
jge 0x285f
cltq
leaq 0x59a1(%rip), %rcx      # 0x8130
movl (%rcx,%rax,4), %edx
movl %edx, 0x8(%r14)
leaq 0x59b3(%rip), %rcx      # 0x8150
movl (%rcx,%rax,4), %edi
movl %edi, 0xc(%r14)
xorl %r8d, %r8d
movl %edx, 0x14(%rsp)
movl %edx, %r9d
movl %edi, %r10d
xorl %r13d, %r13d
pushq $0x1
popq %r11
cmpl $0xf, %r11d
je 0x2868
movl 0x1c(%r14), %edx
movl 0x20(%r14), %ebx
leal (%r9,%rdx), %eax
movl %eax, 0x24(%r14)
leal (%r10,%rbx), %eax
movl %eax, 0x28(%r14)
movq 0x24(%r14), %r12
movabsq $0xf00000000, %rax      # imm = 0xF00000000
cmpq %rax, %r12
jae 0x2868
cmpl $0xe, %r12d
ja 0x2868
movl $0x4000, %ebp           # imm = 0x4000
movl %r12d, %ecx
shrl %cl, %ebp
movq %r12, %rax
shrq $0x20, %rax
testw %bp, 0x2(%r15,%rax,2)
jne 0x2868
testw %bp, 0x3e(%r15,%rax,2)
jne 0x2868
addl %r13d, %edx
cmpl $0xe, %edx
ja 0x2868
addl %r8d, %ebx
cmpl $0xe, %ebx
ja 0x2868
movq %r15, %rsi
movl $0x4000, %r15d          # imm = 0x4000
movl %edx, %ecx
shrl %cl, %r15d
movl %ebx, %ecx
testw %r15w, 0x2(%rsi,%rcx,2)
movq %rsi, %r15
je 0x2868
testw %bp, 0x20(%r15,%rax,2)
jne 0x287b
incl %r11d
movl 0x14(%rsp), %eax
subl %eax, %r13d
subl %edi, %r8d
addl %edi, %r10d
addl %eax, %r9d
jmp 0x27b8
movq 0x1c(%r14), %rsi
jmp 0x274d
movl %r11d, 0x14(%r14)
movl 0x10(%r14), %eax
incl %eax
movl %eax, 0x10(%r14)
jmp 0x277d
movl %r11d, 0x14(%r14)
leaq 0x18(%r14), %rsi
movq %r12, 0x30(%r14)
leaq -0x22(%rip), %rax       # 0x286c
movq %rax, (%r14)
pushq $0x16
popq %rcx
movq 0x8(%rsp), %rax
movq %rax, %rdi
jmp 0x296f
leaq 0x2(%r15), %rdi
callq 0x2c4d
movq %rax, 0x1c(%r14)
cmpl $-0x1, %eax
je 0x28bc
andl $0x0, 0x10(%r14)
xorl %eax, %eax
jmp 0x28ca
andq $0x0, (%r14)
movq 0x8(%rsp), %rax
jmp 0x2971
cmpl $0x8, %eax
jge 0x294a
cltq
leaq 0x5858(%rip), %rcx      # 0x8130
movl (%rcx,%rax,4), %ecx
movl %ecx, 0x8(%r14)
leaq 0x586a(%rip), %rcx      # 0x8150
movl (%rcx,%rax,4), %eax
movl %eax, 0xc(%r14)
movl $0x1, 0x14(%r14)
pushq $0x1
popq %rax
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), %rsi
cmpl $0xf, %esi
setae %al
movabsq $0xf00000000, %rcx      # imm = 0xF00000000
cmpq %rcx, %rsi
setae %cl
orb %al, %cl
jne 0x293e
movq %r15, %rdi
callq 0x2d08
testb %al, %al
je 0x2953
movl 0x10(%r14), %eax
incl %eax
movl %eax, 0x10(%r14)
jmp 0x28ca
movq 0x1c(%r14), %rsi
jmp 0x28a1
leaq -0x23e(%rip), %rax      # 0x271c
movq %rax, (%r14)
addq $0x18, %r14
pushq $0x16
popq %rcx
movq 0x8(%rsp), %rax
movq %rax, %rdi
movq %r14, %rsi
rep  movsl (%rsi), %es:(%rdi)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq