// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/iso9660.c
// Function: do_directory
static void do_directory(deark *c, lctx *d, i64 pos1, i64 len, int nesting_level)
{
	struct dir_record *dr = NULL;
	i64 pos = pos1;
	int saved_indent_level;
	int idx = 0;

	de_dbg_indent_save(c, &saved_indent_level);
	if(pos1<=0) goto done;

	if((len>=0x08000000LL) & (d->dirsize_hack_state!=0)) {
		// A few CDs seem to have garbage in the high bits of the directory length fields.
		// Examples:
		//  https://archive.org/details/NIghtsOwl
		//  https://archive.org/details/SDN1__793

		if(d->dirsize_hack_state<0) {
			de_warn(c, "Possibly corrupt directory length found (0x%08x). Enabling workaround.", (UI)len);
			d->dirsize_hack_state = 1;
		}
		len &= 0x07ffffffLL;
	}

	de_dbg(c, "directory at %"I64_FMT", len=%"I64_FMT, pos1, len);
	de_dbg_indent(c, 1);

	if(!de_inthashtable_add_item(c, d->dirs_seen, pos1, NULL)) {
		de_warn(c, "Duplicate directory or loop detected (@%"I64_FMT")", pos1);
		goto done;
	}

	if(nesting_level>MAX_NESTING_LEVEL) {
		de_err(c, "Maximum directory nesting level exceeded");
		goto done;
	}

	if(pos1+len > c->infile->len) {
		de_warn(c, "Directory at %"I64_FMT" goes beyond end of file (size=%"I64_FMT")",
			pos1, len);
	}

	while(1) {
		int ret;

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

		// Peek at the first byte of the dir record (the length)
		if(pos%d->secsize != 0) {
			if(de_getbyte(pos)==0) {
				// No more dir records in this sector; advance to the next sector
				pos = de_pad_to_n(pos, d->secsize);
			}

			if(pos >= pos1+len) break;
			if(pos >= c->infile->len) break;
		}

		de_dbg(c, "file/dir record at %"I64_FMT" (item[%d] in dir@%"I64_FMT")", pos,
			idx, pos1);
		dr = de_malloc(c, sizeof(struct dir_record));
		de_dbg_indent(c, 1);
		ret = do_directory_record(c, d, pos, dr, nesting_level);
		de_dbg_indent(c, -1);
		if(!ret) break;
		if(dr->len_dir_rec<1) break;

		pos += dr->len_dir_rec; // + ext_len??
		free_dir_record(c, dr);
		dr = NULL;
		idx++;
	}

done:
	if(dr) free_dir_record(c, dr);
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x38(%rbp)
movl $0x0, -0x40(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x3c(%rbp), %rsi
callq 0x15b580
cmpq $0x0, -0x18(%rbp)
jg 0xccf4c
jmp 0xcd1ae
cmpq $0x8000000, -0x20(%rbp) # imm = 0x8000000
setge %al
andb $0x1, %al
movzbl %al, %eax
movq -0x10(%rbp), %rcx
cmpl $0x0, 0xc(%rcx)
setne %cl
andb $0x1, %cl
movzbl %cl, %ecx
andl %ecx, %eax
cmpl $0x0, %eax
je 0xccfaf
movq -0x10(%rbp), %rax
cmpl $0x0, 0xc(%rax)
jge 0xccfa1
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movl %eax, %edx
leaq 0xecca9(%rip), %rsi     # 0x1b9c38
movb $0x0, %al
callq 0x15bc10
movq -0x10(%rbp), %rax
movl $0x1, 0xc(%rax)
movq -0x20(%rbp), %rax
andq $0x7ffffff, %rax        # imm = 0x7FFFFFF
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
leaq 0xeccbd(%rip), %rsi     # 0x1b9c7f
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rsi
movq -0x18(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x15e810
cmpl $0x0, %eax
jne 0xcd010
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rdx
leaq 0xecc95(%rip), %rsi     # 0x1b9c99
movb $0x0, %al
callq 0x15bc10
jmp 0xcd1ae
cmpl $0x20, -0x24(%rbp)
jle 0xcd02d
movq -0x8(%rbp), %rdi
leaq 0xecca4(%rip), %rsi     # 0x1b9cc5
movb $0x0, %al
callq 0x15bfb0
jmp 0xcd1ae
movq -0x18(%rbp), %rax
addq -0x20(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jle 0xcd05d
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
leaq 0xecc98(%rip), %rsi     # 0x1b9cee
movb $0x0, %al
callq 0x15bc10
jmp 0xcd05f
movq -0x38(%rbp), %rax
movq -0x18(%rbp), %rcx
addq -0x20(%rbp), %rcx
cmpq %rcx, %rax
jl 0xcd075
jmp 0xcd1ac
movq -0x38(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jl 0xcd08c
jmp 0xcd1ac
movq -0x38(%rbp), %rax
movq -0x10(%rbp), %rcx
cqto
idivq 0x18(%rcx)
cmpq $0x0, %rdx
je 0xcd0fd
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
callq 0x165060
movzbl %al, %eax
cmpl $0x0, %eax
jne 0xcd0ce
movq -0x38(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
callq 0x15ce90
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x18(%rbp), %rcx
addq -0x20(%rbp), %rcx
cmpq %rcx, %rax
jl 0xcd0e4
jmp 0xcd1ac
movq -0x38(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jl 0xcd0fb
jmp 0xcd1ac
jmp 0xcd0fd
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rdx
movl -0x40(%rbp), %ecx
movq -0x18(%rbp), %r8
leaq 0xecc0f(%rip), %rsi     # 0x1b9d22
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0xb0, %esi
callq 0x15b780
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x30(%rbp), %rcx
movl -0x24(%rbp), %r8d
callq 0xce4d0
movl %eax, -0x44(%rbp)
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
cmpl $0x0, -0x44(%rbp)
jne 0xcd16c
jmp 0xcd1ac
movq -0x30(%rbp), %rax
cmpq $0x1, 0x10(%rax)
jge 0xcd179
jmp 0xcd1ac
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0xceca0
movq $0x0, -0x30(%rbp)
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0xcd05f
jmp 0xcd1ae
cmpq $0x0, -0x30(%rbp)
je 0xcd1c2
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0xceca0
movq -0x8(%rbp), %rdi
movl -0x3c(%rbp), %esi
callq 0x15b5a0
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)