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

; 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: 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 @frssort(ptr noundef %0, i64 noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, 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
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  call void @llvm.lifetime.start.p0(i64 8, 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 i64 0, ptr %7, align 8, !tbaa !9
  br label %10

10:                                               ; preds = %42, %2
  %11 = load i64, ptr %7, align 8, !tbaa !9
  %12 = load i64, ptr %4, align 8, !tbaa !9
  %13 = icmp ult i64 %11, %12
  br i1 %13, label %14, label %45

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

27:                                               ; preds = %14
  %28 = load ptr, ptr %6, align 8, !tbaa !5
  %29 = load i64, ptr %7, align 8, !tbaa !9
  %30 = add i64 %29, 1
  %31 = getelementptr inbounds %struct.listrec, ptr %28, i64 %30
  %32 = load ptr, ptr %6, align 8, !tbaa !5
  %33 = load i64, ptr %7, align 8, !tbaa !9
  %34 = getelementptr inbounds %struct.listrec, ptr %32, i64 %33
  %35 = getelementptr inbounds %struct.listrec, ptr %34, i32 0, i32 1
  store ptr %31, ptr %35, align 8, !tbaa !14
  br label %41

36:                                               ; preds = %14
  %37 = load ptr, ptr %6, align 8, !tbaa !5
  %38 = load i64, ptr %7, align 8, !tbaa !9
  %39 = getelementptr inbounds %struct.listrec, ptr %37, i64 %38
  %40 = getelementptr inbounds %struct.listrec, ptr %39, i32 0, i32 1
  store ptr null, ptr %40, align 8, !tbaa !14
  br label %41

41:                                               ; preds = %36, %27
  br label %42

42:                                               ; preds = %41
  %43 = load i64, ptr %7, align 8, !tbaa !9
  %44 = add i64 %43, 1
  store i64 %44, ptr %7, align 8, !tbaa !9
  br label %10, !llvm.loop !15

45:                                               ; preds = %10
  %46 = load ptr, ptr %6, align 8, !tbaa !5
  %47 = load i64, ptr %4, align 8, !tbaa !9
  %48 = trunc i64 %47 to i32
  %49 = call ptr @forward2(ptr noundef %46, i32 noundef %48)
  store ptr %49, ptr %6, align 8, !tbaa !5
  store i64 0, ptr %7, align 8, !tbaa !9
  br label %50

50:                                               ; preds = %61, %45
  %51 = load i64, ptr %7, align 8, !tbaa !9
  %52 = load i64, ptr %4, align 8, !tbaa !9
  %53 = icmp ult i64 %51, %52
  br i1 %53, label %54, label %67

54:                                               ; preds = %50
  %55 = load ptr, ptr %6, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.listrec, ptr %55, i32 0, i32 0
  %57 = load ptr, ptr %56, align 8, !tbaa !11
  %58 = load ptr, ptr %3, align 8, !tbaa !5
  %59 = load i64, ptr %7, align 8, !tbaa !9
  %60 = getelementptr inbounds ptr, ptr %58, i64 %59
  store ptr %57, ptr %60, align 8, !tbaa !5
  br label %61

61:                                               ; preds = %54
  %62 = load i64, ptr %7, align 8, !tbaa !9
  %63 = add i64 %62, 1
  store i64 %63, ptr %7, align 8, !tbaa !9
  %64 = load ptr, ptr %6, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.listrec, ptr %64, i32 0, i32 1
  %66 = load ptr, ptr %65, align 8, !tbaa !14
  store ptr %66, ptr %6, align 8, !tbaa !5
  br label %50, !llvm.loop !17

67:                                               ; preds = %50
  %68 = load ptr, ptr %5, align 8, !tbaa !5
  call void @free(ptr noundef %68) #5
  call void @llvm.lifetime.end.p0(i64 8, 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) #2

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

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

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


; 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: nounwind uwtable
define dso_local void @frssort(ptr nocapture noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %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 @forward2(ptr noundef %3, i32 noundef 0)
  br label %._crit_edge33

.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.ph32.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
  %.028 = phi i64 [ 0, %.lr.ph.new ], [ %21, %7 ]
  %niter = phi i64 [ 0, %.lr.ph.new ], [ %niter.next.1, %7 ]
  %8 = getelementptr inbounds ptr, ptr %0, i64 %.028
  %9 = load ptr, ptr %8, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.listrec, ptr %3, i64 %.028
  store ptr %9, ptr %10, align 8, !tbaa !9
  %11 = icmp ult i64 %.028, %5
  %12 = getelementptr 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
  %14 = or disjoint i64 %.028, 1
  %15 = getelementptr inbounds ptr, ptr %0, i64 %14
  %16 = load ptr, ptr %15, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.listrec, ptr %3, i64 %14
  store ptr %16, ptr %17, align 8, !tbaa !9
  %18 = icmp ult i64 %14, %5
  %19 = getelementptr i8, ptr %17, i64 24
  %spec.select.1 = select i1 %18, ptr %19, ptr null
  %20 = getelementptr inbounds i8, ptr %17, i64 8
  store ptr %spec.select.1, ptr %20, align 8
  %21 = add nuw i64 %.028, 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.ph32.preheader.unr-lcssa, label %7, !llvm.loop !12

.lr.ph32.preheader.unr-lcssa:                     ; preds = %7, %.lr.ph
  %.028.unr = phi i64 [ 0, %.lr.ph ], [ %21, %7 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.lr.ph32.preheader, label %.lr.ph32.preheader.epilog-lcssa

.lr.ph32.preheader.epilog-lcssa:                  ; preds = %.lr.ph32.preheader.unr-lcssa
  %22 = getelementptr inbounds ptr, ptr %0, i64 %.028.unr
  %23 = load ptr, ptr %22, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.listrec, ptr %3, i64 %.028.unr
  store ptr %23, ptr %24, align 8, !tbaa !9
  %25 = icmp ult i64 %.028.unr, %5
  %26 = getelementptr i8, ptr %24, i64 24
  %spec.select.epil = select i1 %25, ptr %26, ptr null
  %27 = getelementptr inbounds i8, ptr %24, i64 8
  store ptr %spec.select.epil, ptr %27, align 8
  br label %.lr.ph32.preheader

.lr.ph32.preheader:                               ; preds = %.lr.ph32.preheader.unr-lcssa, %.lr.ph32.preheader.epilog-lcssa
  %28 = trunc i64 %1 to i32
  %29 = tail call ptr @forward2(ptr noundef nonnull %3, i32 noundef %28)
  %xtraiter36 = and i64 %1, 3
  %30 = icmp ult i64 %1, 4
  br i1 %30, label %._crit_edge33.loopexit.unr-lcssa, label %.lr.ph32.preheader.new

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

.lr.ph32:                                         ; preds = %.lr.ph32, %.lr.ph32.preheader.new
  %.130 = phi i64 [ 0, %.lr.ph32.preheader.new ], [ %48, %.lr.ph32 ]
  %.02529 = phi ptr [ %29, %.lr.ph32.preheader.new ], [ %50, %.lr.ph32 ]
  %niter39 = phi i64 [ 0, %.lr.ph32.preheader.new ], [ %niter39.next.3, %.lr.ph32 ]
  %31 = load ptr, ptr %.02529, align 8, !tbaa !9
  %32 = getelementptr inbounds ptr, ptr %0, i64 %.130
  store ptr %31, ptr %32, align 8, !tbaa !5
  %33 = or disjoint i64 %.130, 1
  %34 = getelementptr inbounds i8, ptr %.02529, i64 8
  %35 = load ptr, ptr %34, align 8, !tbaa !14
  %36 = load ptr, ptr %35, align 8, !tbaa !9
  %37 = getelementptr inbounds ptr, ptr %0, i64 %33
  store ptr %36, ptr %37, align 8, !tbaa !5
  %38 = or disjoint i64 %.130, 2
  %39 = getelementptr inbounds i8, ptr %35, 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 %38
  store ptr %41, ptr %42, align 8, !tbaa !5
  %43 = or disjoint i64 %.130, 3
  %44 = getelementptr inbounds i8, ptr %40, i64 8
  %45 = load ptr, ptr %44, align 8, !tbaa !14
  %46 = load ptr, ptr %45, align 8, !tbaa !9
  %47 = getelementptr inbounds ptr, ptr %0, i64 %43
  store ptr %46, ptr %47, align 8, !tbaa !5
  %48 = add nuw i64 %.130, 4
  %49 = getelementptr inbounds i8, ptr %45, i64 8
  %50 = load ptr, ptr %49, align 8, !tbaa !14
  %niter39.next.3 = add i64 %niter39, 4
  %niter39.ncmp.3 = icmp eq i64 %niter39.next.3, %unroll_iter38
  br i1 %niter39.ncmp.3, label %._crit_edge33.loopexit.unr-lcssa, label %.lr.ph32, !llvm.loop !15

._crit_edge33.loopexit.unr-lcssa:                 ; preds = %.lr.ph32, %.lr.ph32.preheader
  %.130.unr = phi i64 [ 0, %.lr.ph32.preheader ], [ %48, %.lr.ph32 ]
  %.02529.unr = phi ptr [ %29, %.lr.ph32.preheader ], [ %50, %.lr.ph32 ]
  %lcmp.mod37.not = icmp eq i64 %xtraiter36, 0
  br i1 %lcmp.mod37.not, label %._crit_edge33, label %.lr.ph32.epil

.lr.ph32.epil:                                    ; preds = %._crit_edge33.loopexit.unr-lcssa, %.lr.ph32.epil
  %.130.epil = phi i64 [ %53, %.lr.ph32.epil ], [ %.130.unr, %._crit_edge33.loopexit.unr-lcssa ]
  %.02529.epil = phi ptr [ %55, %.lr.ph32.epil ], [ %.02529.unr, %._crit_edge33.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.lr.ph32.epil ], [ 0, %._crit_edge33.loopexit.unr-lcssa ]
  %51 = load ptr, ptr %.02529.epil, align 8, !tbaa !9
  %52 = getelementptr inbounds ptr, ptr %0, i64 %.130.epil
  store ptr %51, ptr %52, align 8, !tbaa !5
  %53 = add nuw i64 %.130.epil, 1
  %54 = getelementptr inbounds i8, ptr %.02529.epil, i64 8
  %55 = load ptr, ptr %54, align 8, !tbaa !14
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter36
  br i1 %epil.iter.cmp.not, label %._crit_edge33, label %.lr.ph32.epil, !llvm.loop !16

._crit_edge33:                                    ; preds = %._crit_edge33.loopexit.unr-lcssa, %.lr.ph32.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 #1

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

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

attributes #0 = { 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 #1 = { 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 #2 = { 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 #3 = { 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 #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,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,MergedLoadStoreMotionPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass