// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/t64.c
// Function: do_extract_file
static void do_extract_file(deark *c, lctx *d, i64 dir_pos,
	u8 filetype_c64s, u8 filetype)
{
	i64 load_addr;
	i64 end_addr;
	i64 offset;
	dbuf *f = NULL;
	i64 payload_size; // = file_size-2
	de_ucstring *fname = NULL;
	i64 fname_len;
	i64 i;
	i64 fnpos;
	de_finfo *fi = NULL;

	load_addr = de_getu16le(dir_pos+2);
	end_addr = de_getu16le(dir_pos+4);
	offset = de_getu32le(dir_pos+8);
	de_dbg(c, "load_addr=%d end_addr=%d offset=%d", (int)load_addr,
		(int)end_addr, (int)offset);

	// File name at pos+16

	fnpos = dir_pos+16;

	// Find the length of the (space-padded) filename.
	fname_len = 0;
	for(i=15; i>=0; i--) {
		if(de_getbyte(fnpos+i)!=' ') {
			fname_len = i+1;
			break;
		}
	}
	de_dbg2(c, "filename length: %d", (int)fname_len);

	fname = ucstring_create(c);
	dbuf_read_to_ucstring(c->infile, fnpos, fname_len, fname, 0, DE_ENCODING_PETSCII);
	de_dbg(c, "filename: \"%s\"", ucstring_getpsz(fname));

	ucstring_append_sz(fname, ".prg", DE_ENCODING_LATIN1);

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

	payload_size = end_addr - load_addr;
	if(payload_size < 0) {
		// TODO: Try to support files that don't have end_addr set properly.
		de_err(c, "This type of T64 file is not supported.");
		goto done;
	}

	f = dbuf_create_output_file(c, NULL, fi, 0);
	dbuf_copy(c->infile, dir_pos+2, 2, f);
	dbuf_copy(c->infile, offset, payload_size, f);

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

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movb %r8b, %al
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movb %cl, -0x19(%rbp)
movb %al, -0x1a(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x70(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x2, %rsi
callq 0x165280
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x4, %rsi
callq 0x165280
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x8, %rsi
callq 0x165540
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movl %eax, %edx
movq -0x30(%rbp), %rax
movl %eax, %ecx
movq -0x38(%rbp), %rax
movl %eax, %r8d
leaq 0x9e9ba(%rip), %rsi     # 0x1c72fb
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x68(%rbp)
movq $0x0, -0x58(%rbp)
movq $0xf, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
jl 0x1289a6
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x68(%rbp), %rsi
addq -0x60(%rbp), %rsi
callq 0x165060
movzbl %al, %eax
cmpl $0x20, %eax
je 0x128996
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x1289a6
jmp 0x128998
movq -0x60(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x128964
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rax
movl %eax, %edx
leaq 0x7d8dc(%rip), %rsi     # 0x1a6293
movb $0x0, %al
callq 0x15b350
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x68(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq -0x50(%rbp), %rcx
xorl %r8d, %r8d
movl $0x15, %r9d
callq 0x1669f0
movq -0x8(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x50(%rbp), %rdi
callq 0x172500
movq -0x78(%rbp), %rdi
movq %rax, %rdx
leaq 0x9eb77(%rip), %rsi     # 0x1c7583
movb $0x0, %al
callq 0x15b0d0
movq -0x50(%rbp), %rdi
leaq 0x9e900(%rip), %rsi     # 0x1c731e
movl $0x5, %edx
callq 0x1715e0
movq -0x8(%rbp), %rdi
callq 0x15d230
movq %rax, -0x70(%rbp)
movq -0x8(%rbp), %rdi
movq -0x70(%rbp), %rsi
movq -0x50(%rbp), %rdx
xorl %ecx, %ecx
callq 0x15d2c0
movq -0x70(%rbp), %rax
movb $0x1, 0x8(%rax)
movq -0x30(%rbp), %rax
subq -0x28(%rbp), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jge 0x128a77
movq -0x8(%rbp), %rdi
leaq 0x9e8b5(%rip), %rsi     # 0x1c7323
movb $0x0, %al
callq 0x15bfb0
jmp 0x128ac5
movq -0x8(%rbp), %rdi
movq -0x70(%rbp), %rdx
xorl %eax, %eax
movl %eax, %esi
xorl %ecx, %ecx
callq 0x166c60
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x2, %rsi
movq -0x40(%rbp), %rcx
movl $0x2, %edx
callq 0x166070
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq -0x40(%rbp), %rcx
callq 0x166070
movq -0x40(%rbp), %rdi
callq 0x1676a0
movq -0x8(%rbp), %rdi
movq -0x70(%rbp), %rsi
callq 0x15d260
movq -0x50(%rbp), %rdi
callq 0x171750
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax)