// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/apple2-dsk.c
// Function: do_run_wozmoof
static void do_run_wozmoof(deark *c, de_module_params *mparams, u8 fmt)
{
	struct wozmoofctx_struct *d = NULL;
	struct de_iffctx *ictx = NULL;
	u32 crc;
	i64 pos = 0;

	// WOZ/MOOF has a 12-byte header, then sequence of chunks that are basically the
	// same format as RIFF.
	d = de_malloc(c, sizeof(struct wozmoofctx_struct));
	d->fmt = fmt;
	ictx = fmtutil_create_iff_decoder(c);

	ictx->userdata = (void*)d;
	ictx->preprocess_chunk_fn = my_preprocess_wozmoof_chunk_fn;
	if(d->fmt==FMT_MOOF) {
		ictx->handle_chunk_fn = my_moof_chunk_handler;
	}
	else {
		ictx->handle_chunk_fn = my_woz_chunk_handler;
	}
	ictx->f = c->infile;
	ictx->is_le = 1;
	ictx->reversed_4cc = 0;

	if(ictx->f->len<12) goto done;
	de_dbg(c, "header at %d", (int)pos);
	de_dbg_indent(c, 1);
	if(d->fmt==FMT_WOZ) {
		pos += 3; // "WOZ" part of signature
		d->wozver = dbuf_getbyte_p(ictx->f, &pos);
		de_dbg(c, "format version: '%c'", de_byte_to_printable_char(d->wozver));
		if(d->wozver<'1' || d->wozver>'2') {
			de_err(c, "Unsupported WOZ format version");
			goto done;
		}
	}
	else {
		pos += 4;
	}
	pos += 4; // rest of signature
	crc = (u32)dbuf_getu32le_p(ictx->f, &pos);
	de_dbg(c, "crc: 0x%08x", (unsigned int)crc);
	de_dbg_indent(c, -1);

	fmtutil_read_iff_format(ictx, pos, ictx->f->len-pos);

done:
	fmtutil_destroy_iff_decoder(ictx);
	de_free(c, d);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x10, %rsp
movl %esi, %ebp
movq %rdi, %rbx
movq $0x0, 0x8(%rsp)
movl $0x2, %esi
callq 0xe969b
movq %rax, %r14
movb %bpl, (%rax)
movq %rbx, %rdi
callq 0x107b04
movq %rax, %r15
movq %r14, 0x10(%rax)
leaq 0x135(%rip), %rax       # 0x2e60f
movq %rax, 0x28(%r15)
cmpb $0x1, (%r14)
leaq 0x18e(%rip), %rax       # 0x2e677
leaq 0x377(%rip), %rcx       # 0x2e867
cmoveq %rax, %rcx
movq %rcx, 0x20(%r15)
movq 0x18(%rbx), %rax
movq %rax, 0x18(%r15)
movl $0x1, 0x58(%r15)
movb $0x0, 0x5c(%r15)
cmpq $0xc, 0x18(%rax)
jl 0x2e5ef
movl 0x8(%rsp), %edx
leaq 0x10ae3b(%rip), %rsi    # 0x13935e
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
cmpb $0x0, (%r14)
movq 0x8(%rsp), %rax
je 0x2e550
addq $0x4, %rax
movq %rax, 0x8(%rsp)
jmp 0x2e5a2
addq $0x3, %rax
leaq 0x8(%rsp), %rsi
movq %rax, (%rsi)
movq 0x18(%r15), %rdi
callq 0xef14d
movb %al, 0x1(%r14)
movzbl %al, %edi
callq 0xecff6
movsbl %al, %edx
leaq 0xe81e5(%rip), %rsi     # 0x116760
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movb 0x1(%r14), %al
addb $-0x33, %al
cmpb $-0x3, %al
ja 0x2e5a2
leaq 0xe81df(%rip), %rsi     # 0x116775
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
jmp 0x2e5ef
leaq 0x8(%rsp), %r12
addq $0x4, (%r12)
movq 0x18(%r15), %rdi
movq %r12, %rsi
callq 0xef3a0
leaq 0x116b7f(%rip), %rsi    # 0x14513e
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
movq (%r12), %rsi
movq 0x18(%r15), %rax
movq 0x18(%rax), %rdx
subq %rsi, %rdx
movq %r15, %rdi
callq 0x107b28
movq %r15, %rdi
callq 0x107b17
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe9752
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq