; LLVM IR from KamilSJaron/smudgeplot :: src_ploidyplot (matrix)

; 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.Double_Matrix = type { 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: nounwind uwtable
define dso_local ptr @Transpose_Matrix(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca double, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %10 = load ptr, ptr %2, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.Double_Matrix, ptr %10, i32 0, i32 0
  %12 = load i32, ptr %11, align 8, !tbaa !9
  store i32 %12, ptr %3, align 4, !tbaa !12
  %13 = load ptr, ptr %2, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.Double_Matrix, ptr %13, i32 0, i32 1
  %15 = load ptr, ptr %14, align 8, !tbaa !13
  store ptr %15, ptr %4, align 8, !tbaa !5
  store i32 0, ptr %5, align 4, !tbaa !12
  store i32 0, ptr %8, align 4, !tbaa !12
  br label %16

16:                                               ; preds = %59, %1
  %17 = load i32, ptr %8, align 4, !tbaa !12
  %18 = load i32, ptr %3, align 4, !tbaa !12
  %19 = icmp slt i32 %17, %18
  br i1 %19, label %20, label %62

20:                                               ; preds = %16
  %21 = load i32, ptr %8, align 4, !tbaa !12
  store i32 %21, ptr %6, align 4, !tbaa !12
  store i32 0, ptr %7, align 4, !tbaa !12
  br label %22

22:                                               ; preds = %50, %20
  %23 = load i32, ptr %7, align 4, !tbaa !12
  %24 = load i32, ptr %8, align 4, !tbaa !12
  %25 = icmp slt i32 %23, %24
  br i1 %25, label %26, label %53

26:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  %27 = load ptr, ptr %4, align 8, !tbaa !5
  %28 = load i32, ptr %5, align 4, !tbaa !12
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds double, ptr %27, i64 %29
  %31 = load double, ptr %30, align 8, !tbaa !14
  store double %31, ptr %9, align 8, !tbaa !14
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = load i32, ptr %6, align 4, !tbaa !12
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds double, ptr %32, i64 %34
  %36 = load double, ptr %35, align 8, !tbaa !14
  %37 = load ptr, ptr %4, align 8, !tbaa !5
  %38 = load i32, ptr %5, align 4, !tbaa !12
  %39 = add nsw i32 %38, 1
  store i32 %39, ptr %5, align 4, !tbaa !12
  %40 = sext i32 %38 to i64
  %41 = getelementptr inbounds double, ptr %37, i64 %40
  store double %36, ptr %41, align 8, !tbaa !14
  %42 = load double, ptr %9, align 8, !tbaa !14
  %43 = load ptr, ptr %4, align 8, !tbaa !5
  %44 = load i32, ptr %6, align 4, !tbaa !12
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds double, ptr %43, i64 %45
  store double %42, ptr %46, align 8, !tbaa !14
  %47 = load i32, ptr %3, align 4, !tbaa !12
  %48 = load i32, ptr %6, align 4, !tbaa !12
  %49 = add nsw i32 %48, %47
  store i32 %49, ptr %6, align 4, !tbaa !12
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  br label %50

50:                                               ; preds = %26
  %51 = load i32, ptr %7, align 4, !tbaa !12
  %52 = add nsw i32 %51, 1
  store i32 %52, ptr %7, align 4, !tbaa !12
  br label %22, !llvm.loop !16

53:                                               ; preds = %22
  %54 = load i32, ptr %3, align 4, !tbaa !12
  %55 = load i32, ptr %8, align 4, !tbaa !12
  %56 = sub nsw i32 %54, %55
  %57 = load i32, ptr %5, align 4, !tbaa !12
  %58 = add nsw i32 %57, %56
  store i32 %58, ptr %5, align 4, !tbaa !12
  br label %59

59:                                               ; preds = %53
  %60 = load i32, ptr %8, align 4, !tbaa !12
  %61 = add nsw i32 %60, 1
  store i32 %61, ptr %8, align 4, !tbaa !12
  br label %16, !llvm.loop !18

62:                                               ; preds = %16
  %63 = load ptr, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  ret ptr %63
}

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 }

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


; 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(readwrite, inaccessiblemem: none) uwtable
define dso_local noundef ptr @Transpose_Matrix(ptr noundef readonly returned %0) local_unnamed_addr #0 {
  %2 = load i32, ptr %0, align 8, !tbaa !5
  %3 = getelementptr inbounds i8, ptr %0, i64 8
  %4 = load ptr, ptr %3, align 8, !tbaa !11
  %5 = icmp sgt i32 %2, 0
  br i1 %5, label %.preheader.preheader, label %._crit_edge32

.preheader.preheader:                             ; preds = %1
  %6 = zext nneg i32 %2 to i64
  %scevgep42 = getelementptr i8, ptr %4, i64 8
  %invariant.gep59 = getelementptr i8, ptr %4, i64 8
  %ident.check.not = icmp eq i32 %2, 1
  %invariant.gep57 = getelementptr i8, ptr %4, i64 8
  br label %.preheader

.preheader:                                       ; preds = %.preheader.preheader, %._crit_edge
  %indvars.iv33 = phi i64 [ 0, %.preheader.preheader ], [ %indvars.iv.next34, %._crit_edge ]
  %.031 = phi i32 [ 0, %.preheader.preheader ], [ %41, %._crit_edge ]
  %7 = trunc i64 %indvars.iv33 to i32
  %8 = trunc i64 %indvars.iv33 to i32
  %9 = shl i64 %indvars.iv33, 3
  %10 = add i64 %9, 34359738360
  %11 = and i64 %10, 34359738360
  %12 = shl nuw nsw i64 %indvars.iv33, 3
  %scevgep44 = getelementptr i8, ptr %4, i64 %12
  %gep60 = getelementptr i8, ptr %invariant.gep59, i64 %12
  %scevgep46 = getelementptr i8, ptr %gep60, i64 %11
  %indvars40 = trunc i64 %indvars.iv33 to i32
  %.not = icmp eq i32 %indvars40, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %.preheader
  %13 = sext i32 %.031 to i64
  %min.iters.check = icmp ugt i64 %indvars.iv33, 3
  %or.cond = and i1 %min.iters.check, %ident.check.not
  br i1 %or.cond, label %vector.memcheck, label %.lr.ph.preheader55

vector.memcheck:                                  ; preds = %.lr.ph.preheader
  %14 = shl nsw i64 %13, 3
  %scevgep = getelementptr i8, ptr %4, i64 %14
  %15 = getelementptr i8, ptr %scevgep42, i64 %14
  %scevgep43 = getelementptr i8, ptr %15, i64 %11
  %bound0 = icmp ult ptr %scevgep, %scevgep46
  %bound1 = icmp ult ptr %scevgep44, %scevgep43
  %found.conflict = and i1 %bound0, %bound1
  br i1 %found.conflict, label %.lr.ph.preheader55, label %vector.ph

vector.ph:                                        ; preds = %vector.memcheck
  %n.vec = and i64 %indvars.iv33, 9223372036854775804
  %16 = mul nuw i64 %n.vec, %6
  %ind.end = add nuw i64 %indvars.iv33, %16
  %ind.end47 = add i64 %n.vec, %13
  %ind.end49 = trunc i64 %n.vec to i32
  %invariant.gep = getelementptr double, ptr %4, i64 %13
  %17 = getelementptr double, ptr %4, i64 %indvars.iv33
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %18 = mul nuw i64 %index, %6
  %gep = getelementptr double, ptr %invariant.gep, i64 %index
  %19 = getelementptr inbounds i8, ptr %gep, i64 16
  %wide.load = load <2 x double>, ptr %gep, align 8, !tbaa !12, !alias.scope !14, !noalias !17
  %wide.load52 = load <2 x double>, ptr %19, align 8, !tbaa !12, !alias.scope !14, !noalias !17
  %20 = getelementptr double, ptr %17, i64 %18
  %21 = getelementptr inbounds i8, ptr %20, i64 16
  %wide.load53 = load <2 x double>, ptr %20, align 8, !tbaa !12, !alias.scope !17
  %wide.load54 = load <2 x double>, ptr %21, align 8, !tbaa !12, !alias.scope !17
  store <2 x double> %wide.load53, ptr %gep, align 8, !tbaa !12, !alias.scope !14, !noalias !17
  store <2 x double> %wide.load54, ptr %19, align 8, !tbaa !12, !alias.scope !14, !noalias !17
  store <2 x double> %wide.load, ptr %20, align 8, !tbaa !12, !alias.scope !17
  store <2 x double> %wide.load52, ptr %21, align 8, !tbaa !12, !alias.scope !17
  %index.next = add nuw i64 %index, 4
  %22 = icmp eq i64 %index.next, %n.vec
  br i1 %22, label %middle.block, label %vector.body, !llvm.loop !19

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %indvars.iv33, %n.vec
  br i1 %cmp.n, label %._crit_edge.loopexit, label %.lr.ph.preheader55

.lr.ph.preheader55:                               ; preds = %middle.block, %vector.memcheck, %.lr.ph.preheader
  %indvars.iv35.ph = phi i64 [ %indvars.iv33, %vector.memcheck ], [ %indvars.iv33, %.lr.ph.preheader ], [ %ind.end, %middle.block ]
  %indvars.iv.ph = phi i64 [ %13, %vector.memcheck ], [ %13, %.lr.ph.preheader ], [ %ind.end47, %middle.block ]
  %.02428.ph = phi i32 [ 0, %vector.memcheck ], [ 0, %.lr.ph.preheader ], [ %ind.end49, %middle.block ]
  %23 = sub i32 %7, %.02428.ph
  %xtraiter = and i32 %23, 1
  %lcmp.mod.not = icmp eq i32 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.lr.ph.prol.loopexit, label %.lr.ph.prol

.lr.ph.prol:                                      ; preds = %.lr.ph.preheader55
  %24 = getelementptr inbounds double, ptr %4, i64 %indvars.iv.ph
  %25 = load double, ptr %24, align 8, !tbaa !12
  %26 = getelementptr inbounds double, ptr %4, i64 %indvars.iv35.ph
  %27 = load double, ptr %26, align 8, !tbaa !12
  %indvars.iv.next.prol = add nsw i64 %indvars.iv.ph, 1
  store double %27, ptr %24, align 8, !tbaa !12
  store double %25, ptr %26, align 8, !tbaa !12
  %indvars.iv.next36.prol = add nuw nsw i64 %indvars.iv35.ph, %6
  %28 = add nuw nsw i32 %.02428.ph, 1
  br label %.lr.ph.prol.loopexit

.lr.ph.prol.loopexit:                             ; preds = %.lr.ph.prol, %.lr.ph.preheader55
  %indvars.iv.next.lcssa56.unr = phi i64 [ poison, %.lr.ph.preheader55 ], [ %indvars.iv.next.prol, %.lr.ph.prol ]
  %indvars.iv35.unr = phi i64 [ %indvars.iv35.ph, %.lr.ph.preheader55 ], [ %indvars.iv.next36.prol, %.lr.ph.prol ]
  %indvars.iv.unr = phi i64 [ %indvars.iv.ph, %.lr.ph.preheader55 ], [ %indvars.iv.next.prol, %.lr.ph.prol ]
  %.02428.unr = phi i32 [ %.02428.ph, %.lr.ph.preheader55 ], [ %28, %.lr.ph.prol ]
  %29 = add i32 %8, -1
  %30 = icmp eq i32 %.02428.ph, %29
  br i1 %30, label %._crit_edge.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.prol.loopexit, %.lr.ph
  %indvars.iv35 = phi i64 [ %indvars.iv.next36.1, %.lr.ph ], [ %indvars.iv35.unr, %.lr.ph.prol.loopexit ]
  %indvars.iv = phi i64 [ %indvars.iv.next.1, %.lr.ph ], [ %indvars.iv.unr, %.lr.ph.prol.loopexit ]
  %.02428 = phi i32 [ %38, %.lr.ph ], [ %.02428.unr, %.lr.ph.prol.loopexit ]
  %31 = getelementptr inbounds double, ptr %4, i64 %indvars.iv
  %32 = load double, ptr %31, align 8, !tbaa !12
  %33 = getelementptr inbounds double, ptr %4, i64 %indvars.iv35
  %34 = load double, ptr %33, align 8, !tbaa !12
  store double %34, ptr %31, align 8, !tbaa !12
  store double %32, ptr %33, align 8, !tbaa !12
  %indvars.iv.next36 = add nuw nsw i64 %indvars.iv35, %6
  %gep58 = getelementptr double, ptr %invariant.gep57, i64 %indvars.iv
  %35 = load double, ptr %gep58, align 8, !tbaa !12
  %36 = getelementptr inbounds double, ptr %4, i64 %indvars.iv.next36
  %37 = load double, ptr %36, align 8, !tbaa !12
  %indvars.iv.next.1 = add nsw i64 %indvars.iv, 2
  store double %37, ptr %gep58, align 8, !tbaa !12
  store double %35, ptr %36, align 8, !tbaa !12
  %indvars.iv.next36.1 = add nuw nsw i64 %indvars.iv.next36, %6
  %38 = add nuw nsw i32 %.02428, 2
  %exitcond.not.1 = icmp eq i32 %38, %indvars40
  br i1 %exitcond.not.1, label %._crit_edge.loopexit, label %.lr.ph, !llvm.loop !23

._crit_edge.loopexit:                             ; preds = %.lr.ph.prol.loopexit, %.lr.ph, %middle.block
  %indvars.iv.next.lcssa = phi i64 [ %ind.end47, %middle.block ], [ %indvars.iv.next.lcssa56.unr, %.lr.ph.prol.loopexit ], [ %indvars.iv.next.1, %.lr.ph ]
  %39 = trunc nsw i64 %indvars.iv.next.lcssa to i32
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %.preheader
  %.1.lcssa = phi i32 [ %.031, %.preheader ], [ %39, %._crit_edge.loopexit ]
  %40 = sub nsw i32 %2, %indvars40
  %41 = add nsw i32 %40, %.1.lcssa
  %indvars.iv.next34 = add nuw nsw i64 %indvars.iv33, 1
  %exitcond41.not = icmp eq i64 %indvars.iv.next34, %6
  br i1 %exitcond41.not, label %._crit_edge32, label %.preheader, !llvm.loop !24

._crit_edge32:                                    ; preds = %._crit_edge, %1
  ret ptr %0
}

attributes #0 = { nofree norecurse nosync nounwind memory(readwrite, 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, !7, i64 0}
!6 = !{!"", !7, i64 0, !10, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !10, i64 8}
!12 = !{!13, !13, i64 0}
!13 = !{!"double", !8, i64 0}
!14 = !{!15}
!15 = distinct !{!15, !16}
!16 = distinct !{!16, !"LVerDomain"}
!17 = !{!18}
!18 = distinct !{!18, !16}
!19 = distinct !{!19, !20, !21, !22}
!20 = !{!"llvm.loop.mustprogress"}
!21 = !{!"llvm.loop.isvectorized", i32 1}
!22 = !{!"llvm.loop.unroll.runtime.disable"}
!23 = distinct !{!23, !20, !21}
!24 = distinct !{!24, !20}


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