// File: /workspace/github_c_makefile_star2_O1/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 0xea9a9
movq %rax, %rbp
movq 0x18(%r12), %rdi
movq %r15, %rsi
callq 0xef475
movl %eax, 0x1c(%r14)
leaq 0xa68ba(%rip), %rsi     # 0x13ece3
leaq 0x980d8(%rip), %rdx     # 0x130508
movq %rbx, %rdi
movl %eax, %ecx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r15, %rcx
xorl %r8d, %r8d
callq 0x98866
movq 0x28(%r14), %rdi
callq 0xf6c00
testl %eax, %eax
je 0x98477
cmpb $0x0, 0xd0(%rbx)
je 0x98477
movq 0x28(%r14), %rdx
movq %rbx, %rdi
movq %rbp, %rsi
xorl %ecx, %ecx
callq 0xea9e4
cmpl $0x3, (%rbx)
jl 0x984a7
movq 0x18(%r12), %rsi
movq 0x10(%rsp), %rdx
movl $0x0, (%rsp)
leaq 0x980db(%rip), %r9      # 0x13056f
movl $0x60, %ecx
movl $0x60, %r8d
movq %rbx, %rdi
callq 0xe975a
movq 0x18(%r12), %rdi
leaq 0x10(%rsp), %rsi
callq 0xef1ed
movb $0x1, %r15b
cmpl $0x200, %eax            # imm = 0x200
movq %rbp, 0x20(%rsp)
jne 0x984ed
leaq 0x10(%rsp), %rsi
addq $0x14, (%rsi)
movq 0x18(%r12), %rdi
callq 0xef14d
movzbl %al, %edx
cmpb $0x2b, %dl
je 0x984f4
cmpl $0x56, %edx
jne 0x984fc
movl $0x5622, %r14d          # imm = 0x5622
jmp 0x98503
xorl %ebp, %ebp
jmp 0x9861e
movl $0x2b11, %r14d          # imm = 0x2B11
jmp 0x98503
movl %edx, %r14d
shll $0x8, %r14d
leaq 0x9806c(%rip), %rsi     # 0x130576
xorl %ebp, %ebp
movq %rbx, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0xe9226
cmpq $0x28, %r14
jb 0x9861e
leaq 0x10(%rsp), %rbp
addq $0xb, (%rbp)
movq 0x18(%r12), %rdi
movq %rbp, %rsi
callq 0xef14d
cmpb $-0x1, %al
movl $0x2b, %eax
movl $0x1, %r15d
cmoveq %rax, %r15
addq (%rbp), %r15
movq %r15, (%rbp)
movq 0x18(%rsp), %rax
addq %r13, %rax
movq %rax, %r13
subq %r15, %r13
testq %r13, %r13
jle 0x98619
leaq 0x9802a(%rip), %rsi     # 0x13059b
movq %rbx, %rdi
movq 0x20(%rsp), %rdx
xorl %ecx, %ecx
callq 0xf037a
movq %rax, %rbp
movl $0x52494646, %esi       # imm = 0x52494646
movq %rax, %rdi
callq 0xf1629
movl %r13d, %eax
andl $0x1, %eax
movq %rax, 0x18(%rsp)
leaq (%rax,%r13), %rsi
addq $0x24, %rsi
movq %rbp, %rdi
callq 0xf1645
leaq 0x981ca(%rip), %rsi     # 0x13077c
movl $0x10, %edx
movq %rbp, %rdi
callq 0xefb82
movq %rbp, %rdi
movq %r14, %rsi
callq 0xf1645
movq %rbp, %rdi
movq %r14, %rsi
callq 0xf1645
leaq 0x981b1(%rip), %rsi     # 0x13078d
movl $0x8, %edx
movq %rbp, %rdi
callq 0xefb82
movq %rbp, %rdi
movq %r13, %rsi
callq 0xf1645
movq 0x18(%r12), %rdi
movq %r15, %rsi
xorl %r15d, %r15d
movq %r13, %rdx
movq %rbp, %rcx
callq 0xefabc
movq %rbp, %rdi
movq 0x18(%rsp), %rsi
callq 0xf1346
jmp 0x9861e
xorl %ebp, %ebp
movb $0x1, %r15b
testb %r15b, %r15b
je 0x98634
leaq 0x97f75(%rip), %rsi     # 0x13059f
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9b27
movq %rbp, %rdi
callq 0xf0b13
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
callq 0xea9b3
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq