// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x5c(%rbp), %rsi
callq 0x15b580
movq -0x18(%rbp), %rax
movb $0x1, (%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0x156c1e(%rip), %rsi    # 0x1a4c55
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdx
leaq 0x156c02(%rip), %rsi    # 0x1a4c61
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
callq 0x1651d0
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
addq $0x2, %rsi
callq 0x1651d0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rdx
leaq 0x156bc1(%rip), %rsi    # 0x1a4c6f
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
cmpq $0x1, 0x20(%rax)
jge 0x4e0d5
movq -0x10(%rbp), %rax
movb $0x1, 0x8(%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x9(%rax)
jmp 0x4e41d
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
addq $0x4, %rsi
callq 0x1651d0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rdx
leaq 0x156b83(%rip), %rsi    # 0x1a4c8b
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
addq $0x6, %rsi
callq 0x165470
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rdx
leaq 0x156b61(%rip), %rsi    # 0x1a4ca3
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
cmpq $0x1, 0x28(%rax)
jl 0x4e162
movq -0x18(%rbp), %rax
cmpq $0x800, 0x28(%rax)      # imm = 0x800
jle 0x4e177
movq -0x10(%rbp), %rax
movb $0x1, 0x8(%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x9(%rax)
jmp 0x4e41d
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rcx
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
subq $0x1, %rax
imulq %rax, %rcx
movq -0x18(%rbp), %rax
addq 0x30(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdx
leaq 0x156aff(%rip), %rsi    # 0x1a4cb1
movb $0x0, %al
callq 0x15b0d0
movq -0x30(%rbp), %rax
addq $0xe, %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x1654b0
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rdx
leaq 0x156ae3(%rip), %rsi    # 0x1a4ccc
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x1654b0
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rdx
leaq 0x156acb(%rip), %rsi    # 0x1a4cdf
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
cmpb $0x0, 0x1(%rax)
jne 0x4e272
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x18(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jle 0x4e272
movq -0x58(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jge 0x4e272
movq -0x8(%rbp), %rdi
leaq 0x156a9e(%rip), %rsi    # 0x1a4cf2
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movb $0x1, 0xc(%rax)
movq -0x58(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x98(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rsi
movl $0x18, %edx
callq 0x15c450
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x30(%rbp), %rax
addq -0x48(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rdx
leaq 0x156a58(%rip), %rsi    # 0x1a4d06
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x50(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x28(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x28(%rcx), %rax
jge 0x4e41b
movq -0x20(%rbp), %rcx
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
imulq $0x18, -0x40(%rbp), %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x165210
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
imulq $0x18, -0x40(%rbp), %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
imulq $0x18, -0x40(%rbp), %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
addq -0x20(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rcx
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
imulq $0x18, -0x40(%rbp), %rdx
addq %rdx, %rax
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
imulq $0x18, -0x40(%rbp), %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
cmpq -0x28(%rbp), %rax
jle 0x4e39a
movq -0x28(%rbp), %rcx
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
imulq $0x18, -0x40(%rbp), %rdx
addq %rdx, %rax
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
imulq $0x18, -0x40(%rbp), %rcx
addq %rcx, %rax
movq 0x10(%rax), %rcx
movq -0x28(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rdx
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
imulq $0x18, -0x40(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
imulq $0x18, -0x40(%rbp), %rsi
addq %rsi, %rax
movq 0x8(%rax), %r8
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
imulq $0x18, -0x40(%rbp), %rsi
addq %rsi, %rax
movq 0x10(%rax), %r9
leaq 0x156918(%rip), %rsi    # 0x1a4d1b
movb $0x0, %al
callq 0x15b0d0
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x4e2df
jmp 0x4e41d
movq -0x8(%rbp), %rdi
movl -0x5c(%rbp), %esi
callq 0x15b5a0
addq $0x60, %rsp
popq %rbp
retq
nop