; LLVM IR from UtkarshPathrabe/Competitive-Coding :: Model Algorithms/Huffman Encoding/Huffman Encoding.ll (Z16BuildHuffmanTreePcPii)

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef ptr @_Z7NewNodeci(i8 noundef signext, i32 noundef) #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: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_Z9IsSizeOneP8_minHeap(ptr noundef) #0

; Function Attrs: mustprogress uwtable
declare dso_local noundef ptr @_Z10ExtractMinP8_minHeap(ptr noundef) #2

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

; Function Attrs: mustprogress uwtable
declare dso_local noundef ptr @_Z21CreateAndBuildMinHeapPcPii(ptr noundef, ptr noundef, i32 noundef) #2

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_Z16BuildHuffmanTreePcPii(ptr noundef %0, ptr noundef %1, i32 noundef %2) #2 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i32 %2, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = load ptr, ptr %5, align 8, !tbaa !5
  %13 = load i32, ptr %6, align 4, !tbaa !9
  %14 = call noundef ptr @_Z21CreateAndBuildMinHeapPcPii(ptr noundef %11, ptr noundef %12, i32 noundef %13)
  store ptr %14, ptr %10, align 8, !tbaa !5
  br label %15

15:                                               ; preds = %19, %3
  %16 = load ptr, ptr %10, align 8, !tbaa !5
  %17 = call noundef zeroext i1 @_Z9IsSizeOneP8_minHeap(ptr noundef %16)
  %18 = xor i1 %17, true
  br i1 %18, label %19, label %40

19:                                               ; preds = %15
  %20 = load ptr, ptr %10, align 8, !tbaa !5
  %21 = call noundef ptr @_Z10ExtractMinP8_minHeap(ptr noundef %20)
  store ptr %21, ptr %7, align 8, !tbaa !5
  %22 = load ptr, ptr %10, align 8, !tbaa !5
  %23 = call noundef ptr @_Z10ExtractMinP8_minHeap(ptr noundef %22)
  store ptr %23, ptr %8, align 8, !tbaa !5
  %24 = load ptr, ptr %7, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct._node, ptr %24, i32 0, i32 1
  %26 = load i32, ptr %25, align 4, !tbaa !11
  %27 = load ptr, ptr %8, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct._node, ptr %27, i32 0, i32 1
  %29 = load i32, ptr %28, align 4, !tbaa !11
  %30 = add nsw i32 %26, %29
  %31 = call noundef ptr @_Z7NewNodeci(i8 noundef signext 36, i32 noundef %30)
  store ptr %31, ptr %9, align 8, !tbaa !5
  %32 = load ptr, ptr %7, align 8, !tbaa !5
  %33 = load ptr, ptr %9, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct._node, ptr %33, i32 0, i32 2
  store ptr %32, ptr %34, align 8, !tbaa !13
  %35 = load ptr, ptr %8, align 8, !tbaa !5
  %36 = load ptr, ptr %9, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct._node, ptr %36, i32 0, i32 3
  store ptr %35, ptr %37, align 8, !tbaa !14
  %38 = load ptr, ptr %10, align 8, !tbaa !5
  %39 = load ptr, ptr %9, align 8, !tbaa !5
  call void @_Z13InsertMinHeapP8_minHeapP5_node(ptr noundef %38, ptr noundef %39)
  br label %15, !llvm.loop !15

40:                                               ; preds = %15
  %41 = load ptr, ptr %10, align 8, !tbaa !5
  %42 = call noundef ptr @_Z10ExtractMinP8_minHeap(ptr noundef %41)
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  ret ptr %42
}

attributes #0 = { mustprogress 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 = { mustprogress 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 #3 = { 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 4}
!12 = !{!"_ZTS5_node", !7, i64 0, !10, i64 4, !6, i64 8, !6, i64 16}
!13 = !{!12, !6, i64 8}
!14 = !{!12, !6, i64 16}
!15 = distinct !{!15, !16}
!16 = !{!"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"

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_Z9IsSizeOneP8_minHeap(ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
declare dso_local noundef ptr @_Z10ExtractMinP8_minHeap(ptr noundef) local_unnamed_addr #1

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

; Function Attrs: mustprogress uwtable
declare dso_local noundef ptr @_Z21CreateAndBuildMinHeapPcPii(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_Z16BuildHuffmanTreePcPii(ptr noundef %0, ptr noundef %1, i32 noundef %2) local_unnamed_addr #1 {
  %4 = tail call noundef ptr @_Z21CreateAndBuildMinHeapPcPii(ptr noundef %0, ptr noundef %1, i32 noundef %2)
  %5 = tail call noundef zeroext i1 @_Z9IsSizeOneP8_minHeap(ptr noundef %4)
  br i1 %5, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %3, %.lr.ph
  %6 = tail call noundef ptr @_Z10ExtractMinP8_minHeap(ptr noundef %4)
  %7 = tail call noundef ptr @_Z10ExtractMinP8_minHeap(ptr noundef %4)
  %8 = getelementptr inbounds i8, ptr %6, i64 4
  %9 = load i32, ptr %8, align 4, !tbaa !5
  %10 = getelementptr inbounds i8, ptr %7, i64 4
  %11 = load i32, ptr %10, align 4, !tbaa !5
  %12 = add nsw i32 %11, %9
  %13 = tail call noundef ptr @_Z7NewNodeci(i8 noundef signext 36, i32 noundef %12)
  %14 = getelementptr inbounds i8, ptr %13, i64 8
  store ptr %6, ptr %14, align 8, !tbaa !11
  %15 = getelementptr inbounds i8, ptr %13, i64 16
  store ptr %7, ptr %15, align 8, !tbaa !12
  tail call void @_Z13InsertMinHeapP8_minHeapP5_node(ptr noundef %4, ptr noundef %13)
  %16 = tail call noundef zeroext i1 @_Z9IsSizeOneP8_minHeap(ptr noundef %4)
  br i1 %16, label %._crit_edge, label %.lr.ph, !llvm.loop !13

._crit_edge:                                      ; preds = %.lr.ph, %3
  %17 = tail call noundef ptr @_Z10ExtractMinP8_minHeap(ptr noundef %4)
  ret ptr %17
}

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


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