; LLVM IR from AlgoGenesis/C :: String Algorithms/Huffman Coding (Program)

; 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.MinHeap = type { i32, [256 x ptr] }
%struct.Node = type { i8, i32, 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 void @buildMinHeap(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  %7 = load ptr, ptr %2, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.MinHeap, ptr %7, i32 0, i32 0
  %9 = load i32, ptr %8, align 8, !tbaa !9
  %10 = sub nsw i32 %9, 1
  %11 = sdiv i32 %10, 2
  store i32 %11, ptr %3, align 4, !tbaa !12
  br label %12

12:                                               ; preds = %75, %1
  %13 = load i32, ptr %3, align 4, !tbaa !12
  %14 = icmp sge i32 %13, 0
  br i1 %14, label %16, label %15

15:                                               ; preds = %12
  store i32 2, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  br label %78

16:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  store i32 0, ptr %5, align 4, !tbaa !12
  br label %17

17:                                               ; preds = %71, %16
  %18 = load i32, ptr %5, align 4, !tbaa !12
  %19 = load ptr, ptr %2, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.MinHeap, ptr %19, i32 0, i32 0
  %21 = load i32, ptr %20, align 8, !tbaa !9
  %22 = sub nsw i32 %21, 1
  %23 = icmp slt i32 %18, %22
  br i1 %23, label %25, label %24

24:                                               ; preds = %17
  store i32 5, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  br label %74

25:                                               ; preds = %17
  %26 = load ptr, ptr %2, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.MinHeap, ptr %26, i32 0, i32 1
  %28 = load i32, ptr %5, align 4, !tbaa !12
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [256 x ptr], ptr %27, i64 0, i64 %29
  %31 = load ptr, ptr %30, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.Node, ptr %31, i32 0, i32 1
  %33 = load i32, ptr %32, align 4, !tbaa !13
  %34 = load ptr, ptr %2, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.MinHeap, ptr %34, i32 0, i32 1
  %36 = load i32, ptr %5, align 4, !tbaa !12
  %37 = add nsw i32 %36, 1
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [256 x ptr], ptr %35, i64 0, i64 %38
  %40 = load ptr, ptr %39, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.Node, ptr %40, i32 0, i32 1
  %42 = load i32, ptr %41, align 4, !tbaa !13
  %43 = icmp sgt i32 %33, %42
  br i1 %43, label %44, label %70

44:                                               ; preds = %25
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %45 = load ptr, ptr %2, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.MinHeap, ptr %45, i32 0, i32 1
  %47 = load i32, ptr %5, align 4, !tbaa !12
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [256 x ptr], ptr %46, i64 0, i64 %48
  %50 = load ptr, ptr %49, align 8, !tbaa !5
  store ptr %50, ptr %6, align 8, !tbaa !5
  %51 = load ptr, ptr %2, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.MinHeap, ptr %51, i32 0, i32 1
  %53 = load i32, ptr %5, align 4, !tbaa !12
  %54 = add nsw i32 %53, 1
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds [256 x ptr], ptr %52, i64 0, i64 %55
  %57 = load ptr, ptr %56, align 8, !tbaa !5
  %58 = load ptr, ptr %2, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.MinHeap, ptr %58, i32 0, i32 1
  %60 = load i32, ptr %5, align 4, !tbaa !12
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds [256 x ptr], ptr %59, i64 0, i64 %61
  store ptr %57, ptr %62, align 8, !tbaa !5
  %63 = load ptr, ptr %6, align 8, !tbaa !5
  %64 = load ptr, ptr %2, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.MinHeap, ptr %64, i32 0, i32 1
  %66 = load i32, ptr %5, align 4, !tbaa !12
  %67 = add nsw i32 %66, 1
  %68 = sext i32 %67 to i64
  %69 = getelementptr inbounds [256 x ptr], ptr %65, i64 0, i64 %68
  store ptr %63, ptr %69, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  br label %70

70:                                               ; preds = %44, %25
  br label %71

71:                                               ; preds = %70
  %72 = load i32, ptr %5, align 4, !tbaa !12
  %73 = add nsw i32 %72, 1
  store i32 %73, ptr %5, align 4, !tbaa !12
  br label %17, !llvm.loop !15

74:                                               ; preds = %24
  br label %75

75:                                               ; preds = %74
  %76 = load i32, ptr %3, align 4, !tbaa !12
  %77 = add nsw i32 %76, -1
  store i32 %77, ptr %3, align 4, !tbaa !12
  br label %12, !llvm.loop !17

78:                                               ; preds = %15
  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 0}
!10 = !{!"MinHeap", !11, i64 0, !7, i64 8}
!11 = !{!"int", !7, i64 0}
!12 = !{!11, !11, i64 0}
!13 = !{!14, !11, i64 4}
!14 = !{!"Node", !7, i64 0, !11, i64 4, !6, i64 8, !6, i64 16}
!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: nofree norecurse nosync nounwind memory(read, argmem: readwrite, inaccessiblemem: none) uwtable
define dso_local void @buildMinHeap(ptr nocapture noundef %0) local_unnamed_addr #0 {
  %2 = load i32, ptr %0, align 8, !tbaa !5
  %3 = add i32 %2, -1
  %4 = icmp sgt i32 %2, -1
  br i1 %4, label %.preheader.lr.ph, label %._crit_edge21

.preheader.lr.ph:                                 ; preds = %1
  %5 = icmp ugt i32 %2, 1
  %6 = getelementptr inbounds i8, ptr %0, i64 8
  br i1 %5, label %.preheader.us.preheader, label %._crit_edge21

.preheader.us.preheader:                          ; preds = %.preheader.lr.ph
  %7 = lshr i32 %3, 1
  %wide.trip.count = zext i32 %3 to i64
  %xtraiter = and i64 %wide.trip.count, 1
  %8 = icmp eq i32 %3, 1
  %unroll_iter = and i64 %wide.trip.count, 4294967294
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br label %.preheader.us

.preheader.us:                                    ; preds = %.preheader.us.preheader, %._crit_edge.us
  %.020.us = phi i32 [ %41, %._crit_edge.us ], [ %7, %.preheader.us.preheader ]
  %.pre = load ptr, ptr %6, align 8, !tbaa !10
  br i1 %8, label %._crit_edge.us.unr-lcssa, label %.preheader.us.new

.preheader.us.new:                                ; preds = %.preheader.us, %30
  %9 = phi ptr [ %31, %30 ], [ %.pre, %.preheader.us ]
  %indvars.iv = phi i64 [ %indvars.iv.next.1, %30 ], [ 0, %.preheader.us ]
  %niter = phi i64 [ %niter.next.1, %30 ], [ 0, %.preheader.us ]
  %10 = getelementptr inbounds i8, ptr %9, i64 4
  %11 = load i32, ptr %10, align 4, !tbaa !12
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %12 = getelementptr inbounds [256 x ptr], ptr %6, i64 0, i64 %indvars.iv.next
  %13 = load ptr, ptr %12, align 8, !tbaa !10
  %14 = getelementptr inbounds i8, ptr %13, i64 4
  %15 = load i32, ptr %14, align 4, !tbaa !12
  %16 = icmp sgt i32 %11, %15
  br i1 %16, label %17, label %19

17:                                               ; preds = %.preheader.us.new
  %18 = getelementptr inbounds [256 x ptr], ptr %6, i64 0, i64 %indvars.iv
  store ptr %13, ptr %18, align 8, !tbaa !10
  store ptr %9, ptr %12, align 8, !tbaa !10
  br label %19

19:                                               ; preds = %17, %.preheader.us.new
  %20 = phi ptr [ %9, %17 ], [ %13, %.preheader.us.new ]
  %21 = getelementptr inbounds i8, ptr %20, i64 4
  %22 = load i32, ptr %21, align 4, !tbaa !12
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %23 = getelementptr inbounds [256 x ptr], ptr %6, i64 0, i64 %indvars.iv.next.1
  %24 = load ptr, ptr %23, align 8, !tbaa !10
  %25 = getelementptr inbounds i8, ptr %24, i64 4
  %26 = load i32, ptr %25, align 4, !tbaa !12
  %27 = icmp sgt i32 %22, %26
  br i1 %27, label %28, label %30

28:                                               ; preds = %19
  %29 = getelementptr inbounds [256 x ptr], ptr %6, i64 0, i64 %indvars.iv.next
  store ptr %24, ptr %29, align 8, !tbaa !10
  store ptr %20, ptr %23, align 8, !tbaa !10
  br label %30

30:                                               ; preds = %28, %19
  %31 = phi ptr [ %20, %28 ], [ %24, %19 ]
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.us.unr-lcssa, label %.preheader.us.new, !llvm.loop !14

._crit_edge.us.unr-lcssa:                         ; preds = %30, %.preheader.us
  %.unr = phi ptr [ %.pre, %.preheader.us ], [ %31, %30 ]
  %indvars.iv.unr = phi i64 [ 0, %.preheader.us ], [ %indvars.iv.next.1, %30 ]
  br i1 %lcmp.mod.not, label %._crit_edge.us, label %.epil.preheader

.epil.preheader:                                  ; preds = %._crit_edge.us.unr-lcssa
  %32 = getelementptr inbounds i8, ptr %.unr, i64 4
  %33 = load i32, ptr %32, align 4, !tbaa !12
  %indvars.iv.next.epil = add nuw nsw i64 %indvars.iv.unr, 1
  %34 = getelementptr inbounds [256 x ptr], ptr %6, i64 0, i64 %indvars.iv.next.epil
  %35 = load ptr, ptr %34, align 8, !tbaa !10
  %36 = getelementptr inbounds i8, ptr %35, i64 4
  %37 = load i32, ptr %36, align 4, !tbaa !12
  %38 = icmp sgt i32 %33, %37
  br i1 %38, label %39, label %._crit_edge.us

39:                                               ; preds = %.epil.preheader
  %40 = getelementptr inbounds [256 x ptr], ptr %6, i64 0, i64 %indvars.iv.unr
  store ptr %35, ptr %40, align 8, !tbaa !10
  store ptr %.unr, ptr %34, align 8, !tbaa !10
  br label %._crit_edge.us

._crit_edge.us:                                   ; preds = %.epil.preheader, %39, %._crit_edge.us.unr-lcssa
  %41 = add nsw i32 %.020.us, -1
  %42 = icmp sgt i32 %.020.us, 0
  br i1 %42, label %.preheader.us, label %._crit_edge21, !llvm.loop !16

._crit_edge21:                                    ; preds = %._crit_edge.us, %.preheader.lr.ph, %1
  ret void
}

attributes #0 = { nofree norecurse nosync nounwind memory(read, argmem: readwrite, 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 = !{!"MinHeap", !7, i64 0, !8, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!11, !11, i64 0}
!11 = !{!"any pointer", !8, i64 0}
!12 = !{!13, !7, i64 4}
!13 = !{!"Node", !8, i64 0, !7, i64 4, !11, i64 8, !11, i64 16}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}


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