// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/bsave.c
// Function: de_run_bsave
static void de_run_bsave(deark *c, de_module_params *mparams)
{
	const char *fmtname_req;
	const char *s;
	lctx *d;
	struct bsave_id_info idi;

	d = de_malloc(c, sizeof(lctx));
	d->fmt_id = FMT_UNKNOWN;
	identify_bsave_internal(c, &idi);
	if(!idi.might_be_bsave) {
		de_err(c, "Not a BSAVE image file");
		goto done;
	}
	if(idi.probably_not_bsave) {
		if(c->module_disposition==DE_MODDISP_EXPLICIT) {
			de_warn(c, "This is probably not a BSAVE image file");
		}
	}

	d->load_segment = (u32)de_getu16le(1);
	d->load_offset = (u32)de_getu16le(3);
	d->load_addr = d->load_segment*16 + d->load_offset;
	d->data_size = de_getu16le(5);

	de_dbg(c, "segment: 0x%04x", (UI)d->load_segment);
	de_dbg(c, "offset: 0x%04x", (UI)d->load_offset);
	de_dbg(c, "data size: 0x%04x (%d)", (UI)d->data_size, (int)d->data_size);

	if(d->data_size > c->infile->len) {
		de_warn(c, "Data (size %"I64_FMT") goes beyond end of file", d->data_size);
	}

	fmtname_req = de_get_ext_option(c, "bsave:fmt");
	if(fmtname_req) {
		if(!de_strcmp(fmtname_req, "auto")) {
			;
		}
		else {
			d->fmt_id = bsave_fmt_name_to_id(fmtname_req);
			if(d->fmt_id==FMT_UNKNOWN) {
				de_warn(c, "Unknown format name \"%s\"", fmtname_req);
			}
		}
	}

	collect_metrics(c, d);

	read_pcpaint_palinfo(c, d, &idi);

	use_fmt_by_id(c, d, d->fmt_id);

	if(!d->decoder_fn) {
		detect_bsave_fmt(c, d, &idi);
	}

	if(!d->fmt_info || !d->decoder_fn) {
		de_err(c, "Unidentified BSAVE format, try \"-opt bsave:fmt=...\". "
			"Use \"-m bsave -h\" for a list.");
		goto done;
	}

	de_dbg(c, "format name: %s", d->fmt_info->name);
	de_declare_fmtf(c, "BSAVE-PC %s", d->fmt_info->descr);

	if(d->need_id_warning) {
		de_warn(c, "BSAVE formats can't be reliably identified. You may need to "
			"use \"-opt bsave:fmt=...\". Use \"-m bsave -h\" for a list.");
	}

	s = de_get_ext_option(c, "palfile");
	if(!s) s = de_get_ext_option(c, "file2");
	if(s) {
		if(!do_read_palette_file(c, d, s)) goto done;
	}

	(void)d->decoder_fn(c, d);

done:
	de_free(c, d);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
movl $0x468, %esi            # imm = 0x468
callq 0x15b780
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movl $0x0, 0x28(%rax)
movq -0x8(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x4b5f0
cmpb $0x0, -0x38(%rbp)
jne 0x4b237
movq -0x8(%rbp), %rdi
leaq 0x159496(%rip), %rsi    # 0x1a46c1
movb $0x0, %al
callq 0x15bfb0
jmp 0x4b4e8
cmpb $0x0, -0x37(%rbp)
je 0x4b25b
movq -0x8(%rbp), %rax
cmpl $0x2, 0x24(%rax)
jne 0x4b259
movq -0x8(%rbp), %rdi
leaq 0x159486(%rip), %rsi    # 0x1a46d8
movb $0x0, %al
callq 0x15bc10
jmp 0x4b25b
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x1, %esi
callq 0x165280
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x3, %esi
callq 0x165280
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x28(%rbp), %rax
movl (%rax), %ecx
shll $0x4, %ecx
movq -0x28(%rbp), %rax
addl 0x4(%rax), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x5, %esi
callq 0x165280
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movl (%rax), %edx
leaq 0x15942b(%rip), %rsi    # 0x1a4700
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movl 0x4(%rax), %edx
leaq 0x159422(%rip), %rsi    # 0x1a4710
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movl %eax, %edx
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movl %eax, %ecx
leaq 0x15940b(%rip), %rsi    # 0x1a471f
movb $0x0, %al
callq 0x15b0d0
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jle 0x4b34b
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rdx
leaq 0x1593f2(%rip), %rsi    # 0x1a4736
movb $0x0, %al
callq 0x15bc10
movq -0x8(%rbp), %rdi
leaq 0x159408(%rip), %rsi    # 0x1a475e
callq 0x15cb30
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x4b3b3
movq -0x18(%rbp), %rdi
leaq 0x151202(%rip), %rsi    # 0x19c573
callq 0x191a0
cmpl $0x0, %eax
jne 0x4b37d
jmp 0x4b3b1
movq -0x18(%rbp), %rdi
callq 0x4b730
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x28(%rax)
movq -0x28(%rbp), %rax
cmpl $0x0, 0x28(%rax)
jne 0x4b3af
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rdx
leaq 0x1593c0(%rip), %rsi    # 0x1a4768
movb $0x0, %al
callq 0x15bc10
jmp 0x4b3b1
jmp 0x4b3b3
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x4b7c0
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
leaq -0x38(%rbp), %rdx
callq 0x4b980
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x28(%rbp), %rax
movl 0x28(%rax), %edx
callq 0x4ba70
movq -0x28(%rbp), %rax
cmpq $0x0, 0x30(%rax)
jne 0x4b401
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
leaq -0x38(%rbp), %rdx
callq 0x4bb10
movq -0x28(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0x4b417
movq -0x28(%rbp), %rax
cmpq $0x0, 0x30(%rax)
jne 0x4b42e
movq -0x8(%rbp), %rdi
leaq 0x15935f(%rip), %rsi    # 0x1a4781
movb $0x0, %al
callq 0x15bfb0
jmp 0x4b4e8
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x10(%rax), %rdx
leaq 0x15938f(%rip), %rsi    # 0x1a47d4
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x18(%rax), %rdx
leaq 0x159381(%rip), %rsi    # 0x1a47e4
movb $0x0, %al
callq 0x15e3d0
movq -0x28(%rbp), %rax
cmpb $0x0, 0x2e(%rax)
je 0x4b486
movq -0x8(%rbp), %rdi
leaq 0x159371(%rip), %rsi    # 0x1a47f0
movb $0x0, %al
callq 0x15bc10
movq -0x8(%rbp), %rdi
leaq 0x1593d3(%rip), %rsi    # 0x1a4864
callq 0x15cb30
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x4b4b5
movq -0x8(%rbp), %rdi
leaq 0x1510cc(%rip), %rsi    # 0x19c578
callq 0x15cb30
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x4b4d6
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x4bce0
cmpl $0x0, %eax
jne 0x4b4d4
jmp 0x4b4e8
jmp 0x4b4d6
movq -0x28(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq *%rax
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x15b820
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax,%rax)