// File: /workspace/github_c_makefile_star2_O2/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 0x40c7a
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rdx, %r14
movq %rdi, %r13
movq 0x18(%rdi), %rdi
movq 0x8(%rdx), %rsi
addq $0x8, %rsi
callq 0xe409f
movq %rax, %r15
leaq 0xce410(%rip), %rsi     # 0x10e890
movq %r13, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
leaq 0xc(%r15), %rax
cmpq 0x10(%r14), %rax
jg 0x40c6c
cmpq $0x4, %r15
movq %r13, 0x20(%rsp)
jl 0x404ed
pushq $0x1
popq %rbp
movq 0x18(%r13), %rdi
movq 0x8(%r14), %rsi
addq $0xc, %rsi
leaq 0x40(%rsp), %r12
pushq $0x4
popq %rcx
movq %r12, %rdx
movl %ebp, %r8d
callq 0xe657c
movl 0x4(%r12), %edx
testl %edx, %edx
je 0x40502
cmpl $0x43494447, %edx       # imm = 0x43494447
je 0x404f6
cmpl $0x2b464d45, %edx       # imm = 0x2B464D45
jne 0x4050d
pushq $0x2
popq %rbp
leaq 0xce3c8(%rip), %r12     # 0x10e8b3
jmp 0x40509
leaq 0xde9ed(%rip), %r12     # 0x11eee1
jmp 0x4053a
pushq $0x3
popq %rbp
leaq 0xce3c7(%rip), %r12     # 0x10e8c7
jmp 0x40509
leaq 0xce395(%rip), %r12     # 0x10e89e
xorl %ebx, %ebx
jmp 0x40518
xorl %ebp, %ebp
movb $0x1, %bl
leaq 0xde9c9(%rip), %r12     # 0x11eee1
leaq 0x50(%rsp), %rcx
leaq 0xce3b6(%rip), %rsi     # 0x10e8da
movq 0x20(%rsp), %r13
movq %r13, %rdi
movq %r12, %r8
xorl %eax, %eax
callq 0xde896
testb %bl, %bl
je 0x405b5
movq 0x18(%r13), %rdi
movq 0x8(%r14), %rdx
addq $0xc, %rdx
leaq 0x40(%rsp), %rsi
pushq $0x10
popq %rcx
callq 0xe3cb7
cmpq $0x7, %r15
jl 0x40575
leaq 0xce391(%rip), %rsi     # 0x10e8f1
leaq 0x40(%rsp), %rdi
pushq $0x7
popq %rdx
callq 0x18230
testl %eax, %eax
je 0x40c16
cmpq $0x8, %r15
setge %al
movabsq $0x3d676e6977617244, %rcx # imm = 0x3D676E6977617244
cmpq %rcx, 0x40(%rsp)
sete %cl
andb %al, %cl
leaq 0xce377(%rip), %rax     # 0x10e90e
cmovneq %rax, %r12
movzbl %cl, %eax
leal (%rax,%rax,4), %ebp
leaq 0xdd642(%rip), %rsi     # 0x11dbea
movq %r13, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xde896
cmpl $0x3, %ebp
je 0x40bab
cmpl $0x2, %ebp
jne 0x40bed
movq 0x8(%r14), %rbx
leaq 0x10(%rbx), %r14
leaq -0x4(%r15), %rcx
leaq 0xce348(%rip), %rsi     # 0x10e922
movq %r13, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %r13, %rdi
callq 0xdeba7
leaq (%rbx,%r15), %rax
addq $0xc, %rax
xorl %edx, %edx
movq %rax, 0x38(%rsp)
movq %rax, %r13
subq %r14, %r13
jle 0x40b99
xorl %ecx, %ecx
cmpq $0xc, %r13
jl 0x40a83
movl %edx, 0x30(%rsp)
movq %r14, %rbx
movq 0x20(%rsp), %r14
movq 0x18(%r14), %rdi
movq %rbx, %rsi
callq 0xe3fa3
movq %rax, %r15
movq 0x18(%r14), %rdi
leaq 0x2(%rbx), %rsi
callq 0xe3fa3
movq %rax, %r12
movq 0x18(%r14), %rdi
leaq 0x4(%rbx), %rsi
callq 0xe409f
movq %rax, %r13
cmpl $0x4008, %r15d          # imm = 0x4008
sete %al
testw %r12w, %r12w
sets %bpl
andb %al, %bpl
movzbl %bpl, %eax
movl %eax, 0x34(%rsp)
movq 0x18(%r14), %rdi
movq %rbx, %r14
leaq 0x143bc1(%rip), %rbx    # 0x184240
leaq 0x8(%r14), %rsi
callq 0xe409f
leaq 0xc(%r14), %rsi
xorl %edx, %edx
leaq 0x10(%rdx), %rcx
cmpq $0x300, %rcx            # imm = 0x300
je 0x406ab
cmpl %r15d, (%rdx,%rbx)
movq %rcx, %rdx
jne 0x4068e
movq -0x8(%rcx,%rbx), %rcx
jmp 0x406b2
leaq 0xde82f(%rip), %rcx     # 0x11eee1
movq %rax, 0x18(%rsp)
movq %rax, 0x8(%rsp)
movq %rsi, 0x28(%rsp)
movq %rsi, (%rsp)
movq 0x20(%rsp), %rdi
leaq 0xce26b(%rip), %rsi     # 0x10e93c
movl %r15d, %edx
movq %r14, %r8
movl %r12d, %r9d
xorl %eax, %eax
callq 0xde896
cmpl $0x0, 0x30(%rsp)
setne %al
orb %al, %bpl
jne 0x40a6f
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
pushq $0x1
popq %rsi
callq 0xdeba7
cmpl $0x4003, %r15d          # imm = 0x4003
jne 0x40743
cmpl $0x2, (%rbx)
jl 0x407ff
movq 0x18(%rbx), %rsi
movl $0x1, (%rsp)
movl $0x100, %r8d            # imm = 0x100
movq %rbx, %rdi
movq 0x28(%rsp), %rdx
movq 0x18(%rsp), %rcx
leaq 0xf6c89(%rip), %r9      # 0x1373c2
callq 0xded3a
jmp 0x40a64
cmpl $0x4008, %r15d          # imm = 0x4008
jne 0x4081f
movl %r12d, %r15d
shrl $0x8, %r15d
andl $0x7f, %r15d
leaq 0xde77f(%rip), %rcx     # 0x11eee1
cmpl $0x9, %r15d
ja 0x40779
movl %r15d, %eax
leaq 0xce6aa(%rip), %rdx     # 0x10ee1c
movslq (%rdx,%rax,4), %rcx
addq %rdx, %rcx
movzbl %r12b, %r8d
movq %rbx, %rdi
leaq 0xce49a(%rip), %rsi     # 0x10ec21
movl %r15d, %edx
xorl %eax, %eax
callq 0xde896
movq %rbx, %rdi
pushq $0x1
popq %rsi
callq 0xdeba7
cmpl $0x5, %r15d
movq %rbx, %rbp
jne 0x40a56
movq 0x18(%rbp), %rdi
movq 0x28(%rsp), %rsi
callq 0xe409f
movq %rax, %r15
movq 0x18(%rbp), %rdi
leaq 0x10(%r14), %rsi
callq 0xe409f
movq %rbp, %rdi
leaq 0xce481(%rip), %rsi     # 0x10ec52
movl %r15d, %edx
cmpq $0x2, %rax
je 0x4096e
cmpq $0x1, %rax
je 0x408b4
testq %rax, %rax
jne 0x40a46
xorl %ecx, %ecx
leaq 0xce449(%rip), %r8      # 0x10ec43
jmp 0x40a4f
movq %rbx, %rdi
leaq 0xce3c6(%rip), %rsi     # 0x10ebcf
movq 0x18(%rsp), %rdx
movq 0x28(%rsp), %rcx
xorl %eax, %eax
callq 0xde896
jmp 0x40a64
cmpl $0x401c, %r15d          # imm = 0x401C
jne 0x40a64
leaq 0x14(%r14), %rsi
movq 0x18(%rbx), %rdi
callq 0xe409f
movq %rax, %r12
leaq (%r14,%rax,2), %rax
addq $0x28, %rax
movq 0x18(%rsp), %rcx
addq 0x28(%rsp), %rcx
cmpq %rcx, %rax
jle 0x40858
xorl %r15d, %r15d
jmp 0x408a7
leaq 0x28(%r14), %rbp
addq %r12, %r12
movq %rbx, %rdi
callq 0xeac74
movq %rax, %r15
movq 0x18(%rbx), %rdi
movl $0x3, (%rsp)
movl $0x3e8, %ecx            # imm = 0x3E8
movq %rbp, %rsi
movq %r12, %rdx
movq %rax, %r8
xorl %r9d, %r9d
callq 0xe4ced
movq %r15, %rdi
callq 0xeb7e4
movq %rbx, %rdi
leaq 0xed517(%rip), %rsi     # 0x12ddb4
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movq %r15, %rdi
callq 0xeaf3c
jmp 0x40a64
pushq $0x1
popq %rcx
leaq 0xce38d(%rip), %r8      # 0x10ec4b
xorl %eax, %eax
callq 0xde896
cmpq $0x9, 0x18(%rsp)
jl 0x40a56
leaq 0x14(%r14), %rsi
movq 0x18(%rbp), %rdi
callq 0xe409f
movq %rax, %r15
movq 0x18(%rbp), %rdi
leaq 0x18(%r14), %rsi
callq 0xe409f
movq %rbp, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0xdf038
movq 0x18(%rbp), %rdi
leaq 0x24(%r14), %rsi
callq 0xe409f
movq %rax, %r15
cmpq $0x1, %rax
leaq 0xde5ca(%rip), %rcx     # 0x11eee1
leaq 0xeda6b(%rip), %rax     # 0x12e389
cmoveq %rax, %rcx
testq %r15, %r15
leaq 0xef6ec(%rip), %rax     # 0x130018
cmoveq %rax, %rcx
movq %rbp, %rdi
leaq 0xf504c(%rip), %rsi     # 0x135986
movl %r15d, %edx
xorl %eax, %eax
callq 0xde896
cmpq $0x1, %r15
jne 0x40a56
movq 0x18(%rsp), %rbx
leaq -0x8(%rbx), %rax
cmpq $0x15, %rax
jae 0x40a8d
xorl %r15d, %r15d
movq 0x20(%rsp), %rbp
jmp 0x40b89
pushq $0x2
popq %rcx
leaq 0xd17e0(%rip), %r8      # 0x112158
xorl %eax, %eax
callq 0xde896
cmpq $0x10, 0x18(%rsp)
jl 0x40a56
leaq 0x14(%r14), %rsi
movq 0x18(%rbp), %rdi
callq 0xe409f
movq %rax, %r15
leaq -0x1(%rax), %rbx
leaq 0xde53b(%rip), %rcx     # 0x11eee1
cmpq $0x4, %rbx
ja 0x409ba
leaq 0xce499(%rip), %rax     # 0x10ee4c
movslq (%rax,%rbx,4), %rcx
addq %rax, %rcx
movq 0x20(%rsp), %rbp
movq %rbp, %rdi
leaq 0xf4fbd(%rip), %rsi     # 0x135986
movl %r15d, %edx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbp), %rdi
leaq 0x18(%r14), %rsi
callq 0xe409f
movq %rax, %r12
movq %rbp, %rdi
leaq 0xce2f3(%rip), %rsi     # 0x10ece0
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
testq %r12, %r12
setle %al
movq 0x18(%rsp), %rcx
addq $-0x10, %rcx
cmpq %rcx, %r12
seta %cl
orb %al, %cl
jne 0x40a56
leaq 0xcea70(%rip), %rcx     # 0x10f487
cmpq $0x2, %rbx
jb 0x40a2e
addq $-0x3, %r15
leaq 0xcd65d(%rip), %rcx     # 0x10e085
cmpq $0x2, %r15
ja 0x40a56
movq 0x18(%rbp), %rdi
leaq 0x1c(%r14), %rsi
movq %r12, %rdx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xe4e0b
jmp 0x40a56
movl %eax, %ecx
leaq 0xde492(%rip), %r8      # 0x11eee1
xorl %eax, %eax
callq 0xde896
movq %rbp, %rdi
pushq $-0x1
popq %rsi
callq 0xdeba7
movq %rbp, %rbx
movq %rbx, %rdi
pushq $-0x1
popq %rsi
callq 0xdeba7
cmpq $0xd, %r13
pushq $0xc
popq %rax
cmovlq %rax, %r13
movq 0x38(%rsp), %rax
movl 0x34(%rsp), %ecx
addq %r13, %r14
movl %ecx, %edx
jmp 0x40601
movq 0x28(%rsp), %rcx
addq %rbx, %rcx
movq 0x20(%rsp), %rbp
movq 0x18(%rbp), %rax
cmpq 0x18(%rax), %rcx
jle 0x40aac
xorl %r15d, %r15d
jmp 0x40b89
movq %rcx, 0x28(%rsp)
leaq 0x28(%r14), %r12
addq $-0x1c, %rbx
movq %rbp, %rdi
leaq 0xce1b0(%rip), %rsi     # 0x10ec73
movq %r12, %rdx
movq %rbx, %rcx
xorl %eax, %eax
callq 0xde896
movq %rbp, %rdi
pushq $0x78
popq %rsi
callq 0xdecaf
movq %rax, %r15
movq 0x18(%rbp), %rax
movq %rax, 0x10(%r15)
movq %r12, 0x18(%r15)
movq %rbx, 0x18(%rsp)
movq %rbx, 0x20(%r15)
movb $0x1, (%r15)
movq %rbp, %rdi
movq %r15, %rsi
callq 0xee4da
movl 0x68(%r15), %eax
cmpl $0xc, %eax
je 0x40b25
testl %eax, %eax
jne 0x40b6b
movq %rbp, %rdi
leaq 0xce178(%rip), %rsi     # 0x10ec91
movq %r12, %rdx
xorl %eax, %eax
callq 0xdf05a
jmp 0x40b89
movq 0x18(%rbp), %rdi
movq 0x28(%rsp), %rdx
addq $-0x4, %rdx
movl $0x82, %esi
pushq $0x4
popq %rcx
leaq 0x40(%rsp), %r8
callq 0xe4b21
testl %eax, %eax
je 0x40b6b
movq 0x40(%rsp), %rcx
subq %r12, %rcx
movabsq $0x7ffffffffffffffe, %rax # imm = 0x7FFFFFFFFFFFFFFE
cmpq %rax, %rcx
ja 0x40a56
incq %rcx
movq %rcx, 0x18(%rsp)
movq 0x18(%rbp), %rdi
movq %r15, %rcx
addq $0x6c, %rcx
movq %r12, %rsi
movq 0x18(%rsp), %rdx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xe4e0b
movq %rbp, %rdi
movq %r15, %rsi
callq 0xded32
jmp 0x40a56
pushq $-0x1
popq %rsi
movq 0x20(%rsp), %rdi
callq 0xdeba7
jmp 0x40c6c
movq 0x8(%r14), %rsi
addq $0x10, %rsi
movq 0x18(%r13), %rdi
callq 0xe409f
cmpl $0x80000001, %eax       # imm = 0x80000001
je 0x40c52
cmpl $0x40000004, %eax       # imm = 0x40000004
je 0x40c49
cmpl $0x3, %eax
je 0x40c25
cmpl $0x40, %eax
je 0x40c37
cmpl $0x80, %eax
je 0x40c2e
cmpl $0x2, %eax
jne 0x40c40
leaq 0xce11e(%rip), %rcx     # 0x10ed09
jmp 0x40c59
movq 0x18(%r13), %rsi
movq 0x8(%r14), %rdx
addq $0xc, %rdx
movl $0x1, (%rsp)
movl $0x100, %r8d            # imm = 0x100
movq %r13, %rdi
movq %r15, %rcx
xorl %r9d, %r9d
callq 0xded3a
jmp 0x40c6c
pushq $0x4
popq %rbp
leaq 0xcdcd9(%rip), %r12     # 0x10e8f9
jmp 0x405a1
leaq 0xce0e8(%rip), %rcx     # 0x10ed14
jmp 0x40c59
leaq 0xce104(%rip), %rcx     # 0x10ed39
jmp 0x40c59
leaq 0xce0ec(%rip), %rcx     # 0x10ed2a
jmp 0x40c59
leaq 0xde29a(%rip), %rcx     # 0x11eee1
jmp 0x40c59
leaq 0xce0cd(%rip), %rcx     # 0x10ed1d
jmp 0x40c59
leaq 0xce09f(%rip), %rcx     # 0x10ecf8
leaq 0xce0e5(%rip), %rsi     # 0x10ed45
movq %r13, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
pushq $0x1
popq %rax
retq