// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdi, %rbx
movl $0x468, %esi            # imm = 0x468
callq 0xe969b
movq %rax, %r14
movl $0x0, 0x28(%rax)
leaq 0x48(%rsp), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x38f1f
cmpb $0x0, (%r15)
je 0x388de
cmpb $0x0, 0x49(%rsp)
je 0x38604
cmpl $0x2, 0x24(%rbx)
jne 0x38604
leaq 0xe031e(%rip), %rsi     # 0x118918
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9b27
movq 0x18(%rbx), %rdi
movl $0x1, %esi
callq 0xef1ca
movl %eax, (%r14)
movq 0x18(%rbx), %rdi
movl $0x3, %esi
callq 0xef1ca
movl %eax, 0x4(%r14)
movl (%r14), %ecx
shll $0x4, %ecx
addl %eax, %ecx
movl %ecx, 0x8(%r14)
movq 0x18(%rbx), %rdi
movl $0x5, %esi
callq 0xef1ca
movq %rax, 0x10(%r14)
movl (%r14), %edx
leaq 0xe02f1(%rip), %rsi     # 0x118940
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movl 0x4(%r14), %edx
leaq 0xe02ec(%rip), %rsi     # 0x118950
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movl 0x10(%r14), %ecx
leaq 0xe02e6(%rip), %rsi     # 0x11895f
movq %rbx, %rdi
movl %ecx, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x10(%r14), %rdx
movq 0x18(%rbx), %rax
cmpq 0x18(%rax), %rdx
jle 0x386a4
leaq 0xe02dc(%rip), %rsi     # 0x118976
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9b27
leaq 0xe02f3(%rip), %rsi     # 0x11899e
movq %rbx, %rdi
callq 0xea6cb
testq %rax, %rax
je 0x3871f
movq %rax, %r15
leaq 0xd7ec9(%rip), %rsi     # 0x11058b
movq %rax, %rdi
callq 0x181a0
testl %eax, %eax
je 0x3871f
movl $0x10, %r12d
leaq 0x146e15(%rip), %r13    # 0x17f4f0
movq (%r12,%r13), %rsi
movq %r15, %rdi
callq 0x181a0
testl %eax, %eax
jne 0x386f0
movl -0x10(%r12,%r13), %ebp
testl %eax, %eax
je 0x38703
addq $0x20, %r12
cmpq $0x1b0, %r12            # imm = 0x1B0
jne 0x386db
xorl %ebp, %ebp
movl %ebp, 0x28(%r14)
testl %ebp, %ebp
jne 0x3871f
leaq 0xe0296(%rip), %rsi     # 0x1189a8
movq %rbx, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0xe9b27
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rax
subq 0x10(%r14), %rax
addq $-0x7, %rax
movq %rax, 0x458(%r14)
movl $0x7, %esi
callq 0xef1ca
movq %rax, 0x440(%r14)
movq 0x18(%rbx), %rdi
movl $0x9, %esi
callq 0xef1ca
movq %rax, 0x448(%r14)
movq 0x440(%r14), %rax
leaq 0x7(%rax), %rcx
addq $0xe, %rax
testq %rcx, %rcx
cmovnsq %rcx, %rax
sarq $0x3, %rax
movq %rax, 0x450(%r14)
cmpq $0x0, 0x458(%r14)
js 0x387d0
movq 0x18(%rbx), %rdi
movq 0x10(%r14), %rsi
addq $0x7, %rsi
callq 0xef0d5
cmpb $0x1a, %al
jne 0x387d0
movq 0x18(%rbx), %rdi
movq 0x10(%r14), %rsi
leaq 0x8(%rsi), %rax
xorl %ebp, %ebp
cmpq %rax, 0x18(%rdi)
sete %bpl
addq $0x6, %rsi
callq 0xef0d5
xorl %ecx, %ecx
cmpb $0x1a, %al
sete %cl
addl %ebp, %ebp
subl %ecx, %ebp
addl $0x2, %ebp
movl %ebp, 0x460(%r14)
movq 0x458(%r14), %rdx
leaq 0xe04cb(%rip), %rsi     # 0x118ca9
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe93cf
movb 0x4a(%rsp), %bpl
testb %bpl, %bpl
je 0x38880
movq 0x50(%rsp), %rdx
movq %rsp, %r15
movq %rdx, (%r15)
movl $0x1, 0x18(%r14)
leaq 0xe04b9(%rip), %rsi     # 0x118cc9
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
addq $0xc, (%r15)
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef14d
movb %al, 0x1c(%r14)
movzbl %al, %edx
leaq 0xde89c(%rip), %rsi     # 0x1170e1
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef14d
movb %al, 0x1d(%r14)
movzbl %al, %edx
leaq 0xe047e(%rip), %rsi     # 0x118ce7
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
movl 0x28(%r14), %esi
movq %r14, %rdi
callq 0x38fdd
cmpq $0x0, 0x30(%r14)
jne 0x38cf1
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
movaps %xmm0, (%rsp)
movq $0x0, 0x30(%rsp)
movb $0x1, 0x2e(%r14)
testb %bpl, %bpl
je 0x388ea
movl $0x3, 0x28(%r14)
leaq 0xe042e(%rip), %rsi     # 0x118cf8
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9ef5
movb $0x0, 0x2e(%r14)
jmp 0x38ce5
leaq 0xe001c(%rip), %rsi     # 0x118901
jmp 0x38e34
movl 0x8(%r14), %eax
cmpl $0xb8000, %eax          # imm = 0xB8000
jne 0x3892d
movq 0x10(%r14), %rcx
leaq -0x3fff(%rcx), %rdx
cmpq $0x2, %rdx
jae 0x38912
movl $0x8, %ecx
movl $0xa, %edx
jmp 0x38925
cmpq $0x3e80, %rcx           # imm = 0x3E80
jne 0x3892d
movl $0x3, %ecx
movl $0x4, %edx
movl %edx, 0xc(%rsp)
movl %ecx, 0x8(%rsp)
cmpl $0xa0000, %eax          # imm = 0xA0000
jne 0x38943
movl $0xa, %ecx
cmpq $0xfa00, 0x10(%r14)     # imm = 0xFA00
je 0x38972
movq 0x10(%r14), %rdx
movl $0x4, %ecx
cmpq $0xfa00, %rdx           # imm = 0xFA00
je 0x38972
cmpl $0xa0000, %eax          # imm = 0xA0000
setne %sil
cmpq $0xfde8, %rdx           # imm = 0xFDE8
setne %dl
movl $0x6, %ecx
orb %sil, %dl
jne 0x38976
movl %ecx, 0x14(%rsp)
movq 0x440(%r14), %rdx
testq %rdx, %rdx
je 0x38ac8
movq 0x448(%r14), %rcx
testq %rcx, %rcx
je 0x389fb
movq 0x450(%r14), %rdi
imulq %rcx, %rdi
movq 0x10(%r14), %rsi
subq %rdi, %rsi
addq $0xb, %rdi
movq 0x18(%rbx), %r8
cmpq 0x18(%r8), %rdi
setg %dil
addq $-0x4, %rsi
sets %r8b
orb %dil, %r8b
jne 0x389fb
movl $0x7, %edi
cmpq $0x2, %rsi
jl 0x389e7
movl $0x6, %edi
cmpq $0x9, %rsi
jl 0x389e7
xorl %edi, %edi
cmpq $0x81, %rsi
setl %dil
leal (%rdi,%rdi,2), %edi
leal 0x2(%rdi), %esi
testb $0x1, %dl
jne 0x389f7
movl %esi, 0x1c(%rsp)
incl %edi
movl %edi, %esi
movl %esi, 0x18(%rsp)
testq %rcx, %rcx
sete %sil
testb $0x7, %dl
setne %dl
orb %sil, %dl
jne 0x38a66
movq 0x450(%r14), %rsi
imulq %rcx, %rsi
movq 0x10(%r14), %rdx
subq %rsi, %rdx
addq $0xb, %rsi
movq 0x18(%rbx), %rdi
cmpq 0x18(%rdi), %rsi
setg %sil
addq $-0x4, %rdx
sets %dil
orb %sil, %dil
jne 0x38a66
movl $0x7, %esi
cmpq $0x2, %rdx
jl 0x38a62
movl $0x5, %esi
cmpq $0x9, %rdx
jl 0x38a62
xorl %esi, %esi
cmpq $0x81, %rdx
setl %sil
orl $0x2, %esi
movl %esi, 0x24(%rsp)
testq %rcx, %rcx
je 0x38ac8
imulq 0x450(%r14), %rcx
leaq 0xb(,%rcx,4), %rdx
movq 0x18(%rbx), %rsi
cmpq 0x18(%rsi), %rdx
jg 0x38ac8
leaq (,%rcx,4), %rsi
movq 0x10(%r14), %rdx
subq %rsi, %rdx
addq $-0x4, %rdx
js 0x38ac8
movl $0x8, %esi
cmpq $0x2, %rdx
jl 0x38ac4
movl $0x6, %esi
cmpq $0x11, %rdx
jl 0x38ac4
leaq (%rcx,%rcx,2), %rcx
xorl %esi, %esi
cmpq %rcx, %rdx
setl %sil
leal 0x3(,%rsi,2), %esi
movl %esi, 0x20(%rsp)
cmpl $0xb8000, %eax          # imm = 0xB8000
jne 0x38b17
movq 0x10(%r14), %rdx
movl $0x5, %ecx
cmpq $0xf00, %rdx            # imm = 0xF00
je 0x38b5d
cmpq $0xfa0, %rdx            # imm = 0xFA0
je 0x38b5d
cmpq $0x1000, %rdx           # imm = 0x1000
je 0x38b5d
cmpq $0x1fd0, %rdx           # imm = 0x1FD0
setge %sil
andl $0xfff, %edx            # imm = 0xFFF
cmpl $0xfd0, %edx            # imm = 0xFD0
sete %dl
movl $0x4, %ecx
testb %dl, %sil
jne 0x38b5d
cmpl $0xc000, 0x4(%r14)      # imm = 0xC000
jne 0x38b3e
movq 0x10(%r14), %rcx
cmpq $0x1000, %rcx           # imm = 0x1000
je 0x38b58
cmpq $0x800, %rcx            # imm = 0x800
jne 0x38b3e
movl $0x4, %ecx
jmp 0x38b5d
cmpl $0xb800, (%r14)         # imm = 0xB800
jne 0x38b61
movl $0x4, %ecx
cmpq $0x1000, 0x10(%r14)     # imm = 0x1000
je 0x38b5d
jmp 0x38b61
movl $0x6, %ecx
movl %ecx, 0x4(%rsp)
cmpl $0xb8000, %eax          # imm = 0xB8000
jne 0x38bca
movq 0x10(%r14), %rax
cmpq $0x4000, %rax           # imm = 0x4000
je 0x38b7c
cmpq $0x3e80, %rax           # imm = 0x3E80
jne 0x38bca
leaq 0x58(%rsp), %r15
movl $0x0, 0x8(%r15)
movq $0x0, (%r15)
movq 0x18(%rbx), %rdi
leaq 0x114c(%rip), %rcx      # 0x39ce7
movl $0x7, %esi
movl $0x3e80, %edx           # imm = 0x3E80
movq %r15, %r8
callq 0xefc28
cmpb $0x0, 0x8(%r15)
jne 0x38bca
cmpb $0x0, 0x61(%rsp)
movl $0x5, %eax
movl $0xc, %ecx
cmovel %eax, %ecx
movl %ecx, 0x10(%rsp)
movq 0x10(%r14), %rcx
cmpq $0x50, %rcx
jl 0x38c2a
movabsq $-0x3333333333333333, %rdx # imm = 0xCCCCCCCCCCCCCCCD
movq %rcx, %rax
mulq %rdx
shrq $0x2, %rdx
andq $-0x10, %rdx
leaq (%rdx,%rdx,4), %rdx
movq %rcx, %rax
subq %rdx, %rax
setne %dl
cmpq $0x4000, %rcx           # imm = 0x4000
setne %sil
testb %dl, %sil
jne 0x38c2a
xorl %edx, %edx
cmpq $0x4000, %rcx           # imm = 0x4000
sete %dl
testq %rax, %rax
movl $0x2, %eax
cmovnel %edx, %eax
movl %eax, 0x28(%rsp)
incl %eax
movl %eax, 0x2c(%rsp)
cmpq $0x300, %rcx            # imm = 0x300
je 0x38c57
cmpq $0x1680, %rcx           # imm = 0x1680
jne 0x38c7b
cmpl $0x20000, 0x8(%r14)     # imm = 0x20000
jne 0x38c4e
movl $0x8, 0x30(%rsp)
cmpq $0x300, %rcx            # imm = 0x300
jne 0x38c7b
movl 0x8(%r14), %eax
cmpl $0xb8000, %eax          # imm = 0xB8000
je 0x38c7b
cmpl $0xa0000, %eax          # imm = 0xA0000
je 0x38c7b
movl $0x1, %ecx
testl %eax, %eax
jne 0x38c77
movl $0xd, %ecx
movl %ecx, 0x34(%rsp)
xorl %eax, %eax
xorl %ecx, %ecx
movl (%rsp,%rax,4), %edx
cmpl %ecx, %edx
jle 0x38c8c
movl %eax, 0x28(%r14)
movl %edx, %ecx
incq %rax
cmpq $0xe, %rax
jne 0x38c7f
cmpl $0x2, (%rbx)
jl 0x38ce5
leaq 0x14684f(%rip), %r12    # 0x17f4f0
leaq 0xe0092(%rip), %r15     # 0x118d3a
xorl %r13d, %r13d
movl (%rsp,%r13,4), %ecx
testl %ecx, %ecx
je 0x38cdc
movq %r12, %rax
cmpq $0x1, %r13
je 0x38ccb
movq %r12, %rax
movl 0x20(%rax), %edx
addq $0x20, %rax
cmpq %rdx, %r13
jne 0x38cbf
movq 0x10(%rax), %rdx
movq %rbx, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0xe9226
incq %r13
cmpq $0xe, %r13
jne 0x38cab
movl 0x28(%r14), %esi
movq %r14, %rdi
callq 0x38fdd
movq 0x20(%r14), %rax
testq %rax, %rax
je 0x38e2d
cmpq $0x0, 0x30(%r14)
je 0x38e2d
movq 0x10(%rax), %rdx
leaq 0xdfd00(%rip), %rsi     # 0x118a14
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x20(%r14), %rax
movq 0x18(%rax), %rdx
leaq 0xdfcf7(%rip), %rsi     # 0x118a24
movq %rbx, %rdi
xorl %eax, %eax
callq 0xeb703
cmpb $0x0, 0x2e(%r14)
je 0x38d4f
leaq 0xdfceb(%rip), %rsi     # 0x118a30
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9b27
leaq 0xdfd4e(%rip), %rsi     # 0x118aa4
movq %rbx, %rdi
callq 0xea6cb
movq %rax, %r12
testq %rax, %rax
jne 0x38d78
leaq 0xd7823(%rip), %rsi     # 0x110590
movq %rbx, %rdi
callq 0xea6cb
movq %rax, %r12
testq %r12, %r12
je 0x38e79
leaq 0xdffca(%rip), %rsi     # 0x118d52
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movq %r12, %rsi
callq 0xf1717
movq %rax, %r15
testq %rax, %rax
je 0x38e58
movq %rbx, 0x40(%rsp)
movq %r14, %rbp
addq $0x3c, %rbp
movl $0x7, %r12d
movl $0x3, %ecx
movq %r15, %rdi
movq %rsp, %rsi
movq %r12, %rdx
callq 0xeeee8
movzbl (%rsp), %edi
callq 0xecde3
movzbl %al, %ebx
shll $0x10, %ebx
movzbl 0x1(%rsp), %edi
callq 0xecde3
movzbl %al, %r13d
shll $0x8, %r13d
orl %ebx, %r13d
movzbl 0x2(%rsp), %edi
callq 0xecde3
movzbl %al, %eax
orl %r13d, %eax
orl $0xff000000, %eax       # imm = 0xFF000000
movl %eax, (%rbp)
addq $0x3, %r12
addq $0x4, %rbp
cmpq $0x307, %r12            # imm = 0x307
jne 0x38dbe
movl $0x1, 0x38(%r14)
movq 0x40(%rsp), %rbx
jmp 0x38e6c
leaq 0xdfb8d(%rip), %rsi     # 0x1189c1
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe9752
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xdff0b(%rip), %rsi     # 0x118d6a
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe9df7
movq %r15, %rdi
callq 0xf0b13
testq %r15, %r15
je 0x38e3e
movq %rbx, %rdi
movq %r14, %rsi
callq *0x30(%r14)
jmp 0x38e3e