// File: /workspace/github_c_makefile_star2_O3/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 $0x48, %rsp
movl %edx, %r15d
movq %rsi, -0x60(%rbp)
movq %rdi, %rbx
movl %edx, %r12d
movq %rsp, %r10
leaq 0xf(,%r12,4), %rax
andq $-0x10, %rax
subq %rax, %r10
movq %r10, %rsp
movq %rsp, %r11
subq %rax, %r11
movq %r11, %rsp
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x50(%rbp)
movq %rcx, %rsp
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x58(%rbp)
movq %rcx, %rsp
movq %rsp, %r14
leaq 0xf(%r12), %rcx
andq $-0x10, %rcx
subq %rcx, %r14
movq %r14, %rsp
movq %rsp, %rdx
subq %rcx, %rdx
movq %rdx, -0x30(%rbp)
movq %rdx, %rsp
movq %rsp, %r13
subq %rax, %r13
movq %r13, %rsp
testl %r15d, %r15d
jle 0x775c
xorl %eax, %eax
movq %rbx, %rcx
movq %rbx, %rdx
movl $0x0, (%r11,%rax,4)
movb $0x0, (%r14,%rax)
movq %rcx, %rdi
xorl %esi, %esi
xorl %r8d, %r8d
cmpb $0x31, (%rdx,%r8)
jne 0x7493
incl %esi
movl %esi, (%r11,%rax,4)
cmpb $0x31, (%rdi)
jne 0x749e
incl %esi
movl %esi, (%r11,%rax,4)
incq %r8
addq %r12, %rdi
cmpq %r8, %r12
jne 0x7486
movq -0x50(%rbp), %rdi
movl %esi, (%rdi,%rax,4)
movq -0x58(%rbp), %rdi
movl %esi, (%rdi,%rax,4)
incq %rax
addq %r12, %rdx
incq %rcx
cmpq %r12, %rax
jne 0x7471
testl %r15d, %r15d
jle 0x775c
leaq (%r10,%r12,4), %rax
movq %rax, -0x48(%rbp)
movq %r12, %rax
movq %r10, -0x70(%rbp)
movq %r11, -0x68(%rbp)
leaq -0x1(%rax), %rcx
movq %rcx, -0x40(%rbp)
movq %rax, -0x38(%rbp)
cmpq $0x5, %rax
jb 0x75b7
xorl %eax, %eax
cmpb $0x0, (%r14,%rax)
jne 0x75a9
movb $0x2, (%r14,%rax)
movq $-0x1, %rcx
cmpb $0x0, 0x1(%r14,%rcx)
leaq 0x1(%rcx), %rcx
jne 0x7510
movl %ecx, (%r13)
movb $0x2, (%r14,%rcx)
movl $0x1, %ecx
xorl %edx, %edx
xorl %esi, %esi
xorl %edi, %edi
cmpb $0x0, (%r14,%rdi)
jne 0x756a
movl (%r13,%rdx,4), %r8d
leal (%rsi,%r8), %r9d
movslq %r9d, %r9
cmpb $0x31, (%rbx,%r9)
je 0x755b
imull %r15d, %r8d
addl %edi, %r8d
movslq %r8d, %r8
cmpb $0x31, (%rbx,%r8)
jne 0x756a
movb $0x2, (%r14,%rdi)
movslq %ecx, %r8
incl %ecx
movl %edi, (%r13,%r8,4)
incq %rdi
addl %r15d, %esi
cmpq %rdi, %r12
jne 0x7530
incq %rdx
movslq %ecx, %rsi
cmpq %rsi, %rdx
jl 0x752c
movl %ecx, %ecx
cmpq %rcx, -0x40(%rbp)
movq -0x30(%rbp), %rcx
setne (%rcx,%rax)
xorl %ecx, %ecx
xorl %esi, %esi
movzbl (%r14,%rcx), %edx
cmpb $0x2, %dl
cmovel %esi, %edx
movb %dl, (%r14,%rcx)
incq %rcx
cmpq %rcx, %r12
jne 0x7592
incq %rax
cmpq %r12, %rax
jne 0x74f9
jmp 0x75cd
movq -0x30(%rbp), %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x2120
movq -0x68(%rbp), %r11
movq -0x70(%rbp), %r10
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %eax, %eax
movq -0x30(%rbp), %rdi
cmpb $0x0, (%r14,%rax)
jne 0x75f7
cmpb $0x0, (%rdi,%rax)
jne 0x75f7
testl %ecx, %ecx
js 0x75f5
movl %ecx, %edx
movl (%r11,%rdx,4), %esi
cmpl %esi, (%r11,%rax,4)
jge 0x7604
movl %eax, %ecx
incq %rax
cmpq %r12, %rax
jne 0x75d8
jmp 0x76e7
jne 0x75f7
movq -0x50(%rbp), %rdi
movl (%rdi,%rdx,4), %esi
cmpl %esi, (%rdi,%rax,4)
jge 0x761a
movl %eax, %ecx
movq -0x30(%rbp), %rdi
jmp 0x75f7
movq -0x30(%rbp), %rdi
jne 0x75f7
movq -0x58(%rbp), %rsi
movl (%rsi,%rdx,4), %edx
cmpl %edx, (%rsi,%rax,4)
jl 0x75f5
jne 0x75f7
movq -0x38(%rbp), %rsi
cmpq %r12, %rsi
jge 0x767b
movl %ecx, %esi
imull %r15d, %esi
movl %eax, %edi
imull %r15d, %edi
xorl %edx, %edx
movq -0x48(%rbp), %r8
movl (%r8,%rdx,4), %r9d
leal (%r9,%rdi), %r8d
movslq %r8d, %r8
movb (%rbx,%r8), %r8b
addl %esi, %r9d
movslq %r9d, %r9
cmpb (%rbx,%r9), %r8b
jne 0x767f
incq %rdx
movq -0x38(%rbp), %r8
addl %edx, %r8d
cmpl %r15d, %r8d
jl 0x7645
movq -0x38(%rbp), %rsi
addl %esi, %edx
jmp 0x768c
movl %esi, %edx
jmp 0x7690
movq -0x38(%rbp), %rsi
addl %esi, %edx
cmpb $0x30, %r8b
cmovel %eax, %ecx
movq -0x30(%rbp), %rdi
cmpq %r12, %rsi
setge %sil
cmpl %r15d, %edx
setne %dl
orb %sil, %dl
jne 0x75f7
xorl %edx, %edx
movq -0x48(%rbp), %rsi
movl (%rsi,%rdx,4), %edi
imull %r15d, %edi
leal (%rdi,%rax), %esi
movslq %esi, %rsi
movb (%rbx,%rsi), %sil
addl %ecx, %edi
movslq %edi, %rdi
cmpb (%rbx,%rdi), %sil
jne 0x76db
incq %rdx
movq -0x38(%rbp), %rsi
addl %edx, %esi
cmpl %r15d, %esi
jl 0x76a8
jmp 0x7614
cmpb $0x30, %sil
cmovel %eax, %ecx
jmp 0x7614
movslq %ecx, %rax
cmpq $0x0, -0x40(%rbp)
je 0x76fc
cmpl $0x0, (%r11,%rax,4)
je 0x77c6
movl %eax, %ecx
imull %r15d, %ecx
movslq %ecx, %rcx
leaq (%rbx,%rax), %rdx
addq %rbx, %rcx
xorl %esi, %esi
movl (%r11,%rsi,4), %edi
movq -0x50(%rbp), %r8
movl %edi, (%r8,%rsi,4)
cmpb $0x31, (%rdx)
jne 0x7725
decl %edi
movl %edi, (%r11,%rsi,4)
cmpb $0x31, (%rcx,%rsi)
jne 0x7731
decl %edi
movl %edi, (%r11,%rsi,4)
incq %rsi
addq %r12, %rdx
cmpq %rsi, %r12
jne 0x770e
movq -0x40(%rbp), %rcx
movl %eax, (%r10,%rcx,4)
movb $0x1, (%r14,%rax)
addq $-0x4, -0x48(%rbp)
cmpq $0x1, -0x38(%rbp)
movq %rcx, %rax
jg 0x74e1
movl %r15d, %eax
imull %eax, %eax
movq -0x60(%rbp), %r9
movb $0x0, (%r9,%rax)
movl 0x19daf(%rip), %edx     # 0x21520
testl %edx, %edx
jle 0x77b7
movslq %r15d, %rax
xorl %ecx, %ecx
testl %edx, %edx
jle 0x77a9
movslq (%r10,%rcx,4), %rsi
imulq %rax, %rsi
xorl %edi, %edi
movslq (%r10,%rdi,4), %rdx
addq %rsi, %rdx
movb (%rbx,%rdx), %dl
movb %dl, (%r9,%rdi)
incq %rdi
movl 0x19d81(%rip), %edx     # 0x21520
movslq %edx, %r8
cmpq %r8, %rdi
jl 0x7788
jmp 0x77ac
movslq %edx, %r8
incq %rcx
addq %rax, %r9
cmpq %r8, %rcx
jl 0x777a
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x40(%rbp), %rdi
movq %rbx, %rsi
callq 0x2443