; LLVM IR from Ultrawipf/OpenFFBoard :: Firmware/FFBoard/Src/ringbufferwrapper.ll (ZN17RingBufferWrapperC2Ej)

; 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.RingBufferWrapper = type <{ ptr, %struct.RingBuffer, i8, [7 x i8] }>
%struct.RingBuffer = type { i64, i64, i64, ptr }

@_ZTV17RingBufferWrapper = external dso_local unnamed_addr constant { [4 x ptr] }, align 8

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare hidden noundef i32 @_ZL13isMultipleTwom(i64 noundef) #0

declare i32 @__gxx_personality_v0(...)

; Function Attrs: noinline noreturn nounwind uwtable
declare hidden void @__clang_call_terminate(ptr noundef) #1

declare i32 @ringBufferInit(ptr noundef, ptr noundef, i64 noundef) #2

; Function Attrs: nobuiltin nounwind
declare void @_ZdaPv(ptr noundef) #3

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_ZN17RingBufferWrapperC2Ej(ptr noundef nonnull align 8 dereferenceable(41) %0, i32 noundef %1) unnamed_addr #4 align 2 personality ptr @__gxx_personality_v0 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  %6 = load ptr, ptr %3, align 8
  store ptr getelementptr inbounds inrange(-16, 16) ({ [4 x ptr] }, ptr @_ZTV17RingBufferWrapper, i32 0, i32 0, i32 2), ptr %6, align 8, !tbaa !11
  %7 = getelementptr inbounds %class.RingBufferWrapper, ptr %6, i32 0, i32 1
  %8 = getelementptr inbounds %class.RingBufferWrapper, ptr %6, i32 0, i32 2
  store i8 0, ptr %8, align 8, !tbaa !13
  %9 = load i32, ptr %4, align 4, !tbaa !9
  %10 = zext i32 %9 to i64
  %11 = invoke noundef i32 @_ZL13isMultipleTwom(i64 noundef %10)
          to label %12 unwind label %41

12:                                               ; preds = %2
  %13 = icmp ne i32 %11, 0
  br i1 %13, label %15, label %14

14:                                               ; preds = %12
  br label %40

15:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #7
  %16 = load i32, ptr %4, align 4, !tbaa !9
  %17 = zext i32 %16 to i64
  %18 = invoke noalias noundef nonnull ptr @_Znam(i64 noundef %17) #8
          to label %19 unwind label %41

19:                                               ; preds = %15
  store ptr %18, ptr %5, align 8, !tbaa !5
  %20 = getelementptr inbounds %class.RingBufferWrapper, ptr %6, i32 0, i32 1
  %21 = load ptr, ptr %5, align 8, !tbaa !5
  %22 = load i32, ptr %4, align 4, !tbaa !9
  %23 = zext i32 %22 to i64
  %24 = invoke i32 @ringBufferInit(ptr noundef %20, ptr noundef %21, i64 noundef %23)
          to label %25 unwind label %41

25:                                               ; preds = %19
  %26 = icmp ne i32 %24, 0
  %27 = getelementptr inbounds %class.RingBufferWrapper, ptr %6, i32 0, i32 2
  %28 = zext i1 %26 to i8
  store i8 %28, ptr %27, align 8, !tbaa !13
  %29 = getelementptr inbounds %class.RingBufferWrapper, ptr %6, i32 0, i32 2
  %30 = load i8, ptr %29, align 8, !tbaa !13, !range !18, !noundef !19
  %31 = trunc i8 %30 to i1
  br i1 %31, label %39, label %32

32:                                               ; preds = %25
  %33 = load ptr, ptr %5, align 8, !tbaa !5
  %34 = icmp eq ptr %33, null
  br i1 %34, label %36, label %35

35:                                               ; preds = %32
  call void @_ZdaPv(ptr noundef %33) #9
  br label %36

36:                                               ; preds = %35, %32
  %37 = getelementptr inbounds %class.RingBufferWrapper, ptr %6, i32 0, i32 1
  %38 = getelementptr inbounds %struct.RingBuffer, ptr %37, i32 0, i32 3
  store ptr null, ptr %38, align 8, !tbaa !20
  br label %39

39:                                               ; preds = %36, %25
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #7
  br label %40

40:                                               ; preds = %39, %14
  ret void

41:                                               ; preds = %19, %15, %2
  %42 = landingpad { ptr, i32 }
          catch ptr null
  %43 = extractvalue { ptr, i32 } %42, 0
  call void @__clang_call_terminate(ptr %43) #10
  unreachable
}

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

; Function Attrs: nobuiltin allocsize(0)
declare noundef nonnull ptr @_Znam(i64 noundef) #6

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

attributes #0 = { inlinehint 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 #1 = { noinline noreturn nounwind uwtable "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 = { "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 = { nobuiltin nounwind "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 = { 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 #5 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #6 = { nobuiltin allocsize(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 #7 = { nounwind }
attributes #8 = { builtin allocsize(0) }
attributes #9 = { builtin nounwind }
attributes #10 = { noreturn 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 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"vtable pointer", !8, i64 0}
!13 = !{!14, !17, i64 40}
!14 = !{!"_ZTS17RingBufferWrapper", !15, i64 8, !17, i64 40}
!15 = !{!"_ZTS10RingBuffer", !16, i64 0, !16, i64 8, !16, i64 16, !6, i64 24}
!16 = !{!"long", !7, i64 0}
!17 = !{!"bool", !7, i64 0}
!18 = !{i8 0, i8 2}
!19 = !{}
!20 = !{!14, !6, 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"

@_ZTV17RingBufferWrapper = external dso_local unnamed_addr constant { [4 x ptr] }, align 8

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare hidden noundef i32 @_ZL13isMultipleTwom(i64 noundef) local_unnamed_addr #0

declare i32 @__gxx_personality_v0(...)

; Function Attrs: noinline noreturn nounwind uwtable
declare hidden void @__clang_call_terminate(ptr noundef) local_unnamed_addr #1

declare i32 @ringBufferInit(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #2

; Function Attrs: nobuiltin nounwind
declare void @_ZdaPv(ptr noundef) local_unnamed_addr #3

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_ZN17RingBufferWrapperC2Ej(ptr noundef nonnull align 8 dereferenceable(41) %0, i32 noundef %1) unnamed_addr #4 align 2 personality ptr @__gxx_personality_v0 {
  store ptr getelementptr inbounds inrange(-16, 16) (i8, ptr @_ZTV17RingBufferWrapper, i64 16), ptr %0, align 8, !tbaa !5
  %3 = getelementptr inbounds i8, ptr %0, i64 40
  store i8 0, ptr %3, align 8, !tbaa !8
  %4 = zext i32 %1 to i64
  %5 = tail call noundef i32 @_ZL13isMultipleTwom(i64 noundef %4)
  %.not = icmp eq i32 %5, 0
  br i1 %.not, label %16, label %6

6:                                                ; preds = %2
  %7 = invoke noalias noundef nonnull ptr @_Znam(i64 noundef %4) #6
          to label %8 unwind label %17

8:                                                ; preds = %6
  %9 = getelementptr inbounds i8, ptr %0, i64 8
  %10 = invoke i32 @ringBufferInit(ptr noundef nonnull %9, ptr noundef nonnull %7, i64 noundef %4)
          to label %11 unwind label %17

11:                                               ; preds = %8
  %12 = icmp ne i32 %10, 0
  %13 = zext i1 %12 to i8
  store i8 %13, ptr %3, align 8, !tbaa !8
  br i1 %12, label %16, label %14

14:                                               ; preds = %11
  tail call void @_ZdaPv(ptr noundef nonnull %7) #7
  %15 = getelementptr inbounds i8, ptr %0, i64 32
  store ptr null, ptr %15, align 8, !tbaa !15
  br label %16

16:                                               ; preds = %11, %14, %2
  ret void

17:                                               ; preds = %8, %6
  %18 = landingpad { ptr, i32 }
          catch ptr null
  %19 = extractvalue { ptr, i32 } %18, 0
  tail call void @__clang_call_terminate(ptr %19) #8
  unreachable
}

; Function Attrs: nobuiltin allocsize(0)
declare noundef nonnull ptr @_Znam(i64 noundef) local_unnamed_addr #5

attributes #0 = { inlinehint 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 #1 = { noinline noreturn nounwind uwtable "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 = { "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 = { nobuiltin nounwind "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 = { 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 #5 = { nobuiltin allocsize(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 #6 = { builtin allocsize(0) }
attributes #7 = { builtin nounwind }
attributes #8 = { noreturn 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 = !{!"vtable pointer", !7, i64 0}
!7 = !{!"Simple C++ TBAA"}
!8 = !{!9, !14, i64 40}
!9 = !{!"_ZTS17RingBufferWrapper", !10, i64 8, !14, i64 40}
!10 = !{!"_ZTS10RingBuffer", !11, i64 0, !11, i64 8, !11, i64 16, !13, i64 24}
!11 = !{!"long", !12, i64 0}
!12 = !{!"omnipotent char", !7, i64 0}
!13 = !{!"any pointer", !12, i64 0}
!14 = !{!"bool", !12, i64 0}
!15 = !{!9, !13, i64 32}


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