// File: /workspace/github_c_makefile_star2_O0/sharpyfox[P]citrusleaf-client/src/cf_queue.c
// Function: cf_queue_resize
int
cf_queue_resize(cf_queue *q, uint32_t new_sz)
{
	// check - a lot of the code explodes badly if queue is not full
	if (CF_Q_SZ(q) != q->allocsz) {
//		cf_debug(CF_QUEUE,"cf_queue: internal error: resize on non-full queue");
		return(-1);
	}
	
	// the rare case where the queue is not fragmented, and realloc makes sense
	// and none of the offsets need to move
	if (0 == q->read_offset % q->allocsz) {
		q->queue = (uint8_t*)realloc(q->queue, new_sz * q->elementsz);
		if (!q->queue) {
//			cf_debug(CF_QUEUE," pfft! out of memory! crash!");
			return(-1);
		}
		q->read_offset = 0;
		q->write_offset = q->allocsz;
	}
	else {
		
		uint8_t *newq = (uint8_t*)malloc(new_sz * q->elementsz);
		if (!newq) {
//			cf_debug(CF_QUEUE," pffth! out of memory! crash!");
			return(-1);
		}
		// endsz is used uint8_ts in the old queue from the insert point to the end
		uint32_t endsz = (uint32_t)((q->allocsz - (q->read_offset % q->allocsz)) * q->elementsz);
		memcpy(&newq[0], CF_Q_ELEM_PTR(q, q->read_offset), endsz);
		memcpy(&newq[endsz], &q->queue[0], (q->allocsz * q->elementsz) - endsz); 
		
		free(q->queue);
		q->queue = newq;

		q->write_offset = q->allocsz;
		q->read_offset = 0;
	}

	q->allocsz = new_sz;
	return(0);	
}

// Assembly:
subq $0x28, %rsp
movq %rdi, 0x18(%rsp)
movl %esi, 0x14(%rsp)
movq 0x18(%rsp), %rax
movl 0x8(%rax), %eax
movq 0x18(%rsp), %rcx
subl 0xc(%rcx), %eax
movq 0x18(%rsp), %rcx
cmpl 0x4(%rcx), %eax
je 0x14104
movl $0xffffffff, 0x24(%rsp) # imm = 0xFFFFFFFF
jmp 0x1429c
movq 0x18(%rsp), %rax
movl 0xc(%rax), %eax
movq 0x18(%rsp), %rcx
xorl %edx, %edx
divl 0x4(%rcx)
xorl %eax, %eax
cmpl %edx, %eax
jne 0x14180
movq 0x18(%rsp), %rax
movq 0x70(%rax), %rdi
movl 0x14(%rsp), %eax
movl %eax, %esi
movq 0x18(%rsp), %rax
imulq 0x10(%rax), %rsi
callq 0x2490
movq %rax, %rcx
movq 0x18(%rsp), %rax
movq %rcx, 0x70(%rax)
movq 0x18(%rsp), %rax
cmpq $0x0, 0x70(%rax)
jne 0x1415f
movl $0xffffffff, 0x24(%rsp) # imm = 0xFFFFFFFF
jmp 0x1429c
movq 0x18(%rsp), %rax
movl $0x0, 0xc(%rax)
movq 0x18(%rsp), %rax
movl 0x4(%rax), %ecx
movq 0x18(%rsp), %rax
movl %ecx, 0x8(%rax)
jmp 0x14288
movl 0x14(%rsp), %eax
movl %eax, %edi
movq 0x18(%rsp), %rax
imulq 0x10(%rax), %rdi
callq 0x2310
movq %rax, 0x8(%rsp)
cmpq $0x0, 0x8(%rsp)
jne 0x141af
movl $0xffffffff, 0x24(%rsp) # imm = 0xFFFFFFFF
jmp 0x1429c
movq 0x18(%rsp), %rax
movl 0x4(%rax), %eax
movl %eax, (%rsp)
movq 0x18(%rsp), %rax
movl 0xc(%rax), %eax
movq 0x18(%rsp), %rcx
xorl %edx, %edx
divl 0x4(%rcx)
movl (%rsp), %eax
subl %edx, %eax
movl %eax, %eax
movq 0x18(%rsp), %rcx
imulq 0x10(%rcx), %rax
movl %eax, 0x4(%rsp)
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %rax
movq 0x70(%rax), %rsi
movq 0x18(%rsp), %rax
movl 0xc(%rax), %eax
movq 0x18(%rsp), %rcx
xorl %edx, %edx
divl 0x4(%rcx)
movl %edx, %eax
movq 0x18(%rsp), %rcx
imulq 0x10(%rcx), %rax
addq %rax, %rsi
movl 0x4(%rsp), %eax
movl %eax, %edx
callq 0x21e0
movq 0x8(%rsp), %rdi
movl 0x4(%rsp), %eax
addq %rax, %rdi
movq 0x18(%rsp), %rax
movq 0x70(%rax), %rsi
movq 0x18(%rsp), %rax
movl 0x4(%rax), %eax
movl %eax, %edx
movq 0x18(%rsp), %rax
imulq 0x10(%rax), %rdx
movl 0x4(%rsp), %eax
subq %rax, %rdx
callq 0x21e0
movq 0x18(%rsp), %rax
movq 0x70(%rax), %rdi
callq 0x22e0
movq 0x8(%rsp), %rcx
movq 0x18(%rsp), %rax
movq %rcx, 0x70(%rax)
movq 0x18(%rsp), %rax
movl 0x4(%rax), %ecx
movq 0x18(%rsp), %rax
movl %ecx, 0x8(%rax)
movq 0x18(%rsp), %rax
movl $0x0, 0xc(%rax)
movl 0x14(%rsp), %ecx
movq 0x18(%rsp), %rax
movl %ecx, 0x4(%rax)
movl $0x0, 0x24(%rsp)
movl 0x24(%rsp), %eax
addq $0x28, %rsp
retq
nopw %cs:(%rax,%rax)
nop