// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x40, %rsp
movb %dl, %al
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movb %al, -0x11(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movl $0x2, %esi
callq 0x15b780
movq %rax, -0x20(%rbp)
movb -0x11(%rbp), %cl
movq -0x20(%rbp), %rax
movb %cl, (%rax)
movq -0x8(%rbp), %rdi
callq 0x18c8b0
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x28(%rbp), %rax
leaq 0x1b4(%rip), %rcx       # 0x3bb50
movq %rcx, 0x28(%rax)
movq -0x20(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x1, %eax
jne 0x3b9bd
movq -0x28(%rbp), %rax
leaq 0x249(%rip), %rcx       # 0x3bc00
movq %rcx, 0x20(%rax)
jmp 0x3b9cc
movq -0x28(%rbp), %rax
leaq 0x2d8(%rip), %rcx       # 0x3bca0
movq %rcx, 0x20(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x28(%rbp), %rax
movl $0x1, 0x58(%rax)
movq -0x28(%rbp), %rax
movb $0x0, 0x5c(%rax)
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
cmpq $0xc, 0x18(%rax)
jge 0x3ba03
jmp 0x3bb25
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movl %eax, %edx
leaq 0x18957a(%rip), %rsi    # 0x1c4f8e
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x20(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x0, %eax
jne 0x3bab9
movq -0x38(%rbp), %rax
addq $0x3, %rax
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x165170
movb %al, %cl
movq -0x20(%rbp), %rax
movb %cl, 0x1(%rax)
movq -0x8(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x20(%rbp), %rax
movzbl 0x1(%rax), %edi
callq 0x161a80
movq -0x40(%rbp), %rdi
movsbl %al, %edx
leaq 0x166baa(%rip), %rsi    # 0x1a262c
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rax
movzbl 0x1(%rax), %eax
cmpl $0x31, %eax
jl 0x3baa3
movq -0x20(%rbp), %rax
movzbl 0x1(%rax), %eax
cmpl $0x32, %eax
jle 0x3bab7
movq -0x8(%rbp), %rdi
leaq 0x166b93(%rip), %rsi    # 0x1a2641
movb $0x0, %al
callq 0x15bfb0
jmp 0x3bb25
jmp 0x3bac5
movq -0x38(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x165580
movl %eax, -0x2c(%rbp)
movq -0x8(%rbp), %rdi
movl -0x2c(%rbp), %edx
leaq 0x19563b(%rip), %rsi    # 0x1d112e
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x28(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rdx
subq -0x38(%rbp), %rdx
callq 0x18c920
movq -0x28(%rbp), %rdi
callq 0x18c8f0
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x15b820
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)