#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ fread(double *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(double *);
double kahan_accum(double, double *, double);
double *malloc(int);
int state_expectations_alldisk(int nstates, double *expectations, FILE *fi_d) {
  int i;
  double *compensations = malloc(nstates * sizeof(double));
  double *probabilities = malloc(nstates * sizeof(double));
  for (i = 0; i < nstates; ++i) {
    compensations[i] = 0.0;
  }
  while (fread(probabilities, sizeof(double), nstates, fi_d)) {
    for (i = 0; i < nstates; i++) {
      expectations[i] =
          kahan_accum(expectations[i], compensations + i, probabilities[i]);
    }
  }
  free(compensations);
  free(probabilities);
  return 0;
}


x86 assembly (clang, -O0):
<state_expectations_alldisk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movslq -0x4(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
movslq -0x4(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    11aa <state_expectations_alldisk+0x6a>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
xorps  %xmm0,%xmm0
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1180 <state_expectations_alldisk+0x40>
jmp    11af <state_expectations_alldisk+0x6f>
mov    -0x30(%rbp),%rdi
mov    -0x4(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x8,%esi
call   1040 <fread@plt>
cmp    $0x0,%rax
je     122f <state_expectations_alldisk+0xef>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    122a <state_expectations_alldisk+0xea>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x28(%rbp),%rdi
movslq -0x1c(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rdi
mov    -0x30(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm1
call   1050 <kahan_accum@plt>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11d5 <state_expectations_alldisk+0x95>
jmp    11af <state_expectations_alldisk+0x6f>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<state_expectations_alldisk>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,(%rsp)
mov    %rsi,%rbx
mov    %edi,%r15d
lea    0x0(,%r15,8),%ebp
mov    %ebp,%edi
call   1070 <malloc@plt>
mov    %rax,%r12
mov    %ebp,%edi
call   1070 <malloc@plt>
mov    %rax,%r13
mov    %r15d,%ebp
test   %r15d,%r15d
jle    119b <state_expectations_alldisk+0x4b>
lea    0x0(,%rbp,8),%rdx
mov    %r12,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
mov    %r13,%rdi
mov    $0x8,%esi
mov    %r15d,%edx
mov    (%rsp),%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     1217 <state_expectations_alldisk+0xc7>
shl    $0x3,%rbp
jmp    11d9 <state_expectations_alldisk+0x89>
nopw   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    $0x8,%esi
mov    %r15d,%edx
mov    (%rsp),%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     1217 <state_expectations_alldisk+0xc7>
test   %r15d,%r15d
jle    11c0 <state_expectations_alldisk+0x70>
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movsd  (%rbx,%r14,1),%xmm0
lea    (%r12,%r14,1),%rdi
movsd  0x0(%r13,%r14,1),%xmm1
call   1050 <kahan_accum@plt>
movsd  %xmm0,(%rbx,%r14,1)
add    $0x8,%r14
cmp    %r14,%rbp
jne    11f0 <state_expectations_alldisk+0xa0>
jmp    11c0 <state_expectations_alldisk+0x70>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r13,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @state_expectations_alldisk(i32 %0, double* nocapture %1, i32* nocapture %2) {
%4 = shl i32 %0, 3
%5 = tail call double* @malloc(i32 %4)
%6 = tail call double* @malloc(i32 %4)
%7 = icmp sgt i32 %0, 0
br i1 %7, label %8, label %12
8: ; preds = %3
%9 = bitcast double* %5 to i8*
%10 = zext i32 %0 to i64
%11 = shl nuw nsw i64 %10, 3
br label %12
12: ; preds = %8, %3
%13 = tail call i64 @fread(double* %6, i32 8, i32 %0, i32* %2)
%14 = icmp eq i64 %13, 0
br i1 %14, label %31, label %15
15: ; preds = %12
%16 = zext i32 %0 to i64
br label %20
17: ; preds = %21, %20
%18 = tail call i64 @fread(double* %6, i32 8, i32 %0, i32* %2)
%19 = icmp eq i64 %18, 0
br i1 %19, label %31, label %20
20: ; preds = %15, %17
br i1 %7, label %21, label %17
21: ; preds = %20, %21
%22 = phi i64 [ %29, %21 ], [ 0, %20 ]
%23 = getelementptr inbounds double, double* %1, i64 %22
%24 = load double, double* %23
%25 = getelementptr inbounds double, double* %5, i64 %22
%26 = getelementptr inbounds double, double* %6, i64 %22
%27 = load double, double* %26
%28 = tail call double @kahan_accum(double %24, double* %25, double %27)
store double %28, double* %23
%29 = add nuw nsw i64 %22, 1
%30 = icmp eq i64 %29, %16
br i1 %30, label %17, label %21
31: ; preds = %17, %12
%32 = tail call i32 @free(double* %5)
%33 = tail call i32 @free(double* %6)
ret i32 0
}