// File: /workspace/github_c_makefile_star2_O1/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, -0x48(%rbp)
movq %rcx, %rsp
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x50(%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 0x73fb
xorl %eax, %eax
movq %rbx, %rcx
movq %rbx, %rdx
movl $0x0, (%r11,%rax,4)
movb $0x0, (%r14,%rax)
movq %rcx, %rsi
xorl %edi, %edi
cmpb $0x31, (%rdx,%rdi)
jne 0x73c7
incl (%r11,%rax,4)
cmpb $0x31, (%rsi)
jne 0x73d0
incl (%r11,%rax,4)
incq %rdi
addq %r12, %rsi
cmpq %rdi, %r12
jne 0x73bd
movl (%r11,%rax,4), %esi
movq -0x48(%rbp), %rdi
movl %esi, (%rdi,%rax,4)
movq -0x50(%rbp), %rdi
movl %esi, (%rdi,%rax,4)
incq %rax
addq %r12, %rdx
incq %rcx
cmpq %r12, %rax
jne 0x73ab
testl %r15d, %r15d
jle 0x768a
leaq (%r10,%r12,4), %rax
movq %rax, -0x58(%rbp)
movq %r12, %rax
movq %r10, -0x70(%rbp)
movq %r11, -0x68(%rbp)
movq %rax, -0x38(%rbp)
leaq -0x1(%rax), %rcx
movq %rcx, -0x40(%rbp)
cmpq $0x5, %rax
jl 0x74ed
xorl %eax, %eax
cmpb $0x0, (%r14,%rax)
jne 0x74df
movb $0x2, (%r14,%rax)
movq $-0x1, %rcx
cmpb $0x0, 0x1(%r14,%rcx)
leaq 0x1(%rcx), %rcx
jne 0x7446
movl %ecx, (%r13)
movb $0x2, (%r14,%rcx)
movl $0x1, %ecx
xorl %edx, %edx
xorl %esi, %esi
xorl %edi, %edi
cmpb $0x0, (%r14,%rdi)
jne 0x74a0
movl (%r13,%rdx,4), %r8d
leal (%rsi,%r8), %r9d
movslq %r9d, %r9
cmpb $0x31, (%rbx,%r9)
je 0x7491
imull %r15d, %r8d
addl %edi, %r8d
movslq %r8d, %r8
cmpb $0x31, (%rbx,%r8)
jne 0x74a0
movb $0x2, (%r14,%rdi)
movslq %ecx, %r8
incl %ecx
movl %edi, (%r13,%r8,4)
incq %rdi
addl %r15d, %esi
cmpq %rdi, %r12
jne 0x7466
incq %rdx
movslq %ecx, %rsi
cmpq %rsi, %rdx
jl 0x7462
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 0x74c8
incq %rax
cmpq %r12, %rax
jne 0x742f
jmp 0x7503
movq -0x30(%rbp), %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x2120
movq -0x68(%rbp), %r11
movq -0x70(%rbp), %r10
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %eax, %eax
movq -0x30(%rbp), %rdi
movl %edx, %ecx
cmpb $0x0, (%r14,%rax)
jne 0x751d
cmpb $0x0, (%rdi,%rax)
je 0x752c
movl %ecx, %edx
incq %rax
cmpq %r12, %rax
jne 0x750e
jmp 0x7617
testl %ecx, %ecx
js 0x753c
movl %ecx, %edx
movl (%r11,%rdx,4), %esi
cmpl %esi, (%r11,%rax,4)
jge 0x7540
movl %eax, %edx
jmp 0x751f
jne 0x751d
movq -0x48(%rbp), %rdi
movl (%rdi,%rdx,4), %esi
cmpl %esi, (%rdi,%rax,4)
jge 0x7555
movl %eax, %edx
jmp 0x760e
jne 0x760c
movq -0x50(%rbp), %rdi
movl (%rdi,%rdx,4), %esi
movl %eax, %edx
cmpl %esi, (%rdi,%rax,4)
movq -0x30(%rbp), %rdi
jl 0x751f
jne 0x751d
movq -0x38(%rbp), %rdx
cmpq %r12, %rdx
jge 0x75a9
movl %ecx, %esi
imull %r15d, %esi
movl %eax, %edi
imull %r15d, %edi
movl (%r10,%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 0x75ae
incq %rdx
cmpl %r15d, %edx
jl 0x7584
jmp 0x75b5
movq %rdx, %rsi
jmp 0x75bd
cmpb $0x30, %r8b
cmovel %eax, %ecx
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
cmpq %r12, %rsi
setge %sil
cmpl %r15d, %edx
setne %dl
orb %sil, %dl
jne 0x751d
xorl %edx, %edx
movq -0x58(%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 0x7605
incq %rdx
movq -0x38(%rbp), %rsi
addl %edx, %esi
cmpl %r15d, %esi
jl 0x75d5
jmp 0x760c
cmpb $0x30, %sil
cmovel %eax, %ecx
movl %ecx, %edx
movq -0x30(%rbp), %rdi
jmp 0x751f
movslq %edx, %rax
cmpq $0x0, -0x40(%rbp)
je 0x762c
cmpl $0x0, (%r11,%rax,4)
je 0x76f9
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 -0x48(%rbp), %r8
movl %edi, (%r8,%rsi,4)
cmpb $0x31, (%rdx)
jne 0x7655
decl %edi
movl %edi, (%r11,%rsi,4)
cmpb $0x31, (%rcx,%rsi)
jne 0x765f
decl (%r11,%rsi,4)
incq %rsi
addq %r12, %rdx
cmpq %rsi, %r12
jne 0x763e
movq -0x40(%rbp), %rcx
movl %eax, (%r10,%rcx,4)
movb $0x1, (%r14,%rax)
addq $-0x4, -0x58(%rbp)
cmpq $0x1, -0x38(%rbp)
movq %rcx, %rax
jg 0x7417
movl %r15d, %eax
imull %eax, %eax
movq -0x60(%rbp), %r8
movb $0x0, (%r8,%rax)
cmpl $0x0, 0x19e80(%rip)     # 0x21520
jle 0x76ea
movslq %r15d, %rax
xorl %ecx, %ecx
cmpl $0x0, 0x19e72(%rip)     # 0x21520
jle 0x76d8
movslq (%r10,%rcx,4), %rdx
imulq %rax, %rdx
xorl %esi, %esi
movslq (%r10,%rsi,4), %rdi
addq %rdx, %rdi
movb (%rbx,%rdi), %dil
movb %dil, (%r8,%rsi)
incq %rsi
movslq 0x19e4d(%rip), %rdi     # 0x21520
cmpq %rdi, %rsi
jl 0x76ba
incq %rcx
movslq 0x19e3e(%rip), %rdx     # 0x21520
addq %rax, %r8
cmpq %rdx, %rcx
jl 0x76a7
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
nop