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

    ctx->move.isdwarfmove = false;

    // generate attacks
    ctx->move.from = initpos;
    while (true)
    {
        ctx->move.from = nextpos(board->trolls, 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;

                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) || ! trollat(board, check)) break;

                if (hasneighbor(board->dwarfs, ctx->move.to))
                {
                    ctx->move.numcapts = 0;
                    for (int i=0; i < NUMDIRS; ++i)
                    {
                        struct coord capt = {ctx->move.to.x + DX[i],
                                             ctx->move.to.y + DY[i]};
                        if (inbounds(capt) && dwarfat(board, capt))
                        {
                            ctx->move.capts[ctx->move.numcapts++] = capt;
                        }
                    }

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

    // generate moves
    ctx->move.numcapts = 0;
    ctx->move.from = initpos;
    while (true)
    {
        ctx->move.from = nextpos(board->trolls, 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];

            ctx->move.to.x = ctx->move.from.x + ctx->dx;
            ctx->move.to.y = ctx->move.from.y + ctx->dy;

            if (! inbounds(ctx->move.to)) continue;

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

            ctx->resume = && trollresume2;
            return ctx->move;
trollresume2:
            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, %rbx
movq %rdi, 0x8(%rsp)
movq (%rdx), %rax
testq %rax, %rax
movq %rsi, (%rsp)
je 0x29a7
jmpq *%rax
movb $0x0, 0x18(%r14)
movq 0x576d(%rip), %rsi      # 0x8120
movq %rsi, 0x1c(%r14)
leaq 0x20(%rbx), %rdi
callq 0x2c4d
movq %rax, 0x1c(%r14)
cmpl $-0x1, %eax
je 0x29d2
andl $0x0, 0x10(%r14)
xorl %eax, %eax
jmp 0x29e7
andl $0x0, 0x2c(%r14)
movq 0x5742(%rip), %rsi      # 0x8120
movq %rsi, 0x1c(%r14)
jmp 0x2b94
cmpl $0x8, %eax
jge 0x2af4
cltq
leaq 0x5737(%rip), %rcx      # 0x8130
movl (%rcx,%rax,4), %ecx
movl %ecx, 0x8(%r14)
leaq 0x5749(%rip), %rcx      # 0x8150
movl (%rcx,%rax,4), %eax
movl %eax, 0xc(%r14)
movl $0x1, 0x14(%r14)
pushq $0x1
popq %r12
cmpl $0xe, %r12d
jg 0x2ae1
movl 0x1c(%r14), %ebx
movl 0x8(%r14), %ebp
movl 0xc(%r14), %r15d
movl %ebp, %eax
imull %r12d, %eax
addl %ebx, %eax
movl %eax, 0x24(%r14)
movl 0x20(%r14), %esi
movl %r15d, %eax
imull %r12d, %eax
addl %esi, %eax
movl %eax, 0x28(%r14)
movq 0x24(%r14), %r13
cmpl $0xf, %r13d
setae %al
movabsq $0xf00000000, %rcx      # imm = 0xF00000000
cmpq %rcx, %r13
setae %cl
orb %al, %cl
jne 0x2ae1
movq (%rsp), %rdi
movl %esi, 0x14(%rsp)
movq %r13, %rsi
callq 0x2d08
movl 0x14(%rsp), %esi
testb %al, %al
jne 0x2ae1
decl %r12d
imull %r12d, %ebp
subl %ebp, %ebx
imull %r12d, %r15d
subl %r15d, %esi
cmpl $0xf, %ebx
setae %al
cmpl $0xf, %esi
setae %dl
orb %al, %dl
jne 0x2ae1
movl $0x4000, %eax           # imm = 0x4000
movl %ebx, %ecx
shrl %cl, %eax
movl %esi, %ecx
movq (%rsp), %rdx
testw %ax, 0x20(%rdx,%rcx,2)
je 0x2ae1
movq (%rsp), %rax
leaq 0x2(%rax), %r12
movq %r12, %rdi
movq %r13, %rsi
callq 0x2e81
testb %al, %al
jne 0x2afd
movl 0x14(%r14), %r12d
incl %r12d
movl %r12d, 0x14(%r14)
jmp 0x2a1a
movl 0x10(%r14), %eax
incl %eax
movl %eax, 0x10(%r14)
movq (%rsp), %rbx
jmp 0x29e7
movq 0x1c(%r14), %rsi
jmp 0x29b7
andl $0x0, 0x2c(%r14)
leaq 0x18(%r14), %rsi
xorl %eax, %eax
leaq 0x5621(%rip), %rdx      # 0x8130
leaq 0x563a(%rip), %rdi      # 0x8150
cmpq $0x20, %rax
je 0x2b6e
movl (%rax,%rdx), %ecx
addl 0x24(%r14), %ecx
movl (%rax,%rdi), %r8d
addl 0x28(%r14), %r8d
cmpl $0xf, %ecx
setae %r9b
cmpl $0xf, %r8d
setae %r10b
orb %r9b, %r10b
jne 0x2b68
movl $0x4000, %r9d           # imm = 0x4000
shrl %cl, %r9d
movl %r8d, %r10d
testw %r9w, (%r12,%r10,2)
je 0x2b68
movslq 0x2c(%r14), %r9
leal 0x1(%r9), %r10d
movl %r10d, 0x2c(%r14)
movl %ecx, 0x30(%r14,%r9,8)
movl %r8d, 0x34(%r14,%r9,8)
addq $0x4, %rax
jmp 0x2b16
leaq -0xa4(%rip), %rax       # 0x2ad1
movq %rax, (%r14)
pushq $0x16
popq %rcx
movq 0x8(%rsp), %rax
movq %rax, %rdi
rep  movsl (%rsi), %es:(%rdi)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x20(%rbx), %rdi
callq 0x2c4d
movq %rax, 0x1c(%r14)
cmpl $-0x1, %eax
je 0x2baf
andl $0x0, 0x10(%r14)
xorl %eax, %eax
jmp 0x2bba
andq $0x0, (%r14)
movq 0x8(%rsp), %rax
jmp 0x2b85
cmpl $0x7, %eax
jg 0x2c17
cltq
leaq 0x5568(%rip), %rcx      # 0x8130
movl (%rcx,%rax,4), %ecx
movl %ecx, 0x8(%r14)
leaq 0x557a(%rip), %rdx      # 0x8150
movl (%rdx,%rax,4), %eax
movl %eax, 0xc(%r14)
addl 0x1c(%r14), %ecx
movl %ecx, 0x24(%r14)
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 0x2c20
movq %rbx, %rdi
callq 0x2d08
testb %al, %al
je 0x2c2c
movq 0x1c(%r14), %rsi
jmp 0x2b94
movl 0x10(%r14), %eax
incl %eax
movl %eax, 0x10(%r14)
jmp 0x2bba
leaq -0x13(%rip), %rax       # 0x2c20
movq %rax, (%r14)
addq $0x18, %r14
pushq $0x16
popq %rcx
movq 0x8(%rsp), %rax
movq %rax, %rdi
movq %r14, %rsi
jmp 0x2b83