// File: /workspace/github_c_makefile_star2_O3/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 0x299e
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 0x2987
addq %rax, %rsi
movl %ebx, (%rsi)
testq %r8, %r8
je 0x2881
movq %r15, %rcx
shlq $0x5, %rcx
cmpl %ebx, -0x40(%rax,%rcx)
jae 0x289d
movl 0x20(%rdi), %ecx
cmpq %rcx, %r15
je 0x299e
movq %r15, %rcx
shlq $0x5, %rcx
cmpl %ebx, (%rax,%rcx)
ja 0x299e
movups (%rsi), %xmm0
movups 0x10(%rsi), %xmm1
movaps %xmm1, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
cmpl %ebx, %edx
movq %r8, 0x18(%rsp)
jbe 0x28e1
movl %r8d, %ecx
cmpq $0x2, %r15
jb 0x2931
leaq -0x20(%rax), %rdx
movl %r15d, %ecx
leal -0x1(%rcx), %esi
movq %rsi, %rdi
shlq $0x5, %rdi
cmpl %ebx, (%rdx,%rdi)
jb 0x292f
movl %esi, %ecx
cmpl $0x1, %esi
jg 0x28c7
xorl %ecx, %ecx
jmp 0x2931
movslq %ebp, %r11
addq %r13, %r11
shlq $0x5, %r11
movl 0x20(%rdi), %ecx
decl %ecx
leaq (%rax,%r12), %r8
movq %rbp, %r9
movq %r9, %r14
movq %r11, %rdx
cmpl %r14d, %ecx
jle 0x2916
leaq 0x1(%r14), %r9
leaq 0x20(%rdx), %r11
leaq 0x20(%r8), %r10
cmpl %ebx, (%r8)
movq %r10, %r8
jbe 0x28f7
movq %r15, %rcx
shlq $0x5, %rcx
leaq (%rax,%rcx), %rdi
addq $-0x40, %rdi
callq 0x1110
movl %r14d, %r14d
jmp 0x295c
decl %ecx
movslq %ecx, %rcx
movl %ecx, %r14d
movq %r8, %rdx
subq %rcx, %rdx
shlq $0x5, %rcx
leaq (%rax,%rcx), %rdi
addq $0x20, %rdi
movq %r14, %rsi
shlq $0x5, %rsi
addq %rax, %rsi
shlq $0x5, %rdx
callq 0x1110
movq 0x10(%rsp), %rdi
movq 0x18(%rdi), %rax
shlq $0x5, %r14
movaps 0x20(%rsp), %xmm0
movaps 0x30(%rsp), %xmm1
movups %xmm1, 0x10(%rax,%r14)
movups %xmm0, (%rax,%r14)
movl 0xc(%rsp), %edx
movq 0x18(%rsp), %r8
decq %rbp
incq %r13
addq $-0x20, %r12
cmpq $0x1, %r15
movq %r8, %r15
jg 0x2852
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq