#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 */
struct hdc_item_memories {
  int dummy;
};

/* Variables and functions */
double *calloc(int, int);
double *compute_ngram(double **, struct hdc_item_memories *, int, int, double);
int /*<<< orphan*/ entrywise_sum(double *, double *, double *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(double *);
int /*<<< orphan*/ stderr;
double *compute_sum_hv(double **buffer, int buffer_length,
                       struct hdc_item_memories *item_memories, int len, int n,
                       double precision) {
  double *sum_hv = calloc(len, sizeof(double));
  if (!sum_hv)
    goto mem_error;
  for (int i = 1; i <= buffer_length - n + 1; i++) {
    double *new_ngram =
        compute_ngram(buffer + i, item_memories, len, n, precision);
    entrywise_sum(sum_hv, sum_hv, new_ngram, len);
    free(new_ngram);
  }
  return sum_hv;
mem_error:
  fprintf(stderr, "compute_sum_hv: failed to allocate memory\n");
  return NULL;
}


x86 assembly (clang, -O0):
<compute_sum_hv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8d,-0x28(%rbp)
movsd  %xmm0,-0x30(%rbp)
mov    -0x24(%rbp),%edi
mov    $0x8,%esi
call   1050 <calloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1190 <compute_sum_hv+0x40>
jmp    120a <compute_sum_hv+0xba>
movl   $0x1,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    -0x28(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jg     11fd <compute_sum_hv+0xad>
mov    -0x10(%rbp),%rdi
movslq -0x3c(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rdi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
mov    -0x28(%rbp),%ecx
movsd  -0x30(%rbp),%xmm0
call   1070 <compute_ngram@plt>
mov    %rax,-0x48(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x48(%rbp),%rdx
mov    -0x24(%rbp),%ecx
call   1040 <entrywise_sum@plt>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    1197 <compute_sum_hv+0x47>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1227 <compute_sum_hv+0xd7>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
lea    0xde6(%rip),%rsi
call   1060 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<compute_sum_hv>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
movsd  %xmm0,0x10(%rsp)
mov    %r8d,%r14d
mov    %ecx,%r12d
mov    %rdx,%r15
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %ecx,%edi
mov    $0x8,%esi
call   1050 <calloc@plt>
mov    %rax,%r13
test   %rax,%rax
mov    %rax,0x8(%rsp)
je     11ed <compute_sum_hv+0x9d>
sub    %r14d,%ebp
lea    0x1(%rbp),%eax
test   %eax,%eax
jle    1202 <compute_sum_hv+0xb2>
add    $0x2,%ebp
add    $0x8,%rbx
add    $0xffffffffffffffff,%rbp
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12d,%edx
mov    %r14d,%ecx
movsd  0x10(%rsp),%xmm0
call   1070 <compute_ngram@plt>
mov    %r14d,%r13d
mov    %r15,%r14
mov    %rax,%r15
mov    0x8(%rsp),%rdi
mov    %rdi,%rsi
mov    %rax,%rdx
mov    %r12d,%ecx
call   1040 <entrywise_sum@plt>
mov    %r15,%rdi
mov    %r14,%r15
mov    %r13d,%r14d
call   1030 <free@plt>
add    $0x8,%rbx
add    $0xffffffffffffffff,%rbp
jne    11a0 <compute_sum_hv+0x50>
jmp    1202 <compute_sum_hv+0xb2>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
lea    0xe03(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x8(%rsp),%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.hdc_item_memories = type { i32 }
@stderr = global i32 0
@.str = [43 x i8] c"compute_sum_hv: failed to allocate memory\0A\00"
define double* @compute_sum_hv(double** %0, i32 %1, %struct.hdc_item_memories* %2, i32 %3, i32 %4, double %5) {
%7 = alloca double*
%8 = alloca double**
%9 = alloca i32
%10 = alloca %struct.hdc_item_memories*
%11 = alloca i32
%12 = alloca i32
%13 = alloca double
%14 = alloca double*
%15 = alloca i32
%16 = alloca double*
store double** %0, double*** %8
store i32 %1, i32* %9
store %struct.hdc_item_memories* %2, %struct.hdc_item_memories** %10
store i32 %3, i32* %11
store i32 %4, i32* %12
store double %5, double* %13
%17 = load i32, i32* %11
%18 = call double* @calloc(i32 %17, i32 8)
store double* %18, double** %14
%19 = load double*, double** %14
%20 = icmp ne double* %19, null
br i1 %20, label %22, label %21
21: ; preds = %6
br label %52
22: ; preds = %6
store i32 1, i32* %15
br label %23
23: ; preds = %47, %22
%24 = load i32, i32* %15
%25 = load i32, i32* %9
%26 = load i32, i32* %12
%27 = sub nsw i32 %25, %26
%28 = add nsw i32 %27, 1
%29 = icmp sle i32 %24, %28
br i1 %29, label %30, label %50
30: ; preds = %23
%31 = load double**, double*** %8
%32 = load i32, i32* %15
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds double*, double** %31, i64 %33
%35 = load %struct.hdc_item_memories*, %struct.hdc_item_memories** %10
%36 = load i32, i32* %11
%37 = load i32, i32* %12
%38 = load double, double* %13
%39 = call double* @compute_ngram(double** %34, %struct.hdc_item_memories* %35, i32 %36, i32 %37, double %38)
store double* %39, double** %16
%40 = load double*, double** %14
%41 = load double*, double** %14
%42 = load double*, double** %16
%43 = load i32, i32* %11
%44 = call i32 @entrywise_sum(double* %40, double* %41, double* %42, i32 %43)
%45 = load double*, double** %16
%46 = call i32 @free(double* %45)
br label %47
47: ; preds = %30
%48 = load i32, i32* %15
%49 = add nsw i32 %48, 1
store i32 %49, i32* %15
br label %23
50: ; preds = %23
%51 = load double*, double** %14
store double* %51, double** %7
br label %55
52: ; preds = %21
%53 = load i32, i32* @stderr
%54 = call i32 @fprintf(i32 %53, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
store double* null, double** %7
br label %55
55: ; preds = %52, %50
%56 = load double*, double** %7
ret double* %56
}

LLVM IR (-O2):
%struct.hdc_item_memories = type { i32 }
@stderr = global i32 0
@.str = [43 x i8] c"compute_sum_hv: failed to allocate memory\0A\00"
define double* @compute_sum_hv(double** %0, i32 %1, %struct.hdc_item_memories* %2, i32 %3, i32 %4, double %5) {
%7 = tail call double* @calloc(i32 %3, i32 8)
%8 = icmp eq double* %7, null
br i1 %8, label %25, label %9
9: ; preds = %6
%10 = add i32 %1, 1
%11 = sub i32 %10, %4
%12 = icmp slt i32 %11, 1
br i1 %12, label %28, label %13
13: ; preds = %9
%14 = add i32 %1, 2
%15 = sub i32 %14, %4
%16 = zext i32 %15 to i64
br label %17
17: ; preds = %13, %17
%18 = phi i64 [ 1, %13 ], [ %23, %17 ]
%19 = getelementptr inbounds double*, double** %0, i64 %18
%20 = tail call double* @compute_ngram(double** %19, %struct.hdc_item_memories* %2, i32 %3, i32 %4, double %5)
%21 = tail call i32 @entrywise_sum(double* %7, double* %7, double* %20, i32 %3)
%22 = tail call i32 @free(double* %20)
%23 = add nuw nsw i64 %18, 1
%24 = icmp eq i64 %23, %16
br i1 %24, label %28, label %17
25: ; preds = %6
%26 = load i32, i32* @stderr
%27 = tail call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
br label %28
28: ; preds = %17, %9, %25
ret double* %7
}