// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/epocimage.c
// Function: de_run_epocaif
static void de_run_epocaif(deark *c, lctx *d)
{
	i64 table_offset;
	i64 pos;
	i64 i;
	i64 caption_count_code;
	i64 num_images;
	i64 first_image_pos;
	i64 img_pos;
	struct phys_image_ctx *pi_fg = NULL;
	struct phys_image_ctx *pi_mask = NULL;

	de_dbg(c, "header at %d", 0);
	de_dbg_indent(c, 1);
	table_offset = de_getu32le(16);
	de_dbg(c, "table offset: %"I64_FMT, table_offset);
	de_dbg_indent(c, -1);

	pos = table_offset;
	de_dbg(c, "table at %"I64_FMT, pos);
	de_dbg_indent(c, 1);
	// The first byte seems to be 2 times the number of captions.
	caption_count_code = de_getbyte(pos);
	de_dbg(c, "caption count code(?): %d", (int)caption_count_code);
	pos++;

	// Next, there are 3*caption_count_code partially-unknown bytes
	// (we know that this includes the position of the captions).
	pos += 3*caption_count_code;

	num_images = de_getbyte(pos);
	de_dbg(c, "bitmap count(?): %d", (int)num_images);
	pos++;

	first_image_pos = de_getu32le(pos);
	de_dbg(c, "offset of first bitmap: %"I64_FMT, first_image_pos);

	de_dbg_indent(c, -1);

	// Unfortunately, I don't know what the remaining data in the file is for.
	// (I'm working without specs.) Maybe it indicates which image is a
	// transparency mask for which other image, or something.
	// For now, I'll assume that every second image is the transparency mask for
	// the previous image.

	img_pos = first_image_pos;
	i = 0;
	while(i<num_images) {
		if(pi_fg) {
			destroy_phys_image(c, pi_fg);
			pi_fg = NULL;
		}
		if(pi_mask) {
			destroy_phys_image(c, pi_mask);
			pi_mask = NULL;
		}

		de_dbg(c, "image #%d", (int)(i/2));
		de_dbg_indent(c, 1);

		de_dbg(c, "foreground bitmap at %"I64_FMT, img_pos);
		de_dbg_indent(c, 1);
		pi_fg = de_malloc(c, sizeof(struct phys_image_ctx));
		pi_fg->is_mask = 0;
		do_read_paint_data_section(c, d, pi_fg, img_pos);
		if(pi_fg->paint_data_section_size<=0) break;
		img_pos += pi_fg->paint_data_section_size;
		i++;
		de_dbg_indent(c, -1);

		if(i<num_images) {
			de_dbg(c, "mask bitmap at %"I64_FMT, img_pos);
			de_dbg_indent(c, 1);
			pi_mask = de_malloc(c, sizeof(struct phys_image_ctx));
			pi_mask->is_mask = 1;
			do_read_paint_data_section(c, d, pi_mask, img_pos);
			if(pi_mask->paint_data_section_size<=0) break;
			img_pos += pi_mask->paint_data_section_size;
			i++;
			de_dbg_indent(c, -1);
		}

		do_combine_and_write_images(c, d, pi_fg, pi_mask);
		de_dbg_indent(c, -1);
	}

	destroy_phys_image(c, pi_fg);
	destroy_phys_image(c, pi_mask);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x8(%rbp), %rdi
leaq 0x168323(%rip), %rsi    # 0x1c4f8e
xorl %edx, %edx
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x10, %esi
callq 0x165540
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rdx
leaq 0x16adf2(%rip), %rsi    # 0x1c7a99
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x18(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rdx
leaq 0x1677da(%rip), %rsi    # 0x1c44ad
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
callq 0x165060
movzbl %al, %eax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rax
movl %eax, %edx
leaq 0x14b331(%rip), %rsi    # 0x1a8042
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
imulq $0x3, -0x30(%rbp), %rax
addq -0x20(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
callq 0x165060
movzbl %al, %eax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movl %eax, %edx
leaq 0x14b302(%rip), %rsi    # 0x1a805c
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
callq 0x165540
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rdx
leaq 0x14b2df(%rip), %rsi    # 0x1a8070
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x40(%rbp), %rax
movq %rax, -0x48(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x28(%rbp), %rax
cmpq -0x38(%rbp), %rax
jge 0x5cf7f
cmpq $0x0, -0x50(%rbp)
je 0x5cde0
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x5d8b0
movq $0x0, -0x50(%rbp)
cmpq $0x0, -0x58(%rbp)
je 0x5cdfc
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x5d8b0
movq $0x0, -0x58(%rbp)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movl $0x2, %ecx
cqto
idivq %rcx
movl %eax, %edx
leaq 0x14b275(%rip), %rsi    # 0x1a808c
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rdx
leaq 0x14b25b(%rip), %rsi    # 0x1a8096
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movl $0x440, %esi            # imm = 0x440
callq 0x15b780
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movl $0x0, 0x20(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x50(%rbp), %rdx
movq -0x48(%rbp), %rcx
callq 0x5d420
movq -0x50(%rbp), %rax
cmpq $0x0, 0x38(%rax)
jg 0x5ce92
jmp 0x5cf7f
movq -0x50(%rbp), %rax
movq 0x38(%rax), %rax
addq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x28(%rbp), %rax
cmpq -0x38(%rbp), %rax
jge 0x5cf57
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rdx
leaq 0x14b1d6(%rip), %rsi    # 0x1a80af
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movl $0x440, %esi            # imm = 0x440
callq 0x15b780
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movl $0x1, 0x20(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq -0x48(%rbp), %rcx
callq 0x5d420
movq -0x58(%rbp), %rax
cmpq $0x0, 0x38(%rax)
jg 0x5cf2d
jmp 0x5cf7f
movq -0x58(%rbp), %rax
movq 0x38(%rax), %rax
addq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x50(%rbp), %rdx
movq -0x58(%rbp), %rcx
callq 0x5e370
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
jmp 0x5cdb6
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x5d8b0
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x5d8b0
addq $0x60, %rsp
popq %rbp
retq
nop