// File: /workspace/github_c_makefile_star2_O2/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 0xdecaf
movq %rax, %r14
andl $0x0, 0x28(%rax)
leaq 0x48(%rsp), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x377c5
cmpb $0x0, (%r15)
je 0x36fe0
cmpb $0x0, 0x49(%rsp)
je 0x36edd
cmpl $0x2, 0x24(%rbx)
jne 0x36edd
leaq 0xd4a79(%rip), %rsi     # 0x10b94c
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf05a
movq 0x18(%rbx), %rdi
pushq $0x1
popq %rsi
callq 0xe3fa3
movl %eax, (%r14)
movq 0x18(%rbx), %rdi
pushq $0x3
popq %rsi
callq 0xe3fa3
movl %eax, 0x4(%r14)
movl (%r14), %ecx
shll $0x4, %ecx
addl %eax, %ecx
movl %ecx, 0x8(%r14)
movq 0x18(%rbx), %rdi
pushq $0x5
popq %rsi
callq 0xe3fa3
movq %rax, 0x10(%r14)
movl (%r14), %edx
leaq 0xd4a52(%rip), %rsi     # 0x10b974
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movl 0x4(%r14), %edx
leaq 0xd4a4d(%rip), %rsi     # 0x10b984
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movl 0x10(%r14), %ecx
leaq 0xd4a47(%rip), %rsi     # 0x10b993
movq %rbx, %rdi
movl %ecx, %edx
xorl %eax, %eax
callq 0xde896
movq 0x10(%r14), %rdx
movq 0x18(%rbx), %rax
cmpq 0x18(%rax), %rdx
jle 0x36f77
leaq 0xd4a3d(%rip), %rsi     # 0x10b9aa
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf05a
leaq 0xd4a54(%rip), %rsi     # 0x10b9d2
movq %rbx, %rdi
callq 0xdfa87
testq %rax, %rax
je 0x37008
movq %rax, %r15
leaq 0xcc5e2(%rip), %rsi     # 0x103577
movq %rax, %rdi
callq 0x181a0
testl %eax, %eax
je 0x37008
movq %r14, %r13
pushq $-0x20
popq %rcx
leaq 0x14b622(%rip), %r14    # 0x1825d0
leaq 0x20(%rcx), %r12
cmpq $0x1a0, %r12            # imm = 0x1A0
je 0x36fec
movq 0x30(%rcx,%r14), %rsi
movq %r15, %rdi
callq 0x181a0
movq %r12, %rcx
testl %eax, %eax
jne 0x36fae
movl (%r12,%r14), %eax
movq %r13, %r14
movl %eax, 0x28(%r13)
testl %eax, %eax
jne 0x37008
jmp 0x36ff4
leaq 0xd494e(%rip), %rsi     # 0x10b935
jmp 0x376ca
movq %r13, %r14
andl $0x0, 0x28(%r13)
leaq 0xd49e1(%rip), %rsi     # 0x10b9dc
movq %rbx, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0xdf05a
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rax
subq 0x10(%r14), %rax
addq $-0x7, %rax
movq %rax, 0x458(%r14)
pushq $0x7
popq %r15
movq %r15, %rsi
callq 0xe3fa3
movq %rax, 0x440(%r14)
movq 0x18(%rbx), %rdi
pushq $0x9
popq %rsi
callq 0xe3fa3
movq %rax, 0x448(%r14)
movq 0x440(%r14), %rax
addq $0x7, %rax
pushq $0x8
popq %rcx
cqto
idivq %rcx
movq %rax, 0x450(%r14)
cmpq $0x0, 0x458(%r14)
js 0x370b2
movq 0x18(%rbx), %rdi
movq 0x10(%r14), %rsi
addq $0x7, %rsi
callq 0xe3eb4
cmpb $0x1a, %al
jne 0x370b2
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 0xe3eb4
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 0xd4c1d(%rip), %rsi     # 0x10bcdd
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde9f1
movb 0x4a(%rsp), %bpl
testb %bpl, %bpl
je 0x37162
movq 0x50(%rsp), %r12
movl $0x1, 0x18(%r14)
leaq 0xd4c11(%rip), %rsi     # 0x10bcfd
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
addq $0xc, %r12
movq %rsp, %r13
movq %r12, (%r13)
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xe3f2a
movb %al, 0x1c(%r14)
movzbl %al, %edx
leaq 0xd2fc8(%rip), %rsi     # 0x10a0f1
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xe3f2a
movb %al, 0x1d(%r14)
movzbl %al, %edx
leaq 0xd4bce(%rip), %rsi     # 0x10bd1b
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movl 0x28(%r14), %esi
movq %r14, %rdi
callq 0x37880
cmpq $0x0, 0x30(%r14)
jne 0x37591
andq $0x0, 0x30(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
movaps %xmm0, (%rsp)
movb $0x1, 0x2e(%r14)
testb %bpl, %bpl
je 0x371bd
movl $0x3, 0x28(%r14)
leaq 0xd4b83(%rip), %rsi     # 0x10bd2c
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf37f
movb $0x0, 0x2e(%r14)
jmp 0x37585
leaq 0x14(%rsp), %rdx
movl 0x8(%r14), %eax
pushq $0x4
popq %rsi
cmpl $0xb8000, %eax          # imm = 0xB8000
jne 0x371fa
movq 0x10(%r14), %rdi
leaq -0x3fff(%rdi), %rcx
cmpq $0x2, %rcx
jb 0x37228
cmpq $0xfa00, %rdi           # imm = 0xFA00
je 0x37236
cmpq $0x3e80, %rdi           # imm = 0x3E80
jne 0x37238
pushq $0x3
popq %rsi
pushq $0x4
jmp 0x3722d
movq 0x10(%r14), %rcx
cmpl $0xa0000, %eax          # imm = 0xA0000
jne 0x37218
cmpq $0xfa00, %rcx           # imm = 0xFA00
jne 0x37286
pushq $0xa
popq %rsi
movl $0xfa00, %edi           # imm = 0xFA00
jmp 0x37236
movl $0xfa00, %edi           # imm = 0xFA00
cmpq $0xfa00, %rcx           # imm = 0xFA00
je 0x37236
jmp 0x3723b
pushq $0x8
popq %rsi
pushq $0xa
popq %rcx
leaq 0x8(%rsp), %rdx
movl %ecx, 0x4(%rdx)
movl %esi, (%rdx)
movq %rdi, %rcx
movq 0x440(%r14), %rsi
testq %rsi, %rsi
je 0x37396
movq 0x448(%r14), %rdx
testq %rdx, %rdx
je 0x372cf
movq 0x450(%r14), %r8
imulq %rdx, %r8
leaq 0xb(%r8), %rdi
movq 0x18(%rbx), %r9
cmpq 0x18(%r9), %rdi
jg 0x372cf
movq %rcx, %rdi
subq %r8, %rdi
addq $-0x4, %rdi
js 0x372cf
cmpq $0x2, %rdi
jae 0x37299
pushq $0x7
jmp 0x372a1
cmpq $0xfde8, %rcx           # imm = 0xFDE8
jne 0x3723b
pushq $0x6
popq %rsi
movl $0xfde8, %edi           # imm = 0xFDE8
jmp 0x37236
cmpq $0x9, %rdi
jae 0x372a4
pushq $0x6
popq %rdi
jmp 0x372b6
xorl %r8d, %r8d
cmpq $0x81, %rdi
setb %r8b
leal (%r8,%r8,2), %edi
leal 0x2(%rdi), %r8d
testb $0x1, %sil
jne 0x372ca
movl %r8d, 0x1c(%rsp)
incl %edi
movl %edi, %r8d
movl %r8d, 0x18(%rsp)
testq %rdx, %rdx
sete %dil
testb $0x7, %sil
setne %sil
orb %dil, %sil
jne 0x37312
imulq 0x450(%r14), %rdx
leaq 0xb(%rdx), %rdi
movq 0x18(%rbx), %rsi
movq 0x18(%rsi), %rsi
cmpq %rsi, %rdi
jg 0x37348
movq %rcx, %rdi
subq %rdx, %rdi
addq $-0x4, %rdi
js 0x37348
cmpq $0x2, %rdi
jae 0x37329
pushq $0x7
jmp 0x37331
testq %rdx, %rdx
je 0x37396
movq 0x18(%rbx), %rsi
movq 0x18(%rsi), %rsi
imulq 0x450(%r14), %rdx
jmp 0x37348
cmpq $0x9, %rdi
jae 0x37335
pushq $0x5
popq %r8
jmp 0x37343
xorl %r8d, %r8d
cmpq $0x81, %rdi
adcl $0x2, %r8d
movl %r8d, 0x24(%rsp)
leaq 0xb(,%rdx,4), %rdi
cmpq %rsi, %rdi
jg 0x37396
leaq (,%rdx,4), %rdi
movq %rcx, %rsi
subq %rdi, %rsi
addq $-0x4, %rsi
js 0x37396
cmpq $0x2, %rsi
jae 0x37373
pushq $0x8
jmp 0x3737b
cmpq $0x11, %rsi
jae 0x3737e
pushq $0x6
popq %rdx
jmp 0x37392
leaq (%rdx,%rdx,2), %rdx
xorl %edi, %edi
cmpq %rdx, %rsi
setl %dil
leal 0x3(,%rdi,2), %edx
movl %edx, 0x20(%rsp)
cmpl $0xb8000, %eax          # imm = 0xB8000
jne 0x373dc
pushq $0x5
popq %rdx
cmpq $0xf00, %rcx            # imm = 0xF00
je 0x37427
cmpq $0xfa0, %rcx            # imm = 0xFA0
je 0x37427
cmpq $0x1000, %rcx           # imm = 0x1000
je 0x37427
cmpq $0x1fd0, %rcx           # imm = 0x1FD0
setge %dl
movl %ecx, %esi
andl $0xfff, %esi            # imm = 0xFFF
cmpl $0xfd0, %esi            # imm = 0xFD0
sete %sil
testb %sil, %dl
jne 0x373f8
cmpl $0xc000, 0x4(%r14)      # imm = 0xC000
jne 0x373fc
cmpq $0x1000, %rcx           # imm = 0x1000
je 0x37424
cmpq $0x800, %rcx            # imm = 0x800
jne 0x373fc
pushq $0x4
jmp 0x37426
cmpl $0xb800, (%r14)         # imm = 0xB800
sete %dl
cmpq $0x1000, %rcx           # imm = 0x1000
sete %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0x3742b
pushq $0x4
popq %rdx
movl $0x1000, %ecx           # imm = 0x1000
jmp 0x37427
pushq $0x6
popq %rdx
movl %edx, 0x4(%rsp)
cmpl $0xb8000, %eax          # imm = 0xB8000
jne 0x37489
cmpq $0x4000, %rcx           # imm = 0x4000
je 0x37444
cmpq $0x3e80, %rcx           # imm = 0x3E80
jne 0x37489
leaq 0x58(%rsp), %r12
andl $0x0, 0x8(%r12)
andq $0x0, (%r12)
movq 0x18(%rbx), %rdi
leaq 0x1097(%rip), %rcx      # 0x384f6
pushq $0x7
popq %rsi
movl $0x3e80, %edx           # imm = 0x3E80
movq %r12, %r8
callq 0xe4745
cmpb $0x0, 0x8(%r12)
jne 0x37489
cmpb $0x0, 0x61(%rsp)
pushq $0x5
popq %rax
pushq $0xc
popq %rcx
cmovel %eax, %ecx
movl %ecx, 0x10(%rsp)
movq 0x10(%r14), %rcx
cmpq $0x50, %rcx
jl 0x3752c
pushq $0x50
popq %rsi
movq %rcx, %rax
xorl %edx, %edx
divq %rsi
testq %rdx, %rdx
je 0x374d2
cmpq $0x300, %rcx            # imm = 0x300
je 0x37506
cmpq $0x1680, %rcx           # imm = 0x1680
je 0x374f3
cmpq $0x4000, %rcx           # imm = 0x4000
jne 0x3752c
leaq 0x2c(%rsp), %rax
movl $0x1, -0x4(%rax)
pushq $0x2
jmp 0x37529
movabsq $0x300000002, %rax      # imm = 0x300000002
movq %rax, 0x28(%rsp)
cmpq $0x300, %rcx            # imm = 0x300
je 0x37506
cmpq $0x1680, %rcx           # imm = 0x1680
jne 0x3752c
cmpl $0x20000, 0x8(%r14)     # imm = 0x20000
jne 0x3752c
leaq 0x30(%rsp), %rax
pushq $0x8
jmp 0x37529
movl 0x8(%r14), %ecx
cmpl $0xb8000, %ecx          # imm = 0xB8000
je 0x3752c
cmpl $0xa0000, %ecx          # imm = 0xA0000
je 0x3752c
leaq 0x34(%rsp), %rax
testl %ecx, %ecx
jne 0x37527
pushq $0xd
jmp 0x37529
pushq $0x1
popq %rcx
movl %ecx, (%rax)
xorl %eax, %eax
xorl %ecx, %ecx
cmpq $0xe, %rax
je 0x37548
movl (%rsp,%rax,4), %edx
cmpl %ecx, %edx
jle 0x37543
movl %eax, 0x28(%r14)
movl %edx, %ecx
incq %rax
jmp 0x37530
cmpl $0x2, (%rbx)
jl 0x37585
leaq 0xd481a(%rip), %r12     # 0x10bd6e
xorl %r13d, %r13d
cmpq $0xe, %r13
je 0x37585
movl (%rsp,%r13,4), %ebp
testl %ebp, %ebp
je 0x37580
movl %r13d, %edi
callq 0x384cd
movq 0x10(%rax), %rdx
movq %rbx, %rdi
movq %r12, %rsi
movl %ebp, %ecx
xorl %eax, %eax
callq 0xde896
incq %r13
jmp 0x37557
movl 0x28(%r14), %esi
movq %r14, %rdi
callq 0x37880
movq 0x20(%r14), %rax
testq %rax, %rax
je 0x376c3
cmpq $0x0, 0x30(%r14)
je 0x376c3
movq 0x10(%rax), %rdx
leaq 0xd4494(%rip), %rsi     # 0x10ba48
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x20(%r14), %rax
movq 0x18(%rax), %rdx
leaq 0xd448b(%rip), %rsi     # 0x10ba58
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe0824
cmpb $0x0, 0x2e(%r14)
je 0x375ef
leaq 0xd447f(%rip), %rsi     # 0x10ba64
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf05a
leaq 0xd44e2(%rip), %rsi     # 0x10bad8
movq %rbx, %rdi
callq 0xdfa87
testq %rax, %rax
jne 0x3761b
leaq 0xcbf72(%rip), %rsi     # 0x10357c
movq %rbx, %rdi
callq 0xdfa87
testq %rax, %rax
je 0x37723
movq %rax, %r13
leaq 0xd4761(%rip), %rsi     # 0x10bd86
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movq %rbx, %rdi
movq %r13, %rsi
callq 0xe600b
movq %rax, %r12
testq %rax, %rax
je 0x37702
movq %rbx, 0x40(%rsp)
movq %r14, 0x38(%rsp)
addq $0x3c, %r14
pushq $0x3
popq %rbp
cmpq $0x307, %r15            # imm = 0x307
je 0x376ee
movq %r12, %rdi
movq %rsp, %rsi
movq %r15, %rdx
movq %rbp, %rcx
callq 0xe3cb7
movzbl (%rsp), %edi
callq 0xe1cec
movq %r15, %r13
movzbl %al, %r15d
shll $0x10, %r15d
movzbl 0x1(%rsp), %edi
callq 0xe1cec
movzbl %al, %ebx
shll $0x8, %ebx
orl %r15d, %ebx
movq %r13, %r15
movzbl 0x2(%rsp), %edi
callq 0xe1cec
movzbl %al, %eax
orl %ebx, %eax
orl $0xff000000, %eax       # imm = 0xFF000000
movl %eax, (%r14)
addq $0x4, %r14
addq $0x3, %r15
jmp 0x3765a
leaq 0xd432b(%rip), %rsi     # 0x10b9f5
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
movq %rbx, %rdi
movq %r14, %rsi
callq 0xded32
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x38(%rsp), %r14
movl $0x1, 0x38(%r14)
movq 0x40(%rsp), %rbx
jmp 0x37716
leaq 0xd4695(%rip), %rsi     # 0x10bd9e
movq %rbx, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0xdf2a5
movq %r12, %rdi
callq 0xe5591
testq %r12, %r12
je 0x376d4
movq %rbx, %rdi
movq %r14, %rsi
callq *0x30(%r14)
jmp 0x376d4