// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/portfolio.c
// Function: de_run_pgx
static void de_run_pgx(deark *c, de_module_params *mparams)
{
	lctx *d = NULL;
	i64 pos;
	i64 frame_size;
	int ret;
	int executable = 0;

	if(dbuf_memcmp(c->infile, 0, "PGX", 3)) {
		// Some "PGX" files are actually .COM files with an embedded PGX file.
		// The ones I've seen always have the PGX file at offset 1248, so look
		// for it there.
		if(dbuf_memcmp(c->infile, 0, "PGX", 1248)) {
			executable = 1;
		}
	}

	if(executable) {
		de_declare_fmt(c, "PGX (Portfolio Animation, executable)");
		pos = 1248;
	}
	else{
		de_declare_fmt(c, "PGX (Portfolio Animation)");
		pos = 0;
	}

	d = de_malloc(c, sizeof(lctx));

	d->version = de_getbyte(pos+3);
	de_dbg(c, "Version: %d", (int)d->version);

	pos += 8;
	while(1) {
		if(pos >= c->infile->len) break;
		ret = do_process_frame(c, d, pos, &frame_size);
		if(!ret || !frame_size) break;
		pos += frame_size;
	}

	de_free(c, d);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movq 0x18(%rdi), %rdi
leaq 0x803f0(%rip), %rdx     # 0x137f54
movl $0x3, %ecx
xorl %esi, %esi
callq 0xefc85
testl %eax, %eax
je 0xb7b9e
movq 0x18(%rbx), %rdi
leaq 0x803d5(%rip), %rdx     # 0x137f54
movl $0x4e0, %r14d           # imm = 0x4E0
movl $0x4e0, %ecx            # imm = 0x4E0
xorl %esi, %esi
callq 0xefc85
testl %eax, %eax
je 0xb7b9e
leaq 0x803bc(%rip), %rsi     # 0x137f58
jmp 0xb7ba8
leaq 0x803d9(%rip), %rsi     # 0x137f7e
xorl %r14d, %r14d
movq %rbx, %rdi
callq 0xeb125
movl $0x1, %esi
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, %r15
movq 0x18(%rbx), %rdi
movq %r14, %rsi
orq $0x3, %rsi
callq 0xeead3
movq %r15, 0x8(%rsp)
movb %al, (%r15)
movzbl %al, %edx
leaq 0x803b6(%rip), %rsi     # 0x137f98
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
orq $0x8, %r14
movq 0x18(%rbx), %rax
cmpq 0x18(%rax), %r14
jge 0xb7d83
leaq 0x8039f(%rip), %r13     # 0x137fa4
leaq 0x7c96b(%rip), %rbp     # 0x134577
movq %rbx, %rdi
movq %r13, %rsi
movl %r14d, %edx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xeead3
movzbl %al, %r12d
movq %rbx, %rdi
movq %rbp, %rsi
movl %r12d, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
leaq 0x1(%r14), %rsi
callq 0xeebc8
movq %rax, %r15
movq %rbx, %rdi
leaq 0x8034d(%rip), %rsi     # 0x137fb0
movl %r15d, %edx
xorl %eax, %eax
callq 0xe8617
leaq (%r15,%r14), %rax
addq $0x8, %rax
movq 0x18(%rbx), %rcx
cmpq 0x18(%rcx), %rax
jg 0xb7d46
cmpl $0xfd, %r12d
jg 0xb7cb4
testl %r12d, %r12d
je 0xb7cc2
cmpl $0x1, %r12d
jne 0xb7d62
movq %rbx, %rdi
leaq 0x80343(%rip), %rsi     # 0x137fe8
xorl %eax, %eax
callq 0xe8faa
movl $0x148, %r15d           # imm = 0x148
jmp 0xb7d21
cmpl $0xfe, %r12d
je 0xb7d1d
jmp 0xb7d59
leaq 0x8(%r14), %rax
movq %rax, 0x10(%rsp)
movq %rbx, %rdi
leaq 0x80255(%rip), %rsi     # 0x137f2a
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xefda1
movq %rbp, %r12
movq %r13, %rbp
movq %rax, %r13
movl $0x3, %edx
movq %rax, %rdi
leaq 0x8033b(%rip), %rsi     # 0x138031
callq 0xef547
movq 0x18(%rbx), %rdi
movq 0x10(%rsp), %rsi
movq %r15, %rdx
movq %r13, %rcx
callq 0xef481
movq %r13, %rdi
movq %rbp, %r13
movq %r12, %rbp
callq 0xf052b
addq $0x8, %r15
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
testq %r15, %r15
je 0xb7d83
addq %r15, %r14
movq 0x18(%rbx), %rax
cmpq 0x18(%rax), %r14
jl 0xb7c0c
jmp 0xb7d83
leaq 0x8027d(%rip), %rsi     # 0x137fca
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
jmp 0xb7d76
cmpl $0xff, %r12d
je 0xb7d76
leaq 0x802b0(%rip), %rsi     # 0x138019
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xe9295
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe8be2