// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq -0x10(%rbp), %rsi
leaq 0x40f616(%rip), %rdi    # 0x45d6d0
callq 0x63e0
movw $0x0, -0x28(%rbp)
movswl -0x28(%rbp), %eax
cmpl $0x2400, %eax           # imm = 0x2400
jge 0x4e115
movswq -0x28(%rbp), %rcx
leaq 0x3fcca4(%rip), %rax    # 0x44ad80
movw $0x0, (%rax,%rcx,2)
movswq -0x28(%rbp), %rcx
leaq 0x3ee452(%rip), %rax    # 0x43c540
movw $0x0, (%rax,%rcx,2)
movswq -0x28(%rbp), %rcx
leaq 0x2294c0(%rip), %rax    # 0x2775c0
movl $0x0, (%rax,%rcx,4)
movw -0x28(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x28(%rbp)
jmp 0x4e0c5
movl $0x0, 0x1a7451(%rip)    # 0x1f5570
movl $0x0, 0x40f5bb(%rip)    # 0x45d6e4
movl 0x40f5b5(%rip), %eax    # 0x45d6e4
movw %ax, -0x2c(%rbp)
movswl -0x2c(%rbp), %eax
movl $0xa, %ecx
cltd
idivl %ecx
addl $0x30, %edx
movb %dl, %al
movb %al, 0x40f58d(%rip)     # 0x45d6d7
movswl -0x2c(%rbp), %eax
movl $0xa, %ecx
cltd
idivl %ecx
movl $0xa, %ecx
cltd
idivl %ecx
addl $0x30, %edx
movb %dl, %al
movb %al, 0x40f56d(%rip)     # 0x45d6d6
movswl -0x2c(%rbp), %eax
movl $0x64, %ecx
cltd
idivl %ecx
movl $0xa, %ecx
cltd
idivl %ecx
addl $0x30, %edx
movb %dl, %al
movb %al, 0x40f54d(%rip)     # 0x45d6d5
leaq 0x40f541(%rip), %rdi    # 0x45d6d0
xorl %esi, %esi
callq 0x2fee0
movw %ax, -0x26(%rbp)
cwtl
cmpl $-0x1, %eax
je 0x4e3e3
movswl -0x26(%rbp), %edi
leaq 0x40f539(%rip), %rsi    # 0x45d6e8
movl $0x4, %edx
callq 0x302b0
movl 0x40f529(%rip), %eax    # 0x45d6e8
movl %eax, 0x40f523(%rip)    # 0x45d6e8
cmpl $0x1, 0x40f51c(%rip)    # 0x45d6e8
je 0x4e1ef
leaq 0x65176(%rip), %rdi     # 0xb334b
leaq 0x40f4f4(%rip), %rsi    # 0x45d6d0
movb $0x0, %al
callq 0x37c90
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x4e5c5
movswl -0x26(%rbp), %edi
leaq 0x40f4f2(%rip), %rsi    # 0x45d6ec
movl $0x4, %edx
callq 0x302b0
movl 0x40f4e2(%rip), %eax    # 0x45d6ec
movl %eax, 0x40f4dc(%rip)    # 0x45d6ec
movswl -0x26(%rbp), %edi
leaq -0x1c(%rbp), %rsi
movl $0x4, %edx
callq 0x302b0
movl -0x1c(%rbp), %eax
movl %eax, -0x1c(%rbp)
movswl -0x26(%rbp), %edi
leaq -0x20(%rbp), %rsi
movl $0x4, %edx
callq 0x302b0
movl -0x20(%rbp), %eax
movl %eax, -0x20(%rbp)
movswl -0x26(%rbp), %edi
movslq -0x1c(%rbp), %rax
leaq 0x3fcb31(%rip), %rsi    # 0x44ad80
shlq %rax
addq %rax, %rsi
movl -0x20(%rbp), %edx
subl -0x1c(%rbp), %edx
addl $0x1, %edx
shll %edx
callq 0x302b0
movswl -0x26(%rbp), %edi
movslq -0x1c(%rbp), %rax
leaq 0x3ee2cc(%rip), %rsi    # 0x43c540
shlq %rax
addq %rax, %rsi
movl -0x20(%rbp), %edx
subl -0x1c(%rbp), %edx
addl $0x1, %edx
shll %edx
callq 0x302b0
movswl -0x26(%rbp), %edi
movslq -0x1c(%rbp), %rax
leaq 0x229327(%rip), %rsi    # 0x2775c0
shlq $0x2, %rax
addq %rax, %rsi
movl -0x20(%rbp), %edx
subl -0x1c(%rbp), %edx
addl $0x1, %edx
shll $0x2, %edx
callq 0x302b0
movl -0x1c(%rbp), %eax
movw %ax, -0x28(%rbp)
movswl -0x28(%rbp), %eax
cmpl -0x20(%rbp), %eax
jg 0x4e32d
movswq -0x28(%rbp), %rcx
leaq 0x3fcab3(%rip), %rax    # 0x44ad80
movw (%rax,%rcx,2), %dx
movswq -0x28(%rbp), %rcx
leaq 0x3fcaa3(%rip), %rax    # 0x44ad80
movw %dx, (%rax,%rcx,2)
movswq -0x28(%rbp), %rcx
leaq 0x3ee253(%rip), %rax    # 0x43c540
movw (%rax,%rcx,2), %dx
movswq -0x28(%rbp), %rcx
leaq 0x3ee243(%rip), %rax    # 0x43c540
movw %dx, (%rax,%rcx,2)
movswq -0x28(%rbp), %rcx
leaq 0x2292b3(%rip), %rax    # 0x2775c0
movl (%rax,%rcx,4), %edx
movswq -0x28(%rbp), %rcx
leaq 0x2292a4(%rip), %rax    # 0x2775c0
movl %edx, (%rax,%rcx,4)
movw -0x28(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x28(%rbp)
jmp 0x4e2b8
movl -0x20(%rbp), %eax
subl -0x1c(%rbp), %eax
addl $0x1, %eax
shll $0x3, %eax
addl $0x10, %eax
movl %eax, -0x18(%rbp)
movl -0x1c(%rbp), %eax
movw %ax, -0x28(%rbp)
movswl -0x28(%rbp), %eax
cmpl -0x20(%rbp), %eax
jg 0x4e3cb
movw -0x2c(%rbp), %ax
movb %al, %dl
movswq -0x28(%rbp), %rcx
leaq 0x40f38f(%rip), %rax    # 0x45d6f0
movb %dl, (%rax,%rcx)
movl -0x18(%rbp), %edx
movswq -0x28(%rbp), %rcx
leaq 0x41177d(%rip), %rax    # 0x45faf0
movl %edx, (%rax,%rcx,4)
movswq -0x28(%rbp), %rcx
leaq 0x3fc9fe(%rip), %rax    # 0x44ad80
movswl (%rax,%rcx,2), %eax
movswq -0x28(%rbp), %rdx
leaq 0x3ee1ae(%rip), %rcx    # 0x43c540
movswl (%rcx,%rdx,2), %ecx
imull %ecx, %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
addl -0x18(%rbp), %eax
movl %eax, -0x18(%rbp)
movl -0x24(%rbp), %eax
addl $0xf, %eax
andl $-0x10, %eax
addl 0x1a71bc(%rip), %eax    # 0x1f5570
movl %eax, 0x1a71b6(%rip)    # 0x1f5570
movw -0x28(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x28(%rbp)
jmp 0x4e346
movswl -0x26(%rbp), %edi
callq 0x30960
movl 0x40f30a(%rip), %eax    # 0x45d6e4
addl $0x1, %eax
movl %eax, 0x40f301(%rip)    # 0x45d6e4
jmp 0x4e3e5
movswl -0x2c(%rbp), %eax
cmpl 0x40f2f5(%rip), %eax    # 0x45d6e4
jne 0x4e129
leaq 0x41a6f4(%rip), %rdi    # 0x468af0
movl $0x120, %esi            # imm = 0x120
xorl %edx, %edx
callq 0x6ed50
callq 0x33280
movl -0x14(%rbp), %ecx
cmpq %rcx, %rax
ja 0x4e431
callq 0x33280
movl $0x64, %ecx
xorl %edx, %edx
divq %rcx
imulq $0x3c, %rax, %rax
movq %rax, 0x1a7149(%rip)    # 0x1f5578
jmp 0x4e43c
movslq -0x14(%rbp), %rax
movq %rax, 0x1a713c(%rip)    # 0x1f5578
jmp 0x4e43e
movq 0x1a7133(%rip), %rdi    # 0x1f5578
callq 0x35ef0
movq %rax, 0x1a7117(%rip)    # 0x1f5568
cmpq $0x0, %rax
jne 0x4e486
movq 0x1a711a(%rip), %rax    # 0x1f5578
subq $0x10000, %rax          # imm = 0x10000
movq %rax, 0x1a710d(%rip)    # 0x1f5578
cmpq $0x10000, 0x1a7102(%rip) # imm = 0x10000
jae 0x4e484
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x4e5c5
jmp 0x4e43e
movq 0x1a70db(%rip), %rdi    # 0x1f5568
movq 0x1a70e4(%rip), %rsi    # 0x1f5578
callq 0x2e630
movw $0x0, -0x28(%rbp)
movswl -0x28(%rbp), %eax
cmpl $0x2400, %eax           # imm = 0x2400
jge 0x4e5a0
movw $0xf, -0x2a(%rbp)
movswl -0x2a(%rbp), %ecx
xorl %eax, %eax
cmpl $0x1, %ecx
movb %al, -0x2d(%rbp)
jle 0x4e4e9
movswq -0x2a(%rbp), %rcx
leaq 0x927d2(%rip), %rax     # 0xe0ca0
movl (%rax,%rcx,4), %eax
movswq -0x28(%rbp), %rdx
leaq 0x3fc8a3(%rip), %rcx    # 0x44ad80
movswl (%rcx,%rdx,2), %ecx
cmpl %ecx, %eax
setg %al
movb %al, -0x2d(%rbp)
movb -0x2d(%rbp), %al
testb $0x1, %al
jne 0x4e4f2
jmp 0x4e500
movw -0x2a(%rbp), %ax
addw $-0x1, %ax
movw %ax, -0x2a(%rbp)
jmp 0x4e4b4
movw -0x2a(%rbp), %ax
movb %al, %dl
movswq -0x28(%rbp), %rcx
leaq 0x40109e(%rip), %rax    # 0x44f5b0
movb %dl, (%rax,%rcx)
movw $0xf, -0x2a(%rbp)
movswl -0x2a(%rbp), %ecx
xorl %eax, %eax
cmpl $0x1, %ecx
movb %al, -0x2e(%rbp)
jle 0x4e550
movswq -0x2a(%rbp), %rcx
leaq 0x9276b(%rip), %rax     # 0xe0ca0
movl (%rax,%rcx,4), %eax
movswq -0x28(%rbp), %rdx
leaq 0x3edffc(%rip), %rcx    # 0x43c540
movswl (%rcx,%rdx,2), %ecx
cmpl %ecx, %eax
setg %al
movb %al, -0x2e(%rbp)
movb -0x2e(%rbp), %al
testb $0x1, %al
jne 0x4e559
jmp 0x4e567
movw -0x2a(%rbp), %ax
addw $-0x1, %ax
movw %ax, -0x2a(%rbp)
jmp 0x4e51b
movswl -0x2a(%rbp), %eax
shll $0x4, %eax
movzbl %al, %edx
movswq -0x28(%rbp), %rcx
leaq 0x401033(%rip), %rax    # 0x44f5b0
movzbl (%rax,%rcx), %eax
addl %edx, %eax
movb %al, %dl
leaq 0x401024(%rip), %rax    # 0x44f5b0
movb %dl, (%rax,%rcx)
movw -0x28(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x28(%rbp)
jmp 0x4e49f
movl $0xffffffff, 0x92b92(%rip) # imm = 0xFFFFFFFF
movl $0xffffffff, 0x41a9bc(%rip) # imm = 0xFFFFFFFF
movl $0x0, 0x41a9b6(%rip)    # 0x468f74
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nop