; 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: nounwind uwtable
define dso_local i32 @WebRtc_MoveReadPtr(ptr noundef %0, i32 noundef %1) #1 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = icmp ne ptr %9, null
  br i1 %10, label %12, label %11

11:                                               ; preds = %2
  store i32 0, ptr %3, align 4
  br label %73

12:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %13 = load ptr, ptr %4, align 8, !tbaa !5
  %14 = call i64 @WebRtc_available_write(ptr noundef %13)
  %15 = trunc i64 %14 to i32
  store i32 %15, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = call i64 @WebRtc_available_read(ptr noundef %16)
  %18 = trunc i64 %17 to i32
  store i32 %18, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %19 = load ptr, ptr %4, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.RingBuffer, ptr %19, i32 0, i32 0
  %21 = load i64, ptr %20, align 8, !tbaa !11
  %22 = trunc i64 %21 to i32
  store i32 %22, ptr %8, align 4, !tbaa !9
  %23 = load i32, ptr %5, align 4, !tbaa !9
  %24 = load i32, ptr %7, align 4, !tbaa !9
  %25 = icmp sgt i32 %23, %24
  br i1 %25, label %26, label %28

26:                                               ; preds = %12
  %27 = load i32, ptr %7, align 4, !tbaa !9
  store i32 %27, ptr %5, align 4, !tbaa !9
  br label %28

28:                                               ; preds = %26, %12
  %29 = load i32, ptr %5, align 4, !tbaa !9
  %30 = load i32, ptr %6, align 4, !tbaa !9
  %31 = sub nsw i32 0, %30
  %32 = icmp slt i32 %29, %31
  br i1 %32, label %33, label %36

33:                                               ; preds = %28
  %34 = load i32, ptr %6, align 4, !tbaa !9
  %35 = sub nsw i32 0, %34
  store i32 %35, ptr %5, align 4, !tbaa !9
  br label %36

36:                                               ; preds = %33, %28
  %37 = load i32, ptr %5, align 4, !tbaa !9
  %38 = load i32, ptr %8, align 4, !tbaa !9
  %39 = add nsw i32 %38, %37
  store i32 %39, ptr %8, align 4, !tbaa !9
  %40 = load i32, ptr %8, align 4, !tbaa !9
  %41 = load ptr, ptr %4, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.RingBuffer, ptr %41, i32 0, i32 2
  %43 = load i64, ptr %42, align 8, !tbaa !14
  %44 = trunc i64 %43 to i32
  %45 = icmp sgt i32 %40, %44
  br i1 %45, label %46, label %55

46:                                               ; preds = %36
  %47 = load ptr, ptr %4, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.RingBuffer, ptr %47, i32 0, i32 2
  %49 = load i64, ptr %48, align 8, !tbaa !14
  %50 = trunc i64 %49 to i32
  %51 = load i32, ptr %8, align 4, !tbaa !9
  %52 = sub nsw i32 %51, %50
  store i32 %52, ptr %8, align 4, !tbaa !9
  %53 = load ptr, ptr %4, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.RingBuffer, ptr %53, i32 0, i32 4
  store i32 0, ptr %54, align 8, !tbaa !15
  br label %55

55:                                               ; preds = %46, %36
  %56 = load i32, ptr %8, align 4, !tbaa !9
  %57 = icmp slt i32 %56, 0
  br i1 %57, label %58, label %67

58:                                               ; preds = %55
  %59 = load ptr, ptr %4, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.RingBuffer, ptr %59, i32 0, i32 2
  %61 = load i64, ptr %60, align 8, !tbaa !14
  %62 = trunc i64 %61 to i32
  %63 = load i32, ptr %8, align 4, !tbaa !9
  %64 = add nsw i32 %63, %62
  store i32 %64, ptr %8, align 4, !tbaa !9
  %65 = load ptr, ptr %4, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.RingBuffer, ptr %65, i32 0, i32 4
  store i32 1, ptr %66, align 8, !tbaa !15
  br label %67

67:                                               ; preds = %58, %55
  %68 = load i32, ptr %8, align 4, !tbaa !9
  %69 = sext i32 %68 to i64
  %70 = load ptr, ptr %4, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.RingBuffer, ptr %70, i32 0, i32 0
  store i64 %69, ptr %71, align 8, !tbaa !11
  %72 = load i32, ptr %5, align 4, !tbaa !9
  store i32 %72, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  br label %73

73:                                               ; preds = %67, %11
  %74 = load i32, ptr %3, align 4
  ret i32 %74
}

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

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

attributes #0 = { nocallback nofree nosync 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 = { 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 = !{!"int", !7, i64 0}
!11 = !{!12, !13, i64 0}
!12 = !{!"RingBuffer", !13, i64 0, !13, i64 8, !13, i64 16, !13, i64 24, !10, i64 32, !6, i64 40}
!13 = !{!"long", !7, i64 0}
!14 = !{!12, !13, i64 16}
!15 = !{!12, !10, 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: nounwind uwtable
define dso_local range(i32 -2147483647, -2147483648) i32 @WebRtc_MoveReadPtr(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %26, label %3

3:                                                ; preds = %2
  %4 = tail call i64 @WebRtc_available_write(ptr noundef nonnull %0)
  %5 = trunc i64 %4 to i32
  %6 = tail call i64 @WebRtc_available_read(ptr noundef nonnull %0)
  %7 = trunc i64 %6 to i32
  %8 = load i64, ptr %0, align 8, !tbaa !5
  %9 = trunc i64 %8 to i32
  %spec.select = tail call i32 @llvm.smin.i32(i32 %7, i32 %1)
  %10 = sub nsw i32 0, %5
  %.126 = tail call i32 @llvm.smax.i32(i32 %spec.select, i32 %10)
  %11 = add nsw i32 %.126, %9
  %12 = getelementptr inbounds i8, ptr %0, i64 16
  %13 = load i64, ptr %12, align 8, !tbaa !12
  %14 = trunc i64 %13 to i32
  %15 = icmp sgt i32 %11, %14
  br i1 %15, label %16, label %19

16:                                               ; preds = %3
  %17 = sub nsw i32 %11, %14
  %18 = getelementptr inbounds i8, ptr %0, i64 32
  store i32 0, ptr %18, align 8, !tbaa !13
  br label %19

19:                                               ; preds = %16, %3
  %.0 = phi i32 [ %17, %16 ], [ %11, %3 ]
  %20 = icmp slt i32 %.0, 0
  br i1 %20, label %21, label %24

21:                                               ; preds = %19
  %22 = add nsw i32 %.0, %14
  %23 = getelementptr inbounds i8, ptr %0, i64 32
  store i32 1, ptr %23, align 8, !tbaa !13
  br label %24

24:                                               ; preds = %21, %19
  %.1 = phi i32 [ %22, %21 ], [ %.0, %19 ]
  %25 = sext i32 %.1 to i64
  store i64 %25, ptr %0, align 8, !tbaa !5
  br label %26

26:                                               ; preds = %2, %24
  %.024 = phi i32 [ %.126, %24 ], [ 0, %2 ]
  ret i32 %.024
}

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

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smin.i32(i32, i32) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #1

attributes #0 = { 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 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 0}
!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 16}
!13 = !{!6, !10, i64 32}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,SROAPass,SimplifyCFGPass,TailCallElimPass