// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/atari-dsk.c
// Function: pasti_find_and_read_boot_sector
static void pasti_find_and_read_boot_sector(deark *c, struct pastictx *d,
	struct pasti_track_ctx *tctx)
{
	i64 secnum;
	i64 s1pos = 0; // sector 1 abs pos, 0 if unknown

	for(secnum=0; secnum<tctx->sector_count; secnum++) {
		i64 data_offset_rel;
		i64 n_secnum;
		i64 pos = tctx->sector_descriptors_pos + secnum*16;

		n_secnum = (i64)de_getbyte(pos + 10);
		if(n_secnum == 1) {
			data_offset_rel = de_getu32le(pos);
			s1pos = tctx->track_data_record_pos + data_offset_rel;
			break;
		}
	}

	if(s1pos==0) {
		de_err(c, "Could not find boot sector");
		d->convert_errflag = 1;
		goto done;
	}

	de_dbg(c, "found boot sector; data at %"I64_FMT, s1pos);
	de_dbg_indent(c, 1);
	d->fat_bytes_per_sector = de_getu16le(s1pos+11);
	de_dbg(c, "bytes per sector: %d", (int)d->fat_bytes_per_sector);
	d->fat_sectors_per_track = de_getu16le(s1pos+24);
	de_dbg(c, "sectors per track: %d", (int)d->fat_sectors_per_track);
	d->fat_num_heads = de_getu16le(s1pos+26);
	de_dbg(c, "number of heads: %d", (int)d->fat_num_heads);

	de_dbg_indent(c, -1);

	if(d->fat_bytes_per_sector<128 || d->fat_bytes_per_sector>2048 ||
		d->fat_sectors_per_track<1 || d->fat_sectors_per_track>32 ||
		d->fat_num_heads<1 || d->fat_num_heads>2)
	{
		de_err(c, "Invalid or unsupported disk geometry (%d sectors/track, "
			"%d heads, %d bytes/sector)", (int)d->fat_sectors_per_track,
			(int)d->fat_num_heads, (int)d->fat_bytes_per_sector);
		d->convert_errflag = 1;
		goto done;
	}

done:
	;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jge 0x27433
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x20(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x40(%rbp), %rsi
addq $0xa, %rsi
callq 0x165060
movzbl %al, %eax
movq %rax, -0x38(%rbp)
cmpq $0x1, -0x38(%rbp)
jne 0x27423
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x40(%rbp), %rsi
callq 0x165540
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
addq -0x30(%rbp), %rax
movq %rax, -0x28(%rbp)
jmp 0x27433
jmp 0x27425
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x273b4
cmpq $0x0, -0x28(%rbp)
jne 0x2745c
movq -0x8(%rbp), %rdi
leaq 0x177c74(%rip), %rsi    # 0x19f0b9
movb $0x0, %al
callq 0x15bfb0
movq -0x10(%rbp), %rax
movl $0x1, 0x30(%rax)
jmp 0x275ca
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rdx
leaq 0x177c69(%rip), %rsi    # 0x19f0d4
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rsi
addq $0xb, %rsi
callq 0x165280
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl %eax, %edx
leaq 0x177c3e(%rip), %rsi    # 0x19f0f3
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rsi
addq $0x18, %rsi
callq 0x165280
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl %eax, %edx
leaq 0x177c17(%rip), %rsi    # 0x19f108
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rsi
addq $0x1a, %rsi
callq 0x165280
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movl %eax, %edx
leaq 0x177bf1(%rip), %rsi    # 0x19f11e
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x10(%rbp), %rax
cmpq $0x80, 0x18(%rax)
jl 0x2758a
movq -0x10(%rbp), %rax
cmpq $0x800, 0x18(%rax)      # imm = 0x800
jg 0x2758a
movq -0x10(%rbp), %rax
cmpq $0x1, 0x20(%rax)
jl 0x2758a
movq -0x10(%rbp), %rax
cmpq $0x20, 0x20(%rax)
jg 0x2758a
movq -0x10(%rbp), %rax
cmpq $0x1, 0x28(%rax)
jl 0x2758a
movq -0x10(%rbp), %rax
cmpq $0x2, 0x28(%rax)
jle 0x275c8
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl %eax, %edx
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl %eax, %r8d
leaq 0x177b7e(%rip), %rsi    # 0x19f132
movb $0x0, %al
callq 0x15bfb0
movq -0x10(%rbp), %rax
movl $0x1, 0x30(%rax)
jmp 0x275ca
jmp 0x275ca
addq $0x40, %rsp
popq %rbp
retq