#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 */
double calcEnergy(int const, double *, double const **);
int /*<<< orphan*/ calcOutputPattern(int const, double const **, double *,
                                     double *);
int /*<<< orphan*/ copyPattern(int const, double const *, double *);
int /*<<< orphan*/ equals(double, double);
int /*<<< orphan*/ printf(char *, double);
int /*<<< orphan*/ showPattern(double *);
double calcAssociatedPattern(const int patternSize, const double W[][1000],
                             const double inputPattern[],
                             double associatedPattern[]) {
  double pattern[patternSize];
  copyPattern(patternSize, inputPattern, pattern);
  double energy = calcEnergy(patternSize, pattern, W);
  double energyPrevious = 0.0;
  do {
    energyPrevious = energy;
    calcOutputPattern(patternSize, W, pattern, associatedPattern);
    energy = calcEnergy(patternSize, associatedPattern, W);
    copyPattern(patternSize, associatedPattern, pattern);
    printf("\n    Energy = %9.4f\n\n", energy);
  } while (!equals(energyPrevious, energy));
  showPattern(associatedPattern);
  return energy;
}


x86 assembly (clang, -O0):
<calcAssociatedPattern>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x4(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
lea    0xf(,%rax,8),%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdx
sub    %rcx,%rdx
mov    %rdx,-0x48(%rbp)
mov    %rdx,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1070 <copyPattern@plt>
mov    -0x48(%rbp),%rsi
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rdx
call   1040 <calcEnergy@plt>
movsd  %xmm0,-0x38(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0x48(%rbp),%rdx
movsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rcx
call   1030 <calcOutputPattern@plt>
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1040 <calcEnergy@plt>
mov    -0x48(%rbp),%rdx
movsd  %xmm0,-0x38(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   1070 <copyPattern@plt>
movsd  -0x38(%rbp),%xmm0
lea    0xdea(%rip),%rdi
call   1050 <printf@plt>
movsd  -0x40(%rbp),%xmm0
movsd  -0x38(%rbp),%xmm1
call   1080 <equals@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11c7 <calcAssociatedPattern+0x67>
mov    -0x20(%rbp),%rdi
call   1060 <showPattern@plt>
movsd  -0x38(%rbp),%xmm0
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calcAssociatedPattern>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r15
mov    %rsi,%r14
mov    %edi,%ebx
mov    %edi,%eax
mov    %rsp,%r12
lea    0xf(,%rax,8),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
mov    %rdx,%rsi
mov    %r12,%rdx
call   1070 <copyPattern@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    %r14,%rdx
call   1040 <calcEnergy@plt>
movsd  %xmm0,-0x30(%rbp)
lea    0xe4c(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r12,%rdx
mov    %r15,%rcx
call   1030 <calcOutputPattern@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1040 <calcEnergy@plt>
movsd  %xmm0,-0x30(%rbp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   1070 <copyPattern@plt>
mov    %r13,%rdi
movsd  -0x30(%rbp),%xmm0
call   1050 <printf@plt>
movsd  -0x38(%rbp),%xmm0
movsd  -0x30(%rbp),%xmm1
call   1080 <equals@plt>
test   %eax,%eax
je     11c0 <calcAssociatedPattern+0x60>
mov    %r15,%rdi
call   1060 <showPattern@plt>
movsd  -0x30(%rbp),%xmm0
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [22 x i8] c"\0A Energy = %9.4f\0A\0A\00"
define double @calcAssociatedPattern(i32 %0, [1000 x double]* %1, double* %2, double* %3) {
%5 = alloca i32
%6 = alloca [1000 x double]*
%7 = alloca double*
%8 = alloca double*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca double
%12 = alloca double
store i32 %0, i32* %5
store [1000 x double]* %1, [1000 x double]** %6
store double* %2, double** %7
store double* %3, double** %8
%13 = load i32, i32* %5
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %9
%16 = alloca double, i64 %14
store i64 %14, i64* %10
%17 = load i32, i32* %5
%18 = load double*, double** %7
%19 = call i32 @copyPattern(i32 %17, double* %18, double* %16)
%20 = load i32, i32* %5
%21 = load [1000 x double]*, [1000 x double]** %6
%22 = bitcast [1000 x double]* %21 to double**
%23 = call double @calcEnergy(i32 %20, double* %16, double** %22)
store double %23, double* %11
store double 0.000000e+00, double* %12
br label %24
24: ; preds = %41, %4
%25 = load double, double* %11
store double %25, double* %12
%26 = load i32, i32* %5
%27 = load [1000 x double]*, [1000 x double]** %6
%28 = bitcast [1000 x double]* %27 to double**
%29 = load double*, double** %8
%30 = call i32 @calcOutputPattern(i32 %26, double** %28, double* %16, double* %29)
%31 = load i32, i32* %5
%32 = load double*, double** %8
%33 = load [1000 x double]*, [1000 x double]** %6
%34 = bitcast [1000 x double]* %33 to double**
%35 = call double @calcEnergy(i32 %31, double* %32, double** %34)
store double %35, double* %11
%36 = load i32, i32* %5
%37 = load double*, double** %8
%38 = call i32 @copyPattern(i32 %36, double* %37, double* %16)
%39 = load double, double* %11
%40 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), double %39)
br label %41
41: ; preds = %24
%42 = load double, double* %12
%43 = load double, double* %11
%44 = call i32 @equals(double %42, double %43)
%45 = icmp ne i32 %44, 0
%46 = xor i1 %45, true
br i1 %46, label %24, label %47
47: ; preds = %41
%48 = load double*, double** %8
%49 = call i32 @showPattern(double* %48)
%50 = load double, double* %11
%51 = load i8*, i8** %9
ret double %50
}

LLVM IR (-O2):
@.str = [22 x i8] c"\0A Energy = %9.4f\0A\0A\00"
define double @calcAssociatedPattern(i32 %0, [1000 x double]* %1, double* %2, double* %3) {
%5 = zext i32 %0 to i64
%6 = alloca double, i64 %5
%7 = call i32 @copyPattern(i32 %0, double* %2, double* %6)
%8 = bitcast [1000 x double]* %1 to double**
%9 = call double @calcEnergy(i32 %0, double* %6, double** %8)
br label %10
10: ; preds = %10, %4
%11 = phi double [ %9, %4 ], [ %13, %10 ]
%12 = call i32 @calcOutputPattern(i32 %0, double** %8, double* %6, double* %3)
%13 = call double @calcEnergy(i32 %0, double* %3, double** %8)
%14 = call i32 @copyPattern(i32 %0, double* %3, double* %6)
%15 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), double %13)
%16 = call i32 @equals(double %11, double %13)
%17 = icmp eq i32 %16, 0
br i1 %17, label %10, label %18
18: ; preds = %10
%19 = call i32 @showPattern(double* %3)
ret double %13
}