// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/reko.c
// Function: reko_main
static void reko_main(deark *c, lctx *d)
{
	int saved_indent_level;
	i64 cardidx;
	i64 expected_cardsize;
	i64 full_cardsize;
	de_bitmap *cardimg = NULL;
	de_bitmap *canvas = NULL;
	de_finfo *fi = NULL;
	i64 cards_pos;
	i64 localhdrsize = 0;
	i64 canvas_cols, canvas_rows;
	i64 canvas_w, canvas_h;
	i64 cxpos_maincards, cypos_maincards;
	i64 cxpos_extracards, cypos_extracards;

	de_dbg_indent_save(c, &saved_indent_level);
	cards_pos = d->hdrsize + d->globalpal_nbytes;
	de_dbg(c, "cards at %"I64_FMT, cards_pos);
	de_dbg_indent(c, 1);

	fi = de_finfo_create(c);

	if(d->is_pc) {
		if(d->depth_pixel!=8 && d->depth_pixel!=16) {
			d->fatalerrflag = 1;
			d->need_errmsg = 1;
			goto done;
		}
	}

	if(d->numcards<1 || d->numcards>MAXCARDS ||
		d->w<MINCARDWIDTH || d->w>MAXCARDWIDTH ||
		d->h<MINCARDHEIGHT || d->h>MAXCARDHEIGHT)
	{
		d->fatalerrflag = 1;
		d->need_errmsg = 1;
		goto done;
	}

	if(d->numcards < d->idx_of_first_main_card+52) {
		de_warn(c, "Expected at least %"I64_FMT" cards; only found %"I64_FMT,
			d->idx_of_first_main_card+52, d->numcards);
	}

	if(d->is_pc) {
		expected_cardsize = d->w * d->h * (d->depth_pixel/8);
	}
	else {
		i64 bytes_per_row_per_plane;

		// We expect the width to be a multiple of 8. Other widths are not
		// supported.
		bytes_per_row_per_plane = (d->w)/8;
		d->amiga_plane_stride = bytes_per_row_per_plane;
		d->amiga_row_stride = bytes_per_row_per_plane * d->depth_pixel;
		expected_cardsize = (d->w * d->h * d->depth_pixel)/8;
	}

	if(d->cardsize!=expected_cardsize && !d->suppress_size_warnings) {
		de_warn(c, "Reported cardsize is %"I64_FMT"; expected %"I64_FMT,
			d->cardsize, expected_cardsize);
		d->suppress_size_warnings = 1;
	}

	d->localpal_nbytes = 0;
	if(d->is_pc) {
		localhdrsize = 4;
		if(d->depth_pixel==8) {
			d->localpal_nbytes = 512;
		}
	}
	full_cardsize = localhdrsize + d->localpal_nbytes + d->cardsize;

	canvas_cols = 13;
	canvas_rows = (d->numcards+12) / 13;
	// There has to be at least 1 extra card, so we need at least 5 rows.
	if(canvas_rows<5) canvas_rows = 5;
	cxpos_maincards = 0;
	cypos_maincards = 0;
	cxpos_extracards = 0;
	cypos_extracards = 4;

#define REKO_BORDER 2
	canvas_w = canvas_cols*(d->w+REKO_BORDER)-REKO_BORDER;
	canvas_h = canvas_rows*(d->h+REKO_BORDER)-REKO_BORDER;
	if(d->combine_images) {
		canvas = de_bitmap_create(c, canvas_w, canvas_h, 4);
	}

	cardimg = de_bitmap_create(c, d->w, d->h, 3);

	for(cardidx=0; cardidx<d->numcards; cardidx++) {
		u8 is_main_card;
		i64 dstcxpos, dstcypos;
		i64 dstxpos, dstypos;
		i64 thiscardpos;

		thiscardpos = cards_pos + cardidx*full_cardsize;
		de_dbg(c, "card #%"I64_FMT" at %"I64_FMT, cardidx, thiscardpos);
		de_dbg_indent(c, 1);

		if(d->is_pc && !d->fatalerrflag && (thiscardpos+localhdrsize <= c->infile->len)) {
			i64 cw_raw, ch_raw;

			cw_raw = de_getu16le(thiscardpos);
			ch_raw = de_getu16le(thiscardpos+2);
			if((cw_raw+1 != d->w) || (ch_raw+1 != d->h)) {
				de_err(c, "Card #%d: Bad card header", (int)cardidx);
				d->fatalerrflag = 1;
				// (But keep going.)
			}
		}

		de_bitmap_rect(cardimg, 0, 0, d->w, d->h, DE_STOCKCOLOR_BLACK, 0);

		if(d->fatalerrflag) {
			;
		}
		else if(d->is_pc) {
			if(d->depth_pixel==8) {
				read_image_pc8(c, d, cardimg, thiscardpos+localhdrsize);
			}
			else {
				read_image_pc16(c, d, cardimg, thiscardpos+localhdrsize);
			}
		}
		else {
			read_image_amiga(c, d, cardimg, thiscardpos);
		}

		is_main_card = (cardidx>=d->idx_of_first_main_card &&
			cardidx<(d->idx_of_first_main_card+52));
		if(is_main_card) {
			dstcxpos = cxpos_maincards;
			dstcypos = cypos_maincards;
			cypos_maincards++;
			if(cypos_maincards>=4) {
				cypos_maincards = 0;
				cxpos_maincards++;
			}
		}
		else {
			dstcxpos = cxpos_extracards;
			dstcypos = cypos_extracards;
			cxpos_extracards++;
			if(cxpos_extracards>=13) {
				cxpos_extracards = 0;
				cypos_extracards++;
			}
		}
		dstxpos = dstcxpos*(d->w+REKO_BORDER);
		dstypos = dstcypos*(d->h+REKO_BORDER);
		if(canvas) {
			de_bitmap_copy_rect(cardimg, canvas, 0, 0, d->w, d->h, dstxpos, dstypos, 0);
		}
		else {
			set_reko_card_filename(c, d->idx_of_first_main_card, cardidx, fi);
			de_bitmap_write_to_file_finfo(cardimg, fi, 0);
		}

		if(!d->fatalerrflag && (thiscardpos+full_cardsize > c->infile->len)) {
			de_err(c, "Premature end of file");
			d->fatalerrflag = 1;
			// (But keep going, so we draw the full image template.)
		}

		if(d->fatalerrflag && !canvas) goto done;

		de_dbg_indent(c, -1);
	}

	if(canvas) {
		de_bitmap_write_to_file(canvas, NULL, 0);
	}

done:
	de_bitmap_destroy(cardimg);
	de_bitmap_destroy(canvas);
	de_finfo_destroy(c, fi);
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x14(%rbp), %rsi
callq 0x15b580
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movq -0x10(%rbp), %rcx
addq 0x58(%rcx), %rax
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rdx
leaq 0x7baa3(%rip), %rsi     # 0x1d1526
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
callq 0x15d230
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rax
cmpb $0x0, 0x1(%rax)
je 0x155ada
movq -0x10(%rbp), %rax
cmpl $0x8, 0x38(%rax)
je 0x155ad8
movq -0x10(%rbp), %rax
cmpl $0x10, 0x38(%rax)
je 0x155ad8
movq -0x10(%rbp), %rax
movb $0x1, 0x2(%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x3(%rax)
jmp 0x156162
jmp 0x155ada
movq -0x10(%rbp), %rax
cmpq $0x1, 0x48(%rax)
jl 0x155b1f
movq -0x10(%rbp), %rax
cmpq $0x50, 0x48(%rax)
jg 0x155b1f
movq -0x10(%rbp), %rax
cmpq $0x40, 0x18(%rax)
jl 0x155b1f
movq -0x10(%rbp), %rax
cmpq $0x64, 0x18(%rax)
jg 0x155b1f
movq -0x10(%rbp), %rax
cmpq $0x64, 0x20(%rax)
jl 0x155b1f
movq -0x10(%rbp), %rax
cmpq $0x96, 0x20(%rax)
jle 0x155b34
movq -0x10(%rbp), %rax
movb $0x1, 0x2(%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x3(%rax)
jmp 0x156162
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x68(%rcx), %rcx
addq $0x34, %rcx
cmpq %rcx, %rax
jge 0x155b73
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rdx
addq $0x34, %rdx
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rcx
leaq 0x7b9f0(%rip), %rsi     # 0x1d155c
movb $0x0, %al
callq 0x15bc10
movq -0x10(%rbp), %rax
cmpb $0x0, 0x1(%rax)
je 0x155ba4
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x10(%rbp), %rcx
imulq 0x20(%rcx), %rax
movq -0x10(%rbp), %rcx
movl 0x38(%rcx), %ecx
shrl $0x3, %ecx
movl %ecx, %ecx
imulq %rcx, %rax
movq %rax, -0x28(%rbp)
jmp 0x155c10
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl $0x8, %ecx
cqto
idivq %rcx
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0xa0(%rbp), %rcx
movq -0x10(%rbp), %rax
movl 0x38(%rax), %eax
imulq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x10(%rbp), %rcx
imulq 0x20(%rcx), %rax
movq -0x10(%rbp), %rcx
movl 0x38(%rcx), %ecx
imulq %rcx, %rax
movl $0x8, %ecx
cqto
idivq %rcx
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
cmpq -0x28(%rbp), %rax
je 0x155c4e
movq -0x10(%rbp), %rax
cmpb $0x0, 0x4(%rax)
jne 0x155c4e
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdx
movq -0x28(%rbp), %rcx
leaq 0x7b949(%rip), %rsi     # 0x1d1588
movb $0x0, %al
callq 0x15bc10
movq -0x10(%rbp), %rax
movb $0x1, 0x4(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x60(%rax)
movq -0x10(%rbp), %rax
cmpb $0x0, 0x1(%rax)
je 0x155c84
movq $0x4, -0x58(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x8, 0x38(%rax)
jne 0x155c82
movq -0x10(%rbp), %rax
movq $0x200, 0x60(%rax)      # imm = 0x200
jmp 0x155c84
movq -0x58(%rbp), %rax
movq -0x10(%rbp), %rcx
addq 0x60(%rcx), %rax
movq -0x10(%rbp), %rcx
addq 0x10(%rcx), %rax
movq %rax, -0x30(%rbp)
movq $0xd, -0x60(%rbp)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
addq $0xc, %rax
movl $0xd, %ecx
cqto
idivq %rcx
movq %rax, -0x68(%rbp)
cmpq $0x5, -0x68(%rbp)
jge 0x155ccd
movq $0x5, -0x68(%rbp)
movq $0x0, -0x80(%rbp)
movq $0x0, -0x88(%rbp)
movq $0x0, -0x90(%rbp)
movq $0x4, -0x98(%rbp)
movq -0x60(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x18(%rcx), %rcx
addq $0x2, %rcx
imulq %rcx, %rax
subq $0x2, %rax
movq %rax, -0x70(%rbp)
movq -0x68(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x20(%rcx), %rcx
addq $0x2, %rcx
imulq %rcx, %rax
subq $0x2, %rax
movq %rax, -0x78(%rbp)
movq -0x10(%rbp), %rax
cmpb $0x0, 0x5(%rax)
je 0x155d52
movq -0x8(%rbp), %rdi
movq -0x70(%rbp), %rsi
movq -0x78(%rbp), %rdx
movl $0x4, %ecx
callq 0x16b750
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdx
movl $0x3, %ecx
callq 0x16b750
movq %rax, -0x38(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x48(%rcx), %rax
jge 0x15614a
movq -0x50(%rbp), %rax
movq -0x20(%rbp), %rcx
imulq -0x30(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0xd0(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rdx
movq -0xd0(%rbp), %rcx
leaq 0x7b778(%rip), %rsi     # 0x1d1533
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x10(%rbp), %rax
movzbl 0x1(%rax), %eax
cmpl $0x0, %eax
je 0x155e92
movq -0x10(%rbp), %rax
cmpb $0x0, 0x2(%rax)
jne 0x155e92
movq -0xd0(%rbp), %rax
addq -0x58(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jg 0x155e92
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0xd0(%rbp), %rsi
callq 0x165280
movq %rax, -0xd8(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0xd0(%rbp), %rsi
addq $0x2, %rsi
callq 0x165280
movq %rax, -0xe0(%rbp)
movq -0xd8(%rbp), %rax
addq $0x1, %rax
movq -0x10(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jne 0x155e70
movq -0xe0(%rbp), %rax
addq $0x1, %rax
movq -0x10(%rbp), %rcx
cmpq 0x20(%rcx), %rax
je 0x155e90
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movl %eax, %edx
leaq 0x7b72e(%rip), %rsi     # 0x1d15af
movb $0x0, %al
callq 0x15bfb0
movq -0x10(%rbp), %rax
movb $0x1, 0x2(%rax)
jmp 0x155e92
movq -0x38(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rcx
movq -0x10(%rbp), %rax
movq 0x20(%rax), %r8
xorl %eax, %eax
movl %eax, %edx
movl $0xff000000, %r9d       # imm = 0xFF000000
xorl %eax, %eax
movq %rdx, %rsi
movl $0x0, (%rsp)
callq 0x16cdc0
movq -0x10(%rbp), %rax
cmpb $0x0, 0x2(%rax)
je 0x155ecd
jmp 0x155f37
movq -0x10(%rbp), %rax
cmpb $0x0, 0x1(%rax)
je 0x155f1d
movq -0x10(%rbp), %rax
cmpl $0x8, 0x38(%rax)
jne 0x155eff
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0xd0(%rbp), %rcx
addq -0x58(%rbp), %rcx
callq 0x156280
jmp 0x155f1b
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0xd0(%rbp), %rcx
addq -0x58(%rbp), %rcx
callq 0x1563e0
jmp 0x155f35
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0xd0(%rbp), %rcx
callq 0x1564a0
jmp 0x155f37
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rdx
xorl %eax, %eax
cmpq 0x68(%rdx), %rcx
movb %al, -0xe1(%rbp)
jl 0x155f69
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x68(%rcx), %rcx
addq $0x34, %rcx
cmpq %rcx, %rax
setl %al
movb %al, -0xe1(%rbp)
movb -0xe1(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movb %al, -0xa1(%rbp)
cmpb $0x0, -0xa1(%rbp)
je 0x155fd1
movq -0x80(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0x88(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x88(%rbp)
cmpq $0x4, -0x88(%rbp)
jl 0x155fcf
movq $0x0, -0x88(%rbp)
movq -0x80(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
jmp 0x156028
movq -0x90(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0x90(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x90(%rbp)
cmpq $0xd, -0x90(%rbp)
jl 0x156026
movq $0x0, -0x90(%rbp)
movq -0x98(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x98(%rbp)
jmp 0x156028
movq -0xb0(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x18(%rcx), %rcx
addq $0x2, %rcx
imulq %rcx, %rax
movq %rax, -0xc0(%rbp)
movq -0xb8(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x20(%rcx), %rcx
addq $0x2, %rcx
imulq %rcx, %rax
movq %rax, -0xc8(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x1560b0
movq -0x38(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %r8
movq -0x10(%rbp), %rax
movq 0x20(%rax), %r9
movq -0xc0(%rbp), %r10
movq -0xc8(%rbp), %rax
xorl %ecx, %ecx
xorl %edx, %edx
movq %rcx, %rdx
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
movl $0x0, 0x10(%rsp)
callq 0x16ce50
jmp 0x1560d8
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rsi
movq -0x20(%rbp), %rdx
movq -0x48(%rbp), %rcx
callq 0x1561a0
movq -0x38(%rbp), %rdi
movq -0x48(%rbp), %rsi
xorl %edx, %edx
callq 0x16afa0
movq -0x10(%rbp), %rax
cmpb $0x0, 0x2(%rax)
jne 0x156115
movq -0xd0(%rbp), %rax
addq -0x30(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jle 0x156115
movq -0x8(%rbp), %rdi
leaq 0x7b4c3(%rip), %rsi     # 0x1d15c9
movb $0x0, %al
callq 0x15bfb0
movq -0x10(%rbp), %rax
movb $0x1, 0x2(%rax)
movq -0x10(%rbp), %rax
movzbl 0x2(%rax), %eax
cmpl $0x0, %eax
je 0x15612b
cmpq $0x0, -0x40(%rbp)
jne 0x15612b
jmp 0x156162
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x155d7c
cmpq $0x0, -0x40(%rbp)
je 0x156160
movq -0x40(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x16afd0
jmp 0x156162
movq -0x38(%rbp), %rdi
callq 0x16af60
movq -0x40(%rbp), %rdi
callq 0x16af60
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x15d260
movq -0x8(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x15b5a0
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nopw %cs:(%rax,%rax)