// File: /workspace/github_c_makefile_star2_O3/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 $0x38, %rsp
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
addq $0xf, %r15
andq $-0x10, %r15
movq 0x15f07a(%rip), %rsi    # 0x17e6f0
cmpq %rsi, %r15
ja 0x1f93c
movq %rbx, 0x30(%rsp)
movq %r14, 0x28(%rsp)
cmpb $0x0, (%r14)
je 0x1f96e
movslq 0x15ed26(%rip), %rdx    # 0x17e3c0
testq %rdx, %rdx
jle 0x1f7aa
leal -0x1(%rdx), %eax
leaq (%rax,%rax,2), %rax
leaq 0x15f04f(%rip), %r13    # 0x17e700
leaq 0x8(,%rax,8), %rcx
addq %r13, %rcx
movq $0x0, 0x8(%rsp)
movq $-0x1, %r9
leaq 0x15ecfd(%rip), %r8     # 0x17e3d0
movq %rdx, 0x10(%rsp)
movq %rsi, %rdi
movq $0x0, 0x18(%rsp)
movq %r15, 0x20(%rsp)
movq %rdx, %r10
decq %rdx
leaq (%r10,%r10,2), %rax
subq -0x10(%r13,%rax,8), %rdi
leaq (%rdi,%r15), %rbx
cmpq %rsi, %rbx
ja 0x1f78c
cmpq %rbx, %rdi
jae 0x1f77a
movq %r13, %r15
movq %rcx, %r14
xorl %r11d, %r11d
movq %rdi, %r12
movq 0x8(%r14), %rax
movzbl (%rax), %ebp
testq %rbp, %rbp
je 0x1f748
cmpb $-0x39, %bpl
ja 0x1f784
movq (%r14), %rax
leaq 0x10000(%rax), %r13
movslq (%r8,%rbp,4), %rbp
imulq %r13, %rbp
shrq $0x20, %rbp
addq %rbp, %r11
cmpq %r9, %r11
jb 0x1f74b
jmp 0x1f757
movq (%r14), %rax
addq %rax, %r12
addq $0x18, %r14
cmpq %rbx, %r12
jb 0x1f716
cmpq %r9, %r11
movq %r15, %r13
movq 0x20(%rsp), %r15
jae 0x1f78c
movl %edx, %eax
movq %rax, 0x18(%rsp)
movq %r11, %r9
movq %rdi, 0x8(%rsp)
testq %r11, %r11
jne 0x1f78c
jmp 0x1f7b6
testq %r9, %r9
jne 0x1f7b6
xorl %r9d, %r9d
jmp 0x1f78c
movq %r15, %r13
movq 0x20(%rsp), %r15
addq $-0x18, %rcx
cmpq $0x1, %r10
jg 0x1f6e9
cmpq $-0x1, %r9
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %r8
jne 0x1f7c3
leaq 0x56a07(%rip), %rdi     # 0x761b8
callq 0x1f97a
movq %rdi, 0x8(%rsp)
movl %edx, %r8d
movq 0x10(%rsp), %rsi
movq %r15, %rbx
negq %rbx
js 0x1f7d0
movl %r8d, %ecx
jmp 0x1f804
movslq %r8d, %rax
leaq (%rax,%rax,2), %rax
leaq 0x10(,%rax,8), %rdx
addq %r13, %rdx
movl %r8d, %ecx
movq (%rdx), %rax
cmpb $0x0, (%rax)
je 0x1f7f8
movq -0x10(%rdx), %rax
movq $0x0, (%rax)
incl %ecx
addq -0x8(%rdx), %rbx
leaq 0x18(%rdx), %rdx
js 0x1f7e5
leal 0x1(%r8), %r14d
subl %r14d, %ecx
subl %ecx, %esi
movl %esi, 0x15ebad(%rip)    # 0x17e3c0
addl %r8d, %ecx
movslq %ecx, %rax
leaq (%rax,%rax,2), %rax
leaq (,%rax,8), %rdi
addq %r13, %rdi
movl %r8d, %eax
leaq (%rax,%rax,2), %rax
leaq (,%rax,8), %r12
addq %r13, %r12
movq %r8, %rbp
subl %r8d, %esi
shll $0x3, %esi
leal (%rsi,%rsi,2), %edx
movq %r12, %rsi
callq 0x4d437
movq 0x8(%rsp), %rcx
addq 0x15eb5e(%rip), %rcx    # 0x17e3b8
movq 0x30(%rsp), %rax
movq %rax, (%r12)
movq %rcx, (%rax)
movq %r15, 0x8(%r12)
movq 0x28(%rsp), %rax
movq %rax, 0x10(%r12)
incl 0x15eb35(%rip)          # 0x17e3b0
testq %rbx, %rbx
je 0x1f92d
movl 0x15eb36(%rip), %eax    # 0x17e3c0
cmpl %eax, %r14d
jne 0x1f8ba
leal 0x2(%rbp), %eax
movl %eax, 0x15eb28(%rip)    # 0x17e3c0
cmpl $0x23ff, %ebp           # imm = 0x23FF
jge 0x1f8e5
movl %r14d, %eax
leaq (%rax,%rax,2), %rax
movq %rbx, 0x8(%r13,%rax,8)
leaq 0x15eb01(%rip), %rcx    # 0x17e3b4
movq %rcx, 0x10(%r13,%rax,8)
jmp 0x1f92d
movl %r14d, %ecx
leaq (%rcx,%rcx,2), %rcx
leaq (,%rcx,8), %rcx
addq %r13, %rcx
movq 0x10(%rcx), %rdx
cmpb $0x0, (%rdx)
je 0x1f929
leal 0x1(%rax), %edx
movl %edx, 0x15eae2(%rip)    # 0x17e3c0
cmpl $0x2400, %eax           # imm = 0x2400
jl 0x1f913
leaq 0x568e7(%rip), %rdi     # 0x761d3
callq 0x1f97a
movl %eax, %edx
leaq (%rdx,%rdx,2), %rdx
decl %eax
leaq (%rax,%rax,2), %rsi
movq 0x10(%r13,%rsi,8), %rdi
movq %rdi, 0x10(%r13,%rdx,8)
movups (%r13,%rsi,8), %xmm0
movups %xmm0, (%r13,%rdx,8)
cmpl %r14d, %eax
jg 0x1f8f1
movq %rbx, 0x8(%rcx)
leaq 0x15ea91(%rip), %rax    # 0x17e3b4
movq %rax, 0x10(%rcx)
jmp 0x1f92d
addq %rbx, 0x8(%rcx)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x567f6(%rip), %rdi     # 0x76139
xorl %eax, %eax
callq 0x25221
movzbl (%r14), %ecx
leaq 0x567f4(%rip), %rdi     # 0x76149
movq %rbx, %rsi
movq %r15, %rdx
xorl %eax, %eax
callq 0x25221
leaq 0x5680e(%rip), %rdi     # 0x76177
callq 0x1f97a
leaq 0x56822(%rip), %rdi     # 0x76197
callq 0x1f97a