// File: /workspace/github_c_makefile_star2_O1/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
pushq %rax
movq %rdi, %rbx
movq 0x18(%rdi), %rdi
leaq 0x7f463(%rip), %rdx     # 0x137bc8
movl $0x3, %ecx
xorl %esi, %esi
callq 0xf025e
testl %eax, %eax
je 0xb879f
movq 0x18(%rbx), %rdi
leaq 0x7f448(%rip), %rdx     # 0x137bc8
movl $0x4e0, %r14d           # imm = 0x4E0
movl $0x4e0, %ecx            # imm = 0x4E0
xorl %esi, %esi
callq 0xf025e
testl %eax, %eax
je 0xb879f
leaq 0x7f42f(%rip), %rsi     # 0x137bcc
jmp 0xb87a9
leaq 0x7f44c(%rip), %rsi     # 0x137bf2
xorl %r14d, %r14d
movq %rbx, %rdi
callq 0xeb6d6
movl $0x1, %esi
movq %rbx, %rdi
callq 0xe969b
movq %rax, %r15
movq 0x18(%rbx), %rdi
movq %r14, %rsi
orq $0x3, %rsi
callq 0xef0d5
movq %r15, (%rsp)
movb %al, (%r15)
movzbl %al, %edx
leaq 0x7f42a(%rip), %rsi     # 0x137c0c
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
orq $0x8, %r14
movq 0x18(%rbx), %rax
cmpq 0x18(%rax), %r14
jge 0xb896f
movq %rbx, %rdi
leaq 0x7f410(%rip), %rsi     # 0x137c18
movl %r14d, %edx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xef0d5
movzbl %al, %r12d
movq %rbx, %rdi
leaq 0x7b946(%rip), %rsi     # 0x13417f
movl %r12d, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
leaq 0x1(%r14), %rsi
callq 0xef1ca
movq %rax, %r15
movq %rbx, %rdi
leaq 0x7f3c7(%rip), %rsi     # 0x137c24
movl %r15d, %edx
xorl %eax, %eax
callq 0xe9226
leaq (%r15,%r14), %rax
addq $0x8, %rax
movq 0x18(%rbx), %rcx
cmpq 0x18(%rcx), %rax
jle 0xb8897
xorl %ebp, %ebp
movq %rbx, %rdi
leaq 0x7f3b9(%rip), %rsi     # 0x137c3e
xorl %eax, %eax
callq 0xe9df7
movl $0x8, %r13d
jmp 0xb8950
movl $0x8, %r13d
cmpl $0xfd, %r12d
jg 0xb88d0
testl %r12d, %r12d
je 0xb88fe
cmpl $0x1, %r12d
jne 0xb88e6
movq %rbx, %rdi
leaq 0x7f3a1(%rip), %rsi     # 0x137c5c
xorl %eax, %eax
callq 0xe9b27
movb $0x1, %bpl
movl $0x148, %r13d           # imm = 0x148
jmp 0xb8950
cmpl $0xfe, %r12d
je 0xb8946
cmpl $0xff, %r12d
jne 0xb88e6
xorl %ebp, %ebp
jmp 0xb8950
xorl %ebp, %ebp
movq %rbx, %rdi
leaq 0x7f39b(%rip), %rsi     # 0x137c8d
movl %r12d, %edx
xorl %eax, %eax
callq 0xe9df7
jmp 0xb8950
leaq 0x8(%r14), %r12
movq %rbx, %rdi
leaq 0x7f292(%rip), %rsi     # 0x137b9e
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xf037a
movq %rax, %r13
movl $0x3, %edx
movq %rax, %rdi
leaq 0x7f37e(%rip), %rsi     # 0x137ca5
callq 0xefb82
movq 0x18(%rbx), %rdi
movq %r12, %rsi
movq %r15, %rdx
movq %r13, %rcx
callq 0xefabc
movq %r13, %rdi
callq 0xf0b13
addq $0x8, %r15
movb $0x1, %bpl
movq %r15, %r13
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
testb %bpl, %bpl
je 0xb896f
testq %r13, %r13
je 0xb896f
addq %r13, %r14
jmp 0xb87f0
movq %rbx, %rdi
movq (%rsp), %rsi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe9752