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

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

15:                                               ; preds = %70, %5
  %16 = load i32, ptr %11, align 4, !tbaa !9
  %17 = load i32, ptr %7, align 4, !tbaa !9
  %18 = icmp slt i32 %16, %17
  br i1 %18, label %20, label %19

19:                                               ; preds = %15
  store i32 2, ptr %12, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  br label %74

20:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #2
  store i32 0, ptr %13, align 4, !tbaa !9
  br label %21

21:                                               ; preds = %66, %20
  %22 = load i32, ptr %13, align 4, !tbaa !9
  %23 = load i32, ptr %7, align 4, !tbaa !9
  %24 = icmp slt i32 %22, %23
  br i1 %24, label %26, label %25

25:                                               ; preds = %21
  store i32 5, ptr %12, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #2
  br label %69

26:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #2
  store i32 0, ptr %14, align 4, !tbaa !9
  br label %27

27:                                               ; preds = %62, %26
  %28 = load i32, ptr %14, align 4, !tbaa !9
  %29 = load i32, ptr %10, align 4, !tbaa !9
  %30 = icmp slt i32 %28, %29
  br i1 %30, label %31, label %37

31:                                               ; preds = %27
  %32 = load i32, ptr %11, align 4, !tbaa !9
  %33 = load i32, ptr %14, align 4, !tbaa !9
  %34 = add nsw i32 %32, %33
  %35 = load i32, ptr %7, align 4, !tbaa !9
  %36 = icmp slt i32 %34, %35
  br label %37

37:                                               ; preds = %31, %27
  %38 = phi i1 [ false, %27 ], [ %36, %31 ]
  br i1 %38, label %40, label %39

39:                                               ; preds = %37
  store i32 8, ptr %12, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #2
  br label %65

40:                                               ; preds = %37
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  %42 = load i32, ptr %11, align 4, !tbaa !9
  %43 = load i32, ptr %14, align 4, !tbaa !9
  %44 = add nsw i32 %42, %43
  %45 = load i32, ptr %7, align 4, !tbaa !9
  %46 = mul nsw i32 %44, %45
  %47 = load i32, ptr %13, align 4, !tbaa !9
  %48 = add nsw i32 %46, %47
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds double, ptr %41, i64 %49
  %51 = load double, ptr %50, align 8, !tbaa !11
  %52 = load ptr, ptr %9, align 8, !tbaa !5
  %53 = load i32, ptr %13, align 4, !tbaa !9
  %54 = load i32, ptr %7, align 4, !tbaa !9
  %55 = mul nsw i32 %53, %54
  %56 = load i32, ptr %11, align 4, !tbaa !9
  %57 = add nsw i32 %55, %56
  %58 = load i32, ptr %14, align 4, !tbaa !9
  %59 = add nsw i32 %57, %58
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds double, ptr %52, i64 %60
  store double %51, ptr %61, align 8, !tbaa !11
  br label %62

62:                                               ; preds = %40
  %63 = load i32, ptr %14, align 4, !tbaa !9
  %64 = add nsw i32 %63, 1
  store i32 %64, ptr %14, align 4, !tbaa !9
  br label %27, !llvm.loop !13

65:                                               ; preds = %39
  br label %66

66:                                               ; preds = %65
  %67 = load i32, ptr %13, align 4, !tbaa !9
  %68 = add nsw i32 %67, 1
  store i32 %68, ptr %13, align 4, !tbaa !9
  br label %21, !llvm.loop !15

69:                                               ; preds = %25
  br label %70

70:                                               ; preds = %69
  %71 = load i32, ptr %10, align 4, !tbaa !9
  %72 = load i32, ptr %11, align 4, !tbaa !9
  %73 = add nsw i32 %72, %71
  store i32 %73, ptr %11, align 4, !tbaa !9
  br label %15, !llvm.loop !16

74:                                               ; preds = %19
  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}
!16 = distinct !{!16, !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 @_Z21transposeMatrix_Fast1PKdiiPdi(ptr nocapture noundef readonly %0, i32 noundef %1, i32 noundef %2, ptr nocapture noundef writeonly %3, i32 noundef %4) local_unnamed_addr #0 {
  %6 = icmp sgt i32 %1, 0
  %7 = icmp sgt i32 %4, 0
  %or.cond = and i1 %6, %7
  br i1 %or.cond, label %.preheader26.us.us.preheader, label %._crit_edge32

.preheader26.us.us.preheader:                     ; preds = %5
  %8 = zext nneg i32 %1 to i64
  %9 = zext nneg i32 %4 to i64
  %10 = shl nuw nsw i64 %9, 3
  %11 = add nuw nsw i64 %8, 2305843009213693951
  %12 = mul i64 %11, %8
  %13 = shl i64 %12, 3
  %14 = add nsw i64 %9, -1
  %15 = sub nsw i32 0, %4
  %16 = shl nuw nsw i64 %8, 3
  %17 = add nsw i64 %9, -1
  %18 = getelementptr i8, ptr %3, i64 %13
  %19 = getelementptr i8, ptr %18, i64 8
  %20 = getelementptr i8, ptr %0, i64 %16
  %ident.check.not = icmp eq i32 %1, 1
  br label %.preheader26.us.us

.preheader26.us.us:                               ; preds = %.preheader26.us.us.preheader, %._crit_edge.split.us.us.us
  %indvar = phi i64 [ 0, %.preheader26.us.us.preheader ], [ %indvar.next, %._crit_edge.split.us.us.us ]
  %indvars.iv44 = phi i64 [ 0, %.preheader26.us.us.preheader ], [ %indvars.iv.next45, %._crit_edge.split.us.us.us ]
  %21 = trunc i64 %indvars.iv44 to i32
  %smax54 = tail call i32 @llvm.smax.i32(i32 %1, i32 %21)
  %22 = trunc i64 %indvar to i32
  %23 = mul i32 %22, %15
  %24 = add i32 %smax54, %23
  %25 = zext i32 %24 to i64
  %umin55 = tail call i64 @llvm.umin.i64(i64 %17, i64 %25)
  %26 = add nuw nsw i64 %umin55, 1
  %27 = mul i64 %10, %indvar
  %scevgep = getelementptr i8, ptr %3, i64 %27
  %scevgep49 = getelementptr i8, ptr %19, i64 %27
  %28 = trunc i64 %indvars.iv44 to i32
  %smax = tail call i32 @llvm.smax.i32(i32 %1, i32 %28)
  %29 = trunc i64 %indvar to i32
  %30 = mul i32 %29, %15
  %31 = add i32 %smax, %30
  %32 = zext i32 %31 to i64
  %umin = tail call i64 @llvm.umin.i64(i64 %14, i64 %32)
  %33 = shl nuw nsw i64 %umin, 3
  %scevgep50 = getelementptr i8, ptr %scevgep49, i64 %33
  %scevgep51 = getelementptr i8, ptr %0, i64 %27
  %scevgep52 = getelementptr i8, ptr %20, i64 %27
  %scevgep53 = getelementptr i8, ptr %scevgep52, i64 %33
  %invariant.gep47 = getelementptr inbounds double, ptr %3, i64 %indvars.iv44
  %min.iters.check = icmp ugt i64 %umin55, 3
  %or.cond57 = and i1 %min.iters.check, %ident.check.not
  %bound0 = icmp ult ptr %scevgep, %scevgep53
  %bound1 = icmp ult ptr %scevgep51, %scevgep50
  %found.conflict = and i1 %bound0, %bound1
  %n.mod.vf = and i64 %26, 3
  %34 = icmp eq i64 %n.mod.vf, 0
  %35 = select i1 %34, i64 4, i64 %n.mod.vf
  %n.vec = sub nsw i64 %26, %35
  br label %.preheader.us.us.us

.preheader.us.us.us:                              ; preds = %.critedge.us.us.us, %.preheader26.us.us
  %indvars.iv39 = phi i64 [ %indvars.iv.next40, %.critedge.us.us.us ], [ 0, %.preheader26.us.us ]
  %36 = mul nuw nsw i64 %indvars.iv39, %8
  %invariant.gep = getelementptr inbounds double, ptr %0, i64 %indvars.iv39
  %gep48 = getelementptr inbounds double, ptr %invariant.gep47, i64 %36
  %or.cond57.not = xor i1 %or.cond57, true
  %brmerge = select i1 %or.cond57.not, i1 true, i1 %found.conflict
  br i1 %brmerge, label %scalar.ph.preheader, label %vector.ph

vector.ph:                                        ; preds = %.preheader.us.us.us
  %invariant.gep58 = getelementptr double, ptr %invariant.gep, i64 %indvars.iv44
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %gep59 = getelementptr double, ptr %invariant.gep58, i64 %index
  %37 = getelementptr inbounds i8, ptr %gep59, i64 16
  %wide.load = load <2 x double>, ptr %gep59, align 8, !tbaa !5, !alias.scope !9
  %wide.load56 = load <2 x double>, ptr %37, align 8, !tbaa !5, !alias.scope !9
  %38 = getelementptr inbounds double, ptr %gep48, i64 %index
  %39 = getelementptr inbounds i8, ptr %38, i64 16
  store <2 x double> %wide.load, ptr %38, align 8, !tbaa !5, !alias.scope !12, !noalias !9
  store <2 x double> %wide.load56, ptr %39, align 8, !tbaa !5, !alias.scope !12, !noalias !9
  %index.next = add nuw i64 %index, 4
  %40 = icmp eq i64 %index.next, %n.vec
  br i1 %40, label %scalar.ph.preheader, label %vector.body, !llvm.loop !14

scalar.ph.preheader:                              ; preds = %vector.body, %.preheader.us.us.us
  %indvars.iv.ph = phi i64 [ 0, %.preheader.us.us.us ], [ %n.vec, %vector.body ]
  br label %scalar.ph

scalar.ph:                                        ; preds = %scalar.ph.preheader, %44
  %indvars.iv = phi i64 [ %indvars.iv.next, %44 ], [ %indvars.iv.ph, %scalar.ph.preheader ]
  %41 = add nuw nsw i64 %indvars.iv, %indvars.iv44
  %42 = trunc nuw i64 %41 to i32
  %43 = icmp slt i32 %42, %1
  br i1 %43, label %44, label %.critedge.us.us.us

.critedge.us.us.us:                               ; preds = %scalar.ph, %44
  %indvars.iv.next40 = add nuw nsw i64 %indvars.iv39, 1
  %exitcond43.not = icmp eq i64 %indvars.iv.next40, %8
  br i1 %exitcond43.not, label %._crit_edge.split.us.us.us, label %.preheader.us.us.us, !llvm.loop !18

44:                                               ; preds = %scalar.ph
  %45 = mul nuw nsw i64 %41, %8
  %gep = getelementptr inbounds double, ptr %invariant.gep, i64 %45
  %46 = load double, ptr %gep, align 8, !tbaa !5
  %47 = getelementptr inbounds double, ptr %gep48, i64 %indvars.iv
  store double %46, ptr %47, align 8, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %9
  br i1 %exitcond.not, label %.critedge.us.us.us, label %scalar.ph, !llvm.loop !19

._crit_edge.split.us.us.us:                       ; preds = %.critedge.us.us.us
  %indvars.iv.next45 = add nuw nsw i64 %indvars.iv44, %9
  %48 = trunc nuw i64 %indvars.iv.next45 to i32
  %49 = icmp slt i32 %48, %1
  %indvar.next = add i64 %indvar, 1
  br i1 %49, label %.preheader26.us.us, label %._crit_edge32, !llvm.loop !20

._crit_edge32:                                    ; preds = %._crit_edge.split.us.us.us, %5
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umin.i64(i64, i64) #1

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" }
attributes #1 = { 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, !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, !15}
!19 = distinct !{!19, !15, !16}
!20 = distinct !{!20, !15}


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