// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/fat.c
// Function: do_dir_entry
static int do_dir_entry(deark *c, lctx *d, struct dirctx *dctx,
	i64 pos1, int nesting_level, int scanmode)
{
	u8 firstbyte;
	i64 ddate, dtime;
	int retval = 0;
	int is_deleted = 0;
	int need_curpath_pop = 0;
	de_ucstring *descr = NULL;
	struct member_data *md = NULL;

	md = de_malloc(c, sizeof(struct member_data));

	de_dbg(c, "dir entry at %"I64_FMT, pos1);
	de_dbg_indent(c, 1);

	de_read(md->fn_base, pos1+0, 8);
	de_read(md->fn_ext, pos1+8, 3);
	firstbyte = md->fn_base[0];

	if(firstbyte==0x00) {
		de_dbg(c, "[end of dir marker]");
		goto done;
	}
	retval = 1;

	md->attribs = (UI)de_getbyte(pos1+11);
	descr = ucstring_create(c);
	de_describe_dos_attribs(c, md->attribs, descr, 0x1);
	de_dbg(c, "attribs: 0x%02x (%s)", md->attribs, ucstring_getpsz_d(descr));
	if((md->attribs & 0x3f)==0x0f) {
		do_vfat_entry(c, d, dctx, pos1, firstbyte);
		goto done;
	}

	if((md->attribs & 0x18) == 0x00) {
		; // Normal file
	}
	else if((md->attribs & 0x18) == 0x08) {
		md->is_volume_label = 1;
	}
	else if((md->attribs & 0x18) == 0x10) {
		md->is_subdir = 1;
	}
	else {
		de_warn(c, "Invalid directory entry");
		md->is_special = 1;
		dctx->lfn_valid = 0;
		goto done;
	}

	if(dctx->lfn_valid) {
		handle_vfat_lfn(c, d, dctx, md);
		dctx->lfn_valid = 0;
	}

	if(firstbyte==0xe5) {
		de_dbg(c, "[deleted]");
		is_deleted = 1;
		md->fn_base[0] = '?';
	}
	else if(firstbyte==0x05) {
		md->fn_base[0] = 0xe5;
	}

	md->fn_base_len = get_unpadded_len(md->fn_base, 8);
	md->fn_ext_len = get_unpadded_len(md->fn_ext, 3);

	if(md->is_subdir && md->fn_base_len>=1 && md->fn_base[0]=='.') {
		// special "." and ".." dirs
		md->is_special = 1;
	}

	md->short_fn = ucstring_create(c);
	if(md->is_volume_label) {
		decode_volume_label_name(c, d, md);
	}
	else {
		decode_short_filename(c, d, md);
	}

	de_dbg(c, "filename: \"%s\"", ucstring_getpsz_d(md->short_fn));

	if(ucstring_isnonempty(md->long_fn)) {
		de_strarray_push(d->curpath, md->long_fn);
	}
	else {
		de_strarray_push(d->curpath, md->short_fn);
	}
	need_curpath_pop = 1;

	if(!scanmode && d->num_fat_bits<32) {
		md->ea_handle = (UI)de_getu16le(pos1+20);
		if(md->ea_handle) {
			de_dbg(c, "EA handle (if OS/2): %u", md->ea_handle);
		}
	}

	dtime = de_getu16le(pos1+22);
	ddate = de_getu16le(pos1+24);
	de_dos_datetime_to_timestamp(&md->mod_time, ddate, dtime);
	dbg_timestamp(c, &md->mod_time, "mod time");

	// TODO: This is wrong for FAT32.
	md->first_cluster = de_getu16le(pos1+26);
	de_dbg(c, "first cluster: %"I64_FMT, md->first_cluster);

	md->filesize = de_getu32le(pos1+28);
	de_dbg(c, "file size: %"I64_FMT, md->filesize);
	if(md->is_volume_label) md->filesize = 0;

	// (Done reading dir entry)

	if(is_deleted) goto done;

	if(scanmode) {
		if(!md->is_subdir && !md->is_special && (md->attribs&0x04) &&
			md->fn_base_len==7 && md->fn_ext_len==3 &&
			!de_memcmp(md->fn_base, "EA DATA", 7) &&
			!de_memcmp(md->fn_ext, " SF", 3) )
		{
			do_fat_eadata(c, d, md);
			goto done;
		}
		de_dbg2(c, "[scan mode - not extracting]");
		goto done;
	}

	if(md->ea_handle!=0 && d->ea_data) {
		do_fat_eadata_item(c, d, md);
	}

	if(!md->is_subdir && !md->is_special) {
		do_extract_file(c, d, md);
	}
	else if(md->is_subdir && !md->is_special) {
		do_extract_file(c, d, md);
		do_subdir(c, d, md, nesting_level+1);
	}

done:
	ucstring_destroy(descr);
	if(md) {
		ucstring_destroy(md->short_fn);
		ucstring_destroy(md->long_fn);
	}
	if(need_curpath_pop) {
		de_strarray_pop(d->curpath);
	}
	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 %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movl %r9d, -0x28(%rbp)
movl $0x0, -0x44(%rbp)
movl $0x0, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x8(%rbp), %rdi
movl $0x58, %esi
callq 0x15b780
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rdx
leaq 0x14b0ce(%rip), %rsi    # 0x1cf1b7
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x60(%rbp), %rsi
movq -0x20(%rbp), %rdx
addq $0x0, %rdx
movl $0x8, %ecx
callq 0x164d10
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x60(%rbp), %rsi
addq $0x8, %rsi
movq -0x20(%rbp), %rdx
addq $0x8, %rdx
movl $0x3, %ecx
callq 0x164d10
movq -0x60(%rbp), %rax
movb (%rax), %al
movb %al, -0x29(%rbp)
movzbl -0x29(%rbp), %eax
cmpl $0x0, %eax
jne 0x84167
movq -0x8(%rbp), %rdi
leaq 0x12a105(%rip), %rsi    # 0x1ae260
movb $0x0, %al
callq 0x15b0d0
jmp 0x84683
movl $0x1, -0x44(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
addq $0xb, %rsi
callq 0x165060
movzbl %al, %ecx
movq -0x60(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rax
movl 0x10(%rax), %esi
movq -0x58(%rbp), %rdx
movl $0x1, %ecx
callq 0x15d9f0
movq -0x8(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x60(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x64(%rbp)
movq -0x58(%rbp), %rdi
callq 0x172650
movq -0x70(%rbp), %rdi
movl -0x64(%rbp), %edx
movq %rax, %rcx
leaq 0x14b220(%rip), %rsi    # 0x1cf3ff
movb $0x0, %al
callq 0x15b0d0
movq -0x60(%rbp), %rax
movl 0x10(%rax), %eax
andl $0x3f, %eax
cmpl $0xf, %eax
jne 0x84214
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
movzbl -0x29(%rbp), %r8d
callq 0x846e0
jmp 0x84683
movq -0x60(%rbp), %rax
movl 0x10(%rax), %eax
andl $0x18, %eax
cmpl $0x0, %eax
jne 0x84225
jmp 0x84281
movq -0x60(%rbp), %rax
movl 0x10(%rax), %eax
andl $0x18, %eax
cmpl $0x8, %eax
jne 0x8423e
movq -0x60(%rbp), %rax
movb $0x1, 0xc(%rax)
jmp 0x8427f
movq -0x60(%rbp), %rax
movl 0x10(%rax), %eax
andl $0x18, %eax
cmpl $0x10, %eax
jne 0x84257
movq -0x60(%rbp), %rax
movb $0x1, 0xb(%rax)
jmp 0x8427d
movq -0x8(%rbp), %rdi
leaq 0x12a012(%rip), %rsi    # 0x1ae274
movb $0x0, %al
callq 0x15bc10
movq -0x60(%rbp), %rax
movb $0x1, 0xd(%rax)
movq -0x18(%rbp), %rax
movb $0x0, (%rax)
jmp 0x84683
jmp 0x8427f
jmp 0x84281
movq -0x18(%rbp), %rax
cmpb $0x0, (%rax)
je 0x842a6
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x60(%rbp), %rcx
callq 0x84910
movq -0x18(%rbp), %rax
movb $0x0, (%rax)
movzbl -0x29(%rbp), %eax
cmpl $0xe5, %eax
jne 0x842d3
movq -0x8(%rbp), %rdi
leaq 0x129fd0(%rip), %rsi    # 0x1ae28c
movb $0x0, %al
callq 0x15b0d0
movl $0x1, -0x48(%rbp)
movq -0x60(%rbp), %rax
movb $0x3f, (%rax)
jmp 0x842e5
movzbl -0x29(%rbp), %eax
cmpl $0x5, %eax
jne 0x842e3
movq -0x60(%rbp), %rax
movb $-0x1b, (%rax)
jmp 0x842e5
movq -0x60(%rbp), %rdi
movl $0x8, %esi
callq 0x84ad0
movq %rax, %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x60(%rbp), %rdi
addq $0x8, %rdi
movl $0x3, %esi
callq 0x84ad0
movq %rax, %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x60(%rbp), %rax
movzbl 0xb(%rax), %eax
cmpl $0x0, %eax
je 0x84347
movq -0x60(%rbp), %rax
cmpq $0x1, 0x18(%rax)
jl 0x84347
movq -0x60(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x2e, %eax
jne 0x84347
movq -0x60(%rbp), %rax
movb $0x1, 0xd(%rax)
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x60(%rbp), %rax
cmpb $0x0, 0xc(%rax)
je 0x84378
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x60(%rbp), %rdx
callq 0x84b70
jmp 0x84389
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x60(%rbp), %rdx
callq 0x84c00
movq -0x8(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x60(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0x172650
movq -0x78(%rbp), %rdi
movq %rax, %rdx
leaq 0x1431d7(%rip), %rsi    # 0x1c7583
movb $0x0, %al
callq 0x15b0d0
movq -0x60(%rbp), %rax
movq 0x40(%rax), %rdi
callq 0x1717f0
cmpl $0x0, %eax
je 0x843df
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rdi
movq -0x60(%rbp), %rax
movq 0x40(%rax), %rsi
callq 0x1728d0
jmp 0x843f7
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rdi
movq -0x60(%rbp), %rax
movq 0x38(%rax), %rsi
callq 0x1728d0
movl $0x1, -0x4c(%rbp)
cmpl $0x0, -0x28(%rbp)
jne 0x84454
movq -0x10(%rbp), %rax
movzbl 0x14(%rax), %eax
cmpl $0x20, %eax
jge 0x84454
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
addq $0x14, %rsi
callq 0x165280
movl %eax, %ecx
movq -0x60(%rbp), %rax
movl %ecx, 0x14(%rax)
movq -0x60(%rbp), %rax
cmpl $0x0, 0x14(%rax)
je 0x84452
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rax
movl 0x14(%rax), %edx
leaq 0x129e4b(%rip), %rsi    # 0x1ae296
movb $0x0, %al
callq 0x15b0d0
jmp 0x84454
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
addq $0x16, %rsi
callq 0x165280
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
addq $0x18, %rsi
callq 0x165280
movq %rax, -0x38(%rbp)
movq -0x60(%rbp), %rdi
addq $0x48, %rdi
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rdx
callq 0x15d750
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rsi
addq $0x48, %rsi
leaq 0x1356c4(%rip), %rdx    # 0x1b9b72
callq 0x84ca0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
addq $0x1a, %rsi
callq 0x165280
movq %rax, %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rax
movq 0x30(%rax), %rdx
leaq 0x129dc8(%rip), %rsi    # 0x1ae2ae
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
addq $0x1c, %rsi
callq 0x165540
movq %rax, %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rax
movq 0x28(%rax), %rdx
leaq 0x11dffa(%rip), %rsi    # 0x1a251a
movb $0x0, %al
callq 0x15b0d0
movq -0x60(%rbp), %rax
cmpb $0x0, 0xc(%rax)
je 0x8453d
movq -0x60(%rbp), %rax
movq $0x0, 0x28(%rax)
cmpl $0x0, -0x48(%rbp)
je 0x84548
jmp 0x84683
cmpl $0x0, -0x28(%rbp)
je 0x845f0
movq -0x60(%rbp), %rax
cmpb $0x0, 0xb(%rax)
jne 0x845d9
movq -0x60(%rbp), %rax
cmpb $0x0, 0xd(%rax)
jne 0x845d9
movq -0x60(%rbp), %rax
movl 0x10(%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x845d9
movq -0x60(%rbp), %rax
cmpq $0x7, 0x18(%rax)
jne 0x845d9
movq -0x60(%rbp), %rax
cmpq $0x3, 0x20(%rax)
jne 0x845d9
movq -0x60(%rbp), %rdi
leaq 0x129d2b(%rip), %rsi    # 0x1ae2c1
movl $0x7, %edx
callq 0x19170
cmpl $0x0, %eax
jne 0x845d9
movq -0x60(%rbp), %rdi
addq $0x8, %rdi
leaq 0x129d15(%rip), %rsi    # 0x1ae2c9
movl $0x3, %edx
callq 0x19170
cmpl $0x0, %eax
jne 0x845d9
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x60(%rbp), %rdx
callq 0x84cf0
jmp 0x84683
movq -0x8(%rbp), %rdi
leaq 0x129ce9(%rip), %rsi    # 0x1ae2cd
movb $0x0, %al
callq 0x15b350
jmp 0x84683
movq -0x60(%rbp), %rax
cmpl $0x0, 0x14(%rax)
je 0x84619
movq -0x10(%rbp), %rax
cmpq $0x0, 0xa8(%rax)
je 0x84619
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x60(%rbp), %rdx
callq 0x84dc0
movq -0x60(%rbp), %rax
cmpb $0x0, 0xb(%rax)
jne 0x84640
movq -0x60(%rbp), %rax
cmpb $0x0, 0xd(%rax)
jne 0x84640
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x60(%rbp), %rdx
callq 0x84eb0
jmp 0x84681
movq -0x60(%rbp), %rax
movzbl 0xb(%rax), %eax
cmpl $0x0, %eax
je 0x8467f
movq -0x60(%rbp), %rax
cmpb $0x0, 0xd(%rax)
jne 0x8467f
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x60(%rbp), %rdx
callq 0x84eb0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x60(%rbp), %rdx
movl -0x24(%rbp), %ecx
addl $0x1, %ecx
callq 0x85060
jmp 0x84681
jmp 0x84683
movq -0x58(%rbp), %rdi
callq 0x171750
cmpq $0x0, -0x60(%rbp)
je 0x846ad
movq -0x60(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0x171750
movq -0x60(%rbp), %rax
movq 0x40(%rax), %rdi
callq 0x171750
cmpl $0x0, -0x4c(%rbp)
je 0x846c3
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rdi
callq 0x172850
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movl -0x44(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax)