// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/wmf.c
// Function: wmf_handler_BITBLT_STRETCHBLT_DIBBITBLT
static int wmf_handler_BITBLT_STRETCHBLT_DIBBITBLT(deark *c, lctx *d, struct decoder_params *dp)
{
	i64 pos = dp->dpos;
	int has_src_bitmap;
	unsigned int RasterOperation;
	i64 XSrc, YSrc;
	i64 Width, Height;
	i64 YDest, XDest;

	has_src_bitmap = (dp->recsize_words != ((i64)(dp->recfunc>>8)+3));
	de_dbg(c, "has src bitmap: %d", has_src_bitmap);
	if(!has_src_bitmap) goto done;

	RasterOperation = (unsigned int)de_getu32le(pos);
	de_dbg(c, "RasterOperation: 0x%08x", RasterOperation);
	pos += 4;

	if(dp->rectype==0x23) { // STRETCHBLT
		i64 SrcWidth, SrcHeight;
		SrcHeight = de_geti16le_p(&pos);
		SrcWidth = de_geti16le_p(&pos);
		de_dbg(c, "SrcWidth, SrcHeight: %d"DE_CHAR_TIMES"%d",
			(int)SrcWidth, (int)SrcHeight);
	}

	YSrc = de_geti16le_p(&pos);
	XSrc = de_geti16le_p(&pos);
	de_dbg(c, "XSrc, YSrc: (%d, %d)", (int)XSrc, (int)YSrc);

	Height = de_geti16le_p(&pos);
	Width = de_geti16le_p(&pos);
	de_dbg_dimensions(c, Width, Height);

	YDest = de_geti16le_p(&pos);
	XDest = de_geti16le_p(&pos);
	de_dbg(c, "XDest, YDest: (%d, %d)", (int)XDest, (int)YDest);

	// TODO: Bitmap16 object (if BITBLT or STRETCHBLT)
	if(dp->rectype==0x40) { // DIBBITBLT
		i64 dib_pos, dib_len;

		// TODO: Merge this with the DIBSTRETCHBLT, STRETCHDIB code.
		dib_pos = pos;
		dib_len = dp->dpos + dp->dlen - dib_pos;

		if(dib_len<12) goto done;
		de_dbg(c, "DIB at %d, size=%d", (int)dib_pos, (int)dib_len);

		de_dbg_indent(c, 1);
		de_run_module_by_id_on_slice(c, "dib", NULL, c->infile, dib_pos, dib_len);
		de_dbg_indent(c, -1);
	}

done:
	return 1;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x10, %rsp
movq %rdx, %r14
movq %rdi, %rbx
movq 0x10(%rdx), %r12
movq 0x20(%rdx), %r15
movzbl 0x1(%rdx), %r13d
addq $0x3, %r13
xorl %edx, %edx
cmpq %r13, %r12
setne %dl
leaq 0x6deca(%rip), %rsi     # 0x140b13
xorl %eax, %eax
callq 0xe8617
cmpq %r13, %r12
je 0xd2db7
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeed61
leaq 0x6deba(%rip), %rsi     # 0x140b26
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
addq $0x4, %r15
movq %r15, 0x8(%rsp)
cmpb $0x23, 0x2(%r14)
jne 0xd2cbe
movq 0x18(%rbx), %rdi
leaq 0x8(%rsp), %r15
movq %r15, %rsi
callq 0xeecce
movq %rax, %r12
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeecce
leaq 0x6de8f(%rip), %rsi     # 0x140b3e
movq %rbx, %rdi
movl %eax, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
leaq 0x8(%rsp), %r15
movq %r15, %rsi
callq 0xeecce
movq %rax, %r12
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeecce
leaq 0x6de75(%rip), %rsi     # 0x140b5a
movq %rbx, %rdi
movl %eax, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeecce
movq %rax, %r12
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeecce
movq %rbx, %rdi
movq %rax, %rsi
movq %r12, %rdx
callq 0xe8f88
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeecce
movq %rax, %r12
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeecce
leaq 0x6de30(%rip), %rsi     # 0x140b6f
movq %rbx, %rdi
movl %eax, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0xe8617
cmpb $0x40, 0x2(%r14)
jne 0xd2db7
movq 0x8(%rsp), %r15
movq 0x20(%r14), %r12
subq %r15, %r12
addq 0x28(%r14), %r12
cmpq $0xc, %r12
jl 0xd2db7
leaq 0x6de14(%rip), %rsi     # 0x140b86
movq %rbx, %rdi
movl %r15d, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%rbx), %rcx
leaq 0x50675(%rip), %rsi     # 0x12340f
movq %rbx, %rdi
xorl %edx, %edx
movq %r15, %r8
movq %r12, %r9
callq 0xe9aa3
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movl $0x1, %eax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq