#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 */
typedef scalar_t__ clock_t;

/* Variables and functions */
double CLOCKS_PER_SEC;
scalar_t__ clock();
unsigned long factorValue(unsigned long, unsigned long **, unsigned long **);
int /*<<< orphan*/ printf(char *, ...);
long findPythagTriplets() {
  clock_t start, end;
  double exe_time1;
  unsigned long r = 2;
  unsigned long *sFactors;
  unsigned long *tFactors;
  unsigned long totalFactors;
  unsigned long a;
  unsigned long b;
  unsigned long c;
  unsigned long sum;
  unsigned long product;
  do {
    start = clock();
    totalFactors = factorValue(r * r / 2, &sFactors, &tFactors);
    printf("Generating all possible Pythagorean Triplets\n");
    for (int factors = 0; factors < totalFactors; ++factors) {
      a = r + sFactors[factors];
      b = r + tFactors[factors];
      c = r + sFactors[factors] + tFactors[factors];
      sum = a + b + c;
      product = a * b * c;
      printf("a=%lu : b=%lu : c=%lu : sum:%lu : a*b*c=%lu\n", a, b, c, sum,
             product);
      if (sum == 1000) {
        end = clock();
        exe_time1 = (double)(end - start) / CLOCKS_PER_SEC;
        printf("It took %f to find the Pythagorean Triplet that adds to 1000\n",
               exe_time1);
        return product;
      }
    }
    r += 2;
  } while (r < 1000);
  return 0;
}


x86 assembly (clang, -O0):
<findPythagTriplets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movq   $0x2,-0x28(%rbp)
mov    $0x0,%al
call   1030 <clock@plt>
mov    %rax,-0x10(%rbp)
mov    -0x28(%rbp),%rdi
imul   -0x28(%rbp),%rdi
shr    $0x1,%rdi
lea    -0x30(%rbp),%rsi
lea    -0x38(%rbp),%rdx
call   1050 <factorValue@plt>
mov    %rax,-0x40(%rbp)
lea    0xe90(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x6c(%rbp)
movslq -0x6c(%rbp),%rax
cmp    -0x40(%rbp),%rax
jae    1281 <findPythagTriplets+0x151>
mov    -0x28(%rbp),%rax
mov    -0x30(%rbp),%rcx
movslq -0x6c(%rbp),%rdx
add    (%rcx,%rdx,8),%rax
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x38(%rbp),%rcx
movslq -0x6c(%rbp),%rdx
add    (%rcx,%rdx,8),%rax
mov    %rax,-0x50(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x30(%rbp),%rcx
movslq -0x6c(%rbp),%rdx
add    (%rcx,%rdx,8),%rax
mov    -0x38(%rbp),%rcx
movslq -0x6c(%rbp),%rdx
add    (%rcx,%rdx,8),%rax
mov    %rax,-0x58(%rbp)
mov    -0x48(%rbp),%rax
add    -0x50(%rbp),%rax
add    -0x58(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x48(%rbp),%rax
imul   -0x50(%rbp),%rax
imul   -0x58(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x48(%rbp),%rsi
mov    -0x50(%rbp),%rdx
mov    -0x58(%rbp),%rcx
mov    -0x60(%rbp),%r8
mov    -0x68(%rbp),%r9
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpq   $0x3e8,-0x60(%rbp)
jne    126e <findPythagTriplets+0x13e>
mov    $0x0,%al
call   1030 <clock@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
sub    -0x10(%rbp),%rax
cvtsi2sd %rax,%xmm0
mov    0x2da3(%rip),%rax
divsd  (%rax),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
lea    0xe01(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
mov    -0x68(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12a3 <findPythagTriplets+0x173>
jmp    1273 <findPythagTriplets+0x143>
mov    -0x6c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x6c(%rbp)
jmp    117e <findPythagTriplets+0x4e>
mov    -0x28(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x3e8,-0x28(%rbp)
jb     1140 <findPythagTriplets+0x10>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<findPythagTriplets>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    $0x2,%r12d
lea    0xea5(%rip),%rbx
jmp    1175 <findPythagTriplets+0x35>
nopl   (%rax)
lea    0x2(%r12),%rax
cmp    $0x3e6,%r12
mov    %rax,%r12
jae    1240 <findPythagTriplets+0x100>
xor    %eax,%eax
call   1040 <clock@plt>
mov    %rax,(%rsp)
mov    %r12,%rdi
imul   %r12,%rdi
shr    %rdi
lea    0x10(%rsp),%rsi
lea    0x8(%rsp),%rdx
call   1060 <factorValue@plt>
mov    %rax,%rbp
lea    0xec8(%rip),%rdi
call   1030 <puts@plt>
test   %rbp,%rbp
je     1160 <findPythagTriplets+0x20>
xor    %r13d,%r13d
mov    0x10(%rsp),%rax
mov    (%rax,%r13,8),%rsi
add    %r12,%rsi
mov    0x8(%rsp),%rax
mov    (%rax,%r13,8),%rcx
lea    (%rcx,%r12,1),%rdx
add    %rsi,%rcx
lea    (%rdx,%rsi,1),%r15
add    %rcx,%r15
mov    %rdx,%r14
imul   %rsi,%r14
imul   %rcx,%r14
mov    %rbx,%rdi
mov    %r15,%r8
mov    %r14,%r9
xor    %eax,%eax
call   1050 <printf@plt>
cmp    $0x3e8,%r15
je     1205 <findPythagTriplets+0xc5>
add    $0x1,%r13
cmp    %r13,%rbp
jne    11b0 <findPythagTriplets+0x70>
jmp    1160 <findPythagTriplets+0x20>
xor    %eax,%eax
call   1040 <clock@plt>
sub    (%rsp),%rax
cvtsi2sd %rax,%xmm0
mov    0x2dcc(%rip),%rax
divsd  (%rax),%xmm0
lea    0xe06(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
mov    %r14,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %r14d,%r14d
jmp    122e <findPythagTriplets+0xee>

LLVM IR (-O0):
@.str = [46 x i8] c"Generating all possible Pythagorean Triplets\0A\00"
@.str.1 = [45 x i8] c"a=%lu : b=%lu : c=%lu : sum:%lu : a*b*c=%lu\0A\00"
@CLOCKS_PER_SEC = global double 0.000000e+00
@.str.2 = [62 x i8] c"It took %f to find the Pythagorean Triplet that adds to 1000\0A\00"
define i64 @findPythagTriplets() {
%1 = alloca i64
%2 = alloca i64
%3 = alloca i64
%4 = alloca double
%5 = alloca i64
%6 = alloca i64*
%7 = alloca i64*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
%13 = alloca i64
%14 = alloca i32
store i64 2, i64* %5
br label %15
15: ; preds = %92, %0
%16 = call i64 (...) @clock()
store i64 %16, i64* %2
%17 = load i64, i64* %5
%18 = load i64, i64* %5
%19 = mul i64 %17, %18
%20 = udiv i64 %19, 2
%21 = call i64 @factorValue(i64 %20, i64** %6, i64** %7)
store i64 %21, i64* %8
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %14
br label %23
23: ; preds = %86, %15
%24 = load i32, i32* %14
%25 = sext i32 %24 to i64
%26 = load i64, i64* %8
%27 = icmp ult i64 %25, %26
br i1 %27, label %28, label %89
28: ; preds = %23
%29 = load i64, i64* %5
%30 = load i64*, i64** %6
%31 = load i32, i32* %14
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i64, i64* %30, i64 %32
%34 = load i64, i64* %33
%35 = add i64 %29, %34
store i64 %35, i64* %9
%36 = load i64, i64* %5
%37 = load i64*, i64** %7
%38 = load i32, i32* %14
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i64, i64* %37, i64 %39
%41 = load i64, i64* %40
%42 = add i64 %36, %41
store i64 %42, i64* %10
%43 = load i64, i64* %5
%44 = load i64*, i64** %6
%45 = load i32, i32* %14
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i64, i64* %44, i64 %46
%48 = load i64, i64* %47
%49 = add i64 %43, %48
%50 = load i64*, i64** %7
%51 = load i32, i32* %14
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i64, i64* %50, i64 %52
%54 = load i64, i64* %53
%55 = add i64 %49, %54
store i64 %55, i64* %11
%56 = load i64, i64* %9
%57 = load i64, i64* %10
%58 = add i64 %56, %57
%59 = load i64, i64* %11
%60 = add i64 %58, %59
store i64 %60, i64* %12
%61 = load i64, i64* %9
%62 = load i64, i64* %10
%63 = mul i64 %61, %62
%64 = load i64, i64* %11
%65 = mul i64 %63, %64
store i64 %65, i64* %13
%66 = load i64, i64* %9
%67 = load i64, i64* %10
%68 = load i64, i64* %11
%69 = load i64, i64* %12
%70 = load i64, i64* %13
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i64 %66, i64 %67, i64 %68, i64 %69, i64 %70)
%72 = load i64, i64* %12
%73 = icmp eq i64 %72, 1000
br i1 %73, label %74, label %85
74: ; preds = %28
%75 = call i64 (...) @clock()
store i64 %75, i64* %3
%76 = load i64, i64* %3
%77 = load i64, i64* %2
%78 = sub nsw i64 %76, %77
%79 = sitofp i64 %78 to double
%80 = load double, double* @CLOCKS_PER_SEC
%81 = fdiv double %79, %80
store double %81, double* %4
%82 = load double, double* %4
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.2, i64 0, i64 0), double %82)
%84 = load i64, i64* %13
store i64 %84, i64* %1
br label %96
85: ; preds = %28
br label %86
86: ; preds = %85
%87 = load i32, i32* %14
%88 = add nsw i32 %87, 1
store i32 %88, i32* %14
br label %23
89: ; preds = %23
%90 = load i64, i64* %5
%91 = add i64 %90, 2
store i64 %91, i64* %5
br label %92
92: ; preds = %89
%93 = load i64, i64* %5
%94 = icmp ult i64 %93, 1000
br i1 %94, label %15, label %95
95: ; preds = %92
store i64 0, i64* %1
br label %96
96: ; preds = %95, %74
%97 = load i64, i64* %1
ret i64 %97
}

LLVM IR (-O2):
@.str.1 = [45 x i8] c"a=%lu : b=%lu : c=%lu : sum:%lu : a*b*c=%lu\0A\00"
@CLOCKS_PER_SEC = global double 0.000000e+00
@.str.2 = [62 x i8] c"It took %f to find the Pythagorean Triplet that adds to 1000\0A\00"
@str = [45 x i8] c"Generating all possible Pythagorean Triplets\00"
define i64 @findPythagTriplets() {
%1 = alloca i64*
%2 = alloca i64*
%3 = bitcast i64** %1 to i8*
%4 = bitcast i64** %2 to i8*
br label %5
5: ; preds = %40, %0
%6 = phi i64 [ 2, %0 ], [ %41, %40 ]
%7 = call i64 (...) @clock()
%8 = mul nuw nsw i64 %6, %6
%9 = lshr exact i64 %8, 1
%10 = call i64 @factorValue(i64 %9, i64** %1, i64** %2)
%11 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @str, i64 0, i64 0))
%12 = icmp eq i64 %10, 0
br i1 %12, label %40, label %15
13: ; preds = %15
%14 = icmp eq i64 %32, %10
br i1 %14, label %40, label %15
15: ; preds = %5, %13
%16 = phi i64 [ %32, %13 ], [ 0, %5 ]
%17 = load i64*, i64** %1
%18 = getelementptr inbounds i64, i64* %17, i64 %16
%19 = load i64, i64* %18
%20 = add i64 %19, %6
%21 = load i64*, i64** %2
%22 = getelementptr inbounds i64, i64* %21, i64 %16
%23 = load i64, i64* %22
%24 = add i64 %23, %6
%25 = add i64 %23, %20
%26 = add i64 %24, %20
%27 = add i64 %26, %25
%28 = mul i64 %24, %20
%29 = mul i64 %28, %25
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i64 %20, i64 %24, i64 %25, i64 %27, i64 %29)
%31 = icmp eq i64 %27, 1000
%32 = add nuw i64 %16, 1
br i1 %31, label %33, label %13
33: ; preds = %15
%34 = call i64 (...) @clock()
%35 = sub nsw i64 %34, %7
%36 = sitofp i64 %35 to double
%37 = load double, double* @CLOCKS_PER_SEC
%38 = fdiv double %36, %37
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([62 x i8], [62 x i8]* @.str.2, i64 0, i64 0), double %38)
br label %43
40: ; preds = %13, %5
%41 = add nuw nsw i64 %6, 2
%42 = icmp ult i64 %6, 998
br i1 %42, label %5, label %43
43: ; preds = %40, %33
%44 = phi i64 [ %29, %33 ], [ 0, %40 ]
ret i64 %44
}