; 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: 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
declare dso_local i32 @isEmpty(ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local ptr @createQueue() #1

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

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

; Function Attrs: nounwind uwtable
declare dso_local ptr @frontAndDequeue(ptr noundef) #1

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

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

13:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %14 = call ptr @createQueue()
  store ptr %14, ptr %8, align 8, !tbaa !8
  %15 = load ptr, ptr %5, align 8, !tbaa !8
  %16 = load ptr, ptr %8, align 8, !tbaa !8
  call void @enqueue(ptr noundef %15, ptr noundef %16)
  br label %17

17:                                               ; preds = %48, %13
  %18 = load ptr, ptr %8, align 8, !tbaa !8
  %19 = call i32 @isEmpty(ptr noundef %18)
  %20 = icmp ne i32 %19, 0
  %21 = xor i1 %20, true
  br i1 %21, label %22, label %49

22:                                               ; preds = %17
  %23 = load ptr, ptr %8, align 8, !tbaa !8
  %24 = call ptr @frontAndDequeue(ptr noundef %23)
  store ptr %24, ptr %6, align 8, !tbaa !8
  %25 = load ptr, ptr %6, align 8, !tbaa !8
  %26 = getelementptr inbounds %struct.CSPTreeNode, ptr %25, i32 0, i32 0
  %27 = load i8, ptr %26, align 8, !tbaa !10
  %28 = sext i8 %27 to i32
  %29 = load i8, ptr %4, align 1, !tbaa !5
  %30 = sext i8 %29 to i32
  %31 = icmp eq i32 %28, %30
  br i1 %31, label %32, label %34

32:                                               ; preds = %22
  %33 = load ptr, ptr %6, align 8, !tbaa !8
  store ptr %33, ptr %3, align 8
  store i32 1, ptr %9, align 4
  br label %51

34:                                               ; preds = %22
  %35 = load ptr, ptr %6, align 8, !tbaa !8
  %36 = getelementptr inbounds %struct.CSPTreeNode, ptr %35, i32 0, i32 2
  %37 = load ptr, ptr %36, align 8, !tbaa !12
  store ptr %37, ptr %7, align 8, !tbaa !8
  br label %38

38:                                               ; preds = %41, %34
  %39 = load ptr, ptr %7, align 8, !tbaa !8
  %40 = icmp ne ptr %39, null
  br i1 %40, label %41, label %47

41:                                               ; preds = %38
  %42 = load ptr, ptr %7, align 8, !tbaa !8
  %43 = load ptr, ptr %8, align 8, !tbaa !8
  call void @enqueue(ptr noundef %42, ptr noundef %43)
  %44 = load ptr, ptr %7, align 8, !tbaa !8
  %45 = getelementptr inbounds %struct.CSPTreeNode, ptr %44, i32 0, i32 1
  %46 = load ptr, ptr %45, align 8, !tbaa !13
  store ptr %46, ptr %7, align 8, !tbaa !8
  br label %38, !llvm.loop !14

47:                                               ; preds = %38
  br label %48

48:                                               ; preds = %47
  br label %17, !llvm.loop !16

49:                                               ; preds = %17
  %50 = load ptr, ptr %8, align 8, !tbaa !8
  call void @disposeQueue(ptr noundef %50)
  store ptr null, ptr %3, align 8
  store i32 1, ptr %9, align 4
  br label %51

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

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


; 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 i32 @isEmpty(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local ptr @createQueue() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @disposeQueue(ptr noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
declare dso_local ptr @frontAndDequeue(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local noundef ptr @find(i8 noundef signext %0, ptr noundef %1) local_unnamed_addr #0 {
  %3 = icmp eq ptr %1, null
  br i1 %3, label %.loopexit17, label %4

4:                                                ; preds = %2
  %5 = tail call ptr @createQueue()
  tail call void @enqueue(ptr noundef nonnull %1, ptr noundef %5)
  %6 = tail call i32 @isEmpty(ptr noundef %5)
  %.not21 = icmp eq i32 %6, 0
  br i1 %.not21, label %.lr.ph22, label %._crit_edge

.loopexit:                                        ; preds = %.lr.ph, %11
  %7 = tail call i32 @isEmpty(ptr noundef %5)
  %.not = icmp eq i32 %7, 0
  br i1 %.not, label %.lr.ph22, label %._crit_edge, !llvm.loop !5

.lr.ph22:                                         ; preds = %4, %.loopexit
  %8 = tail call ptr @frontAndDequeue(ptr noundef %5)
  %9 = load i8, ptr %8, align 8, !tbaa !7
  %10 = icmp eq i8 %9, %0
  br i1 %10, label %.loopexit17, label %11

11:                                               ; preds = %.lr.ph22
  %12 = getelementptr inbounds i8, ptr %8, i64 16
  %.01418 = load ptr, ptr %12, align 8, !tbaa !12
  %.not1619 = icmp eq ptr %.01418, null
  br i1 %.not1619, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %11, %.lr.ph
  %.01420 = phi ptr [ %.014, %.lr.ph ], [ %.01418, %11 ]
  tail call void @enqueue(ptr noundef nonnull %.01420, ptr noundef %5)
  %13 = getelementptr inbounds i8, ptr %.01420, i64 8
  %.014 = load ptr, ptr %13, align 8, !tbaa !12
  %.not16 = icmp eq ptr %.014, null
  br i1 %.not16, label %.loopexit, label %.lr.ph, !llvm.loop !13

._crit_edge:                                      ; preds = %.loopexit, %4
  tail call void @disposeQueue(ptr noundef %5)
  br label %.loopexit17

.loopexit17:                                      ; preds = %.lr.ph22, %._crit_edge, %2
  %.1 = phi ptr [ null, %2 ], [ null, %._crit_edge ], [ %8, %.lr.ph22 ]
  ret ptr %.1
}

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


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