#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);
double frand();
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int main() {
  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() * 0.09;
  float sum_error_kalman = 0;
  float sum_error_measure = 0;
  for (int 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() * 0.09;
    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));
    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,-0x8(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0xc(%rbp)
movss  0xeb1(%rip),%xmm0
movss  %xmm0,-0x10(%rbp)
movss  0xea0(%rip),%xmm0
movss  %xmm0,-0x14(%rbp)
movss  0xe8f(%rip),%xmm0
movss  %xmm0,-0x30(%rbp)
xor    %edi,%edi
call   1040 <srand@plt>
movss  -0x30(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  %xmm0,-0x48(%rbp)
mov    $0x0,%al
call   1060 <frand@plt>
movsd  -0x48(%rbp),%xmm1
movsd  0xe51(%rip),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x8(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0x34(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
cmpl   $0x1e,-0x3c(%rbp)
jge    136a <main+0x22a>
movss  -0x8(%rbp),%xmm0
movss  %xmm0,-0x24(%rbp)
movss  -0xc(%rbp),%xmm0
addss  -0x10(%rbp),%xmm0
movss  %xmm0,-0x20(%rbp)
movss  -0x20(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movss  -0x20(%rbp),%xmm1
addss  -0x14(%rbp),%xmm1
cvtss2sd %xmm1,%xmm2
movsd  0xdef(%rip),%xmm1
divsd  %xmm2,%xmm1
mulsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x18(%rbp)
movss  -0x30(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  %xmm0,-0x58(%rbp)
mov    $0x0,%al
call   1060 <frand@plt>
movsd  -0x58(%rbp),%xmm1
movsd  0xdb4(%rip),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x2c(%rbp)
movss  -0x24(%rbp),%xmm1
movss  -0x18(%rbp),%xmm0
movss  -0x2c(%rbp),%xmm2
subss  -0x24(%rbp),%xmm2
mulss  %xmm2,%xmm0
addss  %xmm1,%xmm0
movss  %xmm0,-0x28(%rbp)
movss  0xd9a(%rip),%xmm0
subss  -0x18(%rbp),%xmm0
mulss  -0x20(%rbp),%xmm0
movss  %xmm0,-0x1c(%rbp)
cvttss2si -0x30(%rbp),%esi
lea    0xd83(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cvttss2si -0x2c(%rbp),%eax
mov    %eax,-0x50(%rbp)
movss  -0x30(%rbp),%xmm0
subss  -0x2c(%rbp),%xmm0
call   1050 <fabs@plt>
mov    -0x50(%rbp),%esi
lea    0xd76(%rip),%rdi
mov    $0x1,%al
call   1030 <printf@plt>
cvttss2si -0x28(%rbp),%eax
mov    %eax,-0x4c(%rbp)
movss  -0x30(%rbp),%xmm0
subss  -0x28(%rbp),%xmm0
call   1050 <fabs@plt>
mov    -0x4c(%rbp),%esi
lea    0xd74(%rip),%rdi
mov    $0x1,%al
call   1030 <printf@plt>
movss  -0x30(%rbp),%xmm0
subss  -0x28(%rbp),%xmm0
call   1050 <fabs@plt>
movaps %xmm0,%xmm1
movss  -0x34(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
addsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x34(%rbp)
movss  -0x30(%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  -0x1c(%rbp),%xmm0
movss  %xmm0,-0xc(%rbp)
movss  -0x28(%rbp),%xmm0
movss  %xmm0,-0x8(%rbp)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    11d7 <main+0x97>
cvttss2si -0x38(%rbp),%esi
lea    0xd15(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cvttss2si -0x34(%rbp),%esi
lea    0xd2a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movss  -0x34(%rbp),%xmm0
divss  -0x38(%rbp),%xmm0
movss  0xc7a(%rip),%xmm1
mulss  %xmm1,%xmm0
cvttss2si %xmm0,%eax
mov    $0x64,%esi
sub    %eax,%esi
lea    0xd23(%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   1040 <srand@plt>
xor    %eax,%eax
call   1060 <frand@plt>
mulsd  0xe9e(%rip),%xmm0
addsd  0xe9e(%rip),%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,0xc(%rsp)
xorps  %xmm0,%xmm0
xorpd  %xmm1,%xmm1
movss  %xmm1,0x10(%rsp)
mov    $0x1e,%ebx
lea    0xe9f(%rip),%r14
lea    0xeb3(%rip),%r15
lea    0xed2(%rip),%r12
nopl   0x0(%rax,%rax,1)
movaps %xmm0,0x30(%rsp)
movss  0x10(%rsp),%xmm1
addss  0xe65(%rip),%xmm1
movss  %xmm1,0x10(%rsp)
xorps  %xmm0,%xmm0
cvtss2sd %xmm1,%xmm0
addss  0xe54(%rip),%xmm1
cvtss2sd %xmm1,%xmm1
movsd  0xe3c(%rip),%xmm2
divsd  %xmm1,%xmm2
mulsd  %xmm0,%xmm2
xorps  %xmm0,%xmm0
cvtsd2ss %xmm2,%xmm0
movss  %xmm0,0x20(%rsp)
xor    %eax,%eax
call   1060 <frand@plt>
mulsd  0xe08(%rip),%xmm0
addsd  0xe08(%rip),%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,0x8(%rsp)
movss  0xc(%rsp),%xmm1
subss  %xmm1,%xmm0
movss  0x20(%rsp),%xmm2
mulss  %xmm2,%xmm0
addss  %xmm0,%xmm1
movss  %xmm1,0xc(%rsp)
movss  0xdf0(%rip),%xmm0
subss  %xmm2,%xmm0
movss  0x10(%rsp),%xmm1
mulss  %xmm0,%xmm1
movss  %xmm1,0x10(%rsp)
mov    %r14,%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1030 <printf@plt>
movss  0x8(%rsp),%xmm1
cvttss2si %xmm1,%ebp
movss  0xdc2(%rip),%xmm0
subss  %xmm1,%xmm0
movss  %xmm0,0x8(%rsp)
call   1050 <fabs@plt>
mov    %r15,%rdi
mov    %ebp,%esi
mov    $0x1,%al
call   1030 <printf@plt>
movss  0xc(%rsp),%xmm1
cvttss2si %xmm1,%ebp
movss  0xd95(%rip),%xmm0
subss  %xmm1,%xmm0
movss  %xmm0,0x20(%rsp)
call   1050 <fabs@plt>
mov    %r12,%rdi
mov    %ebp,%esi
mov    $0x1,%al
call   1030 <printf@plt>
movss  0x20(%rsp),%xmm0
call   1050 <fabs@plt>
movaps %xmm0,0x20(%rsp)
movss  0x8(%rsp),%xmm0
call   1050 <fabs@plt>
cvtps2pd 0x30(%rsp),%xmm1
unpcklpd 0x20(%rsp),%xmm0
addpd  %xmm1,%xmm0
cvtpd2ps %xmm0,%xmm0
add    $0xffffffff,%ebx
jne    11a0 <main+0x60>
cvttss2si %xmm0,%esi
lea    0xda7(%rip),%rdi
xor    %eax,%eax
movaps %xmm0,0x30(%rsp)
call   1030 <printf@plt>
movaps 0x30(%rsp),%xmm0
shufps $0x55,%xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
cvttss2si %xmm0,%esi
lea    0xdaa(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
movaps 0x10(%rsp),%xmm0
divss  0x30(%rsp),%xmm0
mulss  0xcfd(%rip),%xmm0
cvttss2si %xmm0,%eax
mov    $0x64,%esi
sub    %eax,%esi
lea    0xda6(%rip),%rdi
xor    %eax,%eax
call   1030 <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 = [40 x i8] c"Total error if using raw measured: %f\0A\00"
@.str.4 = [40 x i8] c"Total error if using kalman filter: %f\0A\00"
@.str.5 = [27 x i8] c"Reduction in error: %d%% \0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca float
%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 i32
store i32 0, i32* %1
store float 0.000000e+00, float* %2
store float 0.000000e+00, float* %3
store float 0x3F96872B00000000, float* %4
store float 0x3FE3BE76C0000000, float* %5
store float 5.000000e-01, float* %12
%16 = call i32 @srand(i32 0)
%17 = load float, float* %12
%18 = fpext float %17 to double
%19 = call double (...) @frand()
%20 = call double @llvm.fmuladd.f64(double %19, double 0x3FB70A3D70A3D70A, double %18)
%21 = fptrunc double %20 to float
store float %21, float* %2
store float 0.000000e+00, float* %13
store float 0.000000e+00, float* %14
store i32 0, i32* %15
br label %22
22: ; preds = %89, %0
%23 = load i32, i32* %15
%24 = icmp slt i32 %23, 30
br i1 %24, label %25, label %92
25: ; preds = %22
%26 = load float, float* %2
store float %26, float* %9
%27 = load float, float* %3
%28 = load float, float* %4
%29 = fadd float %27, %28
store float %29, float* %8
%30 = load float, float* %8
%31 = fpext float %30 to double
%32 = load float, float* %8
%33 = load float, float* %5
%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* %6
%39 = load float, float* %12
%40 = fpext float %39 to double
%41 = call double (...) @frand()
%42 = call double @llvm.fmuladd.f64(double %41, double 0x3FB70A3D70A3D70A, double %40)
%43 = fptrunc double %42 to float
store float %43, float* %11
%44 = load float, float* %9
%45 = load float, float* %6
%46 = load float, float* %11
%47 = load float, float* %9
%48 = fsub float %46, %47
%49 = call float @llvm.fmuladd.f32(float %45, float %48, float %44)
store float %49, float* %10
%50 = load float, float* %6
%51 = fsub float 1.000000e+00, %50
%52 = load float, float* %8
%53 = fmul float %51, %52
store float %53, float* %7
%54 = load float, float* %12
%55 = fptosi float %54 to i32
%56 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %55)
%57 = load float, float* %11
%58 = fptosi float %57 to i32
%59 = load float, float* %12
%60 = load float, float* %11
%61 = fsub float %59, %60
%62 = call double @fabs(float %61)
%63 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %58, double %62)
%64 = load float, float* %10
%65 = fptosi float %64 to i32
%66 = load float, float* %12
%67 = load float, float* %10
%68 = fsub float %66, %67
%69 = call double @fabs(float %68)
%70 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i32 %65, double %69)
%71 = load float, float* %12
%72 = load float, float* %10
%73 = fsub float %71, %72
%74 = call double @fabs(float %73)
%75 = load float, float* %13
%76 = fpext float %75 to double
%77 = fadd double %76, %74
%78 = fptrunc double %77 to float
store float %78, float* %13
%79 = load float, float* %12
%80 = load float, float* %11
%81 = fsub float %79, %80
%82 = call double @fabs(float %81)
%83 = load float, float* %14
%84 = fpext float %83 to double
%85 = fadd double %84, %82
%86 = fptrunc double %85 to float
store float %86, float* %14
%87 = load float, float* %7
store float %87, float* %3
%88 = load float, float* %10
store float %88, float* %2
br label %89
89: ; preds = %25
%90 = load i32, i32* %15
%91 = add nsw i32 %90, 1
store i32 %91, i32* %15
br label %22
92: ; preds = %22
%93 = load float, float* %14
%94 = fptosi float %93 to i32
%95 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i32 %94)
%96 = load float, float* %13
%97 = fptosi float %96 to i32
%98 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0), i32 %97)
%99 = load float, float* %13
%100 = load float, float* %14
%101 = fdiv float %99, %100
%102 = fmul float %101, 1.000000e+02
%103 = fptosi float %102 to i32
%104 = sub nsw i32 100, %103
%105 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i32 %104)
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.3 = [40 x i8] c"Total error if using raw measured: %f\0A\00"
@.str.4 = [40 x i8] c"Total error if using kalman filter: %f\0A\00"
@.str.5 = [27 x i8] c"Reduction in error: %d%% \0A\00"
define i32 @main() {
%1 = tail call i32 @srand(i32 0)
%2 = tail call double (...) @frand()
%3 = tail call double @llvm.fmuladd.f64(double %2, double 0x3FB70A3D70A3D70A, double 5.000000e-01)
%4 = fptrunc double %3 to float
br label %17
5: ; preds = %17
%6 = extractelement <2 x float> %51, i64 0
%7 = fptosi float %6 to i32
%8 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i32 %7)
%9 = extractelement <2 x float> %51, i64 1
%10 = fptosi float %9 to i32
%11 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0), i32 %10)
%12 = fdiv float %9, %6
%13 = fmul float %12, 1.000000e+02
%14 = fptosi float %13 to i32
%15 = sub nsw i32 100, %14
%16 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i32 %15)
ret i32 0
17: ; preds = %0, %17
%18 = phi i32 [ 0, %0 ], [ %52, %17 ]
%19 = phi float [ %4, %0 ], [ %33, %17 ]
%20 = phi float [ 0.000000e+00, %0 ], [ %35, %17 ]
%21 = phi <2 x float> [ zeroinitializer, %0 ], [ %51, %17 ]
%22 = fadd float %20, 0x3F96872B00000000
%23 = fpext float %22 to double
%24 = fadd float %22, 0x3FE3BE76C0000000
%25 = fpext float %24 to double
%26 = fdiv double 1.000000e+00, %25
%27 = fmul double %26, %23
%28 = fptrunc double %27 to float
%29 = tail call double (...) @frand()
%30 = tail call double @llvm.fmuladd.f64(double %29, double 0x3FB70A3D70A3D70A, double 5.000000e-01)
%31 = fptrunc double %30 to float
%32 = fsub float %31, %19
%33 = tail call float @llvm.fmuladd.f32(float %28, float %32, float %19)
%34 = fsub float 1.000000e+00, %28
%35 = fmul float %22, %34
%36 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 0)
%37 = fptosi float %31 to i32
%38 = fsub float 5.000000e-01, %31
%39 = tail call double @fabs(float %38)
%40 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %37, double %39)
%41 = fptosi float %33 to i32
%42 = fsub float 5.000000e-01, %33
%43 = tail call double @fabs(float %42)
%44 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i32 %41, double %43)
%45 = tail call double @fabs(float %42)
%46 = tail call double @fabs(float %38)
%47 = fpext <2 x float> %21 to <2 x double>
%48 = insertelement <2 x double> poison, double %46, i64 0
%49 = insertelement <2 x double> %48, double %45, i64 1
%50 = fadd <2 x double> %49, %47
%51 = fptrunc <2 x double> %50 to <2 x float>
%52 = add nuw nsw i32 %18, 1
%53 = icmp eq i32 %52, 30
br i1 %53, label %5, label %17
}