; LLVM IR from MRPT/mrpt :: libs/math/src/CSparse (cs_norm)

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

; Function Attrs: nounwind uwtable
define dso_local double @cs_norm(ptr noundef %0) #0 {
  %2 = alloca double, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca double, align 8
  %10 = alloca double, align 8
  %11 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  store double 0.000000e+00, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = icmp ne ptr %12, null
  br i1 %13, label %14, label %24

14:                                               ; preds = %1
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.cs_sparse, ptr %15, i32 0, i32 6
  %17 = load i32, ptr %16, align 8, !tbaa !11
  %18 = icmp eq i32 %17, -1
  br i1 %18, label %19, label %24

19:                                               ; preds = %14
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.cs_sparse, ptr %20, i32 0, i32 5
  %22 = load ptr, ptr %21, align 8, !tbaa !14
  %23 = icmp ne ptr %22, null
  br i1 %23, label %25, label %24

24:                                               ; preds = %19, %14, %1
  store double -1.000000e+00, ptr %2, align 8
  store i32 1, ptr %11, align 4
  br label %81

25:                                               ; preds = %19
  %26 = load ptr, ptr %3, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.cs_sparse, ptr %26, i32 0, i32 2
  %28 = load i32, ptr %27, align 8, !tbaa !15
  store i32 %28, ptr %6, align 4, !tbaa !16
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.cs_sparse, ptr %29, i32 0, i32 3
  %31 = load ptr, ptr %30, align 8, !tbaa !17
  store ptr %31, ptr %7, align 8, !tbaa !5
  %32 = load ptr, ptr %3, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.cs_sparse, ptr %32, i32 0, i32 5
  %34 = load ptr, ptr %33, align 8, !tbaa !14
  store ptr %34, ptr %8, align 8, !tbaa !5
  store i32 0, ptr %5, align 4, !tbaa !16
  br label %35

35:                                               ; preds = %76, %25
  %36 = load i32, ptr %5, align 4, !tbaa !16
  %37 = load i32, ptr %6, align 4, !tbaa !16
  %38 = icmp slt i32 %36, %37
  br i1 %38, label %39, label %79

39:                                               ; preds = %35
  store double 0.000000e+00, ptr %10, align 8, !tbaa !9
  %40 = load ptr, ptr %7, align 8, !tbaa !5
  %41 = load i32, ptr %5, align 4, !tbaa !16
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds i32, ptr %40, i64 %42
  %44 = load i32, ptr %43, align 4, !tbaa !16
  store i32 %44, ptr %4, align 4, !tbaa !16
  br label %45

45:                                               ; preds = %63, %39
  %46 = load i32, ptr %4, align 4, !tbaa !16
  %47 = load ptr, ptr %7, align 8, !tbaa !5
  %48 = load i32, ptr %5, align 4, !tbaa !16
  %49 = add nsw i32 %48, 1
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds i32, ptr %47, i64 %50
  %52 = load i32, ptr %51, align 4, !tbaa !16
  %53 = icmp slt i32 %46, %52
  br i1 %53, label %54, label %66

54:                                               ; preds = %45
  %55 = load ptr, ptr %8, align 8, !tbaa !5
  %56 = load i32, ptr %4, align 4, !tbaa !16
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds double, ptr %55, i64 %57
  %59 = load double, ptr %58, align 8, !tbaa !9
  %60 = call double @llvm.fabs.f64(double %59)
  %61 = load double, ptr %10, align 8, !tbaa !9
  %62 = fadd double %61, %60
  store double %62, ptr %10, align 8, !tbaa !9
  br label %63

63:                                               ; preds = %54
  %64 = load i32, ptr %4, align 4, !tbaa !16
  %65 = add nsw i32 %64, 1
  store i32 %65, ptr %4, align 4, !tbaa !16
  br label %45, !llvm.loop !18

66:                                               ; preds = %45
  %67 = load double, ptr %9, align 8, !tbaa !9
  %68 = load double, ptr %10, align 8, !tbaa !9
  %69 = fcmp ogt double %67, %68
  br i1 %69, label %70, label %72

70:                                               ; preds = %66
  %71 = load double, ptr %9, align 8, !tbaa !9
  br label %74

72:                                               ; preds = %66
  %73 = load double, ptr %10, align 8, !tbaa !9
  br label %74

74:                                               ; preds = %72, %70
  %75 = phi double [ %71, %70 ], [ %73, %72 ]
  store double %75, ptr %9, align 8, !tbaa !9
  br label %76

76:                                               ; preds = %74
  %77 = load i32, ptr %5, align 4, !tbaa !16
  %78 = add nsw i32 %77, 1
  store i32 %78, ptr %5, align 4, !tbaa !16
  br label %35, !llvm.loop !20

79:                                               ; preds = %35
  %80 = load double, ptr %9, align 8, !tbaa !9
  store double %80, ptr %2, align 8
  store i32 1, ptr %11, align 4
  br label %81

81:                                               ; preds = %79, %24
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  %82 = load double, ptr %2, align 8
  ret double %82
}

; 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 speculatable willreturn memory(none)
declare double @llvm.fabs.f64(double) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #3 = { 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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"double", !7, i64 0}
!11 = !{!12, !13, i64 40}
!12 = !{!"cs_sparse", !13, i64 0, !13, i64 4, !13, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !13, i64 40}
!13 = !{!"int", !7, i64 0}
!14 = !{!12, !6, i64 32}
!15 = !{!12, !13, i64 8}
!16 = !{!13, !13, i64 0}
!17 = !{!12, !6, i64 16}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}
!20 = distinct !{!20, !19}


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

; Function Attrs: nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) uwtable
define dso_local double @cs_norm(ptr noundef readonly %0) local_unnamed_addr #0 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %.loopexit, label %2

2:                                                ; preds = %1
  %3 = getelementptr inbounds i8, ptr %0, i64 40
  %4 = load i32, ptr %3, align 8, !tbaa !5
  %5 = icmp eq i32 %4, -1
  br i1 %5, label %6, label %.loopexit

6:                                                ; preds = %2
  %7 = getelementptr inbounds i8, ptr %0, i64 32
  %8 = load ptr, ptr %7, align 8, !tbaa !11
  %.not29 = icmp eq ptr %8, null
  br i1 %.not29, label %.loopexit, label %9

9:                                                ; preds = %6
  %10 = getelementptr inbounds i8, ptr %0, i64 8
  %11 = load i32, ptr %10, align 8, !tbaa !12
  %12 = getelementptr inbounds i8, ptr %0, i64 16
  %13 = load ptr, ptr %12, align 8, !tbaa !13
  %14 = icmp sgt i32 %11, 0
  br i1 %14, label %.lr.ph36.preheader, label %.loopexit

.lr.ph36.preheader:                               ; preds = %9
  %wide.trip.count43 = zext nneg i32 %11 to i64
  %.pre = load i32, ptr %13, align 4, !tbaa !14
  %invariant.gep = getelementptr i8, ptr %8, i64 8
  %invariant.gep51 = getelementptr i8, ptr %8, i64 16
  %invariant.gep53 = getelementptr i8, ptr %8, i64 24
  br label %.lr.ph36

.lr.ph36:                                         ; preds = %.lr.ph36.preheader, %._crit_edge
  %15 = phi i32 [ %.pre, %.lr.ph36.preheader ], [ %17, %._crit_edge ]
  %indvars.iv40 = phi i64 [ 0, %.lr.ph36.preheader ], [ %indvars.iv.next41, %._crit_edge ]
  %.02334 = phi double [ 0.000000e+00, %.lr.ph36.preheader ], [ %41, %._crit_edge ]
  %indvars.iv.next41 = add nuw nsw i64 %indvars.iv40, 1
  %16 = getelementptr inbounds i32, ptr %13, i64 %indvars.iv.next41
  %17 = load i32, ptr %16, align 4, !tbaa !14
  %18 = icmp slt i32 %15, %17
  br i1 %18, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %.lr.ph36
  %19 = sext i32 %15 to i64
  %wide.trip.count = sext i32 %17 to i64
  %20 = sub nsw i64 %wide.trip.count, %19
  %xtraiter = and i64 %20, 3
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.lr.ph.prol.loopexit, label %.lr.ph.prol

.lr.ph.prol:                                      ; preds = %.lr.ph.preheader, %.lr.ph.prol
  %indvars.iv.prol = phi i64 [ %indvars.iv.next.prol, %.lr.ph.prol ], [ %19, %.lr.ph.preheader ]
  %.031.prol = phi double [ %24, %.lr.ph.prol ], [ 0.000000e+00, %.lr.ph.preheader ]
  %prol.iter = phi i64 [ %prol.iter.next, %.lr.ph.prol ], [ 0, %.lr.ph.preheader ]
  %21 = getelementptr inbounds double, ptr %8, i64 %indvars.iv.prol
  %22 = load double, ptr %21, align 8, !tbaa !15
  %23 = tail call double @llvm.fabs.f64(double %22)
  %24 = fadd double %.031.prol, %23
  %indvars.iv.next.prol = add nsw i64 %indvars.iv.prol, 1
  %prol.iter.next = add i64 %prol.iter, 1
  %prol.iter.cmp.not = icmp eq i64 %prol.iter.next, %xtraiter
  br i1 %prol.iter.cmp.not, label %.lr.ph.prol.loopexit, label %.lr.ph.prol, !llvm.loop !17

.lr.ph.prol.loopexit:                             ; preds = %.lr.ph.prol, %.lr.ph.preheader
  %.lcssa.unr = phi double [ poison, %.lr.ph.preheader ], [ %24, %.lr.ph.prol ]
  %indvars.iv.unr = phi i64 [ %19, %.lr.ph.preheader ], [ %indvars.iv.next.prol, %.lr.ph.prol ]
  %.031.unr = phi double [ 0.000000e+00, %.lr.ph.preheader ], [ %24, %.lr.ph.prol ]
  %25 = sub nsw i64 %19, %wide.trip.count
  %26 = icmp ugt i64 %25, -4
  br i1 %26, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.prol.loopexit, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next.3, %.lr.ph ], [ %indvars.iv.unr, %.lr.ph.prol.loopexit ]
  %.031 = phi double [ %39, %.lr.ph ], [ %.031.unr, %.lr.ph.prol.loopexit ]
  %27 = getelementptr inbounds double, ptr %8, i64 %indvars.iv
  %28 = load double, ptr %27, align 8, !tbaa !15
  %29 = tail call double @llvm.fabs.f64(double %28)
  %30 = fadd double %.031, %29
  %gep = getelementptr double, ptr %invariant.gep, i64 %indvars.iv
  %31 = load double, ptr %gep, align 8, !tbaa !15
  %32 = tail call double @llvm.fabs.f64(double %31)
  %33 = fadd double %30, %32
  %gep52 = getelementptr double, ptr %invariant.gep51, i64 %indvars.iv
  %34 = load double, ptr %gep52, align 8, !tbaa !15
  %35 = tail call double @llvm.fabs.f64(double %34)
  %36 = fadd double %33, %35
  %gep54 = getelementptr double, ptr %invariant.gep53, i64 %indvars.iv
  %37 = load double, ptr %gep54, align 8, !tbaa !15
  %38 = tail call double @llvm.fabs.f64(double %37)
  %39 = fadd double %36, %38
  %indvars.iv.next.3 = add nsw i64 %indvars.iv, 4
  %exitcond.not.3 = icmp eq i64 %indvars.iv.next.3, %wide.trip.count
  br i1 %exitcond.not.3, label %._crit_edge, label %.lr.ph, !llvm.loop !19

._crit_edge:                                      ; preds = %.lr.ph.prol.loopexit, %.lr.ph, %.lr.ph36
  %.0.lcssa = phi double [ 0.000000e+00, %.lr.ph36 ], [ %.lcssa.unr, %.lr.ph.prol.loopexit ], [ %39, %.lr.ph ]
  %40 = fcmp ogt double %.02334, %.0.lcssa
  %41 = select i1 %40, double %.02334, double %.0.lcssa
  %exitcond44.not = icmp eq i64 %indvars.iv.next41, %wide.trip.count43
  br i1 %exitcond44.not, label %.loopexit, label %.lr.ph36, !llvm.loop !21

.loopexit:                                        ; preds = %._crit_edge, %9, %1, %2, %6
  %.024 = phi double [ -1.000000e+00, %6 ], [ -1.000000e+00, %2 ], [ -1.000000e+00, %1 ], [ 0.000000e+00, %9 ], [ %41, %._crit_edge ]
  ret double %.024
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.fabs.f64(double) #1

attributes #0 = { nofree norecurse nosync nounwind memory(read, 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 = { mustprogress 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, !7, i64 40}
!6 = !{!"cs_sparse", !7, i64 0, !7, i64 4, !7, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !7, i64 40}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !10, i64 32}
!12 = !{!6, !7, i64 8}
!13 = !{!6, !10, i64 16}
!14 = !{!7, !7, i64 0}
!15 = !{!16, !16, i64 0}
!16 = !{!"double", !8, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.unroll.disable"}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}
!21 = distinct !{!21, !20}


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