// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/nufx.c
// Function: extract_main_threads
static void extract_main_threads(deark *c, struct nufx_ctx *d,
	struct nufx_record *rec, u8 disk_image_flag)
{
	struct de_advfile *advf = NULL;
	struct nufx_thread *t_d;
	struct nufx_thread *t_r;
	const char *dname;
	u32 d_crc_calc, r_crc_calc;
	u8 ok_cmpr;

	if(disk_image_flag) {
		dname = "disk image";
		t_d = rec->disk_image_thread_ptr;
		t_r = NULL;
	}
	else {
		dname = "data fork";
		t_d = rec->data_thread_ptr;
		t_r = rec->resource_thread_ptr;
	}

	if(!t_d && !t_r) {
		goto done;
	}

	ok_cmpr = 1;
	if(t_d) {
		if(t_d->orig_len>0 && !cmpr_meth_is_supported(t_d->cmpr_meth)) {
			ok_cmpr = 0;
		}
	}
	if(t_r) {
		if(t_r->orig_len>0 && !cmpr_meth_is_supported(t_r->cmpr_meth)) {
			ok_cmpr = 0;
		}
	}
	// Continue only if we're pretty sure we can decompress both forks.
	if(!ok_cmpr) {
		de_err(c, "record #%u: Compression method not supported", rec->idx);
		goto done;
	}

	advf = de_advfile_create(c);
	advf->userdata = (void*)rec;
	advf->writefork_cbfn = disk_image_flag ? my_advfile_cbfn_diskimage : my_advfile_cbfn;
	advf->enable_wbuffer = 1;

	advf->mainfork.fi->timestamp[DE_TIMESTAMPIDX_MODIFY] = rec->mod_time;
	advf->mainfork.fi->timestamp[DE_TIMESTAMPIDX_CREATE] = rec->create_time;

	ucstring_append_ucstring(advf->filename, rec->filename);
	advf->original_filename_flag = 1;
	advf->snflags |= DE_SNFLAG_FULLPATH;
	//de_advfile_set_orig_filename(rec->advf, ...); // TODO?

	if(t_d) {
		advf->mainfork.fork_exists = 1;
		advf->mainfork.fork_len = t_d->orig_len;
		advf->mainfork.writelistener_cb = de_writelistener_for_crc;
		advf->mainfork.userdata_for_writelistener = (void*)d->crco_dfork;
		de_crcobj_reset(d->crco_dfork);
	}
	if(t_r) {
		advf->rsrcfork.fork_exists = 1;
		advf->rsrcfork.fork_len = t_r->orig_len;
		advf->rsrcfork.writelistener_cb = de_writelistener_for_crc;
		advf->rsrcfork.userdata_for_writelistener = (void*)d->crco_rfork;
		de_crcobj_reset(d->crco_rfork);
	}

	de_advfile_run(advf);

	if(t_d && t_d->dcmpr_ok_flag && t_d->respect_crc_field) {
		d_crc_calc = de_crcobj_getval(d->crco_dfork);
		de_dbg(c, "%s crc (calculated): 0x%04x", dname, (UI)d_crc_calc);
		if(d_crc_calc!=t_d->crc_reported) {
			de_err(c, "CRC check failed for record #%u %s", rec->idx, dname);
		}
	}
	if(t_r && t_r->dcmpr_ok_flag && t_r->respect_crc_field) {
		r_crc_calc = de_crcobj_getval(d->crco_rfork);
		de_dbg(c, "rsrc fork crc (calculated): 0x%04x", (UI)r_crc_calc);
		if(r_crc_calc!=t_r->crc_reported) {
			de_err(c, "CRC check failed for record #%u resource fork", rec->idx);
		}
	}

done:
	de_advfile_destroy(advf);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %ecx, %ebp
movq %rdx, %r14
movq %rsi, %r15
testl %ecx, %ecx
je 0x93c5e
leaq 0xc0(%r14), %rax
leaq 0x8e599(%rip), %rdx     # 0x1221f2
xorl %r12d, %r12d
jmp 0x93c73
leaq 0xb0(%r14), %rax
movq 0xb8(%r14), %r12
leaq 0x82dbc(%rip), %rdx     # 0x116a2f
movq (%rax), %rbx
movq %rbx, %rax
orq %r12, %rax
je 0x93ca0
movb $0x1, %al
testq %rbx, %rbx
je 0x93caa
cmpq $0x0, 0x20(%rbx)
jle 0x93caa
movl 0x8(%rbx), %ecx
cmpl $0x3, %ecx
ja 0x93ca8
shll $0x3, %ecx
movl $0x1010001, %eax        # imm = 0x1010001
shrl %cl, %eax
jmp 0x93caa
xorl %r13d, %r13d
jmp 0x93e4b
xorl %eax, %eax
testq %r12, %r12
je 0x93cc8
cmpq $0x0, 0x20(%r12)
jle 0x93cc8
movl 0x8(%r12), %ecx
cmpl $0x3, %ecx
ja 0x93cc6
cmpl $0x1, %ecx
jne 0x93cc8
xorl %eax, %eax
testb %al, %al
je 0x93e36
movq %rdx, 0x10(%rsp)
movq %rdi, 0x8(%rsp)
callq 0xfca54
movq %rax, %r13
testb %bpl, %bpl
leaq 0x2c5(%rip), %rax       # 0x93fb1
leaq 0x294(%rip), %rcx       # 0x93f87
cmoveq %rax, %rcx
movq %r14, 0x8(%r13)
movq %rcx, 0x60(%r13)
movb $0x1, %bpl
movb %bpl, 0x78(%r13)
movq 0x20(%r13), %rax
movups 0x70(%r14), %xmm0
movups %xmm0, 0x18(%rax)
movq 0x20(%r13), %rax
movups 0x60(%r14), %xmm0
movups %xmm0, 0x28(%rax)
movq 0x68(%r13), %rdi
movq 0x98(%r14), %rsi
callq 0xf6969
orb %bpl, 0x70(%r13)
movb %bpl, 0x79(%r13)
testq %rbx, %rbx
je 0x93d62
movb $0x1, 0x10(%r13)
movq 0x20(%rbx), %rax
movq %rax, 0x18(%r13)
leaq 0x5dd00(%rip), %rax     # 0xf1a51
movq %rax, 0x30(%r13)
movq 0x68(%r15), %rdi
movq %rdi, 0x28(%r13)
callq 0xebdc3
testq %r12, %r12
je 0x93d8d
movb $0x1, 0x38(%r13)
movq 0x20(%r12), %rax
movq %rax, 0x40(%r13)
leaq 0x5dcd5(%rip), %rax     # 0xf1a51
movq %rax, 0x58(%r13)
movq 0x60(%r15), %rdi
movq %rdi, 0x50(%r13)
callq 0xebdc3
movq %r13, %rdi
callq 0xfcb73
testq %rbx, %rbx
je 0x93dec
cmpb $0x0, 0x48(%rbx)
je 0x93dec
cmpb $0x0, 0x49(%rbx)
je 0x93dec
movq 0x68(%r15), %rdi
callq 0xebe2a
movl %eax, %ebp
leaq 0x9b851(%rip), %rsi     # 0x12f609
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rdx
movl %eax, %ecx
xorl %eax, %eax
callq 0xe9226
cmpl 0x10(%rbx), %ebp
je 0x93dec
movl 0x8(%r14), %edx
leaq 0x9b84a(%rip), %rsi     # 0x12f625
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rcx
xorl %eax, %eax
callq 0xe9df7
testq %r12, %r12
je 0x93e4b
cmpb $0x0, 0x48(%r12)
je 0x93e4b
cmpb $0x0, 0x49(%r12)
je 0x93e4b
movq 0x60(%r15), %rdi
callq 0xebe2a
movl %eax, %ebp
leaq 0x9b835(%rip), %rsi     # 0x12f648
movq 0x8(%rsp), %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
cmpl 0x10(%r12), %ebp
je 0x93e4b
leaq 0x9b83c(%rip), %rsi     # 0x12f66b
movq 0x8(%rsp), %rdi
jmp 0x93e40
leaq 0x9b79f(%rip), %rsi     # 0x12f5dc
xorl %r13d, %r13d
movl 0x8(%r14), %edx
xorl %eax, %eax
callq 0xe9df7
movq %r13, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xfca9a