// File: /workspace/github_c_makefile_star2_O3/jonof[P]jftekwar/jfbuild/src/cache1d.c
// Function: klistaddentry
static int klistaddentry(CACHE1D_FIND_REC **rec, char *name, int type, int source)
{
	CACHE1D_FIND_REC *r = NULL, *attach = NULL;

	if (*rec) {
		int insensitive, v;
		CACHE1D_FIND_REC *last = NULL;
		
		for (attach = *rec; attach; last = attach, attach = attach->next) {
			if (type == CACHE1D_FIND_DRIVE) continue;	// we just want to get to the end for drives
#ifdef _WIN32
			insensitive = 1;
#else
			if (source == CACHE1D_SOURCE_GRP || attach->source == CACHE1D_SOURCE_GRP)
				insensitive = 1;
			else if (source == CACHE1D_SOURCE_ZIP || attach->source == CACHE1D_SOURCE_ZIP)
				insensitive = 1;
			else
				insensitive = 0;
#endif
			if (insensitive) v = Bstrcasecmp(name, attach->name);
			else v = Bstrcmp(name, attach->name);
			
			// sorted list
			if (v > 0) continue;	// item to add is bigger than the current one
									// so look for something bigger than us
			if (v < 0) {			// item to add is smaller than the current one
				attach = NULL;		// so wedge it between the current item and the one before
				break;
			}
			
			// matched
			if (source >= attach->source) return 1;	// item to add is of lower priority
			r = attach;
			break;
		}

		// wasn't found in the list, so attach to the end
		if (!attach) attach = last;
	}

	if (r) {
		r->type = type;
		r->source = source;
		return 0;
	}

	r = (CACHE1D_FIND_REC *)malloc(sizeof(CACHE1D_FIND_REC)+strlen(name)+1);
	if (!r) return -1;
	r->name = (char*)r + sizeof(CACHE1D_FIND_REC); strcpy(r->name, name);
	r->type = type;
	r->source = source;
	r->usera = r->userb = NULL;

	if (!attach) {	// we are the first item
		r->prev = NULL;
		r->next = *rec;
		if (*rec) (*rec)->prev = r;
		*rec = r;
	} else {
		r->prev = attach;
		r->next = attach->next;
		if (attach->next) attach->next->prev = r;
		attach->next = r;
	}

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %ecx, %ebp
movl %edx, %r14d
movq %rsi, %r15
movq %rdi, (%rsp)
movq (%rdi), %r13
xorl %ebx, %ebx
testq %r13, %r13
je 0x21144
movq %r13, %rax
movq %rax, %r12
cmpl $0x4, %r14d
je 0x21137
cmpl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
jne 0x21114
movq (%r12), %rsi
movq %r15, %rdi
callq 0x6880
jmp 0x21133
cmpl $0x7ffffffe, %ebp       # imm = 0x7FFFFFFE
je 0x21106
cmpl $0x7ffffffd, 0xc(%r12)  # imm = 0x7FFFFFFD
jg 0x21106
movq (%r12), %rsi
movq %r15, %rdi
callq 0x68d0
testl %eax, %eax
jle 0x211ab
movq 0x10(%r12), %rax
movq %r12, %rbx
testq %rax, %rax
jne 0x210f5
movq %r15, %rdi
callq 0x6420
leaq 0x31(%rax), %rdi
callq 0x69a0
testq %rax, %rax
je 0x211a4
movq %rax, %r12
movq %rax, %rdi
addq $0x30, %rdi
movq %rdi, (%rax)
movq %r15, %rsi
callq 0x6430
movl %r14d, 0x8(%r12)
movl %ebp, 0xc(%r12)
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%r12)
testq %rbx, %rbx
je 0x211c5
movq %rbx, 0x18(%r12)
movq 0x10(%rbx), %rax
movq %rax, 0x10(%r12)
testq %rax, %rax
je 0x2119e
movq %r12, 0x18(%rax)
movq %r12, 0x10(%rbx)
jmp 0x211e3
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x211e5
js 0x21144
movl $0x1, %eax
cmpl %ebp, 0xc(%r12)
jle 0x211e5
movl %r14d, 0x8(%r12)
movl %ebp, 0xc(%r12)
jmp 0x211e3
movq $0x0, 0x18(%r12)
movq %r13, 0x10(%r12)
testq %r13, %r13
je 0x211dc
movq %r12, 0x18(%r13)
movq (%rsp), %rax
movq %r12, (%rax)
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq