// File: /workspace/github_c_makefile_star2_O3/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 0x3ecfa0(%rip), %r14    # 0x422e90
movq %r14, %rdi
callq 0x6430
leaq 0x3da121(%rip), %rdi    # 0x410020
xorl %r13d, %r13d
movl $0x4800, %edx           # imm = 0x4800
xorl %esi, %esi
callq 0x62f0
leaq 0x3cb8cb(%rip), %rdi    # 0x4017e0
movl $0x4800, %edx           # imm = 0x4800
xorl %esi, %esi
callq 0x62f0
leaq 0x204b38(%rip), %rdi    # 0x23aa60
movl $0x9000, %edx           # imm = 0x9000
xorl %esi, %esi
callq 0x62f0
movl %r13d, 0x182ad5(%rip)   # 0x1b8a10
movl %r13d, 0x3ecf62(%rip)   # 0x422ea4
leaq 0x3ecf67(%rip), %r12    # 0x422eb0
leaq 0x3ef360(%rip), %r15    # 0x4252b0
movswl %r13w, %ebx
imull $0x6667, %ebx, %eax     # imm = 0x6667
movl %eax, %ecx
shrl $0x1f, %ecx
sarl $0x12, %eax
addl %ecx, %eax
leal (%rax,%rax), %ecx
leal (%rcx,%rcx,4), %ecx
movl %r13d, %edx
subl %ecx, %edx
addb $0x30, %dl
movb %dl, 0x3ecf1f(%rip)     # 0x422e97
movswl %ax, %ecx
imull $0x6667, %ecx, %ecx     # imm = 0x6667
movl %ecx, %edx
shrl $0x1f, %edx
shrl $0x12, %ecx
addl %edx, %ecx
addl %ecx, %ecx
leal (%rcx,%rcx,4), %ecx
subl %ecx, %eax
addb $0x30, %al
movb %al, 0x3ecefc(%rip)     # 0x422e96
imull $0x147b, %ebx, %eax     # imm = 0x147B
movl %eax, %ecx
shrl $0x1f, %ecx
sarl $0x13, %eax
addl %ecx, %eax
movswl %ax, %ecx
imull $0x6667, %ecx, %ecx     # imm = 0x6667
movl %ecx, %edx
shrl $0x1f, %edx
shrl $0x12, %ecx
addl %edx, %ecx
addl %ecx, %ecx
leal (%rcx,%rcx,4), %ecx
subl %ecx, %eax
addb $0x30, %al
movb %al, 0x3ecec9(%rip)     # 0x422e95
movq %r14, %rdi
xorl %esi, %esi
callq 0x2043c
movl %eax, %ecx
shll $0x10, %ecx
cmpl $0xffff0000, %ecx       # imm = 0xFFFF0000
jne 0x35fef
movl 0x3eceba(%rip), %r13d   # 0x422ea4
jmp 0x36137
movswl %ax, %ebp
movl %ebp, %edi
leaq 0x3ecead(%rip), %rsi    # 0x422ea8
movl $0x4, %edx
callq 0x20684
cmpl $0x1, 0x3ece9c(%rip)    # 0x422ea8
jne 0x36168
movl %ebp, %edi
leaq 0x3ece91(%rip), %rsi    # 0x422eac
movl $0x4, %edx
callq 0x20684
movl %ebp, %edi
leaq 0x10(%rsp), %rsi
movl $0x4, %edx
callq 0x20684
movl %ebp, %edi
leaq 0xc(%rsp), %rsi
movl $0x4, %edx
callq 0x20684
movl 0xc(%rsp), %eax
movslq 0x10(%rsp), %rcx
leaq 0x3d9fc9(%rip), %rdx    # 0x410020
leaq (%rdx,%rcx,2), %rsi
subl %ecx, %eax
leal 0x2(,%rax,2), %edx
movl %ebp, %edi
callq 0x20684
movslq 0x10(%rsp), %rax
leaq 0x3cb769(%rip), %rcx    # 0x4017e0
leaq (%rcx,%rax,2), %rsi
movl 0xc(%rsp), %ecx
subl %eax, %ecx
leal 0x2(,%rcx,2), %edx
movl %ebp, %edi
callq 0x20684
movslq 0x10(%rsp), %rax
leaq 0x2049c5(%rip), %rcx    # 0x23aa60
leaq (%rcx,%rax,4), %rsi
movl 0xc(%rsp), %ecx
subl %eax, %ecx
leal 0x4(,%rcx,4), %edx
movl %ebp, %edi
callq 0x20684
movl 0x10(%rsp), %eax
movl 0xc(%rsp), %ecx
movl %eax, %edx
movswl %dx, %esi
incl %edx
cmpl %esi, %ecx
jge 0x360bd
movswl %ax, %edx
cmpl %edx, %ecx
jl 0x3611f
movl 0x18293d(%rip), %edx    # 0x1b8a10
movl %ecx, %esi
subl %eax, %esi
leal 0x18(,%rsi,8), %esi
leaq 0x3d9f3b(%rip), %r9     # 0x410020
leaq 0x3cb6f4(%rip), %r10    # 0x4017e0
movswq %ax, %rdi
movb %r13b, (%rdi,%r12)
movl %esi, (%r15,%rdi,4)
movswl (%r9,%rdi,2), %r8d
movswl (%r10,%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 0x360ec
movl %edx, 0x1828f1(%rip)    # 0x1b8a10
movl %ebp, %edi
callq 0x20b40
movl 0x3ecd77(%rip), %r13d   # 0x422ea4
incl %r13d
movl %r13d, 0x3ecd6d(%rip)   # 0x422ea4
cmpl %r13d, %ebx
jne 0x35f50
leaq 0x3f8169(%rip), %rdi    # 0x42e2b0
movl $0x120, %esi            # imm = 0x120
xorl %edx, %edx
callq 0x4d2d8
callq 0x22477
movl 0x14(%rsp), %edx
movl %edx, %ecx
cmpq %rcx, %rax
jbe 0x3617f
movslq %edx, %r15
jmp 0x3619d
leaq 0x41349(%rip), %rdi     # 0x774b8
leaq 0x3ecd1a(%rip), %rsi    # 0x422e90
xorl %eax, %eax
callq 0x25221
jmp 0x361d3
callq 0x22477
shrq $0x2, %rax
movabsq $0x28f5c28f5c28f5c3, %rcx # imm = 0x28F5C28F5C28F5C3
mulq %rcx
shrq $0x2, %rdx
imulq $0x3c, %rdx, %r15
movq %r15, 0x182874(%rip)    # 0x1b8a18
movq %r15, %rdi
callq 0x69a0
testq %rax, %rax
jne 0x361dd
addq $-0x10000, %r15         # imm = 0xFFFF0000
movq %r15, 0x182859(%rip)    # 0x1b8a18
cmpq $0x10000, %r15          # imm = 0x10000
jae 0x361a4
movq $0x0, 0x182835(%rip) # 0x1b8a08
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x3628e
movq %rax, 0x182824(%rip)    # 0x1b8a08
movq %rax, %rdi
movq %r15, %rsi
callq 0x1f56c
xorl %eax, %eax
leaq 0x6e5b8(%rip), %rcx     # 0xa47b0
leaq 0x3de651(%rip), %rdx    # 0x414850
leaq 0x3d9e1a(%rip), %r10    # 0x410020
leaq 0x3cb5d3(%rip), %r11    # 0x4017e0
movswl (%r10,%rax,2), %edi
movw $0x10, %si
leal -0x1(%rsi), %r8d
movzwl %r8w, %r8d
cmpl %edi, (%rcx,%r8,4)
jle 0x36232
decl %esi
cmpw $0x2, %si
ja 0x36216
movw $0x1, %si
jmp 0x36234
decl %esi
movswl (%r11,%rax,2), %r8d
movw $0x10, %di
leal -0x1(%rdi), %r9d
movzwl %r9w, %r9d
cmpl %r8d, (%rcx,%r9,4)
jle 0x36259
decl %edi
cmpw $0x2, %di
ja 0x3623d
movw $0x1, %di
jmp 0x3625b
decl %edi
shlb $0x4, %dil
addb %sil, %dil
movb %dil, (%rax,%rdx)
incq %rax
cmpq $0x2400, %rax           # imm = 0x2400
jne 0x3620d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, 0x6e9c8(%rip)     # 0xa4c44
movl %eax, 0x3f84ae(%rip)    # 0x42e730
movl $0x0, 0x3f84a8(%rip)    # 0x42e734
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq