; LLVM IR from ayush-tulsyan/ACM-ICPC-Handbook :: code/SuffixTree.ll (Z15buildSuffixTreev)

; 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"

@text = external dso_local global [100 x i8], align 16
@root = external dso_local global ptr, align 8
@activeNode = external dso_local global ptr, align 8
@rootEnd = external dso_local global ptr, align 8
@size = external dso_local global i32, align 4

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

; 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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_Z16extendSuffixTreei(i32 noundef) #0

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z19setSuffixIndexByDFSP14SuffixTreeNodei(ptr noundef, i32 noundef) #3

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z25freeSuffixTreeByPostOrderP14SuffixTreeNode(ptr noundef) #3

; Function Attrs: mustprogress uwtable
define dso_local void @_Z15buildSuffixTreev() #3 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = call i64 @strlen(ptr noundef @text) #5
  %4 = trunc i64 %3 to i32
  store i32 %4, ptr @size, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #6
  %5 = call noalias ptr @malloc(i64 noundef 4) #7
  store ptr %5, ptr @rootEnd, align 8, !tbaa !9
  %6 = load ptr, ptr @rootEnd, align 8, !tbaa !9
  store i32 -1, ptr %6, align 4, !tbaa !5
  %7 = load ptr, ptr @rootEnd, align 8, !tbaa !9
  %8 = call noundef ptr @_Z7newNodeiPi(i32 noundef -1, ptr noundef %7)
  store ptr %8, ptr @root, align 8, !tbaa !9
  %9 = load ptr, ptr @root, align 8, !tbaa !9
  store ptr %9, ptr @activeNode, align 8, !tbaa !9
  store i32 0, ptr %1, align 4, !tbaa !5
  br label %10

10:                                               ; preds = %16, %0
  %11 = load i32, ptr %1, align 4, !tbaa !5
  %12 = load i32, ptr @size, align 4, !tbaa !5
  %13 = icmp slt i32 %11, %12
  br i1 %13, label %14, label %19

14:                                               ; preds = %10
  %15 = load i32, ptr %1, align 4, !tbaa !5
  call void @_Z16extendSuffixTreei(i32 noundef %15)
  br label %16

16:                                               ; preds = %14
  %17 = load i32, ptr %1, align 4, !tbaa !5
  %18 = add nsw i32 %17, 1
  store i32 %18, ptr %1, align 4, !tbaa !5
  br label %10, !llvm.loop !11

19:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #6
  store i32 0, ptr %2, align 4, !tbaa !5
  %20 = load ptr, ptr @root, align 8, !tbaa !9
  %21 = load i32, ptr %2, align 4, !tbaa !5
  call void @_Z19setSuffixIndexByDFSP14SuffixTreeNodei(ptr noundef %20, i32 noundef %21)
  %22 = load ptr, ptr @root, align 8, !tbaa !9
  call void @_Z25freeSuffixTreeByPostOrderP14SuffixTreeNode(ptr noundef %22)
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #6
  ret void
}

; Function Attrs: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #4

attributes #0 = { 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 #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 = { 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 willreturn memory(read) "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 willreturn memory(read) }
attributes #6 = { nounwind }
attributes #7 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; 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"

@text = external dso_local global [100 x i8], align 16
@root = external dso_local local_unnamed_addr global ptr, align 8
@activeNode = external dso_local local_unnamed_addr global ptr, align 8
@rootEnd = external dso_local local_unnamed_addr global ptr, align 8
@size = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef ptr @_Z7newNodeiPi(i32 noundef, ptr noundef) local_unnamed_addr #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 nounwind uwtable
declare dso_local void @_Z16extendSuffixTreei(i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z19setSuffixIndexByDFSP14SuffixTreeNodei(ptr noundef, i32 noundef) local_unnamed_addr #2

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z25freeSuffixTreeByPostOrderP14SuffixTreeNode(ptr noundef) local_unnamed_addr #2

; Function Attrs: mustprogress uwtable
define dso_local void @_Z15buildSuffixTreev() local_unnamed_addr #2 {
  %1 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) @text) #4
  %2 = trunc i64 %1 to i32
  store i32 %2, ptr @size, align 4, !tbaa !5
  %3 = tail call noalias dereferenceable_or_null(4) ptr @malloc(i64 noundef 4) #5
  store ptr %3, ptr @rootEnd, align 8, !tbaa !9
  store i32 -1, ptr %3, align 4, !tbaa !5
  %4 = tail call noundef ptr @_Z7newNodeiPi(i32 noundef -1, ptr noundef nonnull %3)
  store ptr %4, ptr @root, align 8, !tbaa !9
  store ptr %4, ptr @activeNode, align 8, !tbaa !9
  %5 = load i32, ptr @size, align 4, !tbaa !5
  %6 = icmp sgt i32 %5, 0
  br i1 %6, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %0, %.lr.ph
  %.04 = phi i32 [ %7, %.lr.ph ], [ 0, %0 ]
  tail call void @_Z16extendSuffixTreei(i32 noundef %.04)
  %7 = add nuw nsw i32 %.04, 1
  %8 = load i32, ptr @size, align 4, !tbaa !5
  %9 = icmp slt i32 %7, %8
  br i1 %9, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !11

._crit_edge.loopexit:                             ; preds = %.lr.ph
  %.pre = load ptr, ptr @root, align 8, !tbaa !9
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %0
  %10 = phi ptr [ %.pre, %._crit_edge.loopexit ], [ %4, %0 ]
  tail call void @_Z19setSuffixIndexByDFSP14SuffixTreeNodei(ptr noundef %10, i32 noundef 0)
  %11 = load ptr, ptr @root, align 8, !tbaa !9
  tail call void @_Z25freeSuffixTreeByPostOrderP14SuffixTreeNode(ptr noundef %11)
  ret void
}

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #3

attributes #0 = { 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 #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 nofree nounwind willreturn memory(argmem: read) "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 willreturn memory(read) }
attributes #5 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass