; LLVM IR from yudi-matsuzake/libgenerics :: src (red_black_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.redblacktree_t = type { i64, i64, ptr, ptr, i64, ptr }
%struct.redblacknode_t = type { ptr, ptr, ptr, i32, ptr }

; Function Attrs: nounwind uwtable
define dso_local i32 @rbtree_add(ptr noundef %0, ptr noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = icmp ne ptr %11, null
  br i1 %12, label %14, label %13

13:                                               ; preds = %2
  store i32 1, ptr %3, align 4
  br label %115

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

18:                                               ; preds = %81, %14
  %19 = load ptr, ptr %7, align 8, !tbaa !5
  %20 = icmp ne ptr %19, null
  br i1 %20, label %21, label %82

21:                                               ; preds = %18
  %22 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %22, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.redblacktree_t, ptr %23, i32 0, i32 2
  %25 = load ptr, ptr %24, align 8, !tbaa !14
  %26 = load ptr, ptr %5, align 8, !tbaa !5
  %27 = load ptr, ptr %7, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.redblacknode_t, ptr %27, i32 0, i32 4
  %29 = load ptr, ptr %28, align 8, !tbaa !15
  %30 = load ptr, ptr %4, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.redblacktree_t, ptr %30, i32 0, i32 3
  %32 = load ptr, ptr %31, align 8, !tbaa !17
  %33 = call i32 %25(ptr noundef %26, ptr noundef %29, ptr noundef %32)
  store i32 %33, ptr %9, align 4, !tbaa !12
  %34 = load i32, ptr %9, align 4, !tbaa !12
  switch i32 %34, label %77 [
    i32 1, label %35
    i32 -1, label %39
    i32 0, label %43
  ]

35:                                               ; preds = %21
  %36 = load ptr, ptr %7, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.redblacknode_t, ptr %36, i32 0, i32 1
  %38 = load ptr, ptr %37, align 8, !tbaa !18
  store ptr %38, ptr %7, align 8, !tbaa !5
  store i32 1, ptr %8, align 4, !tbaa !12
  br label %78

39:                                               ; preds = %21
  %40 = load ptr, ptr %7, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.redblacknode_t, ptr %40, i32 0, i32 0
  %42 = load ptr, ptr %41, align 8, !tbaa !19
  store ptr %42, ptr %7, align 8, !tbaa !5
  store i32 0, ptr %8, align 4, !tbaa !12
  br label %78

43:                                               ; preds = %21
  %44 = load ptr, ptr %4, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.redblacktree_t, ptr %44, i32 0, i32 4
  %46 = load i64, ptr %45, align 8, !tbaa !20
  %47 = and i64 %46, 2
  %48 = icmp ne i64 %47, 0
  br i1 %48, label %49, label %61

49:                                               ; preds = %43
  %50 = load ptr, ptr %5, align 8, !tbaa !5
  %51 = icmp ne ptr %50, null
  br i1 %51, label %52, label %60

52:                                               ; preds = %49
  %53 = load ptr, ptr %7, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.redblacknode_t, ptr %53, i32 0, i32 4
  %55 = load ptr, ptr %54, align 8, !tbaa !15
  %56 = load ptr, ptr %5, align 8, !tbaa !5
  %57 = load ptr, ptr %4, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.redblacktree_t, ptr %57, i32 0, i32 1
  %59 = load i64, ptr %58, align 8, !tbaa !21
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %55, ptr align 1 %56, i64 %59, i1 false)
  br label %60

60:                                               ; preds = %52, %49
  store i32 0, ptr %3, align 4
  store i32 1, ptr %10, align 4
  br label %79

61:                                               ; preds = %43
  %62 = load ptr, ptr %4, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.redblacktree_t, ptr %62, i32 0, i32 4
  %64 = load i64, ptr %63, align 8, !tbaa !20
  %65 = and i64 %64, 1
  %66 = icmp ne i64 %65, 0
  br i1 %66, label %67, label %71

67:                                               ; preds = %61
  %68 = load ptr, ptr %7, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.redblacknode_t, ptr %68, i32 0, i32 0
  %70 = load ptr, ptr %69, align 8, !tbaa !19
  store ptr %70, ptr %7, align 8, !tbaa !5
  store i32 0, ptr %8, align 4, !tbaa !12
  br label %75

71:                                               ; preds = %61
  %72 = load ptr, ptr %7, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.redblacknode_t, ptr %72, i32 0, i32 1
  %74 = load ptr, ptr %73, align 8, !tbaa !18
  store ptr %74, ptr %7, align 8, !tbaa !5
  store i32 1, ptr %8, align 4, !tbaa !12
  br label %75

75:                                               ; preds = %71, %67
  br label %76

76:                                               ; preds = %75
  br label %78

77:                                               ; preds = %21
  store i32 15, ptr %3, align 4
  store i32 1, ptr %10, align 4
  br label %79

78:                                               ; preds = %76, %39, %35
  store i32 0, ptr %10, align 4
  br label %79

79:                                               ; preds = %78, %77, %60
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  %80 = load i32, ptr %10, align 4
  switch i32 %80, label %114 [
    i32 0, label %81
  ]

81:                                               ; preds = %79
  br label %18, !llvm.loop !22

82:                                               ; preds = %18
  %83 = load ptr, ptr %4, align 8, !tbaa !5
  %84 = load ptr, ptr %5, align 8, !tbaa !5
  %85 = call ptr @create_node(ptr noundef %83, ptr noundef %84)
  store ptr %85, ptr %7, align 8, !tbaa !5
  %86 = load ptr, ptr %6, align 8, !tbaa !5
  %87 = icmp eq ptr %86, null
  br i1 %87, label %88, label %92

88:                                               ; preds = %82
  %89 = load ptr, ptr %7, align 8, !tbaa !5
  %90 = load ptr, ptr %4, align 8, !tbaa !5
  %91 = getelementptr inbounds %struct.redblacktree_t, ptr %90, i32 0, i32 5
  store ptr %89, ptr %91, align 8, !tbaa !9
  br label %104

92:                                               ; preds = %82
  %93 = load i32, ptr %8, align 4, !tbaa !12
  %94 = icmp eq i32 %93, 0
  br i1 %94, label %95, label %99

95:                                               ; preds = %92
  %96 = load ptr, ptr %7, align 8, !tbaa !5
  %97 = load ptr, ptr %6, align 8, !tbaa !5
  %98 = getelementptr inbounds %struct.redblacknode_t, ptr %97, i32 0, i32 0
  store ptr %96, ptr %98, align 8, !tbaa !19
  br label %103

99:                                               ; preds = %92
  %100 = load ptr, ptr %7, align 8, !tbaa !5
  %101 = load ptr, ptr %6, align 8, !tbaa !5
  %102 = getelementptr inbounds %struct.redblacknode_t, ptr %101, i32 0, i32 1
  store ptr %100, ptr %102, align 8, !tbaa !18
  br label %103

103:                                              ; preds = %99, %95
  br label %104

104:                                              ; preds = %103, %88
  %105 = load ptr, ptr %6, align 8, !tbaa !5
  %106 = load ptr, ptr %7, align 8, !tbaa !5
  %107 = getelementptr inbounds %struct.redblacknode_t, ptr %106, i32 0, i32 2
  store ptr %105, ptr %107, align 8, !tbaa !24
  %108 = load ptr, ptr %4, align 8, !tbaa !5
  %109 = load ptr, ptr %7, align 8, !tbaa !5
  call void @rbtree_insert_fixup(ptr noundef %108, ptr noundef %109)
  %110 = load ptr, ptr %4, align 8, !tbaa !5
  %111 = getelementptr inbounds %struct.redblacktree_t, ptr %110, i32 0, i32 0
  %112 = load i64, ptr %111, align 8, !tbaa !25
  %113 = add i64 %112, 1
  store i64 %113, ptr %111, align 8, !tbaa !25
  store i32 0, ptr %3, align 4
  store i32 1, ptr %10, align 4
  br label %114

114:                                              ; preds = %104, %79
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  br label %115

115:                                              ; preds = %114, %13
  %116 = load i32, ptr %3, align 4
  ret i32 %116
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nounwind uwtable
declare hidden ptr @create_node(ptr noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
declare hidden void @rbtree_insert_fixup(ptr noundef, ptr noundef) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 40}
!10 = !{!"redblacktree_t", !11, i64 0, !11, i64 8, !6, i64 16, !6, i64 24, !11, i64 32, !6, i64 40}
!11 = !{!"long", !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = !{!10, !6, i64 16}
!15 = !{!16, !6, i64 32}
!16 = !{!"redblacknode_t", !6, i64 0, !6, i64 8, !6, i64 16, !13, i64 24, !6, i64 32}
!17 = !{!10, !6, i64 24}
!18 = !{!16, !6, i64 8}
!19 = !{!16, !6, i64 0}
!20 = !{!10, !11, i64 32}
!21 = !{!10, !11, i64 8}
!22 = distinct !{!22, !23}
!23 = !{!"llvm.loop.mustprogress"}
!24 = !{!16, !6, i64 16}
!25 = !{!10, !11, i64 0}


; 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 i32 @rbtree_add(ptr noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %.thread, label %3

3:                                                ; preds = %2
  %4 = getelementptr inbounds i8, ptr %0, i64 40
  %.03959 = load ptr, ptr %4, align 8, !tbaa !5
  %.not4560 = icmp eq ptr %.03959, null
  br i1 %.not4560, label %30, label %.lr.ph

.lr.ph:                                           ; preds = %3
  %5 = getelementptr inbounds i8, ptr %0, i64 16
  %6 = getelementptr inbounds i8, ptr %0, i64 24
  %7 = getelementptr inbounds i8, ptr %0, i64 32
  br label %8

8:                                                ; preds = %.lr.ph, %29
  %.03961 = phi ptr [ %.03959, %.lr.ph ], [ %.039, %29 ]
  %9 = load ptr, ptr %5, align 8, !tbaa !9
  %10 = getelementptr inbounds i8, ptr %.03961, i64 32
  %11 = load ptr, ptr %10, align 8, !tbaa !12
  %12 = load ptr, ptr %6, align 8, !tbaa !15
  %13 = tail call i32 %9(ptr noundef %1, ptr noundef %11, ptr noundef %12) #2
  switch i32 %13, label %.thread [
    i32 1, label %14
    i32 -1, label %29
    i32 0, label %16
  ]

14:                                               ; preds = %8
  %15 = getelementptr inbounds i8, ptr %.03961, i64 8
  br label %29

16:                                               ; preds = %8
  %17 = load i64, ptr %7, align 8, !tbaa !16
  %18 = and i64 %17, 2
  %.not46 = icmp eq i64 %18, 0
  br i1 %.not46, label %25, label %19

19:                                               ; preds = %16
  %.not48 = icmp eq ptr %1, null
  br i1 %.not48, label %.thread, label %20

20:                                               ; preds = %19
  %21 = getelementptr inbounds i8, ptr %.03961, i64 32
  %22 = load ptr, ptr %21, align 8, !tbaa !12
  %23 = getelementptr inbounds i8, ptr %0, i64 8
  %24 = load i64, ptr %23, align 8, !tbaa !17
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %22, ptr nonnull align 1 %1, i64 %24, i1 false)
  br label %.thread

25:                                               ; preds = %16
  %26 = and i64 %17, 1
  %27 = shl nuw nsw i64 %26, 3
  %spec.select.idx = xor i64 %27, 8
  %spec.select = getelementptr inbounds i8, ptr %.03961, i64 %spec.select.idx
  %28 = trunc nuw nsw i64 %26 to i32
  %spec.select49 = xor i32 %28, 1
  br label %29

29:                                               ; preds = %14, %8, %25
  %.140.in = phi ptr [ %15, %14 ], [ %.03961, %8 ], [ %spec.select, %25 ]
  %.137 = phi i32 [ 1, %14 ], [ 0, %8 ], [ %spec.select49, %25 ]
  %.039 = load ptr, ptr %.140.in, align 8, !tbaa !5
  %.not45 = icmp eq ptr %.039, null
  br i1 %.not45, label %32, label %8, !llvm.loop !18

30:                                               ; preds = %3
  %31 = tail call ptr @create_node(ptr noundef nonnull %0, ptr noundef %1)
  store ptr %31, ptr %4, align 8, !tbaa !20
  br label %38

32:                                               ; preds = %29
  %33 = icmp eq i32 %.137, 0
  %34 = tail call ptr @create_node(ptr noundef nonnull %0, ptr noundef %1)
  br i1 %33, label %35, label %36

35:                                               ; preds = %32
  store ptr %34, ptr %.03961, align 8, !tbaa !21
  br label %38

36:                                               ; preds = %32
  %37 = getelementptr inbounds i8, ptr %.03961, i64 8
  store ptr %34, ptr %37, align 8, !tbaa !22
  br label %38

38:                                               ; preds = %35, %36, %30
  %39 = phi ptr [ %34, %35 ], [ %34, %36 ], [ %31, %30 ]
  %.042.lcssa67 = phi ptr [ %.03961, %35 ], [ %.03961, %36 ], [ null, %30 ]
  %40 = getelementptr inbounds i8, ptr %39, i64 16
  store ptr %.042.lcssa67, ptr %40, align 8, !tbaa !23
  tail call void @rbtree_insert_fixup(ptr noundef nonnull %0, ptr noundef %39)
  %41 = load i64, ptr %0, align 8, !tbaa !24
  %42 = add i64 %41, 1
  store i64 %42, ptr %0, align 8, !tbaa !24
  br label %.thread

.thread:                                          ; preds = %8, %19, %20, %38, %2
  %.3 = phi i32 [ 1, %2 ], [ 0, %38 ], [ 0, %19 ], [ 0, %20 ], [ 15, %8 ]
  ret i32 %.3
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

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

; Function Attrs: nounwind uwtable
declare hidden void @rbtree_insert_fixup(ptr noundef, 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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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, !6, i64 16}
!10 = !{!"redblacktree_t", !11, i64 0, !11, i64 8, !6, i64 16, !6, i64 24, !11, i64 32, !6, i64 40}
!11 = !{!"long", !7, i64 0}
!12 = !{!13, !6, i64 32}
!13 = !{!"redblacknode_t", !6, i64 0, !6, i64 8, !6, i64 16, !14, i64 24, !6, i64 32}
!14 = !{!"int", !7, i64 0}
!15 = !{!10, !6, i64 24}
!16 = !{!10, !11, i64 32}
!17 = !{!10, !11, i64 8}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}
!20 = !{!10, !6, i64 40}
!21 = !{!13, !6, i64 0}
!22 = !{!13, !6, i64 8}
!23 = !{!13, !6, i64 16}
!24 = !{!10, !11, i64 0}


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