#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
scalar_t__ calloc(int, int);
int /*<<< orphan*/ place(int, int *);
int /*<<< orphan*/ print(int *, int);
int /*<<< orphan*/ printf(char *, int);
int *nqueens(int num) {
  int current, *position, num_solutions = 0;
  position = (int *)calloc(num + 1, sizeof(int));
  position[1] = 0;
  current = 1;
  while (current > 0) {
    position[current] += 1;
    while (position[current] <= num && !place(current, position))
      position[current] += 1;
    if (position[current] <= num) {
      if (current == num) {
        ++num_solutions;
        print(position, num);
      } else {
        current += 1;
        position[current] = 0;
      }
    } else
      current -= 1;
  }
  printf("Total Number of Solutions : %d\n", num_solutions);
  return (position);
}


x86 assembly (clang, -O0):
<nqueens>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x4(%rbp),%edi
add    $0x1,%edi
mov    $0x4,%esi
call   1050 <calloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movl   $0x0,0x4(%rax)
movl   $0x1,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    124e <nqueens+0x10e>
mov    -0x10(%rbp),%rax
movslq -0x8(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
add    $0x1,%edx
mov    %edx,(%rax,%rcx,4)
mov    -0x10(%rbp),%rax
movslq -0x8(%rbp),%rcx
mov    (%rax,%rcx,4),%ecx
xor    %eax,%eax
cmp    -0x4(%rbp),%ecx
mov    %al,-0x15(%rbp)
jg     11c3 <nqueens+0x83>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1060 <place@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
test   $0x1,%al
jne    11d3 <nqueens+0x93>
jmp    11e9 <nqueens+0xa9>
mov    -0x10(%rbp),%rax
movslq -0x8(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
add    $0x1,%edx
mov    %edx,(%rax,%rcx,4)
jmp    1193 <nqueens+0x53>
mov    -0x10(%rbp),%rax
movslq -0x8(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
cmp    -0x4(%rbp),%eax
jg     1240 <nqueens+0x100>
mov    -0x8(%rbp),%eax
cmp    -0x4(%rbp),%eax
jne    1223 <nqueens+0xe3>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%esi
call   1040 <print@plt>
jmp    123b <nqueens+0xfb>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x8(%rbp),%rcx
movl   $0x0,(%rax,%rcx,4)
jmp    1249 <nqueens+0x109>
mov    -0x8(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1178 <nqueens+0x38>
mov    -0x14(%rbp),%esi
lea    0xda8(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nqueens>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r14d
lea    0x1(%r14),%edi
mov    $0x4,%esi
call   1050 <calloc@plt>
mov    %rax,%rbx
movl   $0x0,0x4(%rax)
mov    $0x1,%ebp
xor    %r15d,%r15d
jmp    1177 <nqueens+0x37>
nopl   0x0(%rax)
add    $0xffffffff,%ebp
test   %ebp,%ebp
jle    11d0 <nqueens+0x90>
mov    %ebp,%ebp
nopl   0x0(%rax)
mov    (%rbx,%rbp,4),%ecx
lea    0x1(%rcx),%eax
mov    %eax,(%rbx,%rbp,4)
cmp    %r14d,%ecx
jge    119f <nqueens+0x5f>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1060 <place@plt>
test   %eax,%eax
je     1180 <nqueens+0x40>
mov    (%rbx,%rbp,4),%eax
cmp    %r14d,%eax
jg     1170 <nqueens+0x30>
cmp    %r14d,%ebp
jne    11c0 <nqueens+0x80>
add    $0x1,%r15d
mov    %rbx,%rdi
mov    %r14d,%esi
call   1040 <print@plt>
mov    %r14d,%ebp
jmp    1173 <nqueens+0x33>
nopl   (%rax)
movslq %ebp,%rax
add    $0x1,%ebp
movl   $0x0,0x4(%rbx,%rax,4)
jmp    1173 <nqueens+0x33>
lea    0xe29(%rip),%rdi
mov    %r15d,%esi
call   1030 <printf@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [32 x i8] c"Total Number of Solutions : %d\0A\00"
define i32* @nqueens(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %5
%6 = load i32, i32* %2
%7 = add nsw i32 %6, 1
%8 = call i64 @calloc(i32 %7, i32 4)
%9 = inttoptr i64 %8 to i32*
store i32* %9, i32** %4
%10 = load i32*, i32** %4
%11 = getelementptr inbounds i32, i32* %10, i64 1
store i32 0, i32* %11
store i32 1, i32* %3
br label %12
12: ; preds = %74, %1
%13 = load i32, i32* %3
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %75
15: ; preds = %12
%16 = load i32*, i32** %4
%17 = load i32, i32* %3
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i32, i32* %16, i64 %18
%20 = load i32, i32* %19
%21 = add nsw i32 %20, 1
store i32 %21, i32* %19
br label %22
22: ; preds = %38, %15
%23 = load i32*, i32** %4
%24 = load i32, i32* %3
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i32, i32* %23, i64 %25
%27 = load i32, i32* %26
%28 = load i32, i32* %2
%29 = icmp sle i32 %27, %28
br i1 %29, label %30, label %36
30: ; preds = %22
%31 = load i32, i32* %3
%32 = load i32*, i32** %4
%33 = call i32 @place(i32 %31, i32* %32)
%34 = icmp ne i32 %33, 0
%35 = xor i1 %34, true
br label %36
36: ; preds = %30, %22
%37 = phi i1 [ false, %22 ], [ %35, %30 ]
br i1 %37, label %38, label %45
38: ; preds = %36
%39 = load i32*, i32** %4
%40 = load i32, i32* %3
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i32, i32* %39, i64 %41
%43 = load i32, i32* %42
%44 = add nsw i32 %43, 1
store i32 %44, i32* %42
br label %22
45: ; preds = %36
%46 = load i32*, i32** %4
%47 = load i32, i32* %3
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i32, i32* %46, i64 %48
%50 = load i32, i32* %49
%51 = load i32, i32* %2
%52 = icmp sle i32 %50, %51
br i1 %52, label %53, label %71
53: ; preds = %45
%54 = load i32, i32* %3
%55 = load i32, i32* %2
%56 = icmp eq i32 %54, %55
br i1 %56, label %57, label %63
57: ; preds = %53
%58 = load i32, i32* %5
%59 = add nsw i32 %58, 1
store i32 %59, i32* %5
%60 = load i32*, i32** %4
%61 = load i32, i32* %2
%62 = call i32 @print(i32* %60, i32 %61)
br label %70
63: ; preds = %53
%64 = load i32, i32* %3
%65 = add nsw i32 %64, 1
store i32 %65, i32* %3
%66 = load i32*, i32** %4
%67 = load i32, i32* %3
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds i32, i32* %66, i64 %68
store i32 0, i32* %69
br label %70
70: ; preds = %63, %57
br label %74
71: ; preds = %45
%72 = load i32, i32* %3
%73 = sub nsw i32 %72, 1
store i32 %73, i32* %3
br label %74
74: ; preds = %71, %70
br label %12
75: ; preds = %12
%76 = load i32, i32* %5
%77 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %76)
%78 = load i32*, i32** %4
ret i32* %78
}

LLVM IR (-O2):
@.str = [32 x i8] c"Total Number of Solutions : %d\0A\00"
define i32* @nqueens(i32 %0) {
%2 = add nsw i32 %0, 1
%3 = tail call i64 @calloc(i32 %2, i32 4)
%4 = inttoptr i64 %3 to i32*
%5 = getelementptr inbounds i32, i32* %4, i64 1
store i32 0, i32* %5
br label %6
6: ; preds = %1, %34
%7 = phi i32 [ 0, %1 ], [ %36, %34 ]
%8 = phi i32 [ 1, %1 ], [ %35, %34 ]
%9 = zext i32 %8 to i64
%10 = getelementptr inbounds i32, i32* %4, i64 %9
br label %11
11: ; preds = %15, %6
%12 = load i32, i32* %10
%13 = add nsw i32 %12, 1
store i32 %13, i32* %10
%14 = icmp slt i32 %12, %0
br i1 %14, label %15, label %20
15: ; preds = %11
%16 = tail call i32 @place(i32 %8, i32* %4)
%17 = icmp eq i32 %16, 0
br i1 %17, label %11, label %18
18: ; preds = %15
%19 = load i32, i32* %10
br label %20
20: ; preds = %11, %18
%21 = phi i32 [ %19, %18 ], [ %13, %11 ]
%22 = icmp sgt i32 %21, %0
br i1 %22, label %32, label %23
23: ; preds = %20
%24 = icmp eq i32 %8, %0
br i1 %24, label %25, label %28
25: ; preds = %23
%26 = add nsw i32 %7, 1
%27 = tail call i32 @print(i32* %4, i32 %0)
br label %34
28: ; preds = %23
%29 = add nsw i32 %8, 1
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i32, i32* %4, i64 %30
store i32 0, i32* %31
br label %34
32: ; preds = %20
%33 = add nsw i32 %8, -1
br label %34
34: ; preds = %25, %28, %32
%35 = phi i32 [ %0, %25 ], [ %29, %28 ], [ %33, %32 ]
%36 = phi i32 [ %26, %25 ], [ %7, %28 ], [ %7, %32 ]
%37 = icmp sgt i32 %35, 0
br i1 %37, label %6, label %38
38: ; preds = %34
%39 = tail call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %36)
ret i32* %4
}