#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_therm_energies(double *biased_conf_energies, int n_therm_states,
                              int n_conf_states, double *scratch_M,
                              double *therm_energies) {
  int i, K;
  for (K = 0; K < n_therm_states; ++K) {
    for (i = 0; i < n_conf_states; ++i)
      scratch_M[i] = -biased_conf_energies[K * n_conf_states + i];
    therm_energies[K] =
        -_logsumexp_sort_kahan_inplace(scratch_M, n_conf_states);
  }
}


x86 assembly (clang, -O0):
<_tram_get_therm_energies>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11d6 <_tram_get_therm_energies+0xc6>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    1198 <_tram_get_therm_energies+0x88>
mov    -0x8(%rbp),%rax
mov    -0x28(%rbp),%ecx
imul   -0x10(%rbp),%ecx
add    -0x24(%rbp),%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1144 <_tram_get_therm_energies+0x34>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1030 <_logsumexp_sort_kahan_inplace@plt>
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
mov    -0x20(%rbp),%rax
movslq -0x28(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1131 <_tram_get_therm_energies+0x21>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_tram_get_therm_energies>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %r8,0x50(%rsp)
test   %esi,%esi
jle    1363 <_tram_get_therm_energies+0x253>
mov    %rcx,%rbx
mov    %edx,%r15d
movslq %edx,%rax
mov    %esi,%ecx
mov    %rcx,0x48(%rsp)
mov    %eax,%ebp
lea    (%rbx,%rbp,8),%rcx
mov    %rcx,0x20(%rsp)
mov    %ebp,%ecx
and    $0xfffffffc,%ecx
mov    %rcx,0x18(%rsp)
add    $0xfffffffffffffffc,%rcx
mov    %rcx,0x10(%rsp)
mov    %rcx,%rdx
shr    $0x2,%rdx
add    $0x1,%rdx
mov    %rdx,%rcx
mov    %rdx,0x8(%rsp)
and    $0xfffffffffffffffe,%rdx
mov    %rdx,(%rsp)
mov    %ebp,%esi
and    $0x3,%esi
lea    0x30(%rdi),%r14
mov    %rax,0x28(%rsp)
lea    0x0(,%rax,8),%rax
mov    %rax,0x38(%rsp)
xor    %r12d,%r12d
movaps 0xe67(%rip),%xmm2
movaps 0xe60(%rip),%xmm3
mov    %rdi,%r13
mov    %rdi,0x40(%rsp)
mov    %rsi,0x30(%rsp)
jmp    11fa <_tram_get_therm_energies+0xea>
nop
mov    %rbx,%rdi
mov    %r15d,%esi
call   1030 <_logsumexp_sort_kahan_inplace@plt>
movaps 0xe3e(%rip),%xmm3
movaps 0xe37(%rip),%xmm2
mov    0x30(%rsp),%rsi
mov    0x40(%rsp),%rdi
xorps  %xmm3,%xmm0
mov    0x50(%rsp),%rax
movlps %xmm0,(%rax,%r12,8)
add    $0x1,%r12
mov    0x38(%rsp),%rax
add    %rax,%r14
add    %rax,%r13
cmp    0x48(%rsp),%r12
je     1363 <_tram_get_therm_energies+0x253>
test   %r15d,%r15d
jle    11b0 <_tram_get_therm_energies+0xa0>
cmp    $0x4,%r15d
jb     122e <_tram_get_therm_energies+0x11e>
mov    %r12,%rax
imul   0x28(%rsp),%rax
lea    (%rax,%rbp,1),%rcx
lea    (%rdi,%rcx,8),%rcx
cmp    %rbx,%rcx
jbe    12c9 <_tram_get_therm_energies+0x1b9>
lea    (%rdi,%rax,8),%rcx
cmp    0x20(%rsp),%rcx
jae    12c9 <_tram_get_therm_energies+0x1b9>
xor    %eax,%eax
mov    %rax,%rcx
not    %rcx
add    %rbp,%rcx
test   %rsi,%rsi
je     1268 <_tram_get_therm_energies+0x158>
mov    %rsi,%rdx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movsd  0x0(%r13,%rax,8),%xmm0
xorps  %xmm3,%xmm0
movlps %xmm0,(%rbx,%rax,8)
add    $0x1,%rax
add    $0xffffffffffffffff,%rdx
jne    1250 <_tram_get_therm_energies+0x140>
cmp    $0x3,%rcx
jb     11b0 <_tram_get_therm_energies+0xa0>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movsd  0x0(%r13,%rax,8),%xmm0
xorps  %xmm2,%xmm0
movlps %xmm0,(%rbx,%rax,8)
movsd  0x8(%r13,%rax,8),%xmm0
xorps  %xmm2,%xmm0
movlps %xmm0,0x8(%rbx,%rax,8)
movsd  0x10(%r13,%rax,8),%xmm0
xorps  %xmm2,%xmm0
movlps %xmm0,0x10(%rbx,%rax,8)
movsd  0x18(%r13,%rax,8),%xmm0
xorps  %xmm2,%xmm0
movlps %xmm0,0x18(%rbx,%rax,8)
add    $0x4,%rax
cmp    %rax,%rbp
jne    1280 <_tram_get_therm_energies+0x170>
jmp    11b0 <_tram_get_therm_energies+0xa0>
cmpq   $0x0,0x10(%rsp)
je     1358 <_tram_get_therm_energies+0x248>
mov    (%rsp),%rdx
xor    %ecx,%ecx
nopl   0x0(%rax,%rax,1)
movups -0x30(%r14,%rcx,8),%xmm0
movups -0x20(%r14,%rcx,8),%xmm1
xorps  %xmm3,%xmm0
xorps  %xmm3,%xmm1
movups %xmm0,(%rbx,%rcx,8)
movups %xmm1,0x10(%rbx,%rcx,8)
movups -0x10(%r14,%rcx,8),%xmm0
movups (%r14,%rcx,8),%xmm1
xorps  %xmm3,%xmm0
xorps  %xmm3,%xmm1
movups %xmm0,0x20(%rbx,%rcx,8)
movups %xmm1,0x30(%rbx,%rcx,8)
add    $0x8,%rcx
add    $0xfffffffffffffffe,%rdx
jne    12e0 <_tram_get_therm_energies+0x1d0>
testb  $0x1,0x8(%rsp)
je     1342 <_tram_get_therm_energies+0x232>
add    %rcx,%rax
movups (%rdi,%rax,8),%xmm0
movups 0x10(%rdi,%rax,8),%xmm1
xorps  %xmm3,%xmm0
xorps  %xmm3,%xmm1
movups %xmm0,(%rbx,%rcx,8)
movups %xmm1,0x10(%rbx,%rcx,8)
mov    0x18(%rsp),%rcx
mov    %rcx,%rax
cmp    %rbp,%rcx
je     11b0 <_tram_get_therm_energies+0xa0>
jmp    1230 <_tram_get_therm_energies+0x120>
xor    %ecx,%ecx
testb  $0x1,0x8(%rsp)
jne    1327 <_tram_get_therm_energies+0x217>
jmp    1342 <_tram_get_therm_energies+0x232>
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @_tram_get_therm_energies(double* %0, i32 %1, i32 %2, double* %3, double* %4) {
%6 = alloca double*
%7 = alloca i32
%8 = alloca i32
%9 = alloca double*
%10 = alloca double*
%11 = alloca i32
%12 = alloca i32
store double* %0, double** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store double* %3, double** %9
store double* %4, double** %10
store i32 0, i32* %12
br label %13
13: ; preds = %49, %5
%14 = load i32, i32* %12
%15 = load i32, i32* %7
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %52
17: ; preds = %13
store i32 0, i32* %11
br label %18
18: ; preds = %37, %17
%19 = load i32, i32* %11
%20 = load i32, i32* %8
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %40
22: ; preds = %18
%23 = load double*, double** %6
%24 = load i32, i32* %12
%25 = load i32, i32* %8
%26 = mul nsw i32 %24, %25
%27 = load i32, i32* %11
%28 = add nsw i32 %26, %27
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds double, double* %23, i64 %29
%31 = load double, double* %30
%32 = fneg double %31
%33 = load double*, double** %9
%34 = load i32, i32* %11
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds double, double* %33, i64 %35
store double %32, double* %36
br label %37
37: ; preds = %22
%38 = load i32, i32* %11
%39 = add nsw i32 %38, 1
store i32 %39, i32* %11
br label %18
40: ; preds = %18
%41 = load double*, double** %9
%42 = load i32, i32* %8
%43 = call double @_logsumexp_sort_kahan_inplace(double* %41, i32 %42)
%44 = fneg double %43
%45 = load double*, double** %10
%46 = load i32, i32* %12
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds double, double* %45, i64 %47
store double %44, double* %48
br label %49
49: ; preds = %40
%50 = load i32, i32* %12
%51 = add nsw i32 %50, 1
store i32 %51, i32* %12
br label %13
52: ; preds = %13
ret void
}

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