// File: /workspace/github_c_makefile_star2_O3/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 0xe8b2b
movq %rax, %r14
movq %rbx, %rdi
callq 0x8e069
movl %eax, (%r14)
cmpl $0x2, %eax
je 0x8ba4e
testl %eax, %eax
jne 0x8ba84
leaq 0xa2d96(%rip), %rsi     # 0x12e7d5
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
jmp 0x8bbe3
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rsi
addq $-0x8, %rsi
callq 0xeee96
movq %r14, %rdi
addq $0x8, %rdi
movq %rax, 0x8(%r14)
callq 0xebad2
movq 0x18(%rbx), %rax
movq 0x18(%rax), %rcx
movq 0x8(%r14), %rdx
subq %rdx, %rcx
addq $-0xc, %rcx
jmp 0x8baab
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rsi
addq $-0x4, %rsi
callq 0xeed0d
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 0xa2d31(%rip), %rsi     # 0x12e7e7
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
callq 0x1075c0
movq %rax, %r15
movq %r14, 0x10(%rax)
leaq 0x25e9(%rip), %rax      # 0x8e0bf
movq %rax, 0x28(%r15)
leaq 0x2665(%rip), %rax      # 0x8e146
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 0x1075e4
movq %r15, %rdi
callq 0x1075d3
movq 0x18(%r14), %r15
movq 0x18(%rbx), %r12
leaq 0x8000(%r15), %rsi
movq %r12, %rdi
callq 0x8de77
testl %eax, %eax
je 0x8bb7e
leaq 0x8800(%r15), %rsi
movq %r12, %rdi
callq 0x8de77
testl %eax, %eax
je 0x8bb7e
movq 0x18(%r14), %rdx
leaq 0xa2cbc(%rip), %rsi     # 0x12e807
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%rbx), %rcx
movq 0x8(%r14), %r9
subq %r15, %r9
leaq 0xa1edc(%rip), %rsi     # 0x12da50
movq %rbx, %rdi
xorl %edx, %edx
movq %r15, %r8
jmp 0x8bbd1
movq %rsp, %r15
movl $0x0, (%r15)
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0x8deea
cmpl $0x0, (%r15)
je 0x8bbe3
leaq 0xa2c7c(%rip), %rsi     # 0x12e81d
movq %rbx, %rdi
xorl %edx, %edx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%rbx), %rcx
movq 0x8(%r14), %r9
leaq 0xa1ea7(%rip), %rsi     # 0x12da70
movq %rbx, %rdi
xorl %edx, %edx
xorl %r8d, %r8d
callq 0xe9aa3
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movq %rbx, %rdi
movq %r14, %rsi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
jmp 0xe8be2