; LLVM IR from AlgoGenesis/C :: String Algorithms/Suffix Array Algorithm (Program)

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @cmp(ptr noundef, ptr noundef) #0

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

13:                                               ; preds = %34, %2
  %14 = load i32, ptr %6, align 4, !tbaa !9
  %15 = load i32, ptr %4, align 4, !tbaa !9
  %16 = icmp slt i32 %14, %15
  br i1 %16, label %18, label %17

17:                                               ; preds = %13
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #5
  br label %37

18:                                               ; preds = %13
  %19 = load i32, ptr %6, align 4, !tbaa !9
  %20 = load ptr, ptr %5, align 8, !tbaa !5
  %21 = load i32, ptr %6, align 4, !tbaa !9
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds %struct.suffix, ptr %20, i64 %22
  %24 = getelementptr inbounds %struct.suffix, ptr %23, i32 0, i32 0
  store i32 %19, ptr %24, align 8, !tbaa !11
  %25 = load ptr, ptr %3, align 8, !tbaa !5
  %26 = load i32, ptr %6, align 4, !tbaa !9
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds i8, ptr %25, i64 %27
  %29 = load ptr, ptr %5, align 8, !tbaa !5
  %30 = load i32, ptr %6, align 4, !tbaa !9
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds %struct.suffix, ptr %29, i64 %31
  %33 = getelementptr inbounds %struct.suffix, ptr %32, i32 0, i32 1
  store ptr %28, ptr %33, align 8, !tbaa !13
  br label %34

34:                                               ; preds = %18
  %35 = load i32, ptr %6, align 4, !tbaa !9
  %36 = add nsw i32 %35, 1
  store i32 %36, ptr %6, align 4, !tbaa !9
  br label %13, !llvm.loop !14

37:                                               ; preds = %17
  %38 = load ptr, ptr %5, align 8, !tbaa !5
  %39 = load i32, ptr %4, align 4, !tbaa !9
  %40 = sext i32 %39 to i64
  call void @qsort(ptr noundef %38, i64 noundef %40, i64 noundef 16, ptr noundef @cmp)
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #5
  %41 = load i32, ptr %4, align 4, !tbaa !9
  %42 = sext i32 %41 to i64
  %43 = mul i64 %42, 4
  %44 = call noalias ptr @malloc(i64 noundef %43) #6
  store ptr %44, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #5
  store i32 0, ptr %8, align 4, !tbaa !9
  br label %45

45:                                               ; preds = %61, %37
  %46 = load i32, ptr %8, align 4, !tbaa !9
  %47 = load i32, ptr %4, align 4, !tbaa !9
  %48 = icmp slt i32 %46, %47
  br i1 %48, label %50, label %49

49:                                               ; preds = %45
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #5
  br label %64

50:                                               ; preds = %45
  %51 = load ptr, ptr %5, align 8, !tbaa !5
  %52 = load i32, ptr %8, align 4, !tbaa !9
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds %struct.suffix, ptr %51, i64 %53
  %55 = getelementptr inbounds %struct.suffix, ptr %54, i32 0, i32 0
  %56 = load i32, ptr %55, align 8, !tbaa !11
  %57 = load ptr, ptr %7, align 8, !tbaa !5
  %58 = load i32, ptr %8, align 4, !tbaa !9
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds i32, ptr %57, i64 %59
  store i32 %56, ptr %60, align 4, !tbaa !9
  br label %61

61:                                               ; preds = %50
  %62 = load i32, ptr %8, align 4, !tbaa !9
  %63 = add nsw i32 %62, 1
  store i32 %63, ptr %8, align 4, !tbaa !9
  br label %45, !llvm.loop !16

64:                                               ; preds = %49
  %65 = load ptr, ptr %5, align 8, !tbaa !5
  call void @free(ptr noundef %65) #5
  %66 = load ptr, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  ret ptr %66
}

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

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #2

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

declare void @qsort(ptr noundef, i64 noundef, i64 noundef, ptr noundef) #3

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

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { "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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"suffix", !10, i64 0, !6, i64 8}
!13 = !{!12, !6, i64 8}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}


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

; Function Attrs: nounwind uwtable
declare dso_local i32 @cmp(ptr noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
define dso_local noalias noundef ptr @buildSuffixArray(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = sext i32 %1 to i64
  %4 = shl nsw i64 %3, 4
  %5 = tail call noalias ptr @malloc(i64 noundef %4) #4
  %6 = icmp sgt i32 %1, 0
  br i1 %6, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %2
  %wide.trip.count = zext nneg i32 %1 to i64
  %xtraiter = and i64 %wide.trip.count, 3
  %7 = icmp ult i32 %1, 4
  br i1 %7, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %wide.trip.count, 2147483644
  br label %.lr.ph

._crit_edge.loopexit.unr-lcssa:                   ; preds = %.lr.ph, %.lr.ph.preheader
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next.3, %.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.loopexit.unr-lcssa, %.lr.ph.epil
  %indvars.iv.epil = phi i64 [ %indvars.iv.next.epil, %.lr.ph.epil ], [ %indvars.iv.unr, %._crit_edge.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.lr.ph.epil ], [ 0, %._crit_edge.loopexit.unr-lcssa ]
  %8 = getelementptr inbounds %struct.suffix, ptr %5, i64 %indvars.iv.epil
  %9 = trunc nuw nsw i64 %indvars.iv.epil to i32
  store i32 %9, ptr %8, align 8, !tbaa !5
  %10 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv.epil
  %11 = getelementptr inbounds i8, ptr %8, i64 8
  store ptr %10, ptr %11, align 8, !tbaa !11
  %indvars.iv.next.epil = add nuw nsw i64 %indvars.iv.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 !12

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %.lr.ph.epil, %2
  tail call void @qsort(ptr noundef %5, i64 noundef %3, i64 noundef 16, ptr noundef nonnull @cmp) #5
  %12 = shl nsw i64 %3, 2
  %13 = tail call noalias ptr @malloc(i64 noundef %12) #4
  br i1 %6, label %.lr.ph27.preheader, label %._crit_edge28

.lr.ph27.preheader:                               ; preds = %._crit_edge
  %wide.trip.count33 = zext nneg i32 %1 to i64
  %min.iters.check = icmp ult i32 %1, 9
  br i1 %min.iters.check, label %.lr.ph27.preheader35, label %vector.ph

.lr.ph27.preheader35:                             ; preds = %vector.body, %.lr.ph27.preheader
  %indvars.iv30.ph = phi i64 [ 0, %.lr.ph27.preheader ], [ %n.vec, %vector.body ]
  br label %.lr.ph27

vector.ph:                                        ; preds = %.lr.ph27.preheader
  %n.mod.vf = and i64 %wide.trip.count33, 7
  %14 = icmp eq i64 %n.mod.vf, 0
  %15 = select i1 %14, i64 8, i64 %n.mod.vf
  %n.vec = sub nsw i64 %wide.trip.count33, %15
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %16 = or disjoint i64 %index, 1
  %17 = or disjoint i64 %index, 2
  %18 = or disjoint i64 %index, 3
  %19 = or disjoint i64 %index, 4
  %20 = or disjoint i64 %index, 5
  %21 = or disjoint i64 %index, 6
  %22 = or disjoint i64 %index, 7
  %23 = getelementptr inbounds %struct.suffix, ptr %5, i64 %index
  %24 = getelementptr inbounds %struct.suffix, ptr %5, i64 %16
  %25 = getelementptr inbounds %struct.suffix, ptr %5, i64 %17
  %26 = getelementptr inbounds %struct.suffix, ptr %5, i64 %18
  %27 = getelementptr inbounds %struct.suffix, ptr %5, i64 %19
  %28 = getelementptr inbounds %struct.suffix, ptr %5, i64 %20
  %29 = getelementptr inbounds %struct.suffix, ptr %5, i64 %21
  %30 = getelementptr inbounds %struct.suffix, ptr %5, i64 %22
  %31 = load i32, ptr %23, align 8, !tbaa !5
  %32 = load i32, ptr %24, align 8, !tbaa !5
  %33 = load i32, ptr %25, align 8, !tbaa !5
  %34 = load i32, ptr %26, align 8, !tbaa !5
  %35 = insertelement <4 x i32> poison, i32 %31, i64 0
  %36 = insertelement <4 x i32> %35, i32 %32, i64 1
  %37 = insertelement <4 x i32> %36, i32 %33, i64 2
  %38 = insertelement <4 x i32> %37, i32 %34, i64 3
  %39 = load i32, ptr %27, align 8, !tbaa !5
  %40 = load i32, ptr %28, align 8, !tbaa !5
  %41 = load i32, ptr %29, align 8, !tbaa !5
  %42 = load i32, ptr %30, align 8, !tbaa !5
  %43 = insertelement <4 x i32> poison, i32 %39, i64 0
  %44 = insertelement <4 x i32> %43, i32 %40, i64 1
  %45 = insertelement <4 x i32> %44, i32 %41, i64 2
  %46 = insertelement <4 x i32> %45, i32 %42, i64 3
  %47 = getelementptr inbounds i32, ptr %13, i64 %index
  %48 = getelementptr inbounds i8, ptr %47, i64 16
  store <4 x i32> %38, ptr %47, align 4, !tbaa !14
  store <4 x i32> %46, ptr %48, align 4, !tbaa !14
  %index.next = add nuw i64 %index, 8
  %49 = icmp eq i64 %index.next, %n.vec
  br i1 %49, label %.lr.ph27.preheader35, label %vector.body, !llvm.loop !15

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader.new ], [ %indvars.iv.next.3, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.3, %.lr.ph ]
  %50 = getelementptr inbounds %struct.suffix, ptr %5, i64 %indvars.iv
  %51 = trunc nuw nsw i64 %indvars.iv to i32
  store i32 %51, ptr %50, align 8, !tbaa !5
  %52 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv
  %53 = getelementptr inbounds i8, ptr %50, i64 8
  store ptr %52, ptr %53, align 8, !tbaa !11
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %54 = getelementptr inbounds %struct.suffix, ptr %5, i64 %indvars.iv.next
  %55 = trunc nuw nsw i64 %indvars.iv.next to i32
  store i32 %55, ptr %54, align 8, !tbaa !5
  %56 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv.next
  %57 = getelementptr inbounds i8, ptr %54, i64 8
  store ptr %56, ptr %57, align 8, !tbaa !11
  %indvars.iv.next.1 = or disjoint i64 %indvars.iv, 2
  %58 = getelementptr inbounds %struct.suffix, ptr %5, i64 %indvars.iv.next.1
  %59 = trunc nuw nsw i64 %indvars.iv.next.1 to i32
  store i32 %59, ptr %58, align 8, !tbaa !5
  %60 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv.next.1
  %61 = getelementptr inbounds i8, ptr %58, i64 8
  store ptr %60, ptr %61, align 8, !tbaa !11
  %indvars.iv.next.2 = or disjoint i64 %indvars.iv, 3
  %62 = getelementptr inbounds %struct.suffix, ptr %5, i64 %indvars.iv.next.2
  %63 = trunc nuw nsw i64 %indvars.iv.next.2 to i32
  store i32 %63, ptr %62, align 8, !tbaa !5
  %64 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv.next.2
  %65 = getelementptr inbounds i8, ptr %62, i64 8
  store ptr %64, ptr %65, align 8, !tbaa !11
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 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.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !19

._crit_edge28:                                    ; preds = %.lr.ph27, %._crit_edge
  tail call void @free(ptr noundef %5) #5
  ret ptr %13

.lr.ph27:                                         ; preds = %.lr.ph27.preheader35, %.lr.ph27
  %indvars.iv30 = phi i64 [ %indvars.iv.next31, %.lr.ph27 ], [ %indvars.iv30.ph, %.lr.ph27.preheader35 ]
  %66 = getelementptr inbounds %struct.suffix, ptr %5, i64 %indvars.iv30
  %67 = load i32, ptr %66, align 8, !tbaa !5
  %68 = getelementptr inbounds i32, ptr %13, i64 %indvars.iv30
  store i32 %67, ptr %68, align 4, !tbaa !14
  %indvars.iv.next31 = add nuw nsw i64 %indvars.iv30, 1
  %exitcond34.not = icmp eq i64 %indvars.iv.next31, %wide.trip.count33
  br i1 %exitcond34.not, label %._crit_edge28, label %.lr.ph27, !llvm.loop !20
}

; 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: nofree
declare void @qsort(ptr noundef, i64 noundef, i64 noundef, ptr nocapture 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,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 = { nofree "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) }
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, !7, i64 0}
!6 = !{!"suffix", !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 = distinct !{!12, !13}
!13 = !{!"llvm.loop.unroll.disable"}
!14 = !{!7, !7, i64 0}
!15 = distinct !{!15, !16, !17, !18}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!"llvm.loop.isvectorized", i32 1}
!18 = !{!"llvm.loop.unroll.runtime.disable"}
!19 = distinct !{!19, !16}
!20 = distinct !{!20, !16, !18, !17}


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