// File: /workspace/github_c_makefile_star2_O1/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 0xe958c
movb $0x1, (%r14)
movq 0x8(%r14), %rdx
leaq 0xdebb1(%rip), %rsi     # 0x118e95
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x18(%r14), %rdx
leaq 0xdeb9b(%rip), %rsi     # 0x118ea1
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef169
movq %rax, %r13
movq 0x18(%rbx), %rdi
leaq 0x2(%r15), %rsi
callq 0xef169
movq %rax, 0x20(%r14)
leaq 0xdeb78(%rip), %rsi     # 0x118eaf
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
cmpq $0x0, 0x20(%r14)
jle 0x3a3a8
movq 0x18(%rbx), %rdi
leaq 0x4(%r15), %rsi
callq 0xef169
movq %rax, 0x30(%r14)
leaq 0xdeb68(%rip), %rsi     # 0x118ecb
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
leaq 0x6(%r15), %rsi
callq 0xef32a
movq %rax, 0x28(%r14)
leaq 0xdeb5b(%rip), %rsi     # 0x118ee3
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq 0x28(%r14), %rdx
leaq -0x801(%rdx), %rax
cmpq $-0x800, %rax           # imm = 0xF800
jae 0x3a3b5
movw $0x101, 0x8(%r12)       # imm = 0x101
jmp 0x3a523
movq %r12, 0x8(%rsp)
decq %rdx
imulq 0x20(%r14), %rdx
addq 0x30(%r14), %rdx
movq %rdx, 0x10(%r14)
leaq 0xdeb20(%rip), %rsi     # 0x118ef1
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
leaq 0xe(%r15), %rax
leaq 0x10(%rsp), %r12
movq %rax, (%r12)
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xef34e
movq %rax, %rbp
leaq 0xdeb0e(%rip), %rsi     # 0x118f0c
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xef34e
movq %rax, %r12
leaq 0xdeafe(%rip), %rsi     # 0x118f1f
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
cmpb $0x0, 0x1(%r14)
jne 0x3a46f
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rax
cmpq 0x10(%r14), %rax
jle 0x3a46f
movq 0x18(%rbx), %rax
cmpq 0x18(%rax), %r12
jge 0x3a46f
leaq 0xdeadd(%rip), %rsi     # 0x118f32
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x8(%rsp), %rax
movb $0x1, 0xc(%rax)
movq %r12, 0x98(%rax)
movq 0x28(%r14), %rsi
movl $0x18, %edx
movq %rbx, %rdi
callq 0xea259
movq %rax, 0x38(%r14)
addq %r15, %r13
movq %r13, 0x10(%rsp)
leaq 0xdeab3(%rip), %rsi     # 0x118f46
movq %rbx, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
cmpq $0x0, 0x28(%r14)
jle 0x3a523
movq 0x10(%r14), %r15
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x38(%r14), %rax
movq %rbp, (%rax,%r12)
movq 0x18(%rbx), %rdi
leaq 0x10(%rsp), %rsi
callq 0xef193
movq 0x38(%r14), %rdx
movq %rax, 0x8(%rdx,%r12)
movq 0x20(%r14), %rcx
movq %rcx, 0x10(%rdx,%r12)
cmpq %r15, %rcx
jle 0x3a4f0
movq %r15, 0x10(%rdx,%r12)
addq %rax, %rbp
movq (%rdx,%r12), %rcx
movq 0x10(%rdx,%r12), %r9
subq %r9, %r15
movq %rbx, %rdi
leaq 0xdea52(%rip), %rsi     # 0x118f5b
movq %r13, %rdx
movq %rax, %r8
xorl %eax, %eax
callq 0xe9226
incq %r13
addq $0x18, %r12
cmpq 0x28(%r14), %r13
jl 0x3a4be
movl 0x4(%rsp), %esi
movq %rbx, %rdi
callq 0xe9595
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq