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

; 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
define dso_local void @_ZN16RunLenIntEncoder21computeZigZagLiteralsEv(ptr noundef nonnull align 8 dereferenceable(168) %0) #1 align 2 {
  %2 = alloca ptr, align 8
  %3 = alloca i64, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %6 = load ptr, ptr %2, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  store i64 0, ptr %3, align 8, !tbaa !9
  %7 = getelementptr inbounds %class.RunLenIntEncoder, ptr %6, i32 0, i32 4
  %8 = load i8, ptr %7, align 8, !tbaa !11, !range !20, !noundef !21
  %9 = trunc i8 %8 to i1
  br i1 %9, label %10, label %35

10:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  store i32 0, ptr %4, align 4, !tbaa !22
  br label %11

11:                                               ; preds = %31, %10
  %12 = load i32, ptr %4, align 4, !tbaa !22
  %13 = getelementptr inbounds %class.RunLenIntEncoder, ptr %6, i32 0, i32 1
  %14 = load i32, ptr %13, align 4, !tbaa !23
  %15 = icmp slt i32 %12, %14
  br i1 %15, label %17, label %16

16:                                               ; preds = %11
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  br label %34

17:                                               ; preds = %11
  %18 = getelementptr inbounds %class.RunLenIntEncoder, ptr %6, i32 0, i32 20
  %19 = load ptr, ptr %18, align 8, !tbaa !24
  %20 = load i32, ptr %4, align 4, !tbaa !22
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds i64, ptr %19, i64 %21
  %23 = load i64, ptr %22, align 8, !tbaa !9
  %24 = call noundef i64 @_ZN16RunLenIntEncoder12zigzagEncodeEl(ptr noundef nonnull align 8 dereferenceable(168) %6, i64 noundef %23)
  store i64 %24, ptr %3, align 8, !tbaa !9
  %25 = load i64, ptr %3, align 8, !tbaa !9
  %26 = getelementptr inbounds %class.RunLenIntEncoder, ptr %6, i32 0, i32 21
  %27 = load ptr, ptr %26, align 8, !tbaa !25
  %28 = load i32, ptr %4, align 4, !tbaa !22
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds i64, ptr %27, i64 %29
  store i64 %25, ptr %30, align 8, !tbaa !9
  br label %31

31:                                               ; preds = %17
  %32 = load i32, ptr %4, align 4, !tbaa !22
  %33 = add nsw i32 %32, 1
  store i32 %33, ptr %4, align 4, !tbaa !22
  br label %11, !llvm.loop !26

34:                                               ; preds = %16
  br label %59

35:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  store i32 0, ptr %5, align 4, !tbaa !22
  br label %36

36:                                               ; preds = %55, %35
  %37 = load i32, ptr %5, align 4, !tbaa !22
  %38 = getelementptr inbounds %class.RunLenIntEncoder, ptr %6, i32 0, i32 1
  %39 = load i32, ptr %38, align 4, !tbaa !23
  %40 = icmp slt i32 %37, %39
  br i1 %40, label %42, label %41

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

42:                                               ; preds = %36
  %43 = getelementptr inbounds %class.RunLenIntEncoder, ptr %6, i32 0, i32 20
  %44 = load ptr, ptr %43, align 8, !tbaa !24
  %45 = load i32, ptr %5, align 4, !tbaa !22
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds i64, ptr %44, i64 %46
  %48 = load i64, ptr %47, align 8, !tbaa !9
  store i64 %48, ptr %3, align 8, !tbaa !9
  %49 = load i64, ptr %3, align 8, !tbaa !9
  %50 = getelementptr inbounds %class.RunLenIntEncoder, ptr %6, i32 0, i32 21
  %51 = load ptr, ptr %50, align 8, !tbaa !25
  %52 = load i32, ptr %5, align 4, !tbaa !22
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds i64, ptr %51, i64 %53
  store i64 %49, ptr %54, align 8, !tbaa !9
  br label %55

55:                                               ; preds = %42
  %56 = load i32, ptr %5, align 4, !tbaa !22
  %57 = add nsw i32 %56, 1
  store i32 %57, ptr %5, align 4, !tbaa !22
  br label %36, !llvm.loop !28

58:                                               ; preds = %41
  br label %59

59:                                               ; preds = %58, %34
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  ret void
}

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

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


; 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: mustprogress nounwind uwtable
define dso_local void @_ZN16RunLenIntEncoder21computeZigZagLiteralsEv(ptr noundef nonnull align 8 dereferenceable(168) %0) local_unnamed_addr #0 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 4
  %6 = load i32, ptr %5, align 4, !tbaa !20
  %7 = icmp sgt i32 %6, 0
  br i1 %4, label %.preheader, label %.preheader11

.preheader11:                                     ; preds = %1
  br i1 %7, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %.preheader11
  %8 = getelementptr inbounds i8, ptr %0, i64 104
  %9 = load ptr, ptr %8, align 8, !tbaa !21
  %10 = getelementptr inbounds i8, ptr %0, i64 112
  %11 = load ptr, ptr %10, align 8, !tbaa !22
  %wide.trip.count = zext nneg i32 %6 to i64
  %min.iters.check = icmp ult i32 %6, 6
  %12 = ptrtoint ptr %11 to i64
  %13 = ptrtoint ptr %9 to i64
  %14 = sub i64 %12, %13
  %diff.check = icmp ult i64 %14, 32
  %or.cond = select i1 %min.iters.check, i1 true, i1 %diff.check
  br i1 %or.cond, label %scalar.ph.preheader, label %vector.ph

scalar.ph.preheader:                              ; preds = %middle.block, %.lr.ph
  %indvars.iv.ph = phi i64 [ 0, %.lr.ph ], [ %n.vec, %middle.block ]
  %xtraiter = and i64 %wide.trip.count, 3
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %scalar.ph.prol.loopexit, label %scalar.ph.prol

scalar.ph.prol:                                   ; preds = %scalar.ph.preheader, %scalar.ph.prol
  %indvars.iv.prol = phi i64 [ %indvars.iv.next.prol, %scalar.ph.prol ], [ %indvars.iv.ph, %scalar.ph.preheader ]
  %prol.iter = phi i64 [ %prol.iter.next, %scalar.ph.prol ], [ 0, %scalar.ph.preheader ]
  %15 = getelementptr inbounds i64, ptr %9, i64 %indvars.iv.prol
  %16 = load i64, ptr %15, align 8, !tbaa !23
  %17 = getelementptr inbounds i64, ptr %11, i64 %indvars.iv.prol
  store i64 %16, ptr %17, align 8, !tbaa !23
  %indvars.iv.next.prol = add nuw nsw i64 %indvars.iv.prol, 1
  %prol.iter.next = add i64 %prol.iter, 1
  %prol.iter.cmp.not = icmp eq i64 %prol.iter.next, %xtraiter
  br i1 %prol.iter.cmp.not, label %scalar.ph.prol.loopexit, label %scalar.ph.prol, !llvm.loop !24

scalar.ph.prol.loopexit:                          ; preds = %scalar.ph.prol, %scalar.ph.preheader
  %indvars.iv.unr = phi i64 [ %indvars.iv.ph, %scalar.ph.preheader ], [ %indvars.iv.next.prol, %scalar.ph.prol ]
  %18 = sub nsw i64 %indvars.iv.ph, %wide.trip.count
  %19 = icmp ugt i64 %18, -4
  br i1 %19, label %.loopexit, label %scalar.ph

vector.ph:                                        ; preds = %.lr.ph
  %n.vec = and i64 %wide.trip.count, 2147483644
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %20 = getelementptr inbounds i64, ptr %9, i64 %index
  %21 = getelementptr inbounds i8, ptr %20, i64 16
  %wide.load = load <2 x i64>, ptr %20, align 8, !tbaa !23
  %wide.load23 = load <2 x i64>, ptr %21, align 8, !tbaa !23
  %22 = getelementptr inbounds i64, ptr %11, i64 %index
  %23 = getelementptr inbounds i8, ptr %22, i64 16
  store <2 x i64> %wide.load, ptr %22, align 8, !tbaa !23
  store <2 x i64> %wide.load23, ptr %23, align 8, !tbaa !23
  %index.next = add nuw i64 %index, 4
  %24 = icmp eq i64 %index.next, %n.vec
  br i1 %24, label %middle.block, label %vector.body, !llvm.loop !26

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %n.vec, %wide.trip.count
  br i1 %cmp.n, label %.loopexit, label %scalar.ph.preheader

.preheader:                                       ; preds = %1
  br i1 %7, label %.lr.ph15, label %.loopexit

.lr.ph15:                                         ; preds = %.preheader
  %25 = getelementptr inbounds i8, ptr %0, i64 104
  %26 = getelementptr inbounds i8, ptr %0, i64 112
  br label %27

27:                                               ; preds = %.lr.ph15, %27
  %indvars.iv18 = phi i64 [ 0, %.lr.ph15 ], [ %indvars.iv.next19, %27 ]
  %28 = load ptr, ptr %25, align 8, !tbaa !21
  %29 = getelementptr inbounds i64, ptr %28, i64 %indvars.iv18
  %30 = load i64, ptr %29, align 8, !tbaa !23
  %31 = tail call noundef i64 @_ZN16RunLenIntEncoder12zigzagEncodeEl(ptr noundef nonnull align 8 dereferenceable(168) %0, i64 noundef %30)
  %32 = load ptr, ptr %26, align 8, !tbaa !22
  %33 = getelementptr inbounds i64, ptr %32, i64 %indvars.iv18
  store i64 %31, ptr %33, align 8, !tbaa !23
  %indvars.iv.next19 = add nuw nsw i64 %indvars.iv18, 1
  %34 = load i32, ptr %5, align 4, !tbaa !20
  %35 = sext i32 %34 to i64
  %36 = icmp slt i64 %indvars.iv.next19, %35
  br i1 %36, label %27, label %.loopexit, !llvm.loop !30

scalar.ph:                                        ; preds = %scalar.ph.prol.loopexit, %scalar.ph
  %indvars.iv = phi i64 [ %indvars.iv.next.3, %scalar.ph ], [ %indvars.iv.unr, %scalar.ph.prol.loopexit ]
  %37 = getelementptr inbounds i64, ptr %9, i64 %indvars.iv
  %38 = load i64, ptr %37, align 8, !tbaa !23
  %39 = getelementptr inbounds i64, ptr %11, i64 %indvars.iv
  store i64 %38, ptr %39, align 8, !tbaa !23
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %40 = getelementptr inbounds i64, ptr %9, i64 %indvars.iv.next
  %41 = load i64, ptr %40, align 8, !tbaa !23
  %42 = getelementptr inbounds i64, ptr %11, i64 %indvars.iv.next
  store i64 %41, ptr %42, align 8, !tbaa !23
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %43 = getelementptr inbounds i64, ptr %9, i64 %indvars.iv.next.1
  %44 = load i64, ptr %43, align 8, !tbaa !23
  %45 = getelementptr inbounds i64, ptr %11, i64 %indvars.iv.next.1
  store i64 %44, ptr %45, align 8, !tbaa !23
  %indvars.iv.next.2 = add nuw nsw i64 %indvars.iv, 3
  %46 = getelementptr inbounds i64, ptr %9, i64 %indvars.iv.next.2
  %47 = load i64, ptr %46, align 8, !tbaa !23
  %48 = getelementptr inbounds i64, ptr %11, i64 %indvars.iv.next.2
  store i64 %47, ptr %48, align 8, !tbaa !23
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 4
  %exitcond.not.3 = icmp eq i64 %indvars.iv.next.3, %wide.trip.count
  br i1 %exitcond.not.3, label %.loopexit, label %scalar.ph, !llvm.loop !31

.loopexit:                                        ; preds = %scalar.ph.prol.loopexit, %scalar.ph, %27, %middle.block, %.preheader11, %.preheader
  ret void
}

; 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

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

!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, !10, i64 4}
!21 = !{!6, !13, i64 104}
!22 = !{!6, !13, i64 112}
!23 = !{!12, !12, i64 0}
!24 = distinct !{!24, !25}
!25 = !{!"llvm.loop.unroll.disable"}
!26 = distinct !{!26, !27, !28, !29}
!27 = !{!"llvm.loop.mustprogress"}
!28 = !{!"llvm.loop.isvectorized", i32 1}
!29 = !{!"llvm.loop.unroll.runtime.disable"}
!30 = distinct !{!30, !27}
!31 = distinct !{!31, !27, !28}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass