// File: /workspace/github_c_makefile_star2_O3/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, 0x50(%rsp)
movq %rdi, 0x48(%rsp)
movzwl 0x4(%rsi), %eax
rolw $0x8, %ax
testw %ax, %ax
je 0x195f
movq %rsi, %rbp
movzwl %ax, %eax
movl %eax, 0x34(%rsp)
leaq (%rdx,%rsi), %rax
addq $-0x3f, %rax
movq %rax, 0x40(%rsp)
addq $0x6, %rbp
xorl %r12d, %r12d
movl %r12d, 0x10(%rsp)
movzbl (%rbp), %eax
movzbl 0x1(%rbp), %r14d
shll $0x9, %eax
leal (%rax,%r14,2), %ecx
movzbl 0x2(%rbp), %r12d
movl %r12d, 0xc(%rsp)
shll $0x8, %r12d
movzbl 0x3(%rbp), %r15d
orl %r15d, %r12d
movzbl 0x4(%rbp), %eax
shll $0x8, %eax
movzbl 0x5(%rbp), %edx
movl %edx, 0x8(%rsp)
orl %edx, %eax
movl %r12d, 0x18(%rsp)
movl %ecx, 0x1c(%rsp)
imull %ecx, %r12d
movl %r12d, %ecx
movl %eax, 0x14(%rsp)
imull %eax, %ecx
movq %rcx, %r13
leal 0xb0(%rcx), %edi
movq %rdi, 0x58(%rsp)
callq 0x1100
testq %rax, %rax
je 0x192a
movq %rax, %rbx
movl $0xa8, %edx
movq %rax, %rdi
leaq 0x1c12(%rip), %rsi      # 0x3350
callq 0x10f0
movq %r13, %rdx
leal 0xa8(%r13), %eax
movl %eax, %ecx
shrl $0x18, %ecx
movb %cl, 0x4(%rbx)
movl %eax, %ecx
shrl $0x10, %ecx
movb %cl, 0x5(%rbx)
movb %ah, 0x6(%rbx)
andb $-0x2, %al
movb %al, 0x7(%rbx)
shlb $0x4, %r14b
movl 0x1c(%rsp), %r8d
movl %r8d, %eax
shrl $0x5, %eax
movb %al, 0x14(%rbx)
movb %r14b, 0x15(%rbx)
movl 0xc(%rsp), %ecx
movb %cl, 0x16(%rbx)
movb %r15b, 0x17(%rbx)
movl 0x8(%rsp), %esi
movb %sil, 0x1c(%rbx)
movb %al, 0x24(%rbx)
movb %r14b, 0x25(%rbx)
movb %cl, 0x26(%rbx)
movb %r15b, 0x27(%rbx)
movq %rbp, 0x28(%rsp)
movl 0x6(%rbp), %eax
movl %eax, 0x3c(%rbx)
movl $0x59444f42, 0xa8(%rbx) # imm = 0x59444F42
movl %edx, %eax
shrl $0x18, %eax
movb %al, 0xac(%rbx)
movl %edx, %eax
shrl $0x10, %eax
movb %al, 0xad(%rbx)
movb %dh, 0xae(%rbx)
movq %r13, 0x38(%rsp)
movb %dl, 0xaf(%rbx)
movl $0x4a, %eax
movq 0x40(%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(%rbx,%rax)
movl %esi, %edx
shrb $0x4, %dl
movzbl %dl, %edx
movl %edx, %edi
shll $0x4, %edi
addl %edx, %edi
movb %dil, -0x1(%rbx,%rax)
andb $0xf, %sil
movzbl %sil, %edx
movl %edx, %esi
shll $0x4, %esi
addl %edx, %esi
movb %sil, (%rbx,%rax)
addq $0x3, %rax
addq $0x2, %rcx
cmpq $0xaa, %rax
jne 0x17e2
movq %rbx, 0x20(%rsp)
cmpl $0x0, 0x18(%rsp)
je 0x18a8
movq 0x20(%rsp), %rbx
addq $0xb0, %rbx
movl %r8d, %r15d
movl 0x14(%rsp), %eax
movq %rax, 0x60(%rsp)
xorl %eax, %eax
movl $0xa, %ebp
movl %eax, 0xc(%rsp)
cmpl $0x0, 0x14(%rsp)
movl %ebp, 0x8(%rsp)
je 0x1890
movq 0x60(%rsp), %r14
movq 0x28(%rsp), %r13
movl %ebp, %esi
addq %r13, %rsi
movq %rbx, %rdi
movq %r15, %rdx
callq 0x10f0
addq %r15, %rbx
addl %r12d, %ebp
decq %r14
jne 0x1875
movl 0xc(%rsp), %eax
incl %eax
movl 0x1c(%rsp), %r8d
movl 0x8(%rsp), %ebp
addl %r8d, %ebp
cmpl 0x18(%rsp), %eax
jne 0x185c
movl 0x10(%rsp), %r12d
incl %r12d
movq 0x48(%rsp), %r15
movq %r15, %rdi
leaq 0x1764(%rip), %rsi      # 0x3023
movq 0x50(%rsp), %r14
movq %r14, %rdx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x1383
testq %rax, %rax
je 0x1946
movq %rax, %rbx
leaq 0x174e(%rip), %rdi      # 0x302e
movq %r15, %rsi
movq %r14, %rdx
movl %r12d, %ecx
movq %rax, %r8
movq 0x58(%rsp), %r15
movl %r15d, %r9d
xorl %eax, %eax
callq 0x10c0
movq %rbx, %rdi
movq 0x20(%rsp), %r14
movq %r14, %rsi
movq %r15, %rdx
callq 0x131e
movq %rbx, %rdi
callq 0x1030
movq 0x28(%rsp), %rbp
movq 0x38(%rsp), %r13
movq %r14, %rdi
callq 0x1030
jmp 0x1932
movl 0x10(%rsp), %r12d
incl %r12d
addl $0xa, %r13d
addq %r13, %rbp
cmpl 0x34(%rsp), %r12d
jne 0x16bd
jmp 0x195f
movq %r15, %rdi
callq 0x1120
movq 0x28(%rsp), %rbp
movq 0x38(%rsp), %r13
movq 0x20(%rsp), %r14
jmp 0x1920
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq