; LLVM IR from TyrantLucifer/DataStructure :: Tree (HuffmanTree)

; 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.HFTree = type { ptr, i32 }
%struct.TreeNode = type { i32, i32, i32, i32 }

; 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 ptr @selectMin(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @createHFTree(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %8 = load ptr, ptr %2, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.HFTree, ptr %8, i32 0, i32 1
  %10 = load i32, ptr %9, align 8, !tbaa !9
  %11 = mul nsw i32 %10, 2
  %12 = sub nsw i32 %11, 1
  store i32 %12, ptr %6, align 4, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %13 = load ptr, ptr %2, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.HFTree, ptr %13, i32 0, i32 1
  %15 = load i32, ptr %14, align 8, !tbaa !9
  store i32 %15, ptr %7, align 4, !tbaa !12
  br label %16

16:                                               ; preds = %97, %1
  %17 = load i32, ptr %7, align 4, !tbaa !12
  %18 = load i32, ptr %6, align 4, !tbaa !12
  %19 = icmp slt i32 %17, %18
  br i1 %19, label %21, label %20

20:                                               ; preds = %16
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  br label %100

21:                                               ; preds = %16
  %22 = load ptr, ptr %2, align 8, !tbaa !5
  %23 = call ptr @selectMin(ptr noundef %22)
  store ptr %23, ptr %3, align 8, !tbaa !5
  %24 = load ptr, ptr %3, align 8, !tbaa !5
  %25 = getelementptr inbounds i32, ptr %24, i64 0
  %26 = load i32, ptr %25, align 4, !tbaa !12
  store i32 %26, ptr %4, align 4, !tbaa !12
  %27 = load ptr, ptr %3, align 8, !tbaa !5
  %28 = getelementptr inbounds i32, ptr %27, i64 1
  %29 = load i32, ptr %28, align 4, !tbaa !12
  store i32 %29, ptr %5, align 4, !tbaa !12
  %30 = load ptr, ptr %2, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.HFTree, ptr %30, i32 0, i32 0
  %32 = load ptr, ptr %31, align 8, !tbaa !13
  %33 = load i32, ptr %4, align 4, !tbaa !12
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds %struct.TreeNode, ptr %32, i64 %34
  %36 = getelementptr inbounds %struct.TreeNode, ptr %35, i32 0, i32 0
  %37 = load i32, ptr %36, align 4, !tbaa !14
  %38 = load ptr, ptr %2, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.HFTree, ptr %38, i32 0, i32 0
  %40 = load ptr, ptr %39, align 8, !tbaa !13
  %41 = load i32, ptr %5, align 4, !tbaa !12
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds %struct.TreeNode, ptr %40, i64 %42
  %44 = getelementptr inbounds %struct.TreeNode, ptr %43, i32 0, i32 0
  %45 = load i32, ptr %44, align 4, !tbaa !14
  %46 = add nsw i32 %37, %45
  %47 = load ptr, ptr %2, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.HFTree, ptr %47, i32 0, i32 0
  %49 = load ptr, ptr %48, align 8, !tbaa !13
  %50 = load i32, ptr %7, align 4, !tbaa !12
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds %struct.TreeNode, ptr %49, i64 %51
  %53 = getelementptr inbounds %struct.TreeNode, ptr %52, i32 0, i32 0
  store i32 %46, ptr %53, align 4, !tbaa !14
  %54 = load i32, ptr %4, align 4, !tbaa !12
  %55 = load ptr, ptr %2, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.HFTree, ptr %55, i32 0, i32 0
  %57 = load ptr, ptr %56, align 8, !tbaa !13
  %58 = load i32, ptr %7, align 4, !tbaa !12
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds %struct.TreeNode, ptr %57, i64 %59
  %61 = getelementptr inbounds %struct.TreeNode, ptr %60, i32 0, i32 2
  store i32 %54, ptr %61, align 4, !tbaa !16
  %62 = load i32, ptr %5, align 4, !tbaa !12
  %63 = load ptr, ptr %2, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.HFTree, ptr %63, i32 0, i32 0
  %65 = load ptr, ptr %64, align 8, !tbaa !13
  %66 = load i32, ptr %7, align 4, !tbaa !12
  %67 = sext i32 %66 to i64
  %68 = getelementptr inbounds %struct.TreeNode, ptr %65, i64 %67
  %69 = getelementptr inbounds %struct.TreeNode, ptr %68, i32 0, i32 3
  store i32 %62, ptr %69, align 4, !tbaa !17
  %70 = load ptr, ptr %2, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.HFTree, ptr %70, i32 0, i32 0
  %72 = load ptr, ptr %71, align 8, !tbaa !13
  %73 = load i32, ptr %7, align 4, !tbaa !12
  %74 = sext i32 %73 to i64
  %75 = getelementptr inbounds %struct.TreeNode, ptr %72, i64 %74
  %76 = getelementptr inbounds %struct.TreeNode, ptr %75, i32 0, i32 1
  store i32 0, ptr %76, align 4, !tbaa !18
  %77 = load i32, ptr %7, align 4, !tbaa !12
  %78 = load ptr, ptr %2, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.HFTree, ptr %78, i32 0, i32 0
  %80 = load ptr, ptr %79, align 8, !tbaa !13
  %81 = load i32, ptr %4, align 4, !tbaa !12
  %82 = sext i32 %81 to i64
  %83 = getelementptr inbounds %struct.TreeNode, ptr %80, i64 %82
  %84 = getelementptr inbounds %struct.TreeNode, ptr %83, i32 0, i32 1
  store i32 %77, ptr %84, align 4, !tbaa !18
  %85 = load i32, ptr %7, align 4, !tbaa !12
  %86 = load ptr, ptr %2, align 8, !tbaa !5
  %87 = getelementptr inbounds %struct.HFTree, ptr %86, i32 0, i32 0
  %88 = load ptr, ptr %87, align 8, !tbaa !13
  %89 = load i32, ptr %5, align 4, !tbaa !12
  %90 = sext i32 %89 to i64
  %91 = getelementptr inbounds %struct.TreeNode, ptr %88, i64 %90
  %92 = getelementptr inbounds %struct.TreeNode, ptr %91, i32 0, i32 1
  store i32 %85, ptr %92, align 4, !tbaa !18
  %93 = load ptr, ptr %2, align 8, !tbaa !5
  %94 = getelementptr inbounds %struct.HFTree, ptr %93, i32 0, i32 1
  %95 = load i32, ptr %94, align 8, !tbaa !9
  %96 = add nsw i32 %95, 1
  store i32 %96, ptr %94, align 8, !tbaa !9
  br label %97

97:                                               ; preds = %21
  %98 = load i32, ptr %7, align 4, !tbaa !12
  %99 = add nsw i32 %98, 1
  store i32 %99, ptr %7, align 4, !tbaa !12
  br label %16, !llvm.loop !19

100:                                              ; preds = %20
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  ret void
}

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


; 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"

%struct.TreeNode = type { i32, i32, i32, i32 }

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

; Function Attrs: nounwind uwtable
define dso_local void @createHFTree(ptr noundef %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 8
  %3 = load i32, ptr %2, align 8, !tbaa !5
  %4 = shl nsw i32 %3, 1
  %5 = add nsw i32 %4, -1
  %6 = icmp slt i32 %3, %5
  br i1 %6, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %1
  %7 = sext i32 %3 to i64
  br label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %1
  ret void

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %indvars.iv = phi i64 [ %7, %.lr.ph.preheader ], [ %indvars.iv.next, %.lr.ph ]
  %8 = tail call ptr @selectMin(ptr noundef nonnull %0)
  %9 = load i32, ptr %8, align 4, !tbaa !11
  %10 = getelementptr inbounds i8, ptr %8, i64 4
  %11 = load i32, ptr %10, align 4, !tbaa !11
  %12 = load ptr, ptr %0, align 8, !tbaa !12
  %13 = sext i32 %9 to i64
  %14 = getelementptr inbounds %struct.TreeNode, ptr %12, i64 %13
  %15 = load i32, ptr %14, align 4, !tbaa !13
  %16 = sext i32 %11 to i64
  %17 = getelementptr inbounds %struct.TreeNode, ptr %12, i64 %16
  %18 = load i32, ptr %17, align 4, !tbaa !13
  %19 = add nsw i32 %18, %15
  %20 = getelementptr inbounds %struct.TreeNode, ptr %12, i64 %indvars.iv
  store i32 %19, ptr %20, align 4, !tbaa !13
  %21 = getelementptr inbounds %struct.TreeNode, ptr %12, i64 %indvars.iv, i32 2
  store i32 %9, ptr %21, align 4, !tbaa !15
  %22 = getelementptr inbounds %struct.TreeNode, ptr %12, i64 %indvars.iv, i32 3
  store i32 %11, ptr %22, align 4, !tbaa !16
  %23 = getelementptr inbounds %struct.TreeNode, ptr %12, i64 %indvars.iv, i32 1
  store i32 0, ptr %23, align 4, !tbaa !17
  %24 = getelementptr inbounds %struct.TreeNode, ptr %12, i64 %13, i32 1
  %25 = trunc nsw i64 %indvars.iv to i32
  store i32 %25, ptr %24, align 4, !tbaa !17
  %26 = getelementptr inbounds %struct.TreeNode, ptr %12, i64 %16, i32 1
  store i32 %25, ptr %26, align 4, !tbaa !17
  %27 = load i32, ptr %2, align 8, !tbaa !5
  %28 = add nsw i32 %27, 1
  store i32 %28, ptr %2, align 8, !tbaa !5
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %lftr.wideiv = trunc i64 %indvars.iv.next to i32
  %exitcond.not = icmp eq i32 %5, %lftr.wideiv
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !18
}

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


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass