// File: /workspace/github_c_makefile_star2_O3/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 $0x38, %rsp
movq %rdi, %rbx
leaq 0x14(%rsp), %rsi
callq 0xe8a1c
leaq 0x8(%rsp), %rax
movq $0x14, (%rax)
movq %rbx, %rdi
callq 0xea504
movq %rax, 0x30(%rsp)
movq $0x0, 0x18(%rsp)
movq 0x8(%rsp), %rcx
leaq 0x12(%rcx), %rax
movq 0x18(%rbx), %rdx
cmpq 0x18(%rdx), %rax
movq 0x18(%rsp), %r15
jg 0xe5822
movq %rbx, %rdi
leaq 0x60450(%rip), %rsi     # 0x145a80
movq %r15, %rdx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%rbx), %rdi
leaq 0x8(%rsp), %r12
movq %r12, %rsi
callq 0xeed83
movq %rax, %r14
movq %rbx, %rdi
leaq 0x40425(%rip), %rsi     # 0x125a8a
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x8(%rsp), %rax
addq %r14, %rax
addq $0x8, %rax
movq %rax, 0x8(%rsp)
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xeeb4b
movl %eax, %ebp
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xeeb4b
movl %eax, %r14d
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xeed83
movq %rax, %r13
movq %rbx, %rdi
leaq 0x604b3(%rip), %rsi     # 0x145b69
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
testq %r13, %r13
je 0xe5811
movzbl %r14b, %r12d
movzbl %bpl, %r15d
movq %rbx, %rdi
leaq 0x604a8(%rip), %rsi     # 0x145b83
movl %r12d, %edx
movl %r15d, %ecx
xorl %eax, %eax
callq 0xe8617
cmpq $0x20, %r13
jl 0xe57f2
movq 0x8(%rsp), %rsi
leaq (%rsi,%r13), %rax
movq 0x18(%rbx), %rdi
cmpq 0x18(%rdi), %rax
jg 0xe57f2
movl $0x3, %ecx
leaq 0x2ee94(%rip), %rdx     # 0x1145a9
callq 0xefc85
testl %eax, %eax
jne 0xe585e
decb %bpl
cmpb $0xd, %bpl
setae %al
cmpb $0x4, %r14b
setae %cl
orb %al, %cl
jne 0xe5769
decl %r15d
leaq 0x60350(%rip), %rax     # 0x145a91
movb (%r15,%rax), %al
movb %al, 0x20(%rsp)
leaq 0x6034f(%rip), %rax     # 0x145a9f
movb (%r12,%rax), %al
movb %al, 0x21(%rsp)
movb $0x0, 0x22(%rsp)
movq 0x18(%rsp), %r15
leaq 0x20(%rsp), %r12
jmp 0xe5795
movq 0x18(%rsp), %r15
movl $0x10, %edx
leaq 0x20(%rsp), %r12
movq %r12, %rdi
testq %r15, %r15
je 0xe5789
leaq 0x67146(%rip), %rsi     # 0x14c8cd
jmp 0xe5790
leaq 0x60314(%rip), %rsi     # 0x145aa4
callq 0xe8219
movq %rbx, %rdi
movq 0x30(%rsp), %r14
movq %r14, %rsi
movq %r12, %rdx
xorl %ecx, %ecx
movl $0x5, %r8d
callq 0xea78f
movq 0x18(%rbx), %rdi
movq 0x8(%rsp), %rsi
movq %r13, %rdx
leaq 0x4e0ef(%rip), %rcx     # 0x1338b2
movq %r14, %r8
xorl %r9d, %r9d
callq 0xefd48
addq %r13, 0x8(%rsp)
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
incq %r15
movq %r15, 0x18(%rsp)
cmpq $0x49, %r15
jne 0xe560a
movl 0x14(%rsp), %esi
movq %rbx, %rdi
callq 0xe8a25
leaq 0x3378a(%rip), %rsi     # 0x118f8f
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
jmp 0xe5880
leaq 0x6035e(%rip), %rsi     # 0x145b76
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movl 0x14(%rsp), %esi
movq %rbx, %rdi
callq 0xe8a25
leaq 0x6037e(%rip), %rsi     # 0x145bb3
movq %rbx, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0xe8617
cmpq $0x34, %r15
ja 0xe5880
leaq 0x60379(%rip), %rsi     # 0x145bc8
movq %rbx, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0xe8faa
jmp 0xe5880
movq 0x8(%rsp), %rdx
leaq 0x60329(%rip), %rsi     # 0x145b93
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
movl 0x14(%rsp), %esi
movq %rbx, %rdi
callq 0xe8a25
movq %rbx, %rdi
movq 0x30(%rsp), %rsi
callq 0xea50e
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq