// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movq -0x8(%rbp), %rax
movl 0x20(%rax), %eax
subl $0x1, %eax
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jl 0x3481
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x14(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl (%rax), %eax
cmpl -0xc(%rbp), %eax
jne 0x3471
movl -0x10(%rbp), %ecx
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x14(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rax
movl %ecx, (%rax)
cmpl $0x0, -0x14(%rbp)
je 0x32d4
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x14(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl (%rax), %eax
cmpl -0x10(%rbp), %eax
jae 0x3309
movl -0x14(%rbp), %eax
movq -0x8(%rbp), %rcx
movl 0x20(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
je 0x3304
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x14(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl (%rax), %eax
cmpl -0x10(%rbp), %eax
jbe 0x3309
jmp 0x3481
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x14(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq %rcx, -0x38(%rbp)
movq 0x8(%rax), %rcx
movq %rcx, -0x30(%rbp)
movq 0x10(%rax), %rcx
movq %rcx, -0x28(%rbp)
movq 0x18(%rax), %rax
movq %rax, -0x20(%rbp)
movl -0xc(%rbp), %eax
cmpl -0x10(%rbp), %eax
jbe 0x33be
movl -0x14(%rbp), %eax
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
jle 0x337d
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x18(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl (%rax), %eax
cmpl -0x10(%rbp), %eax
jae 0x3370
jmp 0x337d
jmp 0x3372
movl -0x18(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x3349
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl -0x18(%rbp), %eax
addl $0x1, %eax
cltq
shlq $0x5, %rax
addq %rax, %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movslq -0x18(%rbp), %rax
shlq $0x5, %rax
addq %rax, %rsi
movl -0x14(%rbp), %eax
subl -0x18(%rbp), %eax
movslq %eax, %rdx
shlq $0x5, %rdx
callq 0x1120
jmp 0x343f
movl -0x14(%rbp), %eax
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
movq -0x8(%rbp), %rcx
movl 0x20(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x3403
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x18(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl (%rax), %eax
cmpl -0x10(%rbp), %eax
jbe 0x33f6
jmp 0x3403
jmp 0x33f8
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x33c4
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl -0x14(%rbp), %eax
subl $0x1, %eax
cltq
shlq $0x5, %rax
addq %rax, %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movslq -0x14(%rbp), %rax
shlq $0x5, %rax
addq %rax, %rsi
movl -0x18(%rbp), %eax
subl -0x14(%rbp), %eax
movslq %eax, %rdx
shlq $0x5, %rdx
callq 0x1120
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x18(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq -0x38(%rbp), %rcx
movq %rcx, (%rax)
movq -0x30(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rcx
movq %rcx, 0x10(%rax)
movq -0x20(%rbp), %rcx
movq %rcx, 0x18(%rax)
jmp 0x3473
movl -0x14(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x326f
xorl %eax, %eax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)