// File: /workspace/github_c_makefile_star2_O3/icarroll[P]clunk/thudlib.c
// Function: alltrollmoves
struct movelist alltrollmoves(struct thudboard * board)
{
    struct movelist moves;
    initlist(& moves);

    // generate attacks
    struct coord from = initpos;
    while (true)
    {
        from = nextpos(board->trolls, from);
        if (from.x == -1) break;

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

            for (int dist = 1; dist < SIZE ; ++dist)
            {
                struct coord to = {from.x + dx * dist,
                                   from.y + dy * dist};
                if (! inbounds(to)) break;

                if (occupied(board, to)) break;

                struct coord check = {from.x - dx * (dist-1),
                                      from.y - dy * (dist-1)};
                if (! inbounds(check) || ! trollat(board, check)) break;

                if (hasneighbor(board->dwarfs, to))
                {
                    struct move * move = next(& moves);
                    * move = (struct move) {false, from, to, 0, {}};

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

    // generate moves
    from = initpos;
    while (true)
    {
        from = nextpos(board->trolls, from);
        if (from.x == -1) break;

        for (int dir = 0; dir < NUMDIRS; ++dir)
        {
            struct coord to = {from.x + DX[dir],
                               from.y + DY[dir]};

            if (! inbounds(to)) continue;

            if (occupied(board, to)) break;

            * next(& moves) = (struct move) {false, from, to, 0, {}};
        }
    }

    return moves;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %rbx
leaq 0x20(%rsp), %rdi
callq 0x3ce8
movq %rbx, 0x8(%rsp)
leaq 0x20(%rbx), %rdi
movq 0x5c43(%rip), %r15      # 0x8120
movq %rdi, (%rsp)
movq %r15, %rsi
callq 0x2e0d
movq %rax, %rsi
cmpl $-0x1, %esi
je 0x26ea
movq 0x8(%rsp), %rax
leaq 0x2(%rax), %r15
leaq 0x5c2b(%rip), %r12      # 0x8130
leaq 0x5c44(%rip), %rbx      # 0x8150
movq %rsi, %rax
movq %rax, %rcx
shrq $0x20, %rcx
movq %rcx, 0x40(%rsp)
movq %rax, 0x30(%rsp)
movq %rax, %r8
sarq $0x20, %r8
xorl %eax, %eax
movq (%rsp), %rdi
movq %rsi, 0x18(%rsp)
movq %r8, 0x48(%rsp)
movl (%r12,%rax,4), %r9d
movq %rax, 0x38(%rsp)
movslq (%rbx,%rax,4), %r10
movl $0x1, %r13d
movl %r9d, 0x14(%rsp)
movq %r10, 0x50(%rsp)
movq %r13, %r14
imulq %r10, %r14
addq %r8, %r14
cmpq $0xe, %r14
ja 0x26aa
movl %r9d, %ebp
imull %r13d, %ebp
addl %esi, %ebp
cmpl $0xe, %ebp
ja 0x26aa
movl $0x4000, %eax           # imm = 0x4000
movl %ebp, %ecx
shrl %cl, %eax
movl %r14d, %ecx
testw %ax, (%r15,%rcx,2)
jne 0x26aa
testw %ax, (%rdi,%rcx,2)
jne 0x26aa
movq 0x8(%rsp), %rdx
testw %ax, 0x3e(%rdx,%rcx,2)
jne 0x26aa
leaq -0x1(%r13), %rdx
movq %rdx, %rcx
imulq %r10, %rcx
movq %r8, %rax
subq %rcx, %rax
cmpq $0xe, %rax
ja 0x26aa
imull %r9d, %edx
movl %esi, %ecx
subl %edx, %ecx
cmpl $0xe, %ecx
ja 0x26aa
movl $0x4000, %edx           # imm = 0x4000
shrl %cl, %edx
testw %dx, (%rdi,%rax,2)
je 0x26aa
movq %r14, %rax
shlq $0x20, %rax
leaq (%rax,%rbp), %rsi
movq %r15, %rdi
callq 0x30ba
testb %al, %al
je 0x2685
leaq 0x20(%rsp), %rdi
callq 0x3d09
xorl %ecx, %ecx
movl %ecx, (%rax)
movq 0x18(%rsp), %rdx
movl %edx, 0x4(%rax)
movq 0x40(%rsp), %rdx
movl %edx, 0x8(%rax)
movl %ebp, 0xc(%rax)
movl %r14d, 0x10(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x14(%rax)
movups %xmm0, 0x24(%rax)
movups %xmm0, 0x34(%rax)
movups %xmm0, 0x44(%rax)
movl %ecx, 0x54(%rax)
xorl %edx, %edx
movl (%rdx,%r12), %ecx
addl %ebp, %ecx
cmpl $0xe, %ecx
ja 0x267b
movl (%rdx,%rbx), %esi
addl %r14d, %esi
cmpl $0xe, %esi
ja 0x267b
movl $0x4000, %edi           # imm = 0x4000
shrl %cl, %edi
movl %esi, %r8d
testw %di, (%r15,%r8,2)
je 0x267b
movslq 0x14(%rax), %rdi
leal 0x1(%rdi), %r8d
movl %r8d, 0x14(%rax)
movl %ecx, 0x18(%rax,%rdi,8)
movl %esi, 0x1c(%rax,%rdi,8)
addq $0x4, %rdx
cmpq $0x20, %rdx
jne 0x2640
incq %r13
cmpq $0xf, %r13
movq (%rsp), %rdi
movq 0x18(%rsp), %rsi
movq 0x48(%rsp), %r8
movl 0x14(%rsp), %r9d
movq 0x50(%rsp), %r10
jne 0x2554
movq 0x38(%rsp), %rax
incq %rax
cmpq $0x8, %rax
jne 0x2537
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
movq 0x30(%rsp), %rcx
andq %rax, %rcx
movl %esi, %esi
orq %rcx, %rsi
callq 0x2e0d
movl %eax, %esi
cmpl $-0x1, %eax
jne 0x250f
movq 0x5a36(%rip), %r15      # 0x8120
movq (%rsp), %rdi
movq %r15, %rsi
callq 0x2e0d
movq %rax, %r12
cmpl $-0x1, %r12d
je 0x27d0
movq %r12, %r15
movq %r15, %r13
shrq $0x20, %r13
xorl %ebx, %ebx
movq (%rsp), %rdi
leaq 0x5a16(%rip), %rax      # 0x8130
movl (%rbx,%rax), %ebp
addl %r12d, %ebp
cmpl $0xe, %ebp
ja 0x279a
leaq 0x5a24(%rip), %rax      # 0x8150
movl (%rbx,%rax), %r14d
addl %r13d, %r14d
cmpl $0xe, %r14d
ja 0x279a
movl $0x4000, %eax           # imm = 0x4000
movl %ebp, %ecx
shrl %cl, %eax
movl %r14d, %ecx
movq 0x8(%rsp), %rdx
testw %ax, 0x2(%rdx,%rcx,2)
jne 0x27a8
testw %ax, (%rdi,%rcx,2)
jne 0x27a8
movq 0x8(%rsp), %rdx
testw %ax, 0x3e(%rdx,%rcx,2)
jne 0x27a8
leaq 0x20(%rsp), %rdi
callq 0x3d09
movq (%rsp), %rdi
xorl %ecx, %ecx
movl %ecx, (%rax)
movl %r12d, 0x4(%rax)
movl %r13d, 0x8(%rax)
movl %ebp, 0xc(%rax)
movl %r14d, 0x10(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x14(%rax)
movups %xmm0, 0x24(%rax)
movups %xmm0, 0x34(%rax)
movups %xmm0, 0x44(%rax)
movl %ecx, 0x54(%rax)
addq $0x4, %rbx
cmpq $0x20, %rbx
jne 0x2713
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
andq %rax, %r15
movl %r12d, %esi
orq %r15, %rsi
callq 0x2e0d
movq %rax, %r15
movl %r15d, %r12d
cmpl $-0x1, %r15d
jne 0x2706
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rdx
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq