; 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
define dso_local i64 @PaUtil_InitializeRingBuffer(ptr noundef %0, i64 noundef %1, i64 noundef %2, ptr noundef %3) #0 {
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca ptr, align 8
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i64 %1, ptr %7, align 8, !tbaa !9
  store i64 %2, ptr %8, align 8, !tbaa !9
  store ptr %3, ptr %9, align 8, !tbaa !5
  %10 = load i64, ptr %8, align 8, !tbaa !9
  %11 = sub nsw i64 %10, 1
  %12 = load i64, ptr %8, align 8, !tbaa !9
  %13 = and i64 %11, %12
  %14 = icmp ne i64 %13, 0
  br i1 %14, label %15, label %16

15:                                               ; preds = %4
  store i64 -1, ptr %5, align 8
  br label %36

16:                                               ; preds = %4
  %17 = load i64, ptr %8, align 8, !tbaa !9
  %18 = load ptr, ptr %6, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.PaUtilRingBuffer, ptr %18, i32 0, i32 0
  store i64 %17, ptr %19, align 8, !tbaa !11
  %20 = load ptr, ptr %9, align 8, !tbaa !5
  %21 = load ptr, ptr %6, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.PaUtilRingBuffer, ptr %21, i32 0, i32 6
  store ptr %20, ptr %22, align 8, !tbaa !13
  %23 = load ptr, ptr %6, align 8, !tbaa !5
  call void @PaUtil_FlushRingBuffer(ptr noundef %23)
  %24 = load i64, ptr %8, align 8, !tbaa !9
  %25 = mul nsw i64 %24, 2
  %26 = sub nsw i64 %25, 1
  %27 = load ptr, ptr %6, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.PaUtilRingBuffer, ptr %27, i32 0, i32 3
  store i64 %26, ptr %28, align 8, !tbaa !14
  %29 = load i64, ptr %8, align 8, !tbaa !9
  %30 = sub nsw i64 %29, 1
  %31 = load ptr, ptr %6, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.PaUtilRingBuffer, ptr %31, i32 0, i32 4
  store i64 %30, ptr %32, align 8, !tbaa !15
  %33 = load i64, ptr %7, align 8, !tbaa !9
  %34 = load ptr, ptr %6, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.PaUtilRingBuffer, ptr %34, i32 0, i32 5
  store i64 %33, ptr %35, align 8, !tbaa !16
  store i64 0, ptr %5, align 8
  br label %36

36:                                               ; preds = %16, %15
  %37 = load i64, ptr %5, align 8
  ret i64 %37
}

; Function Attrs: nounwind uwtable
declare dso_local void @PaUtil_FlushRingBuffer(ptr noundef) #0

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

!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 0}
!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, !6, i64 48}
!14 = !{!12, !10, i64 24}
!15 = !{!12, !10, i64 32}
!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
define dso_local range(i64 -1, 1) i64 @PaUtil_InitializeRingBuffer(ptr noundef %0, i64 noundef %1, i64 noundef %2, ptr noundef %3) local_unnamed_addr #0 {
  %5 = tail call range(i64 0, 65) i64 @llvm.ctpop.i64(i64 %2)
  %.not = icmp ult i64 %5, 2
  br i1 %.not, label %6, label %14

6:                                                ; preds = %4
  %7 = add nsw i64 %2, -1
  store i64 %2, ptr %0, align 8, !tbaa !5
  %8 = getelementptr inbounds i8, ptr %0, i64 48
  store ptr %3, ptr %8, align 8, !tbaa !11
  tail call void @PaUtil_FlushRingBuffer(ptr noundef nonnull %0)
  %9 = shl nsw i64 %2, 1
  %10 = add nsw i64 %9, -1
  %11 = getelementptr inbounds i8, ptr %0, i64 24
  store i64 %10, ptr %11, align 8, !tbaa !12
  %12 = getelementptr inbounds i8, ptr %0, i64 32
  store i64 %7, ptr %12, align 8, !tbaa !13
  %13 = getelementptr inbounds i8, ptr %0, i64 40
  store i64 %1, ptr %13, align 8, !tbaa !14
  br label %14

14:                                               ; preds = %4, %6
  %.0 = phi i64 [ 0, %6 ], [ -1, %4 ]
  ret i64 %.0
}

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.ctpop.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 0}
!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, !10, i64 48}
!12 = !{!6, !7, i64 24}
!13 = !{!6, !7, i64 32}
!14 = !{!6, !7, i64 40}


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