// File: /workspace/github_c_makefile_star2_O1/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
testq %r13, %r13
je 0x20d2c
xorl %r12d, %r12d
movq %r13, %rax
movq %rax, %rbx
cmpl $0x4, %r14d
je 0x20d01
cmpl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
jne 0x20cdd
movq (%rbx), %rsi
movq %r15, %rdi
callq 0x6880
jmp 0x20cf9
cmpl $0x7ffffffe, %ebp       # imm = 0x7FFFFFFE
je 0x20cd0
cmpl $0x7ffffffd, 0xc(%rbx)  # imm = 0x7FFFFFFD
jg 0x20cd0
movq (%rbx), %rsi
movq %r15, %rdi
callq 0x68d0
testl %eax, %eax
jle 0x20db1
movq 0x10(%rbx), %rax
movq %rbx, %r12
testq %rax, %rax
jne 0x20cbf
movq %rbx, %r12
xorl %ebx, %ebx
testq %rbx, %rbx
cmovneq %rbx, %r12
movb $0x1, %dl
movq %rbx, %rcx
movq %r12, %rbx
movl $0x1, %eax
testb %dl, %dl
jne 0x20d30
jmp 0x20d3e
xorl %ecx, %ecx
xorl %ebx, %ebx
testq %rcx, %rcx
je 0x20d4d
movl %r14d, 0x8(%rcx)
movl %ebp, 0xc(%rcx)
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %rdi
callq 0x6420
leaq 0x31(%rax), %rdi
callq 0x69a0
testq %rax, %rax
je 0x20dc9
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 0x20dd3
movq %rbx, 0x18(%r12)
movq 0x10(%rbx), %rax
movq %rax, 0x10(%r12)
movq 0x10(%rbx), %rax
testq %rax, %rax
je 0x20dab
movq %r12, 0x18(%rax)
movq %r12, 0x10(%rbx)
jmp 0x20d3c
js 0x20d10
cmpl %ebp, 0xc(%rbx)
jg 0x20d12
xorl %ecx, %ecx
xorl %edx, %edx
jmp 0x20d21
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x20d3e
movq $0x0, 0x18(%r12)
movq %r13, 0x10(%r12)
testq %r13, %r13
je 0x20dea
movq %r12, 0x18(%r13)
movq (%rsp), %rax
movq %r12, (%rax)
jmp 0x20d3c