// File: /workspace/github_c_makefile_star2_O1/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 0xe969b
movq %rax, %r14
movq %rbx, %rdi
callq 0x4cfca
movb %al, 0x4(%r14)
cmpb $0x2, %al
je 0x4cc8f
movzbl %al, %eax
cmpl $0x3, %eax
jne 0x4ccc7
leaq 0xd1b8b(%rip), %rsi     # 0x11e818
jmp 0x4cc96
leaq 0xd1b7a(%rip), %rsi     # 0x11e810
movq %rbx, %rdi
callq 0xeb6d6
movb $0x1, 0x5(%r14)
movq %rbx, %rdi
xorl %esi, %esi
movl $0xe, %edx
callq 0xeb7d1
movl %eax, (%r14)
movq 0x18(%rbx), %rdi
movl $0x7, %esi
callq 0xef0d5
testb %al, %al
je 0x4cd11
movb $0x1, 0x6(%r14)
xorl %r12d, %r12d
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4d1b3
testq %r14, %r14
je 0x4cd02
cmpb $0x0, 0x6(%r14)
je 0x4ccf7
leaq 0xd1b49(%rip), %rsi     # 0x11e836
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe9752
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x8, %r15d
cmpb $0x2, 0x4(%r14)
jne 0x4cd55
movq 0x18(%rbx), %rsi
leaq 0x8(%r14), %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 0xf32f2
addq $0x20, %rsp
movl $0x548, %r15d           # imm = 0x548
leaq 0xd1ac4(%rip), %rsi     # 0x11e820
xorl %r12d, %r12d
movq %rbx, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0xe9226
leaq 0x40(%r15), %rax
movq 0x18(%rbx), %rcx
cmpq 0x18(%rcx), %rax
jg 0x4cccf
leaq 0x8(%rsp), %r13
leaq 0xd1ada(%rip), %rbp     # 0x11e864
xorl %r12d, %r12d
testq %r12, %r12
je 0x4cd9d
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4d1b3
movl $0x40, %esi
movq %rbx, %rdi
callq 0xe969b
movq %rax, %r12
movq %r15, (%rax)
movq %rbx, %rdi
leaq 0x14(%rsp), %rsi
callq 0xe958c
movq (%r12), %rdx
movq %rbx, %rdi
leaq 0xd1a87(%rip), %rsi     # 0x11e852
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq %rbx, %rdi
callq 0xea9a9
movq %rax, 0x38(%r12)
movq (%r12), %rax
addq $0x4, %rax
movq %rax, 0x8(%rsp)
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xef3a0
movq %rax, 0x8(%r12)
movq %rbx, %rdi
movq %rbp, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq 0x8(%r12), %rax
cmpq $0x40, %rax
jl 0x4ce33
addq (%r12), %rax
movq 0x18(%rbx), %rcx
cmpq 0x18(%rcx), %rax
jle 0x4ce41
movq $0x0, 0x8(%r12)
jmp 0x4cf76
cmpq $0x0, 0x28(%r12)
jne 0x4ce56
movq %rbx, %rdi
callq 0xf6870
movq %rax, 0x28(%r12)
movq 0x18(%rbx), %rdi
movq 0x8(%rsp), %rsi
movq 0x28(%r12), %rcx
movzbl (%r14), %r9d
orl $0x300, %r9d            # imm = 0x300
movl $0x20, %edx
movl $0x1, %r8d
callq 0xf01b0
movq 0x28(%r12), %rdi
callq 0xf7557
movq %rbx, %rdi
leaq 0xdaeb1(%rip), %rsi     # 0x127d44
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
addq $0x20, 0x8(%rsp)
movq %rbx, %rdi
callq 0xf6870
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 0xf01b0
movq 0x30(%r12), %rdi
callq 0xf7557
movq %rbx, %rdi
leaq 0xeeaf1(%rip), %rsi     # 0x13b9d4
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
cmpb $0x0, 0xd0(%rbx)
je 0x4cf18
movq 0x30(%r12), %rdi
callq 0xf6c00
testl %eax, %eax
je 0x4cf18
movq 0x30(%r12), %rdx
movq 0x38(%r12), %rsi
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0xea9e4
addq $0xc, 0x8(%rsp)
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xef3a0
movq %rax, 0x18(%r12)
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xef3a0
movq %rax, 0x20(%r12)
movq 0x18(%r12), %rsi
movq %rbx, %rdi
movq %rax, %rdx
callq 0xe9b05
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x4d072
movq 0x8(%rsp), %rax
movq %rax, 0x10(%r12)
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x4d098
xorl %edi, %edi
callq 0xf28d4
movl 0x14(%rsp), %esi
movq %rbx, %rdi
callq 0xe9595
movq 0x8(%r12), %rax
cmpq $0x40, %rax
jl 0x4cccf
leaq (%rax,%r15), %rcx
addq $0x40, %rcx
addq %rax, %r15
movq 0x18(%rbx), %rax
cmpq 0x18(%rax), %rcx
jle 0x4cd8d
jmp 0x4cccf