; LLVM IR from bbenligiray/stag :: src/Ellipse.ll (ZN13customEllipse22GetAverageFittingErrorEv)

; 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.pix = type { i32, i32 }
%class.customEllipse = type { double, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double, i32, ptr, ptr, ptr, ptr, ptr, ptr }

; 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 allocsize(0)
declare noalias ptr @malloc(i64 noundef) #1

; 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) #2

; Function Attrs: mustprogress uwtable
declare dso_local noundef double @_ZN13customEllipse11GetDistanceE3pixRd(ptr noundef nonnull align 8 dereferenceable(272), i64, ptr noundef nonnull align 8 dereferenceable(8)) #3 align 2

; Function Attrs: mustprogress uwtable
declare dso_local noundef double @_ZN13customEllipse11GetDistanceEddRd(ptr noundef nonnull align 8 dereferenceable(272), double noundef, double noundef, ptr noundef nonnull align 8 dereferenceable(8)) #3 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef double @_ZN13customEllipse22GetAverageFittingErrorEv(ptr noundef nonnull align 8 dereferenceable(272) %0) #3 align 2 {
  %2 = alloca double, align 8
  %3 = alloca ptr, align 8
  %4 = alloca double, align 8
  %5 = alloca i32, align 4
  %6 = alloca %struct.pix, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  %7 = load ptr, ptr %3, align 8
  %8 = getelementptr inbounds %class.customEllipse, ptr %7, i32 0, i32 24
  %9 = load double, ptr %8, align 8, !tbaa !9
  %10 = fcmp une double %9, 0.000000e+00
  br i1 %10, label %11, label %14

11:                                               ; preds = %1
  %12 = getelementptr inbounds %class.customEllipse, ptr %7, i32 0, i32 24
  %13 = load double, ptr %12, align 8, !tbaa !9
  store double %13, ptr %2, align 8
  br label %84

14:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  %15 = getelementptr inbounds %class.customEllipse, ptr %7, i32 0, i32 27
  %16 = load i32, ptr %15, align 8, !tbaa !13
  %17 = sext i32 %16 to i64
  %18 = mul i64 %17, 8
  %19 = call noalias ptr @malloc(i64 noundef %18) #5
  %20 = getelementptr inbounds %class.customEllipse, ptr %7, i32 0, i32 30
  store ptr %19, ptr %20, align 8, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  store i32 0, ptr %5, align 4, !tbaa !15
  br label %21

21:                                               ; preds = %72, %14
  %22 = load i32, ptr %5, align 4, !tbaa !15
  %23 = getelementptr inbounds %class.customEllipse, ptr %7, i32 0, i32 27
  %24 = load i32, ptr %23, align 8, !tbaa !13
  %25 = icmp slt i32 %22, %24
  br i1 %25, label %27, label %26

26:                                               ; preds = %21
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  br label %75

27:                                               ; preds = %21
  %28 = getelementptr inbounds %class.customEllipse, ptr %7, i32 0, i32 31
  %29 = load ptr, ptr %28, align 8, !tbaa !16
  %30 = icmp ne ptr %29, null
  br i1 %30, label %31, label %48

31:                                               ; preds = %27
  %32 = getelementptr inbounds %class.customEllipse, ptr %7, i32 0, i32 31
  %33 = load ptr, ptr %32, align 8, !tbaa !16
  %34 = load i32, ptr %5, align 4, !tbaa !15
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds %struct.pix, ptr %33, i64 %35
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %6, ptr align 4 %36, i64 8, i1 false), !tbaa.struct !17
  %37 = load i64, ptr %6, align 4
  %38 = call noundef double @_ZN13customEllipse11GetDistanceE3pixRd(ptr noundef nonnull align 8 dereferenceable(272) %7, i64 %37, ptr noundef nonnull align 8 dereferenceable(8) %4)
  %39 = getelementptr inbounds %class.customEllipse, ptr %7, i32 0, i32 24
  %40 = load double, ptr %39, align 8, !tbaa !9
  %41 = fadd double %40, %38
  store double %41, ptr %39, align 8, !tbaa !9
  %42 = load double, ptr %4, align 8, !tbaa !18
  %43 = getelementptr inbounds %class.customEllipse, ptr %7, i32 0, i32 30
  %44 = load ptr, ptr %43, align 8, !tbaa !14
  %45 = load i32, ptr %5, align 4, !tbaa !15
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds double, ptr %44, i64 %46
  store double %42, ptr %47, align 8, !tbaa !18
  br label %71

48:                                               ; preds = %27
  %49 = getelementptr inbounds %class.customEllipse, ptr %7, i32 0, i32 28
  %50 = load ptr, ptr %49, align 8, !tbaa !19
  %51 = load i32, ptr %5, align 4, !tbaa !15
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds double, ptr %50, i64 %52
  %54 = load double, ptr %53, align 8, !tbaa !18
  %55 = getelementptr inbounds %class.customEllipse, ptr %7, i32 0, i32 29
  %56 = load ptr, ptr %55, align 8, !tbaa !20
  %57 = load i32, ptr %5, align 4, !tbaa !15
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds double, ptr %56, i64 %58
  %60 = load double, ptr %59, align 8, !tbaa !18
  %61 = call noundef double @_ZN13customEllipse11GetDistanceEddRd(ptr noundef nonnull align 8 dereferenceable(272) %7, double noundef %54, double noundef %60, ptr noundef nonnull align 8 dereferenceable(8) %4)
  %62 = getelementptr inbounds %class.customEllipse, ptr %7, i32 0, i32 24
  %63 = load double, ptr %62, align 8, !tbaa !9
  %64 = fadd double %63, %61
  store double %64, ptr %62, align 8, !tbaa !9
  %65 = load double, ptr %4, align 8, !tbaa !18
  %66 = getelementptr inbounds %class.customEllipse, ptr %7, i32 0, i32 30
  %67 = load ptr, ptr %66, align 8, !tbaa !14
  %68 = load i32, ptr %5, align 4, !tbaa !15
  %69 = sext i32 %68 to i64
  %70 = getelementptr inbounds double, ptr %67, i64 %69
  store double %65, ptr %70, align 8, !tbaa !18
  br label %71

71:                                               ; preds = %48, %31
  br label %72

72:                                               ; preds = %71
  %73 = load i32, ptr %5, align 4, !tbaa !15
  %74 = add nsw i32 %73, 1
  store i32 %74, ptr %5, align 4, !tbaa !15
  br label %21, !llvm.loop !21

75:                                               ; preds = %26
  %76 = getelementptr inbounds %class.customEllipse, ptr %7, i32 0, i32 27
  %77 = load i32, ptr %76, align 8, !tbaa !13
  %78 = sitofp i32 %77 to double
  %79 = getelementptr inbounds %class.customEllipse, ptr %7, i32 0, i32 24
  %80 = load double, ptr %79, align 8, !tbaa !9
  %81 = fdiv double %80, %78
  store double %81, ptr %79, align 8, !tbaa !9
  %82 = getelementptr inbounds %class.customEllipse, ptr %7, i32 0, i32 24
  %83 = load double, ptr %82, align 8, !tbaa !9
  store double %83, ptr %2, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  br label %84

84:                                               ; preds = %75, %11
  %85 = load double, ptr %2, align 8
  ret double %85
}

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

!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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !11, i64 192}
!10 = !{!"_ZTS13customEllipse", !11, i64 0, !11, i64 8, !11, i64 16, !11, i64 24, !11, i64 32, !11, i64 40, !11, i64 48, !11, i64 56, !11, i64 64, !11, i64 72, !11, i64 80, !11, i64 88, !11, i64 96, !11, i64 104, !11, i64 112, !11, i64 120, !11, i64 128, !11, i64 136, !11, i64 144, !11, i64 152, !11, i64 160, !11, i64 168, !11, i64 176, !11, i64 184, !11, i64 192, !11, i64 200, !11, i64 208, !12, i64 216, !6, i64 224, !6, i64 232, !6, i64 240, !6, i64 248, !6, i64 256, !6, i64 264}
!11 = !{!"double", !7, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!10, !12, i64 216}
!14 = !{!10, !6, i64 240}
!15 = !{!12, !12, i64 0}
!16 = !{!10, !6, i64 248}
!17 = !{i64 0, i64 4, !15, i64 4, i64 4, !15}
!18 = !{!11, !11, i64 0}
!19 = !{!10, !6, i64 224}
!20 = !{!10, !6, i64 232}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}


; 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.pix = type { i32, i32 }

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

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

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress uwtable
declare dso_local noundef double @_ZN13customEllipse11GetDistanceE3pixRd(ptr noundef nonnull align 8 dereferenceable(272), i64, ptr noundef nonnull align 8 dereferenceable(8)) local_unnamed_addr #2 align 2

; Function Attrs: mustprogress uwtable
declare dso_local noundef double @_ZN13customEllipse11GetDistanceEddRd(ptr noundef nonnull align 8 dereferenceable(272), double noundef, double noundef, ptr noundef nonnull align 8 dereferenceable(8)) local_unnamed_addr #2 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef double @_ZN13customEllipse22GetAverageFittingErrorEv(ptr noundef nonnull align 8 dereferenceable(272) %0) local_unnamed_addr #2 align 2 {
  %2 = alloca double, align 8
  %3 = getelementptr inbounds i8, ptr %0, i64 192
  %4 = load double, ptr %3, align 8, !tbaa !5
  %5 = fcmp une double %4, 0.000000e+00
  br i1 %5, label %41, label %6

6:                                                ; preds = %1
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2) #3
  %7 = getelementptr inbounds i8, ptr %0, i64 216
  %8 = load i32, ptr %7, align 8, !tbaa !12
  %9 = sext i32 %8 to i64
  %10 = shl nsw i64 %9, 3
  %11 = tail call noalias ptr @malloc(i64 noundef %10) #4
  %12 = getelementptr inbounds i8, ptr %0, i64 240
  store ptr %11, ptr %12, align 8, !tbaa !13
  %13 = icmp sgt i32 %8, 0
  br i1 %13, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %6
  %14 = getelementptr inbounds i8, ptr %0, i64 248
  %15 = getelementptr inbounds i8, ptr %0, i64 224
  %16 = getelementptr inbounds i8, ptr %0, i64 232
  br label %20

._crit_edge.loopexit:                             ; preds = %33
  %.pre = load double, ptr %3, align 8, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %6
  %17 = phi double [ %4, %6 ], [ %.pre, %._crit_edge.loopexit ]
  %.lcssa = phi i32 [ %8, %6 ], [ %38, %._crit_edge.loopexit ]
  %18 = sitofp i32 %.lcssa to double
  %19 = fdiv double %17, %18
  store double %19, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2) #3
  br label %41

20:                                               ; preds = %.lr.ph, %33
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %33 ]
  %21 = load ptr, ptr %14, align 8, !tbaa !14
  %.not = icmp eq ptr %21, null
  br i1 %.not, label %25, label %22

22:                                               ; preds = %20
  %23 = getelementptr inbounds %struct.pix, ptr %21, i64 %indvars.iv
  %.sroa.0.0.copyload = load i64, ptr %23, align 4
  %24 = call noundef double @_ZN13customEllipse11GetDistanceE3pixRd(ptr noundef nonnull align 8 dereferenceable(272) %0, i64 %.sroa.0.0.copyload, ptr noundef nonnull align 8 dereferenceable(8) %2)
  br label %33

25:                                               ; preds = %20
  %26 = load ptr, ptr %15, align 8, !tbaa !15
  %27 = getelementptr inbounds double, ptr %26, i64 %indvars.iv
  %28 = load double, ptr %27, align 8, !tbaa !16
  %29 = load ptr, ptr %16, align 8, !tbaa !17
  %30 = getelementptr inbounds double, ptr %29, i64 %indvars.iv
  %31 = load double, ptr %30, align 8, !tbaa !16
  %32 = call noundef double @_ZN13customEllipse11GetDistanceEddRd(ptr noundef nonnull align 8 dereferenceable(272) %0, double noundef %28, double noundef %31, ptr noundef nonnull align 8 dereferenceable(8) %2)
  br label %33

33:                                               ; preds = %22, %25
  %.sink19 = phi double [ %24, %22 ], [ %32, %25 ]
  %34 = load double, ptr %3, align 8, !tbaa !5
  %35 = fadd double %.sink19, %34
  store double %35, ptr %3, align 8, !tbaa !5
  %.sink = load double, ptr %2, align 8, !tbaa !16
  %36 = load ptr, ptr %12, align 8, !tbaa !13
  %37 = getelementptr inbounds double, ptr %36, i64 %indvars.iv
  store double %.sink, ptr %37, align 8, !tbaa !16
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %38 = load i32, ptr %7, align 8, !tbaa !12
  %39 = sext i32 %38 to i64
  %40 = icmp slt i64 %indvars.iv.next, %39
  br i1 %40, label %20, label %._crit_edge.loopexit, !llvm.loop !18

41:                                               ; preds = %1, %._crit_edge
  %.0 = phi double [ %19, %._crit_edge ], [ %4, %1 ]
  ret double %.0
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { mustprogress 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 #3 = { nounwind }
attributes #4 = { nounwind allocsize(0) }

!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, !7, i64 192}
!6 = !{!"_ZTS13customEllipse", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !7, i64 32, !7, i64 40, !7, i64 48, !7, i64 56, !7, i64 64, !7, i64 72, !7, i64 80, !7, i64 88, !7, i64 96, !7, i64 104, !7, i64 112, !7, i64 120, !7, i64 128, !7, i64 136, !7, i64 144, !7, i64 152, !7, i64 160, !7, i64 168, !7, i64 176, !7, i64 184, !7, i64 192, !7, i64 200, !7, i64 208, !10, i64 216, !11, i64 224, !11, i64 232, !11, i64 240, !11, i64 248, !11, i64 256, !11, i64 264}
!7 = !{!"double", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"any pointer", !8, i64 0}
!12 = !{!6, !10, i64 216}
!13 = !{!6, !11, i64 240}
!14 = !{!6, !11, i64 248}
!15 = !{!6, !11, i64 224}
!16 = !{!7, !7, i64 0}
!17 = !{!6, !11, i64 232}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass