// File: /workspace/github_c_makefile_star2_O2/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 $0x38, %rsp
movq %rsi, -0x50(%rbp)
movl %edx, %r13d
movq %rsp, %rbx
leaq 0xf(%r13), %rax
andq $-0x10, %rax
subq %rax, %rbx
movq %rbx, %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, -0x30(%rbp)
movq %rcx, %rsp
movq %r13, %rax
imulq %r13, %rax
movq %rsp, %rsi
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %rsi
movq %rsi, %rsp
movslq %edx, %r10
xorl %eax, %eax
testl %r10d, %r10d
movl $0x0, %r11d
movq %rdx, -0x40(%rbp)
cmovgl %edx, %r11d
movq %rdi, %rcx
movq %rdi, -0x48(%rbp)
movq %rdi, %rdx
movq %rsi, -0x58(%rbp)
cmpq %r11, %rax
je 0x7896
movb $0x0, (%rbx,%rax)
movq %rcx, %rdi
xorl %r8d, %r8d
cmpq %r8, %r13
je 0x7888
movb $0x1, %r9b
cmpb $0x31, (%rdx,%r8)
je 0x787c
cmpb $0x31, (%rdi)
sete %r9b
movb %r9b, (%rsi,%r8)
incq %r8
addq %r10, %rdi
jmp 0x7866
incq %rax
addq %r13, %rsi
addq %r10, %rdx
incq %rcx
jmp 0x7857
movq %r10, -0x38(%rbp)
xorl %r15d, %r15d
movq -0x30(%rbp), %rdi
movq %r11, -0x60(%rbp)
cmpq %r11, %r15
je 0x7937
xorl %eax, %eax
xorl %r14d, %r14d
movq -0x58(%rbp), %r8
cmpq %r13, %rax
je 0x78ee
cmpb $0x0, (%rbx,%rax)
jne 0x78e9
movq %rax, %rcx
imulq %r13, %rcx
addq %r8, %rcx
xorl %edx, %edx
cmpq %rdx, %r15
je 0x78e9
movslq (%r12,%rdx,4), %rsi
incq %rdx
cmpb $0x1, (%rcx,%rsi)
jne 0x78ce
movslq %r14d, %rcx
incl %r14d
movl %eax, (%rdi,%rcx,4)
incq %rax
jmp 0x78b7
testl %r14d, %r14d
je 0x7919
callq 0x2110
movq -0x30(%rbp), %rdi
movslq %r14d, %rcx
cqto
idivq %rcx
movslq (%rdi,%rdx,4), %rax
movl %eax, (%r12,%r15,4)
movb $0x1, (%rbx,%rax)
incq %r15
movq -0x60(%rbp), %r11
jmp 0x78a5
xorl %eax, %eax
xorl %r14d, %r14d
cmpq %rax, %r13
je 0x78f3
cmpb $0x0, (%rbx,%rax)
jne 0x7932
movslq %r14d, %rcx
incl %r14d
movl %eax, (%rdi,%rcx,4)
incq %rax
jmp 0x791e
movq -0x40(%rbp), %rax
imull %eax, %eax
movq -0x50(%rbp), %rdi
movb $0x0, (%rdi,%rax)
xorl %eax, %eax
movq -0x48(%rbp), %r8
movq -0x38(%rbp), %r9
cmpq %r11, %rax
je 0x7980
movslq (%r12,%rax,4), %rcx
imulq %r9, %rcx
xorl %edx, %edx
cmpq %rdx, %r11
je 0x7978
movslq (%r12,%rdx,4), %rsi
addq %rcx, %rsi
movb (%r8,%rsi), %sil
movb %sil, (%rdi,%rdx)
incq %rdx
jmp 0x795f
incq %rax
addq %r9, %rdi
jmp 0x7950
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop