// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/bsave.c
// Function: detect_bsave_fmt
static void detect_bsave_fmt(deark *c, lctx *d, struct bsave_id_info *idi)
{
	int confidence[FMT_MAX+1]; // Indexed by fmt_id.
	int best_conf;
	UI k;

	de_zeromem(confidence, sizeof(confidence));
	d->need_id_warning = 1; // default

	if(idi->pcpaint_sig_found) {
		d->fmt_id = FMT_CGA4;
		// Most files with a signature are cga4, but I found a few that are cga2.
		// There are also images that are contrived to work in either mode.
		// I'm guessing that these are the only two possibilities.
		de_info(c, "Note: If the image doesn't look right, try \"-opt bsave:fmt=cga2\".");
		d->need_id_warning = 0;
		goto done;
	}

	// TODO: Better autodetection. This barely does anything.
	detect_cga2_4(c, d, confidence);
	detect_mcga4(c, d, confidence);
	detect_wh2_4(c, d, confidence);
	detect_wh256(c, d, confidence);
	detect_wh16(c, d, confidence);
	detect_char(c, d, confidence);
	detect_cga16(c, d, confidence);
	detect_2_4col(c, d, confidence);
	detect_b265(c, d, confidence);
	detect_pal256(c, d, confidence);

	best_conf = 0;
	for(k=0; k<=FMT_MAX; k++) {
		if(confidence[k] > best_conf) {
			d->fmt_id = k;
			best_conf = confidence[k];
		}
	}

	if(c->debug_level>=2) {
		for(k=0; k<=FMT_MAX; k++) {
			const struct bsave_fmt_arr_item *item;

			if(confidence[k]) {
				item = get_fmt_info_by_id(k);
				de_dbg(c, "possible format: %s, %d", item->name, confidence[k]);
			}
		}
	}

done:
	use_fmt_by_id(c, d, d->fmt_id);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
leaq -0x50(%rbp), %rdi
xorl %esi, %esi
movl $0x38, %edx
callq 0x19140
movq -0x10(%rbp), %rax
movb $0x1, 0x2e(%rax)
movq -0x18(%rbp), %rax
cmpb $0x0, 0x2(%rax)
je 0x4bb70
movq -0x10(%rbp), %rax
movl $0x3, 0x28(%rax)
movq -0x8(%rbp), %rdi
leaq 0x158f5c(%rip), %rsi    # 0x1a4ab8
movb $0x0, %al
callq 0x15c0c0
movq -0x10(%rbp), %rax
movb $0x0, 0x2e(%rax)
jmp 0x4bcb9
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
leaq -0x50(%rbp), %rdx
callq 0x4cf60
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
leaq -0x50(%rbp), %rdx
callq 0x4cfe0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
leaq -0x50(%rbp), %rdx
callq 0x4d060
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
leaq -0x50(%rbp), %rdx
callq 0x4d170
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
leaq -0x50(%rbp), %rdx
callq 0x4d260
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
leaq -0x50(%rbp), %rdx
callq 0x4d350
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
leaq -0x50(%rbp), %rdx
callq 0x4d470
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
leaq -0x50(%rbp), %rdx
callq 0x4d520
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
leaq -0x50(%rbp), %rdx
callq 0x4d5b0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
leaq -0x50(%rbp), %rdx
callq 0x4d5f0
movl $0x0, -0x54(%rbp)
movl $0x0, -0x58(%rbp)
cmpl $0xd, -0x58(%rbp)
ja 0x4bc5b
movl -0x58(%rbp), %eax
movl -0x50(%rbp,%rax,4), %eax
cmpl -0x54(%rbp), %eax
jle 0x4bc4e
movl -0x58(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x28(%rax)
movl -0x58(%rbp), %eax
movl -0x50(%rbp,%rax,4), %eax
movl %eax, -0x54(%rbp)
jmp 0x4bc50
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x4bc28
movq -0x8(%rbp), %rax
cmpl $0x2, (%rax)
jl 0x4bcb7
movl $0x0, -0x58(%rbp)
cmpl $0xd, -0x58(%rbp)
ja 0x4bcb5
movl -0x58(%rbp), %eax
cmpl $0x0, -0x50(%rbp,%rax,4)
je 0x4bca8
movl -0x58(%rbp), %edi
callq 0x4cef0
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rdx
movl -0x58(%rbp), %eax
movl -0x50(%rbp,%rax,4), %ecx
leaq 0x158e59(%rip), %rsi    # 0x1a4afa
movb $0x0, %al
callq 0x15b0d0
jmp 0x4bcaa
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x4bc6b
jmp 0x4bcb7
jmp 0x4bcb9
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x10(%rbp), %rax
movl 0x28(%rax), %edx
callq 0x4ba70
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)