// File: /workspace/github_c_makefile_star2_O2/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 0xd625d(%rip), %rsi     # 0x10aed7
xorl %r12d, %r12d
xorl %eax, %eax
callq 0xde896
pushq $0x10
popq %rsi
movq %rbx, %rdi
callq 0xdecaf
movq %rax, %r14
movq 0x8(%r15), %rdi
testq %rdi, %rdi
je 0x34ca3
callq 0xdfb3b
movq %rax, %r12
xorl %eax, %eax
testq %r12, %r12
cmovleq %rax, %r12
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xe59cd
movq %rax, 0x8(%r14)
movq 0x10(%r15), %rsi
movq %rsi, (%r14)
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %r13
subq %rsi, %r13
jle 0x34d3f
callq 0xe3fa3
cmpl $0xea60, %eax           # imm = 0xEA60
jne 0x34d38
movq 0x18(%rbx), %rdi
movq (%r14), %rsi
addq $0x2, %rsi
callq 0xe3fa3
movq %rax, 0x10(%rsp)
addq $-0xa29, %rax           # imm = 0xF5D7
cmpq $-0xa0b, %rax           # imm = 0xF5F5
jb 0x34d38
movq 0x18(%rbx), %rdi
movq (%r14), %rsi
addq $0x8, %rsi
callq 0xe3eb4
movl %eax, %ebp
movq 0x18(%rbx), %rdi
movq (%r14), %rsi
addq $0xa, %rsi
callq 0xe3eb4
cmpb $0x2, %al
jne 0x34d38
testb $0x40, %bpl
jne 0x34d7c
testb $0x8, %bpl
jne 0x34d8f
xorl %ebp, %ebp
jmp 0x34e81
xorl %edi, %edi
callq 0xe0e8d
leaq 0xd61bb(%rip), %rsi     # 0x10af01
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
movq %rbp, %rdi
callq 0xe5591
movq 0x8(%r14), %rdi
callq 0xe5591
movq %rbx, %rdi
movq %r14, %rsi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xded32
movb %bpl, 0xe(%rsp)
movb $-0x40, 0xf(%rsp)
leaq 0xd619a(%rip), %rsi     # 0x10af27
jmp 0x34da0
movb %bpl, 0xe(%rsp)
movb $-0x8, 0xf(%rsp)
leaq 0xd61ad(%rip), %rsi     # 0x10af4d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf37f
movb 0xf(%rsp), %bpl
addb 0xe(%rsp), %bpl
movq 0x18(%rbx), %rdi
movq (%r14), %rsi
movq 0x8(%r14), %rcx
pushq $0x8
popq %rdx
callq 0xe45f4
movq 0x8(%r14), %rdi
movzbl %bpl, %esi
callq 0xe5bc5
movq 0x8(%r14), %rdi
xorl %esi, %esi
callq 0xe5bc5
movq 0x18(%rbx), %rdi
movq (%r14), %rsi
movq 0x8(%r14), %rcx
addq $0xa, %rsi
pushq $0xa
popq %rdx
callq 0xe45f4
movq 0x8(%r14), %rdi
xorl %esi, %esi
callq 0xe5f7f
movq 0x8(%r14), %rdi
xorl %esi, %esi
callq 0xe5f7f
movq 0x18(%rbx), %rdi
movq (%r14), %rsi
movq 0x8(%r14), %rcx
addq $0x1c, %rsi
pushq $0x2
popq %rdx
callq 0xe45f4
movq 0x8(%r14), %rdi
xorl %esi, %esi
callq 0xe5f08
movq 0x18(%rbx), %rdi
movq (%r14), %rsi
movq 0x8(%r14), %rcx
addq $0x20, %rsi
movq 0x10(%rsp), %rdx
addq $-0x1c, %rdx
callq 0xe45f4
pushq $0x10
popq %rsi
movq %rbx, %rdi
callq 0xe0ae9
movq %rax, %rbp
movq 0x8(%r14), %rsi
pushq $0x4
popq %rdx
movq %rax, %rdi
movq 0x10(%rsp), %rcx
callq 0xe0f33
movq %rbp, %rdi
callq 0xe0e95
movq 0x8(%r14), %rdi
movslq %eax, %rsi
callq 0xe5f7f
movq %rbp, %rdi
callq 0xe0e8d
movq (%r14), %rdx
leaq 0xd6058(%rip), %rsi     # 0x10aeeb
movq %rbx, %rdi
movq %r12, %rcx
xorl %eax, %eax
callq 0xde896
leaq 0xd5fb0(%rip), %rsi     # 0x10ae57
movq %rbx, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xe4e62
movq %rax, %rbp
movq %rax, %rdi
movq %r12, %rsi
callq 0xe5d20
movq 0x8(%r14), %rdi
movq 0x18(%rdi), %rdx
xorl %esi, %esi
movq %rbp, %rcx
callq 0xe45f4
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 0xe45f4
movq (%r15), %rax
testq %rax, %rax
je 0x34d52
orb $0x1, 0x40(%rax)
jmp 0x34d52