; LLVM IR from GCY/wxECGAnalyzer :: embedded/stm32f407 - 360Hz/src/stm32f4/algorithm (adaptive_algorithm)

; Unoptimized IR (clang -disable-llvm-passes):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

%struct.SignalPoint = type { float, i32 }

@current_max = external hidden global float, align 4
@current_min = external hidden global float, align 4
@is_detecting_emi = external hidden global i8, align 1
@max_point = external hidden global i32, align 4
@min_point = external hidden global i32, align 4
@peak = external hidden global %struct.SignalPoint, align 4

; Function Attrs: nounwind uwtable
define dso_local i64 @PeakDetect(float noundef %0, i32 noundef %1, float noundef %2, ptr noundef %3) #0 {
  %5 = alloca %struct.SignalPoint, align 4
  %6 = alloca float, align 4
  %7 = alloca i32, align 4
  %8 = alloca float, align 4
  %9 = alloca ptr, align 8
  store float %0, ptr %6, align 4, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !9
  store float %2, ptr %8, align 4, !tbaa !5
  store ptr %3, ptr %9, align 8, !tbaa !11
  %10 = load float, ptr %6, align 4, !tbaa !5
  %11 = load float, ptr @current_max, align 4, !tbaa !5
  %12 = fcmp ogt float %10, %11
  br i1 %12, label %13, label %16

13:                                               ; preds = %4
  %14 = load i32, ptr %7, align 4, !tbaa !9
  store i32 %14, ptr @max_point, align 4, !tbaa !9
  %15 = load float, ptr %6, align 4, !tbaa !5
  store float %15, ptr @current_max, align 4, !tbaa !5
  br label %16

16:                                               ; preds = %13, %4
  %17 = load float, ptr %6, align 4, !tbaa !5
  %18 = load float, ptr @current_min, align 4, !tbaa !5
  %19 = fcmp olt float %17, %18
  br i1 %19, label %20, label %23

20:                                               ; preds = %16
  %21 = load i32, ptr %7, align 4, !tbaa !9
  store i32 %21, ptr @min_point, align 4, !tbaa !9
  %22 = load float, ptr %6, align 4, !tbaa !5
  store float %22, ptr @current_min, align 4, !tbaa !5
  br label %23

23:                                               ; preds = %20, %16
  %24 = load i8, ptr @is_detecting_emi, align 1, !tbaa !13, !range !15, !noundef !16
  %25 = trunc i8 %24 to i1
  br i1 %25, label %26, label %38

26:                                               ; preds = %23
  %27 = load float, ptr %6, align 4, !tbaa !5
  %28 = load float, ptr @current_max, align 4, !tbaa !5
  %29 = load float, ptr %8, align 4, !tbaa !5
  %30 = fsub float %28, %29
  %31 = fcmp olt float %27, %30
  br i1 %31, label %32, label %38

32:                                               ; preds = %26
  store i8 0, ptr @is_detecting_emi, align 1, !tbaa !13
  %33 = load float, ptr @current_max, align 4, !tbaa !5
  store float %33, ptr @current_min, align 4, !tbaa !5
  %34 = load i32, ptr @max_point, align 4, !tbaa !9
  store i32 %34, ptr @min_point, align 4, !tbaa !9
  %35 = load ptr, ptr %9, align 8, !tbaa !11
  store i8 1, ptr %35, align 1, !tbaa !13
  %36 = load float, ptr @current_max, align 4, !tbaa !5
  store float %36, ptr @peak, align 4, !tbaa !17
  %37 = load i32, ptr @max_point, align 4, !tbaa !9
  store i32 %37, ptr getelementptr inbounds (%struct.SignalPoint, ptr @peak, i32 0, i32 1), align 4, !tbaa !19
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %5, ptr align 4 @peak, i64 8, i1 false), !tbaa.struct !20
  br label %55

38:                                               ; preds = %26, %23
  %39 = load i8, ptr @is_detecting_emi, align 1, !tbaa !13, !range !15, !noundef !16
  %40 = trunc i8 %39 to i1
  br i1 %40, label %53, label %41

41:                                               ; preds = %38
  %42 = load float, ptr %6, align 4, !tbaa !5
  %43 = load float, ptr @current_min, align 4, !tbaa !5
  %44 = load float, ptr %8, align 4, !tbaa !5
  %45 = fadd float %43, %44
  %46 = fcmp ogt float %42, %45
  br i1 %46, label %47, label %53

47:                                               ; preds = %41
  store i8 1, ptr @is_detecting_emi, align 1, !tbaa !13
  %48 = load float, ptr @current_min, align 4, !tbaa !5
  store float %48, ptr @current_max, align 4, !tbaa !5
  %49 = load i32, ptr @min_point, align 4, !tbaa !9
  store i32 %49, ptr @max_point, align 4, !tbaa !9
  %50 = load ptr, ptr %9, align 8, !tbaa !11
  store i8 0, ptr %50, align 1, !tbaa !13
  %51 = load float, ptr @current_min, align 4, !tbaa !5
  store float %51, ptr @peak, align 4, !tbaa !17
  %52 = load i32, ptr @min_point, align 4, !tbaa !9
  store i32 %52, ptr getelementptr inbounds (%struct.SignalPoint, ptr @peak, i32 0, i32 1), align 4, !tbaa !19
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %5, ptr align 4 @peak, i64 8, i1 false), !tbaa.struct !20
  br label %55

53:                                               ; preds = %41, %38
  br label %54

54:                                               ; preds = %53
  store i32 -1, ptr getelementptr inbounds (%struct.SignalPoint, ptr @peak, i32 0, i32 1), align 4, !tbaa !19
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %5, ptr align 4 @peak, i64 8, i1 false), !tbaa.struct !20
  br label %55

55:                                               ; preds = %54, %47, %32
  %56 = load i64, ptr %5, align 4
  ret i64 %56
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

attributes #0 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"float", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"_Bool", !7, i64 0}
!15 = !{i8 0, i8 2}
!16 = !{}
!17 = !{!18, !6, i64 0}
!18 = !{!"", !6, i64 0, !10, i64 4}
!19 = !{!18, !10, i64 4}
!20 = !{i64 0, i64 4, !5, i64 4, i64 4, !9}


; Optimized IR (opt -passes='default<O3>'):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

%struct.SignalPoint = type { float, i32 }

@current_max = external hidden local_unnamed_addr global float, align 4
@current_min = external hidden local_unnamed_addr global float, align 4
@is_detecting_emi = external hidden local_unnamed_addr global i8, align 1
@max_point = external hidden local_unnamed_addr global i32, align 4
@min_point = external hidden local_unnamed_addr global i32, align 4
@peak = external hidden local_unnamed_addr global %struct.SignalPoint, align 4

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(readwrite, inaccessiblemem: none) uwtable
define dso_local i64 @PeakDetect(float noundef %0, i32 noundef %1, float noundef %2, ptr nocapture noundef writeonly %3) local_unnamed_addr #0 {
  %5 = load float, ptr @current_max, align 4, !tbaa !5
  %6 = fcmp olt float %5, %0
  br i1 %6, label %7, label %8

7:                                                ; preds = %4
  store i32 %1, ptr @max_point, align 4, !tbaa !9
  store float %0, ptr @current_max, align 4, !tbaa !5
  br label %8

8:                                                ; preds = %7, %4
  %9 = phi float [ %0, %7 ], [ %5, %4 ]
  %10 = load float, ptr @current_min, align 4, !tbaa !5
  %11 = fcmp ogt float %10, %0
  br i1 %11, label %12, label %13

12:                                               ; preds = %8
  store i32 %1, ptr @min_point, align 4, !tbaa !9
  store float %0, ptr @current_min, align 4, !tbaa !5
  br label %13

13:                                               ; preds = %12, %8
  %14 = phi float [ %0, %12 ], [ %10, %8 ]
  %15 = load i8, ptr @is_detecting_emi, align 1, !tbaa !11, !range !13, !noundef !14
  %16 = trunc nuw i8 %15 to i1
  br i1 %16, label %17, label %21

17:                                               ; preds = %13
  %18 = fsub float %9, %2
  %19 = fcmp ogt float %18, %0
  br i1 %19, label %20, label %.thread

20:                                               ; preds = %17
  store i8 0, ptr @is_detecting_emi, align 1, !tbaa !11
  store float %9, ptr @current_min, align 4, !tbaa !5
  br label %.thread.sink.split

21:                                               ; preds = %13
  %22 = fadd float %14, %2
  %23 = fcmp olt float %22, %0
  br i1 %23, label %24, label %.thread

24:                                               ; preds = %21
  store i8 1, ptr @is_detecting_emi, align 1, !tbaa !11
  store float %14, ptr @current_max, align 4, !tbaa !5
  br label %.thread.sink.split

.thread.sink.split:                               ; preds = %20, %24
  %min_point.sink = phi ptr [ @min_point, %24 ], [ @max_point, %20 ]
  %max_point.sink = phi ptr [ @max_point, %24 ], [ @min_point, %20 ]
  %.sink14 = phi i8 [ 0, %24 ], [ 1, %20 ]
  %.sink13 = phi float [ %14, %24 ], [ %9, %20 ]
  %25 = load i32, ptr %min_point.sink, align 4, !tbaa !9
  store i32 %25, ptr %max_point.sink, align 4, !tbaa !9
  store i8 %.sink14, ptr %3, align 1, !tbaa !11
  store float %.sink13, ptr @peak, align 4, !tbaa !15
  br label %.thread

.thread:                                          ; preds = %.thread.sink.split, %21, %17
  %.sink = phi i32 [ -1, %17 ], [ -1, %21 ], [ %25, %.thread.sink.split ]
  store i32 %.sink, ptr getelementptr inbounds (i8, ptr @peak, i64 4), align 4, !tbaa !17
  %.sroa.0.0 = load i64, ptr @peak, align 4
  ret i64 %.sroa.0.0
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(readwrite, inaccessiblemem: none) uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"float", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"_Bool", !7, i64 0}
!13 = !{i8 0, i8 2}
!14 = !{}
!15 = !{!16, !6, i64 0}
!16 = !{!"", !6, i64 0, !10, i64 4}
!17 = !{!16, !10, i64 4}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass