; LLVM IR from ybubnov/libhuffman :: src (tree)

; 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.__huf_node = type { i16, ptr, ptr, ptr }

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

13:                                               ; preds = %3
  %14 = load ptr, ptr %6, align 8, !tbaa !5
  %15 = icmp eq ptr %14, null
  br i1 %15, label %16, label %17

16:                                               ; preds = %13
  store i32 2, ptr %8, align 4, !tbaa !9
  br label %68

17:                                               ; preds = %13
  br label %18

18:                                               ; preds = %17
  br label %19

19:                                               ; preds = %18
  br label %20

20:                                               ; preds = %19
  %21 = load ptr, ptr %7, align 8, !tbaa !5
  %22 = icmp eq ptr %21, null
  br i1 %22, label %23, label %24

23:                                               ; preds = %20
  store i32 2, ptr %8, align 4, !tbaa !9
  br label %68

24:                                               ; preds = %20
  br label %25

25:                                               ; preds = %24
  br label %26

26:                                               ; preds = %25
  br label %27

27:                                               ; preds = %61, %26
  %28 = load ptr, ptr %9, align 8, !tbaa !5
  %29 = icmp ne ptr %28, null
  br i1 %29, label %30, label %67

30:                                               ; preds = %27
  %31 = load ptr, ptr %9, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.__huf_node, ptr %31, i32 0, i32 1
  %33 = load ptr, ptr %32, align 8, !tbaa !13
  %34 = icmp ne ptr %33, null
  br i1 %34, label %36, label %35

35:                                               ; preds = %30
  br label %67

36:                                               ; preds = %30
  %37 = load i64, ptr %10, align 8, !tbaa !11
  %38 = load ptr, ptr %7, align 8, !tbaa !5
  %39 = load i64, ptr %38, align 8, !tbaa !11
  %40 = icmp uge i64 %37, %39
  br i1 %40, label %41, label %45

41:                                               ; preds = %36
  br label %42

42:                                               ; preds = %41
  store i32 0, ptr %8, align 4, !tbaa !9
  br label %68

43:                                               ; No predecessors!
  br label %44

44:                                               ; preds = %43
  br label %45

45:                                               ; preds = %44, %36
  %46 = load ptr, ptr %9, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.__huf_node, ptr %46, i32 0, i32 1
  %48 = load ptr, ptr %47, align 8, !tbaa !13
  %49 = getelementptr inbounds %struct.__huf_node, ptr %48, i32 0, i32 2
  %50 = load ptr, ptr %49, align 8, !tbaa !16
  %51 = load ptr, ptr %9, align 8, !tbaa !5
  %52 = icmp eq ptr %50, %51
  br i1 %52, label %53, label %57

53:                                               ; preds = %45
  %54 = load ptr, ptr %6, align 8, !tbaa !5
  %55 = load i64, ptr %10, align 8, !tbaa !11
  %56 = getelementptr inbounds i8, ptr %54, i64 %55
  store i8 48, ptr %56, align 1, !tbaa !17
  br label %61

57:                                               ; preds = %45
  %58 = load ptr, ptr %6, align 8, !tbaa !5
  %59 = load i64, ptr %10, align 8, !tbaa !11
  %60 = getelementptr inbounds i8, ptr %58, i64 %59
  store i8 49, ptr %60, align 1, !tbaa !17
  br label %61

61:                                               ; preds = %57, %53
  %62 = load i64, ptr %10, align 8, !tbaa !11
  %63 = add i64 %62, 1
  store i64 %63, ptr %10, align 8, !tbaa !11
  %64 = load ptr, ptr %9, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.__huf_node, ptr %64, i32 0, i32 1
  %66 = load ptr, ptr %65, align 8, !tbaa !13
  store ptr %66, ptr %9, align 8, !tbaa !5
  br label %27, !llvm.loop !18

67:                                               ; preds = %35, %27
  br label %68

68:                                               ; preds = %67, %42, %23, %16
  %69 = load i64, ptr %10, align 8, !tbaa !11
  %70 = load ptr, ptr %7, align 8, !tbaa !5
  store i64 %69, ptr %70, align 8, !tbaa !11
  br label %71

71:                                               ; preds = %68
  %72 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %72, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %75

73:                                               ; No predecessors!
  br label %74

74:                                               ; preds = %73
  store i32 0, ptr %11, align 4
  br label %75

75:                                               ; preds = %74, %71
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  %76 = load i32, ptr %11, align 4
  switch i32 %76, label %79 [
    i32 0, label %77
    i32 1, label %77
  ]

77:                                               ; preds = %75, %75
  %78 = load i32, ptr %4, align 4
  ret i32 %78

79:                                               ; preds = %75
  unreachable
}

; 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

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !6, i64 8}
!14 = !{!"__huf_node", !15, i64 0, !6, i64 8, !6, i64 16, !6, i64 24}
!15 = !{!"short", !7, i64 0}
!16 = !{!14, !6, i64 16}
!17 = !{!7, !7, i64 0}
!18 = distinct !{!18, !19}
!19 = !{!"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: nofree norecurse nosync nounwind memory(read, argmem: readwrite, inaccessiblemem: none) uwtable
define dso_local range(i32 0, 3) i32 @huf_node_to_string(ptr noundef readonly %0, ptr noundef writeonly %1, ptr noundef %2) local_unnamed_addr #0 {
  %4 = icmp eq ptr %1, null
  %5 = icmp eq ptr %2, null
  %or.cond = or i1 %4, %5
  br i1 %or.cond, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %3
  %.not24 = icmp eq ptr %0, null
  br i1 %.not24, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %10
  %.01826 = phi i64 [ %15, %10 ], [ 0, %.preheader ]
  %.01925 = phi ptr [ %16, %10 ], [ %0, %.preheader ]
  %6 = getelementptr inbounds i8, ptr %.01925, i64 8
  %7 = load ptr, ptr %6, align 8, !tbaa !5
  %.not22 = icmp eq ptr %7, null
  br i1 %.not22, label %.loopexit, label %8

8:                                                ; preds = %.lr.ph
  %9 = load i64, ptr %2, align 8, !tbaa !11
  %.not23 = icmp ult i64 %.01826, %9
  br i1 %.not23, label %10, label %.loopexit

10:                                               ; preds = %8
  %11 = getelementptr inbounds i8, ptr %7, i64 16
  %12 = load ptr, ptr %11, align 8, !tbaa !13
  %13 = icmp eq ptr %12, %.01925
  %spec.select = select i1 %13, i8 48, i8 49
  %14 = getelementptr inbounds i8, ptr %1, i64 %.01826
  store i8 %spec.select, ptr %14, align 1
  %15 = add nuw i64 %.01826, 1
  %16 = load ptr, ptr %6, align 8, !tbaa !5
  %.not = icmp eq ptr %16, null
  br i1 %.not, label %.loopexit, label %.lr.ph, !llvm.loop !14

.loopexit:                                        ; preds = %.lr.ph, %10, %8, %.preheader, %3
  %.1 = phi i64 [ 0, %3 ], [ 0, %.preheader ], [ %.01826, %.lr.ph ], [ %15, %10 ], [ %.01826, %8 ]
  %.0 = phi i32 [ 2, %3 ], [ 0, %.preheader ], [ 0, %8 ], [ 0, %10 ], [ 0, %.lr.ph ]
  store i64 %.1, ptr %2, align 8, !tbaa !11
  ret i32 %.0
}

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, !10, i64 8}
!6 = !{!"__huf_node", !7, i64 0, !10, i64 8, !10, i64 16, !10, i64 24}
!7 = !{!"short", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !8, i64 0}
!13 = !{!6, !10, i64 16}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,MergedLoadStoreMotionPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass