// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/arj.c
// Function: de_run_arj
static void de_run_arj(deark *c, de_module_params *mparams)
{
	lctx *d = NULL;
	u8 archive_start_req_flag = 0;
	u8 used_scan = 0;
	i64 archive_start_req = 0;
	i64 exact_archive_start = 0;
	const char *s;
	u8 scan_opt; // 0 or 1, 0xff for unset
	int modcode_R = 0;
	struct options_struct *arj_opts = NULL;
	struct arjvol_struct *firstvol = NULL;
	struct arjvol_struct *mpvol = NULL;

	arj_opts = de_malloc(c, sizeof(struct options_struct));
	arj_opts->mparams = mparams;

	if(c->module_disposition==DE_MODDISP_INTERNAL) {
		modcode_R = de_havemodcode(c, mparams, 'R');
	}

	if(c->module_disposition!=DE_MODDISP_INTERNAL) {
		scan_opt = (u8)de_get_ext_option_bool(c, "arj:scan", 0xff);
	}
	else {
		scan_opt = 0xff;
	}

	// Starting point for the scan for the archive header
	// (or the exact header pos if scan=0).
	if(c->module_disposition!=DE_MODDISP_INTERNAL) {
		s = de_get_ext_option(c, "arj:entrypoint");
		if(s) {
			archive_start_req_flag = 1;
			archive_start_req = de_atoi64(s);
		}
	}

	// Try various things to find the start of the ARJ archive.
	// TODO?: This is unpleasantly complicated.

	if(archive_start_req_flag && scan_opt==0) {
		exact_archive_start = archive_start_req;
		goto after_archive_start_known;
	}

	if(modcode_R && mparams && mparams->in_params.obj1) {
		struct fmtutil_specialexe_detection_data *edd;

		edd = (struct fmtutil_specialexe_detection_data*)mparams->in_params.obj1;
		if(edd->payload_valid) {
			exact_archive_start = edd->payload_pos;
			goto after_archive_start_known;
		}
	}

	if(c->module_disposition==DE_MODDISP_AUTODETECT) {
		exact_archive_start = 0; // The "identify" routine only looks here
		goto after_archive_start_known;
	}

	if(scan_opt!=0) {
		int ret;
		i64 scan_startpos;
		u8 allow_bad_crc;

		if(archive_start_req_flag) {
			scan_startpos = archive_start_req;
			allow_bad_crc = 1;
		}
		else if(is_exe_format(c)) {
			scan_startpos = get_exe_overlay_pos(c);
			allow_bad_crc = 0;
		}
		else {
			scan_startpos = 0;
			allow_bad_crc = 1;
		}

		if(allow_bad_crc) {
			if(is_arj_data_at(c, scan_startpos)) {
				exact_archive_start = scan_startpos;
				goto after_archive_start_known;
			}
		}

		ret = fmtutil_scan_for_arj_data(c->infile, scan_startpos, c->infile->len-scan_startpos, 0,
			&exact_archive_start);
		if(ret) {
			if(exact_archive_start != archive_start_req) {
				used_scan = 1;
			}
			goto after_archive_start_known;
		}
	}

	exact_archive_start = 0; // Give up

after_archive_start_known:
	arj_opts->archive_start = exact_archive_start;

	if(dbuf_memcmp(c->infile, arj_opts->archive_start, g_arj_hdr_id, 2)) {
		if(!modcode_R) {
			de_err(c, "Not an ARJ file, or could not find ARJ data");
		}
		goto done;
	}

	if(used_scan) {
		de_dbg(c, "ARJ data found at %"I64_FMT, arj_opts->archive_start);
	}

	if(c->module_disposition!=DE_MODDISP_INTERNAL) {
		const char *s;

		s = de_get_ext_option(c, "arj:reloc");
		if(s) {
			arj_opts->reloc_opt = s;
			do_run_arj_relocator(c, arj_opts);
			goto done;
		}
	}

	if(modcode_R) {
		do_run_arj_relocator(c, arj_opts);
		goto done;
	}

	d = de_malloc(c, sizeof(lctx));

	de_declare_fmt(c, "ARJ");
	d->input_encoding = de_get_input_encoding(c, NULL, DE_ENCODING_UNKNOWN);
	if(c->mp_opt_used) {
		if(c->module_disposition!=DE_MODDISP_INTERNAL) {
			// Did the user use "-mp"?
			d->mp_opt_used = 1;
		}
	}

	d->crco = de_crcobj_create(c, DE_CRCOBJ_CRC32_IEEE);

	firstvol = de_malloc(c, sizeof(struct arjvol_struct));
	firstvol->volnum = 0;
	firstvol->inf = c->infile;
	firstvol->archive_start = arj_opts->archive_start;

	do_arj_volume(c, d, firstvol);

	if(c->mp_data) {
		int k;

		for(k=0; k<c->mp_data->count; k++) {
			dbuf *inf_part;

			de_dbg(c, "[mp file %d: %s]", k, c->mp_data->item[k].fn);
			if(mpvol) {
				de_free(c, mpvol);
			}
			mpvol = de_malloc(c, sizeof(struct arjvol_struct));
			mpvol->volnum = k+1;
			mpvol->archive_start = 0;

			inf_part = de_mp_acquire_dbuf(c, k+1);
			if(!inf_part) goto done;  // todo: error?
			mpvol->inf = inf_part;
			do_arj_volume(c, d, mpvol);
			de_mp_release_dbuf(c, k+1, &inf_part);
		}
	}

done:
	if(d) {
		arj_logical_member_finish_and_free(c, d, 1);
		de_crcobj_destroy(d->crco);
		de_free(c, d);
	}
	de_free(c, firstvol);
	de_free(c, mpvol);
	de_free(c, arj_opts);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movb $0x0, -0x19(%rbp)
movb $0x0, -0x1a(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movl $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x8(%rbp), %rdi
movl $0x18, %esi
callq 0x15b780
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rcx
movq -0x48(%rbp), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rax
cmpl $0x3, 0x24(%rax)
jne 0x4744e
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl $0x52, %edx
callq 0x15d1d0
movl %eax, -0x40(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x3, 0x24(%rax)
je 0x47472
movq -0x8(%rbp), %rdi
leaq 0x15c809(%rip), %rsi    # 0x1a3c6c
movl $0xff, %edx
callq 0x15cbc0
movb %al, -0x39(%rbp)
jmp 0x47476
movb $-0x1, -0x39(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x3, 0x24(%rax)
je 0x474ae
movq -0x8(%rbp), %rdi
leaq 0x15c7ea(%rip), %rsi    # 0x1a3c75
callq 0x15cb30
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x474ac
movb $0x1, -0x19(%rbp)
movq -0x38(%rbp), %rdi
callq 0x15ccc0
movq %rax, -0x28(%rbp)
jmp 0x474ae
movzbl -0x19(%rbp), %eax
cmpl $0x0, %eax
je 0x474cd
movzbl -0x39(%rbp), %eax
cmpl $0x0, %eax
jne 0x474cd
movq -0x28(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0x475e2
cmpl $0x0, -0x40(%rbp)
je 0x4750e
cmpq $0x0, -0x10(%rbp)
je 0x4750e
movq -0x10(%rbp), %rax
cmpq $0x0, 0x38(%rax)
je 0x4750e
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
cmpb $0x0, 0x3(%rax)
je 0x4750c
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x30(%rbp)
jmp 0x475e2
jmp 0x4750e
movq -0x8(%rbp), %rax
cmpl $0x1, 0x24(%rax)
jne 0x47525
movq $0x0, -0x30(%rbp)
jmp 0x475e2
movzbl -0x39(%rbp), %eax
cmpl $0x0, %eax
je 0x475da
cmpb $0x0, -0x19(%rbp)
je 0x47546
movq -0x28(%rbp), %rax
movq %rax, -0x70(%rbp)
movb $0x1, -0x71(%rbp)
jmp 0x47574
movq -0x8(%rbp), %rdi
callq 0x479e0
cmpb $0x0, %al
je 0x47566
movq -0x8(%rbp), %rdi
callq 0x47a40
movq %rax, -0x70(%rbp)
movb $0x0, -0x71(%rbp)
jmp 0x47572
movq $0x0, -0x70(%rbp)
movb $0x1, -0x71(%rbp)
jmp 0x47574
cmpb $0x0, -0x71(%rbp)
je 0x47598
movq -0x8(%rbp), %rdi
movq -0x70(%rbp), %rsi
callq 0x47aa0
cmpl $0x0, %eax
je 0x47596
movq -0x70(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0x475e2
jmp 0x47598
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x70(%rbp), %rsi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rdx
subq -0x70(%rbp), %rdx
xorl %ecx, %ecx
leaq -0x30(%rbp), %r8
callq 0x176310
movl %eax, -0x64(%rbp)
cmpl $0x0, -0x64(%rbp)
je 0x475d8
movq -0x30(%rbp), %rax
cmpq -0x28(%rbp), %rax
je 0x475d6
movb $0x1, -0x1a(%rbp)
jmp 0x475e2
jmp 0x475da
movq $0x0, -0x30(%rbp)
movq -0x30(%rbp), %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x48(%rbp), %rax
movq 0x10(%rax), %rsi
movq 0x1ddefb(%rip), %rdx    # 0x225500
movl $0x2, %ecx
callq 0x166ab0
cmpl $0x0, %eax
je 0x47631
cmpl $0x0, -0x40(%rbp)
jne 0x4762c
movq -0x8(%rbp), %rdi
leaq 0x15c65f(%rip), %rsi    # 0x1a3c84
movb $0x0, %al
callq 0x15bfb0
jmp 0x47887
cmpb $0x0, -0x1a(%rbp)
je 0x47651
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rax
movq 0x10(%rax), %rdx
leaq 0x15c666(%rip), %rsi    # 0x1a3cb0
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
cmpl $0x3, 0x24(%rax)
je 0x47696
movq -0x8(%rbp), %rdi
leaq 0x15c660(%rip), %rsi    # 0x1a3cc6
callq 0x15cb30
movq %rax, -0x80(%rbp)
cmpq $0x0, -0x80(%rbp)
je 0x47694
movq -0x80(%rbp), %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x47b60
jmp 0x47887
jmp 0x47696
cmpl $0x0, -0x40(%rbp)
je 0x476ae
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x47b60
jmp 0x47887
movq -0x8(%rbp), %rdi
movl $0x30, %esi
callq 0x15b780
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rdi
leaq 0x15c59d(%rip), %rsi    # 0x1a3c68
callq 0x15e380
movq -0x8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x15e4a0
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x8(%rbp), %rax
cmpb $0x0, 0x7b(%rax)
je 0x47706
movq -0x8(%rbp), %rax
cmpl $0x3, 0x24(%rax)
je 0x47704
movq -0x18(%rbp), %rax
movb $0x1, 0x14(%rax)
jmp 0x47706
movq -0x8(%rbp), %rdi
movl $0x10, %esi
callq 0x15eac0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x8(%rbp), %rdi
movl $0x38, %esi
callq 0x15b780
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movl $0x0, 0x8(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rcx
movq -0x50(%rbp), %rax
movq %rcx, (%rax)
movq -0x48(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x50(%rbp), %rdx
callq 0x47d50
movq -0x8(%rbp), %rax
cmpq $0x0, 0x30(%rax)
je 0x47885
movl $0x0, -0x84(%rbp)
movl -0x84(%rbp), %eax
movq -0x8(%rbp), %rcx
movq 0x30(%rcx), %rcx
cmpl (%rcx), %eax
jge 0x47883
movq -0x8(%rbp), %rdi
movl -0x84(%rbp), %edx
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rax
movq 0x8(%rax), %rax
movslq -0x84(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rcx
leaq 0x15c507(%rip), %rsi    # 0x1a3cd0
movb $0x0, %al
callq 0x15b0d0
cmpq $0x0, -0x58(%rbp)
je 0x477e4
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x15b820
movq -0x8(%rbp), %rdi
movl $0x38, %esi
callq 0x15b780
movq %rax, -0x58(%rbp)
movl -0x84(%rbp), %ecx
addl $0x1, %ecx
movq -0x58(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x58(%rbp), %rax
movq $0x0, 0x10(%rax)
movq -0x8(%rbp), %rdi
movl -0x84(%rbp), %esi
addl $0x1, %esi
callq 0x15f890
movq %rax, -0x90(%rbp)
cmpq $0x0, -0x90(%rbp)
jne 0x47837
jmp 0x47887
movq -0x90(%rbp), %rcx
movq -0x58(%rbp), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x58(%rbp), %rdx
callq 0x47d50
movq -0x8(%rbp), %rdi
movl -0x84(%rbp), %esi
addl $0x1, %esi
leaq -0x90(%rbp), %rdx
callq 0x15f970
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0x47785
jmp 0x47885
jmp 0x47887
cmpq $0x0, -0x18(%rbp)
je 0x478ba
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl $0x1, %edx
callq 0x47e00
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x15f2e0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x15b820
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x15b820
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x15b820
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x15b820
addq $0x90, %rsp
popq %rbp
retq
nopw (%rax,%rax)