// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/apple2-dsk.c
// Function: read_sector_list
static int read_sector_list(deark *c, lctx *d, struct member_data *md)
{
	int retval = 0;
	UI seclist_tr = 0;
	UI seclist_se = 0;
	UI next_seclist_tr, next_seclist_se;
	UI items_left_in_this_seclist_sector;
	i64 dsidx = 0; // Data sector index (# of data sectors found)
	i64 curpos = 0;
	i64 num_sectors_remaining;
	int saved_indent_level;

	de_dbg_indent_save(c, &saved_indent_level);

	if(md->len_in_sectors > d->ntracks * d->secsize) {
		de_err(c, "Bad file size");
		goto done;
	}

	// We alloc enough space for all sectors, but only the *data* sectors will actually
	// be stored in sector_list.
	md->sector_list = de_mallocarray(c, md->len_in_sectors, sizeof(struct trksec_pair));

	next_seclist_tr = md->first_sec_list_tr;
	next_seclist_se = md->first_sec_list_se;
	items_left_in_this_seclist_sector = 0;

	// Note that, apparently, len_in_sectors includes the sector_list sectors.
	num_sectors_remaining = md->len_in_sectors;

	de_dbg_indent(c, 1);
	while(num_sectors_remaining>0) {
		if(items_left_in_this_seclist_sector==0) {
			seclist_tr = next_seclist_tr;
			seclist_se = next_seclist_se;
			curpos = trksec_to_offset(d, seclist_tr, seclist_se);
			de_dbg_indent(c, -1);
			de_dbg(c, "sector list sector: %u,%u (%"I64_FMT")", seclist_tr, seclist_se,
				curpos);
			de_dbg_indent(c, 1);
			if(seclist_tr==0) {
				de_err(c, "Bad sector list");
				goto done;
			}
			if(!claim_sector(c, d, seclist_tr, seclist_se)) goto done;
			num_sectors_remaining--;
			curpos++;
			next_seclist_tr = de_getbyte_p(&curpos);
			next_seclist_se = de_getbyte_p(&curpos);
			de_dbg(c, "next sector list sector: %u,%u", (UI)next_seclist_tr, (UI)next_seclist_se);
			curpos += 2; // unused
			curpos += 2; // first sector described by this list (?)
			curpos += 5; // unused

			items_left_in_this_seclist_sector = d->sector_list_sector_capacity;
		}
		if(num_sectors_remaining<1) break;

		md->sector_list[dsidx].tr = de_getbyte_p(&curpos);
		md->sector_list[dsidx].se = de_getbyte_p(&curpos);
		de_dbg2(c, "found data sector: %u,%u",
			md->sector_list[dsidx].tr, md->sector_list[dsidx].se);
		items_left_in_this_seclist_sector--;

		if(md->sector_list[dsidx].tr==0 && md->sector_list[dsidx].se==0) {
			// TODO: How to determine the expected file size
			de_warn(c, "%s: Expected more data sectors than were found. "
				"This may or may not indicate a problem.", ucstring_getpsz_d(md->fn));
			break;
		}

		if(!claim_sector(c, d, md->sector_list[dsidx].tr, md->sector_list[dsidx].se)) {
			goto done;
		}
		dsidx++;
		num_sectors_remaining--;
	}

	de_dbg_indent_restore(c, saved_indent_level);
	md->num_data_sectors = dsidx;
	de_dbg(c, "data sectors found: %"I64_FMT, md->num_data_sectors);
	retval = 1;
done:
	de_dbg_indent_restore(c, saved_indent_level);
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x4c(%rbp), %rsi
callq 0x15b580
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x8(%rcx), %rcx
movq -0x10(%rbp), %rdx
imulq (%rdx), %rcx
cmpq %rcx, %rax
jle 0x3b45a
movq -0x8(%rbp), %rdi
leaq 0x172fb8(%rip), %rsi    # 0x1ae406
movb $0x0, %al
callq 0x15bfb0
jmp 0x3b750
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rsi
movl $0x2, %edx
callq 0x15c450
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x18(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x28(%rbp)
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
cmpq $0x0, -0x48(%rbp)
jle 0x3b717
cmpl $0x0, -0x30(%rbp)
jne 0x3b5e1
movl -0x28(%rbp), %eax
movl %eax, -0x20(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, -0x24(%rbp)
movq -0x10(%rbp), %rdi
movl -0x20(%rbp), %esi
movl -0x24(%rbp), %edx
callq 0x3abd0
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x8(%rbp), %rdi
movl -0x20(%rbp), %edx
movl -0x24(%rbp), %ecx
movq -0x40(%rbp), %r8
leaq 0x167047(%rip), %rsi    # 0x1a254d
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
cmpl $0x0, -0x20(%rbp)
jne 0x3b538
movq -0x8(%rbp), %rdi
leaq 0x167041(%rip), %rsi    # 0x1a256d
movb $0x0, %al
callq 0x15bfb0
jmp 0x3b750
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x20(%rbp), %edx
movl -0x24(%rbp), %ecx
callq 0x3ad40
cmpl $0x0, %eax
jne 0x3b555
jmp 0x3b750
movq -0x48(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x40(%rbp), %rsi
callq 0x165170
movzbl %al, %eax
movl %eax, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x40(%rbp), %rsi
callq 0x165170
movzbl %al, %eax
movl %eax, -0x2c(%rbp)
movq -0x8(%rbp), %rdi
movl -0x28(%rbp), %edx
movl -0x2c(%rbp), %ecx
leaq 0x166fd1(%rip), %rsi    # 0x1a257d
movb $0x0, %al
callq 0x15b0d0
movq -0x40(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
addq $0x5, %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movl 0x20(%rax), %eax
movl %eax, -0x30(%rbp)
cmpq $0x1, -0x48(%rbp)
jge 0x3b5ed
jmp 0x3b717
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x40(%rbp), %rsi
callq 0x165170
movb %al, %dl
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x38(%rbp), %rcx
movb %dl, (%rax,%rcx,2)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x40(%rbp), %rsi
callq 0x165170
movb %al, %dl
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x38(%rbp), %rcx
movb %dl, 0x1(%rax,%rcx,2)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x38(%rbp), %rcx
movzbl (%rax,%rcx,2), %edx
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x38(%rbp), %rcx
movzbl 0x1(%rax,%rcx,2), %ecx
leaq 0x166f3e(%rip), %rsi    # 0x1a259c
movb $0x0, %al
callq 0x15b350
movl -0x30(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x38(%rbp), %rcx
movzbl (%rax,%rcx,2), %eax
cmpl $0x0, %eax
jne 0x3b6c5
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x38(%rbp), %rcx
movzbl 0x1(%rax,%rcx,2), %eax
cmpl $0x0, %eax
jne 0x3b6c5
movq -0x8(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x172650
movq -0x58(%rbp), %rdi
movq %rax, %rdx
leaq 0x166ef9(%rip), %rsi    # 0x1a25b5
movb $0x0, %al
callq 0x15bc10
jmp 0x3b717
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x38(%rbp), %rcx
movzbl (%rax,%rcx,2), %edx
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x38(%rbp), %rcx
movzbl 0x1(%rax,%rcx,2), %ecx
callq 0x3ad40
cmpl $0x0, %eax
jne 0x3b6fa
jmp 0x3b750
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
movq -0x48(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x3b4af
movq -0x8(%rbp), %rdi
movl -0x4c(%rbp), %esi
callq 0x15b5a0
movq -0x38(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdx
leaq 0x166ecb(%rip), %rsi    # 0x1a260d
movb $0x0, %al
callq 0x15b0d0
movl $0x1, -0x1c(%rbp)
movq -0x8(%rbp), %rdi
movl -0x4c(%rbp), %esi
callq 0x15b5a0
movl -0x1c(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)