// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/portfolio.c
// Function: do_process_frame
static int do_process_frame(deark *c, lctx *d, i64 pos1, i64 *bytes_consumed)
{
	i64 pos;
	u8 frame_type;
	i64 frame_payload_size;
	int retval = 1;

	*bytes_consumed = 0;
	pos = pos1;

	de_dbg(c, "frame at %d", (int)pos1);
	de_dbg_indent(c, 1);

	// 8-byte frame header
	frame_type = de_getbyte(pos);
	de_dbg(c, "type: %d", (int)frame_type);

	frame_payload_size = de_getu16le(pos+1);
	de_dbg(c, "reported payload size: %d", (int)frame_payload_size);

	*bytes_consumed += 8;
	pos += 8;
	if(pos + frame_payload_size > c->infile->len) {
		de_err(c, "Frame goes beyond end of file");
		retval = 0;
		goto done;
	}

	switch(frame_type) {
	case 0x00: // PGC
		do_pgc_in_pgx(c, d, pos, frame_payload_size);
		*bytes_consumed += frame_payload_size;
		break;

	case 0x01:
		de_warn(c, "PGT frames (text screen dumps) are not supported");

		// The spec contradicts itself about how to figure out the frame
		// payload size of PGT frames. First it says the size field is not
		// used. The it says it *is* used, and is expected to always be 320.
		// In the only example file I have, it is 317, though the actual size
		// of the frame in that file is 320.
		*bytes_consumed += 320;

		break;

	case 0xfe: // APPS
		*bytes_consumed += frame_payload_size;
		break;

	case 0xff: // EOF
		retval = 0;
		break;

	default:
		de_err(c, "Unknown frame type (%d)", (int)frame_type);
		retval = 0;
		break;
	}

done:
	de_dbg_indent(c, -1);
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movl $0x1, -0x3c(%rbp)
movq -0x20(%rbp), %rax
movq $0x0, (%rax)
movq -0x18(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl %eax, %edx
leaq 0xb3ef5(%rip), %rsi     # 0x1c3858
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rsi
callq 0x165060
movb %al, -0x29(%rbp)
movq -0x8(%rbp), %rdi
movzbl -0x29(%rbp), %edx
leaq 0xb0623(%rip), %rsi     # 0x1bffbe
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rsi
addq $0x1, %rsi
callq 0x165280
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movl %eax, %edx
leaq 0xb3e98(%rip), %rsi     # 0x1c3864
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rax
movq (%rax), %rcx
addq $0x8, %rcx
movq %rcx, (%rax)
movq -0x28(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
addq -0x38(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jle 0x10fa21
movq -0x8(%rbp), %rdi
leaq 0xb3e70(%rip), %rsi     # 0x1c387e
movb $0x0, %al
callq 0x15bfb0
movl $0x0, -0x3c(%rbp)
jmp 0x10fad2
movzbl -0x29(%rbp), %eax
movl %eax, -0x40(%rbp)
testl %eax, %eax
je 0x10fa50
jmp 0x10fa2e
movl -0x40(%rbp), %eax
subl $0x1, %eax
je 0x10fa75
jmp 0x10fa38
movl -0x40(%rbp), %eax
subl $0xfe, %eax
je 0x10fa9a
jmp 0x10fa44
movl -0x40(%rbp), %eax
subl $0xff, %eax
je 0x10faaa
jmp 0x10fab3
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x38(%rbp), %rcx
callq 0x10faf0
movq -0x38(%rbp), %rcx
movq -0x20(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
jmp 0x10fad0
movq -0x8(%rbp), %rdi
leaq 0xb3e1c(%rip), %rsi     # 0x1c389c
movb $0x0, %al
callq 0x15bc10
movq -0x20(%rbp), %rax
movq (%rax), %rcx
addq $0x140, %rcx            # imm = 0x140
movq %rcx, (%rax)
jmp 0x10fad0
movq -0x38(%rbp), %rcx
movq -0x20(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
jmp 0x10fad0
movl $0x0, -0x3c(%rbp)
jmp 0x10fad0
movq -0x8(%rbp), %rdi
movzbl -0x29(%rbp), %edx
leaq 0xb3e0b(%rip), %rsi     # 0x1c38cd
movb $0x0, %al
callq 0x15bfb0
movl $0x0, -0x3c(%rbp)
jmp 0x10fad2
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movl -0x3c(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)