// File: /workspace/github_c_makefile_star2_O0/lukas783[P]N-Queens-MPI/nqueens.c
// Function: nqueens(int, unsigned long long, unsigned long long)
int nqueens(int proc, ull i, ull n) {

  // declare some variables and allocate arrays
  int a, b = 0;
  int *fact = (int *)calloc(n, sizeof(int));
  int *perm = (int *)calloc(n, sizeof(int));

  // calculate factorial based on i, store in perm
  fact[b] = 1;
  while(++b < (int)n) {
    fact[b] = fact[b-1]*b;
  }
  
  for(b = 0; b < (int)n; ++b) {
    perm[b] = i / fact[n - 1 - b];
    i = i % fact[n - 1 - b];
  }

  for(b = n - 1; b > 0; --b) {
    for(a = b - 1; a >= 0; --a) {
      if(perm[a] <= perm[b]) {
	perm[b]++;
      }
    }
  }

  // free up fact array now
  free(fact);

  // loop through all elements of the permutation
  for(ull j = 0; j < n; j++) {
    int val = perm[j];

    // check everything ahead of j
    for(int k = j+1, dist = 1; k < (int)n; k++, dist++) {
      // check if the value +/- dist is equal (means its a diagnoal)
      if(val - dist == perm[k] || val + dist  == perm[k]) {
	free(perm);
	return 0;
      }
    }

    // check everything behind j
    for(int k = j-1, dist = 1; k >= 0; k--, dist++) {
      // check if the value +/- dist is equal (means its a diagonal)
      if(val - dist == perm[k] || val + dist  == perm[k]) {
	free(perm);
	return 0;
      }
    }
  }

  //iff we made it to here, free and return 1
  free(perm);
  return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl $0x0, -0x20(%rbp)
movq -0x18(%rbp), %rdi
movl $0x4, %esi
callq 0x7300
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rdi
movl $0x4, %esi
callq 0x7300
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rax
movslq -0x20(%rbp), %rcx
movl $0x1, (%rax,%rcx,4)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
movq -0x18(%rbp), %rcx
cmpl %ecx, %eax
jge 0x7e9f
movq -0x28(%rbp), %rax
movl -0x20(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edx
imull -0x20(%rbp), %edx
movq -0x28(%rbp), %rax
movslq -0x20(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x7e6d
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl %ecx, %eax
jge 0x7f0d
movq -0x10(%rbp), %rax
movq -0x28(%rbp), %rcx
movq -0x18(%rbp), %rdx
subq $0x1, %rdx
movslq -0x20(%rbp), %rsi
subq %rsi, %rdx
movslq (%rcx,%rdx,4), %rcx
xorl %edx, %edx
divq %rcx
movl %eax, %edx
movq -0x30(%rbp), %rax
movslq -0x20(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x10(%rbp), %rax
movq -0x28(%rbp), %rcx
movq -0x18(%rbp), %rdx
subq $0x1, %rdx
movslq -0x20(%rbp), %rsi
subq %rsi, %rdx
movslq (%rcx,%rdx,4), %rcx
xorl %edx, %edx
divq %rcx
movq %rdx, -0x10(%rbp)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x7ea6
movq -0x18(%rbp), %rax
subq $0x1, %rax
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jle 0x7f70
movl -0x20(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jl 0x7f63
movq -0x30(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movq -0x30(%rbp), %rcx
movslq -0x20(%rbp), %rdx
cmpl (%rcx,%rdx,4), %eax
jg 0x7f56
movq -0x30(%rbp), %rax
movslq -0x20(%rbp), %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
jmp 0x7f58
movl -0x1c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x7f27
jmp 0x7f65
movl -0x20(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x7f18
movq -0x28(%rbp), %rdi
callq 0x7610
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x18(%rbp), %rax
jae 0x8084
movq -0x30(%rbp), %rax
movq -0x38(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x3c(%rbp)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movl %eax, -0x40(%rbp)
movl $0x1, -0x44(%rbp)
movl -0x40(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl %ecx, %eax
jge 0x800b
movl -0x3c(%rbp), %eax
subl -0x44(%rbp), %eax
movq -0x30(%rbp), %rcx
movslq -0x40(%rbp), %rdx
cmpl (%rcx,%rdx,4), %eax
je 0x7fe0
movl -0x3c(%rbp), %eax
addl -0x44(%rbp), %eax
movq -0x30(%rbp), %rcx
movslq -0x40(%rbp), %rdx
cmpl (%rcx,%rdx,4), %eax
jne 0x7ff5
movq -0x30(%rbp), %rdi
callq 0x7610
movl $0x0, -0x4(%rbp)
jmp 0x8094
jmp 0x7ff7
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x7faf
movq -0x38(%rbp), %rax
subq $0x1, %rax
movl %eax, -0x48(%rbp)
movl $0x1, -0x4c(%rbp)
cmpl $0x0, -0x48(%rbp)
jl 0x8071
movl -0x3c(%rbp), %eax
subl -0x4c(%rbp), %eax
movq -0x30(%rbp), %rcx
movslq -0x48(%rbp), %rdx
cmpl (%rcx,%rdx,4), %eax
je 0x8049
movl -0x3c(%rbp), %eax
addl -0x4c(%rbp), %eax
movq -0x30(%rbp), %rcx
movslq -0x48(%rbp), %rdx
cmpl (%rcx,%rdx,4), %eax
jne 0x805b
movq -0x30(%rbp), %rdi
callq 0x7610
movl $0x0, -0x4(%rbp)
jmp 0x8094
jmp 0x805d
movl -0x48(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x48(%rbp)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x801d
jmp 0x8073
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x7f81
movq -0x30(%rbp), %rdi
callq 0x7610
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)