// File: /workspace/github_c_makefile_star2_O3/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 0xeee2f
movl %eax, 0x1c(%r15)
leaq 0xa7a51(%rip), %rsi     # 0x13f0e3
leaq 0x991b2(%rip), %rdx     # 0x13084b
xorl %r14d, %r14d
movq %r12, %rdi
movl %eax, %ecx
xorl %eax, %eax
callq 0xe8617
movq %r12, %rdi
movq %r15, %rsi
movq %rbx, %rdx
movq %r13, %rcx
xorl %r8d, %r8d
callq 0x97fd7
movq (%r13), %r13
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xeed61
leaq -0x7d(%rax), %rcx
cmpq $-0x55, %rcx
jb 0x97b36
leaq (%rax,%r13), %rcx
movq %rcx, 0x10(%rsp)
subq %rcx, %rbp
addq 0x30(%rsp), %rbp
movq %rbp, 0x8(%rsp)
movq 0x18(%rbx), %rsi
leaq 0x38(%rsp), %rdx
xorl %r14d, %r14d
movq %r12, %rdi
movq %r13, 0x28(%rsp)
movq %r13, %rcx
movq %rax, 0x18(%rsp)
movq %rax, %r8
xorl %r9d, %r9d
callq 0xf7425
testl %eax, %eax
je 0x97b36
cmpq $0x100, 0x90(%rsp) # imm = 0x100
jg 0x97b36
movq 0x68(%rsp), %r14
cmpq $0x1, %r14
jne 0x97761
movq 0x58(%rsp), %rdi
movl $0x10, %esi
callq 0xe9cb4
leaq 0x7(%rax), %rbp
testq %rax, %rax
cmovnsq %rax, %rbp
sarq $0x3, %rbp
movq 0x68(%rsp), %rax
jmp 0x9776c
movq 0x98(%rsp), %rbp
movq %r14, %rax
movq 0x8(%rsp), %r13
cmpl $0x0, 0x70(%rsp)
jne 0x97780
movq 0xa0(%rsp), %r13
cmpq $0x4, %rax
jne 0x977e8
cmpb $0x0, 0x20(%r15)
jne 0x977e3
leaq 0x38(%r15), %rdi
movl $0x10, %esi
xorl %edx, %edx
callq 0xf3bdb
movb $0x1, 0x20(%r15)
cmpb $0x0, 0x4(%r15)
je 0x977bc
leaq 0x990a2(%rip), %rsi     # 0x130852
movq %r12, %rdi
xorl %eax, %eax
callq 0xe8faa
jmp 0x977e3
leaq 0x9900b(%rip), %rsi     # 0x1307ce
movq %r12, %rdi
xorl %eax, %eax
callq 0xe9295
leaq 0x9921f(%rip), %rsi     # 0x1309f3
movq %r12, %rdi
xorl %eax, %eax
callq 0xe9393
movb $0x1, 0x10(%r15)
movq 0x68(%rsp), %rax
cmpq $0x8, %rax
jne 0x9795b
cmpb $0x0, 0x20(%r15)
jne 0x9795b
movq %r14, (%rsp)
leaq 0x99072(%rip), %rsi     # 0x13087a
xorl %r14d, %r14d
movq %r12, %rdi
xorl %eax, %eax
callq 0xe8faa
leaq 0xd8(%r15), %rax
leaq 0x9928e(%rip), %rcx     # 0x130ab1
movzbl (%r14,%rcx), %edx
shll $0x10, %edx
movq %rax, %rsi
xorl %edi, %edi
movzbl (%rdi,%rcx), %r8d
shll $0x8, %r8d
orl %edx, %r8d
xorl %r9d, %r9d
movzbl (%r9,%rcx), %r10d
addl %r8d, %r10d
addl $0xff000000, %r10d      # imm = 0xFF000000
movl %r10d, (%rsi,%r9,4)
incq %r9
cmpq $0x6, %r9
jne 0x9783f
incq %rdi
addq $0x18, %rsi
cmpq $0x6, %rdi
jne 0x97830
incq %r14
addq $0x90, %rax
cmpq $0x6, %r14
jne 0x97823
leaq 0x3c(%r15), %rax
movdqa 0x98e4d(%rip), %xmm0    # 0x1306d0
xorl %ecx, %ecx
movdqa 0x98e53(%rip), %xmm1    # 0x1306e0
movdqa 0x98e5b(%rip), %xmm2    # 0x1306f0
pshufd $0xf5, %xmm2, %xmm3     # xmm3 = xmm2[1,1,3,3]
movdqa 0x98e5e(%rip), %xmm4    # 0x130700
movq (%rsp), %r14
movdqa %xmm0, %xmm5
pxor %xmm1, %xmm5
movdqa %xmm2, %xmm6
pcmpgtd %xmm5, %xmm6
pshufd $0xf5, %xmm5, %xmm5     # xmm5 = xmm5[1,1,3,3]
pcmpeqd %xmm3, %xmm5
pand %xmm6, %xmm5
movd %xmm5, %edx
testb $0x1, %dl
je 0x978d7
movl %ecx, %edx
orl $0xff000000, %edx       # imm = 0xFF000000
movl %edx, -0x4(%rax)
pextrw $0x4, %xmm5, %edx
testb $0x1, %dl
je 0x978ef
leal 0x181818(%rcx), %edx
orl $0xff000000, %edx       # imm = 0xFF000000
movl %edx, (%rax)
paddq %xmm4, %xmm0
addq $0x8, %rax
addq $0x303030, %rcx         # imm = 0x303030
cmpq $0x1212120, %rcx        # imm = 0x1212120
jne 0x978a6
xorl %eax, %eax
movl $0xff000000, %ecx       # imm = 0xFF000000
xorl %edx, %edx
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 0x97910
movb $0x1, 0x20(%r15)
cmpb $0x0, 0x10(%r15)
je 0x9796a
xorl %r14d, %r14d
jmp 0x97b36
movq %rbp, 0x20(%rsp)
leaq 0x897ec(%rip), %rsi     # 0x121162
movq %r12, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xefda1
movq %r14, %rbp
movq %rax, %r14
addq 0x18(%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 0xf78fa
movq 0x18(%rbx), %rdi
movl $0x14, %edx
movq 0x28(%rsp), %r12
movq %r12, %rsi
movq %r14, %rcx
callq 0xef481
movq %rbp, (%rsp)
cmpq $0x1, %rbp
jne 0x979de
movl $0x4, %esi
movq %r14, %rdi
callq 0xf0d35
jmp 0x979f4
movq 0x18(%rbx), %rdi
leaq 0x14(%r12), %rsi
movl $0x4, %edx
movq %r14, %rcx
callq 0xef481
movq 0x18(%rbx), %rdi
addq $0x18, %r12
movq 0x18(%rsp), %rdx
addq $-0x18, %rdx
movq %r12, %rsi
movq %r14, %rcx
callq 0xef481
movq 0x68(%rsp), %rax
cmpq $0x1, %rax
jne 0x97a68
movb $0x1, %bpl
xorl %r12d, %r12d
movzbl 0x30(%r15,%r12,4), %esi
movq %r14, %rdi
callq 0xf0bd6
movzbl 0x31(%r15,%r12,4), %esi
movq %r14, %rdi
callq 0xf0bd6
movzbl 0x32(%r15,%r12,4), %esi
movq %r14, %rdi
callq 0xf0bd6
movq %r14, %rdi
xorl %esi, %esi
callq 0xf0bd6
movl $0x1, %r12d
testb $0x1, %bpl
movl $0x0, %ebp
jne 0x97a21
jmp 0x97acb
cmpq $0x8, %rax
jg 0x97acb
movq 0x90(%rsp), %rax
cmpq $0x100, %rax            # imm = 0x100
jg 0x97b36
testq %rax, %rax
jle 0x97acb
xorl %r12d, %r12d
movzbl 0x38(%r15,%r12,4), %esi
movq %r14, %rdi
callq 0xf0bd6
movzbl 0x39(%r15,%r12,4), %esi
movq %r14, %rdi
callq 0xf0bd6
movzbl 0x3a(%r15,%r12,4), %esi
movq %r14, %rdi
callq 0xf0bd6
movq %r14, %rdi
xorl %esi, %esi
callq 0xf0bd6
incq %r12
cmpq 0x90(%rsp), %r12
jl 0x97a8a
cmpq $0x1, (%rsp)
jne 0x97b20
movq 0x60(%rsp), %rsi
testq %rsi, %rsi
movq 0x10(%rsp), %r13
jle 0x97b36
movq $-0x1, %r15
xorl %r12d, %r12d
movq 0x20(%rsp), %rbp
movq 0x18(%rbx), %rdi
addq %r15, %rsi
imulq %rbp, %rsi
addq %r13, %rsi
movq 0x98(%rsp), %rdx
movq %r14, %rcx
callq 0xef481
incq %r12
movq 0x60(%rsp), %rsi
decq %r15
cmpq %rsi, %r12
jl 0x97af0
jmp 0x97b36
movq 0x18(%rbx), %rdi
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %rdx
movq %r14, %rcx
callq 0xef481
movq %r14, %rdi
callq 0xf052b
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq