// File: /workspace/github_c_makefile_star2_O2/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 0x5781b
cmpl $0xaf11, %edx           # imm = 0xAF11
jne 0x5782c
leaq 0xbd738(%rip), %rsi     # 0x114f51
jmp 0x57822
leaq 0xbd733(%rip), %rsi     # 0x114f55
movq %r14, %rdi
callq 0xe07f7
jmp 0x5783d
leaq 0xbd7d3(%rip), %rsi     # 0x115006
movq %r14, %rdi
xorl %eax, %eax
callq 0xdf05a
movq 0x18(%r14), %rdi
leaq 0x8(%rsp), %rbp
movq %rbp, %rsi
callq 0xe3fc4
movq %rax, 0x8(%r15)
leaq 0xbd7f0(%rip), %rsi     # 0x115049
movq %r14, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq 0x18(%r14), %rdi
movq %rbp, %rsi
callq 0xe3fc4
movq %rax, %r12
movq 0x18(%r14), %rdi
movq %rbp, %rsi
callq 0xe3fc4
movq %rax, %r13
leaq 0xbd7ce(%rip), %rsi     # 0x115058
movq %r14, %rdi
movq %r12, 0x10(%rsp)
movq %r12, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0xde896
movq 0x18(%r14), %rdi
movq %rbp, %rsi
callq 0xe3fc4
movl %eax, 0x38(%r15)
leaq 0xbd811(%rip), %rsi     # 0x1150c7
movq %r14, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
cmpl $0x0, 0x38(%r15)
jne 0x578d1
movl $0x8, 0x38(%r15)
leaq 0x8(%rsp), %rsi
addq $0x2, (%rsi)
movq 0x18(%r14), %rdi
callq 0xe40bf
movl (%rbx), %ecx
cmpl $0xaf11, %ecx           # imm = 0xAF11
sete %dl
testq %rax, %rax
setne %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0x5790d
cvtsi2sd %rax, %xmm1
movsd 0xbd57d(%rip), %xmm0    # 0x114e88
jmp 0x57934
testq %rax, %rax
setne %dl
cmpl $0xaf12, %ecx           # imm = 0xAF12
sete %cl
andb %dl, %cl
xorpd %xmm0, %xmm0
cmpb $0x1, %cl
jne 0x57938
cvtsi2sd %rax, %xmm1
movsd 0xbd54c(%rip), %xmm0    # 0x114e80
divsd %xmm1, %xmm0
leaq 0xbd735(%rip), %rsi     # 0x115074
movq %r14, %rdi
movq %rax, %rdx
movb $0x1, %al
callq 0xde896
movq 0x8(%rsp), %rsi
addq $0x2, %rsi
movq %rsi, 0x8(%rsp)
movl (%rbx), %eax
cmpl $0xaf12, %eax           # imm = 0xAF12
je 0x579a1
cmpl $0xaf11, %eax           # imm = 0xAF11
jne 0x57a4a
movq 0x10(%rsp), %rax
xorq $0x140, %rax            # imm = 0x140
movq %r13, %rcx
xorq $0xc8, %rcx
orq %rax, %rcx
jne 0x57a4a
movq $0x6, 0x40(%r15)
movq $0x5, 0x48(%r15)
jmp 0x57a4a
leaq 0x18(%r15), %rdx
leaq 0xb3882(%rip), %rcx     # 0x10b22e
movq %r14, %rdi
callq 0x584c5
leaq 0x8(%rsp), %rbp
movq (%rbp), %rsi
addq $0x4, %rsi
movq %rsi, (%rbp)
leaq 0xbd6c5(%rip), %rdx     # 0x115091
movq %r14, %rdi
callq 0x58599
movq (%rbp), %rsi
addq $0x4, %rsi
movq %rsi, (%rbp)
leaq 0x28(%r15), %rdx
leaq 0xb36fb(%rip), %rcx     # 0x10b0e6
movq %r14, %rdi
callq 0x584c5
movq (%rbp), %rsi
addq $0x4, %rsi
movq %rsi, (%rbp)
leaq 0xbd696(%rip), %rdx     # 0x11509c
movq %r14, %rdi
callq 0x58599
addq $0x4, (%rbp)
movq 0x18(%r14), %rdi
movq %rbp, %rsi
callq 0xe3fc4
movq %rax, 0x40(%r15)
movq 0x18(%r14), %rdi
movq %rbp, %rsi
callq 0xe3fc4
movq %rax, 0x48(%r15)
movl 0x40(%r15), %edx
leaq 0xbd669(%rip), %rsi     # 0x1150a7
movq %r14, %rdi
movl %eax, %ecx
xorl %eax, %eax
callq 0xde896
movl 0x38(%r15), %r12d
cmpl $0x8, %r12d
je 0x57a68
leaq 0xbd660(%rip), %rsi     # 0x1150bb
movq %r14, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xdf2a5
movq %r14, %rdi
movq 0x10(%rsp), %rsi
movq %r13, %rdx
callq 0xe6be1
xorl %ebp, %ebp
cmpl $0x8, %r12d
jne 0x57ac8
testl %eax, %eax
je 0x57ac8
movq %r14, %rdi
movq 0x10(%rsp), %rsi
movq %r13, %rdx
callq 0x58625
movq %rax, %rbp
movq %rax, 0x48(%rbx)
movq 0x40(%r15), %rax
testq %rax, %rax
je 0x57ac8
movq 0x48(%r15), %rcx
testq %rcx, %rcx
je 0x57ac8
movl $0x1, 0x18(%rbp)
cvtsi2sd %rax, %xmm0
movsd %xmm0, 0x20(%rbp)
cvtsi2sd %rcx, %xmm0
movsd %xmm0, 0x28(%rbp)
movq 0x10(%rbx), %rcx
subq $-0x80, %rcx
movq %rcx, 0x8(%rsp)
pushq $-0x1
popq %r8
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x5866f
movq %r14, %rdi
movq %rbp, %rsi
callq 0x5875a
andq $0x0, 0x48(%rbx)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq