; LLVM IR from fmela/libdict :: src (hb_tree)

; 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.hb_tree = type { ptr, i64, ptr, i64 }
%struct.hb_node = type { ptr, ptr, %union.anon, ptr, ptr }
%union.anon = type { i64 }

; 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 zeroext i1 @hb_tree_select(ptr noundef %0, i64 noundef %1, ptr noundef %2, ptr noundef %3) #1 {
  %5 = alloca i1, align 1
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i64 %1, ptr %7, align 8, !tbaa !9
  store ptr %2, ptr %8, align 8, !tbaa !5
  store ptr %3, ptr %9, align 8, !tbaa !5
  %11 = load i64, ptr %7, align 8, !tbaa !9
  %12 = load ptr, ptr %6, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.hb_tree, ptr %12, i32 0, i32 1
  %14 = load i64, ptr %13, align 8, !tbaa !11
  %15 = icmp uge i64 %11, %14
  br i1 %15, label %16, label %27

16:                                               ; preds = %4
  %17 = load ptr, ptr %8, align 8, !tbaa !5
  %18 = icmp ne ptr %17, null
  br i1 %18, label %19, label %21

19:                                               ; preds = %16
  %20 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr null, ptr %20, align 8, !tbaa !5
  br label %21

21:                                               ; preds = %19, %16
  %22 = load ptr, ptr %9, align 8, !tbaa !5
  %23 = icmp ne ptr %22, null
  br i1 %23, label %24, label %26

24:                                               ; preds = %21
  %25 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr null, ptr %25, align 8, !tbaa !5
  br label %26

26:                                               ; preds = %24, %21
  store i1 false, ptr %5, align 1
  br label %83

27:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %28 = load i64, ptr %7, align 8, !tbaa !9
  %29 = load ptr, ptr %6, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.hb_tree, ptr %29, i32 0, i32 1
  %31 = load i64, ptr %30, align 8, !tbaa !11
  %32 = udiv i64 %31, 2
  %33 = icmp uge i64 %28, %32
  br i1 %33, label %34, label %53

34:                                               ; preds = %27
  %35 = load ptr, ptr %6, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.hb_tree, ptr %35, i32 0, i32 0
  %37 = load ptr, ptr %36, align 8, !tbaa !13
  %38 = call ptr @tree_node_max(ptr noundef %37)
  store ptr %38, ptr %10, align 8, !tbaa !5
  %39 = load ptr, ptr %6, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.hb_tree, ptr %39, i32 0, i32 1
  %41 = load i64, ptr %40, align 8, !tbaa !11
  %42 = sub i64 %41, 1
  %43 = load i64, ptr %7, align 8, !tbaa !9
  %44 = sub i64 %42, %43
  store i64 %44, ptr %7, align 8, !tbaa !9
  br label %45

45:                                               ; preds = %49, %34
  %46 = load i64, ptr %7, align 8, !tbaa !9
  %47 = add i64 %46, -1
  store i64 %47, ptr %7, align 8, !tbaa !9
  %48 = icmp ne i64 %46, 0
  br i1 %48, label %49, label %52

49:                                               ; preds = %45
  %50 = load ptr, ptr %10, align 8, !tbaa !5
  %51 = call ptr @node_prev(ptr noundef %50)
  store ptr %51, ptr %10, align 8, !tbaa !5
  br label %45, !llvm.loop !14

52:                                               ; preds = %45
  br label %66

53:                                               ; preds = %27
  %54 = load ptr, ptr %6, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.hb_tree, ptr %54, i32 0, i32 0
  %56 = load ptr, ptr %55, align 8, !tbaa !13
  %57 = call ptr @tree_node_min(ptr noundef %56)
  store ptr %57, ptr %10, align 8, !tbaa !5
  br label %58

58:                                               ; preds = %62, %53
  %59 = load i64, ptr %7, align 8, !tbaa !9
  %60 = add i64 %59, -1
  store i64 %60, ptr %7, align 8, !tbaa !9
  %61 = icmp ne i64 %59, 0
  br i1 %61, label %62, label %65

62:                                               ; preds = %58
  %63 = load ptr, ptr %10, align 8, !tbaa !5
  %64 = call ptr @node_next(ptr noundef %63)
  store ptr %64, ptr %10, align 8, !tbaa !5
  br label %58, !llvm.loop !16

65:                                               ; preds = %58
  br label %66

66:                                               ; preds = %65, %52
  %67 = load ptr, ptr %8, align 8, !tbaa !5
  %68 = icmp ne ptr %67, null
  br i1 %68, label %69, label %74

69:                                               ; preds = %66
  %70 = load ptr, ptr %10, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.hb_node, ptr %70, i32 0, i32 0
  %72 = load ptr, ptr %71, align 8, !tbaa !17
  %73 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %72, ptr %73, align 8, !tbaa !5
  br label %74

74:                                               ; preds = %69, %66
  %75 = load ptr, ptr %9, align 8, !tbaa !5
  %76 = icmp ne ptr %75, null
  br i1 %76, label %77, label %82

77:                                               ; preds = %74
  %78 = load ptr, ptr %10, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.hb_node, ptr %78, i32 0, i32 1
  %80 = load ptr, ptr %79, align 8, !tbaa !19
  %81 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %80, ptr %81, align 8, !tbaa !5
  br label %82

82:                                               ; preds = %77, %74
  store i1 true, ptr %5, align 1
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  br label %83

83:                                               ; preds = %82, %26
  %84 = load i1, ptr %5, align 1
  ret i1 %84
}

declare ptr @tree_node_max(ptr noundef) #2

; Function Attrs: nounwind uwtable
declare hidden ptr @node_prev(ptr noundef) #1

declare ptr @tree_node_min(ptr noundef) #2

; Function Attrs: nounwind uwtable
declare hidden ptr @node_next(ptr 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 = { "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 = { 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 = !{!"long", !7, i64 0}
!11 = !{!12, !10, i64 8}
!12 = !{!"hb_tree", !6, i64 0, !10, i64 8, !6, i64 16, !10, i64 24}
!13 = !{!12, !6, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}
!17 = !{!18, !6, i64 0}
!18 = !{!"hb_node", !6, i64 0, !6, i64 8, !7, i64 16, !6, i64 24, !6, i64 32}
!19 = !{!18, !6, i64 8}


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

; Function Attrs: nounwind uwtable
define dso_local noundef zeroext i1 @hb_tree_select(ptr nocapture noundef readonly %0, i64 noundef %1, ptr noundef writeonly %2, ptr noundef writeonly %3) local_unnamed_addr #0 {
  %5 = getelementptr inbounds i8, ptr %0, i64 8
  %6 = load i64, ptr %5, align 8, !tbaa !5
  %.not = icmp ugt i64 %6, %1
  br i1 %.not, label %10, label %7

7:                                                ; preds = %4
  %.not36 = icmp eq ptr %2, null
  br i1 %.not36, label %9, label %8

8:                                                ; preds = %7
  store ptr null, ptr %2, align 8, !tbaa !11
  br label %9

9:                                                ; preds = %8, %7
  %.not37 = icmp eq ptr %3, null
  br i1 %.not37, label %30, label %.sink.split

10:                                               ; preds = %4
  %11 = lshr i64 %6, 1
  %.not31 = icmp ugt i64 %11, %1
  %12 = load ptr, ptr %0, align 8, !tbaa !12
  br i1 %.not31, label %20, label %13

13:                                               ; preds = %10
  %14 = tail call ptr @tree_node_max(ptr noundef %12) #2
  %15 = load i64, ptr %5, align 8, !tbaa !5
  %16 = xor i64 %1, -1
  %17 = add i64 %15, %16
  %.not3339 = icmp eq i64 %17, 0
  br i1 %.not3339, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %13, %.lr.ph
  %.041 = phi ptr [ %19, %.lr.ph ], [ %14, %13 ]
  %.02340 = phi i64 [ %18, %.lr.ph ], [ %17, %13 ]
  %18 = add i64 %.02340, -1
  %19 = tail call ptr @node_prev(ptr noundef %.041)
  %.not33 = icmp eq i64 %18, 0
  br i1 %.not33, label %.loopexit, label %.lr.ph, !llvm.loop !13

20:                                               ; preds = %10
  %21 = tail call ptr @tree_node_min(ptr noundef %12) #2
  %.not3242 = icmp eq i64 %1, 0
  br i1 %.not3242, label %.loopexit, label %.lr.ph45

.lr.ph45:                                         ; preds = %20, %.lr.ph45
  %.144 = phi ptr [ %23, %.lr.ph45 ], [ %21, %20 ]
  %.12443 = phi i64 [ %22, %.lr.ph45 ], [ %1, %20 ]
  %22 = add i64 %.12443, -1
  %23 = tail call ptr @node_next(ptr noundef %.144)
  %.not32 = icmp eq i64 %22, 0
  br i1 %.not32, label %.loopexit, label %.lr.ph45, !llvm.loop !15

.loopexit:                                        ; preds = %.lr.ph, %.lr.ph45, %13, %20
  %.2 = phi ptr [ %21, %20 ], [ %14, %13 ], [ %23, %.lr.ph45 ], [ %19, %.lr.ph ]
  %.not34 = icmp eq ptr %2, null
  br i1 %.not34, label %26, label %24

24:                                               ; preds = %.loopexit
  %25 = load ptr, ptr %.2, align 8, !tbaa !16
  store ptr %25, ptr %2, align 8, !tbaa !11
  br label %26

26:                                               ; preds = %24, %.loopexit
  %.not35 = icmp eq ptr %3, null
  br i1 %.not35, label %30, label %27

27:                                               ; preds = %26
  %28 = getelementptr inbounds i8, ptr %.2, i64 8
  %29 = load ptr, ptr %28, align 8, !tbaa !18
  br label %.sink.split

.sink.split:                                      ; preds = %9, %27
  %.sink = phi ptr [ %29, %27 ], [ null, %9 ]
  store ptr %.sink, ptr %3, align 8, !tbaa !11
  br label %30

30:                                               ; preds = %.sink.split, %26, %9
  ret i1 %.not
}

declare ptr @tree_node_max(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden ptr @node_prev(ptr noundef) local_unnamed_addr #0

declare ptr @tree_node_min(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden ptr @node_next(ptr 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 = { "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, !10, i64 8}
!6 = !{!"hb_tree", !7, i64 0, !10, 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 = !{!"long", !8, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!6, !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}
!16 = !{!17, !7, i64 0}
!17 = !{!"hb_node", !7, i64 0, !7, i64 8, !8, i64 16, !7, i64 24, !7, i64 32}
!18 = !{!17, !7, i64 8}


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