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

; 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
@_ZN9Constants23MAX_SHORT_REPEAT_LENGTHE = external global i32, align 4

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN16RunLenIntEncoder5flushEv(ptr noundef nonnull align 8 dereferenceable(168) %0) #0 align 2 {
  %2 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  %3 = load ptr, ptr %2, align 8
  %4 = getelementptr inbounds %class.RunLenIntEncoder, ptr %3, i32 0, i32 1
  %5 = load i32, ptr %4, align 4, !tbaa !9
  %6 = icmp ne i32 %5, 0
  br i1 %6, label %7, label %45

7:                                                ; preds = %1
  %8 = getelementptr inbounds %class.RunLenIntEncoder, ptr %3, i32 0, i32 3
  %9 = load i32, ptr %8, align 4, !tbaa !19
  %10 = icmp ne i32 %9, 0
  br i1 %10, label %11, label %12

11:                                               ; preds = %7
  call void @_ZN16RunLenIntEncoder17determineEncodingEv(ptr noundef nonnull align 8 dereferenceable(168) %3)
  call void @_ZN16RunLenIntEncoder11writeValuesEv(ptr noundef nonnull align 8 dereferenceable(168) %3)
  br label %44

12:                                               ; preds = %7
  %13 = getelementptr inbounds %class.RunLenIntEncoder, ptr %3, i32 0, i32 2
  %14 = load i32, ptr %13, align 8, !tbaa !20
  %15 = icmp ne i32 %14, 0
  br i1 %15, label %16, label %43

16:                                               ; preds = %12
  %17 = getelementptr inbounds %class.RunLenIntEncoder, ptr %3, i32 0, i32 2
  %18 = load i32, ptr %17, align 8, !tbaa !20
  %19 = load i32, ptr @_ZN9Constants10MIN_REPEATE, align 4, !tbaa !21
  %20 = icmp slt i32 %18, %19
  br i1 %20, label %21, label %26

21:                                               ; preds = %16
  %22 = getelementptr inbounds %class.RunLenIntEncoder, ptr %3, i32 0, i32 2
  %23 = load i32, ptr %22, align 8, !tbaa !20
  %24 = getelementptr inbounds %class.RunLenIntEncoder, ptr %3, i32 0, i32 3
  store i32 %23, ptr %24, align 4, !tbaa !19
  %25 = getelementptr inbounds %class.RunLenIntEncoder, ptr %3, i32 0, i32 2
  store i32 0, ptr %25, align 8, !tbaa !20
  call void @_ZN16RunLenIntEncoder17determineEncodingEv(ptr noundef nonnull align 8 dereferenceable(168) %3)
  call void @_ZN16RunLenIntEncoder11writeValuesEv(ptr noundef nonnull align 8 dereferenceable(168) %3)
  br label %42

26:                                               ; preds = %16
  %27 = getelementptr inbounds %class.RunLenIntEncoder, ptr %3, i32 0, i32 2
  %28 = load i32, ptr %27, align 8, !tbaa !20
  %29 = load i32, ptr @_ZN9Constants10MIN_REPEATE, align 4, !tbaa !21
  %30 = icmp sge i32 %28, %29
  br i1 %30, label %31, label %38

31:                                               ; preds = %26
  %32 = getelementptr inbounds %class.RunLenIntEncoder, ptr %3, i32 0, i32 2
  %33 = load i32, ptr %32, align 8, !tbaa !20
  %34 = load i32, ptr @_ZN9Constants23MAX_SHORT_REPEAT_LENGTHE, align 4, !tbaa !21
  %35 = icmp sle i32 %33, %34
  br i1 %35, label %36, label %38

36:                                               ; preds = %31
  %37 = getelementptr inbounds %class.RunLenIntEncoder, ptr %3, i32 0, i32 0
  store i32 0, ptr %37, align 8, !tbaa !22
  call void @_ZN16RunLenIntEncoder11writeValuesEv(ptr noundef nonnull align 8 dereferenceable(168) %3)
  br label %41

38:                                               ; preds = %31, %26
  %39 = getelementptr inbounds %class.RunLenIntEncoder, ptr %3, i32 0, i32 0
  store i32 3, ptr %39, align 8, !tbaa !22
  %40 = getelementptr inbounds %class.RunLenIntEncoder, ptr %3, i32 0, i32 9
  store i8 1, ptr %40, align 4, !tbaa !23
  call void @_ZN16RunLenIntEncoder11writeValuesEv(ptr noundef nonnull align 8 dereferenceable(168) %3)
  br label %41

41:                                               ; preds = %38, %36
  br label %42

42:                                               ; preds = %41, %21
  br label %43

43:                                               ; preds = %42, %12
  br label %44

44:                                               ; preds = %43, %11
  br label %45

45:                                               ; preds = %44, %1
  ret void
}

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

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

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

!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, !12, i64 4}
!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 = !{!10, !12, i64 12}
!20 = !{!10, !12, i64 8}
!21 = !{!12, !12, i64 0}
!22 = !{!10, !11, i64 0}
!23 = !{!10, !13, i64 44}


; 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
@_ZN9Constants23MAX_SHORT_REPEAT_LENGTHE = external local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN16RunLenIntEncoder5flushEv(ptr noundef nonnull align 8 dereferenceable(168) %0) local_unnamed_addr #0 align 2 {
  %2 = getelementptr inbounds i8, ptr %0, i64 4
  %3 = load i32, ptr %2, align 4, !tbaa !5
  %.not = icmp eq i32 %3, 0
  br i1 %.not, label %20, label %4

4:                                                ; preds = %1
  %5 = getelementptr inbounds i8, ptr %0, i64 12
  %6 = load i32, ptr %5, align 4, !tbaa !18
  %.not1 = icmp eq i32 %6, 0
  br i1 %.not1, label %8, label %7

7:                                                ; preds = %4
  tail call void @_ZN16RunLenIntEncoder17determineEncodingEv(ptr noundef nonnull align 8 dereferenceable(168) %0)
  br label %.sink.split

8:                                                ; preds = %4
  %9 = getelementptr inbounds i8, ptr %0, i64 8
  %10 = load i32, ptr %9, align 8, !tbaa !19
  %.not2 = icmp eq i32 %10, 0
  br i1 %.not2, label %20, label %11

11:                                               ; preds = %8
  %12 = load i32, ptr @_ZN9Constants10MIN_REPEATE, align 4, !tbaa !20
  %13 = icmp slt i32 %10, %12
  br i1 %13, label %14, label %15

14:                                               ; preds = %11
  store i32 %10, ptr %5, align 4, !tbaa !18
  store i32 0, ptr %9, align 8, !tbaa !19
  tail call void @_ZN16RunLenIntEncoder17determineEncodingEv(ptr noundef nonnull align 8 dereferenceable(168) %0)
  br label %.sink.split

15:                                               ; preds = %11
  %16 = load i32, ptr @_ZN9Constants23MAX_SHORT_REPEAT_LENGTHE, align 4, !tbaa !20
  %.not3 = icmp sgt i32 %10, %16
  br i1 %.not3, label %18, label %17

17:                                               ; preds = %15
  store i32 0, ptr %0, align 8, !tbaa !21
  br label %.sink.split

18:                                               ; preds = %15
  store i32 3, ptr %0, align 8, !tbaa !21
  %19 = getelementptr inbounds i8, ptr %0, i64 44
  store i8 1, ptr %19, align 4, !tbaa !22
  br label %.sink.split

.sink.split:                                      ; preds = %17, %18, %14, %7
  tail call void @_ZN16RunLenIntEncoder11writeValuesEv(ptr noundef nonnull align 8 dereferenceable(168) %0)
  br label %20

20:                                               ; preds = %.sink.split, %8, %1
  ret void
}

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

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

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

!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 4}
!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 = !{!6, !10, i64 12}
!19 = !{!6, !10, i64 8}
!20 = !{!10, !10, i64 0}
!21 = !{!6, !7, i64 0}
!22 = !{!6, !11, i64 44}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InstCombinePass,SROAPass,SimplifyCFGPass,TailCallElimPass