// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/gif.c
// Function: do_image_internal
static int do_image_internal(deark *c, lctx *d,
	struct gif_image_data *gi, i64 pos1, i64 *bytesused)
{
	int retval = 0;
	i64 pos;
	int bypp;
	int saved_indent_level;
	unsigned int lzw_min_code_size;
	i64 npixels_total;
	dbuf *custom_outf = NULL;
	struct de_lzw_params delzwp;
	struct de_dfilter_out_params dcmpro;
	struct de_dfilter_results dres;
	struct my_giflzw_userdata u;

	de_dbg_indent_save(c, &saved_indent_level);
	pos = pos1;
	*bytesused = 0;
	gi->failure_flag = 0;
	gi->dfctx = NULL;

	do_read_image_header(c, d, gi, pos);
	pos += 9;

	if(gi->has_local_color_table) {
		de_dbg(c, "local color table at %d", (int)pos);
		de_dbg_indent(c, 1);
		do_read_color_table(c, d, pos, gi->local_color_table_size, gi->local_ct);
		de_dbg_indent(c, -1);
		pos += 3*gi->local_color_table_size;
	}

	if(c->infile->len-pos < 1) {
		on_unexpected_eof(c, d);
		goto done;
	}
	de_dbg(c, "image data at %d", (int)pos);
	de_dbg_indent(c, 1);
	lzw_min_code_size = (unsigned int)de_getbyte(pos++);
	de_dbg(c, "lzw min code size: %u", lzw_min_code_size);

	// Using a failure_flag variable like this is ugly, but I don't like any
	// of the other options either, short of a major redesign of this module.
	// We have to continue to parse the image segment, even after most errors,
	// so that we know where it ends.

	if(gi->width==0 || gi->height==0) {
		// This doesn't seem to be forbidden by the spec.
		de_warn(c, "Image has zero size (%d"DE_CHAR_TIMES"%d)", (int)gi->width, (int)gi->height);
		gi->failure_flag = 1;
	}
	else if(!de_good_image_dimensions(c, gi->width, gi->height)) {
		gi->failure_flag = 1;
	}

	if(d->gce && d->gce->trns_color_idx_valid)
		bypp = 4;
	else
		bypp = 3;

	if(gi->failure_flag) {
		gi->img = de_bitmap_create(c, 1, 1, 1);
	}
	else {
		gi->img = de_bitmap_create(c, gi->width, gi->height, bypp);
	}

	if(gi->interlaced && !gi->failure_flag) {
		do_create_interlace_map(c, d, gi);
	}

	npixels_total = gi->width * gi->height;

	de_dfilter_init_objects(c, NULL, &dcmpro, &dres);
	de_zeromem(&delzwp, sizeof(struct de_lzw_params));
	de_zeromem(&u, sizeof(struct my_giflzw_userdata));
	custom_outf = dbuf_create_custom_dbuf(c, 0, 0);
	dbuf_enable_wbuffer(custom_outf);
	u.c = c;
	u.d = d;
	u.gi = gi;
	custom_outf->userdata_for_customwrite = (void*)&u;
	custom_outf->customwrite_fn = my_giflzw_write_cb;
	delzwp.fmt = DE_LZWFMT_GIF;
	delzwp.gif_root_code_size = lzw_min_code_size;
	dcmpro.f = custom_outf;
	dcmpro.len_known = 1;
	dcmpro.expected_len = npixels_total;

	gi->dfctx = de_dfilter_create(c, dfilter_lzw_codec, &delzwp, &dcmpro, &dres);

	do_read_subblocks_p(c, d, c->infile, callback_for_image_subblock, (void*)gi, &pos);
	*bytesused = pos - pos1;
	retval = 1;

	if(gi->failure_flag) {
		goto done;
	}

	de_dfilter_finish(gi->dfctx);
	dbuf_flush(custom_outf);

	if(dres.errcode) {
		de_err(c, "Decompression failed: %s", de_dfilter_get_errmsg(c, &dres));
		goto done;
	}

	if(gi->pixels_set < npixels_total) {
		de_warn(c, "Expected %"I64_FMT" pixels, only found %"I64_FMT, npixels_total, gi->pixels_set);
	}

done:
	if(gi->failure_flag) {
		de_bitmap_destroy(gi->img);
		gi->img = NULL;
	}
	if(gi->dfctx) {
		de_dfilter_destroy(gi->dfctx);
		gi->dfctx = NULL;
	}
	dbuf_close(custom_outf);
	de_dbg_indent_restore(c, saved_indent_level);
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x15b580
movq -0x20(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rax
movq $0x0, (%rax)
movq -0x18(%rbp), %rax
movl $0x0, 0x38(%rax)
movq -0x18(%rbp), %rax
movq $0x0, 0x40(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x38(%rbp), %rcx
callq 0x752e0
movq -0x38(%rbp), %rax
addq $0x9, %rax
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, 0x34(%rax)
je 0x74f06
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movl %eax, %edx
leaq 0x136c40(%rip), %rsi    # 0x1abaf1
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rcx
movq -0x18(%rbp), %r8
addq $0x58, %r8
callq 0x73430
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x18(%rbp), %rax
imulq $0x3, 0x48(%rax), %rax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rax
subq -0x38(%rbp), %rax
cmpq $0x1, %rax
jge 0x74f2e
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x72dd0
jmp 0x75278
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movl %eax, %edx
leaq 0x136bca(%rip), %rsi    # 0x1abb09
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
movq %rsi, %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
callq 0x165060
movzbl %al, %eax
movl %eax, -0x44(%rbp)
movq -0x8(%rbp), %rdi
movl -0x44(%rbp), %edx
leaq 0x136b96(%rip), %rsi    # 0x1abb1a
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x74fa1
movq -0x18(%rbp), %rax
cmpq $0x0, 0x20(%rax)
jne 0x74fd4
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl %eax, %edx
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movl %eax, %ecx
leaq 0x136b70(%rip), %rsi    # 0x1abb30
movb $0x0, %al
callq 0x15bc10
movq -0x18(%rbp), %rax
movl $0x1, 0x38(%rax)
jmp 0x74fff
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rdx
callq 0x16a850
cmpl $0x0, %eax
jne 0x74ffd
movq -0x18(%rbp), %rax
movl $0x1, 0x38(%rax)
jmp 0x74fff
movq -0x10(%rbp), %rax
cmpq $0x0, 0x440(%rax)
je 0x7502a
movq -0x10(%rbp), %rax
movq 0x440(%rax), %rax
movzbl 0x1(%rax), %eax
cmpl $0x0, %eax
je 0x7502a
movl $0x4, -0x3c(%rbp)
jmp 0x75031
movl $0x3, -0x3c(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, 0x38(%rax)
je 0x7505d
movq -0x8(%rbp), %rdi
movl $0x1, %edx
movl $0x1, %ecx
movq %rdx, %rsi
callq 0x16b750
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
jmp 0x75083
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rdx
movl -0x3c(%rbp), %ecx
callq 0x16b750
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0x18(%rbp), %rax
cmpl $0x0, 0x30(%rax)
je 0x750a8
movq -0x18(%rbp), %rax
cmpl $0x0, 0x38(%rax)
jne 0x750a8
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x75540
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x18(%rbp), %rcx
imulq 0x20(%rcx), %rax
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
leaq -0x88(%rbp), %rdx
leaq -0xe8(%rbp), %rcx
callq 0x1773f0
leaq -0x6c(%rbp), %rdi
xorl %esi, %esi
movl $0x14, %edx
callq 0x19140
leaq -0x100(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x19140
movq -0x8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x169220
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rdi
callq 0x167b90
movq -0x8(%rbp), %rax
movq %rax, -0x100(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xf8(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq -0x58(%rbp), %rax
leaq -0x100(%rbp), %rcx
movq %rcx, 0xa8(%rax)
movq -0x58(%rbp), %rax
leaq 0x50b(%rip), %rcx       # 0x75660
movq %rcx, 0xb0(%rax)
movl $0x2, -0x6c(%rbp)
movl -0x44(%rbp), %eax
movl %eax, -0x64(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x88(%rbp)
movb $0x1, -0x80(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x8(%rbp), %rdi
leaq 0x10f275(%rip), %rsi    # 0x184400
leaq -0x6c(%rbp), %rdx
leaq -0x88(%rbp), %rcx
leaq -0xe8(%rbp), %r8
callq 0x1775a0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdx
movq -0x18(%rbp), %r8
leaq 0x528(%rip), %rcx       # 0x756f0
leaq -0x38(%rbp), %r9
callq 0x73ce0
movq -0x38(%rbp), %rcx
subq -0x20(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movl $0x1, -0x2c(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, 0x38(%rax)
je 0x751f6
jmp 0x75278
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rdi
callq 0x1776f0
movq -0x58(%rbp), %rdi
callq 0x167c30
cmpl $0x0, -0xe8(%rbp)
je 0x7524a
movq -0x8(%rbp), %rax
movq %rax, -0x108(%rbp)
movq -0x8(%rbp), %rdi
leaq -0xe8(%rbp), %rsi
callq 0x177360
movq -0x108(%rbp), %rdi
movq %rax, %rdx
leaq 0x12adbc(%rip), %rsi    # 0x19fffd
movb $0x0, %al
callq 0x15bfb0
jmp 0x75278
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
cmpq -0x50(%rbp), %rax
jge 0x75276
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rdx
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rcx
leaq 0x1368de(%rip), %rsi    # 0x1abb4d
movb $0x0, %al
callq 0x15bc10
jmp 0x75278
movq -0x18(%rbp), %rax
cmpl $0x0, 0x38(%rax)
je 0x75299
movq -0x18(%rbp), %rax
movq (%rax), %rdi
callq 0x16af60
movq -0x18(%rbp), %rax
movq $0x0, (%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x40(%rax)
je 0x752bd
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rdi
callq 0x177720
movq -0x18(%rbp), %rax
movq $0x0, 0x40(%rax)
movq -0x58(%rbp), %rdi
callq 0x1676a0
movq -0x8(%rbp), %rdi
movl -0x40(%rbp), %esi
callq 0x15b5a0
movl -0x2c(%rbp), %eax
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
nop