// File: /workspace/github_c_makefile_star2_O3/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 0xe8b2b
movq %rax, %r14
movl $0x0, 0x28(%rax)
leaq 0x48(%rsp), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x38e15
cmpb $0x0, (%r15)
je 0x385fa
cmpb $0x0, 0x49(%rsp)
je 0x384fc
cmpl $0x2, 0x24(%rbx)
jne 0x384fc
leaq 0xe054a(%rip), %rsi     # 0x118a3c
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8faa
movq 0x18(%rbx), %rdi
movl $0x1, %esi
callq 0xeebc8
movl %eax, (%r14)
movq 0x18(%rbx), %rdi
movl $0x3, %esi
callq 0xeebc8
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 0xeebc8
movq %rax, 0x10(%r14)
movl (%r14), %edx
leaq 0xe051d(%rip), %rsi     # 0x118a64
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movl 0x4(%r14), %edx
leaq 0xe0518(%rip), %rsi     # 0x118a74
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movl 0x10(%r14), %ecx
leaq 0xe0512(%rip), %rsi     # 0x118a83
movq %rbx, %rdi
movl %ecx, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x10(%r14), %rdx
movq 0x18(%rbx), %rax
cmpq 0x18(%rax), %rdx
jle 0x3859c
leaq 0xe0508(%rip), %rsi     # 0x118a9a
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8faa
leaq 0xe051f(%rip), %rsi     # 0x118ac2
movq %rbx, %rdi
callq 0xe9b74
testq %rax, %rax
je 0x38627
movq %rax, %r15
leaq 0xd7fbd(%rip), %rsi     # 0x110577
movq %rax, %rdi
callq 0x181a0
testl %eax, %eax
je 0x38627
movl $0x10, %r12d
leaq 0x146fbd(%rip), %r13    # 0x17f590
movq (%r12,%r13), %rsi
movq %r15, %rdi
callq 0x181a0
testl %eax, %eax
je 0x38606
addq $0x20, %r12
cmpq $0x1b0, %r12            # imm = 0x1B0
jne 0x385d3
movl $0x0, 0x28(%r14)
jmp 0x38613
leaq 0xe0424(%rip), %rsi     # 0x118a25
jmp 0x38d3a
movl -0x10(%r12,%r13), %eax
movl %eax, 0x28(%r14)
testl %eax, %eax
jne 0x38627
leaq 0xe04b2(%rip), %rsi     # 0x118acc
movq %rbx, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0xe8faa
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rax
subq 0x10(%r14), %rax
addq $-0x7, %rax
movq %rax, 0x458(%r14)
movl $0x7, %esi
callq 0xeebc8
movq %rax, 0x440(%r14)
movq 0x18(%rbx), %rdi
movl $0x9, %esi
callq 0xeebc8
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 0x386d8
movq 0x18(%rbx), %rdi
movq 0x10(%r14), %rsi
addq $0x7, %rsi
callq 0xeead3
cmpb $0x1a, %al
jne 0x386d8
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 0xeead3
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 0xe06e7(%rip), %rsi     # 0x118dcd
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe87e4
movb 0x4a(%rsp), %bpl
testb %bpl, %bpl
je 0x3878c
movq 0x50(%rsp), %r15
movl $0x1, 0x18(%r14)
leaq 0xe06db(%rip), %rsi     # 0x118ded
movq %rbx, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
addq $0xc, %r15
movq %rsp, %r12
movq %r15, (%r12)
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xeeb4b
movb %al, 0x1c(%r14)
movzbl %al, %edx
leaq 0xdea30(%rip), %rsi     # 0x117181
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xeeb4b
movb %al, 0x1d(%r14)
movzbl %al, %edx
leaq 0xe0696(%rip), %rsi     # 0x118e0b
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movl 0x28(%r14), %esi
movq %r14, %rdi
callq 0x38ed6
cmpq $0x0, 0x30(%r14)
jne 0x38bdb
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 0x387ea
movl $0x3, 0x28(%r14)
leaq 0xe0646(%rip), %rsi     # 0x118e1c
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9393
movb $0x0, 0x2e(%r14)
jmp 0x38bcf
leaq 0x14(%rsp), %rdx
movl 0x8(%r14), %eax
cmpl $0xb8000, %eax          # imm = 0xB8000
jne 0x38829
movq 0x10(%r14), %rcx
leaq -0x3fff(%rcx), %rsi
cmpq $0x2, %rsi
jb 0x38862
cmpq $0xfa00, %rcx           # imm = 0xFA00
je 0x3885b
cmpq $0x3e80, %rcx           # imm = 0x3E80
jne 0x38876
movl $0x3, %esi
movl $0x4, %edi
jmp 0x3886c
movq 0x10(%r14), %rcx
cmpl $0xa0000, %eax          # imm = 0xA0000
jne 0x3884d
cmpq $0xfa00, %rcx           # imm = 0xFA00
jne 0x38a21
movl $0xfa00, %ecx           # imm = 0xFA00
movl $0xa, %esi
jmp 0x38874
cmpq $0xfa00, %rcx           # imm = 0xFA00
jne 0x38876
movl $0xfa00, %ecx           # imm = 0xFA00
movl $0x4, %esi
jmp 0x38874
movl $0x8, %esi
movl $0xa, %edi
leaq 0x8(%rsp), %rdx
movl %edi, 0x4(%rdx)
movl %esi, (%rdx)
movq 0x440(%r14), %rdi
testq %rdi, %rdi
je 0x3897a
movq 0x448(%r14), %rdx
testq %rdx, %rdx
je 0x3897a
imulq 0x450(%r14), %rdx
leaq 0xb(%rdx), %r8
movq 0x18(%rbx), %rsi
movq 0x18(%rsi), %rsi
cmpq %rsi, %r8
jg 0x3892b
movq %rcx, %r8
subq %rdx, %r8
addq $-0x4, %r8
js 0x3892b
movl $0x7, %r9d
cmpq $0x2, %r8
jb 0x388e5
movl $0x6, %r9d
cmpq $0x9, %r8
jb 0x388e5
xorl %r9d, %r9d
cmpq $0x81, %r8
setb %r9b
leal (%r9,%r9,2), %r9d
leal 0x2(%r9), %r10d
testb $0x1, %dil
jne 0x388fa
movl %r10d, 0x1c(%rsp)
incl %r9d
movl %r9d, %r10d
movl %r10d, 0x18(%rsp)
testb $0x7, %dil
jne 0x3892b
movl $0x7, %edi
cmpq $0x2, %r8
jb 0x38927
movl $0x5, %edi
cmpq $0x9, %r8
jb 0x38927
xorl %edi, %edi
cmpq $0x81, %r8
adcl $0x2, %edi
movl %edi, 0x24(%rsp)
leaq 0xb(,%rdx,4), %rdi
cmpq %rsi, %rdi
jg 0x3897a
leaq (,%rdx,4), %rdi
movq %rcx, %rsi
subq %rdi, %rsi
addq $-0x4, %rsi
js 0x3897a
movl $0x8, %edi
cmpq $0x2, %rsi
jb 0x38976
movl $0x6, %edi
cmpq $0x11, %rsi
jb 0x38976
leaq (%rdx,%rdx,2), %rdx
xorl %edi, %edi
cmpq %rdx, %rsi
setl %dil
leal 0x3(,%rdi,2), %edi
movl %edi, 0x20(%rsp)
cmpl $0xb8000, %eax          # imm = 0xB8000
jne 0x389d4
movl $0x5, %edx
cmpq $0xf00, %rcx            # imm = 0xF00
je 0x38a42
cmpq $0xfa0, %rcx            # imm = 0xFA0
je 0x38a42
cmpq $0x1000, %rcx           # imm = 0x1000
je 0x38a42
cmpq $0x1fd0, %rcx           # imm = 0x1FD0
setge %sil
movl %ecx, %edx
andl $0xfff, %edx            # imm = 0xFFF
cmpl $0xfd0, %edx            # imm = 0xFD0
sete %dil
movl $0x4, %edx
testb %dil, %sil
jne 0x38a42
cmpl $0xc000, 0x4(%r14)      # imm = 0xC000
jne 0x389f7
cmpq $0x1000, %rcx           # imm = 0x1000
je 0x38a3d
cmpq $0x800, %rcx            # imm = 0x800
jne 0x389f7
movl $0x4, %edx
jmp 0x38a42
cmpl $0xb800, (%r14)         # imm = 0xB800
sete %dl
cmpq $0x1000, %rcx           # imm = 0x1000
sete %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0x38a46
movl $0x4, %edx
movl $0x1000, %ecx           # imm = 0x1000
jmp 0x38a42
cmpq $0xfde8, %rcx           # imm = 0xFDE8
jne 0x38876
movl $0xfde8, %ecx           # imm = 0xFDE8
movl $0x6, %esi
jmp 0x38874
movl $0x6, %edx
movl %edx, 0x4(%rsp)
cmpl $0xb8000, %eax          # imm = 0xB8000
jne 0x38aad
cmpq $0x4000, %rcx           # imm = 0x4000
je 0x38a5f
cmpq $0x3e80, %rcx           # imm = 0x3E80
jne 0x38aad
leaq 0x58(%rsp), %r15
movl $0x0, 0x8(%r15)
movq $0x0, (%r15)
movq 0x18(%rbx), %rdi
leaq 0x1165(%rip), %rcx      # 0x39be3
movl $0x7, %esi
movl $0x3e80, %edx           # imm = 0x3E80
movq %r15, %r8
callq 0xef5e4
cmpb $0x0, 0x8(%r15)
jne 0x38aad
cmpb $0x0, 0x61(%rsp)
movl $0x5, %eax
movl $0xc, %ecx
cmovel %eax, %ecx
movl %ecx, 0x10(%rsp)
movq 0x10(%r14), %rax
cmpq $0x50, %rax
jl 0x38b65
movabsq $-0x3333333333333333, %rcx # imm = 0xCCCCCCCCCCCCCCCD
imulq %rax, %rcx
rorq $0x4, %rcx
movabsq $0x333333333333334, %rdx # imm = 0x333333333333334
cmpq %rdx, %rcx
jb 0x38b07
cmpq $0x300, %rax            # imm = 0x300
je 0x38b3c
cmpq $0x1680, %rax           # imm = 0x1680
je 0x38b26
cmpq $0x4000, %rax           # imm = 0x4000
jne 0x38b65
leaq 0x2c(%rsp), %rax
movl $0x1, -0x4(%rax)
movl $0x2, %ecx
jmp 0x38b63
movabsq $0x300000002, %rcx      # imm = 0x300000002
movq %rcx, 0x28(%rsp)
cmpq $0x300, %rax            # imm = 0x300
je 0x38b3c
cmpq $0x1680, %rax           # imm = 0x1680
jne 0x38b65
cmpl $0x20000, 0x8(%r14)     # imm = 0x20000
jne 0x38b65
leaq 0x30(%rsp), %rax
movl $0x8, %ecx
jmp 0x38b63
movl 0x8(%r14), %edx
cmpl $0xb8000, %edx          # imm = 0xB8000
je 0x38b65
cmpl $0xa0000, %edx          # imm = 0xA0000
je 0x38b65
leaq 0x34(%rsp), %rax
movl $0x1, %ecx
testl %edx, %edx
jne 0x38b63
movl $0xd, %ecx
movl %ecx, (%rax)
xorl %eax, %eax
xorl %ecx, %ecx
movl (%rsp,%rax,4), %edx
cmpl %ecx, %edx
jle 0x38b76
movl %eax, 0x28(%r14)
movl %edx, %ecx
incq %rax
cmpq $0xe, %rax
jne 0x38b69
cmpl $0x2, (%rbx)
jl 0x38bcf
leaq 0x146a05(%rip), %r12    # 0x17f590
leaq 0xe02cc(%rip), %r15     # 0x118e5e
xorl %r13d, %r13d
movl (%rsp,%r13,4), %ecx
testl %ecx, %ecx
je 0x38bc6
movq %r12, %rax
cmpq $0x1, %r13
je 0x38bb5
movq %r12, %rax
movl 0x20(%rax), %edx
addq $0x20, %rax
cmpq %rdx, %r13
jne 0x38ba9
movq 0x10(%rax), %rdx
movq %rbx, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0xe8617
incq %r13
cmpq $0xe, %r13
jne 0x38b95
movl 0x28(%r14), %esi
movq %r14, %rdi
callq 0x38ed6
movq 0x20(%r14), %rax
testq %rax, %rax
je 0x38d33
cmpq $0x0, 0x30(%r14)
je 0x38d33
movq 0x10(%rax), %rdx
leaq 0xdff3a(%rip), %rsi     # 0x118b38
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x20(%r14), %rax
movq 0x18(%rax), %rdx
leaq 0xdff31(%rip), %rsi     # 0x118b48
movq %rbx, %rdi
xorl %eax, %eax
callq 0xeb152
cmpb $0x0, 0x2e(%r14)
je 0x38c39
leaq 0xdff25(%rip), %rsi     # 0x118b54
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8faa
leaq 0xdff88(%rip), %rsi     # 0x118bc8
movq %rbx, %rdi
callq 0xe9b74
testq %rax, %rax
jne 0x38c65
leaq 0xd7928(%rip), %rsi     # 0x11057c
movq %rbx, %rdi
callq 0xe9b74
testq %rax, %rax
je 0x38d27
movq %rax, %r12
leaq 0xe0207(%rip), %rsi     # 0x118e76
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movq %r12, %rsi
callq 0xf1114
testq %rax, %rax
je 0x38d5e
movq %rax, %r15
movq %rbx, 0x40(%rsp)
movq %r14, 0x38(%rsp)
movq %r14, %rbp
addq $0x3c, %rbp
movl $0x7, %r12d
movq %rsp, %r13
movl $0x3, %ecx
movq %r15, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0xee8d4
movzbl (%rsp), %edi
callq 0xec78a
movzbl %al, %ebx
shll $0x10, %ebx
movzbl 0x1(%rsp), %edi
callq 0xec78a
movzbl %al, %r14d
shll $0x8, %r14d
orl %ebx, %r14d
movzbl 0x2(%rsp), %edi
callq 0xec78a
movzbl %al, %eax
orl %r14d, %eax
orl $0xff000000, %eax       # imm = 0xFF000000
movl %eax, (%rbp)
addq $0x3, %r12
addq $0x4, %rbp
cmpq $0x307, %r12            # imm = 0x307
jne 0x38cad
movq 0x38(%rsp), %r14
movl $0x1, 0x38(%r14)
movq %r15, %rdi
callq 0xf052b
movq 0x40(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq *0x30(%r14)
jmp 0x38d44
leaq 0xdfdab(%rip), %rsi     # 0x118ae5
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe8be2
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xe0129(%rip), %rsi     # 0x118e8e
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe9295
xorl %edi, %edi
callq 0xf052b
jmp 0x38d44