// File: /workspace/github_c_makefile_star2_O3/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 0x6cfc7
leaq 0xb88d9(%rip), %rsi     # 0x125887
movq %r14, %rdi
callq 0xe9b74
testq %rax, %rax
je 0x6cfc7
movq %rax, %rdi
callq 0xe9c2f
movq %rax, 0x8(%rbx)
cmpq $0x0, 0x10(%rbx)
jne 0x6cfee
leaq 0xb88d6(%rip), %rsi     # 0x1258ab
movq %r14, %rdi
callq 0xe9b74
testq %rax, %rax
je 0x6cfee
movq %rax, %rdi
callq 0xe9c2f
movq %rax, 0x10(%rbx)
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x6d00d
movq 0x10(%rbx), %rdx
testq %rdx, %rdx
jle 0x6d016
movq %r14, %rdi
movq %rax, %rsi
callq 0xe8f88
jmp 0x6d02a
leaq 0xb87c1(%rip), %rsi     # 0x1257d5
jmp 0x6d05a
leaq 0xb8736(%rip), %rsi     # 0x125753
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x8(%rbx), %rax
testq %rax, %rax
jle 0x6d053
movq 0xa8(%r14), %rcx
cmpq %rcx, %rax
jg 0x6d053
movq 0x10(%rbx), %rsi
testq %rsi, %rsi
setns %dl
cmpq %rcx, %rsi
setle %cl
testb %cl, %dl
jne 0x6d089
leaq 0xb87a6(%rip), %rsi     # 0x125800
xorl %r15d, %r15d
movq %r14, %rdi
xorl %eax, %eax
callq 0xe9295
xorl %r12d, %r12d
movq %r15, %rdi
callq 0xf052b
movq %r12, %rdi
callq 0xf228d
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testq %rsi, %rsi
je 0x6d0a0
imulq %rax, %rsi
movq %rsi, 0x20(%rbx)
movq %r14, %rdi
movl $0x1, %edx
jmp 0x6d0af
movq $0x5f5e100, 0x20(%rbx)  # imm = 0x5F5E100
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xf09c5
movq %rax, %r15
cmpb $0x0, (%rbx)
movq 0x18(%rbx), %rcx
je 0x6d0d0
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x6d234
jmp 0x6d14c
movq %rcx, 0x10(%rsp)
movb $0x1, 0x428(%rbx)
movq 0x18(%r14), %rdi
cmpq %rcx, 0x18(%rdi)
jle 0x6d144
xorl %r12d, %r12d
cmpq 0x20(%rbx), %r12
jge 0x6d144
leaq 0x10(%rsp), %r13
movq %r13, %rsi
callq 0xeeb4b
movl %eax, %ebp
movq 0x18(%r14), %rdi
movq %r13, %rsi
callq 0xeeb4b
movzbl %al, %r13d
testb %r13b, %r13b
je 0x6d132
cmpb 0x428(%rbx), %bpl
jbe 0x6d123
movb %bpl, 0x428(%rbx)
movzbl %bpl, %esi
movq %r15, %rdi
movq %r13, %rdx
callq 0xf0eba
addq %r13, %r12
movq 0x10(%rsp), %rax
movq 0x18(%r14), %rdi
cmpq 0x18(%rdi), %rax
jl 0x6d0e9
movq %r15, %rdi
callq 0xf08ba
movq 0x18(%r15), %rdx
leaq 0xb86be(%rip), %rsi     # 0x125815
movq %r14, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x10(%rbx), %rdx
testq %rdx, %rdx
jne 0x6d199
movq 0x18(%r15), %rdi
movq 0x8(%rbx), %rsi
callq 0xe9cb4
cqto
idivq 0x8(%rbx)
movq %rax, 0x10(%rbx)
leaq 0xb86a5(%rip), %rsi     # 0x12582d
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x10(%rbx), %rdx
movq 0x8(%rbx), %rsi
movq %r14, %rdi
callq 0xf1db1
testl %eax, %eax
je 0x6d067
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 0xf266a
movq %rax, %r12
cmpb $0x0, 0x1(%rbx)
jne 0x6d1fc
leaq 0xb865c(%rip), %rsi     # 0x125839
movq %r14, %rdi
xorl %eax, %eax
callq 0xe9393
leaq 0x28(%rbx), %rdi
movzbl 0x428(%rbx), %esi
incq %rsi
xorl %edx, %edx
callq 0xf3bdb
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 0xf2f79
movq %r12, %rdi
xorl %esi, %esi
movl $0x6, %edx
callq 0xf22dc
jmp 0x6d06a