// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/insetpix.c
// Function: de_run_insetpix
static void de_run_insetpix(deark *c, de_module_params *mparams)
{
	lctx *d = NULL;
	UI pix_version;
	i64 item;
	i64 pos;
	i64 num_skipped_items = 0;
	int saved_indent_level;

	de_dbg_indent_save(c, &saved_indent_level);
	d = de_malloc(c, sizeof(lctx));
	d->input_encoding = de_get_input_encoding(c, NULL, DE_ENCODING_CP437);

	pix_version = (UI)de_getu16le(0);
	d->item_count = de_getu16le(2);
	de_dbg(c, "version: %u", pix_version);
	de_dbg(c, "index at 4, %d items", (int)d->item_count);

	// Scan the index, and record the location of items we care about.
	// (The index will be read again when converting the image bitmap.)
	de_dbg_indent(c, 1);
	for(item=0; item<d->item_count; item++) {
		struct insetpix_item_data itd;
		u8 skip_flag = 0;

		pos = 4 + 8*item;
		if(pos+8 > c->infile->len) goto done;

		insetpix_read_item(c, pos, &itd);
		if(itd.is_tile) {
			if(d->num_tiles_found==0) {
				d->idx_of_1st_tile = item;
			}
			d->num_tiles_found++;
		}
		skip_flag = itd.is_special;
		if(skip_flag) {
			num_skipped_items++;
		}
		if(skip_flag && c->debug_level<2) { // Skip "tile" items for now
			continue;
		}

		insetpix_dbg_item(c, &itd, item);

		if(skip_flag) {
			continue;
		}

		if(itd.loc + itd.len > c->infile->len) {
			de_err(c, "Item #%d (ID %u) goes beyond end of file",
				(int)item, itd.id);
			goto done;
		}

		switch(itd.id) {
		case 0:
			d->imginfo_pos = itd.loc;
			d->imginfo_len = itd.len;
			break;
		case 1:
			if(!d->pal_pos) {
				d->pal_pos = itd.loc;
				d->pal_len = itd.len;
			}
			break;
		case 2:
			d->tileinfo_pos = itd.loc;
			d->tileinfo_len = itd.len;
			break;
		}
	}
	if(c->debug_level<2) {
		de_dbg(c, "other items not listed: %"I64_FMT, num_skipped_items);
	}
	de_dbg(c, "number of tiles: %"I64_FMT, d->num_tiles_found);
	de_dbg_indent(c, -1);

	do_image_info(c, d);
	if(!d->have_image_info) {
		de_err(c, "Bad or missing Image Info");
		goto done;
	}

	if(d->pal_sample_descriptor[0]!=0 && d->pal_sample_descriptor[1]==0 &&
		d->pal_sample_descriptor[2]==0 && d->pal_sample_descriptor[3]==0)
	{
		d->is_grayscale = 1;
	}

	do_palette(c, d);
	if(!d->have_pal_data) {
		de_err(c, "Bad or missing palette");
		goto done;
	}

	do_tileinfo(c, d);
	if(!d->have_tile_info) {
		de_err(c, "Bad or missing Tile Info");
		goto done;
	}

	if(d->graphics_type==0) {
		do_char_graphics(c, d);
	}
	else {
		if(d->gfore<1 || d->gfore>8) {
			de_err(c, "Inset PIX with %d bits/pixel are not supported", (int)d->gfore);
			goto done;
		}

		switch(d->descriptor_combined) {
		case 0x00040404U:
		case 0x00404040U:
		case 0x02000000U:
		case 0x02020202U:
			break;
		default:
			de_warn(c, "Not a known image type. This image might not be handled correctly.");
		}

		do_bitmap(c, d);
	}

done:
	if(d) {
		de_bitmap_destroy(d->tile_img);
		de_free(c, d);
	}
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x3c(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rdi
movl $0x4f0, %esi            # imm = 0x4F0
callq 0x15b780
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xd, %edx
callq 0x15e4a0
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x165280
movl %eax, -0x1c(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x2, %esi
callq 0x165280
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x8(%rbp), %rdi
movl -0x1c(%rbp), %edx
leaq 0x10b15e(%rip), %rsi    # 0x1c2686
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rax
movl %eax, %edx
leaq 0xfdd59(%rip), %rsi     # 0x1b529d
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq $0x0, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x40(%rcx), %rax
jge 0xb76eb
movb $0x0, -0x61(%rbp)
movq -0x28(%rbp), %rax
shlq $0x3, %rax
addq $0x4, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
addq $0x8, %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jle 0xb75a2
jmp 0xb78c3
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
leaq -0x60(%rbp), %rdx
callq 0xb7ad0
cmpb $0x0, -0x57(%rbp)
je 0xb75e0
movq -0x18(%rbp), %rax
cmpq $0x0, 0x48(%rax)
jne 0xb75d0
movq -0x28(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x50(%rax)
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x48(%rax)
movb -0x58(%rbp), %al
movb %al, -0x61(%rbp)
cmpb $0x0, -0x61(%rbp)
je 0xb75f8
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
movzbl -0x61(%rbp), %eax
cmpl $0x0, %eax
je 0xb760f
movq -0x8(%rbp), %rax
cmpl $0x2, (%rax)
jge 0xb760f
jmp 0xb76da
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rdx
leaq -0x60(%rbp), %rsi
callq 0xb7bc0
cmpb $0x0, -0x61(%rbp)
je 0xb762b
jmp 0xb76da
movq -0x48(%rbp), %rax
addq -0x50(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jle 0xb7661
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movl %eax, %edx
movl -0x60(%rbp), %ecx
leaq 0xfdc5d(%rip), %rsi     # 0x1b52b2
movb $0x0, %al
callq 0x15bfb0
jmp 0xb78c3
movl -0x60(%rbp), %eax
movl %eax, -0x68(%rbp)
testl %eax, %eax
je 0xb7681
jmp 0xb766d
movl -0x68(%rbp), %eax
subl $0x1, %eax
je 0xb769b
jmp 0xb7677
movl -0x68(%rbp), %eax
subl $0x2, %eax
je 0xb76c0
jmp 0xb76d8
movq -0x48(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x50(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x18(%rax)
jmp 0xb76d8
movq -0x18(%rbp), %rax
cmpq $0x0, 0x20(%rax)
jne 0xb76be
movq -0x48(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x50(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x28(%rax)
jmp 0xb76d8
movq -0x48(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x50(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x38(%rax)
jmp 0xb76da
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0xb7561
movq -0x8(%rbp), %rax
cmpl $0x2, (%rax)
jge 0xb770a
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rdx
leaq 0xfdbd8(%rip), %rsi     # 0x1b52db
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rdx
leaq 0xfdbda(%rip), %rsi     # 0x1b52f7
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0xb7c30
movq -0x18(%rbp), %rax
cmpb $0x0, 0x8(%rax)
jne 0xb7760
movq -0x8(%rbp), %rdi
leaq 0xfdbb8(%rip), %rsi     # 0x1b530c
movb $0x0, %al
callq 0x15bfb0
jmp 0xb78c3
movq -0x18(%rbp), %rax
movzbl 0x90(%rax), %eax
cmpl $0x0, %eax
je 0xb77ae
movq -0x18(%rbp), %rax
movzbl 0x91(%rax), %eax
cmpl $0x0, %eax
jne 0xb77ae
movq -0x18(%rbp), %rax
movzbl 0x92(%rax), %eax
cmpl $0x0, %eax
jne 0xb77ae
movq -0x18(%rbp), %rax
movzbl 0x93(%rax), %eax
cmpl $0x0, %eax
jne 0xb77ae
movq -0x18(%rbp), %rax
movl $0x1, 0xd8(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0xb8080
movq -0x18(%rbp), %rax
cmpb $0x0, 0x9(%rax)
jne 0xb77dc
movq -0x8(%rbp), %rdi
leaq 0xfdb56(%rip), %rsi     # 0x1b5326
movb $0x0, %al
callq 0x15bfb0
jmp 0xb78c3
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0xb8530
movq -0x18(%rbp), %rax
cmpb $0x0, 0xa(%rax)
jne 0xb780a
movq -0x8(%rbp), %rdi
leaq 0xfdb3f(%rip), %rsi     # 0x1b533d
movb $0x0, %al
callq 0x15bfb0
jmp 0xb78c3
movq -0x18(%rbp), %rax
movzbl 0x6(%rax), %eax
cmpl $0x0, %eax
jne 0xb7829
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0xb86d0
jmp 0xb78c1
movq -0x18(%rbp), %rax
cmpq $0x1, 0x80(%rax)
jl 0xb7845
movq -0x18(%rbp), %rax
cmpq $0x8, 0x80(%rax)
jle 0xb7866
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x80(%rax), %rax
movl %eax, %edx
leaq 0xfdaf9(%rip), %rsi     # 0x1b5356
movb $0x0, %al
callq 0x15bfb0
jmp 0xb78c3
movq -0x18(%rbp), %rax
movl 0x94(%rax), %eax
movl %eax, -0x6c(%rbp)
subl $0x40404, %eax          # imm = 0x40404
je 0xb78a0
jmp 0xb787c
movl -0x6c(%rbp), %eax
subl $0x404040, %eax         # imm = 0x404040
je 0xb78a0
jmp 0xb7888
movl -0x6c(%rbp), %eax
subl $0x2000000, %eax        # imm = 0x2000000
je 0xb78a0
jmp 0xb7894
movl -0x6c(%rbp), %eax
subl $0x2020202, %eax        # imm = 0x2020202
jne 0xb78a2
jmp 0xb78a0
jmp 0xb78b4
movq -0x8(%rbp), %rdi
leaq 0xfdad8(%rip), %rsi     # 0x1b5385
movb $0x0, %al
callq 0x15bc10
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0xb8930
jmp 0xb78c3
cmpq $0x0, -0x18(%rbp)
je 0xb78e7
movq -0x18(%rbp), %rax
movq 0xe0(%rax), %rdi
callq 0x16af60
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x15b820
movq -0x8(%rbp), %rdi
movl -0x3c(%rbp), %esi
callq 0x15b5a0
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax)