// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movq -0x8(%rbp), %rdi
movl $0x438, %esi            # imm = 0x438
callq 0x15b780
movq %rax, -0x18(%rbp)
movq $0x4, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165170
movb %al, %cl
movq -0x18(%rbp), %rax
movb %cl, (%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movzbl (%rax), %edx
leaq 0x1565e5(%rip), %rsi    # 0x1a4dc9
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165170
movb %al, %cl
movq -0x18(%rbp), %rax
movb %cl, 0x1(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movzbl 0x1(%rax), %edx
leaq 0x1565bf(%rip), %rsi    # 0x1a4dd7
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1652c0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1652c0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdx
callq 0x15bba0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1652c0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movl %eax, %edx
leaq 0x156545(%rip), %rsi    # 0x1a4de6
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
cmpq $0x100, 0x30(%rax)      # imm = 0x100
jle 0x4e8c2
movl $0x1, -0x34(%rbp)
jmp 0x4eb46
movq -0x18(%rbp), %rax
cmpq $0x0, 0x30(%rax)
jne 0x4e8d9
movl $0x1, -0x34(%rbp)
jmp 0x4eb46
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x20(%rbp), %rdx
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rcx
movq -0x18(%rbp), %r9
addq $0x38, %r9
movl $0x3, %r8d
xorl %eax, %eax
movq $0x100, (%rsp)          # imm = 0x100
movl $0x1, 0x8(%rsp)
movl $0x0, 0x10(%rsp)
callq 0x16c160
movq -0x18(%rbp), %rax
imulq $0x3, 0x30(%rax), %rax
addq -0x20(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rdx
leaq 0x17811b(%rip), %rsi    # 0x1c6a65
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x1, %eax
jle 0x4e969
movl $0x1, -0x34(%rbp)
jmp 0x4eb46
movq -0x18(%rbp), %rax
movzbl 0x1(%rax), %eax
cmpl $0x4, %eax
je 0x4e98f
movq -0x18(%rbp), %rax
movzbl 0x1(%rax), %eax
cmpl $0x8, %eax
je 0x4e98f
movl $0x1, -0x34(%rbp)
jmp 0x4eb46
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdx
callq 0x16a850
cmpl $0x0, %eax
jne 0x4e9b2
jmp 0x4eb46
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x18(%rbp), %rax
movzbl 0x1(%rax), %eax
imulq %rax, %rdi
movl $0x10, %esi
callq 0x15ce90
movl $0x8, %ecx
cqto
idivq %rcx
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x18(%rbp), %rcx
movzbl 0x1(%rcx), %ecx
imulq %rcx, %rax
movl $0x2, %ecx
cqto
idivq %rcx
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rcx
movq -0x18(%rbp), %rax
imulq 0x20(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x18(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x1, %eax
jne 0x4ea80
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rsi
movl $0x1, %edx
callq 0x168020
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
callq 0x167b90
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x30(%rbp), %rcx
callq 0x4ec30
cmpl $0x0, %eax
jne 0x4ea75
jmp 0x4eb46
movq -0x30(%rbp), %rdi
callq 0x167c30
jmp 0x4eaa5
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rdx
subq -0x20(%rbp), %rdx
callq 0x1691b0
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdx
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rcx
movl $0x3, %r8d
callq 0x16b7f0
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rax
movzbl 0x1(%rax), %eax
cmpl $0x4, %eax
jne 0x4eb00
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rdx
movq -0x18(%rbp), %rcx
addq $0x38, %rcx
movq -0x28(%rbp), %r8
xorl %eax, %eax
movl %eax, %esi
callq 0x4ed80
jmp 0x4eb34
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rax
movzbl 0x1(%rax), %eax
movl %eax, %edx
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rcx
movq -0x18(%rbp), %r8
addq $0x38, %r8
movq -0x28(%rbp), %r9
xorl %eax, %eax
movl %eax, %esi
xorl %eax, %eax
movl $0x0, (%rsp)
callq 0x16c520
movq -0x28(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x4, %edx
callq 0x16afd0
movq -0x28(%rbp), %rdi
callq 0x16af60
movq -0x30(%rbp), %rdi
callq 0x1676a0
cmpl $0x0, -0x34(%rbp)
je 0x4eb70
movq -0x8(%rbp), %rdi
leaq 0x156291(%rip), %rsi    # 0x1a4dfa
movb $0x0, %al
callq 0x15bfb0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x15b820
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)