// File: /workspace/github_c_makefile_star2_O3/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, %r15
movl %ecx, %ebp
movq %rdx, %r14
movq %rsi, %r13
movq %rdi, %rbx
movq $0x0, 0x30(%rsp)
movq %rdx, (%rsp)
leaq 0x1c(%rsp), %rsi
callq 0xe8a1c
cmpl $0x1f, %ebp
jle 0xb8763
xorl %r15d, %r15d
xorl %r12d, %r12d
jmp 0xb8a04
movl $0x60, %esi
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, %r12
leaq 0x59f9f(%rip), %rsi     # 0x112719
movq %rbx, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq $0x0, (%r15)
movq 0x18(%rbx), %rdi
movq (%rsp), %rdx
leaq 0x30(%rsp), %r8
movl $0x80, %ecx
xorl %esi, %esi
callq 0xefa77
testl %eax, %eax
je 0xb88ad
movq %r15, 0x20(%rsp)
movq %r13, 0x8(%rsp)
movq 0x30(%rsp), %r13
movq %r14, 0x10(%rsp)
subq %r14, %r13
movq %rbx, %rdi
callq 0xf6410
movq %rax, 0x28(%r12)
movq 0x18(%rbx), %rdi
movq %rsp, %r14
movq (%r14), %rsi
movq %r13, %rdx
movq %rax, %rcx
xorl %r8d, %r8d
movl $0x14, %r9d
callq 0xefbd7
movq 0x28(%r12), %rdi
callq 0xf70f6
leaq 0x69f6c(%rip), %rsi     # 0x12277b
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq (%r14), %rax
leaq (%rax,%r13), %rcx
incq %rcx
movq %rcx, (%r14)
movq 0x18(%rbx), %rsi
movq %r12, %r15
addq $0x30, %r15
movq %rbx, %rdi
movq %r15, %rdx
callq 0xf9284
addq $0x8, (%r14)
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xeed83
movq %rax, %r13
movq 0x18(%rbx), %rsi
movq (%r14), %rcx
movq %rbx, %rdi
movq %r15, %rdx
xorl %r8d, %r8d
callq 0xf9370
addq $0x4, (%r14)
testl %ebp, %ebp
je 0xb88b5
movq 0x18(%rbx), %rdi
movq %rsp, %rsi
callq 0xeed83
movl %eax, (%r12)
leaq 0x79443(%rip), %rsi     # 0x131cc9
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movl (%r12), %eax
testl %eax, %eax
movq 0x10(%rsp), %r14
je 0xb8a65
cmpl $0x1, %eax
je 0xb88bd
movq 0x8(%rsp), %r13
xorl %r15d, %r15d
jmp 0xb8a04
movl $0x1, (%r12)
movb $0x1, 0x4(%r12)
movq $0x0, 0x28(%rsp)
movq %r13, 0x8(%r12)
leaq 0x7f8c4(%rip), %rsi     # 0x13819c
movq %rbx, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0xe8617
testl %ebp, %ebp
jle 0xb8903
movq 0x8(%rsp), %r13
movq 0x8(%r13), %rdi
movq 0x28(%r12), %rsi
callq 0xf72a4
movl $0x1, %eax
jmp 0xb8965
movq %rbx, %rdi
callq 0xf6410
movq %rax, %r14
movq 0x28(%r12), %r15
cmpq $0x0, 0x10(%r15)
jle 0xb894a
xorl %r13d, %r13d
movq 0x8(%r15), %rax
movl (%rax,%r13,4), %esi
cmpl $0x3a, %esi
je 0xb892f
cmpl $0x2e, %esi
jne 0xb8939
movq %r14, %rdi
callq 0xf6423
jmp 0xb8941
movq %r14, %rdi
callq 0xf6542
incq %r13
cmpq 0x10(%r15), %r13
jl 0xb891d
movq 0x8(%rsp), %r13
movq 0x8(%r13), %rdi
movq %r14, %rsi
callq 0xf72a4
movq %r14, %rdi
callq 0xf6753
xorl %eax, %eax
movq %rax, 0x8(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%r12)
movl $0x0, 0x20(%r12)
movq (%rsp), %rcx
movq %rbx, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0xb8b2a
cmpq $0x0, 0x8(%r12)
movq (%rsp), %rdx
jle 0xb89de
incl %ebp
xorl %r15d, %r15d
leaq 0x28(%rsp), %r14
movq 0x18(%rbx), %rax
cmpq 0x18(%rax), %rdx
jge 0xb89fa
movq %rbx, %rdi
movq %r13, %rsi
movl %ebp, %ecx
movq %r14, %r8
callq 0xb8720
testl %eax, %eax
je 0xb89fa
movq 0x28(%rsp), %rdx
testq %rdx, %rdx
jle 0xb89fa
addq (%rsp), %rdx
movq %rdx, (%rsp)
incq %r15
cmpq 0x8(%r12), %r15
jl 0xb89a4
movq 0x10(%rsp), %r14
movq 0x20(%rsp), %r15
subq %r14, %rdx
movq %rdx, (%r15)
movl $0x1, %ebp
movq 0x8(%rsp), %r15
jmp 0xb8a25
movq 0x10(%rsp), %r14
movq 0x8(%rsp), %r15
cmpl $0x0, 0x48(%rbx)
jne 0xb8a1e
leaq 0x7f7ca(%rip), %rsi     # 0x1381db
movq %rbx, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0xe9295
xorl %ebp, %ebp
testq %r12, %r12
je 0xb8a3a
movq 0x28(%r12), %rdi
callq 0xf6753
movq %rbx, %rdi
movq %r12, %rsi
callq 0xe8be2
testl %r15d, %r15d
je 0xb8a48
movq 0x8(%r13), %rdi
callq 0xf7267
movl 0x1c(%rsp), %esi
movq %rbx, %rdi
callq 0xe8a25
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpb $0x0, 0x4(%r12)
movq 0x20(%rsp), %r15
jne 0xb88c3
movq %r13, 0x10(%r12)
leaq 0x7f735(%rip), %rsi     # 0x1381b7
movq %rbx, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq %rsp, %rsi
callq 0xeed83
movq %rax, 0x18(%r12)
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, %rax
jne 0xb8abb
movq 0x10(%r12), %rax
movq %rax, 0x18(%r12)
movl 0x20(%r12), %edx
jmp 0xb8ac9
movl $0x1, 0x20(%r12)
movl $0x1, %edx
movq 0x8(%rsp), %r13
leaq 0x7f6f4(%rip), %rsi     # 0x1381c9
movq $0x0, 0x8(%rsp)
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
cmpl $0x0, 0x20(%r12)
je 0xb8b06
movq 0x18(%r12), %rdx
leaq 0x920e9(%rip), %rsi     # 0x14abe5
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq (%rsp), %rcx
movq %rbx, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0xb8b2a
movq (%rsp), %rdx
addq 0x18(%r12), %rdx
movq %rdx, (%rsp)
jmp 0xb89e8