; LLVM IR from yinqiwen/ardb :: src/common/coro/coro_channel.ll (ZN4ardb11CoroChannel9SyncWriteERNS_6BufferEi)

; 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.ardb::CoroChannel" = type { %"class.ardb::ChannelUpstreamHandler", %"class.ardb::Runnable", ptr, ptr, ptr, ptr, i8, ptr, i8, i32 }
%"class.ardb::ChannelUpstreamHandler" = type { %"class.ardb::AbstractChannelHandler" }
%"class.ardb::AbstractChannelHandler" = type { %"class.ardb::ChannelHandler" }
%"class.ardb::ChannelHandler" = type { ptr }
%"class.ardb::Runnable" = 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
declare dso_local noundef i32 @_ZN4ardb11CoroChannel8WaitCoroEv(ptr noundef nonnull align 8 dereferenceable(72)) #1 align 2

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN4ardb11CoroChannel17CreateTimeoutTaskEi(ptr noundef nonnull align 8 dereferenceable(72), i32 noundef) #1 align 2

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef i32 @_ZN4ardb6Buffer5WriteEPS0_m(ptr noundef nonnull align 8 dereferenceable(33), ptr noundef, i64 noundef) #2 align 2

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef i64 @_ZNK4ardb6Buffer13ReadableBytesEv(ptr noundef nonnull align 8 dereferenceable(33)) #2 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN4ardb11CoroChannel9SyncWriteERNS_6BufferEi(ptr noundef nonnull align 8 dereferenceable(72) %0, ptr noundef nonnull align 8 dereferenceable(33) %1, i32 noundef %2) #1 align 2 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i64, align 8
  %9 = 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
  %10 = load ptr, ptr %5, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  %11 = load i32, ptr %7, align 4, !tbaa !9
  %12 = icmp sgt i32 %11, 0
  br i1 %12, label %13, label %15

13:                                               ; preds = %3
  %14 = call noundef i64 @_ZN4ardb24get_current_epoch_millisEv()
  br label %16

15:                                               ; preds = %3
  br label %16

16:                                               ; preds = %15, %13
  %17 = phi i64 [ %14, %13 ], [ 0, %15 ]
  store i64 %17, ptr %8, align 8, !tbaa !11
  %18 = getelementptr inbounds %"class.ardb::CoroChannel", ptr %10, i32 0, i32 3
  %19 = load ptr, ptr %18, align 8, !tbaa !13
  %20 = call noundef nonnull align 8 dereferenceable(33) ptr @_ZN4ardb7Channel15GetOutputBufferEv(ptr noundef nonnull align 8 dereferenceable(256) %19)
  %21 = load ptr, ptr %6, align 8, !tbaa !5
  %22 = load ptr, ptr %6, align 8, !tbaa !5
  %23 = call noundef i64 @_ZNK4ardb6Buffer13ReadableBytesEv(ptr noundef nonnull align 8 dereferenceable(33) %22)
  %24 = call noundef i32 @_ZN4ardb6Buffer5WriteEPS0_m(ptr noundef nonnull align 8 dereferenceable(33) %20, ptr noundef %21, i64 noundef %23)
  %25 = getelementptr inbounds %"class.ardb::CoroChannel", ptr %10, i32 0, i32 3
  %26 = load ptr, ptr %25, align 8, !tbaa !13
  call void @_ZN4ardb7Channel13EnableWritingEv(ptr noundef nonnull align 8 dereferenceable(256) %26)
  %27 = load i32, ptr %7, align 4, !tbaa !9
  call void @_ZN4ardb11CoroChannel17CreateTimeoutTaskEi(ptr noundef nonnull align 8 dereferenceable(72) %10, i32 noundef %27)
  br label %28

28:                                               ; preds = %50, %16
  %29 = getelementptr inbounds %"class.ardb::CoroChannel", ptr %10, i32 0, i32 3
  %30 = load ptr, ptr %29, align 8, !tbaa !13
  %31 = call noundef nonnull align 8 dereferenceable(33) ptr @_ZN4ardb7Channel15GetOutputBufferEv(ptr noundef nonnull align 8 dereferenceable(256) %30)
  %32 = call noundef i64 @_ZNK4ardb6Buffer13ReadableBytesEv(ptr noundef nonnull align 8 dereferenceable(33) %31)
  %33 = icmp ugt i64 %32, 0
  br i1 %33, label %34, label %51

34:                                               ; preds = %28
  %35 = load i32, ptr %7, align 4, !tbaa !9
  %36 = icmp sgt i32 %35, 0
  br i1 %36, label %37, label %46

37:                                               ; preds = %34
  %38 = call noundef i64 @_ZN4ardb24get_current_epoch_millisEv()
  %39 = load i64, ptr %8, align 8, !tbaa !11
  %40 = sub i64 %38, %39
  %41 = load i32, ptr %7, align 4, !tbaa !9
  %42 = sext i32 %41 to i64
  %43 = icmp uge i64 %40, %42
  br i1 %43, label %44, label %45

44:                                               ; preds = %37
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %52

45:                                               ; preds = %37
  br label %46

46:                                               ; preds = %45, %34
  %47 = call noundef i32 @_ZN4ardb11CoroChannel8WaitCoroEv(ptr noundef nonnull align 8 dereferenceable(72) %10)
  %48 = icmp ne i32 0, %47
  br i1 %48, label %49, label %50

49:                                               ; preds = %46
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %52

50:                                               ; preds = %46
  br label %28, !llvm.loop !20

51:                                               ; preds = %28
  store i32 0, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %52

52:                                               ; preds = %51, %49, %44
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  %53 = load i32, ptr %4, align 4
  ret i32 %53
}

declare noundef i64 @_ZN4ardb24get_current_epoch_millisEv() #3

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(33) ptr @_ZN4ardb7Channel15GetOutputBufferEv(ptr noundef nonnull align 8 dereferenceable(256)) #4 align 2

declare void @_ZN4ardb7Channel13EnableWritingEv(ptr noundef nonnull align 8 dereferenceable(256)) #3

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 = { inlinehint 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 = { "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 = { 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 #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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !6, i64 24}
!14 = !{!"_ZTSN4ardb11CoroChannelE", !15, i64 0, !18, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !19, i64 48, !6, i64 56, !19, i64 64, !10, i64 68}
!15 = !{!"_ZTSN4ardb22ChannelUpstreamHandlerINS_6BufferEEE", !16, i64 0}
!16 = !{!"_ZTSN4ardb22AbstractChannelHandlerINS_6BufferEEE", !17, i64 0}
!17 = !{!"_ZTSN4ardb14ChannelHandlerE"}
!18 = !{!"_ZTSN4ardb8RunnableE"}
!19 = !{!"bool", !7, i64 0}
!20 = distinct !{!20, !21}
!21 = !{!"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 uwtable
declare dso_local noundef i32 @_ZN4ardb11CoroChannel8WaitCoroEv(ptr noundef nonnull align 8 dereferenceable(72)) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN4ardb11CoroChannel17CreateTimeoutTaskEi(ptr noundef nonnull align 8 dereferenceable(72), i32 noundef) local_unnamed_addr #0 align 2

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef i32 @_ZN4ardb6Buffer5WriteEPS0_m(ptr noundef nonnull align 8 dereferenceable(33), ptr noundef, i64 noundef) local_unnamed_addr #1 align 2

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef i64 @_ZNK4ardb6Buffer13ReadableBytesEv(ptr noundef nonnull align 8 dereferenceable(33)) local_unnamed_addr #1 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef range(i32 -1, 1) i32 @_ZN4ardb11CoroChannel9SyncWriteERNS_6BufferEi(ptr noundef nonnull align 8 dereferenceable(72) %0, ptr noundef nonnull align 8 dereferenceable(33) %1, i32 noundef %2) local_unnamed_addr #0 align 2 {
  %4 = icmp sgt i32 %2, 0
  br i1 %4, label %5, label %7

5:                                                ; preds = %3
  %6 = tail call noundef i64 @_ZN4ardb24get_current_epoch_millisEv()
  br label %7

7:                                                ; preds = %3, %5
  %8 = phi i64 [ %6, %5 ], [ 0, %3 ]
  %9 = getelementptr inbounds i8, ptr %0, i64 24
  %10 = load ptr, ptr %9, align 8, !tbaa !5
  %11 = tail call noundef nonnull align 8 dereferenceable(33) ptr @_ZN4ardb7Channel15GetOutputBufferEv(ptr noundef nonnull align 8 dereferenceable(256) %10)
  %12 = tail call noundef i64 @_ZNK4ardb6Buffer13ReadableBytesEv(ptr noundef nonnull align 8 dereferenceable(33) %1)
  %13 = tail call noundef i32 @_ZN4ardb6Buffer5WriteEPS0_m(ptr noundef nonnull align 8 dereferenceable(33) %11, ptr noundef nonnull %1, i64 noundef %12)
  %14 = load ptr, ptr %9, align 8, !tbaa !5
  tail call void @_ZN4ardb7Channel13EnableWritingEv(ptr noundef nonnull align 8 dereferenceable(256) %14)
  tail call void @_ZN4ardb11CoroChannel17CreateTimeoutTaskEi(ptr noundef nonnull align 8 dereferenceable(72) %0, i32 noundef %2)
  %15 = zext nneg i32 %2 to i64
  br i1 %4, label %.split.us, label %.split

.split.us:                                        ; preds = %7, %22
  %16 = load ptr, ptr %9, align 8, !tbaa !5
  %17 = tail call noundef nonnull align 8 dereferenceable(33) ptr @_ZN4ardb7Channel15GetOutputBufferEv(ptr noundef nonnull align 8 dereferenceable(256) %16)
  %18 = tail call noundef i64 @_ZNK4ardb6Buffer13ReadableBytesEv(ptr noundef nonnull align 8 dereferenceable(33) %17)
  %.not.us = icmp eq i64 %18, 0
  br i1 %.not.us, label %.split12.us, label %19

19:                                               ; preds = %.split.us
  %20 = tail call noundef i64 @_ZN4ardb24get_current_epoch_millisEv()
  %21 = sub i64 %20, %8
  %.not9.us = icmp ult i64 %21, %15
  br i1 %.not9.us, label %22, label %.split12.us

22:                                               ; preds = %19
  %23 = tail call noundef i32 @_ZN4ardb11CoroChannel8WaitCoroEv(ptr noundef nonnull align 8 dereferenceable(72) %0)
  %.not10.us = icmp eq i32 %23, 0
  br i1 %.not10.us, label %.split.us, label %.split12.us, !llvm.loop !16

.split:                                           ; preds = %7, %27
  %24 = load ptr, ptr %9, align 8, !tbaa !5
  %25 = tail call noundef nonnull align 8 dereferenceable(33) ptr @_ZN4ardb7Channel15GetOutputBufferEv(ptr noundef nonnull align 8 dereferenceable(256) %24)
  %26 = tail call noundef i64 @_ZNK4ardb6Buffer13ReadableBytesEv(ptr noundef nonnull align 8 dereferenceable(33) %25)
  %.not = icmp eq i64 %26, 0
  br i1 %.not, label %.split12.us, label %27

27:                                               ; preds = %.split
  %28 = tail call noundef i32 @_ZN4ardb11CoroChannel8WaitCoroEv(ptr noundef nonnull align 8 dereferenceable(72) %0)
  %.not10 = icmp eq i32 %28, 0
  br i1 %.not10, label %.split, label %.split12.us, !llvm.loop !16

.split12.us:                                      ; preds = %27, %.split, %.split.us, %19, %22
  %.us-phi = phi i32 [ -1, %19 ], [ -1, %22 ], [ 0, %.split.us ], [ -1, %27 ], [ 0, %.split ]
  ret i32 %.us-phi
}

declare noundef i64 @_ZN4ardb24get_current_epoch_millisEv() local_unnamed_addr #2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(33) ptr @_ZN4ardb7Channel15GetOutputBufferEv(ptr noundef nonnull align 8 dereferenceable(256)) local_unnamed_addr #3 align 2

declare void @_ZN4ardb7Channel13EnableWritingEv(ptr noundef nonnull align 8 dereferenceable(256)) local_unnamed_addr #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" }
attributes #1 = { inlinehint 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 = { "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 = { 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" }

!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 24}
!6 = !{!"_ZTSN4ardb11CoroChannelE", !7, i64 0, !10, i64 8, !11, i64 16, !11, i64 24, !11, i64 32, !11, i64 40, !14, i64 48, !11, i64 56, !14, i64 64, !15, i64 68}
!7 = !{!"_ZTSN4ardb22ChannelUpstreamHandlerINS_6BufferEEE", !8, i64 0}
!8 = !{!"_ZTSN4ardb22AbstractChannelHandlerINS_6BufferEEE", !9, i64 0}
!9 = !{!"_ZTSN4ardb14ChannelHandlerE"}
!10 = !{!"_ZTSN4ardb8RunnableE"}
!11 = !{!"any pointer", !12, i64 0}
!12 = !{!"omnipotent char", !13, i64 0}
!13 = !{!"Simple C++ TBAA"}
!14 = !{!"bool", !12, i64 0}
!15 = !{!"int", !12, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


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