// File: /workspace/github_c_makefile_star2_O1/kyz[P]amostools/dumpamos.c
// Function: decode_sprites
void decode_sprites(char *fname, uint8_t *src, size_t len, char *prefix) {
    uint32_t num_sprites = amos_deek(&src[4]);
    uint8_t *sp = &src[6], *pal = &src[len - 64];

    /* create an IFF ILBM file for each sprite/icon */
    for (uint32_t i = 0; i < num_sprites; i++) {
        uint32_t w, h, d, sp_len, ilbm_len, line, plane;
        uint8_t *ilbm, *body;

        w = amos_deek(&sp[0]) * 2;
        h = amos_deek(&sp[2]);
        d = amos_deek(&sp[4]);

        sp_len = w * h * d;
        ilbm_len = IFF_ILBM_HEADER_LEN + sp_len;
        if ((ilbm = malloc(ilbm_len))) {
            char *outname;
            memcpy(ilbm, iff_ilbm_header, IFF_ILBM_HEADER_LEN);
            amos_loke(&ilbm[0x04], ilbm_len - 8);
            amos_doke(&ilbm[0x14], w * 8);  /* width */
            amos_doke(&ilbm[0x16], h);      /* height */
            ilbm[0x1c] = d & 0xFF;          /* number of bitplanes */
            amos_doke(&ilbm[0x24], w * 8);  /* page width */
            amos_doke(&ilbm[0x26], h);      /* page height */
            memcpy(&ilbm[0x3C], &sp[6], 4); /* x/y hotspot */
            memcpy(&ilbm[0xA8], "BODY", 4);
            amos_loke(&ilbm[0xAC], sp_len); /* body length */

            /* convert palette from 0x0RGB to 0xRR 0xGG 0xBB */
            for (int j = 0; j < 32; j++) {
                uint32_t c = amos_deek(&pal[j * 2]);
                ilbm[0x48 + (j * 3) + 0] = ((c >> 8) & 0xF) * 0x11;
                ilbm[0x48 + (j * 3) + 1] = ((c >> 4) & 0xF) * 0x11;
                ilbm[0x48 + (j * 3) + 2] = ((c     ) & 0xF) * 0x11;
            }

            /* interleave the sprite data (all lines of plane 0, plane 1, ...)
             * into ILBM format (line 0 plane 0, line 0 plane 1, ...) */
            body = &ilbm[IFF_ILBM_HEADER_LEN];
            for (line = 0; line < h; line++) {
                for (plane = 0; plane < d; plane++) {
                    memcpy(body, &sp[10 + line*w + plane*w*h], w);
                    body += w;
                }
            }

            /* save IFF ILBM */
            if ((outname = output_fname(fname, "%s%02X.iff", prefix, i + 1))) {
                printf("%s: converting %s %d to ILBM image %s (%d bytes)\n",
                       fname, prefix, i+1, outname, ilbm_len);
                write_file(outname, ilbm, ilbm_len);
                free(outname);
            }
            else {
                perror(fname);
            }

            free(ilbm);
        }
        sp += 10 + sp_len;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rcx, 0x48(%rsp)
movq %rdi, 0x40(%rsp)
movzwl 0x4(%rsi), %eax
rolw $0x8, %ax
testw %ax, %ax
je 0x1960
movq %rsi, %r14
movzwl %ax, %eax
movl %eax, 0x34(%rsp)
leaq (%rdx,%rsi), %rax
addq $-0x3f, %rax
movq %rax, 0x38(%rsp)
addq $0x6, %r14
movq $0x0, 0x20(%rsp)
movzbl (%r14), %eax
movzbl 0x1(%r14), %r13d
shll $0x9, %eax
leal (%rax,%r13,2), %ecx
movzbl 0x2(%r14), %r15d
movl %r15d, 0x8(%rsp)
shll $0x8, %r15d
movzbl 0x3(%r14), %r12d
orl %r12d, %r15d
movzbl 0x4(%r14), %eax
shll $0x8, %eax
movzbl 0x5(%r14), %edx
movl %edx, 0x4(%rsp)
orl %edx, %eax
movl %r15d, 0x10(%rsp)
movl %ecx, 0x14(%rsp)
imull %ecx, %r15d
movl %r15d, %ebx
movl %eax, 0xc(%rsp)
imull %eax, %ebx
leal 0xb0(%rbx), %edi
movq %rdi, 0x58(%rsp)
callq 0x1110
testq %rax, %rax
je 0x192e
movq %rax, %rbp
movl $0xb0, %edx
movq %rax, %rdi
leaq 0x1c07(%rip), %rsi      # 0x3350
callq 0x1100
leal 0xa8(%rbx), %eax
movl %eax, %ecx
shrl $0x18, %ecx
movb %cl, 0x4(%rbp)
movl %eax, %ecx
shrl $0x10, %ecx
movb %cl, 0x5(%rbp)
movb %ah, 0x6(%rbp)
andb $-0x2, %al
movb %al, 0x7(%rbp)
shlb $0x4, %r13b
movl 0x14(%rsp), %r8d
movl %r8d, %eax
shrl $0x5, %eax
movb %al, 0x14(%rbp)
movb %r13b, 0x15(%rbp)
movl 0x8(%rsp), %ecx
movb %cl, 0x16(%rbp)
movb %r12b, 0x17(%rbp)
movl 0x4(%rsp), %edx
movb %dl, 0x1c(%rbp)
movb %al, 0x24(%rbp)
movb %r13b, 0x25(%rbp)
movb %cl, 0x26(%rbp)
movb %r12b, 0x27(%rbp)
movq %r14, 0x28(%rsp)
movl 0x6(%r14), %eax
movl %eax, 0x3c(%rbp)
movl $0x59444f42, 0xa8(%rbp) # imm = 0x59444F42
movl %ebx, %eax
shrl $0x18, %eax
movb %al, 0xac(%rbp)
movl %ebx, %eax
shrl $0x10, %eax
movb %al, 0xad(%rbp)
movb %bh, 0xae(%rbp)
movb %bl, 0xaf(%rbp)
movl $0x4a, %eax
movq 0x38(%rsp), %rcx
movb -0x1(%rcx), %dl
movb (%rcx), %sil
andb $0xf, %dl
movzbl %dl, %edx
movl %edx, %edi
shll $0x4, %edi
addl %edx, %edi
movb %dil, -0x2(%rbp,%rax)
movl %esi, %edx
shrb $0x4, %dl
movzbl %dl, %edx
movl %edx, %edi
shll $0x4, %edi
addl %edx, %edi
movb %dil, -0x1(%rbp,%rax)
andb $0xf, %sil
movzbl %sil, %edx
movl %edx, %esi
shll $0x4, %esi
addl %edx, %esi
movb %sil, (%rbp,%rax)
addq $0x3, %rax
addq $0x2, %rcx
cmpq $0xaa, %rax
jne 0x17e4
movq %rbp, 0x18(%rsp)
movq %rbx, 0x50(%rsp)
cmpl $0x0, 0x10(%rsp)
je 0x18b0
movq 0x18(%rsp), %rbp
addq $0xb0, %rbp
movl %r8d, %r13d
movl 0xc(%rsp), %eax
movq %rax, 0x60(%rsp)
xorl %eax, %eax
movl $0xa, %ebx
movl %eax, 0x8(%rsp)
cmpl $0x0, 0xc(%rsp)
movl %ebx, 0x4(%rsp)
je 0x1898
movq 0x60(%rsp), %r14
movq 0x28(%rsp), %r12
movl %ebx, %esi
addq %r12, %rsi
movq %rbp, %rdi
movq %r13, %rdx
callq 0x1100
addq %r13, %rbp
addl %r15d, %ebx
decq %r14
jne 0x187d
movl 0x8(%rsp), %eax
incl %eax
movl 0x14(%rsp), %r8d
movl 0x4(%rsp), %ebx
addl %r8d, %ebx
cmpl 0x10(%rsp), %eax
jne 0x1864
movq 0x20(%rsp), %rax
leal 0x1(%rax), %ebp
movq 0x40(%rsp), %r14
movq %r14, %rdi
leaq 0x175c(%rip), %rsi      # 0x3023
movq 0x48(%rsp), %rbx
movq %rbx, %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x1394
testq %rax, %rax
je 0x194c
movq %rax, %r13
leaq 0x1747(%rip), %rdi      # 0x302e
movq %r14, %rsi
movq %rbx, %rdx
movl %ebp, %ecx
movq %rax, %r8
movq 0x58(%rsp), %r14
movl %r14d, %r9d
xorl %eax, %eax
callq 0x10c0
movq %r13, %rdi
movq 0x18(%rsp), %rbx
movq %rbx, %rsi
movq %r14, %rdx
callq 0x132e
movq %r13, %rdi
callq 0x1030
movq 0x28(%rsp), %r14
movq %rbx, %rdi
callq 0x1030
movq 0x50(%rsp), %rbx
addl $0xa, %ebx
addq %rbx, %r14
movq 0x20(%rsp), %rax
incl %eax
movq %rax, 0x20(%rsp)
cmpl 0x34(%rsp), %eax
jne 0x16ce
jmp 0x1960
movq %r14, %rdi
callq 0x1130
movq 0x28(%rsp), %r14
movq 0x18(%rsp), %rbx
jmp 0x1921
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq