; LLVM IR from AlgoGenesis/C :: Greedy Algorithms/huffmann encoding (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.HuffmanNode = type { i8, i32, ptr, ptr }

@.str = external hidden unnamed_addr constant [5 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [2 x i8], align 1

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

; Function Attrs: nounwind uwtable
define dso_local void @printCodes(ptr noundef %0, ptr noundef %1, i32 noundef %2, ptr noundef %3) #1 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !9
  store ptr %3, ptr %8, align 8, !tbaa !5
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.HuffmanNode, ptr %11, i32 0, i32 2
  %13 = load ptr, ptr %12, align 8, !tbaa !11
  %14 = icmp ne ptr %13, null
  br i1 %14, label %15, label %27

15:                                               ; preds = %4
  %16 = load ptr, ptr %6, align 8, !tbaa !5
  %17 = load i32, ptr %7, align 4, !tbaa !9
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds i32, ptr %16, i64 %18
  store i32 0, ptr %19, align 4, !tbaa !9
  %20 = load ptr, ptr %5, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.HuffmanNode, ptr %20, i32 0, i32 2
  %22 = load ptr, ptr %21, align 8, !tbaa !11
  %23 = load ptr, ptr %6, align 8, !tbaa !5
  %24 = load i32, ptr %7, align 4, !tbaa !9
  %25 = add nsw i32 %24, 1
  %26 = load ptr, ptr %8, align 8, !tbaa !5
  call void @printCodes(ptr noundef %22, ptr noundef %23, i32 noundef %25, ptr noundef %26)
  br label %27

27:                                               ; preds = %15, %4
  %28 = load ptr, ptr %5, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.HuffmanNode, ptr %28, i32 0, i32 3
  %30 = load ptr, ptr %29, align 8, !tbaa !13
  %31 = icmp ne ptr %30, null
  br i1 %31, label %32, label %44

32:                                               ; preds = %27
  %33 = load ptr, ptr %6, align 8, !tbaa !5
  %34 = load i32, ptr %7, align 4, !tbaa !9
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds i32, ptr %33, i64 %35
  store i32 1, ptr %36, align 4, !tbaa !9
  %37 = load ptr, ptr %5, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.HuffmanNode, ptr %37, i32 0, i32 3
  %39 = load ptr, ptr %38, align 8, !tbaa !13
  %40 = load ptr, ptr %6, align 8, !tbaa !5
  %41 = load i32, ptr %7, align 4, !tbaa !9
  %42 = add nsw i32 %41, 1
  %43 = load ptr, ptr %8, align 8, !tbaa !5
  call void @printCodes(ptr noundef %39, ptr noundef %40, i32 noundef %42, ptr noundef %43)
  br label %44

44:                                               ; preds = %32, %27
  %45 = load ptr, ptr %5, align 8, !tbaa !5
  %46 = call i32 @isLeaf(ptr noundef %45)
  %47 = icmp ne i32 %46, 0
  br i1 %47, label %48, label %106

48:                                               ; preds = %44
  %49 = load ptr, ptr %5, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.HuffmanNode, ptr %49, i32 0, i32 0
  %51 = load i8, ptr %50, align 8, !tbaa !14
  %52 = sext i8 %51 to i32
  %53 = call i32 (ptr, ...) @printf(ptr noundef @.str, i32 noundef %52)
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  store i32 0, ptr %9, align 4, !tbaa !9
  br label %54

54:                                               ; preds = %66, %48
  %55 = load i32, ptr %9, align 4, !tbaa !9
  %56 = load i32, ptr %7, align 4, !tbaa !9
  %57 = icmp slt i32 %55, %56
  br i1 %57, label %59, label %58

58:                                               ; preds = %54
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  br label %69

59:                                               ; preds = %54
  %60 = load ptr, ptr %6, align 8, !tbaa !5
  %61 = load i32, ptr %9, align 4, !tbaa !9
  %62 = sext i32 %61 to i64
  %63 = getelementptr inbounds i32, ptr %60, i64 %62
  %64 = load i32, ptr %63, align 4, !tbaa !9
  %65 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %64)
  br label %66

66:                                               ; preds = %59
  %67 = load i32, ptr %9, align 4, !tbaa !9
  %68 = add nsw i32 %67, 1
  store i32 %68, ptr %9, align 4, !tbaa !9
  br label %54, !llvm.loop !15

69:                                               ; preds = %58
  %70 = call i32 (ptr, ...) @printf(ptr noundef @.str.2)
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  store i32 0, ptr %10, align 4, !tbaa !9
  br label %71

71:                                               ; preds = %93, %69
  %72 = load i32, ptr %10, align 4, !tbaa !9
  %73 = load i32, ptr %7, align 4, !tbaa !9
  %74 = icmp slt i32 %72, %73
  br i1 %74, label %76, label %75

75:                                               ; preds = %71
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  br label %96

76:                                               ; preds = %71
  %77 = load ptr, ptr %6, align 8, !tbaa !5
  %78 = load i32, ptr %10, align 4, !tbaa !9
  %79 = sext i32 %78 to i64
  %80 = getelementptr inbounds i32, ptr %77, i64 %79
  %81 = load i32, ptr %80, align 4, !tbaa !9
  %82 = add nsw i32 %81, 48
  %83 = trunc i32 %82 to i8
  %84 = load ptr, ptr %8, align 8, !tbaa !5
  %85 = load ptr, ptr %5, align 8, !tbaa !5
  %86 = getelementptr inbounds %struct.HuffmanNode, ptr %85, i32 0, i32 0
  %87 = load i8, ptr %86, align 8, !tbaa !14
  %88 = sext i8 %87 to i64
  %89 = getelementptr inbounds [256 x i8], ptr %84, i64 %88
  %90 = load i32, ptr %10, align 4, !tbaa !9
  %91 = sext i32 %90 to i64
  %92 = getelementptr inbounds [256 x i8], ptr %89, i64 0, i64 %91
  store i8 %83, ptr %92, align 1, !tbaa !17
  br label %93

93:                                               ; preds = %76
  %94 = load i32, ptr %10, align 4, !tbaa !9
  %95 = add nsw i32 %94, 1
  store i32 %95, ptr %10, align 4, !tbaa !9
  br label %71, !llvm.loop !18

96:                                               ; preds = %75
  %97 = load ptr, ptr %8, align 8, !tbaa !5
  %98 = load ptr, ptr %5, align 8, !tbaa !5
  %99 = getelementptr inbounds %struct.HuffmanNode, ptr %98, i32 0, i32 0
  %100 = load i8, ptr %99, align 8, !tbaa !14
  %101 = sext i8 %100 to i64
  %102 = getelementptr inbounds [256 x i8], ptr %97, i64 %101
  %103 = load i32, ptr %7, align 4, !tbaa !9
  %104 = sext i32 %103 to i64
  %105 = getelementptr inbounds [256 x i8], ptr %102, i64 0, i64 %104
  store i8 0, ptr %105, align 1, !tbaa !17
  br label %106

106:                                              ; preds = %96, %44
  ret void
}

declare i32 @printf(ptr noundef, ...) #2

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

@.str = external hidden unnamed_addr constant [5 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [2 x i8], align 1

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

; Function Attrs: nounwind uwtable
define dso_local void @printCodes(ptr noundef %0, ptr noundef %1, i32 noundef %2, ptr noundef %3) local_unnamed_addr #0 {
  %5 = getelementptr inbounds i8, ptr %0, i64 8
  %6 = load ptr, ptr %5, align 8, !tbaa !5
  %.not = icmp eq ptr %6, null
  br i1 %.not, label %11, label %7

7:                                                ; preds = %4
  %8 = sext i32 %2 to i64
  %9 = getelementptr inbounds i32, ptr %1, i64 %8
  store i32 0, ptr %9, align 4, !tbaa !11
  %10 = add nsw i32 %2, 1
  tail call void @printCodes(ptr noundef nonnull %6, ptr noundef %1, i32 noundef %10, ptr noundef %3)
  br label %11

11:                                               ; preds = %7, %4
  %12 = getelementptr inbounds i8, ptr %0, i64 16
  %13 = load ptr, ptr %12, align 8, !tbaa !12
  %.not33 = icmp eq ptr %13, null
  br i1 %.not33, label %18, label %14

14:                                               ; preds = %11
  %15 = sext i32 %2 to i64
  %16 = getelementptr inbounds i32, ptr %1, i64 %15
  store i32 1, ptr %16, align 4, !tbaa !11
  %17 = add nsw i32 %2, 1
  tail call void @printCodes(ptr noundef nonnull %13, ptr noundef %1, i32 noundef %17, ptr noundef %3)
  br label %18

18:                                               ; preds = %14, %11
  %19 = tail call i32 @isLeaf(ptr noundef nonnull %0)
  %.not34 = icmp eq i32 %19, 0
  br i1 %.not34, label %56, label %20

20:                                               ; preds = %18
  %21 = load i8, ptr %0, align 8, !tbaa !13
  %22 = sext i8 %21 to i32
  %23 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %22)
  %24 = icmp sgt i32 %2, 0
  br i1 %24, label %.lr.ph.preheader, label %._crit_edge.thread

._crit_edge.thread:                               ; preds = %20
  %25 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2)
  br label %._crit_edge39

.lr.ph.preheader:                                 ; preds = %20
  %wide.trip.count = zext nneg i32 %2 to i64
  br label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph
  %26 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2)
  br i1 %24, label %.lr.ph38.preheader, label %._crit_edge39

.lr.ph38.preheader:                               ; preds = %._crit_edge
  %xtraiter = and i64 %wide.trip.count, 1
  %27 = icmp eq i32 %2, 1
  br i1 %27, label %._crit_edge39.loopexit.unr-lcssa, label %.lr.ph38.preheader.new

.lr.ph38.preheader.new:                           ; preds = %.lr.ph38.preheader
  %unroll_iter = and i64 %wide.trip.count, 2147483646
  br label %.lr.ph38

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next, %.lr.ph ]
  %28 = getelementptr inbounds i32, ptr %1, i64 %indvars.iv
  %29 = load i32, ptr %28, align 4, !tbaa !11
  %30 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %29)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !14

._crit_edge39.loopexit.unr-lcssa:                 ; preds = %.lr.ph38, %.lr.ph38.preheader
  %indvars.iv41.unr = phi i64 [ 0, %.lr.ph38.preheader ], [ %indvars.iv.next42.1, %.lr.ph38 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge39, label %.lr.ph38.epil

.lr.ph38.epil:                                    ; preds = %._crit_edge39.loopexit.unr-lcssa
  %31 = getelementptr inbounds i32, ptr %1, i64 %indvars.iv41.unr
  %32 = load i32, ptr %31, align 4, !tbaa !11
  %33 = trunc i32 %32 to i8
  %34 = add i8 %33, 48
  %35 = load i8, ptr %0, align 8, !tbaa !13
  %36 = sext i8 %35 to i64
  %37 = getelementptr inbounds [256 x i8], ptr %3, i64 %36, i64 %indvars.iv41.unr
  store i8 %34, ptr %37, align 1, !tbaa !16
  br label %._crit_edge39

._crit_edge39:                                    ; preds = %.lr.ph38.epil, %._crit_edge39.loopexit.unr-lcssa, %._crit_edge.thread, %._crit_edge
  %38 = load i8, ptr %0, align 8, !tbaa !13
  %39 = sext i8 %38 to i64
  %40 = sext i32 %2 to i64
  %41 = getelementptr inbounds [256 x i8], ptr %3, i64 %39, i64 %40
  store i8 0, ptr %41, align 1, !tbaa !16
  br label %56

.lr.ph38:                                         ; preds = %.lr.ph38, %.lr.ph38.preheader.new
  %indvars.iv41 = phi i64 [ 0, %.lr.ph38.preheader.new ], [ %indvars.iv.next42.1, %.lr.ph38 ]
  %niter = phi i64 [ 0, %.lr.ph38.preheader.new ], [ %niter.next.1, %.lr.ph38 ]
  %42 = getelementptr inbounds i32, ptr %1, i64 %indvars.iv41
  %43 = load i32, ptr %42, align 4, !tbaa !11
  %44 = trunc i32 %43 to i8
  %45 = add i8 %44, 48
  %46 = load i8, ptr %0, align 8, !tbaa !13
  %47 = sext i8 %46 to i64
  %48 = getelementptr inbounds [256 x i8], ptr %3, i64 %47, i64 %indvars.iv41
  store i8 %45, ptr %48, align 1, !tbaa !16
  %indvars.iv.next42 = or disjoint i64 %indvars.iv41, 1
  %49 = getelementptr inbounds i32, ptr %1, i64 %indvars.iv.next42
  %50 = load i32, ptr %49, align 4, !tbaa !11
  %51 = trunc i32 %50 to i8
  %52 = add i8 %51, 48
  %53 = load i8, ptr %0, align 8, !tbaa !13
  %54 = sext i8 %53 to i64
  %55 = getelementptr inbounds [256 x i8], ptr %3, i64 %54, i64 %indvars.iv.next42
  store i8 %52, ptr %55, align 1, !tbaa !16
  %indvars.iv.next42.1 = add nuw nsw i64 %indvars.iv41, 2
  %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_edge39.loopexit.unr-lcssa, label %.lr.ph38, !llvm.loop !17

56:                                               ; preds = %._crit_edge39, %18
  ret void
}

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #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" }
attributes #1 = { nofree nounwind "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 = !{!"HuffmanNode", !7, i64 0, !9, i64 4, !10, i64 8, !10, i64 16}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"int", !7, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!9, !9, i64 0}
!12 = !{!6, !10, i64 16}
!13 = !{!6, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!7, !7, i64 0}
!17 = distinct !{!17, !15}


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