// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/os2bmp.c
// Function: do_apply_os2bmp_mask
static void do_apply_os2bmp_mask(deark *c, de_bitmap *fg, de_bitmap *mask, int is_color)
{
	i64 i, j;
	i64 mask_adj_height;
	int inverse_used = 0;

	mask_adj_height = mask->height / 2;

	for(j=0; j<fg->height && j<mask_adj_height; j++) {
		for(i=0; i<fg->width && i<mask->width; i++) {
			u8 andmaskclr;
			u8 xormaskclr;
			u32 oldclr;
			u32 newclr;

			oldclr = de_bitmap_getpixel(fg, i, j);
			newclr = oldclr;
			xormaskclr = DE_COLOR_K(de_bitmap_getpixel(mask, i, j));
			andmaskclr =  DE_COLOR_K(de_bitmap_getpixel(mask, i, mask_adj_height+j));

			if(andmaskclr==0) {
				if(is_color) {
					// For color bitmaps, the XOR bit is not used when the AND bit is 0.
					// Always use foreground color.
					;
				}
				else {
					if(xormaskclr==0) {
						newclr = DE_STOCKCOLOR_BLACK;
					}
					else {
						newclr = DE_STOCKCOLOR_WHITE;
					}
				}
			}
			else {
				if(xormaskclr==0) { // transparent
					newclr = DE_SET_ALPHA(oldclr, 0);
				}
				else { // inverse background
					newclr = get_inv_bkgd_replacement_clr(i, j);
					inverse_used = 1;
				}
			}

			if(newclr!=oldclr) {
				de_bitmap_setpixel_rgb(fg, i, j, newclr);
			}
		}
	}

	if(inverse_used) {
		de_warn(c, "This image contains inverse background pixels, which are not fully supported.");
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl $0x0, -0x3c(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl $0x2, %ecx
cqto
idivq %rcx
movq %rax, -0x38(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x30(%rbp), %rcx
movq -0x10(%rbp), %rdx
xorl %eax, %eax
cmpq 0x10(%rdx), %rcx
movb %al, -0x49(%rbp)
jge 0xe6acd
movq -0x30(%rbp), %rax
cmpq -0x38(%rbp), %rax
setl %al
movb %al, -0x49(%rbp)
movb -0x49(%rbp), %al
testb $0x1, %al
jne 0xe6ad9
jmp 0xe6c0a
movq $0x0, -0x28(%rbp)
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rdx
xorl %eax, %eax
cmpq 0x8(%rdx), %rcx
movb %al, -0x4a(%rbp)
jge 0xe6b06
movq -0x28(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x8(%rcx), %rax
setl %al
movb %al, -0x4a(%rbp)
movb -0x4a(%rbp), %al
testb $0x1, %al
jne 0xe6b12
jmp 0xe6bf7
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x16b540
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %eax
movl %eax, -0x48(%rbp)
movq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x16b540
shrl $0x10, %eax
andl $0xff, %eax
movb %al, -0x3e(%rbp)
movq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x38(%rbp), %rdx
addq -0x30(%rbp), %rdx
callq 0x16b540
shrl $0x10, %eax
andl $0xff, %eax
movb %al, -0x3d(%rbp)
movzbl -0x3d(%rbp), %eax
cmpl $0x0, %eax
jne 0xe6b96
cmpl $0x0, -0x1c(%rbp)
je 0xe6b79
jmp 0xe6b94
movzbl -0x3e(%rbp), %eax
cmpl $0x0, %eax
jne 0xe6b8b
movl $0xff000000, -0x48(%rbp) # imm = 0xFF000000
jmp 0xe6b92
movl $0xffffffff, -0x48(%rbp) # imm = 0xFFFFFFFF
jmp 0xe6b94
jmp 0xe6bc8
movzbl -0x3e(%rbp), %eax
cmpl $0x0, %eax
jne 0xe6baf
movl -0x44(%rbp), %eax
andl $0xffffff, %eax         # imm = 0xFFFFFF
orl $0x0, %eax
movl %eax, -0x48(%rbp)
jmp 0xe6bc6
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0xe6e50
movl %eax, -0x48(%rbp)
movl $0x1, -0x3c(%rbp)
jmp 0xe6bc8
movl -0x48(%rbp), %eax
cmpl -0x44(%rbp), %eax
je 0xe6be4
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
movl -0x48(%rbp), %ecx
callq 0x16b310
jmp 0xe6be6
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0xe6ae1
jmp 0xe6bf9
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0xe6aac
cmpl $0x0, -0x3c(%rbp)
je 0xe6c22
movq -0x8(%rbp), %rdi
leaq 0xcd2c1(%rip), %rsi     # 0x1b3edc
movb $0x0, %al
callq 0x15bc10
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax,%rax)