// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/exe.c
// Function: do_lx_rsrc
static void do_lx_rsrc(deark *c, lctx *d,
	i64 obj_num, i64 rsrc_offset, i64 rsrc_size, i64 rsrc_type)
{
	i64 lpos;
	i64 vsize;
	i64 reloc_base_addr;
	i64 flags;
	i64 page_table_index;
	i64 page_table_entries;
	i64 rsrc_offset_real;
	i64 pg_data_offset_raw;
	const char *ext;
	//i64 data_size;

	if(obj_num<1 || obj_num>d->lx_object_tbl_entries) {
		de_err(c, "Invalid object number (%d).", (int)obj_num);
		return;
	}

	// Read the Object Table
	lpos = d->lx_object_tbl_offset + 24*(obj_num-1);
	de_dbg(c, "LX object table entry at %"I64_FMT, lpos);

	vsize = de_getu32le(lpos);
	reloc_base_addr = de_getu32le(lpos+4);
	flags = de_getu32le(lpos+8);
	page_table_index = de_getu32le(lpos+12);
	page_table_entries = de_getu32le(lpos+16);
	de_dbg(c, "object #%d: vsize=%"I64_FMT" raddr=%"I64_FMT" flags=0x%x pti=%d pte=%d",
		(int)obj_num,
		vsize, reloc_base_addr, (UI)flags, (int)page_table_index,
		(int)page_table_entries);

	if(page_table_index<1) return;

	// Now read the Object Page table
	lpos = d->lx_object_page_tbl_offset + 8*(page_table_index-1);
	de_dbg(c, "LX page table entry at %"I64_FMT, lpos);

	pg_data_offset_raw = de_getu32le(lpos);
	//data_size = de_getu16le(lpos+4);

	rsrc_offset_real = pg_data_offset_raw;
	if(d->lx_page_offset_shift > 0 ) {
		rsrc_offset_real <<= (UI)d->lx_page_offset_shift;
	}
	rsrc_offset_real += d->lx_data_pages_offset;
	rsrc_offset_real += rsrc_offset;
	de_dbg(c, "resource offset: %"I64_FMT, rsrc_offset_real);

	switch(rsrc_type) {
		// TODO: Support other types of resources.
	case 1: // Icon or cursor (?)
	case 2: // Bitmap (?)
		ext = identify_lx_rsrc(c, d, rsrc_offset_real, rsrc_size);
		if(!ext) break;
		// TODO: This assumes the resource is stored contiguously in the file, but
		// for all I know that isn't always the case.

		// Unlike in NE and PE format, it seems that image resources in LX files
		// include the BITMAPFILEHEADER. That makes it easy.
		if(d->extract_std_resources) {
			dbuf_create_file_from_slice(c->infile, rsrc_offset_real, rsrc_size,
				ext, NULL, 0);
		}
		break;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
cmpq $0x1, -0x18(%rbp)
jl 0x6318b
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0xa8(%rcx), %rax
jle 0x631a8
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl %eax, %edx
leaq 0x146086(%rip), %rsi    # 0x1a9222
movb $0x0, %al
callq 0x15bfb0
jmp 0x633ad
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rax
movq -0x18(%rbp), %rcx
subq $0x1, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rdx
leaq 0x146069(%rip), %rsi    # 0x1a923e
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
callq 0x165540
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
addq $0x4, %rsi
callq 0x165540
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
addq $0x8, %rsi
callq 0x165540
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
addq $0xc, %rsi
callq 0x165540
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
addq $0x10, %rsi
callq 0x165540
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl %eax, %edx
movq -0x40(%rbp), %rcx
movq -0x48(%rbp), %r8
movq -0x50(%rbp), %rax
movl %eax, %r9d
movq -0x58(%rbp), %rax
movl %eax, %r10d
movq -0x60(%rbp), %rax
leaq 0x145fdb(%rip), %rsi    # 0x1a925b
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
movb $0x0, %al
callq 0x15b0d0
cmpq $0x1, -0x58(%rbp)
jge 0x6329b
jmp 0x633ad
movq -0x10(%rbp), %rax
movq 0xb0(%rax), %rax
movq -0x58(%rbp), %rcx
subq $0x1, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rdx
leaq 0x145fcc(%rip), %rsi    # 0x1a9294
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
callq 0x165540
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x98(%rax)
jle 0x63314
movq -0x10(%rbp), %rax
movq 0x98(%rax), %rax
movl %eax, %ecx
movq -0x68(%rbp), %rax
movl %ecx, %ecx
shlq %cl, %rax
movq %rax, -0x68(%rbp)
movq -0x10(%rbp), %rax
movq 0xc8(%rax), %rcx
movq -0x68(%rbp), %rax
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movq -0x20(%rbp), %rcx
movq -0x68(%rbp), %rax
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rdi
movq -0x68(%rbp), %rdx
leaq 0x145f67(%rip), %rsi    # 0x1a92af
xorl %eax, %eax
callq 0x15b0d0
movq -0x30(%rbp), %rax
decq %rax
subq $0x1, %rax
ja 0x633ad
jmp 0x6335e
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x68(%rbp), %rdx
movq -0x28(%rbp), %rcx
callq 0x633c0
movq %rax, -0x78(%rbp)
cmpq $0x0, -0x78(%rbp)
jne 0x63380
jmp 0x633ad
movq -0x10(%rbp), %rax
cmpb $0x0, 0x4(%rax)
je 0x633ab
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x68(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x78(%rbp), %rcx
xorl %eax, %eax
movl %eax, %r8d
xorl %r9d, %r9d
callq 0x166be0
jmp 0x633ad
addq $0x90, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)