// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/mmm.c
// Function: do_mmm_snd
static void do_mmm_snd(deark *c, struct mmm_ctx *d, struct de_iffctx *ictx, i64 pos1, i64 len)
{
	i64 pos = pos1;
	i64 apos, alen;
	dbuf *outf = NULL;
	de_finfo *fi = NULL;
	i64 pad = 0;
	i64 sample_rate;
	UI sr_code;
	UI n;
	u8 x;
	u8 ok = 0;

	fi = de_finfo_create(c);
	mmm_read_rsrcid_p(c, d, ictx, &pos, NULL);

	mmm_read_name_p(c, d, ictx, &pos, NULL);
	if(ucstring_isnonempty(d->tmp_namestr) && c->filenames_from_file) {
		de_finfo_set_name_from_ucstring(c, fi, d->tmp_namestr, 0);
	}

	// Note: The code in this function is probably wrong. Don't use it for
	// anything important.

	if(c->debug_level>=3) {
		de_dbg_hexdump(c, ictx->f, pos, 96, 96, "sndhdr", 0);
	}

	n = (UI)dbuf_getu16le_p(ictx->f, &pos);
	if(n != 0x0200) goto done;
	pos += 20;
	sr_code = (UI)dbuf_getbyte_p(ictx->f, &pos);
	if(sr_code==0x2b) sample_rate = 11025;
	else if(sr_code==0x56) sample_rate = 22050;
	else sample_rate = sr_code * 256;
	de_dbg(c, "sample rate info: 0x%02x (using %"I64_FMT")", sr_code, sample_rate);
	if(sample_rate<40) goto done;
	pos += 11;
	x = dbuf_getbyte_p(ictx->f, &pos);
	pos += 1;
	// Usually the header is 36 bytes, but sometimes 78.
	if(x==0xff) pos += 42;

	apos = pos;
	alen = pos1+len-apos;
	if(alen<=0) goto done;

	outf = dbuf_create_output_file(c, "wav", fi, 0);
	dbuf_writeu32be(outf, CODE_RIFF);
	pad = alen%2;
	dbuf_writeu32le(outf, alen+36+pad);
	dbuf_write(outf, (const u8*)"WAVEfmt \x10\0\0\0\x01\0\x01\0", 16);
	dbuf_writeu32le(outf, sample_rate);
	dbuf_writeu32le(outf, sample_rate);
	dbuf_write(outf, (const u8*)"\x01\0\x08\0" "data", 8);
	dbuf_writeu32le(outf, alen);
	dbuf_copy(ictx->f, apos, alen, outf);
	dbuf_write_zeroes(outf, pad);
	ok = 1;

done:
	if(!ok) {
		de_warn(c, "Unsupported type of audio resource");
	}
	dbuf_close(outf);
	de_finfo_destroy(c, fi);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r8, 0x18(%rsp)
movq %rcx, %r13
movq %rdx, %r12
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x10(%rsp), %r15
movq %rcx, (%r15)
callq 0xea504
movq %rax, 0x20(%rsp)
movq 0x18(%r12), %rdi
movq %r15, %rsi
callq 0xeee2f
movl %eax, 0x1c(%r14)
leaq 0xa754a(%rip), %rsi     # 0x13f0e3
leaq 0x98ca3(%rip), %rdx     # 0x130843
movq %rbx, %rdi
movl %eax, %ecx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r15, %rcx
xorl %r8d, %r8d
callq 0x97fd7
movq 0x28(%r14), %rdi
callq 0xf67a3
testl %eax, %eax
je 0x97be9
cmpb $0x0, 0xd0(%rbx)
je 0x97be9
movq 0x28(%r14), %rdx
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
xorl %ecx, %ecx
callq 0xea53f
cmpl $0x3, (%rbx)
jl 0x97c19
movq 0x18(%r12), %rsi
movq 0x10(%rsp), %rdx
movl $0x0, (%rsp)
leaq 0x98ca4(%rip), %r9      # 0x1308aa
movl $0x60, %ecx
movl $0x60, %r8d
movq %rbx, %rdi
callq 0xe8bea
movq 0x18(%r12), %rdi
leaq 0x10(%rsp), %rsi
callq 0xeebeb
cmpl $0x200, %eax            # imm = 0x200
jne 0x97d91
leaq 0x10(%rsp), %rsi
addq $0x14, (%rsi)
movq 0x18(%r12), %rdi
callq 0xeeb4b
movzbl %al, %r14d
cmpb $0x2b, %r14b
je 0x97c5d
cmpl $0x56, %r14d
jne 0x97c7b
movl $0x5622, %ecx           # imm = 0x5622
jmp 0x97c62
movl $0x2b11, %ecx           # imm = 0x2B11
leaq 0x98c48(%rip), %rsi     # 0x1308b1
movq %rbx, %rdi
movl %r14d, %edx
movq %rcx, %rbp
xorl %eax, %eax
callq 0xe8617
jmp 0x97ca1
movl %r14d, %ecx
shll $0x8, %ecx
leaq 0x98c29(%rip), %rsi     # 0x1308b1
movq %rbx, %rdi
movl %r14d, %edx
movq %rcx, %rbp
xorl %eax, %eax
callq 0xe8617
testl %r14d, %r14d
je 0x97d91
leaq 0x10(%rsp), %r15
addq $0xb, (%r15)
movq 0x18(%r12), %rdi
movq %r15, %rsi
callq 0xeeb4b
cmpb $-0x1, %al
movl $0x2b, %eax
movl $0x1, %r14d
cmoveq %rax, %r14
addq (%r15), %r14
movq %r14, (%r15)
movq 0x18(%rsp), %rax
addq %r13, %rax
subq %r14, %rax
testq %rax, %rax
jle 0x97d91
movq %rax, %r15
leaq 0x98bea(%rip), %rsi     # 0x1308d6
movq %rbx, %rdi
movq 0x20(%rsp), %rdx
xorl %ecx, %ecx
callq 0xefda1
movq %rax, %r13
movl $0x52494646, %esi       # imm = 0x52494646
movq %rax, %rdi
callq 0xf1026
movl %r15d, %eax
andl $0x1, %eax
movq %rax, 0x18(%rsp)
leaq (%r15,%rax), %rsi
addq $0x24, %rsi
movq %r13, %rdi
callq 0xf1042
leaq 0x98d8a(%rip), %rsi     # 0x130ab7
movl $0x10, %edx
movq %r13, %rdi
callq 0xef547
movq %r13, %rdi
movq %rbp, %rsi
callq 0xf1042
movq %r13, %rdi
movq %rbp, %rsi
callq 0xf1042
leaq 0x98d71(%rip), %rsi     # 0x130ac8
movl $0x8, %edx
movq %r13, %rdi
callq 0xef547
movq %r13, %rdi
movq %r15, %rsi
callq 0xf1042
movq 0x18(%r12), %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %r13, %rcx
callq 0xef481
movq %r13, %rdi
movq 0x18(%rsp), %rsi
callq 0xf0d35
jmp 0x97da5
leaq 0x98b42(%rip), %rsi     # 0x1308da
xorl %r13d, %r13d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8faa
movq %r13, %rdi
callq 0xf052b
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
callq 0xea50e
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq