// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x4(%rax), %eax
shll $0x8, %eax
movq -0x10(%rbp), %rcx
movzbl 0x5(%rcx), %ecx
orl %ecx, %eax
movl %eax, -0x24(%rbp)
movq -0x10(%rbp), %rax
addq $0x6, %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq -0x18(%rbp), %rcx
subq $0x40, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x24(%rbp), %eax
jae 0x1ee2
movq -0x30(%rbp), %rax
movzbl (%rax), %eax
shll $0x8, %eax
movq -0x30(%rbp), %rcx
movzbl 0x1(%rcx), %ecx
orl %ecx, %eax
shll %eax
movl %eax, -0x40(%rbp)
movq -0x30(%rbp), %rax
movzbl 0x2(%rax), %eax
shll $0x8, %eax
movq -0x30(%rbp), %rcx
movzbl 0x3(%rcx), %ecx
orl %ecx, %eax
movl %eax, -0x44(%rbp)
movq -0x30(%rbp), %rax
movzbl 0x4(%rax), %eax
shll $0x8, %eax
movq -0x30(%rbp), %rcx
movzbl 0x5(%rcx), %ecx
orl %ecx, %eax
movl %eax, -0x48(%rbp)
movl -0x40(%rbp), %eax
imull -0x44(%rbp), %eax
imull -0x48(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
addl $0xb0, %eax
movl %eax, -0x50(%rbp)
movl -0x50(%rbp), %eax
movl %eax, %edi
callq 0x1110
movq %rax, -0x60(%rbp)
cmpq $0x0, %rax
je 0x1ec1
movq -0x60(%rbp), %rdi
leaq 0x27d6(%rip), %rsi      # 0x4360
movl $0xb0, %edx
callq 0x1100
movl -0x50(%rbp), %eax
subl $0x8, %eax
shrl $0x18, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x60(%rbp), %rax
movb %cl, 0x4(%rax)
movl -0x50(%rbp), %eax
subl $0x8, %eax
shrl $0x10, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x60(%rbp), %rax
movb %cl, 0x5(%rax)
movl -0x50(%rbp), %eax
subl $0x8, %eax
shrl $0x8, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x60(%rbp), %rax
movb %cl, 0x6(%rax)
movl -0x50(%rbp), %eax
subl $0x8, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x60(%rbp), %rax
movb %cl, 0x7(%rax)
jmp 0x1bef
movl -0x40(%rbp), %eax
shll $0x3, %eax
shrl $0x8, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x60(%rbp), %rax
movb %cl, 0x14(%rax)
movl -0x40(%rbp), %eax
shll $0x3, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x60(%rbp), %rax
movb %cl, 0x15(%rax)
jmp 0x1c1c
movl -0x44(%rbp), %eax
shrl $0x8, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x60(%rbp), %rax
movb %cl, 0x16(%rax)
movl -0x44(%rbp), %eax
andl $0xff, %eax
movb %al, %cl
movq -0x60(%rbp), %rax
movb %cl, 0x17(%rax)
movl -0x48(%rbp), %eax
andl $0xff, %eax
movb %al, %cl
movq -0x60(%rbp), %rax
movb %cl, 0x1c(%rax)
movl -0x40(%rbp), %eax
shll $0x3, %eax
shrl $0x8, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x60(%rbp), %rax
movb %cl, 0x24(%rax)
movl -0x40(%rbp), %eax
shll $0x3, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x60(%rbp), %rax
movb %cl, 0x25(%rax)
jmp 0x1c7f
movl -0x44(%rbp), %eax
shrl $0x8, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x60(%rbp), %rax
movb %cl, 0x26(%rax)
movl -0x44(%rbp), %eax
andl $0xff, %eax
movb %al, %cl
movq -0x60(%rbp), %rax
movb %cl, 0x27(%rax)
movq -0x60(%rbp), %rax
movq -0x30(%rbp), %rcx
movl 0x6(%rcx), %ecx
movl %ecx, 0x3c(%rax)
movq -0x60(%rbp), %rax
movl 0x2367(%rip), %ecx      # 0x4023
movl %ecx, 0xa8(%rax)
movl -0x4c(%rbp), %eax
shrl $0x18, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x60(%rbp), %rax
movb %cl, 0xac(%rax)
movl -0x4c(%rbp), %eax
shrl $0x10, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x60(%rbp), %rax
movb %cl, 0xad(%rax)
movl -0x4c(%rbp), %eax
shrl $0x8, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x60(%rbp), %rax
movb %cl, 0xae(%rax)
movl -0x4c(%rbp), %eax
andl $0xff, %eax
movb %al, %cl
movq -0x60(%rbp), %rax
movb %cl, 0xaf(%rax)
movl $0x0, -0x74(%rbp)
cmpl $0x20, -0x74(%rbp)
jge 0x1dc6
movq -0x38(%rbp), %rax
movl -0x74(%rbp), %ecx
shll %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
shll $0x8, %eax
movq -0x38(%rbp), %rcx
movl -0x74(%rbp), %edx
shll %edx
movslq %edx, %rdx
movzbl 0x1(%rcx,%rdx), %ecx
orl %ecx, %eax
movl %eax, -0x78(%rbp)
movl -0x78(%rbp), %eax
shrl $0x8, %eax
andl $0xf, %eax
imull $0x11, %eax, %eax
movb %al, %dl
movq -0x60(%rbp), %rax
imull $0x3, -0x74(%rbp), %ecx
addl $0x48, %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl -0x78(%rbp), %eax
shrl $0x4, %eax
andl $0xf, %eax
imull $0x11, %eax, %eax
movb %al, %dl
movq -0x60(%rbp), %rax
imull $0x3, -0x74(%rbp), %ecx
addl $0x48, %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl -0x78(%rbp), %eax
andl $0xf, %eax
imull $0x11, %eax, %eax
movb %al, %dl
movq -0x60(%rbp), %rax
imull $0x3, -0x74(%rbp), %ecx
addl $0x48, %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x1d22
movq -0x60(%rbp), %rax
addq $0xb0, %rax
movq %rax, -0x68(%rbp)
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x44(%rbp), %eax
jae 0x1e48
movl $0x0, -0x58(%rbp)
movl -0x58(%rbp), %eax
cmpl -0x48(%rbp), %eax
jae 0x1e3b
movq -0x68(%rbp), %rdi
movq -0x30(%rbp), %rsi
movl -0x54(%rbp), %eax
imull -0x40(%rbp), %eax
addl $0xa, %eax
movl -0x58(%rbp), %ecx
imull -0x40(%rbp), %ecx
imull -0x44(%rbp), %ecx
addl %ecx, %eax
movl %eax, %eax
addq %rax, %rsi
movl -0x40(%rbp), %eax
movl %eax, %edx
callq 0x1100
movl -0x40(%rbp), %ecx
movq -0x68(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x1dea
jmp 0x1e3d
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x1ddb
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rdx
movl -0x3c(%rbp), %ecx
addl $0x1, %ecx
leaq 0x21cb(%rip), %rsi      # 0x4028
movb $0x0, %al
callq 0x1420
movq %rax, -0x70(%rbp)
cmpq $0x0, %rax
je 0x1eaf
movq -0x8(%rbp), %rsi
movq -0x20(%rbp), %rdx
movl -0x3c(%rbp), %ecx
addl $0x1, %ecx
movq -0x70(%rbp), %r8
movl -0x50(%rbp), %r9d
leaq 0x21a8(%rip), %rdi      # 0x4033
movb $0x0, %al
callq 0x10b0
movq -0x70(%rbp), %rdi
movq -0x60(%rbp), %rsi
movl -0x50(%rbp), %eax
movl %eax, %edx
callq 0x13a0
movq -0x70(%rbp), %rdi
callq 0x1030
jmp 0x1eb8
movq -0x8(%rbp), %rdi
callq 0x1130
movq -0x60(%rbp), %rdi
callq 0x1030
movl -0x4c(%rbp), %ecx
addl $0xa, %ecx
movq -0x30(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x1af9
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax,%rax)