// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %r8, %rbp
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r12
leaq 0x30(%rsp), %r13
movq %rcx, (%r13)
movq 0x18(%rdx), %rdi
movq %r13, %rsi
callq 0xef475
movl %eax, 0x1c(%r15)
leaq 0xa6d65(%rip), %rsi     # 0x13ece3
leaq 0x9858b(%rip), %rdx     # 0x130510
xorl %r14d, %r14d
movq %r12, %rdi
movl %eax, %ecx
xorl %eax, %eax
callq 0xe9226
movq %r12, %rdi
movq %r15, %rsi
movq %rbx, %rdx
movq %r13, %rcx
xorl %r8d, %r8d
callq 0x98866
movq (%r13), %r13
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xef37e
leaq -0x7d(%rax), %rcx
cmpq $-0x55, %rcx
jb 0x983c8
leaq (%rax,%r13), %rcx
movq %rcx, 0x18(%rsp)
subq %rcx, %rbp
addq 0x30(%rsp), %rbp
movq %rbp, 0x10(%rsp)
movq 0x18(%rbx), %rsi
leaq 0x38(%rsp), %rdx
xorl %r14d, %r14d
movq %r12, %rdi
movq %r13, 0x28(%rsp)
movq %r13, %rcx
movq %rax, 0x20(%rsp)
movq %rax, %r8
xorl %r9d, %r9d
callq 0xf7881
testl %eax, %eax
je 0x983c8
cmpq $0x100, 0x90(%rsp) # imm = 0x100
jg 0x983c8
movq 0x68(%rsp), %r14
cmpq $0x1, %r14
jne 0x9804c
movq 0x58(%rsp), %rdi
imulq %r14, %rdi
movl $0x10, %esi
callq 0xea80b
leaq 0x7(%rax), %rbp
testq %rax, %rax
cmovnsq %rax, %rbp
sarq $0x3, %rbp
jmp 0x98054
movq 0x98(%rsp), %rbp
movq 0x10(%rsp), %rax
cmpl $0x0, 0x70(%rsp)
jne 0x98068
movq 0xa0(%rsp), %rax
cmpq $0x4, 0x68(%rsp)
jne 0x980d5
cmpb $0x0, 0x20(%r15)
jne 0x980d5
movq %rax, (%rsp)
leaq 0x38(%r15), %rdi
movl $0x10, %esi
xorl %edx, %edx
callq 0xf41f4
movb $0x1, 0x20(%r15)
cmpb $0x0, 0x4(%r15)
je 0x980aa
leaq 0x98479(%rip), %rsi     # 0x130517
movq %r12, %rdi
xorl %eax, %eax
callq 0xe9b27
jmp 0x980d1
leaq 0x983e2(%rip), %rsi     # 0x130493
movq %r12, %rdi
xorl %eax, %eax
callq 0xe9df7
leaq 0x985f6(%rip), %rsi     # 0x1306b8
movq %r12, %rdi
xorl %eax, %eax
callq 0xe9ef5
movb $0x1, 0x10(%r15)
movq (%rsp), %rax
cmpq $0x8, 0x68(%rsp)
jne 0x981ee
cmpb $0x0, 0x20(%r15)
jne 0x981ee
movq %rax, (%rsp)
movq %r14, 0x8(%rsp)
leaq 0x98443(%rip), %rsi     # 0x13053f
xorl %r14d, %r14d
movq %r12, %rdi
xorl %eax, %eax
callq 0xe9b27
leaq 0x38(%r15), %rax
leaq 0xd8(%r15), %rcx
leaq 0x9865b(%rip), %rdx     # 0x130776
movzbl (%r14,%rdx), %esi
shll $0x10, %esi
movq %rcx, %rdi
xorl %r8d, %r8d
movzbl (%r8,%rdx), %r9d
shll $0x8, %r9d
orl %esi, %r9d
xorl %r10d, %r10d
movzbl (%r10,%rdx), %r11d
addl %r9d, %r11d
addl $0xff000000, %r11d      # imm = 0xFF000000
movl %r11d, (%rdi,%r10,4)
incq %r10
cmpq $0x6, %r10
jne 0x98138
incq %r8
addq $0x18, %rdi
cmpq $0x6, %r8
jne 0x98129
incq %r14
addq $0x90, %rcx
cmpq $0x6, %r14
jne 0x9811b
xorl %ecx, %ecx
movl %ecx, %edx
orl $0xff000000, %edx       # imm = 0xFF000000
movl %edx, (%rax)
addq $0x181818, %rcx         # imm = 0x181818
addq $0x4, %rax
cmpq $0x1090908, %rcx        # imm = 0x1090908
jne 0x98173
xorl %eax, %eax
movl $0xff000000, %ecx       # imm = 0xFF000000
xorl %edx, %edx
movq 0x8(%rsp), %r14
xorl %esi, %esi
cmpq $0x5, %rdx
setae %sil
leal (%rax,%rsi,4), %edi
addl $0x10, %edi
leal (%rax,%rsi,4), %esi
addl $0xff000010, %esi       # imm = 0xFF000010
movl %esi, 0x64(%r15,%rdx,4)
movl %edi, %esi
shll $0x8, %esi
addl %ecx, %esi
movl %esi, 0x8c(%r15,%rdx,4)
shll $0x10, %edi
orl %ecx, %edi
movl %edi, 0xb4(%r15,%rdx,4)
incq %rdx
addl $0x18, %eax
cmpq $0xa, %rdx
jne 0x9819f
movb $0x1, 0x20(%r15)
movq (%rsp), %rax
cmpb $0x0, 0x10(%r15)
je 0x981fd
xorl %r14d, %r14d
jmp 0x983c8
movq %rbp, (%rsp)
leaq 0x88e4a(%rip), %rsi     # 0x121052
movq %r12, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
movq %rax, %r13
callq 0xf037a
movq %r14, %rbp
movq %rax, %r14
addq 0x20(%rsp), %r13
leaq 0x38(%rsp), %rdx
movq 0x58(%rdx), %rax
leaq 0xe(,%rax,4), %rcx
addq %r13, %rcx
movq %r12, %rdi
movq %r14, %rsi
callq 0xf7d41
movq 0x18(%rbx), %rdi
movl $0x14, %edx
movq 0x28(%rsp), %r12
movq %r12, %rsi
movq %r14, %rcx
callq 0xefabc
movq %rbp, 0x8(%rsp)
cmpq $0x1, %rbp
jne 0x98274
movl $0x4, %esi
movq %r14, %rdi
callq 0xf1346
jmp 0x9828a
movq 0x18(%rbx), %rdi
leaq 0x14(%r12), %rsi
movl $0x4, %edx
movq %r14, %rcx
callq 0xefabc
movq 0x18(%rbx), %rdi
addq $0x18, %r12
movq 0x20(%rsp), %rdx
addq $-0x18, %rdx
movq %r12, %rsi
movq %r14, %rcx
callq 0xefabc
movq 0x68(%rsp), %rax
cmpq $0x1, %rax
movq (%rsp), %rbp
jne 0x982fc
xorl %r12d, %r12d
movq 0x18(%rsp), %r13
movzbl 0x30(%r15,%r12,4), %esi
movq %r14, %rdi
callq 0xf11be
movzbl 0x31(%r15,%r12,4), %esi
movq %r14, %rdi
callq 0xf11be
movzbl 0x32(%r15,%r12,4), %esi
movq %r14, %rdi
callq 0xf11be
movq %r14, %rdi
xorl %esi, %esi
callq 0xf11be
incq %r12
cmpq $0x1, %r12
je 0x982bd
jmp 0x98368
cmpq $0x8, %rax
movq 0x18(%rsp), %r13
jg 0x98368
cmpq $0x100, 0x90(%rsp) # imm = 0x100
jg 0x983c8
cmpq $0x0, 0x90(%rsp)
jle 0x98368
xorl %r12d, %r12d
movzbl 0x38(%r15,%r12,4), %esi
movq %r14, %rdi
callq 0xf11be
movzbl 0x39(%r15,%r12,4), %esi
movq %r14, %rdi
callq 0xf11be
movzbl 0x3a(%r15,%r12,4), %esi
movq %r14, %rdi
callq 0xf11be
movq %r14, %rdi
xorl %esi, %esi
callq 0xf11be
incq %r12
cmpq 0x90(%rsp), %r12
jl 0x98327
cmpq $0x1, 0x8(%rsp)
jne 0x983b4
movq 0x60(%rsp), %rsi
testq %rsi, %rsi
jle 0x983c8
movq $-0x1, %r15
xorl %r12d, %r12d
movq 0x18(%rbx), %rdi
addq %r15, %rsi
imulq %rbp, %rsi
addq %r13, %rsi
movq 0x98(%rsp), %rdx
movq %r14, %rcx
callq 0xefabc
incq %r12
movq 0x60(%rsp), %rsi
decq %r15
cmpq %rsi, %r12
jl 0x98384
jmp 0x983c8
movq 0x18(%rbx), %rdi
movq %r13, %rsi
movq 0x10(%rsp), %rdx
movq %r14, %rcx
callq 0xefabc
movq %r14, %rdi
callq 0xf0b13
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq