// File: /workspace/github_c_makefile_star2_O2/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, %r12
leaq 0x1c(%rsp), %rsi
callq 0xdebae
leaq 0x8(%rsp), %rax
movq $0x14, (%rax)
movq %r12, %rdi
callq 0xdfd1a
movq %rax, 0x38(%rsp)
xorl %r14d, %r14d
cmpq $0x49, %r14
movq %r14, 0x10(%rsp)
je 0xdbc3d
movq 0x8(%rsp), %rcx
leaq 0x12(%rcx), %rax
movq 0x18(%r12), %rdx
movb $0x1, %bl
cmpq 0x18(%rdx), %rax
jg 0xdbc56
movq %r12, %rdi
leaq 0x5cbb5(%rip), %rsi     # 0x138630
movq %r14, %rdx
xorl %eax, %eax
callq 0xde896
movq %r12, %rdi
pushq $0x1
popq %rsi
callq 0xdeba7
movq 0x18(%r12), %rdi
leaq 0x8(%rsp), %rbx
movq %rbx, %rsi
callq 0xe40bf
movq %rax, %r14
movq %r12, %rdi
leaq 0x3cddb(%rip), %rsi     # 0x11888a
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movq 0x8(%rsp), %rax
addq %r14, %rax
addq $0x8, %rax
movq %rax, 0x8(%rsp)
movq 0x18(%r12), %rdi
movq %rbx, %rsi
callq 0xe3f2a
movl %eax, %r13d
movq 0x18(%r12), %rdi
movq %rbx, %rsi
callq 0xe3f2a
movl %eax, %r14d
movq 0x18(%r12), %rdi
movq %rbx, %rsi
callq 0xe40bf
movq %rax, %rbp
movq %r12, %rdi
leaq 0x5cc15(%rip), %rsi     # 0x138719
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
testq %rbp, %rbp
je 0xdbc43
movzbl %r14b, %edx
movzbl %r13b, %r15d
xorl %ebx, %ebx
movq %r12, %rdi
leaq 0x5cc08(%rip), %rsi     # 0x138733
movq %rdx, 0x40(%rsp)
movl %r15d, %ecx
xorl %eax, %eax
callq 0xde896
movl $0x0, %eax
cmpq $0x20, %rbp
jl 0xdbc74
movq 0x8(%rsp), %rsi
leaq (%rsi,%rbp), %rax
movq 0x18(%r12), %rdi
cmpq 0x18(%rdi), %rax
jg 0xdbc3d
leaq 0x2b9e1(%rip), %rdx     # 0x107549
pushq $0x3
popq %rcx
callq 0xe4d48
testl %eax, %eax
jne 0xdbc5a
decb %r13b
cmpb $0xd, %r13b
setae %al
cmpb $0x4, %r14b
setae %cl
orb %al, %cl
jne 0xdbbc3
decl %r15d
leaq 0x5caaa(%rip), %rax     # 0x138641
movb (%r15,%rax), %al
movb %al, 0x20(%rsp)
leaq 0x5caa9(%rip), %rax     # 0x13864f
movq 0x40(%rsp), %rcx
movb (%rcx,%rax), %al
movb %al, 0x21(%rsp)
movb $0x0, 0x22(%rsp)
movq 0x10(%rsp), %r14
leaq 0x20(%rsp), %r15
jmp 0xdbbed
movq 0x10(%rsp), %r14
leaq 0x20(%rsp), %r15
movq %r15, %rdi
testq %r14, %r14
je 0xdbbde
leaq 0x637f1(%rip), %rsi     # 0x13f3cd
jmp 0xdbbe5
leaq 0x5ca6f(%rip), %rsi     # 0x138654
pushq $0x10
popq %rdx
callq 0xde492
movq %r12, %rdi
movq 0x38(%rsp), %rbx
movq %rbx, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
pushq $0x5
popq %r8
callq 0xdffbd
movq 0x18(%r12), %rdi
movq 0x8(%rsp), %rsi
movq %rbp, %rdx
leaq 0x4a9c0(%rip), %rcx     # 0x1265da
movq %rbx, %r8
xorl %r9d, %r9d
callq 0xe4e0b
addq %rbp, 0x8(%rsp)
movq %r12, %rdi
pushq $-0x1
popq %rsi
callq 0xdeba7
incq %r14
jmp 0xdba48
xorl %ebx, %ebx
xorl %eax, %eax
jmp 0xdbc74
leaq 0x5cadc(%rip), %rsi     # 0x138726
movq %r12, %rdi
xorl %eax, %eax
callq 0xde896
movb $0x1, %bl
movb $0x1, %al
jmp 0xdbc74
movq 0x8(%rsp), %rdx
leaq 0x5cadd(%rip), %rsi     # 0x138743
movq %r12, %rdi
xorl %eax, %eax
callq 0xdf2a5
xorl %eax, %eax
movb $0x1, %bl
movl 0x1c(%rsp), %esi
movq %r12, %rdi
movl %eax, %ebp
callq 0xdebb7
testb %bpl, %bpl
movq 0x10(%rsp), %r15
je 0xdbcba
leaq 0x5cad0(%rip), %rsi     # 0x138763
movq %r12, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0xde896
cmpq $0x34, %r15
ja 0xdbcba
leaq 0x5cacb(%rip), %rsi     # 0x138778
movq %r12, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0xdf05a
testb %bl, %bl
jne 0xdbccf
leaq 0x301da(%rip), %rsi     # 0x10be9f
movq %r12, %rdi
xorl %eax, %eax
callq 0xdf2a5
movq %r12, %rdi
movq 0x38(%rsp), %rsi
callq 0xdfd24
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq