// File: /workspace/github_c_makefile_star2_O2/ComplexNetworks-DCC-FCUP[P]gtrieScanner/Isomorphism.cpp
// Function: Isomorphism::_goCan2(int, char const*, int*, bool*, char*, int, char*)
void Isomorphism::_goCan2(int pos, const char *in, int *perm, bool *used, char *best, int size, char *current) {
  int i, j;

  if (strcmp(current,best)<0) return;

  if (pos==size) {
    strcpy(best, current);
    return;
  }
  
  // At the beggining everyone is a possible candidate
  bool candidate[size];
  for (i=0; i<size; i++)
    if (used[i]) candidate[i]=false;
    else         candidate[i]=true;


  for (i=0; i<pos; i++) {
    for (j=0; j<size ;j++)
      if (candidate[j] && in[perm[i]*size+j]=='1') break;
    if (j<size) // at least one neighbour
      for (j=0; j<size ;j++)
	if (candidate[j] && in[perm[i]*size+j]=='0') candidate[j]=false;
  }

  // Among those that are still candidates, select the
  // biggest neighborhood on those candidates
  int degree[size];
  int max=-1;
  for (i=0; i<size; i++) {
    degree[i]=0;
    if (candidate[i]) {
      for (j=0; j<size; j++)
	if (i!=j && candidate[j] && in[j*size+i]=='1')
	  degree[i]++;
      if (degree[i]>max) max=degree[i];
    }
  }

  for (i=0; i<size; i++)
    if (candidate[i] && degree[i]==max) {
      perm[pos]=i;
      used[i]=true;
      current[pos*size+pos] = in[i*size+i];
      for (j=0; j<pos; j++) {
	current[pos*size+j] = in[i*size+perm[j]]; 
	current[j*size+pos] = in[perm[j]*size+i]; 
      }

      _goCan2(pos+1, in, perm, used, best, size, current);

      current[pos*size+pos] = '2';
      for (j=0; j<pos; j++) {
	current[pos*size+j] = '2';
	current[j*size+pos] = '2';
      }
      used[i]=false;
    }

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movl %r9d, %r14d
movq %rcx, -0x68(%rbp)
movq %rdx, %r12
movq %rsi, -0x60(%rbp)
movl %edi, %ebx
movq 0x10(%rbp), %rdi
movq %rdi, -0x38(%rbp)
movq %r8, -0x70(%rbp)
movq %r8, %rsi
callq 0x2300
testl %eax, %eax
js 0x76ff
cmpl %r14d, %ebx
jne 0x7499
movq -0x70(%rbp), %rdi
movq -0x38(%rbp), %rsi
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x21e0
movq %rsp, -0x78(%rbp)
movl %r14d, %eax
movl %r14d, %edx
movq %rsp, %r14
leaq 0xf(%rax), %rcx
andq $-0x10, %rcx
subq %rcx, %r14
movq %r14, %rsp
xorl %esi, %esi
testl %edx, %edx
movl $0x0, %r13d
movl %edx, -0x2c(%rbp)
cmovgl %edx, %r13d
movq -0x68(%rbp), %rcx
movq -0x60(%rbp), %r10
cmpq %rsi, %r13
je 0x74e1
movb (%rcx,%rsi), %dl
xorb $0x1, %dl
movb %dl, (%r14,%rsi)
incq %rsi
jmp 0x74cd
xorl %edi, %edi
testl %ebx, %ebx
movl $0x0, %r8d
movq %rbx, -0x40(%rbp)
cmovgl %ebx, %r8d
movslq -0x2c(%rbp), %rbx
cmpq %r8, %rdi
je 0x7543
xorl %esi, %esi
cmpq %rsi, %r13
je 0x753e
cmpb $0x1, (%r14,%rsi)
jne 0x751b
movslq (%r12,%rdi,4), %rdx
imulq %rbx, %rdx
addq %r10, %rdx
cmpb $0x31, (%rsi,%rdx)
je 0x7520
incq %rsi
jmp 0x74fe
xorl %esi, %esi
cmpq %rsi, %r13
je 0x753e
cmpb $0x1, (%r14,%rsi)
jne 0x7539
cmpb $0x30, (%rdx,%rsi)
jne 0x7539
movb $0x0, (%r14,%rsi)
incq %rsi
jmp 0x7522
incq %rdi
jmp 0x74f7
movq %r8, -0x58(%rbp)
movq %rsp, %rsi
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
subq %rdx, %rsi
movq %rsi, -0x48(%rbp)
movq %rsi, %rsp
pushq $-0x1
popq %rdi
xorl %r9d, %r9d
movq %r10, %rdx
movq -0x48(%rbp), %r11
cmpq %r13, %r9
je 0x75c2
movq -0x48(%rbp), %rsi
andl $0x0, (%rsi,%r9,4)
cmpb $0x1, (%r14,%r9)
jne 0x75ba
movl %edi, %r15d
xorl %esi, %esi
movq %rdx, %rdi
xorl %r8d, %r8d
cmpq %r8, %rax
je 0x75b1
cmpq %r8, %r9
je 0x75a9
cmpb $0x1, (%r14,%r8)
jne 0x75a9
cmpb $0x31, (%rdi)
jne 0x75a9
incl %esi
movl %esi, (%r11,%r9,4)
incq %r8
addq %rbx, %rdi
jmp 0x758d
movl %r15d, %edi
cmpl %r15d, %esi
cmovgl %esi, %edi
incq %r9
incq %rdx
jmp 0x756d
movq -0x40(%rbp), %r11
movslq %r11d, %rsi
movl -0x2c(%rbp), %eax
imull %r11d, %eax
leal (%rax,%r11), %edx
movslq %edx, %r8
incl %r11d
movq %r11, -0x40(%rbp)
movslq %eax, %r15
movq -0x38(%rbp), %rax
addq %rax, %r15
movq %rsi, -0x90(%rbp)
leaq (%rax,%rsi), %r11
xorl %edx, %edx
movq -0x58(%rbp), %r9
movl %edi, -0x4c(%rbp)
movq %r8, -0x88(%rbp)
movq %r11, -0x80(%rbp)
cmpq %r13, %rdx
je 0x76fb
cmpb $0x1, (%r14,%rdx)
jne 0x76f3
movq -0x48(%rbp), %rax
cmpl %edi, (%rax,%rdx,4)
jne 0x76f3
movq -0x90(%rbp), %rax
movl %edx, (%r12,%rax,4)
movb $0x1, (%rcx,%rdx)
movq %rdx, %rax
imulq %rbx, %rax
addq %r10, %rax
movq %rdx, %rdi
movb (%rdx,%rax), %dl
movq -0x38(%rbp), %rsi
movb %dl, (%rsi,%r8)
movq %rdi, -0x98(%rbp)
leaq (%r10,%rdi), %r8
movq %r11, %rdx
xorl %esi, %esi
cmpq %rsi, %r9
je 0x7687
movslq (%r12,%rsi,4), %rdi
movb (%rax,%rdi), %dil
movb %dil, (%r15,%rsi)
movslq (%r12,%rsi,4), %rdi
imulq %rbx, %rdi
movb (%r8,%rdi), %dil
movb %dil, (%rdx)
incq %rsi
addq %rbx, %rdx
jmp 0x765f
subq $0x8, %rsp
movq -0x40(%rbp), %rdi
movq %r10, %rsi
movq %r12, %rdx
movq -0x70(%rbp), %r8
movl -0x2c(%rbp), %r9d
pushq -0x38(%rbp)
callq 0x743c
movq -0x80(%rbp), %r11
movq -0x58(%rbp), %r9
addq $0x10, %rsp
movq -0x88(%rbp), %r8
movq -0x38(%rbp), %rax
movb $0x32, (%rax,%r8)
movq %r11, %rax
xorl %ecx, %ecx
movl -0x4c(%rbp), %edi
movq -0x98(%rbp), %rdx
movb $0x32, %sil
cmpq %rcx, %r9
je 0x76e7
movb %sil, (%r15,%rcx)
movb %sil, (%rax)
incq %rcx
addq %rbx, %rax
jmp 0x76d3
movq -0x68(%rbp), %rcx
movb $0x0, (%rcx,%rdx)
movq -0x60(%rbp), %r10
incq %rdx
jmp 0x7607
movq -0x78(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq