// File: /workspace/github_c_makefile_star2_O2/jonof[P]jftekwar/jfbuild/src/engine.c
// Function: loadpics
int loadpics(char *filename, int askedsize)
{
	int offscount, localtilestart, localtileend, dasiz;
	short fil, i, j, k;

	Bstrcpy(artfilename,filename);

	for(i=0;i<MAXTILES;i++)
	{
		tilesizx[i] = 0;
		tilesizy[i] = 0;
		picanm[i] = 0L;
	}

	artsize = 0L;

	numtilefiles = 0;
	do
	{
		k = numtilefiles;

		artfilename[7] = (k%10)+48;
		artfilename[6] = ((k/10)%10)+48;
		artfilename[5] = ((k/100)%10)+48;
		if ((fil = kopen4load(artfilename,0)) != -1)
		{
			kread(fil,&artversion,4); artversion = B_LITTLE32(artversion);
			if (artversion != 1) {
				buildprintf("loadpics(): Invalid art file version in %s\n", artfilename);
				return(-1);
			}
			kread(fil,&numtiles,4);       numtiles       = B_LITTLE32(numtiles);
			kread(fil,&localtilestart,4); localtilestart = B_LITTLE32(localtilestart);
			kread(fil,&localtileend,4);   localtileend   = B_LITTLE32(localtileend);
			kread(fil,&tilesizx[localtilestart],(localtileend-localtilestart+1)<<1);
			kread(fil,&tilesizy[localtilestart],(localtileend-localtilestart+1)<<1);
			kread(fil,&picanm[localtilestart],(localtileend-localtilestart+1)<<2);
			for (i=localtilestart; i<=localtileend; i++) {
				tilesizx[i] = B_LITTLE16(tilesizx[i]);
				tilesizy[i] = B_LITTLE16(tilesizy[i]);
				picanm[i]   = B_LITTLE32(picanm[i]);
			}

			offscount = 4+4+4+4+((localtileend-localtilestart+1)<<3);
			for(i=localtilestart;i<=localtileend;i++)
			{
				tilefilenum[i] = k;
				tilefileoffs[i] = offscount;
				dasiz = (int)(tilesizx[i]*tilesizy[i]);
				offscount += dasiz;
				artsize += ((dasiz+15)&0xfffffff0);
			}
			kclose(fil);

			numtilefiles++;
		}
	}
	while (k != numtilefiles);

	clearbuf(&gotpic[0],(int)((MAXTILES+31)>>5),0L);

	//try dpmi_DETERMINEMAXREALALLOC!

	//cachesize = min((int)((Bgetsysmemsize()/100)*60),max(artsize,askedsize));
	if (Bgetsysmemsize() <= (unsigned int)askedsize)
		cachesize = (Bgetsysmemsize()/100)*60;
	else
		cachesize = askedsize;
	while ((pic = kmalloc(cachesize)) == NULL)
	{
		cachesize -= 65536L;
		if (cachesize < 65536) return(-1);
	}
	initcache(pic, cachesize);

	for(i=0;i<MAXTILES;i++)
	{
		j = 15;
		while ((j > 1) && (pow2long[j] > tilesizx[i])) j--;
		picsiz[i] = ((unsigned char)j);
		j = 15;
		while ((j > 1) && (pow2long[j] > tilesizy[i])) j--;
		picsiz[i] += ((unsigned char)(j<<4));
	}

	artfil = -1;
	artfilnum = -1;
	artfilplc = 0L;

	return(0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %esi, 0x14(%rsp)
movq %rdi, %rsi
leaq 0x3e9658(%rip), %rdi    # 0x41be90
callq 0x6430
leaq 0x20121c(%rip), %rax    # 0x233a60
xorl %ecx, %ecx
leaq 0x3d67d3(%rip), %r13    # 0x409020
leaq 0x3c7f8c(%rip), %r15    # 0x3fa7e0
cmpq $0x4800, %rcx           # imm = 0x4800
je 0x32876
andw $0x0, (%rcx,%r13)
andw $0x0, (%rcx,%r15)
andl $0x0, (%rax)
addq $0x2, %rcx
addq $0x4, %rax
jmp 0x32854
andl $0x0, 0x17f193(%rip)    # 0x1b1a10
andl $0x0, 0x3e9620(%rip)    # 0x41bea4
xorl %ebp, %ebp
movw $0xa, %bx
leaq 0x3e961f(%rip), %r12    # 0x41beb0
leaq 0x3eba18(%rip), %r14    # 0x41e2b0
movl %ebp, %eax
cwtd
idivw %bx
addb $0x30, %dl
movb %dl, 0x3e95ef(%rip)     # 0x41be97
cwtd
idivw %bx
addb $0x30, %dl
movb %dl, 0x3e95e0(%rip)     # 0x41be96
movl %ebp, %eax
cwtd
movw $0x64, %cx
idivw %cx
cwtd
idivw %bx
addb $0x30, %dl
movb %dl, 0x3e95c6(%rip)     # 0x41be95
leaq 0x3e95ba(%rip), %rdi    # 0x41be90
xorl %esi, %esi
callq 0x1dc36
movl %eax, %ecx
shll $0x10, %ecx
cmpl $0xffff0000, %ecx       # imm = 0xFFFF0000
jne 0x328f5
movl 0x3e95b4(%rip), %eax    # 0x41bea4
jmp 0x32a27
movq %r13, %r15
movswl %ax, %ebx
movl %ebx, %edi
leaq 0x3e95a4(%rip), %rsi    # 0x41bea8
pushq $0x4
popq %r13
movl %r13d, %edx
callq 0x1de3f
cmpl $0x1, 0x3e9591(%rip)    # 0x41bea8
jne 0x32a5c
movl %ebx, %edi
leaq 0x3e9586(%rip), %rsi    # 0x41beac
movl %r13d, %edx
callq 0x1de3f
movl %ebx, %edi
leaq 0x10(%rsp), %rsi
movl %r13d, %edx
callq 0x1de3f
movl %ebx, %edi
leaq 0xc(%rsp), %rsi
movl %r13d, %edx
callq 0x1de3f
movl 0xc(%rsp), %eax
movslq 0x10(%rsp), %rcx
movq %r15, %r13
leaq (%r15,%rcx,2), %rsi
subl %ecx, %eax
leal 0x2(,%rax,2), %edx
movl %ebx, %edi
callq 0x1de3f
movslq 0x10(%rsp), %rax
leaq 0x3c7e68(%rip), %r15    # 0x3fa7e0
leaq (%r15,%rax,2), %rsi
movl 0xc(%rsp), %ecx
subl %eax, %ecx
leal 0x2(,%rcx,2), %edx
movl %ebx, %edi
callq 0x1de3f
movslq 0x10(%rsp), %rax
leaq 0x2010c4(%rip), %rcx    # 0x233a60
leaq (%rcx,%rax,4), %rsi
movl 0xc(%rsp), %ecx
subl %eax, %ecx
leal 0x4(,%rcx,4), %edx
movl %ebx, %edi
callq 0x1de3f
movl 0x10(%rsp), %eax
movl 0xc(%rsp), %ecx
movl %eax, %edx
movswl %dx, %esi
incl %edx
cmpl %esi, %ecx
jge 0x329be
movl %ecx, %edx
subl %eax, %edx
leal 0x18(,%rdx,8), %edx
movl 0x17f038(%rip), %esi    # 0x1b1a10
movswl %ax, %edi
cmpl %edi, %ecx
jl 0x32a0e
movswq %ax, %rdi
movb %bpl, (%rdi,%r12)
movl %edx, (%r14,%rdi,4)
movswl (%r13,%rdi,2), %r8d
movswl (%r15,%rdi,2), %edi
imull %r8d, %edi
addl %edi, %edx
addl $0xf, %edi
andl $-0x10, %edi
addl %edi, %esi
movl %esi, 0x17f006(%rip)    # 0x1b1a10
incl %eax
jmp 0x329d8
movl %ebx, %edi
callq 0x1e2db
movl 0x3e9489(%rip), %eax    # 0x41bea4
incl %eax
movl %eax, 0x3e9481(%rip)    # 0x41bea4
movw $0xa, %bx
movswl %bp, %ecx
movl %eax, %ebp
cmpl %eax, %ecx
jne 0x32898
leaq 0x3f4875(%rip), %rdi    # 0x4272b0
movl $0x120, %esi            # imm = 0x120
xorl %edx, %edx
callq 0x444af
callq 0x1fab8
movl 0x14(%rsp), %edx
movl %edx, %ecx
cmpq %rcx, %rax
jbe 0x32a73
movslq %edx, %rbx
jmp 0x32a84
leaq 0x39915(%rip), %rdi     # 0x6c378
leaq 0x3e9426(%rip), %rsi    # 0x41be90
xorl %eax, %eax
callq 0x22427
jmp 0x32ab7
callq 0x1fab8
pushq $0x64
popq %rcx
xorl %edx, %edx
divq %rcx
imulq $0x3c, %rax, %rbx
movq %rbx, 0x17ef8d(%rip)    # 0x1b1a18
movq %rbx, %rdi
callq 0x69a0
testq %rax, %rax
jne 0x32abf
addq $-0x10000, %rbx         # imm = 0xFFFF0000
movq %rbx, 0x17ef72(%rip)    # 0x1b1a18
cmpq $0x10000, %rbx          # imm = 0x10000
jae 0x32a8b
andq $0x0, 0x17ef51(%rip)    # 0x1b1a08
pushq $-0x1
popq %rax
jmp 0x32b52
movq %rax, 0x17ef42(%rip)    # 0x1b1a08
movq %rax, %rdi
movq %rbx, %rsi
callq 0x1cf38
xorl %eax, %eax
leaq 0x6acd6(%rip), %rcx     # 0x9d7b0
leaq 0x3dad6f(%rip), %rdx    # 0x40d850
cmpq $0x2400, %rax           # imm = 0x2400
je 0x32b3b
movw $0xf, %si
cmpw $0x2, %si
jb 0x32b06
movzwl %si, %edi
movswl (%r13,%rax,2), %r8d
cmpl %r8d, (%rcx,%rdi,4)
jle 0x32b0a
decl %esi
jmp 0x32aed
movw $0x1, %si
movw $0xf, %di
cmpw $0x2, %di
jb 0x32b27
movzwl %di, %r8d
movswl (%r15,%rax,2), %r9d
cmpl %r9d, (%rcx,%r8,4)
jle 0x32b2b
decl %edi
jmp 0x32b0e
movw $0x1, %di
shlb $0x4, %dil
addb %sil, %dil
movb %dil, (%rax,%rdx)
incq %rax
jmp 0x32ae1
orl $-0x1, 0x6b102(%rip)    # 0x9dc44
orl $-0x1, 0x3f4be7(%rip)   # 0x427730
andl $0x0, 0x3f4be4(%rip)    # 0x427734
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq