; LLVM IR from ritikbanger/Hacktoberfest2022-DSA :: 01. Mathematics/maxavg.ll (Z4goodd)

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

%"class.std::vector" = type { %"struct.std::_Vector_base" }
%"struct.std::_Vector_base" = type { %"struct.std::_Vector_base<double, std::allocator<double>>::_Vector_impl" }
%"struct.std::_Vector_base<double, std::allocator<double>>::_Vector_impl" = type { %"struct.std::_Vector_base<double, std::allocator<double>>::_Vector_impl_data" }
%"struct.std::_Vector_base<double, std::allocator<double>>::_Vector_impl_data" = type { ptr, ptr, ptr }

@n = external dso_local global i64, align 8
@d = external dso_local global i64, align 8
@arr = external dso_local global %"class.std::vector", align 8
@pre = external dso_local global %"class.std::vector", align 8

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef zeroext i1 @_Z4goodd(double noundef %0) #0 {
  %2 = alloca i1, align 1
  %3 = alloca double, align 8
  %4 = alloca i64, align 8
  %5 = alloca double, align 8
  %6 = alloca i64, align 8
  %7 = alloca i32, align 4
  store double %0, ptr %3, align 8, !tbaa !5
  %8 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSt6vectorIdSaIdEEixEm(ptr noundef nonnull align 8 dereferenceable(24) @pre, i64 noundef 0) #2
  store double 0.000000e+00, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  store i64 1, ptr %4, align 8, !tbaa !9
  br label %9

9:                                                ; preds = %28, %1
  %10 = load i64, ptr %4, align 8, !tbaa !9
  %11 = load i64, ptr @n, align 8, !tbaa !9
  %12 = icmp sle i64 %10, %11
  br i1 %12, label %14, label %13

13:                                               ; preds = %9
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  br label %31

14:                                               ; preds = %9
  %15 = load i64, ptr %4, align 8, !tbaa !9
  %16 = sub nsw i64 %15, 1
  %17 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSt6vectorIdSaIdEEixEm(ptr noundef nonnull align 8 dereferenceable(24) @pre, i64 noundef %16) #2
  %18 = load double, ptr %17, align 8, !tbaa !5
  %19 = load i64, ptr %4, align 8, !tbaa !9
  %20 = sub nsw i64 %19, 1
  %21 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSt6vectorIdSaIdEEixEm(ptr noundef nonnull align 8 dereferenceable(24) @arr, i64 noundef %20) #2
  %22 = load double, ptr %21, align 8, !tbaa !5
  %23 = fadd double %18, %22
  %24 = load double, ptr %3, align 8, !tbaa !5
  %25 = fsub double %23, %24
  %26 = load i64, ptr %4, align 8, !tbaa !9
  %27 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSt6vectorIdSaIdEEixEm(ptr noundef nonnull align 8 dereferenceable(24) @pre, i64 noundef %26) #2
  store double %25, ptr %27, align 8, !tbaa !5
  br label %28

28:                                               ; preds = %14
  %29 = load i64, ptr %4, align 8, !tbaa !9
  %30 = add nsw i64 %29, 1
  store i64 %30, ptr %4, align 8, !tbaa !9
  br label %9, !llvm.loop !11

31:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  store double 0.000000e+00, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %32 = load i64, ptr @d, align 8, !tbaa !9
  store i64 %32, ptr %6, align 8, !tbaa !9
  br label %33

33:                                               ; preds = %60, %31
  %34 = load i64, ptr %6, align 8, !tbaa !9
  %35 = load i64, ptr @n, align 8, !tbaa !9
  %36 = icmp sle i64 %34, %35
  br i1 %36, label %38, label %37

37:                                               ; preds = %33
  store i32 5, ptr %7, align 4
  br label %63

38:                                               ; preds = %33
  %39 = load double, ptr %5, align 8, !tbaa !5
  %40 = load i64, ptr %6, align 8, !tbaa !9
  %41 = load i64, ptr @d, align 8, !tbaa !9
  %42 = sub nsw i64 %40, %41
  %43 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSt6vectorIdSaIdEEixEm(ptr noundef nonnull align 8 dereferenceable(24) @pre, i64 noundef %42) #2
  %44 = load double, ptr %43, align 8, !tbaa !5
  %45 = fcmp ogt double %39, %44
  br i1 %45, label %46, label %52

46:                                               ; preds = %38
  %47 = load i64, ptr %6, align 8, !tbaa !9
  %48 = load i64, ptr @d, align 8, !tbaa !9
  %49 = sub nsw i64 %47, %48
  %50 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSt6vectorIdSaIdEEixEm(ptr noundef nonnull align 8 dereferenceable(24) @pre, i64 noundef %49) #2
  %51 = load double, ptr %50, align 8, !tbaa !5
  store double %51, ptr %5, align 8, !tbaa !5
  br label %52

52:                                               ; preds = %46, %38
  %53 = load double, ptr %5, align 8, !tbaa !5
  %54 = load i64, ptr %6, align 8, !tbaa !9
  %55 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSt6vectorIdSaIdEEixEm(ptr noundef nonnull align 8 dereferenceable(24) @pre, i64 noundef %54) #2
  %56 = load double, ptr %55, align 8, !tbaa !5
  %57 = fcmp ole double %53, %56
  br i1 %57, label %58, label %59

58:                                               ; preds = %52
  store i1 true, ptr %2, align 1
  store i32 1, ptr %7, align 4
  br label %63

59:                                               ; preds = %52
  br label %60

60:                                               ; preds = %59
  %61 = load i64, ptr %6, align 8, !tbaa !9
  %62 = add nsw i64 %61, 1
  store i64 %62, ptr %6, align 8, !tbaa !9
  br label %33, !llvm.loop !13

63:                                               ; preds = %58, %37
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  %64 = load i32, ptr %7, align 4
  switch i32 %64, label %66 [
    i32 5, label %65
  ]

65:                                               ; preds = %63
  store i1 false, ptr %2, align 1
  store i32 1, ptr %7, align 4
  br label %66

66:                                               ; preds = %65, %63
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  %67 = load i1, ptr %2, align 1
  ret i1 %67
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(8) ptr @_ZNSt6vectorIdSaIdEEixEm(ptr noundef nonnull align 8 dereferenceable(24), i64 noundef) #0 align 2

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

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

attributes #0 = { mustprogress 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 nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"double", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


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

%"class.std::vector" = type { %"struct.std::_Vector_base" }
%"struct.std::_Vector_base" = type { %"struct.std::_Vector_base<double, std::allocator<double>>::_Vector_impl" }
%"struct.std::_Vector_base<double, std::allocator<double>>::_Vector_impl" = type { %"struct.std::_Vector_base<double, std::allocator<double>>::_Vector_impl_data" }
%"struct.std::_Vector_base<double, std::allocator<double>>::_Vector_impl_data" = type { ptr, ptr, ptr }

@n = external dso_local local_unnamed_addr global i64, align 8
@d = external dso_local local_unnamed_addr global i64, align 8
@arr = external dso_local global %"class.std::vector", align 8
@pre = external dso_local global %"class.std::vector", align 8

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef zeroext i1 @_Z4goodd(double noundef %0) local_unnamed_addr #0 {
  %2 = tail call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSt6vectorIdSaIdEEixEm(ptr noundef nonnull align 8 dereferenceable(24) @pre, i64 noundef 0) #1
  store double 0.000000e+00, ptr %2, align 8, !tbaa !5
  %3 = load i64, ptr @n, align 8, !tbaa !9
  %.not20 = icmp slt i64 %3, 1
  br i1 %.not20, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %1
  %4 = phi i64 [ %3, %1 ], [ %15, %.lr.ph ]
  %5 = load i64, ptr @d, align 8, !tbaa !9
  %.not1922.not = icmp sgt i64 %5, %4
  br i1 %.not1922.not, label %.critedge, label %.lr.ph27

.lr.ph:                                           ; preds = %1, %.lr.ph
  %.01621 = phi i64 [ %14, %.lr.ph ], [ 1, %1 ]
  %6 = add nsw i64 %.01621, -1
  %7 = tail call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSt6vectorIdSaIdEEixEm(ptr noundef nonnull align 8 dereferenceable(24) @pre, i64 noundef %6) #1
  %8 = load double, ptr %7, align 8, !tbaa !5
  %9 = tail call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSt6vectorIdSaIdEEixEm(ptr noundef nonnull align 8 dereferenceable(24) @arr, i64 noundef %6) #1
  %10 = load double, ptr %9, align 8, !tbaa !5
  %11 = fadd double %8, %10
  %12 = fsub double %11, %0
  %13 = tail call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSt6vectorIdSaIdEEixEm(ptr noundef nonnull align 8 dereferenceable(24) @pre, i64 noundef %.01621) #1
  store double %12, ptr %13, align 8, !tbaa !5
  %14 = add nuw nsw i64 %.01621, 1
  %15 = load i64, ptr @n, align 8, !tbaa !9
  %.not.not = icmp slt i64 %.01621, %15
  br i1 %.not.not, label %.lr.ph, label %._crit_edge, !llvm.loop !11

16:                                               ; preds = %29
  %17 = add nsw i64 %.01424, 1
  %18 = load i64, ptr @n, align 8, !tbaa !9
  %.not19 = icmp slt i64 %.01424, %18
  br i1 %.not19, label %.lr.ph27, label %.critedge, !llvm.loop !13

.lr.ph27:                                         ; preds = %._crit_edge, %16
  %.01424 = phi i64 [ %17, %16 ], [ %5, %._crit_edge ]
  %.01523 = phi double [ %.1, %16 ], [ 0.000000e+00, %._crit_edge ]
  %19 = load i64, ptr @d, align 8, !tbaa !9
  %20 = sub nsw i64 %.01424, %19
  %21 = tail call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSt6vectorIdSaIdEEixEm(ptr noundef nonnull align 8 dereferenceable(24) @pre, i64 noundef %20) #1
  %22 = load double, ptr %21, align 8, !tbaa !5
  %23 = fcmp ogt double %.01523, %22
  br i1 %23, label %24, label %29

24:                                               ; preds = %.lr.ph27
  %25 = load i64, ptr @d, align 8, !tbaa !9
  %26 = sub nsw i64 %.01424, %25
  %27 = tail call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSt6vectorIdSaIdEEixEm(ptr noundef nonnull align 8 dereferenceable(24) @pre, i64 noundef %26) #1
  %28 = load double, ptr %27, align 8, !tbaa !5
  br label %29

29:                                               ; preds = %24, %.lr.ph27
  %.1 = phi double [ %28, %24 ], [ %.01523, %.lr.ph27 ]
  %30 = tail call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSt6vectorIdSaIdEEixEm(ptr noundef nonnull align 8 dereferenceable(24) @pre, i64 noundef %.01424) #1
  %31 = load double, ptr %30, align 8, !tbaa !5
  %32 = fcmp ole double %.1, %31
  br i1 %32, label %.critedge, label %16

.critedge:                                        ; preds = %29, %16, %._crit_edge
  %.not19.lcssa = phi i1 [ false, %._crit_edge ], [ %32, %16 ], [ %32, %29 ]
  ret i1 %.not19.lcssa
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(8) ptr @_ZNSt6vectorIdSaIdEEixEm(ptr noundef nonnull align 8 dereferenceable(24), i64 noundef) local_unnamed_addr #0 align 2

attributes #0 = { mustprogress 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 = { 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 = !{!"double", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass