// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/iso9660.c
// Function: de_run_nrg
static void de_run_nrg(deark *c, de_module_params *mparams)
{
	struct cdraw_params cdrp;
	struct nrg_ctx *nrg = NULL;
	i64 startpos;

	nrg = de_malloc(c, sizeof(struct nrg_ctx));

	nrg->ver = detect_nrg_internal(c);
	if(nrg->ver==0) {
		de_err(c, "Not in NRG format");
		goto done;
	}

	if(nrg->ver==2) {
		nrg->chunk_list_start = de_geti64be(c->infile->len-8);
		de_sanitize_offset(&nrg->chunk_list_start);
		nrg->chunk_list_size = c->infile->len - 12 - nrg->chunk_list_start;
	}
	else {
		nrg->chunk_list_start = de_getu32be(c->infile->len-4);
		nrg->chunk_list_size = c->infile->len - 8 - nrg->chunk_list_start;
	}
	de_dbg(c, "chunk list: offset=%"I64_FMT", len=%"I64_FMT,
		nrg->chunk_list_start, nrg->chunk_list_size);

	do_nrg_chunks(c, nrg);

	// TODO: The NRG data we just read probably tells us the image format,
	// somehow, so it seems wrong to autodetect it.

	startpos = nrg->pre_gap;
	if(cdsig_at2(c->infile, nrg->pre_gap+32768, nrg->pre_gap+32768+2048)) {
		de_dbg(c, "ISO 9660 image at %"I64_FMT, nrg->pre_gap);
		de_dbg_indent(c, 1);
		de_run_module_by_id_on_slice(c, "iso9660", NULL, c->infile, startpos,
			nrg->chunk_list_start-startpos);
		de_dbg_indent(c, -1);
		goto done;
	}

	cdraw_setdefaults(&cdrp);
	cdraw_detect_params(c->infile, &cdrp);
	if(cdrp.ok) {
		de_dbg(c, "raw CD image at %d", 0);
		de_dbg_indent(c, 1);
		de_run_module_by_id_on_slice(c, "cd_raw", NULL, c->infile, 0, nrg->chunk_list_start);
		de_dbg_indent(c, -1);
	}

done:
	de_free(c, nrg);
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
subq $0x30, %rsp
movq %rdi, %rbx
pushq $0x20
popq %rsi
callq 0xdecaf
movq %rax, %r14
movq %rbx, %rdi
callq 0x88dc9
movl %eax, (%r14)
cmpl $0x2, %eax
je 0x86938
testl %eax, %eax
jne 0x8696e
leaq 0x9ac3c(%rip), %rsi     # 0x121565
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
jmp 0x86ab4
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rsi
addq $-0x8, %rsi
callq 0xe4143
movq %r14, %rdi
addq $0x8, %rdi
movq %rax, 0x8(%r14)
callq 0xe10b1
movq 0x18(%rbx), %rax
movq 0x18(%rax), %rcx
movq 0x8(%r14), %rdx
subq %rdx, %rcx
addq $-0xc, %rcx
jmp 0x86995
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rsi
addq $-0x4, %rsi
callq 0xe404f
movq %rax, %rdx
movq %rax, 0x8(%r14)
movq 0x18(%rbx), %rax
movq 0x18(%rax), %rcx
subq %rdx, %rcx
addq $-0x8, %rcx
movq %rcx, 0x10(%r14)
leaq 0x9abd7(%rip), %rsi     # 0x121577
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq %rbx, %rdi
callq 0xfacc0
movq %rax, %r15
movq %r14, 0x10(%rax)
leaq 0x2459(%rip), %rax      # 0x88e19
movq %rax, 0x28(%r15)
leaq 0x24c3(%rip), %rax      # 0x88e8e
movq %rax, 0x20(%r15)
movq 0x18(%rbx), %rax
movq %rax, 0x18(%r15)
andl $0x0, 0x58(%r15)
movb $0x0, 0x5c(%r15)
movq 0x8(%r14), %rsi
movq 0x10(%r14), %rdx
movq %r15, %rdi
callq 0xface4
movq %r15, %rdi
callq 0xfacd3
movq 0x18(%r14), %r15
movq 0x18(%rbx), %rdi
leaq 0x8000(%r15), %rsi
leaq 0x8800(%r15), %rdx
callq 0x88bef
testl %eax, %eax
je 0x86a54
movq 0x18(%r14), %rdx
leaq 0x9ab74(%rip), %rsi     # 0x121597
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq 0x18(%rbx), %rcx
movq 0x8(%r14), %r9
subq %r15, %r9
leaq 0x99d96(%rip), %rsi     # 0x1207e0
movq %rbx, %rdi
xorl %edx, %edx
movq %r15, %r8
jmp 0x86aa4
leaq 0x8(%rsp), %r15
andl $0x0, (%r15)
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0x88c97
cmpl $0x0, (%r15)
je 0x86ab4
leaq 0x9ab37(%rip), %rsi     # 0x1215ad
movq %rbx, %rdi
xorl %edx, %edx
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq 0x18(%rbx), %rcx
movq 0x8(%r14), %r9
leaq 0x99d64(%rip), %rsi     # 0x120800
movq %rbx, %rdi
xorl %edx, %edx
xorl %r8d, %r8d
callq 0xdf9b8
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq %rbx, %rdi
movq %r14, %rsi
addq $0x30, %rsp
popq %rbx
popq %r14
popq %r15
jmp 0xded32