#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 */
scalar_t__ exp(double);
double log(double);
extern double _logsumexp(double *array, int size, double array_max) {
  int i;
  double sum = 0.0;
  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)
    sum += exp(array[i] - array_max);
  return array_max + log(sum);
}


x86 assembly (clang, -O0):
<_logsumexp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movsd  %xmm0,-0x20(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
xor    %eax,%eax
cmp    -0x14(%rbp),%eax
jne    1159 <_logsumexp+0x39>
movsd  0xeb1(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11f0 <_logsumexp+0xd0>
movsd  0xe9f(%rip),%xmm0
ucomisd -0x20(%rbp),%xmm0
jne    1184 <_logsumexp+0x64>
jp     1184 <_logsumexp+0x64>
movsd  0xe86(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11f0 <_logsumexp+0xd0>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11cb <_logsumexp+0xab>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
subsd  -0x20(%rbp),%xmm0
call   1030 <exp@plt>
cvtsi2sd %rax,%xmm0
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    118b <_logsumexp+0x6b>
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x30(%rbp),%xmm0
call   1040 <log@plt>
movaps %xmm0,%xmm1
movsd  -0x38(%rbp),%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_logsumexp>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
movapd %xmm0,%xmm2
movsd  0xecb(%rip),%xmm0
test   %esi,%esi
je     11ab <_logsumexp+0x8b>
movsd  0xebf(%rip),%xmm1
ucomisd %xmm2,%xmm1
jae    11ab <_logsumexp+0x8b>
test   %esi,%esi
movsd  %xmm2,0x8(%rsp)
jle    119c <_logsumexp+0x7c>
mov    %rdi,%r14
mov    %esi,%r15d
xorpd  %xmm0,%xmm0
xor    %ebx,%ebx
nopl   (%rax)
movsd  %xmm0,(%rsp)
movsd  (%r14,%rbx,8),%xmm0
subsd  0x8(%rsp),%xmm0
call   1030 <exp@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
movsd  (%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,(%rsp)
movsd  (%rsp),%xmm0
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1160 <_logsumexp+0x40>
jmp    11a0 <_logsumexp+0x80>
xorpd  %xmm0,%xmm0
call   1040 <log@plt>
addsd  0x8(%rsp),%xmm0
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define double @_logsumexp(double* nocapture %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 %26, label %7
7: ; preds = %3
%8 = icmp sgt i32 %1, 0
br i1 %8, label %9, label %22
9: ; preds = %7
%10 = zext i32 %1 to i64
br label %11
11: ; preds = %9, %11
%12 = phi i64 [ 0, %9 ], [ %20, %11 ]
%13 = phi double [ 0.000000e+00, %9 ], [ %19, %11 ]
%14 = getelementptr inbounds double, double* %0, i64 %12
%15 = load double, double* %14
%16 = fsub double %15, %2
%17 = tail call i64 @exp(double %16)
%18 = sitofp i64 %17 to double
%19 = fadd double %13, %18
%20 = add nuw nsw i64 %12, 1
%21 = icmp eq i64 %20, %10
br i1 %21, label %22, label %11
22: ; preds = %11, %7
%23 = phi double [ 0.000000e+00, %7 ], [ %19, %11 ]
%24 = tail call double @log(double %23)
%25 = fadd double %24, %2
br label %26
26: ; preds = %3, %22
%27 = phi double [ %25, %22 ], [ 0xFFF0000000000000, %3 ]
ret double %27
}