// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl $0x0, -0x24(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x10(%rbp), %rax
addq $0xf, %rax
andq $-0x10, %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
cmpq 0x18cb22(%rip), %rax    # 0x1bb250
jbe 0x2e76e
movq 0x18cb19(%rip), %rsi    # 0x1bb250
leaq 0x837c2(%rip), %rdi     # 0xb1f00
movb $0x0, %al
callq 0x37c90
movq -0x8(%rbp), %rsi
movq -0x10(%rbp), %rdx
movq -0x18(%rbp), %rax
movzbl (%rax), %ecx
leaq 0x837b5(%rip), %rdi     # 0xb1f10
movb $0x0, %al
callq 0x37c90
leaq 0x837d5(%rip), %rdi     # 0xb1f3e
callq 0x2ec10
movq -0x18(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x0, %eax
jne 0x2e786
leaq 0x837dd(%rip), %rdi     # 0xb1f5e
callq 0x2ec10
movq $-0x1, -0x40(%rbp)
movq 0x18cabb(%rip), %rax    # 0x1bb250
movq %rax, -0x50(%rbp)
movl 0x18c781(%rip), %eax    # 0x1baf20
subl $0x1, %eax
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jl 0x2e92a
movslq -0x1c(%rbp), %rcx
leaq 0x18caa6(%rip), %rax    # 0x1bb260
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rcx
movq -0x50(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
addq -0x10(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpq 0x18ca69(%rip), %rax    # 0x1bb250
jbe 0x2e7ee
jmp 0x2e91c
movq $0x0, -0x38(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x30(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0x20(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x58(%rbp), %rax
jae 0x2e8ef
movslq -0x20(%rbp), %rcx
leaq 0x18ca43(%rip), %rax    # 0x1bb260
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movzbl (%rax), %eax
cmpl $0x0, %eax
jne 0x2e835
jmp 0x2e8c2
movslq -0x20(%rbp), %rcx
leaq 0x18ca20(%rip), %rax    # 0x1bb260
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movzbl (%rax), %eax
cmpl $0xc8, %eax
jl 0x2e862
movq $-0x1, -0x38(%rbp)
jmp 0x2e8ef
movslq -0x20(%rbp), %rcx
leaq 0x18c9f3(%rip), %rax    # 0x1bb260
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
addq $0x10000, %rax          # imm = 0x10000
movslq -0x20(%rbp), %rdx
leaq 0x18c9d7(%rip), %rcx    # 0x1bb260
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movq 0x10(%rcx), %rcx
movzbl (%rcx), %ecx
movl %ecx, %edx
leaq 0x18c690(%rip), %rcx    # 0x1baf30
movslq (%rcx,%rdx,4), %rcx
imulq %rcx, %rax
shrq $0x20, %rax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x40(%rbp), %rax
jb 0x2e8c0
jmp 0x2e8ef
jmp 0x2e8c2
movl -0x20(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x20(%rbp)
movslq %eax, %rcx
leaq 0x18c989(%rip), %rax    # 0x1bb260
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0x2e804
movq -0x38(%rbp), %rax
cmpq -0x40(%rbp), %rax
jae 0x2e91a
movq -0x38(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x48(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0x24(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x2e918
jmp 0x2e92a
jmp 0x2e91a
jmp 0x2e91c
movl -0x1c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x2e7a5
cmpq $-0x1, -0x40(%rbp)
jne 0x2e93d
leaq 0x83647(%rip), %rdi     # 0xb1f7f
callq 0x2ec10
xorl %eax, %eax
subq -0x10(%rbp), %rax
movq %rax, -0x60(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0x28(%rbp)
cmpq $0x0, -0x60(%rbp)
jge 0x2e9b7
movslq -0x28(%rbp), %rcx
leaq 0x18c901(%rip), %rax    # 0x1bb260
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
cmpb $0x0, (%rax)
je 0x2e98b
movslq -0x28(%rbp), %rcx
leaq 0x18c8e6(%rip), %rax    # 0x1bb260
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq $0x0, (%rax)
jmp 0x2e98d
movl -0x28(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x28(%rbp)
movslq %eax, %rcx
leaq 0x18c8be(%rip), %rax    # 0x1bb260
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
jmp 0x2e94d
movl -0x24(%rbp), %ecx
addl $0x1, %ecx
movl -0x28(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %ecx
movl 0x18c552(%rip), %eax    # 0x1baf20
subl %ecx, %eax
movl %eax, 0x18c54a(%rip)    # 0x1baf20
movl -0x24(%rbp), %eax
addl -0x28(%rbp), %eax
cltq
leaq 0x18c87b(%rip), %rdi    # 0x1bb260
imulq $0x18, %rax, %rax
addq %rax, %rdi
movslq -0x24(%rbp), %rax
leaq 0x18c869(%rip), %rsi    # 0x1bb260
imulq $0x18, %rax, %rax
addq %rax, %rsi
movl 0x18c51c(%rip), %eax    # 0x1baf20
subl -0x24(%rbp), %eax
cltq
imulq $0x18, %rax, %rax
movl %eax, %edx
callq 0x6ef00
movq -0x8(%rbp), %rcx
movslq -0x24(%rbp), %rdx
leaq 0x18c83d(%rip), %rax    # 0x1bb260
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movq 0x18c4e4(%rip), %rcx    # 0x1baf18
addq -0x48(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rcx
movslq -0x24(%rbp), %rdx
leaq 0x18c812(%rip), %rax    # 0x1bb260
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rcx
movslq -0x24(%rbp), %rdx
leaq 0x18c7f8(%rip), %rax    # 0x1bb260
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x10(%rax)
movl 0x18c497(%rip), %eax    # 0x1baf10
addl $0x1, %eax
movl %eax, 0x18c48e(%rip)    # 0x1baf10
cmpq $0x0, -0x60(%rbp)
jg 0x2ea8e
jmp 0x2ec02
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl 0x18c480(%rip), %eax    # 0x1baf20
jne 0x2eb05
movl 0x18c478(%rip), %eax    # 0x1baf20
addl $0x1, %eax
movl %eax, 0x18c46f(%rip)    # 0x1baf20
cmpl $0x2400, 0x18c465(%rip) # imm = 0x2400
jle 0x2eac9
leaq 0x834d6(%rip), %rdi     # 0xb1f9a
callq 0x2ec10
movq -0x60(%rbp), %rcx
movslq -0x24(%rbp), %rdx
leaq 0x18c788(%rip), %rax    # 0x1bb260
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movslq -0x24(%rbp), %rcx
leaq 0x18c772(%rip), %rax    # 0x1bb260
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
leaq 0x18c418(%rip), %rcx    # 0x1baf14
movq %rcx, 0x10(%rax)
jmp 0x2ec02
movslq -0x24(%rbp), %rcx
leaq 0x18c750(%rip), %rax    # 0x1bb260
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movzbl (%rax), %eax
cmpl $0x0, %eax
jne 0x2eb46
movq -0x60(%rbp), %rcx
movslq -0x24(%rbp), %rdx
leaq 0x18c72e(%rip), %rax    # 0x1bb260
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
addq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
jmp 0x2ec02
movl 0x18c3d4(%rip), %eax    # 0x1baf20
addl $0x1, %eax
movl %eax, 0x18c3cb(%rip)    # 0x1baf20
cmpl $0x2400, 0x18c3c1(%rip) # imm = 0x2400
jle 0x2eb6d
leaq 0x83432(%rip), %rdi     # 0xb1f9a
callq 0x2ec10
movl 0x18c3ad(%rip), %eax    # 0x1baf20
subl $0x1, %eax
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x24(%rbp), %eax
jle 0x2ebcb
movslq -0x1c(%rbp), %rcx
leaq 0x18c6d4(%rip), %rax    # 0x1bb260
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl -0x1c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rdx
leaq 0x18c6bd(%rip), %rcx    # 0x1bb260
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rdx
movq %rdx, 0x8(%rax)
movq 0x10(%rcx), %rcx
movq %rcx, 0x10(%rax)
movl -0x1c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x2eb79
movq -0x60(%rbp), %rcx
movslq -0x24(%rbp), %rdx
leaq 0x18c686(%rip), %rax    # 0x1bb260
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movslq -0x24(%rbp), %rcx
leaq 0x18c670(%rip), %rax    # 0x1bb260
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
leaq 0x18c316(%rip), %rcx    # 0x1baf14
movq %rcx, 0x10(%rax)
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax,%rax)