; LLVM IR from HarshCasper/NeoAlgo :: C/ds (Iterative_inorder)

; 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.Tree = 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
define dso_local ptr @Search_Link(ptr noundef %0, i8 noundef signext %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i8, align 1
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i8 %1, ptr %5, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %8 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %8, ptr %6, align 8, !tbaa !5
  %9 = load ptr, ptr %6, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.Tree, ptr %9, i32 0, i32 0
  %11 = load i8, ptr %10, align 8, !tbaa !10
  %12 = sext i8 %11 to i32
  %13 = load i8, ptr %5, align 1, !tbaa !9
  %14 = sext i8 %13 to i32
  %15 = icmp ne i32 %12, %14
  br i1 %15, label %16, label %50

16:                                               ; preds = %2
  %17 = load ptr, ptr %6, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.Tree, ptr %17, i32 0, i32 1
  %19 = load ptr, ptr %18, align 8, !tbaa !12
  %20 = icmp ne ptr %19, null
  br i1 %20, label %21, label %27

21:                                               ; preds = %16
  %22 = load ptr, ptr %6, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.Tree, ptr %22, i32 0, i32 1
  %24 = load ptr, ptr %23, align 8, !tbaa !12
  %25 = load i8, ptr %5, align 1, !tbaa !9
  %26 = call ptr @Search_Link(ptr noundef %24, i8 noundef signext %25)
  br label %27

27:                                               ; preds = %21, %16
  %28 = load ptr, ptr %6, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.Tree, ptr %28, i32 0, i32 2
  %30 = load ptr, ptr %29, align 8, !tbaa !13
  %31 = icmp ne ptr %30, null
  br i1 %31, label %32, label %38

32:                                               ; preds = %27
  %33 = load ptr, ptr %6, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.Tree, ptr %33, i32 0, i32 2
  %35 = load ptr, ptr %34, align 8, !tbaa !13
  %36 = load i8, ptr %5, align 1, !tbaa !9
  %37 = call ptr @Search_Link(ptr noundef %35, i8 noundef signext %36)
  br label %38

38:                                               ; preds = %32, %27
  %39 = load ptr, ptr %6, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.Tree, ptr %39, i32 0, i32 2
  %41 = load ptr, ptr %40, align 8, !tbaa !13
  %42 = icmp eq ptr %41, null
  br i1 %42, label %43, label %49

43:                                               ; preds = %38
  %44 = load ptr, ptr %6, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.Tree, ptr %44, i32 0, i32 1
  %46 = load ptr, ptr %45, align 8, !tbaa !12
  %47 = icmp eq ptr %46, null
  br i1 %47, label %48, label %49

48:                                               ; preds = %43
  store ptr null, ptr %3, align 8
  store i32 1, ptr %7, align 4
  br label %53

49:                                               ; preds = %43, %38
  br label %52

50:                                               ; preds = %2
  %51 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %51, ptr %3, align 8
  store i32 1, ptr %7, align 4
  br label %53

52:                                               ; preds = %49
  store i32 0, ptr %7, align 4
  br label %53

53:                                               ; preds = %52, %50, %48
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  %54 = load i32, ptr %7, align 4
  switch i32 %54, label %57 [
    i32 0, label %55
    i32 1, label %55
  ]

55:                                               ; preds = %53, %53
  %56 = load ptr, ptr %3, align 8
  ret ptr %56

57:                                               ; preds = %53
  unreachable
}

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 = !{!7, !7, i64 0}
!10 = !{!11, !7, i64 0}
!11 = !{!"Tree", !7, i64 0, !6, i64 8, !6, i64 16}
!12 = !{!11, !6, i64 8}
!13 = !{!11, !6, i64 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: nofree nosync nounwind memory(read, inaccessiblemem: none) uwtable
define dso_local noundef ptr @Search_Link(ptr noundef readonly %0, i8 noundef signext %1) local_unnamed_addr #0 {
  %3 = load i8, ptr %0, align 8, !tbaa !5
  %.not18 = icmp eq i8 %3, %1
  br i1 %.not18, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %2
  %4 = getelementptr inbounds i8, ptr %0, i64 8
  %5 = load ptr, ptr %4, align 8, !tbaa !10
  %.not14.peel = icmp eq ptr %5, null
  br i1 %.not14.peel, label %8, label %6

6:                                                ; preds = %.lr.ph.preheader
  %7 = tail call ptr @Search_Link(ptr noundef nonnull %5, i8 noundef signext %1)
  br label %8

8:                                                ; preds = %6, %.lr.ph.preheader
  %9 = getelementptr inbounds i8, ptr %0, i64 16
  %10 = load ptr, ptr %9, align 8, !tbaa !11
  %.not15.peel.not = icmp eq ptr %10, null
  br i1 %.not15.peel.not, label %._crit_edge, label %tailrecurse.peel

tailrecurse.peel:                                 ; preds = %8
  %11 = load i8, ptr %10, align 8, !tbaa !5
  %.not.peel = icmp eq i8 %11, %1
  br i1 %.not.peel, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %tailrecurse.peel, %tailrecurse
  %.tr19 = phi ptr [ %18, %tailrecurse ], [ %10, %tailrecurse.peel ]
  %12 = getelementptr inbounds i8, ptr %.tr19, i64 8
  %13 = load ptr, ptr %12, align 8, !tbaa !10
  %.not14 = icmp eq ptr %13, null
  br i1 %.not14, label %16, label %14

14:                                               ; preds = %.lr.ph
  %15 = tail call ptr @Search_Link(ptr noundef nonnull %13, i8 noundef signext %1)
  br label %16

16:                                               ; preds = %14, %.lr.ph
  %17 = getelementptr inbounds i8, ptr %.tr19, i64 16
  %18 = load ptr, ptr %17, align 8, !tbaa !11
  %.not15 = icmp eq ptr %18, null
  br i1 %.not15, label %._crit_edge, label %tailrecurse

tailrecurse:                                      ; preds = %16
  %19 = load i8, ptr %18, align 8, !tbaa !5
  %.not = icmp eq i8 %19, %1
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !12

._crit_edge:                                      ; preds = %tailrecurse, %16, %8, %tailrecurse.peel, %2
  %ret.known.tr.lcssa = phi ptr [ %0, %2 ], [ null, %tailrecurse.peel ], [ null, %8 ], [ null, %16 ], [ null, %tailrecurse ]
  ret ptr %ret.known.tr.lcssa
}

attributes #0 = { nofree nosync nounwind memory(read, inaccessiblemem: none) 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, !7, i64 0}
!6 = !{!"Tree", !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 = !{!6, !9, i64 16}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.peeled.count", i32 1}


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