// File: /workspace/github_c_makefile_star2_O3/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 0xe8b2b
movq %rax, %r14
movq $0x0, 0x10(%rsp)
movq 0x18(%rbx), %rdi
callq 0x3bfb3
movl %eax, (%r14)
movl %eax, %eax
leaq 0xddd9c(%rip), %rcx     # 0x11959c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0xdde01(%rip), %rsi     # 0x119611
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
jmp 0x3bf83
leaq 0xdddbe(%rip), %rsi     # 0x1195e4
leaq 0xdddb3(%rip), %rax     # 0x1195e0
jmp 0x3b84d
leaq 0xdddc9(%rip), %rsi     # 0x1195ff
leaq 0xdddbe(%rip), %rax     # 0x1195fb
jmp 0x3b84d
leaq 0xddd91(%rip), %rsi     # 0x1195d7
leaq 0xddd86(%rip), %rax     # 0x1195d3
movq %rax, 0x10(%r14)
movq %rbx, %rdi
callq 0xeb125
movq 0x10(%r14), %rdx
leaq 0x1007c0(%rip), %rsi    # 0x13c024
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq $0x3, 0x10(%rsp)
cmpl $0x3, (%r14)
jne 0x3b88e
leaq 0xdddbb(%rip), %rsi     # 0x11963f
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8faa
movq %rbx, %rdi
xorl %esi, %esi
movl $0x9, %edx
callq 0xeb220
movl %eax, 0x8(%r14)
movl (%r14), %ecx
cmpl $0x3, %ecx
jne 0x3b8b3
movl $0x3, 0x4(%r14)
jmp 0x3b8c5
movl %eax, 0x4(%r14)
cmpl $0x2, %ecx
jne 0x3b8c5
movq $0x7, 0x10(%rsp)
movq 0x18(%rbx), %rdi
leaq 0x10(%rsp), %r15
movq %r15, %rsi
callq 0xeebeb
movq %rax, 0x18(%r14)
leaq 0xddd96(%rip), %rsi     # 0x119677
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq (%r15), %rbp
movq 0x18(%r14), %rax
imulq $0x34, %rax, %rcx
addq %rbp, %rcx
movq %rcx, 0x20(%r14)
testq %rax, %rax
jle 0x3bf83
xorl %r12d, %r12d
cmpb $0x0, 0x30(%r14)
jne 0x3bf83
leaq 0x34(%rbp), %rax
movq 0x18(%rbx), %rcx
cmpq 0x18(%rcx), %rax
jg 0x3bf83
movq %rbx, %rdi
leaq 0x3c(%rsp), %rsi
callq 0xe8a1c
movl $0x28, %esi
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, %r13
movq %r12, (%rax)
movq %rbx, %rdi
leaq 0xddd39(%rip), %rsi     # 0x11968b
movl %r12d, %edx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq %rbx, %rdi
leaq 0xfe272(%rip), %rsi     # 0x139be5
movq %rbp, %rdx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%rbx), %rdi
leaq 0x6(%rbp), %rsi
callq 0xeed61
movq %rax, 0x8(%r13)
movq %rbx, %rdi
leaq 0xddcef(%rip), %rsi     # 0x119694
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
callq 0xf6410
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 0x3b9ee
xorl %r8d, %r8d
movl $0x3, %r9d
callq 0xefbd7
movq 0x20(%r13), %rdi
callq 0xf6461
jmp 0x3b9f9
movl $0x1, %r8d
callq 0xefbd7
movq 0x20(%r13), %rdi
callq 0xf70f6
movq %rbx, %rdi
leaq 0xddc95(%rip), %rsi     # 0x1196a1
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x8(%r13), %rax
cmpq 0x20(%r14), %rax
jl 0x3ba33
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %r15
subq %rax, %r15
jl 0x3ba33
cmpq 0x28(%r14), %rax
jge 0x3ba8d
movb $0x1, 0x30(%r14)
movl (%r13), %edx
movq %rbx, %rdi
leaq 0xddc7c(%rip), %rsi     # 0x1196c2
xorl %eax, %eax
callq 0xe9295
movq 0x20(%r13), %rdi
callq 0xf6753
movq %rbx, %rdi
movq %r13, %rsi
callq 0xe8be2
movl 0x3c(%rsp), %esi
movq %rbx, %rdi
callq 0xe8a25
movq 0x10(%rsp), %rbp
addq $0x34, %rbp
movq %rbp, 0x10(%rsp)
incq %r12
cmpq 0x18(%r14), %r12
jl 0x3b90b
jmp 0x3bf83
movq %rax, 0x28(%r14)
movq 0x18(%r14), %rax
decq %rax
cmpq %rax, (%r13)
jge 0x3babf
addq $0x3a, %rbp
movq %rbp, %rsi
callq 0xeed61
movq %rax, %rcx
subq 0x8(%r13), %rcx
jle 0x3babf
movq 0x18(%rbx), %rdx
cmpq 0x18(%rdx), %rax
cmovlq %rcx, %r15
movq %rbx, %rdi
leaq 0xddbe9(%rip), %rsi     # 0x1196b2
movq %r15, %rdx
xorl %eax, %eax
callq 0xe87e4
cmpq $0x2, %r15
jl 0x3ba38
addq 0x8(%r13), %r15
movq %r15, 0x10(%r13)
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movq 0x8(%r13), %rdx
movq %rbx, %rdi
leaq 0xddc76(%rip), %rsi     # 0x119776
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
cmpl $0x2, (%r14)
jne 0x3bc15
movq 0x8(%r13), %rax
movq %rax, 0x18(%rsp)
movq 0x18(%rbx), %rdi
leaq 0x18(%rsp), %rsi
callq 0xeebeb
movq %rax, %r15
movq %rbx, %rdi
leaq 0x109a52(%rip), %rsi    # 0x145594
movl %r15d, %edx
xorl %eax, %eax
callq 0xe8617
testl %r15d, %r15d
je 0x3bf03
movq 0x18(%rsp), %rax
movq %rax, 0x20(%rsp)
movq %rax, (%rsp)
movq 0x18(%rbx), %rdi
movq %rsp, %r15
movq %r15, %rsi
callq 0xeed83
movq %rbx, %rdi
leaq 0xddbe3(%rip), %rsi     # 0x11975f
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movl $0x70, %esi
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, %rbp
leaq 0xe1753(%rip), %rax     # 0x11d2ef
movq %rax, (%rbp)
movl 0x8(%r14), %eax
movl %eax, 0xc(%rbp)
movq (%rsp), %rdx
movq %rbx, %rdi
leaq 0xddbbc(%rip), %rsi     # 0x119771
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%rbx), %rcx
movq (%rsp), %r8
movq 0x10(%r13), %r9
subq %r8, %r9
addq $-0x10, %r9
movq %rbx, %rdi
leaq 0xddb9c(%rip), %rsi     # 0x119782
movq %rbp, %rdx
callq 0xe9aa3
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
testb $0x1, 0x40(%rbp)
jne 0x3be16
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xe8be2
jmp 0x3bf4a
movq 0x18(%rbx), %rdi
movq 0x8(%r13), %rsi
callq 0xeebc8
movq %rax, %r15
movq %rbx, %rdi
leaq 0xddbac(%rip), %rsi     # 0x1197db
movl %r15d, %edx
xorl %eax, %eax
callq 0xe8617
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 0x3bf37
addq %rcx, %rax
addq $0x2, %rax
movq %rax, 0x20(%rsp)
movq 0x18(%rbx), %rdi
callq 0xeebc8
movq %rax, %rbp
movq %r15, 0x8(%rsp)
movl %ebp, %r15d
notl %r15d
movq %rbx, %rdi
leaq 0xdda9b(%rip), %rsi     # 0x11972e
movl %ebp, %edx
xorl %eax, %eax
callq 0xe8617
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 0x3bf37
testq %rdx, %rdx
je 0x3bdfa
movq %r8, 0x8(%rsp)
movq %rbx, %rdi
callq 0xea504
movq %rax, %r15
cmpb $0x0, 0xd0(%rbx)
je 0x3bd0e
movq 0x20(%r13), %rdx
movq %rbx, %rdi
movq %r15, %rsi
xorl %ecx, %ecx
callq 0xea53f
movq %r15, 0x30(%rsp)
movq 0x18(%rbx), %rdi
movq 0x8(%r13), %rsi
addq $0x2, %rsi
callq 0xeebc8
movq %rax, %rbp
movq 0x18(%rbx), %rdi
movq 0x8(%r13), %rsi
addq $0x4, %rsi
callq 0xeebc8
movq %rax, %r15
movq %rbx, %rdi
leaq 0xfe005(%rip), %rsi     # 0x139d4a
movl %ebp, %edx
movl %r15d, %ecx
xorl %eax, %eax
callq 0xe8617
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 0x3bf37
testq %rbp, %rbp
sete %al
testq %rdx, %rdx
sete %cl
orb %al, %cl
jne 0x3bf37
movq %rbx, %rdi
movq %rbp, %rsi
movq %rdx, 0x40(%rsp)
movl $0x1, %ecx
callq 0xf266a
movq %rax, 0x28(%rsp)
movq 0x18(%rbx), %rdi
movq 0x8(%r13), %rsi
addq $0xa, %rsi
movq %rbp, %rdx
movq 0x40(%rsp), %rcx
movq %r15, %r8
xorl %r9d, %r9d
pushq $0x0
movq 0x38(%rsp), %r15
pushq %r15
callq 0xf2b5b
addq $0x10, %rsp
movq 0x28(%rsp), %rdi
callq 0xf228d
movq %rbx, %rdi
movq %r15, %rsi
callq 0xea50e
movq 0x8(%rsp), %r8
testq %r8, %r8
je 0x3ba4d
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rdx
movq 0x20(%rsp), %rcx
jmp 0x3bf79
movq (%rsp), %rdx
addq 0x58(%rbp), %rdx
movq %rdx, (%rsp)
movq %rbx, %rdi
leaq 0xdd95b(%rip), %rsi     # 0x119787
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeebeb
movq %r15, %rsi
movq %rax, %r15
movq 0x18(%rbx), %rdi
callq 0xeebeb
movq %rbx, %rdi
leaq 0xdd948(%rip), %rsi     # 0x1197a0
movl %r15d, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq %rsp, %r15
movq %r15, %rsi
callq 0xeecce
movq %rax, 0x8(%rsp)
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeecce
movq %rax, 0x30(%rsp)
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeebeb
movq %rax, 0x28(%rsp)
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeebeb
movq %rbx, %rdi
leaq 0xdd909(%rip), %rsi     # 0x1197b9
movq 0x8(%rsp), %rdx
movq 0x30(%rsp), %rcx
movq 0x28(%rsp), %r8
movl %eax, %r9d
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeebeb
movq %rbx, %rdi
leaq 0xdd8ec(%rip), %rsi     # 0x1197cb
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq (%rsp), %r15
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xe8be2
subq 0x20(%rsp), %r15
jle 0x3bf4a
addq %r15, 0x18(%rsp)
movq 0x18(%rbx), %rdi
leaq 0x18(%rsp), %rsi
callq 0xeebeb
movq %rax, %r15
movq %rbx, %rdi
leaq 0xdd810(%rip), %rsi     # 0x11972e
movl %r15d, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rsp), %rcx
leaq (%rcx,%r15), %rax
cmpq 0x10(%r13), %rax
jle 0x3bf64
movl (%r13), %edx
movq %rbx, %rdi
leaq 0xdd7f9(%rip), %rsi     # 0x11973e
jmp 0x3ba46
movl (%r13), %edx
movq %rbx, %rdi
leaq 0xdd785(%rip), %rsi     # 0x1196dd
xorl %eax, %eax
callq 0xe8faa
jmp 0x3ba4d
testq %r15, %r15
je 0x3ba4d
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %r15, %r8
callq 0x3c028
jmp 0x3ba4d
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe8be2
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq