void find_three_squares(unsigned long int num) {
  unsigned long int max, i, x, j;
  int min;
  short int flag_found = 0, permutation;
  int length_counter = 0;
  unsigned long int *array =
      (unsigned long int *)malloc(sizeof(unsigned long int) * 3);
  unsigned long int *array_frame_ptr;
  unsigned long int tmparray[3];
  if (num < (1 + 4 + 9)) {
    printf("\n\nHow can a number be the sum of three squares if it is minor "
           "than 14?\n");
    free(array);
    return;
  }
  min = 9;
  max = num - (1 + 4);
  for (i = min; i <= max; i++) {
    if (is_square(i)) {
      for (x = 1; x <= (num - i - 4); x++) {
        if (is_square(x) && is_square(num - i - x)) {
          permutation = 0;
          tmparray[0] = i;
          tmparray[1] = x;
          tmparray[2] = num - i - x;
          if (length_counter > 0) {
            for (j = 0; j < length_counter; j++) {
              array_frame_ptr = array + (j * 3);
              if (are_permutations(tmparray, array_frame_ptr, 3)) {
                goto next_square;
              }
            }
          }
          memcpy(array + (length_counter * 3), tmparray,
                 sizeof(unsigned long int) * 3);
          length_counter++;
          realloc(array, (length_counter) * sizeof(unsigned long int) * 3);
          flag_found = 1;
          printf("\nI found a sum of square numbers: %lu + %lu + %lu = %lu", i,
                 x, num - x - i, num);
        next_square:;
        }
      }
    }
  }
  if (!flag_found) {
    printf("\nCouldn\'t find three square numbers that matched the requests\n");
  }
}


x86 assembly (clang, -O0):
<find_three_squares>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
movw   $0x0,-0x2e(%rbp)
movl   $0x0,-0x34(%rbp)
mov    $0x18,%edi
call   1070 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0xe,-0x8(%rbp)
jae    11ae <find_three_squares+0x4e>
lea    0xe67(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
jmp    1394 <find_three_squares+0x234>
movl   $0x9,-0x2c(%rbp)
mov    -0x8(%rbp),%rax
sub    $0x5,%rax
mov    %rax,-0x10(%rbp)
movslq -0x2c(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
ja     137b <find_three_squares+0x21b>
mov    -0x18(%rbp),%rdi
mov    $0x0,%al
call   1060 <is_square@plt>
cmp    $0x0,%eax
je     1365 <find_three_squares+0x205>
movq   $0x1,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    -0x18(%rbp),%rcx
sub    $0x4,%rcx
cmp    %rcx,%rax
ja     1360 <find_three_squares+0x200>
mov    -0x20(%rbp),%rdi
mov    $0x0,%al
call   1060 <is_square@plt>
cmp    $0x0,%eax
je     134a <find_three_squares+0x1ea>
mov    -0x8(%rbp),%rdi
sub    -0x18(%rbp),%rdi
sub    -0x20(%rbp),%rdi
mov    $0x0,%al
call   1060 <is_square@plt>
cmp    $0x0,%eax
je     134a <find_three_squares+0x1ea>
movw   $0x0,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x8(%rbp),%rax
sub    -0x18(%rbp),%rax
sub    -0x20(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpl   $0x0,-0x34(%rbp)
jle    12d6 <find_three_squares+0x176>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movslq -0x34(%rbp),%rcx
cmp    %rcx,%rax
jae    12d1 <find_three_squares+0x171>
mov    -0x40(%rbp),%rax
imul   $0x3,-0x28(%rbp),%rcx
shl    $0x3,%rcx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    $0x3,%edx
mov    $0x0,%al
call   1050 <are_permutations@plt>
cmp    $0x0,%eax
je     12bb <find_three_squares+0x15b>
jmp    1345 <find_three_squares+0x1e5>
jmp    12c0 <find_three_squares+0x160>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1274 <find_three_squares+0x114>
jmp    12d6 <find_three_squares+0x176>
mov    -0x40(%rbp),%rax
imul   $0x3,-0x34(%rbp),%ecx
movslq %ecx,%rcx
mov    -0x60(%rbp),%rdx
mov    %rdx,(%rax,%rcx,8)
mov    -0x58(%rbp),%rdx
mov    %rdx,0x8(%rax,%rcx,8)
mov    -0x50(%rbp),%rdx
mov    %rdx,0x10(%rax,%rcx,8)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    -0x40(%rbp),%rdi
movslq -0x34(%rbp),%rax
shl    $0x3,%rax
imul   $0x3,%rax,%rsi
call   1080 <realloc@plt>
movw   $0x1,-0x2e(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x8(%rbp),%rcx
sub    -0x20(%rbp),%rcx
sub    -0x18(%rbp),%rcx
mov    -0x8(%rbp),%r8
lea    0xd0a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    134a <find_three_squares+0x1ea>
jmp    134f <find_three_squares+0x1ef>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11f3 <find_three_squares+0x93>
jmp    1365 <find_three_squares+0x205>
jmp    136a <find_three_squares+0x20a>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    11c9 <find_three_squares+0x69>
cmpw   $0x0,-0x2e(%rbp)
jne    1394 <find_three_squares+0x234>
lea    0xcf3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_three_squares>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rdi,%rbp
mov    $0x18,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
cmp    $0xd,%rbp
ja     11b6 <find_three_squares+0x46>
lea    0xedb(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>
mov    %rbx,0x18(%rsp)
mov    %rbp,0x8(%rsp)
lea    -0x5(%rbp),%rax
mov    %rax,0x38(%rsp)
mov    $0x9,%r12d
lea    0x20(%rsp),%r14
xor    %r13d,%r13d
movl   $0x0,0x4(%rsp)
jmp    11ff <find_three_squares+0x8f>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%r12
cmp    0x38(%rsp),%r12
ja     1315 <find_three_squares+0x1a5>
mov    %r12,%rdi
xor    %eax,%eax
call   1070 <is_square@plt>
test   %eax,%eax
je     11f0 <find_three_squares+0x80>
mov    0x8(%rsp),%rax
sub    %r12,%rax
mov    %rax,0x40(%rsp)
mov    %rax,%r15
add    $0xfffffffffffffffc,%r15
je     11f0 <find_three_squares+0x80>
mov    $0x1,%ebp
mov    %r15,0x10(%rsp)
jmp    129f <find_three_squares+0x12f>
nop
lea    0x0(%r13,%r13,2),%eax
cltq
mov    0x30(%rsp),%rcx
mov    0x18(%rsp),%rdi
mov    %rcx,0x10(%rdi,%rax,8)
movaps 0x20(%rsp),%xmm0
movups %xmm0,(%rdi,%rax,8)
add    $0x1,%r13d
movslq %r13d,%rax
shl    $0x3,%rax
lea    (%rax,%rax,2),%rsi
call   1090 <realloc@plt>
mov    0x8(%rsp),%r8
mov    %r8,%rcx
sub    %r12,%rcx
sub    %rbp,%rcx
lea    0xd88(%rip),%rdi
mov    %r12,%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%ax
mov    %eax,0x4(%rsp)
mov    0x10(%rsp),%r15
add    $0x1,%rbp
cmp    %r15,%rbp
ja     11f0 <find_three_squares+0x80>
mov    %rbp,%rdi
xor    %eax,%eax
call   1070 <is_square@plt>
test   %eax,%eax
je     1292 <find_three_squares+0x122>
mov    0x40(%rsp),%rbx
sub    %rbp,%rbx
mov    %rbx,%rdi
xor    %eax,%eax
call   1070 <is_square@plt>
test   %eax,%eax
je     1292 <find_three_squares+0x122>
mov    %r12,0x20(%rsp)
mov    %rbp,0x28(%rsp)
mov    %rbx,0x30(%rsp)
test   %r13d,%r13d
jle    1230 <find_three_squares+0xc0>
mov    %r13d,%ebx
mov    0x18(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r14,%rdi
mov    %r15,%rsi
mov    $0x3,%edx
xor    %eax,%eax
call   1060 <are_permutations@plt>
test   %eax,%eax
jne    128d <find_three_squares+0x11d>
add    $0x18,%r15
add    $0xffffffffffffffff,%rbx
jne    12f0 <find_three_squares+0x180>
jmp    1230 <find_three_squares+0xc0>
cmpw   $0x0,0x4(%rsp)
jne    1329 <find_three_squares+0x1b9>
lea    0xd14(%rip),%rdi
call   1040 <puts@plt>
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [72 x i8] c"\0A\0AHow can a number be the sum of three squares if it is minor than 14?\0A\00"
@.str.1 = [56 x i8] c"\0AI found a sum of square numbers: %lu + %lu + %lu = %lu\00"
@.str.2 = [63 x i8] c"\0ACouldn't find three square numbers that matched the requests\0A\00"
define void @find_three_squares(i64 %0) {
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32
%8 = alloca i16
%9 = alloca i16
%10 = alloca i32
%11 = alloca i64*
%12 = alloca i64*
%13 = alloca [3 x i64]
store i64 %0, i64* %2
store i16 0, i16* %8
store i32 0, i32* %10
%14 = call i8* @malloc(i64 24)
%15 = bitcast i8* %14 to i64*
store i64* %15, i64** %11
%16 = load i64, i64* %2
%17 = icmp ult i64 %16, 14
br i1 %17, label %18, label %22
18: ; preds = %1
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str, i64 0, i64 0))
%20 = load i64*, i64** %11
%21 = bitcast i64* %20 to i8*
br label %131
22: ; preds = %1
store i32 9, i32* %7
%23 = load i64, i64* %2
%24 = sub i64 %23, 5
store i64 %24, i64* %3
%25 = load i32, i32* %7
%26 = sext i32 %25 to i64
store i64 %26, i64* %4
br label %27
27: ; preds = %123, %22
%28 = load i64, i64* %4
%29 = load i64, i64* %3
%30 = icmp ule i64 %28, %29
br i1 %30, label %31, label %126
31: ; preds = %27
%32 = load i64, i64* %4
%33 = call i32 (i64, ...) bitcast (i32 (...)* @is_square to i32 (i64, ...)*)(i64 %32)
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %122
35: ; preds = %31
store i64 1, i64* %5
br label %36
36: ; preds = %118, %35
%37 = load i64, i64* %5
%38 = load i64, i64* %2
%39 = load i64, i64* %4
%40 = sub i64 %38, %39
%41 = sub i64 %40, 4
%42 = icmp ule i64 %37, %41
br i1 %42, label %43, label %121
43: ; preds = %36
%44 = load i64, i64* %5
%45 = call i32 (i64, ...) bitcast (i32 (...)* @is_square to i32 (i64, ...)*)(i64 %44)
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %117
47: ; preds = %43
%48 = load i64, i64* %2
%49 = load i64, i64* %4
%50 = sub i64 %48, %49
%51 = load i64, i64* %5
%52 = sub i64 %50, %51
%53 = call i32 (i64, ...) bitcast (i32 (...)* @is_square to i32 (i64, ...)*)(i64 %52)
%54 = icmp ne i32 %53, 0
br i1 %54, label %55, label %117
55: ; preds = %47
store i16 0, i16* %9
%56 = load i64, i64* %4
%57 = getelementptr inbounds [3 x i64], [3 x i64]* %13, i64 0, i64 0
store i64 %56, i64* %57
%58 = load i64, i64* %5
%59 = getelementptr inbounds [3 x i64], [3 x i64]* %13, i64 0, i64 1
store i64 %58, i64* %59
%60 = load i64, i64* %2
%61 = load i64, i64* %4
%62 = sub i64 %60, %61
%63 = load i64, i64* %5
%64 = sub i64 %62, %63
%65 = getelementptr inbounds [3 x i64], [3 x i64]* %13, i64 0, i64 2
store i64 %64, i64* %65
%66 = load i32, i32* %10
%67 = icmp sgt i32 %66, 0
br i1 %67, label %68, label %89
68: ; preds = %55
store i64 0, i64* %6
br label %69
69: ; preds = %85, %68
%70 = load i64, i64* %6
%71 = load i32, i32* %10
%72 = sext i32 %71 to i64
%73 = icmp ult i64 %70, %72
br i1 %73, label %74, label %88
74: ; preds = %69
%75 = load i64*, i64** %11
%76 = load i64, i64* %6
%77 = mul i64 %76, 3
%78 = getelementptr inbounds i64, i64* %75, i64 %77
store i64* %78, i64** %12
%79 = getelementptr inbounds [3 x i64], [3 x i64]* %13, i64 0, i64 0
%80 = load i64*, i64** %12
%81 = call i32 (i64*, i64*, i32, ...) bitcast (i32 (...)* @are_permutations to i32 (i64*, i64*, i32, ...)*)(i64* %79, i64* %80, i32 3)
%82 = icmp ne i32 %81, 0
br i1 %82, label %83, label %84
83: ; preds = %74
br label %116
84: ; preds = %74
br label %85
85: ; preds = %84
%86 = load i64, i64* %6
%87 = add i64 %86, 1
store i64 %87, i64* %6
br label %69
88: ; preds = %69
br label %89
89: ; preds = %88, %55
%90 = load i64*, i64** %11
%91 = load i32, i32* %10
%92 = mul nsw i32 %91, 3
%93 = sext i32 %92 to i64
%94 = getelementptr inbounds i64, i64* %90, i64 %93
%95 = bitcast i64* %94 to i8*
%96 = getelementptr inbounds [3 x i64], [3 x i64]* %13, i64 0, i64 0
%97 = bitcast i64* %96 to i8*
%98 = load i32, i32* %10
%99 = add nsw i32 %98, 1
store i32 %99, i32* %10
%100 = load i64*, i64** %11
%101 = bitcast i64* %100 to i8*
%102 = load i32, i32* %10
%103 = sext i32 %102 to i64
%104 = mul i64 %103, 8
%105 = mul i64 %104, 3
%106 = call i8* @realloc(i8* %101, i64 %105)
store i16 1, i16* %8
%107 = load i64, i64* %4
%108 = load i64, i64* %5
%109 = load i64, i64* %2
%110 = load i64, i64* %5
%111 = sub i64 %109, %110
%112 = load i64, i64* %4
%113 = sub i64 %111, %112
%114 = load i64, i64* %2
%115 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0), i64 %107, i64 %108, i64 %113, i64 %114)
br label %116
116: ; preds = %89, %83
br label %117
117: ; preds = %116, %47, %43
br label %118
118: ; preds = %117
%119 = load i64, i64* %5
%120 = add i64 %119, 1
store i64 %120, i64* %5
br label %36
121: ; preds = %36
br label %122
122: ; preds = %121, %31
br label %123
123: ; preds = %122
%124 = load i64, i64* %4
%125 = add i64 %124, 1
store i64 %125, i64* %4
br label %27
126: ; preds = %27
%127 = load i16, i16* %8
%128 = icmp ne i16 %127, 0
br i1 %128, label %131, label %129
129: ; preds = %126
%130 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.2, i64 0, i64 0))
br label %131
131: ; preds = %18, %129, %126
ret void
}

LLVM IR (-O2):
@.str.1 = [56 x i8] c"\0AI found a sum of square numbers: %lu + %lu + %lu = %lu\00"
@str = [62 x i8] c"\0ACouldn't find three square numbers that matched the requests\00"
@str.3 = [71 x i8] c"\0A\0AHow can a number be the sum of three squares if it is minor than 14?\00"
define void @find_three_squares(i64 %0) {
%2 = alloca [3 x i64]
%3 = tail call dereferenceable_or_null(24) i8* @malloc(i64 24)
%4 = bitcast i8* %3 to i64*
%5 = bitcast [3 x i64]* %2 to i8*
%6 = icmp ult i64 %0, 14
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([71 x i8], [71 x i8]* @str.3, i64 0, i64 0))
tail call void @free(i8* %3)
br label %73
9: ; preds = %1
%10 = add i64 %0, -5
%11 = getelementptr inbounds [3 x i64], [3 x i64]* %2, i64 0, i64 0
%12 = getelementptr inbounds [3 x i64], [3 x i64]* %2, i64 0, i64 1
%13 = getelementptr inbounds [3 x i64], [3 x i64]* %2, i64 0, i64 2
br label %14
14: ; preds = %9, %64
%15 = phi i64 [ 9, %9 ], [ %67, %64 ]
%16 = phi i32 [ 0, %9 ], [ %66, %64 ]
%17 = phi i16 [ 0, %9 ], [ %65, %64 ]
%18 = call i32 (i64, ...) bitcast (i32 (...)* @is_square to i32 (i64, ...)*)(i64 %15)
%19 = icmp eq i32 %18, 0
br i1 %19, label %64, label %20
20: ; preds = %14
%21 = sub i64 %0, %15
%22 = add i64 %21, -4
%23 = icmp eq i64 %22, 0
br i1 %23, label %64, label %24
24: ; preds = %20, %59
%25 = phi i64 [ %62, %59 ], [ 1, %20 ]
%26 = phi i32 [ %61, %59 ], [ %16, %20 ]
%27 = phi i16 [ %60, %59 ], [ %17, %20 ]
%28 = call i32 (i64, ...) bitcast (i32 (...)* @is_square to i32 (i64, ...)*)(i64 %25)
%29 = icmp eq i32 %28, 0
br i1 %29, label %59, label %30
30: ; preds = %24
%31 = sub i64 %21, %25
%32 = call i32 (i64, ...) bitcast (i32 (...)* @is_square to i32 (i64, ...)*)(i64 %31)
%33 = icmp eq i32 %32, 0
br i1 %33, label %59, label %34
34: ; preds = %30
store i64 %15, i64* %11
store i64 %25, i64* %12
store i64 %31, i64* %13
%35 = icmp sgt i32 %26, 0
br i1 %35, label %36, label %47
36: ; preds = %34
%37 = zext i32 %26 to i64
br label %40
38: ; preds = %40
%39 = icmp eq i64 %46, %37
br i1 %39, label %47, label %40
40: ; preds = %36, %38
%41 = phi i64 [ 0, %36 ], [ %46, %38 ]
%42 = mul i64 %41, 3
%43 = getelementptr inbounds i64, i64* %4, i64 %42
%44 = call i32 (i64*, i64*, i32, ...) bitcast (i32 (...)* @are_permutations to i32 (i64*, i64*, i32, ...)*)(i64* %11, i64* %43, i32 3)
%45 = icmp eq i32 %44, 0
%46 = add nuw i64 %41, 1
br i1 %45, label %38, label %59
47: ; preds = %38, %34
%48 = mul nsw i32 %26, 3
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i64, i64* %4, i64 %49
%51 = bitcast i64* %50 to i8*
%52 = add nsw i32 %26, 1
%53 = sext i32 %52 to i64
%54 = mul nsw i64 %53, 24
%55 = call i8* @realloc(i8* %3, i64 %54)
%56 = add i64 %15, %25
%57 = sub i64 %0, %56
%58 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0), i64 %15, i64 %25, i64 %57, i64 %0)
br label %59
59: ; preds = %40, %24, %30, %47
%60 = phi i16 [ 1, %47 ], [ %27, %30 ], [ %27, %24 ], [ %27, %40 ]
%61 = phi i32 [ %52, %47 ], [ %26, %30 ], [ %26, %24 ], [ %26, %40 ]
%62 = add i64 %25, 1
%63 = icmp ugt i64 %62, %22
br i1 %63, label %64, label %24
64: ; preds = %59, %20, %14
%65 = phi i16 [ %17, %14 ], [ %17, %20 ], [ %60, %59 ]
%66 = phi i32 [ %16, %14 ], [ %16, %20 ], [ %61, %59 ]
%67 = add i64 %15, 1
%68 = icmp ugt i64 %67, %10
br i1 %68, label %69, label %14
69: ; preds = %64
%70 = icmp eq i16 %65, 0
br i1 %70, label %71, label %73
71: ; preds = %69
%72 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([62 x i8], [62 x i8]* @str, i64 0, i64 0))
br label %73
73: ; preds = %69, %71, %7
ret void
}