// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/emf.c
// Function: do_one_emfplus_record
static void do_one_emfplus_record(deark *c, lctx *d, i64 pos, i64 len,
	i64 *bytes_consumed, int *continuation_flag)
{
	u32 rectype;
	u32 flags;
	i64 size, datasize;
	i64 payload_pos;
	const struct emfplus_rec_info *epinfo = NULL;
	size_t k;
	int is_continued = 0;

	if(len<12) {
		*bytes_consumed = len;
		*continuation_flag = 0;
		return;
	}

	rectype = (u32)de_getu16le(pos);
	flags = (u32)de_getu16le(pos+2);
	size = de_getu32le(pos+4);

	is_continued = (rectype==0x4008) && (flags&0x8000);

	// The documentation suggests that the datasize field is in a different
	// place if the continuation flag is set. It also suggests the opposite
	// (or maybe just that it's safe to behave as if it were in the same place).
	// It doesn't really matter, since we don't support 'continued' records.
	//
	// I don't know why the datasize field is padded to the next multiple of 4.
	// It seems clearly unnecessary, and counterproductive.
	// There is already the 'size' field above, which is padded, so the
	// only reason for the datasize field to exist at all would be if it
	// told us the *non-padded* size. Yet it doesn't. So now I have to write
	// code to try to detect where an embedded PNG or whatever file ends.
	// (The existence of 'continued' records makes this issue more complicated,
	// but they are already a special case, so that's no excuse.)
	datasize = de_getu32le(pos+8);
	payload_pos = pos+12;

	// Find the name, etc. of this record type
	for(k=0; k<DE_ARRAYCOUNT(emfplus_rec_info_arr); k++) {
		if(emfplus_rec_info_arr[k].rectype == rectype) {
			epinfo = &emfplus_rec_info_arr[k];
			break;
		}
	}

	de_dbg(c, "rectype 0x%04x (%s) at %"I64_FMT", flags=0x%04x, dpos=%"I64_FMT", dlen=%"I64_FMT,
		(UI)rectype, epinfo ? epinfo->name : "?",
		pos,
		(UI)flags,
		payload_pos, datasize);

	// If this record or the previous record had the continuation flag set,
	// give up.
	if(is_continued || *continuation_flag) {
		goto done;
	}

	de_dbg_indent(c, 1);
	if(rectype==0x4003) {
		emfplus_handler_4003(c, d, rectype, payload_pos, datasize);
	}
	else if(rectype==0x4008) {
		do_emfplus_object(c, d, payload_pos, datasize, flags);
	}
	else if(rectype==0x401c) {
		emfplus_handler_401c(c, d, rectype, payload_pos, datasize);
	}
	de_dbg_indent(c, -1);

done:
	if(size<12) size=12;
	*bytes_consumed = size;
	*continuation_flag = is_continued;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq $0x0, -0x58(%rbp)
movl $0x0, -0x64(%rbp)
cmpq $0xc, -0x20(%rbp)
jge 0x5b9d3
movq -0x20(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movq -0x30(%rbp), %rax
movl $0x0, (%rax)
jmp 0x5bbdc
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
callq 0x165280
movl %eax, -0x34(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x2, %rsi
callq 0x165280
movl %eax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x4, %rsi
callq 0x165540
movq %rax, -0x40(%rbp)
xorl %eax, %eax
cmpl $0x4008, -0x34(%rbp)    # imm = 0x4008
movb %al, -0x65(%rbp)
jne 0x5ba37
movl -0x38(%rbp), %eax
andl $0x8000, %eax           # imm = 0x8000
cmpl $0x0, %eax
setne %al
movb %al, -0x65(%rbp)
movb -0x65(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x64(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x8, %rsi
callq 0x165540
movq %rax, -0x48(%rbp)
movq -0x18(%rbp), %rax
addq $0xc, %rax
movq %rax, -0x50(%rbp)
movq $0x0, -0x60(%rbp)
cmpq $0x2f, -0x60(%rbp)
jae 0x5bab7
movq -0x60(%rbp), %rcx
leaq 0x1bba6f(%rip), %rax    # 0x2174f0
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
cmpl -0x34(%rbp), %eax
jne 0x5baa7
movq -0x60(%rbp), %rcx
leaq 0x1bba56(%rip), %rax    # 0x2174f0
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
jmp 0x5bab7
jmp 0x5baa9
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x5ba6f
movq -0x8(%rbp), %rax
movq %rax, -0x78(%rbp)
movl -0x34(%rbp), %eax
movl %eax, -0x6c(%rbp)
cmpq $0x0, -0x58(%rbp)
je 0x5bada
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x80(%rbp)
jmp 0x5bae7
leaq 0x15c14c(%rip), %rax    # 0x1b7c2d
movq %rax, -0x80(%rbp)
jmp 0x5bae7
movl -0x6c(%rbp), %edx
movq -0x78(%rbp), %rdi
movq -0x80(%rbp), %rcx
movq -0x18(%rbp), %r8
movl -0x38(%rbp), %r9d
movq -0x50(%rbp), %r10
movq -0x48(%rbp), %rax
leaq 0x14bb78(%rip), %rsi    # 0x1a7681
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
movb $0x0, %al
callq 0x15b0d0
cmpl $0x0, -0x64(%rbp)
jne 0x5bb28
movq -0x30(%rbp), %rax
cmpl $0x0, (%rax)
je 0x5bb2d
jmp 0x5bbb9
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
cmpl $0x4003, -0x34(%rbp)    # imm = 0x4003
jne 0x5bb60
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x34(%rbp), %eax
movl %eax, %edx
movq -0x50(%rbp), %rcx
movq -0x48(%rbp), %r8
callq 0x5bbf0
jmp 0x5bbab
cmpl $0x4008, -0x34(%rbp)    # imm = 0x4008
jne 0x5bb84
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x50(%rbp), %rdx
movq -0x48(%rbp), %rcx
movl -0x38(%rbp), %r8d
callq 0x5bc70
jmp 0x5bba9
cmpl $0x401c, -0x34(%rbp)    # imm = 0x401C
jne 0x5bba7
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x34(%rbp), %eax
movl %eax, %edx
movq -0x50(%rbp), %rcx
movq -0x48(%rbp), %r8
callq 0x5bd30
jmp 0x5bba9
jmp 0x5bbab
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
cmpq $0xc, -0x40(%rbp)
jge 0x5bbc8
movq $0xc, -0x40(%rbp)
movq -0x40(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movl -0x64(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
addq $0x90, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)