// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/exe.c
// Function: do_pe_resource_node
static void do_pe_resource_node(deark *c, lctx *d, i64 rel_pos, int level)
{
	u32 name_or_id;
	i64 next_offset;
	int has_name, is_branch_node;
	int saved_indent_level;

	de_dbg_indent_save(c, &saved_indent_level);

	d->rsrc_item_count++;
	if(d->rsrc_item_count>MAX_RESOURCES) {
		de_err(c, "Too many resources");
		d->rsrc_errflag = 1;
		goto done;
	}

	has_name = 0;
	is_branch_node = 0;

	name_or_id = (u32)de_getu32le(d->pe_cur_base_addr+rel_pos);
	if(name_or_id & 0x80000000U) {
		has_name = 1;
		name_or_id -= 0x80000000U;
	}
	next_offset = de_getu32le(d->pe_cur_base_addr+rel_pos+4);
	if(next_offset & 0x80000000U) {
		is_branch_node = 1;
		next_offset -= 0x80000000U;
	}

	if(level==1) {
		d->cur_rsrc_type = name_or_id;
		d->cur_rsrc_type_info = get_rsrc_type_info((u32)d->cur_rsrc_type);
	}

	de_dbg(c, "level %d node at %"I64_FMT"(%"I64_FMT") id=%d next-offset=%"I64_FMT" is-named=%d is-branch=%d",
		level, (i64)(d->pe_cur_base_addr+rel_pos), rel_pos,
		(int)name_or_id, next_offset, has_name, is_branch_node);
	if(d->pe_cur_base_addr+rel_pos > c->infile->len) {
		d->rsrc_errflag = 1;
		goto done;
	}
	de_dbg_indent(c, 1);

	if(!ne_pe_resource_type_is_supported(c, d, d->cur_rsrc_type)) {
		const char *rsrcname;
		if(d->cur_rsrc_type_info && d->cur_rsrc_type_info->name)
			rsrcname = d->cur_rsrc_type_info->name;
		else
			rsrcname = "?";

		// We don't support this type of resource, so don't go down this path.
		de_dbg(c, "resource type %d (%s) not supported", (int)d->cur_rsrc_type, rsrcname);
		goto done;
	}

	// If a resource has a name (at level 2), keep track of it so we can
	// use it in the filename.
	if(level==2) {
		if(has_name) {
			d->pe_cur_name_offset = d->pe_cur_section_data_offset + name_or_id;
			de_dbg(c, "resource name at %"I64_FMT, d->pe_cur_name_offset);
		}
		else {
			d->pe_cur_name_offset = 0;
		}
	}
	else if(level<2) {
		d->pe_cur_name_offset = 0;
	}

	// If high bit is 1, we need to go deeper.
	if(is_branch_node) {
		do_pe_resource_dir_table(c, d, next_offset, level+1);
	}
	else {
		do_pe_resource_data_entry(c, d, next_offset);
	}

done:
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x34(%rbp), %rsi
callq 0x15b580
movq -0x10(%rbp), %rax
movq 0x118(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x118(%rax)
movq -0x10(%rbp), %rax
cmpq $0x2710, 0x118(%rax) # imm = 0x2710
jle 0x61fca
movq -0x8(%rbp), %rdi
leaq 0x146e33(%rip), %rsi    # 0x1a8de9
movb $0x0, %al
callq 0x15bfb0
movq -0x10(%rbp), %rax
movb $0x1, 0x6(%rax)
jmp 0x62238
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0xe8(%rax), %rsi
addq -0x18(%rbp), %rsi
callq 0x165540
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
andl $0x80000000, %eax       # imm = 0x80000000
cmpl $0x0, %eax
je 0x62016
movl $0x1, -0x2c(%rbp)
movl -0x20(%rbp), %eax
subl $0x80000000, %eax       # imm = 0x80000000
movl %eax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0xe8(%rax), %rsi
addq -0x18(%rbp), %rsi
addq $0x4, %rsi
callq 0x165540
movq %rax, -0x28(%rbp)
movabsq $0x80000000, %rax       # imm = 0x80000000
andq -0x28(%rbp), %rax
cmpq $0x0, %rax
je 0x6206a
movl $0x1, -0x30(%rbp)
movq -0x28(%rbp), %rax
movabsq $0x80000000, %rcx       # imm = 0x80000000
subq %rcx, %rax
movq %rax, -0x28(%rbp)
cmpl $0x1, -0x1c(%rbp)
jne 0x6209a
movl -0x20(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x108(%rax)
movq -0x10(%rbp), %rax
movl 0x108(%rax), %edi
callq 0x62250
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x110(%rax)
movq -0x8(%rbp), %rdi
movl -0x1c(%rbp), %edx
movq -0x10(%rbp), %rax
movq 0xe8(%rax), %rcx
addq -0x18(%rbp), %rcx
movq -0x18(%rbp), %r8
movl -0x20(%rbp), %r9d
movq -0x28(%rbp), %r11
movl -0x2c(%rbp), %r10d
movl -0x30(%rbp), %eax
leaq 0x146d32(%rip), %rsi    # 0x1a8dfc
movq %r11, (%rsp)
movl %r10d, 0x8(%rsp)
movl %eax, 0x10(%rsp)
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movq 0xe8(%rax), %rax
addq -0x18(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jle 0x62108
movq -0x10(%rbp), %rax
movb $0x1, 0x6(%rax)
jmp 0x62238
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x10(%rbp), %rax
movl 0x108(%rax), %edx
callq 0x622b0
cmpl $0x0, %eax
jne 0x62197
movq -0x10(%rbp), %rax
cmpq $0x0, 0x110(%rax)
je 0x62167
movq -0x10(%rbp), %rax
movq 0x110(%rax), %rax
cmpq $0x0, 0x8(%rax)
je 0x62167
movq -0x10(%rbp), %rax
movq 0x110(%rax), %rax
movq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
jmp 0x62172
leaq 0x155abf(%rip), %rax    # 0x1b7c2d
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x108(%rax), %edx
movq -0x40(%rbp), %rcx
leaq 0x146cba(%rip), %rsi    # 0x1a8e45
movb $0x0, %al
callq 0x15b0d0
jmp 0x62238
cmpl $0x2, -0x1c(%rbp)
jne 0x621ef
cmpl $0x0, -0x2c(%rbp)
je 0x621de
movq -0x10(%rbp), %rax
movq 0xf8(%rax), %rcx
movl -0x20(%rbp), %eax
addq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x100(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x100(%rax), %rdx
leaq 0x146c94(%rip), %rsi    # 0x1a8e69
movb $0x0, %al
callq 0x15b0d0
jmp 0x621ed
movq -0x10(%rbp), %rax
movq $0x0, 0x100(%rax)
jmp 0x62206
cmpl $0x2, -0x1c(%rbp)
jge 0x62204
movq -0x10(%rbp), %rax
movq $0x0, 0x100(%rax)
jmp 0x62206
cmpl $0x0, -0x30(%rbp)
je 0x62225
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x28(%rbp), %rdx
movl -0x1c(%rbp), %ecx
addl $0x1, %ecx
callq 0x61e20
jmp 0x62236
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x62300
jmp 0x62238
movq -0x8(%rbp), %rdi
movl -0x34(%rbp), %esi
callq 0x15b5a0
addq $0x60, %rsp
popq %rbp
retq
nopw (%rax,%rax)