// File: /workspace/github_c_makefile_star2_O1/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 0x3ed1dc(%rip), %rdi    # 0x421e90
callq 0x6430
leaq 0x3da360(%rip), %rbx    # 0x40f020
xorl %ebp, %ebp
movl $0x4800, %edx           # imm = 0x4800
movq %rbx, %rdi
xorl %esi, %esi
callq 0x62f0
leaq 0x3cbb08(%rip), %r14    # 0x4007e0
movl $0x4800, %edx           # imm = 0x4800
movq %r14, %rdi
xorl %esi, %esi
callq 0x62f0
leaq 0x204d72(%rip), %rdi    # 0x239a60
movl $0x9000, %edx           # imm = 0x9000
xorl %esi, %esi
callq 0x62f0
movl %ebp, 0x182d10(%rip)    # 0x1b7a10
movl %ebp, 0x3ed19e(%rip)    # 0x421ea4
leaq 0x3ed1a3(%rip), %r13    # 0x421eb0
leaq 0x3ef59c(%rip), %r12    # 0x4242b0
movl 0x3ed189(%rip), %r15d   # 0x421ea4
movswq %r15w, %rax
imulq $0x66666667, %rax, %rcx # imm = 0x66666667
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x22, %rcx
addl %edx, %ecx
leal (%rcx,%rcx), %edx
leal (%rdx,%rdx,4), %edx
movl %r15d, %esi
subl %edx, %esi
addb $0x30, %sil
movb %sil, 0x3ed14e(%rip)    # 0x421e97
movslq %ecx, %rcx
imulq $0x66666667, %rcx, %rdx # imm = 0x66666667
movq %rdx, %rsi
shrq $0x3f, %rsi
sarq $0x22, %rdx
addl %esi, %edx
addl %edx, %edx
leal (%rdx,%rdx,4), %edx
subl %edx, %ecx
addb $0x30, %cl
movb %cl, 0x3ed126(%rip)     # 0x421e96
imulq $0x51eb851f, %rax, %rax # imm = 0x51EB851F
movq %rax, %rcx
shrq $0x3f, %rcx
sarq $0x25, %rax
addl %ecx, %eax
cltq
imulq $0x66666667, %rax, %rcx # imm = 0x66666667
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x22, %rcx
addl %edx, %ecx
addl %ecx, %ecx
leal (%rcx,%rcx,4), %ecx
subl %ecx, %eax
addb $0x30, %al
movb %al, 0x3ed0ec(%rip)     # 0x421e95
leaq 0x3ed0e0(%rip), %rdi    # 0x421e90
xorl %esi, %esi
callq 0x1ff50
movl %eax, %ecx
shll $0x10, %ecx
cmpl $0xffff0000, %ecx       # imm = 0xFFFF0000
je 0x34ee9
movswl %ax, %ebp
movl %ebp, %edi
leaq 0x3ed0d4(%rip), %rsi    # 0x421ea8
movl $0x4, %edx
callq 0x201c2
cmpl $0x1, 0x3ed0c3(%rip)    # 0x421ea8
jne 0x34f21
movl %ebp, %edi
leaq 0x3ed0b8(%rip), %rsi    # 0x421eac
movl $0x4, %edx
callq 0x201c2
movl %ebp, %edi
leaq 0x10(%rsp), %rsi
movl $0x4, %edx
callq 0x201c2
movl %ebp, %edi
leaq 0xc(%rsp), %rsi
movl $0x4, %edx
callq 0x201c2
movl 0xc(%rsp), %eax
movslq 0x10(%rsp), %rcx
leaq (%rbx,%rcx,2), %rsi
subl %ecx, %eax
leal 0x2(,%rax,2), %edx
movl %ebp, %edi
callq 0x201c2
movslq 0x10(%rsp), %rax
leaq (%r14,%rax,2), %rsi
movl 0xc(%rsp), %ecx
subl %eax, %ecx
leal 0x2(,%rcx,2), %edx
movl %ebp, %edi
callq 0x201c2
movslq 0x10(%rsp), %rax
leaq 0x204bfa(%rip), %rcx    # 0x239a60
leaq (%rcx,%rax,4), %rsi
movl 0xc(%rsp), %ecx
subl %eax, %ecx
leal 0x4(,%rcx,4), %edx
movl %ebp, %edi
callq 0x201c2
movl 0x10(%rsp), %eax
movl 0xc(%rsp), %ecx
movl %eax, %edx
movswl %dx, %esi
incl %edx
cmpl %esi, %ecx
jge 0x34e88
movswl %ax, %edx
cmpl %edx, %ecx
jl 0x34edc
movl 0x182b72(%rip), %edx    # 0x1b7a10
movl %ecx, %esi
subl %eax, %esi
leal 0x18(,%rsi,8), %esi
movswq %ax, %rdi
movb %r15b, (%rdi,%r13)
movl %esi, (%r12,%rdi,4)
movswl (%rbx,%rdi,2), %r8d
movswl (%r14,%rdi,2), %edi
imull %r8d, %edi
addl %edi, %esi
addl $0xf, %edi
andl $-0x10, %edi
addl %edi, %edx
incl %eax
movswl %ax, %edi
cmpl %edi, %ecx
jge 0x34ea9
movl %edx, 0x182b34(%rip)    # 0x1b7a10
movl %ebp, %edi
callq 0x20671
incl 0x3ecfbb(%rip)          # 0x421ea4
movswl %r15w, %eax
cmpl 0x3ecfb1(%rip), %eax    # 0x421ea4
jne 0x34d14
leaq 0x3f83b0(%rip), %rdi    # 0x42d2b0
movl $0x120, %esi            # imm = 0x120
xorl %edx, %edx
callq 0x4abc9
callq 0x220a9
movl 0x14(%rsp), %edx
movl %edx, %ecx
cmpq %rcx, %rax
jbe 0x34f38
movslq %edx, %r15
jmp 0x34f56
leaq 0x41410(%rip), %rdi     # 0x76338
leaq 0x3ecf61(%rip), %rsi    # 0x421e90
xorl %eax, %eax
callq 0x24cb4
jmp 0x34f88
callq 0x220a9
shrq $0x2, %rax
movabsq $0x28f5c28f5c28f5c3, %rcx # imm = 0x28F5C28F5C28F5C3
mulq %rcx
shrq $0x2, %rdx
imulq $0x3c, %rdx, %r15
movq %r15, 0x182abb(%rip)    # 0x1b7a18
movq %r15, %rdi
callq 0x69a0
testq %rax, %rax
jne 0x34f92
addq $-0x10000, %r15         # imm = 0xFFFF0000
movq %r15, 0x182aa0(%rip)    # 0x1b7a18
cmpq $0x10000, %r15          # imm = 0x10000
jae 0x34f5d
movq %rax, 0x182a80(%rip)    # 0x1b7a08
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x35038
movq %rax, 0x182a6f(%rip)    # 0x1b7a08
movq %rax, %rdi
movq %r15, %rsi
callq 0x1f0e0
xorl %eax, %eax
leaq 0x6e803(%rip), %rcx     # 0xa37b0
leaq 0x3de89c(%rip), %rdx    # 0x413850
movswl (%rbx,%rax,2), %edi
movw $0x10, %si
leal -0x1(%rsi), %r8d
movzwl %r8w, %r8d
cmpl %edi, (%rcx,%r8,4)
jle 0x34fd8
decl %esi
cmpw $0x2, %si
ja 0x34fbc
movw $0x1, %si
jmp 0x34fda
decl %esi
movb %sil, (%rax,%rdx)
movswl (%r14,%rax,2), %r8d
movw $0x10, %di
leal -0x1(%rdi), %r9d
movzwl %r9w, %r9d
cmpl %r8d, (%rcx,%r9,4)
jle 0x35003
decl %edi
cmpw $0x2, %di
ja 0x34fe7
movw $0x1, %di
jmp 0x35005
decl %edi
shlb $0x4, %dil
addb %sil, %dil
movb %dil, (%rax,%rdx)
incq %rax
cmpq $0x2400, %rax           # imm = 0x2400
jne 0x34fb4
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, 0x6ec1e(%rip)     # 0xa3c44
movl %eax, 0x3f8704(%rip)    # 0x42d730
movl $0x0, 0x3f86fe(%rip)    # 0x42d734
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq