// File: /workspace/github_c_makefile_star2_O1/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 %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
movl $0x20, %esi
callq 0xe969b
movq %rax, %r14
movq %rbx, %rdi
callq 0x8e966
movl %eax, (%r14)
cmpl $0x2, %eax
je 0x8c221
testl %eax, %eax
jne 0x8c254
leaq 0xa22cb(%rip), %rsi     # 0x12e4dd
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
jmp 0x8c3d4
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rsi
addq $-0x8, %rsi
callq 0xef4c3
movq %r14, %rdi
addq $0x8, %rdi
movq %rax, 0x8(%r14)
callq 0xec0d1
movq 0x18(%rbx), %rax
movq 0x18(%rax), %rcx
subq 0x8(%r14), %rcx
addq $-0xc, %rcx
jmp 0x8c278
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rsi
addq $-0x4, %rsi
callq 0xef32a
movq %rax, 0x8(%r14)
movq 0x18(%rbx), %rcx
movq 0x18(%rcx), %rcx
subq %rax, %rcx
addq $-0x8, %rcx
movq %rcx, 0x10(%r14)
movq 0x8(%r14), %rdx
leaq 0xa2268(%rip), %rsi     # 0x12e4ef
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
callq 0x107b04
movq %rax, %r15
movq %r14, 0x10(%rax)
leaq 0x2715(%rip), %rax      # 0x8e9bc
movq %rax, 0x28(%r15)
leaq 0x279b(%rip), %rax      # 0x8ea4d
movq %rax, 0x20(%r15)
movq 0x18(%rbx), %rax
movq %rax, 0x18(%r15)
movl $0x0, 0x58(%r15)
movb $0x0, 0x5c(%r15)
movq 0x8(%r14), %rsi
movq 0x10(%r14), %rdx
movq %r15, %rdi
callq 0x107b28
movq %r15, %rdi
callq 0x107b17
movq 0x18(%r14), %r15
movq 0x18(%rbx), %r12
leaq 0x8000(%r15), %rsi
movq %r12, %rdi
callq 0x8e774
testl %eax, %eax
je 0x8c34f
leaq 0x8800(%r15), %rsi
movq %r12, %rdi
callq 0x8e774
testl %eax, %eax
je 0x8c34f
movq 0x18(%r14), %rdx
leaq 0xa21f3(%rip), %rsi     # 0x12e50f
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x18(%rbx), %rcx
movq 0x8(%r14), %r9
subq %r15, %r9
leaq 0xa1413(%rip), %rsi     # 0x12d758
movq %rbx, %rdi
xorl %edx, %edx
movq %r15, %r8
jmp 0x8c3c2
movq %rsp, %r15
movl $0x0, (%r15)
movl $0x800, %eax            # imm = 0x800
movq %rax, 0x8(%r15)
movq %rax, 0x10(%r15)
movq $0x0, 0x18(%r15)
leaq 0xa90bc(%rip), %rax     # 0x135431
movq %rax, 0x20(%r15)
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0x8e7e7
cmpl $0x0, (%r15)
je 0x8c3d4
leaq 0xa2193(%rip), %rsi     # 0x12e525
movq %rbx, %rdi
xorl %edx, %edx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x18(%rbx), %rcx
movq 0x8(%r14), %r9
leaq 0xa13be(%rip), %rsi     # 0x12d778
movq %rbx, %rdi
xorl %edx, %edx
xorl %r8d, %r8d
callq 0xea5fa
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
movq %rbx, %rdi
movq %r14, %rsi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
jmp 0xe9752