// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/dms.c
// Function: dms_scan_file
static int dms_scan_file(deark *c, struct dmsctx *d, i64 pos1)
{
	i64 pos = pos1;
	i64 i;
	i64 lowest_real_track_num = DMS_MAX_TRACKS;
	i64 highest_real_track_num = 0;
	u32 next_real_tracknum_expected;
	int retval = 0;

	de_dbg(c, "scanning file");
	de_dbg_indent(c, 1);

	d->num_tracks_in_file = 0;

	while(1) {
		i64 track_num_reported;
		i64 cmpr_len;
		i64 uncmpr_len;
		u8 track_flags;
		u8 cmpr_type;

		if(pos+DMS_TRACK_HDR_LEN > c->infile->len) break;

		if(dbuf_memcmp(c->infile, pos, "TR", 2)) {
			de_dbg(c, "[track not found at %"I64_FMT"; assuming disk image ends here]", pos);
			break;
		}
		if(d->num_tracks_in_file >= DMS_MAX_TRACKS) {
			de_err(c, "Too many tracks in file");
			break;
		}

		track_num_reported = de_getu16be(pos+2);
		cmpr_len = de_getu16be(pos+6);
		uncmpr_len = de_getu16be(pos+10);
		track_flags = de_getbyte(pos+12);
		cmpr_type = de_getbyte(pos+13);

		de_dbg(c, "track[%d] at %"I64_FMT", #%d, len=%"I64_FMT"/%"I64_FMT", cmpr=%u, flags=0x%02x",
			(int)d->num_tracks_in_file, pos, (int)track_num_reported, cmpr_len, uncmpr_len,
			(UI)cmpr_type, (UI)track_flags);

		d->tracks_by_file_order[d->num_tracks_in_file].file_pos = pos;
		d->tracks_by_file_order[d->num_tracks_in_file].track_num = (u32)track_num_reported;

		if(d->have_first_last_track_info) {
			if(track_num_reported>=d->first_track && track_num_reported<=d->last_track) {
				d->tracks_by_track_num[track_num_reported].order_in_file = (u32)d->num_tracks_in_file;
				d->tracks_by_track_num[track_num_reported].in_use = 1;
			}
		}
		else {
			if(track_num_reported<DMS_MAX_TRACKS) {
				d->tracks_by_track_num[track_num_reported].order_in_file = (u32)d->num_tracks_in_file;
				d->tracks_by_track_num[track_num_reported].in_use = 1;
			}
		}

		if(track_num_reported<DMS_MAX_TRACKS) {
			if(track_num_reported<lowest_real_track_num) {
				lowest_real_track_num = track_num_reported;
			}
			if(track_num_reported>highest_real_track_num) {
				highest_real_track_num = track_num_reported;
			}
		}

		d->num_tracks_in_file++;
		pos += DMS_TRACK_HDR_LEN + cmpr_len;
	}

	if(d->num_tracks_in_file==0) {
		de_err(c, "No tracks found");
		goto done;
	}

	if(!d->have_first_last_track_info) {
		d->first_track = lowest_real_track_num;
		d->last_track = highest_real_track_num;
	}

	// Make sure all expected tracks are present, and mark the "real" tracks in
	// tracks_by_file_order[].
	// One reason for doing it this way is that there may be two tracks numbered 0,
	// with the second one being the real one.
	for(i=d->first_track; i<=d->last_track; i++) {
		if(!d->tracks_by_track_num[i].in_use) {
			// TODO: Maybe we should write a track of all zeroes instead (but how many zeroes?)
			de_err(c, "Could not find track #%d", (int)i);
			goto done;
		}

		d->tracks_by_file_order[d->tracks_by_track_num[i].order_in_file].is_real = 1;
	}

	next_real_tracknum_expected = (u32)d->first_track;
	for(i=0; i<d->num_tracks_in_file; i++) {
		if(d->tracks_by_file_order[i].is_real) {
			// I'm not going to bother supporting out-of-order tracks, at least until
			// I learn that such files exist.
			if(d->tracks_by_file_order[i].track_num != next_real_tracknum_expected) {
				de_err(c, "Track numbers not in order. Not supported.");
				goto done;
			}
			next_real_tracknum_expected = d->tracks_by_file_order[i].track_num + 1;
		}
	}

	retval = 1;
done:
	de_dbg_indent(c, -1);
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x20(%rbp)
movq $0xc8, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movl $0x0, -0x40(%rbp)
movq -0x8(%rbp), %rdi
leaq 0x144bb2(%rip), %rsi    # 0x1a9a93
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x10(%rbp), %rax
movq $0x0, 0x18(%rax)
movq -0x20(%rbp), %rax
addq $0x14, %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jle 0x64f1d
jmp 0x65158
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
leaq 0x13db2c(%rip), %rdx    # 0x1a2a5c
movl $0x2, %ecx
callq 0x166ab0
cmpl $0x0, %eax
je 0x64f5a
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rdx
leaq 0x144b53(%rip), %rsi    # 0x1a9aa1
movb $0x0, %al
callq 0x15b0d0
jmp 0x65158
movq -0x10(%rbp), %rax
cmpq $0xc8, 0x18(%rax)
jl 0x64f7f
movq -0x8(%rbp), %rdi
leaq 0x144b66(%rip), %rsi    # 0x1a9ad9
movb $0x0, %al
callq 0x15bfb0
jmp 0x65158
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
addq $0x2, %rsi
callq 0x1651d0
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
addq $0x6, %rsi
callq 0x1651d0
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
addq $0xa, %rsi
callq 0x1651d0
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
addq $0xc, %rsi
callq 0x165060
movb %al, -0x59(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
addq $0xd, %rsi
callq 0x165060
movb %al, -0x5a(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl %eax, %edx
movq -0x20(%rbp), %rcx
movq -0x48(%rbp), %rax
movl %eax, %r8d
movq -0x50(%rbp), %r9
movq -0x58(%rbp), %r11
movzbl -0x5a(%rbp), %r10d
movzbl -0x59(%rbp), %eax
leaq 0x144ac6(%rip), %rsi    # 0x1a9af1
movq %r11, (%rsp)
movl %r10d, 0x8(%rsp)
movl %eax, 0x10(%rsp)
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rax
addq $0x30, %rax
movq -0x10(%rbp), %rdx
movq 0x18(%rdx), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movq -0x48(%rbp), %rax
movl %eax, %ecx
movq -0x10(%rbp), %rax
addq $0x30, %rax
movq -0x10(%rbp), %rdx
movq 0x18(%rdx), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
movq -0x10(%rbp), %rax
cmpb $0x0, 0x20(%rax)
je 0x650ce
movq -0x48(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jl 0x650cc
movq -0x48(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jg 0x650cc
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl %eax, %edx
movq -0x10(%rbp), %rax
movq -0x48(%rbp), %rcx
movl %edx, 0xcb0(%rax,%rcx,8)
movq -0x10(%rbp), %rax
movq -0x48(%rbp), %rcx
movb $0x1, 0xcb4(%rax,%rcx,8)
jmp 0x65103
cmpq $0xc8, -0x48(%rbp)
jge 0x65101
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl %eax, %edx
movq -0x10(%rbp), %rax
movq -0x48(%rbp), %rcx
movl %edx, 0xcb0(%rax,%rcx,8)
movq -0x10(%rbp), %rax
movq -0x48(%rbp), %rcx
movb $0x1, 0xcb4(%rax,%rcx,8)
jmp 0x65103
cmpq $0xc8, -0x48(%rbp)
jge 0x65133
movq -0x48(%rbp), %rax
cmpq -0x30(%rbp), %rax
jge 0x6511f
movq -0x48(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x38(%rbp), %rax
jle 0x65131
movq -0x48(%rbp), %rax
movq %rax, -0x38(%rbp)
jmp 0x65133
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x18(%rax)
movq -0x50(%rbp), %rax
addq $0x14, %rax
addq -0x20(%rbp), %rax
movq %rax, -0x20(%rbp)
jmp 0x64f02
movq -0x10(%rbp), %rax
cmpq $0x0, 0x18(%rax)
jne 0x6517a
movq -0x8(%rbp), %rdi
leaq 0x1449bd(%rip), %rsi    # 0x1a9b2b
movb $0x0, %al
callq 0x15bfb0
jmp 0x652b4
movq -0x10(%rbp), %rax
cmpb $0x0, 0x20(%rax)
jne 0x6519c
movq -0x30(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x38(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jg 0x65215
movq -0x10(%rbp), %rax
movq -0x28(%rbp), %rcx
cmpb $0x0, 0xcb4(%rax,%rcx,8)
jne 0x651e5
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movl %eax, %edx
leaq 0x144962(%rip), %rsi    # 0x1a9b3b
movb $0x0, %al
callq 0x15bfb0
jmp 0x652b4
movq -0x10(%rbp), %rax
addq $0x30, %rax
movq -0x10(%rbp), %rcx
movq -0x28(%rbp), %rdx
movl 0xcb0(%rcx,%rdx,8), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movb $0x1, 0xc(%rax)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x651a8
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, -0x3c(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jge 0x652ad
movq -0x10(%rbp), %rax
addq $0x30, %rax
movq -0x28(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
cmpb $0x0, 0xc(%rax)
je 0x6529a
movq -0x10(%rbp), %rax
addq $0x30, %rax
movq -0x28(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
cmpl -0x3c(%rbp), %eax
je 0x6527e
movq -0x8(%rbp), %rdi
leaq 0x1448df(%rip), %rsi    # 0x1a9b54
movb $0x0, %al
callq 0x15bfb0
jmp 0x652b4
movq -0x10(%rbp), %rax
addq $0x30, %rax
movq -0x28(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x6529c
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x65228
movl $0x1, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movl -0x40(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nop