#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 */
int /*<<< orphan*/ _kahan_summation(double *, int);
double exp(double);
double log(int /*<<< orphan*/);
extern double _logsumexp_kahan_inplace(double *array, int size,
                                       double array_max) {
  int i;
  if (0 == size)
    return -(1.0 / 0.0);
  if (-(1.0 / 0.0) == array_max)
    return -(1.0 / 0.0);
  for (i = 0; i < size; ++i)
    array[i] = exp(array[i] - array_max);
  return array_max + log(_kahan_summation(array, size));
}


x86 assembly (clang, -O0):
<_logsumexp_kahan_inplace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movsd  %xmm0,-0x20(%rbp)
xor    %eax,%eax
cmp    -0x14(%rbp),%eax
jne    1161 <_logsumexp_kahan_inplace+0x31>
movsd  0xea9(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11ff <_logsumexp_kahan_inplace+0xcf>
movsd  0xe97(%rip),%xmm0
ucomisd -0x20(%rbp),%xmm0
jne    118c <_logsumexp_kahan_inplace+0x5c>
jp     118c <_logsumexp_kahan_inplace+0x5c>
movsd  0xe7e(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11ff <_logsumexp_kahan_inplace+0xcf>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11d1 <_logsumexp_kahan_inplace+0xa1>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
subsd  -0x20(%rbp),%xmm0
call   1040 <exp@plt>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1193 <_logsumexp_kahan_inplace+0x63>
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <_kahan_summation@plt>
mov    %eax,%edi
call   1050 <log@plt>
movaps %xmm0,%xmm1
movsd  -0x30(%rbp),%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_logsumexp_kahan_inplace>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
movapd %xmm0,%xmm2
movsd  0xebd(%rip),%xmm0
test   %esi,%esi
je     11a4 <_logsumexp_kahan_inplace+0x74>
movsd  0xeb1(%rip),%xmm1
ucomisd %xmm2,%xmm1
jae    11a4 <_logsumexp_kahan_inplace+0x74>
mov    %esi,%r14d
mov    %rdi,%rbx
test   %esi,%esi
movsd  %xmm2,(%rsp)
jle    118d <_logsumexp_kahan_inplace+0x5d>
mov    %r14d,%r15d
xor    %ebp,%ebp
nopl   0x0(%rax)
movsd  (%rbx,%rbp,8),%xmm0
subsd  (%rsp),%xmm0
call   1040 <exp@plt>
movsd  %xmm0,(%rbx,%rbp,8)
add    $0x1,%rbp
cmp    %rbp,%r15
jne    1170 <_logsumexp_kahan_inplace+0x40>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1030 <_kahan_summation@plt>
mov    %eax,%edi
call   1050 <log@plt>
addsd  (%rsp),%xmm0
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define double @_logsumexp_kahan_inplace(double* %0, i32 %1, double %2) {
%4 = alloca double
%5 = alloca double*
%6 = alloca i32
%7 = alloca double
%8 = alloca i32
store double* %0, double** %5
store i32 %1, i32* %6
store double %2, double* %7
%9 = load i32, i32* %6
%10 = icmp eq i32 0, %9
br i1 %10, label %11, label %12
11: ; preds = %3
store double 0xFFF0000000000000, double* %4
br label %44
12: ; preds = %3
%13 = load double, double* %7
%14 = fcmp oeq double 0xFFF0000000000000, %13
br i1 %14, label %15, label %16
15: ; preds = %12
store double 0xFFF0000000000000, double* %4
br label %44
16: ; preds = %12
store i32 0, i32* %8
br label %17
17: ; preds = %34, %16
%18 = load i32, i32* %8
%19 = load i32, i32* %6
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %37
21: ; preds = %17
%22 = load double*, double** %5
%23 = load i32, i32* %8
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds double, double* %22, i64 %24
%26 = load double, double* %25
%27 = load double, double* %7
%28 = fsub double %26, %27
%29 = call double @exp(double %28)
%30 = load double*, double** %5
%31 = load i32, i32* %8
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds double, double* %30, i64 %32
store double %29, double* %33
br label %34
34: ; preds = %21
%35 = load i32, i32* %8
%36 = add nsw i32 %35, 1
store i32 %36, i32* %8
br label %17
37: ; preds = %17
%38 = load double, double* %7
%39 = load double*, double** %5
%40 = load i32, i32* %6
%41 = call i32 @_kahan_summation(double* %39, i32 %40)
%42 = call double @log(i32 %41)
%43 = fadd double %38, %42
store double %43, double* %4
br label %44
44: ; preds = %37, %15, %11
%45 = load double, double* %4
ret double %45
}

LLVM IR (-O2):
define double @_logsumexp_kahan_inplace(double* %0, i32 %1, double %2) {
%4 = icmp eq i32 %1, 0
%5 = fcmp oeq double %2, 0xFFF0000000000000
%6 = or i1 %4, %5
br i1 %6, label %23, label %7
7: ; preds = %3
%8 = icmp sgt i32 %1, 0
br i1 %8, label %9, label %19
9: ; preds = %7
%10 = zext i32 %1 to i64
br label %11
11: ; preds = %9, %11
%12 = phi i64 [ 0, %9 ], [ %17, %11 ]
%13 = getelementptr inbounds double, double* %0, i64 %12
%14 = load double, double* %13
%15 = fsub double %14, %2
%16 = tail call double @exp(double %15)
store double %16, double* %13
%17 = add nuw nsw i64 %12, 1
%18 = icmp eq i64 %17, %10
br i1 %18, label %19, label %11
19: ; preds = %11, %7
%20 = tail call i32 @_kahan_summation(double* %0, i32 %1)
%21 = tail call double @log(i32 %20)
%22 = fadd double %21, %2
br label %23
23: ; preds = %3, %19
%24 = phi double [ %22, %19 ], [ 0xFFF0000000000000, %3 ]
ret double %24
}