; LLVM IR from ryanlayer/giggle :: src (bpt)

; 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.cache_handler = type { ptr, ptr, ptr }
%struct.cache_def = type { ptr, ptr, ptr, ptr, ptr, ptr, ptr }
%struct.bpt_node = type { ptr }

@ORDER = external dso_local global i32, align 4
@bpt_node_cache_handler = external dso_local global %struct.cache_handler, align 8
@cache = external dso_local global %struct.cache_def, align 8

; 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 i32 @bpt_find_leaf(i32 noundef %0, i32 noundef %1, i32 noundef %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  store i32 %0, ptr %5, align 4, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %13 = load ptr, ptr getelementptr inbounds (%struct.cache_def, ptr @cache, i32 0, i32 3), align 8, !tbaa !9
  %14 = load i32, ptr %5, align 4, !tbaa !5
  %15 = load i32, ptr %6, align 4, !tbaa !5
  %16 = sub i32 %15, 1
  %17 = call ptr %13(i32 noundef %14, i32 noundef %16, ptr noundef @bpt_node_cache_handler)
  store ptr %17, ptr %8, align 8, !tbaa !12
  %18 = load ptr, ptr %8, align 8, !tbaa !12
  %19 = icmp eq ptr %18, null
  br i1 %19, label %20, label %21

20:                                               ; preds = %3
  store i32 0, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %79

21:                                               ; preds = %3
  br label %22

22:                                               ; preds = %54, %21
  %23 = load ptr, ptr %8, align 8, !tbaa !12
  %24 = getelementptr inbounds %struct.bpt_node, ptr %23, i32 0, i32 0
  %25 = load ptr, ptr %24, align 8, !tbaa !13
  %26 = getelementptr inbounds i32, ptr %25, i64 2
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %28 = icmp ne i32 %27, 1
  br i1 %28, label %29, label %72

29:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  %30 = load ptr, ptr %8, align 8, !tbaa !12
  %31 = load i32, ptr %7, align 4, !tbaa !5
  %32 = call i32 @bpt_find_insert_pos(ptr noundef %30, i32 noundef %31)
  store i32 %32, ptr %10, align 4, !tbaa !5
  %33 = load i32, ptr %10, align 4, !tbaa !5
  %34 = load ptr, ptr %8, align 8, !tbaa !12
  %35 = getelementptr inbounds %struct.bpt_node, ptr %34, i32 0, i32 0
  %36 = load ptr, ptr %35, align 8, !tbaa !13
  %37 = getelementptr inbounds i32, ptr %36, i64 5
  %38 = load i32, ptr %37, align 4, !tbaa !5
  %39 = icmp ult i32 %33, %38
  br i1 %39, label %40, label %54

40:                                               ; preds = %29
  %41 = load ptr, ptr %8, align 8, !tbaa !12
  %42 = getelementptr inbounds %struct.bpt_node, ptr %41, i32 0, i32 0
  %43 = load ptr, ptr %42, align 8, !tbaa !13
  %44 = getelementptr inbounds i32, ptr %43, i64 6
  %45 = load i32, ptr %10, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds i32, ptr %44, i64 %46
  %48 = load i32, ptr %47, align 4, !tbaa !5
  %49 = load i32, ptr %7, align 4, !tbaa !5
  %50 = icmp eq i32 %48, %49
  br i1 %50, label %51, label %54

51:                                               ; preds = %40
  %52 = load i32, ptr %10, align 4, !tbaa !5
  %53 = add nsw i32 %52, 1
  store i32 %53, ptr %10, align 4, !tbaa !5
  br label %54

54:                                               ; preds = %51, %40, %29
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  %55 = load ptr, ptr %8, align 8, !tbaa !12
  %56 = getelementptr inbounds %struct.bpt_node, ptr %55, i32 0, i32 0
  %57 = load ptr, ptr %56, align 8, !tbaa !13
  %58 = load i32, ptr @ORDER, align 4, !tbaa !5
  %59 = add i32 6, %58
  %60 = add i32 %59, 2
  %61 = zext i32 %60 to i64
  %62 = getelementptr inbounds i32, ptr %57, i64 %61
  %63 = load i32, ptr %10, align 4, !tbaa !5
  %64 = sext i32 %63 to i64
  %65 = getelementptr inbounds i32, ptr %62, i64 %64
  %66 = load i32, ptr %65, align 4, !tbaa !5
  %67 = sub i32 %66, 1
  store i32 %67, ptr %11, align 4, !tbaa !5
  %68 = load ptr, ptr getelementptr inbounds (%struct.cache_def, ptr @cache, i32 0, i32 3), align 8, !tbaa !9
  %69 = load i32, ptr %5, align 4, !tbaa !5
  %70 = load i32, ptr %11, align 4, !tbaa !5
  %71 = call ptr %68(i32 noundef %69, i32 noundef %70, ptr noundef @bpt_node_cache_handler)
  store ptr %71, ptr %8, align 8, !tbaa !12
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  br label %22, !llvm.loop !15

72:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  %73 = load ptr, ptr %8, align 8, !tbaa !12
  %74 = getelementptr inbounds %struct.bpt_node, ptr %73, i32 0, i32 0
  %75 = load ptr, ptr %74, align 8, !tbaa !13
  %76 = getelementptr inbounds i32, ptr %75, i64 0
  %77 = load i32, ptr %76, align 4, !tbaa !5
  store i32 %77, ptr %12, align 4, !tbaa !5
  %78 = load i32, ptr %12, align 4, !tbaa !5
  store i32 %78, ptr %4, align 4
  store i32 1, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  br label %79

79:                                               ; preds = %72, %20
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  %80 = load i32, ptr %4, align 4
  ret i32 %80
}

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

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

%struct.cache_handler = type { ptr, ptr, ptr }
%struct.cache_def = type { ptr, ptr, ptr, ptr, ptr, ptr, ptr }

@ORDER = external dso_local local_unnamed_addr global i32, align 4
@bpt_node_cache_handler = external dso_local global %struct.cache_handler, align 8
@cache = external dso_local local_unnamed_addr global %struct.cache_def, align 8

; Function Attrs: nounwind uwtable
define dso_local i32 @bpt_find_leaf(i32 noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = load ptr, ptr getelementptr inbounds (i8, ptr @cache, i64 24), align 8, !tbaa !5
  %5 = add i32 %1, -1
  %6 = tail call ptr %4(i32 noundef %0, i32 noundef %5, ptr noundef nonnull @bpt_node_cache_handler) #1
  %7 = icmp eq ptr %6, null
  br i1 %7, label %38, label %.preheader

.preheader:                                       ; preds = %3
  %8 = load ptr, ptr %6, align 8, !tbaa !10
  %9 = getelementptr inbounds i8, ptr %8, i64 8
  %10 = load i32, ptr %9, align 4, !tbaa !12
  %.not22 = icmp eq i32 %10, 1
  br i1 %.not22, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %23
  %.01823 = phi ptr [ %33, %23 ], [ %6, %.preheader ]
  %11 = tail call i32 @bpt_find_insert_pos(ptr noundef nonnull %.01823, i32 noundef %2)
  %12 = load ptr, ptr %.01823, align 8, !tbaa !10
  %13 = getelementptr inbounds i8, ptr %12, i64 20
  %14 = load i32, ptr %13, align 4, !tbaa !12
  %15 = icmp ult i32 %11, %14
  br i1 %15, label %16, label %23

16:                                               ; preds = %.lr.ph
  %17 = getelementptr inbounds i8, ptr %12, i64 24
  %18 = sext i32 %11 to i64
  %19 = getelementptr inbounds i32, ptr %17, i64 %18
  %20 = load i32, ptr %19, align 4, !tbaa !12
  %21 = icmp eq i32 %20, %2
  %22 = zext i1 %21 to i32
  %spec.select = add nuw nsw i32 %11, %22
  br label %23

23:                                               ; preds = %16, %.lr.ph
  %.019 = phi i32 [ %11, %.lr.ph ], [ %spec.select, %16 ]
  %24 = load i32, ptr @ORDER, align 4, !tbaa !12
  %25 = add i32 %24, 8
  %26 = zext i32 %25 to i64
  %27 = getelementptr inbounds i32, ptr %12, i64 %26
  %28 = sext i32 %.019 to i64
  %29 = getelementptr inbounds i32, ptr %27, i64 %28
  %30 = load i32, ptr %29, align 4, !tbaa !12
  %31 = add i32 %30, -1
  %32 = load ptr, ptr getelementptr inbounds (i8, ptr @cache, i64 24), align 8, !tbaa !5
  %33 = tail call ptr %32(i32 noundef %0, i32 noundef %31, ptr noundef nonnull @bpt_node_cache_handler) #1
  %34 = load ptr, ptr %33, align 8, !tbaa !10
  %35 = getelementptr inbounds i8, ptr %34, i64 8
  %36 = load i32, ptr %35, align 4, !tbaa !12
  %.not = icmp eq i32 %36, 1
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !14

._crit_edge:                                      ; preds = %23, %.preheader
  %.lcssa = phi ptr [ %8, %.preheader ], [ %34, %23 ]
  %37 = load i32, ptr %.lcssa, align 4, !tbaa !12
  br label %38

38:                                               ; preds = %3, %._crit_edge
  %.0 = phi i32 [ %37, %._crit_edge ], [ 0, %3 ]
  ret i32 %.0
}

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

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 = { 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 24}
!6 = !{!"cache_def", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !7, i64 32, !7, i64 40, !7, i64 48}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!11, !7, i64 0}
!11 = !{!"bpt_node", !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"int", !8, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


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