// File: /workspace/github_c_makefile_star2_O0/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:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl $0x0, -0x2c(%rbp)
movq -0x18(%rbp), %rax
cmpq $0x10, 0x10(%rax)
jge 0x5ac9e
jmp 0x5aef0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rsi
addq $0x8, %rsi
callq 0x165540
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rdx
leaq 0x14c902(%rip), %rsi    # 0x1a75cc
movb $0x0, %al
callq 0x15b0d0
movq -0x28(%rbp), %rax
addq $0xc, %rax
movq -0x18(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jle 0x5ace8
jmp 0x5aef0
movl $0x0, -0x30(%rbp)
leaq 0x15cf37(%rip), %rax    # 0x1b7c2d
movq %rax, -0x20(%rbp)
cmpq $0x4, -0x28(%rbp)
jl 0x5ada4
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rsi
addq $0xc, %rsi
leaq -0x60(%rbp), %rdx
movl $0x4, %ecx
movl $0x1, %r8d
callq 0x169860
cmpl $0x0, -0x5c(%rbp)
jne 0x5ad47
movl $0x1, -0x30(%rbp)
leaq 0x14c899(%rip), %rax    # 0x1a75da
movq %rax, -0x20(%rbp)
jmp 0x5ad83
cmpl $0x2b464d45, -0x5c(%rbp) # imm = 0x2B464D45
jne 0x5ad64
movl $0x2, -0x30(%rbp)
leaq 0x14c891(%rip), %rax    # 0x1a75ef
movq %rax, -0x20(%rbp)
jmp 0x5ad81
cmpl $0x43494447, -0x5c(%rbp) # imm = 0x43494447
jne 0x5ad7f
movl $0x3, -0x30(%rbp)
leaq 0x14c888(%rip), %rax    # 0x1a7603
movq %rax, -0x20(%rbp)
jmp 0x5ad81
jmp 0x5ad83
movq -0x8(%rbp), %rdi
movl -0x5c(%rbp), %edx
leaq -0x60(%rbp), %rcx
addq $0x10, %rcx
movq -0x20(%rbp), %r8
leaq 0x14c879(%rip), %rsi    # 0x1a7616
movb $0x0, %al
callq 0x15b0d0
cmpl $0x0, -0x30(%rbp)
jne 0x5ae55
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x70(%rbp), %rsi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdx
addq $0xc, %rdx
movl $0x10, %ecx
callq 0x164d10
cmpq $0x7, -0x28(%rbp)
jl 0x5ae05
leaq -0x70(%rbp), %rdi
leaq 0x14c84b(%rip), %rsi    # 0x1a762d
movl $0x7, %edx
callq 0x19170
cmpl $0x0, %eax
jne 0x5ae05
movl $0x4, -0x30(%rbp)
leaq 0x14c836(%rip), %rax    # 0x1a7635
movq %rax, -0x20(%rbp)
jmp 0x5ae3f
cmpq $0x8, -0x28(%rbp)
jl 0x5ae3d
leaq -0x70(%rbp), %rax
movq (%rax), %rax
movabsq $0x3d676e6977617244, %rcx # imm = 0x3D676E6977617244
subq %rcx, %rax
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x5ae3d
movl $0x5, -0x30(%rbp)
leaq 0x14c81a(%rip), %rax    # 0x1a7653
movq %rax, -0x20(%rbp)
jmp 0x5ae3f
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rdx
leaq 0x15ba1c(%rip), %rsi    # 0x1b686a
movb $0x0, %al
callq 0x15b0d0
cmpl $0x2, -0x30(%rbp)
jne 0x5ae85
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdx
addq $0x10, %rdx
movq -0x28(%rbp), %rcx
subq $0x4, %rcx
callq 0x5b7e0
movl $0x1, -0x2c(%rbp)
jmp 0x5aeb5
cmpl $0x3, -0x30(%rbp)
jne 0x5aeb3
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdx
addq $0x10, %rdx
movq -0x28(%rbp), %rcx
subq $0x4, %rcx
callq 0x5b890
movl $0x1, -0x2c(%rbp)
jmp 0x5aeb5
cmpl $0x0, -0x2c(%rbp)
jne 0x5aeee
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdx
addq $0xc, %rdx
movq -0x28(%rbp), %rcx
movl $0x100, %r8d            # imm = 0x100
xorl %eax, %eax
movl %eax, %r9d
movl $0x1, (%rsp)
callq 0x15b840
jmp 0x5aef0
movl $0x1, %eax
addq $0x80, %rsp
popq %rbp
retq
nop