#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 int /*<<< orphan*/ FILE;

/* Variables and functions */
double calculate_energy_function(int **, int *);
int /*<<< orphan*/ display_pattern(int *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int, double);
int /*<<< orphan*/ generate_pattern(int **);
int /*<<< orphan*/ set_initial_state(int **, int *);
int /*<<< orphan*/ set_weight_value(int **, int **);
int /*<<< orphan*/ srand(unsigned int);
scalar_t__ time(int /*<<< orphan*/ *);
int /*<<< orphan*/ update_state(int **, int *);
int main() {
  int pattern[3][100];
  int state[100];
  int weight[100][100] = {};
  double energy;
  int update;
  FILE *fp;
  fp = fopen("hopfield.dat", "w");
  generate_pattern(pattern);
  set_weight_value(pattern, weight);
  srand((unsigned int)time(NULL));
  set_initial_state(pattern, state);
  display_pattern(state);
  for (update = 0; update < 1e4; update++) {
    update_state(weight, state);
    energy = calculate_energy_function(weight, state);
    fprintf(fp, "%10d %15.6e\n", update, energy);
  }
  display_pattern(state);
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa2b0,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xa290(%rbp),%rdi
xor    %esi,%esi
mov    $0x9c40,%edx
call   1050 <memset@plt>
lea    0xe1c(%rip),%rdi
lea    0xe22(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0xa2a8(%rbp)
lea    -0x4c0(%rbp),%rdi
call   10c0 <generate_pattern@plt>
lea    -0x4c0(%rbp),%rdi
lea    -0xa290(%rbp),%rsi
call   10e0 <set_weight_value@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10b0 <time@plt>
mov    %eax,%edi
call   1080 <srand@plt>
lea    -0x4c0(%rbp),%rdi
lea    -0x650(%rbp),%rsi
call   10a0 <set_initial_state@plt>
lea    -0x650(%rbp),%rdi
call   1060 <display_pattern@plt>
movl   $0x0,-0xa29c(%rbp)
cvtsi2sdl -0xa29c(%rbp),%xmm1
ff
movsd  0xd99(%rip),%xmm0
ucomisd %xmm1,%xmm0
jbe    12d4 <main+0x114>
lea    -0xa290(%rbp),%rdi
lea    -0x650(%rbp),%rsi
call   1070 <update_state@plt>
lea    -0xa290(%rbp),%rdi
lea    -0x650(%rbp),%rsi
call   1030 <calculate_energy_function@plt>
movsd  %xmm0,-0xa298(%rbp)
ff
mov    -0xa2a8(%rbp),%rdi
mov    -0xa29c(%rbp),%edx
movsd  -0xa298(%rbp),%xmm0
ff
lea    0xd5c(%rip),%rsi
call   1090 <fprintf@plt>
mov    -0xa29c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xa29c(%rbp)
jmp    1257 <main+0x97>
lea    -0x650(%rbp),%rdi
call   1060 <display_pattern@plt>
mov    -0xa2a8(%rbp),%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0xa2b0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xa280,%rsp
lea    0x640(%rsp),%r14
xor    %ebx,%ebx
mov    $0x9c40,%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
lea    0xe10(%rip),%rdi
lea    0xe16(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,%r15
lea    0x190(%rsp),%r13
mov    %r13,%rdi
call   10c0 <generate_pattern@plt>
mov    %r13,%rdi
mov    %r14,%rsi
call   10e0 <set_weight_value@plt>
xor    %edi,%edi
call   10b0 <time@plt>
mov    %eax,%edi
call   1080 <srand@plt>
mov    %rsp,%r12
mov    %r13,%rdi
mov    %r12,%rsi
call   10a0 <set_initial_state@plt>
mov    %r12,%rdi
call   1060 <display_pattern@plt>
lea    0xdca(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14,%rdi
mov    %r12,%rsi
call   1070 <update_state@plt>
mov    %r14,%rdi
mov    %r12,%rsi
call   1030 <calculate_energy_function@plt>
mov    %r15,%rdi
mov    %r13,%rsi
mov    %ebx,%edx
call   1090 <fprintf@plt>
add    $0x1,%ebx
cmp    $0x2710,%ebx
jne    1250 <main+0x90>
mov    %rsp,%rdi
call   1060 <display_pattern@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0xa280,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [13 x i8] c"hopfield.dat\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [13 x i8] c"%10d %15.6e\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [3 x [100 x i32]]
%3 = alloca [100 x i32]
%4 = alloca [100 x [100 x i32]]
%5 = alloca double
%6 = alloca i32
%7 = alloca i32*
store i32 0, i32* %1
%8 = bitcast [100 x [100 x i32]]* %4 to i8*
%9 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %7
%10 = getelementptr inbounds [3 x [100 x i32]], [3 x [100 x i32]]* %2, i64 0, i64 0
%11 = bitcast [100 x i32]* %10 to i32**
%12 = call i32 @generate_pattern(i32** %11)
%13 = getelementptr inbounds [3 x [100 x i32]], [3 x [100 x i32]]* %2, i64 0, i64 0
%14 = bitcast [100 x i32]* %13 to i32**
%15 = getelementptr inbounds [100 x [100 x i32]], [100 x [100 x i32]]* %4, i64 0, i64 0
%16 = bitcast [100 x i32]* %15 to i32**
%17 = call i32 @set_weight_value(i32** %14, i32** %16)
%18 = call i64 @time(i32* null)
%19 = trunc i64 %18 to i32
%20 = call i32 @srand(i32 %19)
%21 = getelementptr inbounds [3 x [100 x i32]], [3 x [100 x i32]]* %2, i64 0, i64 0
%22 = bitcast [100 x i32]* %21 to i32**
%23 = getelementptr inbounds [100 x i32], [100 x i32]* %3, i64 0, i64 0
%24 = call i32 @set_initial_state(i32** %22, i32* %23)
%25 = getelementptr inbounds [100 x i32], [100 x i32]* %3, i64 0, i64 0
%26 = call i32 @display_pattern(i32* %25)
store i32 0, i32* %6
br label %27
27: ; preds = %44, %0
%28 = load i32, i32* %6
%29 = sitofp i32 %28 to double
%30 = fcmp olt double %29, 1.000000e+04
br i1 %30, label %31, label %47
31: ; preds = %27
%32 = getelementptr inbounds [100 x [100 x i32]], [100 x [100 x i32]]* %4, i64 0, i64 0
%33 = bitcast [100 x i32]* %32 to i32**
%34 = getelementptr inbounds [100 x i32], [100 x i32]* %3, i64 0, i64 0
%35 = call i32 @update_state(i32** %33, i32* %34)
%36 = getelementptr inbounds [100 x [100 x i32]], [100 x [100 x i32]]* %4, i64 0, i64 0
%37 = bitcast [100 x i32]* %36 to i32**
%38 = getelementptr inbounds [100 x i32], [100 x i32]* %3, i64 0, i64 0
%39 = call double @calculate_energy_function(i32** %37, i32* %38)
store double %39, double* %5
%40 = load i32*, i32** %7
%41 = load i32, i32* %6
%42 = load double, double* %5
%43 = call i32 @fprintf(i32* %40, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %41, double %42)
br label %44
44: ; preds = %31
%45 = load i32, i32* %6
%46 = add nsw i32 %45, 1
store i32 %46, i32* %6
br label %27
47: ; preds = %27
%48 = getelementptr inbounds [100 x i32], [100 x i32]* %3, i64 0, i64 0
%49 = call i32 @display_pattern(i32* %48)
%50 = load i32*, i32** %7
%51 = call i32 @fclose(i32* %50)
ret i32 0
}

LLVM IR (-O2):
@.str = [13 x i8] c"hopfield.dat\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [13 x i8] c"%10d %15.6e\0A\00"
define i32 @main() {
%1 = alloca [3 x [100 x i32]]
%2 = alloca [100 x i32]
%3 = alloca [100 x [100 x i32]]
%4 = bitcast [3 x [100 x i32]]* %1 to i8*
%5 = bitcast [100 x i32]* %2 to i8*
%6 = bitcast [100 x [100 x i32]]* %3 to i8*
%7 = tail call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = bitcast [3 x [100 x i32]]* %1 to i32**
%9 = call i32 @generate_pattern(i32** %8)
%10 = bitcast [100 x [100 x i32]]* %3 to i32**
%11 = call i32 @set_weight_value(i32** %8, i32** %10)
%12 = call i64 @time(i32* null)
%13 = trunc i64 %12 to i32
%14 = call i32 @srand(i32 %13)
%15 = getelementptr inbounds [100 x i32], [100 x i32]* %2, i64 0, i64 0
%16 = call i32 @set_initial_state(i32** %8, i32* %15)
%17 = call i32 @display_pattern(i32* %15)
br label %18
18: ; preds = %0, %18
%19 = phi i32 [ 0, %0 ], [ %23, %18 ]
%20 = call i32 @update_state(i32** %10, i32* %15)
%21 = call double @calculate_energy_function(i32** %10, i32* %15)
%22 = call i32 @fprintf(i32* %7, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %19, double %21)
%23 = add nuw nsw i32 %19, 1
%24 = icmp eq i32 %23, 10000
br i1 %24, label %25, label %18
25: ; preds = %18
%26 = call i32 @display_pattern(i32* %15)
%27 = call i32 @fclose(i32* %7)
ret i32 0
}