// File: /workspace/github_c_makefile_star2_O1/kernfel[P]fNEAT/cppn.c
// Function: CPPN_update_innov_id
int CPPN_update_innov_id( CPPN *net, unsigned int old_id, unsigned int new_id ) {
	int i,j;
	for ( i=net->num_links-1; i>=0; i-- ) {
		if ( net->links[i].innov_id == old_id ) {
			net->links[i].innov_id = new_id;

			// Check first: Do we need to move at all?
			if (
				( i==0 || net->links[i-1].innov_id < new_id ) // Lowest or younger than lower neighbour
				&& ( i==net->num_links-1 || net->links[i+1].innov_id > new_id ) // Highest or older than higher neighbour
			)
				break;
			
			CPPN_Link tmp;
			// Excise
			memcpy( &tmp, &net->links[i], sizeof tmp );
			if ( old_id > new_id ) {
				// Search downwards...
				for ( j=i; j>0; j-- )
					if ( net->links[j-1].innov_id < new_id )
						break;
				// Move the block [j..i] up by one
				memmove( &net->links[j+1], &net->links[j], (i-j)*sizeof tmp );
			} else {
				// Search upwards...
				for ( j=i; j<net->num_links-1; j++ )
					if ( net->links[j+1].innov_id > new_id )
						break;
				// Move the block [i..j] down by one
				memmove( &net->links[i-1], &net->links[i], (j-i)*sizeof tmp );
			}
			
			// Reinsert
			memcpy( &net->links[j], &tmp, sizeof tmp );
		}
	}

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movslq 0x20(%rdi), %r15
testq %r15, %r15
jle 0x28ef
movl %edx, %ebx
movl %esi, %edx
leal -0x1(%r15), %ebp
movl $0x1, %r13d
subq %r15, %r13
movq %rbp, %r12
shlq $0x5, %r12
addq $0x20, %r12
movq %rdi, 0x10(%rsp)
movl %esi, 0xc(%rsp)
leaq -0x1(%r15), %r8
movq 0x18(%rdi), %rax
movq %r8, %rsi
shlq $0x5, %rsi
cmpl %edx, (%rax,%rsi)
jne 0x28d9
addq %rax, %rsi
movl %ebx, (%rsi)
testq %r8, %r8
je 0x27da
movq %r15, %rcx
shlq $0x5, %rcx
cmpl %ebx, -0x40(%rax,%rcx)
jae 0x27f6
movl 0x20(%rdi), %ecx
cmpq %rcx, %r15
je 0x28ef
movq %r15, %rcx
shlq $0x5, %rcx
cmpl %ebx, (%rax,%rcx)
ja 0x28ef
movups (%rsi), %xmm0
movups 0x10(%rsi), %xmm1
movaps %xmm1, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
cmpl %ebx, %edx
movq %r8, 0x18(%rsp)
jbe 0x283d
movl %r8d, %r14d
cmpq $0x2, %r15
jl 0x2886
leaq -0x20(%rax), %rcx
movl %r15d, %r14d
leal -0x1(%r14), %edx
movq %rdx, %rsi
shlq $0x5, %rsi
cmpl %ebx, (%rcx,%rsi)
jb 0x2883
movl %edx, %r14d
cmpl $0x1, %edx
jg 0x2820
xorl %r14d, %r14d
jmp 0x2886
movslq %ebp, %r11
addq %r13, %r11
shlq $0x5, %r11
movl 0x20(%rdi), %ecx
decl %ecx
leaq (%rax,%r12), %r8
movl %ebp, %r9d
movq %r11, %rdx
movl %r9d, %r14d
cmpl %r9d, %ecx
jle 0x2872
leaq 0x20(%rdx), %r11
leal 0x1(%r14), %r9d
leaq 0x20(%r8), %r10
cmpl %ebx, (%r8)
movq %r10, %r8
jbe 0x2853
movq %r15, %rcx
shlq $0x5, %rcx
leaq (%rax,%rcx), %rdi
addq $-0x40, %rdi
jmp 0x28a8
decl %r14d
movslq %r14d, %rcx
movl %ecx, %esi
movq %r8, %rdx
subq %rcx, %rdx
shlq $0x5, %rcx
leaq (%rax,%rcx), %rdi
addq $0x20, %rdi
shlq $0x5, %rsi
addq %rax, %rsi
shlq $0x5, %rdx
callq 0x1110
movq 0x10(%rsp), %rdi
movq 0x18(%rdi), %rax
movl %r14d, %ecx
shlq $0x5, %rcx
movaps 0x20(%rsp), %xmm0
movaps 0x30(%rsp), %xmm1
movups %xmm1, 0x10(%rax,%rcx)
movups %xmm0, (%rax,%rcx)
movl 0xc(%rsp), %edx
movq 0x18(%rsp), %r8
incq %r13
decl %ebp
addq $-0x20, %r12
cmpq $0x1, %r15
movq %r8, %r15
jg 0x27ab
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq