#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 _logsumexp_sort_kahan_inplace(double *, int);
extern void _wham_update_conf_energies(
    double *log_therm_state_counts, double *log_conf_state_counts,
    double *therm_energies, double *bias_energies, int n_therm_states,
    int n_conf_states, double *scratch_T, double *conf_energies) {
  int i, K;
  for (i = 0; i < n_conf_states; ++i) {
    for (K = 0; K < n_therm_states; ++K)
      scratch_T[K] = log_therm_state_counts[K] -
                     bias_energies[K * n_conf_states + i] + therm_energies[K];
    conf_energies[i] =
        _logsumexp_sort_kahan_inplace(scratch_T, n_therm_states) -
        log_conf_state_counts[i];
  }
}


x86 assembly (clang, -O0):
<_wham_update_conf_energies>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x18(%rbp),%rax
mov    0x10(%rbp),%rax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    %r9d,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    11dd <_wham_update_conf_energies+0xcd>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    11a9 <_wham_update_conf_energies+0x99>
mov    -0x8(%rbp),%rax
movslq -0x30(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x20(%rbp),%rax
mov    -0x30(%rbp),%ecx
imul   -0x28(%rbp),%ecx
add    -0x2c(%rbp),%ecx
movslq %ecx,%rcx
subsd  (%rax,%rcx,8),%xmm0
mov    -0x18(%rbp),%rax
movslq -0x30(%rbp),%rcx
addsd  (%rax,%rcx,8),%xmm0
mov    0x10(%rbp),%rax
movslq -0x30(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    1152 <_wham_update_conf_energies+0x42>
mov    0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1030 <_logsumexp_sort_kahan_inplace@plt>
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
subsd  (%rax,%rcx,8),%xmm0
mov    0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    113f <_wham_update_conf_energies+0x2f>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_wham_update_conf_energies>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rcx,0x10(%rsp)
mov    %rsi,0x20(%rsp)
test   %r9d,%r9d
jle    1241 <_wham_update_conf_energies+0x131>
mov    %rdx,%rbx
mov    %rdi,%rbp
mov    0x70(%rsp),%r13
mov    %r9d,%r14d
mov    %r8d,%r15d
mov    %r15,0x18(%rsp)
and    $0xfffffffe,%r15d
mov    %r14,%r12
shl    $0x4,%r12
xor    %esi,%esi
mov    0x10(%rsp),%rdi
mov    %r8d,0xc(%rsp)
jmp    11a9 <_wham_update_conf_energies+0x99>
mov    %rdi,0x28(%rsp)
mov    %r13,%rdi
mov    %rsi,0x30(%rsp)
mov    %r8d,%esi
call   1030 <_logsumexp_sort_kahan_inplace@plt>
mov    0x28(%rsp),%rdi
mov    0x30(%rsp),%rsi
mov    0xc(%rsp),%r8d
mov    0x20(%rsp),%rax
subsd  (%rax,%rsi,8),%xmm0
mov    0x78(%rsp),%rax
movsd  %xmm0,(%rax,%rsi,8)
add    $0x1,%rsi
add    $0x8,%rdi
cmp    %r14,%rsi
je     1241 <_wham_update_conf_energies+0x131>
test   %r8d,%r8d
jle    1160 <_wham_update_conf_energies+0x50>
cmp    $0x1,%r8d
jne    11c0 <_wham_update_conf_energies+0xb0>
xor    %eax,%eax
jmp    120b <_wham_update_conf_energies+0xfb>
nopl   0x0(%rax,%rax,1)
mov    %rdi,%rcx
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
nop
movsd  0x0(%rbp,%rax,8),%xmm0
subsd  (%rcx),%xmm0
addsd  (%rbx,%rax,8),%xmm0
movsd  %xmm0,0x0(%r13,%rax,8)
movsd  0x8(%rbp,%rax,8),%xmm0
subsd  (%rcx,%r14,8),%xmm0
addsd  0x8(%rbx,%rax,8),%xmm0
movsd  %xmm0,0x8(%r13,%rax,8)
add    $0x2,%rax
add    %r12,%rcx
cmp    %rax,%r15
jne    11d0 <_wham_update_conf_energies+0xc0>
testb  $0x1,0x18(%rsp)
je     1160 <_wham_update_conf_energies+0x50>
movsd  0x0(%rbp,%rax,8),%xmm0
mov    %rax,%rcx
imul   %r14,%rcx
add    %rsi,%rcx
mov    0x10(%rsp),%rdx
subsd  (%rdx,%rcx,8),%xmm0
addsd  (%rbx,%rax,8),%xmm0
movsd  %xmm0,0x0(%r13,%rax,8)
jmp    1160 <_wham_update_conf_energies+0x50>
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @_wham_update_conf_energies(double* %0, double* %1, double* %2, double* %3, i32 %4, i32 %5, double* %6, double* %7) {
%9 = alloca double*
%10 = alloca double*
%11 = alloca double*
%12 = alloca double*
%13 = alloca i32
%14 = alloca i32
%15 = alloca double*
%16 = alloca double*
%17 = alloca i32
%18 = alloca i32
store double* %0, double** %9
store double* %1, double** %10
store double* %2, double** %11
store double* %3, double** %12
store i32 %4, i32* %13
store i32 %5, i32* %14
store double* %6, double** %15
store double* %7, double** %16
store i32 0, i32* %17
br label %19
19: ; preds = %71, %8
%20 = load i32, i32* %17
%21 = load i32, i32* %14
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %74
23: ; preds = %19
store i32 0, i32* %18
br label %24
24: ; preds = %54, %23
%25 = load i32, i32* %18
%26 = load i32, i32* %13
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %57
28: ; preds = %24
%29 = load double*, double** %9
%30 = load i32, i32* %18
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds double, double* %29, i64 %31
%33 = load double, double* %32
%34 = load double*, double** %12
%35 = load i32, i32* %18
%36 = load i32, i32* %14
%37 = mul nsw i32 %35, %36
%38 = load i32, i32* %17
%39 = add nsw i32 %37, %38
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds double, double* %34, i64 %40
%42 = load double, double* %41
%43 = fsub double %33, %42
%44 = load double*, double** %11
%45 = load i32, i32* %18
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds double, double* %44, i64 %46
%48 = load double, double* %47
%49 = fadd double %43, %48
%50 = load double*, double** %15
%51 = load i32, i32* %18
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds double, double* %50, i64 %52
store double %49, double* %53
br label %54
54: ; preds = %28
%55 = load i32, i32* %18
%56 = add nsw i32 %55, 1
store i32 %56, i32* %18
br label %24
57: ; preds = %24
%58 = load double*, double** %15
%59 = load i32, i32* %13
%60 = call double @_logsumexp_sort_kahan_inplace(double* %58, i32 %59)
%61 = load double*, double** %10
%62 = load i32, i32* %17
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds double, double* %61, i64 %63
%65 = load double, double* %64
%66 = fsub double %60, %65
%67 = load double*, double** %16
%68 = load i32, i32* %17
%69 = sext i32 %68 to i64
%70 = getelementptr inbounds double, double* %67, i64 %69
store double %66, double* %70
br label %71
71: ; preds = %57
%72 = load i32, i32* %17
%73 = add nsw i32 %72, 1
store i32 %73, i32* %17
br label %19
74: ; preds = %19
ret void
}

LLVM IR (-O2):
define void @_wham_update_conf_energies(double* nocapture %0, double* nocapture %1, double* nocapture %2, double* nocapture %3, i32 %4, i32 %5, double* %6, double* nocapture writeonly %7) {
%9 = icmp sgt i32 %5, 0
br i1 %9, label %10, label %73
10: ; preds = %8
%11 = icmp sgt i32 %4, 0
%12 = zext i32 %5 to i64
%13 = zext i32 %5 to i64
%14 = zext i32 %4 to i64
%15 = and i64 %14, 1
%16 = icmp eq i32 %4, 1
%17 = and i64 %14, 4294967294
%18 = icmp eq i64 %15, 0
br label %19
19: ; preds = %10, %65
%20 = phi i64 [ 0, %10 ], [ %71, %65 ]
br i1 %11, label %21, label %65
21: ; preds = %19
br i1 %16, label %51, label %22
22: ; preds = %21, %22
%23 = phi i64 [ %48, %22 ], [ 0, %21 ]
%24 = phi i64 [ %49, %22 ], [ 0, %21 ]
%25 = getelementptr inbounds double, double* %0, i64 %23
%26 = load double, double* %25
%27 = mul nsw i64 %23, %12
%28 = add nuw nsw i64 %27, %20
%29 = getelementptr inbounds double, double* %3, i64 %28
%30 = load double, double* %29
%31 = fsub double %26, %30
%32 = getelementptr inbounds double, double* %2, i64 %23
%33 = load double, double* %32
%34 = fadd double %31, %33
%35 = getelementptr inbounds double, double* %6, i64 %23
store double %34, double* %35
%36 = or i64 %23, 1
%37 = getelementptr inbounds double, double* %0, i64 %36
%38 = load double, double* %37
%39 = mul nsw i64 %36, %12
%40 = add nuw nsw i64 %39, %20
%41 = getelementptr inbounds double, double* %3, i64 %40
%42 = load double, double* %41
%43 = fsub double %38, %42
%44 = getelementptr inbounds double, double* %2, i64 %36
%45 = load double, double* %44
%46 = fadd double %43, %45
%47 = getelementptr inbounds double, double* %6, i64 %36
store double %46, double* %47
%48 = add nuw nsw i64 %23, 2
%49 = add i64 %24, 2
%50 = icmp eq i64 %49, %17
br i1 %50, label %51, label %22
51: ; preds = %22, %21
%52 = phi i64 [ 0, %21 ], [ %48, %22 ]
br i1 %18, label %65, label %53
53: ; preds = %51
%54 = getelementptr inbounds double, double* %0, i64 %52
%55 = load double, double* %54
%56 = mul nsw i64 %52, %12
%57 = add nuw nsw i64 %56, %20
%58 = getelementptr inbounds double, double* %3, i64 %57
%59 = load double, double* %58
%60 = fsub double %55, %59
%61 = getelementptr inbounds double, double* %2, i64 %52
%62 = load double, double* %61
%63 = fadd double %60, %62
%64 = getelementptr inbounds double, double* %6, i64 %52
store double %63, double* %64
br label %65
65: ; preds = %53, %51, %19
%66 = tail call double @_logsumexp_sort_kahan_inplace(double* %6, i32 %4)
%67 = getelementptr inbounds double, double* %1, i64 %20
%68 = load double, double* %67
%69 = fsub double %66, %68
%70 = getelementptr inbounds double, double* %7, i64 %20
store double %69, double* %70
%71 = add nuw nsw i64 %20, 1
%72 = icmp eq i64 %71, %13
br i1 %72, label %73, label %19
73: ; preds = %65, %8
ret void
}