// File: /workspace/github_c_makefile_star2_O2/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, %rdx
movq %rdi, %rbx
testl %ecx, %ecx
je 0x8de6d
leaq 0xc0(%r14), %rax
leaq 0x873d2(%rip), %rdi     # 0x11523a
xorl %r13d, %r13d
jmp 0x8de82
leaq 0xb0(%r14), %rax
movq 0xb8(%r14), %r13
leaq 0x7bbcd(%rip), %rdi     # 0x109a4f
movq (%rax), %r15
movq %r15, %rax
orq %r13, %rax
je 0x8dead
testq %r15, %r15
je 0x8deb5
cmpq $0x0, 0x20(%r15)
jle 0x8deb5
movl 0x8(%r15), %ecx
movb $0x1, %al
cmpl $0x3, %ecx
ja 0x8deb7
andb $0xf, %cl
movb $0x2, %al
shrb %cl, %al
jmp 0x8deb7
xorl %r12d, %r12d
jmp 0x8e074
xorl %eax, %eax
testq %r13, %r13
je 0x8dedb
cmpq $0x0, 0x20(%r13)
jle 0x8dedb
movl 0x8(%r13), %ecx
leaq 0x94769(%rip), %rsi     # 0x122637
xorl %r12d, %r12d
cmpl $0x3, %ecx
ja 0x8dee9
cmpl $0x1, %ecx
je 0x8dee9
testb $0x1, %al
je 0x8defc
leaq 0x94751(%rip), %rsi     # 0x122637
xorl %r12d, %r12d
movl 0x8(%r14), %edx
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
jmp 0x8e074
movq %rdi, 0x10(%rsp)
movq %rdx, 0x8(%rsp)
movq %rbx, %rdi
callq 0xf07cc
movq %rax, %r12
testb %bpl, %bpl
leaq 0x29f(%rip), %rax       # 0x8e1ba
leaq 0x270(%rip), %rcx       # 0x8e192
cmoveq %rax, %rcx
movq %r14, 0x8(%r12)
movq %rcx, 0x60(%r12)
movb $0x1, %bpl
movb %bpl, 0x78(%r12)
movq 0x20(%r12), %rax
movups 0x70(%r14), %xmm0
movups %xmm0, 0x18(%rax)
movq 0x20(%r12), %rax
movups 0x60(%r14), %xmm0
movups %xmm0, 0x28(%rax)
movq 0x68(%r12), %rdi
movq 0x98(%r14), %rsi
callq 0xead71
orb %bpl, 0x70(%r12)
movb %bpl, 0x79(%r12)
testq %r15, %r15
je 0x8dfa2
movb $0x1, 0x10(%r12)
movq 0x20(%r15), %rax
movq %rax, 0x18(%r12)
leaq 0x58337(%rip), %rax     # 0xe62c1
movq %rax, 0x30(%r12)
movq 0x8(%rsp), %rax
movq 0x68(%rax), %rdi
movq %rdi, 0x28(%r12)
callq 0xe0e44
testq %r13, %r13
je 0x8dfd5
movb $0x1, 0x38(%r12)
movq 0x20(%r13), %rax
movq %rax, 0x40(%r12)
leaq 0x58304(%rip), %rax     # 0xe62c1
movq %rax, 0x58(%r12)
movq 0x8(%rsp), %rax
movq 0x60(%rax), %rdi
movq %rdi, 0x50(%r12)
callq 0xe0e44
movq %r12, %rdi
callq 0xf08e8
testq %r15, %r15
je 0x8e038
cmpb $0x0, 0x48(%r15)
je 0x8e038
cmpb $0x0, 0x49(%r15)
je 0x8e038
movq 0x8(%rsp), %rax
movq 0x68(%rax), %rdi
callq 0xe0e95
movl %eax, %ebp
leaq 0x9465d(%rip), %rsi     # 0x122664
movq %rbx, %rdi
movq 0x10(%rsp), %rdx
movl %eax, %ecx
xorl %eax, %eax
callq 0xde896
cmpl 0x10(%r15), %ebp
je 0x8e038
movl 0x8(%r14), %edx
leaq 0x94657(%rip), %rsi     # 0x122680
movq %rbx, %rdi
movq 0x10(%rsp), %rcx
xorl %eax, %eax
callq 0xdf2a5
testq %r13, %r13
je 0x8e074
cmpb $0x0, 0x48(%r13)
je 0x8e074
cmpb $0x0, 0x49(%r13)
je 0x8e074
movq 0x8(%rsp), %rax
movq 0x60(%rax), %rdi
callq 0xe0e95
movl %eax, %ebp
leaq 0x94641(%rip), %rsi     # 0x1226a3
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
cmpl 0x10(%r13), %ebp
jne 0x8e08a
movq %r12, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xf0812
leaq 0x94635(%rip), %rsi     # 0x1226c6
jmp 0x8dee9