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

; 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.tree = type { ptr, i64, ptr, i64 }
%struct.tree_node = type { ptr, ptr, ptr, ptr, ptr }

; 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
declare dso_local ptr @tree_node_prev(ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local ptr @tree_node_max(ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local ptr @tree_node_next(ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local ptr @tree_node_min(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local zeroext i1 @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
  %11 = alloca i32, align 4
  %12 = 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
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  %13 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %13, ptr %10, align 8, !tbaa !5
  %14 = load i64, ptr %7, align 8, !tbaa !9
  %15 = load ptr, ptr %10, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.tree, ptr %15, i32 0, i32 1
  %17 = load i64, ptr %16, align 8, !tbaa !11
  %18 = icmp uge i64 %14, %17
  br i1 %18, label %19, label %22

19:                                               ; preds = %4
  %20 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr null, ptr %20, align 8, !tbaa !5
  %21 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr null, ptr %21, align 8, !tbaa !5
  store i1 false, ptr %5, align 1
  store i32 1, ptr %11, align 4
  br label %70

22:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #2
  %23 = load i64, ptr %7, align 8, !tbaa !9
  %24 = load ptr, ptr %10, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.tree, ptr %24, i32 0, i32 1
  %26 = load i64, ptr %25, align 8, !tbaa !11
  %27 = udiv i64 %26, 2
  %28 = icmp uge i64 %23, %27
  br i1 %28, label %29, label %48

29:                                               ; preds = %22
  %30 = load ptr, ptr %10, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.tree, ptr %30, i32 0, i32 0
  %32 = load ptr, ptr %31, align 8, !tbaa !13
  %33 = call ptr @tree_node_max(ptr noundef %32)
  store ptr %33, ptr %12, align 8, !tbaa !5
  %34 = load ptr, ptr %10, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.tree, ptr %34, i32 0, i32 1
  %36 = load i64, ptr %35, align 8, !tbaa !11
  %37 = sub i64 %36, 1
  %38 = load i64, ptr %7, align 8, !tbaa !9
  %39 = sub i64 %37, %38
  store i64 %39, ptr %7, align 8, !tbaa !9
  br label %40

40:                                               ; preds = %44, %29
  %41 = load i64, ptr %7, align 8, !tbaa !9
  %42 = add i64 %41, -1
  store i64 %42, ptr %7, align 8, !tbaa !9
  %43 = icmp ne i64 %41, 0
  br i1 %43, label %44, label %47

44:                                               ; preds = %40
  %45 = load ptr, ptr %12, align 8, !tbaa !5
  %46 = call ptr @tree_node_prev(ptr noundef %45)
  store ptr %46, ptr %12, align 8, !tbaa !5
  br label %40, !llvm.loop !14

47:                                               ; preds = %40
  br label %61

48:                                               ; preds = %22
  %49 = load ptr, ptr %10, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.tree, ptr %49, i32 0, i32 0
  %51 = load ptr, ptr %50, align 8, !tbaa !13
  %52 = call ptr @tree_node_min(ptr noundef %51)
  store ptr %52, ptr %12, align 8, !tbaa !5
  br label %53

53:                                               ; preds = %57, %48
  %54 = load i64, ptr %7, align 8, !tbaa !9
  %55 = add i64 %54, -1
  store i64 %55, ptr %7, align 8, !tbaa !9
  %56 = icmp ne i64 %54, 0
  br i1 %56, label %57, label %60

57:                                               ; preds = %53
  %58 = load ptr, ptr %12, align 8, !tbaa !5
  %59 = call ptr @tree_node_next(ptr noundef %58)
  store ptr %59, ptr %12, align 8, !tbaa !5
  br label %53, !llvm.loop !16

60:                                               ; preds = %53
  br label %61

61:                                               ; preds = %60, %47
  %62 = load ptr, ptr %12, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.tree_node, ptr %62, i32 0, i32 0
  %64 = load ptr, ptr %63, align 8, !tbaa !17
  %65 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %64, ptr %65, align 8, !tbaa !5
  %66 = load ptr, ptr %12, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.tree_node, ptr %66, i32 0, i32 1
  %68 = load ptr, ptr %67, align 8, !tbaa !19
  %69 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %68, ptr %69, align 8, !tbaa !5
  store i1 true, ptr %5, align 1
  store i32 1, ptr %11, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #2
  br label %70

70:                                               ; preds = %61, %19
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  %71 = load i1, ptr %5, align 1
  ret i1 %71
}

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 = !{!"long", !7, i64 0}
!11 = !{!12, !10, i64 8}
!12 = !{!"", !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 = !{!"tree_node", !6, i64 0, !6, i64 8, !6, 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
declare dso_local ptr @tree_node_prev(ptr noundef) local_unnamed_addr #0

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

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

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

; Function Attrs: nounwind uwtable
define dso_local noundef zeroext i1 @tree_select(ptr nocapture noundef readonly %0, i64 noundef %1, ptr nocapture noundef writeonly %2, ptr nocapture 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 %8, label %7

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

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

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

.lr.ph:                                           ; preds = %11, %.lr.ph
  %.030 = phi ptr [ %17, %.lr.ph ], [ %12, %11 ]
  %.02029 = phi i64 [ %16, %.lr.ph ], [ %15, %11 ]
  %16 = add i64 %.02029, -1
  %17 = tail call ptr @tree_node_prev(ptr noundef %.030)
  %.not26 = icmp eq i64 %16, 0
  br i1 %.not26, label %.loopexit, label %.lr.ph, !llvm.loop !13

18:                                               ; preds = %8
  %19 = tail call ptr @tree_node_min(ptr noundef %10)
  %.not2531 = icmp eq i64 %1, 0
  br i1 %.not2531, label %.loopexit, label %.lr.ph34

.lr.ph34:                                         ; preds = %18, %.lr.ph34
  %.133 = phi ptr [ %21, %.lr.ph34 ], [ %19, %18 ]
  %.12132 = phi i64 [ %20, %.lr.ph34 ], [ %1, %18 ]
  %20 = add i64 %.12132, -1
  %21 = tail call ptr @tree_node_next(ptr noundef %.133)
  %.not25 = icmp eq i64 %20, 0
  br i1 %.not25, label %.loopexit, label %.lr.ph34, !llvm.loop !15

.loopexit:                                        ; preds = %.lr.ph, %.lr.ph34, %11, %18
  %.2 = phi ptr [ %19, %18 ], [ %12, %11 ], [ %21, %.lr.ph34 ], [ %17, %.lr.ph ]
  %22 = load ptr, ptr %.2, align 8, !tbaa !16
  store ptr %22, ptr %2, align 8, !tbaa !11
  %23 = getelementptr inbounds i8, ptr %.2, i64 8
  %24 = load ptr, ptr %23, align 8, !tbaa !18
  br label %25

25:                                               ; preds = %.loopexit, %7
  %storemerge = phi ptr [ %24, %.loopexit ], [ null, %7 ]
  store ptr %storemerge, ptr %3, align 8, !tbaa !11
  ret i1 %.not
}

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

!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 = !{!"", !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 = !{!"tree_node", !7, i64 0, !7, i64 8, !7, 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