// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/divgs.c
// Function: de_run_div_fpg
static void de_run_div_fpg(deark *c, de_module_params *mparams)
{
	struct div_ctx *d = NULL;
	struct div_image_ctx *md = NULL;
	i64 pos;

	d = de_malloc(c, sizeof(struct div_ctx));
	d->fmt = identify_div_fmt(c);
	if(d->fmt==DIVFMT_FPG) {
		de_declare_fmt(c, "DIV FPG");
	}
	else if(d->fmt==DIVFMT_F16) {
		de_declare_fmt(c, "DIV F16");
	}
	else {
		d->need_errmsg = 1;
		goto done;
	}
	d->is_archive = 1;
	d->input_encoding = de_get_input_encoding(c, NULL, DE_ENCODING_CP850);

	if(de_getbyte(7) != 0) {
		d->need_errmsg = 1;
		goto done;
	}

	pos = 8;
	if(d->fmt==DIVFMT_FPG) {
		div_read_pal_p(c, d, &pos);
	}

	de_dbg(c, "item sequence at: %"I64_FMT, pos);

	while(1) {
		if(pos+FPG_MIN_ITEM_LEN > c->infile->len) goto done;

		if(md) {
			div_image_destroy(c, md);
		}

		md = de_malloc(c, sizeof(struct div_image_ctx));
		md->imgpos = pos;
		div_fpg_image(c, d, md);
		if(md->item_len < FPG_MIN_ITEM_LEN) goto done;
		pos += md->item_len;
	}

done:
	div_image_destroy(c, md);
	if(d) {
		if(d->need_errmsg) {
			de_err(c, "Bad or unsupported FPG file");
		}
		de_free(c, d);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movl $0x408, %esi            # imm = 0x408
callq 0xdecaf
movq %rax, %r14
movq %rbx, %rdi
callq 0x4a95f
movb %al, 0x4(%r14)
cmpb $0x2, %al
je 0x4a67d
movzbl %al, %eax
cmpl $0x3, %eax
jne 0x4a6b1
leaq 0xc71d5(%rip), %rsi     # 0x111850
jmp 0x4a684
leaq 0xc71c4(%rip), %rsi     # 0x111848
movq %rbx, %rdi
callq 0xe07f7
movb $0x1, 0x5(%r14)
pushq $0xe
popq %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0xe08d2
movl %eax, (%r14)
movq 0x18(%rbx), %rdi
pushq $0x7
popq %rsi
callq 0xe3eb4
testb %al, %al
je 0x4a6be
movb $0x1, 0x6(%r14)
xorl %r13d, %r13d
jmp 0x4a90b
movq $0x8, 0x10(%rsp)
cmpb $0x2, 0x4(%r14)
jne 0x4a6e6
leaq 0x10(%rsp), %r15
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x4aa06
movq (%r15), %rbp
jmp 0x4a6e9
pushq $0x8
popq %rbp
leaq 0xc7168(%rip), %rsi     # 0x111858
movq %rbx, %rdi
movq %rbp, %rdx
xorl %eax, %eax
callq 0xde896
movq %rsp, %r12
xorl %r13d, %r13d
movq %rbp, %r15
leaq 0x40(%rbp), %rax
movq 0x18(%rbx), %rcx
cmpq 0x18(%rcx), %rax
jg 0x4a906
testq %r13, %r13
je 0x4a728
movq %rbx, %rdi
movq %r13, %rsi
callq 0x4ab6d
movq %rbx, %rdi
pushq $0x40
popq %rsi
callq 0xdecaf
movq %rax, %r13
movq %r15, (%rax)
movq %rbx, %rdi
leaq 0xc(%rsp), %rsi
callq 0xdebae
movq (%r13), %rdx
movq %rbx, %rdi
leaq 0xc7136(%rip), %rsi     # 0x11188a
xorl %eax, %eax
callq 0xde896
movq %rbx, %rdi
pushq $0x1
popq %rsi
callq 0xdeba7
movq %rbx, %rdi
callq 0xdfd1a
movq %rax, 0x38(%r13)
movq (%r13), %rax
addq $0x4, %rax
movq %rax, (%rsp)
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xe40bf
movq %rax, 0x8(%r13)
movq %rbx, %rdi
leaq 0xc7104(%rip), %rsi     # 0x11189c
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movq 0x8(%r13), %rax
cmpq $0x40, %rax
jl 0x4a7ba
addq (%r13), %rax
movq 0x18(%rbx), %rdi
cmpq 0x18(%rdi), %rax
jle 0x4a7c4
andq $0x0, 0x8(%r13)
jmp 0x4a8e1
movq 0x28(%r13), %rcx
testq %rcx, %rcx
jne 0x4a7e0
movq %rbx, %rdi
callq 0xeac74
movq %rax, %rcx
movq %rax, 0x28(%r13)
movq 0x18(%rbx), %rdi
movq (%rsp), %rsi
movzbl (%r14), %r9d
orl $0x300, %r9d            # imm = 0x300
pushq $0x20
popq %rdx
pushq $0x1
popq %rbp
movl %ebp, %r8d
callq 0xe4c9a
movq 0x28(%r13), %rdi
callq 0xeb877
movq %rbx, %rdi
leaq 0xd0524(%rip), %rsi     # 0x11ad34
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
addq $0x20, (%rsp)
movq %rbx, %rdi
callq 0xeac74
movq %rax, 0x30(%r13)
movq 0x18(%rbx), %rdi
movq (%rsp), %rsi
movl (%r14), %r9d
pushq $0xc
popq %rdx
movq %rax, %rcx
movl %ebp, %r8d
callq 0xe4c9a
movq 0x30(%r13), %rdi
callq 0xeb877
movq %rbx, %rdi
leaq 0xe4207(%rip), %rsi     # 0x12ea5e
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
cmpb $0x0, 0xd0(%rbx)
je 0x4a889
movq 0x30(%r13), %rdi
callq 0xeaf8a
testl %eax, %eax
je 0x4a889
movq 0x30(%r13), %rdx
movq 0x38(%r13), %rsi
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0xdfd55
addq $0xc, (%rsp)
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xe40bf
movq %rax, 0x18(%r13)
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xe40bf
movq %rax, 0x20(%r13)
movq 0x18(%r13), %rsi
movq %rbx, %rdi
movq %rax, %rdx
callq 0xdf038
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x4aa40
movq (%rsp), %rax
movq %rax, 0x10(%r13)
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x4aa66
xorl %edi, %edi
callq 0xe7084
movl 0xc(%rsp), %esi
movq %rbx, %rdi
callq 0xdebb7
movq 0x8(%r13), %rax
leaq (%rax,%r15), %rbp
cmpq $0x3f, %rax
jg 0x4a703
movq %r15, 0x10(%rsp)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x4ab6d
testq %r14, %r14
je 0x4a93e
cmpb $0x0, 0x6(%r14)
je 0x4a933
leaq 0xc6f45(%rip), %rsi     # 0x11186e
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
movq %rbx, %rdi
movq %r14, %rsi
callq 0xded32
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq