// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/emf.c
// Function: extract_dib
static void extract_dib(deark *c, lctx *d, i64 bmi_pos, i64 bmi_len,
	i64 bits_pos, i64 bits_len)
{
	struct de_bmpinfo bi;
	dbuf *outf = NULL;
	i64 real_height;

	if(bmi_len<12 || bmi_len>2048) goto done;
	if(bits_len<1 || bmi_len+bits_len>DE_MAX_SANE_OBJECT_SIZE) goto done;

	if(!fmtutil_get_bmpinfo(c, c->infile, &bi, bmi_pos, bmi_len, 0)) {
		de_warn(c, "Invalid bitmap");
		goto done;
	}

	real_height = bi.height;

	// Sometimes, only a portion of the image is present. In most cases, we
	// can compensate for that.
	if(bi.bitcount>0 && bi.rowspan>0) {
		i64 nscanlines_present;

		nscanlines_present = bits_len/bi.rowspan;
		if(nscanlines_present>0 && nscanlines_present<bi.height && bi.infohdrsize>=16) {
			real_height = nscanlines_present;
		}
	}

	outf = dbuf_create_output_file(c, "bmp", NULL, 0);

	fmtutil_generate_bmpfileheader(c, outf, &bi, 14 + bmi_len + bits_len);

	if(real_height == bi.height) {
		// Copy the BITMAPINFO (headers & palette)
		dbuf_copy(c->infile, bmi_pos, bmi_len, outf);
	}
	else {
		u8 *tmp_bmi;

		// Make a copy of the BITMAPINFO data, for us to modify.
		tmp_bmi = de_malloc(c, bmi_len);
		de_read(tmp_bmi, bmi_pos, bmi_len);

		de_writeu32le_direct(&tmp_bmi[8], real_height); // Correct the biHeight field

		if(bmi_len>=24) {
			// Correct (or set) the biSizeImage field
			de_writeu32le_direct(&tmp_bmi[20], bits_len);
		}
		dbuf_write(outf, tmp_bmi, bmi_len);
		de_free(c, tmp_bmi);
	}

	// Copy the bitmap bits
	dbuf_copy(c->infile, bits_pos, bits_len, outf);

done:
	dbuf_close(outf);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rcx, %r14
cmpq $0x801, %rdx            # imm = 0x801
setge %al
leaq (%r8,%rdx), %rcx
cmpq $0x5f5e101, %rcx        # imm = 0x5F5E101
setge %cl
orb %al, %cl
je 0x42efa
xorl %ebp, %ebp
jmp 0x4304a
movq %r8, %rbx
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %r15
movq 0x18(%rdi), %rsi
leaq 0x18(%rsp), %rdx
movq %r13, %rcx
movq %r12, %r8
xorl %r9d, %r9d
callq 0xf7881
testl %eax, %eax
je 0x42fc0
movq 0x40(%rsp), %rsi
movq 0x78(%rsp), %rcx
cmpq $0x0, 0x48(%rsp)
setg %al
testq %rcx, %rcx
setg %dl
andb %al, %dl
cmpb $0x1, %dl
movq %r14, 0x10(%rsp)
jne 0x42f6b
movq %rbx, %rax
xorl %edx, %edx
divq %rcx
testq %rax, %rax
jle 0x42f6b
cmpq $0x10, 0x30(%rsp)
movq %rsi, %rcx
cmovgeq %rax, %rcx
cmpq %rsi, %rax
cmovlq %rcx, %rsi
movq %rsi, 0x8(%rsp)
leaq 0xde0db(%rip), %rsi     # 0x121052
movq %r15, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xf037a
movq %rax, %rbp
leaq (%rbx,%r12), %rcx
addq $0xe, %rcx
leaq 0x18(%rsp), %r14
movq %r15, %rdi
movq %rax, %rsi
movq %r14, %rdx
callq 0xf7d41
movq 0x8(%rsp), %rax
cmpq 0x28(%r14), %rax
jne 0x42fd5
movq 0x18(%r15), %rdi
movq %r13, %rsi
movq %r12, %rdx
movq %rbp, %rcx
callq 0xefabc
jmp 0x43036
leaq 0xd8db8(%rip), %rsi     # 0x11bd7f
xorl %ebp, %ebp
movq %r15, %rdi
xorl %eax, %eax
callq 0xe9b27
jmp 0x4304a
movq %r15, %rdi
movq %rax, 0x8(%rsp)
movq %r12, %rsi
callq 0xe969b
movq %rax, %r14
movq 0x18(%r15), %rdi
movq %rax, %rsi
movq %r13, %rdx
movq %r12, %rcx
callq 0xeeee8
leaq 0x8(%r14), %rdi
movq 0x8(%rsp), %rsi
callq 0xf1642
cmpq $0x18, %r12
jl 0x4301d
movq %r14, %rdi
addq $0x14, %rdi
movq %rbx, %rsi
callq 0xf1642
movq %rbp, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0xefb82
movq %r15, %rdi
movq %r14, %rsi
callq 0xe9752
movq 0x18(%r15), %rdi
movq 0x10(%rsp), %rsi
movq %rbx, %rdx
movq %rbp, %rcx
callq 0xefabc
movq %rbp, %rdi
callq 0xf0b13
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq