// File: /workspace/github_c_makefile_star2_O2/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, %r13
cmpq $0x801, %rdx            # imm = 0x801
setge %al
leaq (%r8,%rdx), %r14
cmpq $0x5f5e101, %r14        # imm = 0x5F5E101
setge %cl
orb %al, %cl
je 0x4109f
xorl %ebp, %ebp
jmp 0x41201
movq %r8, %rbx
movq %rdx, %r12
movq %rsi, %rbp
movq %rdi, %r15
movq 0x18(%rdi), %rsi
leaq 0x18(%rsp), %rdx
movq %rbp, %rcx
movq %r12, %r8
xorl %r9d, %r9d
callq 0xebba5
testl %eax, %eax
je 0x41176
movq 0x40(%rsp), %rax
movq %rax, (%rsp)
movq 0x78(%rsp), %rcx
cmpq $0x0, 0x48(%rsp)
setg %al
testq %rcx, %rcx
setg %dl
andb %al, %dl
cmpb $0x1, %dl
movq %r13, 0x10(%rsp)
movq %rbp, 0x8(%rsp)
jne 0x41121
movq %rbx, %rax
xorl %edx, %edx
divq %rcx
testq %rax, %rax
jle 0x41121
cmpq $0x10, 0x30(%rsp)
movq (%rsp), %rdx
movq %rdx, %rcx
cmovgeq %rax, %rcx
cmpq %rdx, %rax
cmovlq %rcx, %rdx
movq %rdx, (%rsp)
leaq 0xd2f4a(%rip), %rsi     # 0x114072
movq %r15, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xe4e62
movq %rax, %rbp
addq $0xe, %r14
leaq 0x18(%rsp), %r13
movq %r15, %rdi
movq %rax, %rsi
movq %r13, %rdx
movq %r14, %rcx
callq 0xec053
movq (%rsp), %rax
cmpq 0x28(%r13), %rax
jne 0x4118b
movq 0x18(%r15), %rdi
movq 0x8(%rsp), %rsi
movq %r12, %rdx
movq %rbp, %rcx
callq 0xe45f4
movq 0x10(%rsp), %r13
jmp 0x411ef
leaq 0xcdc3a(%rip), %rsi     # 0x10edb7
xorl %ebp, %ebp
movq %r15, %rdi
xorl %eax, %eax
callq 0xdf05a
jmp 0x41201
movq %rax, %r13
movq %r15, %rdi
movq %r12, %rsi
callq 0xdecaf
movq %rax, %r14
movq 0x18(%r15), %rdi
movq %rax, %rsi
movq 0x8(%rsp), %rdx
movq %r12, %rcx
callq 0xe3cb7
leaq 0x8(%r14), %rdi
movq %r13, %rsi
callq 0xe5f7c
cmpq $0x18, %r12
movq 0x10(%rsp), %r13
jl 0x411d6
movq %r14, %rdi
addq $0x14, %rdi
movq %rbx, %rsi
callq 0xe5f7c
movq %rbp, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0xe46ba
movq %r15, %rdi
movq %r14, %rsi
callq 0xded32
movq 0x18(%r15), %rdi
movq %r13, %rsi
movq %rbx, %rdx
movq %rbp, %rcx
callq 0xe45f4
movq %rbp, %rdi
callq 0xe5591
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq