#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 log_sum(double, double);
double probvec_tailsum(double *probvec, int tail_startindex, int probvec_len) {
  double tailsum;
  int i;
  tailsum = probvec[tail_startindex];
  for (i = tail_startindex + 1; i < probvec_len; i++) {
    tailsum = log_sum(tailsum, probvec[i]);
  }
  return tailsum;
}


x86 assembly (clang, -O0):
<probvec_tailsum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    1173 <probvec_tailsum+0x63>
movsd  -0x18(%rbp),%xmm0
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm1
call   1030 <log_sum@plt>
movsd  %xmm0,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    113d <probvec_tailsum+0x2d>
movsd  -0x18(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<probvec_tailsum>:
push   %r15
push   %r14
push   %rbx
movslq %esi,%rax
movsd  (%rdi,%rax,8),%xmm0
add    $0x1,%eax
cmp    %edx,%eax
jge    1154 <probvec_tailsum+0x44>
mov    %esi,%r15d
cltq
lea    (%rdi,%rax,8),%r14
not    %r15d
add    %edx,%r15d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
movsd  (%r14,%rbx,8),%xmm1
call   1030 <log_sum@plt>
add    $0x1,%rbx
cmp    %ebx,%r15d
jne    1140 <probvec_tailsum+0x30>
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define double @probvec_tailsum(double* %0, i32 %1, i32 %2) {
%4 = alloca double*
%5 = alloca i32
%6 = alloca i32
%7 = alloca double
%8 = alloca i32
store double* %0, double** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%9 = load double*, double** %4
%10 = load i32, i32* %5
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds double, double* %9, i64 %11
%13 = load double, double* %12
store double %13, double* %7
%14 = load i32, i32* %5
%15 = add nsw i32 %14, 1
store i32 %15, i32* %8
br label %16
16: ; preds = %28, %3
%17 = load i32, i32* %8
%18 = load i32, i32* %6
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %31
20: ; preds = %16
%21 = load double, double* %7
%22 = load double*, double** %4
%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 = call double @log_sum(double %21, double %26)
store double %27, double* %7
br label %28
28: ; preds = %20
%29 = load i32, i32* %8
%30 = add nsw i32 %29, 1
store i32 %30, i32* %8
br label %16
31: ; preds = %16
%32 = load double, double* %7
ret double %32
}

LLVM IR (-O2):
define double @probvec_tailsum(double* nocapture %0, i32 %1, i32 %2) {
%4 = sext i32 %1 to i64
%5 = getelementptr inbounds double, double* %0, i64 %4
%6 = load double, double* %5
%7 = add i32 %1, 1
%8 = icmp slt i32 %7, %2
br i1 %8, label %9, label %20
9: ; preds = %3
%10 = sext i32 %7 to i64
br label %11
11: ; preds = %9, %11
%12 = phi i64 [ %10, %9 ], [ %17, %11 ]
%13 = phi double [ %6, %9 ], [ %16, %11 ]
%14 = getelementptr inbounds double, double* %0, i64 %12
%15 = load double, double* %14
%16 = tail call double @log_sum(double %13, double %15)
%17 = add nsw i64 %12, 1
%18 = trunc i64 %17 to i32
%19 = icmp eq i32 %18, %2
br i1 %19, label %20, label %11
20: ; preds = %11, %3
%21 = phi double [ %6, %3 ], [ %16, %11 ]
ret double %21
}