// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/packdir.c
// Function: do_packdir_object
static int do_packdir_object(deark *c, struct pdctx_struct *d, i64 pos1,
	int level, i64 *bytes_consumed1)
{
	int saved_indent_level;
	i64 foundpos = 0;
	i64 pos = pos1;
	i64 name_len;
	i64 length_raw;
	struct pdctx_object *md = NULL;
	int retval = 0;
	int need_dirname_pop = 0;

	de_dbg_indent_save(c, &saved_indent_level);

	if(level >= MAX_NESTING_LEVEL) {
		goto done;
	}

	md = de_malloc(c, sizeof(struct pdctx_object));
	de_dbg(c, "object at %"I64_FMT, pos1);
	de_dbg_indent(c, 1);
	*bytes_consumed1 = 0;

	if(!dbuf_search_byte(c->infile, 0x00, pos, 128, &foundpos)) {
		goto done;
	}
	name_len = foundpos - pos1;
	md->name = ucstring_create(c);
	dbuf_read_to_ucstring(c->infile, pos, name_len, md->name, 0x0, DE_ENCODING_RISCOS);
	de_dbg(c, "name: \"%s\"", ucstring_getpsz_d(md->name));
	pos += name_len + 1;

	fmtutil_riscos_read_load_exec(c, c->infile, &md->rfa, pos);
	pos += 8;

	length_raw = de_getu32le_p(&pos);

	fmtutil_riscos_read_attribs_field(c, c->infile, &md->rfa, pos, 0);
	pos += 4;

	if(level==0) {
		md->object_type = 1;
	}
	else {
		md->object_type = (u32)de_getu32le_p(&pos);
		de_dbg(c, "type: %u", (unsigned int)md->object_type);
	}

	if(md->object_type==0) {
		; // regular file
	}
	else if(md->object_type==1) {
		md->is_dir = 1;
	}
	else {
		goto done; // unknown type
	}

	if(md->is_dir) {
		i64 bytes_consumed2 = 0;
		i64 i;
		int ret;

		md->num_children = length_raw;
		de_dbg(c, "number of dir entries: %"I64_FMT, md->num_children);

		if(level<=0) {
			de_ucstring *tmpstr = ucstring_create(c);
			convert_root_name(c, d, md->name, tmpstr);
			de_strarray_push(d->curpath, tmpstr);
			ucstring_destroy(tmpstr);
		}
		else {
			de_strarray_push(d->curpath, md->name);
			need_dirname_pop = 1;
		}

		md->is_compressed = 0;
		md->orig_len = 0;
		md->cmpr_len = 0;
		do_packdir_extract_file(c, d, md, pos);

		for(i=0; i<md->num_children; i++) {
			if(pos >= c->infile->len) goto done;
			ret = do_packdir_object(c, d, pos, level+1, &bytes_consumed2);
			if((!ret) || bytes_consumed2<1) goto done;
			pos += bytes_consumed2;
		}
	}
	else {
		md->orig_len = length_raw;
		de_dbg(c, "original len: %"I64_FMT, md->orig_len);

		md->cmpr_len = de_getu32le_p(&pos);
		if(md->cmpr_len==0xffffffffLL) {
			// uncompressed
			md->cmpr_len = md->orig_len;
		}
		else {
			md->is_compressed = 1;
		}
		de_dbg(c, "is compressed: %d", md->is_compressed);
		if(md->is_compressed) {
			de_dbg(c, "cmpr len: %"I64_FMT, md->cmpr_len);
		}

		do_packdir_extract_file(c, d, md, pos);

		pos += md->cmpr_len;
	}

	*bytes_consumed1 = pos - pos1;
	retval = 1;

done:
	if(!retval && c->error_count==0) {
		de_err(c, "Can't parse object at %"I64_FMT, pos1);
	}
	if(md) {
		ucstring_destroy(md->name);
		de_free(c, md);
	}
	if(need_dirname_pop) {
		de_strarray_pop(d->curpath);
	}
	de_dbg_indent_restore(c, saved_indent_level);
	return retval;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r8, %r13
movl %ecx, %ebp
movq %rdx, %r12
movq %rsi, %r14
movq %rdi, %rbx
movq $0x0, 0x30(%rsp)
movq %rdx, (%rsp)
leaq 0x1c(%rsp), %rsi
callq 0xe958c
movb $0x1, %al
cmpl $0x1f, %ebp
movq %r12, 0x20(%rsp)
jle 0xb9346
xorl %ebp, %ebp
xorl %r12d, %r12d
xorl %r15d, %r15d
jmp 0xb96a1
movl %ebp, 0x18(%rsp)
movl $0x60, %esi
movq %rbx, %rdi
callq 0xe969b
movq %rax, %r15
leaq 0x593a8(%rip), %rsi     # 0x112709
xorl %ebp, %ebp
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq %r13, 0x10(%rsp)
movq $0x0, (%r13)
movq 0x18(%rbx), %rdi
movq (%rsp), %rdx
leaq 0x30(%rsp), %r8
movl $0x80, %ecx
xorl %esi, %esi
callq 0xf0057
testl %eax, %eax
je 0xb94dd
movq %r14, 0x8(%rsp)
movq 0x30(%rsp), %r13
subq %r12, %r13
movq %rbx, %rdi
callq 0xf6870
movq %rax, 0x28(%r15)
movq 0x18(%rbx), %rdi
movq %rsp, %r14
movq (%r14), %rsi
movq %r13, %rdx
movq %rax, %rcx
xorl %r8d, %r8d
movl $0x14, %r9d
callq 0xf01b0
movq 0x28(%r15), %rdi
callq 0xf7557
leaq 0x69181(%rip), %rsi     # 0x122573
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq (%r14), %rax
leaq (%rax,%r13), %rcx
incq %rcx
movq %rcx, (%r14)
movq 0x18(%rbx), %rsi
movq %r15, %r12
addq $0x30, %r12
movq %rbx, %rdi
movq %r12, %rdx
callq 0xf978c
addq $0x8, (%r14)
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xef3a0
movq %rax, %r13
movq 0x18(%rbx), %rsi
movq (%r14), %rcx
movq %rbx, %rdi
movq %r12, %rdx
xorl %r8d, %r8d
callq 0xf9878
addq $0x4, (%r14)
movl 0x18(%rsp), %ebp
testl %ebp, %ebp
je 0xb947a
movq 0x18(%rbx), %rdi
movq %rsp, %rsi
callq 0xef3a0
movl %eax, (%r15)
leaq 0x784d9(%rip), %rsi     # 0x131945
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
jmp 0xb9481
movl $0x1, (%r15)
movl (%r15), %eax
testl %eax, %eax
movq 0x8(%rsp), %r14
je 0xb9497
cmpl $0x1, %eax
jne 0xb94db
movb $0x1, 0x4(%r15)
cmpb $0x0, 0x4(%r15)
je 0xb94e7
movq $0x0, 0x28(%rsp)
movq %r13, 0x8(%r15)
leaq 0x7e95e(%rip), %rsi     # 0x137e10
movq %rbx, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0xe9226
testl %ebp, %ebp
jle 0xb952a
movq 0x8(%r14), %rdi
movq 0x28(%r15), %rsi
callq 0xf7705
movl $0x1, %r12d
jmp 0xb9593
xorl %ebp, %ebp
xorl %r12d, %r12d
movb $0x1, %al
jmp 0xb96a1
movq %r13, 0x10(%r15)
leaq 0x7e939(%rip), %rsi     # 0x137e2b
movq %rbx, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %rsp, %rsi
callq 0xef3a0
movq %rax, 0x18(%r15)
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, %rax
jne 0xb9632
movq 0x10(%r15), %rax
movq %rax, 0x18(%r15)
jmp 0xb963a
movq %rbx, %rdi
callq 0xf6870
movq %rax, %r14
movq 0x28(%r15), %r12
cmpq $0x0, 0x10(%r12)
jle 0xb9573
xorl %r13d, %r13d
movq 0x8(%r12), %rax
movl (%rax,%r13,4), %esi
cmpl $0x3a, %esi
je 0xb9557
cmpl $0x2e, %esi
jne 0xb9561
movq %r14, %rdi
callq 0xf6883
jmp 0xb9569
movq %r14, %rdi
callq 0xf69a2
incq %r13
cmpq 0x10(%r12), %r13
jl 0xb9544
movq 0x8(%rsp), %r12
movq 0x8(%r12), %rdi
movq %r14, %rsi
callq 0xf7705
movq %r14, %rdi
movq %r12, %r14
callq 0xf6bb0
xorl %r12d, %r12d
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%r15)
movl $0x0, 0x20(%r15)
movq (%rsp), %rcx
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0xb9705
cmpq $0x0, 0x8(%r15)
setg %r13b
jle 0xb9626
movq (%rsp), %rdx
movq 0x18(%rbx), %rax
cmpq 0x18(%rax), %rdx
jge 0xb9614
incl %ebp
xorl %r14d, %r14d
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
movl %ebp, %ecx
leaq 0x28(%rsp), %r8
callq 0xb92fa
testl %eax, %eax
je 0xb9614
movq 0x28(%rsp), %rax
testq %rax, %rax
jle 0xb9614
addq %rax, (%rsp)
incq %r14
cmpq 0x8(%r15), %r14
setl %r13b
jge 0xb9626
movq (%rsp), %rdx
movq 0x18(%rbx), %rax
cmpq 0x18(%rax), %rdx
jl 0xb95d3
testb $0x1, %r13b
je 0xb9626
xorl %ebp, %ebp
movq 0x8(%rsp), %r14
jmp 0xb94e0
movq 0x8(%rsp), %r14
movq 0x10(%rsp), %r13
jmp 0xb968d
movl $0x1, 0x20(%r15)
movq 0x10(%rsp), %r13
movl 0x20(%r15), %edx
leaq 0x7e7f3(%rip), %rsi     # 0x137e3d
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
cmpl $0x0, 0x20(%r15)
je 0xb9673
movq 0x18(%r15), %rdx
leaq 0x90fcc(%rip), %rsi     # 0x14a635
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq (%rsp), %rcx
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0xb9705
movq 0x18(%r15), %rax
addq %rax, (%rsp)
movq (%rsp), %rax
subq 0x20(%rsp), %rax
movq %rax, (%r13)
movl $0x1, %ebp
xorl %eax, %eax
testb %al, %al
je 0xb96c1
cmpl $0x0, 0x48(%rbx)
jne 0xb96c1
leaq 0x7e79d(%rip), %rsi     # 0x137e4f
movq %rbx, %rdi
movq 0x20(%rsp), %rdx
xorl %eax, %eax
callq 0xe9df7
testq %r15, %r15
je 0xb96da
movq 0x28(%r15), %rdi
callq 0xf6bb0
movq %rbx, %rdi
movq %r15, %rsi
callq 0xe9752
testl %r12d, %r12d
je 0xb96e8
movq 0x8(%r14), %rdi
callq 0xf76c8
movl 0x1c(%rsp), %esi
movq %rbx, %rdi
callq 0xe9595
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq