// File: /workspace/github_c_makefile_star2_O0/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:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
cmpq $0x16, -0x18(%rbp)
jae 0x27f0
jmp 0x2b4f
movq -0x10(%rbp), %rax
movzbl 0x14(%rax), %eax
shll $0x8, %eax
movq -0x10(%rbp), %rcx
movzbl 0x15(%rcx), %ecx
orl %ecx, %eax
movl %eax, -0x1c(%rbp)
movq -0x18(%rbp), %rax
movl -0x1c(%rbp), %ecx
shll $0x2, %ecx
addl $0x16, %ecx
movl %ecx, %ecx
cmpq %rcx, %rax
jae 0x2821
jmp 0x2b4f
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jae 0x2b4f
movq -0x10(%rbp), %rax
movl -0x20(%rbp), %ecx
shll $0x2, %ecx
addl $0x16, %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %eax
shll $0x18, %eax
movq -0x10(%rbp), %rcx
movl -0x20(%rbp), %edx
shll $0x2, %edx
addl $0x16, %edx
movl %edx, %edx
movzbl 0x1(%rcx,%rdx), %ecx
shll $0x10, %ecx
orl %ecx, %eax
movq -0x10(%rbp), %rcx
movl -0x20(%rbp), %edx
shll $0x2, %edx
addl $0x16, %edx
movl %edx, %edx
movzbl 0x2(%rcx,%rdx), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movq -0x10(%rbp), %rcx
movl -0x20(%rbp), %edx
shll $0x2, %edx
addl $0x16, %edx
movl %edx, %edx
movzbl 0x3(%rcx,%rdx), %ecx
orl %ecx, %eax
addl $0x16, %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpq -0x18(%rbp), %rax
jbe 0x28a6
jmp 0x2b4f
movq -0x10(%rbp), %rax
movl -0x2c(%rbp), %ecx
movl (%rax,%rcx), %edx
movl %edx, -0x3f(%rbp)
movw 0x4(%rax,%rcx), %ax
movw %ax, -0x3b(%rbp)
movl $0x0, -0x44(%rbp)
cmpl $0x6, -0x44(%rbp)
jge 0x28ff
callq 0x1160
movq (%rax), %rax
movslq -0x44(%rbp), %rcx
movzbl -0x3f(%rbp,%rcx), %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x8, %eax
cmpl $0x0, %eax
jne 0x28f2
movslq -0x44(%rbp), %rax
movb $0x5f, -0x3f(%rbp,%rax)
jmp 0x28f4
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x28c3
movb $0x0, -0x39(%rbp)
movq -0x10(%rbp), %rax
movl -0x2c(%rbp), %ecx
addl $0x8, %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %eax
shll $0x8, %eax
movq -0x10(%rbp), %rcx
movl -0x2c(%rbp), %edx
addl $0x8, %edx
movl %edx, %edx
movzbl 0x1(%rcx,%rdx), %ecx
orl %ecx, %eax
movl %eax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movl -0x2c(%rbp), %ecx
addl $0xa, %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %eax
shll $0x18, %eax
movq -0x10(%rbp), %rcx
movl -0x2c(%rbp), %edx
addl $0xa, %edx
movl %edx, %edx
movzbl 0x1(%rcx,%rdx), %ecx
shll $0x10, %ecx
orl %ecx, %eax
movq -0x10(%rbp), %rcx
movl -0x2c(%rbp), %edx
addl $0xa, %edx
movl %edx, %edx
movzbl 0x2(%rcx,%rdx), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movq -0x10(%rbp), %rcx
movl -0x2c(%rbp), %edx
addl $0xa, %edx
movl %edx, %edx
movzbl 0x3(%rcx,%rdx), %ecx
orl %ecx, %eax
movl %eax, -0x34(%rbp)
movl -0x2c(%rbp), %eax
addl $0xe, %eax
addl -0x34(%rbp), %eax
movl %eax, %eax
cmpq -0x18(%rbp), %rax
jbe 0x29a4
movq -0x18(%rbp), %rax
movl -0x2c(%rbp), %ecx
addl $0xe, %ecx
movl %ecx, %ecx
subq %rcx, %rax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
addl $0x30, %eax
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %eax
movl %eax, %edi
callq 0x1110
movq %rax, -0x28(%rbp)
cmpq $0x0, %rax
je 0x2b3f
movq -0x28(%rbp), %rdi
leaq 0x1960(%rip), %rsi      # 0x4330
movl $0x30, %edx
callq 0x1100
movl -0x38(%rbp), %eax
subl $0x8, %eax
shrl $0x18, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, 0x4(%rax)
movl -0x38(%rbp), %eax
subl $0x8, %eax
shrl $0x10, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, 0x5(%rax)
movl -0x38(%rbp), %eax
subl $0x8, %eax
shrl $0x8, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, 0x6(%rax)
movl -0x38(%rbp), %eax
subl $0x8, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, 0x7(%rax)
jmp 0x2a35
movl -0x30(%rbp), %eax
shrl $0x8, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, 0x20(%rax)
movl -0x30(%rbp), %eax
andl $0xff, %eax
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, 0x21(%rax)
jmp 0x2a5c
movl -0x34(%rbp), %eax
shrl $0x18, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, 0x2c(%rax)
movl -0x34(%rbp), %eax
shrl $0x10, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, 0x2d(%rax)
movl -0x34(%rbp), %eax
shrl $0x8, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, 0x2e(%rax)
movl -0x34(%rbp), %eax
andl $0xff, %eax
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, 0x2f(%rax)
movq -0x28(%rbp), %rdi
addq $0x30, %rdi
movq -0x10(%rbp), %rsi
movl -0x2c(%rbp), %eax
addl $0xe, %eax
movl %eax, %eax
addq %rax, %rsi
movl -0x34(%rbp), %eax
movl %eax, %edx
callq 0x1100
movq -0x8(%rbp), %rdi
movl -0x20(%rbp), %edx
addl $0x1, %edx
leaq -0x3f(%rbp), %rcx
leaq 0x15e0(%rip), %rsi      # 0x40bf
movb $0x0, %al
callq 0x1420
movq %rax, -0x50(%rbp)
cmpq $0x0, %rax
je 0x2b2d
movq -0x8(%rbp), %rsi
movl -0x20(%rbp), %edx
addl $0x1, %edx
movq -0x50(%rbp), %rcx
movl -0x38(%rbp), %r8d
leaq 0x15c9(%rip), %rdi      # 0x40d2
movb $0x0, %al
callq 0x10b0
movq -0x50(%rbp), %rdi
movq -0x28(%rbp), %rsi
movl -0x38(%rbp), %eax
movl %eax, %edx
callq 0x13a0
movq -0x50(%rbp), %rdi
callq 0x1030
jmp 0x2b36
movq -0x8(%rbp), %rdi
callq 0x1130
movq -0x28(%rbp), %rdi
callq 0x1030
jmp 0x2b41
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x2828
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)