// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movb $0x0, -0x19(%rbp)
movb $0x0, -0x1a(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x34(%rbp), %rsi
callq 0x15b580
movq $0x14, -0x28(%rbp)
movq -0x8(%rbp), %rdi
callq 0x15d230
movq %rax, -0x30(%rbp)
cmpq $0x48, -0x18(%rbp)
jle 0x154cee
movb $0x1, -0x19(%rbp)
movb $0x1, -0x1a(%rbp)
jmp 0x154ed2
movq -0x28(%rbp), %rax
addq $0x12, %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jle 0x154d09
jmp 0x154ed2
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rdx
movq -0x28(%rbp), %rcx
leaq 0x7c817(%rip), %rsi     # 0x1d1533
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x28(%rbp), %rsi
callq 0x165580
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rdx
leaq 0x5c1a5(%rip), %rsi     # 0x1b0efa
movb $0x0, %al
callq 0x15b0d0
movq -0x40(%rbp), %rax
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x28(%rbp), %rsi
callq 0x165170
movb %al, -0x49(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x28(%rbp), %rsi
callq 0x165170
movb %al, -0x4a(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x28(%rbp), %rsi
callq 0x165580
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rdx
leaq 0x7c85c(%rip), %rsi     # 0x1d161c
movb $0x0, %al
callq 0x15b0d0
cmpq $0x0, -0x48(%rbp)
jne 0x154de5
movq -0x8(%rbp), %rdi
leaq 0x7c850(%rip), %rsi     # 0x1d1629
movb $0x0, %al
callq 0x15b0d0
jmp 0x154ed2
movq -0x8(%rbp), %rdi
movzbl -0x4a(%rbp), %edx
movzbl -0x49(%rbp), %ecx
leaq 0x7c83e(%rip), %rsi     # 0x1d1636
movb $0x0, %al
callq 0x15b0d0
cmpq $0x20, -0x48(%rbp)
jl 0x154e1c
movq -0x28(%rbp), %rax
addq -0x48(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jle 0x154e29
movb $0x1, -0x19(%rbp)
movb $0x1, -0x1a(%rbp)
jmp 0x154ed2
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rsi
leaq 0x4b552(%rip), %rdx     # 0x1a038e
movl $0x3, %ecx
callq 0x166ab0
cmpl $0x0, %eax
je 0x154e67
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rdx
leaq 0x7c7ec(%rip), %rsi     # 0x1d1646
movb $0x0, %al
callq 0x15bfb0
movb $0x1, -0x19(%rbp)
jmp 0x154ed2
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movb -0x49(%rbp), %cl
movb -0x4a(%rbp), %al
movq -0x30(%rbp), %r8
movzbl %cl, %edx
movzbl %al, %ecx
callq 0x1568b0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq -0x30(%rbp), %r8
leaq 0x6a45a(%rip), %rcx     # 0x1bf2f9
xorl %r9d, %r9d
callq 0x166be0
movq -0x48(%rbp), %rax
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
jmp 0x154cda
movq -0x8(%rbp), %rdi
movl -0x34(%rbp), %esi
callq 0x15b5a0
cmpb $0x0, -0x19(%rbp)
jne 0x154f1f
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rdx
leaq 0x7c773(%rip), %rsi     # 0x1d1666
movb $0x0, %al
callq 0x15b0d0
cmpq $0x35, -0x18(%rbp)
jge 0x154f1d
cmpb $0x0, -0x19(%rbp)
jne 0x154f1d
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rdx
leaq 0x7c765(%rip), %rsi     # 0x1d167b
movb $0x0, %al
callq 0x15bc10
jmp 0x154f1f
cmpb $0x0, -0x1a(%rbp)
je 0x154f37
movq -0x8(%rbp), %rdi
leaq 0x4fcff(%rip), %rsi     # 0x1a4c2f
movb $0x0, %al
callq 0x15bfb0
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x15d260
addq $0x50, %rsp
popq %rbp
retq
nopw (%rax,%rax)