// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/atari-dsk.c
// Function: do_directory_entry
static void do_directory_entry(deark *c, lctx *d, dbuf *f, i64 pos)
{
	u8 flags;
	i64 sector_count;
	i64 starting_sector;
	de_ucstring *fn_u = NULL;
	de_ucstring *fn_ext = NULL;
	de_finfo *fi = NULL;
	dbuf *outf = NULL;
	de_ext_encoding fn_encoding;

	flags = dbuf_getbyte(f, pos);
	de_dbg(c, "flags: 0x%02x", (unsigned int)flags);
	if((flags&0x40)==0) {
		// Unused or deleted directory entry
		return;
	}

	sector_count = dbuf_getu16le(f, pos+1);
	starting_sector = dbuf_getu16le(f, pos+3);
	de_dbg(c, "sector start: %d, count: %d", (int)starting_sector, (int)sector_count);

	if(starting_sector<1) goto done;

	if(starting_sector > 720) {
		de_err(c, "Bad starting sector: %d", (int)starting_sector);
		goto done;
	}
	if(sector_count > 720) {
		de_err(c, "Bad file size: %d blocks", (int)sector_count);
		goto done;
	}

	fn_u = ucstring_create(c);
	fn_ext = ucstring_create(c);

	// TODO: Use correct Atari encoding.
	fn_encoding = DE_EXTENC_MAKE(DE_ENCODING_ASCII, DE_ENCSUBTYPE_PRINTABLE);
	dbuf_read_to_ucstring(f, pos+5, 8, fn_u, 0,fn_encoding);
	dbuf_read_to_ucstring(f, pos+13, 3, fn_ext, 0, fn_encoding);
	de_dbg(c, "filename: \"%s.%s\"",
		ucstring_getpsz(fn_u), ucstring_getpsz(fn_ext));
	ucstring_strip_trailing_spaces(fn_u);
	ucstring_strip_trailing_spaces(fn_ext);
	if(fn_u->len==0) {
		ucstring_append_char(fn_u, '_');
	}
	if(ucstring_isnonempty(fn_ext)) {
		ucstring_append_char(fn_u, '.');
		ucstring_append_ucstring(fn_u, fn_ext);
	}

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

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

	do_extract_file_contents(c, d, f, outf, starting_sector, sector_count);

done:
	de_finfo_destroy(c, fi);
	ucstring_destroy(fn_u);
	ucstring_destroy(fn_ext);
	dbuf_close(outf);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x165060
movb %al, -0x21(%rbp)
movq -0x8(%rbp), %rdi
movzbl -0x21(%rbp), %edx
leaq 0x1959c2(%rip), %rsi    # 0x1bbb19
movb $0x0, %al
callq 0x15b0d0
movzbl -0x21(%rbp), %eax
andl $0x40, %eax
cmpl $0x0, %eax
jne 0x2616f
jmp 0x26383
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
addq $0x1, %rsi
callq 0x165280
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
addq $0x3, %rsi
callq 0x165280
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movl %eax, %edx
movq -0x30(%rbp), %rax
movl %eax, %ecx
leaq 0x178ab9(%rip), %rsi    # 0x19ec69
movb $0x0, %al
callq 0x15b0d0
cmpq $0x1, -0x38(%rbp)
jge 0x261c3
jmp 0x2635b
cmpq $0x2d0, -0x38(%rbp)     # imm = 0x2D0
jle 0x261ea
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movl %eax, %edx
leaq 0x178aa7(%rip), %rsi    # 0x19ec85
movb $0x0, %al
callq 0x15bfb0
jmp 0x2635b
cmpq $0x2d0, -0x30(%rbp)     # imm = 0x2D0
jle 0x26211
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rax
movl %eax, %edx
leaq 0x178a98(%rip), %rsi    # 0x19ec9d
movb $0x0, %al
callq 0x15bfb0
jmp 0x2635b
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, -0x48(%rbp)
movl $0x401, -0x5c(%rbp)     # imm = 0x401
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
addq $0x5, %rsi
movq -0x40(%rbp), %rcx
movl -0x5c(%rbp), %r9d
movl $0x8, %edx
xorl %r8d, %r8d
callq 0x1669f0
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
addq $0xd, %rsi
movq -0x48(%rbp), %rcx
movl -0x5c(%rbp), %r9d
movl $0x3, %edx
xorl %r8d, %r8d
callq 0x1669f0
movq -0x8(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x40(%rbp), %rdi
callq 0x172500
movq %rax, -0x68(%rbp)
movq -0x48(%rbp), %rdi
callq 0x172500
movq -0x70(%rbp), %rdi
movq -0x68(%rbp), %rdx
movq %rax, %rcx
leaq 0x178a12(%rip), %rsi    # 0x19ecb6
movb $0x0, %al
callq 0x15b0d0
movq -0x40(%rbp), %rdi
callq 0x171360
movq -0x48(%rbp), %rdi
callq 0x171360
movq -0x40(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jne 0x262d6
movq -0x40(%rbp), %rdi
movl $0x5f, %esi
callq 0x1714c0
movq -0x48(%rbp), %rdi
callq 0x1717f0
cmpl $0x0, %eax
je 0x262ff
movq -0x40(%rbp), %rdi
movl $0x2e, %esi
callq 0x1714c0
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x171460
movq -0x8(%rbp), %rdi
callq 0x15d230
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x40(%rbp), %rdx
xorl %ecx, %ecx
callq 0x15d2c0
movq -0x50(%rbp), %rax
movb $0x1, 0x8(%rax)
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rdx
xorl %eax, %eax
movl %eax, %esi
xorl %ecx, %ecx
callq 0x166c60
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x58(%rbp), %rcx
movq -0x38(%rbp), %r8
movq -0x30(%rbp), %r9
callq 0x26390
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x15d260
movq -0x40(%rbp), %rdi
callq 0x171750
movq -0x48(%rbp), %rdi
callq 0x171750
movq -0x58(%rbp), %rdi
callq 0x1676a0
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax)