; LLVM IR from MostafaTwfiq/C-DataStructures-And-Algorithms :: DataStructure/Trees/Sources (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.RBNode = type { ptr, i32, ptr, ptr, ptr }
%struct.RBTree = type { ptr, i32, 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
declare void @free(ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @doubleBlackCase(ptr noundef, ptr noundef, ptr noundef) #2

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

23:                                               ; preds = %2
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.RBNode, ptr %24, i32 0, i32 3
  %26 = load ptr, ptr %25, align 8, !tbaa !9
  %27 = load ptr, ptr %3, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.RBTree, ptr %27, i32 0, i32 0
  store ptr %26, ptr %28, align 8, !tbaa !15
  br label %44

29:                                               ; preds = %2
  %30 = load ptr, ptr %6, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.RBNode, ptr %30, i32 0, i32 3
  %32 = load ptr, ptr %31, align 8, !tbaa !9
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = icmp eq ptr %32, %33
  br i1 %34, label %35, label %39

35:                                               ; preds = %29
  %36 = load ptr, ptr %5, align 8, !tbaa !5
  %37 = load ptr, ptr %6, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.RBNode, ptr %37, i32 0, i32 3
  store ptr %36, ptr %38, align 8, !tbaa !9
  br label %43

39:                                               ; preds = %29
  %40 = load ptr, ptr %5, align 8, !tbaa !5
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.RBNode, ptr %41, i32 0, i32 2
  store ptr %40, ptr %42, align 8, !tbaa !17
  br label %43

43:                                               ; preds = %39, %35
  br label %44

44:                                               ; preds = %43, %23
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %45 = load ptr, ptr %4, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.RBNode, ptr %45, i32 0, i32 0
  %47 = load ptr, ptr %46, align 8, !tbaa !18
  store ptr %47, ptr %8, align 8, !tbaa !5
  %48 = load ptr, ptr %4, align 8, !tbaa !5
  call void @free(ptr noundef %48) #3
  %49 = load ptr, ptr %5, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.RBNode, ptr %49, i32 0, i32 1
  %51 = load i32, ptr %50, align 8, !tbaa !13
  %52 = icmp eq i32 %51, 0
  br i1 %52, label %53, label %59

53:                                               ; preds = %44
  %54 = load i32, ptr %7, align 4, !tbaa !14
  %55 = icmp eq i32 %54, 1
  br i1 %55, label %56, label %59

56:                                               ; preds = %53
  %57 = load ptr, ptr %5, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.RBNode, ptr %57, i32 0, i32 1
  store i32 1, ptr %58, align 8, !tbaa !13
  br label %74

59:                                               ; preds = %53, %44
  %60 = load ptr, ptr %5, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.RBNode, ptr %60, i32 0, i32 1
  %62 = load i32, ptr %61, align 8, !tbaa !13
  %63 = icmp eq i32 %62, 1
  br i1 %63, label %64, label %73

64:                                               ; preds = %59
  %65 = load i32, ptr %7, align 4, !tbaa !14
  %66 = icmp eq i32 %65, 1
  br i1 %66, label %67, label %73

67:                                               ; preds = %64
  %68 = load ptr, ptr %3, align 8, !tbaa !5
  %69 = load ptr, ptr %5, align 8, !tbaa !5
  %70 = load ptr, ptr %5, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.RBNode, ptr %70, i32 0, i32 4
  %72 = load ptr, ptr %71, align 8, !tbaa !12
  call void @doubleBlackCase(ptr noundef %68, ptr noundef %69, ptr noundef %72)
  br label %73

73:                                               ; preds = %67, %64, %59
  br label %74

74:                                               ; preds = %73, %56
  %75 = load ptr, ptr %3, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.RBTree, ptr %75, i32 0, i32 1
  %77 = load i32, ptr %76, align 8, !tbaa !19
  %78 = add nsw i32 %77, -1
  store i32 %78, ptr %76, align 8, !tbaa !19
  %79 = load ptr, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  ret ptr %79
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nounwind "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 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 #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, !6, i64 24}
!10 = !{!"RBNode", !6, i64 0, !11, i64 8, !6, i64 16, !6, i64 24, !6, i64 32}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !6, i64 32}
!13 = !{!10, !11, i64 8}
!14 = !{!11, !11, i64 0}
!15 = !{!16, !6, i64 0}
!16 = !{!"RBTree", !6, i64 0, !11, i64 8, !6, i64 16, !6, i64 24}
!17 = !{!10, !6, i64 16}
!18 = !{!10, !6, i64 0}
!19 = !{!16, !11, 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: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @doubleBlackCase(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local ptr @deleteLeftNullNodeWtoFr(ptr noundef %0, ptr noundef %1) local_unnamed_addr #1 {
  %3 = getelementptr inbounds i8, ptr %1, i64 24
  %4 = load ptr, ptr %3, align 8, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %1, i64 32
  %6 = load ptr, ptr %5, align 8, !tbaa !11
  %7 = getelementptr inbounds i8, ptr %4, i64 32
  store ptr %6, ptr %7, align 8, !tbaa !11
  %8 = getelementptr inbounds i8, ptr %1, i64 8
  %9 = load i32, ptr %8, align 8, !tbaa !12
  %10 = icmp eq ptr %6, null
  br i1 %10, label %16, label %11

11:                                               ; preds = %2
  %12 = getelementptr inbounds i8, ptr %6, i64 24
  %13 = load ptr, ptr %12, align 8, !tbaa !5
  %14 = icmp eq ptr %13, %1
  %15 = getelementptr inbounds i8, ptr %6, i64 16
  %spec.select = select i1 %14, ptr %12, ptr %15
  br label %16

16:                                               ; preds = %11, %2
  %.sink = phi ptr [ %0, %2 ], [ %spec.select, %11 ]
  store ptr %4, ptr %.sink, align 8, !tbaa !13
  %17 = load ptr, ptr %1, align 8, !tbaa !14
  tail call void @free(ptr noundef nonnull %1) #2
  %18 = getelementptr inbounds i8, ptr %4, i64 8
  %19 = load i32, ptr %18, align 8, !tbaa !12
  %20 = icmp eq i32 %19, 0
  %21 = icmp eq i32 %9, 1
  %or.cond = select i1 %20, i1 %21, i1 false
  br i1 %or.cond, label %22, label %23

22:                                               ; preds = %16
  store i32 1, ptr %18, align 8, !tbaa !12
  br label %27

23:                                               ; preds = %16
  %24 = icmp eq i32 %19, 1
  %or.cond3 = select i1 %24, i1 %21, i1 false
  br i1 %or.cond3, label %25, label %27

25:                                               ; preds = %23
  %26 = load ptr, ptr %7, align 8, !tbaa !11
  tail call void @doubleBlackCase(ptr noundef %0, ptr noundef nonnull %4, ptr noundef %26)
  br label %27

27:                                               ; preds = %23, %25, %22
  %28 = getelementptr inbounds i8, ptr %0, i64 8
  %29 = load i32, ptr %28, align 8, !tbaa !15
  %30 = add nsw i32 %29, -1
  store i32 %30, ptr %28, align 8, !tbaa !15
  ret ptr %17
}

attributes #0 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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 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, !7, i64 24}
!6 = !{!"RBNode", !7, i64 0, !10, i64 8, !7, i64 16, !7, i64 24, !7, i64 32}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!6, !7, i64 32}
!12 = !{!6, !10, i64 8}
!13 = !{!7, !7, i64 0}
!14 = !{!6, !7, i64 0}
!15 = !{!16, !10, i64 8}
!16 = !{!"RBTree", !7, i64 0, !10, i64 8, !7, i64 16, !7, i64 24}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,SROAPass,SimplifyCFGPass,TailCallElimPass