// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/misc2.c
// Function: do_fastgraph_internal
static void do_fastgraph_internal(deark *c, struct fastgraph_ctx *d)
{
	dbuf *unc_pixels = NULL;
	de_bitmap *img = NULL;
	const char *s;

	if(d->w==0) {
		s = de_get_ext_option(c, "width");
		if(s) {
			d->w = de_atoi64(s);
		}
	}
	if(d->h==0) {
		s = de_get_ext_option(c, "height");
		if(s) {
			d->h = de_atoi64(s);
		}
	}

	if(d->w==0) {
		de_err(c, "Image width unknown. Try \"-opt width=...\".");
		goto done;
	}

	if(d->h>0) {
		de_dbg_dimensions(c, d->w, d->h);
	}
	else {
		de_dbg(c, "width: %"I64_FMT, d->w);
	}

	if(d->w<1 || d->w>c->max_image_dimension ||
		d->h<0 || d->h>c->max_image_dimension)
	{
		de_err(c, "Bad image dimensions");
		goto done;
	}

	if(d->h==0) {
		d->max_unc_bytes = DE_MAX_SANE_OBJECT_SIZE;
		unc_pixels = dbuf_create_membuf(c, 0, 0);
	}
	else {
		d->max_unc_bytes = d->w * d->h;
		unc_pixels = dbuf_create_membuf(c, d->max_unc_bytes, 0x1);
	}

	if(d->is_ppr) {
		decompress_fastgraph_ppr(c, d, unc_pixels, d->img_startpos);
	}
	else {
		decompress_fastgraph_spr(c, d, unc_pixels, d->img_startpos);
	}

	de_dbg(c, "decompressed %"I64_FMT" pixels", unc_pixels->len);
	if(d->h==0) {
		d->h = de_pad_to_n(unc_pixels->len, d->w) / d->w;
		de_dbg(c, "height: %"I64_FMT, d->h);
	}

	if(!de_good_image_dimensions(c, d->w, d->h)) goto done;

	img = de_bitmap_create(c, d->w, d->h, d->pal_is_set?3:1);
	if(!d->pal_is_set) {
		de_info(c, "Note: Color map unknown. Rendering as grayscale.");
		de_make_grayscale_palette(d->pal, 1+(i64)d->max_color_used, 0);
	}

	de_convert_image_paletted(unc_pixels, 0, 8, d->w, d->pal, img, 0);
	de_bitmap_write_to_file(img, NULL, DE_CREATEFLAG_FLIP_IMAGE |
		DE_CREATEFLAG_OPT_IMAGE);

done:
	dbuf_close(unc_pixels);
	de_bitmap_destroy(img);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %rbx
movq %rdi, %r14
cmpq $0x0, 0x8(%rsi)
jne 0x6d20c
leaq 0xb8464(%rip), %rsi     # 0x125657
movq %r14, %rdi
callq 0xea6cb
testq %rax, %rax
je 0x6d20c
movq %rax, %rdi
callq 0xea786
movq %rax, 0x8(%rbx)
cmpq $0x0, 0x10(%rbx)
jne 0x6d233
leaq 0xb8461(%rip), %rsi     # 0x12567b
movq %r14, %rdi
callq 0xea6cb
testq %rax, %rax
je 0x6d233
movq %rax, %rdi
callq 0xea786
movq %rax, 0x10(%rbx)
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x6d252
movq 0x10(%rbx), %rdx
testq %rdx, %rdx
jle 0x6d25b
movq %r14, %rdi
movq %rax, %rsi
callq 0xe9b05
jmp 0x6d26f
leaq 0xb834c(%rip), %rsi     # 0x1255a5
jmp 0x6d29f
leaq 0xb82c1(%rip), %rsi     # 0x125523
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq 0x8(%rbx), %rax
testq %rax, %rax
jle 0x6d298
movq 0xa8(%r14), %rcx
cmpq %rcx, %rax
jg 0x6d298
movq 0x10(%rbx), %rsi
testq %rsi, %rsi
setns %dl
cmpq %rcx, %rsi
setle %cl
testb %cl, %dl
jne 0x6d2ce
leaq 0xb8331(%rip), %rsi     # 0x1255d0
xorl %r15d, %r15d
movq %r14, %rdi
xorl %eax, %eax
callq 0xe9df7
xorl %r12d, %r12d
movq %r15, %rdi
callq 0xf0b13
movq %r12, %rdi
callq 0xf28d4
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testq %rsi, %rsi
je 0x6d2e5
imulq %rax, %rsi
movq %rsi, 0x20(%rbx)
movq %r14, %rdi
movl $0x1, %edx
jmp 0x6d2f4
movq $0x5f5e100, 0x20(%rbx)  # imm = 0x5F5E100
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xf0faf
movq %rax, %r15
cmpb $0x0, (%rbx)
movq 0x18(%rbx), %rcx
je 0x6d315
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x6d47a
jmp 0x6d394
leaq 0x10(%rsp), %rax
movq %rcx, (%rax)
movb $0x1, 0x428(%rbx)
xorl %r12d, %r12d
movq 0x10(%rsp), %rax
movq 0x18(%r14), %rdi
cmpq 0x18(%rdi), %rax
jge 0x6d386
cmpq 0x20(%rbx), %r12
jge 0x6d386
leaq 0x10(%rsp), %r13
movq %r13, %rsi
callq 0xef14d
movl %eax, %ebp
movq 0x18(%r14), %rdi
movq %r13, %rsi
callq 0xef14d
movzbl %al, %r13d
testb %r13b, %r13b
je 0x6d37f
cmpb 0x428(%rbx), %bpl
jbe 0x6d370
movb %bpl, 0x428(%rbx)
movzbl %bpl, %esi
movq %r15, %rdi
movq %r13, %rdx
callq 0xf14bd
addq %r13, %r12
movb $0x1, %al
jmp 0x6d388
xorl %eax, %eax
testb %al, %al
jne 0x6d327
movq %r15, %rdi
callq 0xf0ea4
movq 0x18(%r15), %rdx
leaq 0xb8246(%rip), %rsi     # 0x1255e5
movq %r14, %rdi
xorl %eax, %eax
callq 0xe9226
cmpq $0x0, 0x10(%rbx)
jne 0x6d3db
movq 0x18(%r15), %rdi
movq 0x8(%rbx), %rsi
callq 0xea80b
cqto
idivq 0x8(%rbx)
movq %rax, 0x10(%rbx)
leaq 0xb822f(%rip), %rsi     # 0x1255fd
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq 0x8(%rbx), %rsi
movq 0x10(%rbx), %rdx
movq %r14, %rdi
callq 0xf241d
testl %eax, %eax
je 0x6d2ac
movq 0x8(%rbx), %rsi
movq 0x10(%rbx), %rdx
xorl %eax, %eax
cmpb $0x0, 0x1(%rbx)
setne %al
leal 0x1(,%rax,2), %ecx
movq %r14, %rdi
callq 0xf2cab
movq %rax, %r12
cmpb $0x0, 0x1(%rbx)
jne 0x6d442
leaq 0xb81e6(%rip), %rsi     # 0x125609
movq %r14, %rdi
xorl %eax, %eax
callq 0xe9ef5
leaq 0x28(%rbx), %rdi
movzbl 0x428(%rbx), %esi
incq %rsi
xorl %edx, %edx
callq 0xf41f4
movq 0x8(%rbx), %rcx
addq $0x28, %rbx
movl $0x0, (%rsp)
movl $0x8, %edx
movq %r15, %rdi
xorl %esi, %esi
movq %rbx, %r8
movq %r12, %r9
callq 0xf35b8
movq %r12, %rdi
xorl %esi, %esi
movl $0x6, %edx
callq 0xf2923
jmp 0x6d2af