// File: /workspace/github_c_makefile_star2_O1/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, %ebx
movq %rcx, -0x58(%rbp)
movq %rdx, %r12
movq %rsi, -0x50(%rbp)
movl %edi, %r14d
movq 0x10(%rbp), %rdi
movq %rdi, -0x30(%rbp)
movq %r8, -0x60(%rbp)
movq %r8, %rsi
callq 0x22c0
testl %eax, %eax
js 0x7b79
cmpl %ebx, %r14d
jne 0x7924
movq -0x60(%rbp), %rdi
movq -0x30(%rbp), %rsi
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x21d0
movl %ebx, %r9d
movq %r14, %rdi
movq %rsp, -0x68(%rbp)
movl %ebx, %r13d
movq %rsp, %r15
leaq 0xf(%r13), %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
testl %ebx, %ebx
movq -0x58(%rbp), %rcx
jle 0x795e
xorl %eax, %eax
movb (%rcx,%rax), %dl
xorb $0x1, %dl
movb %dl, (%r15,%rax)
incq %rax
cmpq %rax, %r13
jne 0x794c
movl %edi, %r11d
movslq %r9d, %r8
testl %edi, %edi
movq -0x50(%rbp), %r10
jle 0x79c0
xorl %eax, %eax
testl %r9d, %r9d
jle 0x79b8
xorl %edx, %edx
cmpb $0x1, (%r15,%rdx)
jne 0x798d
movslq (%r12,%rax,4), %rsi
imulq %r8, %rsi
addq %r10, %rsi
cmpb $0x31, (%rdx,%rsi)
je 0x7997
incq %rdx
cmpq %rdx, %r13
jne 0x7975
jmp 0x79b8
testl %r9d, %r9d
jle 0x79b8
xorl %edx, %edx
cmpb $0x1, (%r15,%rdx)
jne 0x79b0
cmpb $0x30, (%rsi,%rdx)
jne 0x79b0
movb $0x0, (%r15,%rdx)
incq %rdx
cmpq %rdx, %r13
jne 0x799e
incq %rax
cmpq %r11, %rax
jne 0x796e
movq %rsp, %rbx
leaq 0xf(,%r13,4), %rax
andq $-0x10, %rax
subq %rax, %rbx
movq %rbx, %rsp
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
testl %r9d, %r9d
jle 0x7a27
xorl %eax, %eax
movl $0x0, (%rbx,%rax,4)
cmpb $0x1, (%r15,%rax)
jne 0x7a1f
movq %r10, %rsi
xorl %edx, %edx
cmpq %rdx, %rax
je 0x7a0a
cmpb $0x1, (%r15,%rdx)
jne 0x7a0a
cmpb $0x31, (%rsi,%rax)
jne 0x7a0a
incl (%rbx,%rax,4)
incq %rdx
addq %r13, %rsi
cmpq %rdx, %r13
jne 0x79f5
movl (%rbx,%rax,4), %edx
cmpl %r14d, %edx
cmovgl %edx, %r14d
incq %rax
cmpq %r13, %rax
jne 0x79e2
movl %r14d, -0x3c(%rbp)
movq %rbx, -0x98(%rbp)
movq %r8, -0x90(%rbp)
testl %r9d, %r9d
jle 0x7b75
movslq %edi, %rax
movq %rax, -0x78(%rbp)
movl %r9d, %r14d
imull %edi, %r14d
leal (%r14,%rdi), %eax
movslq %eax, %r8
leal 0x1(%rdi), %eax
movl %eax, -0x38(%rbp)
movq -0x30(%rbp), %rax
addq %rax, %r14
addq %r11, %rax
movq %rax, -0x48(%rbp)
xorl %ebx, %ebx
movl %r9d, -0x34(%rbp)
movq %rdi, -0x88(%rbp)
movq %r11, -0x80(%rbp)
movq %r8, -0x70(%rbp)
cmpb $0x1, (%r15,%rbx)
jne 0x7b69
movq -0x98(%rbp), %rax
movl -0x3c(%rbp), %edx
cmpl %edx, (%rax,%rbx,4)
jne 0x7b69
movl %ebx, %edx
imull %r9d, %edx
leaq (%r10,%rbx), %rax
movq -0x78(%rbp), %rsi
movl %ebx, (%r12,%rsi,4)
movb $0x1, (%rcx,%rbx)
movslq %edx, %rdx
movb (%rdx,%rax), %dl
movq -0x30(%rbp), %rsi
movb %dl, (%rsi,%r8)
testl %edi, %edi
movq -0x90(%rbp), %r9
jle 0x7b03
movq %rbx, %r8
imulq %r13, %r8
addq %r10, %r8
movq -0x48(%rbp), %rdx
xorl %esi, %esi
movslq (%r12,%rsi,4), %rdi
movb (%r8,%rdi), %dil
movb %dil, (%r14,%rsi)
movslq (%r12,%rsi,4), %rdi
imulq %r9, %rdi
movb (%rax,%rdi), %dil
movb %dil, (%rdx)
incq %rsi
addq %r13, %rdx
cmpq %rsi, %r11
jne 0x7add
subq $0x8, %rsp
movl -0x38(%rbp), %edi
movq %r10, %rsi
movq %r12, %rdx
movq -0x60(%rbp), %r8
movl -0x34(%rbp), %r9d
pushq -0x30(%rbp)
callq 0x78c6
movq -0x80(%rbp), %r11
addq $0x10, %rsp
movq -0x70(%rbp), %r8
movq -0x30(%rbp), %rax
movb $0x32, (%rax,%r8)
movq -0x88(%rbp), %rdi
testl %edi, %edi
movb $0x32, %dl
jle 0x7b59
movq -0x48(%rbp), %rax
xorl %ecx, %ecx
movb %dl, (%r14,%rcx)
movb %dl, (%rax)
incq %rcx
addq %r13, %rax
cmpq %rcx, %r11
jne 0x7b48
movq -0x58(%rbp), %rcx
movb $0x0, (%rcx,%rbx)
movl -0x34(%rbp), %r9d
movq -0x50(%rbp), %r10
incq %rbx
cmpq %r13, %rbx
jne 0x7a80
movq -0x68(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq