#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*/ free(char *);
int getSumPatch(int *, int);
scalar_t__ malloc(int);
int /*<<< orphan*/ multiplyImages(int *, int *, int *, int, int);
int calculateG(int *G, int *DX, int *DY, int half_window_size) {
  int patch_size;
  int *DXX;
  int *DXY;
  int *DYY;
  patch_size = 2 * half_window_size + 1;
  DXX = (int *)malloc(patch_size * patch_size * sizeof(int));
  DXY = (int *)malloc(patch_size * patch_size * sizeof(int));
  DYY = (int *)malloc(patch_size * patch_size * sizeof(int));
  if (DXX == 0 || DXY == 0 || DYY == 0)
    return -1;
  multiplyImages(DX, DX, DXX, patch_size, patch_size);
  multiplyImages(DX, DY, DXY, patch_size, patch_size);
  multiplyImages(DY, DY, DYY, patch_size, patch_size);
  G[0] = getSumPatch(DXX, patch_size);
  G[1] = getSumPatch(DXY, patch_size);
  G[2] = G[1];
  G[3] = getSumPatch(DYY, patch_size);
  free((char *)DXX);
  free((char *)DXY);
  free((char *)DYY);
  return 0;
}


x86 assembly (clang, -O0):
<calculateG>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x24(%rbp),%eax
shl    $0x1,%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
imul   -0x28(%rbp),%eax
cltq
shl    $0x2,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%eax
imul   -0x28(%rbp),%eax
cltq
shl    $0x2,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%eax
imul   -0x28(%rbp),%eax
cltq
shl    $0x2,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11cc <calculateG+0x8c>
cmpq   $0x0,-0x38(%rbp)
je     11cc <calculateG+0x8c>
cmpq   $0x0,-0x40(%rbp)
jne    11d8 <calculateG+0x98>
movl   $0xffffffff,-0x4(%rbp)
jmp    128e <calculateG+0x14e>
mov    -0x18(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    -0x28(%rbp),%ecx
mov    -0x28(%rbp),%r8d
call   1060 <multiplyImages@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0x28(%rbp),%ecx
mov    -0x28(%rbp),%r8d
call   1060 <multiplyImages@plt>
mov    -0x20(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x40(%rbp),%rdx
mov    -0x28(%rbp),%ecx
mov    -0x28(%rbp),%r8d
call   1060 <multiplyImages@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%esi
call   1050 <getSumPatch@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%esi
call   1050 <getSumPatch@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%esi
call   1050 <getSumPatch@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0xc(%rax)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calculateG>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,0x8(%rsp)
mov    %rsi,%r14
mov    %rdi,0x10(%rsp)
lea    (%rcx,%rcx,1),%r13d
add    $0x1,%r13d
mov    %r13d,%ebp
imul   %r13d,%ebp
shl    $0x2,%ebp
mov    %ebp,%edi
call   1040 <malloc@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1040 <malloc@plt>
mov    %rax,%r15
mov    %ebp,%edi
call   1040 <malloc@plt>
mov    %rax,%r12
mov    $0xffffffff,%eax
test   %rbx,%rbx
je     1238 <calculateG+0xf8>
test   %r15,%r15
je     1238 <calculateG+0xf8>
test   %r12,%r12
je     1238 <calculateG+0xf8>
mov    %r14,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
mov    %r13d,%ecx
mov    %r13d,%r8d
call   1060 <multiplyImages@plt>
mov    %r14,%rdi
mov    0x8(%rsp),%rbp
mov    %rbp,%rsi
mov    %r15,%rdx
mov    %r13d,%ecx
mov    %r13d,%r8d
call   1060 <multiplyImages@plt>
mov    %rbp,%rdi
mov    %rbp,%rsi
mov    %r12,%rdx
mov    %r13d,%ecx
mov    %r13d,%r8d
call   1060 <multiplyImages@plt>
mov    %rbx,%rdi
mov    %r13d,%esi
call   1050 <getSumPatch@plt>
mov    0x10(%rsp),%rbp
mov    %eax,0x0(%rbp)
mov    %r15,%rdi
mov    %r13d,%esi
call   1050 <getSumPatch@plt>
mov    %eax,0x4(%rbp)
mov    %eax,0x8(%rbp)
mov    %r12,%rdi
mov    %r13d,%esi
call   1050 <getSumPatch@plt>
mov    %eax,0xc(%rbp)
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @calculateG(i32* %0, i32* %1, i32* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32*
%12 = alloca i32*
%13 = alloca i32*
store i32* %0, i32** %6
store i32* %1, i32** %7
store i32* %2, i32** %8
store i32 %3, i32* %9
%14 = load i32, i32* %9
%15 = mul nsw i32 2, %14
%16 = add nsw i32 %15, 1
store i32 %16, i32* %10
%17 = load i32, i32* %10
%18 = load i32, i32* %10
%19 = mul nsw i32 %17, %18
%20 = sext i32 %19 to i64
%21 = mul i64 %20, 4
%22 = trunc i64 %21 to i32
%23 = call i64 @malloc(i32 %22)
%24 = inttoptr i64 %23 to i32*
store i32* %24, i32** %11
%25 = load i32, i32* %10
%26 = load i32, i32* %10
%27 = mul nsw i32 %25, %26
%28 = sext i32 %27 to i64
%29 = mul i64 %28, 4
%30 = trunc i64 %29 to i32
%31 = call i64 @malloc(i32 %30)
%32 = inttoptr i64 %31 to i32*
store i32* %32, i32** %12
%33 = load i32, i32* %10
%34 = load i32, i32* %10
%35 = mul nsw i32 %33, %34
%36 = sext i32 %35 to i64
%37 = mul i64 %36, 4
%38 = trunc i64 %37 to i32
%39 = call i64 @malloc(i32 %38)
%40 = inttoptr i64 %39 to i32*
store i32* %40, i32** %13
%41 = load i32*, i32** %11
%42 = icmp eq i32* %41, null
br i1 %42, label %49, label %43
43: ; preds = %4
%44 = load i32*, i32** %12
%45 = icmp eq i32* %44, null
br i1 %45, label %49, label %46
46: ; preds = %43
%47 = load i32*, i32** %13
%48 = icmp eq i32* %47, null
br i1 %48, label %49, label %50
49: ; preds = %46, %43, %4
store i32 -1, i32* %5
br label %98
50: ; preds = %46
%51 = load i32*, i32** %7
%52 = load i32*, i32** %7
%53 = load i32*, i32** %11
%54 = load i32, i32* %10
%55 = load i32, i32* %10
%56 = call i32 @multiplyImages(i32* %51, i32* %52, i32* %53, i32 %54, i32 %55)
%57 = load i32*, i32** %7
%58 = load i32*, i32** %8
%59 = load i32*, i32** %12
%60 = load i32, i32* %10
%61 = load i32, i32* %10
%62 = call i32 @multiplyImages(i32* %57, i32* %58, i32* %59, i32 %60, i32 %61)
%63 = load i32*, i32** %8
%64 = load i32*, i32** %8
%65 = load i32*, i32** %13
%66 = load i32, i32* %10
%67 = load i32, i32* %10
%68 = call i32 @multiplyImages(i32* %63, i32* %64, i32* %65, i32 %66, i32 %67)
%69 = load i32*, i32** %11
%70 = load i32, i32* %10
%71 = call i32 @getSumPatch(i32* %69, i32 %70)
%72 = load i32*, i32** %6
%73 = getelementptr inbounds i32, i32* %72, i64 0
store i32 %71, i32* %73
%74 = load i32*, i32** %12
%75 = load i32, i32* %10
%76 = call i32 @getSumPatch(i32* %74, i32 %75)
%77 = load i32*, i32** %6
%78 = getelementptr inbounds i32, i32* %77, i64 1
store i32 %76, i32* %78
%79 = load i32*, i32** %6
%80 = getelementptr inbounds i32, i32* %79, i64 1
%81 = load i32, i32* %80
%82 = load i32*, i32** %6
%83 = getelementptr inbounds i32, i32* %82, i64 2
store i32 %81, i32* %83
%84 = load i32*, i32** %13
%85 = load i32, i32* %10
%86 = call i32 @getSumPatch(i32* %84, i32 %85)
%87 = load i32*, i32** %6
%88 = getelementptr inbounds i32, i32* %87, i64 3
store i32 %86, i32* %88
%89 = load i32*, i32** %11
%90 = bitcast i32* %89 to i8*
%91 = call i32 @free(i8* %90)
%92 = load i32*, i32** %12
%93 = bitcast i32* %92 to i8*
%94 = call i32 @free(i8* %93)
%95 = load i32*, i32** %13
%96 = bitcast i32* %95 to i8*
%97 = call i32 @free(i8* %96)
store i32 0, i32* %5
br label %98
98: ; preds = %50, %49
%99 = load i32, i32* %5
ret i32 %99
}

LLVM IR (-O2):
define i32 @calculateG(i32* nocapture writeonly %0, i32* %1, i32* %2, i32 %3) {
%5 = shl nsw i32 %3, 1
%6 = or i32 %5, 1
%7 = shl i32 %6, 2
%8 = mul i32 %7, %6
%9 = tail call i64 @malloc(i32 %8)
%10 = inttoptr i64 %9 to i32*
%11 = tail call i64 @malloc(i32 %8)
%12 = inttoptr i64 %11 to i32*
%13 = tail call i64 @malloc(i32 %8)
%14 = inttoptr i64 %13 to i32*
%15 = icmp eq i64 %9, 0
%16 = icmp eq i64 %11, 0
%17 = select i1 %15, i1 true, i1 %16
%18 = icmp eq i64 %13, 0
%19 = select i1 %17, i1 true, i1 %18
br i1 %19, label %36, label %20
20: ; preds = %4
%21 = tail call i32 @multiplyImages(i32* %1, i32* %1, i32* %10, i32 %6, i32 %6)
%22 = tail call i32 @multiplyImages(i32* %1, i32* %2, i32* %12, i32 %6, i32 %6)
%23 = tail call i32 @multiplyImages(i32* %2, i32* %2, i32* %14, i32 %6, i32 %6)
%24 = tail call i32 @getSumPatch(i32* %10, i32 %6)
store i32 %24, i32* %0
%25 = tail call i32 @getSumPatch(i32* %12, i32 %6)
%26 = getelementptr inbounds i32, i32* %0, i64 1
store i32 %25, i32* %26
%27 = getelementptr inbounds i32, i32* %0, i64 2
store i32 %25, i32* %27
%28 = tail call i32 @getSumPatch(i32* %14, i32 %6)
%29 = getelementptr inbounds i32, i32* %0, i64 3
store i32 %28, i32* %29
%30 = inttoptr i64 %9 to i8*
%31 = tail call i32 @free(i8* %30)
%32 = inttoptr i64 %11 to i8*
%33 = tail call i32 @free(i8* %32)
%34 = inttoptr i64 %13 to i8*
%35 = tail call i32 @free(i8* %34)
br label %36
36: ; preds = %4, %20
%37 = phi i32 [ 0, %20 ], [ -1, %4 ]
ret i32 %37
}