// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/mmm.c
// Function: do_mmm_dib
static void do_mmm_dib(deark *c, struct mmm_ctx *d, struct de_iffctx *ictx, i64 pos1, i64 len)
{
	i64 pos;
	i64 i_infohdr_pos;
	i64 i_bits_pos;
	i64 infosize;
	i64 i_bits_size;
	i64 o_bits_size;
	i64 i_rowspan;
	i64 k;
	i64 j;
	u8 special_1bpp = 0;
	int ret;
	dbuf *outf = NULL;
	struct de_bmpinfo bi;

	pos = pos1;
	mmm_read_rsrcid_p(c, d, ictx, &pos, "dib id");
	mmm_read_name_p(c, d, ictx, &pos, NULL);
	i_infohdr_pos = pos;

	infosize = dbuf_getu32le(ictx->f, i_infohdr_pos);
	if(infosize<40 || infosize>124) goto done;
	i_bits_pos = i_infohdr_pos + infosize;
	i_bits_size = pos+len-i_bits_pos;

	ret = fmtutil_get_bmpinfo(c, ictx->f, &bi, i_infohdr_pos, infosize, 0);
	if(!ret) goto done;
	if(bi.num_colors > 256) goto done;

	if(bi.bitcount==1) {
		special_1bpp = 1;
	}

	if(special_1bpp) {
		i_rowspan = de_pad_to_n(bi.bitcount*bi.width, 16) / 8;
	}
	else {
		i_rowspan = bi.rowspan;
	}

	if(bi.compression_field==0) {
		o_bits_size = bi.foreground_size;
	}
	else {
		o_bits_size = i_bits_size;
	}

	// color table
	if(bi.bitcount==4 && !d->have_pal) {
		mmm_default_pal16(c, d);
	}
	if(bi.bitcount==8 && !d->have_pal) {
		mmm_default_pal256(c, d);
	}
	if(d->errflag) goto done;

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

	// file header
	fmtutil_generate_bmpfileheader(c, outf, &bi,
		14 + infosize + bi.num_colors*4 + o_bits_size);

	// info header
	dbuf_copy(ictx->f, i_infohdr_pos, 20, outf);
	if(special_1bpp) {
		// She biSizeImage field may be wrong, so zero it out.
		dbuf_write_zeroes(outf, 4);
	}
	else {
		dbuf_copy(ictx->f, i_infohdr_pos+20, 4, outf);
	}
	dbuf_copy(ictx->f, i_infohdr_pos+24, infosize-24, outf);

	// color table

	if(bi.bitcount==1) {
		for(k=0; k<2; k++) {
			dbuf_writebyte(outf, DE_COLOR_B(d->pal1[k]));
			dbuf_writebyte(outf, DE_COLOR_G(d->pal1[k]));
			dbuf_writebyte(outf, DE_COLOR_R(d->pal1[k]));
			dbuf_writebyte(outf, 0);
		}
	}
	else if(bi.bitcount<=8) {
		if(bi.num_colors>256) goto done;
		for(k=0; k<bi.num_colors; k++) {
			dbuf_writebyte(outf, DE_COLOR_B(d->pal[k]));
			dbuf_writebyte(outf, DE_COLOR_G(d->pal[k]));
			dbuf_writebyte(outf, DE_COLOR_R(d->pal[k]));
			dbuf_writebyte(outf, 0);
		}
	}

	// bits
	if(special_1bpp) {
		for(j=0; j<bi.height; j++) {
			dbuf_copy(ictx->f, i_bits_pos+(bi.height-1-j)*i_rowspan, bi.rowspan, outf);
		}
	}
	else {
		dbuf_copy(ictx->f, i_bits_pos, i_bits_size, outf);
	}

done:
	dbuf_close(outf);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movb $0x0, -0x71(%rbp)
movq $0x0, -0x80(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
leaq -0x30(%rbp), %rcx
leaq 0xdd6b1(%rip), %r8      # 0x1bc2fb
callq 0xdf600
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
leaq -0x30(%rbp), %rcx
xorl %eax, %eax
movl %eax, %r8d
callq 0xdf680
movq -0x30(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
callq 0x165540
movq %rax, -0x48(%rbp)
cmpq $0x28, -0x48(%rbp)
jl 0xdec94
cmpq $0x7c, -0x48(%rbp)
jle 0xdec99
jmp 0xdf029
movq -0x38(%rbp), %rax
addq -0x48(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x30(%rbp), %rax
addq -0x28(%rbp), %rax
subq -0x40(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x38(%rbp), %rcx
movq -0x48(%rbp), %r8
leaq -0x120(%rbp), %rdx
xorl %r9d, %r9d
callq 0x172d70
movl %eax, -0x78(%rbp)
cmpl $0x0, -0x78(%rbp)
jne 0xdece6
jmp 0xdf029
cmpq $0x100, -0xc8(%rbp) # imm = 0x100
jle 0xdecf8
jmp 0xdf029
cmpq $0x1, -0xf0(%rbp)
jne 0xded06
movb $0x1, -0x71(%rbp)
cmpb $0x0, -0x71(%rbp)
je 0xded35
movq -0xf0(%rbp), %rdi
imulq -0x100(%rbp), %rdi
movl $0x10, %esi
callq 0x15ce90
movl $0x8, %ecx
cqto
idivq %rcx
movq %rax, -0x60(%rbp)
jmp 0xded40
movq -0xc0(%rbp), %rax
movq %rax, -0x60(%rbp)
cmpl $0x0, -0xe8(%rbp)
jne 0xded56
movq -0xb8(%rbp), %rax
movq %rax, -0x58(%rbp)
jmp 0xded5e
movq -0x50(%rbp), %rax
movq %rax, -0x58(%rbp)
cmpq $0x4, -0xf0(%rbp)
jne 0xded7f
movq -0x10(%rbp), %rax
cmpb $0x0, 0x20(%rax)
jne 0xded7f
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0xdf780
cmpq $0x8, -0xf0(%rbp)
jne 0xdeda0
movq -0x10(%rbp), %rax
cmpb $0x0, 0x20(%rax)
jne 0xdeda0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0xdf800
movq -0x10(%rbp), %rax
cmpb $0x0, 0x10(%rax)
je 0xdedaf
jmp 0xdf029
movq -0x8(%rbp), %rdi
leaq 0xcdf4d(%rip), %rsi     # 0x1acd07
xorl %eax, %eax
movl %eax, %edx
xorl %ecx, %ecx
callq 0x166c60
movq %rax, -0x80(%rbp)
movq -0x8(%rbp), %rdi
movq -0x80(%rbp), %rsi
movq -0x48(%rbp), %rcx
addq $0xe, %rcx
movq -0xc8(%rbp), %rax
shlq $0x2, %rax
addq %rax, %rcx
addq -0x58(%rbp), %rcx
leaq -0x120(%rbp), %rdx
callq 0x1734e0
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
movq -0x80(%rbp), %rcx
movl $0x14, %edx
callq 0x166070
cmpb $0x0, -0x71(%rbp)
je 0xdee27
movq -0x80(%rbp), %rdi
movl $0x4, %esi
callq 0x1685f0
jmp 0xdee45
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
addq $0x14, %rsi
movq -0x80(%rbp), %rcx
movl $0x4, %edx
callq 0x166070
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
addq $0x18, %rsi
movq -0x48(%rbp), %rdx
subq $0x18, %rdx
movq -0x80(%rbp), %rcx
callq 0x166070
cmpq $0x1, -0xf0(%rbp)
jne 0xdeefe
movq $0x0, -0x68(%rbp)
cmpq $0x2, -0x68(%rbp)
jge 0xdeef9
movq -0x80(%rbp), %rdi
movq -0x10(%rbp), %rax
movq -0x68(%rbp), %rcx
movl 0x30(%rax,%rcx,4), %eax
andl $0xff, %eax
movzbl %al, %esi
callq 0x168380
movq -0x80(%rbp), %rdi
movq -0x10(%rbp), %rax
movq -0x68(%rbp), %rcx
movl 0x30(%rax,%rcx,4), %eax
shrl $0x8, %eax
andl $0xff, %eax
movzbl %al, %esi
callq 0x168380
movq -0x80(%rbp), %rdi
movq -0x10(%rbp), %rax
movq -0x68(%rbp), %rcx
movl 0x30(%rax,%rcx,4), %eax
shrl $0x10, %eax
andl $0xff, %eax
movzbl %al, %esi
callq 0x168380
movq -0x80(%rbp), %rdi
xorl %esi, %esi
callq 0x168380
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0xdee7c
jmp 0xdefb0
cmpq $0x8, -0xf0(%rbp)
jg 0xdefae
cmpq $0x100, -0xc8(%rbp) # imm = 0x100
jle 0xdef1e
jmp 0xdf029
movq $0x0, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq -0xc8(%rbp), %rax
jge 0xdefac
movq -0x80(%rbp), %rdi
movq -0x10(%rbp), %rax
movq -0x68(%rbp), %rcx
movl 0x38(%rax,%rcx,4), %eax
andl $0xff, %eax
movzbl %al, %esi
callq 0x168380
movq -0x80(%rbp), %rdi
movq -0x10(%rbp), %rax
movq -0x68(%rbp), %rcx
movl 0x38(%rax,%rcx,4), %eax
shrl $0x8, %eax
andl $0xff, %eax
movzbl %al, %esi
callq 0x168380
movq -0x80(%rbp), %rdi
movq -0x10(%rbp), %rax
movq -0x68(%rbp), %rcx
movl 0x38(%rax,%rcx,4), %eax
shrl $0x10, %eax
andl $0xff, %eax
movzbl %al, %esi
callq 0x168380
movq -0x80(%rbp), %rdi
xorl %esi, %esi
callq 0x168380
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0xdef26
jmp 0xdefae
jmp 0xdefb0
cmpb $0x0, -0x71(%rbp)
je 0xdf00e
movq $0x0, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpq -0xf8(%rbp), %rax
jge 0xdf00c
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x40(%rbp), %rsi
movq -0xf8(%rbp), %rax
subq $0x1, %rax
subq -0x70(%rbp), %rax
imulq -0x60(%rbp), %rax
addq %rax, %rsi
movq -0xc0(%rbp), %rdx
movq -0x80(%rbp), %rcx
callq 0x166070
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0xdefbe
jmp 0xdf027
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x40(%rbp), %rsi
movq -0x50(%rbp), %rdx
movq -0x80(%rbp), %rcx
callq 0x166070
jmp 0xdf029
movq -0x80(%rbp), %rdi
callq 0x1676a0
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
nopl (%rax,%rax)