// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/src/deark-bitmap.c
// Function: de_bitmap16_write_to_file_finfo
void de_bitmap16_write_to_file_finfo(de_bitmap *img, de_bitmap *imglo,
	de_finfo *fi, unsigned int createflags)
{
	deark *c;
	struct image_scan_opt_data optctx;
	struct de_write_image_params wp;

	if(!img) return;
	c = img->c;
	if(img->invalid_image_flag) return;
	de_zeromem(&optctx, sizeof(struct image_scan_opt_data));

	if(!img->bitmap) de_bitmap_alloc_pixels(img);
	if(imglo) {
		if(!imglo->bitmap) de_bitmap_alloc_pixels(imglo);
		if(!valid_imglo(img, imglo)) return;
	}

	de_zeromem(&wp, sizeof(struct de_write_image_params));
	wp.createflags = createflags;

	if(imglo && (createflags & DE_CREATEFLAG_OPT_IMAGE)) {
		// If the high and low bytes are the same in every sample, we don't need
		// the low byte.
		if(!bitmap16_low_bits_important(img, imglo)) {
			imglo = NULL;
		}
	}

	if(imglo) {
		; // The routines below don't support 16-bit images
	}
	else if(createflags & DE_CREATEFLAG_IS_BWIMG) {
		// The BWIMG flag/optimization has to be handled in a different way than the
		// other optimizations, because our de_bitmap object does not support a
		// 1 bit/pixel image type.
		wp.flags2 |= 0x1;
	}
	else if(createflags & DE_CREATEFLAG_OPT_IMAGE) {
		// This should probably be the default, but our optimization routine
		// isn't very efficient, and wouldn't change anything in most cases.
		get_optimized_image(img, &optctx);
		if(optctx.optimg) {
			de_dbg3(c, "reducing image depth (%d->%d)", img->bytes_per_pixel,
				optctx.optimg->bytes_per_pixel);
		}
		if(!optctx.is_nonbilevel) {
			de_dbg3(c, "reducing to bilevel (from %d samples)", img->bytes_per_pixel);
			wp.flags2 |= 0x1;
		}
	}

	if(fi && fi->linear_colorpace) {
		wp.flags2 |= 0x2;
	}

	wp.f = dbuf_create_output_file(c, "png", fi, createflags);
	if(optctx.optimg) {
		wp.img = optctx.optimg;
	}
	else {
		wp.img = img;
		wp.imglo = imglo;
	}
	de_write_png(c, &wp);
	dbuf_close(wp.f);

	if(optctx.optimg) de_bitmap_destroy(optctx.optimg);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rdx, 0x20(%rsp)
testq %rdi, %rdi
je 0xe6d6c
movq %rdi, %rbx
cmpl $0x0, 0x20(%rdi)
je 0xe6d7b
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %ecx, %ebp
movq %rsi, %r12
movq (%rbx), %r13
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
cmpq $0x0, 0x28(%rbx)
jne 0xe6d9a
movq %rbx, %rdi
callq 0xe7025
pushq $0x2
popq %r14
testq %r12, %r12
je 0xe6e01
cmpq $0x0, 0x28(%r12)
jne 0xe6db3
movq %r12, %rdi
callq 0xe7025
cmpl $0x0, 0x20(%r12)
jne 0xe6d6c
movl 0x24(%rbx), %eax
cmpl 0x24(%r12), %eax
jne 0xe6d6c
movq 0x8(%rbx), %rcx
cmpq 0x8(%r12), %rcx
jne 0xe6d6c
movq 0x10(%rbx), %r15
cmpq 0x10(%r12), %r15
jne 0xe6d6c
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movl %ebp, 0x68(%rsp)
testb $0x2, %bpl
jne 0xe6f90
movq $0x0, 0x18(%rsp)
jmp 0xe6e45
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movl %ebp, 0x68(%rsp)
testb $0x8, %bpl
jne 0xe6e2d
testb $0x2, %bpl
jne 0xe6ed0
movq $0x0, 0x18(%rsp)
jmp 0xe6e42
movq $0x0, 0x18(%rsp)
movl $0x1, 0x6c(%rsp)
pushq $0x3
popq %r14
xorl %r12d, %r12d
cmpq $0x0, 0x20(%rsp)
je 0xe6e5d
movq 0x20(%rsp), %rax
cmpb $0x0, 0xf(%rax)
je 0xe6e5d
movl %r14d, 0x6c(%rsp)
leaq 0x3468c(%rip), %rsi     # 0x11b4f0
movq %r13, %rdi
movq 0x20(%rsp), %rdx
movl %ebp, %ecx
callq 0xe4e62
movq %rax, 0x60(%rsp)
movq 0x18(%rsp), %r14
testq %r14, %r14
je 0xe6eab
leaq 0x50(%rsp), %rbx
movq %r14, (%rbx)
movq %r13, %rdi
movq %rbx, %rsi
callq 0x101f9c
movq 0x10(%rbx), %rdi
callq 0xe5591
movq %r14, %rdi
callq 0xe7084
jmp 0xe6d6c
leaq 0x50(%rsp), %r14
movq %r12, 0x8(%r14)
movq %rbx, (%r14)
movq %r13, %rdi
movq %r14, %rsi
callq 0x101f9c
movq 0x10(%r14), %rdi
callq 0xe5591
jmp 0xe6d6c
leaq 0x30(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe8580
movb 0x3(%r14), %r14b
testb %r14b, %r14b
je 0xe6f3e
xorl %eax, %eax
cmpb $0x0, 0x30(%rsp)
setne %al
xorl %ecx, %ecx
cmpb $0x0, 0x31(%rsp)
setne %cl
leal (%rcx,%rax,2), %r12d
incl %r12d
cmpl 0x24(%rbx), %r12d
jge 0xe6f3e
movq %rbx, %rdi
callq 0xe88c1
movq %rax, 0x38(%rsp)
movl %r12d, 0x24(%rax)
movq 0x8(%rbx), %r8
movq 0x10(%rbx), %r9
xorps %xmm0, %xmm0
movups %xmm0, (%rsp)
andl $0x0, 0x10(%rsp)
movq %rbx, %rdi
movq %rax, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xe8314
movq 0x38(%rsp), %r12
testq %r12, %r12
je 0xe6f61
movl 0x24(%rbx), %edx
movl 0x24(%r12), %ecx
leaq 0x551d0(%rip), %rsi     # 0x13c127
movq %r13, %rdi
xorl %eax, %eax
callq 0xdea83
movq %r12, 0x18(%rsp)
testb %r14b, %r14b
je 0xe6f77
xorl %r12d, %r12d
pushq $0x2
popq %r14
jmp 0xe6e45
movl 0x24(%rbx), %edx
leaq 0x551c4(%rip), %rsi     # 0x13c145
movq %r13, %rdi
xorl %eax, %eax
callq 0xdea83
jmp 0xe6e36
movq %rcx, %rax
xorl %ecx, %ecx
testq %r15, %r15
cmovleq %rcx, %r15
movq %rax, %rdx
testq %rax, %rax
movl $0x0, %eax
movq %rax, 0x18(%rsp)
cmovleq %rcx, %rdx
movq %rdx, 0x70(%rsp)
xorl %eax, %eax
movq %r13, 0x40(%rsp)
movl %ebp, 0x2c(%rsp)
cmpq %r15, %rax
je 0xe6e12
movq %rax, 0x48(%rsp)
xorl %r13d, %r13d
cmpq %r13, 0x70(%rsp)
je 0xe700c
movq %rbx, %rdi
movq %r13, %rsi
movq 0x48(%rsp), %r14
movq %r14, %rdx
callq 0xe735f
movl %eax, %ebp
movq %r12, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0xe735f
incq %r13
cmpl %eax, %ebp
pushq $0x2
popq %r14
movl 0x2c(%rsp), %ebp
je 0xe6fd1
jmp 0xe701b
movq 0x48(%rsp), %rax
incq %rax
movq 0x40(%rsp), %r13
jmp 0xe6fc0
movq 0x40(%rsp), %r13
jmp 0xe6e45