#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);
double ighmm_cvector_log_sum(double *a, int length) {
  int i;
  double max = 1.0;
  int argmax = 0;
  double result;
  for (i = 0; i < length; i++)
    if (max == 1.0 || (a[i] > max && a[i] != 1.0)) {
      max = a[i];
      argmax = i;
    }
  result = 1.0;
  for (i = 0; i < length; i++) {
    if (a[i] != 1.0 && i != argmax)
      result += exp(a[i] - max);
  }
  result = log(result);
  result += max;
  return result;
}


x86 assembly (clang, -O0):
<ighmm_cvector_log_sum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movsd  0xec9(%rip),%xmm0
movsd  %xmm0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11e5 <ighmm_cvector_log_sum+0xc5>
movsd  -0x18(%rbp),%xmm0
movsd  0xe9d(%rip),%xmm1
ucomisd %xmm1,%xmm0
jne    1178 <ighmm_cvector_log_sum+0x58>
jp     1178 <ighmm_cvector_log_sum+0x58>
jmp    11ba <ighmm_cvector_log_sum+0x9a>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
ucomisd -0x18(%rbp),%xmm0
jbe    11d2 <ighmm_cvector_log_sum+0xb2>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  0xe5b(%rip),%xmm1
ucomisd %xmm1,%xmm0
jne    11ba <ighmm_cvector_log_sum+0x9a>
jp     11ba <ighmm_cvector_log_sum+0x9a>
jmp    11d2 <ighmm_cvector_log_sum+0xb2>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    11d7 <ighmm_cvector_log_sum+0xb7>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    114a <ighmm_cvector_log_sum+0x2a>
movsd  0xe13(%rip),%xmm0
movsd  %xmm0,-0x28(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1274 <ighmm_cvector_log_sum+0x154>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  0xde6(%rip),%xmm1
ucomisd %xmm1,%xmm0
jne    122f <ighmm_cvector_log_sum+0x10f>
jp     122f <ighmm_cvector_log_sum+0x10f>
jmp    1261 <ighmm_cvector_log_sum+0x141>
mov    -0x10(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     1261 <ighmm_cvector_log_sum+0x141>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
subsd  -0x18(%rbp),%xmm0
call   1030 <exp@plt>
cvtsi2sd %rax,%xmm0
addsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
jmp    1266 <ighmm_cvector_log_sum+0x146>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11f9 <ighmm_cvector_log_sum+0xd9>
movsd  -0x28(%rbp),%xmm0
call   1040 <log@plt>
movsd  %xmm0,-0x28(%rbp)
movsd  -0x18(%rbp),%xmm0
addsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ighmm_cvector_log_sum>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %esi,%esi
jle    11f1 <ighmm_cvector_log_sum+0xd1>
mov    %rdi,%r12
mov    %esi,%r14d
cmp    $0x1,%esi
jne    1201 <ighmm_cvector_log_sum+0xe1>
movsd  0xeb6(%rip),%xmm2
xor    %eax,%eax
xor    %ecx,%ecx
test   $0x1,%r14b
je     117e <ighmm_cvector_log_sum+0x5e>
ucomisd 0xea4(%rip),%xmm2
movsd  (%r12,%rax,8),%xmm0
jne    1166 <ighmm_cvector_log_sum+0x46>
jnp    1178 <ighmm_cvector_log_sum+0x58>
ucomisd %xmm2,%xmm0
jbe    117e <ighmm_cvector_log_sum+0x5e>
ucomisd 0xe8c(%rip),%xmm0
jne    1178 <ighmm_cvector_log_sum+0x58>
jnp    117e <ighmm_cvector_log_sum+0x5e>
movapd %xmm0,%xmm2
mov    %eax,%ecx
test   %esi,%esi
movsd  %xmm2,0x8(%rsp)
jle    1275 <ighmm_cvector_log_sum+0x155>
mov    %ecx,%r15d
movsd  0xe69(%rip),%xmm3
xor    %ebx,%ebx
movapd %xmm3,%xmm1
jmp    11ad <ighmm_cvector_log_sum+0x8d>
nop
add    $0x1,%rbx
cmp    %rbx,%r14
je     127d <ighmm_cvector_log_sum+0x15d>
movsd  (%r12,%rbx,8),%xmm0
ucomisd %xmm3,%xmm0
jne    11bb <ighmm_cvector_log_sum+0x9b>
jnp    11a0 <ighmm_cvector_log_sum+0x80>
cmp    %rbx,%r15
je     11a0 <ighmm_cvector_log_sum+0x80>
subsd  %xmm2,%xmm0
movsd  %xmm1,0x10(%rsp)
call   1030 <exp@plt>
movsd  0xe29(%rip),%xmm3
movsd  0x10(%rsp),%xmm1
movsd  0x8(%rsp),%xmm2
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
addsd  %xmm0,%xmm1
jmp    11a0 <ighmm_cvector_log_sum+0x80>
movsd  0xe07(%rip),%xmm1
movsd  %xmm1,0x8(%rsp)
jmp    127d <ighmm_cvector_log_sum+0x15d>
mov    %r14d,%edx
and    $0xfffffffe,%edx
movsd  0xdf1(%rip),%xmm0
xor    %eax,%eax
xor    %ecx,%ecx
movapd %xmm0,%xmm2
jmp    1234 <ighmm_cvector_log_sum+0x114>
nopl   0x0(%rax)
lea    0x1(%rax),%ecx
movapd %xmm1,%xmm2
add    $0x2,%rax
cmp    %rax,%rdx
je     114e <ighmm_cvector_log_sum+0x2e>
ucomisd %xmm0,%xmm2
movsd  (%r12,%rax,8),%xmm1
jne    1242 <ighmm_cvector_log_sum+0x122>
jnp    1250 <ighmm_cvector_log_sum+0x130>
ucomisd %xmm2,%xmm1
jbe    1256 <ighmm_cvector_log_sum+0x136>
ucomisd %xmm0,%xmm1
jne    1250 <ighmm_cvector_log_sum+0x130>
jnp    1256 <ighmm_cvector_log_sum+0x136>
movapd %xmm1,%xmm2
mov    %eax,%ecx
ucomisd %xmm0,%xmm2
movsd  0x8(%r12,%rax,8),%xmm1
jne    1265 <ighmm_cvector_log_sum+0x145>
jnp    1220 <ighmm_cvector_log_sum+0x100>
ucomisd %xmm2,%xmm1
jbe    1227 <ighmm_cvector_log_sum+0x107>
ucomisd %xmm0,%xmm1
jne    1220 <ighmm_cvector_log_sum+0x100>
jp     1220 <ighmm_cvector_log_sum+0x100>
jmp    1227 <ighmm_cvector_log_sum+0x107>
movsd  0xd83(%rip),%xmm1
movapd %xmm1,%xmm0
call   1040 <log@plt>
addsd  0x8(%rsp),%xmm0
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define double @ighmm_cvector_log_sum(double* %0, i32 %1) {
%3 = alloca double*
%4 = alloca i32
%5 = alloca i32
%6 = alloca double
%7 = alloca i32
%8 = alloca double
store double* %0, double** %3
store i32 %1, i32* %4
store double 1.000000e+00, double* %6
store i32 0, i32* %7
store i32 0, i32* %5
br label %9
9: ; preds = %39, %2
%10 = load i32, i32* %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %42
13: ; preds = %9
%14 = load double, double* %6
%15 = fcmp oeq double %14, 1.000000e+00
br i1 %15, label %31, label %16
16: ; preds = %13
%17 = load double*, double** %3
%18 = load i32, i32* %5
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds double, double* %17, i64 %19
%21 = load double, double* %20
%22 = load double, double* %6
%23 = fcmp ogt double %21, %22
br i1 %23, label %24, label %38
24: ; preds = %16
%25 = load double*, double** %3
%26 = load i32, i32* %5
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds double, double* %25, i64 %27
%29 = load double, double* %28
%30 = fcmp une double %29, 1.000000e+00
br i1 %30, label %31, label %38
31: ; preds = %24, %13
%32 = load double*, double** %3
%33 = load i32, i32* %5
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds double, double* %32, i64 %34
%36 = load double, double* %35
store double %36, double* %6
%37 = load i32, i32* %5
store i32 %37, i32* %7
br label %38
38: ; preds = %31, %24, %16
br label %39
39: ; preds = %38
%40 = load i32, i32* %5
%41 = add nsw i32 %40, 1
store i32 %41, i32* %5
br label %9
42: ; preds = %9
store double 1.000000e+00, double* %8
store i32 0, i32* %5
br label %43
43: ; preds = %71, %42
%44 = load i32, i32* %5
%45 = load i32, i32* %4
%46 = icmp slt i32 %44, %45
br i1 %46, label %47, label %74
47: ; preds = %43
%48 = load double*, double** %3
%49 = load i32, i32* %5
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds double, double* %48, i64 %50
%52 = load double, double* %51
%53 = fcmp une double %52, 1.000000e+00
br i1 %53, label %54, label %70
54: ; preds = %47
%55 = load i32, i32* %5
%56 = load i32, i32* %7
%57 = icmp ne i32 %55, %56
br i1 %57, label %58, label %70
58: ; preds = %54
%59 = load double*, double** %3
%60 = load i32, i32* %5
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds double, double* %59, i64 %61
%63 = load double, double* %62
%64 = load double, double* %6
%65 = fsub double %63, %64
%66 = call i64 @exp(double %65)
%67 = sitofp i64 %66 to double
%68 = load double, double* %8
%69 = fadd double %68, %67
store double %69, double* %8
br label %70
70: ; preds = %58, %54, %47
br label %71
71: ; preds = %70
%72 = load i32, i32* %5
%73 = add nsw i32 %72, 1
store i32 %73, i32* %5
br label %43
74: ; preds = %43
%75 = load double, double* %8
%76 = call double @log(double %75)
store double %76, double* %8
%77 = load double, double* %6
%78 = load double, double* %8
%79 = fadd double %78, %77
store double %79, double* %8
%80 = load double, double* %8
ret double %80
}

LLVM IR (-O2):
define double @ighmm_cvector_log_sum(double* nocapture %0, i32 %1) {
%3 = icmp sgt i32 %1, 0
br i1 %3, label %4, label %83
4: ; preds = %2
%5 = zext i32 %1 to i64
%6 = and i64 %5, 1
%7 = icmp eq i32 %1, 1
br i1 %7, label %10, label %8
8: ; preds = %4
%9 = and i64 %5, 4294967294
br label %33
10: ; preds = %60, %4
%11 = phi double [ undef, %4 ], [ %61, %60 ]
%12 = phi i32 [ undef, %4 ], [ %62, %60 ]
%13 = phi i64 [ 0, %4 ], [ %63, %60 ]
%14 = phi i32 [ 0, %4 ], [ %62, %60 ]
%15 = phi double [ 1.000000e+00, %4 ], [ %61, %60 ]
%16 = icmp eq i64 %6, 0
br i1 %16, label %27, label %17
17: ; preds = %10
%18 = fcmp oeq double %15, 1.000000e+00
%19 = getelementptr inbounds double, double* %0, i64 %13
%20 = load double, double* %19
br i1 %18, label %25, label %21
21: ; preds = %17
%22 = fcmp ogt double %20, %15
%23 = fcmp une double %20, 1.000000e+00
%24 = and i1 %22, %23
br i1 %24, label %25, label %27
25: ; preds = %21, %17
%26 = trunc i64 %13 to i32
br label %27
27: ; preds = %21, %25, %10
%28 = phi double [ %11, %10 ], [ %20, %25 ], [ %15, %21 ]
%29 = phi i32 [ %12, %10 ], [ %26, %25 ], [ %14, %21 ]
br i1 %3, label %30, label %83
30: ; preds = %27
%31 = zext i32 %29 to i64
%32 = zext i32 %1 to i64
br label %66
33: ; preds = %60, %8
%34 = phi i64 [ 0, %8 ], [ %63, %60 ]
%35 = phi i32 [ 0, %8 ], [ %62, %60 ]
%36 = phi double [ 1.000000e+00, %8 ], [ %61, %60 ]
%37 = phi i64 [ 0, %8 ], [ %64, %60 ]
%38 = fcmp oeq double %36, 1.000000e+00
%39 = getelementptr inbounds double, double* %0, i64 %34
%40 = load double, double* %39
br i1 %38, label %45, label %41
41: ; preds = %33
%42 = fcmp ogt double %40, %36
%43 = fcmp une double %40, 1.000000e+00
%44 = and i1 %42, %43
br i1 %44, label %45, label %47
45: ; preds = %33, %41
%46 = trunc i64 %34 to i32
br label %47
47: ; preds = %41, %45
%48 = phi double [ %40, %45 ], [ %36, %41 ]
%49 = phi i32 [ %46, %45 ], [ %35, %41 ]
%50 = or i64 %34, 1
%51 = fcmp oeq double %48, 1.000000e+00
%52 = getelementptr inbounds double, double* %0, i64 %50
%53 = load double, double* %52
br i1 %51, label %58, label %54
54: ; preds = %47
%55 = fcmp ogt double %53, %48
%56 = fcmp une double %53, 1.000000e+00
%57 = and i1 %55, %56
br i1 %57, label %58, label %60
58: ; preds = %54, %47
%59 = trunc i64 %50 to i32
br label %60
60: ; preds = %58, %54
%61 = phi double [ %53, %58 ], [ %48, %54 ]
%62 = phi i32 [ %59, %58 ], [ %49, %54 ]
%63 = add nuw nsw i64 %34, 2
%64 = add i64 %37, 2
%65 = icmp eq i64 %64, %9
br i1 %65, label %10, label %33
66: ; preds = %30, %79
%67 = phi i64 [ 0, %30 ], [ %81, %79 ]
%68 = phi double [ 1.000000e+00, %30 ], [ %80, %79 ]
%69 = getelementptr inbounds double, double* %0, i64 %67
%70 = load double, double* %69
%71 = fcmp oeq double %70, 1.000000e+00
%72 = icmp eq i64 %67, %31
%73 = select i1 %71, i1 true, i1 %72
br i1 %73, label %79, label %74
74: ; preds = %66
%75 = fsub double %70, %28
%76 = tail call i64 @exp(double %75)
%77 = sitofp i64 %76 to double
%78 = fadd double %68, %77
br label %79
79: ; preds = %66, %74
%80 = phi double [ %78, %74 ], [ %68, %66 ]
%81 = add nuw nsw i64 %67, 1
%82 = icmp eq i64 %81, %32
br i1 %82, label %83, label %66
83: ; preds = %79, %2, %27
%84 = phi double [ %28, %27 ], [ 1.000000e+00, %2 ], [ %28, %79 ]
%85 = phi double [ 1.000000e+00, %27 ], [ 1.000000e+00, %2 ], [ %80, %79 ]
%86 = tail call double @log(double %85)
%87 = fadd double %84, %86
ret double %87
}