; 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: nounwind uwtable
declare dso_local void @init_tensor(ptr noundef) #1

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

; Function Attrs: nounwind uwtable
define dso_local void @linearSolve(ptr dead_on_unwind noalias writable sret(%struct.tensor) align 8 %0, ptr noundef %1, ptr noundef %2) #1 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca %struct.tensor, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store ptr %1, ptr %4, align 8, !tbaa !5
  store ptr %2, ptr %5, align 8, !tbaa !5
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  call void @copyTensor(ptr dead_on_unwind writable sret(%struct.tensor) align 8 %0, ptr noundef %9)
  call void @llvm.lifetime.start.p0(i64 24, ptr %6) #4
  %10 = load ptr, ptr %5, align 8, !tbaa !5
  call void @copyTensor(ptr dead_on_unwind writable sret(%struct.tensor) align 8 %6, ptr noundef %10)
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  store i32 0, ptr %8, align 4, !tbaa !9
  %11 = getelementptr inbounds %struct.tensor, ptr %0, i32 0, i32 3
  %12 = load ptr, ptr %11, align 8, !tbaa !11
  %13 = getelementptr inbounds %struct.tensor, ptr %0, i32 0, i32 2
  %14 = load i32, ptr %13, align 4, !tbaa !13
  %15 = sext i32 %14 to i64
  %16 = mul i64 %15, 4
  %17 = call ptr @realloc(ptr noundef %12, i64 noundef %16) #5
  %18 = getelementptr inbounds %struct.tensor, ptr %0, i32 0, i32 3
  store ptr %17, ptr %18, align 8, !tbaa !11
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %19

19:                                               ; preds = %31, %3
  %20 = load i32, ptr %7, align 4, !tbaa !9
  %21 = getelementptr inbounds %struct.tensor, ptr %0, i32 0, i32 2
  %22 = load i32, ptr %21, align 4, !tbaa !13
  %23 = icmp slt i32 %20, %22
  br i1 %23, label %24, label %34

24:                                               ; preds = %19
  %25 = load i32, ptr %7, align 4, !tbaa !9
  %26 = getelementptr inbounds %struct.tensor, ptr %0, i32 0, i32 3
  %27 = load ptr, ptr %26, align 8, !tbaa !11
  %28 = load i32, ptr %7, align 4, !tbaa !9
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds i32, ptr %27, i64 %29
  store i32 %25, ptr %30, align 4, !tbaa !9
  br label %31

31:                                               ; preds = %24
  %32 = load i32, ptr %7, align 4, !tbaa !9
  %33 = add nsw i32 %32, 1
  store i32 %33, ptr %7, align 4, !tbaa !9
  br label %19, !llvm.loop !14

34:                                               ; preds = %19
  call void @preCond(ptr noundef %0, ptr noundef %6)
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %35

35:                                               ; preds = %49, %34
  %36 = load i32, ptr %7, align 4, !tbaa !9
  %37 = getelementptr inbounds %struct.tensor, ptr %0, i32 0, i32 1
  %38 = load i32, ptr %37, align 8, !tbaa !16
  %39 = icmp slt i32 %36, %38
  br i1 %39, label %40, label %52

40:                                               ; preds = %35
  %41 = load i32, ptr %7, align 4, !tbaa !9
  call void @pivot(ptr noundef %0, ptr noundef %6, i32 noundef %41)
  %42 = load i32, ptr %7, align 4, !tbaa !9
  %43 = call i32 @normWithPivotValue(ptr noundef %0, ptr noundef %6, i32 noundef %42)
  store i32 %43, ptr %8, align 4, !tbaa !9
  %44 = load i32, ptr %8, align 4, !tbaa !9
  %45 = icmp ne i32 %44, 0
  br i1 %45, label %46, label %47

46:                                               ; preds = %40
  br label %52

47:                                               ; preds = %40
  %48 = load i32, ptr %7, align 4, !tbaa !9
  call void @eliminationStage1(ptr noundef %0, ptr noundef %6, i32 noundef %48)
  br label %49

49:                                               ; preds = %47
  %50 = load i32, ptr %7, align 4, !tbaa !9
  %51 = add nsw i32 %50, 1
  store i32 %51, ptr %7, align 4, !tbaa !9
  br label %35, !llvm.loop !17

52:                                               ; preds = %46, %35
  %53 = load i32, ptr %8, align 4, !tbaa !9
  %54 = icmp ne i32 %53, 0
  br i1 %54, label %55, label %58

55:                                               ; preds = %52
  call void @destruct_tensor(ptr noundef %6)
  call void @destruct_tensor(ptr noundef %0)
  call void @init_tensor(ptr noundef %0)
  %56 = call noalias ptr @malloc(i64 noundef 8) #6
  %57 = getelementptr inbounds %struct.tensor, ptr %0, i32 0, i32 0
  store ptr %56, ptr %57, align 8, !tbaa !18
  br label %71

58:                                               ; preds = %52
  %59 = getelementptr inbounds %struct.tensor, ptr %0, i32 0, i32 1
  %60 = load i32, ptr %59, align 8, !tbaa !16
  %61 = sub nsw i32 %60, 1
  store i32 %61, ptr %7, align 4, !tbaa !9
  br label %62

62:                                               ; preds = %67, %58
  %63 = load i32, ptr %7, align 4, !tbaa !9
  %64 = icmp sgt i32 %63, -1
  br i1 %64, label %65, label %70

65:                                               ; preds = %62
  %66 = load i32, ptr %7, align 4, !tbaa !9
  call void @eliminationStage2(ptr noundef %0, ptr noundef %6, i32 noundef %66)
  br label %67

67:                                               ; preds = %65
  %68 = load i32, ptr %7, align 4, !tbaa !9
  %69 = add nsw i32 %68, -1
  store i32 %69, ptr %7, align 4, !tbaa !9
  br label %62, !llvm.loop !19

70:                                               ; preds = %62
  call void @backSwap(ptr noundef %0, ptr noundef %6)
  call void @destruct_tensor(ptr noundef %6)
  br label %71

71:                                               ; preds = %70, %55
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 24, ptr %6) #4
  ret void
}

; Function Attrs: nounwind uwtable
declare dso_local void @destruct_tensor(ptr noundef) #1

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

; Function Attrs: nounwind allocsize(1)
declare ptr @realloc(ptr noundef, i64 noundef) #3

; Function Attrs: nounwind uwtable
declare dso_local void @copyTensor(ptr dead_on_unwind noalias writable sret(%struct.tensor) align 8, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @preCond(ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @pivot(ptr noundef, ptr noundef, i32 noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @normWithPivotValue(ptr noundef, ptr noundef, i32 noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @eliminationStage1(ptr noundef, ptr noundef, i32 noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @eliminationStage2(ptr noundef, ptr noundef, i32 noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @backSwap(ptr noundef, ptr noundef) #1

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


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

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

; Function Attrs: nounwind uwtable
declare dso_local void @init_tensor(ptr noundef) local_unnamed_addr #1

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

; Function Attrs: nounwind uwtable
define dso_local void @linearSolve(ptr dead_on_unwind noalias writable sret(%struct.tensor) align 8 %0, ptr noundef %1, ptr noundef %2) local_unnamed_addr #1 {
  %4 = alloca %struct.tensor, align 8
  tail call void @copyTensor(ptr dead_on_unwind writable sret(%struct.tensor) align 8 %0, ptr noundef %1)
  call void @llvm.lifetime.start.p0(i64 24, ptr nonnull %4) #4
  call void @copyTensor(ptr dead_on_unwind nonnull writable sret(%struct.tensor) align 8 %4, ptr noundef %2)
  %5 = getelementptr inbounds i8, ptr %0, i64 16
  %6 = load ptr, ptr %5, align 8, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %0, i64 12
  %8 = load i32, ptr %7, align 4, !tbaa !11
  %9 = sext i32 %8 to i64
  %10 = shl nsw i64 %9, 2
  %11 = call ptr @realloc(ptr noundef %6, i64 noundef %10) #5
  store ptr %11, ptr %5, align 8, !tbaa !5
  %12 = load i32, ptr %7, align 4, !tbaa !11
  %13 = icmp sgt i32 %12, 0
  br i1 %13, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %3
  %wide.trip.count = zext nneg i32 %12 to i64
  %min.iters.check = icmp ult i32 %12, 8
  br i1 %min.iters.check, label %.lr.ph.preheader32, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %wide.trip.count, 2147483640
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.ind = phi <4 x i32> [ <i32 0, i32 1, i32 2, i32 3>, %vector.ph ], [ %vec.ind.next, %vector.body ]
  %step.add = add <4 x i32> %vec.ind, <i32 4, i32 4, i32 4, i32 4>
  %14 = getelementptr inbounds i32, ptr %11, i64 %index
  %15 = getelementptr inbounds i8, ptr %14, i64 16
  store <4 x i32> %vec.ind, ptr %14, align 4, !tbaa !12
  store <4 x i32> %step.add, ptr %15, align 4, !tbaa !12
  %index.next = add nuw i64 %index, 8
  %vec.ind.next = add <4 x i32> %vec.ind, <i32 8, i32 8, i32 8, i32 8>
  %16 = icmp eq i64 %index.next, %n.vec
  br i1 %16, label %middle.block, label %vector.body, !llvm.loop !13

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %n.vec, %wide.trip.count
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph.preheader32

.lr.ph.preheader32:                               ; preds = %middle.block, %.lr.ph.preheader
  %indvars.iv.ph = phi i64 [ 0, %.lr.ph.preheader ], [ %n.vec, %middle.block ]
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader32, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ %indvars.iv.ph, %.lr.ph.preheader32 ]
  %17 = getelementptr inbounds i32, ptr %11, i64 %indvars.iv
  %18 = trunc nuw nsw i64 %indvars.iv to i32
  store i32 %18, ptr %17, align 4, !tbaa !12
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !17

._crit_edge:                                      ; preds = %.lr.ph, %middle.block, %3
  call void @preCond(ptr noundef nonnull %0, ptr noundef nonnull %4)
  %19 = getelementptr inbounds i8, ptr %0, i64 8
  %20 = load i32, ptr %19, align 8, !tbaa !18
  %.not2023 = icmp sgt i32 %20, 0
  br i1 %.not2023, label %.lr.ph26, label %._crit_edge29

.lr.ph26:                                         ; preds = %._crit_edge, %22
  %.11624 = phi i32 [ %23, %22 ], [ 0, %._crit_edge ]
  call void @pivot(ptr noundef nonnull %0, ptr noundef nonnull %4, i32 noundef %.11624)
  %21 = call i32 @normWithPivotValue(ptr noundef nonnull %0, ptr noundef nonnull %4, i32 noundef %.11624)
  %.not = icmp eq i32 %21, 0
  br i1 %.not, label %22, label %25

22:                                               ; preds = %.lr.ph26
  call void @eliminationStage1(ptr noundef nonnull %0, ptr noundef nonnull %4, i32 noundef %.11624)
  %23 = add nuw nsw i32 %.11624, 1
  %24 = load i32, ptr %19, align 8, !tbaa !18
  %.not20 = icmp slt i32 %23, %24
  br i1 %.not20, label %.lr.ph26, label %.critedge, !llvm.loop !19

25:                                               ; preds = %.lr.ph26
  call void @destruct_tensor(ptr noundef nonnull %4)
  call void @destruct_tensor(ptr noundef nonnull %0)
  call void @init_tensor(ptr noundef nonnull %0)
  %26 = call noalias dereferenceable_or_null(8) ptr @malloc(i64 noundef 8) #6
  store ptr %26, ptr %0, align 8, !tbaa !20
  br label %29

.critedge:                                        ; preds = %22
  %27 = icmp sgt i32 %24, 0
  br i1 %27, label %.lr.ph28, label %._crit_edge29

.lr.ph28:                                         ; preds = %.critedge, %.lr.ph28
  %.2.in27 = phi i32 [ %.2, %.lr.ph28 ], [ %24, %.critedge ]
  %.2 = add nsw i32 %.2.in27, -1
  call void @eliminationStage2(ptr noundef %0, ptr noundef nonnull %4, i32 noundef %.2)
  %28 = icmp ugt i32 %.2.in27, 1
  br i1 %28, label %.lr.ph28, label %._crit_edge29, !llvm.loop !21

._crit_edge29:                                    ; preds = %.lr.ph28, %._crit_edge, %.critedge
  call void @backSwap(ptr noundef %0, ptr noundef nonnull %4)
  call void @destruct_tensor(ptr noundef nonnull %4)
  br label %29

29:                                               ; preds = %._crit_edge29, %25
  call void @llvm.lifetime.end.p0(i64 24, ptr nonnull %4) #4
  ret void
}

; Function Attrs: nounwind uwtable
declare dso_local void @destruct_tensor(ptr noundef) local_unnamed_addr #1

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

; Function Attrs: mustprogress nounwind willreturn allockind("realloc") allocsize(1) memory(argmem: readwrite, inaccessiblemem: readwrite)
declare noalias noundef ptr @realloc(ptr allocptr nocapture noundef, i64 noundef) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
declare dso_local void @copyTensor(ptr dead_on_unwind noalias writable sret(%struct.tensor) align 8, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @preCond(ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @pivot(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @normWithPivotValue(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @eliminationStage1(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @eliminationStage2(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @backSwap(ptr noundef, ptr noundef) local_unnamed_addr #1

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { 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 #3 = { mustprogress nounwind willreturn allockind("realloc") allocsize(1) memory(argmem: readwrite, 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 #4 = { nounwind }
attributes #5 = { nounwind allocsize(1) }
attributes #6 = { 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 16}
!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 12}
!12 = !{!10, !10, i64 0}
!13 = distinct !{!13, !14, !15, !16}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!"llvm.loop.isvectorized", i32 1}
!16 = !{!"llvm.loop.unroll.runtime.disable"}
!17 = distinct !{!17, !14, !16, !15}
!18 = !{!6, !10, i64 8}
!19 = distinct !{!19, !14}
!20 = !{!6, !7, i64 0}
!21 = distinct !{!21, !14}


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