// File: /workspace/github_c_makefile_star2_O3/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 0x2115
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, %r13
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 0x2107
movl %eax, %eax
movq %rax, 0x20(%rsp)
xorl %r12d, %r12d
movq %r13, 0x10(%rsp)
movq %rdi, 0x18(%rsp)
movl 0x16(%r14,%r12,4), %r15d
bswapl %r15d
leal 0x16(%r15), %eax
cmpq %r13, %rax
ja 0x2107
movzwl 0x4(%r14,%rax), %ecx
movw %cx, 0xc(%rsp)
movl (%r14,%rax), %eax
movl %eax, 0x8(%rsp)
callq 0x1140
movq (%rax), %rax
xorl %ecx, %ecx
movzbl 0x8(%rsp,%rcx), %edx
testb $0x8, (%rax,%rdx,2)
jne 0x1fb2
movb $0x5f, 0x8(%rsp,%rcx)
incq %rcx
cmpq $0x6, %rcx
jne 0x1fa2
movb $0x0, 0xe(%rsp)
leal 0x1e(%r15), %eax
movb (%r14,%rax), %cl
movb %cl, 0x7(%rsp)
movb 0x1(%r14,%rax), %al
movb %al, 0x6(%rsp)
leal 0x20(%r15), %eax
movl (%r14,%rax), %eax
bswapl %eax
movl %r15d, %ecx
addl $0x24, %r15d
addl %eax, %ecx
addl $0x24, %ecx
movl %r13d, %ebx
subl %r15d, %ebx
cmpq %r13, %rcx
cmovbel %eax, %ebx
leal 0x30(%rbx), %r13d
movq %r13, %rdi
callq 0x1100
testq %rax, %rax
je 0x20e8
movq %rax, %rbp
movaps 0x130a(%rip), %xmm0     # 0x3320
movups %xmm0, (%rax)
movups 0x131c(%rip), %xmm0     # 0x333c
movups %xmm0, 0x1c(%rax)
movaps 0x1305(%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 0x7(%rsp), %al
movb %al, 0x20(%rbp)
movb 0x6(%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 0x10f0
incq %r12
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
leaq 0x1028(%rip), %rsi      # 0x30ba
movl %r12d, %edx
leaq 0x8(%rsp), %rcx
xorl %eax, %eax
callq 0x1383
testq %rax, %rax
je 0x20fd
movq %rax, %r15
leaq 0x101d(%rip), %rdi      # 0x30cd
movq %rbx, %rsi
movl %r12d, %edx
movq %rax, %rcx
movl %r13d, %r8d
xorl %eax, %eax
callq 0x10c0
movq %r15, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x131e
movq %r15, %rdi
callq 0x1030
movq 0x10(%rsp), %r13
movq %rbp, %rdi
callq 0x1030
jmp 0x20f0
incq %r12
movq 0x10(%rsp), %r13
cmpq 0x20(%rsp), %r12
jne 0x1f70
jmp 0x2107
movq %rbx, %rdi
callq 0x1120
jmp 0x20d9
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq