// File: /workspace/github_c_makefile_star2_O3/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 0xe8b2b
movq %rax, %r14
movq %rbx, %rdi
callq 0x4ce6c
movb %al, 0x4(%r14)
cmpb $0x2, %al
je 0x4cb33
movzbl %al, %eax
cmpl $0x3, %eax
jne 0x4cb6b
leaq 0xd1def(%rip), %rsi     # 0x11e920
jmp 0x4cb3a
leaq 0xd1dde(%rip), %rsi     # 0x11e918
movq %rbx, %rdi
callq 0xeb125
movb $0x1, 0x5(%r14)
movq %rbx, %rdi
xorl %esi, %esi
movl $0xe, %edx
callq 0xeb220
movl %eax, (%r14)
movq 0x18(%rbx), %rdi
movl $0x7, %esi
callq 0xeead3
testb %al, %al
je 0x4cb7a
movb $0x1, 0x6(%r14)
movq %rbx, %rdi
xorl %esi, %esi
jmp 0x4ce21
movl $0x8, %r15d
cmpb $0x2, 0x4(%r14)
jne 0x4cbc1
movq 0x18(%rbx), %rsi
movq %r14, %r9
addq $0x8, %r9
subq $0x8, %rsp
movl $0x8, %edx
movl $0x100, %ecx            # imm = 0x100
movl $0x3, %r8d
movq %rbx, %rdi
pushq $0x0
pushq $0x2
pushq $0x100                  # imm = 0x100
callq 0xf2cb1
addq $0x20, %rsp
movl $0x548, %r15d           # imm = 0x548
leaq 0xd1d60(%rip), %rsi     # 0x11e928
xorl %r12d, %r12d
movq %rbx, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0xe8617
leaq 0x40(%r15), %rax
movq 0x18(%rbx), %rcx
cmpq 0x18(%rcx), %rax
jg 0x4ce1b
leaq 0x8(%rsp), %r13
leaq 0xd1d76(%rip), %rbp     # 0x11e96c
xorl %r12d, %r12d
testq %r12, %r12
je 0x4cc09
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4d05f
movl $0x40, %esi
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, %r12
movq %r15, (%rax)
movq %rbx, %rdi
leaq 0x14(%rsp), %rsi
callq 0xe8a1c
movq (%r12), %rdx
movq %rbx, %rdi
leaq 0xd1d23(%rip), %rsi     # 0x11e95a
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq %rbx, %rdi
callq 0xea504
movq %rax, 0x38(%r12)
movq (%r12), %rax
addq $0x4, %rax
movq %rax, 0x8(%rsp)
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xeed83
movq %rax, 0x8(%r12)
movq %rbx, %rdi
movq %rbp, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x8(%r12), %rax
cmpq $0x40, %rax
jl 0x4cc9f
addq (%r12), %rax
movq 0x18(%rbx), %rdi
cmpq 0x18(%rdi), %rax
jle 0x4ccad
movq $0x0, 0x8(%r12)
jmp 0x4cde4
movq 0x28(%r12), %rcx
testq %rcx, %rcx
jne 0x4cccb
movq %rbx, %rdi
callq 0xf6410
movq %rax, %rcx
movq %rax, 0x28(%r12)
movq 0x18(%rbx), %rdi
movq 0x8(%rsp), %rsi
movzbl (%r14), %r9d
orl $0x300, %r9d            # imm = 0x300
movl $0x20, %edx
movl $0x1, %r8d
callq 0xefbd7
movq 0x28(%r12), %rdi
callq 0xf70f6
movq %rbx, %rdi
leaq 0xdb275(%rip), %rsi     # 0x127f74
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
addq $0x20, 0x8(%rsp)
movq %rbx, %rdi
callq 0xf6410
movq %rax, 0x30(%r12)
movq 0x18(%rbx), %rdi
movq 0x8(%rsp), %rsi
movl (%r14), %r9d
movl $0xc, %edx
movq %rax, %rcx
movl $0x1, %r8d
callq 0xefbd7
movq 0x30(%r12), %rdi
callq 0xf70f6
movq %rbx, %rdi
leaq 0xeefff(%rip), %rsi     # 0x13bd4e
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
cmpb $0x0, 0xd0(%rbx)
je 0x4cd84
movq 0x30(%r12), %rdi
callq 0xf67a3
testl %eax, %eax
je 0x4cd84
movq 0x30(%r12), %rdx
movq 0x38(%r12), %rsi
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0xea53f
addq $0xc, 0x8(%rsp)
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xeed83
movq %rax, 0x18(%r12)
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xeed83
movq %rax, 0x20(%r12)
movq 0x18(%r12), %rsi
movq %rbx, %rdi
movq %rax, %rdx
callq 0xe8f88
movzbl 0x5(%r14), %esi
movq %rbx, %rdi
movq %r13, %rdx
callq 0x4cf14
movq 0x8(%rsp), %rax
movq %rax, 0x10(%r12)
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x4cf3c
xorl %edi, %edi
callq 0xf228d
movl 0x14(%rsp), %esi
movq %rbx, %rdi
callq 0xe8a25
movq 0x8(%r12), %rax
cmpq $0x40, %rax
jl 0x4ce1b
leaq (%rax,%r15), %rcx
addq $0x40, %rcx
addq %rax, %r15
movq 0x18(%rbx), %rax
cmpq 0x18(%rax), %rcx
jle 0x4cbf9
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4d05f
cmpb $0x0, 0x6(%r14)
je 0x4ce3e
leaq 0xd1b0a(%rip), %rsi     # 0x11e93e
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe8be2
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq