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

@.str.5 = external hidden unnamed_addr constant [29 x i8], align 1
@stdin = external global ptr, align 8

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

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

; Function Attrs: nounwind uwtable
declare dso_local void @buildCodes(ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @encode(ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @main() #1 {
  %1 = alloca i32, align 4
  %2 = alloca [1000 x i8], align 16
  %3 = alloca i64, align 8
  %4 = alloca [256 x i32], align 16
  %5 = alloca i32, align 4
  %6 = alloca [256 x i8], align 16
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca [256 x [256 x i8]], align 16
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 1000, ptr %2) #5
  %11 = call i32 (ptr, ...) @printf(ptr noundef @.str.5)
  %12 = getelementptr inbounds [1000 x i8], ptr %2, i64 0, i64 0
  %13 = load ptr, ptr @stdin, align 8, !tbaa !5
  %14 = call ptr @fgets(ptr noundef %12, i32 noundef 1000, ptr noundef %13)
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #5
  %15 = getelementptr inbounds [1000 x i8], ptr %2, i64 0, i64 0
  %16 = call i64 @strlen(ptr noundef %15) #6
  store i64 %16, ptr %3, align 8, !tbaa !9
  %17 = load i64, ptr %3, align 8, !tbaa !9
  %18 = sub i64 %17, 1
  %19 = getelementptr inbounds [1000 x i8], ptr %2, i64 0, i64 %18
  %20 = load i8, ptr %19, align 1, !tbaa !11
  %21 = sext i8 %20 to i32
  %22 = icmp eq i32 %21, 10
  br i1 %22, label %23, label %27

23:                                               ; preds = %0
  %24 = load i64, ptr %3, align 8, !tbaa !9
  %25 = sub i64 %24, 1
  %26 = getelementptr inbounds [1000 x i8], ptr %2, i64 0, i64 %25
  store i8 0, ptr %26, align 1, !tbaa !11
  br label %27

27:                                               ; preds = %23, %0
  call void @llvm.lifetime.start.p0(i64 1024, ptr %4) #5
  call void @llvm.memset.p0.i64(ptr align 16 %4, i8 0, i64 1024, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #5
  store i32 0, ptr %5, align 4, !tbaa !12
  br label %28

28:                                               ; preds = %45, %27
  %29 = load i32, ptr %5, align 4, !tbaa !12
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [1000 x i8], ptr %2, i64 0, i64 %30
  %32 = load i8, ptr %31, align 1, !tbaa !11
  %33 = sext i8 %32 to i32
  %34 = icmp ne i32 %33, 0
  br i1 %34, label %36, label %35

35:                                               ; preds = %28
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #5
  br label %48

36:                                               ; preds = %28
  %37 = load i32, ptr %5, align 4, !tbaa !12
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [1000 x i8], ptr %2, i64 0, i64 %38
  %40 = load i8, ptr %39, align 1, !tbaa !11
  %41 = zext i8 %40 to i64
  %42 = getelementptr inbounds [256 x i32], ptr %4, i64 0, i64 %41
  %43 = load i32, ptr %42, align 4, !tbaa !12
  %44 = add i32 %43, 1
  store i32 %44, ptr %42, align 4, !tbaa !12
  br label %45

45:                                               ; preds = %36
  %46 = load i32, ptr %5, align 4, !tbaa !12
  %47 = add nsw i32 %46, 1
  store i32 %47, ptr %5, align 4, !tbaa !12
  br label %28, !llvm.loop !14

48:                                               ; preds = %35
  call void @llvm.lifetime.start.p0(i64 256, ptr %6) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #5
  store i32 0, ptr %7, align 4, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #5
  store i32 0, ptr %8, align 4, !tbaa !12
  br label %49

49:                                               ; preds = %68, %48
  %50 = load i32, ptr %8, align 4, !tbaa !12
  %51 = icmp slt i32 %50, 256
  br i1 %51, label %53, label %52

52:                                               ; preds = %49
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #5
  br label %71

53:                                               ; preds = %49
  %54 = load i32, ptr %8, align 4, !tbaa !12
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds [256 x i32], ptr %4, i64 0, i64 %55
  %57 = load i32, ptr %56, align 4, !tbaa !12
  %58 = icmp ne i32 %57, 0
  br i1 %58, label %59, label %67

59:                                               ; preds = %53
  %60 = load i32, ptr %8, align 4, !tbaa !12
  %61 = trunc i32 %60 to i8
  %62 = load i32, ptr %7, align 4, !tbaa !12
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds [256 x i8], ptr %6, i64 0, i64 %63
  store i8 %61, ptr %64, align 1, !tbaa !11
  %65 = load i32, ptr %7, align 4, !tbaa !12
  %66 = add nsw i32 %65, 1
  store i32 %66, ptr %7, align 4, !tbaa !12
  br label %67

67:                                               ; preds = %59, %53
  br label %68

68:                                               ; preds = %67
  %69 = load i32, ptr %8, align 4, !tbaa !12
  %70 = add nsw i32 %69, 1
  store i32 %70, ptr %8, align 4, !tbaa !12
  br label %49, !llvm.loop !16

71:                                               ; preds = %52
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #5
  %72 = getelementptr inbounds [256 x i8], ptr %6, i64 0, i64 0
  %73 = getelementptr inbounds [256 x i32], ptr %4, i64 0, i64 0
  %74 = load i32, ptr %7, align 4, !tbaa !12
  %75 = call ptr @buildHuffmanTree(ptr noundef %72, ptr noundef %73, i32 noundef %74)
  store ptr %75, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 65536, ptr %10) #5
  call void @llvm.memset.p0.i64(ptr align 16 %10, i8 0, i64 65536, i1 false)
  %76 = load ptr, ptr %9, align 8, !tbaa !5
  %77 = getelementptr inbounds [256 x [256 x i8]], ptr %10, i64 0, i64 0
  call void @buildCodes(ptr noundef %76, ptr noundef %77)
  %78 = getelementptr inbounds [1000 x i8], ptr %2, i64 0, i64 0
  %79 = getelementptr inbounds [256 x [256 x i8]], ptr %10, i64 0, i64 0
  call void @encode(ptr noundef %78, ptr noundef %79)
  call void @llvm.lifetime.end.p0(i64 65536, ptr %10) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 256, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 1024, ptr %4) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #5
  call void @llvm.lifetime.end.p0(i64 1000, ptr %2) #5
  ret i32 0
}

declare ptr @fgets(ptr noundef, i32 noundef, ptr noundef) #2

; Function Attrs: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #3

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #4

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 willreturn memory(read) "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 #4 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #5 = { nounwind }
attributes #6 = { nounwind willreturn memory(read) }

!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 = !{!"long", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}


; 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.5 = external hidden unnamed_addr constant [29 x i8], align 1
@stdin = external local_unnamed_addr global ptr, align 8

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: mustprogress 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 @buildHuffmanTree(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare dso_local void @buildCodes(ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @encode(ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #1 {
  %1 = alloca [1000 x i8], align 16
  %2 = alloca [256 x i32], align 16
  %3 = alloca [256 x i8], align 16
  %4 = alloca [256 x [256 x i8]], align 16
  call void @llvm.lifetime.start.p0(i64 1000, ptr nonnull %1) #5
  %5 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.5)
  %6 = load ptr, ptr @stdin, align 8, !tbaa !5
  %7 = call ptr @fgets(ptr noundef nonnull %1, i32 noundef 1000, ptr noundef %6)
  %8 = call i64 @strlen(ptr noundef nonnull dereferenceable(1) %1) #6
  %9 = add i64 %8, -1
  %10 = getelementptr inbounds [1000 x i8], ptr %1, i64 0, i64 %9
  %11 = load i8, ptr %10, align 1, !tbaa !9
  %12 = icmp eq i8 %11, 10
  br i1 %12, label %13, label %14

13:                                               ; preds = %0
  store i8 0, ptr %10, align 1, !tbaa !9
  br label %14

14:                                               ; preds = %13, %0
  call void @llvm.lifetime.start.p0(i64 1024, ptr nonnull %2) #5
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(1024) %2, i8 0, i64 1024, i1 false)
  %15 = load i8, ptr %1, align 16, !tbaa !9
  %.not15 = icmp eq i8 %15, 0
  br i1 %.not15, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %14
  call void @llvm.lifetime.start.p0(i64 256, ptr nonnull %3) #5
  br label %25

.lr.ph:                                           ; preds = %14, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %14 ]
  %16 = phi i8 [ %22, %.lr.ph ], [ %15, %14 ]
  %17 = zext i8 %16 to i64
  %18 = getelementptr inbounds [256 x i32], ptr %2, i64 0, i64 %17
  %19 = load i32, ptr %18, align 4, !tbaa !10
  %20 = add i32 %19, 1
  store i32 %20, ptr %18, align 4, !tbaa !10
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %21 = getelementptr inbounds [1000 x i8], ptr %1, i64 0, i64 %indvars.iv.next
  %22 = load i8, ptr %21, align 1, !tbaa !9
  %.not = icmp eq i8 %22, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !12

23:                                               ; preds = %41
  %24 = call ptr @buildHuffmanTree(ptr noundef nonnull %3, ptr noundef nonnull %2, i32 noundef %.1.1)
  call void @llvm.lifetime.start.p0(i64 65536, ptr nonnull %4) #5
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(65536) %4, i8 0, i64 65536, i1 false)
  call void @buildCodes(ptr noundef %24, ptr noundef nonnull %4)
  call void @encode(ptr noundef nonnull %1, ptr noundef nonnull %4)
  call void @llvm.lifetime.end.p0(i64 65536, ptr nonnull %4) #5
  call void @llvm.lifetime.end.p0(i64 256, ptr nonnull %3) #5
  call void @llvm.lifetime.end.p0(i64 1024, ptr nonnull %2) #5
  call void @llvm.lifetime.end.p0(i64 1000, ptr nonnull %1) #5
  ret i32 0

25:                                               ; preds = %41, %._crit_edge
  %indvars.iv19 = phi i64 [ 0, %._crit_edge ], [ %indvars.iv.next20.1, %41 ]
  %.01317 = phi i32 [ 0, %._crit_edge ], [ %.1.1, %41 ]
  %26 = getelementptr inbounds [256 x i32], ptr %2, i64 0, i64 %indvars.iv19
  %27 = load i32, ptr %26, align 8, !tbaa !10
  %.not14 = icmp eq i32 %27, 0
  br i1 %.not14, label %33, label %28

28:                                               ; preds = %25
  %29 = trunc i64 %indvars.iv19 to i8
  %30 = sext i32 %.01317 to i64
  %31 = getelementptr inbounds [256 x i8], ptr %3, i64 0, i64 %30
  store i8 %29, ptr %31, align 1, !tbaa !9
  %32 = add nsw i32 %.01317, 1
  br label %33

33:                                               ; preds = %25, %28
  %.1 = phi i32 [ %32, %28 ], [ %.01317, %25 ]
  %indvars.iv.next20 = or disjoint i64 %indvars.iv19, 1
  %34 = getelementptr inbounds [256 x i32], ptr %2, i64 0, i64 %indvars.iv.next20
  %35 = load i32, ptr %34, align 4, !tbaa !10
  %.not14.1 = icmp eq i32 %35, 0
  br i1 %.not14.1, label %41, label %36

36:                                               ; preds = %33
  %37 = trunc i64 %indvars.iv.next20 to i8
  %38 = sext i32 %.1 to i64
  %39 = getelementptr inbounds [256 x i8], ptr %3, i64 0, i64 %38
  store i8 %37, ptr %39, align 1, !tbaa !9
  %40 = add nsw i32 %.1, 1
  br label %41

41:                                               ; preds = %36, %33
  %.1.1 = phi i32 [ %40, %36 ], [ %.1, %33 ]
  %indvars.iv.next20.1 = add nuw nsw i64 %indvars.iv19, 2
  %exitcond.not.1 = icmp eq i64 %indvars.iv.next20.1, 256
  br i1 %exitcond.not.1, label %23, label %25, !llvm.loop !14
}

; Function Attrs: nofree nounwind
declare noundef ptr @fgets(ptr noundef, i32 noundef, ptr nocapture noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #3

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #4

attributes #0 = { mustprogress 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 = { 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" }
attributes #3 = { mustprogress nofree nounwind willreturn memory(argmem: read) "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 #4 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #5 = { nounwind }
attributes #6 = { nounwind willreturn memory(read) }

!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, !11, i64 0}
!11 = !{!"int", !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !13}


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