; LLVM IR from bartobri/data-structures-c :: symbol-table/patricia-trie (st)

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

@head = external hidden global ptr, align 8
@STselectR.c = external hidden global i32, align 4

; 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 ptr @STselectR(ptr noundef %0, i32 noundef %1, i32 noundef %2) #1 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store i32 %2, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %10 = load ptr, ptr %5, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.STnode, ptr %10, i32 0, i32 3
  %12 = load i32, ptr %11, align 8, !tbaa !11
  %13 = load i32, ptr %6, align 4, !tbaa !9
  %14 = icmp sle i32 %12, %13
  br i1 %14, label %15, label %31

15:                                               ; preds = %3
  %16 = load ptr, ptr %5, align 8, !tbaa !5
  %17 = load ptr, ptr @head, align 8, !tbaa !5
  %18 = icmp ne ptr %16, %17
  br i1 %18, label %19, label %22

19:                                               ; preds = %15
  %20 = load i32, ptr @STselectR.c, align 4, !tbaa !9
  %21 = add nsw i32 %20, 1
  store i32 %21, ptr @STselectR.c, align 4, !tbaa !9
  br label %22

22:                                               ; preds = %19, %15
  %23 = load i32, ptr %7, align 4, !tbaa !9
  %24 = load i32, ptr @STselectR.c, align 4, !tbaa !9
  %25 = icmp eq i32 %23, %24
  br i1 %25, label %26, label %30

26:                                               ; preds = %22
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.STnode, ptr %27, i32 0, i32 0
  %29 = load ptr, ptr %28, align 8, !tbaa !13
  store ptr %29, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %54

30:                                               ; preds = %22
  store ptr null, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %54

31:                                               ; preds = %3
  %32 = load ptr, ptr %5, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.STnode, ptr %32, i32 0, i32 1
  %34 = load ptr, ptr %33, align 8, !tbaa !14
  %35 = load ptr, ptr %5, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.STnode, ptr %35, i32 0, i32 3
  %37 = load i32, ptr %36, align 8, !tbaa !11
  %38 = load i32, ptr %7, align 4, !tbaa !9
  %39 = call ptr @STselectR(ptr noundef %34, i32 noundef %37, i32 noundef %38)
  store ptr %39, ptr %8, align 8, !tbaa !5
  %40 = load ptr, ptr %8, align 8, !tbaa !5
  %41 = icmp ne ptr %40, null
  br i1 %41, label %42, label %44

42:                                               ; preds = %31
  %43 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %43, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %54

44:                                               ; preds = %31
  %45 = load ptr, ptr %5, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.STnode, ptr %45, i32 0, i32 2
  %47 = load ptr, ptr %46, align 8, !tbaa !15
  %48 = load ptr, ptr %5, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.STnode, ptr %48, i32 0, i32 3
  %50 = load i32, ptr %49, align 8, !tbaa !11
  %51 = load i32, ptr %7, align 4, !tbaa !9
  %52 = call ptr @STselectR(ptr noundef %47, i32 noundef %50, i32 noundef %51)
  store ptr %52, ptr %8, align 8, !tbaa !5
  %53 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %53, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %54

54:                                               ; preds = %44, %42, %30, %26
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  %55 = load ptr, ptr %4, align 8
  ret ptr %55
}

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 }

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

@head = external hidden local_unnamed_addr global ptr, align 8
@STselectR.c = external hidden local_unnamed_addr global i32, align 4

; Function Attrs: nofree nosync nounwind memory(readwrite, argmem: read, inaccessiblemem: none) uwtable
define dso_local ptr @STselectR(ptr noundef readonly %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = getelementptr inbounds i8, ptr %0, i64 24
  %5 = load i32, ptr %4, align 8, !tbaa !5
  %.not21 = icmp sgt i32 %5, %1
  br i1 %.not21, label %.lr.ph, label %tailrecurse._crit_edge

tailrecurse._crit_edge:                           ; preds = %tailrecurse, %3
  %.tr.lcssa = phi ptr [ %0, %3 ], [ %20, %tailrecurse ]
  %6 = load ptr, ptr @head, align 8, !tbaa !11
  %.not17 = icmp eq ptr %6, %.tr.lcssa
  %.pre = load i32, ptr @STselectR.c, align 4, !tbaa !12
  br i1 %.not17, label %9, label %7

7:                                                ; preds = %tailrecurse._crit_edge
  %8 = add nsw i32 %.pre, 1
  store i32 %8, ptr @STselectR.c, align 4, !tbaa !12
  br label %9

9:                                                ; preds = %7, %tailrecurse._crit_edge
  %10 = phi i32 [ %8, %7 ], [ %.pre, %tailrecurse._crit_edge ]
  %11 = icmp eq i32 %10, %2
  br i1 %11, label %12, label %.loopexit

12:                                               ; preds = %9
  %13 = load ptr, ptr %.tr.lcssa, align 8, !tbaa !13
  br label %.loopexit

.lr.ph:                                           ; preds = %3, %tailrecurse
  %14 = phi i32 [ %23, %tailrecurse ], [ %5, %3 ]
  %15 = phi ptr [ %22, %tailrecurse ], [ %4, %3 ]
  %.tr22 = phi ptr [ %20, %tailrecurse ], [ %0, %3 ]
  %16 = getelementptr inbounds i8, ptr %.tr22, i64 8
  %17 = load ptr, ptr %16, align 8, !tbaa !14
  %18 = tail call ptr @STselectR(ptr noundef %17, i32 noundef %14, i32 noundef %2)
  %.not16 = icmp eq ptr %18, null
  br i1 %.not16, label %tailrecurse, label %.loopexit

tailrecurse:                                      ; preds = %.lr.ph
  %19 = getelementptr inbounds i8, ptr %.tr22, i64 16
  %20 = load ptr, ptr %19, align 8, !tbaa !15
  %21 = load i32, ptr %15, align 8, !tbaa !5
  %22 = getelementptr inbounds i8, ptr %20, i64 24
  %23 = load i32, ptr %22, align 8, !tbaa !5
  %.not = icmp sgt i32 %23, %21
  br i1 %.not, label %.lr.ph, label %tailrecurse._crit_edge

.loopexit:                                        ; preds = %.lr.ph, %9, %12
  %.0 = phi ptr [ %13, %12 ], [ null, %9 ], [ %18, %.lr.ph ]
  ret ptr %.0
}

attributes #0 = { nofree nosync nounwind memory(readwrite, argmem: read, inaccessiblemem: none) 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" }

!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, !10, i64 24}
!6 = !{!"STnode", !7, i64 0, !7, i64 8, !7, i64 16, !10, i64 24}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!10, !10, i64 0}
!13 = !{!6, !7, i64 0}
!14 = !{!6, !7, i64 8}
!15 = !{!6, !7, i64 16}


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