// File: /workspace/github_c_makefile_star2_O2/ComplexNetworks-DCC-FCUP[P]gtrieScanner/Isomorphism.cpp
// Function: Isomorphism::canonicalBasedNauty(char const*, char*, int)
void Isomorphism::canonicalBasedNauty(const char *in, char *best, int size) {

  int i, j, k, min_i, ss, mymap[size];
  int degree[size], last_degree[size], total_degree[size];
  char used[size], articulation[size];
  int nbfs, bfs[size];

  for (i=0; i<size; i++) {
    degree[i] = 0;
    used[i]=0;
    for (j=0; j<size; j++) {
      if (in[i*size+j]=='1') degree[i]++;
      if (in[i+size*j]=='1') degree[i]++;
    }
    total_degree[i] = last_degree[i] = degree[i];
  }

  // Find the node with the smaller number of connections
  for (ss = size-1; ss>=0; ss--) {

    if (ss>3) {
      // Find articulation points (cannot be removed)
      for (i=0; i<size; i++) {
	if (used[i]) continue;
	used[i]=2;
	j=0; while (used[j]) j++;
	nbfs=1; bfs[0]=j; used[j]=2; j=0;
	while (j<nbfs) {
	  for (k=0; k<size; k++)
	    if (!used[k] && (in[k*size+bfs[j]]=='1' || in[k+size*bfs[j]]=='1')) {
	      used[k]=2;
	      bfs[nbfs++]=k;
	    }
	  j++;
	}
	if (nbfs!=ss) articulation[i]=1;
	else articulation[i]=0;
	for (j=0; j<size; j++)
	  if (used[j]==2) used[j]=0;
      }
    } else for (i=0; i<size; i++) articulation[i]=0;

    min_i = -1;
    for (i=0; i<size; i++) {
      if (used[i] || articulation[i]) continue;
      if (min_i<0 || degree[i] < degree[min_i]) min_i=i;
      else if (degree[i] == degree[min_i]) {
	if (last_degree[i] < last_degree[min_i]) min_i=i;
	else if (last_degree[i] == last_degree[min_i]) {
	  if (total_degree[i] < total_degree[min_i]) min_i = i;
	  else if (total_degree[i] == total_degree[min_i]) {
	    for (j=ss+1; j<size; j++)
	      if (in[i*size+mymap[j]] != in[min_i*size+mymap[j]]) {
		if (in[i*size+mymap[j]]=='0') min_i=i;
		break;
	      }
	    if (j==size) {
	      for (j=ss+1; j<size; j++)
		if (in[i+size*mymap[j]] != in[min_i+size*mymap[j]]) {
		  if (in[i+size*mymap[j]]=='0') min_i=i;
		  break;
		}	      
	    }
	  }
	}
      }
    }
    
    if (ss!=0 && degree[min_i]==0) {
      fprintf(stderr, "[%s]\n", in);
      fprintf(stderr, "!!! OH NO %d [canonicalBasedNauty]\n", ss); fflush(stdout);
      exit(1);
    }
    
    for (i=0; i<size; i++) {
      last_degree[i] = degree[i];
      if (in[i*size+min_i]=='1') degree[i]--;
      if (in[i+size*min_i]=='1') degree[i]--;
    }
    mymap[ss] = min_i;
    used[min_i]=1;
  }

  best[size*size]=0;
  for (i=0; i<n; i++)
    for (j=0; j<n; j++)
      best[i*size+j] = in[mymap[i]*size+mymap[j]];  
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %edx, %r15d
movq %rsi, -0x80(%rbp)
movq %rdi, %rdx
movl %r15d, %r8d
movq %rsp, %rcx
leaq 0xf(,%r8,4), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x68(%rbp)
movq %rcx, %rsp
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x30(%rbp)
movq %rcx, %rsp
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x58(%rbp)
movq %rcx, %rsp
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x70(%rbp)
movq %rcx, %rsp
movq %rsp, %r14
leaq 0xf(%r8), %rcx
andq $-0x10, %rcx
subq %rcx, %r14
movq %r14, %rsp
movq %rsp, %rsi
subq %rcx, %rsi
movq %rsi, -0x60(%rbp)
movq %rsi, %rsp
movq %rsp, %r12
subq %rax, %r12
movq %r12, %rsp
movslq %r15d, %rdi
xorl %r10d, %r10d
testl %edi, %edi
movl $0x0, %r13d
cmovgl %r15d, %r13d
movq %rdx, %rsi
movq %rdx, %r11
cmpq %r13, %r10
je 0x6f33
movq -0x30(%rbp), %rax
andl $0x0, (%rax,%r10,4)
movb $0x0, (%r14,%r10)
xorl %eax, %eax
movq %rsi, %rcx
xorl %ebx, %ebx
cmpq %rbx, %r8
je 0x6f18
cmpb $0x31, (%r11,%rbx)
jne 0x6f01
incl %eax
movq -0x30(%rbp), %r9
movl %eax, (%r9,%r10,4)
cmpb $0x31, (%rcx)
jne 0x6f10
incl %eax
movq -0x30(%rbp), %r9
movl %eax, (%r9,%r10,4)
incq %rbx
addq %rdi, %rcx
jmp 0x6eeb
movq -0x58(%rbp), %rcx
movl %eax, (%rcx,%r10,4)
movq -0x70(%rbp), %rcx
movl %eax, (%rcx,%r10,4)
incq %r10
addq %rdi, %r11
incq %rsi
jmp 0x6ed1
movq -0x68(%rbp), %r10
leaq (%r10,%r8,4), %rax
movq %rax, -0x88(%rbp)
xorl %r11d, %r11d
movq %r10, -0x78(%rbp)
movq $0x0, -0x40(%rbp)
movq %r8, %rax
movq -0x30(%rbp), %rsi
testl %eax, %eax
jle 0x71ef
leaq -0x1(%rax), %rcx
movq %rcx, -0x38(%rbp)
cmpq $0x4, %rax
jbe 0x702c
xorl %r10d, %r10d
cmpq %r13, %r10
je 0x7040
cmpb $0x0, (%r14,%r10)
je 0x6f8a
incq %r10
jmp 0x6f75
movb $0x2, (%r14,%r10)
pushq $-0x1
popq %rax
cmpb $0x0, 0x1(%r14,%rax)
leaq 0x1(%rax), %rax
jne 0x6f92
movl %eax, (%r12)
movb $0x2, (%r14,%rax)
xorl %esi, %esi
pushq $0x1
popq %rbx
movslq %ebx, %rax
cmpq %rax, %rsi
jge 0x6ffc
movq %rdx, %rax
xorl %ecx, %ecx
cmpq %rcx, %r8
je 0x6ff7
cmpb $0x0, (%r14,%rcx)
jne 0x6fef
movslq (%r12,%rsi,4), %r9
cmpb $0x31, (%rax,%r9)
je 0x6fe1
imull %r15d, %r9d
addl %ecx, %r9d
movslq %r9d, %r9
cmpb $0x31, (%rdx,%r9)
jne 0x6fef
movb $0x2, (%r14,%rcx)
movslq %ebx, %r9
incl %ebx
movl %ecx, (%r12,%r9,4)
incq %rcx
addq %rdi, %rax
jmp 0x6fb9
incq %rsi
jmp 0x6fac
cmpl -0x38(%rbp), %ebx
movq -0x60(%rbp), %rax
setne (%rax,%r10)
xorl %eax, %eax
movq -0x30(%rbp), %rsi
cmpq %rax, %r13
je 0x6f85
movzbl (%r14,%rax), %ecx
cmpb $0x2, %cl
cmovel %r11d, %ecx
movb %cl, (%r14,%rax)
incq %rax
jmp 0x700e
xorl %eax, %eax
cmpq %rax, %r13
je 0x7040
movq -0x60(%rbp), %rcx
movb $0x0, (%rcx,%rax)
incq %rax
jmp 0x702e
xorl %r10d, %r10d
pushq $-0x1
popq %rcx
cmpq %r13, %r10
je 0x715a
cmpb $0x0, (%r14,%r10)
jne 0x7105
movq -0x60(%rbp), %rax
cmpb $0x0, (%rax,%r10)
jne 0x7105
testl %ecx, %ecx
js 0x707a
movl %ecx, %ebx
movl %ecx, %eax
movl (%rsi,%rax,4), %ecx
cmpl %ecx, (%rsi,%r10,4)
jge 0x7082
movl %r10d, %ecx
jmp 0x7105
jne 0x7103
movq -0x58(%rbp), %r9
movl (%r9,%rax,4), %ecx
cmpl %ecx, (%r9,%r10,4)
jl 0x707a
jne 0x7103
movq -0x70(%rbp), %rcx
movl (%rcx,%rax,4), %eax
cmpl %eax, (%rcx,%r10,4)
jl 0x707a
jne 0x7103
movq %r10, %rsi
imulq %rdi, %rsi
movl %ebx, %ecx
movl %ebx, %eax
imull %r15d, %eax
addq %rdx, %rsi
movq %rsi, -0x50(%rbp)
cltq
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rax
movq %rax, %rsi
addl %r8d, %eax
cmpl %r15d, %eax
jge 0x70f9
movq -0x88(%rbp), %rax
movslq (%rax,%rsi,4), %rcx
movq -0x50(%rbp), %rax
movb (%rax,%rcx), %r9b
addq -0x48(%rbp), %rcx
leaq 0x1(%rsi), %rax
cmpb (%rdx,%rcx), %r9b
movl %ebx, %ecx
je 0x70c3
cmpb $0x30, %r9b
cmovel %r10d, %ecx
testl %esi, %esi
je 0x710d
movq -0x30(%rbp), %rsi
jmp 0x7105
movl %ebx, %ecx
incq %r10
jmp 0x7046
movl %ecx, %r9d
movq %r8, %rax
movq -0x30(%rbp), %rsi
movq -0x40(%rbp), %rcx
addl %eax, %ecx
cmpl %r8d, %ecx
jge 0x7155
movq -0x78(%rbp), %rcx
movl (%rcx,%rax,4), %esi
imull %r15d, %esi
leal (%rsi,%r10), %ecx
movslq %ecx, %rcx
movb (%rdx,%rcx), %cl
addl %r9d, %esi
movslq %esi, %rsi
incq %rax
cmpb (%rdx,%rsi), %cl
movq -0x30(%rbp), %rsi
je 0x7117
cmpb $0x30, %cl
movl %r9d, %ecx
cmovel %r10d, %ecx
jmp 0x7105
movl %r9d, %ecx
jmp 0x7105
movslq %ecx, %r9
cmpl $0x0, -0x38(%rbp)
je 0x716e
cmpl $0x0, (%rsi,%r9,4)
je 0x724f
movl %ecx, -0x50(%rbp)
movl %ecx, %eax
imull %r15d, %eax
cltq
movq %r9, -0x48(%rbp)
leaq (%rdx,%r9), %rcx
addq %rdx, %rax
xorl %r10d, %r10d
cmpq %r10, %r13
je 0x71c3
movq %rsi, %r9
movl (%rsi,%r10,4), %esi
movq -0x58(%rbp), %rbx
movl %esi, (%rbx,%r10,4)
cmpb $0x31, (%rcx)
jne 0x71a6
decl %esi
movl %esi, (%r9,%r10,4)
cmpb $0x31, (%rax,%r10)
jne 0x71b7
decl %esi
movq -0x30(%rbp), %r9
movl %esi, (%r9,%r10,4)
incq %r10
addq %rdi, %rcx
movq -0x30(%rbp), %rsi
jmp 0x7187
movq -0x38(%rbp), %rcx
movl %ecx, %eax
movq -0x68(%rbp), %r10
movl -0x50(%rbp), %r9d
movl %r9d, (%r10,%rax,4)
movq -0x48(%rbp), %rax
movb $0x1, (%r14,%rax)
decq -0x40(%rbp)
addq $-0x4, -0x78(%rbp)
movq %rcx, %rax
jmp 0x6f58
imull %r15d, %r15d
movq -0x80(%rbp), %r9
movb $0x0, (%r9,%r15)
xorl %eax, %eax
movl 0x1a33c(%rip), %ecx     # 0x21540
movslq %ecx, %rsi
cmpq %rsi, %rax
jge 0x7240
xorl %esi, %esi
movslq %ecx, %r8
cmpq %r8, %rsi
jge 0x7238
movslq (%r10,%rax,4), %rcx
imulq %rdi, %rcx
movslq (%r10,%rsi,4), %r8
addq %rcx, %r8
movb (%rdx,%r8), %cl
movb %cl, (%r9,%rsi)
incq %rsi
movl 0x1a30a(%rip), %ecx     # 0x21540
jmp 0x720e
incq %rax
addq %rdi, %r9
jmp 0x7204
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10d82(%rip), %rbx     # 0x17fd8
movq (%rbx), %rdi
leaq 0x9a10(%rip), %rsi      # 0x10c70
xorl %eax, %eax
callq 0x2310
movq (%rbx), %rdi
leaq 0x9a05(%rip), %rsi      # 0x10c76
movq -0x38(%rbp), %rdx
xorl %eax, %eax
callq 0x2310
movq 0x10d3d(%rip), %rax     # 0x17fc0
movq (%rax), %rdi
callq 0x2260
pushq $0x1
popq %rdi
callq 0x2290
nop