// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/atari-dsk.c
// Function: do_pasti_track
static int do_pasti_track(deark *c, struct pastictx *d, i64 trkidx, i64 pos1, i64 len)
{
	i64 pos = pos1;
	u8 track_number_raw;
	struct pasti_track_ctx *tctx = NULL;
	int retval = 0;

	de_dbg(c, "track record[%d] at %"I64_FMT", len=%"I64_FMT, (int)trkidx, pos1, len);
	de_dbg_indent(c, 1);

	tctx = de_malloc(c, sizeof(struct pasti_track_ctx));
	pos = pos1;
	pos += 4; // record size, already read

	tctx->fuzzy_count = de_getu32le_p(&pos);
	de_dbg(c, "fuzzyCount: %"I64_FMT, tctx->fuzzy_count);
	tctx->sector_count = de_getu16le_p(&pos);
	de_dbg(c, "sectorCount: %"I64_FMT, tctx->sector_count);
	if(tctx->sector_count > PASTI_MAX_SECTORS_PER_TRACK) {
		goto done;
	}

	tctx->track_flags = (UI)de_getu16le_p(&pos);
	de_dbg(c, "trackFlags: 0x%02x", tctx->track_flags);

	tctx->track_length = de_getu16le_p(&pos);
	de_dbg(c, "trackLength: %"I64_FMT, tctx->track_length);

	track_number_raw = de_getbyte_p(&pos);
	tctx->track_num = (i64)(track_number_raw & 0x7f);
	tctx->side_num = (i64)(track_number_raw>>7);
	de_dbg(c, "trackNumber: 0x%02x (track %u, side %u)", (UI)track_number_raw,
		(UI)tctx->track_num, (UI)tctx->side_num);

	pos += 1; // trackType, unused

	if(tctx->track_flags & TRK_SECT) {
		tctx->sector_descriptors_pos = pos;
		pos += 16 * tctx->sector_count;
	}

	pos += tctx->fuzzy_count;

	tctx->track_data_record_pos = pos;

	if(!(tctx->track_flags & TRK_SECT)) {
		// TODO: Support this
		de_err(c, "Pasti files without sector descriptors are not supported");
		goto done;
	}

	if(trkidx==0) {
		// Special handling for the first track in the file

		if(track_number_raw == 0x00) {
			pasti_find_and_read_boot_sector(c, d, tctx);
		}
		else {
			de_err(c, "First track in file is not track 0/side 0; can't convert this file");
			d->convert_errflag = 1;
		}
	}

	if(tctx->sector_count>0) {
		if(!do_pasti_sectors(c, d, tctx)) goto done;
	}

	retval = 1;
done:
	if(tctx) {
		de_free(c, tctx);
	}
	de_dbg_indent(c, -1);
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x30(%rbp)
movq $0x0, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl %eax, %edx
movq -0x20(%rbp), %rcx
movq -0x28(%rbp), %r8
leaq 0x177eb3(%rip), %rsi    # 0x19efaf
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movl $0x40, %esi
callq 0x15b780
movq %rax, -0x40(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165580
movq %rax, %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rdx
leaq 0x177e6a(%rip), %rsi    # 0x19efd0
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x1652c0
movq %rax, %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rdx
leaq 0x177e44(%rip), %rsi    # 0x19efe0
movb $0x0, %al
callq 0x15b0d0
movq -0x40(%rbp), %rax
cmpq $0x64, 0x18(%rax)
jle 0x271b3
jmp 0x27365
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x1652c0
movl %eax, %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x20(%rax)
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rax
movl 0x20(%rax), %edx
leaq 0x177e12(%rip), %rsi    # 0x19eff1
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x1652c0
movq %rax, %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rax
movq 0x28(%rax), %rdx
leaq 0x177def(%rip), %rsi    # 0x19f004
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165170
movb %al, -0x31(%rbp)
movzbl -0x31(%rbp), %eax
andl $0x7f, %eax
movslq %eax, %rcx
movq -0x40(%rbp), %rax
movq %rcx, (%rax)
movzbl -0x31(%rbp), %eax
sarl $0x7, %eax
movslq %eax, %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rdi
movzbl -0x31(%rbp), %edx
movq -0x40(%rbp), %rax
movq (%rax), %rax
movl %eax, %ecx
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, %r8d
leaq 0x177d9f(%rip), %rsi    # 0x19f015
movb $0x0, %al
callq 0x15b0d0
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
movq -0x40(%rbp), %rax
movl 0x20(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x272b8
movq -0x30(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rax
shlq $0x4, %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x40(%rbp), %rax
movl 0x20(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x272f7
movq -0x8(%rbp), %rdi
leaq 0x177d4f(%rip), %rsi    # 0x19f03d
movb $0x0, %al
callq 0x15bfb0
jmp 0x27365
cmpq $0x0, -0x18(%rbp)
jne 0x27339
movzbl -0x31(%rbp), %eax
cmpl $0x0, %eax
jne 0x2731a
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x40(%rbp), %rdx
callq 0x27390
jmp 0x27337
movq -0x8(%rbp), %rdi
leaq 0x177d51(%rip), %rsi    # 0x19f076
movb $0x0, %al
callq 0x15bfb0
movq -0x10(%rbp), %rax
movl $0x1, 0x30(%rax)
jmp 0x27339
movq -0x40(%rbp), %rax
cmpq $0x0, 0x18(%rax)
jle 0x2735e
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x40(%rbp), %rdx
callq 0x275d0
cmpl $0x0, %eax
jne 0x2735c
jmp 0x27365
jmp 0x2735e
movl $0x1, -0x44(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x27379
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x15b820
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movl -0x44(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq