; LLVM IR from kdrag0n/fxdsp :: external/oboe/src/common/FixedBlockReader.ll (ZN16FixedBlockReader4readEPhi)

; 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.FixedBlockAdapter = type { ptr, ptr, %"class.std::unique_ptr", i32, i32 }
%"class.std::unique_ptr" = type { %"struct.std::__uniq_ptr_data" }
%"struct.std::__uniq_ptr_data" = type { %"class.std::__uniq_ptr_impl" }
%"class.std::__uniq_ptr_impl" = type { %"class.std::tuple" }
%"class.std::tuple" = type { %"struct.std::_Tuple_impl" }
%"struct.std::_Tuple_impl" = type { %"struct.std::_Head_base.1" }
%"struct.std::_Head_base.1" = type { ptr }
%class.FixedBlockReader = type <{ %class.FixedBlockAdapter, i32, [4 x i8] }>

; 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 i32 @_ZN16FixedBlockReader15readFromStorageEPhi(ptr noundef nonnull align 8 dereferenceable(36), ptr noundef, i32 noundef) #1 align 2

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN16FixedBlockReader4readEPhi(ptr noundef nonnull align 8 dereferenceable(36) %0, ptr noundef %1, i32 noundef %2) #2 align 2 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !9
  %11 = load ptr, ptr %5, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  %12 = load i32, ptr %7, align 4, !tbaa !9
  store i32 %12, ptr %9, align 4, !tbaa !9
  br label %13

13:                                               ; preds = %84, %3
  %14 = load i32, ptr %9, align 4, !tbaa !9
  %15 = icmp sgt i32 %14, 0
  br i1 %15, label %16, label %85

16:                                               ; preds = %13
  %17 = getelementptr inbounds %class.FixedBlockAdapter, ptr %11, i32 0, i32 4
  %18 = load i32, ptr %17, align 4, !tbaa !11
  %19 = getelementptr inbounds %class.FixedBlockReader, ptr %11, i32 0, i32 1
  %20 = load i32, ptr %19, align 8, !tbaa !19
  %21 = icmp slt i32 %18, %20
  br i1 %21, label %22, label %33

22:                                               ; preds = %16
  %23 = load ptr, ptr %6, align 8, !tbaa !5
  %24 = load i32, ptr %9, align 4, !tbaa !9
  %25 = call noundef i32 @_ZN16FixedBlockReader15readFromStorageEPhi(ptr noundef nonnull align 8 dereferenceable(36) %11, ptr noundef %23, i32 noundef %24)
  store i32 %25, ptr %8, align 4, !tbaa !9
  %26 = load i32, ptr %8, align 4, !tbaa !9
  %27 = load ptr, ptr %6, align 8, !tbaa !5
  %28 = sext i32 %26 to i64
  %29 = getelementptr inbounds i8, ptr %27, i64 %28
  store ptr %29, ptr %6, align 8, !tbaa !5
  %30 = load i32, ptr %8, align 4, !tbaa !9
  %31 = load i32, ptr %9, align 4, !tbaa !9
  %32 = sub nsw i32 %31, %30
  store i32 %32, ptr %9, align 4, !tbaa !9
  br label %84

33:                                               ; preds = %16
  %34 = load i32, ptr %9, align 4, !tbaa !9
  %35 = getelementptr inbounds %class.FixedBlockAdapter, ptr %11, i32 0, i32 3
  %36 = load i32, ptr %35, align 8, !tbaa !21
  %37 = icmp sge i32 %34, %36
  br i1 %37, label %38, label %60

38:                                               ; preds = %33
  %39 = getelementptr inbounds %class.FixedBlockAdapter, ptr %11, i32 0, i32 1
  %40 = load ptr, ptr %39, align 8, !tbaa !22
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  %42 = getelementptr inbounds %class.FixedBlockAdapter, ptr %11, i32 0, i32 3
  %43 = load i32, ptr %42, align 8, !tbaa !21
  %44 = load ptr, ptr %40, align 8, !tbaa !23
  %45 = getelementptr inbounds ptr, ptr %44, i64 2
  %46 = load ptr, ptr %45, align 8
  %47 = call noundef i32 %46(ptr noundef nonnull align 8 dereferenceable(8) %40, ptr noundef %41, i32 noundef %43)
  store i32 %47, ptr %8, align 4, !tbaa !9
  %48 = load i32, ptr %8, align 4, !tbaa !9
  %49 = icmp slt i32 %48, 0
  br i1 %49, label %50, label %52

50:                                               ; preds = %38
  %51 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %51, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %89

52:                                               ; preds = %38
  %53 = load i32, ptr %8, align 4, !tbaa !9
  %54 = load ptr, ptr %6, align 8, !tbaa !5
  %55 = sext i32 %53 to i64
  %56 = getelementptr inbounds i8, ptr %54, i64 %55
  store ptr %56, ptr %6, align 8, !tbaa !5
  %57 = load i32, ptr %8, align 4, !tbaa !9
  %58 = load i32, ptr %9, align 4, !tbaa !9
  %59 = sub nsw i32 %58, %57
  store i32 %59, ptr %9, align 4, !tbaa !9
  br label %83

60:                                               ; preds = %33
  %61 = getelementptr inbounds %class.FixedBlockAdapter, ptr %11, i32 0, i32 1
  %62 = load ptr, ptr %61, align 8, !tbaa !22
  %63 = getelementptr inbounds %class.FixedBlockAdapter, ptr %11, i32 0, i32 2
  %64 = call noundef ptr @_ZNKSt10unique_ptrIA_hSt14default_deleteIS0_EE3getEv(ptr noundef nonnull align 8 dereferenceable(8) %63) #3
  %65 = getelementptr inbounds %class.FixedBlockAdapter, ptr %11, i32 0, i32 3
  %66 = load i32, ptr %65, align 8, !tbaa !21
  %67 = load ptr, ptr %62, align 8, !tbaa !23
  %68 = getelementptr inbounds ptr, ptr %67, i64 2
  %69 = load ptr, ptr %68, align 8
  %70 = call noundef i32 %69(ptr noundef nonnull align 8 dereferenceable(8) %62, ptr noundef %64, i32 noundef %66)
  store i32 %70, ptr %8, align 4, !tbaa !9
  %71 = load i32, ptr %8, align 4, !tbaa !9
  %72 = icmp slt i32 %71, 0
  br i1 %72, label %73, label %75

73:                                               ; preds = %60
  %74 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %74, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %89

75:                                               ; preds = %60
  %76 = getelementptr inbounds %class.FixedBlockAdapter, ptr %11, i32 0, i32 4
  store i32 0, ptr %76, align 4, !tbaa !11
  %77 = load i32, ptr %8, align 4, !tbaa !9
  %78 = getelementptr inbounds %class.FixedBlockReader, ptr %11, i32 0, i32 1
  store i32 %77, ptr %78, align 8, !tbaa !19
  %79 = load i32, ptr %8, align 4, !tbaa !9
  %80 = icmp eq i32 %79, 0
  br i1 %80, label %81, label %82

81:                                               ; preds = %75
  br label %85

82:                                               ; preds = %75
  br label %83

83:                                               ; preds = %82, %52
  br label %84

84:                                               ; preds = %83, %22
  br label %13, !llvm.loop !25

85:                                               ; preds = %81, %13
  %86 = load i32, ptr %7, align 4, !tbaa !9
  %87 = load i32, ptr %9, align 4, !tbaa !9
  %88 = sub nsw i32 %86, %87
  store i32 %88, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %89

89:                                               ; preds = %85, %73, %50
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  %90 = load i32, ptr %4, align 4
  ret i32 %90
}

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 = { 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 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 28}
!12 = !{!"_ZTS17FixedBlockAdapter", !6, i64 8, !13, i64 16, !10, i64 24, !10, i64 28}
!13 = !{!"_ZTSSt10unique_ptrIA_hSt14default_deleteIS0_EE", !14, i64 0}
!14 = !{!"_ZTSSt15__uniq_ptr_dataIhSt14default_deleteIA_hELb1ELb1EE", !15, i64 0}
!15 = !{!"_ZTSSt15__uniq_ptr_implIhSt14default_deleteIA_hEE", !16, i64 0}
!16 = !{!"_ZTSSt5tupleIJPhSt14default_deleteIA_hEEE", !17, i64 0}
!17 = !{!"_ZTSSt11_Tuple_implILm0EJPhSt14default_deleteIA_hEEE", !18, i64 0}
!18 = !{!"_ZTSSt10_Head_baseILm0EPhLb0EE", !6, i64 0}
!19 = !{!20, !10, i64 32}
!20 = !{!"_ZTS16FixedBlockReader", !12, i64 0, !10, i64 32}
!21 = !{!12, !10, i64 24}
!22 = !{!12, !6, i64 8}
!23 = !{!24, !24, i64 0}
!24 = !{!"vtable pointer", !8, i64 0}
!25 = distinct !{!25, !26}
!26 = !{!"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"

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

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN16FixedBlockReader4readEPhi(ptr noundef nonnull align 8 dereferenceable(36) %0, ptr noundef %1, i32 noundef %2) local_unnamed_addr #1 align 2 {
  %4 = icmp sgt i32 %2, 0
  br i1 %4, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %3
  %5 = getelementptr inbounds i8, ptr %0, i64 28
  %6 = getelementptr inbounds i8, ptr %0, i64 32
  %7 = getelementptr inbounds i8, ptr %0, i64 24
  %8 = getelementptr inbounds i8, ptr %0, i64 8
  %9 = getelementptr inbounds i8, ptr %0, i64 16
  br label %10

10:                                               ; preds = %.lr.ph, %42
  %.033 = phi i32 [ %2, %.lr.ph ], [ %.1, %42 ]
  %.02432 = phi ptr [ %1, %.lr.ph ], [ %.125, %42 ]
  %11 = load i32, ptr %5, align 4, !tbaa !5
  %12 = load i32, ptr %6, align 8, !tbaa !17
  %13 = icmp slt i32 %11, %12
  br i1 %13, label %14, label %19

14:                                               ; preds = %10
  %15 = tail call noundef i32 @_ZN16FixedBlockReader15readFromStorageEPhi(ptr noundef nonnull align 8 dereferenceable(36) %0, ptr noundef %.02432, i32 noundef %.033)
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds i8, ptr %.02432, i64 %16
  %18 = sub nsw i32 %.033, %15
  br label %42

19:                                               ; preds = %10
  %20 = load i32, ptr %7, align 8, !tbaa !19
  %.not = icmp slt i32 %.033, %20
  %21 = load ptr, ptr %8, align 8, !tbaa !20
  br i1 %.not, label %32, label %22

22:                                               ; preds = %19
  %23 = load ptr, ptr %21, align 8, !tbaa !21
  %24 = getelementptr inbounds i8, ptr %23, i64 16
  %25 = load ptr, ptr %24, align 8
  %26 = tail call noundef i32 %25(ptr noundef nonnull align 8 dereferenceable(8) %21, ptr noundef %.02432, i32 noundef %20)
  %27 = icmp slt i32 %26, 0
  br i1 %27, label %.loopexit, label %28

28:                                               ; preds = %22
  %29 = zext nneg i32 %26 to i64
  %30 = getelementptr inbounds i8, ptr %.02432, i64 %29
  %31 = sub nsw i32 %.033, %26
  br label %42

32:                                               ; preds = %19
  %33 = tail call noundef ptr @_ZNKSt10unique_ptrIA_hSt14default_deleteIS0_EE3getEv(ptr noundef nonnull align 8 dereferenceable(8) %9) #2
  %34 = load i32, ptr %7, align 8, !tbaa !19
  %35 = load ptr, ptr %21, align 8, !tbaa !21
  %36 = getelementptr inbounds i8, ptr %35, i64 16
  %37 = load ptr, ptr %36, align 8
  %38 = tail call noundef i32 %37(ptr noundef nonnull align 8 dereferenceable(8) %21, ptr noundef %33, i32 noundef %34)
  %39 = icmp slt i32 %38, 0
  br i1 %39, label %.loopexit, label %40

40:                                               ; preds = %32
  store i32 0, ptr %5, align 4, !tbaa !5
  store i32 %38, ptr %6, align 8, !tbaa !17
  %41 = icmp eq i32 %38, 0
  br i1 %41, label %._crit_edge, label %42

42:                                               ; preds = %28, %40, %14
  %.125 = phi ptr [ %17, %14 ], [ %30, %28 ], [ %.02432, %40 ]
  %.1 = phi i32 [ %18, %14 ], [ %31, %28 ], [ %.033, %40 ]
  %43 = icmp sgt i32 %.1, 0
  br i1 %43, label %10, label %._crit_edge, !llvm.loop !23

._crit_edge:                                      ; preds = %42, %40, %3
  %.0.lcssa = phi i32 [ %2, %3 ], [ %.033, %40 ], [ %.1, %42 ]
  %44 = sub nsw i32 %2, %.0.lcssa
  br label %.loopexit

.loopexit:                                        ; preds = %32, %22, %._crit_edge
  %.023 = phi i32 [ %44, %._crit_edge ], [ %38, %32 ], [ %26, %22 ]
  ret i32 %.023
}

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 = { 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, !16, i64 28}
!6 = !{!"_ZTS17FixedBlockAdapter", !7, i64 8, !10, i64 16, !16, i64 24, !16, i64 28}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"_ZTSSt10unique_ptrIA_hSt14default_deleteIS0_EE", !11, i64 0}
!11 = !{!"_ZTSSt15__uniq_ptr_dataIhSt14default_deleteIA_hELb1ELb1EE", !12, i64 0}
!12 = !{!"_ZTSSt15__uniq_ptr_implIhSt14default_deleteIA_hEE", !13, i64 0}
!13 = !{!"_ZTSSt5tupleIJPhSt14default_deleteIA_hEEE", !14, i64 0}
!14 = !{!"_ZTSSt11_Tuple_implILm0EJPhSt14default_deleteIA_hEEE", !15, i64 0}
!15 = !{!"_ZTSSt10_Head_baseILm0EPhLb0EE", !7, i64 0}
!16 = !{!"int", !8, i64 0}
!17 = !{!18, !16, i64 32}
!18 = !{!"_ZTS16FixedBlockReader", !6, i64 0, !16, i64 32}
!19 = !{!6, !16, i64 24}
!20 = !{!6, !7, i64 8}
!21 = !{!22, !22, i64 0}
!22 = !{!"vtable pointer", !9, i64 0}
!23 = distinct !{!23, !24}
!24 = !{!"llvm.loop.mustprogress"}


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