// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/aldus.c
// Function: aldus_read_fork
static void aldus_read_fork(deark *c, lctx *d, struct fork_data *fkd)
{
	i64 cur_cmpr_pos;
	i64 num_orig_bytes_remaining;
	i64 pos1 = fkd->pos;
	i64 pos;
	i64 i;
	i64 offset_to_seg_table;
	i64 data_area_pos, next_fork_pos;
	int saved_indent_level;

	de_dbg_indent_save(c, &saved_indent_level);
	fkd->fork_exists = 1;
	de_dbg(c, "fork at %"I64_FMT, fkd->pos);
	de_dbg_indent(c, 1);
	de_dbg(c, "fork type: %s", fkd->forkname);

	offset_to_seg_table = de_getu16be(pos1);

	fkd->nominal_seg_size = de_getu16be(pos1+2);
	de_dbg(c, "nominal orig bytes/seg: %"I64_FMT, fkd->nominal_seg_size);
	if(fkd->nominal_seg_size<1) {
		d->errflag = 1;
		d->need_errmsg = 1;
		goto done;
	}

	fkd->orig_len_of_last_seg = de_getu16be(pos1+4);
	de_dbg(c, "last seg orig size: %"I64_FMT, fkd->orig_len_of_last_seg);

	fkd->nsegs = de_getu32be(pos1+6);
	de_dbg(c, "num segs: %"I64_FMT, fkd->nsegs);

	if(fkd->nsegs<1 || fkd->nsegs>ALDUS_MAX_SEGS) {
		d->errflag = 1;
		d->need_errmsg = 1;
		goto done;
	}

	fkd->orig_len = fkd->nominal_seg_size*(fkd->nsegs-1) + fkd->orig_len_of_last_seg;
	de_dbg(c, "orig len (calculated): %"I64_FMT, fkd->orig_len);

	pos = pos1+14;
	data_area_pos = de_getu32be_p(&pos);
	de_dbg(c, "data area pos: %"I64_FMT, data_area_pos);
	next_fork_pos = de_getu32be_p(&pos);
	de_dbg(c, "next fork pos: %"I64_FMT, next_fork_pos);
	if((!fkd->is_rsrc_fork) && (d->total_orig_len > fkd->orig_len) &&
		(next_fork_pos < c->infile->len))
	{
		de_dbg(c, "[has resource fork]");
		d->found_rsrc_fork = 1;
		d->rfork.pos = next_fork_pos;
	}

	fkd->sdata = de_mallocarray(c, fkd->nsegs, sizeof(struct seg_data));

	pos = pos1+offset_to_seg_table;
	de_dbg(c, "segment table at %"I64_FMT, pos);
	de_dbg_indent(c, 1);
	cur_cmpr_pos = data_area_pos;
	num_orig_bytes_remaining = fkd->orig_len;
	for(i=0; i<fkd->nsegs; i++) {
		fkd->sdata[i].cmpr_pos = cur_cmpr_pos;
		// Note that cmpr_len may be padded (with garbage?) to always be an
		// even number.
		// (Not a problem, since both compression schemes are self-terminating.)
		fkd->sdata[i].cmpr_len = de_getu16be_p(&pos);
		cur_cmpr_pos += fkd->sdata[i].cmpr_len;

		fkd->sdata[i].orig_len = fkd->nominal_seg_size;
		if(fkd->sdata[i].orig_len > num_orig_bytes_remaining) {
			fkd->sdata[i].orig_len = num_orig_bytes_remaining;
		}
		num_orig_bytes_remaining -= fkd->sdata[i].orig_len;

		de_dbg(c, "seg[%"I64_FMT"]: pos=%"I64_FMT", c_len=%"I64_FMT
			", u_len=%"I64_FMT, i,fkd->sdata[i].cmpr_pos,
			fkd->sdata[i].cmpr_len, fkd->sdata[i].orig_len);
	}

done:
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, %r14
movq %rsi, %r12
movq %rdi, %rbx
movq 0x8(%rdx), %r15
leaq 0x4(%rsp), %rsi
callq 0xdebae
movb $0x1, (%r14)
movq 0x8(%r14), %rdx
leaq 0xd3417(%rip), %rsi     # 0x10bec5
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq 0x18(%r14), %rdx
leaq 0xd3403(%rip), %rsi     # 0x10bed1
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xe3f46
movq %rax, %r13
movq 0x18(%rbx), %rdi
leaq 0x2(%r15), %rsi
callq 0xe3f46
movq %rax, 0x20(%r14)
leaq 0xd33e0(%rip), %rsi     # 0x10bedf
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
cmpq $0x0, 0x20(%r14)
jle 0x38b70
movq 0x18(%rbx), %rdi
leaq 0x4(%r15), %rsi
callq 0xe3f46
movq %rax, 0x30(%r14)
leaq 0xd33d0(%rip), %rsi     # 0x10befb
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
leaq 0x6(%r15), %rsi
callq 0xe404f
movq %rax, 0x28(%r14)
leaq 0xd33c3(%rip), %rsi     # 0x10bf13
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movq 0x28(%r14), %rdx
leaq -0x801(%rdx), %rax
cmpq $-0x800, %rax           # imm = 0xF800
jae 0x38b93
movw $0x101, 0x8(%r12)       # imm = 0x101
movl 0x4(%rsp), %esi
movq %rbx, %rdi
callq 0xdebb7
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, 0x8(%rsp)
decq %rdx
imulq 0x20(%r14), %rdx
addq 0x30(%r14), %rdx
movq %rdx, 0x10(%r14)
leaq 0xd3372(%rip), %rsi     # 0x10bf21
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
leaq 0xe(%r15), %rax
leaq 0x10(%rsp), %r12
movq %rax, (%r12)
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xe4071
movq %rax, %rbp
leaq 0xd3360(%rip), %rsi     # 0x10bf3c
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xe4071
movq %rax, %r12
leaq 0xd3350(%rip), %rsi     # 0x10bf4f
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
cmpb $0x0, 0x1(%r14)
jne 0x38c4d
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rax
cmpq 0x10(%r14), %rax
jle 0x38c4d
movq 0x18(%rbx), %rax
cmpq 0x18(%rax), %r12
jge 0x38c4d
leaq 0xd332f(%rip), %rsi     # 0x10bf62
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x8(%rsp), %rax
movb $0x1, 0xc(%rax)
movq %r12, 0x98(%rax)
movq 0x28(%r14), %rsi
pushq $0x18
popq %rdx
movq %rbx, %rdi
callq 0xdf66f
movq %rax, 0x38(%r14)
addq %r15, %r13
leaq 0x10(%rsp), %rax
movq %r13, (%rax)
leaq 0xd3304(%rip), %rsi     # 0x10bf76
movq %rbx, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq 0x10(%r14), %r15
xorl %r12d, %r12d
xorl %r13d, %r13d
cmpq 0x28(%r14), %r13
jge 0x38b78
movq 0x38(%r14), %rax
movq %rbp, (%rax,%r12)
movq 0x18(%rbx), %rdi
leaq 0x10(%rsp), %rsi
callq 0xe3f6e
movq 0x38(%r14), %rcx
movq %rax, 0x8(%rcx,%r12)
movq 0x20(%r14), %r9
movq %r9, 0x10(%rcx,%r12)
cmpq %r15, %r9
jle 0x38cd3
movq %r15, 0x10(%rcx,%r12)
movq %r15, %r9
addq %rax, %rbp
subq %r9, %r15
movq (%rcx,%r12), %rcx
movq %rbx, %rdi
leaq 0xd32a4(%rip), %rsi     # 0x10bf8b
movq %r13, %rdx
movq %rax, %r8
xorl %eax, %eax
callq 0xde896
incq %r13
addq $0x18, %r12
jmp 0x38c94