// File: /workspace/github_c_makefile_star2_O2/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 $0x48, %rsp
movq %r8, %r12
movl %ecx, %ebp
movq %rdx, %r15
movq %rsi, %r13
movq %rdi, %rbx
andq $0x0, 0x40(%rsp)
movq %rdx, 0x8(%rsp)
leaq 0x2c(%rsp), %rsi
callq 0xdebae
cmpl $0x1f, %ebp
jle 0xb1563
xorl %r12d, %r12d
xorl %r14d, %r14d
jmp 0xb17d6
pushq $0x60
popq %rsi
movq %rbx, %rdi
callq 0xdecaf
movq %rax, %r14
leaq 0x54181(%rip), %rsi     # 0x1056f9
movq %rbx, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
andq $0x0, (%r12)
movq 0x18(%rbx), %rdi
movq 0x8(%rsp), %rdx
leaq 0x40(%rsp), %r8
movl $0x80, %ecx
xorl %esi, %esi
callq 0xe4b21
testl %eax, %eax
je 0xb16ac
movq %r12, 0x30(%rsp)
movq %r13, 0x10(%rsp)
movq 0x40(%rsp), %r13
movq %r15, 0x20(%rsp)
subq %r15, %r13
movq %rbx, %rdi
callq 0xeac74
movq %rax, 0x28(%r14)
movq 0x18(%rbx), %rdi
leaq 0x8(%rsp), %r12
movq (%r12), %rsi
pushq $0x14
popq %r9
movq %r13, %rdx
movq %rax, %rcx
xorl %r8d, %r8d
callq 0xe4c9a
movq 0x28(%r14), %rdi
callq 0xeb877
leaq 0x63fb2(%rip), %rsi     # 0x1155bb
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movq (%r12), %rax
leaq (%rax,%r13), %rcx
incq %rcx
movq %rcx, (%r12)
movq 0x18(%rbx), %rsi
movq %r14, %r15
addq $0x30, %r15
movq %rbx, %rdi
movq %r15, %rdx
callq 0xed922
addq $0x8, (%r12)
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xe40bf
movq %rax, %r13
movq 0x18(%rbx), %rsi
movq (%r12), %rcx
movq %rbx, %rdi
movq %r15, %rdx
xorl %r8d, %r8d
callq 0xeda08
addq $0x4, (%r12)
testl %ebp, %ebp
je 0xb16b4
movq 0x18(%rbx), %rdi
leaq 0x8(%rsp), %rsi
callq 0xe40bf
movl %eax, (%r14)
leaq 0x73357(%rip), %rsi     # 0x1249dd
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movl (%r14), %eax
testl %eax, %eax
movq 0x20(%rsp), %r15
je 0xb184a
cmpl $0x1, %eax
je 0xb16bb
movq 0x10(%rsp), %r13
xorl %r12d, %r12d
jmp 0xb17d6
movl $0x1, (%r14)
movb $0x1, 0x4(%r14)
andq $0x0, 0x38(%rsp)
movq %r13, 0x8(%r14)
leaq 0x797cb(%rip), %rsi     # 0x12ae9c
movq %rbx, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0xde896
testl %ebp, %ebp
jle 0xb16fe
movq 0x10(%rsp), %r13
movq 0x8(%r13), %rdi
movq 0x28(%r14), %rsi
callq 0xeb9f5
pushq $0x1
popq %rax
movq %rax, 0x18(%rsp)
jmp 0xb1761
movq %rbx, %rdi
callq 0xeac74
movq %rax, %r12
movq 0x28(%r14), %r15
xorl %r13d, %r13d
cmpq 0x10(%r15), %r13
jge 0xb173f
movq 0x8(%r15), %rax
movl (%rax,%r13,4), %esi
cmpl $0x3a, %esi
je 0xb1728
cmpl $0x2e, %esi
jne 0xb1732
movq %r12, %rdi
callq 0xeac85
jmp 0xb173a
movq %r12, %rdi
callq 0xeada5
incq %r13
jmp 0xb1710
movq 0x10(%rsp), %r13
movq 0x8(%r13), %rdi
movq %r12, %rsi
callq 0xeb9f5
movq %r12, %rdi
callq 0xeaf3c
movq $0x0, 0x18(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%r14)
andl $0x0, 0x20(%r14)
movq 0x8(%rsp), %rcx
movq %rbx, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0xb191c
incl %ebp
xorl %r15d, %r15d
leaq 0x38(%rsp), %r12
cmpq 0x8(%r14), %r15
jge 0xb1836
movq 0x8(%rsp), %rdx
movq 0x18(%rbx), %rax
cmpq 0x18(%rax), %rdx
jge 0xb17cc
movq %rbx, %rdi
movq %r13, %rsi
movl %ebp, %ecx
movq %r12, %r8
callq 0xb1522
testl %eax, %eax
je 0xb17cc
movq 0x38(%rsp), %rax
testq %rax, %rax
jle 0xb17cc
addq %rax, 0x8(%rsp)
incq %r15
jmp 0xb178b
movq 0x20(%rsp), %r15
movq 0x18(%rsp), %r12
cmpl $0x0, 0x48(%rbx)
jne 0xb17f0
leaq 0x796f8(%rip), %rsi     # 0x12aedb
movq %rbx, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0xdf2a5
xorl %ebp, %ebp
testq %r14, %r14
je 0xb180b
movq 0x28(%r14), %rdi
callq 0xeaf3c
movq %rbx, %rdi
movq %r14, %rsi
callq 0xded32
testl %r12d, %r12d
je 0xb1819
movq 0x8(%r13), %rdi
callq 0xeb9bd
movl 0x2c(%rsp), %esi
movq %rbx, %rdi
callq 0xdebb7
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %rax
movq 0x20(%rsp), %r15
movq 0x30(%rsp), %r12
jmp 0xb1908
cmpb $0x0, 0x4(%r14)
movq 0x30(%rsp), %r12
jne 0xb16c0
movq %r13, 0x10(%r14)
leaq 0x79652(%rip), %rsi     # 0x12aeb7
movq %rbx, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
leaq 0x8(%rsp), %rsi
callq 0xe40bf
movq %rax, 0x18(%r14)
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, %rax
jne 0xb18a1
movq 0x10(%r14), %rax
movq %rax, 0x18(%r14)
movl 0x20(%r14), %edx
movq 0x10(%rsp), %r13
jmp 0xb18b1
movl $0x1, 0x20(%r14)
movq 0x10(%rsp), %r13
pushq $0x1
popq %rdx
leaq 0x79611(%rip), %rsi     # 0x12aec9
movq $0x0, 0x18(%rsp)
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
cmpl $0x0, 0x20(%r14)
je 0xb18e7
movq 0x18(%r14), %rdx
leaq 0x8be38(%rip), %rsi     # 0x13d715
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x8(%rsp), %rcx
movq %rbx, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0xb191c
movq 0x8(%rsp), %rax
addq 0x18(%r14), %rax
movq %rax, 0x8(%rsp)
subq %r15, %rax
movq %rax, (%r12)
pushq $0x1
popq %rbp
movq 0x18(%rsp), %r12
jmp 0xb17f7