; LLVM IR from rantala/string-sorting :: src/msd_a2.ll (Z15msd_A2_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.TempSpace = type { ptr, ptr, i64 }
%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 uwtable
declare dso_local void @_ZN9TempSpaceC2EPPhm(ptr noundef nonnull align 8 dereferenceable(24), ptr noundef, i64 noundef) unnamed_addr #2 align 2

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

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

; Function Attrs: mustprogress uwtable
define dso_local void @_Z15msd_A2_adaptivePPhm(ptr noundef %0, i64 noundef %1) #2 {
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca %struct.TempSpace, align 8
  %8 = 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
  %9 = load i64, ptr %4, align 8, !tbaa !9
  %10 = mul i64 %9, 16
  %11 = call noalias ptr @malloc(i64 noundef %10) #6
  store ptr %11, 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 %12

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

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

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

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

29:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 24, ptr %7) #5
  %30 = load ptr, ptr %3, align 8, !tbaa !5
  %31 = load i64, ptr %4, align 8, !tbaa !9
  call void @_ZN9TempSpaceC2EPPhm(ptr noundef nonnull align 8 dereferenceable(24) %7, ptr noundef %30, i64 noundef %31)
  %32 = load ptr, ptr %5, align 8, !tbaa !5
  %33 = load i64, ptr %4, align 8, !tbaa !9
  call void @_ZL10fill_cacheP12cacheblock_tmm(ptr noundef %32, i64 noundef %33, i64 noundef 0)
  %34 = load ptr, ptr %5, align 8, !tbaa !5
  %35 = load i64, ptr %4, align 8, !tbaa !9
  call void @_ZL15msd_A2_adaptiveP12cacheblock_tmmmR9TempSpace(ptr noundef %34, i64 noundef %35, i64 noundef 0, i64 noundef 0, ptr noundef nonnull align 8 dereferenceable(24) %7)
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  store i64 0, ptr %8, align 8, !tbaa !9
  br label %36

36:                                               ; preds = %50, %29
  %37 = load i64, ptr %8, align 8, !tbaa !9
  %38 = load i64, ptr %4, align 8, !tbaa !9
  %39 = icmp ult i64 %37, %38
  br i1 %39, label %41, label %40

40:                                               ; preds = %36
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  br label %53

41:                                               ; preds = %36
  %42 = load ptr, ptr %5, align 8, !tbaa !5
  %43 = load i64, ptr %8, align 8, !tbaa !9
  %44 = getelementptr inbounds %struct.cacheblock_t, ptr %42, i64 %43
  %45 = getelementptr inbounds %struct.cacheblock_t, ptr %44, i32 0, i32 1
  %46 = load ptr, ptr %45, align 8, !tbaa !11
  %47 = load ptr, ptr %3, align 8, !tbaa !5
  %48 = load i64, ptr %8, align 8, !tbaa !9
  %49 = getelementptr inbounds ptr, ptr %47, i64 %48
  store ptr %46, ptr %49, align 8, !tbaa !5
  br label %50

50:                                               ; preds = %41
  %51 = load i64, ptr %8, align 8, !tbaa !9
  %52 = add i64 %51, 1
  store i64 %52, ptr %8, align 8, !tbaa !9
  br label %36, !llvm.loop !15

53:                                               ; preds = %40
  %54 = load ptr, ptr %5, align 8, !tbaa !5
  call void @free(ptr noundef %54) #5
  call void @llvm.lifetime.end.p0(i64 24, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  ret void
}

; Function Attrs: mustprogress uwtable
declare hidden void @_ZL15msd_A2_adaptiveP12cacheblock_tmmmR9TempSpace(ptr noundef, i64 noundef, i64 noundef, i64 noundef, ptr noundef nonnull align 8 dereferenceable(24)) #2

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 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 = { 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 #4 = { 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 #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.TempSpace = type { ptr, ptr, i64 }
%struct.cacheblock_t = type { [4 x i8], 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: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 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: mustprogress uwtable
declare dso_local void @_ZN9TempSpaceC2EPPhm(ptr noundef nonnull align 8 dereferenceable(24), ptr noundef, i64 noundef) unnamed_addr #2 align 2

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

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

; Function Attrs: mustprogress uwtable
define dso_local void @_Z15msd_A2_adaptivePPhm(ptr noundef %0, i64 noundef %1) local_unnamed_addr #2 {
  %3 = alloca %struct.TempSpace, align 8
  %4 = shl i64 %1, 4
  %5 = tail call noalias ptr @malloc(i64 noundef %4) #5
  %.not = icmp eq i64 %1, 0
  br i1 %.not, label %._crit_edge27.critedge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %2
  %xtraiter = and i64 %1, 3
  %6 = icmp ult i64 %1, 4
  br i1 %6, 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
  %.02123.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %44, %.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
  %.02123.epil = phi i64 [ %10, %.lr.ph.epil ], [ %.02123.unr, %._crit_edge.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.lr.ph.epil ], [ 0, %._crit_edge.unr-lcssa ]
  %7 = getelementptr inbounds ptr, ptr %0, i64 %.02123.epil
  %8 = load ptr, ptr %7, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.cacheblock_t, ptr %5, i64 %.02123.epil, i32 1
  store ptr %8, ptr %9, align 8, !tbaa !9
  %10 = add nuw i64 %.02123.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
  call void @llvm.lifetime.start.p0(i64 24, ptr nonnull %3) #6
  call void @_ZN9TempSpaceC2EPPhm(ptr noundef nonnull align 8 dereferenceable(24) %3, ptr noundef %0, i64 noundef %1)
  call void @_ZL10fill_cacheP12cacheblock_tmm(ptr noundef %5, i64 noundef %1, i64 noundef 0)
  call void @_ZL15msd_A2_adaptiveP12cacheblock_tmmmR9TempSpace(ptr noundef %5, i64 noundef %1, i64 noundef 0, i64 noundef 0, ptr noundef nonnull align 8 dereferenceable(24) %3)
  %min.iters.check = icmp ult i64 %1, 4
  br i1 %min.iters.check, label %.lr.ph26.preheader, label %vector.ph

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

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 ]
  %11 = or disjoint i64 %index, 1
  %12 = or disjoint i64 %index, 2
  %13 = or disjoint i64 %index, 3
  %14 = getelementptr inbounds %struct.cacheblock_t, ptr %5, i64 %index, i32 1
  %15 = getelementptr inbounds %struct.cacheblock_t, ptr %5, i64 %11, i32 1
  %16 = getelementptr inbounds %struct.cacheblock_t, ptr %5, i64 %12, i32 1
  %17 = getelementptr inbounds %struct.cacheblock_t, ptr %5, i64 %13, i32 1
  %18 = load ptr, ptr %14, align 8, !tbaa !9
  %19 = load ptr, ptr %15, align 8, !tbaa !9
  %20 = insertelement <2 x ptr> poison, ptr %18, i64 0
  %21 = insertelement <2 x ptr> %20, ptr %19, i64 1
  %22 = load ptr, ptr %16, align 8, !tbaa !9
  %23 = load ptr, ptr %17, align 8, !tbaa !9
  %24 = insertelement <2 x ptr> poison, ptr %22, i64 0
  %25 = insertelement <2 x ptr> %24, ptr %23, i64 1
  %26 = getelementptr inbounds ptr, ptr %0, i64 %index
  %27 = getelementptr inbounds i8, ptr %26, i64 16
  store <2 x ptr> %21, ptr %26, align 8, !tbaa !5
  store <2 x ptr> %25, ptr %27, align 8, !tbaa !5
  %index.next = add nuw i64 %index, 4
  %28 = icmp eq i64 %index.next, %n.vec
  br i1 %28, 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_edge27, label %.lr.ph26.preheader

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %.02123 = phi i64 [ 0, %.lr.ph.preheader.new ], [ %44, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.3, %.lr.ph ]
  %29 = getelementptr inbounds ptr, ptr %0, i64 %.02123
  %30 = load ptr, ptr %29, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.cacheblock_t, ptr %5, i64 %.02123, i32 1
  store ptr %30, ptr %31, align 8, !tbaa !9
  %32 = or disjoint i64 %.02123, 1
  %33 = getelementptr inbounds ptr, ptr %0, i64 %32
  %34 = load ptr, ptr %33, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.cacheblock_t, ptr %5, i64 %32, i32 1
  store ptr %34, ptr %35, align 8, !tbaa !9
  %36 = or disjoint i64 %.02123, 2
  %37 = getelementptr inbounds ptr, ptr %0, i64 %36
  %38 = load ptr, ptr %37, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.cacheblock_t, ptr %5, i64 %36, i32 1
  store ptr %38, ptr %39, align 8, !tbaa !9
  %40 = or disjoint i64 %.02123, 3
  %41 = getelementptr inbounds ptr, ptr %0, i64 %40
  %42 = load ptr, ptr %41, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.cacheblock_t, ptr %5, i64 %40, i32 1
  store ptr %42, ptr %43, align 8, !tbaa !9
  %44 = add nuw i64 %.02123, 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_edge27.critedge:                           ; preds = %2
  call void @llvm.lifetime.start.p0(i64 24, ptr nonnull %3) #6
  call void @_ZN9TempSpaceC2EPPhm(ptr noundef nonnull align 8 dereferenceable(24) %3, ptr noundef %0, i64 noundef %1)
  call void @_ZL10fill_cacheP12cacheblock_tmm(ptr noundef %5, i64 noundef %1, i64 noundef 0)
  call void @_ZL15msd_A2_adaptiveP12cacheblock_tmmmR9TempSpace(ptr noundef %5, i64 noundef %1, i64 noundef 0, i64 noundef 0, ptr noundef nonnull align 8 dereferenceable(24) %3)
  br label %._crit_edge27

._crit_edge27:                                    ; preds = %.lr.ph26, %middle.block, %._crit_edge27.critedge
  call void @free(ptr noundef %5) #6
  call void @llvm.lifetime.end.p0(i64 24, ptr nonnull %3) #6
  ret void

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

; Function Attrs: mustprogress uwtable
declare hidden void @_ZL15msd_A2_adaptiveP12cacheblock_tmmmR9TempSpace(ptr noundef, i64 noundef, i64 noundef, i64 noundef, ptr noundef nonnull align 8 dereferenceable(24)) local_unnamed_addr #2

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { 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 #3 = { 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 #4 = { 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 #5 = { nounwind allocsize(0) }
attributes #6 = { 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,SROAPass,SimplifyCFGPass,TailCallElimPass