; LLVM IR from pixelsdb/mini-pixels :: pixels-core/lib/encoding (RunLenIntEncoder)

; 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.RunLenIntEncoder = type { i32, i32, i32, i32, i8, i8, i64, i64, i32, i8, i32, i32, i32, ptr, i32, i32, i32, i32, i32, i64, ptr, ptr, ptr, ptr, %class.EncodingUtils, %"class.std::shared_ptr" }
%class.EncodingUtils = type { ptr, ptr }
%"class.std::shared_ptr" = type { %"class.std::__shared_ptr" }
%"class.std::__shared_ptr" = type { ptr, %"class.std::__shared_count" }
%"class.std::__shared_count" = type { ptr }

@_ZN9Constants10MIN_REPEATE = external global i32, align 4

; 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 nounwind uwtable
declare dso_local noundef ptr @_ZNKSt19__shared_ptr_accessI10ByteBufferLN9__gnu_cxx12_Lock_policyE2ELb0ELb0EEptEv(ptr noundef nonnull align 1 dereferenceable(1)) #1 align 2

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_ZN16RunLenIntEncoder18findClosestNumBitsEl(ptr noundef nonnull align 8 dereferenceable(168), i64 noundef) #2 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN16RunLenIntEncoder22writeShortRepeatValuesEv(ptr noundef nonnull align 8 dereferenceable(168) %0) #2 align 2 {
  %2 = alloca ptr, align 8
  %3 = alloca i64, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i8, align 1
  store ptr %0, ptr %2, align 8, !tbaa !5
  %9 = load ptr, ptr %2, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  %10 = getelementptr inbounds %class.RunLenIntEncoder, ptr %9, i32 0, i32 4
  %11 = load i8, ptr %10, align 8, !tbaa !9, !range !19, !noundef !20
  %12 = trunc i8 %11 to i1
  br i1 %12, label %13, label %19

13:                                               ; preds = %1
  %14 = getelementptr inbounds %class.RunLenIntEncoder, ptr %9, i32 0, i32 20
  %15 = load ptr, ptr %14, align 8, !tbaa !21
  %16 = getelementptr inbounds i64, ptr %15, i64 0
  %17 = load i64, ptr %16, align 8, !tbaa !22
  %18 = call noundef i64 @_ZN16RunLenIntEncoder12zigzagEncodeEl(ptr noundef nonnull align 8 dereferenceable(168) %9, i64 noundef %17)
  store i64 %18, ptr %3, align 8, !tbaa !22
  br label %24

19:                                               ; preds = %1
  %20 = getelementptr inbounds %class.RunLenIntEncoder, ptr %9, i32 0, i32 20
  %21 = load ptr, ptr %20, align 8, !tbaa !21
  %22 = getelementptr inbounds i64, ptr %21, i64 0
  %23 = load i64, ptr %22, align 8, !tbaa !22
  store i64 %23, ptr %3, align 8, !tbaa !22
  br label %24

24:                                               ; preds = %19, %13
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  %25 = load i64, ptr %3, align 8, !tbaa !22
  %26 = call noundef i32 @_ZN16RunLenIntEncoder18findClosestNumBitsEl(ptr noundef nonnull align 8 dereferenceable(168) %9, i64 noundef %25)
  store i32 %26, ptr %4, align 4, !tbaa !23
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  %27 = load i32, ptr %4, align 4, !tbaa !23
  %28 = srem i32 %27, 8
  %29 = icmp eq i32 %28, 0
  br i1 %29, label %30, label %33

30:                                               ; preds = %24
  %31 = load i32, ptr %4, align 4, !tbaa !23
  %32 = lshr i32 %31, 3
  br label %37

33:                                               ; preds = %24
  %34 = load i32, ptr %4, align 4, !tbaa !23
  %35 = lshr i32 %34, 3
  %36 = add i32 %35, 1
  br label %37

37:                                               ; preds = %33, %30
  %38 = phi i32 [ %32, %30 ], [ %36, %33 ]
  store i32 %38, ptr %5, align 4, !tbaa !23
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  %39 = call noundef i32 @_ZN16RunLenIntEncoder9getOpcodeEv(ptr noundef nonnull align 8 dereferenceable(168) %9)
  store i32 %39, ptr %6, align 4, !tbaa !23
  %40 = load i32, ptr %5, align 4, !tbaa !23
  %41 = sub nsw i32 %40, 1
  %42 = shl i32 %41, 3
  %43 = load i32, ptr %6, align 4, !tbaa !23
  %44 = or i32 %43, %42
  store i32 %44, ptr %6, align 4, !tbaa !23
  %45 = load i32, ptr @_ZN9Constants10MIN_REPEATE, align 4, !tbaa !23
  %46 = getelementptr inbounds %class.RunLenIntEncoder, ptr %9, i32 0, i32 2
  %47 = load i32, ptr %46, align 8, !tbaa !24
  %48 = sub nsw i32 %47, %45
  store i32 %48, ptr %46, align 8, !tbaa !24
  %49 = getelementptr inbounds %class.RunLenIntEncoder, ptr %9, i32 0, i32 2
  %50 = load i32, ptr %49, align 8, !tbaa !24
  %51 = load i32, ptr %6, align 4, !tbaa !23
  %52 = or i32 %51, %50
  store i32 %52, ptr %6, align 4, !tbaa !23
  %53 = getelementptr inbounds %class.RunLenIntEncoder, ptr %9, i32 0, i32 25
  %54 = call noundef ptr @_ZNKSt19__shared_ptr_accessI10ByteBufferLN9__gnu_cxx12_Lock_policyE2ELb0ELb0EEptEv(ptr noundef nonnull align 1 dereferenceable(1) %53) #4
  %55 = load i32, ptr %6, align 4, !tbaa !23
  %56 = trunc i32 %55 to i8
  call void @_ZN10ByteBuffer3putEh(ptr noundef nonnull align 8 dereferenceable(62) %54, i8 noundef zeroext %56)
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  %57 = load i32, ptr %5, align 4, !tbaa !23
  %58 = sub nsw i32 %57, 1
  store i32 %58, ptr %7, align 4, !tbaa !23
  br label %59

59:                                               ; preds = %74, %37
  %60 = load i32, ptr %7, align 4, !tbaa !23
  %61 = icmp sge i32 %60, 0
  br i1 %61, label %63, label %62

62:                                               ; preds = %59
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  br label %77

63:                                               ; preds = %59
  call void @llvm.lifetime.start.p0(i64 1, ptr %8) #4
  %64 = load i64, ptr %3, align 8, !tbaa !22
  %65 = load i32, ptr %7, align 4, !tbaa !23
  %66 = mul nsw i32 %65, 8
  %67 = zext i32 %66 to i64
  %68 = lshr i64 %64, %67
  %69 = and i64 %68, 255
  %70 = trunc i64 %69 to i8
  store i8 %70, ptr %8, align 1, !tbaa !25
  %71 = getelementptr inbounds %class.RunLenIntEncoder, ptr %9, i32 0, i32 25
  %72 = call noundef ptr @_ZNKSt19__shared_ptr_accessI10ByteBufferLN9__gnu_cxx12_Lock_policyE2ELb0ELb0EEptEv(ptr noundef nonnull align 1 dereferenceable(1) %71) #4
  %73 = load i8, ptr %8, align 1, !tbaa !25
  call void @_ZN10ByteBuffer3putEh(ptr noundef nonnull align 8 dereferenceable(62) %72, i8 noundef zeroext %73)
  call void @llvm.lifetime.end.p0(i64 1, ptr %8) #4
  br label %74

74:                                               ; preds = %63
  %75 = load i32, ptr %7, align 4, !tbaa !23
  %76 = add nsw i32 %75, -1
  store i32 %76, ptr %7, align 4, !tbaa !23
  br label %59, !llvm.loop !26

77:                                               ; preds = %62
  %78 = getelementptr inbounds %class.RunLenIntEncoder, ptr %9, i32 0, i32 2
  store i32 0, ptr %78, align 8, !tbaa !24
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  ret void
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZN16RunLenIntEncoder12zigzagEncodeEl(ptr noundef nonnull align 8 dereferenceable(168), i64 noundef) #1 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZN16RunLenIntEncoder9getOpcodeEv(ptr noundef nonnull align 8 dereferenceable(168)) #1 align 2

declare void @_ZN10ByteBuffer3putEh(ptr noundef nonnull align 8 dereferenceable(62), i8 noundef zeroext) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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 = { 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 #3 = { "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 = { 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, !13, i64 16}
!10 = !{!"_ZTS16RunLenIntEncoder", !11, i64 0, !12, i64 4, !12, i64 8, !12, i64 12, !13, i64 16, !13, i64 17, !14, i64 24, !14, i64 32, !12, i64 40, !13, i64 44, !12, i64 48, !12, i64 52, !12, i64 56, !6, i64 64, !12, i64 72, !12, i64 76, !12, i64 80, !12, i64 84, !12, i64 88, !14, i64 96, !6, i64 104, !6, i64 112, !6, i64 120, !6, i64 128, !15, i64 136, !16, i64 152}
!11 = !{!"_ZTSN16RunLenIntEncoder12EncodingTypeE", !7, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!"bool", !7, i64 0}
!14 = !{!"long", !7, i64 0}
!15 = !{!"_ZTS13EncodingUtils", !6, i64 0, !6, i64 8}
!16 = !{!"_ZTSSt10shared_ptrI10ByteBufferE", !17, i64 0}
!17 = !{!"_ZTSSt12__shared_ptrI10ByteBufferLN9__gnu_cxx12_Lock_policyE2EE", !6, i64 0, !18, i64 8}
!18 = !{!"_ZTSSt14__shared_countILN9__gnu_cxx12_Lock_policyE2EE", !6, i64 0}
!19 = !{i8 0, i8 2}
!20 = !{}
!21 = !{!10, !6, i64 104}
!22 = !{!14, !14, i64 0}
!23 = !{!12, !12, i64 0}
!24 = !{!10, !12, i64 8}
!25 = !{!7, !7, i64 0}
!26 = distinct !{!26, !27}
!27 = !{!"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"

@_ZN9Constants10MIN_REPEATE = external local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef ptr @_ZNKSt19__shared_ptr_accessI10ByteBufferLN9__gnu_cxx12_Lock_policyE2ELb0ELb0EEptEv(ptr noundef nonnull align 1 dereferenceable(1)) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_ZN16RunLenIntEncoder18findClosestNumBitsEl(ptr noundef nonnull align 8 dereferenceable(168), i64 noundef) local_unnamed_addr #1 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN16RunLenIntEncoder22writeShortRepeatValuesEv(ptr noundef nonnull align 8 dereferenceable(168) %0) local_unnamed_addr #1 align 2 {
  %2 = getelementptr inbounds i8, ptr %0, i64 16
  %3 = load i8, ptr %2, align 8, !tbaa !5, !range !18, !noundef !19
  %4 = trunc nuw i8 %3 to i1
  %5 = getelementptr inbounds i8, ptr %0, i64 104
  %6 = load ptr, ptr %5, align 8, !tbaa !20
  %7 = load i64, ptr %6, align 8, !tbaa !21
  br i1 %4, label %8, label %10

8:                                                ; preds = %1
  %9 = tail call noundef i64 @_ZN16RunLenIntEncoder12zigzagEncodeEl(ptr noundef nonnull align 8 dereferenceable(168) %0, i64 noundef %7)
  br label %10

10:                                               ; preds = %1, %8
  %.0 = phi i64 [ %9, %8 ], [ %7, %1 ]
  %11 = tail call noundef i32 @_ZN16RunLenIntEncoder18findClosestNumBitsEl(ptr noundef nonnull align 8 dereferenceable(168) %0, i64 noundef %.0)
  %12 = and i32 %11, 7
  %13 = icmp eq i32 %12, 0
  %14 = lshr i32 %11, 3
  %15 = sext i1 %13 to i32
  %16 = add nsw i32 %14, %15
  %17 = tail call noundef i32 @_ZN16RunLenIntEncoder9getOpcodeEv(ptr noundef nonnull align 8 dereferenceable(168) %0)
  %18 = shl i32 %16, 3
  %19 = load i32, ptr @_ZN9Constants10MIN_REPEATE, align 4, !tbaa !22
  %20 = getelementptr inbounds i8, ptr %0, i64 8
  %21 = load i32, ptr %20, align 8, !tbaa !23
  %22 = sub nsw i32 %21, %19
  store i32 %22, ptr %20, align 8, !tbaa !23
  %23 = or i32 %17, %18
  %24 = or i32 %23, %22
  %25 = getelementptr inbounds i8, ptr %0, i64 152
  %26 = tail call noundef ptr @_ZNKSt19__shared_ptr_accessI10ByteBufferLN9__gnu_cxx12_Lock_policyE2ELb0ELb0EEptEv(ptr noundef nonnull align 1 dereferenceable(1) %25) #3
  %27 = trunc i32 %24 to i8
  tail call void @_ZN10ByteBuffer3putEh(ptr noundef nonnull align 8 dereferenceable(62) %26, i8 noundef zeroext %27)
  %28 = icmp sgt i32 %16, -1
  br i1 %28, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %10
  %29 = zext nneg i32 %16 to i64
  br label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %10
  store i32 0, ptr %20, align 8, !tbaa !23
  ret void

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %indvars.iv = phi i64 [ %29, %.lr.ph.preheader ], [ %indvars.iv.next, %.lr.ph ]
  %30 = shl nuw nsw i64 %indvars.iv, 3
  %31 = lshr i64 %.0, %30
  %32 = trunc i64 %31 to i8
  %33 = tail call noundef ptr @_ZNKSt19__shared_ptr_accessI10ByteBufferLN9__gnu_cxx12_Lock_policyE2ELb0ELb0EEptEv(ptr noundef nonnull align 1 dereferenceable(1) %25) #3
  tail call void @_ZN10ByteBuffer3putEh(ptr noundef nonnull align 8 dereferenceable(62) %33, i8 noundef zeroext %32)
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %34 = trunc nuw i64 %indvars.iv to i32
  %35 = icmp sgt i32 %34, 0
  br i1 %35, label %.lr.ph, label %._crit_edge, !llvm.loop !24
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZN16RunLenIntEncoder12zigzagEncodeEl(ptr noundef nonnull align 8 dereferenceable(168), i64 noundef) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZN16RunLenIntEncoder9getOpcodeEv(ptr noundef nonnull align 8 dereferenceable(168)) local_unnamed_addr #0 align 2

declare void @_ZN10ByteBuffer3putEh(ptr noundef nonnull align 8 dereferenceable(62), i8 noundef zeroext) local_unnamed_addr #2

attributes #0 = { mustprogress 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 = { 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 = { "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, !11, i64 16}
!6 = !{!"_ZTS16RunLenIntEncoder", !7, i64 0, !10, i64 4, !10, i64 8, !10, i64 12, !11, i64 16, !11, i64 17, !12, i64 24, !12, i64 32, !10, i64 40, !11, i64 44, !10, i64 48, !10, i64 52, !10, i64 56, !13, i64 64, !10, i64 72, !10, i64 76, !10, i64 80, !10, i64 84, !10, i64 88, !12, i64 96, !13, i64 104, !13, i64 112, !13, i64 120, !13, i64 128, !14, i64 136, !15, i64 152}
!7 = !{!"_ZTSN16RunLenIntEncoder12EncodingTypeE", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"bool", !8, i64 0}
!12 = !{!"long", !8, i64 0}
!13 = !{!"any pointer", !8, i64 0}
!14 = !{!"_ZTS13EncodingUtils", !13, i64 0, !13, i64 8}
!15 = !{!"_ZTSSt10shared_ptrI10ByteBufferE", !16, i64 0}
!16 = !{!"_ZTSSt12__shared_ptrI10ByteBufferLN9__gnu_cxx12_Lock_policyE2EE", !13, i64 0, !17, i64 8}
!17 = !{!"_ZTSSt14__shared_countILN9__gnu_cxx12_Lock_policyE2EE", !13, i64 0}
!18 = !{i8 0, i8 2}
!19 = !{}
!20 = !{!6, !13, i64 104}
!21 = !{!12, !12, i64 0}
!22 = !{!10, !10, i64 0}
!23 = !{!6, !10, i64 8}
!24 = distinct !{!24, !25}
!25 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass