// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/bmi.c
// Function: do_bitmap
static void do_bitmap(deark *c, lctx *d, i64 pos1)
{
	int saved_indent_level;
	i64 pos = pos1;
	i64 unc_data_size_reported;
	i64 unc_data_size_calc;
	i64 max_uncmpr_block_size;
	i64 rowspan;
	de_bitmap *img = NULL;
	dbuf *unc_pixels = NULL;
	struct imageinfo ii;
	const u32 *pal_to_use = d->globalimg.pal;

	de_zeromem(&ii, sizeof(struct imageinfo));

	de_dbg_indent_save(c, &saved_indent_level);

	de_dbg(c, "bitmap at %"I64_FMT, pos1);
	de_dbg_indent(c, 1);
	ii.w = de_getu16le_p(&pos);
	ii.h = de_getu16le_p(&pos);
	de_dbg_dimensions(c, ii.w, ii.h);

	ii.bpp = de_getu16le_p(&pos);
	de_dbg(c, "bits/pixel: %d", (int)ii.bpp);

	ii.palmode = (unsigned int)de_getu16le_p(&pos);
	de_dbg(c, "palette mode: %u", ii.palmode);

	if(ii.palmode) {
		pal_to_use = ii.pal;
	}

	if(ii.palmode && ii.bpp>=1 && ii.bpp<=8) {
		ii.num_pal_entries = de_pow2(ii.bpp);
	}

	pos += 2;

	unc_data_size_reported = de_getu32le_p(&pos);
	de_dbg(c, "uncmpr data size (reported): %"I64_FMT, unc_data_size_reported);

	rowspan = de_pad_to_n(ii.w*ii.bpp, 32)/8;
	unc_data_size_calc = rowspan * ii.h;
	de_dbg(c, "uncmpr data size (calculated): %"I64_FMT, unc_data_size_calc);

	if(unc_data_size_reported>DE_MAX_SANE_OBJECT_SIZE) goto done;

	max_uncmpr_block_size = de_getu16le_p(&pos);
	de_dbg(c, "max uncmpr block size: %d", (int)max_uncmpr_block_size);
	if(max_uncmpr_block_size > unc_data_size_calc) {
		max_uncmpr_block_size = unc_data_size_calc;
	}

	if(ii.num_pal_entries>0) {
		read_palette(c, d, &ii, pos);
		pos += 4*ii.num_pal_entries;
	}

	if(!de_good_image_dimensions(c, ii.w, ii.h)) goto done;
	if(ii.bpp!=24 && ii.bpp!=8 && ii.bpp!=4 && ii.bpp!=1) {
		de_err(c, "Unsupported image type");
		goto done;
	}

	unc_pixels = dbuf_create_membuf(c, unc_data_size_calc, 1);

	while(1) {
		i64 blen;

		if(unc_pixels->len >= unc_data_size_reported) break;
		if(pos >= c->infile->len) goto done;

		de_dbg(c, "block at %d", (int)pos);
		de_dbg_indent(c, 1);
		blen = de_getu16le_p(&pos);
		de_dbg(c, "block len: %d", (int)blen);
		pos++;
		if(pos+blen > c->infile->len) goto done;
		if(blen>max_uncmpr_block_size) goto done;

		if(unc_pixels->len < unc_data_size_calc) {
			i64 len_before = unc_pixels->len;

			if(!fmtutil_decompress_deflate(c->infile, pos, blen, unc_pixels,
				max_uncmpr_block_size, NULL,
				DE_DEFLATEFLAG_ISZLIB|DE_DEFLATEFLAG_USEMAXUNCMPRSIZE))
			{
				goto done;
			}

			de_dbg(c, "decompressed to: %"I64_FMT" (total=%"I64_FMT")",
				unc_pixels->len - len_before, unc_pixels->len);
		}
		de_dbg_indent(c, -1);

		pos += blen;
	}

	img = de_bitmap_create(c, ii.w, ii.h, 3);

	if(ii.bpp==24) {
		de_convert_image_rgb(unc_pixels, 0, rowspan, 3, img, DE_GETRGBFLAG_BGR);
	}
	else {
		de_convert_image_paletted(unc_pixels, 0, ii.bpp, rowspan, pal_to_use, img, 0);
	}

	de_bitmap_write_to_file(img, NULL, DE_CREATEFLAG_OPT_IMAGE);

done:
	de_bitmap_destroy(img);
	dbuf_close(unc_pixels);
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x4a0, %rsp            # imm = 0x4A0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x28(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x10(%rbp), %rax
addq $0x20, %rax
addq $0x28, %rax
movq %rax, -0x488(%rbp)
leaq -0x480(%rbp), %rdi
xorl %esi, %esi
movl $0x428, %edx            # imm = 0x428
callq 0x19140
movq -0x8(%rbp), %rdi
leaq -0x1c(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rdx
leaq 0x17f9b6(%rip), %rsi    # 0x1c1f07
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x28(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x480(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x28(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x478(%rbp)
movq -0x8(%rbp), %rdi
movq -0x480(%rbp), %rsi
movq -0x478(%rbp), %rdx
callq 0x15bba0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x28(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x468(%rbp)
movq -0x8(%rbp), %rdi
movq -0x468(%rbp), %rax
movl %eax, %edx
leaq 0x18f61b(%rip), %rsi    # 0x1d1bf4
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x28(%rbp), %rsi
callq 0x1652c0
movl %eax, -0x470(%rbp)
movq -0x8(%rbp), %rdi
movl -0x470(%rbp), %edx
leaq 0x160dcb(%rip), %rsi    # 0x1a33d3
movb $0x0, %al
callq 0x15b0d0
cmpl $0x0, -0x470(%rbp)
je 0x4262a
leaq -0x480(%rbp), %rax
addq $0x28, %rax
movq %rax, -0x488(%rbp)
cmpl $0x0, -0x470(%rbp)
je 0x4265a
cmpq $0x1, -0x468(%rbp)
jl 0x4265a
cmpq $0x8, -0x468(%rbp)
jg 0x4265a
movq -0x468(%rbp), %rdi
callq 0x15ce50
movq %rax, -0x460(%rbp)
movq -0x28(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x28(%rbp), %rsi
callq 0x165580
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rdx
leaq 0x160dd8(%rip), %rsi    # 0x1a3462
movb $0x0, %al
callq 0x15b0d0
movq -0x480(%rbp), %rdi
imulq -0x468(%rbp), %rdi
movl $0x20, %esi
callq 0x15ce90
movl $0x8, %ecx
cqto
idivq %rcx
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
imulq -0x478(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rdx
leaq 0x160dac(%rip), %rsi    # 0x1a3483
movb $0x0, %al
callq 0x15b0d0
cmpq $0x5f5e100, -0x30(%rbp) # imm = 0x5F5E100
jle 0x426ed
jmp 0x429be
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x28(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rax
movl %eax, %edx
leaq 0x160d93(%rip), %rsi    # 0x1a34a6
movb $0x0, %al
callq 0x15b0d0
movq -0x40(%rbp), %rax
cmpq -0x38(%rbp), %rax
jle 0x4272c
movq -0x38(%rbp), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x460(%rbp)
jle 0x42761
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x28(%rbp), %rcx
leaq -0x480(%rbp), %rdx
callq 0x42120
movq -0x460(%rbp), %rax
shlq $0x2, %rax
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x480(%rbp), %rsi
movq -0x478(%rbp), %rdx
callq 0x16a850
cmpl $0x0, %eax
jne 0x42782
jmp 0x429be
cmpq $0x18, -0x468(%rbp)
je 0x427c1
cmpq $0x8, -0x468(%rbp)
je 0x427c1
cmpq $0x4, -0x468(%rbp)
je 0x427c1
cmpq $0x1, -0x468(%rbp)
je 0x427c1
movq -0x8(%rbp), %rdi
leaq 0x160d0b(%rip), %rsi    # 0x1a34c0
movb $0x0, %al
callq 0x15bfb0
jmp 0x429be
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
movl $0x1, %edx
callq 0x168020
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq 0x18(%rax), %rax
cmpq -0x30(%rbp), %rax
jl 0x427ea
jmp 0x42934
movq -0x28(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jl 0x42801
jmp 0x429be
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movl %eax, %edx
leaq 0x18932b(%rip), %rsi    # 0x1cbb3d
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x28(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x490(%rbp)
movq -0x8(%rbp), %rdi
movq -0x490(%rbp), %rax
movl %eax, %edx
leaq 0x160c84(%rip), %rsi    # 0x1a34d7
movb $0x0, %al
callq 0x15b0d0
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
addq -0x490(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jle 0x42884
jmp 0x429be
movq -0x490(%rbp), %rax
cmpq -0x40(%rbp), %rax
jle 0x42896
jmp 0x429be
movq -0x58(%rbp), %rax
movq 0x18(%rax), %rax
cmpq -0x38(%rbp), %rax
jge 0x42912
movq -0x58(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x498(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rsi
movq -0x490(%rbp), %rdx
movq -0x58(%rbp), %rcx
movq -0x40(%rbp), %r8
xorl %eax, %eax
movl %eax, %r9d
movl $0x3, (%rsp)
callq 0x17f4f0
cmpl $0x0, %eax
jne 0x428e9
jmp 0x429be
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rax
movq 0x18(%rax), %rdx
subq -0x498(%rbp), %rdx
movq -0x58(%rbp), %rax
movq 0x18(%rax), %rcx
leaq 0x160bda(%rip), %rsi    # 0x1a34e5
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x490(%rbp), %rax
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
jmp 0x427d7
movq -0x8(%rbp), %rdi
movq -0x480(%rbp), %rsi
movq -0x478(%rbp), %rdx
movl $0x3, %ecx
callq 0x16b750
movq %rax, -0x50(%rbp)
cmpq $0x18, -0x468(%rbp)
jne 0x42980
movq -0x58(%rbp), %rdi
movq -0x48(%rbp), %rdx
movq -0x50(%rbp), %r8
xorl %eax, %eax
movl %eax, %esi
movl $0x3, %ecx
movl $0x1, %r9d
callq 0x16c8b0
jmp 0x429ac
movq -0x58(%rbp), %rdi
movq -0x468(%rbp), %rdx
movq -0x48(%rbp), %rcx
movq -0x488(%rbp), %r8
movq -0x50(%rbp), %r9
xorl %eax, %eax
movl %eax, %esi
xorl %eax, %eax
movl $0x0, (%rsp)
callq 0x16c520
movq -0x50(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x16afd0
movq -0x50(%rbp), %rdi
callq 0x16af60
movq -0x58(%rbp), %rdi
callq 0x1676a0
movq -0x8(%rbp), %rdi
movl -0x1c(%rbp), %esi
callq 0x15b5a0
addq $0x4a0, %rsp            # imm = 0x4A0
popq %rbp
retq
nopw %cs:(%rax,%rax)
nop