// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/wmf.c
// Function: do_ESCAPE_MFCOMMENT_EMF
static void do_ESCAPE_MFCOMMENT_EMF(deark *c, lctx *d, struct decoder_params *dp,
	i64 pos1, i64 bytecount)
{
	i64 pos = pos1;
	i64 endpos = dp->dpos + dp->dlen;
	unsigned int CommentId;
	i64 n;
	i64 CommentRecordCount, CurrentRecordSize;
	i64 RemainingBytes, EnhancedMetafileDataSize;

	if(pos+34>endpos) {
		de_dbg(c, "[bad/unsupported embedded EMF data (too short)]");
		goto done;
	}

	CommentId = (unsigned int)de_getu32le_p(&pos);
	de_dbg(c, "CommentIdentifier: 0x%08x", CommentId);
	if(CommentId!=0x43464d57U) goto done;

	n = de_getu32le_p(&pos);
	de_dbg(c, "CommentType: 0x%08x", (unsigned int)n);
	if(n != 1) {
		de_dbg(c, "[bad/unsupported embedded EMF data (unsupported CommentType)]");
		goto done;
	}

	n = de_getu32le_p(&pos);
	de_dbg(c, "Version: 0x%08x", (unsigned int)n);
	n = de_getu16le_p(&pos);
	de_dbg(c, "CheckSum (reported): 0x%04x", (unsigned int)n);
	n = de_getu32le_p(&pos);
	de_dbg(c, "Flags: 0x%08x", (unsigned int)n);
	CommentRecordCount = de_getu32le_p(&pos);
	de_dbg(c, "CommentRecordCount: %d", (int)CommentRecordCount);
	CurrentRecordSize = de_getu32le_p(&pos);
	de_dbg(c, "CurrentRecordSize: %d", (int)CurrentRecordSize);
	RemainingBytes = de_getu32le_p(&pos);
	de_dbg(c, "RemainingBytes: %d", (int)RemainingBytes);

	// The spec says that the ByteCount field must be 34 +
	// EnhancedMetafileDataSize, but that doesn't make sense to me.
	// Maybe it was supposed to be 34 + CurrentRecordSize?
	EnhancedMetafileDataSize = de_getu32le_p(&pos);
	de_dbg(c, "EnhancedMetafileDataSize: %d", (int)EnhancedMetafileDataSize);

	if(pos+CurrentRecordSize>endpos) goto done;
	de_dbg(c, "embedded EMF data at %d, len=%d", (int)pos, (int)CurrentRecordSize);

	if(!d->embedded_emf && (CurrentRecordSize+RemainingBytes==EnhancedMetafileDataSize)) {
		// Looks like the first record
		d->embedded_emf = dbuf_create_output_file(c, "emf", NULL, 0);
	}

	if(d->embedded_emf) {
		dbuf_copy(c->infile, pos, CurrentRecordSize, d->embedded_emf);
	}

	if(d->embedded_emf && RemainingBytes==0) {
		// Looks like the last record
		dbuf_close(d->embedded_emf);
		d->embedded_emf = NULL;
	}

done:
	;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x18(%rbp), %rcx
addq 0x28(%rcx), %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rax
addq $0x22, %rax
cmpq -0x38(%rbp), %rax
jle 0x13a59d
movq -0x8(%rbp), %rdi
leaq 0x925f7(%rip), %rsi     # 0x1ccb88
movb $0x0, %al
callq 0x15b0d0
jmp 0x13a81a
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165580
movl %eax, -0x3c(%rbp)
movq -0x8(%rbp), %rdi
movl -0x3c(%rbp), %edx
leaq 0x925f9(%rip), %rsi     # 0x1ccbb8
movb $0x0, %al
callq 0x15b0d0
cmpl $0x43464d57, -0x3c(%rbp) # imm = 0x43464D57
je 0x13a5d4
jmp 0x13a81a
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165580
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rax
movl %eax, %edx
leaq 0x925d8(%rip), %rsi     # 0x1ccbd2
movb $0x0, %al
callq 0x15b0d0
cmpq $0x1, -0x48(%rbp)
je 0x13a61f
movq -0x8(%rbp), %rdi
leaq 0x925d3(%rip), %rsi     # 0x1ccbe6
movb $0x0, %al
callq 0x15b0d0
jmp 0x13a81a
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165580
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rax
movl %eax, %edx
leaq 0x82e76(%rip), %rsi     # 0x1bd4bb
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rax
movl %eax, %edx
leaq 0x925b2(%rip), %rsi     # 0x1ccc24
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165580
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rax
movl %eax, %edx
leaq 0x925a1(%rip), %rsi     # 0x1ccc40
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165580
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rax
movl %eax, %edx
leaq 0x92582(%rip), %rsi     # 0x1ccc4e
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165580
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rax
movl %eax, %edx
leaq 0x9256c(%rip), %rsi     # 0x1ccc65
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165580
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rax
movl %eax, %edx
leaq 0x92555(%rip), %rsi     # 0x1ccc7b
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165580
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rdi
movq -0x68(%rbp), %rax
movl %eax, %edx
leaq 0x9253b(%rip), %rsi     # 0x1ccc8e
movb $0x0, %al
callq 0x15b0d0
movq -0x30(%rbp), %rax
addq -0x58(%rbp), %rax
cmpq -0x38(%rbp), %rax
jle 0x13a76d
jmp 0x13a81a
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rax
movl %eax, %edx
movq -0x58(%rbp), %rax
movl %eax, %ecx
leaq 0x92527(%rip), %rsi     # 0x1cccab
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
cmpq $0x0, 0x20(%rax)
jne 0x13a7c5
movq -0x58(%rbp), %rax
addq -0x60(%rbp), %rax
cmpq -0x68(%rbp), %rax
jne 0x13a7c5
movq -0x8(%rbp), %rdi
leaq 0x6c612(%rip), %rsi     # 0x1a6dc1
xorl %eax, %eax
movl %eax, %edx
xorl %ecx, %ecx
callq 0x166c60
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0x13a7ed
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rcx
callq 0x166070
movq -0x10(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0x13a818
cmpq $0x0, -0x60(%rbp)
jne 0x13a818
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x1676a0
movq -0x10(%rbp), %rax
movq $0x0, 0x20(%rax)
jmp 0x13a81a
addq $0x70, %rsp
popq %rbp
retq