; LLVM IR from GPUOpen-LibrariesAndSDKs/TAN :: tan/common/fifo.ll (ZN10FifoBuffer11retrieveSegEj)

; 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.FifoBuffer = type <{ ptr, i32, i32, i32, [4 x i8] }>

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZN10FifoBuffer10fifoLengthEv(ptr noundef nonnull align 8 dereferenceable(20)) #0 align 2

; 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: mustprogress nounwind uwtable
define dso_local noundef zeroext i1 @_ZN10FifoBuffer11retrieveSegEj(ptr noundef nonnull align 8 dereferenceable(20) %0, i32 noundef %1) #0 align 2 {
  %3 = alloca i1, align 1
  %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
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %10 = call noundef i32 @_ZN10FifoBuffer10fifoLengthEv(ptr noundef nonnull align 8 dereferenceable(20) %9)
  store i32 %10, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %11 = getelementptr inbounds %class.FifoBuffer, ptr %9, i32 0, i32 1
  %12 = load i32, ptr %11, align 8, !tbaa !11
  %13 = getelementptr inbounds %class.FifoBuffer, ptr %9, i32 0, i32 2
  %14 = load i32, ptr %13, align 4, !tbaa !13
  %15 = sub i32 %12, %14
  store i32 %15, ptr %7, align 4, !tbaa !9
  %16 = load i32, ptr %7, align 4, !tbaa !9
  %17 = load i32, ptr %6, align 4, !tbaa !9
  %18 = icmp ult i32 %16, %17
  br i1 %18, label %19, label %21

19:                                               ; preds = %2
  %20 = load i32, ptr %7, align 4, !tbaa !9
  store i32 %20, ptr %6, align 4, !tbaa !9
  br label %21

21:                                               ; preds = %19, %2
  %22 = load i32, ptr %5, align 4, !tbaa !9
  %23 = load i32, ptr %6, align 4, !tbaa !9
  %24 = icmp ugt i32 %22, %23
  br i1 %24, label %25, label %26

25:                                               ; preds = %21
  store i1 false, ptr %3, align 1
  store i32 1, ptr %8, align 4
  br label %39

26:                                               ; preds = %21
  %27 = load i32, ptr %5, align 4, !tbaa !9
  %28 = getelementptr inbounds %class.FifoBuffer, ptr %9, i32 0, i32 2
  %29 = load i32, ptr %28, align 4, !tbaa !13
  %30 = add i32 %29, %27
  store i32 %30, ptr %28, align 4, !tbaa !13
  %31 = getelementptr inbounds %class.FifoBuffer, ptr %9, i32 0, i32 2
  %32 = load i32, ptr %31, align 4, !tbaa !13
  %33 = getelementptr inbounds %class.FifoBuffer, ptr %9, i32 0, i32 1
  %34 = load i32, ptr %33, align 8, !tbaa !11
  %35 = icmp uge i32 %32, %34
  br i1 %35, label %36, label %38

36:                                               ; preds = %26
  %37 = getelementptr inbounds %class.FifoBuffer, ptr %9, i32 0, i32 2
  store i32 0, ptr %37, align 4, !tbaa !13
  br label %38

38:                                               ; preds = %36, %26
  store i1 true, ptr %3, align 1
  store i32 1, ptr %8, align 4
  br label %39

39:                                               ; preds = %38, %25
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  %40 = load i1, ptr %3, align 1
  ret i1 %40
}

attributes #0 = { 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 = { 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 8}
!12 = !{!"_ZTS10FifoBuffer", !6, i64 0, !10, i64 8, !10, i64 12, !10, i64 16}
!13 = !{!12, !10, i64 12}


; 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 nounwind uwtable
declare dso_local noundef i32 @_ZN10FifoBuffer10fifoLengthEv(ptr noundef nonnull align 8 dereferenceable(20)) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef zeroext i1 @_ZN10FifoBuffer11retrieveSegEj(ptr noundef nonnull align 8 dereferenceable(20) %0, i32 noundef %1) local_unnamed_addr #0 align 2 {
  %3 = tail call noundef i32 @_ZN10FifoBuffer10fifoLengthEv(ptr noundef nonnull align 8 dereferenceable(20) %0)
  %4 = getelementptr inbounds i8, ptr %0, i64 8
  %5 = load i32, ptr %4, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %0, i64 12
  %7 = load i32, ptr %6, align 4, !tbaa !11
  %8 = sub i32 %5, %7
  %spec.select = tail call i32 @llvm.umin.i32(i32 %8, i32 %3)
  %9 = icmp uge i32 %spec.select, %1
  br i1 %9, label %10, label %12

10:                                               ; preds = %2
  %11 = add i32 %7, %1
  %.not = icmp ult i32 %11, %5
  %spec.store.select = select i1 %.not, i32 %11, i32 0
  store i32 %spec.store.select, ptr %6, align 4
  br label %12

12:                                               ; preds = %2, %10
  ret i1 %9
}

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

attributes #0 = { 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 = { 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, !10, i64 8}
!6 = !{!"_ZTS10FifoBuffer", !7, i64 0, !10, i64 8, !10, i64 12, !10, i64 16}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!6, !10, i64 12}


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