// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/icns.c
// Function: de_run_icns_pass
static void de_run_icns_pass(deark *c, lctx *d, int pass)
{
	i64 segment_pos;
	struct page_ctx *pg = NULL;
	int image_count;
	int saved_indent_level;

	de_dbg_indent_save(c, &saved_indent_level);
	segment_pos = 8;
	image_count = 0;

	while(1) {
		i64 segment_len;

		if(pg) { de_free(c, pg); pg=NULL; }

		if(segment_pos+8 > d->file_size) break;

		pg = de_malloc(c, sizeof(struct page_ctx));
		pg->segment_pos = segment_pos;
		pg->image_num = image_count;

		dbuf_read_fourcc(c->infile, segment_pos, &pg->code4cc, 4, 0x0);

		segment_len = de_getu32be(segment_pos+4);

		pg->image_pos = segment_pos + 8;
		pg->image_len = segment_len - 8;

		if(pass==2) {
			de_dbg(c, "image #%d, type '%s', segment at %"I64_FMT", image at %"I64_FMT", size=%"I64_FMT,
				pg->image_num, pg->code4cc.id_dbgstr,
				pg->segment_pos, pg->image_pos, pg->image_len);
		}
		de_dbg_indent(c, 1);

		if(segment_len<8 || segment_pos+segment_len > d->file_size) {
			if(pass==2) {
				de_err(c, "Invalid length for segment '%s' (%u)", pg->code4cc.id_sanitized_sz,
					(unsigned int)segment_len);
			}
			break;
		}

		if(segment_len==8) {
			if(pass==2) {
				de_dbg(c, "[empty icon]");
			}
			goto next_icon;
		}

		if(pass==2) {
			size_t i;

			// Find this type code in the image_type_info array
			pg->type_info = NULL;
			for(i=0; i<DE_ARRAYCOUNT(image_type_info_arr); i++) {
				if(image_type_info_arr[i].code==pg->code4cc.id) {
					pg->type_info = &image_type_info_arr[i];
					break;
				}
			}
			if(!pg->type_info) {
				de_warn(c, "(Image #%d) Unknown image type '%s'", pg->image_num, pg->code4cc.id_sanitized_sz);
			}
		}

		if(pass==1) {
			de_dbg_indent(c, -1);
			switch(pg->code4cc.id) {
			case 0x69636d23: // icm# 16x12x1
				do_read_mask(c, d, pg, MASKTYPEID_16_12_1, 1, 16, 12);
				break;
			case 0x69637323: // ics# 16x16x1
				do_read_mask(c, d, pg, MASKTYPEID_16_16_1, 1, 16, 16);
				break;
			case 0x49434e23: // ICN# 32x32x1
				do_read_mask(c, d, pg, MASKTYPEID_32_32_1, 1, 32, 32);
				break;
			case 0x69636823: // ich# 48x48x1
				do_read_mask(c, d, pg, MASKTYPEID_48_48_1, 1, 48, 48);
				break;
			case 0x73386d6b: // s8mk 16x16x8
				do_read_mask(c, d, pg, MASKTYPEID_16_16_8, 8, 16, 16);
				break;
			case 0x6c386d6b: // l8mk 32x32x8
				do_read_mask(c, d, pg, MASKTYPEID_32_32_8, 8, 32, 32);
				break;
			case 0x68386d6b: // h8mk 48x48x8
				do_read_mask(c, d, pg, MASKTYPEID_48_48_8, 8, 48, 48);
				break;
			case 0x74386d6b: // t8mk 128x128x8
				do_read_mask(c, d, pg, MASKTYPEID_128_128_8, 8, 128, 128);
				break;
			}
			de_dbg_indent(c, 1);
		}
		else if(pass==2) {
			do_icon(c, d, pg);
		}

next_icon:
		image_count++;
		segment_pos += segment_len;
		de_dbg_indent(c, -1);
	}

	if(pg) de_free(c, pg);
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x15b580
movq $0x8, -0x20(%rbp)
movl $0x0, -0x2c(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0xa75e3
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x15b820
movq $0x0, -0x28(%rbp)
movq -0x20(%rbp), %rax
addq $0x8, %rax
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
jle 0xa75f9
jmp 0xa7a27
movq -0x8(%rbp), %rdi
movl $0x490, %esi            # imm = 0x490
callq 0x15b780
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
movl -0x2c(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
addq $0x38, %rdx
movl $0x4, %ecx
xorl %r8d, %r8d
callq 0x169860
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
addq $0x4, %rsi
callq 0x165470
movq %rax, -0x38(%rbp)
movq -0x20(%rbp), %rcx
addq $0x8, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x38(%rbp), %rcx
subq $0x8, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x18(%rax)
cmpl $0x2, -0x14(%rbp)
jne 0xa76c0
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movl (%rax), %edx
movq -0x28(%rbp), %rcx
addq $0x38, %rcx
addq $0x10, %rcx
movq -0x28(%rbp), %rax
movq 0x8(%rax), %r8
movq -0x28(%rbp), %rax
movq 0x10(%rax), %r9
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
leaq 0x10b2fd(%rip), %rsi    # 0x1b29b2
movq %rax, (%rsp)
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
cmpq $0x8, -0x38(%rbp)
jl 0xa76e6
movq -0x20(%rbp), %rax
addq -0x38(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
jle 0xa7715
cmpl $0x2, -0x14(%rbp)
jne 0xa7710
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rdx
addq $0x38, %rdx
addq $0x8, %rdx
movq -0x38(%rbp), %rax
movl %eax, %ecx
leaq 0x10b2e6(%rip), %rsi    # 0x1b29ef
movb $0x0, %al
callq 0x15bfb0
jmp 0xa7a27
cmpq $0x8, -0x38(%rbp)
jne 0xa7739
cmpl $0x2, -0x14(%rbp)
jne 0xa7734
movq -0x8(%rbp), %rdi
leaq 0x10b2e7(%rip), %rsi    # 0x1b2a14
movb $0x0, %al
callq 0x15b0d0
jmp 0xa79ff
cmpl $0x2, -0x14(%rbp)
jne 0xa77d2
movq -0x28(%rbp), %rax
movq $0x0, 0x30(%rax)
movq $0x0, -0x40(%rbp)
cmpq $0x28, -0x40(%rbp)
jae 0xa77a1
leaq 0x10b55b(%rip), %rax    # 0x1b2cc0
imulq $0x14, -0x40(%rbp), %rcx
addq %rcx, %rax
movl (%rax), %eax
movq -0x28(%rbp), %rcx
cmpl 0x3c(%rcx), %eax
jne 0xa7791
leaq 0x10b541(%rip), %rcx    # 0x1b2cc0
imulq $0x14, -0x40(%rbp), %rax
addq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x30(%rax)
jmp 0xa77a1
jmp 0xa7793
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0xa7757
movq -0x28(%rbp), %rax
cmpq $0x0, 0x30(%rax)
jne 0xa77d0
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movl (%rax), %edx
movq -0x28(%rbp), %rcx
addq $0x38, %rcx
addq $0x8, %rcx
leaq 0x10b258(%rip), %rsi    # 0x1b2a21
movb $0x0, %al
callq 0x15bc10
jmp 0xa77d2
cmpl $0x1, -0x14(%rbp)
jne 0xa79e4
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x28(%rbp), %rax
movl 0x3c(%rax), %eax
movl %eax, -0x44(%rbp)
subl $0x49434e23, %eax       # imm = 0x49434E23
je 0xa78c5
jmp 0xa7801
movl -0x44(%rbp), %eax
subl $0x68386d6b, %eax       # imm = 0x68386D6B
je 0xa797e
jmp 0xa7811
movl -0x44(%rbp), %eax
subl $0x69636823, %eax       # imm = 0x69636823
je 0xa78f4
jmp 0xa7821
movl -0x44(%rbp), %eax
subl $0x69636d23, %eax       # imm = 0x69636D23
je 0xa786c
jmp 0xa782d
movl -0x44(%rbp), %eax
subl $0x69637323, %eax       # imm = 0x69637323
je 0xa7898
jmp 0xa7839
movl -0x44(%rbp), %eax
subl $0x6c386d6b, %eax       # imm = 0x6C386D6B
je 0xa7952
jmp 0xa7849
movl -0x44(%rbp), %eax
subl $0x73386d6b, %eax       # imm = 0x73386D6B
je 0xa7923
jmp 0xa7859
movl -0x44(%rbp), %eax
subl $0x74386d6b, %eax       # imm = 0x74386D6B
je 0xa79aa
jmp 0xa79d4
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x28(%rbp), %rdx
xorl %ecx, %ecx
movl $0x1, %r8d
movl $0x10, %r9d
movq $0xc, (%rsp)
callq 0xa7b40
jmp 0xa79d4
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x28(%rbp), %rdx
movl $0x1, %r8d
movl $0x10, %r9d
movl %r8d, %ecx
movq $0x10, (%rsp)
callq 0xa7b40
jmp 0xa79d4
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x28(%rbp), %rdx
movl $0x2, %ecx
movl $0x1, %r8d
movl $0x20, %r9d
movq $0x20, (%rsp)
callq 0xa7b40
jmp 0xa79d4
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x28(%rbp), %rdx
movl $0x3, %ecx
movl $0x1, %r8d
movl $0x30, %r9d
movq $0x30, (%rsp)
callq 0xa7b40
jmp 0xa79d4
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x28(%rbp), %rdx
movl $0x4, %ecx
movl $0x8, %r8d
movl $0x10, %r9d
movq $0x10, (%rsp)
callq 0xa7b40
jmp 0xa79d4
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x28(%rbp), %rdx
movl $0x5, %ecx
movl $0x8, %r8d
movl $0x20, %r9d
movq $0x20, (%rsp)
callq 0xa7b40
jmp 0xa79d4
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x28(%rbp), %rdx
movl $0x6, %ecx
movl $0x8, %r8d
movl $0x30, %r9d
movq $0x30, (%rsp)
callq 0xa7b40
jmp 0xa79d4
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x28(%rbp), %rdx
movl $0x7, %ecx
movl $0x8, %r8d
movl $0x80, %r9d
movq $0x80, (%rsp)
callq 0xa7b40
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
jmp 0xa79fd
cmpl $0x2, -0x14(%rbp)
jne 0xa79fb
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0xa7d10
jmp 0xa79fd
jmp 0xa79ff
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
movq -0x38(%rbp), %rax
addq -0x20(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
jmp 0xa75c7
cmpq $0x0, -0x28(%rbp)
je 0xa7a3b
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x15b820
movq -0x8(%rbp), %rdi
movl -0x30(%rbp), %esi
callq 0x15b5a0
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)