// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/fli.c
// Function: do_chunk_FLI_FLC
static void do_chunk_FLI_FLC(deark *c, lctx *d, struct chunk_info_type *ci)
{
	i64 pos = ci->dpos;
	i64 scr_width, scr_height;
	i64 n;
	struct image_ctx_type *ictx = NULL;
	double fps = 0.0;
	int can_decode_images = 1;

	d->main_chunktype = ci->chunktype;
	if(d->main_chunktype==CHUNKTYPE_FLI) {
		de_declare_fmt(c, "FLI");
	}
	else if(d->main_chunktype==CHUNKTYPE_FLC) {
		de_declare_fmt(c, "FLC");
	}
	else {
		de_warn(c, "Unrecognized signature (0x%04x). This might not be a FLI/FLC file.",
			d->main_chunktype);
	}

	d->num_frames = de_getu16le_p(&pos);
	de_dbg(c, "num frames: %d", (int)d->num_frames);

	scr_width = de_getu16le_p(&pos);
	scr_height = de_getu16le_p(&pos);
	de_dbg(c, "screen dimensions: %"I64_FMT DE_CHAR_TIMES "%"I64_FMT, scr_width, scr_height);

	d->depth = (int)de_getu16le_p(&pos);
	de_dbg(c, "depth: %d", d->depth);
	if(d->depth==0) d->depth = 8;
	pos += 2; // flags

	n = de_getu32le_p(&pos);
	if(ci->chunktype==CHUNKTYPE_FLI && n!=0) {
		fps = 70.0/(double)n;
	}
	else if(ci->chunktype==CHUNKTYPE_FLC && n!=0) {
		fps = 1000.0/(double)n;
	}
	de_dbg(c, "speed: %"I64_FMT" (%.4f frames/sec)", n, fps);

	pos += 2; // reserved

	if(ci->chunktype==CHUNKTYPE_FLI) {
		if(scr_width==320 && scr_height==200) {
			d->aspect_x = 6;
			d->aspect_y = 5;
		}
	}

	if(ci->chunktype==CHUNKTYPE_FLC) {
		read_timestamp(c, d, pos, &d->create_timestamp, "create");
		pos += 4;
		dbg_id(c, d, pos, "creator ID");
		pos += 4;

		read_timestamp(c, d, pos, &d->mod_timestamp, "mod");
		pos += 4;
		dbg_id(c, d, pos, "updater ID");
		pos += 4;

		d->aspect_x = de_getu16le_p(&pos);
		d->aspect_y = de_getu16le_p(&pos);
		de_dbg(c, "aspect ratio: %d,%d", (int)d->aspect_x, (int)d->aspect_y);
	}

	can_decode_images = 1;
	if(d->depth != 8) {
		de_err(c, "Unsupported depth: %d", d->depth);
		can_decode_images = 0;
	}
	if(!de_good_image_dimensions(c, scr_width, scr_height)) {
		can_decode_images = 0;
	}

	if(can_decode_images) {
		ictx = create_image_ctx(c, d, scr_width, scr_height);
		ci->ictx = ictx;
		if(d->aspect_x && d->aspect_y) {
			ictx->density.code = DE_DENSITY_UNK_UNITS;
			ictx->density.xdens = (double)d->aspect_x;
			ictx->density.ydens = (double)d->aspect_y;
		}
	}

	pos = ci->pos + 128;
	do_sequence_of_chunks(c, d, ci, pos, -1);

	destroy_image_ctx(c, ictx);
	ci->ictx = NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r14
movq 0x20(%rdx), %rax
movq %rax, 0x8(%rsp)
movl (%rdx), %edx
movl %edx, (%rsi)
cmpl $0xaf12, %edx           # imm = 0xAF12
je 0x5a983
cmpl $0xaf11, %edx           # imm = 0xAF11
jne 0x5a994
leaq 0xc7588(%rip), %rsi     # 0x121f09
jmp 0x5a98a
leaq 0xc7583(%rip), %rsi     # 0x121f0d
movq %r14, %rdi
callq 0xeb6d6
jmp 0x5a9a5
leaq 0xc7623(%rip), %rsi     # 0x121fbe
movq %r14, %rdi
xorl %eax, %eax
callq 0xe9b27
movq 0x18(%r14), %rdi
leaq 0x8(%rsp), %rbp
movq %rbp, %rsi
callq 0xef1ed
movq %rax, 0x8(%r15)
leaq 0xc7640(%rip), %rsi     # 0x122001
movq %r14, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%r14), %rdi
movq %rbp, %rsi
callq 0xef1ed
movq %rax, %r12
movq 0x18(%r14), %rdi
movq %rbp, %rsi
callq 0xef1ed
movq %rax, %r13
leaq 0xc761e(%rip), %rsi     # 0x122010
movq %r14, %rdi
movq %r12, 0x10(%rsp)
movq %r12, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%r14), %rdi
movq %rbp, %rsi
callq 0xef1ed
movl %eax, 0x38(%r15)
leaq 0xc7661(%rip), %rsi     # 0x12207f
movq %r14, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
cmpl $0x0, 0x38(%r15)
jne 0x5aa39
movl $0x8, 0x38(%r15)
leaq 0x8(%rsp), %rsi
addq $0x2, (%rsi)
movq 0x18(%r14), %rdi
callq 0xef3a0
movl (%rbx), %ecx
cmpl $0xaf11, %ecx           # imm = 0xAF11
sete %dl
testq %rax, %rax
setne %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0x5aa75
cvtsi2sd %rax, %xmm1
movsd 0xc73cd(%rip), %xmm0    # 0x121e40
jmp 0x5aa9c
testq %rax, %rax
setne %dl
cmpl $0xaf12, %ecx           # imm = 0xAF12
sete %cl
andb %dl, %cl
xorpd %xmm0, %xmm0
cmpb $0x1, %cl
jne 0x5aaa0
cvtsi2sd %rax, %xmm1
movsd 0xc739c(%rip), %xmm0    # 0x121e38
divsd %xmm1, %xmm0
leaq 0xc7585(%rip), %rsi     # 0x12202c
movq %r14, %rdi
movq %rax, %rdx
movb $0x1, %al
callq 0xe9226
movq 0x8(%rsp), %rsi
addq $0x2, %rsi
movq %rsi, 0x8(%rsp)
movl (%rbx), %eax
cmpl $0xaf12, %eax           # imm = 0xAF12
je 0x5ab0c
cmpl $0xaf11, %eax           # imm = 0xAF11
jne 0x5abb5
movq 0x10(%rsp), %rcx
xorq $0x140, %rcx            # imm = 0x140
movq %r13, %rdx
xorq $0xc8, %rdx
orq %rcx, %rdx
jne 0x5ab01
movq $0x6, 0x40(%r15)
movq $0x5, 0x48(%r15)
cmpl $0xaf12, %eax           # imm = 0xAF12
jne 0x5abb5
leaq 0x18(%r15), %rdx
leaq 0xbd6cf(%rip), %rcx     # 0x1181e6
movq %r14, %rdi
callq 0x5b75a
leaq 0x8(%rsp), %rbp
movq (%rbp), %rsi
addq $0x4, %rsi
movq %rsi, (%rbp)
leaq 0xc7512(%rip), %rdx     # 0x122049
movq %r14, %rdi
callq 0x5b838
movq (%rbp), %rsi
addq $0x4, %rsi
movq %rsi, (%rbp)
leaq 0x28(%r15), %rdx
leaq 0xbd548(%rip), %rcx     # 0x11809e
movq %r14, %rdi
callq 0x5b75a
movq (%rbp), %rsi
addq $0x4, %rsi
movq %rsi, (%rbp)
leaq 0xc74e3(%rip), %rdx     # 0x122054
movq %r14, %rdi
callq 0x5b838
addq $0x4, (%rbp)
movq 0x18(%r14), %rdi
movq %rbp, %rsi
callq 0xef1ed
movq %rax, 0x40(%r15)
movq 0x18(%r14), %rdi
movq %rbp, %rsi
callq 0xef1ed
movq %rax, 0x48(%r15)
movl 0x40(%r15), %edx
leaq 0xc74b6(%rip), %rsi     # 0x12205f
movq %r14, %rdi
movl %eax, %ecx
xorl %eax, %eax
callq 0xe9226
movl 0x38(%r15), %r12d
cmpl $0x8, %r12d
je 0x5abd3
leaq 0xc74ad(%rip), %rsi     # 0x122073
movq %r14, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xe9df7
movq %r14, %rdi
movq 0x10(%rsp), %rsi
movq %r13, %rdx
callq 0xf241d
xorl %ebp, %ebp
cmpl $0x8, %r12d
jne 0x5ac56
testl %eax, %eax
je 0x5ac56
movl $0x438, %esi            # imm = 0x438
movq %r14, %rdi
callq 0xe969b
movq %rax, %rbp
movq 0x10(%rsp), %rsi
movq %rsi, (%rax)
movq %r13, 0x8(%rax)
movq %r14, %rdi
movq %r13, %rdx
movl $0x3, %ecx
callq 0xf2cab
movq %rax, 0x30(%rbp)
movq %rbp, 0x48(%rbx)
movq 0x40(%r15), %rax
testq %rax, %rax
je 0x5ac56
movq 0x48(%r15), %rcx
testq %rcx, %rcx
je 0x5ac56
movl $0x1, 0x18(%rbp)
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movsd %xmm0, 0x20(%rbp)
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
movsd %xmm0, 0x28(%rbp)
movq 0x10(%rbx), %rcx
subq $-0x80, %rcx
movq %rcx, 0x8(%rsp)
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
movq $-0x1, %r8
callq 0x5b8c6
movq %r14, %rdi
movq %rbp, %rsi
callq 0x5b9cf
movq $0x0, 0x48(%rbx)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq