// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/atari-dsk.c
// Function: do_pasti_sectors
static int do_pasti_sectors(deark *c, struct pastictx *d, struct pasti_track_ctx *tctx)
{
	int saved_indent_level;
	int retval = 0;
	i64 secnum;

	de_dbg_indent_save(c, &saved_indent_level);

	de_dbg(c, "[%d sector descriptors at %"I64_FMT"]", (int)tctx->sector_count,
		tctx->sector_descriptors_pos);
	de_dbg_indent(c, 1);

	for(secnum=0; secnum<tctx->sector_count; secnum++) {
		i64 data_offset_rel;
		u8 size_code;
		i64 pos = tctx->sector_descriptors_pos + secnum*16;
		struct pasti_sector_ctx secctx;

		de_dbg2(c, "sector[%d] descriptor at %"I64_FMT, (int)secnum, pos);
		de_dbg_indent(c, 1);
		de_zeromem(&secctx, sizeof(struct pasti_sector_ctx));
		data_offset_rel = de_getu32le_p(&pos);
		secctx.data_offset_abs = tctx->track_data_record_pos + data_offset_rel;
		de_dbg2(c, "dataOffset: %"I64_FMT" (+%"I64_FMT"=%"I64_FMT")", data_offset_rel,
			tctx->track_data_record_pos, secctx.data_offset_abs);
		pos += 2; // bitPosition
		pos += 2; // readTime

		secctx.n_track = (i64)de_getbyte_p(&pos);
		secctx.n_head = (i64)de_getbyte_p(&pos);
		secctx.n_secnum = (i64)de_getbyte_p(&pos);
		de_dbg2(c, "track %d, side %d, sector %d", (int)secctx.n_track,
			(int)secctx.n_head, (int)secctx.n_secnum);
		// TODO: What should we do if the track and head are not the expected values?
		// Who do we trust?

		size_code = de_getbyte_p(&pos);
		if(size_code>4) goto done;
		secctx.size_in_bytes = ((i64)128)<<(UI)size_code;
		de_dbg2(c, "size: 0x%02x (%"I64_FMT" bytes)", (UI)size_code, secctx.size_in_bytes);

		secctx.crc_reported = (u32)de_getu16le_p(&pos);
		de_dbg2(c, "sector crc (reported): 0x%04x", (UI)secctx.crc_reported);
		secctx.fdcFlags = de_getbyte_p(&pos);
		de_dbg2(c, "fdcFlags: 0x%02x", (UI)secctx.fdcFlags);
		pos += 1; // reserved

		pasti_save_sector_data(c, d, &secctx, tctx->track_num, tctx->side_num);
		de_dbg_indent(c, -1);
	}

	retval = 1;
done:
	de_dbg_indent_restore(c, saved_indent_level);
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl $0x0, -0x20(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x1c(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl %eax, %edx
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rcx
leaq 0x177b70(%rip), %rsi    # 0x19f185
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq $0x0, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jge 0x2784f
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x28(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movl %eax, %edx
movq -0x40(%rbp), %rcx
leaq 0x177b34(%rip), %rsi    # 0x19f1a4
movb $0x0, %al
callq 0x15b350
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
leaq -0x70(%rbp), %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x19140
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x40(%rbp), %rsi
callq 0x165580
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
addq -0x30(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rdx
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rcx
movq -0x70(%rbp), %r8
leaq 0x177aec(%rip), %rsi    # 0x19f1c1
movb $0x0, %al
callq 0x15b350
movq -0x40(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x40(%rbp), %rsi
callq 0x165170
movzbl %al, %eax
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x40(%rbp), %rsi
callq 0x165170
movzbl %al, %eax
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x40(%rbp), %rsi
callq 0x165170
movzbl %al, %eax
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rax
movl %eax, %edx
movq -0x58(%rbp), %rax
movl %eax, %ecx
movq -0x50(%rbp), %rax
movl %eax, %r8d
leaq 0x177a82(%rip), %rsi    # 0x19f1dc
movb $0x0, %al
callq 0x15b350
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x40(%rbp), %rsi
callq 0x165170
movb %al, -0x31(%rbp)
movzbl -0x31(%rbp), %eax
cmpl $0x4, %eax
jle 0x27783
jmp 0x27856
movzbl -0x31(%rbp), %eax
movl %eax, %eax
movl %eax, %ecx
movl $0x80, %eax
shlq %cl, %rax
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rdi
movzbl -0x31(%rbp), %edx
movq -0x68(%rbp), %rcx
leaq 0x177a4f(%rip), %rsi    # 0x19f1f9
movb $0x0, %al
callq 0x15b350
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x40(%rbp), %rsi
callq 0x1652c0
movl %eax, -0x44(%rbp)
movq -0x8(%rbp), %rdi
movl -0x44(%rbp), %edx
leaq 0x177a3f(%rip), %rsi    # 0x19f212
movb $0x0, %al
callq 0x15b350
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x40(%rbp), %rsi
callq 0x165170
movb %al, -0x48(%rbp)
movq -0x8(%rbp), %rdi
movzbl -0x48(%rbp), %edx
leaq 0x177a33(%rip), %rsi    # 0x19f230
movb $0x0, %al
callq 0x15b350
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rax
movq (%rax), %rcx
movq -0x18(%rbp), %rax
movq 0x8(%rax), %r8
leaq -0x70(%rbp), %rdx
callq 0x27870
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x27632
movl $0x1, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movl -0x1c(%rbp), %esi
callq 0x15b5a0
movl -0x20(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax,%rax)