; LLVM IR from wgtdkp/wgtcc :: test/data-structure (rbtree)

; 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.rbtree_node = type { i32, i32, ptr, ptr, ptr }

@rbtree_nil = external dso_local global ptr, 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 @rbtree_insert(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
  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) #2
  %7 = load ptr, ptr @rbtree_nil, align 8, !tbaa !5
  store ptr %7, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %8, ptr %6, align 8, !tbaa !5
  br label %9

9:                                                ; preds = %30, %2
  %10 = load ptr, ptr %6, align 8, !tbaa !5
  %11 = load ptr, ptr @rbtree_nil, align 8, !tbaa !5
  %12 = icmp ne ptr %10, %11
  br i1 %12, label %13, label %32

13:                                               ; preds = %9
  %14 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %14, ptr %5, align 8, !tbaa !5
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.rbtree_node, ptr %15, i32 0, i32 1
  %17 = load i32, ptr %16, align 4, !tbaa !9
  %18 = load ptr, ptr %6, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.rbtree_node, ptr %18, i32 0, i32 1
  %20 = load i32, ptr %19, align 4, !tbaa !9
  %21 = icmp slt i32 %17, %20
  br i1 %21, label %22, label %26

22:                                               ; preds = %13
  %23 = load ptr, ptr %6, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.rbtree_node, ptr %23, i32 0, i32 2
  %25 = load ptr, ptr %24, align 8, !tbaa !12
  br label %30

26:                                               ; preds = %13
  %27 = load ptr, ptr %6, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.rbtree_node, ptr %27, i32 0, i32 3
  %29 = load ptr, ptr %28, align 8, !tbaa !13
  br label %30

30:                                               ; preds = %26, %22
  %31 = phi ptr [ %25, %22 ], [ %29, %26 ]
  store ptr %31, ptr %6, align 8, !tbaa !5
  br label %9, !llvm.loop !14

32:                                               ; preds = %9
  %33 = load ptr, ptr %5, align 8, !tbaa !5
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.rbtree_node, ptr %34, i32 0, i32 4
  store ptr %33, ptr %35, align 8, !tbaa !16
  %36 = load ptr, ptr %5, align 8, !tbaa !5
  %37 = load ptr, ptr @rbtree_nil, align 8, !tbaa !5
  %38 = icmp eq ptr %36, %37
  br i1 %38, label %39, label %41

39:                                               ; preds = %32
  %40 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %40, ptr %3, align 8, !tbaa !5
  br label %58

41:                                               ; preds = %32
  %42 = load ptr, ptr %4, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.rbtree_node, ptr %42, i32 0, i32 1
  %44 = load i32, ptr %43, align 4, !tbaa !9
  %45 = load ptr, ptr %5, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.rbtree_node, ptr %45, i32 0, i32 1
  %47 = load i32, ptr %46, align 4, !tbaa !9
  %48 = icmp slt i32 %44, %47
  br i1 %48, label %49, label %53

49:                                               ; preds = %41
  %50 = load ptr, ptr %4, align 8, !tbaa !5
  %51 = load ptr, ptr %5, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.rbtree_node, ptr %51, i32 0, i32 2
  store ptr %50, ptr %52, align 8, !tbaa !12
  br label %57

53:                                               ; preds = %41
  %54 = load ptr, ptr %4, align 8, !tbaa !5
  %55 = load ptr, ptr %5, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.rbtree_node, ptr %55, i32 0, i32 3
  store ptr %54, ptr %56, align 8, !tbaa !13
  br label %57

57:                                               ; preds = %53, %49
  br label %58

58:                                               ; preds = %57, %39
  %59 = load ptr, ptr %4, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.rbtree_node, ptr %59, i32 0, i32 0
  store i32 0, ptr %60, align 8, !tbaa !17
  %61 = load ptr, ptr %3, align 8, !tbaa !5
  %62 = load ptr, ptr %4, align 8, !tbaa !5
  %63 = call ptr @rbtree_insert_fixup(ptr noundef %61, ptr noundef %62)
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  ret ptr %63
}

; Function Attrs: nounwind uwtable
declare hidden ptr @rbtree_insert_fixup(ptr noundef, 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 = { 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 4}
!10 = !{!"rbtree_node", !11, i64 0, !11, i64 4, !6, i64 8, !6, i64 16, !6, i64 24}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !6, i64 8}
!13 = !{!10, !6, i64 16}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!10, !6, i64 24}
!17 = !{!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"

@rbtree_nil = external dso_local local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local ptr @rbtree_insert(ptr noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  %3 = load ptr, ptr @rbtree_nil, align 8, !tbaa !5
  %.not22 = icmp eq ptr %3, %0
  br i1 %.not22, label %._crit_edge.thread, label %.lr.ph

._crit_edge.thread:                               ; preds = %2
  %4 = getelementptr inbounds i8, ptr %1, i64 24
  store ptr %3, ptr %4, align 8, !tbaa !9
  br label %24

.lr.ph:                                           ; preds = %2
  %5 = getelementptr inbounds i8, ptr %1, i64 4
  %6 = load i32, ptr %5, align 4, !tbaa !12
  br label %7

7:                                                ; preds = %.lr.ph, %7
  %.023 = phi ptr [ %0, %.lr.ph ], [ %11, %7 ]
  %8 = getelementptr inbounds i8, ptr %.023, i64 4
  %9 = load i32, ptr %8, align 4, !tbaa !12
  %10 = icmp slt i32 %6, %9
  %.in.v = select i1 %10, i64 8, i64 16
  %.in = getelementptr inbounds i8, ptr %.023, i64 %.in.v
  %11 = load ptr, ptr %.in, align 8, !tbaa !5
  %.not = icmp eq ptr %11, %3
  br i1 %.not, label %._crit_edge, label %7, !llvm.loop !13

._crit_edge:                                      ; preds = %7
  %12 = getelementptr inbounds i8, ptr %1, i64 24
  store ptr %.023, ptr %12, align 8, !tbaa !9
  %13 = icmp eq ptr %.023, %3
  br i1 %13, label %24, label %14

14:                                               ; preds = %._crit_edge
  %15 = getelementptr inbounds i8, ptr %1, i64 4
  %16 = load i32, ptr %15, align 4, !tbaa !12
  %17 = getelementptr inbounds i8, ptr %.023, i64 4
  %18 = load i32, ptr %17, align 4, !tbaa !12
  %19 = icmp slt i32 %16, %18
  br i1 %19, label %20, label %22

20:                                               ; preds = %14
  %21 = getelementptr inbounds i8, ptr %.023, i64 8
  store ptr %1, ptr %21, align 8, !tbaa !15
  br label %24

22:                                               ; preds = %14
  %23 = getelementptr inbounds i8, ptr %.023, i64 16
  store ptr %1, ptr %23, align 8, !tbaa !16
  br label %24

24:                                               ; preds = %._crit_edge.thread, %._crit_edge, %20, %22
  %.021 = phi ptr [ %0, %20 ], [ %0, %22 ], [ %1, %._crit_edge ], [ %1, %._crit_edge.thread ]
  store i32 0, ptr %1, align 8, !tbaa !17
  %25 = tail call ptr @rbtree_insert_fixup(ptr noundef %.021, ptr noundef nonnull %1)
  ret ptr %25
}

; Function Attrs: nounwind uwtable
declare hidden ptr @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" }

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


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