; LLVM IR from pavlosdais/Abstract-Data-Types :: modules/RedBlackTree (RedBlackTree)

; 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.tnode = type { ptr, i32, ptr, ptr, ptr }
%struct.Set = type { ptr, i64, ptr, ptr }

@NULLNode = external dso_local global %struct.tnode, 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 ptr @rbt_find_node(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.Set, ptr %9, i32 0, i32 1
  %11 = load i64, ptr %10, align 8, !tbaa !9
  %12 = icmp eq i64 %11, 0
  br i1 %12, label %13, label %14

13:                                               ; preds = %2
  store ptr null, ptr %3, align 8
  br label %52

14:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.Set, ptr %15, i32 0, i32 0
  %17 = load ptr, ptr %16, align 8, !tbaa !12
  store ptr %17, ptr %6, align 8, !tbaa !5
  br label %18

18:                                               ; preds = %49, %14
  %19 = load ptr, ptr %6, align 8, !tbaa !5
  %20 = icmp ne ptr %19, @NULLNode
  br i1 %20, label %21, label %50

21:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %22 = load ptr, ptr %4, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.Set, ptr %22, i32 0, i32 2
  %24 = load ptr, ptr %23, align 8, !tbaa !13
  %25 = load ptr, ptr %5, align 8, !tbaa !5
  %26 = load ptr, ptr %6, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.tnode, ptr %26, i32 0, i32 0
  %28 = load ptr, ptr %27, align 8, !tbaa !14
  %29 = call i32 %24(ptr noundef %25, ptr noundef %28)
  store i32 %29, ptr %7, align 4, !tbaa !17
  %30 = load i32, ptr %7, align 4, !tbaa !17
  %31 = icmp eq i32 %30, 0
  br i1 %31, label %32, label %34

32:                                               ; preds = %21
  %33 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %33, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %47

34:                                               ; preds = %21
  %35 = load i32, ptr %7, align 4, !tbaa !17
  %36 = icmp slt i32 %35, 0
  br i1 %36, label %37, label %41

37:                                               ; preds = %34
  %38 = load ptr, ptr %6, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.tnode, ptr %38, i32 0, i32 2
  %40 = load ptr, ptr %39, align 8, !tbaa !18
  store ptr %40, ptr %6, align 8, !tbaa !5
  br label %45

41:                                               ; preds = %34
  %42 = load ptr, ptr %6, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.tnode, ptr %42, i32 0, i32 3
  %44 = load ptr, ptr %43, align 8, !tbaa !19
  store ptr %44, ptr %6, align 8, !tbaa !5
  br label %45

45:                                               ; preds = %41, %37
  br label %46

46:                                               ; preds = %45
  store i32 0, ptr %8, align 4
  br label %47

47:                                               ; preds = %46, %32
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  %48 = load i32, ptr %8, align 4
  switch i32 %48, label %51 [
    i32 0, label %49
  ]

49:                                               ; preds = %47
  br label %18, !llvm.loop !20

50:                                               ; preds = %18
  store ptr null, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %51

51:                                               ; preds = %50, %47
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  br label %52

52:                                               ; preds = %51, %13
  %53 = load ptr, ptr %3, align 8
  ret ptr %53
}

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, !11, i64 8}
!10 = !{!"Set", !6, i64 0, !11, i64 8, !6, i64 16, !6, i64 24}
!11 = !{!"long", !7, i64 0}
!12 = !{!10, !6, i64 0}
!13 = !{!10, !6, i64 16}
!14 = !{!15, !6, i64 0}
!15 = !{!"tnode", !6, i64 0, !16, i64 8, !6, i64 16, !6, i64 24, !6, i64 32}
!16 = !{!"int", !7, i64 0}
!17 = !{!16, !16, i64 0}
!18 = !{!15, !6, i64 16}
!19 = !{!15, !6, i64 24}
!20 = distinct !{!20, !21}
!21 = !{!"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.tnode = type { ptr, i32, ptr, ptr, ptr }

@NULLNode = external dso_local global %struct.tnode, align 8

; Function Attrs: nounwind uwtable
define dso_local noundef ptr @rbt_find_node(ptr nocapture noundef readonly %0, ptr noundef %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 8
  %4 = load i64, ptr %3, align 8, !tbaa !5
  %5 = icmp eq i64 %4, 0
  br i1 %5, label %.thread, label %.preheader

.preheader:                                       ; preds = %2
  %6 = getelementptr inbounds i8, ptr %0, i64 16
  %.01320 = load ptr, ptr %0, align 8, !tbaa !11
  %.not21 = icmp eq ptr %.01320, @NULLNode
  br i1 %.not21, label %.thread, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %10
  %.01322 = phi ptr [ %.013, %10 ], [ %.01320, %.preheader ]
  %7 = load ptr, ptr %6, align 8, !tbaa !12
  %8 = load ptr, ptr %.01322, align 8, !tbaa !13
  %9 = tail call i32 %7(ptr noundef %1, ptr noundef %8) #1
  %.not17 = icmp eq i32 %9, 0
  br i1 %.not17, label %.thread, label %10

10:                                               ; preds = %.lr.ph
  %11 = icmp slt i32 %9, 0
  %.114.in.v = select i1 %11, i64 16, i64 24
  %.114.in = getelementptr inbounds i8, ptr %.01322, i64 %.114.in.v
  %.013 = load ptr, ptr %.114.in, align 8, !tbaa !11
  %.not = icmp eq ptr %.013, @NULLNode
  br i1 %.not, label %.thread, label %.lr.ph, !llvm.loop !16

.thread:                                          ; preds = %10, %.lr.ph, %.preheader, %2
  %.3 = phi ptr [ null, %2 ], [ null, %.preheader ], [ null, %10 ], [ %.01322, %.lr.ph ]
  ret ptr %.3
}

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, !10, i64 8}
!6 = !{!"Set", !7, i64 0, !10, i64 8, !7, i64 16, !7, 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 16}
!13 = !{!14, !7, i64 0}
!14 = !{!"tnode", !7, i64 0, !15, i64 8, !7, i64 16, !7, i64 24, !7, i64 32}
!15 = !{!"int", !8, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


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