; LLVM IR from YuchenHHH/Sparse-linear-systems :: Source (cs_cumsum)

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

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

14:                                               ; preds = %3
  %15 = load ptr, ptr %6, align 8, !tbaa !5
  %16 = icmp ne ptr %15, null
  br i1 %16, label %18, label %17

17:                                               ; preds = %14, %3
  store double -1.000000e+00, ptr %4, align 8
  store i32 1, ptr %11, align 4
  br label %57

18:                                               ; preds = %14
  store i64 0, ptr %8, align 8, !tbaa !9
  br label %19

19:                                               ; preds = %48, %18
  %20 = load i64, ptr %8, align 8, !tbaa !9
  %21 = load i64, ptr %7, align 8, !tbaa !9
  %22 = icmp slt i64 %20, %21
  br i1 %22, label %23, label %51

23:                                               ; preds = %19
  %24 = load i64, ptr %9, align 8, !tbaa !9
  %25 = load ptr, ptr %5, align 8, !tbaa !5
  %26 = load i64, ptr %8, align 8, !tbaa !9
  %27 = getelementptr inbounds i64, ptr %25, i64 %26
  store i64 %24, ptr %27, align 8, !tbaa !9
  %28 = load ptr, ptr %6, align 8, !tbaa !5
  %29 = load i64, ptr %8, align 8, !tbaa !9
  %30 = getelementptr inbounds i64, ptr %28, i64 %29
  %31 = load i64, ptr %30, align 8, !tbaa !9
  %32 = load i64, ptr %9, align 8, !tbaa !9
  %33 = add nsw i64 %32, %31
  store i64 %33, ptr %9, align 8, !tbaa !9
  %34 = load ptr, ptr %6, align 8, !tbaa !5
  %35 = load i64, ptr %8, align 8, !tbaa !9
  %36 = getelementptr inbounds i64, ptr %34, i64 %35
  %37 = load i64, ptr %36, align 8, !tbaa !9
  %38 = sitofp i64 %37 to double
  %39 = load double, ptr %10, align 8, !tbaa !11
  %40 = fadd double %39, %38
  store double %40, ptr %10, align 8, !tbaa !11
  %41 = load ptr, ptr %5, align 8, !tbaa !5
  %42 = load i64, ptr %8, align 8, !tbaa !9
  %43 = getelementptr inbounds i64, ptr %41, i64 %42
  %44 = load i64, ptr %43, align 8, !tbaa !9
  %45 = load ptr, ptr %6, align 8, !tbaa !5
  %46 = load i64, ptr %8, align 8, !tbaa !9
  %47 = getelementptr inbounds i64, ptr %45, i64 %46
  store i64 %44, ptr %47, align 8, !tbaa !9
  br label %48

48:                                               ; preds = %23
  %49 = load i64, ptr %8, align 8, !tbaa !9
  %50 = add nsw i64 %49, 1
  store i64 %50, ptr %8, align 8, !tbaa !9
  br label %19, !llvm.loop !13

51:                                               ; preds = %19
  %52 = load i64, ptr %9, align 8, !tbaa !9
  %53 = load ptr, ptr %5, align 8, !tbaa !5
  %54 = load i64, ptr %7, align 8, !tbaa !9
  %55 = getelementptr inbounds i64, ptr %53, i64 %54
  store i64 %52, ptr %55, align 8, !tbaa !9
  %56 = load double, ptr %10, align 8, !tbaa !11
  store double %56, ptr %4, align 8
  store i32 1, ptr %11, align 4
  br label %57

57:                                               ; preds = %51, %17
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  %58 = load double, ptr %4, align 8
  ret double %58
}

; 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 = { 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"double", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"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"

; Function Attrs: nofree norecurse nosync nounwind memory(argmem: readwrite) uwtable
define dso_local double @cs_cumsum(ptr noundef writeonly %0, ptr noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = icmp ne ptr %0, null
  %5 = icmp ne ptr %1, null
  %or.cond = and i1 %4, %5
  br i1 %or.cond, label %.preheader, label %29

.preheader:                                       ; preds = %3
  %6 = icmp sgt i64 %2, 0
  br i1 %6, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %.preheader
  %xtraiter = and i64 %2, 1
  %7 = icmp eq i64 %2, 1
  br i1 %7, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %2, 9223372036854775806
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %.031 = phi double [ 0.000000e+00, %.lr.ph.preheader.new ], [ %20, %.lr.ph ]
  %.02330 = phi i64 [ 0, %.lr.ph.preheader.new ], [ %18, %.lr.ph ]
  %.02429 = phi i64 [ 0, %.lr.ph.preheader.new ], [ %21, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %.lr.ph ]
  %8 = getelementptr inbounds i64, ptr %0, i64 %.02429
  store i64 %.02330, ptr %8, align 8, !tbaa !5
  %9 = getelementptr inbounds i64, ptr %1, i64 %.02429
  %10 = load i64, ptr %9, align 8, !tbaa !5
  %11 = add nsw i64 %10, %.02330
  %12 = sitofp i64 %10 to double
  %13 = fadd double %.031, %12
  store i64 %.02330, ptr %9, align 8, !tbaa !5
  %14 = or disjoint i64 %.02429, 1
  %15 = getelementptr inbounds i64, ptr %0, i64 %14
  store i64 %11, ptr %15, align 8, !tbaa !5
  %16 = getelementptr inbounds i64, ptr %1, i64 %14
  %17 = load i64, ptr %16, align 8, !tbaa !5
  %18 = add nsw i64 %17, %11
  %19 = sitofp i64 %17 to double
  %20 = fadd double %13, %19
  store i64 %11, ptr %16, align 8, !tbaa !5
  %21 = add nuw nsw i64 %.02429, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !9

._crit_edge.loopexit.unr-lcssa:                   ; preds = %.lr.ph, %.lr.ph.preheader
  %.lcssa36.ph = phi i64 [ poison, %.lr.ph.preheader ], [ %18, %.lr.ph ]
  %.lcssa.ph = phi double [ poison, %.lr.ph.preheader ], [ %20, %.lr.ph ]
  %.031.unr = phi double [ 0.000000e+00, %.lr.ph.preheader ], [ %20, %.lr.ph ]
  %.02330.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %18, %.lr.ph ]
  %.02429.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %21, %.lr.ph ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa
  %22 = getelementptr inbounds i64, ptr %0, i64 %.02429.unr
  store i64 %.02330.unr, ptr %22, align 8, !tbaa !5
  %23 = getelementptr inbounds i64, ptr %1, i64 %.02429.unr
  %24 = load i64, ptr %23, align 8, !tbaa !5
  %25 = add nsw i64 %24, %.02330.unr
  %26 = sitofp i64 %24 to double
  %27 = fadd double %.031.unr, %26
  store i64 %.02330.unr, ptr %23, align 8, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph.epil, %._crit_edge.loopexit.unr-lcssa, %.preheader
  %.023.lcssa = phi i64 [ 0, %.preheader ], [ %.lcssa36.ph, %._crit_edge.loopexit.unr-lcssa ], [ %25, %.lr.ph.epil ]
  %.0.lcssa = phi double [ 0.000000e+00, %.preheader ], [ %.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %27, %.lr.ph.epil ]
  %28 = getelementptr inbounds i64, ptr %0, i64 %2
  store i64 %.023.lcssa, ptr %28, align 8, !tbaa !5
  br label %29

29:                                               ; preds = %3, %._crit_edge
  %.025 = phi double [ %.0.lcssa, %._crit_edge ], [ -1.000000e+00, %3 ]
  ret double %.025
}

attributes #0 = { nofree norecurse nosync nounwind memory(argmem: readwrite) 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" }

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


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