// File: /workspace/github_c_makefile_star2_O2/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, %r15
movq %rsi, %r14
movq %rdi, %rbx
addq $0xf, %r14
andq $-0x10, %r14
movq 0x15a715(%rip), %rsi    # 0x1776f0
cmpq %rsi, %r14
ja 0x1d25f
movq %rbx, 0x10(%rsp)
movq %r15, 0x8(%rsp)
cmpb $0x0, (%r15)
je 0x1d291
movl 0x15a3c1(%rip), %r8d    # 0x1773c0
pushq $-0x1
popq %rcx
xorl %eax, %eax
leaq 0x15a3c5(%rip), %rdi    # 0x1773d0
movq %r8, %r13
movq %r8, (%rsp)
xorl %edx, %edx
movq %rsi, %r12
movq %rdx, 0x18(%rsp)
movq %rax, 0x20(%rsp)
leal -0x1(%r8), %r10d
testl %r13d, %r13d
jle 0x1d0d1
decl %r8d
decq %r13
movl %r13d, %edx
imulq $0x18, %rdx, %rdx
leaq 0x15a6be(%rip), %rax    # 0x177700
subq 0x8(%rdx,%rax), %r12
leaq (%r12,%r14), %rbx
cmpq %rsi, %rbx
ja 0x1d0b5
movl %r10d, %edx
imulq $0x18, %rdx, %rdx
leaq 0x15a6a2(%rip), %rax    # 0x177700
leaq (%rdx,%rax), %r15
addq $0x8, %r15
movq %r12, %rbp
xorl %r11d, %r11d
cmpq %rbx, %rbp
jae 0x1d0b0
movq 0x8(%r15), %rdx
movzbl (%rdx), %r9d
testq %r9, %r9
je 0x1d0a4
cmpb $-0x39, %r9b
ja 0x1d0b5
movq (%r15), %rdx
leaq 0x10000(%rdx), %rax
movslq (%rdi,%r9,4), %r9
imulq %rax, %r9
shrq $0x20, %r9
addq %r9, %r11
cmpq %rcx, %r11
jb 0x1d0a7
jmp 0x1d0b0
movq (%r15), %rdx
addq %rdx, %rbp
addq $0x18, %r15
jmp 0x1d06c
cmpq %rcx, %r11
jb 0x1d0bd
decl %r10d
jmp 0x1d025
movl %r13d, %eax
movq %r11, %rcx
movq %r12, %rdx
testq %r11, %r11
jne 0x1d017
jmp 0x1d0e5
movq 0x18(%rsp), %r12
movq 0x20(%rsp), %r13
cmpq $-0x1, %rcx
je 0x1d29d
movq %r14, %rbx
negq %rbx
movslq %r13d, %rax
imulq $0x18, %rax, %rax
leaq 0x15a607(%rip), %rcx    # 0x177700
leaq (%rax,%rcx), %rdx
addq $0x10, %rdx
pushq $-0x1
popq %rsi
xorl %ecx, %ecx
testq %rbx, %rbx
jns 0x1d12a
movq (%rdx,%rcx), %rax
cmpb $0x0, (%rax)
je 0x1d11d
movq -0x10(%rdx,%rcx), %rax
andq $0x0, (%rax)
addq -0x8(%rdx,%rcx), %rbx
incl %esi
addq $0x18, %rcx
jmp 0x1d106
movq (%rsp), %rax
imull $0x18, %eax, %edx
subl %esi, %eax
movl %eax, 0x15a287(%rip)    # 0x1773c0
addl %r13d, %esi
movslq %esi, %rax
imulq $0x18, %rax, %rdi
leaq 0x15a5b6(%rip), %rbp    # 0x177700
addq %rbp, %rdi
movl %r13d, %eax
imulq $0x18, %rax, %r15
leaq (%r15,%rbp), %rsi
imull $0x18, %r13d, %eax
subl %eax, %edx
subl %ecx, %edx
addl $0x18, %edx
callq 0x44524
addq 0x15a249(%rip), %r12    # 0x1773b8
movq 0x10(%rsp), %rax
movq %rax, (%r15,%rbp)
movq %r12, (%rax)
movq %r14, 0x8(%r15,%rbp)
movq 0x8(%rsp), %rax
movq %rax, 0x10(%r15,%rbp)
incl 0x15a220(%rip)          # 0x1773b0
testq %rbx, %rbx
je 0x1d250
leal 0x1(%r13), %eax
movl 0x15a21d(%rip), %ecx    # 0x1773c0
cmpl %ecx, %eax
jne 0x1d1de
leal 0x2(%r13), %ecx
movl %ecx, 0x15a20f(%rip)    # 0x1773c0
cmpl $0x23ff, %r13d          # imm = 0x23FF
jge 0x1d2a9
movl %eax, %eax
imulq $0x18, %rax, %rax
leaq 0x15a535(%rip), %rdx    # 0x177700
movq %rbx, 0x8(%rax,%rdx)
leaq 0x15a1dd(%rip), %rcx    # 0x1773b4
movq %rcx, 0x10(%rax,%rdx)
jmp 0x1d250
movl %eax, %edx
imulq $0x18, %rdx, %rsi
leaq 0x15a515(%rip), %rdi    # 0x177700
leaq (%rdi,%rsi), %rdx
movq 0x10(%rsi,%rdi), %rsi
cmpb $0x0, (%rsi)
je 0x1d24c
leal 0x1(%rcx), %esi
movl %esi, 0x15a1be(%rip)    # 0x1773c0
cmpl $0x23ff, %ecx           # imm = 0x23FF
jg 0x1d2a9
leaq 0x15a4eb(%rip), %r9     # 0x177700
cmpl %eax, %ecx
jle 0x1d23b
movl %ecx, %esi
imulq $0x18, %rsi, %rsi
decl %ecx
imulq $0x18, %rcx, %rdi
movq 0x10(%rdi,%r9), %r8
movq %r8, 0x10(%rsi,%r9)
movups (%rdi,%r9), %xmm0
movups %xmm0, (%rsi,%r9)
jmp 0x1d215
movq %rbx, 0x8(%rdx)
leaq 0x15a16e(%rip), %rax    # 0x1773b4
movq %rax, 0x10(%rdx)
jmp 0x1d250
addq %rbx, 0x8(%rdx)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4dde7(%rip), %rdi     # 0x6b04d
xorl %eax, %eax
callq 0x22427
movzbl (%r15), %ecx
leaq 0x4dde5(%rip), %rdi     # 0x6b05d
movq %rbx, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x22427
leaq 0x4ddff(%rip), %rdi     # 0x6b08b
callq 0x1d2b5
leaq 0x4de13(%rip), %rdi     # 0x6b0ab
callq 0x1d2b5
leaq 0x4de28(%rip), %rdi     # 0x6b0cc
callq 0x1d2b5
leaq 0x4de37(%rip), %rdi     # 0x6b0e7
callq 0x1d2b5