// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/reko.c
// Function: de_run_wizsolitaire
static void de_run_wizsolitaire(deark *c, de_module_params *mparams)
{
	i64 cardidx = 0;
	u8 errflag = 0;
	u8 need_errmsg = 0;
	i64 pos;
	de_finfo *fi = NULL;
	int saved_indent_level;

	de_dbg_indent_save(c, &saved_indent_level);
	pos = 20;
	fi = de_finfo_create(c);
	while(1) {
		i64 extralen;
		i64 imglen;
		u8 cardval, cardsuit;

		if(cardidx>72) {
			errflag = 1;
			need_errmsg = 1;
			goto done;
		}
		if(pos+18 > c->infile->len) goto done;
		de_dbg(c, "card #%"I64_FMT" at %"I64_FMT, cardidx, pos);
		de_dbg_indent(c, 1);

		// Card headers start with 00 00 00 00, except the 54th which starts
		// with 04 00 00 00 00 00 00 00.
		// I don't know how to interpret this. But the 04 does not seem to be the
		// number of extra cards, so I'm assuming it's the number of extra bytes
		// in the header.
		extralen = de_getu32le_p(&pos);
		de_dbg(c, "len1: %"I64_FMT, extralen);
		pos += extralen;
		pos += 8;
		cardval = de_getbyte_p(&pos);
		cardsuit = de_getbyte_p(&pos);
		imglen = de_getu32le_p(&pos);
		de_dbg(c, "img len: %"I64_FMT, imglen);
		if(imglen==0) {
			de_dbg(c, "[eof marker]");
			goto done;
		}

		de_dbg(c, "suit %u card %u", (UI)cardsuit, (UI)cardval);

		if(imglen<32 || pos+imglen>c->infile->len) {
			errflag = 1;
			need_errmsg = 1;
			goto done;
		}
		if(dbuf_memcmp(c->infile, pos, (const void*)"\xff\xd8\xff", 3)) {
			de_err(c, "Expected image not found at %"I64_FMT, pos);
			errflag = 1;
			goto done;
		}
		set_wizsol_card_filename(c, cardidx, cardval, cardsuit, fi);
		dbuf_create_file_from_slice(c->infile, pos, imglen, "jpg", fi, 0);
		pos += imglen;
		de_dbg_indent(c, -1);
		cardidx++;
	}

done:
	de_dbg_indent_restore(c, saved_indent_level);
	if(!errflag) {
		de_dbg(c, "number of cards: %"I64_FMT, cardidx);
		if(cardidx<53 && !errflag) {
			de_warn(c, "Expected at least 53 cards, found %"I64_FMT, cardidx);
		}
	}
	if(need_errmsg) {
		de_err(c, "Bad or unsupported file");
	}
	de_finfo_destroy(c, fi);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %rbx
leaq 0x1c(%rsp), %rsi
callq 0xe958c
movq %rsp, %rax
movq $0x14, (%rax)
movq %rbx, %rdi
callq 0xea9a9
movq %rax, 0x20(%rsp)
xorl %r12d, %r12d
xorl %r14d, %r14d
xorl %ebp, %ebp
cmpq $0x48, %rbp
jle 0xe6126
xorl %r15d, %r15d
movb $0x1, %r12b
movb $0x1, %r14b
jmp 0xe6254
movq (%rsp), %rcx
leaq 0x12(%rcx), %rax
movq 0x18(%rbx), %rdx
cmpq 0x18(%rdx), %rax
jle 0xe6140
xorl %r15d, %r15d
jmp 0xe6254
movq %r14, 0x28(%rsp)
movq %rbx, %rdi
leaq 0x5f451(%rip), %rsi     # 0x1455a0
movq %rbp, 0x8(%rsp)
movq %rbp, %rdx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x18(%rbx), %rdi
movq %rsp, %r15
movq %r15, %rsi
callq 0xef3a0
movq %rax, %r14
movq %rbx, %rdi
leaq 0x3f6d3(%rip), %rsi     # 0x12585a
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq (%rsp), %rax
addq %r14, %rax
addq $0x8, %rax
movq %rax, (%rsp)
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef14d
movl %eax, %ebp
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef14d
movl %eax, %r14d
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef3a0
movq %rax, %r13
movq %rbx, %rdi
leaq 0x5f4b3(%rip), %rsi     # 0x145689
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
testq %r13, %r13
je 0xe6236
movq %r12, 0x10(%rsp)
movzbl %r14b, %edx
movzbl %bpl, %r12d
xorl %r15d, %r15d
movq %rbx, %rdi
leaq 0x5f4a4(%rip), %rsi     # 0x1456a3
movq %rdx, 0x40(%rsp)
movl %r12d, %ecx
xorl %eax, %eax
callq 0xe9226
cmpq $0x20, %r13
jl 0xe6229
movq (%rsp), %rsi
leaq (%rsi,%r13), %rax
movq 0x18(%rbx), %rdi
cmpq 0x18(%rdi), %rax
jle 0xe6262
xorl %r15d, %r15d
movb $0x1, %r12b
movb $0x1, %r14b
movq 0x8(%rsp), %rbp
jmp 0xe6254
xorl %r15d, %r15d
movq %rbx, %rdi
leaq 0x5f453(%rip), %rsi     # 0x145696
xorl %eax, %eax
callq 0xe9226
movq 0x8(%rsp), %rbp
movq 0x28(%rsp), %r14
testb %r15b, %r15b
jne 0xe6112
jmp 0xe636e
movl $0x3, %ecx
leaq 0x2e28b(%rip), %rdx     # 0x1144f9
callq 0xf025e
testl %eax, %eax
je 0xe6299
movq (%rsp), %rdx
xorl %r15d, %r15d
movq %rbx, %rdi
leaq 0x5f42b(%rip), %rsi     # 0x1456b3
xorl %eax, %eax
callq 0xe9df7
movb $0x1, %r14b
movq 0x10(%rsp), %r12
jmp 0xe622f
decb %bpl
cmpb $0xd, %bpl
setae %al
cmpb $0x4, %r14b
setae %cl
orb %al, %cl
jne 0xe62e9
decl %r12d
leaq 0x5f2f9(%rip), %rax     # 0x1455b1
movb (%r12,%rax), %al
movb %al, 0x30(%rsp)
leaq 0x5f2f8(%rip), %rax     # 0x1455bf
movq 0x40(%rsp), %rcx
movb (%rcx,%rax), %al
movb %al, 0x31(%rsp)
movb $0x0, 0x32(%rsp)
movq 0x10(%rsp), %r12
movq 0x8(%rsp), %rbp
leaq 0x30(%rsp), %r15
jmp 0xe631a
movq 0x8(%rsp), %rbp
testq %rbp, %rbp
leaq 0x30(%rsp), %r15
movl $0x10, %edx
movq %r15, %rdi
je 0xe6309
leaq 0x65fa6(%rip), %rsi     # 0x14c2ad
jmp 0xe6310
leaq 0x5f2b4(%rip), %rsi     # 0x1455c4
callq 0xe8ded
movq 0x10(%rsp), %r12
movq %rbx, %rdi
movq 0x20(%rsp), %r14
movq %r14, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
movl $0x5, %r8d
callq 0xeac7c
movq 0x18(%rbx), %rdi
movq (%rsp), %rsi
movq %r13, %rdx
leaq 0x4d173(%rip), %rcx     # 0x1334ba
movq %r14, %r8
xorl %r9d, %r9d
callq 0xf0321
addq %r13, (%rsp)
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
incq %rbp
movb $0x1, %r15b
jmp 0xe624f
movl 0x1c(%rsp), %esi
movq %rbx, %rdi
callq 0xe9595
testb %r14b, %r14b
jne 0xe63ad
leaq 0x5f34d(%rip), %rsi     # 0x1456d3
movq %rbx, %rdi
movq %rbp, %rdx
xorl %eax, %eax
callq 0xe9226
cmpq $0x34, %rbp
jg 0xe63ad
leaq 0x5f348(%rip), %rsi     # 0x1456e8
movq %rbx, %rdi
movq %rbp, %rdx
xorl %eax, %eax
callq 0xe9b27
testb %r12b, %r12b
je 0xe63c3
leaq 0x32ab6(%rip), %rsi     # 0x118e6f
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
callq 0xea9b3
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq