; LLVM IR from bonamarton/phys_num :: gauss_jordan_elim (gauss)

; 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.tensor = type { ptr, i32, i32, 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.fabs.f64(double) #1

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

7:                                                ; preds = %47, %1
  %8 = load i32, ptr %3, align 4, !tbaa !11
  %9 = load ptr, ptr %2, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.tensor, ptr %9, i32 0, i32 2
  %11 = load i32, ptr %10, align 4, !tbaa !13
  %12 = icmp slt i32 %8, %11
  br i1 %12, label %13, label %50

13:                                               ; preds = %7
  store i32 0, ptr %4, align 4, !tbaa !11
  br label %14

14:                                               ; preds = %37, %13
  %15 = load i32, ptr %4, align 4, !tbaa !11
  %16 = load ptr, ptr %2, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.tensor, ptr %16, i32 0, i32 1
  %18 = load i32, ptr %17, align 8, !tbaa !15
  %19 = icmp slt i32 %15, %18
  br i1 %19, label %20, label %40

20:                                               ; preds = %14
  %21 = load ptr, ptr %2, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.tensor, ptr %21, i32 0, i32 0
  %23 = load ptr, ptr %22, align 8, !tbaa !16
  %24 = load i32, ptr %4, align 4, !tbaa !11
  %25 = load ptr, ptr %2, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.tensor, ptr %25, i32 0, i32 2
  %27 = load i32, ptr %26, align 4, !tbaa !13
  %28 = mul nsw i32 %24, %27
  %29 = load i32, ptr %3, align 4, !tbaa !11
  %30 = add nsw i32 %28, %29
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds double, ptr %23, i64 %31
  %33 = load double, ptr %32, align 8, !tbaa !9
  %34 = call double @llvm.fabs.f64(double %33)
  %35 = load double, ptr %5, align 8, !tbaa !9
  %36 = fadd double %35, %34
  store double %36, ptr %5, align 8, !tbaa !9
  br label %37

37:                                               ; preds = %20
  %38 = load i32, ptr %4, align 4, !tbaa !11
  %39 = add nsw i32 %38, 1
  store i32 %39, ptr %4, align 4, !tbaa !11
  br label %14, !llvm.loop !17

40:                                               ; preds = %14
  %41 = load double, ptr %5, align 8, !tbaa !9
  %42 = load double, ptr %6, align 8, !tbaa !9
  %43 = fcmp ogt double %41, %42
  br i1 %43, label %44, label %46

44:                                               ; preds = %40
  %45 = load double, ptr %5, align 8, !tbaa !9
  store double %45, ptr %6, align 8, !tbaa !9
  br label %46

46:                                               ; preds = %44, %40
  store double 0.000000e+00, ptr %5, align 8, !tbaa !9
  br label %47

47:                                               ; preds = %46
  %48 = load i32, ptr %3, align 4, !tbaa !11
  %49 = add nsw i32 %48, 1
  store i32 %49, ptr %3, align 4, !tbaa !11
  br label %7, !llvm.loop !19

50:                                               ; preds = %7
  %51 = load double, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  ret double %51
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #2 = { 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 #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, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !12, i64 12}
!14 = !{!"tensor", !6, i64 0, !12, i64 8, !12, i64 12, !6, i64 16}
!15 = !{!14, !12, i64 8}
!16 = !{!14, !6, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = distinct !{!19, !18}


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

; Function Attrs: nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) uwtable
define dso_local double @norm1(ptr nocapture noundef readonly %0) local_unnamed_addr #1 {
  %2 = getelementptr inbounds i8, ptr %0, i64 12
  %3 = load i32, ptr %2, align 4, !tbaa !5
  %4 = icmp sgt i32 %3, 0
  br i1 %4, label %.preheader.lr.ph, label %._crit_edge22

.preheader.lr.ph:                                 ; preds = %1
  %5 = getelementptr inbounds i8, ptr %0, i64 8
  %6 = load i32, ptr %5, align 8, !tbaa !11
  %7 = icmp sgt i32 %6, 0
  br i1 %7, label %.preheader.lr.ph.split.us, label %._crit_edge22

.preheader.lr.ph.split.us:                        ; preds = %.preheader.lr.ph
  %8 = load ptr, ptr %0, align 8, !tbaa !12
  %9 = zext nneg i32 %3 to i64
  %wide.trip.count = zext nneg i32 %6 to i64
  %xtraiter = and i64 %wide.trip.count, 3
  %10 = icmp ult i32 %6, 4
  %unroll_iter = and i64 %wide.trip.count, 2147483644
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br label %.preheader.us

.preheader.us:                                    ; preds = %._crit_edge.us, %.preheader.lr.ph.split.us
  %indvars.iv26 = phi i64 [ %indvars.iv.next27, %._crit_edge.us ], [ 0, %.preheader.lr.ph.split.us ]
  %.021.us = phi double [ %.1.us, %._crit_edge.us ], [ 0.000000e+00, %.preheader.lr.ph.split.us ]
  %invariant.gep = getelementptr inbounds double, ptr %8, i64 %indvars.iv26
  br i1 %10, label %._crit_edge.us.unr-lcssa, label %.preheader.us.new

.preheader.us.new:                                ; preds = %.preheader.us, %.preheader.us.new
  %indvars.iv = phi i64 [ %indvars.iv.next.3, %.preheader.us.new ], [ 0, %.preheader.us ]
  %.11519.us = phi double [ %26, %.preheader.us.new ], [ 0.000000e+00, %.preheader.us ]
  %niter = phi i64 [ %niter.next.3, %.preheader.us.new ], [ 0, %.preheader.us ]
  %11 = mul nuw nsw i64 %indvars.iv, %9
  %gep = getelementptr inbounds double, ptr %invariant.gep, i64 %11
  %12 = load double, ptr %gep, align 8, !tbaa !13
  %13 = tail call double @llvm.fabs.f64(double %12)
  %14 = fadd double %.11519.us, %13
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %15 = mul nuw nsw i64 %indvars.iv.next, %9
  %gep.1 = getelementptr inbounds double, ptr %invariant.gep, i64 %15
  %16 = load double, ptr %gep.1, align 8, !tbaa !13
  %17 = tail call double @llvm.fabs.f64(double %16)
  %18 = fadd double %14, %17
  %indvars.iv.next.1 = or disjoint i64 %indvars.iv, 2
  %19 = mul nuw nsw i64 %indvars.iv.next.1, %9
  %gep.2 = getelementptr inbounds double, ptr %invariant.gep, i64 %19
  %20 = load double, ptr %gep.2, align 8, !tbaa !13
  %21 = tail call double @llvm.fabs.f64(double %20)
  %22 = fadd double %18, %21
  %indvars.iv.next.2 = or disjoint i64 %indvars.iv, 3
  %23 = mul nuw nsw i64 %indvars.iv.next.2, %9
  %gep.3 = getelementptr inbounds double, ptr %invariant.gep, i64 %23
  %24 = load double, ptr %gep.3, align 8, !tbaa !13
  %25 = tail call double @llvm.fabs.f64(double %24)
  %26 = fadd double %22, %25
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 4
  %niter.next.3 = add i64 %niter, 4
  %niter.ncmp.3 = icmp eq i64 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3, label %._crit_edge.us.unr-lcssa, label %.preheader.us.new, !llvm.loop !15

._crit_edge.us.unr-lcssa:                         ; preds = %.preheader.us.new, %.preheader.us
  %.lcssa.ph = phi double [ poison, %.preheader.us ], [ %26, %.preheader.us.new ]
  %indvars.iv.unr = phi i64 [ 0, %.preheader.us ], [ %indvars.iv.next.3, %.preheader.us.new ]
  %.11519.us.unr = phi double [ 0.000000e+00, %.preheader.us ], [ %26, %.preheader.us.new ]
  br i1 %lcmp.mod.not, label %._crit_edge.us, label %.epil.preheader

.epil.preheader:                                  ; preds = %._crit_edge.us.unr-lcssa, %.epil.preheader
  %indvars.iv.epil = phi i64 [ %indvars.iv.next.epil, %.epil.preheader ], [ %indvars.iv.unr, %._crit_edge.us.unr-lcssa ]
  %.11519.us.epil = phi double [ %30, %.epil.preheader ], [ %.11519.us.unr, %._crit_edge.us.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.epil.preheader ], [ 0, %._crit_edge.us.unr-lcssa ]
  %27 = mul nuw nsw i64 %indvars.iv.epil, %9
  %gep.epil = getelementptr inbounds double, ptr %invariant.gep, i64 %27
  %28 = load double, ptr %gep.epil, align 8, !tbaa !13
  %29 = tail call double @llvm.fabs.f64(double %28)
  %30 = fadd double %.11519.us.epil, %29
  %indvars.iv.next.epil = add nuw nsw i64 %indvars.iv.epil, 1
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %._crit_edge.us, label %.epil.preheader, !llvm.loop !17

._crit_edge.us:                                   ; preds = %.epil.preheader, %._crit_edge.us.unr-lcssa
  %.lcssa = phi double [ %.lcssa.ph, %._crit_edge.us.unr-lcssa ], [ %30, %.epil.preheader ]
  %31 = fcmp ogt double %.lcssa, %.021.us
  %.1.us = select i1 %31, double %.lcssa, double %.021.us
  %indvars.iv.next27 = add nuw nsw i64 %indvars.iv26, 1
  %exitcond30.not = icmp eq i64 %indvars.iv.next27, %9
  br i1 %exitcond30.not, label %._crit_edge22, label %.preheader.us, !llvm.loop !19

._crit_edge22:                                    ; preds = %._crit_edge.us, %.preheader.lr.ph, %1
  %.0.lcssa = phi double [ 0.000000e+00, %1 ], [ 0.000000e+00, %.preheader.lr.ph ], [ %.1.us, %._crit_edge.us ]
  ret double %.0.lcssa
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #1 = { 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" }

!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, !10, i64 12}
!6 = !{!"tensor", !7, i64 0, !10, i64 8, !10, i64 12, !7, i64 16}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!6, !10, i64 8}
!12 = !{!6, !7, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"double", !8, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.unroll.disable"}
!19 = distinct !{!19, !16}


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