// File: /workspace/github_c_makefile_star2_O3/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 %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
movabsq $0xf00000000, %r13      # imm = 0xF00000000
movq (%rdx), %rax
testq %rax, %rax
je 0x2b61
jmpq *%rax
movb $0x0, 0x18(%r14)
movq 0x55b3(%rip), %rsi      # 0x8120
movq %rsi, 0x1c(%r14)
leaq 0x20(%r15), %rdi
callq 0x2e0d
movq %rax, 0x1c(%r14)
cmpl $-0x1, %eax
je 0x2b8f
movl $0x0, 0x10(%r14)
xorl %eax, %eax
jmp 0x2ba7
movl $0x0, 0x2c(%r14)
movq 0x5582(%rip), %rsi      # 0x8120
movq %rsi, 0x1c(%r14)
jmp 0x2d49
cmpl $0x8, %eax
jge 0x2cad
cltq
leaq 0x5577(%rip), %rcx      # 0x8130
movl (%rcx,%rax,4), %ecx
movl %ecx, 0x8(%r14)
leaq 0x5589(%rip), %rcx      # 0x8150
movl (%rcx,%rax,4), %eax
movl %eax, 0xc(%r14)
movl $0x1, 0x14(%r14)
movl $0x1, %edx
cmpl $0xe, %edx
jg 0x2c9e
movl 0x1c(%r14), %eax
movl 0x8(%r14), %r9d
movl 0xc(%r14), %r8d
movl %r9d, %ecx
imull %edx, %ecx
addl %eax, %ecx
movl %ecx, 0x24(%r14)
movl 0x20(%r14), %edi
movl %r8d, %ecx
imull %edx, %ecx
addl %edi, %ecx
movl %ecx, 0x28(%r14)
movq 0x24(%r14), %rsi
cmpl $0xf, %esi
setae %cl
cmpq %r13, %rsi
setae %r10b
orb %cl, %r10b
jne 0x2c9e
movl $0x4000, %r10d          # imm = 0x4000
movl %esi, %ecx
shrl %cl, %r10d
movq %rsi, %rcx
shrq $0x20, %rcx
testw %r10w, 0x2(%r15,%rcx,2)
jne 0x2c9e
testw %r10w, 0x20(%r15,%rcx,2)
jne 0x2c9e
testw %r10w, 0x3e(%r15,%rcx,2)
jne 0x2c9e
decl %edx
imull %edx, %r9d
subl %r9d, %eax
imull %edx, %r8d
subl %r8d, %edi
cmpl $0xf, %eax
setae %cl
cmpl $0xf, %edi
setae %dl
orb %cl, %dl
jne 0x2c9e
movl $0x4000, %edx           # imm = 0x4000
movl %eax, %ecx
shrl %cl, %edx
movl %edi, %eax
testw %dx, 0x20(%r15,%rax,2)
je 0x2c9e
leaq 0x2(%r15), %r12
movq %r12, %rdi
callq 0x30ba
testb %al, %al
jne 0x2cb6
movl 0x14(%r14), %edx
incl %edx
movl %edx, 0x14(%r14)
jmp 0x2bdb
movl 0x10(%r14), %eax
incl %eax
movl %eax, 0x10(%r14)
jmp 0x2ba7
movq 0x1c(%r14), %rsi
jmp 0x2b71
leaq 0x18(%r14), %rsi
movl $0x0, 0x2c(%r14)
xorl %eax, %eax
leaq 0x5465(%rip), %rdx      # 0x8130
leaq 0x547e(%rip), %rdi      # 0x8150
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 0x2d1e
movl $0x4000, %r9d           # imm = 0x4000
shrl %cl, %r9d
movl %r8d, %r10d
testw %r9w, (%r12,%r10,2)
je 0x2d1e
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
cmpq $0x20, %rax
jne 0x2cd2
leaq -0xa0(%rip), %rax       # 0x2c8f
movq %rax, (%r14)
movl $0x16, %ecx
movq %rbx, %rdi
rep  movsl (%rsi), %es:(%rdi)
movq %rbx, %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
leaq 0x20(%r15), %rdi
callq 0x2e0d
movq %rax, 0x1c(%r14)
cmpl $-0x1, %eax
je 0x2d67
movl $0x0, 0x10(%r14)
xorl %eax, %eax
jmp 0x2d70
movq $0x0, (%r14)
jmp 0x2d3c
cmpl $0x7, %eax
jg 0x2dda
cltq
leaq 0x53b2(%rip), %rcx      # 0x8130
movl (%rcx,%rax,4), %ecx
movl %ecx, 0x8(%r14)
leaq 0x53c4(%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), %rcx
cmpl $0xf, %ecx
setae %al
cmpq %r13, %rcx
setae %dl
orb %al, %dl
jne 0x2de3
movl $0x4000, %eax           # imm = 0x4000
shrl %cl, %eax
shrq $0x20, %rcx
testw %ax, 0x2(%r15,%rcx,2)
jne 0x2dda
testw %ax, 0x20(%r15,%rcx,2)
jne 0x2dda
testw %ax, 0x3e(%r15,%rcx,2)
je 0x2def
movq 0x1c(%r14), %rsi
jmp 0x2d49
movl 0x10(%r14), %eax
incl %eax
movl %eax, 0x10(%r14)
jmp 0x2d70
leaq -0x13(%rip), %rax       # 0x2de3
movq %rax, (%r14)
addq $0x18, %r14
movl $0x16, %ecx
movq %rbx, %rdi
movq %r14, %rsi
jmp 0x2d3a