// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x8(%rbp), %rdi
movl $0x48, %esi
callq 0x15b780
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movl $0x40, %esi
callq 0x15b780
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rcx
addq $0x8, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x30(%rax)
movq $0x0, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
callq 0x1651d0
movl %eax, -0x34(%rbp)
movq -0x8(%rbp), %rdi
movl -0x34(%rbp), %edx
leaq 0x177a4e(%rip), %rsi    # 0x19f56c
xorl %eax, %eax
movb %al, -0x55(%rbp)
callq 0x15b0d0
movl -0x34(%rbp), %eax
andl $0x3, %eax
movl %eax, -0x38(%rbp)
movzwl -0x34(%rbp), %ecx
shrl $0xf, %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movb -0x55(%rbp), %al
movq -0x8(%rbp), %rdi
movl -0x38(%rbp), %edx
leaq 0x177a23(%rip), %rsi    # 0x19f580
callq 0x15b0d0
movb -0x55(%rbp), %al
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rcx
movl (%rcx), %edx
leaq 0x18d010(%rip), %rsi    # 0x1b4b86
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x30(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x30(%rbp)
movl -0x38(%rbp), %eax
movl %eax, -0x54(%rbp)
testl %eax, %eax
je 0x27bb5
jmp 0x27ba1
movl -0x54(%rbp), %eax
subl $0x1, %eax
je 0x27bda
jmp 0x27bab
movl -0x54(%rbp), %eax
subl $0x2, %eax
je 0x27bff
jmp 0x27c24
movq -0x20(%rbp), %rax
movq $0x4, (%rax)
movq -0x20(%rbp), %rax
movq $0x140, 0x10(%rax)      # imm = 0x140
movq -0x20(%rbp), %rax
movq $0xc8, 0x18(%rax)
jmp 0x27c3e
movq -0x20(%rbp), %rax
movq $0x2, (%rax)
movq -0x20(%rbp), %rax
movq $0x280, 0x10(%rax)      # imm = 0x280
movq -0x20(%rbp), %rax
movq $0xc8, 0x18(%rax)
jmp 0x27c3e
movq -0x20(%rbp), %rax
movq $0x1, (%rax)
movq -0x20(%rbp), %rax
movq $0x280, 0x10(%rax)      # imm = 0x280
movq -0x20(%rbp), %rax
movq $0x190, 0x18(%rax)      # imm = 0x190
jmp 0x27c3e
movq -0x8(%rbp), %rdi
movl -0x38(%rbp), %edx
leaq 0x177962(%rip), %rsi    # 0x19f594
movb $0x0, %al
callq 0x15b0d0
jmp 0x27f17
movq -0x20(%rbp), %rax
movq (%rax), %rdi
callq 0x15ce50
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movl %eax, %edx
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movl %eax, %ecx
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, %r8d
leaq 0x17793c(%rip), %rsi    # 0x19f5bb
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x2aa30
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x4(%rax), %ecx
leaq 0x1a8f22(%rip), %rdx    # 0x1d0bd0
leaq 0x177943(%rip), %rax    # 0x19f5f8
cmpl $0x0, %ecx
cmovneq %rax, %rdx
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl (%rax), %esi
leaq 0x17792c(%rip), %r8     # 0x19f5ff
leaq 0x1a8ef6(%rip), %rax    # 0x1d0bd0
cmpl $0x0, %esi
cmovneq %rax, %r8
leaq 0x1778f2(%rip), %rsi    # 0x19f5da
movb $0x0, %al
callq 0x15e3d0
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x30(%rbp), %rdx
movq -0x20(%rbp), %rax
movq 0x30(%rax), %rcx
movq -0x20(%rbp), %rax
movq 0x8(%rax), %r9
movl $0x10, %r8d
xorl %eax, %eax
movl $0x0, (%rsp)
callq 0x174460
movq -0x30(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x2abf0
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
je 0x27de9
movq -0x20(%rbp), %rax
movl $0x1, 0x28(%rax)
movq -0x8(%rbp), %rdi
movl $0x7d00, %esi           # imm = 0x7D00
movl $0x1, %edx
callq 0x168020
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x167b90
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rdx
subq -0x30(%rbp), %rdx
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rcx
leaq -0x48(%rbp), %r8
callq 0x177e60
cmpl $0x0, %eax
jne 0x27db6
jmp 0x27f17
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x167c30
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rax
movl %eax, %edx
leaq 0x17782e(%rip), %rsi    # 0x19f602
movb $0x0, %al
callq 0x15b0d0
movq -0x48(%rbp), %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0x27e61
movq $0x7d00, -0x50(%rbp)    # imm = 0x7D00
movq -0x30(%rbp), %rax
addq $0x7d00, %rax           # imm = 0x7D00
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jle 0x27e35
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rax
subq -0x30(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rax
movl %eax, %edx
leaq 0x1777ef(%rip), %rsi    # 0x19f61d
movb $0x0, %al
callq 0x15bc10
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
movq -0x50(%rbp), %rdx
callq 0x1691b0
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x50(%rbp), %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
addq $0x20, %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jne 0x27e88
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x2ac90
movq -0x20(%rbp), %rax
movq 0x30(%rax), %rdi
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x16aa20
movl %eax, -0x3c(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rdx
movl -0x3c(%rbp), %r8d
movl $0x3, %ecx
movl $0x1, %eax
cmpl $0x0, %r8d
cmovnel %eax, %ecx
callq 0x16b750
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x8(%rbp), %rdi
callq 0x15d230
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x174df0
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x174910
movq -0x20(%rbp), %rax
movq 0x38(%rax), %rdi
movq -0x28(%rbp), %rsi
xorl %edx, %edx
callq 0x16afa0
cmpq $0x0, -0x20(%rbp)
je 0x27f45
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x1676a0
movq -0x20(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0x16af60
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x15b820
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x15d260
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x15b820
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)