// File: /workspace/github_c_makefile_star2_O2/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 $0x88, %rsp
movq %rdi, %rbx
movl $0x438, %esi            # imm = 0x438
callq 0xdecaf
movq %rax, %r15
movq %rax, %r14
leaq 0x30(%rsp), %r12
movq $0x4, (%r12)
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xe3f2a
movb %al, (%r15)
movzbl %al, %edx
leaq 0xd30db(%rip), %rsi     # 0x10c039
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xe3f2a
movb %al, 0x1(%r15)
movzbl %al, %edx
leaq 0xd30c5(%rip), %rsi     # 0x10c047
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xe3fc4
movq %rax, 0x8(%r15)
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xe3fc4
movq %rax, 0x18(%r15)
movq 0x8(%r15), %rsi
movq %rbx, %rdi
movq %rax, %rdx
callq 0xdf038
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xe3fc4
movq %rax, 0x30(%r15)
leaq 0xd3084(%rip), %rsi     # 0x10c056
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq 0x30(%r15), %rcx
cmpq $0x101, %rcx            # imm = 0x101
setge %al
testq %rcx, %rcx
sete %dl
orb %al, %dl
jne 0x391d9
movq %r15, %r9
movq 0x18(%rbx), %rsi
movq 0x30(%rsp), %rdx
addq $0x38, %r9
andl $0x0, 0x10(%rsp)
movl $0x1, 0x8(%rsp)
movq $0x100, (%rsp)          # imm = 0x100
pushq $0x3
popq %r8
movq %rbx, %rdi
movq %r9, 0x50(%rsp)
callq 0xe79ca
imulq $0x3, 0x30(%r14), %rax
movq 0x30(%rsp), %rcx
leaq (%rcx,%rax), %rdx
addq $0x2, %rdx
movq %rdx, 0x30(%rsp)
leaq 0xf4d79(%rip), %rsi     # 0x12ddc7
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
cmpb $0x1, (%r14)
ja 0x391d9
movzbl 0x1(%r14), %eax
cmpl $0x8, %eax
je 0x39075
cmpl $0x4, %eax
jne 0x391d9
movq 0x8(%r14), %rsi
movq 0x18(%r14), %rdx
movq %rbx, %rdi
callq 0xe6be1
testl %eax, %eax
je 0x39215
movzbl 0x1(%r14), %edi
imulq 0x8(%r14), %rdi
pushq $0x10
popq %rsi
callq 0xdfbb6
pushq $0x8
popq %rcx
cqto
idivq %rcx
movq %rax, %rsi
movq %rax, 0x20(%r14)
movzbl 0x1(%r14), %eax
imulq %rsi, %rax
pushq $0x2
popq %rcx
cqto
idivq %rcx
movq %rax, 0x10(%r14)
imulq 0x18(%r14), %rsi
movq %rsi, 0x28(%r14)
cmpb $0x1, (%r14)
movq %rbx, 0x28(%rsp)
movq %r14, 0x20(%rsp)
jne 0x39220
pushq $0x1
popq %rdx
movq %rbx, %rdi
callq 0xe59cd
movq %rax, %r13
movq %rax, %rdi
callq 0xe589c
movq 0x30(%rsp), %rax
leaq 0x38(%rsp), %rcx
movq %rax, (%rcx)
xorl %r12d, %r12d
leaq 0x4e(%rsp), %rbp
pushq $0x2
popq %r15
xorl %r14d, %r14d
movq 0x20(%rsp), %rax
cmpq 0x28(%rax), %r12
jge 0x3923a
movq 0x38(%rsp), %rax
movq 0x28(%rsp), %rcx
movq 0x18(%rcx), %rdi
cmpq 0x18(%rdi), %rax
jg 0x391bb
movq 0x20(%rsp), %rax
cmpq 0x20(%rax), %r14
jg 0x391bb
leaq 0x38(%rsp), %rsi
callq 0xe3fc4
movq %rax, %rbx
movq 0x28(%rsp), %rax
movq 0x18(%rax), %rdi
movq 0x38(%rsp), %rdx
movq %rbp, %rsi
movq %r15, %rcx
callq 0xe3cb7
addq $0x2, 0x38(%rsp)
testq %rbx, %rbx
je 0x391ac
movq %r12, 0x40(%rsp)
movl $0x0, %r12d
cmovgq %rbx, %r12
subq $0x1, %r12
jb 0x3919a
movq %r13, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0xe46ba
jmp 0x39184
movq 0x40(%rsp), %r12
leaq (%r12,%rbx,2), %r12
leaq (%r14,%rbx,2), %r14
jmp 0x39112
movq 0x20(%rsp), %rax
cmpq 0x20(%rax), %r14
je 0x3910f
leaq 0xd2ec6(%rip), %rsi     # 0x10c088
xorl %r15d, %r15d
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
jmp 0x3937c
xorl %edi, %edi
callq 0xe7084
xorl %edi, %edi
callq 0xe5591
leaq 0xd2e7c(%rip), %rsi     # 0x10c06a
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
movq %rbx, %rdi
movq %r14, %rsi
callq 0xded32
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r15d, %r15d
xorl %r13d, %r13d
jmp 0x39381
movq 0x18(%rbx), %rdi
movq 0x30(%rsp), %rsi
movq 0x18(%rdi), %rdx
subq %rsi, %rdx
callq 0xe6248
movq %rax, %r13
jmp 0x3924c
movq %r13, %rdi
callq 0xe58e8
movq 0x28(%rsp), %rbx
movq 0x20(%rsp), %r14
movq 0x8(%r14), %rsi
movq 0x10(%r14), %rdx
movq 0x18(%r14), %rcx
pushq $0x3
popq %r8
movq %rbx, %rdi
callq 0xe746f
movq %rax, %r15
movzbl 0x1(%r14), %edx
cmpq $0x4, %rdx
jne 0x39350
movq 0x20(%r14), %rax
xorl %ecx, %ecx
testq %rax, %rax
movl $0x0, %edx
movq %rax, 0x70(%rsp)
cmovgq %rax, %rdx
movq %rdx, 0x68(%rsp)
xorl %eax, %eax
movq %r15, 0x58(%rsp)
movq %r13, 0x80(%rsp)
cmpq 0x10(%r15), %rax
jge 0x3936a
movq %rax, 0x60(%rsp)
movq 0x68(%rsp), %r12
movq %rcx, 0x78(%rsp)
pushq $0x1
popq %r14
subq $0x1, %r12
jb 0x39334
movq %r13, %rdi
movq %rcx, %rbx
movq %rcx, %rsi
callq 0xe3eb4
movzbl %al, %r15d
movl %r15d, %eax
andl $0xf, %eax
leaq -0x1(%r14), %rsi
movq 0x50(%rsp), %r13
movl (%r13,%rax,4), %ecx
movq %r14, 0x40(%rsp)
movq 0x58(%rsp), %r14
movq %r14, %rdi
movq 0x60(%rsp), %rbp
movq %rbp, %rdx
callq 0xe728c
shrl $0x4, %r15d
movl (%r13,%r15,4), %ecx
movq 0x80(%rsp), %r13
movq %r14, %rdi
movq 0x40(%rsp), %r14
movq %r14, %rsi
movq %rbp, %rdx
callq 0xe728c
movq %rbx, %rcx
addq $0x2, %r14
incq %rcx
jmp 0x392be
movq 0x60(%rsp), %rax
incq %rax
movq 0x78(%rsp), %rcx
addq 0x70(%rsp), %rcx
movq 0x58(%rsp), %r15
jmp 0x392a1
movq 0x20(%r14), %rcx
andl $0x0, (%rsp)
movq %r13, %rdi
xorl %esi, %esi
movq 0x50(%rsp), %r8
movq %r15, %r9
callq 0xe7cbc
pushq $0x4
popq %rdx
movq %r15, %rdi
xorl %esi, %esi
callq 0xe70b7
movq 0x28(%rsp), %rbx
movq 0x20(%rsp), %r14
movq %r15, %rdi
callq 0xe7084
movq %r13, %rdi
callq 0xe5591
jmp 0x391f8