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

  int n, i, j, max_i, conn, max_conn;
  bool used[size], flag;
  int v[size];
  int degree_levels = size;
  int degree[degree_levels][size];
  bool adj[size][size];

  for (i=0; i<size; i++) {
    used[i]      = false;
    degree[0][i] = 0;
    for (j=0; j<size; j++)
      if (in[i*size+j]=='1'|| in[j*size+i]=='1') {
	adj[i][j]=true;
	degree[0][i]++;
      }
      else adj[i][j]=false;
  }
 
  for (j=1; j<degree_levels; j++)
    for (n=0; n<size; n++) {
      degree[j][n]=0;
      for (i=0; i<size; i++)
	if (adj[n][i]) degree[j][n]+=degree[j-1][i];
    }

  for (n=0; n<size; n++) {
    max_i = max_conn = INT_MAX;
    for (i=0;i<size;i++) {
      if (used[i]) continue;
      conn = 0;

      for (j=0;j<n;j++)
	if (adj[i][v[j]]) conn++;
      
      flag = false;

      if (max_conn==INT_MAX) flag=true;
      else if (conn>0 && conn < max_conn) flag = true;
      else if (max_conn==0 && conn>0) flag = true;
      else if (conn == max_conn) {
	for (j=0; j<degree_levels; j++)
	    if (degree[j][i] != degree[j][max_i]) {
	      if (degree[j][i] < degree[j][max_i]) flag=true;
	      break;
	    }
	if (j==degree_levels) {
	  for (j=0; j<n; j++)	  
	    if (adj[i][v[j]] != adj[max_i][v[j]]) {
	      if (!adj[i][v[j]]) flag = true;
	      break;
	    }
	}
      }

      if (flag) {
	max_i      = i;
	max_conn   = conn;
      }
    }
    used[max_i]=true;
    v[n] = max_i;
  }

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

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %edx, %r15d
movq %rsp, %rcx
leaq 0xf(%r15), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x38(%rbp)
movq %rcx, %rsp
movq %rsp, %rax
leaq 0xf(,%r15,4), %rcx
andq $-0x10, %rcx
subq %rcx, %rax
movq %rax, %rsp
movq %r15, %rcx
imulq %r15, %rcx
movq %rsp, %r12
leaq 0xf(,%rcx,4), %r8
andq $-0x10, %r8
subq %r8, %r12
movq %r12, %rsp
movq %rsp, %r8
addq $0xf, %rcx
andq $-0x10, %rcx
subq %rcx, %r8
movq %r8, -0x40(%rbp)
movq %r8, %rsp
movl %edx, -0x2c(%rbp)
testl %edx, %edx
jle 0x7ef7
xorl %edx, %edx
movq -0x40(%rbp), %r9
movq %rdi, %r10
movq %rdi, %r11
movq -0x38(%rbp), %rcx
movb $0x0, (%rcx,%rdx)
movl $0x0, (%r12,%rdx,4)
movq %r10, %rcx
xorl %ebx, %ebx
cmpb $0x31, (%r11,%rbx)
je 0x7ecb
cmpb $0x31, (%rcx)
jne 0x7ed6
movb $0x1, (%r9,%rbx)
incl (%r12,%rdx,4)
jmp 0x7edb
movb $0x0, (%r9,%rbx)
incq %rbx
addq %r15, %rcx
cmpq %rbx, %r15
jne 0x7ebf
incq %rdx
addq %r15, %r11
incq %r10
addq %r15, %r9
cmpq %r15, %rdx
jne 0x7eaa
cmpl $0x2, -0x2c(%rbp)
jl 0x7f56
leaq (,%r15,4), %rdx
movl $0x1, %r9d
movq %r12, %r10
movq %r9, %rcx
imulq %r15, %rcx
leaq (%r12,%rcx,4), %r11
movq -0x40(%rbp), %rbx
xorl %r14d, %r14d
movl $0x0, (%r11,%r14,4)
xorl %ecx, %ecx
cmpb $0x1, (%rbx,%rcx)
jne 0x7f38
movl (%r10,%rcx,4), %r8d
addl %r8d, (%r11,%r14,4)
incq %rcx
cmpq %rcx, %r15
jne 0x7f2a
incq %r14
addq %r15, %rbx
cmpq %r15, %r14
jne 0x7f20
incq %r9
addq %rdx, %r10
cmpq %r15, %r9
jne 0x7f0e
movl -0x2c(%rbp), %ebx
testl %ebx, %ebx
jle 0x80b7
movq %r12, -0x58(%rbp)
movq %rdi, -0x48(%rbp)
movq %rsi, -0x50(%rbp)
leaq (,%r15,4), %rcx
movq %rcx, -0x60(%rbp)
xorl %r11d, %r11d
movq -0x38(%rbp), %r9
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
xorl %r12d, %r12d
xorl %r13d, %r13d
movl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
cmpb $0x0, (%r9,%r13)
jne 0x8085
testq %r11, %r11
je 0x7fc6
movq %r13, %rcx
imulq %r15, %rcx
addq -0x40(%rbp), %rcx
xorl %edx, %edx
xorl %r10d, %r10d
movslq (%rax,%rdx,4), %rsi
movzbl (%rcx,%rsi), %esi
addl %esi, %r10d
incq %rdx
cmpq %rdx, %r11
jne 0x7fb1
jmp 0x7fc9
xorl %r10d, %r10d
movb $0x1, %dl
cmpl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
je 0x807b
cmpl %r14d, %r10d
setl %cl
testl %r14d, %r14d
sete %r8b
testl %r10d, %r10d
jle 0x7ff3
orb %cl, %r8b
jne 0x807b
cmpl %r14d, %r10d
jne 0x801e
movslq %edi, %r9
movq -0x58(%rbp), %rdx
xorl %ecx, %ecx
movl (%rdx,%r9,4), %esi
cmpl %esi, (%rdx,%r12)
jne 0x8022
incq %rcx
addq -0x60(%rbp), %rdx
cmpq %rcx, %r15
jne 0x8001
xorl %edx, %edx
movl %r15d, %ecx
jmp 0x8025
xorl %edx, %edx
jmp 0x807b
setl %dl
testq %r11, %r11
sete %sil
cmpl %ebx, %ecx
setne %cl
orb %sil, %cl
jne 0x8077
movq %r13, %rcx
imulq %r15, %rcx
movq -0x40(%rbp), %rsi
addq %rsi, %rcx
imulq %r15, %r9
addq %rsi, %r9
xorl %r8d, %r8d
movslq (%rax,%r8,4), %rbx
movb (%rcx,%rbx), %sil
cmpb (%r9,%rbx), %sil
jne 0x8066
incq %r8
cmpq %r8, %r11
jne 0x804e
jmp 0x8074
movzbl %dl, %edx
testb %sil, %sil
movl $0x1, %ecx
cmovel %ecx, %edx
movl -0x2c(%rbp), %ebx
movq -0x38(%rbp), %r9
testb %dl, %dl
cmovnel %r10d, %r14d
cmovnel %r13d, %edi
incq %r13
addq $0x4, %r12
cmpq %r15, %r13
jne 0x7f91
movslq %edi, %rcx
movb $0x1, (%r9,%rcx)
movl %ecx, (%rax,%r11,4)
incq %r11
cmpq %r15, %r11
jne 0x7f80
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdi
jmp 0x80ba
xorl %r11d, %r11d
movl %ebx, %ecx
imull %ecx, %ecx
movb $0x0, (%rsi,%rcx)
testl %r11d, %r11d
je 0x80fe
movslq %ebx, %rcx
movl %r11d, %edx
xorl %r11d, %r11d
movslq (%rax,%r11,4), %r8
imulq %rcx, %r8
xorl %r10d, %r10d
movslq (%rax,%r10,4), %r9
addq %r8, %r9
movb (%rdi,%r9), %r9b
movb %r9b, (%rsi,%r10)
incq %r10
cmpq %r10, %rdx
jne 0x80dc
incq %r11
addq %rcx, %rsi
cmpq %rdx, %r11
jne 0x80d1
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)