#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 fabs(float);
int frand();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int main() {
  int i;
  float x_est_last = 0;
  float P_last = 0;
  float Q = 0.022;
  float R = 0.617;
  float K;
  float P;
  float P_temp;
  float x_temp_est;
  float x_est;
  float z_measured;
  float z_real = 0.5;
  srand(0);
  x_est_last = z_real + frand() * 10;
  float sum_error_kalman = 0;
  float sum_error_measure = 0;
  for (i = 0; i < 30; i++) {
    x_temp_est = x_est_last;
    P_temp = P_last + Q;
    K = P_temp * (1.0 / (P_temp + R));
    z_measured = z_real + frand() * 10;
    x_est = x_temp_est + K * (z_measured - x_temp_est);
    P = (1 - K) * P_temp;
    printf("Ideal    position: %6.3f \n", z_real);
    printf("Mesaured position: %6.3f [diff:%.3f]\n", z_measured,
           fabs(z_real - z_measured));
    printf("Kalman   position: %6.3f [diff:%.3f]\n", x_est,
           fabs(z_real - x_est));
    printf("\n+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
           "+++++++++++\n");
    sum_error_kalman += fabs(z_real - x_est);
    sum_error_measure += fabs(z_real - z_measured);
    P_last = P;
    x_est_last = x_est;
  }
  printf("Total error if using raw measured:  %f\n", sum_error_measure);
  printf("Total error if using kalman filter: %f\n", sum_error_kalman);
  printf("Reduction in error: %d%% \n",
         100 - (int)((sum_error_kalman / sum_error_measure) * 100));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0xc(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0x10(%rbp)
movss  0xea1(%rip),%xmm0
movss  %xmm0,-0x14(%rbp)
movss  0xe90(%rip),%xmm0
movss  %xmm0,-0x18(%rbp)
movss  0xe7f(%rip),%xmm0
movss  %xmm0,-0x34(%rbp)
xor    %edi,%edi
call   1040 <srand@plt>
movss  -0x34(%rbp),%xmm0
movss  %xmm0,-0x40(%rbp)
mov    $0x0,%al
call   1060 <frand@plt>
movss  -0x40(%rbp),%xmm0
imul   $0xa,%eax,%eax
cvtsi2ss %eax,%xmm1
addss  %xmm1,%xmm0
movss  %xmm0,-0xc(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0x38(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0x3c(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0x1e,-0x8(%rbp)
jge    1378 <main+0x238>
movss  -0xc(%rbp),%xmm0
movss  %xmm0,-0x28(%rbp)
movss  -0x10(%rbp),%xmm0
addss  -0x14(%rbp),%xmm0
movss  %xmm0,-0x24(%rbp)
movss  -0x24(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movss  -0x24(%rbp),%xmm1
addss  -0x18(%rbp),%xmm1
cvtss2sd %xmm1,%xmm2
movsd  0xe0c(%rip),%xmm1
divsd  %xmm2,%xmm1
mulsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x1c(%rbp)
movss  -0x34(%rbp),%xmm0
movss  %xmm0,-0x54(%rbp)
mov    $0x0,%al
call   1060 <frand@plt>
movss  -0x54(%rbp),%xmm0
imul   $0xa,%eax,%eax
cvtsi2ss %eax,%xmm1
addss  %xmm1,%xmm0
movss  %xmm0,-0x30(%rbp)
movss  -0x28(%rbp),%xmm1
movss  -0x1c(%rbp),%xmm0
movss  -0x30(%rbp),%xmm2
subss  -0x28(%rbp),%xmm2
mulss  %xmm2,%xmm0
addss  %xmm1,%xmm0
movss  %xmm0,-0x2c(%rbp)
movss  0xda4(%rip),%xmm0
subss  -0x1c(%rbp),%xmm0
mulss  -0x24(%rbp),%xmm0
movss  %xmm0,-0x20(%rbp)
movss  -0x34(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xd95(%rip),%rdi
mov    $0x1,%al
call   1030 <printf@plt>
movss  -0x30(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  %xmm0,-0x50(%rbp)
movss  -0x34(%rbp),%xmm0
subss  -0x30(%rbp),%xmm0
call   1050 <fabs@plt>
movaps %xmm0,%xmm1
movsd  -0x50(%rbp),%xmm0
lea    0xd7d(%rip),%rdi
mov    $0x2,%al
call   1030 <printf@plt>
movss  -0x2c(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  %xmm0,-0x48(%rbp)
movss  -0x34(%rbp),%xmm0
subss  -0x2c(%rbp),%xmm0
call   1050 <fabs@plt>
movaps %xmm0,%xmm1
movsd  -0x48(%rbp),%xmm0
lea    0xd70(%rip),%rdi
mov    $0x2,%al
call   1030 <printf@plt>
lea    0xd88(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movss  -0x34(%rbp),%xmm0
subss  -0x2c(%rbp),%xmm0
call   1050 <fabs@plt>
movaps %xmm0,%xmm1
movss  -0x38(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
addsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x38(%rbp)
movss  -0x34(%rbp),%xmm0
subss  -0x30(%rbp),%xmm0
call   1050 <fabs@plt>
movaps %xmm0,%xmm1
movss  -0x3c(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
addsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x3c(%rbp)
movss  -0x20(%rbp),%xmm0
movss  %xmm0,-0x10(%rbp)
movss  -0x2c(%rbp),%xmm0
movss  %xmm0,-0xc(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11ca <main+0x8a>
movss  -0x3c(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xd4e(%rip),%rdi
mov    $0x1,%al
call   1030 <printf@plt>
movss  -0x38(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xd5f(%rip),%rdi
mov    $0x1,%al
call   1030 <printf@plt>
movss  -0x38(%rbp),%xmm0
divss  -0x3c(%rbp),%xmm0
movss  0xc54(%rip),%xmm1
mulss  %xmm1,%xmm0
cvttss2si %xmm0,%eax
mov    $0x64,%esi
sub    %eax,%esi
lea    0xd58(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x40,%rsp
xor    %edi,%edi
call   1050 <srand@plt>
xor    %eax,%eax
call   1070 <frand@plt>
add    %eax,%eax
lea    (%rax,%rax,4),%eax
cvtsi2ss %eax,%xmm0
addss  0xe85(%rip),%xmm0
movss  %xmm0,0x4(%rsp)
xorps  %xmm0,%xmm0
xorpd  %xmm1,%xmm1
movss  %xmm1,0x10(%rsp)
mov    $0x1e,%ebp
lea    0xe8e(%rip),%r14
lea    0xea2(%rip),%r15
lea    0xec1(%rip),%r12
lea    0xf4b(%rip),%rbx
nop
movaps %xmm0,0x30(%rsp)
movss  0x10(%rsp),%xmm1
addss  0xe41(%rip),%xmm1
movss  %xmm1,0x10(%rsp)
xorps  %xmm0,%xmm0
cvtss2sd %xmm1,%xmm0
addss  0xe30(%rip),%xmm1
cvtss2sd %xmm1,%xmm1
movsd  0xe34(%rip),%xmm2
divsd  %xmm1,%xmm2
mulsd  %xmm0,%xmm2
xorps  %xmm0,%xmm0
cvtsd2ss %xmm2,%xmm0
movss  %xmm0,(%rsp)
xor    %eax,%eax
call   1070 <frand@plt>
add    %eax,%eax
lea    (%rax,%rax,4),%eax
xorps  %xmm1,%xmm1
cvtsi2ss %eax,%xmm1
movss  0xded(%rip),%xmm0
addss  %xmm0,%xmm1
movss  %xmm1,0x20(%rsp)
movaps %xmm1,%xmm0
movss  0x4(%rsp),%xmm1
subss  %xmm1,%xmm0
movss  (%rsp),%xmm2
mulss  %xmm2,%xmm0
addss  %xmm0,%xmm1
movss  %xmm1,0x4(%rsp)
movss  0xdc7(%rip),%xmm0
subss  %xmm2,%xmm0
movss  0x10(%rsp),%xmm1
mulss  %xmm0,%xmm1
movss  %xmm1,0x10(%rsp)
mov    %r14,%rdi
movsd  0xdbc(%rip),%xmm0
mov    $0x1,%al
call   1040 <printf@plt>
movss  0x20(%rsp),%xmm1
xorps  %xmm0,%xmm0
cvtss2sd %xmm1,%xmm0
movsd  %xmm0,0x8(%rsp)
movss  0xd7a(%rip),%xmm0
subss  %xmm1,%xmm0
movss  %xmm0,(%rsp)
call   1060 <fabs@plt>
movaps %xmm0,%xmm1
mov    %r15,%rdi
movsd  0x8(%rsp),%xmm0
mov    $0x2,%al
call   1040 <printf@plt>
movss  0x4(%rsp),%xmm0
xorps  %xmm1,%xmm1
cvtss2sd %xmm0,%xmm1
movsd  %xmm1,0x8(%rsp)
movss  0xd3e(%rip),%xmm1
subss  %xmm0,%xmm1
movss  %xmm1,0x20(%rsp)
movaps %xmm1,%xmm0
call   1060 <fabs@plt>
movaps %xmm0,%xmm1
mov    %r12,%rdi
movsd  0x8(%rsp),%xmm0
mov    $0x2,%al
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
movss  0x20(%rsp),%xmm0
call   1060 <fabs@plt>
movaps %xmm0,0x20(%rsp)
movss  (%rsp),%xmm0
call   1060 <fabs@plt>
cvtps2pd 0x30(%rsp),%xmm1
unpcklpd 0x20(%rsp),%xmm0
addpd  %xmm1,%xmm0
cvtpd2ps %xmm0,%xmm0
add    $0xffffffff,%ebp
jne    11b0 <main+0x60>
movaps %xmm0,0x30(%rsp)
cvtss2sd %xmm0,%xmm0
lea    0xd5a(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
movaps 0x30(%rsp),%xmm0
shufps $0x55,%xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
cvtss2sd %xmm0,%xmm0
lea    0xd62(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
movaps 0x10(%rsp),%xmm0
divss  0x30(%rsp),%xmm0
mulss  0xca1(%rip),%xmm0
cvttss2si %xmm0,%eax
mov    $0x64,%esi
sub    %eax,%esi
lea    0xd5e(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [27 x i8] c"Ideal position: %6.3f \0A\00"
@.str.1 = [38 x i8] c"Mesaured position: %6.3f [diff:%.3f]\0A\00"
@.str.2 = [38 x i8] c"Kalman position: %6.3f [diff:%.3f]\0A\00"
@.str.3 = [79 x i8] c"\0A++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\0A\00"
@.str.4 = [40 x i8] c"Total error if using raw measured: %f\0A\00"
@.str.5 = [40 x i8] c"Total error if using kalman filter: %f\0A\00"
@.str.6 = [27 x i8] c"Reduction in error: %d%% \0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca float
%4 = alloca float
%5 = alloca float
%6 = alloca float
%7 = alloca float
%8 = alloca float
%9 = alloca float
%10 = alloca float
%11 = alloca float
%12 = alloca float
%13 = alloca float
%14 = alloca float
%15 = alloca float
store i32 0, i32* %1
store float 0.000000e+00, float* %3
store float 0.000000e+00, float* %4
store float 0x3F96872B00000000, float* %5
store float 0x3FE3BE76C0000000, float* %6
store float 5.000000e-01, float* %13
%16 = call i32 @srand(i32 0)
%17 = load float, float* %13
%18 = call i32 (...) @frand()
%19 = mul nsw i32 %18, 10
%20 = sitofp i32 %19 to float
%21 = fadd float %17, %20
store float %21, float* %3
store float 0.000000e+00, float* %14
store float 0.000000e+00, float* %15
store i32 0, i32* %2
br label %22
22: ; preds = %90, %0
%23 = load i32, i32* %2
%24 = icmp slt i32 %23, 30
br i1 %24, label %25, label %93
25: ; preds = %22
%26 = load float, float* %3
store float %26, float* %10
%27 = load float, float* %4
%28 = load float, float* %5
%29 = fadd float %27, %28
store float %29, float* %9
%30 = load float, float* %9
%31 = fpext float %30 to double
%32 = load float, float* %9
%33 = load float, float* %6
%34 = fadd float %32, %33
%35 = fpext float %34 to double
%36 = fdiv double 1.000000e+00, %35
%37 = fmul double %31, %36
%38 = fptrunc double %37 to float
store float %38, float* %7
%39 = load float, float* %13
%40 = call i32 (...) @frand()
%41 = mul nsw i32 %40, 10
%42 = sitofp i32 %41 to float
%43 = fadd float %39, %42
store float %43, float* %12
%44 = load float, float* %10
%45 = load float, float* %7
%46 = load float, float* %12
%47 = load float, float* %10
%48 = fsub float %46, %47
%49 = call float @llvm.fmuladd.f32(float %45, float %48, float %44)
store float %49, float* %11
%50 = load float, float* %7
%51 = fsub float 1.000000e+00, %50
%52 = load float, float* %9
%53 = fmul float %51, %52
store float %53, float* %8
%54 = load float, float* %13
%55 = fpext float %54 to double
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), double %55)
%57 = load float, float* %12
%58 = fpext float %57 to double
%59 = load float, float* %13
%60 = load float, float* %12
%61 = fsub float %59, %60
%62 = call double @fabs(float %61)
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), double %58, double %62)
%64 = load float, float* %11
%65 = fpext float %64 to double
%66 = load float, float* %13
%67 = load float, float* %11
%68 = fsub float %66, %67
%69 = call double @fabs(float %68)
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), double %65, double %69)
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([79 x i8], [79 x i8]* @.str.3, i64 0, i64 0))
%72 = load float, float* %13
%73 = load float, float* %11
%74 = fsub float %72, %73
%75 = call double @fabs(float %74)
%76 = load float, float* %14
%77 = fpext float %76 to double
%78 = fadd double %77, %75
%79 = fptrunc double %78 to float
store float %79, float* %14
%80 = load float, float* %13
%81 = load float, float* %12
%82 = fsub float %80, %81
%83 = call double @fabs(float %82)
%84 = load float, float* %15
%85 = fpext float %84 to double
%86 = fadd double %85, %83
%87 = fptrunc double %86 to float
store float %87, float* %15
%88 = load float, float* %8
store float %88, float* %4
%89 = load float, float* %11
store float %89, float* %3
br label %90
90: ; preds = %25
%91 = load i32, i32* %2
%92 = add nsw i32 %91, 1
store i32 %92, i32* %2
br label %22
93: ; preds = %22
%94 = load float, float* %15
%95 = fpext float %94 to double
%96 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0), double %95)
%97 = load float, float* %14
%98 = fpext float %97 to double
%99 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0), double %98)
%100 = load float, float* %14
%101 = load float, float* %15
%102 = fdiv float %100, %101
%103 = fmul float %102, 1.000000e+02
%104 = fptosi float %103 to i32
%105 = sub nsw i32 100, %104
%106 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i32 %105)
ret i32 0
}

LLVM IR (-O2):
@.str = [27 x i8] c"Ideal position: %6.3f \0A\00"
@.str.1 = [38 x i8] c"Mesaured position: %6.3f [diff:%.3f]\0A\00"
@.str.2 = [38 x i8] c"Kalman position: %6.3f [diff:%.3f]\0A\00"
@.str.4 = [40 x i8] c"Total error if using raw measured: %f\0A\00"
@.str.5 = [40 x i8] c"Total error if using kalman filter: %f\0A\00"
@.str.6 = [27 x i8] c"Reduction in error: %d%% \0A\00"
@str = [78 x i8] c"\0A++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\00"
define i32 @main() {
%1 = tail call i32 @srand(i32 0)
%2 = tail call i32 (...) @frand()
%3 = mul nsw i32 %2, 10
%4 = sitofp i32 %3 to float
%5 = fadd float %4, 5.000000e-01
br label %6
6: ; preds = %0, %6
%7 = phi i32 [ 0, %0 ], [ %43, %6 ]
%8 = phi float [ %5, %0 ], [ %23, %6 ]
%9 = phi float [ 0.000000e+00, %0 ], [ %25, %6 ]
%10 = phi <2 x float> [ zeroinitializer, %0 ], [ %42, %6 ]
%11 = fadd float %9, 0x3F96872B00000000
%12 = fpext float %11 to double
%13 = fadd float %11, 0x3FE3BE76C0000000
%14 = fpext float %13 to double
%15 = fdiv double 1.000000e+00, %14
%16 = fmul double %15, %12
%17 = fptrunc double %16 to float
%18 = tail call i32 (...) @frand()
%19 = mul nsw i32 %18, 10
%20 = sitofp i32 %19 to float
%21 = fadd float %20, 5.000000e-01
%22 = fsub float %21, %8
%23 = tail call float @llvm.fmuladd.f32(float %17, float %22, float %8)
%24 = fsub float 1.000000e+00, %17
%25 = fmul float %11, %24
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), double 5.000000e-01)
%27 = fpext float %21 to double
%28 = fsub float 5.000000e-01, %21
%29 = tail call double @fabs(float %28)
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), double %27, double %29)
%31 = fpext float %23 to double
%32 = fsub float 5.000000e-01, %23
%33 = tail call double @fabs(float %32)
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), double %31, double %33)
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([78 x i8], [78 x i8]* @str, i64 0, i64 0))
%36 = tail call double @fabs(float %32)
%37 = tail call double @fabs(float %28)
%38 = fpext <2 x float> %10 to <2 x double>
%39 = insertelement <2 x double> poison, double %37, i64 0
%40 = insertelement <2 x double> %39, double %36, i64 1
%41 = fadd <2 x double> %40, %38
%42 = fptrunc <2 x double> %41 to <2 x float>
%43 = add nuw nsw i32 %7, 1
%44 = icmp eq i32 %43, 30
br i1 %44, label %45, label %6
45: ; preds = %6
%46 = extractelement <2 x float> %42, i64 0
%47 = fpext float %46 to double
%48 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0), double %47)
%49 = extractelement <2 x float> %42, i64 1
%50 = fpext float %49 to double
%51 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0), double %50)
%52 = fdiv float %49, %46
%53 = fmul float %52, 1.000000e+02
%54 = fptosi float %53 to i32
%55 = sub nsw i32 100, %54
%56 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i32 %55)
ret i32 0
}