// File: /workspace/github_c_makefile_star2_O2/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 $0x58, %rsp
movq %rsi, %r14
movzwl 0x4(%rsi), %eax
rolw $0x8, %ax
movzwl %ax, %eax
movl %eax, 0x28(%rsp)
leaq (%rdx,%rsi), %rax
addq $-0x3f, %rax
movq %rax, 0x38(%rsp)
addq $0x6, %r14
xorl %r13d, %r13d
movq %rcx, 0x48(%rsp)
movq %rdi, 0x40(%rsp)
cmpl 0x28(%rsp), %r13d
je 0x1903
movl %r13d, 0x8(%rsp)
movzbl (%r14), %eax
movzbl 0x1(%r14), %r13d
shll $0x9, %eax
leal (%rax,%r13,2), %r15d
movzbl 0x2(%r14), %r12d
movl %r12d, 0xc(%rsp)
shll $0x8, %r12d
movzbl 0x3(%r14), %eax
movl %eax, 0x20(%rsp)
orl %eax, %r12d
movzbl 0x4(%r14), %eax
shll $0x8, %eax
movzbl 0x5(%r14), %ecx
movl %ecx, 0x18(%rsp)
orl %ecx, %eax
movl %r12d, 0x2c(%rsp)
imull %r15d, %r12d
movl %r12d, %ebx
movl %eax, 0x10(%rsp)
imull %eax, %ebx
leal 0xb0(%rbx), %edi
movq %rdi, 0x50(%rsp)
callq 0x1100
testq %rax, %rax
je 0x18dc
movq %rax, %rbp
movl $0xa8, %edx
movq %rax, %rdi
leaq 0x1c39(%rip), %rsi      # 0x3350
callq 0x10f0
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 %r15d, 0x14(%rsp)
shrl $0x5, %r15d
movb %r15b, 0x14(%rbp)
movb %r13b, 0x15(%rbp)
movl 0xc(%rsp), %ecx
movb %cl, 0x16(%rbp)
movl 0x20(%rsp), %edx
movb %dl, 0x17(%rbp)
movl 0x18(%rsp), %esi
movb %sil, 0x1c(%rbp)
movb %r15b, 0x24(%rbp)
movb %r13b, 0x25(%rbp)
movb %cl, 0x26(%rbp)
movb %dl, 0x27(%rbp)
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)
movq 0x38(%rsp), %rax
pushq $0x4a
popq %rcx
cmpq $0xaa, %rcx
je 0x17f2
movb -0x1(%rax), %dl
movb (%rax), %sil
andb $0xf, %dl
movzbl %dl, %edx
imull $0x11, %edx, %edx
movb %dl, -0x2(%rbp,%rcx)
movl %esi, %edx
shrb $0x4, %dl
movzbl %dl, %edx
imull $0x11, %edx, %edx
movb %dl, -0x1(%rbp,%rcx)
andb $0xf, %sil
movzbl %sil, %edx
imull $0x11, %edx, %edx
movb %dl, (%rbp,%rcx)
addq $0x3, %rcx
addq $0x2, %rax
jmp 0x17ae
movq %rbx, 0x18(%rsp)
movq %rbp, 0x30(%rsp)
movq %rbp, %rbx
addq $0xb0, %rbx
movl 0x14(%rsp), %r15d
movl 0x10(%rsp), %eax
movq %rax, 0x20(%rsp)
xorl %ecx, %ecx
pushq $0xa
popq %rbp
cmpl 0x2c(%rsp), %ecx
je 0x185a
movl %ecx, 0x10(%rsp)
movl %ebp, 0xc(%rsp)
movq 0x20(%rsp), %r13
subq $0x1, %r13
jb 0x184a
movl %ebp, %esi
addq %r14, %rsi
movq %rbx, %rdi
movq %r15, %rdx
callq 0x10f0
addq %r15, %rbx
addl %r12d, %ebp
jmp 0x182c
movl 0x10(%rsp), %ecx
incl %ecx
movl 0xc(%rsp), %ebp
addl 0x14(%rsp), %ebp
jmp 0x1819
movl 0x8(%rsp), %ebp
incl %ebp
movq 0x40(%rsp), %r12
movq %r12, %rdi
leaq 0x17b4(%rip), %rsi      # 0x3023
movq 0x48(%rsp), %r15
movq %r15, %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x1378
testq %rax, %rax
movl %ebp, 0x8(%rsp)
je 0x18ef
movq %rax, %rbx
leaq 0x179b(%rip), %rdi      # 0x302e
movq %r12, %rsi
movq %r15, %rdx
movl %ebp, %ecx
movq %rax, %r8
movq 0x50(%rsp), %r15
movl %r15d, %r9d
xorl %eax, %eax
callq 0x10c0
movq %rbx, %rdi
movq 0x30(%rsp), %rbp
movq %rbp, %rsi
movq %r15, %rdx
callq 0x1317
movq %rbx, %rdi
callq 0x1030
movq 0x18(%rsp), %rbx
movq %rbp, %rdi
callq 0x1030
movl 0x8(%rsp), %r13d
jmp 0x18e4
movl 0x8(%rsp), %r13d
incl %r13d
addl $0xa, %ebx
addq %rbx, %r14
jmp 0x168c
movq %r12, %rdi
callq 0x1120
movq 0x18(%rsp), %rbx
movq 0x30(%rsp), %rbp
jmp 0x18cd
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq