#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 calcula_energia();
double calcula_magnetizacao();
double fabsf(double);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int, double, double);
int /*<<< orphan*/ metropolis(double);
int /*<<< orphan*/ printf(char *, double);
void calcula_termalizacao() {
  int i, j;
  double magnetizacao, energia, temperatura, fator_normalizacao;
  double porcentagem_passos;
  fator_normalizacao = (double)8 * 8 * 8;
  FILE *fp = fopen("termalizacao.dat", "w");
  j = 1.0;
  for (i = 1; i <= 10000; i++) {
    metropolis(4.5);
    energia = fabsf(calcula_energia() / fator_normalizacao);
    magnetizacao = fabsf(calcula_magnetizacao() / fator_normalizacao);
    fprintf(fp, "%d %f %f\n", i, energia, magnetizacao);
    porcentagem_passos = ((i * 1.0) / 10000) * 100.0;
    if (porcentagem_passos >= j) {
      printf("%2.0f%%\n", porcentagem_passos);
      j++;
    }
  }
  fclose(fp);
}


x86 assembly (clang, -O0):
<calcula_termalizacao>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movsd  0xe70(%rip),%xmm0
movsd  %xmm0,-0x28(%rbp)
lea    0xe8c(%rip),%rdi
lea    0xe96(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x38(%rbp)
movl   $0x1,-0x8(%rbp)
movl   $0x1,-0x4(%rbp)
cmpl   $0x2710,-0x4(%rbp)
jg     128c <calcula_termalizacao+0x10c>
movsd  0xe51(%rip),%xmm0
call   1090 <metropolis@plt>
mov    $0x0,%al
call   1060 <calcula_energia@plt>
divsd  -0x28(%rbp),%xmm0
call   1030 <fabsf@plt>
movsd  %xmm0,-0x18(%rbp)
mov    $0x0,%al
call   1040 <calcula_magnetizacao@plt>
divsd  -0x28(%rbp),%xmm0
call   1030 <fabsf@plt>
movsd  %xmm0,-0x10(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x4(%rbp),%edx
movsd  -0x18(%rbp),%xmm0
movsd  -0x10(%rbp),%xmm1
lea    0xe23(%rip),%rsi
call   1080 <fprintf@plt>
cvtsi2sdl -0x4(%rbp),%xmm0
movsd  0xdee(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  0xdda(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  0xdc6(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
cvtsi2sdl -0x8(%rbp),%xmm1
ucomisd %xmm1,%xmm0
jb     1279 <calcula_termalizacao+0xf9>
movsd  -0x30(%rbp),%xmm0
lea    0xdda(%rip),%rdi
call   1070 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    127e <calcula_termalizacao+0xfe>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    11ba <calcula_termalizacao+0x3a>
mov    -0x38(%rbp),%rdi
call   1050 <fclose@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calcula_termalizacao>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
lea    0xebd(%rip),%rdi
lea    0xec7(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%r12
mov    $0x1,%ebp
movsd  0xe61(%rip),%xmm2
lea    0xead(%rip),%r14
lea    0xeb0(%rip),%r15
mov    $0x1,%ebx
jmp    11d7 <calcula_termalizacao+0x67>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
addsd  0xe38(%rip),%xmm2
add    $0x1,%ebp
cmp    $0x2711,%ebp
je     1281 <calcula_termalizacao+0x111>
movsd  %xmm2,0x8(%rsp)
movsd  0xe23(%rip),%xmm0
call   1080 <metropolis@plt>
xor    %eax,%eax
call   1050 <calcula_energia@plt>
movapd %xmm0,%xmm1
movsd  0xe13(%rip),%xmm0
mulsd  %xmm0,%xmm1
movapd 0xe27(%rip),%xmm0
andpd  %xmm0,%xmm1
movapd %xmm1,0x10(%rsp)
xor    %eax,%eax
call   1030 <calcula_magnetizacao@plt>
movapd %xmm0,%xmm1
mulsd  0xdea(%rip),%xmm1
andpd  0xe02(%rip),%xmm1
mov    %r12,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
movaps 0x10(%rsp),%xmm0
call   1070 <fprintf@plt>
movsd  0x8(%rsp),%xmm2
movapd %xmm2,%xmm0
divsd  0xdc6(%rip),%xmm0
mulsd  0xdc6(%rip),%xmm0
xorps  %xmm1,%xmm1
cvtsi2sd %ebx,%xmm1
ucomisd %xmm1,%xmm0
jb     11c0 <calcula_termalizacao+0x50>
mov    %r15,%rdi
call   1060 <printf@plt>
movsd  0x8(%rsp),%xmm2
add    $0x1,%ebx
jmp    11c0 <calcula_termalizacao+0x50>
mov    %r12,%rdi
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <fclose@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"termalizacao.dat\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [10 x i8] c"%d %f %f\0A\00"
@.str.3 = [9 x i8] c"%2.0f%%\0A\00"
define void @calcula_termalizacao() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca double
%4 = alloca double
%5 = alloca double
%6 = alloca double
%7 = alloca double
%8 = alloca i32*
store double 5.120000e+02, double* %6
%9 = call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 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** %8
store i32 1, i32* %2
store i32 1, i32* %1
br label %10
10: ; preds = %43, %0
%11 = load i32, i32* %1
%12 = icmp sle i32 %11, 10000
br i1 %12, label %13, label %46
13: ; preds = %10
%14 = call i32 @metropolis(double 4.500000e+00)
%15 = call double (...) @calcula_energia()
%16 = load double, double* %6
%17 = fdiv double %15, %16
%18 = call double @fabsf(double %17)
store double %18, double* %4
%19 = call double (...) @calcula_magnetizacao()
%20 = load double, double* %6
%21 = fdiv double %19, %20
%22 = call double @fabsf(double %21)
store double %22, double* %3
%23 = load i32*, i32** %8
%24 = load i32, i32* %1
%25 = load double, double* %4
%26 = load double, double* %3
%27 = call i32 @fprintf(i32* %23, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %24, double %25, double %26)
%28 = load i32, i32* %1
%29 = sitofp i32 %28 to double
%30 = fmul double %29, 1.000000e+00
%31 = fdiv double %30, 1.000000e+04
%32 = fmul double %31, 1.000000e+02
store double %32, double* %7
%33 = load double, double* %7
%34 = load i32, i32* %2
%35 = sitofp i32 %34 to double
%36 = fcmp oge double %33, %35
br i1 %36, label %37, label %42
37: ; preds = %13
%38 = load double, double* %7
%39 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), double %38)
%40 = load i32, i32* %2
%41 = add nsw i32 %40, 1
store i32 %41, i32* %2
br label %42
42: ; preds = %37, %13
br label %43
43: ; preds = %42
%44 = load i32, i32* %1
%45 = add nsw i32 %44, 1
store i32 %45, i32* %1
br label %10
46: ; preds = %10
%47 = load i32*, i32** %8
%48 = call i32 @fclose(i32* %47)
ret void
}

LLVM IR (-O2):
@.str = [17 x i8] c"termalizacao.dat\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [10 x i8] c"%d %f %f\0A\00"
@.str.3 = [9 x i8] c"%2.0f%%\0A\00"
define void @calcula_termalizacao() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %2
2: ; preds = %0, %21
%3 = phi i32 [ 1, %0 ], [ %23, %21 ]
%4 = phi i32 [ 1, %0 ], [ %22, %21 ]
%5 = tail call i32 @metropolis(double 4.500000e+00)
%6 = tail call double (...) @calcula_energia()
%7 = fmul double %6, 0x3F60000000000000
%8 = tail call double @llvm.fabs.f64(double %7)
%9 = tail call double (...) @calcula_magnetizacao()
%10 = fmul double %9, 0x3F60000000000000
%11 = tail call double @llvm.fabs.f64(double %10)
%12 = tail call i32 @fprintf(i32* %1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %3, double %8, double %11)
%13 = sitofp i32 %3 to double
%14 = fdiv double %13, 1.000000e+04
%15 = fmul double %14, 1.000000e+02
%16 = sitofp i32 %4 to double
%17 = fcmp ult double %15, %16
br i1 %17, label %21, label %18
18: ; preds = %2
%19 = tail call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), double %15)
%20 = add nsw i32 %4, 1
br label %21
21: ; preds = %2, %18
%22 = phi i32 [ %20, %18 ], [ %4, %2 ]
%23 = add nuw nsw i32 %3, 1
%24 = icmp eq i32 %23, 10001
br i1 %24, label %25, label %2
25: ; preds = %21
%26 = tail call i32 @fclose(i32* %1)
ret void
}