// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/arj.c
// Function: do_run_arj_relocator
static void do_run_arj_relocator(deark *c, struct options_struct *arj_opts)
{
	dbuf *outf = NULL;
	i64 dst_startpos = 0;
	i64 archive_size;
	struct arjreloc_ctx *d = NULL;
	int ok = 0;

	de_dbg(c, "ARJ relocation mode");
	d = de_malloc(c, sizeof(struct arjreloc_ctx));

	if(arj_opts->reloc_opt) {
		dst_startpos = de_atoi64(arj_opts->reloc_opt);
	}
	if(dst_startpos<0) dst_startpos = 0;

	d->ahdr = dbuf_create_membuf(c, 0, 0);

	d->src_startpos = arj_opts->archive_start;
	archive_size = c->infile->len - d->src_startpos;
	if(archive_size<1) goto done;

	if(!reloc_process_archive_hdr(c, d)) goto done;

	de_dbg(c, "reloc from %"I64_FMT" to %"I64_FMT, d->src_startpos, dst_startpos);
	outf = dbuf_create_output_file(c, "arj", NULL, 0);
	dbuf_write_zeroes(outf, dst_startpos);

	// Copy the potentially-changed part of the file
	dbuf_copy(d->ahdr, 0, d->ahdr->len, outf);
	// Copy the definitely-unchanged part of the file
	dbuf_copy(c->infile, d->src_startpos+d->ahdr->len, archive_size-d->ahdr->len, outf);
	ok = 1;

done:
	if(ok) {
		if(arj_opts->mparams) {
			// Inform the caller of success
			arj_opts->mparams->out_params.flags |= 0x1;
		}
	}
	else {
		de_err(c, "Cannot relocate/extract this ARJ file");
	}
	dbuf_close(outf);
	if(d) {
		dbuf_close(d->ahdr);
		de_free(c, d);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r15
movq %rdi, %rbx
leaq 0xe1e3f(%rip), %rsi     # 0x117fc7
xorl %r12d, %r12d
xorl %eax, %eax
callq 0xe8617
movl $0x10, %esi
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, %r14
movq 0x8(%r15), %rdi
testq %rdi, %rdi
je 0x361b3
callq 0xe9c2f
movq %rax, %r12
xorl %eax, %eax
testq %r12, %r12
cmovleq %rax, %r12
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xf09c5
movq %rax, 0x8(%r14)
movq 0x10(%r15), %rsi
movq %rsi, (%r14)
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %r13
subq %rsi, %r13
jle 0x3624f
callq 0xeebc8
cmpl $0xea60, %eax           # imm = 0xEA60
jne 0x36248
movq 0x18(%rbx), %rdi
movq (%r14), %rsi
addq $0x2, %rsi
callq 0xeebc8
movq %rax, 0x10(%rsp)
addq $-0xa29, %rax           # imm = 0xF5D7
cmpq $-0xa0b, %rax           # imm = 0xF5F5
jb 0x36248
movq 0x18(%rbx), %rdi
movq (%r14), %rsi
addq $0x8, %rsi
callq 0xeead3
movl %eax, %ebp
movq 0x18(%rbx), %rdi
movq (%r14), %rsi
addq $0xa, %rsi
callq 0xeead3
cmpb $0x2, %al
jne 0x36248
testb $0x40, %bpl
jne 0x3628c
testb $0x8, %bpl
jne 0x3629f
xorl %ebp, %ebp
jmp 0x3639b
xorl %edi, %edi
callq 0xeb858
leaq 0xe1d9b(%rip), %rsi     # 0x117ff1
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
movq %rbp, %rdi
callq 0xf052b
movq 0x8(%r14), %rdi
callq 0xf052b
movq %rbx, %rdi
movq %r14, %rsi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe8be2
movb %bpl, 0xe(%rsp)
movb $-0x40, 0xf(%rsp)
leaq 0xe1d7a(%rip), %rsi     # 0x118017
jmp 0x362b0
movb %bpl, 0xe(%rsp)
movb $-0x8, 0xf(%rsp)
leaq 0xe1d8d(%rip), %rsi     # 0x11803d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9393
movb 0xf(%rsp), %bpl
addb 0xe(%rsp), %bpl
movq 0x18(%rbx), %rdi
movq (%r14), %rsi
movq 0x8(%r14), %rcx
movl $0x8, %edx
callq 0xef481
movq 0x8(%r14), %rdi
movzbl %bpl, %esi
callq 0xf0bd6
movq 0x8(%r14), %rdi
xorl %esi, %esi
callq 0xf0bd6
movq 0x18(%rbx), %rdi
movq (%r14), %rsi
movq 0x8(%r14), %rcx
addq $0xa, %rsi
movl $0xa, %edx
callq 0xef481
movq 0x8(%r14), %rdi
xorl %esi, %esi
callq 0xf1042
movq 0x8(%r14), %rdi
xorl %esi, %esi
callq 0xf1042
movq 0x18(%rbx), %rdi
movq (%r14), %rsi
movq 0x8(%r14), %rcx
addq $0x1c, %rsi
movl $0x2, %edx
callq 0xef481
movq 0x8(%r14), %rdi
xorl %esi, %esi
callq 0xf0f95
movq 0x18(%rbx), %rdi
movq (%r14), %rsi
movq 0x8(%r14), %rcx
addq $0x20, %rsi
movq 0x10(%rsp), %rdx
addq $-0x1c, %rdx
callq 0xef481
movq %rbx, %rdi
movl $0x10, %esi
callq 0xeb499
movq %rax, %rbp
movq 0x8(%r14), %rsi
movl $0x4, %edx
movq %rax, %rdi
movq 0x10(%rsp), %rcx
callq 0xeb944
movq %rbp, %rdi
callq 0xeb865
movq 0x8(%r14), %rdi
movslq %eax, %rsi
callq 0xf1042
movq %rbp, %rdi
callq 0xeb858
movq (%r14), %rdx
leaq 0xe1c2e(%rip), %rsi     # 0x117fdb
movq %rbx, %rdi
movq %r12, %rcx
xorl %eax, %eax
callq 0xe8617
leaq 0xe1b86(%rip), %rsi     # 0x117f47
movq %rbx, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xefda1
movq %rax, %rbp
movq %rax, %rdi
movq %r12, %rsi
callq 0xf0d35
movq 0x8(%r14), %rdi
movq 0x18(%rdi), %rdx
xorl %esi, %esi
movq %rbp, %rcx
callq 0xef481
movq 0x18(%rbx), %rdi
movq 0x8(%r14), %rax
movq 0x18(%rax), %rax
movq (%r14), %rsi
addq %rax, %rsi
subq %rax, %r13
movq %r13, %rdx
movq %rbp, %rcx
callq 0xef481
movq (%r15), %rax
testq %rax, %rax
je 0x36262
orb $0x1, 0x40(%rax)
jmp 0x36262