; LLVM IR from TobiasKozel/VAE :: external/portaudio/src/common (pa_ringbuffer)

; 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.PaUtilRingBuffer = type { i64, i64, i64, i64, i64, i64, ptr }

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

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

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

; Function Attrs: nounwind uwtable
define dso_local i64 @PaUtil_GetRingBufferReadRegions(ptr noundef %0, i64 noundef %1, ptr noundef %2, ptr noundef %3, ptr noundef %4, ptr noundef %5) #0 {
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca ptr, align 8
  %13 = alloca i64, align 8
  %14 = alloca i64, align 8
  %15 = alloca i64, align 8
  store ptr %0, ptr %7, align 8, !tbaa !5
  store i64 %1, ptr %8, align 8, !tbaa !9
  store ptr %2, ptr %9, align 8, !tbaa !5
  store ptr %3, ptr %10, align 8, !tbaa !5
  store ptr %4, ptr %11, align 8, !tbaa !5
  store ptr %5, ptr %12, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #2
  %16 = load ptr, ptr %7, align 8, !tbaa !5
  %17 = call i64 @PaUtil_GetRingBufferReadAvailable(ptr noundef %16)
  store i64 %17, ptr %14, align 8, !tbaa !9
  %18 = load i64, ptr %8, align 8, !tbaa !9
  %19 = load i64, ptr %14, align 8, !tbaa !9
  %20 = icmp sgt i64 %18, %19
  br i1 %20, label %21, label %23

21:                                               ; preds = %6
  %22 = load i64, ptr %14, align 8, !tbaa !9
  store i64 %22, ptr %8, align 8, !tbaa !9
  br label %23

23:                                               ; preds = %21, %6
  %24 = load ptr, ptr %7, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.PaUtilRingBuffer, ptr %24, i32 0, i32 2
  %26 = load volatile i64, ptr %25, align 8, !tbaa !11
  %27 = load ptr, ptr %7, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.PaUtilRingBuffer, ptr %27, i32 0, i32 4
  %29 = load i64, ptr %28, align 8, !tbaa !13
  %30 = and i64 %26, %29
  store i64 %30, ptr %13, align 8, !tbaa !9
  %31 = load i64, ptr %13, align 8, !tbaa !9
  %32 = load i64, ptr %8, align 8, !tbaa !9
  %33 = add nsw i64 %31, %32
  %34 = load ptr, ptr %7, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.PaUtilRingBuffer, ptr %34, i32 0, i32 0
  %36 = load i64, ptr %35, align 8, !tbaa !14
  %37 = icmp sgt i64 %33, %36
  br i1 %37, label %38, label %65

38:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #2
  %39 = load ptr, ptr %7, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.PaUtilRingBuffer, ptr %39, i32 0, i32 0
  %41 = load i64, ptr %40, align 8, !tbaa !14
  %42 = load i64, ptr %13, align 8, !tbaa !9
  %43 = sub nsw i64 %41, %42
  store i64 %43, ptr %15, align 8, !tbaa !9
  %44 = load ptr, ptr %7, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.PaUtilRingBuffer, ptr %44, i32 0, i32 6
  %46 = load ptr, ptr %45, align 8, !tbaa !15
  %47 = load i64, ptr %13, align 8, !tbaa !9
  %48 = load ptr, ptr %7, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.PaUtilRingBuffer, ptr %48, i32 0, i32 5
  %50 = load i64, ptr %49, align 8, !tbaa !16
  %51 = mul nsw i64 %47, %50
  %52 = getelementptr inbounds i8, ptr %46, i64 %51
  %53 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %52, ptr %53, align 8, !tbaa !5
  %54 = load i64, ptr %15, align 8, !tbaa !9
  %55 = load ptr, ptr %10, align 8, !tbaa !5
  store i64 %54, ptr %55, align 8, !tbaa !9
  %56 = load ptr, ptr %7, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.PaUtilRingBuffer, ptr %56, i32 0, i32 6
  %58 = load ptr, ptr %57, align 8, !tbaa !15
  %59 = getelementptr inbounds i8, ptr %58, i64 0
  %60 = load ptr, ptr %11, align 8, !tbaa !5
  store ptr %59, ptr %60, align 8, !tbaa !5
  %61 = load i64, ptr %8, align 8, !tbaa !9
  %62 = load i64, ptr %15, align 8, !tbaa !9
  %63 = sub nsw i64 %61, %62
  %64 = load ptr, ptr %12, align 8, !tbaa !5
  store i64 %63, ptr %64, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #2
  br label %80

65:                                               ; preds = %23
  %66 = load ptr, ptr %7, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.PaUtilRingBuffer, ptr %66, i32 0, i32 6
  %68 = load ptr, ptr %67, align 8, !tbaa !15
  %69 = load i64, ptr %13, align 8, !tbaa !9
  %70 = load ptr, ptr %7, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.PaUtilRingBuffer, ptr %70, i32 0, i32 5
  %72 = load i64, ptr %71, align 8, !tbaa !16
  %73 = mul nsw i64 %69, %72
  %74 = getelementptr inbounds i8, ptr %68, i64 %73
  %75 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %74, ptr %75, align 8, !tbaa !5
  %76 = load i64, ptr %8, align 8, !tbaa !9
  %77 = load ptr, ptr %10, align 8, !tbaa !5
  store i64 %76, ptr %77, align 8, !tbaa !9
  %78 = load ptr, ptr %11, align 8, !tbaa !5
  store ptr null, ptr %78, align 8, !tbaa !5
  %79 = load ptr, ptr %12, align 8, !tbaa !5
  store i64 0, ptr %79, align 8, !tbaa !9
  br label %80

80:                                               ; preds = %65, %38
  %81 = load i64, ptr %14, align 8, !tbaa !9
  %82 = icmp ne i64 %81, 0
  br i1 %82, label %83, label %84

83:                                               ; preds = %80
  fence seq_cst
  br label %84

84:                                               ; preds = %83, %80
  %85 = load i64, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #2
  ret i64 %85
}

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 willreturn memory(argmem: readwrite) }
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 = !{!"long", !7, i64 0}
!11 = !{!12, !10, i64 16}
!12 = !{!"PaUtilRingBuffer", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !10, i64 40, !6, i64 48}
!13 = !{!12, !10, i64 32}
!14 = !{!12, !10, i64 0}
!15 = !{!12, !6, i64 48}
!16 = !{!12, !10, i64 40}


; 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
declare dso_local i64 @PaUtil_GetRingBufferReadAvailable(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i64 @PaUtil_GetRingBufferReadRegions(ptr noundef %0, i64 noundef %1, ptr nocapture noundef writeonly %2, ptr nocapture noundef writeonly %3, ptr nocapture noundef writeonly %4, ptr nocapture noundef writeonly %5) local_unnamed_addr #0 {
  %7 = tail call i64 @PaUtil_GetRingBufferReadAvailable(ptr noundef %0)
  %spec.select = tail call i64 @llvm.smin.i64(i64 %7, i64 %1)
  %8 = getelementptr inbounds i8, ptr %0, i64 16
  %9 = load volatile i64, ptr %8, align 8, !tbaa !5
  %10 = getelementptr inbounds i8, ptr %0, i64 32
  %11 = load i64, ptr %10, align 8, !tbaa !11
  %12 = and i64 %11, %9
  %13 = add nsw i64 %12, %spec.select
  %14 = load i64, ptr %0, align 8, !tbaa !12
  %15 = icmp sgt i64 %13, %14
  br i1 %15, label %16, label %26

16:                                               ; preds = %6
  %17 = sub nsw i64 %14, %12
  %18 = getelementptr inbounds i8, ptr %0, i64 48
  %19 = load ptr, ptr %18, align 8, !tbaa !13
  %20 = getelementptr inbounds i8, ptr %0, i64 40
  %21 = load i64, ptr %20, align 8, !tbaa !14
  %22 = mul nsw i64 %21, %12
  %23 = getelementptr inbounds i8, ptr %19, i64 %22
  store ptr %23, ptr %2, align 8, !tbaa !15
  %24 = load ptr, ptr %18, align 8, !tbaa !13
  %25 = sub nsw i64 %spec.select, %17
  br label %33

26:                                               ; preds = %6
  %27 = getelementptr inbounds i8, ptr %0, i64 48
  %28 = load ptr, ptr %27, align 8, !tbaa !13
  %29 = getelementptr inbounds i8, ptr %0, i64 40
  %30 = load i64, ptr %29, align 8, !tbaa !14
  %31 = mul nsw i64 %30, %12
  %32 = getelementptr inbounds i8, ptr %28, i64 %31
  store ptr %32, ptr %2, align 8, !tbaa !15
  br label %33

33:                                               ; preds = %26, %16
  %spec.select.sink = phi i64 [ %17, %16 ], [ %spec.select, %26 ]
  %.sink = phi ptr [ %24, %16 ], [ null, %26 ]
  %storemerge = phi i64 [ %25, %16 ], [ 0, %26 ]
  store i64 %spec.select.sink, ptr %3, align 8
  store ptr %.sink, ptr %4, align 8
  store i64 %storemerge, ptr %5, align 8, !tbaa !16
  %.not = icmp eq i64 %7, 0
  br i1 %.not, label %35, label %34

34:                                               ; preds = %33
  fence seq_cst
  br label %35

35:                                               ; preds = %34, %33
  ret i64 %spec.select
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.smin.i64(i64, i64) #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 16}
!6 = !{!"PaUtilRingBuffer", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !7, i64 32, !7, i64 40, !10, i64 48}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !7, i64 32}
!12 = !{!6, !7, i64 0}
!13 = !{!6, !10, i64 48}
!14 = !{!6, !7, i64 40}
!15 = !{!10, !10, i64 0}
!16 = !{!7, !7, i64 0}


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