// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x30(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movb $0x0, -0x6a(%rbp)
movq -0x8(%rbp), %rdi
callq 0x15d230
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
leaq -0x30(%rbp), %rcx
xorl %eax, %eax
movl %eax, %r8d
callq 0xdf600
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
leaq -0x30(%rbp), %rcx
xorl %eax, %eax
movl %eax, %r8d
callq 0xdf680
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x1717f0
cmpl $0x0, %eax
je 0xdf0fd
movq -0x8(%rbp), %rax
movzbl 0xd0(%rax), %eax
cmpl $0x0, %eax
je 0xdf0fd
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdx
xorl %ecx, %ecx
callq 0x15d2c0
movq -0x8(%rbp), %rax
cmpl $0x3, (%rax)
jl 0xdf134
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x30(%rbp), %rdx
movl $0x60, %r8d
leaq 0xdd237(%rip), %r9      # 0x1bc35a
xorl %eax, %eax
movq %r8, %rcx
movl $0x0, (%rsp)
callq 0x15b840
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x1652c0
movl %eax, -0x68(%rbp)
cmpl $0x200, -0x68(%rbp)     # imm = 0x200
je 0xdf156
jmp 0xdf2fd
movq -0x30(%rbp), %rax
addq $0x14, %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165170
movzbl %al, %eax
movl %eax, -0x64(%rbp)
cmpl $0x2b, -0x64(%rbp)
jne 0xdf189
movq $0x2b11, -0x60(%rbp)    # imm = 0x2B11
jmp 0xdf1a7
cmpl $0x56, -0x64(%rbp)
jne 0xdf199
movq $0x5622, -0x60(%rbp)    # imm = 0x5622
jmp 0xdf1a5
movl -0x64(%rbp), %eax
shll $0x8, %eax
movl %eax, %eax
movq %rax, -0x60(%rbp)
jmp 0xdf1a7
movq -0x8(%rbp), %rdi
movl -0x64(%rbp), %edx
movq -0x60(%rbp), %rcx
leaq 0xdd1a8(%rip), %rsi     # 0x1bc361
movb $0x0, %al
callq 0x15b0d0
cmpq $0x28, -0x60(%rbp)
jge 0xdf1cc
jmp 0xdf2fd
movq -0x30(%rbp), %rax
addq $0xb, %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165170
movb %al, -0x69(%rbp)
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
movzbl -0x69(%rbp), %eax
cmpl $0xff, %eax
jne 0xdf20f
movq -0x30(%rbp), %rax
addq $0x2a, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x20(%rbp), %rax
addq -0x28(%rbp), %rax
subq -0x38(%rbp), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jg 0xdf233
jmp 0xdf2fd
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rdx
leaq 0xdd144(%rip), %rsi     # 0x1bc386
xorl %ecx, %ecx
callq 0x166c60
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
movl $0x52494646, %esi       # imm = 0x52494646
callq 0x168b80
movq -0x40(%rbp), %rax
movl $0x2, %ecx
cqto
idivq %rcx
movq %rdx, -0x58(%rbp)
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rsi
addq $0x24, %rsi
addq -0x58(%rbp), %rsi
callq 0x168c30
movq -0x48(%rbp), %rdi
leaq 0xdd2da(%rip), %rsi     # 0x1bc567
movl $0x10, %edx
callq 0x166170
movq -0x48(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0x168c30
movq -0x48(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0x168c30
movq -0x48(%rbp), %rdi
leaq 0xdd2bc(%rip), %rsi     # 0x1bc578
movl $0x8, %edx
callq 0x166170
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x168c30
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x48(%rbp), %rcx
callq 0x166070
movq -0x48(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x1685f0
movb $0x1, -0x6a(%rbp)
cmpb $0x0, -0x6a(%rbp)
jne 0xdf315
movq -0x8(%rbp), %rdi
leaq 0xdd07c(%rip), %rsi     # 0x1bc38a
movb $0x0, %al
callq 0x15bc10
movq -0x48(%rbp), %rdi
callq 0x1676a0
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x15d260
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)