#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 const);
double find_max(double const *, int);
double log(double);
double log_sum(const double *v, int n) {
  double max, sum = 0.0;
  int i;
  max = find_max(v, n);
  for (i = 0; i < n; i++, v++)
    sum += exp(*v - max);
  return log(sum) + max;
}


x86 assembly (clang, -O0):
<log_sum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1050 <find_max@plt>
movsd  %xmm0,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11a6 <log_sum+0x76>
mov    -0x8(%rbp),%rax
movsd  (%rax),%xmm0
subsd  -0x18(%rbp),%xmm0
call   1030 <exp@plt>
cvtsi2sd %rax,%xmm0
addsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x8(%rbp)
jmp    115f <log_sum+0x2f>
movsd  -0x20(%rbp),%xmm0
call   1040 <log@plt>
addsd  -0x18(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<log_sum>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
call   1050 <find_max@plt>
test   %ebp,%ebp
movsd  %xmm0,(%rsp)
jle    1192 <log_sum+0x62>
mov    %ebp,%ebp
xorpd  %xmm1,%xmm1
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsd  %xmm1,0x8(%rsp)
movsd  (%r14,%rbx,8),%xmm0
subsd  (%rsp),%xmm0
call   1030 <exp@plt>
movsd  0x8(%rsp),%xmm1
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
addsd  %xmm0,%xmm1
add    $0x1,%rbx
cmp    %ebx,%ebp
jne    1160 <log_sum+0x30>
jmp    1196 <log_sum+0x66>
xorpd  %xmm1,%xmm1
movapd %xmm1,%xmm0
call   1040 <log@plt>
addsd  (%rsp),%xmm0
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define double @log_sum(double* %0, i32 %1) {
%3 = tail call double @find_max(double* %0, i32 %1)
%4 = icmp sgt i32 %1, 0
br i1 %4, label %5, label %17
5: ; preds = %2, %5
%6 = phi i32 [ %14, %5 ], [ 0, %2 ]
%7 = phi double [ %13, %5 ], [ 0.000000e+00, %2 ]
%8 = phi double* [ %15, %5 ], [ %0, %2 ]
%9 = load double, double* %8
%10 = fsub double %9, %3
%11 = tail call i64 @exp(double %10)
%12 = sitofp i64 %11 to double
%13 = fadd double %7, %12
%14 = add nuw nsw i32 %6, 1
%15 = getelementptr inbounds double, double* %8, i64 1
%16 = icmp eq i32 %14, %1
br i1 %16, label %17, label %5
17: ; preds = %5, %2
%18 = phi double [ 0.000000e+00, %2 ], [ %13, %5 ]
%19 = tail call double @log(double %18)
%20 = fadd double %3, %19
ret double %20
}