; LLVM IR from digital-salon/Digital-Salon :: extern/include/contrib/Open3DGC (o3dgcArithmeticCodec)

; 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.o3dgc::Arithmetic_Codec" = type <{ ptr, ptr, ptr, i32, i32, i32, i32, i32, [4 x i8] }>

@.str.6 = external hidden unnamed_addr constant [21 x i8], align 1
@.str.7 = external hidden unnamed_addr constant [24 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: inlinehint mustprogress nounwind uwtable
declare dso_local void @_ZN5o3dgc16Arithmetic_Codec15propagate_carryEv(ptr noundef nonnull align 8 dereferenceable(44)) #1 align 2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local void @_ZN5o3dgc16Arithmetic_Codec19renorm_enc_intervalEv(ptr noundef nonnull align 8 dereferenceable(44)) #1 align 2

; Function Attrs: mustprogress noreturn uwtable
declare hidden void @_ZN5o3dgcL8AC_ErrorEPKc(ptr noundef) #2

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

9:                                                ; preds = %1
  call void @_ZN5o3dgcL8AC_ErrorEPKc(ptr noundef @.str.7) #4
  unreachable

10:                                               ; preds = %1
  %11 = getelementptr inbounds %"class.o3dgc::Arithmetic_Codec", ptr %5, i32 0, i32 7
  store i32 0, ptr %11, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #5
  %12 = getelementptr inbounds %"class.o3dgc::Arithmetic_Codec", ptr %5, i32 0, i32 3
  %13 = load i32, ptr %12, align 8, !tbaa !12
  store i32 %13, ptr %3, align 4, !tbaa !13
  %14 = getelementptr inbounds %"class.o3dgc::Arithmetic_Codec", ptr %5, i32 0, i32 5
  %15 = load i32, ptr %14, align 8, !tbaa !14
  %16 = icmp ugt i32 %15, 33554432
  br i1 %16, label %17, label %22

17:                                               ; preds = %10
  %18 = getelementptr inbounds %"class.o3dgc::Arithmetic_Codec", ptr %5, i32 0, i32 3
  %19 = load i32, ptr %18, align 8, !tbaa !12
  %20 = add i32 %19, 16777216
  store i32 %20, ptr %18, align 8, !tbaa !12
  %21 = getelementptr inbounds %"class.o3dgc::Arithmetic_Codec", ptr %5, i32 0, i32 5
  store i32 8388608, ptr %21, align 8, !tbaa !14
  br label %27

22:                                               ; preds = %10
  %23 = getelementptr inbounds %"class.o3dgc::Arithmetic_Codec", ptr %5, i32 0, i32 3
  %24 = load i32, ptr %23, align 8, !tbaa !12
  %25 = add i32 %24, 8388608
  store i32 %25, ptr %23, align 8, !tbaa !12
  %26 = getelementptr inbounds %"class.o3dgc::Arithmetic_Codec", ptr %5, i32 0, i32 5
  store i32 32768, ptr %26, align 8, !tbaa !14
  br label %27

27:                                               ; preds = %22, %17
  %28 = load i32, ptr %3, align 4, !tbaa !13
  %29 = getelementptr inbounds %"class.o3dgc::Arithmetic_Codec", ptr %5, i32 0, i32 3
  %30 = load i32, ptr %29, align 8, !tbaa !12
  %31 = icmp ugt i32 %28, %30
  br i1 %31, label %32, label %33

32:                                               ; preds = %27
  call void @_ZN5o3dgc16Arithmetic_Codec15propagate_carryEv(ptr noundef nonnull align 8 dereferenceable(44) %5)
  br label %33

33:                                               ; preds = %32, %27
  call void @_ZN5o3dgc16Arithmetic_Codec19renorm_enc_intervalEv(ptr noundef nonnull align 8 dereferenceable(44) %5)
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #5
  %34 = getelementptr inbounds %"class.o3dgc::Arithmetic_Codec", ptr %5, i32 0, i32 2
  %35 = load ptr, ptr %34, align 8, !tbaa !15
  %36 = getelementptr inbounds %"class.o3dgc::Arithmetic_Codec", ptr %5, i32 0, i32 0
  %37 = load ptr, ptr %36, align 8, !tbaa !16
  %38 = ptrtoint ptr %35 to i64
  %39 = ptrtoint ptr %37 to i64
  %40 = sub i64 %38, %39
  %41 = trunc i64 %40 to i32
  store i32 %41, ptr %4, align 4, !tbaa !13
  %42 = load i32, ptr %4, align 4, !tbaa !13
  %43 = getelementptr inbounds %"class.o3dgc::Arithmetic_Codec", ptr %5, i32 0, i32 6
  %44 = load i32, ptr %43, align 4, !tbaa !17
  %45 = icmp ugt i32 %42, %44
  br i1 %45, label %46, label %47

46:                                               ; preds = %33
  call void @_ZN5o3dgcL8AC_ErrorEPKc(ptr noundef @.str.6) #4
  unreachable

47:                                               ; preds = %33
  %48 = load i32, ptr %4, align 4, !tbaa !13
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #5
  ret i32 %48
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { inlinehint 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 noreturn 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 = { 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 #4 = { noreturn }
attributes #5 = { 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, !11, i64 40}
!10 = !{!"_ZTSN5o3dgc16Arithmetic_CodecE", !6, i64 0, !6, i64 8, !6, i64 16, !11, i64 24, !11, i64 28, !11, i64 32, !11, i64 36, !11, i64 40}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !11, i64 24}
!13 = !{!11, !11, i64 0}
!14 = !{!10, !11, i64 32}
!15 = !{!10, !6, i64 16}
!16 = !{!10, !6, i64 0}
!17 = !{!10, !11, i64 36}


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

@.str.6 = external hidden unnamed_addr constant [21 x i8], align 1
@.str.7 = external hidden unnamed_addr constant [24 x i8], align 1

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local void @_ZN5o3dgc16Arithmetic_Codec15propagate_carryEv(ptr noundef nonnull align 8 dereferenceable(44)) local_unnamed_addr #0 align 2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local void @_ZN5o3dgc16Arithmetic_Codec19renorm_enc_intervalEv(ptr noundef nonnull align 8 dereferenceable(44)) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress noreturn uwtable
declare hidden void @_ZN5o3dgcL8AC_ErrorEPKc(ptr noundef) local_unnamed_addr #1

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

4:                                                ; preds = %1
  tail call void @_ZN5o3dgcL8AC_ErrorEPKc(ptr noundef nonnull @.str.7) #3
  unreachable

5:                                                ; preds = %1
  store i32 0, ptr %2, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %0, i64 24
  %7 = load i32, ptr %6, align 8, !tbaa !11
  %8 = getelementptr inbounds i8, ptr %0, i64 32
  %9 = load i32, ptr %8, align 8, !tbaa !12
  %10 = icmp ugt i32 %9, 33554432
  %storemerge3.v = select i1 %10, i32 16777216, i32 8388608
  %storemerge3 = add i32 %storemerge3.v, %7
  %storemerge = select i1 %10, i32 8388608, i32 32768
  store i32 %storemerge3, ptr %6, align 8, !tbaa !11
  store i32 %storemerge, ptr %8, align 8, !tbaa !12
  %11 = icmp ugt i32 %7, %storemerge3
  br i1 %11, label %12, label %13

12:                                               ; preds = %5
  tail call void @_ZN5o3dgc16Arithmetic_Codec15propagate_carryEv(ptr noundef nonnull align 8 dereferenceable(44) %0)
  br label %13

13:                                               ; preds = %12, %5
  tail call void @_ZN5o3dgc16Arithmetic_Codec19renorm_enc_intervalEv(ptr noundef nonnull align 8 dereferenceable(44) %0)
  %14 = getelementptr inbounds i8, ptr %0, i64 16
  %15 = load ptr, ptr %14, align 8, !tbaa !13
  %16 = load ptr, ptr %0, align 8, !tbaa !14
  %17 = ptrtoint ptr %15 to i64
  %18 = ptrtoint ptr %16 to i64
  %19 = sub i64 %17, %18
  %20 = trunc i64 %19 to i32
  %21 = getelementptr inbounds i8, ptr %0, i64 36
  %22 = load i32, ptr %21, align 4, !tbaa !15
  %23 = icmp ult i32 %22, %20
  br i1 %23, label %24, label %25

24:                                               ; preds = %13
  tail call void @_ZN5o3dgcL8AC_ErrorEPKc(ptr noundef nonnull @.str.6) #3
  unreachable

25:                                               ; preds = %13
  ret i32 %20
}

attributes #0 = { inlinehint 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 noreturn 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 = { noreturn }

!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 40}
!6 = !{!"_ZTSN5o3dgc16Arithmetic_CodecE", !7, i64 0, !7, i64 8, !7, i64 16, !10, i64 24, !10, i64 28, !10, i64 32, !10, i64 36, !10, i64 40}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!6, !10, i64 24}
!12 = !{!6, !10, i64 32}
!13 = !{!6, !7, i64 16}
!14 = !{!6, !7, i64 0}
!15 = !{!6, !10, i64 36}


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