// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/mbk.c
// Function: run_mbk_mbs_internal
static void run_mbk_mbs_internal(deark *c, de_module_params *mparams, UI mode)
{
	lctx *d = NULL;
	u32 id;
	const char *s;
	char opt_res0_c = '\0';
	char opt_res1_c = '\0';
	u8 buf[10];

	d = de_malloc(c, sizeof(lctx));

	d->imgtype_of_res1 = PKPIC_IMGTYPE_UNKNOWN;
	d->imgtype_of_res0 = PKPIC_IMGTYPE_UNKNOWN;

	if(mode==1) { // .pp1 extension
		d->imgtype_of_res1 = PKPIC_IMGTYPE_PP1;
	}
	else if(mode==2) {
		d->imgtype_of_res1 = PKPIC_IMGTYPE_MED;
	}
	else if(mode==3) {
		d->imgtype_of_res1 = PKPIC_IMGTYPE_PP3;
	}
	else if(mode==4) { // .daj extension
		d->imgtype_of_res0 = PKPIC_IMGTYPE_M4P;
		d->imgtype_of_res1 = PKPIC_IMGTYPE_MED;
	}

	s = de_get_ext_option(c, "stos:res0");
	if(s) opt_res0_c = s[0];
	s = de_get_ext_option(c, "stos:res1");
	if(s) opt_res1_c = s[0];

	if(opt_res1_c=='l') {
		d->imgtype_of_res1 = PKPIC_IMGTYPE_PP1;
	}
	else if(opt_res1_c=='m') {
		d->imgtype_of_res1 = PKPIC_IMGTYPE_MED;
	}
	else if(opt_res1_c=='h') {
		d->imgtype_of_res1 = PKPIC_IMGTYPE_PP3;
	}

	if(opt_res0_c=='l') {
		d->imgtype_of_res0 = PKPIC_IMGTYPE_LOW;
	}
	else if(opt_res0_c=='m') {
		d->imgtype_of_res0 = PKPIC_IMGTYPE_M4P;
	}

	de_read(buf, 0, sizeof(buf));
	if(!de_memcmp(buf, g_lion_sig, 10)) {
		d->is_container_fmt = 1;
		d->banknum = de_getu32be(10);
		if(d->banknum==0) {
			de_declare_fmt(c, "STOS MBS");
			do_mbs(c, d);
		}
		else {
			de_declare_fmt(c, "STOS MBK");
			do_mbk(c, d);
		}
	}
	else {
		id = (u32)de_getu32be_direct(buf);

		if(id==BANKID_SPRITE) {
			de_declare_fmt(c, "STOS Sprite Bank");
			do_sprite_bank(c, d, 0);
		}
		else if(id==BANKID_PKSCREEN) {
			de_declare_fmt(c, "STOS Packed Screen / Picture Packer");
			do_pkscreen_bank(c, d, 0);
		}
		else {
			de_err(c, "Not a (supported) STOS/MBK format");
		}
	}

	de_free(c, d);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %esi, %r14d
movq %rdi, %rbx
movl $0x430, %esi            # imm = 0x430
callq 0xe969b
movq %rax, %r15
movabsq $0x6300000063, %rax     # imm = 0x6300000063
movq %rax, 0x4(%r15)
decl %r14d
cmpl $0x3, %r14d
ja 0x95f46
leaq 0x99f72(%rip), %rax     # 0x12fe90
movslq (%rax,%r14,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movl $0x6e, %eax
jmp 0x95f42
movl $0x70, %eax
jmp 0x95f42
movl $0x78, 0x4(%r15)
movl $0x65, %eax
movl %eax, 0x8(%r15)
leaq 0x9a008(%rip), %rsi     # 0x12ff55
movq %rbx, %rdi
callq 0xea6cb
xorl %r14d, %r14d
movl $0x0, %ebp
testq %rax, %rax
je 0x95f65
movsbl (%rax), %ebp
leaq 0x99ff3(%rip), %rsi     # 0x12ff5f
movq %rbx, %rdi
callq 0xea6cb
testq %rax, %rax
je 0x95f7d
movsbl (%rax), %r14d
cmpl $0x68, %r14d
je 0x95f9d
cmpl $0x6c, %r14d
je 0x95f96
cmpl $0x6d, %r14d
jne 0x95fa6
movl $0x65, %eax
jmp 0x95fa2
movl $0x6e, %eax
jmp 0x95fa2
movl $0x70, %eax
movl %eax, 0x8(%r15)
cmpl $0x6c, %ebp
je 0x95fb7
cmpl $0x6d, %ebp
jne 0x95fc0
movl $0x78, %eax
jmp 0x95fbc
movl $0x64, %eax
movl %eax, 0x4(%r15)
movq 0x18(%rbx), %rdi
leaq 0x1e(%rsp), %r14
movl $0xa, %ecx
movq %r14, %rsi
xorl %edx, %edx
callq 0xeeee8
movabsq $0x62756f706e6f694c, %rax # imm = 0x62756F706E6F694C
xorq (%r14), %rax
movzwl 0x8(%r14), %ecx
xorq $0x6b6e, %rcx           # imm = 0x6B6E
orq %rax, %rcx
je 0x96037
leaq 0x1e(%rsp), %rdi
callq 0xef325
cmpl $0x6071963, %eax        # imm = 0x6071963
je 0x960ed
cmpl $0x19861987, %eax       # imm = 0x19861987
jne 0x9610e
leaq 0x99f5e(%rip), %rsi     # 0x12ff7b
movq %rbx, %rdi
callq 0xeb6d6
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x9645e
jmp 0x96444
movb $0x1, (%r15)
movq 0x18(%rbx), %rdi
movl $0xa, %esi
callq 0xef32a
movq %rax, 0x10(%r15)
testq %rax, %rax
je 0x96124
leaq 0x99f15(%rip), %rsi     # 0x12ff72
movq %rbx, %rdi
callq 0xeb6d6
leaq 0x99f82(%rip), %rsi     # 0x12ffee
movq %rbx, %rdi
xorl %edx, %edx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movl 0x10(%r15), %edx
leaq 0x99f6f(%rip), %rsi     # 0x12ffff
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movl $0xe, %esi
callq 0xef32a
movq %rax, %rdx
shrq $0x18, %rdx
movb %dl, 0x18(%r15)
andl $0xffffff, %eax         # imm = 0xFFFFFF
movq %rax, 0x20(%r15)
movzbl %dl, %eax
leaq 0x95da3(%rip), %rcx     # 0x12be69
leal -0x81(%rax), %esi
cmpl $0x4, %esi
ja 0x9614b
leaq 0x99dc8(%rip), %rax     # 0x12fea0
movslq (%rax,%rsi,4), %rsi
addq %rax, %rsi
jmpq *%rsi
leaq 0xa3eb9(%rip), %rcx     # 0x139fa1
jmp 0x96180
leaq 0x99e98(%rip), %rsi     # 0x12ff8c
movq %rbx, %rdi
callq 0xeb6d6
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x968f4
jmp 0x96444
leaq 0x99e9b(%rip), %rsi     # 0x12ffb0
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
jmp 0x96444
leaq 0x99e3e(%rip), %rsi     # 0x12ff69
movq %rbx, %rdi
callq 0xeb6d6
leaq 0x99ea3(%rip), %rsi     # 0x12ffdd
movq %rbx, %rdi
xorl %edx, %edx
xorl %eax, %eax
callq 0xe9226
jmp 0x96444
cmpl $0x2, %eax
je 0x96179
cmpl $0x1, %eax
jne 0x96180
leaq 0x99eb3(%rip), %rcx     # 0x13000f
jmp 0x96180
leaq 0x99eba(%rip), %rcx     # 0x13001f
jmp 0x96180
leaq 0x99ea6(%rip), %rcx     # 0x130014
jmp 0x96180
leaq 0x88962(%rip), %rcx     # 0x11ead9
jmp 0x96180
leaq 0x99eed(%rip), %rcx     # 0x13006d
movzbl %dl, %edx
leaq 0x99ea2(%rip), %rsi     # 0x13002c
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movl 0x20(%r15), %edx
leaq 0x99ea4(%rip), %rsi     # 0x130043
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
cmpb $-0x7f, 0x18(%r15)
jne 0x96444
leaq 0x99e89(%rip), %rsi     # 0x130051
movq %rbx, %rdi
movl $0x12, %edx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x18(%rbx), %rdi
movl $0x12, %esi
callq 0xef32a
movl %eax, 0x28(%r15)
leaq 0x95c6c(%rip), %rcx     # 0x12be69
cmpl $0x19861986, %eax       # imm = 0x19861986
jle 0x96222
cmpl $0x19861987, %eax       # imm = 0x19861987
je 0x96239
cmpl $0x28091960, %eax       # imm = 0x28091960
je 0x96242
cmpl $0x4d414553, %eax       # imm = 0x4D414553
jne 0x96252
leaq 0x99e75(%rip), %rcx     # 0x130095
jmp 0x96252
cmpl $0x6071963, %eax        # imm = 0x6071963
je 0x9624b
cmpl $0x13490157, %eax       # imm = 0x13490157
jne 0x96252
leaq 0x99e3d(%rip), %rcx     # 0x130074
jmp 0x96252
leaq 0x99e49(%rip), %rcx     # 0x130089
jmp 0x96252
leaq 0x99e36(%rip), %rcx     # 0x13007f
jmp 0x96252
leaq 0x99e14(%rip), %rcx     # 0x130066
leaq 0x99e45(%rip), %rsi     # 0x13009e
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movl 0x28(%r15), %eax
cmpl $0x6071963, %eax        # imm = 0x6071963
movq %r15, 0x8(%rsp)
je 0x96422
cmpl $0x28091960, %eax       # imm = 0x28091960
je 0x962a2
cmpl $0x19861987, %eax       # imm = 0x19861987
jne 0x96432
movl $0x12, %edx
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
callq 0x9645e
jmp 0x96432
movq 0x18(%rbx), %rdi
movl $0x16, %esi
callq 0xef169
movq %rax, %r14
leaq 0x99dfe(%rip), %rsi     # 0x1300b8
movq %rbx, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0xe9226
testq %r14, %r14
jle 0x96432
movl $0x24, %r12d
xorl %ebp, %ebp
movq %r14, 0x10(%rsp)
leal -0xc(%r12), %ecx
movq %rbx, %rdi
leaq 0x99de0(%rip), %rsi     # 0x1300cc
movl %ebp, %edx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x18(%rbx), %rdi
leaq -0x8(%r12), %rsi
callq 0xef169
movq %rbx, %rdi
leaq 0x99dc2(%rip), %rsi     # 0x1300dc
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
leaq -0x6(%r12), %rsi
callq 0xef169
movq %rax, %r14
movq 0x18(%rbx), %rdi
leaq -0x4(%r12), %rsi
callq 0xef169
movq %rax, %r15
movq %rbx, %rdi
leaq 0x99da1(%rip), %rsi     # 0x1300f0
movl %r14d, %edx
movl %r15d, %ecx
xorl %eax, %eax
callq 0xe9226
testq %r15, %r15
setne %al
testq %r14, %r14
sete %cl
orb %al, %cl
movzbl %cl, %r14d
movl $0x10, %esi
movl $0x10, %edx
movq %rbx, %rdi
movl $0x2, %ecx
callq 0xf2cab
movq %rax, %r13
movl $0x10, %esi
movl $0x10, %edx
movq %rbx, %rdi
movl $0x1, %ecx
callq 0xf2cab
movq %rax, %r15
leaq -0x2(%r12), %rsi
movq 0x18(%rbx), %rdi
movl $0x4, %edx
movq %rax, %rcx
xorl %r8d, %r8d
callq 0xf3081
movq 0x18(%rbx), %rdi
movl $0x4, %edx
movq %r12, %rsi
movq %r13, %rcx
movl %r14d, %r8d
movq 0x10(%rsp), %r14
callq 0xf3081
movq %r13, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0xf3e1c
movq %r13, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0xf2923
movq %r13, %rdi
callq 0xf28d4
movq %r15, %rdi
callq 0xf28d4
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
incq %rbp
addq $0x54, %r12
cmpq %rbp, %r14
jne 0x962dd
jmp 0x96432
movl $0x12, %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x968f4
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
movq 0x8(%rsp), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0xe9752
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq