; LLVM IR from rantala/string-sorting :: src/msd_a.ll (Z14msd_A_adaptivePPhm)

; 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.cacheblock_t = type { [4 x i8], 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 allocsize(0)
declare noalias ptr @malloc(i64 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: mustprogress nounwind uwtable
declare hidden void @_ZL10fill_cacheP12cacheblock_tmm(ptr noundef, i64 noundef, i64 noundef) #2

; Function Attrs: nounwind
declare void @free(ptr noundef) #3

; Function Attrs: mustprogress uwtable
define dso_local void @_Z14msd_A_adaptivePPhm(ptr noundef %0, i64 noundef %1) #4 {
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i64 %1, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #5
  %8 = load i64, ptr %4, align 8, !tbaa !9
  %9 = mul i64 %8, 16
  %10 = call noalias ptr @malloc(i64 noundef %9) #6
  store ptr %10, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  store i64 0, ptr %6, align 8, !tbaa !9
  br label %11

11:                                               ; preds = %25, %2
  %12 = load i64, ptr %6, align 8, !tbaa !9
  %13 = load i64, ptr %4, align 8, !tbaa !9
  %14 = icmp ult i64 %12, %13
  br i1 %14, label %16, label %15

15:                                               ; preds = %11
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  br label %28

16:                                               ; preds = %11
  %17 = load ptr, ptr %3, align 8, !tbaa !5
  %18 = load i64, ptr %6, align 8, !tbaa !9
  %19 = getelementptr inbounds ptr, ptr %17, i64 %18
  %20 = load ptr, ptr %19, align 8, !tbaa !5
  %21 = load ptr, ptr %5, align 8, !tbaa !5
  %22 = load i64, ptr %6, align 8, !tbaa !9
  %23 = getelementptr inbounds %struct.cacheblock_t, ptr %21, i64 %22
  %24 = getelementptr inbounds %struct.cacheblock_t, ptr %23, i32 0, i32 1
  store ptr %20, ptr %24, align 8, !tbaa !11
  br label %25

25:                                               ; preds = %16
  %26 = load i64, ptr %6, align 8, !tbaa !9
  %27 = add i64 %26, 1
  store i64 %27, ptr %6, align 8, !tbaa !9
  br label %11, !llvm.loop !13

28:                                               ; preds = %15
  %29 = load ptr, ptr %5, align 8, !tbaa !5
  %30 = load i64, ptr %4, align 8, !tbaa !9
  call void @_ZL10fill_cacheP12cacheblock_tmm(ptr noundef %29, i64 noundef %30, i64 noundef 0)
  %31 = load ptr, ptr %5, align 8, !tbaa !5
  %32 = load i64, ptr %4, align 8, !tbaa !9
  call void @_ZL14msd_A_adaptiveP12cacheblock_tmmm(ptr noundef %31, i64 noundef %32, i64 noundef 0, i64 noundef 0)
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #5
  store i64 0, ptr %7, align 8, !tbaa !9
  br label %33

33:                                               ; preds = %47, %28
  %34 = load i64, ptr %7, align 8, !tbaa !9
  %35 = load i64, ptr %4, align 8, !tbaa !9
  %36 = icmp ult i64 %34, %35
  br i1 %36, label %38, label %37

37:                                               ; preds = %33
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #5
  br label %50

38:                                               ; preds = %33
  %39 = load ptr, ptr %5, align 8, !tbaa !5
  %40 = load i64, ptr %7, align 8, !tbaa !9
  %41 = getelementptr inbounds %struct.cacheblock_t, ptr %39, i64 %40
  %42 = getelementptr inbounds %struct.cacheblock_t, ptr %41, i32 0, i32 1
  %43 = load ptr, ptr %42, align 8, !tbaa !11
  %44 = load ptr, ptr %3, align 8, !tbaa !5
  %45 = load i64, ptr %7, align 8, !tbaa !9
  %46 = getelementptr inbounds ptr, ptr %44, i64 %45
  store ptr %43, ptr %46, align 8, !tbaa !5
  br label %47

47:                                               ; preds = %38
  %48 = load i64, ptr %7, align 8, !tbaa !9
  %49 = add i64 %48, 1
  store i64 %49, ptr %7, align 8, !tbaa !9
  br label %33, !llvm.loop !15

50:                                               ; preds = %37
  %51 = load ptr, ptr %5, align 8, !tbaa !5
  call void @free(ptr noundef %51) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  ret void
}

; Function Attrs: mustprogress uwtable
declare hidden void @_ZL14msd_A_adaptiveP12cacheblock_tmmm(ptr noundef, i64 noundef, i64 noundef, i64 noundef) #4

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { 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 #3 = { nounwind "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 = { mustprogress 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 #5 = { nounwind }
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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !6, i64 8}
!12 = !{!"_ZTS12cacheblock_t", !7, i64 0, !6, i64 8}
!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"

%struct.cacheblock_t = type { [4 x i8], ptr }

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

; Function Attrs: mustprogress nounwind uwtable
declare hidden void @_ZL10fill_cacheP12cacheblock_tmm(ptr noundef, i64 noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #2

; Function Attrs: mustprogress uwtable
define dso_local void @_Z14msd_A_adaptivePPhm(ptr nocapture noundef %0, i64 noundef %1) local_unnamed_addr #3 {
  %3 = shl i64 %1, 4
  %4 = tail call noalias ptr @malloc(i64 noundef %3) #4
  %.not = icmp eq i64 %1, 0
  br i1 %.not, label %._crit_edge25.critedge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %2
  %xtraiter = and i64 %1, 3
  %5 = icmp ult i64 %1, 4
  br i1 %5, label %._crit_edge.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %1, -4
  br label %.lr.ph

._crit_edge.unr-lcssa:                            ; preds = %.lr.ph, %.lr.ph.preheader
  %.01921.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %43, %.lr.ph ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.unr-lcssa, %.lr.ph.epil
  %.01921.epil = phi i64 [ %9, %.lr.ph.epil ], [ %.01921.unr, %._crit_edge.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.lr.ph.epil ], [ 0, %._crit_edge.unr-lcssa ]
  %6 = getelementptr inbounds ptr, ptr %0, i64 %.01921.epil
  %7 = load ptr, ptr %6, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.cacheblock_t, ptr %4, i64 %.01921.epil, i32 1
  store ptr %7, ptr %8, align 8, !tbaa !9
  %9 = add nuw i64 %.01921.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, label %.lr.ph.epil, !llvm.loop !11

._crit_edge:                                      ; preds = %.lr.ph.epil, %._crit_edge.unr-lcssa
  tail call void @_ZL10fill_cacheP12cacheblock_tmm(ptr noundef %4, i64 noundef %1, i64 noundef 0)
  tail call void @_ZL14msd_A_adaptiveP12cacheblock_tmmm(ptr noundef %4, i64 noundef %1, i64 noundef 0, i64 noundef 0)
  %min.iters.check = icmp ult i64 %1, 4
  br i1 %min.iters.check, label %.lr.ph24.preheader, label %vector.ph

.lr.ph24.preheader:                               ; preds = %middle.block, %._crit_edge
  %.022.ph = phi i64 [ 0, %._crit_edge ], [ %n.vec, %middle.block ]
  br label %.lr.ph24

vector.ph:                                        ; preds = %._crit_edge
  %n.vec = and i64 %1, -4
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %10 = or disjoint i64 %index, 1
  %11 = or disjoint i64 %index, 2
  %12 = or disjoint i64 %index, 3
  %13 = getelementptr inbounds %struct.cacheblock_t, ptr %4, i64 %index, i32 1
  %14 = getelementptr inbounds %struct.cacheblock_t, ptr %4, i64 %10, i32 1
  %15 = getelementptr inbounds %struct.cacheblock_t, ptr %4, i64 %11, i32 1
  %16 = getelementptr inbounds %struct.cacheblock_t, ptr %4, i64 %12, i32 1
  %17 = load ptr, ptr %13, align 8, !tbaa !9
  %18 = load ptr, ptr %14, align 8, !tbaa !9
  %19 = insertelement <2 x ptr> poison, ptr %17, i64 0
  %20 = insertelement <2 x ptr> %19, ptr %18, i64 1
  %21 = load ptr, ptr %15, align 8, !tbaa !9
  %22 = load ptr, ptr %16, align 8, !tbaa !9
  %23 = insertelement <2 x ptr> poison, ptr %21, i64 0
  %24 = insertelement <2 x ptr> %23, ptr %22, i64 1
  %25 = getelementptr inbounds ptr, ptr %0, i64 %index
  %26 = getelementptr inbounds i8, ptr %25, i64 16
  store <2 x ptr> %20, ptr %25, align 8, !tbaa !5
  store <2 x ptr> %24, ptr %26, align 8, !tbaa !5
  %index.next = add nuw i64 %index, 4
  %27 = icmp eq i64 %index.next, %n.vec
  br i1 %27, label %middle.block, label %vector.body, !llvm.loop !13

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %n.vec, %1
  br i1 %cmp.n, label %._crit_edge25, label %.lr.ph24.preheader

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %.01921 = phi i64 [ 0, %.lr.ph.preheader.new ], [ %43, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.3, %.lr.ph ]
  %28 = getelementptr inbounds ptr, ptr %0, i64 %.01921
  %29 = load ptr, ptr %28, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.cacheblock_t, ptr %4, i64 %.01921, i32 1
  store ptr %29, ptr %30, align 8, !tbaa !9
  %31 = or disjoint i64 %.01921, 1
  %32 = getelementptr inbounds ptr, ptr %0, i64 %31
  %33 = load ptr, ptr %32, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.cacheblock_t, ptr %4, i64 %31, i32 1
  store ptr %33, ptr %34, align 8, !tbaa !9
  %35 = or disjoint i64 %.01921, 2
  %36 = getelementptr inbounds ptr, ptr %0, i64 %35
  %37 = load ptr, ptr %36, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.cacheblock_t, ptr %4, i64 %35, i32 1
  store ptr %37, ptr %38, align 8, !tbaa !9
  %39 = or disjoint i64 %.01921, 3
  %40 = getelementptr inbounds ptr, ptr %0, i64 %39
  %41 = load ptr, ptr %40, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.cacheblock_t, ptr %4, i64 %39, i32 1
  store ptr %41, ptr %42, align 8, !tbaa !9
  %43 = add nuw i64 %.01921, 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.unr-lcssa, label %.lr.ph, !llvm.loop !17

._crit_edge25.critedge:                           ; preds = %2
  tail call void @_ZL10fill_cacheP12cacheblock_tmm(ptr noundef %4, i64 noundef %1, i64 noundef 0)
  tail call void @_ZL14msd_A_adaptiveP12cacheblock_tmmm(ptr noundef %4, i64 noundef %1, i64 noundef 0, i64 noundef 0)
  br label %._crit_edge25

._crit_edge25:                                    ; preds = %.lr.ph24, %middle.block, %._crit_edge25.critedge
  tail call void @free(ptr noundef %4) #5
  ret void

.lr.ph24:                                         ; preds = %.lr.ph24.preheader, %.lr.ph24
  %.022 = phi i64 [ %47, %.lr.ph24 ], [ %.022.ph, %.lr.ph24.preheader ]
  %44 = getelementptr inbounds %struct.cacheblock_t, ptr %4, i64 %.022, i32 1
  %45 = load ptr, ptr %44, align 8, !tbaa !9
  %46 = getelementptr inbounds ptr, ptr %0, i64 %.022
  store ptr %45, ptr %46, align 8, !tbaa !5
  %47 = add nuw i64 %.022, 1
  %exitcond27.not = icmp eq i64 %47, %1
  br i1 %exitcond27.not, label %._crit_edge25, label %.lr.ph24, !llvm.loop !18
}

; Function Attrs: mustprogress uwtable
declare hidden void @_ZL14msd_A_adaptiveP12cacheblock_tmmm(ptr noundef, i64 noundef, i64 noundef, i64 noundef) local_unnamed_addr #3

attributes #0 = { 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 #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 = { mustprogress nounwind willreturn allockind("free") 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 #3 = { mustprogress 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 #4 = { nounwind allocsize(0) }
attributes #5 = { 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, !6, i64 8}
!10 = !{!"_ZTS12cacheblock_t", !7, i64 0, !6, i64 8}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.unroll.disable"}
!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}
!18 = distinct !{!18, !14, !16, !15}


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