#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 asum(double *, int);
scalar_t__ calloc(int, int);
int /*<<< orphan*/ fatalx(char *, int);
int /*<<< orphan*/ free(double *);
int /*<<< orphan*/ genlogbin(double *, int, double);
double log(double);
int /*<<< orphan*/ vexp(double *, double *, int);
int /*<<< orphan*/ vsp(double *, double *, double, int);
double binlogtail(int n, int t, double p, char c) {
  double *bindis;
  double val, base;
  if ((bindis = (double *)calloc((n + 1), sizeof(double))) == NULL)
    fatalx("Unable to allocate %d unit(s) for item \n", n + 1);
  genlogbin(bindis, n, p);
  base = bindis[t];
  vsp(bindis, bindis, -base, n + 1);
  if (c == '+') {
    vexp(bindis + t, bindis + t, n - t + 1);
    val = asum(bindis + t, n - t + 1);
  } else {
    vexp(bindis, bindis, t);
    val = asum(bindis, t);
  }
  free(bindis);
  return (log(val) + base);
}


x86 assembly (clang, -O0):
<binlogtail>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %dl,%al
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movsd  %xmm0,-0x10(%rbp)
mov    %al,-0x11(%rbp)
mov    -0x4(%rbp),%edi
add    $0x1,%edi
mov    $0x8,%esi
call   1070 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11c8 <binlogtail+0x48>
mov    -0x4(%rbp),%esi
add    $0x1,%esi
lea    0xe3d(%rip),%rdi
call   1060 <fatalx@plt>
mov    -0x20(%rbp),%rdi
mov    -0x4(%rbp),%esi
movsd  -0x10(%rbp),%xmm0
call   10a0 <genlogbin@plt>
mov    -0x20(%rbp),%rax
movslq -0x8(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x20(%rbp),%rsi
movsd  -0x30(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
mov    -0x4(%rbp),%edx
add    $0x1,%edx
call   1050 <vsp@plt>
movsbl -0x11(%rbp),%eax
cmp    $0x2b,%eax
jne    127a <binlogtail+0xfa>
mov    -0x20(%rbp),%rdi
movslq -0x8(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rdi
mov    -0x20(%rbp),%rsi
movslq -0x8(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rsi
mov    -0x4(%rbp),%edx
sub    -0x8(%rbp),%edx
add    $0x1,%edx
call   1030 <vexp@plt>
mov    -0x20(%rbp),%rdi
movslq -0x8(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rdi
mov    -0x4(%rbp),%esi
sub    -0x8(%rbp),%esi
add    $0x1,%esi
call   1080 <asum@plt>
movsd  %xmm0,-0x28(%rbp)
jmp    129b <binlogtail+0x11b>
mov    -0x20(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x8(%rbp),%edx
call   1030 <vexp@plt>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%esi
call   1080 <asum@plt>
movsd  %xmm0,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
movsd  -0x28(%rbp),%xmm0
call   1090 <log@plt>
addsd  -0x30(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<binlogtail>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edx,%r15d
movsd  %xmm0,0x10(%rsp)
mov    %esi,%r12d
mov    %edi,%r14d
lea    0x1(%r14),%r13d
mov    %r13d,%edi
mov    $0x8,%esi
call   1070 <calloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11c5 <binlogtail+0x45>
lea    0xe53(%rip),%rdi
mov    %r13d,%esi
call   1060 <fatalx@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
movsd  0x10(%rsp),%xmm0
call   10a0 <genlogbin@plt>
movslq %r12d,%rbp
movsd  (%rbx,%rbp,8),%xmm1
movaps 0xe1b(%rip),%xmm0
movaps %xmm1,0x10(%rsp)
xorps  %xmm1,%xmm0
mov    %rbx,%rdi
mov    %rbx,%rsi
mov    %r13d,%edx
call   1050 <vsp@plt>
cmp    $0x2b,%r15b
jne    1222 <binlogtail+0xa2>
lea    (%rbx,%rbp,8),%rbp
sub    %r12d,%r14d
add    $0x1,%r14d
mov    %rbp,%rdi
mov    %rbp,%rsi
mov    %r14d,%edx
call   1030 <vexp@plt>
mov    %rbp,%rdi
mov    %r14d,%esi
jmp    1236 <binlogtail+0xb6>
mov    %rbx,%rdi
mov    %rbx,%rsi
mov    %r12d,%edx
call   1030 <vexp@plt>
mov    %rbx,%rdi
mov    %r12d,%esi
call   1080 <asum@plt>
movsd  %xmm0,0x8(%rsp)
mov    %rbx,%rdi
call   1040 <free@plt>
movsd  0x8(%rsp),%xmm0
call   1090 <log@plt>
addsd  0x10(%rsp),%xmm0
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [41 x i8] c"Unable to allocate %d unit(s) for item \0A\00"
define double @binlogtail(i32 %0, i32 %1, double %2, i8 signext %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca double
%8 = alloca i8
%9 = alloca double*
%10 = alloca double
%11 = alloca double
store i32 %0, i32* %5
store i32 %1, i32* %6
store double %2, double* %7
store i8 %3, i8* %8
%12 = load i32, i32* %5
%13 = add nsw i32 %12, 1
%14 = call i64 @calloc(i32 %13, i32 8)
%15 = inttoptr i64 %14 to double*
store double* %15, double** %9
%16 = icmp eq double* %15, null
br i1 %16, label %17, label %21
17: ; preds = %4
%18 = load i32, i32* %5
%19 = add nsw i32 %18, 1
%20 = call i32 @fatalx(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %19)
br label %21
21: ; preds = %17, %4
%22 = load double*, double** %9
%23 = load i32, i32* %5
%24 = load double, double* %7
%25 = call i32 @genlogbin(double* %22, i32 %23, double %24)
%26 = load double*, double** %9
%27 = load i32, i32* %6
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds double, double* %26, i64 %28
%30 = load double, double* %29
store double %30, double* %11
%31 = load double*, double** %9
%32 = load double*, double** %9
%33 = load double, double* %11
%34 = fneg double %33
%35 = load i32, i32* %5
%36 = add nsw i32 %35, 1
%37 = call i32 @vsp(double* %31, double* %32, double %34, i32 %36)
%38 = load i8, i8* %8
%39 = sext i8 %38 to i32
%40 = icmp eq i32 %39, 43
br i1 %40, label %41, label %64
41: ; preds = %21
%42 = load double*, double** %9
%43 = load i32, i32* %6
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds double, double* %42, i64 %44
%46 = load double*, double** %9
%47 = load i32, i32* %6
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds double, double* %46, i64 %48
%50 = load i32, i32* %5
%51 = load i32, i32* %6
%52 = sub nsw i32 %50, %51
%53 = add nsw i32 %52, 1
%54 = call i32 @vexp(double* %45, double* %49, i32 %53)
%55 = load double*, double** %9
%56 = load i32, i32* %6
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds double, double* %55, i64 %57
%59 = load i32, i32* %5
%60 = load i32, i32* %6
%61 = sub nsw i32 %59, %60
%62 = add nsw i32 %61, 1
%63 = call double @asum(double* %58, i32 %62)
store double %63, double* %10
br label %72
64: ; preds = %21
%65 = load double*, double** %9
%66 = load double*, double** %9
%67 = load i32, i32* %6
%68 = call i32 @vexp(double* %65, double* %66, i32 %67)
%69 = load double*, double** %9
%70 = load i32, i32* %6
%71 = call double @asum(double* %69, i32 %70)
store double %71, double* %10
br label %72
72: ; preds = %64, %41
%73 = load double*, double** %9
%74 = call i32 @free(double* %73)
%75 = load double, double* %10
%76 = call double @log(double %75)
%77 = load double, double* %11
%78 = fadd double %76, %77
ret double %78
}

LLVM IR (-O2):
@.str = [41 x i8] c"Unable to allocate %d unit(s) for item \0A\00"
define double @binlogtail(i32 %0, i32 %1, double %2, i8 signext %3) {
%5 = add nsw i32 %0, 1
%6 = tail call i64 @calloc(i32 %5, i32 8)
%7 = inttoptr i64 %6 to double*
%8 = icmp eq i64 %6, 0
br i1 %8, label %9, label %11
9: ; preds = %4
%10 = tail call i32 @fatalx(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %5)
br label %11
11: ; preds = %9, %4
%12 = tail call i32 @genlogbin(double* %7, i32 %0, double %2)
%13 = sext i32 %1 to i64
%14 = getelementptr inbounds double, double* %7, i64 %13
%15 = load double, double* %14
%16 = fneg double %15
%17 = tail call i32 @vsp(double* %7, double* %7, double %16, i32 %5)
%18 = icmp eq i8 %3, 43
br i1 %18, label %19, label %24
19: ; preds = %11
%20 = sub nsw i32 %0, %1
%21 = add nsw i32 %20, 1
%22 = tail call i32 @vexp(double* %14, double* %14, i32 %21)
%23 = tail call double @asum(double* %14, i32 %21)
br label %27
24: ; preds = %11
%25 = tail call i32 @vexp(double* %7, double* %7, i32 %1)
%26 = tail call double @asum(double* %7, i32 %1)
br label %27
27: ; preds = %24, %19
%28 = phi double [ %23, %19 ], [ %26, %24 ]
%29 = tail call i32 @free(double* %7)
%30 = tail call double @log(double %28)
%31 = fadd double %15, %30
ret double %31
}