// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/mbk.c
// Function: do_sprite_bank
static void do_sprite_bank(deark *c, lctx *d, i64 pos)
{
	i64 res;
	i64 paramoffs_raw[3]; // One for each resolution: low, med, hi
	i64 paramoffs[3];
	i64 nsprites[3];
	i64 nsprites_total = 0;
	i64 pal_pos;

	for(res=0; res<3; res++) {
		paramoffs_raw[res] = de_getu32be(pos+4+4*res);
		// paramoffs is relative to the first position after the ID.
		paramoffs[res] = pos + 4 + paramoffs_raw[res];
		nsprites[res] = de_getu16be(pos+16+2*res);
		de_dbg(c, "%s-res sprites: %d, param blk offset: %d ("DE_CHAR_RIGHTARROW" %d)", sprite_res_name[res],
			(int)nsprites[res], (int)paramoffs_raw[res], (int)paramoffs[res]);
		nsprites_total += nsprites[res];
	}

	// TODO: What's the right way to calculate the position of the palette?
	pal_pos = pos + 22 + (nsprites_total)*8;
	read_sprite_palette(c, d, pal_pos);

	for(res=0; res<3; res++) {
		if(nsprites[res]<1) continue;
		if(paramoffs[res]>(c->infile->len-8)) continue;
		do_sprite_param_blocks(c, d, res, nsprites[res], paramoffs[res]);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rsi, 0x10(%rsp)
movq %rdi, %r14
leaq 0x4(%rdx), %r13
movq %rdx, 0x18(%rsp)
leaq 0x10(%rdx), %r15
xorl %ebx, %ebx
movq %r13, 0x8(%rsp)
xorl %ebp, %ebp
movq 0x18(%r14), %rdi
movq %r13, %rsi
callq 0xef32a
movq %rax, %rcx
movq %rax, 0x28(%rsp)
movq 0x8(%rsp), %rax
addq %rcx, %rax
movq %rax, 0x20(%rsp)
movq %rax, 0x70(%rsp,%rbx)
movq 0x18(%r14), %rdi
movq %r15, %rsi
callq 0xef169
movq %rax, %r12
movq %rax, 0x50(%rsp,%rbx)
leaq 0xeedf2(%rip), %rax     # 0x1852c0
movq (%rbx,%rax), %rdx
movq %r14, %rdi
leaq 0x99c31(%rip), %rsi     # 0x13010d
movl %r12d, %ecx
movq 0x28(%rsp), %r8
movq 0x20(%rsp), %r9
xorl %eax, %eax
callq 0xe9226
addq %r12, %rbp
addq $0x2, %r15
addq $0x4, %r13
addq $0x8, %rbx
cmpq $0x18, %rbx
jne 0x9648d
movq 0x18(%rsp), %rax
leaq (%rax,%rbp,8), %rbx
addq $0x16, %rbx
movq 0x18(%r14), %rdi
cmpq %rbx, 0x18(%rdi)
jle 0x965ad
movq %rbx, %rsi
callq 0xef32a
cmpq $0x50414c54, %rax       # imm = 0x50414C54
jne 0x9658e
leaq 0x99c32(%rip), %rsi     # 0x130169
movq %r14, %rdi
movl %ebx, %edx
xorl %eax, %eax
callq 0xe9226
movq %r14, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x18(%r14), %rsi
addq $0x4, %rbx
movq 0x10(%rsp), %rax
leaq 0x2c(%rax), %rcx
movl $0x0, (%rsp)
movl $0x10, %r8d
movl $0x10, %r9d
movq %r14, %rdi
movq %rbx, %rdx
callq 0xf8c62
movq %r14, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
jmp 0x965ad
leaq 0x99baa(%rip), %rsi     # 0x13013f
movq %r14, %rdi
movl %ebx, %edx
xorl %eax, %eax
callq 0xe9b27
movq 0x10(%rsp), %rax
movl $0xffffffff, 0x2c(%rax) # imm = 0xFFFFFFFF
addq $0x2c, 0x10(%rsp)
movq $0x0, 0x8(%rsp)
leaq 0xeecfd(%rip), %rcx     # 0x1852c0
movq 0x8(%rsp), %rax
movq 0x50(%rsp,%rax,8), %rax
movq %rax, 0x18(%rsp)
testq %rax, %rax
jle 0x968cb
movq 0x8(%rsp), %rax
movq 0x70(%rsp,%rax,8), %r15
movq 0x18(%r14), %rax
movq 0x18(%rax), %rax
addq $-0x8, %rax
cmpq %rax, %r15
jg 0x968cb
movq 0x8(%rsp), %rax
movq (%rcx,%rax,8), %rdx
movq %r14, %rdi
leaq 0x99b71(%rip), %rsi     # 0x13017e
movq %rdx, 0x40(%rsp)
movl %r15d, %ecx
xorl %eax, %eax
callq 0xe9226
movq %r14, %rdi
movl $0x1, %esi
callq 0xe9585
movq %r15, 0x38(%rsp)
xorl %ecx, %ecx
movabsq $-0xffffff00000001, %rax # imm = 0xFF000000FFFFFFFF
movq %rax, 0x48(%rsp)
movq %r14, %rdi
leaq 0x99b56(%rip), %rsi     # 0x13019f
movq 0x40(%rsp), %rdx
movq %rcx, 0x20(%rsp)
movl %r15d, %r8d
xorl %eax, %eax
callq 0xe9226
movq %r14, %rdi
movl $0x1, %esi
callq 0xe9585
movl $0x40, %esi
movq %r14, %rdi
callq 0xe969b
movq %rax, %rbx
movl $0x40, %esi
movq %r14, %rdi
callq 0xe969b
movq %rax, %r12
leaq 0x99823(%rip), %rax     # 0x12feb4
movq 0x8(%rsp), %rcx
movzbl (%rcx,%rax), %ecx
movq %rcx, (%rbx)
movl $0x1, %eax
shlq %cl, %rax
movq %rax, 0x8(%rbx)
movq $0x1, (%r12)
movq $0x2, 0x8(%r12)
movq 0x18(%r14), %rdi
movq %r15, %rsi
callq 0xef32a
movq %rax, %r13
movq 0x18(%r14), %rdi
leaq 0x4(%r15), %rsi
callq 0xef0d5
movzbl %al, %ebp
movl %ebp, %eax
shll $0x4, %eax
movq %rax, 0x10(%rbx)
movq 0x18(%r14), %rdi
movq %r15, 0x28(%rsp)
leaq 0x5(%r15), %rsi
callq 0xef0d5
movzbl %al, %edx
movq %rdx, 0x18(%rbx)
movq 0x10(%rbx), %rsi
movq %r14, %rdi
callq 0xe9b05
movq 0x10(%rbx), %rsi
movq 0x18(%rbx), %rdx
movq %r14, %rdi
callq 0xf241d
testl %eax, %eax
je 0x96844
movq 0x10(%rbx), %rax
movq %rax, 0x10(%r12)
movq 0x18(%rbx), %r15
movq %r15, 0x18(%r12)
addq 0x38(%rsp), %r13
addl %ebp, %ebp
imulq %rbp, %r15
movq %r14, %rdi
leaq 0x99a7d(%rip), %rsi     # 0x1301c3
movl %r13d, %edx
movl %r15d, %ecx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%r14), %rax
cmpq 0x18(%rax), %r13
jge 0x96844
leaq (%r15,%r13), %rdx
movq %rdx, 0x30(%rsp)
imulq (%rbx), %rbp
imulq 0x18(%rbx), %rbp
movq %r14, %rdi
leaq 0x99a5f(%rip), %rsi     # 0x1301dc
movl %ebp, %ecx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%r14), %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0xf1988
movq %rax, 0x20(%r12)
movq 0x18(%r14), %rdi
movq 0x30(%rsp), %rsi
movq %rbp, %rdx
callq 0xf1988
movq %rax, 0x20(%rbx)
leaq 0x48(%rsp), %rax
movq %rax, 0x30(%r12)
movq 0x10(%rsp), %rax
movq %rax, 0x30(%rbx)
movq 0x10(%rbx), %rsi
movq 0x18(%rbx), %rdx
movq %r14, %rdi
movl $0x1, %ecx
callq 0xf2cab
movq %rax, 0x38(%r12)
movq 0x10(%rbx), %rsi
movq 0x18(%rbx), %rdx
movq %r14, %rdi
movl $0x4, %ecx
callq 0xf2cab
movq %rax, 0x38(%rbx)
movq %r14, %rdi
movq %r12, %rsi
callq 0xf900c
movq %r14, %rdi
movq %rbx, %rsi
callq 0xf900c
movq 0x38(%rbx), %rdi
movq 0x38(%r12), %rsi
xorl %edx, %edx
callq 0xf3e1c
movq %r14, %rdi
callq 0xea9a9
movq %rax, %r13
movq %r14, %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0xf9323
movq 0x38(%rbx), %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0xf2917
jmp 0x96847
xorl %r13d, %r13d
movq 0x20(%rbx), %rdi
callq 0xf0b13
movq 0x38(%rbx), %rdi
callq 0xf28d4
movq %r14, %rdi
movq %rbx, %rsi
callq 0xe9752
movq 0x20(%r12), %rdi
callq 0xf0b13
movq 0x38(%r12), %rdi
callq 0xf28d4
movq %r14, %rdi
movq %r12, %rsi
callq 0xe9752
movq %r14, %rdi
movq %r13, %rsi
callq 0xea9b3
movq %r14, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
movq 0x20(%rsp), %rcx
incq %rcx
movq 0x28(%rsp), %r15
addq $0x8, %r15
cmpq %rcx, 0x18(%rsp)
jne 0x96630
movq %r14, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
leaq 0xee9f5(%rip), %rcx     # 0x1852c0
movq 0x8(%rsp), %rdx
incq %rdx
movq %rdx, 0x8(%rsp)
cmpq $0x3, %rdx
jne 0x965c3
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq