; LLVM IR from emathew1/3DCompactSolver :: Utils.ll (Z15transposeMatrixPdiiS_)

; 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: 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: mustprogress nounwind uwtable
define dso_local void @_Z15transposeMatrixPdiiS_(ptr noundef %0, i32 noundef %1, i32 noundef %2, ptr noundef %3) #1 {
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store i32 %2, ptr %7, align 4, !tbaa !9
  store ptr %3, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  store i32 0, ptr %9, align 4, !tbaa !9
  br label %12

12:                                               ; preds = %45, %4
  %13 = load i32, ptr %9, align 4, !tbaa !9
  %14 = load i32, ptr %7, align 4, !tbaa !9
  %15 = icmp slt i32 %13, %14
  br i1 %15, label %17, label %16

16:                                               ; preds = %12
  store i32 2, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  br label %48

17:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  store i32 0, ptr %11, align 4, !tbaa !9
  br label %18

18:                                               ; preds = %41, %17
  %19 = load i32, ptr %11, align 4, !tbaa !9
  %20 = load i32, ptr %6, align 4, !tbaa !9
  %21 = icmp slt i32 %19, %20
  br i1 %21, label %23, label %22

22:                                               ; preds = %18
  store i32 5, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  br label %44

23:                                               ; preds = %18
  %24 = load ptr, ptr %5, align 8, !tbaa !5
  %25 = load i32, ptr %11, align 4, !tbaa !9
  %26 = load i32, ptr %7, align 4, !tbaa !9
  %27 = mul nsw i32 %25, %26
  %28 = load i32, ptr %9, align 4, !tbaa !9
  %29 = add nsw i32 %27, %28
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds double, ptr %24, i64 %30
  %32 = load double, ptr %31, align 8, !tbaa !11
  %33 = load ptr, ptr %8, align 8, !tbaa !5
  %34 = load i32, ptr %9, align 4, !tbaa !9
  %35 = load i32, ptr %6, align 4, !tbaa !9
  %36 = mul nsw i32 %34, %35
  %37 = load i32, ptr %11, align 4, !tbaa !9
  %38 = add nsw i32 %36, %37
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds double, ptr %33, i64 %39
  store double %32, ptr %40, align 8, !tbaa !11
  br label %41

41:                                               ; preds = %23
  %42 = load i32, ptr %11, align 4, !tbaa !9
  %43 = add nsw i32 %42, 1
  store i32 %43, ptr %11, align 4, !tbaa !9
  br label %18, !llvm.loop !13

44:                                               ; preds = %22
  br label %45

45:                                               ; preds = %44
  %46 = load i32, ptr %9, align 4, !tbaa !9
  %47 = add nsw i32 %46, 1
  store i32 %47, ptr %9, align 4, !tbaa !9
  br label %12, !llvm.loop !15

48:                                               ; preds = %16
  ret void
}

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

!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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"double", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


; 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 nofree norecurse nosync nounwind memory(argmem: readwrite) uwtable
define dso_local void @_Z15transposeMatrixPdiiS_(ptr nocapture noundef readonly %0, i32 noundef %1, i32 noundef %2, ptr nocapture noundef writeonly %3) local_unnamed_addr #0 {
  %5 = icmp sgt i32 %2, 0
  %6 = icmp sgt i32 %1, 0
  %or.cond = and i1 %5, %6
  br i1 %or.cond, label %.preheader.us.preheader, label %._crit_edge18

.preheader.us.preheader:                          ; preds = %4
  %7 = zext nneg i32 %2 to i64
  %8 = zext nneg i32 %1 to i64
  %9 = mul nuw nsw i64 %7, %8
  %10 = shl i64 %9, 3
  %scevgep = getelementptr i8, ptr %3, i64 %10
  %11 = add nuw nsw i64 %7, %8
  %12 = shl nuw nsw i64 %11, 3
  %13 = getelementptr i8, ptr %0, i64 %12
  %scevgep28 = getelementptr i8, ptr %13, i64 -8
  %min.iters.check = icmp ugt i32 %1, 5
  %ident.check.not = icmp eq i32 %2, 1
  %or.cond30 = and i1 %min.iters.check, %ident.check.not
  %bound0 = icmp ugt ptr %scevgep28, %3
  %bound1 = icmp ugt ptr %scevgep, %0
  %found.conflict = and i1 %bound0, %bound1
  %n.vec = and i64 %8, 2147483644
  %cmp.n = icmp eq i64 %n.vec, %8
  %xtraiter = and i64 %8, 3
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br label %.preheader.us

.preheader.us:                                    ; preds = %.preheader.us.preheader, %._crit_edge.us
  %indvars.iv21 = phi i64 [ 0, %.preheader.us.preheader ], [ %indvars.iv.next22, %._crit_edge.us ]
  %14 = mul nuw nsw i64 %indvars.iv21, %8
  %invariant.gep = getelementptr inbounds double, ptr %0, i64 %indvars.iv21
  %invariant.gep26 = getelementptr inbounds double, ptr %3, i64 %14
  %or.cond30.not = xor i1 %or.cond30, true
  %brmerge = select i1 %or.cond30.not, i1 true, i1 %found.conflict
  br i1 %brmerge, label %scalar.ph.preheader, label %vector.body

vector.body:                                      ; preds = %.preheader.us, %vector.body
  %index = phi i64 [ %index.next, %vector.body ], [ 0, %.preheader.us ]
  %15 = getelementptr inbounds double, ptr %invariant.gep, i64 %index
  %16 = getelementptr inbounds i8, ptr %15, i64 16
  %wide.load = load <2 x double>, ptr %15, align 8, !tbaa !5, !alias.scope !9
  %wide.load29 = load <2 x double>, ptr %16, align 8, !tbaa !5, !alias.scope !9
  %17 = getelementptr inbounds double, ptr %invariant.gep26, i64 %index
  %18 = getelementptr inbounds i8, ptr %17, i64 16
  store <2 x double> %wide.load, ptr %17, align 8, !tbaa !5, !alias.scope !12, !noalias !9
  store <2 x double> %wide.load29, ptr %18, align 8, !tbaa !5, !alias.scope !12, !noalias !9
  %index.next = add nuw i64 %index, 4
  %19 = icmp eq i64 %index.next, %n.vec
  br i1 %19, label %middle.block, label %vector.body, !llvm.loop !14

middle.block:                                     ; preds = %vector.body
  br i1 %cmp.n, label %._crit_edge.us, label %scalar.ph.preheader

scalar.ph.preheader:                              ; preds = %.preheader.us, %middle.block
  %indvars.iv.ph = phi i64 [ 0, %.preheader.us ], [ %n.vec, %middle.block ]
  br i1 %lcmp.mod.not, label %scalar.ph.prol.loopexit, label %scalar.ph.prol

scalar.ph.prol:                                   ; preds = %scalar.ph.preheader, %scalar.ph.prol
  %indvars.iv.prol = phi i64 [ %indvars.iv.next.prol, %scalar.ph.prol ], [ %indvars.iv.ph, %scalar.ph.preheader ]
  %prol.iter = phi i64 [ %prol.iter.next, %scalar.ph.prol ], [ 0, %scalar.ph.preheader ]
  %20 = mul nuw nsw i64 %indvars.iv.prol, %7
  %gep.prol = getelementptr inbounds double, ptr %invariant.gep, i64 %20
  %21 = load double, ptr %gep.prol, align 8, !tbaa !5
  %gep27.prol = getelementptr inbounds double, ptr %invariant.gep26, i64 %indvars.iv.prol
  store double %21, ptr %gep27.prol, align 8, !tbaa !5
  %indvars.iv.next.prol = add nuw 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 %scalar.ph.prol.loopexit, label %scalar.ph.prol, !llvm.loop !18

scalar.ph.prol.loopexit:                          ; preds = %scalar.ph.prol, %scalar.ph.preheader
  %indvars.iv.unr = phi i64 [ %indvars.iv.ph, %scalar.ph.preheader ], [ %indvars.iv.next.prol, %scalar.ph.prol ]
  %22 = sub nsw i64 %indvars.iv.ph, %8
  %23 = icmp ugt i64 %22, -4
  br i1 %23, label %._crit_edge.us, label %scalar.ph

scalar.ph:                                        ; preds = %scalar.ph.prol.loopexit, %scalar.ph
  %indvars.iv = phi i64 [ %indvars.iv.next.3, %scalar.ph ], [ %indvars.iv.unr, %scalar.ph.prol.loopexit ]
  %24 = mul nuw nsw i64 %indvars.iv, %7
  %gep = getelementptr inbounds double, ptr %invariant.gep, i64 %24
  %25 = load double, ptr %gep, align 8, !tbaa !5
  %gep27 = getelementptr inbounds double, ptr %invariant.gep26, i64 %indvars.iv
  store double %25, ptr %gep27, align 8, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %26 = mul nuw nsw i64 %indvars.iv.next, %7
  %gep.1 = getelementptr inbounds double, ptr %invariant.gep, i64 %26
  %27 = load double, ptr %gep.1, align 8, !tbaa !5
  %gep27.1 = getelementptr inbounds double, ptr %invariant.gep26, i64 %indvars.iv.next
  store double %27, ptr %gep27.1, align 8, !tbaa !5
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %28 = mul nuw nsw i64 %indvars.iv.next.1, %7
  %gep.2 = getelementptr inbounds double, ptr %invariant.gep, i64 %28
  %29 = load double, ptr %gep.2, align 8, !tbaa !5
  %gep27.2 = getelementptr inbounds double, ptr %invariant.gep26, i64 %indvars.iv.next.1
  store double %29, ptr %gep27.2, align 8, !tbaa !5
  %indvars.iv.next.2 = add nuw nsw i64 %indvars.iv, 3
  %30 = mul nuw nsw i64 %indvars.iv.next.2, %7
  %gep.3 = getelementptr inbounds double, ptr %invariant.gep, i64 %30
  %31 = load double, ptr %gep.3, align 8, !tbaa !5
  %gep27.3 = getelementptr inbounds double, ptr %invariant.gep26, i64 %indvars.iv.next.2
  store double %31, ptr %gep27.3, align 8, !tbaa !5
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 4
  %exitcond.not.3 = icmp eq i64 %indvars.iv.next.3, %8
  br i1 %exitcond.not.3, label %._crit_edge.us, label %scalar.ph, !llvm.loop !20

._crit_edge.us:                                   ; preds = %scalar.ph.prol.loopexit, %scalar.ph, %middle.block
  %indvars.iv.next22 = add nuw nsw i64 %indvars.iv21, 1
  %exitcond25.not = icmp eq i64 %indvars.iv.next22, %7
  br i1 %exitcond25.not, label %._crit_edge18, label %.preheader.us, !llvm.loop !21

._crit_edge18:                                    ; preds = %._crit_edge.us, %4
  ret void
}

attributes #0 = { mustprogress 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 = !{!"double", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10}
!10 = distinct !{!10, !11}
!11 = distinct !{!11, !"LVerDomain"}
!12 = !{!13}
!13 = distinct !{!13, !11}
!14 = distinct !{!14, !15, !16, !17}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!"llvm.loop.isvectorized", i32 1}
!17 = !{!"llvm.loop.unroll.runtime.disable"}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.unroll.disable"}
!20 = distinct !{!20, !15, !16}
!21 = distinct !{!21, !15}


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