// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/amigaicon.c
// Function: do_read_main_icon
static int do_read_main_icon(deark *c, lctx *d,
	i64 pos, i64 icon_index)
{
	i64 npwidth, height;
	i64 pdwidth;
	i64 depth;
	i64 src_rowspan, src_planespan;
	i64 i;
	int retval = 0;
	de_bitmap *img = NULL;
	de_color pal[256];

	de_dbg(c, "main icon #%d at %"I64_FMT, (int)icon_index, pos);
	de_dbg_indent(c, 1);

	// 20-byte header, followed by one or more bitmap "planes".
	npwidth = de_getu16be(pos+4);
	height = de_getu16be(pos+6);
	depth = de_getu16be(pos+8);
	de_dbg_dimensions(c, npwidth, height);
	de_dbg(c, "depth: %d", (int)depth);

	if(depth<1 || depth>8) {
		de_err(c, "Unsupported bit depth: %d", (int)depth);
		goto done;
	}

	pdwidth = de_pad_to_n(npwidth, 16);
	src_rowspan = pdwidth/8;
	src_planespan = src_rowspan * height;

	img = de_bitmap_create2(c, npwidth, pdwidth, height, 3);

	// Figure out what palette to use

	// Start with a meaningless grayscale palette.
	de_make_grayscale_palette(pal, 256, 0x0);

	if(depth==1) {
		// The only 1-bit images I've seen are dummy images.
		// I don't know how they're supposed to be handled, but this should do.
		pal[0] = DE_STOCKCOLOR_BLACK;
		pal[1] = DE_STOCKCOLOR_WHITE;
		if(!d->has_newicons && !d->has_glowicons) {
			de_warn(c, "Don't know how to handle 1-bit images");
		}
	}
	else if(d->icon_revision==0 && depth==2) {
		for(i=0; i<4; i++) pal[i] = rev1pal[i];
	}
	else if(depth==2) {
		for(i=0; i<4; i++) pal[i] = rev2pal[i];
	}
	else if(depth==3) {
		for(i=0; i<8; i++) pal[i] = magicwbpal[i];
	}
	else { // depth 4..8
		for(i=0; i<(1LL<<depth); i++) {
			// I don't know the logic behind this, and it might not be correct in
			// all cases.
			pal[i] = magicwbpal[i>>(depth-3)];
		}
	}

	pos += 20;
	de_convert_image_paletted_planar(c->infile, pos, depth, src_rowspan, src_planespan,
		pal, img, 0);
	de_bitmap_write_to_file(img, NULL, (d->has_newicons||d->has_glowicons)?DE_CREATEFLAG_IS_AUX:0);

	retval = 1;

done:
	de_bitmap_destroy(img);
	de_dbg_indent(c, -1);
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x490, %rsp            # imm = 0x490
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movl $0x0, -0x5c(%rbp)
movq $0x0, -0x68(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movl %eax, %edx
movq -0x18(%rbp), %rcx
leaq 0x17f540(%rip), %rsi    # 0x19d7cf
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x4, %rsi
callq 0x1651d0
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x6, %rsi
callq 0x1651d0
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x8, %rsi
callq 0x1651d0
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x15bba0
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rax
movl %eax, %edx
leaq 0x18f9de(%rip), %rsi    # 0x1adcef
movb $0x0, %al
callq 0x15b0d0
cmpq $0x1, -0x40(%rbp)
jl 0x1e326
cmpq $0x8, -0x40(%rbp)
jle 0x1e343
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rax
movl %eax, %edx
leaq 0x17f4ad(%rip), %rsi    # 0x19d7e4
movb $0x0, %al
callq 0x15bfb0
jmp 0x1e5b1
movq -0x28(%rbp), %rdi
movl $0x10, %esi
callq 0x15ce90
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movl $0x8, %ecx
cqto
idivq %rcx
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
imulq -0x30(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x30(%rbp), %rcx
movl $0x3, %r8d
callq 0x16b7f0
movq %rax, -0x68(%rbp)
leaq -0x470(%rbp), %rdi
movl $0x100, %esi            # imm = 0x100
xorl %edx, %edx
callq 0x16d580
cmpq $0x1, -0x40(%rbp)
jne 0x1e3ec
movl $0xff000000, -0x470(%rbp) # imm = 0xFF000000
movl $0xffffffff, -0x46c(%rbp) # imm = 0xFFFFFFFF
movq -0x10(%rbp), %rax
cmpl $0x0, 0x38(%rax)
jne 0x1e3e7
movq -0x10(%rbp), %rax
cmpl $0x0, 0x68(%rax)
jne 0x1e3e7
movq -0x8(%rbp), %rdi
leaq 0x17f41e(%rip), %rsi    # 0x19d7fe
movb $0x0, %al
callq 0x15bc10
jmp 0x1e516
movq -0x10(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x1e438
cmpq $0x2, -0x40(%rbp)
jne 0x1e438
movq $0x0, -0x58(%rbp)
cmpq $0x4, -0x58(%rbp)
jge 0x1e433
movq -0x58(%rbp), %rcx
leaq 0x17eff9(%rip), %rax    # 0x19d410
movl (%rax,%rcx,4), %ecx
movq -0x58(%rbp), %rax
movl %ecx, -0x470(%rbp,%rax,4)
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x1e405
jmp 0x1e514
cmpq $0x2, -0x40(%rbp)
jne 0x1e47a
movq $0x0, -0x58(%rbp)
cmpq $0x4, -0x58(%rbp)
jge 0x1e475
movq -0x58(%rbp), %rcx
leaq 0x17efc7(%rip), %rax    # 0x19d420
movl (%rax,%rcx,4), %ecx
movq -0x58(%rbp), %rax
movl %ecx, -0x470(%rbp,%rax,4)
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x1e447
jmp 0x1e512
cmpq $0x3, -0x40(%rbp)
jne 0x1e4b9
movq $0x0, -0x58(%rbp)
cmpq $0x8, -0x58(%rbp)
jge 0x1e4b7
movq -0x58(%rbp), %rcx
leaq 0x17ef95(%rip), %rax    # 0x19d430
movl (%rax,%rcx,4), %ecx
movq -0x58(%rbp), %rax
movl %ecx, -0x470(%rbp,%rax,4)
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x1e489
jmp 0x1e510
movq $0x0, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq -0x40(%rbp), %rcx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
cmpq %rcx, %rax
jge 0x1e50e
movq -0x58(%rbp), %rax
movq -0x40(%rbp), %rcx
subq $0x3, %rcx
sarq %cl, %rax
movq %rax, %rcx
leaq 0x17ef3e(%rip), %rax    # 0x19d430
movl (%rax,%rcx,4), %ecx
movq -0x58(%rbp), %rax
movl %ecx, -0x470(%rbp,%rax,4)
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x1e4c1
jmp 0x1e510
jmp 0x1e512
jmp 0x1e514
jmp 0x1e516
movq -0x18(%rbp), %rax
addq $0x14, %rax
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x48(%rbp), %rcx
movq -0x50(%rbp), %r8
leaq -0x470(%rbp), %r9
movq -0x68(%rbp), %rax
xorl %r10d, %r10d
movq %rax, (%rsp)
movl $0x0, 0x8(%rsp)
callq 0x16c690
movq -0x68(%rbp), %rax
movq %rax, -0x480(%rbp)
movq -0x10(%rbp), %rcx
movb $0x1, %al
cmpl $0x0, 0x38(%rcx)
movb %al, -0x471(%rbp)
jne 0x1e587
movq -0x10(%rbp), %rax
cmpl $0x0, 0x68(%rax)
setne %al
movb %al, -0x471(%rbp)
movq -0x480(%rbp), %rdi
movb -0x471(%rbp), %cl
xorl %edx, %edx
movl $0x1, %eax
testb $0x1, %cl
cmovnel %eax, %edx
xorl %eax, %eax
movl %eax, %esi
callq 0x16afd0
movl $0x1, -0x5c(%rbp)
movq -0x68(%rbp), %rdi
callq 0x16af60
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movl -0x5c(%rbp), %eax
addq $0x490, %rsp            # imm = 0x490
popq %rbp
retq
nopw %cs:(%rax,%rax)