// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/misc2.c
// Function: do_fmtownsicn_FJ
static void do_fmtownsicn_FJ(deark *c, struct fmtownsicn_ctx *d)
{
	i64 num_icons;
	i64 num_icons_capacity;
	i64 w, h;
	i64 rowspan;
	i64 icon_size;
	UI bits_per_pixel;
	i64 i;
	i64 pos1 = 0;
	i64 pos = pos1;

	de_dbg(c, "header at %"I64_FMT, pos1);
	de_dbg_indent(c, 1);
	pos += 8;

	bits_per_pixel = (UI)dbuf_getu16x(c->infile, pos, d->is_le);
	pos += 2;
	de_dbg(c, "bits per pixel: %u", bits_per_pixel);

	w = dbuf_getu16x(c->infile, pos, d->is_le);
	pos += 2;
	h = dbuf_getu16x(c->infile, pos, d->is_le);
	pos += 2;
	de_dbg_dimensions(c, w, h);

	num_icons = dbuf_getu16x(c->infile, pos, d->is_le);
	//pos += 2;
	de_dbg(c, "num icons: %"I64_FMT, num_icons);

	de_dbg_indent(c, -1);

	if(w!=32 || h!=32 || bits_per_pixel!=4) {
		d->need_errmsg = 1;
		goto done;
	}
	rowspan = 16;
	icon_size = 2+rowspan*h;

	if(c->infile->len<16 ||
		((c->infile->len-16) % icon_size)!=0)
	{
		d->need_errmsg = 1;
		goto done;
	}

	num_icons_capacity = (c->infile->len-16) / icon_size;
	if(num_icons_capacity < num_icons) {
		de_warn(c, "Expected %"I64_FMT" icons, only found %"I64_FMT,
			num_icons, num_icons_capacity);
		num_icons = num_icons_capacity;
	}

	for(i=0; i<num_icons; i++) {
		de_bitmap *img;
		UI id;

		pos = 16+i*icon_size;
		de_dbg(c, "icon at %"I64_FMT, pos);
		de_dbg_indent(c, 1);
		id = (UI)dbuf_getu16x(c->infile, pos, d->is_le);
		de_dbg(c, "id: 0x%04x", id);

		img = de_bitmap_create(c, w, h, 3);
		de_convert_image_paletted(c->infile, pos+2, 4, rowspan,
			d->pal16, img, 0x01);
		de_bitmap_write_to_file(img, NULL, DE_CREATEFLAG_OPT_IMAGE);
		de_bitmap_destroy(img);
		de_dbg_indent(c, -1);
	}
done:
	;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rdx
leaq 0x112271(%rip), %rsi    # 0x1aed63
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x60(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x60(%rbp), %rsi
movq -0x10(%rbp), %rax
movl 0x4(%rax), %edx
callq 0x165820
movl %eax, -0x44(%rbp)
movq -0x60(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rdi
movl -0x44(%rbp), %edx
leaq 0x114275(%rip), %rsi    # 0x1b0dbd
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x60(%rbp), %rsi
movq -0x10(%rbp), %rax
movl 0x4(%rax), %edx
callq 0x165820
movq %rax, -0x28(%rbp)
movq -0x60(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x60(%rbp), %rsi
movq -0x10(%rbp), %rax
movl 0x4(%rax), %edx
callq 0x165820
movq %rax, -0x30(%rbp)
movq -0x60(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x15bba0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x60(%rbp), %rsi
movq -0x10(%rbp), %rax
movl 0x4(%rax), %edx
callq 0x165820
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rdx
leaq 0x1141f5(%rip), %rsi    # 0x1b0dd0
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
cmpq $0x20, -0x28(%rbp)
jne 0x9cc04
cmpq $0x20, -0x30(%rbp)
jne 0x9cc04
cmpl $0x4, -0x44(%rbp)
je 0x9cc11
movq -0x10(%rbp), %rax
movb $0x1, 0x1(%rax)
jmp 0x9cdaa
movq $0x10, -0x38(%rbp)
movq -0x38(%rbp), %rax
imulq -0x30(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
cmpq $0x10, 0x18(%rax)
jl 0x9cc55
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rax
subq $0x10, %rax
cqto
idivq -0x40(%rbp)
cmpq $0x0, %rdx
je 0x9cc62
movq -0x10(%rbp), %rax
movb $0x1, 0x1(%rax)
jmp 0x9cdaa
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rax
subq $0x10, %rax
cqto
idivq -0x40(%rbp)
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
cmpq -0x18(%rbp), %rax
jge 0x9cca8
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
leaq 0x114146(%rip), %rsi    # 0x1b0ddf
movb $0x0, %al
callq 0x15bc10
movq -0x20(%rbp), %rax
movq %rax, -0x18(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x18(%rbp), %rax
jge 0x9cda8
movq -0x50(%rbp), %rax
imulq -0x40(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rdx
leaq 0x114047(%rip), %rsi    # 0x1b0d25
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x60(%rbp), %rsi
movq -0x10(%rbp), %rax
movl 0x4(%rax), %edx
callq 0x165820
movl %eax, -0x6c(%rbp)
movq -0x8(%rbp), %rdi
movl -0x6c(%rbp), %edx
leaq 0x10964b(%rip), %rsi    # 0x1a6367
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
movl $0x3, %ecx
callq 0x16b750
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x60(%rbp), %rsi
addq $0x2, %rsi
movq -0x38(%rbp), %rcx
movq -0x10(%rbp), %r8
addq $0x10, %r8
movq -0x68(%rbp), %r9
movl $0x4, %edx
movl $0x1, (%rsp)
callq 0x16c520
movq -0x68(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x16afd0
movq -0x68(%rbp), %rdi
callq 0x16af60
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x9ccb0
jmp 0x9cdaa
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)