// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/d64.c
// Function: do_extract_file
static void do_extract_file(deark *c, lctx *d, i64 dir_entry_pos,
	u8 file_type, i64 ftrack, i64 fsector, i64 nsectors)
{
	i64 nsectors_written = 0;
	i64 curtrack, cursector;
	i64 nexttrack, nextsector;
	const char *ext;
	dbuf *f = NULL;
	de_finfo *fi = NULL;
	de_ucstring *fname = NULL;
	i64 fname_len;
	i64 i;
	u8 z;

	de_dbg(c, "extracting file: t=%d,s=%d,sectors=%d", (int)ftrack, (int)fsector,
		(int)nsectors);

	// Figure out the filename

	// Find the length of the filename.
	fname_len = 0;
	for(i=15; i>=0; i--) {
		z = de_getbyte(dir_entry_pos+5+i);
		// TODO: Filenames are padded with 0xa0 bytes. I'm not sure if the
		// filename length is determined by the first 0xa0 byte, or the
		// last non-0xa0 byte. This assumes it's the last non-0xa0 byte.
		if(z!=0xa0) {
			fname_len = i+1;
			break;
		}
	}
	de_dbg2(c, "filename length: %d", (int)fname_len);
	fname = ucstring_create(c);
	dbuf_read_to_ucstring(c->infile, dir_entry_pos+5, fname_len, fname, 0, DE_ENCODING_PETSCII);
	de_dbg(c, "filename: \"%s\"", ucstring_getpsz(fname));

	switch(file_type) {
	case FTYPE_SEQ: ext="seq"; break;
	case FTYPE_PRG: ext="prg"; break;
	case FTYPE_USR: ext="usr"; break;
	default: ext="bin"; break;
	}
	ucstring_append_sz(fname, ".", DE_ENCODING_LATIN1);
	ucstring_append_sz(fname, ext, DE_ENCODING_LATIN1);
	///////

	fi = de_finfo_create(c);
	de_finfo_set_name_from_ucstring(c, fi, fname, 0);
	fi->original_filename_flag = 1;

	f = dbuf_create_output_file(c, NULL, fi, 0);

	curtrack = ftrack;
	cursector = fsector;
	while(1) {
		i64 pos;
		i64 amt_to_copy;

		if(nsectors_written>=nsectors) break;

		pos = sector_offset(curtrack, cursector);

		nexttrack = (i64)de_getbyte(pos+0);
		nextsector = (i64)de_getbyte(pos+1);
		de_dbg2(c, "next sector: t=%d,s=%d", (int)nexttrack, (int)nextsector);

		if(nexttrack==0 && nextsector>=1) amt_to_copy = (i64)nextsector-1;
		else amt_to_copy = 254;

		dbuf_copy(c->infile, pos+2, amt_to_copy, f);
		nsectors_written++;

		if(nexttrack<1) break;
		curtrack = nexttrack;
		cursector = nextsector;
	}

	dbuf_close(f);
	de_finfo_destroy(c, fi);
	ucstring_destroy(fname);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movb %cl, %al
movq 0x10(%rbp), %rcx
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movb %al, -0x19(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
movq $0x0, -0x78(%rbp)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movl %eax, %edx
movq -0x30(%rbp), %rax
movl %eax, %ecx
movq 0x10(%rbp), %rax
movl %eax, %r8d
leaq 0x14ee97(%rip), %rsi    # 0x1a626d
movb $0x0, %al
callq 0x15b0d0
movq $0x0, -0x80(%rbp)
movq $0xf, -0x88(%rbp)
cmpq $0x0, -0x88(%rbp)
jl 0x57451
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x5, %rsi
addq -0x88(%rbp), %rsi
callq 0x165060
movb %al, -0x89(%rbp)
movzbl -0x89(%rbp), %eax
cmpl $0xa0, %eax
je 0x5743b
movq -0x88(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
jmp 0x57451
jmp 0x5743d
movq -0x88(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x88(%rbp)
jmp 0x573f0
movq -0x8(%rbp), %rdi
movl -0x80(%rbp), %edx
leaq 0x14ee34(%rip), %rsi    # 0x1a6293
xorl %eax, %eax
movl %eax, -0xb4(%rbp)
movb %al, -0xa5(%rbp)
callq 0x15b350
movq -0x8(%rbp), %rdi
callq 0x1711d0
movl -0xb4(%rbp), %r8d
movq %rax, -0x78(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x5, %rsi
movq -0x80(%rbp), %rdx
movq -0x78(%rbp), %rcx
movl $0x15, %r9d
callq 0x1669f0
movq -0x8(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x78(%rbp), %rdi
callq 0x172500
movq -0xb0(%rbp), %rdi
movq %rax, %rdx
movb -0xa5(%rbp), %al
leaq 0x1700af(%rip), %rsi    # 0x1c7583
callq 0x15b0d0
movzbl -0x19(%rbp), %eax
movl %eax, -0xa4(%rbp)
subl $0x1, %eax
je 0x57504
jmp 0x574ea
movl -0xa4(%rbp), %eax
subl $0x2, %eax
je 0x57511
jmp 0x574f7
movl -0xa4(%rbp), %eax
subl $0x3, %eax
je 0x5751e
jmp 0x5752b
leaq 0x14ed9c(%rip), %rax    # 0x1a62a7
movq %rax, -0x60(%rbp)
jmp 0x57536
leaq 0x16fe07(%rip), %rax    # 0x1c731f
movq %rax, -0x60(%rbp)
jmp 0x57536
leaq 0x14ed86(%rip), %rax    # 0x1a62ab
movq %rax, -0x60(%rbp)
jmp 0x57536
leaq 0x169cd5(%rip), %rax    # 0x1c1207
movq %rax, -0x60(%rbp)
movq -0x78(%rbp), %rdi
leaq 0x16fc62(%rip), %rsi    # 0x1c71a3
movl $0x5, %edx
callq 0x1715e0
movq -0x78(%rbp), %rdi
movq -0x60(%rbp), %rsi
movl $0x5, %edx
callq 0x1715e0
movq -0x8(%rbp), %rdi
callq 0x15d230
movq %rax, -0x70(%rbp)
movq -0x8(%rbp), %rdi
movq -0x70(%rbp), %rsi
movq -0x78(%rbp), %rdx
xorl %ecx, %ecx
callq 0x15d2c0
movq -0x70(%rbp), %rax
movb $0x1, 0x8(%rax)
movq -0x8(%rbp), %rdi
movq -0x70(%rbp), %rdx
xorl %eax, %eax
movl %eax, %esi
xorl %ecx, %ecx
callq 0x166c60
movq %rax, -0x68(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x38(%rbp), %rax
cmpq 0x10(%rbp), %rax
jl 0x575bb
jmp 0x576a2
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x56f40
movq %rax, -0x98(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x98(%rbp), %rsi
addq $0x0, %rsi
callq 0x165060
movzbl %al, %eax
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x98(%rbp), %rsi
addq $0x1, %rsi
callq 0x165060
movzbl %al, %eax
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rax
movl %eax, %edx
movq -0x58(%rbp), %rax
movl %eax, %ecx
leaq 0x14ec8b(%rip), %rsi    # 0x1a62af
movb $0x0, %al
callq 0x15b350
cmpq $0x0, -0x50(%rbp)
jne 0x5764a
cmpq $0x1, -0x58(%rbp)
jl 0x5764a
movq -0x58(%rbp), %rax
subq $0x1, %rax
movq %rax, -0xa0(%rbp)
jmp 0x57655
movq $0xfe, -0xa0(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x98(%rbp), %rsi
addq $0x2, %rsi
movq -0xa0(%rbp), %rdx
movq -0x68(%rbp), %rcx
callq 0x166070
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
cmpq $0x1, -0x50(%rbp)
jge 0x5768d
jmp 0x576a2
movq -0x50(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x48(%rbp)
jmp 0x575ac
movq -0x68(%rbp), %rdi
callq 0x1676a0
movq -0x8(%rbp), %rdi
movq -0x70(%rbp), %rsi
callq 0x15d260
movq -0x78(%rbp), %rdi
callq 0x171750
addq $0xc0, %rsp
popq %rbp
retq
nopw (%rax,%rax)