// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/sunras.c
// Function: de_run_sunras
static void de_run_sunras(deark *c, de_module_params *mparams)
{
	lctx *d = NULL;
	dbuf *unc_pixels = NULL;
	i64 pos;
	i64 bits_per_row;
	int saved_indent_level;
	de_dbg_indent_save(c, &saved_indent_level);

	d = de_malloc(c, sizeof(lctx));
	handle_options(c, d);

	pos = 0;
	read_header(c, d, pos);
	pos += 32;

	if(pos >= c->infile->len) goto done;

	if(d->maplen > 0)
		de_dbg(c, "colormap at %"I64_FMT, pos);

	de_dbg_indent(c, 1);

	if(d->maptype==RMT_EQUAL_RGB) {
		if(d->depth<=8) {
			d->is_paletted = 1;
			do_read_palette(c, d, pos);
		}
		else {
			de_err(c, "This type of image is not supported");
			goto done;
		}
	}
	else if(d->maptype==RMT_NONE) {
		if(d->depth<=8) {
			d->is_grayscale = 1;
			de_make_grayscale_palette(d->pal, ((i64)1)<<d->depth, d->depth==1 ? 1 : 0);
		}
	}
	else {
		// TODO: Support RMT_RAW
		de_err(c, "Colormap type (%u) is not supported", d->maptype);
		goto done;
	}
	pos += d->maplen;
	de_dbg_indent(c, -1);

	if(pos >= c->infile->len) goto done;
	de_dbg(c, "image data at %"I64_FMT, pos);
	de_dbg_indent(c, 1);

	bits_per_row = de_pad_to_n(d->npwidth * (i64)d->depth, 16);
	d->rowspan = bits_per_row / 8;
	d->pdwidth = bits_per_row / (i64)d->depth;
	d->unc_pixels_size = d->rowspan * d->height;

	if(d->imgtype>5) {
		de_err(c, "This type of image (%u) is not supported", d->imgtype);
		goto done;
	}

	if((d->imgtype==RT_STANDARD || d->imgtype==RT_FORMAT_RGB) && d->imglen!=d->unc_pixels_size) {
		de_warn(c, "Inconsistent image length: reported=%"I64_FMT", calculated=%"I64_FMT,
			d->imglen, d->unc_pixels_size);
	}

	if(d->is_compressed) {
		unc_pixels = dbuf_create_membuf(c, d->unc_pixels_size, 0x1);
		dbuf_enable_wbuffer(unc_pixels);
		do_decompress_image(c, d, pos, c->infile->len - pos, unc_pixels);
		dbuf_flush(unc_pixels);
	}
	else {
		unc_pixels = dbuf_open_input_subfile(c->infile, pos, c->infile->len - pos);
	}

	do_image(c, d, unc_pixels);
	de_dbg_indent(c, -1);

done:
	dbuf_close(unc_pixels);
	de_free(c, d);
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x34(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rdi
movl $0x478, %esi            # imm = 0x478
callq 0x15b780
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x127ac0
movq $0x0, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x127b70
movq -0x28(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jl 0x127758
jmp 0x127a16
movq -0x18(%rbp), %rax
cmpq $0x0, 0x48(%rax)
jle 0x127779
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rdx
leaq 0x9f8a5(%rip), %rsi     # 0x1c7017
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x18(%rbp), %rax
cmpl $0x1, 0x40(%rax)
jne 0x1277cf
movq -0x18(%rbp), %rax
cmpl $0x8, 0x18(%rax)
ja 0x1277b6
movq -0x18(%rbp), %rax
movb $0x1, 0x60(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x127d70
jmp 0x1277cd
movq -0x8(%rbp), %rdi
leaq 0x9949e(%rip), %rsi     # 0x1c0c5f
movb $0x0, %al
callq 0x15bfb0
jmp 0x127a16
jmp 0x12783f
movq -0x18(%rbp), %rax
cmpl $0x0, 0x40(%rax)
jne 0x12781f
movq -0x18(%rbp), %rax
cmpl $0x8, 0x18(%rax)
ja 0x12781d
movq -0x18(%rbp), %rax
movb $0x1, 0x61(%rax)
movq -0x18(%rbp), %rdi
addq $0x64, %rdi
movq -0x18(%rbp), %rax
movl 0x18(%rax), %eax
movl %eax, %ecx
movl $0x1, %esi
shlq %cl, %rsi
movq -0x18(%rbp), %rax
movl 0x18(%rax), %ecx
xorl %edx, %edx
movl $0x1, %eax
cmpl $0x1, %ecx
cmovel %eax, %edx
callq 0x16d580
jmp 0x12783d
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x40(%rax), %edx
leaq 0x9f7f6(%rip), %rsi     # 0x1c7027
movb $0x0, %al
callq 0x15bfb0
jmp 0x127a16
jmp 0x12783f
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x28(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jl 0x127874
jmp 0x127a16
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rdx
leaq 0x9f1e2(%rip), %rsi     # 0x1c6a65
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movq -0x18(%rbp), %rax
movl 0x18(%rax), %eax
imulq %rax, %rdi
movl $0x10, %esi
callq 0x15ce90
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movl $0x8, %ecx
cqto
idivq %rcx
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x50(%rax)
movq -0x30(%rbp), %rax
movq -0x18(%rbp), %rcx
movl 0x18(%rcx), %ecx
cqto
idivq %rcx
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rcx
movq -0x18(%rbp), %rax
imulq 0x8(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x58(%rax)
movq -0x18(%rbp), %rax
cmpl $0x5, 0x1c(%rax)
jbe 0x12792d
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x1c(%rax), %edx
leaq 0x9f72a(%rip), %rsi     # 0x1c704b
movb $0x0, %al
callq 0x15bfb0
jmp 0x127a16
movq -0x18(%rbp), %rax
cmpl $0x1, 0x1c(%rax)
je 0x127941
movq -0x18(%rbp), %rax
cmpl $0x3, 0x1c(%rax)
jne 0x127975
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
movq -0x18(%rbp), %rcx
cmpq 0x58(%rcx), %rax
je 0x127975
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rdx
movq -0x18(%rbp), %rax
movq 0x58(%rax), %rcx
leaq 0x9f706(%rip), %rsi     # 0x1c7074
movb $0x0, %al
callq 0x15bc10
movq -0x18(%rbp), %rax
cmpb $0x0, 0x20(%rax)
je 0x1279d2
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x58(%rax), %rsi
movl $0x1, %edx
callq 0x168020
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
callq 0x167b90
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rcx
subq -0x28(%rbp), %rcx
movq -0x20(%rbp), %r8
callq 0x127e80
movq -0x20(%rbp), %rdi
callq 0x167c30
jmp 0x1279f7
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rsi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rdx
subq -0x28(%rbp), %rdx
callq 0x1691b0
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x127f70
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x20(%rbp), %rdi
callq 0x1676a0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x15b820
movq -0x8(%rbp), %rdi
movl -0x34(%rbp), %esi
callq 0x15b5a0
addq $0x40, %rsp
popq %rbp
retq
nop