// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq $0x0, -0xd8(%rbp)
cmpq $0xc, -0x20(%rbp)
jl 0x5c3af
cmpq $0x800, -0x20(%rbp)     # imm = 0x800
jle 0x5c3b4
jmp 0x5c589
cmpq $0x1, -0x30(%rbp)
jl 0x5c3cb
movq -0x20(%rbp), %rax
addq -0x30(%rbp), %rax
cmpq $0x5f5e100, %rax        # imm = 0x5F5E100
jle 0x5c3d0
jmp 0x5c589
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x18(%rbp), %rcx
movq -0x20(%rbp), %r8
leaq -0xd0(%rbp), %rdx
xorl %r9d, %r9d
callq 0x172d70
cmpl $0x0, %eax
jne 0x5c40f
movq -0x8(%rbp), %rdi
leaq 0x14b6f9(%rip), %rsi    # 0x1a7afc
movb $0x0, %al
callq 0x15bc10
jmp 0x5c589
movq -0xa8(%rbp), %rax
movq %rax, -0xe0(%rbp)
cmpq $0x0, -0xa0(%rbp)
jle 0x5c473
cmpq $0x0, -0x70(%rbp)
jle 0x5c473
movq -0x30(%rbp), %rax
cqto
idivq -0x70(%rbp)
movq %rax, -0xe8(%rbp)
cmpq $0x0, -0xe8(%rbp)
jle 0x5c471
movq -0xe8(%rbp), %rax
cmpq -0xa8(%rbp), %rax
jge 0x5c471
cmpq $0x10, -0xb8(%rbp)
jl 0x5c471
movq -0xe8(%rbp), %rax
movq %rax, -0xe0(%rbp)
jmp 0x5c473
movq -0x8(%rbp), %rdi
leaq 0x150889(%rip), %rsi    # 0x1acd07
xorl %eax, %eax
movl %eax, %edx
xorl %ecx, %ecx
callq 0x166c60
movq %rax, -0xd8(%rbp)
movq -0x8(%rbp), %rdi
movq -0xd8(%rbp), %rsi
movq -0x20(%rbp), %rcx
addq $0xe, %rcx
addq -0x30(%rbp), %rcx
leaq -0xd0(%rbp), %rdx
callq 0x1734e0
movq -0xe0(%rbp), %rax
cmpq -0xa8(%rbp), %rax
jne 0x5c4e4
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0xd8(%rbp), %rcx
callq 0x166070
jmp 0x5c56d
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x15b780
movq %rax, -0xf0(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0xf0(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
callq 0x164d10
movq -0xf0(%rbp), %rdi
addq $0x8, %rdi
movq -0xe0(%rbp), %rsi
callq 0x168bc0
cmpq $0x18, -0x20(%rbp)
jl 0x5c546
movq -0xf0(%rbp), %rdi
addq $0x14, %rdi
movq -0x30(%rbp), %rsi
callq 0x168bc0
movq -0xd8(%rbp), %rdi
movq -0xf0(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x166170
movq -0x8(%rbp), %rdi
movq -0xf0(%rbp), %rsi
callq 0x15b820
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0xd8(%rbp), %rcx
callq 0x166070
movq -0xd8(%rbp), %rdi
callq 0x1676a0
addq $0xf0, %rsp
popq %rbp
retq
nop