// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/cardfile.c
// Function: do_card
static void do_card(deark *c, lctx *d, i64 cardnum, i64 pos)
{
	int saved_indent_level;
	i64 avail_data;
	u8 bad_datapos_flag = 0;
	struct page_ctx *pg = NULL;

	de_dbg_indent_save(c, &saved_indent_level);

	pg = de_malloc(c, sizeof(struct page_ctx));
	pg->cardnum = cardnum;
	de_dbg(c, "card #%d", (int)pg->cardnum);
	de_dbg_indent(c, 1);
	de_dbg(c, "index at %"I64_FMT, pos);
	de_dbg_indent(c, 1);
	pg->datapos = de_getu32le(pos+6);
	de_dbg(c, "datapos: %"I64_FMT, pg->datapos);

	pg->name = ucstring_create(c);
	if(d->crd_encoding==DE_ENCODING_UTF16LE) {
		dbuf_read_to_ucstring(c->infile, pos+11, 40, pg->name, 0,
			d->crd_encoding);
		ucstring_truncate_at_NUL(pg->name);
	}
	else {
		dbuf_read_to_ucstring(c->infile, pos+11, 40, pg->name, DE_CONVFLAG_STOP_AT_NUL,
			d->crd_encoding);
	}
	de_dbg(c, "index text: \"%s\"", ucstring_getpsz_d(pg->name));

	// We're strict about the data items appearing in the same order as the
	// index items. In practice, they always seem to. There are a number of
	// seemingly malformed files out there, and this helps to detect if we've
	// gone off the rails.
	if(pg->datapos<d->data_area_start || pg->datapos>c->infile->len ||
		pg->datapos<d->prev_datapos)
	{
		bad_datapos_flag = 1;
		d->fatalerrflag = 1;
		goto done;
	}

	d->prev_datapos = pg->datapos;

	avail_data = c->infile->len - pg->datapos; // default
	if(pg->cardnum<(d->numcards-1)) { // if this is NOT the last card
		i64 next_card_dpos;

		// peek at the next card's data pointer
		next_card_dpos = de_getu32le(pos+CRD_INDEX_ITEM_LEN+6);
		if(next_card_dpos>pg->datapos && next_card_dpos<c->infile->len) {
			avail_data = next_card_dpos - pg->datapos;
		}
	}

	de_dbg2(c, "avail data: %"I64_FMT, avail_data);

	if(avail_data<2) {
		bad_datapos_flag = 1;
		goto done;
	}

	pg->data_endpos = pg->datapos + avail_data;

	de_dbg_indent(c, -1);

	de_dbg(c, "data at %"I64_FMT, pg->datapos);
	de_dbg_indent(c, 1);

	if(d->fmt==DE_CRDFMT_RRG) {
		do_carddata_rrg(c, d, pg);
	}
	else {
		do_carddata_mgc(c, d, pg);
	}

done:
	if(bad_datapos_flag) {
		de_err(c, "card #%d: Bad data pointer", (int)pg->cardnum);
	}
	if(pg) {
		ucstring_destroy(pg->name);
		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)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movb $0x0, -0x31(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x24(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rdi
movl $0x28, %esi
callq 0x15b780
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rax
movq (%rax), %rax
movl %eax, %edx
leaq 0x154dbe(%rip), %rsi    # 0x1a5310
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rdx
leaq 0x174ecf(%rip), %rsi    # 0x1c5445
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
addq $0x6, %rsi
callq 0x165540
movq %rax, %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0x154d5b(%rip), %rsi    # 0x1a5319
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x10(%rbp), %rax
cmpl $0x3, 0x4(%rax)
jne 0x5061f
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
addq $0xb, %rsi
movq -0x40(%rbp), %rax
movq 0x20(%rax), %rcx
movq -0x10(%rbp), %rax
movl 0x4(%rax), %r9d
movl $0x28, %edx
xorl %r8d, %r8d
callq 0x1669f0
movq -0x40(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x1712b0
jmp 0x5064f
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
addq $0xb, %rsi
movq -0x40(%rbp), %rax
movq 0x20(%rax), %rcx
movq -0x10(%rbp), %rax
movl 0x4(%rax), %r9d
movl $0x28, %edx
movl $0x1, %r8d
callq 0x1669f0
movq -0x8(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x40(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x172650
movq -0x50(%rbp), %rdi
movq %rax, %rdx
leaq 0x154cb4(%rip), %rsi    # 0x1a5326
movb $0x0, %al
callq 0x15b0d0
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x20(%rcx), %rax
jl 0x506b3
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jg 0x506b3
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x28(%rcx), %rax
jge 0x506c4
movb $0x1, -0x31(%rbp)
movq -0x10(%rbp), %rax
movb $0x1, 0x30(%rax)
jmp 0x507ef
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rax
movq -0x40(%rbp), %rcx
subq 0x8(%rcx), %rax
movq %rax, -0x30(%rbp)
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x18(%rcx), %rcx
subq $0x1, %rcx
cmpq %rcx, %rax
jge 0x50753
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
addq $0x34, %rsi
addq $0x6, %rsi
callq 0x165540
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq -0x40(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jle 0x50751
movq -0x48(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jge 0x50751
movq -0x48(%rbp), %rax
movq -0x40(%rbp), %rcx
subq 0x8(%rcx), %rax
movq %rax, -0x30(%rbp)
jmp 0x50753
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rdx
leaq 0x154bd5(%rip), %rsi    # 0x1a5337
movb $0x0, %al
callq 0x15b350
cmpq $0x2, -0x30(%rbp)
jge 0x50776
movb $0x1, -0x31(%rbp)
jmp 0x507ef
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rcx
addq -0x30(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0x154c50(%rip), %rsi    # 0x1a53fb
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x10(%rbp), %rax
cmpl $0x2, (%rax)
jne 0x507dc
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x40(%rbp), %rdx
callq 0x50850
jmp 0x507ed
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x40(%rbp), %rdx
callq 0x50980
jmp 0x507ef
cmpb $0x0, -0x31(%rbp)
je 0x50810
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rax
movq (%rax), %rax
movl %eax, %edx
leaq 0x154b3e(%rip), %rsi    # 0x1a5347
movb $0x0, %al
callq 0x15bfb0
cmpq $0x0, -0x40(%rbp)
je 0x50831
movq -0x40(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x171750
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x15b820
movq -0x8(%rbp), %rdi
movl -0x24(%rbp), %esi
callq 0x15b5a0
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)