; LLVM IR from rantala/string-sorting :: external (adaptive)

; 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.listrec = type { ptr, ptr, i32 }

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

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

10:                                               ; preds = %49, %2
  %11 = load i32, ptr %7, align 4, !tbaa !11
  %12 = sext i32 %11 to i64
  %13 = load i64, ptr %4, align 8, !tbaa !9
  %14 = icmp ult i64 %12, %13
  br i1 %14, label %15, label %52

15:                                               ; preds = %10
  %16 = load ptr, ptr %3, align 8, !tbaa !5
  %17 = load i32, ptr %7, align 4, !tbaa !11
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds ptr, ptr %16, i64 %18
  %20 = load ptr, ptr %19, align 8, !tbaa !5
  %21 = load ptr, ptr %6, align 8, !tbaa !5
  %22 = load i32, ptr %7, align 4, !tbaa !11
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds %struct.listrec, ptr %21, i64 %23
  %25 = getelementptr inbounds %struct.listrec, ptr %24, i32 0, i32 0
  store ptr %20, ptr %25, align 8, !tbaa !13
  %26 = load i32, ptr %7, align 4, !tbaa !11
  %27 = sext i32 %26 to i64
  %28 = load i64, ptr %4, align 8, !tbaa !9
  %29 = sub i64 %28, 1
  %30 = icmp ult i64 %27, %29
  br i1 %30, label %31, label %42

31:                                               ; preds = %15
  %32 = load ptr, ptr %6, align 8, !tbaa !5
  %33 = load i32, ptr %7, align 4, !tbaa !11
  %34 = add nsw i32 %33, 1
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds %struct.listrec, ptr %32, i64 %35
  %37 = load ptr, ptr %6, align 8, !tbaa !5
  %38 = load i32, ptr %7, align 4, !tbaa !11
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds %struct.listrec, ptr %37, i64 %39
  %41 = getelementptr inbounds %struct.listrec, ptr %40, i32 0, i32 1
  store ptr %36, ptr %41, align 8, !tbaa !15
  br label %48

42:                                               ; preds = %15
  %43 = load ptr, ptr %6, align 8, !tbaa !5
  %44 = load i32, ptr %7, align 4, !tbaa !11
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds %struct.listrec, ptr %43, i64 %45
  %47 = getelementptr inbounds %struct.listrec, ptr %46, i32 0, i32 1
  store ptr null, ptr %47, align 8, !tbaa !15
  br label %48

48:                                               ; preds = %42, %31
  br label %49

49:                                               ; preds = %48
  %50 = load i32, ptr %7, align 4, !tbaa !11
  %51 = add nsw i32 %50, 1
  store i32 %51, ptr %7, align 4, !tbaa !11
  br label %10, !llvm.loop !16

52:                                               ; preds = %10
  %53 = load ptr, ptr %6, align 8, !tbaa !5
  %54 = load i64, ptr %4, align 8, !tbaa !9
  %55 = trunc i64 %54 to i32
  %56 = call ptr @MSD(ptr noundef %53, i32 noundef %55)
  store ptr %56, ptr %6, align 8, !tbaa !5
  store i32 0, ptr %7, align 4, !tbaa !11
  br label %57

57:                                               ; preds = %70, %52
  %58 = load i32, ptr %7, align 4, !tbaa !11
  %59 = sext i32 %58 to i64
  %60 = load i64, ptr %4, align 8, !tbaa !9
  %61 = icmp ult i64 %59, %60
  br i1 %61, label %62, label %76

62:                                               ; preds = %57
  %63 = load ptr, ptr %6, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.listrec, ptr %63, i32 0, i32 0
  %65 = load ptr, ptr %64, align 8, !tbaa !13
  %66 = load ptr, ptr %3, align 8, !tbaa !5
  %67 = load i32, ptr %7, align 4, !tbaa !11
  %68 = sext i32 %67 to i64
  %69 = getelementptr inbounds ptr, ptr %66, i64 %68
  store ptr %65, ptr %69, align 8, !tbaa !5
  br label %70

70:                                               ; preds = %62
  %71 = load i32, ptr %7, align 4, !tbaa !11
  %72 = add nsw i32 %71, 1
  store i32 %72, ptr %7, align 4, !tbaa !11
  %73 = load ptr, ptr %6, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.listrec, ptr %73, i32 0, i32 1
  %75 = load ptr, ptr %74, align 8, !tbaa !15
  store ptr %75, ptr %6, align 8, !tbaa !5
  br label %57, !llvm.loop !18

76:                                               ; preds = %57
  %77 = load ptr, ptr %5, align 8, !tbaa !5
  call void @free(ptr noundef %77) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  ret void
}

; Function Attrs: nounwind allocsize(0,1)
declare noalias ptr @calloc(i64 noundef, i64 noundef) #3

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @MSD(ptr noundef, i32 noundef) #4

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { 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 allocsize(0,1) "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 = { inlinehint 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 #5 = { nounwind }
attributes #6 = { nounwind allocsize(0,1) }

!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, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !6, i64 0}
!14 = !{!"listrec", !6, i64 0, !6, i64 8, !12, i64 16}
!15 = !{!14, !6, i64 8}
!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"

%struct.listrec = type { ptr, ptr, i32 }

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

; Function Attrs: nounwind uwtable
define dso_local void @arssort(ptr nocapture noundef %0, i64 noundef %1) local_unnamed_addr #1 {
  %3 = tail call noalias ptr @calloc(i64 noundef %1, i64 noundef 24) #4
  %.not = icmp eq i64 %1, 0
  br i1 %.not, label %._crit_edge.thread, label %.lr.ph

._crit_edge.thread:                               ; preds = %2
  %4 = tail call ptr @MSD(ptr noundef %3, i32 noundef 0)
  br label %._crit_edge35

.lr.ph:                                           ; preds = %2
  %5 = add i64 %1, -1
  %xtraiter = and i64 %1, 1
  %6 = icmp eq i64 %5, 0
  br i1 %6, label %.lr.ph34.preheader.unr-lcssa, label %.lr.ph.new

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

7:                                                ; preds = %7, %.lr.ph.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.new ], [ %indvars.iv.next.1, %7 ]
  %niter = phi i64 [ 0, %.lr.ph.new ], [ %niter.next.1, %7 ]
  %8 = getelementptr inbounds ptr, ptr %0, i64 %indvars.iv
  %9 = load ptr, ptr %8, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.listrec, ptr %3, i64 %indvars.iv
  store ptr %9, ptr %10, align 8, !tbaa !9
  %11 = icmp ugt i64 %5, %indvars.iv
  %12 = getelementptr inbounds i8, ptr %10, i64 24
  %spec.select = select i1 %11, ptr %12, ptr null
  %13 = getelementptr inbounds i8, ptr %10, i64 8
  store ptr %spec.select, ptr %13, align 8
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %14 = getelementptr inbounds ptr, ptr %0, i64 %indvars.iv.next
  %15 = load ptr, ptr %14, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.listrec, ptr %3, i64 %indvars.iv.next
  store ptr %15, ptr %16, align 8, !tbaa !9
  %17 = icmp ugt i64 %5, %indvars.iv.next
  %18 = getelementptr inbounds i8, ptr %16, i64 24
  %spec.select.1 = select i1 %17, ptr %18, ptr null
  %19 = getelementptr inbounds i8, ptr %16, i64 8
  store ptr %spec.select.1, ptr %19, align 8
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %.lr.ph34.preheader.unr-lcssa, label %7, !llvm.loop !12

.lr.ph34.preheader.unr-lcssa:                     ; preds = %7, %.lr.ph
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next.1, %7 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.lr.ph34.preheader, label %.lr.ph34.preheader.epilog-lcssa

.lr.ph34.preheader.epilog-lcssa:                  ; preds = %.lr.ph34.preheader.unr-lcssa
  %20 = getelementptr inbounds ptr, ptr %0, i64 %indvars.iv.unr
  %21 = load ptr, ptr %20, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.listrec, ptr %3, i64 %indvars.iv.unr
  store ptr %21, ptr %22, align 8, !tbaa !9
  %23 = icmp ugt i64 %5, %indvars.iv.unr
  %24 = getelementptr inbounds i8, ptr %22, i64 24
  %spec.select.epil = select i1 %23, ptr %24, ptr null
  %25 = getelementptr inbounds i8, ptr %22, i64 8
  store ptr %spec.select.epil, ptr %25, align 8
  br label %.lr.ph34.preheader

.lr.ph34.preheader:                               ; preds = %.lr.ph34.preheader.unr-lcssa, %.lr.ph34.preheader.epilog-lcssa
  %26 = trunc i64 %1 to i32
  %27 = tail call ptr @MSD(ptr noundef nonnull %3, i32 noundef %26)
  %xtraiter42 = and i64 %1, 3
  %28 = icmp ult i64 %1, 4
  br i1 %28, label %._crit_edge35.loopexit.unr-lcssa, label %.lr.ph34.preheader.new

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

.lr.ph34:                                         ; preds = %.lr.ph34, %.lr.ph34.preheader.new
  %indvars.iv38 = phi i64 [ 0, %.lr.ph34.preheader.new ], [ %indvars.iv.next39.3, %.lr.ph34 ]
  %.02531 = phi ptr [ %27, %.lr.ph34.preheader.new ], [ %44, %.lr.ph34 ]
  %niter45 = phi i64 [ 0, %.lr.ph34.preheader.new ], [ %niter45.next.3, %.lr.ph34 ]
  %29 = load ptr, ptr %.02531, align 8, !tbaa !9
  %30 = getelementptr inbounds ptr, ptr %0, i64 %indvars.iv38
  store ptr %29, ptr %30, align 8, !tbaa !5
  %indvars.iv.next39 = or disjoint i64 %indvars.iv38, 1
  %31 = getelementptr inbounds i8, ptr %.02531, i64 8
  %32 = load ptr, ptr %31, align 8, !tbaa !14
  %33 = load ptr, ptr %32, align 8, !tbaa !9
  %34 = getelementptr inbounds ptr, ptr %0, i64 %indvars.iv.next39
  store ptr %33, ptr %34, align 8, !tbaa !5
  %indvars.iv.next39.1 = or disjoint i64 %indvars.iv38, 2
  %35 = getelementptr inbounds i8, ptr %32, i64 8
  %36 = load ptr, ptr %35, align 8, !tbaa !14
  %37 = load ptr, ptr %36, align 8, !tbaa !9
  %38 = getelementptr inbounds ptr, ptr %0, i64 %indvars.iv.next39.1
  store ptr %37, ptr %38, align 8, !tbaa !5
  %indvars.iv.next39.2 = or disjoint i64 %indvars.iv38, 3
  %39 = getelementptr inbounds i8, ptr %36, i64 8
  %40 = load ptr, ptr %39, align 8, !tbaa !14
  %41 = load ptr, ptr %40, align 8, !tbaa !9
  %42 = getelementptr inbounds ptr, ptr %0, i64 %indvars.iv.next39.2
  store ptr %41, ptr %42, align 8, !tbaa !5
  %indvars.iv.next39.3 = add nuw nsw i64 %indvars.iv38, 4
  %43 = getelementptr inbounds i8, ptr %40, i64 8
  %44 = load ptr, ptr %43, align 8, !tbaa !14
  %niter45.next.3 = add i64 %niter45, 4
  %niter45.ncmp.3 = icmp eq i64 %niter45.next.3, %unroll_iter44
  br i1 %niter45.ncmp.3, label %._crit_edge35.loopexit.unr-lcssa, label %.lr.ph34, !llvm.loop !15

._crit_edge35.loopexit.unr-lcssa:                 ; preds = %.lr.ph34, %.lr.ph34.preheader
  %indvars.iv38.unr = phi i64 [ 0, %.lr.ph34.preheader ], [ %indvars.iv.next39.3, %.lr.ph34 ]
  %.02531.unr = phi ptr [ %27, %.lr.ph34.preheader ], [ %44, %.lr.ph34 ]
  %lcmp.mod43.not = icmp eq i64 %xtraiter42, 0
  br i1 %lcmp.mod43.not, label %._crit_edge35, label %.lr.ph34.epil

.lr.ph34.epil:                                    ; preds = %._crit_edge35.loopexit.unr-lcssa, %.lr.ph34.epil
  %indvars.iv38.epil = phi i64 [ %indvars.iv.next39.epil, %.lr.ph34.epil ], [ %indvars.iv38.unr, %._crit_edge35.loopexit.unr-lcssa ]
  %.02531.epil = phi ptr [ %48, %.lr.ph34.epil ], [ %.02531.unr, %._crit_edge35.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.lr.ph34.epil ], [ 0, %._crit_edge35.loopexit.unr-lcssa ]
  %45 = load ptr, ptr %.02531.epil, align 8, !tbaa !9
  %46 = getelementptr inbounds ptr, ptr %0, i64 %indvars.iv38.epil
  store ptr %45, ptr %46, align 8, !tbaa !5
  %indvars.iv.next39.epil = add nuw nsw i64 %indvars.iv38.epil, 1
  %47 = getelementptr inbounds i8, ptr %.02531.epil, i64 8
  %48 = load ptr, ptr %47, align 8, !tbaa !14
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter42
  br i1 %epil.iter.cmp.not, label %._crit_edge35, label %.lr.ph34.epil, !llvm.loop !16

._crit_edge35:                                    ; preds = %._crit_edge35.loopexit.unr-lcssa, %.lr.ph34.epil, %._crit_edge.thread
  tail call void @free(ptr noundef %3) #5
  ret void
}

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @MSD(ptr noundef, i32 noundef) local_unnamed_addr #3

attributes #0 = { 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 #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 = { mustprogress nofree nounwind willreturn allockind("alloc,zeroed") allocsize(0,1) 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 #3 = { inlinehint 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 allocsize(0,1) }
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/C++ TBAA"}
!9 = !{!10, !6, i64 0}
!10 = !{!"listrec", !6, i64 0, !6, i64 8, !11, i64 16}
!11 = !{!"int", !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!10, !6, i64 8}
!15 = distinct !{!15, !13}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.unroll.disable"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,MergedLoadStoreMotionPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass