; LLVM IR from newbiediver/rioring :: src/io_buffer.ll (ZN7rioring9io_buffer4pushEPKhm)

; 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.rioring::io_buffer" = type { %"class.rioring::buffer_base", ptr, i64, i64, i64 }
%"class.rioring::buffer_base" = type { ptr }

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #0

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN7rioring9io_buffer4pushEPKhm(ptr noundef nonnull align 8 dereferenceable(40) %0, ptr noundef %1, i64 noundef %2) unnamed_addr #1 align 2 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i64 %2, ptr %6, align 8, !tbaa !9
  %8 = load ptr, ptr %4, align 8
  br label %9

9:                                                ; preds = %35, %3
  %10 = getelementptr inbounds %"class.rioring::io_buffer", ptr %8, i32 0, i32 4
  %11 = load i64, ptr %10, align 8, !tbaa !11
  %12 = load i64, ptr %6, align 8, !tbaa !9
  %13 = add i64 %11, %12
  %14 = getelementptr inbounds %"class.rioring::io_buffer", ptr %8, i32 0, i32 2
  %15 = load i64, ptr %14, align 8, !tbaa !14
  %16 = icmp ugt i64 %13, %15
  br i1 %16, label %17, label %37

17:                                               ; preds = %9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %18 = getelementptr inbounds %"class.rioring::io_buffer", ptr %8, i32 0, i32 2
  %19 = load i64, ptr %18, align 8, !tbaa !14
  %20 = getelementptr inbounds %"class.rioring::io_buffer", ptr %8, i32 0, i32 3
  %21 = load i64, ptr %20, align 8, !tbaa !15
  %22 = add i64 %19, %21
  store i64 %22, ptr %7, align 8, !tbaa !9
  br label %23

23:                                               ; preds = %30, %17
  %24 = getelementptr inbounds %"class.rioring::io_buffer", ptr %8, i32 0, i32 4
  %25 = load i64, ptr %24, align 8, !tbaa !11
  %26 = load i64, ptr %6, align 8, !tbaa !9
  %27 = add i64 %25, %26
  %28 = load i64, ptr %7, align 8, !tbaa !9
  %29 = icmp uge i64 %27, %28
  br i1 %29, label %30, label %35

30:                                               ; preds = %23
  %31 = getelementptr inbounds %"class.rioring::io_buffer", ptr %8, i32 0, i32 3
  %32 = load i64, ptr %31, align 8, !tbaa !15
  %33 = load i64, ptr %7, align 8, !tbaa !9
  %34 = add i64 %33, %32
  store i64 %34, ptr %7, align 8, !tbaa !9
  br label %23, !llvm.loop !16

35:                                               ; preds = %23
  %36 = load i64, ptr %7, align 8, !tbaa !9
  call void @_ZN7rioring9io_buffer11on_overflowEm(ptr noundef nonnull align 8 dereferenceable(40) %8, i64 noundef %36)
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  br label %9, !llvm.loop !18

37:                                               ; preds = %9
  %38 = getelementptr inbounds %"class.rioring::io_buffer", ptr %8, i32 0, i32 1
  %39 = load ptr, ptr %38, align 8, !tbaa !19
  %40 = getelementptr inbounds %"class.rioring::io_buffer", ptr %8, i32 0, i32 4
  %41 = load i64, ptr %40, align 8, !tbaa !11
  %42 = getelementptr inbounds i8, ptr %39, i64 %41
  %43 = load ptr, ptr %5, align 8, !tbaa !5
  %44 = load i64, ptr %6, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %42, ptr align 1 %43, i64 %44, i1 false)
  %45 = load i64, ptr %6, align 8, !tbaa !9
  %46 = getelementptr inbounds %"class.rioring::io_buffer", ptr %8, i32 0, i32 4
  %47 = load i64, ptr %46, align 8, !tbaa !11
  %48 = add i64 %47, %45
  store i64 %48, ptr %46, align 8, !tbaa !11
  ret void
}

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN7rioring9io_buffer11on_overflowEm(ptr noundef nonnull align 8 dereferenceable(40), i64 noundef) #3 align 2

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

attributes #0 = { nocallback nofree 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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 #4 = { 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 = !{!"long", !7, i64 0}
!11 = !{!12, !10, i64 32}
!12 = !{!"_ZTSN7rioring9io_bufferE", !13, i64 0, !6, i64 8, !10, i64 16, !10, i64 24, !10, i64 32}
!13 = !{!"_ZTSN7rioring11buffer_baseE"}
!14 = !{!12, !10, i64 16}
!15 = !{!12, !10, i64 24}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = distinct !{!18, !17}
!19 = !{!12, !6, i64 8}


; 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 nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_ZN7rioring9io_buffer4pushEPKhm(ptr noundef nonnull align 8 dereferenceable(40) %0, ptr nocapture noundef readonly %1, i64 noundef %2) unnamed_addr #1 align 2 {
  %4 = getelementptr inbounds i8, ptr %0, i64 32
  %5 = getelementptr inbounds i8, ptr %0, i64 16
  %6 = load i64, ptr %4, align 8, !tbaa !5
  %7 = add i64 %6, %2
  %8 = load i64, ptr %5, align 8, !tbaa !12
  %9 = icmp ugt i64 %7, %8
  br i1 %9, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %3
  %10 = getelementptr inbounds i8, ptr %0, i64 24
  br label %11

11:                                               ; preds = %.lr.ph, %16
  %12 = phi i64 [ %8, %.lr.ph ], [ %19, %16 ]
  %13 = phi i64 [ %7, %.lr.ph ], [ %18, %16 ]
  %14 = load i64, ptr %10, align 8, !tbaa !13
  br label %15

15:                                               ; preds = %15, %11
  %.pn = phi i64 [ %12, %11 ], [ %.0, %15 ]
  %.0 = add i64 %.pn, %14
  %.not = icmp ult i64 %13, %.0
  br i1 %.not, label %16, label %15, !llvm.loop !14

16:                                               ; preds = %15
  tail call void @_ZN7rioring9io_buffer11on_overflowEm(ptr noundef nonnull align 8 dereferenceable(40) %0, i64 noundef %.0)
  %17 = load i64, ptr %4, align 8, !tbaa !5
  %18 = add i64 %17, %2
  %19 = load i64, ptr %5, align 8, !tbaa !12
  %20 = icmp ugt i64 %18, %19
  br i1 %20, label %11, label %._crit_edge, !llvm.loop !16

._crit_edge:                                      ; preds = %16, %3
  %.lcssa = phi i64 [ %6, %3 ], [ %17, %16 ]
  %21 = getelementptr inbounds i8, ptr %0, i64 8
  %22 = load ptr, ptr %21, align 8, !tbaa !17
  %23 = getelementptr inbounds i8, ptr %22, i64 %.lcssa
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %23, ptr align 1 %1, i64 %2, i1 false)
  %24 = load i64, ptr %4, align 8, !tbaa !5
  %25 = add i64 %24, %2
  store i64 %25, ptr %4, align 8, !tbaa !5
  ret void
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN7rioring9io_buffer11on_overflowEm(ptr noundef nonnull align 8 dereferenceable(40), i64 noundef) local_unnamed_addr #1 align 2

attributes #0 = { mustprogress nocallback nofree 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" }

!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 32}
!6 = !{!"_ZTSN7rioring9io_bufferE", !7, i64 0, !8, i64 8, !11, i64 16, !11, i64 24, !11, i64 32}
!7 = !{!"_ZTSN7rioring11buffer_baseE"}
!8 = !{!"any pointer", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C++ TBAA"}
!11 = !{!"long", !9, i64 0}
!12 = !{!6, !11, i64 16}
!13 = !{!6, !11, i64 24}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}
!17 = !{!6, !8, i64 8}


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