; LLVM IR from Ryuk17/SpeechAlgorithms :: WebRTC_AEC/src (ring_buffer)

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

%struct.RingBuffer = type { i64, i64, i64, i64, i32, 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: 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) #1

; Function Attrs: nounwind uwtable
define dso_local i64 @WebRtc_WriteBuffer(ptr noundef %0, ptr noundef %1, i64 noundef %2) #2 {
  %4 = alloca i64, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca i64, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i64 %2, ptr %7, align 8, !tbaa !9
  %12 = load ptr, ptr %5, align 8, !tbaa !5
  %13 = icmp ne ptr %12, null
  br i1 %13, label %15, label %14

14:                                               ; preds = %3
  store i64 0, ptr %4, align 8
  br label %99

15:                                               ; preds = %3
  %16 = load ptr, ptr %6, align 8, !tbaa !5
  %17 = icmp ne ptr %16, null
  br i1 %17, label %19, label %18

18:                                               ; preds = %15
  store i64 0, ptr %4, align 8
  br label %99

19:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %20 = load ptr, ptr %5, align 8, !tbaa !5
  %21 = call i64 @WebRtc_available_write(ptr noundef %20)
  store i64 %21, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %22 = load i64, ptr %8, align 8, !tbaa !9
  %23 = load i64, ptr %7, align 8, !tbaa !9
  %24 = icmp ult i64 %22, %23
  br i1 %24, label %25, label %27

25:                                               ; preds = %19
  %26 = load i64, ptr %8, align 8, !tbaa !9
  br label %29

27:                                               ; preds = %19
  %28 = load i64, ptr %7, align 8, !tbaa !9
  br label %29

29:                                               ; preds = %27, %25
  %30 = phi i64 [ %26, %25 ], [ %28, %27 ]
  store i64 %30, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %31 = load i64, ptr %9, align 8, !tbaa !9
  store i64 %31, ptr %10, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  %32 = load ptr, ptr %5, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.RingBuffer, ptr %32, i32 0, i32 2
  %34 = load i64, ptr %33, align 8, !tbaa !11
  %35 = load ptr, ptr %5, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.RingBuffer, ptr %35, i32 0, i32 1
  %37 = load i64, ptr %36, align 8, !tbaa !14
  %38 = sub i64 %34, %37
  store i64 %38, ptr %11, align 8, !tbaa !9
  %39 = load i64, ptr %9, align 8, !tbaa !9
  %40 = load i64, ptr %11, align 8, !tbaa !9
  %41 = icmp ugt i64 %39, %40
  br i1 %41, label %42, label %67

42:                                               ; preds = %29
  %43 = load ptr, ptr %5, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.RingBuffer, ptr %43, i32 0, i32 5
  %45 = load ptr, ptr %44, align 8, !tbaa !15
  %46 = load ptr, ptr %5, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.RingBuffer, ptr %46, i32 0, i32 1
  %48 = load i64, ptr %47, align 8, !tbaa !14
  %49 = load ptr, ptr %5, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.RingBuffer, ptr %49, i32 0, i32 3
  %51 = load i64, ptr %50, align 8, !tbaa !16
  %52 = mul i64 %48, %51
  %53 = getelementptr inbounds i8, ptr %45, i64 %52
  %54 = load ptr, ptr %6, align 8, !tbaa !5
  %55 = load i64, ptr %11, align 8, !tbaa !9
  %56 = load ptr, ptr %5, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.RingBuffer, ptr %56, i32 0, i32 3
  %58 = load i64, ptr %57, align 8, !tbaa !16
  %59 = mul i64 %55, %58
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %53, ptr align 1 %54, i64 %59, i1 false)
  %60 = load ptr, ptr %5, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.RingBuffer, ptr %60, i32 0, i32 1
  store i64 0, ptr %61, align 8, !tbaa !14
  %62 = load i64, ptr %11, align 8, !tbaa !9
  %63 = load i64, ptr %10, align 8, !tbaa !9
  %64 = sub i64 %63, %62
  store i64 %64, ptr %10, align 8, !tbaa !9
  %65 = load ptr, ptr %5, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.RingBuffer, ptr %65, i32 0, i32 4
  store i32 1, ptr %66, align 8, !tbaa !17
  br label %67

67:                                               ; preds = %42, %29
  %68 = load ptr, ptr %5, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.RingBuffer, ptr %68, i32 0, i32 5
  %70 = load ptr, ptr %69, align 8, !tbaa !15
  %71 = load ptr, ptr %5, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.RingBuffer, ptr %71, i32 0, i32 1
  %73 = load i64, ptr %72, align 8, !tbaa !14
  %74 = load ptr, ptr %5, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.RingBuffer, ptr %74, i32 0, i32 3
  %76 = load i64, ptr %75, align 8, !tbaa !16
  %77 = mul i64 %73, %76
  %78 = getelementptr inbounds i8, ptr %70, i64 %77
  %79 = load ptr, ptr %6, align 8, !tbaa !5
  %80 = load i64, ptr %9, align 8, !tbaa !9
  %81 = load i64, ptr %10, align 8, !tbaa !9
  %82 = sub i64 %80, %81
  %83 = load ptr, ptr %5, align 8, !tbaa !5
  %84 = getelementptr inbounds %struct.RingBuffer, ptr %83, i32 0, i32 3
  %85 = load i64, ptr %84, align 8, !tbaa !16
  %86 = mul i64 %82, %85
  %87 = getelementptr inbounds i8, ptr %79, i64 %86
  %88 = load i64, ptr %10, align 8, !tbaa !9
  %89 = load ptr, ptr %5, align 8, !tbaa !5
  %90 = getelementptr inbounds %struct.RingBuffer, ptr %89, i32 0, i32 3
  %91 = load i64, ptr %90, align 8, !tbaa !16
  %92 = mul i64 %88, %91
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %78, ptr align 1 %87, i64 %92, i1 false)
  %93 = load i64, ptr %10, align 8, !tbaa !9
  %94 = load ptr, ptr %5, align 8, !tbaa !5
  %95 = getelementptr inbounds %struct.RingBuffer, ptr %94, i32 0, i32 1
  %96 = load i64, ptr %95, align 8, !tbaa !14
  %97 = add i64 %96, %93
  store i64 %97, ptr %95, align 8, !tbaa !14
  %98 = load i64, ptr %9, align 8, !tbaa !9
  store i64 %98, ptr %4, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  br label %99

99:                                               ; preds = %67, %18, %14
  %100 = load i64, ptr %4, align 8
  ret i64 %100
}

; Function Attrs: nounwind uwtable
declare dso_local i64 @WebRtc_available_write(ptr noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !10, i64 16}
!12 = !{!"RingBuffer", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !13, i64 32, !6, i64 40}
!13 = !{!"int", !7, i64 0}
!14 = !{!12, !10, i64 8}
!15 = !{!12, !6, i64 40}
!16 = !{!12, !10, i64 24}
!17 = !{!12, !13, i64 32}


; 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: nounwind uwtable
define dso_local i64 @WebRtc_WriteBuffer(ptr noundef %0, ptr noundef readonly %1, i64 noundef %2) local_unnamed_addr #1 {
  %.not = icmp eq ptr %0, null
  %.not39 = icmp eq ptr %1, null
  %or.cond = or i1 %.not, %.not39
  br i1 %or.cond, label %37, label %4

4:                                                ; preds = %3
  %5 = tail call i64 @WebRtc_available_write(ptr noundef nonnull %0)
  %6 = tail call i64 @llvm.umin.i64(i64 %5, i64 %2)
  %7 = getelementptr inbounds i8, ptr %0, i64 16
  %8 = load i64, ptr %7, align 8, !tbaa !5
  %9 = getelementptr inbounds i8, ptr %0, i64 8
  %10 = load i64, ptr %9, align 8, !tbaa !12
  %11 = sub i64 %8, %10
  %12 = icmp ugt i64 %6, %11
  br i1 %12, label %13, label %23

13:                                               ; preds = %4
  %14 = getelementptr inbounds i8, ptr %0, i64 40
  %15 = load ptr, ptr %14, align 8, !tbaa !13
  %16 = getelementptr inbounds i8, ptr %0, i64 24
  %17 = load i64, ptr %16, align 8, !tbaa !14
  %18 = mul i64 %17, %10
  %19 = getelementptr inbounds i8, ptr %15, i64 %18
  %20 = mul i64 %17, %11
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %19, ptr nonnull align 1 %1, i64 %20, i1 false)
  store i64 0, ptr %9, align 8, !tbaa !12
  %21 = sub i64 %6, %11
  %22 = getelementptr inbounds i8, ptr %0, i64 32
  store i32 1, ptr %22, align 8, !tbaa !15
  br label %23

23:                                               ; preds = %13, %4
  %24 = phi i64 [ 0, %13 ], [ %10, %4 ]
  %.034 = phi i64 [ %21, %13 ], [ %6, %4 ]
  %25 = getelementptr inbounds i8, ptr %0, i64 40
  %26 = load ptr, ptr %25, align 8, !tbaa !13
  %27 = getelementptr inbounds i8, ptr %0, i64 24
  %28 = load i64, ptr %27, align 8, !tbaa !14
  %29 = mul i64 %28, %24
  %30 = getelementptr inbounds i8, ptr %26, i64 %29
  %31 = sub i64 %6, %.034
  %32 = mul i64 %28, %31
  %33 = getelementptr inbounds i8, ptr %1, i64 %32
  %34 = mul i64 %28, %.034
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %30, ptr nonnull align 1 %33, i64 %34, i1 false)
  %35 = load i64, ptr %9, align 8, !tbaa !12
  %36 = add i64 %35, %.034
  store i64 %36, ptr %9, align 8, !tbaa !12
  br label %37

37:                                               ; preds = %3, %23
  %.0 = phi i64 [ %6, %23 ], [ 0, %3 ]
  ret i64 %.0
}

; Function Attrs: nounwind uwtable
declare dso_local i64 @WebRtc_available_write(ptr noundef) local_unnamed_addr #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umin.i64(i64, i64) #2

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

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


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