// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/emf.c
// Function: emf_handler_46
static int emf_handler_46(deark *c, lctx *d, struct decoder_params *dp)
{
	const char *name;
	i64 datasize;
	int handled = 0;
	enum cmtid_enum { CMTID_UNK, CMTID_EMFSPOOL, CMTID_EMFPLUS, CMTID_PUBLIC,
		CMTID_INKSCAPESCREEN, CMTID_INKSCAPEDRAWING };
	enum cmtid_enum cmtid;

	if(dp->recsize_bytes<16) goto done;

	// Datasize is measured from the beginning of the next field (CommentIdentifier).
	datasize = de_getu32le(dp->recpos+8);
	de_dbg(c, "datasize: %"I64_FMT, datasize);
	if(12+datasize > dp->recsize_bytes) goto done;

	cmtid = CMTID_UNK;
	name="?";

	if(datasize>=4) {
		struct de_fourcc id4cc;

		// The first 4 bytes of comment data might or might not be a signature.
		// The spec expects these bytes to be read as a little-endian int, which is
		// then interpreted as a FOURCC, most-significant byte first.
		// The standard FOURCC codes are designed backwards, so that in the
		// file they appear forward. E.g. the spec says a code is "+FME", but in
		// the file the bytes "EMF+" appear in that order. Our messages respect the
		// spec, though it looks strange.
		dbuf_read_fourcc(c->infile, dp->recpos+12, &id4cc, 4, 0x1);

		if(id4cc.id==0x00000000) {
			cmtid = CMTID_EMFSPOOL;
			name = "EMR_COMMENT_EMFSPOOL";
		}
		else if(id4cc.id==CODE_EMFPLUS) {
			cmtid = CMTID_EMFPLUS;
			name = "EMR_COMMENT_EMFPLUS";
		}
		else if(id4cc.id==CODE_GDIC) {
			cmtid = CMTID_PUBLIC;
			name = "EMR_COMMENT_PUBLIC";
		}

		de_dbg(c, "type: 0x%08x '%s' (%s)", (UI)id4cc.id,
			id4cc.id_dbgstr, name);
	}

	if(cmtid==CMTID_UNK) {
		u8 buf[16];

		// FOURCC not recognized; try other methods
		de_read(buf, dp->recpos+12, 16);
		if(datasize>=7 && !de_memcmp(buf, "Screen=", 7)) {
			cmtid = CMTID_INKSCAPESCREEN;
			name = "Inkscape canvas size";
		}
		else if(datasize>=8 && !de_memcmp(buf, "Drawing=", 8)) {
			cmtid = CMTID_INKSCAPEDRAWING;
			name = "Inkscape image size";
		}

		de_dbg(c, "identified as: %s", name);
	}

	if(cmtid==CMTID_EMFPLUS) {
		do_comment_emfplus(c, d, dp->recpos+16, datasize-4);
		handled = 1;
	}
	else if(cmtid==CMTID_PUBLIC) {
		do_comment_public(c, d, dp->recpos+16, datasize-4);
		handled = 1;
	}

	if(!handled) {
		de_dbg_hexdump(c, c->infile, dp->recpos+12, datasize, 256, NULL, 0x1);
	}

done:
	return 1;
}

// Assembly:
cmpq $0x10, 0x10(%rdx)
jl 0x42ac5
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rdx, %r14
movq %rdi, %rbp
movq 0x18(%rdi), %rdi
movq 0x8(%rdx), %rsi
addq $0x8, %rsi
callq 0xef37e
movq %rax, %r15
leaq 0xd9565(%rip), %rsi     # 0x11b858
xorl %ebx, %ebx
movq %rbp, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
leaq 0xc(%r15), %rax
cmpq 0x10(%r14), %rax
jg 0x42ab7
cmpq $0x4, %r15
jl 0x42366
movq 0x18(%rbp), %rdi
movq 0x8(%r14), %rsi
addq $0xc, %rsi
leaq 0x40(%rsp), %r12
movl $0x1, %ebx
movq %r12, %rdx
movl $0x4, %ecx
movl $0x1, %r8d
callq 0xf1d74
movl 0x4(%r12), %edx
testl %edx, %edx
je 0x4237d
cmpl $0x43494447, %edx       # imm = 0x43494447
je 0x4236f
cmpl $0x2b464d45, %edx       # imm = 0x2B464D45
jne 0x42386
movl $0x2, %ebx
leaq 0xd9517(%rip), %r12     # 0x11b87b
jmp 0x4238f
leaq 0xe9afc(%rip), %r12     # 0x12be69
jmp 0x423a8
movl $0x3, %ebx
leaq 0xd9514(%rip), %r12     # 0x11b88f
jmp 0x4238f
leaq 0xd94e2(%rip), %r12     # 0x11b866
jmp 0x4238f
xorl %ebx, %ebx
leaq 0xe9ada(%rip), %r12     # 0x12be69
leaq 0x50(%rsp), %rcx
leaq 0xd9507(%rip), %rsi     # 0x11b8a2
movq %rbp, %rdi
movq %r12, %r8
xorl %eax, %eax
callq 0xe9226
testl %ebx, %ebx
jne 0x42427
movq 0x18(%rbp), %rdi
movq 0x8(%r14), %rdx
addq $0xc, %rdx
leaq 0x40(%rsp), %rsi
movl $0x10, %ecx
callq 0xeeee8
cmpq $0x7, %r15
jl 0x423e7
movl $0x65726353, %eax       # imm = 0x65726353
xorl 0x40(%rsp), %eax
movl $0x3d6e6565, %ecx       # imm = 0x3D6E6565
xorl 0x43(%rsp), %ecx
orl %eax, %ecx
je 0x42a68
cmpq $0x8, %r15
setge %al
movabsq $0x3d676e6977617244, %rcx # imm = 0x3D676E6977617244
cmpq %rcx, 0x40(%rsp)
sete %cl
andb %al, %cl
leaq 0xd94cd(%rip), %rax     # 0x11b8d6
cmovneq %rax, %r12
movzbl %cl, %eax
leal (%rax,%rax,4), %ebx
leaq 0xe8770(%rip), %rsi     # 0x12ab8a
movq %rbp, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe9226
cmpl $0x3, %ebx
je 0x429ec
cmpl $0x2, %ebx
jne 0x42a23
movq 0x8(%r14), %rbx
leaq 0x10(%rbx), %r14
leaq -0x4(%r15), %rcx
leaq 0xd949e(%rip), %rsi     # 0x11b8ea
movq %rbp, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0xe9226
movq %rbp, %rdi
movl $0x1, %esi
callq 0xe9585
cmpq $0x5, %r15
jl 0x429da
leaq (%rbx,%r15), %rax
addq $0xc, %rax
xorl %ebx, %ebx
movq %rax, 0x38(%rsp)
movq %rax, %r12
subq %r14, %r12
xorl %ecx, %ecx
cmpq $0xc, %r12
jl 0x428e7
movq 0x18(%rbp), %rdi
movq %r14, %rsi
callq 0xef1ca
movq %rax, %r13
movq 0x18(%rbp), %rdi
leaq 0x2(%r14), %rsi
callq 0xef1ca
movq %rax, %r15
movq 0x18(%rbp), %rdi
leaq 0x4(%r14), %rsi
callq 0xef37e
movq %rax, %r12
cmpl $0x4008, %r13d          # imm = 0x4008
sete %al
movq %r15, 0x28(%rsp)
testw %r15w, %r15w
sets %r15b
andb %al, %r15b
movzbl %r15b, %eax
movl %eax, 0x34(%rsp)
movq 0x18(%rbp), %rdi
leaq 0x8(%r14), %rsi
callq 0xef37e
leaq 0x13ec6a(%rip), %rcx    # 0x181160
leaq 0xc(%r14), %rsi
xorl %edx, %edx
cmpl %r13d, (%rdx,%rcx)
je 0x42513
addq $0x10, %rdx
cmpq $0x2f0, %rdx            # imm = 0x2F0
jne 0x424fc
xorl %edx, %edx
jmp 0x42516
addq %rcx, %rdx
leaq 0xe994c(%rip), %rcx     # 0x12be69
testq %rdx, %rdx
je 0x42526
movq 0x8(%rdx), %rcx
movq %rax, 0x18(%rsp)
movq %rax, 0x8(%rsp)
movq %rsi, 0x20(%rsp)
movq %rsi, (%rsp)
movq %rbp, %rdi
leaq 0xd93c1(%rip), %rsi     # 0x11b904
movl %r13d, %edx
movq %r14, %r8
movq 0x28(%rsp), %r9
xorl %eax, %eax
callq 0xe9226
testl %ebx, %ebx
setne %al
orb %al, %r15b
jne 0x428d1
movq %rbp, %rdi
movl $0x1, %esi
callq 0xe9585
cmpl $0x4003, %r13d          # imm = 0x4003
jne 0x425b2
cmpl $0x2, (%rbp)
jl 0x42742
movq 0x18(%rbp), %rsi
movl $0x1, (%rsp)
movl $0x100, %r8d            # imm = 0x100
movq %rbp, %rdi
movq 0x20(%rsp), %rdx
movq 0x18(%rsp), %rcx
leaq 0x101d8a(%rip), %r9     # 0x144332
callq 0xe975a
jmp 0x428c4
cmpl $0x4008, %r13d          # imm = 0x4008
jne 0x42762
movq 0x28(%rsp), %rax
movl %eax, %r13d
shrl $0x8, %r13d
andl $0x7f, %r13d
leaq 0xe9893(%rip), %rcx     # 0x12be69
cmpl $0x9, %r13d
ja 0x425ed
movl %r13d, %eax
leaq 0xd97fe(%rip), %rdx     # 0x11bde4
movslq (%rdx,%rax,4), %rcx
addq %rdx, %rcx
movzbl 0x28(%rsp), %r8d
movq %rbp, %rdi
leaq 0xd95ec(%rip), %rsi     # 0x11bbe9
movl %r13d, %edx
xorl %eax, %eax
callq 0xe9226
movq %rbp, %rdi
movl $0x1, %esi
callq 0xe9585
cmpl $0x5, %r13d
jne 0x428b7
movq 0x18(%rbp), %rdi
movq 0x20(%rsp), %rsi
callq 0xef37e
movq %rax, %r13
movq 0x18(%rbp), %rdi
leaq 0x10(%r14), %rsi
callq 0xef37e
movq %rax, %r15
leaq 0xe9823(%rip), %r8      # 0x12be69
cmpq $0x2, %rax
ja 0x4265a
leaq 0xd97c1(%rip), %rax     # 0x11be14
movslq (%rax,%r15,4), %r8
addq %rax, %r8
movq %rbp, %rdi
leaq 0xd95b6(%rip), %rsi     # 0x11bc1a
movl %r13d, %edx
movl %r15d, %ecx
xorl %eax, %eax
callq 0xe9226
cmpq $0x2, %r15
je 0x427f7
cmpq $0x1, %r15
jne 0x428b7
cmpq $0x9, 0x18(%rsp)
jl 0x428b7
leaq 0x14(%r14), %rsi
movq 0x18(%rbp), %rdi
callq 0xef37e
movq %rax, %r15
movq 0x18(%rbp), %rdi
leaq 0x18(%r14), %rsi
callq 0xef37e
movq %rbp, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0xe9b05
movq 0x18(%rbp), %rdi
leaq 0x24(%r14), %rsi
callq 0xef37e
movq %rax, %r15
cmpq $0x1, %rax
leaq 0xe9792(%rip), %rcx     # 0x12be69
leaq 0xf8be3(%rip), %rax     # 0x13b2c1
cmoveq %rax, %rcx
testq %r15, %r15
leaq 0xfa85c(%rip), %rax     # 0x13cf48
cmoveq %rax, %rcx
movq %rbp, %rdi
leaq 0x1001fc(%rip), %rsi    # 0x1428f6
movl %r15d, %edx
xorl %eax, %eax
callq 0xe9226
cmpq $0x1, %r15
jne 0x428b7
movq 0x18(%rsp), %rbx
cmpq $0x1d, %rbx
jl 0x4272f
movq 0x20(%rsp), %rcx
addq %rbx, %rcx
movq 0x18(%rbp), %rax
cmpq 0x18(%rax), %rcx
jle 0x428fa
xorl %r15d, %r15d
movq %rbp, %rdi
movq %r15, %rsi
callq 0xe9752
jmp 0x428b7
movq %rbp, %rdi
leaq 0xd944b(%rip), %rsi     # 0x11bb97
movq 0x18(%rsp), %rdx
movq 0x20(%rsp), %rcx
xorl %eax, %eax
callq 0xe9226
jmp 0x428c4
cmpl $0x401c, %r13d          # imm = 0x401C
jne 0x428c4
leaq 0x14(%r14), %rsi
movq 0x18(%rbp), %rdi
callq 0xef37e
movq %rax, %r13
leaq (%r14,%rax,2), %rax
addq $0x28, %rax
movq 0x18(%rsp), %rcx
addq 0x20(%rsp), %rcx
cmpq %rcx, %rax
jle 0x4279b
xorl %r15d, %r15d
jmp 0x427ea
leaq 0x28(%r14), %rbx
addq %r13, %r13
movq %rbp, %rdi
callq 0xf6870
movq %rax, %r15
movq 0x18(%rbp), %rdi
movl $0x3, (%rsp)
movl $0x3e8, %ecx            # imm = 0x3E8
movq %rbx, %rsi
movq %r13, %rdx
movq %rax, %r8
xorl %r9d, %r9d
callq 0xf0203
movq %r15, %rdi
callq 0xf74bd
movq %rbp, %rdi
leaq 0xf852c(%rip), %rsi     # 0x13ad0c
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq %r15, %rdi
callq 0xf6bb0
jmp 0x428c4
cmpq $0x10, 0x18(%rsp)
jl 0x428b7
leaq 0x14(%r14), %rsi
movq 0x18(%rbp), %rdi
callq 0xef37e
movq %rax, %r15
leaq -0x1(%rax), %rbx
leaq 0xe964b(%rip), %rcx     # 0x12be69
cmpq $0x4, %rbx
ja 0x42832
leaq 0xd95f5(%rip), %rax     # 0x11be20
movslq (%rax,%rbx,4), %rcx
addq %rax, %rcx
movq %rbp, %rdi
leaq 0x1000ba(%rip), %rsi    # 0x1428f6
movl %r15d, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbp), %rdi
leaq 0x18(%r14), %rsi
callq 0xef37e
movq %rax, %r13
movq %rbp, %rdi
leaq 0xd9448(%rip), %rsi     # 0x11bca8
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
testq %r13, %r13
setle %al
movq 0x18(%rsp), %rcx
addq $-0x10, %rcx
cmpq %rcx, %r13
setg %cl
orb %al, %cl
jne 0x428b7
leaq 0xd9bdd(%rip), %rcx     # 0x11c467
cmpq $0x2, %rbx
jb 0x428a1
addq $-0x3, %r15
leaq 0xd87b2(%rip), %rcx     # 0x11b04d
cmpq $0x2, %r15
ja 0x428b7
movq 0x18(%rbp), %rdi
leaq 0x1c(%r14), %rsi
movq %r13, %rdx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xf0321
movq %rbp, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
movq %rbp, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
cmpq $0xd, %r12
movl $0xc, %eax
cmovlq %rax, %r12
movq 0x38(%rsp), %rax
movl 0x34(%rsp), %ecx
addq %r12, %r14
movl %ecx, %ebx
cmpq %rax, %r14
jl 0x4247f
jmp 0x429da
movq %rcx, 0x20(%rsp)
leaq 0x28(%r14), %r13
addq $-0x1c, %rbx
movq %rbp, %rdi
leaq 0xd932a(%rip), %rsi     # 0x11bc3b
movq %r13, %rdx
movq %rbx, %rcx
xorl %eax, %eax
callq 0xe9226
movl $0x78, %esi
movq %rbp, %rdi
callq 0xe969b
movq %rax, %r15
movq 0x18(%rbp), %rax
movq %rax, 0x10(%r15)
movq %r13, 0x18(%r15)
movq %rbx, 0x18(%rsp)
movq %rbx, 0x20(%r15)
movb $0x1, (%r15)
movq %rbp, %rdi
movq %r15, %rsi
callq 0xfa395
movl 0x68(%r15), %eax
cmpl $0xc, %eax
je 0x42978
testl %eax, %eax
jne 0x429ae
movq %rbp, %rdi
leaq 0xd92f0(%rip), %rsi     # 0x11bc59
movq %r13, %rdx
xorl %eax, %eax
callq 0xe9b27
jmp 0x42732
movq 0x18(%rbp), %rdi
movq 0x20(%rsp), %rdx
addq $-0x4, %rdx
movl $0x4, %ecx
movl $0x82, %esi
leaq 0x40(%rsp), %r8
callq 0xf0057
testl %eax, %eax
je 0x429ae
movq 0x40(%rsp), %rax
subq %r14, %rax
addq $-0x27, %rax
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rdx
testq %rdx, %rdx
jle 0x428b7
movq 0x18(%rbp), %rdi
movq %r15, %rcx
addq $0x6c, %rcx
movq %r13, %rsi
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xf0321
jmp 0x42732
movq %rbp, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
jmp 0x42ab7
movq 0x8(%r14), %rsi
addq $0x10, %rsi
movq 0x18(%rbp), %rdi
callq 0xef37e
cmpl $0x3f, %eax
jg 0x42a4c
cmpl $0x80000001, %eax       # imm = 0x80000001
je 0x42a79
cmpl $0x2, %eax
je 0x42a94
cmpl $0x3, %eax
jne 0x42a8b
leaq 0xd92be(%rip), %rcx     # 0x11bcdc
jmp 0x42aa4
movq 0x18(%rbp), %rsi
movq 0x8(%r14), %rdx
addq $0xc, %rdx
movl $0x1, (%rsp)
movl $0x100, %r8d            # imm = 0x100
movq %rbp, %rdi
movq %r15, %rcx
xorl %r9d, %r9d
callq 0xe975a
jmp 0x42ab7
cmpl $0x40, %eax
je 0x42a82
cmpl $0x80, %eax
je 0x42a9d
cmpl $0x40000004, %eax       # imm = 0x40000004
jne 0x42a8b
leaq 0xd927f(%rip), %rcx     # 0x11bce5
jmp 0x42aa4
movl $0x4, %ebx
leaq 0xd8e4d(%rip), %r12     # 0x11b8c1
jmp 0x42413
leaq 0xd9240(%rip), %rcx     # 0x11bcc0
jmp 0x42aa4
leaq 0xd9269(%rip), %rcx     # 0x11bcf2
jmp 0x42aa4
leaq 0xe93d7(%rip), %rcx     # 0x12be69
jmp 0x42aa4
leaq 0xd9236(%rip), %rcx     # 0x11bcd1
jmp 0x42aa4
leaq 0xd925d(%rip), %rcx     # 0x11bd01
leaq 0xd9262(%rip), %rsi     # 0x11bd0d
movq %rbp, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
movl $0x1, %eax
retq