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

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

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

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

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

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN16FixedBlockWriter5writeEPhi(ptr noundef nonnull align 8 dereferenceable(32) %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
  %11 = alloca i32, align 4
  %12 = 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
  %13 = load ptr, ptr %5, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %14 = load i32, ptr %7, align 4, !tbaa !9
  store i32 %14, ptr %8, align 4, !tbaa !9
  %15 = getelementptr inbounds %class.FixedBlockAdapter, ptr %13, i32 0, i32 4
  %16 = load i32, ptr %15, align 4, !tbaa !11
  %17 = icmp sgt i32 %16, 0
  br i1 %17, label %18, label %61

18:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  %19 = load ptr, ptr %6, align 8, !tbaa !5
  %20 = load i32, ptr %8, align 4, !tbaa !9
  %21 = call noundef i32 @_ZN16FixedBlockWriter14writeToStorageEPhi(ptr noundef nonnull align 8 dereferenceable(32) %13, ptr noundef %19, i32 noundef %20)
  store i32 %21, ptr %9, align 4, !tbaa !9
  %22 = load i32, ptr %9, align 4, !tbaa !9
  %23 = load ptr, ptr %6, align 8, !tbaa !5
  %24 = sext i32 %22 to i64
  %25 = getelementptr inbounds i8, ptr %23, i64 %24
  store ptr %25, ptr %6, align 8, !tbaa !5
  %26 = load i32, ptr %9, align 4, !tbaa !9
  %27 = load i32, ptr %8, align 4, !tbaa !9
  %28 = sub nsw i32 %27, %26
  store i32 %28, ptr %8, align 4, !tbaa !9
  %29 = getelementptr inbounds %class.FixedBlockAdapter, ptr %13, i32 0, i32 4
  %30 = load i32, ptr %29, align 4, !tbaa !11
  %31 = getelementptr inbounds %class.FixedBlockAdapter, ptr %13, i32 0, i32 3
  %32 = load i32, ptr %31, align 8, !tbaa !19
  %33 = icmp eq i32 %30, %32
  br i1 %33, label %34, label %57

34:                                               ; preds = %18
  %35 = getelementptr inbounds %class.FixedBlockAdapter, ptr %13, i32 0, i32 1
  %36 = load ptr, ptr %35, align 8, !tbaa !20
  %37 = getelementptr inbounds %class.FixedBlockAdapter, ptr %13, i32 0, i32 2
  %38 = call noundef ptr @_ZNKSt10unique_ptrIA_hSt14default_deleteIS0_EE3getEv(ptr noundef nonnull align 8 dereferenceable(8) %37) #3
  %39 = getelementptr inbounds %class.FixedBlockAdapter, ptr %13, i32 0, i32 3
  %40 = load i32, ptr %39, align 8, !tbaa !19
  %41 = load ptr, ptr %36, align 8, !tbaa !21
  %42 = getelementptr inbounds ptr, ptr %41, i64 2
  %43 = load ptr, ptr %42, align 8
  %44 = call noundef i32 %43(ptr noundef nonnull align 8 dereferenceable(8) %36, ptr noundef %38, i32 noundef %40)
  store i32 %44, ptr %9, align 4, !tbaa !9
  %45 = load i32, ptr %9, align 4, !tbaa !9
  %46 = icmp slt i32 %45, 0
  br i1 %46, label %47, label %49

47:                                               ; preds = %34
  %48 = load i32, ptr %9, align 4, !tbaa !9
  store i32 %48, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %58

49:                                               ; preds = %34
  %50 = getelementptr inbounds %class.FixedBlockAdapter, ptr %13, i32 0, i32 4
  store i32 0, ptr %50, align 4, !tbaa !11
  %51 = load i32, ptr %9, align 4, !tbaa !9
  %52 = getelementptr inbounds %class.FixedBlockAdapter, ptr %13, i32 0, i32 3
  %53 = load i32, ptr %52, align 8, !tbaa !19
  %54 = icmp slt i32 %51, %53
  br i1 %54, label %55, label %56

55:                                               ; preds = %49
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %58

56:                                               ; preds = %49
  br label %57

57:                                               ; preds = %56, %18
  store i32 0, ptr %10, align 4
  br label %58

58:                                               ; preds = %57, %55, %47
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  %59 = load i32, ptr %10, align 4
  switch i32 %59, label %106 [
    i32 0, label %60
  ]

60:                                               ; preds = %58
  br label %61

61:                                               ; preds = %60, %3
  br label %62

62:                                               ; preds = %91, %61
  %63 = load i32, ptr %8, align 4, !tbaa !9
  %64 = getelementptr inbounds %class.FixedBlockAdapter, ptr %13, i32 0, i32 3
  %65 = load i32, ptr %64, align 8, !tbaa !19
  %66 = icmp sgt i32 %63, %65
  br i1 %66, label %67, label %92

67:                                               ; preds = %62
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  %68 = getelementptr inbounds %class.FixedBlockAdapter, ptr %13, i32 0, i32 1
  %69 = load ptr, ptr %68, align 8, !tbaa !20
  %70 = load ptr, ptr %6, align 8, !tbaa !5
  %71 = getelementptr inbounds %class.FixedBlockAdapter, ptr %13, i32 0, i32 3
  %72 = load i32, ptr %71, align 8, !tbaa !19
  %73 = load ptr, ptr %69, align 8, !tbaa !21
  %74 = getelementptr inbounds ptr, ptr %73, i64 2
  %75 = load ptr, ptr %74, align 8
  %76 = call noundef i32 %75(ptr noundef nonnull align 8 dereferenceable(8) %69, ptr noundef %70, i32 noundef %72)
  store i32 %76, ptr %11, align 4, !tbaa !9
  %77 = load i32, ptr %11, align 4, !tbaa !9
  %78 = icmp slt i32 %77, 0
  br i1 %78, label %79, label %81

79:                                               ; preds = %67
  %80 = load i32, ptr %11, align 4, !tbaa !9
  store i32 %80, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %89

81:                                               ; preds = %67
  %82 = load i32, ptr %11, align 4, !tbaa !9
  %83 = load ptr, ptr %6, align 8, !tbaa !5
  %84 = sext i32 %82 to i64
  %85 = getelementptr inbounds i8, ptr %83, i64 %84
  store ptr %85, ptr %6, align 8, !tbaa !5
  %86 = load i32, ptr %11, align 4, !tbaa !9
  %87 = load i32, ptr %8, align 4, !tbaa !9
  %88 = sub nsw i32 %87, %86
  store i32 %88, ptr %8, align 4, !tbaa !9
  store i32 0, ptr %10, align 4
  br label %89

89:                                               ; preds = %81, %79
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  %90 = load i32, ptr %10, align 4
  switch i32 %90, label %106 [
    i32 0, label %91
  ]

91:                                               ; preds = %89
  br label %62, !llvm.loop !23

92:                                               ; preds = %62
  %93 = load i32, ptr %8, align 4, !tbaa !9
  %94 = icmp sgt i32 %93, 0
  br i1 %94, label %95, label %102

95:                                               ; preds = %92
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  %96 = load ptr, ptr %6, align 8, !tbaa !5
  %97 = load i32, ptr %8, align 4, !tbaa !9
  %98 = call noundef i32 @_ZN16FixedBlockWriter14writeToStorageEPhi(ptr noundef nonnull align 8 dereferenceable(32) %13, ptr noundef %96, i32 noundef %97)
  store i32 %98, ptr %12, align 4, !tbaa !9
  %99 = load i32, ptr %12, align 4, !tbaa !9
  %100 = load i32, ptr %8, align 4, !tbaa !9
  %101 = sub nsw i32 %100, %99
  store i32 %101, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  br label %102

102:                                              ; preds = %95, %92
  %103 = load i32, ptr %7, align 4, !tbaa !9
  %104 = load i32, ptr %8, align 4, !tbaa !9
  %105 = sub nsw i32 %103, %104
  store i32 %105, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %106

106:                                              ; preds = %102, %89, %58
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  %107 = load i32, ptr %4, align 4
  ret i32 %107
}

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!12, !10, i64 24}
!20 = !{!12, !6, i64 8}
!21 = !{!22, !22, i64 0}
!22 = !{!"vtable pointer", !8, i64 0}
!23 = distinct !{!23, !24}
!24 = !{!"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 @_ZN16FixedBlockWriter14writeToStorageEPhi(ptr noundef nonnull align 8 dereferenceable(32), 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 @_ZN16FixedBlockWriter5writeEPhi(ptr noundef nonnull align 8 dereferenceable(32) %0, ptr noundef %1, i32 noundef %2) local_unnamed_addr #1 align 2 {
  %4 = getelementptr inbounds i8, ptr %0, i64 28
  %5 = load i32, ptr %4, align 4, !tbaa !5
  %6 = icmp sgt i32 %5, 0
  br i1 %6, label %7, label %30

7:                                                ; preds = %3
  %8 = tail call noundef i32 @_ZN16FixedBlockWriter14writeToStorageEPhi(ptr noundef nonnull align 8 dereferenceable(32) %0, ptr noundef %1, i32 noundef %2)
  %9 = sext i32 %8 to i64
  %10 = getelementptr inbounds i8, ptr %1, i64 %9
  %11 = sub nsw i32 %2, %8
  %12 = load i32, ptr %4, align 4, !tbaa !5
  %13 = getelementptr inbounds i8, ptr %0, i64 24
  %14 = load i32, ptr %13, align 8, !tbaa !17
  %15 = icmp eq i32 %12, %14
  br i1 %15, label %16, label %30

16:                                               ; preds = %7
  %17 = getelementptr inbounds i8, ptr %0, i64 8
  %18 = load ptr, ptr %17, align 8, !tbaa !18
  %19 = getelementptr inbounds i8, ptr %0, i64 16
  %20 = tail call noundef ptr @_ZNKSt10unique_ptrIA_hSt14default_deleteIS0_EE3getEv(ptr noundef nonnull align 8 dereferenceable(8) %19) #2
  %21 = load i32, ptr %13, align 8, !tbaa !17
  %22 = load ptr, ptr %18, align 8, !tbaa !19
  %23 = getelementptr inbounds i8, ptr %22, i64 16
  %24 = load ptr, ptr %23, align 8
  %25 = tail call noundef i32 %24(ptr noundef nonnull align 8 dereferenceable(8) %18, ptr noundef %20, i32 noundef %21)
  %26 = icmp slt i32 %25, 0
  br i1 %26, label %.thread, label %27

27:                                               ; preds = %16
  store i32 0, ptr %4, align 4, !tbaa !5
  %28 = load i32, ptr %13, align 8, !tbaa !17
  %29 = icmp slt i32 %25, %28
  br i1 %29, label %.thread, label %30

30:                                               ; preds = %27, %7, %3
  %.034 = phi i32 [ %2, %3 ], [ %11, %7 ], [ %11, %27 ]
  %.029 = phi ptr [ %1, %3 ], [ %10, %7 ], [ %10, %27 ]
  %31 = getelementptr inbounds i8, ptr %0, i64 24
  %32 = getelementptr inbounds i8, ptr %0, i64 8
  br label %33

33:                                               ; preds = %36, %30
  %.135 = phi i32 [ %.034, %30 ], [ %45, %36 ]
  %.130 = phi ptr [ %.029, %30 ], [ %44, %36 ]
  %34 = load i32, ptr %31, align 8, !tbaa !17
  %35 = icmp sgt i32 %.135, %34
  br i1 %35, label %36, label %46

36:                                               ; preds = %33
  %37 = load ptr, ptr %32, align 8, !tbaa !18
  %38 = load ptr, ptr %37, align 8, !tbaa !19
  %39 = getelementptr inbounds i8, ptr %38, i64 16
  %40 = load ptr, ptr %39, align 8
  %41 = tail call noundef i32 %40(ptr noundef nonnull align 8 dereferenceable(8) %37, ptr noundef %.130, i32 noundef %34)
  %42 = icmp sgt i32 %41, -1
  %43 = zext nneg i32 %41 to i64
  %44 = getelementptr inbounds i8, ptr %.130, i64 %43
  %45 = sub nsw i32 %.135, %41
  br i1 %42, label %33, label %.thread, !llvm.loop !21

46:                                               ; preds = %33
  %47 = icmp sgt i32 %.135, 0
  br i1 %47, label %48, label %51

48:                                               ; preds = %46
  %49 = tail call noundef i32 @_ZN16FixedBlockWriter14writeToStorageEPhi(ptr noundef nonnull align 8 dereferenceable(32) %0, ptr noundef %.130, i32 noundef %.135)
  %50 = sub nsw i32 %.135, %49
  br label %51

51:                                               ; preds = %48, %46
  %.337 = phi i32 [ %50, %48 ], [ %.135, %46 ]
  %52 = sub nsw i32 %2, %.337
  br label %.thread

.thread:                                          ; preds = %36, %27, %16, %51
  %.4 = phi i32 [ %52, %51 ], [ -1, %27 ], [ %25, %16 ], [ %41, %36 ]
  ret i32 %.4
}

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 = !{!6, !16, i64 24}
!18 = !{!6, !7, i64 8}
!19 = !{!20, !20, i64 0}
!20 = !{!"vtable pointer", !9, i64 0}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}


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