// File: /workspace/github_c_makefile_star2_O1/kyz[P]amostools/dumpamos.c
// Function: decode_samples
void decode_samples(char *fname, uint8_t *src, size_t len) {
    uint32_t num_samples;

    if (len < 22) return;
    num_samples = amos_deek(&src[20]);
    if (len < 22 + (num_samples * 4)) return;

    /* create an IFF 8SVX file for each sample */
    for (uint32_t i = 0; i < num_samples; i++) {
        uint8_t *svx;
        uint32_t offset = amos_leek(&src[22 + i*4]) + 22;
        uint32_t freq, sam_len, svx_len;
        uint8_t name[7];

        if (offset > len) return;

        /* copy sample name, change non-alnum to "_" */
        memcpy(name, &src[offset], 6);
        for (int i = 0; i < 6; i++) if (!isalnum(name[i])) name[i] = '_';
        name[6] = 0;

        freq    = amos_deek(&src[offset + 8]);
        sam_len = amos_leek(&src[offset + 10]);

        if ((offset + 14 + sam_len) > len) {
            sam_len = len - (offset + 14);
        }

        svx_len = IFF_8SVX_HEADER_LEN + sam_len;
        if ((svx = malloc(svx_len))) {
            char *outname;
            memcpy(svx, iff_8svx_header, IFF_8SVX_HEADER_LEN);
            amos_loke(&svx[0x04], svx_len - 8);
            amos_doke(&svx[0x20], freq);
            amos_loke(&svx[0x2C], sam_len);
            memcpy(&svx[0x30], &src[offset + 14], sam_len);

            /* save IFF 8SVX */
            if ((outname = output_fname(fname, "sample%02X.%s.8svx", i + 1, name))) {
                printf("%s: extracting sample %d to 8SVX sample %s (%d bytes)\n",
                       fname, i+1, outname, svx_len);
                write_file(outname, svx, svx_len);
                free(outname);
            }
            else {
                perror(fname);
            }

            free(svx);
        }
    }

}

// Assembly:
cmpq $0x16, %rdx
jb 0x2238
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdx, %rbp
movq %rsi, %r14
movzwl 0x14(%rsi), %eax
rolw $0x8, %ax
movzwl %ax, %eax
leal 0x16(,%rax,4), %ecx
cmpq %rdx, %rcx
seta %cl
testw %ax, %ax
sete %dl
orb %cl, %dl
jne 0x222a
movl %eax, %eax
movq %rax, 0x20(%rsp)
xorl %r12d, %r12d
movq %rbp, 0x28(%rsp)
movq %rdi, 0x18(%rsp)
movl 0x16(%r14,%r12,4), %r15d
bswapl %r15d
leal 0x16(%r15), %eax
cmpq %rbp, %rax
ja 0x222a
movzwl 0x4(%r14,%rax), %ecx
movw %cx, 0x14(%rsp)
movl (%r14,%rax), %eax
movl %eax, 0x10(%rsp)
callq 0x1150
xorl %ecx, %ecx
movq (%rax), %rdx
movzbl 0x10(%rsp,%rcx), %esi
testb $0x8, (%rdx,%rsi,2)
jne 0x20dc
movb $0x5f, 0x10(%rsp,%rcx)
incq %rcx
cmpq $0x6, %rcx
jne 0x20c9
movb $0x0, 0x16(%rsp)
leal 0x1e(%r15), %eax
movb (%r14,%rax), %r13b
movb 0x1(%r14,%rax), %al
movb %al, 0xf(%rsp)
leal 0x20(%r15), %eax
movl (%r14,%rax), %eax
bswapl %eax
movl %r15d, %ecx
addl $0x24, %r15d
addl %eax, %ecx
addl $0x24, %ecx
movl %ebp, %ebx
subl %r15d, %ebx
cmpq %rbp, %rcx
cmovbel %eax, %ebx
leal 0x30(%rbx), %edi
movq %rdi, 0x30(%rsp)
callq 0x1110
testq %rax, %rax
je 0x220b
movq %rax, %rbp
movaps 0x11e4(%rip), %xmm0     # 0x3320
movups %xmm0, (%rax)
movaps 0x11fa(%rip), %xmm0     # 0x3340
movups %xmm0, 0x20(%rax)
movaps 0x11df(%rip), %xmm0     # 0x3330
movups %xmm0, 0x10(%rax)
leal 0x28(%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)
movb %al, 0x7(%rbp)
movb %r13b, 0x20(%rbp)
movb 0xf(%rsp), %al
movb %al, 0x21(%rbp)
movl %ebx, %eax
shrl $0x18, %eax
movb %al, 0x2c(%rbp)
movl %ebx, %eax
shrl $0x10, %eax
movb %al, 0x2d(%rbp)
movb %bh, 0x2e(%rbp)
movb %bl, 0x2f(%rbp)
movq %rbp, %rdi
addq $0x30, %rdi
movl %r15d, %esi
addq %r14, %rsi
movl %ebx, %edx
callq 0x1100
leaq 0x1(%r12), %r15
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
leaq 0xf03(%rip), %rsi       # 0x30ba
movl %r15d, %edx
leaq 0x10(%rsp), %rcx
xorl %eax, %eax
callq 0x1394
testq %rax, %rax
je 0x2220
movq %rax, %r13
leaq 0xef8(%rip), %rdi       # 0x30cd
movq %rbx, %rsi
movl %r15d, %edx
movq %rax, %rcx
movq 0x30(%rsp), %rbx
movl %ebx, %r8d
xorl %eax, %eax
callq 0x10c0
movq %r13, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x132e
movq %r13, %rdi
callq 0x1030
movq %rbp, %rdi
callq 0x1030
incq %r12
cmpq 0x20(%rsp), %r12
movq 0x28(%rsp), %rbp
jne 0x209a
jmp 0x222a
movq %rbx, %rdi
callq 0x1130
jmp 0x2203
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq