// File: /workspace/github_c_makefile_star2_O3/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
movq %rsi, -0x40(%rbp)
movl %edx, %r12d
movq %rsp, %r9
leaq 0xf(%r12), %rax
andq $-0x10, %rax
subq %rax, %r9
movq %r9, %rsp
movq %rsp, %rcx
leaq 0xf(,%r12,4), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, %rsp
movq %r12, %rax
imulq %r12, %rax
movq %rsp, %r8
leaq 0xf(,%rax,4), %rsi
andq $-0x10, %rsi
subq %rsi, %r8
movq %r8, %rsp
movq %rsp, %rsi
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %rsi
movq %rsi, %rsp
testl %edx, %edx
jle 0x8225
movq %rsi, %rax
xorl %esi, %esi
movq %rax, -0x38(%rbp)
movq %rdi, %r10
movq %rdi, -0x58(%rbp)
movq %rdi, %r11
movb $0x0, (%r9,%rsi)
movl $0x0, (%r8,%rsi,4)
movq %r10, %rbx
xorl %r14d, %r14d
xorl %r15d, %r15d
cmpb $0x31, (%r11,%r15)
je 0x7ff3
cmpb $0x31, (%rbx)
jne 0x8001
movb $0x1, (%rax,%r15)
incl %r14d
movl %r14d, (%r8,%rsi,4)
jmp 0x8006
movb $0x0, (%rax,%r15)
incq %r15
addq %r12, %rbx
cmpq %r15, %r12
jne 0x7fe7
incq %rsi
addq %r12, %r11
incq %r10
addq %r12, %rax
cmpq %r12, %rsi
jne 0x7fd1
cmpl $0x1, %edx
je 0x8083
leaq (,%r12,4), %rsi
movl $0x1, %edi
movq %r8, %r10
movq %rdi, %rax
imulq %r12, %rax
leaq (%r8,%rax,4), %r11
movq -0x38(%rbp), %rbx
xorl %r14d, %r14d
movl $0x0, (%r11,%r14,4)
xorl %eax, %eax
xorl %r15d, %r15d
cmpb $0x1, (%rbx,%r15)
jne 0x8065
addl (%r10,%r15,4), %eax
movl %eax, (%r11,%r14,4)
incq %r15
cmpq %r15, %r12
jne 0x8056
incq %r14
addq %r12, %rbx
cmpq %r12, %r14
jne 0x8049
incq %rdi
addq %rsi, %r10
cmpq %r12, %rdi
jne 0x8037
testl %edx, %edx
movq -0x38(%rbp), %rsi
jle 0x8225
movq %rdx, -0x50(%rbp)
leaq (,%r12,4), %r11
xorl %ebx, %ebx
movq %r9, -0x48(%rbp)
movl $0x7fffffff, -0x30(%rbp) # imm = 0x7FFFFFFF
xorl %r14d, %r14d
xorl %r15d, %r15d
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
cmpb $0x0, (%r9,%r15)
je 0x80c0
movl %r10d, %r13d
jmp 0x810e
testq %rbx, %rbx
je 0x80e9
movq %r15, %rax
imulq %r12, %rax
addq %rsi, %rax
xorl %edx, %edx
xorl %r13d, %r13d
movslq (%rcx,%rdx,4), %rdi
movzbl (%rax,%rdi), %edi
addl %edi, %r13d
incq %rdx
cmpq %rdx, %rbx
jne 0x80d4
jmp 0x80ec
xorl %r13d, %r13d
cmpl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
je 0x810a
cmpl %r10d, %r13d
setl %al
testl %r10d, %r10d
sete %dl
testl %r13d, %r13d
jle 0x8122
orb %al, %dl
je 0x8122
movl %r15d, -0x30(%rbp)
incq %r15
addq $0x4, %r14
movl %r13d, %r10d
cmpq %r12, %r15
jne 0x80b4
jmp 0x81c5
cmpl %r10d, %r13d
jne 0x80bb
movslq -0x30(%rbp), %rdx
movq %r8, %r9
xorl %eax, %eax
movl (%r9,%rdx,4), %edi
cmpl %edi, (%r9,%r14)
jne 0x8151
incq %rax
addq %r11, %r9
cmpq %rax, %r12
jne 0x8130
movl $0x0, -0x2c(%rbp)
movl %r12d, %eax
jmp 0x8158
setl %sil
movl %esi, -0x2c(%rbp)
testq %rbx, %rbx
sete %dil
cmpl -0x50(%rbp), %eax
setne %al
orb %dil, %al
jne 0x8198
movq %r15, %rax
imulq %r12, %rax
movq -0x38(%rbp), %rsi
addq %rsi, %rax
imulq %r12, %rdx
addq %rsi, %rdx
xorl %r9d, %r9d
movslq (%rcx,%r9,4), %rsi
movb (%rax,%rsi), %dil
cmpb (%rdx,%rsi), %dil
jne 0x81b7
incq %r9
cmpq %r9, %rbx
jne 0x8182
movl -0x2c(%rbp), %eax
testb %al, %al
jne 0x81a2
movl %r10d, %r13d
testb %al, %al
movq -0x48(%rbp), %r9
movq -0x38(%rbp), %rsi
jne 0x810a
jmp 0x810e
xorb $0x1, %dil
movl -0x2c(%rbp), %eax
orb %dil, %al
je 0x819f
jmp 0x81a2
movslq -0x30(%rbp), %rax
movb $0x1, (%r9,%rax)
movl %eax, (%rcx,%rbx,4)
incq %rbx
cmpq %r12, %rbx
jne 0x80a1
movq %r12, %rax
imulq %r12, %rax
movq -0x40(%rbp), %r9
movb $0x0, (%r9,%rax)
xorl %edx, %edx
movslq -0x50(%rbp), %rdi
movq -0x58(%rbp), %r10
movslq (%rcx,%rdx,4), %rax
imulq %rdi, %rax
xorl %esi, %esi
movslq (%rcx,%rsi,4), %r8
addq %rax, %r8
movb (%r10,%r8), %r8b
movb %r8b, (%r9,%rsi)
incq %rsi
cmpq %rsi, %r12
jne 0x8201
incq %rdx
addq %r12, %r9
cmpq %r12, %rdx
jne 0x81f7
jmp 0x8230
imull %edx, %edx
movq -0x40(%rbp), %rax
movb $0x0, (%rax,%rdx)
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop