#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 */
int /*<<< orphan*/ printf(char *, long);
long findSquareOfSum_efficient(int maxValue) {
  long largestIndex = maxValue + 1;
  long totalSum;
  if (maxValue % 2 == 0)
    totalSum = largestIndex * (maxValue / 2);
  else
    totalSum = largestIndex * ((maxValue + 1) / 2);
  totalSum *= totalSum;
  printf("Square of Sums (1 + 2 + 3 + 4)^2 = %lu\n", totalSum);
  return totalSum;
}


x86 assembly (clang, -O0):
<findSquareOfSum_efficient>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1,%eax
cltq
mov    %rax,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    1164 <findSquareOfSum_efficient+0x54>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
imul   %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    118b <findSquareOfSum_efficient+0x7b>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
imul   %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
imul   -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xe5d(%rip),%rdi
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<findSquareOfSum_efficient>:
push   %rbx
lea    0x1(%rdi),%eax
test   $0x1,%dil
cmovne %eax,%edi
cltq
mov    %edi,%ecx
shr    $0x1f,%ecx
add    %edi,%ecx
sar    %ecx
movslq %ecx,%rbx
imul   %rax,%rbx
imul   %rbx,%rbx
lea    0xec8(%rip),%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [40 x i8] c"Square of Sums (1 + 2 + 3 + 4)^2 = %lu\0A\00"
define i64 @findSquareOfSum_efficient(i32 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca i64
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = add nsw i32 %5, 1
%7 = sext i32 %6 to i64
store i64 %7, i64* %3
%8 = load i32, i32* %2
%9 = srem i32 %8, 2
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %17
11: ; preds = %1
%12 = load i64, i64* %3
%13 = load i32, i32* %2
%14 = sdiv i32 %13, 2
%15 = sext i32 %14 to i64
%16 = mul nsw i64 %12, %15
store i64 %16, i64* %4
br label %24
17: ; preds = %1
%18 = load i64, i64* %3
%19 = load i32, i32* %2
%20 = add nsw i32 %19, 1
%21 = sdiv i32 %20, 2
%22 = sext i32 %21 to i64
%23 = mul nsw i64 %18, %22
store i64 %23, i64* %4
br label %24
24: ; preds = %17, %11
%25 = load i64, i64* %4
%26 = load i64, i64* %4
%27 = mul nsw i64 %26, %25
store i64 %27, i64* %4
%28 = load i64, i64* %4
%29 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i64 %28)
%30 = load i64, i64* %4
ret i64 %30
}

LLVM IR (-O2):
@.str = [40 x i8] c"Square of Sums (1 + 2 + 3 + 4)^2 = %lu\0A\00"
define i64 @findSquareOfSum_efficient(i32 %0) {
%2 = add nsw i32 %0, 1
%3 = and i32 %0, 1
%4 = icmp eq i32 %3, 0
%5 = select i1 %4, i32 %0, i32 %2
%6 = sext i32 %2 to i64
%7 = sdiv i32 %5, 2
%8 = sext i32 %7 to i64
%9 = mul nsw i64 %8, %6
%10 = mul nsw i64 %9, %9
%11 = tail call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i64 %10)
ret i64 %10
}