; LLVM IR from xxks-kkk/algo :: trees/child-sibling-pointer (csp)

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

; Function Attrs: nounwind uwtable
declare dso_local ptr @createNewNode(i8 noundef signext) #0

; 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 nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nounwind uwtable
declare dso_local ptr @find(i8 noundef signext, ptr noundef) #0

; Function Attrs: nounwind uwtable
define dso_local ptr @insert(i8 noundef signext %0, ptr noundef %1, i8 noundef signext %2, i32 noundef %3) #0 {
  %5 = alloca i8, align 1
  %6 = alloca ptr, align 8
  %7 = alloca i8, align 1
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  store i8 %0, ptr %5, align 1, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !8
  store i8 %2, ptr %7, align 1, !tbaa !5
  store i32 %3, ptr %8, align 4, !tbaa !10
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #2
  %12 = load i8, ptr %7, align 1, !tbaa !5
  %13 = load ptr, ptr %6, align 8, !tbaa !8
  %14 = call ptr @find(i8 noundef signext %12, ptr noundef %13)
  store ptr %14, ptr %11, align 8, !tbaa !8
  %15 = load ptr, ptr %6, align 8, !tbaa !8
  %16 = icmp eq ptr %15, null
  br i1 %16, label %17, label %20

17:                                               ; preds = %4
  %18 = load i8, ptr %5, align 1, !tbaa !5
  %19 = call ptr @createNewNode(i8 noundef signext %18)
  store ptr %19, ptr %6, align 8, !tbaa !8
  br label %74

20:                                               ; preds = %4
  %21 = load i8, ptr %5, align 1, !tbaa !5
  %22 = load ptr, ptr %6, align 8, !tbaa !8
  %23 = call ptr @find(i8 noundef signext %21, ptr noundef %22)
  %24 = icmp ne ptr %23, null
  br i1 %24, label %73, label %25

25:                                               ; preds = %20
  %26 = load i32, ptr %8, align 4, !tbaa !10
  %27 = icmp ne i32 %26, 0
  br i1 %27, label %28, label %55

28:                                               ; preds = %25
  %29 = load i8, ptr %5, align 1, !tbaa !5
  %30 = call ptr @createNewNode(i8 noundef signext %29)
  store ptr %30, ptr %9, align 8, !tbaa !8
  %31 = load ptr, ptr %11, align 8, !tbaa !8
  %32 = getelementptr inbounds %struct.CSPTreeNode, ptr %31, i32 0, i32 2
  %33 = load ptr, ptr %32, align 8, !tbaa !12
  store ptr %33, ptr %10, align 8, !tbaa !8
  %34 = load ptr, ptr %10, align 8, !tbaa !8
  %35 = icmp eq ptr %34, null
  br i1 %35, label %36, label %40

36:                                               ; preds = %28
  %37 = load ptr, ptr %9, align 8, !tbaa !8
  %38 = load ptr, ptr %11, align 8, !tbaa !8
  %39 = getelementptr inbounds %struct.CSPTreeNode, ptr %38, i32 0, i32 2
  store ptr %37, ptr %39, align 8, !tbaa !12
  br label %54

40:                                               ; preds = %28
  br label %41

41:                                               ; preds = %46, %40
  %42 = load ptr, ptr %10, align 8, !tbaa !8
  %43 = getelementptr inbounds %struct.CSPTreeNode, ptr %42, i32 0, i32 1
  %44 = load ptr, ptr %43, align 8, !tbaa !14
  %45 = icmp ne ptr %44, null
  br i1 %45, label %46, label %50

46:                                               ; preds = %41
  %47 = load ptr, ptr %10, align 8, !tbaa !8
  %48 = getelementptr inbounds %struct.CSPTreeNode, ptr %47, i32 0, i32 1
  %49 = load ptr, ptr %48, align 8, !tbaa !14
  store ptr %49, ptr %10, align 8, !tbaa !8
  br label %41, !llvm.loop !15

50:                                               ; preds = %41
  %51 = load ptr, ptr %9, align 8, !tbaa !8
  %52 = load ptr, ptr %10, align 8, !tbaa !8
  %53 = getelementptr inbounds %struct.CSPTreeNode, ptr %52, i32 0, i32 1
  store ptr %51, ptr %53, align 8, !tbaa !14
  br label %54

54:                                               ; preds = %50, %36
  br label %72

55:                                               ; preds = %25
  %56 = load i8, ptr %5, align 1, !tbaa !5
  %57 = call ptr @createNewNode(i8 noundef signext %56)
  store ptr %57, ptr %9, align 8, !tbaa !8
  %58 = load ptr, ptr %11, align 8, !tbaa !8
  store ptr %58, ptr %10, align 8, !tbaa !8
  br label %59

59:                                               ; preds = %64, %55
  %60 = load ptr, ptr %10, align 8, !tbaa !8
  %61 = getelementptr inbounds %struct.CSPTreeNode, ptr %60, i32 0, i32 1
  %62 = load ptr, ptr %61, align 8, !tbaa !14
  %63 = icmp ne ptr %62, null
  br i1 %63, label %64, label %68

64:                                               ; preds = %59
  %65 = load ptr, ptr %10, align 8, !tbaa !8
  %66 = getelementptr inbounds %struct.CSPTreeNode, ptr %65, i32 0, i32 1
  %67 = load ptr, ptr %66, align 8, !tbaa !14
  store ptr %67, ptr %10, align 8, !tbaa !8
  br label %59, !llvm.loop !17

68:                                               ; preds = %59
  %69 = load ptr, ptr %9, align 8, !tbaa !8
  %70 = load ptr, ptr %10, align 8, !tbaa !8
  %71 = getelementptr inbounds %struct.CSPTreeNode, ptr %70, i32 0, i32 1
  store ptr %69, ptr %71, align 8, !tbaa !14
  br label %72

72:                                               ; preds = %68, %54
  br label %73

73:                                               ; preds = %72, %20
  br label %74

74:                                               ; preds = %73, %17
  %75 = load ptr, ptr %6, align 8, !tbaa !8
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  ret ptr %75
}

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 = { 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"any pointer", !6, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"int", !6, i64 0}
!12 = !{!13, !9, i64 16}
!13 = !{!"CSPTreeNode", !6, i64 0, !9, i64 8, !9, i64 16}
!14 = !{!13, !9, i64 8}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = distinct !{!17, !16}


; 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 @createNewNode(i8 noundef signext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local ptr @find(i8 noundef signext, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local ptr @insert(i8 noundef signext %0, ptr noundef %1, i8 noundef signext %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = tail call ptr @find(i8 noundef signext %2, ptr noundef %1)
  %6 = icmp eq ptr %1, null
  br i1 %6, label %7, label %9

7:                                                ; preds = %4
  %8 = tail call ptr @createNewNode(i8 noundef signext %0)
  br label %26

9:                                                ; preds = %4
  %10 = tail call ptr @find(i8 noundef signext %0, ptr noundef nonnull %1)
  %.not = icmp eq ptr %10, null
  br i1 %.not, label %11, label %26

11:                                               ; preds = %9
  %.not27 = icmp eq i32 %3, 0
  %12 = tail call ptr @createNewNode(i8 noundef signext %0)
  br i1 %.not27, label %.preheader32, label %13

13:                                               ; preds = %11
  %14 = getelementptr inbounds i8, ptr %5, i64 16
  %15 = load ptr, ptr %14, align 8, !tbaa !5
  %16 = icmp eq ptr %15, null
  br i1 %16, label %17, label %.preheader

17:                                               ; preds = %13
  store ptr %12, ptr %14, align 8, !tbaa !5
  br label %26

.preheader:                                       ; preds = %13, %.preheader
  %.022 = phi ptr [ %19, %.preheader ], [ %15, %13 ]
  %18 = getelementptr inbounds i8, ptr %.022, i64 8
  %19 = load ptr, ptr %18, align 8, !tbaa !10
  %.not29 = icmp eq ptr %19, null
  br i1 %.not29, label %20, label %.preheader, !llvm.loop !11

20:                                               ; preds = %.preheader
  %21 = getelementptr inbounds i8, ptr %.022, i64 8
  store ptr %12, ptr %21, align 8, !tbaa !10
  br label %26

.preheader32:                                     ; preds = %11, %.preheader32
  %.1 = phi ptr [ %23, %.preheader32 ], [ %5, %11 ]
  %22 = getelementptr inbounds i8, ptr %.1, i64 8
  %23 = load ptr, ptr %22, align 8, !tbaa !10
  %.not28 = icmp eq ptr %23, null
  br i1 %.not28, label %24, label %.preheader32, !llvm.loop !13

24:                                               ; preds = %.preheader32
  %25 = getelementptr inbounds i8, ptr %.1, i64 8
  store ptr %12, ptr %25, align 8, !tbaa !10
  br label %26

26:                                               ; preds = %9, %17, %20, %24, %7
  %.0 = phi ptr [ %8, %7 ], [ %1, %9 ], [ %1, %17 ], [ %1, %20 ], [ %1, %24 ]
  ret ptr %.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, !9, i64 16}
!6 = !{!"CSPTreeNode", !7, i64 0, !9, i64 8, !9, i64 16}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"any pointer", !7, i64 0}
!10 = !{!6, !9, i64 8}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


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