// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/awbm.c
// Function: do_v2
static void do_v2(deark *c, lctx *d)
{
	de_bitmap *img = NULL;
	i64 rowspan1;
	i64 rowspan;
	i64 bitmap_start;
	i64 bitmap_size;
	i64 palette_start;
	const char *s;
	i64 ncolors = 0; // 16 or 256

	d->w = de_getu16le(4);
	d->h = de_getu16le(6);
	de_dbg_dimensions(c, d->w, d->h);
	if(!de_good_image_dimensions(c, d->w, d->h)) goto done;

	bitmap_start = 8;
	rowspan1 = (d->w+7)/8;

	rowspan = d->w; // Start by guessing 8bpp
	bitmap_size = rowspan * d->h;

	if(detect_palette_at(c, d, bitmap_start+bitmap_size, 256)) {
		ncolors=256;
		de_declare_fmt(c, "Award BIOS logo v2 8-bit");
	}
	else {
		// Doesn't seem to be an 8pp image. Try 4bpp.
		rowspan = rowspan1 * 4;
		bitmap_size = rowspan * d->h;
		if(detect_palette_at(c, d, bitmap_start+bitmap_size, 16)) {
			ncolors=16;
			de_declare_fmt(c, "Award BIOS logo v2 4-bit");
		}
	}

	if(!ncolors) {
		de_err(c, "Can't detect image format");
		goto done;
	}

	// Default to BGR for 16-color images, RGB for 256-color.
	if(ncolors==16)
		d->rgb_order = 0;
	else
		d->rgb_order = 1;

	s = de_get_ext_option(c, "awbm:rgb");
	if(s) d->rgb_order = de_atoi(s);

	palette_start = bitmap_start+bitmap_size+4;

	de_read_simple_palette(c, c->infile, palette_start, ncolors, 3, d->pal, ncolors,
		DE_RDPALTYPE_VGA18BIT, ((d->rgb_order)?0:DE_RDPALFLAG_BGR));

	img = de_bitmap_create(c, d->w, d->h, 3);
	if(ncolors==16) {
		de_convert_image_paletted_planar(c->infile, bitmap_start, 4, rowspan,
			rowspan1, d->pal, img, 0x2);
	}
	else {
		de_convert_image_paletted(c->infile, bitmap_start, 8, rowspan, d->pal,
			img, 0);
	}

	de_bitmap_write_to_file(img, NULL, 0);
done:
	de_bitmap_destroy(img);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x4, %esi
callq 0x165280
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x6, %esi
callq 0x165280
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
callq 0x15bba0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
callq 0x16a850
cmpl $0x0, %eax
jne 0x2c453
jmp 0x2c68a
movq $0x8, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
addq $0x7, %rax
movl $0x8, %ecx
cqto
idivq %rcx
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
imulq 0x8(%rcx), %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x30(%rbp), %rdx
addq -0x38(%rbp), %rdx
movl $0x100, %ecx            # imm = 0x100
callq 0x2c790
cmpl $0x0, %eax
je 0x2c4c9
movq $0x100, -0x50(%rbp)     # imm = 0x100
movq -0x8(%rbp), %rdi
leaq 0x17374d(%rip), %rsi    # 0x19fc0f
callq 0x15e380
jmp 0x2c51f
movq -0x20(%rbp), %rax
shlq $0x2, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
imulq 0x8(%rcx), %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x30(%rbp), %rdx
addq -0x38(%rbp), %rdx
movl $0x10, %ecx
callq 0x2c790
cmpl $0x0, %eax
je 0x2c51d
movq $0x10, -0x50(%rbp)
movq -0x8(%rbp), %rdi
leaq 0x173710(%rip), %rsi    # 0x19fc28
callq 0x15e380
jmp 0x2c51f
cmpq $0x0, -0x50(%rbp)
jne 0x2c53d
movq -0x8(%rbp), %rdi
leaq 0x173710(%rip), %rsi    # 0x19fc41
movb $0x0, %al
callq 0x15bfb0
jmp 0x2c68a
cmpq $0x10, -0x50(%rbp)
jne 0x2c551
movq -0x10(%rbp), %rax
movl $0x0, 0x10(%rax)
jmp 0x2c55c
movq -0x10(%rbp), %rax
movl $0x1, 0x10(%rax)
movq -0x8(%rbp), %rdi
leaq 0x1736f4(%rip), %rsi    # 0x19fc5b
callq 0x15cb30
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x2c589
movq -0x48(%rbp), %rdi
callq 0x15cca0
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x30(%rbp), %rax
addq -0x38(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x40(%rbp), %rdx
movq -0x50(%rbp), %rcx
movq -0x10(%rbp), %r9
addq $0x14, %r9
movq -0x50(%rbp), %r10
movq -0x10(%rbp), %rax
movl 0x10(%rax), %r11d
movl $0x1, %eax
xorl %r8d, %r8d
cmpl $0x0, %r11d
cmovnel %r8d, %eax
movl $0x3, %r8d
movq %r10, (%rsp)
movl $0x2, 0x8(%rsp)
movl %eax, 0x10(%rsp)
callq 0x16c160
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
movl $0x3, %ecx
callq 0x16b750
movq %rax, -0x18(%rbp)
cmpq $0x10, -0x50(%rbp)
jne 0x2c64c
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
movq -0x28(%rbp), %rcx
movq -0x20(%rbp), %r8
movq -0x10(%rbp), %r9
addq $0x14, %r9
movq -0x18(%rbp), %rax
movl $0x4, %edx
movq %rax, (%rsp)
movl $0x2, 0x8(%rsp)
callq 0x16c690
jmp 0x2c67b
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %r8
addq $0x14, %r8
movq -0x18(%rbp), %r9
movl $0x8, %edx
xorl %eax, %eax
movl $0x0, (%rsp)
callq 0x16c520
movq -0x18(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x16afd0
movq -0x18(%rbp), %rdi
callq 0x16af60
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax)