// File: /workspace/github_c_makefile_star2_O1/jonof[P]jftekwar/jfbuild/src/cache1d.c
// Function: allocache
void allocache(void **newhandle, size_t newbytes, unsigned char *newlockptr)
{
	int z, zz, bestz=0, suckz;
	size_t i, daval, bestval, besto=0, o1, o2;
	ssize_t sucklen;

	newbytes = ((newbytes+15)& ~15);

	if (newbytes > cachesize)
	{
		buildprintf("Cachesize: %zu\n",cachesize);
		buildprintf("Newhandle: 0x%p, Newbytes: %zu, *Newlock: %d\n", (void*)newhandle,newbytes,*newlockptr);
		reportandexit("BUFFER TOO BIG TO FIT IN CACHE!");
	}

	if (*newlockptr == 0)
	{
		reportandexit("ALLOCACHE CALLED WITH LOCK OF 0!");
	}

		//Find best place
	bestval = SIZE_MAX; o1 = cachesize;
	for(z=cacnum-1;z>=0;z--)
	{
		o1 -= cac[z].leng;
		o2 = o1+newbytes; if (o2 > cachesize) continue;

		daval = 0;
		for(i=o1,zz=z;i<o2;i+=cac[zz++].leng)
		{
			if (*cac[zz].lock == 0) continue;
			if (*cac[zz].lock >= 200) { daval = SIZE_MAX; break; }
#if SIZE_MAX > UINT_MAX
			daval += ((cac[zz].leng+65536) * lockrecip[*cac[zz].lock]) >> 32;
#else
			daval += mulscale32(cac[zz].leng+65536,lockrecip[*cac[zz].lock]);
#endif
			if (daval >= bestval) break;
		}
		if (daval < bestval)
		{
			bestval = daval; besto = o1; bestz = z;
			if (bestval == 0) break;
		}
	}

	//printf("%ld %ld %ld\n",besto,newbytes,*newlockptr);

	if (bestval == SIZE_MAX)
		reportandexit("CACHE SPACE ALL LOCKED UP!");

		//Suck things out
	for(sucklen=-newbytes,suckz=bestz;sucklen<0;sucklen+=cac[suckz++].leng)
		if (*cac[suckz].lock) *cac[suckz].hand = 0;

		//Remove all blocks except 1
	suckz -= (bestz+1); cacnum -= suckz;
	copybufbyte(&cac[bestz+suckz],&cac[bestz],(cacnum-bestz)*sizeof(cactype));
	cac[bestz].hand = newhandle; *newhandle = (void*)(cachestart+besto);
	cac[bestz].leng = newbytes;
	cac[bestz].lock = newlockptr;
	cachecount++;

		//Add new empty block if necessary
	if (sucklen <= 0) return;

	bestz++;
	if (bestz == cacnum)
	{
		cacnum++; if (cacnum > MAXCACHEOBJECTS) reportandexit("Too many objects in cache! (cacnum > MAXCACHEOBJECTS)");
		cac[bestz].leng = sucklen;
		cac[bestz].lock = &zerochar;
		return;
	}

	if (*cac[bestz].lock == 0) { cac[bestz].leng += sucklen; return; }

	cacnum++; if (cacnum > MAXCACHEOBJECTS) reportandexit("Too many objects in cache! (cacnum > MAXCACHEOBJECTS)");
	for(z=cacnum-1;z>bestz;z--) cac[z] = cac[z-1];
	cac[bestz].leng = sucklen;
	cac[bestz].lock = &zerochar;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, %r14
movq %rsi, %rbx
movq %rdi, %r13
addq $0xf, %rbx
andq $-0x10, %rbx
movq 0x15e57a(%rip), %rsi    # 0x17d6f0
cmpq %rsi, %rbx
ja 0x1f432
cmpb $0x0, (%r14)
je 0x1f464
movq %r14, 0x20(%rsp)
movslq 0x15e22b(%rip), %rcx    # 0x17d3c0
testq %rcx, %rcx
movq %rbx, 0x10(%rsp)
jle 0x1f28b
movq %r13, 0x18(%rsp)
movq 0x15e541(%rip), %rdx    # 0x17d6f0
leal -0x1(%rcx), %eax
leaq (%rax,%rax,2), %rax
leaq 0x15e543(%rip), %r9     # 0x17d700
leaq 0x8(%r9,%rax,8), %r8
movq $0x0, (%rsp)
movq $-0x1, %rsi
movl $0x10000, %r10d         # imm = 0x10000
leaq 0x15e1f2(%rip), %r11    # 0x17d3d0
movq %rdx, %r13
movq $0x0, 0x8(%rsp)
movq %rcx, %r14
decq %rcx
leaq (%r14,%r14,2), %rdi
subq -0x10(%r9,%rdi,8), %r13
leaq (%rbx,%r13), %r12
cmpq %rdx, %r12
ja 0x1f27b
movq %r9, %rax
cmpq %r12, %r13
jae 0x1f24f
movq %r8, %rbp
xorl %r15d, %r15d
movq %r13, %r9
movq 0x8(%rbp), %rdi
movzbl (%rdi), %edi
testq %rdi, %rdi
je 0x1f240
cmpb $-0x39, %dil
ja 0x1f254
movq (%rbp), %rbx
addq %r10, %rbx
movslq (%r11,%rdi,4), %rdi
imulq %rbx, %rdi
shrq $0x20, %rdi
addq %rdi, %r15
cmpq %rsi, %r15
jae 0x1f25b
addq (%rbp), %r9
addq $0x18, %rbp
cmpq %r12, %r9
jb 0x1f213
jmp 0x1f25b
xorl %r15d, %r15d
jmp 0x1f25b
movq $-0x1, %r15
cmpq %rsi, %r15
movq 0x10(%rsp), %rbx
movq %rax, %r9
jae 0x1f27b
movl %ecx, %eax
movq %rax, 0x8(%rsp)
movq %r15, %rsi
movq %r13, (%rsp)
testq %r15, %r15
je 0x1f299
addq $-0x18, %r8
cmpq $0x1, %r14
jg 0x1f1ea
jmp 0x1f2a4
xorl %eax, %eax
movb $0x1, %cl
movq $0x0, (%rsp)
jmp 0x1f2b5
xorl %esi, %esi
movq %rcx, 0x8(%rsp)
movq %r13, (%rsp)
cmpq $-0x1, %rsi
sete %cl
movq 0x18(%rsp), %r13
movq 0x8(%rsp), %rax
testb %cl, %cl
jne 0x1f470
negq %rbx
leaq 0x15e439(%rip), %r14    # 0x17d700
js 0x1f2cd
movl %eax, %ecx
jmp 0x1f2fd
movslq %eax, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%r14,%rcx,8), %rdx
addq $0x10, %rdx
movl %eax, %ecx
movq (%rdx), %rsi
cmpb $0x0, (%rsi)
je 0x1f2f1
movq -0x10(%rdx), %rsi
movq $0x0, (%rsi)
incl %ecx
addq -0x8(%rdx), %rbx
leaq 0x18(%rdx), %rdx
js 0x1f2de
leal 0x1(%rax), %r15d
subl %r15d, %ecx
movl 0x15e0b6(%rip), %edx    # 0x17d3c0
subl %ecx, %edx
movl %edx, 0x15e0ae(%rip)    # 0x17d3c0
addl %eax, %ecx
movslq %ecx, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%r14,%rcx,8), %rdi
movl %eax, %ecx
leaq (%rcx,%rcx,2), %rcx
leaq (%r14,%rcx,8), %r12
subl %eax, %edx
shll $0x3, %edx
leal (%rdx,%rdx,2), %edx
movq %r12, %rsi
callq 0x4ac52
movq (%rsp), %rax
addq 0x15e074(%rip), %rax    # 0x17d3b8
movq %r13, (%r12)
movq %rax, (%r13)
movq 0x10(%rsp), %rax
movq %rax, 0x8(%r12)
movq 0x20(%rsp), %rax
movq %rax, 0x10(%r12)
incl 0x15e04a(%rip)          # 0x17d3b0
testq %rbx, %rbx
je 0x1f423
movl 0x15e04b(%rip), %ecx    # 0x17d3c0
cmpl %ecx, %r15d
jne 0x1f3a9
leal 0x1(%rcx), %eax
movl %eax, 0x15e03d(%rip)    # 0x17d3c0
cmpl $0x2400, %ecx           # imm = 0x2400
jge 0x1f47c
movl %r15d, %eax
leaq (%rax,%rax,2), %rax
movq %rbx, 0x8(%r14,%rax,8)
leaq 0x15e012(%rip), %rcx    # 0x17d3b4
movq %rcx, 0x10(%r14,%rax,8)
jmp 0x1f423
movl %r15d, %eax
leaq (%rax,%rax,2), %rax
leaq (%r14,%rax,8), %rax
movq 0x10(%rax), %rdx
cmpb $0x0, (%rdx)
je 0x1f41f
leal 0x1(%rcx), %edx
movl %edx, 0x15dffa(%rip)    # 0x17d3c0
cmpl $0x2400, %ecx           # imm = 0x2400
jge 0x1f47c
movl 0x15dfe8(%rip), %ecx    # 0x17d3c0
leal -0x1(%rcx), %edx
cmpl %r15d, %edx
jle 0x1f40e
addl $-0x2, %ecx
leal 0x1(%rcx), %edx
leaq (%rdx,%rdx,2), %rdx
movl %ecx, %esi
leaq (%rsi,%rsi,2), %rsi
movq 0x10(%r14,%rsi,8), %rdi
movq %rdi, 0x10(%r14,%rdx,8)
movups (%r14,%rsi,8), %xmm0
movups %xmm0, (%r14,%rdx,8)
leal -0x1(%rcx), %edx
cmpl %r15d, %ecx
movl %edx, %ecx
jg 0x1f3e3
movq %rbx, 0x8(%rax)
leaq 0x15df9b(%rip), %rcx    # 0x17d3b4
movq %rcx, 0x10(%rax)
jmp 0x1f423
addq %rbx, 0x8(%rax)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x55be4(%rip), %rdi     # 0x7501d
xorl %eax, %eax
callq 0x24cb4
movzbl (%r14), %ecx
leaq 0x55be2(%rip), %rdi     # 0x7502d
movq %r13, %rsi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x24cb4
leaq 0x55bfc(%rip), %rdi     # 0x7505b
callq 0x1f488
leaq 0x55c10(%rip), %rdi     # 0x7507b
callq 0x1f488
leaq 0x55c25(%rip), %rdi     # 0x7509c
callq 0x1f488
leaq 0x55c34(%rip), %rdi     # 0x750b7
callq 0x1f488