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

; 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: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.fmuladd.f64(double, double, double) #1

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #2

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

; Function Attrs: nounwind
declare double @cos(double noundef) #4

; Function Attrs: nounwind
declare double @sin(double noundef) #4

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

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_ZN13customEllipse16GetClosestPointsEv(ptr noundef nonnull align 8 dereferenceable(272) %0) #5 align 2 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca %struct.pix, align 4
  %5 = alloca double, align 8
  %6 = alloca double, align 8
  %7 = alloca double, align 8
  %8 = alloca double, align 8
  %9 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  %10 = load ptr, ptr %3, align 8
  %11 = getelementptr inbounds %class.customEllipse, ptr %10, i32 0, i32 33
  %12 = load ptr, ptr %11, align 8, !tbaa !9
  %13 = icmp ne ptr %12, null
  br i1 %13, label %14, label %17

14:                                               ; preds = %1
  %15 = getelementptr inbounds %class.customEllipse, ptr %10, i32 0, i32 33
  %16 = load ptr, ptr %15, align 8, !tbaa !9
  store ptr %16, ptr %2, align 8
  br label %93

17:                                               ; preds = %1
  %18 = getelementptr inbounds %class.customEllipse, ptr %10, i32 0, i32 27
  %19 = load i32, ptr %18, align 8, !tbaa !13
  %20 = sext i32 %19 to i64
  %21 = mul i64 %20, 8
  %22 = call noalias ptr @malloc(i64 noundef %21) #6
  %23 = getelementptr inbounds %class.customEllipse, ptr %10, i32 0, i32 33
  store ptr %22, ptr %23, align 8, !tbaa !9
  %24 = call noundef double @_ZN13customEllipse22GetAverageFittingErrorEv(ptr noundef nonnull align 8 dereferenceable(272) %10)
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #7
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #7
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #7
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #7
  %25 = getelementptr inbounds %class.customEllipse, ptr %10, i32 0, i32 23
  %26 = load double, ptr %25, align 8, !tbaa !14
  %27 = call double @sin(double noundef %26) #7
  store double %27, ptr %7, align 8, !tbaa !15
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #7
  %28 = getelementptr inbounds %class.customEllipse, ptr %10, i32 0, i32 23
  %29 = load double, ptr %28, align 8, !tbaa !14
  %30 = call double @cos(double noundef %29) #7
  store double %30, ptr %8, align 8, !tbaa !15
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #7
  store i32 0, ptr %9, align 4, !tbaa !16
  br label %31

31:                                               ; preds = %87, %17
  %32 = load i32, ptr %9, align 4, !tbaa !16
  %33 = getelementptr inbounds %class.customEllipse, ptr %10, i32 0, i32 27
  %34 = load i32, ptr %33, align 8, !tbaa !13
  %35 = icmp slt i32 %32, %34
  br i1 %35, label %37, label %36

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

37:                                               ; preds = %31
  %38 = getelementptr inbounds %class.customEllipse, ptr %10, i32 0, i32 20
  %39 = load double, ptr %38, align 8, !tbaa !17
  %40 = getelementptr inbounds %class.customEllipse, ptr %10, i32 0, i32 30
  %41 = load ptr, ptr %40, align 8, !tbaa !18
  %42 = load i32, ptr %9, align 4, !tbaa !16
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds double, ptr %41, i64 %43
  %45 = load double, ptr %44, align 8, !tbaa !15
  %46 = call double @cos(double noundef %45) #7
  %47 = fmul double %39, %46
  store double %47, ptr %5, align 8, !tbaa !15
  %48 = getelementptr inbounds %class.customEllipse, ptr %10, i32 0, i32 21
  %49 = load double, ptr %48, align 8, !tbaa !19
  %50 = getelementptr inbounds %class.customEllipse, ptr %10, i32 0, i32 30
  %51 = load ptr, ptr %50, align 8, !tbaa !18
  %52 = load i32, ptr %9, align 4, !tbaa !16
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds double, ptr %51, i64 %53
  %55 = load double, ptr %54, align 8, !tbaa !15
  %56 = call double @sin(double noundef %55) #7
  %57 = fmul double %49, %56
  store double %57, ptr %6, align 8, !tbaa !15
  %58 = load double, ptr %5, align 8, !tbaa !15
  %59 = load double, ptr %8, align 8, !tbaa !15
  %60 = load double, ptr %6, align 8, !tbaa !15
  %61 = load double, ptr %7, align 8, !tbaa !15
  %62 = fmul double %60, %61
  %63 = fneg double %62
  %64 = call double @llvm.fmuladd.f64(double %58, double %59, double %63)
  %65 = getelementptr inbounds %class.customEllipse, ptr %10, i32 0, i32 18
  %66 = load double, ptr %65, align 8, !tbaa !20
  %67 = fadd double %64, %66
  %68 = fptosi double %67 to i32
  %69 = getelementptr inbounds %struct.pix, ptr %4, i32 0, i32 0
  store i32 %68, ptr %69, align 4, !tbaa !21
  %70 = load double, ptr %5, align 8, !tbaa !15
  %71 = load double, ptr %7, align 8, !tbaa !15
  %72 = load double, ptr %6, align 8, !tbaa !15
  %73 = load double, ptr %8, align 8, !tbaa !15
  %74 = fmul double %72, %73
  %75 = call double @llvm.fmuladd.f64(double %70, double %71, double %74)
  %76 = getelementptr inbounds %class.customEllipse, ptr %10, i32 0, i32 19
  %77 = load double, ptr %76, align 8, !tbaa !23
  %78 = fadd double %75, %77
  %79 = fptosi double %78 to i32
  %80 = sub nsw i32 0, %79
  %81 = getelementptr inbounds %struct.pix, ptr %4, i32 0, i32 1
  store i32 %80, ptr %81, align 4, !tbaa !24
  %82 = getelementptr inbounds %class.customEllipse, ptr %10, i32 0, i32 33
  %83 = load ptr, ptr %82, align 8, !tbaa !9
  %84 = load i32, ptr %9, align 4, !tbaa !16
  %85 = sext i32 %84 to i64
  %86 = getelementptr inbounds %struct.pix, ptr %83, i64 %85
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %86, ptr align 4 %4, i64 8, i1 false), !tbaa.struct !25
  br label %87

87:                                               ; preds = %37
  %88 = load i32, ptr %9, align 4, !tbaa !16
  %89 = add nsw i32 %88, 1
  store i32 %89, ptr %9, align 4, !tbaa !16
  br label %31, !llvm.loop !26

90:                                               ; preds = %36
  %91 = getelementptr inbounds %class.customEllipse, ptr %10, i32 0, i32 33
  %92 = load ptr, ptr %91, align 8, !tbaa !9
  store ptr %92, ptr %2, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #7
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #7
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #7
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #7
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #7
  br label %93

93:                                               ; preds = %90, %14
  %94 = load ptr, ptr %2, align 8
  ret ptr %94
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #2 = { 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 #3 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { nounwind "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 #5 = { 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 #6 = { nounwind allocsize(0) }
attributes #7 = { 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !6, i64 264}
!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, !11, i64 184}
!15 = !{!11, !11, i64 0}
!16 = !{!12, !12, i64 0}
!17 = !{!10, !11, i64 160}
!18 = !{!10, !6, i64 240}
!19 = !{!10, !11, i64 168}
!20 = !{!10, !11, i64 144}
!21 = !{!22, !12, i64 0}
!22 = !{!"_ZTS3pix", !12, i64 0, !12, i64 4}
!23 = !{!10, !11, i64 152}
!24 = !{!22, !12, i64 4}
!25 = !{i64 0, i64 4, !16, i64 4, i64 4, !16}
!26 = distinct !{!26, !27}
!27 = !{!"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 speculatable willreturn memory(none)
declare double @llvm.fmuladd.f64(double, double, double) #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 nofree nounwind willreturn memory(write)
declare double @cos(double noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nofree nounwind willreturn memory(write)
declare double @sin(double noundef) local_unnamed_addr #2

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

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_ZN13customEllipse16GetClosestPointsEv(ptr noundef nonnull align 8 dereferenceable(272) %0) local_unnamed_addr #3 align 2 {
  %2 = getelementptr inbounds i8, ptr %0, i64 264
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %.not = icmp eq ptr %3, null
  br i1 %.not, label %4, label %._crit_edge

4:                                                ; preds = %1
  %5 = getelementptr inbounds i8, ptr %0, i64 216
  %6 = load i32, ptr %5, align 8, !tbaa !12
  %7 = sext i32 %6 to i64
  %8 = shl nsw i64 %7, 3
  %9 = tail call noalias ptr @malloc(i64 noundef %8) #4
  store ptr %9, ptr %2, align 8, !tbaa !5
  %10 = tail call noundef double @_ZN13customEllipse22GetAverageFittingErrorEv(ptr noundef nonnull align 8 dereferenceable(272) %0)
  %11 = getelementptr inbounds i8, ptr %0, i64 184
  %12 = load double, ptr %11, align 8, !tbaa !13
  %13 = tail call double @sin(double noundef %12) #5
  %14 = load double, ptr %11, align 8, !tbaa !13
  %15 = tail call double @cos(double noundef %14) #5
  %16 = load i32, ptr %5, align 8, !tbaa !12
  %17 = icmp sgt i32 %16, 0
  br i1 %17, label %.lr.ph, label %.._crit_edge_crit_edge

.._crit_edge_crit_edge:                           ; preds = %4
  %.pre = load ptr, ptr %2, align 8, !tbaa !5
  br label %._crit_edge

.lr.ph:                                           ; preds = %4
  %18 = getelementptr inbounds i8, ptr %0, i64 160
  %19 = getelementptr inbounds i8, ptr %0, i64 240
  %20 = getelementptr inbounds i8, ptr %0, i64 168
  %21 = getelementptr inbounds i8, ptr %0, i64 144
  %22 = getelementptr inbounds i8, ptr %0, i64 152
  br label %23

23:                                               ; preds = %.lr.ph, %23
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %23 ]
  %24 = load double, ptr %18, align 8, !tbaa !14
  %25 = load ptr, ptr %19, align 8, !tbaa !15
  %26 = getelementptr inbounds double, ptr %25, i64 %indvars.iv
  %27 = load double, ptr %26, align 8, !tbaa !16
  %28 = tail call double @cos(double noundef %27) #5
  %29 = fmul double %24, %28
  %30 = load double, ptr %20, align 8, !tbaa !17
  %31 = load ptr, ptr %19, align 8, !tbaa !15
  %32 = getelementptr inbounds double, ptr %31, i64 %indvars.iv
  %33 = load double, ptr %32, align 8, !tbaa !16
  %34 = tail call double @sin(double noundef %33) #5
  %35 = fmul double %30, %34
  %36 = fneg double %35
  %37 = fmul double %13, %36
  %38 = tail call double @llvm.fmuladd.f64(double %29, double %15, double %37)
  %39 = load double, ptr %21, align 8, !tbaa !18
  %40 = fadd double %39, %38
  %41 = fptosi double %40 to i32
  %42 = fmul double %15, %35
  %43 = tail call double @llvm.fmuladd.f64(double %29, double %13, double %42)
  %44 = load double, ptr %22, align 8, !tbaa !19
  %45 = fadd double %44, %43
  %46 = fptosi double %45 to i32
  %47 = sub nsw i32 0, %46
  %48 = load ptr, ptr %2, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.pix, ptr %48, i64 %indvars.iv
  store i32 %41, ptr %49, align 4, !tbaa !20
  %.sroa.4.0..sroa_idx = getelementptr inbounds i8, ptr %49, i64 4
  store i32 %47, ptr %.sroa.4.0..sroa_idx, align 4, !tbaa !20
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %50 = load i32, ptr %5, align 8, !tbaa !12
  %51 = sext i32 %50 to i64
  %52 = icmp slt i64 %indvars.iv.next, %51
  br i1 %52, label %23, label %._crit_edge, !llvm.loop !21

._crit_edge:                                      ; preds = %23, %.._crit_edge_crit_edge, %1
  %.014 = phi ptr [ %3, %1 ], [ %.pre, %.._crit_edge_crit_edge ], [ %48, %23 ]
  ret ptr %.014
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 nofree nounwind willreturn memory(write) "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 = { 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 allocsize(0) }
attributes #5 = { 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, !11, i64 264}
!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, !7, i64 184}
!14 = !{!6, !7, i64 160}
!15 = !{!6, !11, i64 240}
!16 = !{!7, !7, i64 0}
!17 = !{!6, !7, i64 168}
!18 = !{!6, !7, i64 144}
!19 = !{!6, !7, i64 152}
!20 = !{!10, !10, i64 0}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}


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