; LLVM IR from vczh/herodb :: Kernel/Source/Vlpp.ll (ZN2vl6stream10LzwEncoder11WriteNumberEll)

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

%"class.vl::stream::LzwEncoder" = type { %"class.vl::stream::LzwBase", %"class.vl::stream::IEncoder", ptr, [1024 x i8], i64, ptr }
%"class.vl::stream::LzwBase" = type { %"class.vl::Object", %"class.vl::collections::PushOnlyAllocator", %"class.vl::collections::PushOnlyAllocator.30", ptr, i64, i64, i64 }
%"class.vl::Object" = type { ptr }
%"class.vl::collections::PushOnlyAllocator" = type { %"class.vl::Object", i64, i64, %"class.vl::collections::List.22" }
%"class.vl::collections::List.22" = type { %"class.vl::collections::ListBase.base.28", %"class.vl::collections::IEnumerable.26" }
%"class.vl::collections::ListBase.base.28" = type <{ %"class.vl::collections::ArrayBase.base.27", i64, i8 }>
%"class.vl::collections::ArrayBase.base.27" = type { %"class.vl::collections::ListStore.25", ptr, i64 }
%"class.vl::collections::ListStore.25" = type { %"class.vl::Object" }
%"class.vl::collections::IEnumerable.26" = type { %"class.vl::Interface" }
%"class.vl::Interface" = type { ptr }
%"class.vl::collections::PushOnlyAllocator.30" = type { %"class.vl::Object", i64, i64, %"class.vl::collections::List.31" }
%"class.vl::collections::List.31" = type { %"class.vl::collections::ListBase.base.37", %"class.vl::collections::IEnumerable.35" }
%"class.vl::collections::ListBase.base.37" = type <{ %"class.vl::collections::ArrayBase.base.36", i64, i8 }>
%"class.vl::collections::ArrayBase.base.36" = type { %"class.vl::collections::ListStore.34", ptr, i64 }
%"class.vl::collections::ListStore.34" = type { %"class.vl::Object" }
%"class.vl::collections::IEnumerable.35" = type { %"class.vl::Interface" }
%"class.vl::stream::IEncoder" = type { %"class.vl::Interface" }

@_ZN2vl6stream9highMarksE = external dso_local global [9 x i8], align 1
@_ZN2vl6stream8lowMarksE = external dso_local global [9 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: mustprogress uwtable
declare dso_local void @_ZN2vl6stream10LzwEncoder5FlushEv(ptr noundef nonnull align 8 dereferenceable(1240)) #1 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN2vl6stream10LzwEncoder11WriteNumberEll(ptr noundef nonnull align 8 dereferenceable(1240) %0, i64 noundef %1, i64 noundef %2) #1 align 2 {
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca i8, align 1
  %12 = alloca i8, align 1
  %13 = alloca i64, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  store i64 %2, ptr %6, align 8, !tbaa !9
  %14 = load ptr, ptr %4, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  store i64 0, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %15 = getelementptr inbounds %"class.vl::stream::LzwEncoder", ptr %14, i32 0, i32 4
  %16 = load i64, ptr %15, align 8, !tbaa !11
  %17 = srem i64 %16, 8
  %18 = sub nsw i64 8, %17
  store i64 %18, ptr %8, align 8, !tbaa !9
  %19 = load i64, ptr %8, align 8, !tbaa !9
  %20 = load i64, ptr %6, align 8, !tbaa !9
  %21 = icmp sgt i64 %19, %20
  br i1 %21, label %22, label %24

22:                                               ; preds = %3
  %23 = load i64, ptr %6, align 8, !tbaa !9
  store i64 %23, ptr %8, align 8, !tbaa !9
  br label %24

24:                                               ; preds = %22, %3
  br label %25

25:                                               ; preds = %95, %24
  %26 = load i64, ptr %7, align 8, !tbaa !9
  %27 = load i64, ptr %6, align 8, !tbaa !9
  %28 = icmp slt i64 %26, %27
  br i1 %28, label %29, label %97

29:                                               ; preds = %25
  %30 = getelementptr inbounds %"class.vl::stream::LzwEncoder", ptr %14, i32 0, i32 4
  %31 = load i64, ptr %30, align 8, !tbaa !11
  %32 = icmp eq i64 %31, 8192
  br i1 %32, label %33, label %34

33:                                               ; preds = %29
  call void @_ZN2vl6stream10LzwEncoder5FlushEv(ptr noundef nonnull align 8 dereferenceable(1240) %14)
  br label %34

34:                                               ; preds = %33, %29
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  %35 = getelementptr inbounds %"class.vl::stream::LzwEncoder", ptr %14, i32 0, i32 4
  %36 = load i64, ptr %35, align 8, !tbaa !11
  %37 = srem i64 %36, 8
  store i64 %37, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  %38 = getelementptr inbounds %"class.vl::stream::LzwEncoder", ptr %14, i32 0, i32 4
  %39 = load i64, ptr %38, align 8, !tbaa !11
  %40 = sdiv i64 %39, 8
  store i64 %40, ptr %10, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 1, ptr %11) #2
  %41 = getelementptr inbounds %"class.vl::stream::LzwEncoder", ptr %14, i32 0, i32 3
  %42 = load i64, ptr %10, align 8, !tbaa !9
  %43 = getelementptr inbounds [1024 x i8], ptr %41, i64 0, i64 %42
  %44 = load i8, ptr %43, align 1, !tbaa !28
  store i8 %44, ptr %11, align 1, !tbaa !28
  %45 = load i64, ptr %9, align 8, !tbaa !9
  %46 = getelementptr inbounds [9 x i8], ptr @_ZN2vl6stream9highMarksE, i64 0, i64 %45
  %47 = load i8, ptr %46, align 1, !tbaa !28
  %48 = zext i8 %47 to i32
  %49 = load i8, ptr %11, align 1, !tbaa !28
  %50 = zext i8 %49 to i32
  %51 = and i32 %50, %48
  %52 = trunc i32 %51 to i8
  store i8 %52, ptr %11, align 1, !tbaa !28
  call void @llvm.lifetime.start.p0(i64 1, ptr %12) #2
  %53 = load i64, ptr %5, align 8, !tbaa !9
  %54 = load i64, ptr %7, align 8, !tbaa !9
  %55 = ashr i64 %53, %54
  %56 = load i64, ptr %8, align 8, !tbaa !9
  %57 = getelementptr inbounds [9 x i8], ptr @_ZN2vl6stream8lowMarksE, i64 0, i64 %56
  %58 = load i8, ptr %57, align 1, !tbaa !28
  %59 = zext i8 %58 to i64
  %60 = and i64 %55, %59
  %61 = trunc i64 %60 to i8
  %62 = zext i8 %61 to i32
  %63 = load i64, ptr %9, align 8, !tbaa !9
  %64 = sub nsw i64 8, %63
  %65 = load i64, ptr %8, align 8, !tbaa !9
  %66 = sub nsw i64 %64, %65
  %67 = trunc i64 %66 to i32
  %68 = shl i32 %62, %67
  %69 = trunc i32 %68 to i8
  store i8 %69, ptr %12, align 1, !tbaa !28
  %70 = load i8, ptr %12, align 1, !tbaa !28
  %71 = zext i8 %70 to i32
  %72 = load i8, ptr %11, align 1, !tbaa !28
  %73 = zext i8 %72 to i32
  %74 = or i32 %73, %71
  %75 = trunc i32 %74 to i8
  store i8 %75, ptr %11, align 1, !tbaa !28
  %76 = load i8, ptr %11, align 1, !tbaa !28
  %77 = getelementptr inbounds %"class.vl::stream::LzwEncoder", ptr %14, i32 0, i32 3
  %78 = load i64, ptr %10, align 8, !tbaa !9
  %79 = getelementptr inbounds [1024 x i8], ptr %77, i64 0, i64 %78
  store i8 %76, ptr %79, align 1, !tbaa !28
  %80 = load i64, ptr %8, align 8, !tbaa !9
  %81 = getelementptr inbounds %"class.vl::stream::LzwEncoder", ptr %14, i32 0, i32 4
  %82 = load i64, ptr %81, align 8, !tbaa !11
  %83 = add nsw i64 %82, %80
  store i64 %83, ptr %81, align 8, !tbaa !11
  %84 = load i64, ptr %8, align 8, !tbaa !9
  %85 = load i64, ptr %7, align 8, !tbaa !9
  %86 = add nsw i64 %85, %84
  store i64 %86, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #2
  %87 = load i64, ptr %6, align 8, !tbaa !9
  %88 = load i64, ptr %7, align 8, !tbaa !9
  %89 = sub nsw i64 %87, %88
  store i64 %89, ptr %13, align 8, !tbaa !9
  %90 = load i64, ptr %13, align 8, !tbaa !9
  %91 = icmp slt i64 %90, 8
  br i1 %91, label %92, label %94

92:                                               ; preds = %34
  %93 = load i64, ptr %13, align 8, !tbaa !9
  br label %95

94:                                               ; preds = %34
  br label %95

95:                                               ; preds = %94, %92
  %96 = phi i64 [ %93, %92 ], [ 8, %94 ]
  store i64 %96, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %12) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  br label %25, !llvm.loop !29

97:                                               ; preds = %25
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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" }
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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !10, i64 1224}
!12 = !{!"_ZTSN2vl6stream10LzwEncoderE", !13, i64 0, !26, i64 184, !6, i64 192, !7, i64 200, !10, i64 1224, !6, i64 1232}
!13 = !{!"_ZTSN2vl6stream7LzwBaseE", !14, i64 0, !15, i64 8, !21, i64 80, !6, i64 152, !10, i64 160, !10, i64 168, !10, i64 176}
!14 = !{!"_ZTSN2vl6ObjectE"}
!15 = !{!"_ZTSN2vl11collections17PushOnlyAllocatorINS_6stream3lzw4CodeEEE", !14, i64 0, !10, i64 8, !10, i64 16, !16, i64 24}
!16 = !{!"_ZTSN2vl11collections4ListIPNS_6stream3lzw4CodeES5_EE", !17, i64 0}
!17 = !{!"_ZTSN2vl11collections8ListBaseIPNS_6stream3lzw4CodeES5_EE", !18, i64 0, !10, i64 24, !20, i64 32}
!18 = !{!"_ZTSN2vl11collections9ArrayBaseIPNS_6stream3lzw4CodeEEE", !19, i64 0, !6, i64 8, !10, i64 16}
!19 = !{!"_ZTSN2vl11collections9ListStoreIPNS_6stream3lzw4CodeELb1EEE", !14, i64 0}
!20 = !{!"bool", !7, i64 0}
!21 = !{!"_ZTSN2vl11collections17PushOnlyAllocatorINS0_10bom_helper8TreeNodeEEE", !14, i64 0, !10, i64 8, !10, i64 16, !22, i64 24}
!22 = !{!"_ZTSN2vl11collections4ListIPNS0_10bom_helper8TreeNodeES4_EE", !23, i64 0}
!23 = !{!"_ZTSN2vl11collections8ListBaseIPNS0_10bom_helper8TreeNodeES4_EE", !24, i64 0, !10, i64 24, !20, i64 32}
!24 = !{!"_ZTSN2vl11collections9ArrayBaseIPNS0_10bom_helper8TreeNodeEEE", !25, i64 0, !6, i64 8, !10, i64 16}
!25 = !{!"_ZTSN2vl11collections9ListStoreIPNS0_10bom_helper8TreeNodeELb1EEE", !14, i64 0}
!26 = !{!"_ZTSN2vl6stream8IEncoderE", !27, i64 0}
!27 = !{!"_ZTSN2vl9InterfaceE"}
!28 = !{!7, !7, i64 0}
!29 = distinct !{!29, !30}
!30 = !{!"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"

@_ZN2vl6stream9highMarksE = external dso_local local_unnamed_addr global [9 x i8], align 1
@_ZN2vl6stream8lowMarksE = external dso_local local_unnamed_addr global [9 x i8], align 1

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN2vl6stream10LzwEncoder5FlushEv(ptr noundef nonnull align 8 dereferenceable(1240)) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN2vl6stream10LzwEncoder11WriteNumberEll(ptr noundef nonnull align 8 dereferenceable(1240) %0, i64 noundef %1, i64 noundef %2) local_unnamed_addr #0 align 2 {
  %4 = getelementptr inbounds i8, ptr %0, i64 1224
  %5 = icmp sgt i64 %2, 0
  br i1 %5, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %3
  %6 = load i64, ptr %4, align 8, !tbaa !5
  %7 = srem i64 %6, 8
  %8 = sub nsw i64 8, %7
  %9 = tail call i64 @llvm.umin.i64(i64 %8, i64 %2)
  %10 = getelementptr inbounds i8, ptr %0, i64 200
  br label %11

11:                                               ; preds = %.lr.ph, %15
  %12 = phi i64 [ %6, %.lr.ph ], [ %37, %15 ]
  %.029 = phi i64 [ 0, %.lr.ph ], [ %38, %15 ]
  %.128 = phi i64 [ %9, %.lr.ph ], [ %40, %15 ]
  %13 = icmp eq i64 %12, 8192
  br i1 %13, label %14, label %15

14:                                               ; preds = %11
  tail call void @_ZN2vl6stream10LzwEncoder5FlushEv(ptr noundef nonnull align 8 dereferenceable(1240) %0)
  %.pre = load i64, ptr %4, align 8, !tbaa !5
  br label %15

15:                                               ; preds = %14, %11
  %16 = phi i64 [ %.pre, %14 ], [ %12, %11 ]
  %17 = srem i64 %16, 8
  %18 = sdiv i64 %16, 8
  %19 = getelementptr inbounds [1024 x i8], ptr %10, i64 0, i64 %18
  %20 = load i8, ptr %19, align 1, !tbaa !26
  %21 = getelementptr inbounds [9 x i8], ptr @_ZN2vl6stream9highMarksE, i64 0, i64 %17
  %22 = load i8, ptr %21, align 1, !tbaa !26
  %23 = and i8 %22, %20
  %24 = ashr i64 %1, %.029
  %25 = getelementptr inbounds [9 x i8], ptr @_ZN2vl6stream8lowMarksE, i64 0, i64 %.128
  %26 = load i8, ptr %25, align 1, !tbaa !26
  %27 = trunc i64 %24 to i8
  %28 = and i8 %26, %27
  %29 = zext i8 %28 to i32
  %30 = add i64 %17, %.128
  %31 = trunc i64 %30 to i32
  %32 = sub i32 8, %31
  %33 = shl i32 %29, %32
  %34 = trunc i32 %33 to i8
  %35 = or i8 %23, %34
  store i8 %35, ptr %19, align 1, !tbaa !26
  %36 = load i64, ptr %4, align 8, !tbaa !5
  %37 = add nsw i64 %36, %.128
  store i64 %37, ptr %4, align 8, !tbaa !5
  %38 = add nsw i64 %.029, %.128
  %39 = sub nsw i64 %2, %38
  %40 = tail call i64 @llvm.smin.i64(i64 %39, i64 8)
  %41 = icmp slt i64 %38, %2
  br i1 %41, label %11, label %._crit_edge, !llvm.loop !27

._crit_edge:                                      ; preds = %15, %3
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.smin.i64(i64, i64) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umin.i64(i64, i64) #1

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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 1224}
!6 = !{!"_ZTSN2vl6stream10LzwEncoderE", !7, i64 0, !24, i64 184, !17, i64 192, !11, i64 200, !10, i64 1224, !17, i64 1232}
!7 = !{!"_ZTSN2vl6stream7LzwBaseE", !8, i64 0, !9, i64 8, !19, i64 80, !17, i64 152, !10, i64 160, !10, i64 168, !10, i64 176}
!8 = !{!"_ZTSN2vl6ObjectE"}
!9 = !{!"_ZTSN2vl11collections17PushOnlyAllocatorINS_6stream3lzw4CodeEEE", !8, i64 0, !10, i64 8, !10, i64 16, !13, i64 24}
!10 = !{!"long", !11, i64 0}
!11 = !{!"omnipotent char", !12, i64 0}
!12 = !{!"Simple C++ TBAA"}
!13 = !{!"_ZTSN2vl11collections4ListIPNS_6stream3lzw4CodeES5_EE", !14, i64 0}
!14 = !{!"_ZTSN2vl11collections8ListBaseIPNS_6stream3lzw4CodeES5_EE", !15, i64 0, !10, i64 24, !18, i64 32}
!15 = !{!"_ZTSN2vl11collections9ArrayBaseIPNS_6stream3lzw4CodeEEE", !16, i64 0, !17, i64 8, !10, i64 16}
!16 = !{!"_ZTSN2vl11collections9ListStoreIPNS_6stream3lzw4CodeELb1EEE", !8, i64 0}
!17 = !{!"any pointer", !11, i64 0}
!18 = !{!"bool", !11, i64 0}
!19 = !{!"_ZTSN2vl11collections17PushOnlyAllocatorINS0_10bom_helper8TreeNodeEEE", !8, i64 0, !10, i64 8, !10, i64 16, !20, i64 24}
!20 = !{!"_ZTSN2vl11collections4ListIPNS0_10bom_helper8TreeNodeES4_EE", !21, i64 0}
!21 = !{!"_ZTSN2vl11collections8ListBaseIPNS0_10bom_helper8TreeNodeES4_EE", !22, i64 0, !10, i64 24, !18, i64 32}
!22 = !{!"_ZTSN2vl11collections9ArrayBaseIPNS0_10bom_helper8TreeNodeEEE", !23, i64 0, !17, i64 8, !10, i64 16}
!23 = !{!"_ZTSN2vl11collections9ListStoreIPNS0_10bom_helper8TreeNodeELb1EEE", !8, i64 0}
!24 = !{!"_ZTSN2vl6stream8IEncoderE", !25, i64 0}
!25 = !{!"_ZTSN2vl9InterfaceE"}
!26 = !{!11, !11, i64 0}
!27 = distinct !{!27, !28}
!28 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass