// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/cardfile.c
// Function: de_run_cardfile
static void de_run_cardfile(deark *c, de_module_params *mparams)
{
	lctx *d = NULL;
	i64 pos;
	i64 n;

	d = de_malloc(c, sizeof(lctx));

	pos = 0;
	d->fmt = detect_crd_fmt(c);
	if(d->fmt==DE_CRDFMT_MGC) {
		d->signature = "MGC";
		de_declare_fmt(c, "CardFile");
	}
	else if(d->fmt==DE_CRDFMT_RRG) {
		d->signature = "RRG";
		de_declare_fmt(c, "CardFile, with objects");
	}
	else if(d->fmt==DE_CRDFMT_DKO) {
		d->signature = "DKO";
		de_declare_fmt(c, "CardFile, Unicode");
	}
	else {
		de_err(c, "This is not a known/supported CardFile format");
		goto done;
	}
	de_dbg(c, "signature: %s", d->signature);
	pos+=3;

	if(d->fmt==DE_CRDFMT_DKO) {
		// TODO: Samples needed
		de_warn(c, "Unicode Cardfile files might not be supported correctly");
	}

	// Microsoft's (old) Cardfile format documentation says that text is in "low
	// ASCII format", but that seems doubtful on the face of it, and indeed I have
	// seen files where it is not.
	d->ole_encoding = de_get_input_encoding(c, NULL, DE_ENCODING_WINDOWS1252);
	if(d->fmt==DE_CRDFMT_DKO) {
		d->crd_encoding = DE_ENCODING_UTF16LE;
	}
	else {
		d->crd_encoding = d->ole_encoding;
	}

	if(d->fmt==DE_CRDFMT_RRG) {
		pos += 4; // Last object's ID
	}

	d->numcards = de_getu16le_p(&pos);
	de_dbg(c, "number of cards: %d", (int)d->numcards);

	d->data_area_start = pos + CRD_INDEX_ITEM_LEN*d->numcards;

	for(n=0; n<d->numcards; n++) {
		if(d->fatalerrflag) goto done;
		if(pos+CRD_INDEX_ITEM_LEN > c->infile->len) goto done;
		do_card(c, d, n, pos);
		pos += CRD_INDEX_ITEM_LEN;
	}

done:
	de_free(c, d);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %rbx
movl $0x38, %esi
callq 0xe969b
movq %rax, %r14
movq $0x0, 0x10(%rsp)
movq %rbx, %rdi
callq 0x3c130
movl %eax, (%r14)
movl %eax, %eax
leaq 0xddaf5(%rip), %rcx     # 0x11947c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0xddb5a(%rip), %rsi     # 0x1194f1
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
jmp 0x3c104
leaq 0xddb17(%rip), %rsi     # 0x1194c4
leaq 0xddb0c(%rip), %rax     # 0x1194c0
jmp 0x3b9d4
leaq 0xddb22(%rip), %rsi     # 0x1194df
leaq 0xddb17(%rip), %rax     # 0x1194db
jmp 0x3b9d4
leaq 0xddaea(%rip), %rsi     # 0x1194b7
leaq 0xddadf(%rip), %rax     # 0x1194b3
movq %rax, 0x10(%r14)
movq %rbx, %rdi
callq 0xeb6d6
movq 0x10(%r14), %rdx
leaq 0x100299(%rip), %rsi    # 0x13bc84
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
addq $0x3, 0x10(%rsp)
cmpl $0x3, (%r14)
jne 0x3ba12
leaq 0xddb17(%rip), %rsi     # 0x11951f
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9b27
movq %rbx, %rdi
xorl %esi, %esi
movl $0x9, %edx
callq 0xeb7d1
movl %eax, 0x8(%r14)
movl (%r14), %ecx
cmpl $0x3, %ecx
cmovel %ecx, %eax
movl %eax, 0x4(%r14)
cmpl $0x2, %ecx
jne 0x3ba3d
addq $0x4, 0x10(%rsp)
movq 0x18(%rbx), %rdi
leaq 0x10(%rsp), %r15
movq %r15, %rsi
callq 0xef1ed
movq %rax, 0x18(%r14)
leaq 0xddafe(%rip), %rsi     # 0x119557
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%r14), %rax
imulq $0x34, %rax, %rcx
addq (%r15), %rcx
movq %rcx, 0x20(%r14)
testq %rax, %rax
jle 0x3c104
xorl %r12d, %r12d
cmpb $0x0, 0x30(%r14)
jne 0x3c104
movq 0x10(%rsp), %rbp
leaq 0x34(%rbp), %rax
movq 0x18(%rbx), %rcx
cmpq 0x18(%rcx), %rax
jg 0x3c104
movq %rbx, %rdi
leaq 0x44(%rsp), %rsi
callq 0xe958c
movl $0x28, %esi
movq %rbx, %rdi
callq 0xe969b
movq %rax, %r13
movq %r12, (%rax)
movq %rbx, %rdi
leaq 0xdda9f(%rip), %rsi     # 0x11956b
movl %r12d, %edx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq %rbx, %rdi
leaq 0xfdd58(%rip), %rsi     # 0x139845
movq %rbp, %rdx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x18(%rbx), %rdi
leaq 0x6(%rbp), %rsi
callq 0xef37e
movq %rax, 0x8(%r13)
movq %rbx, %rdi
leaq 0xdda55(%rip), %rsi     # 0x119574
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
callq 0xf6870
movq %rax, 0x20(%r13)
movl 0x4(%r14), %r9d
leaq 0xb(%rbp), %rsi
movq 0x18(%rbx), %rdi
movl $0x28, %edx
movq %rax, %rcx
cmpl $0x3, %r9d
jne 0x3bb68
xorl %r8d, %r8d
movl $0x3, %r9d
callq 0xf01b0
movq 0x20(%r13), %rdi
callq 0xf68c1
jmp 0x3bb73
movl $0x1, %r8d
callq 0xf01b0
movq 0x20(%r13), %rdi
callq 0xf7557
movq %rbx, %rdi
leaq 0xdd9fb(%rip), %rsi     # 0x119581
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq 0x8(%r13), %rax
cmpq 0x20(%r14), %rax
jl 0x3bbad
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %r15
subq %rax, %r15
jl 0x3bbad
cmpq 0x28(%r14), %rax
jge 0x3bbff
movb $0x1, 0x30(%r14)
movl (%r13), %edx
movq %rbx, %rdi
leaq 0xdd9e2(%rip), %rsi     # 0x1195a2
xorl %eax, %eax
callq 0xe9df7
movq 0x20(%r13), %rdi
callq 0xf6bb0
movq %rbx, %rdi
movq %r13, %rsi
callq 0xe9752
movl 0x44(%rsp), %esi
movq %rbx, %rdi
callq 0xe9595
addq $0x34, 0x10(%rsp)
incq %r12
cmpq 0x18(%r14), %r12
jl 0x3ba80
jmp 0x3c104
movq %rax, 0x28(%r14)
movq 0x18(%r14), %rax
decq %rax
cmpq %rax, (%r13)
jge 0x3bc31
addq $0x3a, %rbp
movq %rbp, %rsi
callq 0xef37e
movq %rax, %rcx
subq 0x8(%r13), %rcx
jle 0x3bc31
movq 0x18(%rbx), %rdx
cmpq 0x18(%rdx), %rax
cmovlq %rcx, %r15
movq %rbx, %rdi
leaq 0xdd957(%rip), %rsi     # 0x119592
movq %r15, %rdx
xorl %eax, %eax
callq 0xe93cf
cmpq $0x2, %r15
jl 0x3bbb2
addq 0x8(%r13), %r15
movq %r15, 0x10(%r13)
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
movq 0x8(%r13), %rdx
movq %rbx, %rdi
leaq 0xdd9e4(%rip), %rsi     # 0x119656
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
cmpl $0x2, (%r14)
jne 0x3be5c
movq 0x8(%r13), %rax
movq %rax, 0x18(%rsp)
movq 0x18(%rbx), %rdi
leaq 0x18(%rsp), %rsi
callq 0xef1ed
movq %rax, %r15
movq %rbx, %rdi
leaq 0x109400(%rip), %rsi    # 0x1450b4
movl %r15d, %edx
xorl %eax, %eax
callq 0xe9226
testl %r15d, %r15d
je 0x3c09e
movq 0x18(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %rax, (%rsp)
movq 0x18(%rbx), %rdi
movq %rsp, %r15
movq %r15, %rsi
callq 0xef3a0
movq %rbx, %rdi
leaq 0xdd951(%rip), %rsi     # 0x11963f
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movl $0x70, %esi
movq %rbx, %rdi
callq 0xe969b
movq %rax, %rbp
leaq 0xe14d1(%rip), %rax     # 0x11d1df
movq %rax, (%rbp)
movl 0x8(%r14), %eax
movl %eax, 0xc(%rbp)
movq (%rsp), %rdx
movq %rbx, %rdi
leaq 0xdd92a(%rip), %rsi     # 0x119651
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x18(%rbx), %rcx
movq (%rsp), %r8
movq 0x10(%r13), %r9
subq %r8, %r9
addq $-0x10, %r9
movq %rbx, %rdi
leaq 0xdd90a(%rip), %rsi     # 0x119662
movq %rbp, %rdx
callq 0xea5fa
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
movl 0x40(%rbp), %eax
movl %eax, 0x20(%rsp)
testb $0x1, %al
je 0x3c05d
movq (%rsp), %rdx
addq 0x58(%rbp), %rdx
movq %rdx, (%rsp)
movq %rbx, %rdi
leaq 0xdd8d5(%rip), %rsi     # 0x119667
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef1ed
movq %r15, %rsi
movq %rax, %r15
movq 0x18(%rbx), %rdi
callq 0xef1ed
movq %rbx, %rdi
leaq 0xdd8c2(%rip), %rsi     # 0x119680
movl %r15d, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %rsp, %r15
movq %r15, %rsi
callq 0xef2dd
movq %rax, 0x38(%rsp)
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef2dd
movq %rax, 0x30(%rsp)
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef1ed
movq %rax, 0x28(%rsp)
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef1ed
movq %rbx, %rdi
leaq 0xdd883(%rip), %rsi     # 0x119699
movq 0x38(%rsp), %rdx
movq 0x30(%rsp), %rcx
movq 0x28(%rsp), %r8
movl %eax, %r9d
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef1ed
movq %rbx, %rdi
leaq 0xdd866(%rip), %rsi     # 0x1196ab
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq (%rsp), %r15
subq 0x8(%rsp), %r15
jmp 0x3c060
movq 0x18(%rbx), %rdi
movq 0x8(%r13), %rsi
callq 0xef1ca
movq %rax, %r15
movq %rbx, %rdi
leaq 0xdd845(%rip), %rsi     # 0x1196bb
movl %r15d, %edx
xorl %eax, %eax
callq 0xe9226
leaq 0xa(%r15), %rax
testq %r15, %r15
movl $0x2, %ecx
cmoveq %rcx, %rax
movq 0x8(%r13), %rcx
movq 0x10(%r13), %rdx
leaq (%rcx,%rax), %rsi
movq %rsi, 0x18(%r13)
addq $-0x2, %rdx
cmpq %rdx, %rsi
jg 0x3c0d2
addq %rcx, %rax
addq $0x2, %rax
movq %rax, 0x20(%rsp)
movq 0x18(%rbx), %rdi
callq 0xef1ca
movq %rax, %rbp
movq %r15, 0x8(%rsp)
movl %ebp, %r15d
notl %r15d
movq %rbx, %rdi
leaq 0xdd734(%rip), %rsi     # 0x11960e
movl %ebp, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x20(%rsp), %rsi
leaq (%rsi,%rbp), %rax
movq 0x10(%r13), %rcx
andl $0xff00, %r15d          # imm = 0xFF00
movzbl %bpl, %r8d
leaq (%r8,%rsi), %rdx
xorq %rcx, %rdx
cmpq %rcx, %rax
cmovleq %rbp, %r8
orq %rdx, %r15
movq 0x8(%rsp), %rdx
cmovneq %rbp, %r8
leaq (%r8,%rsi), %rax
cmpq %rcx, %rax
jg 0x3c0d2
testq %rdx, %rdx
je 0x3c041
movq %r8, 0x8(%rsp)
movq %rbx, %rdi
callq 0xea9a9
movq %rax, %r15
cmpb $0x0, 0xd0(%rbx)
je 0x3bf55
movq 0x20(%r13), %rdx
movq %rbx, %rdi
movq %r15, %rsi
xorl %ecx, %ecx
callq 0xea9e4
movq %r15, 0x38(%rsp)
movq 0x18(%rbx), %rdi
movq 0x8(%r13), %rsi
addq $0x2, %rsi
callq 0xef1ca
movq %rax, %rbp
movq 0x18(%rbx), %rdi
movq 0x8(%r13), %rsi
addq $0x4, %rsi
callq 0xef1ca
movq %rax, %r15
movq %rbx, %rdi
leaq 0xfda1e(%rip), %rsi     # 0x1399aa
movl %ebp, %edx
movl %r15d, %ecx
xorl %eax, %eax
callq 0xe9226
movq %r15, %rdx
leaq 0xf(%rbp), %rax
leaq 0x1e(%rbp), %r15
testq %rax, %rax
cmovnsq %rax, %r15
sarq $0x4, %r15
addq %r15, %r15
movq 0x8(%r13), %rax
movq %r15, %rcx
imulq %rdx, %rcx
addq %rcx, %rax
addq $0xa, %rax
cmpq 0x18(%r13), %rax
jg 0x3c0d2
testq %rbp, %rbp
sete %al
testq %rdx, %rdx
sete %cl
orb %al, %cl
jne 0x3c0d2
movq %rbx, %rdi
movq %rbp, %rsi
movq %rdx, 0x28(%rsp)
movl $0x1, %ecx
callq 0xf2cab
movq %rax, 0x30(%rsp)
movq 0x18(%rbx), %rdi
movq 0x8(%r13), %rsi
addq $0xa, %rsi
movq %rbp, %rdx
movq 0x28(%rsp), %rcx
movq %r15, %r8
xorl %r9d, %r9d
pushq $0x0
movq 0x40(%rsp), %r15
pushq %r15
callq 0xf319c
addq $0x10, %rsp
movq 0x30(%rsp), %rdi
callq 0xf28d4
movq %rbx, %rdi
movq %r15, %rsi
callq 0xea9b3
movq 0x8(%rsp), %r8
testq %r8, %r8
je 0x3bbc7
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rdx
movq 0x20(%rsp), %rcx
jmp 0x3c0fa
xorl %r15d, %r15d
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xe9752
testq %r15, %r15
setg %bpl
andb 0x20(%rsp), %bpl
je 0x3c080
addq %r15, 0x18(%rsp)
jmp 0x3c095
movl (%r13), %edx
movq %rbx, %rdi
leaq 0xdd52f(%rip), %rsi     # 0x1195bd
xorl %eax, %eax
callq 0xe9b27
testb %bpl, %bpl
je 0x3bbc7
movq 0x18(%rbx), %rdi
leaq 0x18(%rsp), %rsi
callq 0xef1ed
movq %rax, %r15
movq %rbx, %rdi
leaq 0xdd555(%rip), %rsi     # 0x11960e
movl %r15d, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rsp), %rcx
leaq (%rcx,%r15), %rax
cmpq 0x10(%r13), %rax
jle 0x3c0e5
movl (%r13), %edx
movq %rbx, %rdi
leaq 0xdd53e(%rip), %rsi     # 0x11961e
jmp 0x3bbc0
testq %r15, %r15
je 0x3bbc7
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %r15, %r8
callq 0x3c1a9
jmp 0x3bbc7
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe9752
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq