#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 _tram_get_pointwise_unbiased_free_energies(
    int k, double *bias_energy_sequence, double *therm_energies,
    int *state_sequence, int seq_length, double *log_R_K_i, int n_therm_states,
    int n_conf_states, double *scratch_T,
    double *pointwise_unbiased_free_energies) {
  int L, o, i, x;
  double log_divisor;
  for (x = 0; x < seq_length; ++x) {
    i = state_sequence[x];
    if (i < 0) {
      pointwise_unbiased_free_energies[x] = (1.0 / 0.0);
      continue;
    }
    o = 0;
    for (L = 0; L < n_therm_states; ++L) {
      if (-(1.0 / 0.0) == log_R_K_i[L * n_conf_states + i])
        continue;
      scratch_T[o++] = log_R_K_i[L * n_conf_states + i] -
                       bias_energy_sequence[x * n_therm_states + L];
    }
    log_divisor = _logsumexp_sort_kahan_inplace(scratch_T, o);
    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):
<_tram_get_pointwise_unbiased_free_energies>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    0x28(%rbp),%rax
mov    0x20(%rbp),%rax
mov    0x18(%rbp),%eax
mov    0x10(%rbp),%eax
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    %r9,-0x30(%rbp)
movl   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    1296 <_tram_get_pointwise_unbiased_free_energies+0x186>
mov    -0x20(%rbp),%rax
movslq -0x40(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    1182 <_tram_get_pointwise_unbiased_free_energies+0x72>
mov    0x28(%rbp),%rax
movslq -0x40(%rbp),%rcx
movsd  0xe90(%rip),%xmm0
movsd  %xmm0,(%rax,%rcx,8)
jmp    1288 <_tram_get_pointwise_unbiased_free_energies+0x178>
movl   $0x0,-0x38(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    0x10(%rbp),%eax
jge    121c <_tram_get_pointwise_unbiased_free_energies+0x10c>
mov    -0x30(%rbp),%rax
mov    -0x34(%rbp),%ecx
imul   0x18(%rbp),%ecx
add    -0x3c(%rbp),%ecx
movslq %ecx,%rcx
movsd  0xe4b(%rip),%xmm0
ucomisd (%rax,%rcx,8),%xmm0
jne    11cb <_tram_get_pointwise_unbiased_free_energies+0xbb>
jp     11cb <_tram_get_pointwise_unbiased_free_energies+0xbb>
jmp    120e <_tram_get_pointwise_unbiased_free_energies+0xfe>
mov    -0x30(%rbp),%rax
mov    -0x34(%rbp),%ecx
imul   0x18(%rbp),%ecx
add    -0x3c(%rbp),%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x10(%rbp),%rax
mov    -0x40(%rbp),%ecx
imul   0x10(%rbp),%ecx
add    -0x34(%rbp),%ecx
movslq %ecx,%rcx
subsd  (%rax,%rcx,8),%xmm0
mov    0x20(%rbp),%rax
mov    -0x38(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x38(%rbp)
movslq %ecx,%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1190 <_tram_get_pointwise_unbiased_free_energies+0x80>
mov    0x20(%rbp),%rdi
mov    -0x38(%rbp),%esi
call   1030 <_logsumexp_sort_kahan_inplace@plt>
movsd  %xmm0,-0x48(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    124e <_tram_get_pointwise_unbiased_free_energies+0x13e>
movsd  -0x48(%rbp),%xmm0
mov    0x28(%rbp),%rax
movslq -0x40(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
jmp    1283 <_tram_get_pointwise_unbiased_free_energies+0x173>
mov    -0x10(%rbp),%rax
mov    -0x40(%rbp),%ecx
imul   0x10(%rbp),%ecx
add    -0x4(%rbp),%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
addsd  -0x48(%rbp),%xmm0
mov    -0x18(%rbp),%rax
movslq -0x4(%rbp),%rcx
subsd  (%rax,%rcx,8),%xmm0
mov    0x28(%rbp),%rax
movslq -0x40(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
jmp    1288 <_tram_get_pointwise_unbiased_free_energies+0x178>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    1144 <_tram_get_pointwise_unbiased_free_energies+0x34>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_tram_get_pointwise_unbiased_free_energies>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %r9,0x30(%rsp)
mov    %rcx,0x40(%rsp)
mov    %rdx,0x28(%rsp)
mov    %rsi,0x10(%rsp)
mov    %edi,0xc(%rsp)
test   %r8d,%r8d
jle    125f <_tram_get_pointwise_unbiased_free_energies+0x14f>
mov    0x90(%rsp),%rbp
movslq 0xc(%rsp),%rax
mov    %rax,0x20(%rsp)
movslq 0x88(%rsp),%r12
movslq 0x80(%rsp),%rax
mov    %r8d,%ecx
mov    %rcx,0x38(%rsp)
mov    %eax,%r13d
mov    %rax,0x18(%rsp)
lea    0x0(,%rax,8),%r15
shl    $0x3,%r12
xor    %ebx,%ebx
movsd  0xe81(%rip),%xmm1
mov    0x10(%rsp),%r14
jmp    11b7 <_tram_get_pointwise_unbiased_free_energies+0xa7>
xchg   %ax,%ax
movsd  0xe70(%rip),%xmm1
mov    0x98(%rsp),%rax
movsd  %xmm0,(%rax,%rbx,8)
add    $0x1,%rbx
add    %r15,%r14
cmp    0x38(%rsp),%rbx
je     125f <_tram_get_pointwise_unbiased_free_energies+0x14f>
mov    0x40(%rsp),%rax
mov    (%rax,%rbx,4),%eax
movsd  0xe39(%rip),%xmm0
test   %eax,%eax
js     1198 <_tram_get_pointwise_unbiased_free_energies+0x88>
cmpl   $0x0,0x80(%rsp)
jle    1220 <_tram_get_pointwise_unbiased_free_energies+0x110>
mov    0x30(%rsp),%rcx
lea    (%rcx,%rax,8),%rax
xor    %ecx,%ecx
xor    %esi,%esi
jmp    11fc <_tram_get_pointwise_unbiased_free_energies+0xec>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x1,%rcx
add    %r12,%rax
cmp    %rcx,%r13
je     1222 <_tram_get_pointwise_unbiased_free_energies+0x112>
movsd  (%rax),%xmm0
ucomisd %xmm0,%xmm1
jae    11f0 <_tram_get_pointwise_unbiased_free_energies+0xe0>
subsd  (%r14,%rcx,8),%xmm0
movslq %esi,%rdx
add    $0x1,%esi
movsd  %xmm0,0x0(%rbp,%rdx,8)
jmp    11f0 <_tram_get_pointwise_unbiased_free_energies+0xe0>
nopw   0x0(%rax,%rax,1)
xor    %esi,%esi
mov    %rbp,%rdi
call   1030 <_logsumexp_sort_kahan_inplace@plt>
cmpl   $0xffffffff,0xc(%rsp)
je     1190 <_tram_get_pointwise_unbiased_free_energies+0x80>
mov    %rbx,%rax
imul   0x18(%rsp),%rax
mov    0x20(%rsp),%rdx
add    %rdx,%rax
mov    0x10(%rsp),%rcx
addsd  (%rcx,%rax,8),%xmm0
mov    0x28(%rsp),%rax
subsd  (%rax,%rdx,8),%xmm0
jmp    1190 <_tram_get_pointwise_unbiased_free_energies+0x80>
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @_tram_get_pointwise_unbiased_free_energies(i32 %0, double* %1, double* %2, i32* %3, i32 %4, double* %5, i32 %6, i32 %7, double* %8, double* %9) {
%11 = alloca i32
%12 = alloca double*
%13 = alloca double*
%14 = alloca i32*
%15 = alloca i32
%16 = alloca double*
%17 = alloca i32
%18 = alloca i32
%19 = alloca double*
%20 = alloca double*
%21 = alloca i32
%22 = alloca i32
%23 = alloca i32
%24 = alloca i32
%25 = alloca double
store i32 %0, i32* %11
store double* %1, double** %12
store double* %2, double** %13
store i32* %3, i32** %14
store i32 %4, i32* %15
store double* %5, double** %16
store i32 %6, i32* %17
store i32 %7, i32* %18
store double* %8, double** %19
store double* %9, double** %20
store i32 0, i32* %24
br label %26
26: ; preds = %123, %10
%27 = load i32, i32* %24
%28 = load i32, i32* %15
%29 = icmp slt i32 %27, %28
br i1 %29, label %30, label %126
30: ; preds = %26
%31 = load i32*, i32** %14
%32 = load i32, i32* %24
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i32, i32* %31, i64 %33
%35 = load i32, i32* %34
store i32 %35, i32* %23
%36 = load i32, i32* %23
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %43
38: ; preds = %30
%39 = load double*, double** %20
%40 = load i32, i32* %24
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds double, double* %39, i64 %41
store double 0x7FF0000000000000, double* %42
br label %123
43: ; preds = %30
store i32 0, i32* %22
store i32 0, i32* %21
br label %44
44: ; preds = %85, %43
%45 = load i32, i32* %21
%46 = load i32, i32* %17
%47 = icmp slt i32 %45, %46
br i1 %47, label %48, label %88
48: ; preds = %44
%49 = load double*, double** %16
%50 = load i32, i32* %21
%51 = load i32, i32* %18
%52 = mul nsw i32 %50, %51
%53 = load i32, i32* %23
%54 = add nsw i32 %52, %53
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds double, double* %49, i64 %55
%57 = load double, double* %56
%58 = fcmp oeq double 0xFFF0000000000000, %57
br i1 %58, label %59, label %60
59: ; preds = %48
br label %85
60: ; preds = %48
%61 = load double*, double** %16
%62 = load i32, i32* %21
%63 = load i32, i32* %18
%64 = mul nsw i32 %62, %63
%65 = load i32, i32* %23
%66 = add nsw i32 %64, %65
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds double, double* %61, i64 %67
%69 = load double, double* %68
%70 = load double*, double** %12
%71 = load i32, i32* %24
%72 = load i32, i32* %17
%73 = mul nsw i32 %71, %72
%74 = load i32, i32* %21
%75 = add nsw i32 %73, %74
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds double, double* %70, i64 %76
%78 = load double, double* %77
%79 = fsub double %69, %78
%80 = load double*, double** %19
%81 = load i32, i32* %22
%82 = add nsw i32 %81, 1
store i32 %82, i32* %22
%83 = sext i32 %81 to i64
%84 = getelementptr inbounds double, double* %80, i64 %83
store double %79, double* %84
br label %85
85: ; preds = %60, %59
%86 = load i32, i32* %21
%87 = add nsw i32 %86, 1
store i32 %87, i32* %21
br label %44
88: ; preds = %44
%89 = load double*, double** %19
%90 = load i32, i32* %22
%91 = call double @_logsumexp_sort_kahan_inplace(double* %89, i32 %90)
store double %91, double* %25
%92 = load i32, i32* %11
%93 = icmp eq i32 %92, -1
br i1 %93, label %94, label %100
94: ; preds = %88
%95 = load double, double* %25
%96 = load double*, double** %20
%97 = load i32, i32* %24
%98 = sext i32 %97 to i64
%99 = getelementptr inbounds double, double* %96, i64 %98
store double %95, double* %99
br label %122
100: ; preds = %88
%101 = load double*, double** %12
%102 = load i32, i32* %24
%103 = load i32, i32* %17
%104 = mul nsw i32 %102, %103
%105 = load i32, i32* %11
%106 = add nsw i32 %104, %105
%107 = sext i32 %106 to i64
%108 = getelementptr inbounds double, double* %101, i64 %107
%109 = load double, double* %108
%110 = load double, double* %25
%111 = fadd double %109, %110
%112 = load double*, double** %13
%113 = load i32, i32* %11
%114 = sext i32 %113 to i64
%115 = getelementptr inbounds double, double* %112, i64 %114
%116 = load double, double* %115
%117 = fsub double %111, %116
%118 = load double*, double** %20
%119 = load i32, i32* %24
%120 = sext i32 %119 to i64
%121 = getelementptr inbounds double, double* %118, i64 %120
store double %117, double* %121
br label %122
122: ; preds = %100, %94
br label %123
123: ; preds = %122, %38
%124 = load i32, i32* %24
%125 = add nsw i32 %124, 1
store i32 %125, i32* %24
br label %26
126: ; preds = %26
ret void
}

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