// File: /workspace/github_c_makefile_star2_O3/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 0xe8b2b
movq %rax, %r14
movq %rax, %rbx
leaq 0x8(%rsp), %r12
movq $0x4, (%r12)
movq 0x18(%r15), %rdi
movq %r12, %rsi
callq 0xeeb4b
movb %al, (%r14)
movzbl %al, %edx
leaq 0xdeaa6(%rip), %rsi     # 0x119129
movq %r15, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x18(%r15), %rdi
movq %r12, %rsi
callq 0xeeb4b
movb %al, 0x1(%r14)
movzbl %al, %edx
leaq 0xdea90(%rip), %rsi     # 0x119137
movq %r15, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x18(%r15), %rdi
movq %r12, %rsi
callq 0xeebeb
movq %rax, 0x8(%r14)
movq 0x18(%r15), %rdi
movq %r12, %rsi
callq 0xeebeb
movq %rax, 0x18(%r14)
movq 0x8(%r14), %rsi
movq %r15, %rdi
movq %rax, %rdx
callq 0xe8f88
movq 0x18(%r15), %rdi
movq %r12, %rsi
callq 0xeebeb
movq %rax, 0x30(%r14)
leaq 0xdea4f(%rip), %rsi     # 0x119146
movq %r15, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x30(%r14), %rcx
cmpq $0x101, %rcx            # imm = 0x101
setge %al
testq %rcx, %rcx
sete %dl
orb %al, %dl
jne 0x3a8ee
movq 0x18(%r15), %rsi
movq 0x8(%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 0xf2cb1
addq $0x20, %rsp
movq 0x30(%rbx), %rax
leaq (%rax,%rax,2), %rax
movq 0x8(%rsp), %rcx
leaq 0x2(%rcx,%rax), %rdx
movq %rdx, 0x8(%rsp)
leaq 0x10094b(%rip), %rsi    # 0x13b0b7
movq %r15, %rdi
xorl %eax, %eax
callq 0xe8617
cmpb $0x1, (%rbx)
ja 0x3a8ee
movzbl 0x1(%rbx), %eax
cmpl $0x8, %eax
je 0x3a791
cmpl $0x4, %eax
jne 0x3a8ee
movq 0x8(%rbx), %rsi
movq 0x18(%rbx), %rdx
movq %r15, %rdi
callq 0xf1db1
testl %eax, %eax
je 0x3a927
movzbl 0x1(%rbx), %edi
imulq 0x8(%rbx), %rdi
movl $0x10, %esi
callq 0xe9cb4
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, 0x20(%rsp)
movq %r14, 0x28(%rsp)
jne 0x3a932
movq %r15, %rdi
movl $0x1, %edx
callq 0xf09c5
movq %rax, (%rsp)
movq %rax, %rdi
callq 0xf0855
movq 0x8(%rsp), %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, 0x28(%rbx)
jle 0x3a8db
xorl %r14d, %r14d
leaq 0x36(%rsp), %rbp
xorl %r12d, %r12d
movq (%rsp), %r13
movq 0x18(%rsp), %rax
movq 0x18(%r15), %rdi
cmpq 0x18(%rdi), %rax
jg 0x3aa79
cmpq 0x20(%rbx), %r14
jg 0x3aa79
leaq 0x18(%rsp), %rsi
callq 0xeebeb
movq %rax, %rbx
movq 0x18(%r15), %rdi
movq 0x18(%rsp), %rdx
movl $0x2, %ecx
movq %rbp, %rsi
callq 0xee8d4
addq $0x2, 0x18(%rsp)
testq %rbx, %rbx
je 0x3a8bf
movq %r12, 0x10(%rsp)
movq %rbx, %r12
jle 0x3a8ab
movl $0x2, %edx
movq %r13, %rdi
movq %rbp, %rsi
callq 0xef547
decq %r12
jne 0x3a896
movq 0x10(%rsp), %r12
leaq (%r12,%rbx,2), %r12
leaq (%r14,%rbx,2), %r14
movq 0x20(%rsp), %rbx
jmp 0x3a8d1
movq 0x20(%rsp), %rbx
cmpq 0x20(%rbx), %r14
jne 0x3aa79
xorl %r14d, %r14d
cmpq 0x28(%rbx), %r12
jl 0x3a841
movq (%rsp), %r12
movq %r12, %rdi
callq 0xf08ba
movq 0x28(%rsp), %r14
jmp 0x3a94a
xorl %edi, %edi
callq 0xf228d
xorl %edi, %edi
callq 0xf052b
leaq 0xde857(%rip), %rsi     # 0x11915a
movq %r15, %rdi
xorl %eax, %eax
callq 0xe9295
movq %r15, %rdi
movq %rbx, %rsi
callq 0xe8be2
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r13d, %r13d
xorl %r14d, %r14d
jmp 0x3aa91
movq 0x18(%r15), %rdi
movq 0x8(%rsp), %rsi
movq 0x18(%rdi), %rdx
subq %rsi, %rdx
callq 0xf1388
movq %rax, %r12
movq 0x8(%rbx), %rsi
movq 0x10(%rbx), %rdx
movq 0x18(%rbx), %rcx
movq %r15, %rdi
movl $0x3, %r8d
callq 0xf26a5
movq %rax, %r13
movzbl 0x1(%rbx), %edx
cmpq $0x4, %rdx
movq %r12, (%rsp)
jne 0x3aa4a
movq 0x10(%r13), %rax
testq %rax, %rax
jle 0x3aa68
movq 0x20(%rbx), %r12
xorl %ecx, %ecx
xorl %edx, %edx
movq %r15, 0x48(%rsp)
movq %r12, 0x40(%rsp)
movq %r13, 0x38(%rsp)
testq %r12, %r12
jle 0x3aa39
movl $0x1, %r14d
movq %rcx, 0x50(%rsp)
movq %rcx, %rbx
movq %rdx, 0x10(%rsp)
movq (%rsp), %rdi
movq %rbx, %rsi
callq 0xeead3
movzbl %al, %r15d
movl %r15d, %eax
andl $0xf, %eax
leaq -0x1(%r14), %rsi
movq 0x28(%rsp), %rcx
movl (%rcx,%rax,4), %ecx
movq 0x38(%rsp), %r13
movq %r13, %rdi
movq 0x10(%rsp), %rbp
movq %rbp, %rdx
callq 0xf24bf
shrl $0x4, %r15d
movq 0x28(%rsp), %rax
movl (%rax,%r15,4), %ecx
movq %r13, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0xf24bf
addq $0x2, %r14
incq %rbx
decq %r12
jne 0x3a9b9
movq 0x38(%rsp), %r13
movq 0x10(%r13), %rax
movq 0x48(%rsp), %r15
movq 0x20(%rsp), %rbx
movq 0x40(%rsp), %r12
movq 0x50(%rsp), %rcx
movq 0x10(%rsp), %rdx
incq %rdx
addq %r12, %rcx
cmpq %rax, %rdx
jl 0x3a99d
jmp 0x3aa68
movq 0x20(%rbx), %rcx
subq $0x8, %rsp
movq %r12, %rdi
xorl %esi, %esi
movq %r14, %r8
movq %r13, %r9
pushq $0x0
callq 0xf2f79
addq $0x10, %rsp
movq %r13, %rdi
xorl %esi, %esi
movl $0x4, %edx
callq 0xf22dc
jmp 0x3aa8d
leaq 0xde6f8(%rip), %rsi     # 0x119178
xorl %r13d, %r13d
movq %r15, %rdi
xorl %eax, %eax
callq 0xe9295
movq (%rsp), %r14
movq %r13, %rdi
callq 0xf228d
movq %r14, %rdi
callq 0xf052b
jmp 0x3a90d