// File: /workspace/github_c_makefile_star2_O3/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 0xe8a1c
movb $0x1, (%r14)
movq 0x8(%r14), %rdx
leaq 0xdeddf(%rip), %rsi     # 0x118fb5
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%r14), %rdx
leaq 0xdedc9(%rip), %rsi     # 0x118fc1
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeeb67
movq %rax, %r13
movq 0x18(%rbx), %rdi
leaq 0x2(%r15), %rsi
callq 0xeeb67
movq %rax, 0x20(%r14)
leaq 0xdeda6(%rip), %rsi     # 0x118fcf
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
cmpq $0x0, 0x20(%r14)
jle 0x3a29a
movq 0x18(%rbx), %rdi
leaq 0x4(%r15), %rsi
callq 0xeeb67
movq %rax, 0x30(%r14)
leaq 0xded96(%rip), %rsi     # 0x118feb
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
leaq 0x6(%r15), %rsi
callq 0xeed0d
movq %rax, 0x28(%r14)
leaq 0xded89(%rip), %rsi     # 0x119003
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x28(%r14), %rdx
leaq -0x801(%rdx), %rax
cmpq $-0x800, %rax           # imm = 0xF800
jae 0x3a2a7
movw $0x101, 0x8(%r12)       # imm = 0x101
jmp 0x3a413
movq %r12, 0x8(%rsp)
decq %rdx
imulq 0x20(%r14), %rdx
addq 0x30(%r14), %rdx
movq %rdx, 0x10(%r14)
leaq 0xded4e(%rip), %rsi     # 0x119011
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
leaq 0xe(%r15), %rax
leaq 0x10(%rsp), %r12
movq %rax, (%r12)
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xeed31
movq %rax, %rbp
leaq 0xded3c(%rip), %rsi     # 0x11902c
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xeed31
movq %rax, %r12
leaq 0xded2c(%rip), %rsi     # 0x11903f
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
cmpb $0x0, 0x1(%r14)
jne 0x3a361
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rax
cmpq 0x10(%r14), %rax
jle 0x3a361
movq 0x18(%rbx), %rax
cmpq 0x18(%rax), %r12
jge 0x3a361
leaq 0xded0b(%rip), %rsi     # 0x119052
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x8(%rsp), %rax
movb $0x1, 0xc(%rax)
movq %r12, 0x98(%rax)
movq 0x28(%r14), %rsi
movl $0x18, %edx
movq %rbx, %rdi
callq 0xe96f7
movq %rax, 0x38(%r14)
addq %r15, %r13
movq %r13, 0x10(%rsp)
leaq 0xdece1(%rip), %rsi     # 0x119066
movq %rbx, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
cmpq $0x0, 0x28(%r14)
jle 0x3a413
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 0xeeb91
movq 0x38(%r14), %rcx
movq %rax, 0x8(%rcx,%r12)
movq 0x20(%r14), %r9
movq %r9, 0x10(%rcx,%r12)
cmpq %r15, %r9
jle 0x3a3e5
movq %r15, 0x10(%rcx,%r12)
movq %r15, %r9
addq %rax, %rbp
subq %r9, %r15
movq (%rcx,%r12), %rcx
movq %rbx, %rdi
leaq 0xdec82(%rip), %rsi     # 0x11907b
movq %r13, %rdx
movq %rax, %r8
xorl %eax, %eax
callq 0xe8617
incq %r13
addq $0x18, %r12
cmpq 0x28(%r14), %r13
jl 0x3a3b0
movl 0x4(%rsp), %esi
movq %rbx, %rdi
callq 0xe8a25
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq