// File: /workspace/github_c_makefile_star2_O1/ComplexNetworks-DCC-FCUP[P]gtrieScanner/Isomorphism.cpp
// Function: Isomorphism::canonicalRandom(char const*, char*, int)
void Isomorphism::canonicalRandom(const char *in, char *best, int size) {
  int n, i, j, aux;
  bool used[size];
  int v[size];
  int ncand, cand[size];
  bool adj[size][size];

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


  for (n=0; n<size; n++) {
    
    ncand = 0;
    for (i=0; i<size; i++) {
      if (used[i]) continue;
      for (j=0;j<n;j++)
	if (adj[i][v[j]]) break;
      if (j<n) cand[ncand++]=i;
    }

    if (ncand==0)
      for (i=0; i<size; i++)
	if (!used[i]) cand[ncand++]=i;

    aux = random()%ncand;

    v[n]=cand[aux];
    used[cand[aux]]=true;
  }
  

  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 $0x28, %rsp
movl %edx, %r13d
movq %rsp, %r15
leaq 0xf(%r13), %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
movq %rsp, %r12
leaq 0xf(,%r13,4), %rax
andq $-0x10, %rax
subq %rax, %r12
movq %r12, %rsp
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x38(%rbp)
movq %rcx, %rsp
movq %r13, %rax
imulq %r13, %rax
movq %rsp, %r8
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %r8
movq %r8, %rsp
movl %edx, -0x2c(%rbp)
testl %edx, %edx
movq %r8, -0x40(%rbp)
jle 0x7d1b
xorl %eax, %eax
movq %rdi, %rcx
movq %rdi, %rdx
movb $0x0, (%r15,%rax)
movq %rax, %r11
imulq %r13, %r11
addq %r8, %r11
movq %rcx, %rbx
xorl %r8d, %r8d
movb $0x1, %r9b
cmpb $0x31, (%rdx,%r8)
movq %r11, %r10
je 0x7cfa
cmpb $0x31, (%rbx)
sete %r9b
movq %r11, %r10
cmoveq %r11, %r10
movb %r9b, (%r10,%r8)
incq %r8
addq %r13, %rbx
cmpq %r8, %r13
jne 0x7cdf
incq %rax
addq %r13, %rdx
incq %rcx
cmpq %r13, %rax
movq -0x40(%rbp), %r8
jne 0x7cca
movl -0x2c(%rbp), %ecx
testl %ecx, %ecx
jle 0x7dd2
movq %rdi, -0x48(%rbp)
movq %rsi, -0x50(%rbp)
xorl %r14d, %r14d
movq -0x38(%rbp), %rdi
xorl %eax, %eax
xorl %ebx, %ebx
testq %r14, %r14
je 0x7d6d
cmpb $0x0, (%r15,%rax)
jne 0x7d6d
movq %rax, %rcx
imulq %r13, %rcx
addq %r8, %rcx
xorl %edx, %edx
movslq (%r12,%rdx,4), %rsi
cmpb $0x1, (%rcx,%rsi)
je 0x7d65
incq %rdx
cmpq %rdx, %r14
jne 0x7d51
jmp 0x7d6d
movslq %ebx, %rcx
incl %ebx
movl %eax, (%rdi,%rcx,4)
incq %rax
cmpq %r13, %rax
jne 0x7d39
testl %ebx, %ebx
jne 0x7d94
xorl %eax, %eax
xorl %ebx, %ebx
cmpb $0x0, (%r15,%rax)
jne 0x7d8c
movslq %ebx, %rcx
incl %ebx
movl %eax, (%rdi,%rcx,4)
incq %rax
cmpq %rax, %r13
jne 0x7d7d
callq 0x2100
movq -0x38(%rbp), %rdi
movslq %ebx, %rcx
cqto
idivq %rcx
movslq %edx, %rax
movslq (%rdi,%rax,4), %rax
movl %eax, (%r12,%r14,4)
movb $0x1, (%r15,%rax)
incq %r14
cmpq %r13, %r14
movq -0x40(%rbp), %r8
jne 0x7d35
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdi
movl -0x2c(%rbp), %ecx
jmp 0x7dd5
xorl %r14d, %r14d
movl %ecx, %eax
imull %eax, %eax
movb $0x0, (%rsi,%rax)
testl %r14d, %r14d
je 0x7e18
movslq %ecx, %rax
movl %r14d, %ecx
xorl %edx, %edx
movslq (%r12,%rdx,4), %r9
imulq %rax, %r9
xorl %r10d, %r10d
movslq (%r12,%r10,4), %r8
addq %r9, %r8
movb (%rdi,%r8), %r8b
movb %r8b, (%rsi,%r10)
incq %r10
cmpq %r10, %rcx
jne 0x7df6
incq %rdx
addq %rax, %rsi
cmpq %rcx, %rdx
jne 0x7deb
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop