// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/atari-img.c
// Function: de_run_degas
static void de_run_degas(deark *c, de_module_params *mparams)
{
	degasctx *d = NULL;
	struct atari_img_decode_data *adata = NULL;
	de_finfo *fi = NULL;
	i64 pos;
	unsigned int format_code, resolution_code;
	int is_grayscale;
	i64 cmpr_bytes_consumed = 0;

	d = de_malloc(c, sizeof(degasctx));
	adata = de_malloc(c, sizeof(struct atari_img_decode_data));

	adata->pal = d->pal;

	pos = 0;
	format_code = (unsigned int)de_getu16be(pos);
	de_dbg(c, "format code: 0x%04x", format_code);
	resolution_code = format_code & 0x0003;
	d->compression_code = (format_code & 0x8000)>>15;
	de_dbg_indent(c, 1);
	de_dbg(c, "resolution code: %u", resolution_code);
	de_dbg(c, "compression code: %u", d->compression_code);
	de_dbg_indent(c, -1);
	pos += 2;

	switch(resolution_code) {
	case 0:
		adata->bpp = 4;
		adata->w = 320;
		adata->h = 200;
		break;
	case 1:
		adata->bpp = 2;
		adata->w = 640;
		adata->h = 200;
		break;
	case 2:
		adata->bpp = 1;
		adata->w = 640;
		adata->h = 400;
		break;
	default:
		de_dbg(c, "Invalid or unsupported resolution (%u)", resolution_code);
		goto done;
	}
	adata->ncolors = de_pow2(adata->bpp);

	de_dbg(c, "dimensions: %d"DE_CHAR_TIMES"%d, colors: %d", (int)adata->w, (int)adata->h, (int)adata->ncolors);

	d->degas_elite_flag = is_degas_elite(c, d);
	de_declare_fmtf(c, "DEGAS%s %d-color %scompressed",
		d->degas_elite_flag?" Elite":"",
		(int)adata->ncolors,
		d->compression_code?"":"un");

	fmtutil_read_atari_palette(c, c->infile, pos, adata->pal, 16, adata->ncolors, 0);
	pos += 2*16;
	fix_dark_pal(c, adata);

	if(d->compression_code) {
		adata->was_compressed = 1;
		adata->unc_pixels = dbuf_create_membuf(c, 32000, 1);
		dbuf_enable_wbuffer(adata->unc_pixels);

		if(!fmtutil_decompress_packbits(c->infile, pos, c->infile->len-pos, adata->unc_pixels, &cmpr_bytes_consumed))
			goto done;
		dbuf_flush(adata->unc_pixels);

		de_dbg(c, "Compressed bytes found: %d", (int)cmpr_bytes_consumed);
		pos += cmpr_bytes_consumed;
	}
	else {
		i64 avail_bytes = 32000;
		if(pos+32000 > c->infile->len) {
			avail_bytes = c->infile->len - pos;
			de_warn(c, "Unexpected end of file (expected 32000 bytes, got %d)", (int)avail_bytes);
		}
		adata->unc_pixels = dbuf_open_input_subfile(c->infile, pos, avail_bytes);
		pos += avail_bytes;
	}

	if(pos + 32 == c->infile->len) {
		do_degas_anim_fields(c, d, pos);
	}

	is_grayscale = de_is_grayscale_palette(adata->pal, adata->ncolors);

	adata->img = de_bitmap_create(c, adata->w, adata->h, is_grayscale?1:3);

	fi = de_finfo_create(c);
	fmtutil_atari_set_standard_density(c, adata, fi);

	fmtutil_atari_decode_image(c, adata);

	de_bitmap_write_to_file_finfo(adata->img, fi, 0);

done:
	if(adata) {
		dbuf_close(adata->unc_pixels);
		de_bitmap_destroy(adata->img);
		de_free(c, adata);
	}
	de_finfo_destroy(c, fi);
	de_free(c, d);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movq $0x0, 0x8(%rsp)
movl $0x48, %esi
callq 0xe8b2b
movq %rax, %r13
movl $0x40, %esi
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, %r15
leaq 0x8(%r13), %rax
movq %rax, 0x30(%r15)
movq 0x18(%rbx), %rdi
xorl %esi, %esi
callq 0xeeb67
movq %rax, %r14
leaq 0xf1ff1(%rip), %rsi     # 0x11363c
movq %rbx, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0xe8617
movl %r14d, %r12d
andl $0x3, %r12d
shrl $0xf, %r14d
andl $0x1, %r14d
movl %r14d, (%r13)
movl $0x1, %ebp
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
leaq 0xf1fcc(%rip), %rsi     # 0x113650
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xe8617
movl (%r13), %edx
leaq 0x107bbc(%rip), %rsi    # 0x129258
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
leaq 0xf1c36(%rip), %rax     # 0x1132f0
movslq (%rax,%r12,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movaps 0xf1b86(%rip), %xmm0    # 0x113250
movl $0x4, %edi
jmp 0x21709
movaps 0xf1b88(%rip), %xmm0    # 0x113260
movl $0x1, %edi
jmp 0x21709
leaq 0xf1f7e(%rip), %rsi     # 0x113664
xorl %r12d, %r12d
movq %rbx, %rdi
movl $0x3, %edx
xorl %eax, %eax
callq 0xe8617
jmp 0x219f2
movaps 0xf1b6c(%rip), %xmm0    # 0x113270
movl $0x2, %edi
movq %rdi, (%r15)
movups %xmm0, 0x10(%r15)
callq 0xe9c9b
movq %rax, 0x8(%r15)
movl 0x10(%r15), %edx
movl 0x18(%r15), %ecx
leaq 0xf1f62(%rip), %rsi     # 0x11368b
movq %rbx, %rdi
movl %eax, %r8d
xorl %eax, %eax
callq 0xe8617
leaq 0xf1f8b(%rip), %r12     # 0x1136c8
movq %r13, 0x10(%rsp)
cmpl $0x0, (%r13)
jne 0x21764
movq 0x18(%rbx), %rax
cmpq $0x7d42, 0x18(%rax)     # imm = 0x7D42
jge 0x21a34
leaq 0x12394e(%rip), %r12    # 0x1450b0
xorl %ebp, %ebp
movq 0x10(%rsp), %r13
movl %ebp, 0x4(%r13)
movl 0x8(%r15), %ecx
cmpl $0x0, (%r13)
leaq 0xf1f52(%rip), %rax     # 0x1136cf
leaq 0x12392c(%rip), %r8     # 0x1450b0
cmoveq %rax, %r8
leaq 0xf1f1b(%rip), %rsi     # 0x1136aa
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xeb152
movq 0x18(%rbx), %rsi
movq 0x8(%r15), %r9
movq 0x30(%r15), %rcx
movl $0x0, (%rsp)
movl $0x2, %edx
movl $0x10, %r8d
movq %rbx, %rdi
callq 0xf876e
movq %rbx, %rdi
movq %r15, %rsi
callq 0x23ac6
cmpl $0x0, (%r13)
je 0x21846
movl $0x1, 0x28(%r15)
movl $0x7d00, %esi           # imm = 0x7D00
movq %rbx, %rdi
movl $0x1, %edx
callq 0xf09c5
movq %rax, 0x20(%r15)
movq %rax, %rdi
callq 0xf0855
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rdx
addq $-0x22, %rdx
movq 0x20(%r15), %rcx
leaq 0x8(%rsp), %r8
movl $0x22, %esi
callq 0xfa81c
testl %eax, %eax
je 0x219ef
movq 0x20(%r15), %rdi
callq 0xf08ba
movl 0x8(%rsp), %edx
leaq 0xf1e9d(%rip), %rsi     # 0x1136d2
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x8(%rsp), %r12
jmp 0x2188d
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %r14
movl $0x7d00, %r12d          # imm = 0x7D00
cmpq $0x7d21, %r14           # imm = 0x7D21
jg 0x2187c
addq $-0x22, %r14
leaq 0xf1e85(%rip), %rsi     # 0x1136ed
movq %rbx, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0xe8faa
movq 0x18(%rbx), %rdi
movq %r14, %r12
movl $0x22, %esi
movq %r12, %rdx
callq 0xf1388
movq %rax, 0x20(%r15)
leaq 0x42(%r12), %rax
movq 0x18(%rbx), %rcx
cmpq 0x18(%rcx), %rax
jne 0x21987
leaq 0x22(%r12), %r13
leaq 0xf1e88(%rip), %rbp     # 0x113734
xorl %r14d, %r14d
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xeeb67
movq %rbx, %rdi
movq %rbp, %rsi
movl %r14d, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0xe87e4
incq %r14
addq $0x2, %r13
cmpq $0x4, %r14
jne 0x218af
leaq 0x2a(%r12), %r13
leaq 0xf1e67(%rip), %rbp     # 0x11374d
xorl %r14d, %r14d
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xeeb67
movq %rbx, %rdi
movq %rbp, %rsi
movl %r14d, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0xe87e4
incq %r14
addq $0x2, %r13
cmpq $0x4, %r14
jne 0x218e9
leaq 0x32(%r12), %r13
leaq 0xf1e47(%rip), %rbp     # 0x113767
xorl %r14d, %r14d
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xeeb67
movq %rbx, %rdi
movq %rbp, %rsi
movl %r14d, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0xe87e4
incq %r14
addq $0x2, %r13
cmpq $0x4, %r14
jne 0x21923
addq $0x3a, %r12
leaq 0xf1e29(%rip), %r13     # 0x113782
xorl %r14d, %r14d
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xeeb67
movq %rbx, %rdi
movq %r13, %rsi
movl %r14d, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0xe87e4
incq %r14
addq $0x2, %r12
cmpq $0x4, %r14
jne 0x2195c
movq 0x8(%r15), %rsi
movq 0x30(%r15), %rdi
callq 0xf1ed8
movq 0x10(%r15), %rsi
movq 0x18(%r15), %rdx
xorl %ecx, %ecx
testl %eax, %eax
sete %cl
leal 0x1(,%rcx,2), %ecx
movq %rbx, %rdi
callq 0xf266a
movq %rax, 0x38(%r15)
movq %rbx, %rdi
callq 0xea504
movq %rax, %r12
movq %rbx, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0xf8e1c
movq %rbx, %rdi
movq %r15, %rsi
callq 0xf8b10
movq 0x38(%r15), %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0xf22d0
movq 0x10(%rsp), %r13
jmp 0x219f2
xorl %r12d, %r12d
movq 0x20(%r15), %rdi
callq 0xf052b
movq 0x38(%r15), %rdi
callq 0xf228d
movq %rbx, %rdi
movq %r15, %rsi
callq 0xe8be2
movq %rbx, %rdi
movq %r12, %rsi
callq 0xea50e
movq %rbx, %rdi
movq %r13, %rsi
callq 0xe8be2
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, %r13d
movl $0x7d22, %r14d          # imm = 0x7D22
xorl %ebp, %ebp
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xeeb67
cmpq $0xf, %rax
jg 0x2175b
testq %rax, %rax
cmovnel %ebp, %r13d
addq $0x2, %r14
cmpq $0x7d32, %r14           # imm = 0x7D32
jne 0x21a42
movl $0x7d32, %r14d          # imm = 0x7D32
xorl %ebp, %ebp
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xeeb67
cmpq $0x2, %rax
jg 0x2175b
testq %rax, %rax
cmovnel %ebp, %r13d
addq $0x2, %r14
cmpq $0x7d3a, %r14           # imm = 0x7D3A
jne 0x21a74
movl $0x7d3a, %r14d          # imm = 0x7D3A
xorl %ebp, %ebp
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xeeb67
cmpq $0x80, %rax
jg 0x2175b
testq %rax, %rax
cmovnel %ebp, %r13d
addq $0x2, %r14
cmpq $0x7d42, %r14           # imm = 0x7D42
jne 0x21aa6
movl $0x1, %ebp
testl %r13d, %r13d
je 0x21764
movq 0x18(%rbx), %rax
cmpq $0x7d45, 0x18(%rax)     # imm = 0x7D45
jge 0x2175b
jmp 0x21764