// File: /workspace/github_c_makefile_star2_O0/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 %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movl $0x20, %esi
callq 0x15b780
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
callq 0xd0a90
movl %eax, %ecx
movq -0x40(%rbp), %rax
movl %ecx, (%rax)
movq -0x40(%rbp), %rax
cmpl $0x0, (%rax)
jne 0xcc78b
movq -0x8(%rbp), %rdi
leaq 0xedb74(%rip), %rsi     # 0x1ba2f3
movb $0x0, %al
callq 0x15bfb0
jmp 0xcc983
movq -0x40(%rbp), %rax
cmpl $0x2, (%rax)
jne 0xcc7eb
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rsi
subq $0x8, %rsi
callq 0x165730
movq %rax, %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x40(%rbp), %rdi
addq $0x8, %rdi
callq 0x15f830
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rcx
subq $0xc, %rcx
movq -0x40(%rbp), %rax
subq 0x8(%rax), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x10(%rax)
jmp 0xcc833
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rsi
subq $0x4, %rsi
callq 0x165470
movq %rax, %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rcx
subq $0x8, %rcx
movq -0x40(%rbp), %rax
subq 0x8(%rax), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rdx
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rcx
leaq 0xedab7(%rip), %rsi     # 0x1ba305
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0xd0b20
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rsi
addq $0x8000, %rsi           # imm = 0x8000
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rdx
addq $0x8000, %rdx           # imm = 0x8000
addq $0x800, %rdx            # imm = 0x800
callq 0xd04e0
cmpl $0x0, %eax
je 0xcc909
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rdx
leaq 0xeda6d(%rip), %rsi     # 0x1ba325
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rcx
movq -0x48(%rbp), %r8
movq -0x40(%rbp), %rax
movq 0x8(%rax), %r9
subq -0x48(%rbp), %r9
leaq 0xecc7e(%rip), %rsi     # 0x1b956e
xorl %eax, %eax
movl %eax, %edx
callq 0x15c9f0
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
jmp 0xcc983
leaq -0x38(%rbp), %rdi
callq 0xd0620
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0xd0670
cmpl $0x0, -0x38(%rbp)
je 0xcc981
movq -0x8(%rbp), %rdi
leaq 0xeda07(%rip), %rsi     # 0x1ba33b
xorl %edx, %edx
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rcx
movq -0x40(%rbp), %rax
movq 0x8(%rax), %r9
leaq 0xecc28(%rip), %rsi     # 0x1b958e
xorl %eax, %eax
movl %eax, %r8d
movq %r8, %rdx
callq 0x15c9f0
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
jmp 0xcc983
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x15b820
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)