#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);
void _mbar_get_pointwise_unbiased_free_energies(
    int k, double *log_therm_state_counts, double *therm_energies,
    double *bias_energy_sequence, int n_therm_states, int seq_length,
    double *scratch_T, double *pointwise_unbiased_free_energies) {
  int L, x;
  double log_divisor;
  for (x = 0; x < seq_length; ++x) {
    for (L = 0; L < n_therm_states; ++L)
      scratch_T[L] = log_therm_state_counts[L] + therm_energies[L] -
                     bias_energy_sequence[x * n_therm_states + L];
    log_divisor = _logsumexp_sort_kahan_inplace(scratch_T, n_therm_states);
    if (k == -1)
      pointwise_unbiased_free_energies[x] = log_divisor;
    else
      pointwise_unbiased_free_energies[x] =
          bias_energy_sequence[x * n_therm_states + k] + log_divisor -
          therm_energies[k];
  }
}


x86 assembly (clang, -O0):
<_mbar_get_pointwise_unbiased_free_energies>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x18(%rbp),%rax
mov    0x10(%rbp),%rax
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    %r9d,-0x28(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    1222 <_mbar_get_pointwise_unbiased_free_energies+0x112>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    11a8 <_mbar_get_pointwise_unbiased_free_energies+0x98>
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
addsd  (%rax,%rcx,8),%xmm0
mov    -0x20(%rbp),%rax
mov    -0x30(%rbp),%ecx
imul   -0x24(%rbp),%ecx
add    -0x2c(%rbp),%ecx
movslq %ecx,%rcx
subsd  (%rax,%rcx,8),%xmm0
mov    0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1151 <_mbar_get_pointwise_unbiased_free_energies+0x41>
mov    0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1030 <_logsumexp_sort_kahan_inplace@plt>
movsd  %xmm0,-0x38(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    11da <_mbar_get_pointwise_unbiased_free_energies+0xca>
movsd  -0x38(%rbp),%xmm0
mov    0x18(%rbp),%rax
movslq -0x30(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
jmp    120f <_mbar_get_pointwise_unbiased_free_energies+0xff>
mov    -0x20(%rbp),%rax
mov    -0x30(%rbp),%ecx
imul   -0x24(%rbp),%ecx
add    -0x4(%rbp),%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
addsd  -0x38(%rbp),%xmm0
mov    -0x18(%rbp),%rax
movslq -0x4(%rbp),%rcx
subsd  (%rax,%rcx,8),%xmm0
mov    0x18(%rbp),%rax
movslq -0x30(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
jmp    1214 <_mbar_get_pointwise_unbiased_free_energies+0x104>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    113e <_mbar_get_pointwise_unbiased_free_energies+0x2e>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_mbar_get_pointwise_unbiased_free_energies>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
mov    %r8d,0x8(%rsp)
mov    %rcx,0x10(%rsp)
mov    %edi,0xc(%rsp)
test   %r9d,%r9d
jle    138d <_mbar_get_pointwise_unbiased_free_energies+0x27d>
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    0xa0(%rsp),%r13
movslq 0xc(%rsp),%rax
mov    %rax,0x40(%rsp)
movslq 0x8(%rsp),%rsi
mov    %r9d,%eax
mov    %rax,0x50(%rsp)
mov    %esi,%edi
lea    0x0(,%rdi,8),%rcx
add    %r13,%rcx
lea    0x0(,%rdi,8),%rax
add    %rbp,%rax
lea    (%rdx,%rdi,8),%r8
cmp    %r13,%rax
seta   %al
cmp    %rbp,%rcx
seta   %dl
and    %al,%dl
cmp    %r13,%r8
seta   %al
mov    %rcx,0x30(%rsp)
cmp    %rbx,%rcx
seta   %cl
and    %al,%cl
or     %dl,%cl
mov    %cl,0x7(%rsp)
mov    %edi,%eax
and    $0xfffffffc,%eax
mov    %rax,0x28(%rsp)
mov    %rdi,%rax
neg    %rax
mov    %rax,0x38(%rsp)
mov    0x10(%rsp),%rax
lea    0x10(%rax),%r15
mov    %rsi,0x58(%rsp)
lea    0x0(,%rsi,8),%rcx
mov    %rcx,0x48(%rsp)
lea    0x0(,%rdi,8),%rsi
and    $0xffffffffffffffe0,%rsi
lea    0x8(%rax),%r12
xor    %r14d,%r14d
mov    %rdi,0x20(%rsp)
mov    %rsi,0x18(%rsp)
jmp    1222 <_mbar_get_pointwise_unbiased_free_energies+0x112>
nopl   0x0(%rax)
mov    0x20(%rsp),%rdi
mov    0x18(%rsp),%rsi
mov    0xa8(%rsp),%rax
movsd  %xmm0,(%rax,%r14,8)
add    $0x1,%r14
mov    0x48(%rsp),%rax
add    %rax,%r15
add    %rax,%r12
cmp    0x50(%rsp),%r14
je     138d <_mbar_get_pointwise_unbiased_free_energies+0x27d>
mov    %r14,%r8
mov    0x58(%rsp),%rax
imul   %rax,%r8
test   %eax,%eax
jle    1350 <_mbar_get_pointwise_unbiased_free_energies+0x240>
cmpl   $0x4,0x8(%rsp)
jb     1268 <_mbar_get_pointwise_unbiased_free_energies+0x158>
lea    (%r8,%rdi,1),%rax
mov    0x10(%rsp),%rdx
lea    (%rdx,%r8,8),%rcx
lea    (%rdx,%rax,8),%rax
cmp    %r13,%rax
seta   %al
cmp    0x30(%rsp),%rcx
setb   %cl
and    %al,%cl
or     0x7(%rsp),%cl
je     12ed <_mbar_get_pointwise_unbiased_free_energies+0x1dd>
xor    %ecx,%ecx
mov    %rcx,%rax
test   $0x1,%dil
je     129a <_mbar_get_pointwise_unbiased_free_energies+0x18a>
movsd  0x0(%rbp,%rcx,8),%xmm0
addsd  (%rbx,%rcx,8),%xmm0
lea    (%rcx,%r8,1),%rax
mov    0x10(%rsp),%rdx
subsd  (%rdx,%rax,8),%xmm0
movsd  %xmm0,0x0(%r13,%rcx,8)
mov    %rcx,%rax
or     $0x1,%rax
not    %rcx
cmp    0x38(%rsp),%rcx
je     1350 <_mbar_get_pointwise_unbiased_free_energies+0x240>
nopl   0x0(%rax,%rax,1)
movsd  0x0(%rbp,%rax,8),%xmm0
addsd  (%rbx,%rax,8),%xmm0
subsd  -0x8(%r12,%rax,8),%xmm0
movsd  %xmm0,0x0(%r13,%rax,8)
movsd  0x8(%rbp,%rax,8),%xmm0
addsd  0x8(%rbx,%rax,8),%xmm0
subsd  (%r12,%rax,8),%xmm0
movsd  %xmm0,0x8(%r13,%rax,8)
add    $0x2,%rax
cmp    %rax,%rdi
jne    12b0 <_mbar_get_pointwise_unbiased_free_energies+0x1a0>
jmp    1350 <_mbar_get_pointwise_unbiased_free_energies+0x240>
xor    %eax,%eax
nop
movupd 0x0(%rbp,%rax,1),%xmm0
movupd 0x10(%rbp,%rax,1),%xmm1
movupd (%rbx,%rax,1),%xmm2
addpd  %xmm0,%xmm2
movupd 0x10(%rbx,%rax,1),%xmm0
addpd  %xmm1,%xmm0
movupd -0x10(%r15,%rax,1),%xmm1
subpd  %xmm1,%xmm2
movupd (%r15,%rax,1),%xmm1
subpd  %xmm1,%xmm0
movupd %xmm2,0x0(%r13,%rax,1)
movupd %xmm0,0x10(%r13,%rax,1)
add    $0x20,%rax
cmp    %rax,%rsi
jne    12f0 <_mbar_get_pointwise_unbiased_free_energies+0x1e0>
mov    0x28(%rsp),%rax
mov    %rax,%rcx
cmp    %rdi,%rax
jne    126a <_mbar_get_pointwise_unbiased_free_energies+0x15a>
nopl   0x0(%rax)
mov    %r8,0x60(%rsp)
mov    %r13,%rdi
mov    0x8(%rsp),%esi
call   1030 <_logsumexp_sort_kahan_inplace@plt>
cmpl   $0xffffffff,0xc(%rsp)
je     11f0 <_mbar_get_pointwise_unbiased_free_energies+0xe0>
mov    0x40(%rsp),%rcx
mov    0x60(%rsp),%rdx
add    %rcx,%rdx
mov    0x10(%rsp),%rax
addsd  (%rax,%rdx,8),%xmm0
subsd  (%rbx,%rcx,8),%xmm0
jmp    11f0 <_mbar_get_pointwise_unbiased_free_energies+0xe0>
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define void @_mbar_get_pointwise_unbiased_free_energies(i32 %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 %137
10: ; preds = %8
%11 = icmp sgt i32 %4, 0
%12 = icmp eq i32 %0, -1
%13 = sext i32 %0 to i64
%14 = getelementptr inbounds double, double* %2, i64 %13
%15 = sext i32 %4 to i64
%16 = zext i32 %5 to i64
%17 = zext i32 %4 to i64
%18 = getelementptr double, double* %6, i64 %17
%19 = getelementptr double, double* %1, i64 %17
%20 = getelementptr double, double* %2, i64 %17
%21 = icmp ult i32 %4, 4
%22 = icmp ugt double* %19, %6
%23 = icmp ugt double* %18, %1
%24 = and i1 %22, %23
%25 = icmp ugt double* %20, %6
%26 = icmp ugt double* %18, %2
%27 = and i1 %25, %26
%28 = or i1 %24, %27
%29 = and i64 %17, 4294967292
%30 = icmp eq i64 %29, %17
%31 = and i64 %17, 1
%32 = icmp eq i64 %31, 0
%33 = sub nsw i64 0, %17
br label %34
34: ; preds = %10, %132
%35 = phi i64 [ 0, %10 ], [ %135, %132 ]
%36 = mul i64 %35, %15
%37 = getelementptr double, double* %3, i64 %36
%38 = add i64 %36, %17
%39 = getelementptr double, double* %3, i64 %38
br i1 %11, label %40, label %122
40: ; preds = %34
%41 = mul nsw i64 %35, %15
br i1 %21, label %79, label %42
42: ; preds = %40
%43 = icmp ugt double* %39, %6
%44 = icmp ult double* %37, %18
%45 = and i1 %43, %44
%46 = or i1 %28, %45
br i1 %46, label %79, label %47
47: ; preds = %42, %47
%48 = phi i64 [ %76, %47 ], [ 0, %42 ]
%49 = getelementptr inbounds double, double* %1, i64 %48
%50 = bitcast double* %49 to <2 x double>*
%51 = load <2 x double>, <2 x double>* %50
%52 = getelementptr inbounds double, double* %49, i64 2
%53 = bitcast double* %52 to <2 x double>*
%54 = load <2 x double>, <2 x double>* %53
%55 = getelementptr inbounds double, double* %2, i64 %48
%56 = bitcast double* %55 to <2 x double>*
%57 = load <2 x double>, <2 x double>* %56
%58 = getelementptr inbounds double, double* %55, i64 2
%59 = bitcast double* %58 to <2 x double>*
%60 = load <2 x double>, <2 x double>* %59
%61 = fadd <2 x double> %51, %57
%62 = fadd <2 x double> %54, %60
%63 = add nsw i64 %48, %41
%64 = getelementptr inbounds double, double* %3, i64 %63
%65 = bitcast double* %64 to <2 x double>*
%66 = load <2 x double>, <2 x double>* %65
%67 = getelementptr inbounds double, double* %64, i64 2
%68 = bitcast double* %67 to <2 x double>*
%69 = load <2 x double>, <2 x double>* %68
%70 = fsub <2 x double> %61, %66
%71 = fsub <2 x double> %62, %69
%72 = getelementptr inbounds double, double* %6, i64 %48
%73 = bitcast double* %72 to <2 x double>*
store <2 x double> %70, <2 x double>* %73
%74 = getelementptr inbounds double, double* %72, i64 2
%75 = bitcast double* %74 to <2 x double>*
store <2 x double> %71, <2 x double>* %75
%76 = add nuw i64 %48, 4
%77 = icmp eq i64 %76, %29
br i1 %77, label %78, label %47
78: ; preds = %47
br i1 %30, label %122, label %79
79: ; preds = %42, %40, %78
%80 = phi i64 [ 0, %42 ], [ 0, %40 ], [ %29, %78 ]
%81 = xor i64 %80, -1
br i1 %32, label %94, label %82
82: ; preds = %79
%83 = getelementptr inbounds double, double* %1, i64 %80
%84 = load double, double* %83
%85 = getelementptr inbounds double, double* %2, i64 %80
%86 = load double, double* %85
%87 = fadd double %84, %86
%88 = add nsw i64 %80, %41
%89 = getelementptr inbounds double, double* %3, i64 %88
%90 = load double, double* %89
%91 = fsub double %87, %90
%92 = getelementptr inbounds double, double* %6, i64 %80
store double %91, double* %92
%93 = or i64 %80, 1
br label %94
94: ; preds = %82, %79
%95 = phi i64 [ %80, %79 ], [ %93, %82 ]
%96 = icmp eq i64 %81, %33
br i1 %96, label %122, label %97
97: ; preds = %94, %97
%98 = phi i64 [ %120, %97 ], [ %95, %94 ]
%99 = getelementptr inbounds double, double* %1, i64 %98
%100 = load double, double* %99
%101 = getelementptr inbounds double, double* %2, i64 %98
%102 = load double, double* %101
%103 = fadd double %100, %102
%104 = add nsw i64 %98, %41
%105 = getelementptr inbounds double, double* %3, i64 %104
%106 = load double, double* %105
%107 = fsub double %103, %106
%108 = getelementptr inbounds double, double* %6, i64 %98
store double %107, double* %108
%109 = add nuw nsw i64 %98, 1
%110 = getelementptr inbounds double, double* %1, i64 %109
%111 = load double, double* %110
%112 = getelementptr inbounds double, double* %2, i64 %109
%113 = load double, double* %112
%114 = fadd double %111, %113
%115 = add nsw i64 %109, %41
%116 = getelementptr inbounds double, double* %3, i64 %115
%117 = load double, double* %116
%118 = fsub double %114, %117
%119 = getelementptr inbounds double, double* %6, i64 %109
store double %118, double* %119
%120 = add nuw nsw i64 %98, 2
%121 = icmp eq i64 %120, %17
br i1 %121, label %122, label %97
122: ; preds = %94, %97, %78, %34
%123 = tail call double @_logsumexp_sort_kahan_inplace(double* %6, i32 %4)
br i1 %12, label %132, label %124
124: ; preds = %122
%125 = mul nsw i64 %35, %15
%126 = add nsw i64 %125, %13
%127 = getelementptr inbounds double, double* %3, i64 %126
%128 = load double, double* %127
%129 = fadd double %123, %128
%130 = load double, double* %14
%131 = fsub double %129, %130
br label %132
132: ; preds = %122, %124
%133 = phi double [ %131, %124 ], [ %123, %122 ]
%134 = getelementptr inbounds double, double* %7, i64 %35
store double %133, double* %134
%135 = add nuw nsw i64 %35, 1
%136 = icmp eq i64 %135, %16
br i1 %136, label %137, label %34
137: ; preds = %132, %8
ret void
}