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

; 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"

@MovingWindowIntegral.x = external hidden global [72 x i32], align 16
@MovingWindowIntegral.ptr = external hidden global i32, align 4
@MovingWindowIntegral.sum = external hidden global i64, align 8

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nounwind uwtable
define dso_local i32 @MovingWindowIntegral(i32 noundef %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca i64, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  %6 = load i32, ptr @MovingWindowIntegral.ptr, align 4, !tbaa !5
  %7 = add nsw i32 %6, 1
  store i32 %7, ptr @MovingWindowIntegral.ptr, align 4, !tbaa !5
  %8 = icmp eq i32 %7, 72
  br i1 %8, label %9, label %10

9:                                                ; preds = %1
  store i32 0, ptr @MovingWindowIntegral.ptr, align 4, !tbaa !5
  br label %10

10:                                               ; preds = %9, %1
  %11 = load i32, ptr @MovingWindowIntegral.ptr, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [72 x i32], ptr @MovingWindowIntegral.x, i64 0, i64 %12
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %15 = sext i32 %14 to i64
  %16 = load i64, ptr @MovingWindowIntegral.sum, align 8, !tbaa !9
  %17 = sub nsw i64 %16, %15
  store i64 %17, ptr @MovingWindowIntegral.sum, align 8, !tbaa !9
  %18 = load i32, ptr %2, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = load i64, ptr @MovingWindowIntegral.sum, align 8, !tbaa !9
  %21 = add nsw i64 %20, %19
  store i64 %21, ptr @MovingWindowIntegral.sum, align 8, !tbaa !9
  %22 = load i32, ptr %2, align 4, !tbaa !5
  %23 = load i32, ptr @MovingWindowIntegral.ptr, align 4, !tbaa !5
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [72 x i32], ptr @MovingWindowIntegral.x, i64 0, i64 %24
  store i32 %22, ptr %25, align 4, !tbaa !5
  %26 = load i64, ptr @MovingWindowIntegral.sum, align 8, !tbaa !9
  %27 = ashr i64 %26, 5
  store i64 %27, ptr %3, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  store i32 4096, ptr %5, align 4, !tbaa !5
  %28 = load i64, ptr %3, align 8, !tbaa !9
  %29 = load i32, ptr %5, align 4, !tbaa !5
  %30 = zext i32 %29 to i64
  %31 = icmp sgt i64 %28, %30
  br i1 %31, label %32, label %34

32:                                               ; preds = %10
  %33 = load i32, ptr %5, align 4, !tbaa !5
  store i32 %33, ptr %4, align 4, !tbaa !5
  br label %37

34:                                               ; preds = %10
  %35 = load i64, ptr %3, align 8, !tbaa !9
  %36 = trunc i64 %35 to i32
  store i32 %36, ptr %4, align 4, !tbaa !5
  br label %37

37:                                               ; preds = %34, %32
  %38 = load i32, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  ret i32 %38
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { nounwind }

!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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}


; 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"

@MovingWindowIntegral.x = external hidden local_unnamed_addr global [72 x i32], align 16
@MovingWindowIntegral.ptr = external hidden local_unnamed_addr global i32, align 4
@MovingWindowIntegral.sum = external hidden local_unnamed_addr global i64, align 8

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local i32 @MovingWindowIntegral(i32 noundef %0) local_unnamed_addr #0 {
  %2 = load i32, ptr @MovingWindowIntegral.ptr, align 4, !tbaa !5
  %3 = add nsw i32 %2, 1
  %4 = icmp eq i32 %3, 72
  %spec.store.select = select i1 %4, i32 0, i32 %3
  store i32 %spec.store.select, ptr @MovingWindowIntegral.ptr, align 4
  %5 = sext i32 %spec.store.select to i64
  %6 = getelementptr inbounds [72 x i32], ptr @MovingWindowIntegral.x, i64 0, i64 %5
  %7 = load i32, ptr %6, align 4, !tbaa !5
  %8 = sext i32 %7 to i64
  %9 = load i64, ptr @MovingWindowIntegral.sum, align 8, !tbaa !9
  %10 = sub nsw i64 %9, %8
  %11 = sext i32 %0 to i64
  %12 = add nsw i64 %10, %11
  store i64 %12, ptr @MovingWindowIntegral.sum, align 8, !tbaa !9
  store i32 %0, ptr %6, align 4, !tbaa !5
  %13 = ashr i64 %12, 5
  %.08 = tail call i64 @llvm.smin.i64(i64 %13, i64 4096)
  %.0 = trunc i64 %.08 to i32
  ret i32 %.0
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.smin.i64(i64, i64) #1

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(readwrite, argmem: none, 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" }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass