// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/adex.c
// Function: de_run_adex
static void de_run_adex(deark *c, de_module_params *mparams)
{
	struct adexctx *d = NULL;
	i64 pos;
	de_bitmap *img = NULL;
	dbuf *unc_pixels = NULL;
	int need_errmsg = 0;

	d = de_malloc(c, sizeof(struct adexctx));
	pos = 4;
	d->cmpr_meth = de_getbyte_p(&pos);
	de_dbg(c, "cmpr meth: %u", (UI)d->cmpr_meth);
	d->bpp = de_getbyte_p(&pos);
	de_dbg(c, "bits/pixel: %u", (UI)d->bpp);
	d->npwidth = de_getu16le_p(&pos);
	d->h = de_getu16le_p(&pos);
	de_dbg_dimensions(c, d->npwidth, d->h);
	d->palentries = de_getu16le_p(&pos);
	de_dbg(c, "palette entries: %u", (UI)d->palentries);

	if(d->palentries>256) { need_errmsg = 1; goto done; }
	if(d->palentries==0) {
		// This is likely legal, but we'd have to get the palette from a different file
		need_errmsg = 1;
		goto done;
	}
	de_read_simple_palette(c, c->infile, pos, d->palentries, 3, d->pal, 256,
		DE_RDPALTYPE_24BIT, 0);
	pos += 3*d->palentries;
	pos += 2; // After the palette are two bytes of unknown purpose.

	de_dbg(c, "image data at %"I64_FMT, pos);

	if(d->cmpr_meth>1) { need_errmsg = 1; goto done; }
	if(d->bpp!=4 && d->bpp!=8) { need_errmsg = 1; goto done; }
	if(!de_good_image_dimensions(c, d->npwidth, d->h)) goto done;

	d->rowspan = de_pad_to_n(d->npwidth*d->bpp, 16)/8;
	d->pdwidth = (d->rowspan*d->bpp)/2;
	d->expected_imgsize = d->h * d->rowspan;

	if(d->cmpr_meth==1) {
		unc_pixels = dbuf_create_membuf(c, d->expected_imgsize, 0x1);
		dbuf_enable_wbuffer(unc_pixels);
		if(!adex_decompress_rle(c, d, pos, unc_pixels)) goto done;
		dbuf_flush(unc_pixels);
	}
	else {
		unc_pixels = dbuf_open_input_subfile(c->infile, pos, c->infile->len - pos);
	}

	img = de_bitmap_create2(c, d->npwidth, d->pdwidth, d->h, 3);
	if(d->bpp==4) {
		convert_image_pal4_lsb(unc_pixels, 0, d->rowspan, d->pal, img);
	}
	else {
		de_convert_image_paletted(unc_pixels, 0, d->bpp, d->rowspan, d->pal, img, 0);
	}

	de_bitmap_write_to_file(img, NULL, DE_CREATEFLAG_FLIP_IMAGE);
done:
	de_bitmap_destroy(img);
	dbuf_close(unc_pixels);
	if(need_errmsg) {
		de_err(c, "Bad or unsupported image type");
	}
	de_free(c, d);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %r15
movl $0x438, %esi            # imm = 0x438
callq 0xe969b
movq %rax, %r14
movq %rax, %rbx
leaq 0x18(%rsp), %r12
movq $0x4, (%r12)
movq 0x18(%r15), %rdi
movq %r12, %rsi
callq 0xef14d
movb %al, (%r14)
movzbl %al, %edx
leaq 0xde866(%rip), %rsi     # 0x119009
movq %r15, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x18(%r15), %rdi
movq %r12, %rsi
callq 0xef14d
movb %al, 0x1(%r14)
movzbl %al, %edx
leaq 0xde850(%rip), %rsi     # 0x119017
movq %r15, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x18(%r15), %rdi
movq %r12, %rsi
callq 0xef1ed
movq %rax, 0x8(%r14)
movq 0x18(%r15), %rdi
movq %r12, %rsi
callq 0xef1ed
movq %rax, 0x18(%r14)
movq 0x8(%r14), %rsi
movq %r15, %rdi
movq %rax, %rdx
callq 0xe9b05
movq 0x18(%r15), %rdi
movq %r12, %rsi
callq 0xef1ed
movq %rax, 0x30(%r14)
leaq 0xde80f(%rip), %rsi     # 0x119026
movq %r15, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x30(%r14), %rcx
cmpq $0x101, %rcx            # imm = 0x101
setge %al
testq %rcx, %rcx
sete %dl
orb %al, %dl
jne 0x3aa34
movq 0x18(%r15), %rsi
movq 0x18(%rsp), %rdx
addq $0x38, %r14
subq $0x8, %rsp
movl $0x3, %r8d
movq %r15, %rdi
movq %r14, %r9
pushq $0x0
pushq $0x1
pushq $0x100                  # imm = 0x100
callq 0xf32f2
addq $0x20, %rsp
movq 0x30(%rbx), %rax
leaq (%rax,%rax,2), %rax
movq 0x18(%rsp), %rcx
leaq 0x2(%rcx,%rax), %rdx
movq %rdx, 0x18(%rsp)
leaq 0x100493(%rip), %rsi    # 0x13ad1f
movq %r15, %rdi
xorl %eax, %eax
callq 0xe9226
cmpb $0x1, (%rbx)
ja 0x3aa34
movzbl 0x1(%rbx), %eax
cmpl $0x8, %eax
je 0x3a8b1
cmpl $0x4, %eax
jne 0x3aa34
movq 0x8(%rbx), %rsi
movq 0x18(%rbx), %rdx
movq %r15, %rdi
callq 0xf241d
testl %eax, %eax
je 0x3aa6d
movzbl 0x1(%rbx), %edi
imulq 0x8(%rbx), %rdi
movl $0x10, %esi
callq 0xea80b
leaq 0x7(%rax), %rsi
testq %rax, %rax
cmovnsq %rax, %rsi
sarq $0x3, %rsi
movq %rsi, 0x20(%rbx)
movzbl 0x1(%rbx), %eax
imulq %rsi, %rax
movq %rax, %rcx
shrq $0x3f, %rcx
addq %rax, %rcx
sarq %rcx
movq %rcx, 0x10(%rbx)
imulq 0x18(%rbx), %rsi
movq %rsi, 0x28(%rbx)
cmpb $0x1, (%rbx)
movq %rbx, 0x8(%rsp)
movq %r14, 0x28(%rsp)
jne 0x3aa77
movq %r15, %rdi
movl $0x1, %edx
callq 0xf0faf
movq %rax, %r12
movq %rax, %rdi
callq 0xf0e3f
movq 0x18(%rsp), %rax
leaq 0x20(%rsp), %r13
movq %rax, (%r13)
xorl %r14d, %r14d
leaq 0x36(%rsp), %rbp
xorl %edx, %edx
movl $0x3, %eax
cmpq 0x28(%rbx), %r14
jge 0x3aa10
movq 0x20(%rsp), %rcx
movq 0x18(%r15), %rdi
movl $0x4, %eax
cmpq 0x18(%rdi), %rcx
jg 0x3aa10
cmpq 0x20(%rbx), %rdx
jg 0x3aa10
movq %rdx, 0x10(%rsp)
movq %r13, %rsi
callq 0xef1ed
movq %rax, %rbx
movq 0x18(%r15), %rdi
movq 0x20(%rsp), %rdx
movl $0x2, %ecx
movq %rbp, %rsi
callq 0xeeee8
addq $0x2, 0x20(%rsp)
testq %rbx, %rbx
je 0x3a9f1
movq %r12, %r13
movq %rbx, %r12
jle 0x3a9d3
movl $0x2, %edx
movq %r13, %rdi
movq %rbp, %rsi
callq 0xefb82
decq %r12
jne 0x3a9be
leaq (%r14,%rbx,2), %r14
movq 0x10(%rsp), %rdx
leaq (%rdx,%rbx,2), %rdx
xorl %eax, %eax
movq 0x8(%rsp), %rbx
movq %r13, %r12
leaq 0x20(%rsp), %r13
jmp 0x3aa10
xorl %eax, %eax
movq 0x8(%rsp), %rbx
movq 0x10(%rsp), %rdx
cmpq 0x20(%rbx), %rdx
setne %al
movl $0x0, %ecx
cmoveq %rcx, %rdx
shll $0x2, %eax
testl %eax, %eax
je 0x3a954
cmpl $0x4, %eax
je 0x3aa91
cmpl $0x3, %eax
movq 0x28(%rsp), %r14
jne 0x3aab1
movl $0x1, %ebx
xorl %eax, %eax
jmp 0x3aa9a
xorl %edi, %edi
callq 0xf28d4
xorl %edi, %edi
callq 0xf0b13
leaq 0xde5f1(%rip), %rsi     # 0x11903a
movq %r15, %rdi
xorl %eax, %eax
callq 0xe9df7
movq %r15, %rdi
movq %rbx, %rsi
callq 0xe9752
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebp, %ebp
xorl %r12d, %r12d
jmp 0x3abef
movq 0x18(%r15), %rdi
movq 0x18(%rsp), %rsi
movq 0x18(%rdi), %rdx
subq %rsi, %rdx
callq 0xf1988
movq %rax, %r12
jmp 0x3aac6
xorl %ebx, %ebx
movb $0x1, %al
movq 0x28(%rsp), %r14
testb %al, %al
je 0x3aab1
leaq 0xde5b3(%rip), %rsi     # 0x119058
movq %r15, %rdi
xorl %eax, %eax
callq 0xe9df7
jmp 0x3aab1
testl %ebx, %ebx
je 0x3ac04
movq %r12, %rdi
callq 0xf0ea4
movq 0x8(%rsp), %rbx
movq 0x8(%rbx), %rsi
movq 0x10(%rbx), %rdx
movq 0x18(%rbx), %rcx
movq %r15, 0x50(%rsp)
movq %r15, %rdi
movl $0x3, %r8d
callq 0xf2ce6
movq %rax, %rbp
movzbl 0x1(%rbx), %edx
cmpq $0x4, %rdx
movq %r12, 0x48(%rsp)
jne 0x3abb3
cmpq $0x0, 0x10(%rbp)
jle 0x3abd1
movq 0x8(%rsp), %rax
movq 0x20(%rax), %rax
movq $0x0, 0x38(%rsp)
xorl %r13d, %r13d
movq %rax, 0x40(%rsp)
movq %rbp, 0x10(%rsp)
testq %rax, %rax
jle 0x3ab9a
movl $0x1, %r14d
movq 0x38(%rsp), %rbx
movq 0x40(%rsp), %r12
movq 0x48(%rsp), %rdi
movq %rbx, %rsi
callq 0xef0d5
movzbl %al, %r15d
movl %r15d, %eax
andl $0xf, %eax
leaq -0x1(%r14), %rsi
movq %r13, %rbp
movq 0x28(%rsp), %r13
movl (%r13,%rax,4), %ecx
movq 0x10(%rsp), %rdi
movq %rbp, %rdx
callq 0xf2b02
shrl $0x4, %r15d
movl (%r13,%r15,4), %ecx
movq %rbp, %r13
movq 0x10(%rsp), %rbp
movq %rbp, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0xf2b02
addq $0x2, %r14
incq %rbx
decq %r12
jne 0x3ab3a
incq %r13
movq 0x40(%rsp), %rax
addq %rax, 0x38(%rsp)
cmpq 0x10(%rbp), %r13
jl 0x3ab25
jmp 0x3abd1
movq 0x20(%rbx), %rcx
subq $0x8, %rsp
movq %r12, %rdi
xorl %esi, %esi
movq %r14, %r8
movq %rbp, %r9
pushq $0x0
callq 0xf35b8
addq $0x10, %rsp
movq %rbp, %rdi
xorl %esi, %esi
movl $0x4, %edx
callq 0xf2923
movq 0x50(%rsp), %r15
movq 0x8(%rsp), %rbx
movq 0x48(%rsp), %r12
movq %rbp, %rdi
callq 0xf28d4
movq %r12, %rdi
callq 0xf0b13
jmp 0x3aa53
xorl %ebp, %ebp
movq 0x8(%rsp), %rbx
jmp 0x3abef