; LLVM IR from DeanoC/gfx_imagecompress :: src/amd_hdr_encode.ll (ZN10HDR_Encode13ep_find_floorEfiii)

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

; 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: mustprogress nounwind uwtable
declare dso_local noundef float @_ZN10HDR_Encode5ep_dfEii(i32 noundef, i32 noundef) #1

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_ZN10HDR_Encode13ep_find_floorEfiii(float noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) #1 {
  %5 = alloca float, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store float %0, ptr %5, align 4, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store i32 %2, ptr %7, align 4, !tbaa !9
  store i32 %3, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  store i32 0, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  %12 = load i32, ptr %6, align 4, !tbaa !9
  %13 = load i32, ptr %7, align 4, !tbaa !9
  %14 = sub nsw i32 %12, %13
  %15 = shl i32 1, %14
  store i32 %15, ptr %10, align 4, !tbaa !9
  %16 = load i32, ptr %7, align 4, !tbaa !9
  %17 = icmp ne i32 %16, 0
  br i1 %17, label %18, label %20

18:                                               ; preds = %4
  %19 = load i32, ptr %8, align 4, !tbaa !9
  br label %21

20:                                               ; preds = %4
  br label %21

21:                                               ; preds = %20, %18
  %22 = phi i32 [ %19, %18 ], [ 0, %20 ]
  store i32 %22, ptr %8, align 4, !tbaa !9
  br label %23

23:                                               ; preds = %47, %21
  %24 = load i32, ptr %10, align 4, !tbaa !9
  %25 = load i32, ptr %9, align 4, !tbaa !9
  %26 = sub nsw i32 %24, %25
  %27 = icmp sgt i32 %26, 1
  br i1 %27, label %28, label %48

28:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  %29 = load i32, ptr %9, align 4, !tbaa !9
  %30 = load i32, ptr %10, align 4, !tbaa !9
  %31 = add nsw i32 %29, %30
  %32 = sdiv i32 %31, 2
  store i32 %32, ptr %11, align 4, !tbaa !9
  %33 = load float, ptr %5, align 4, !tbaa !5
  %34 = load i32, ptr %6, align 4, !tbaa !9
  %35 = sub nsw i32 %34, 5
  %36 = load i32, ptr %11, align 4, !tbaa !9
  %37 = load i32, ptr %7, align 4, !tbaa !9
  %38 = shl i32 %36, %37
  %39 = load i32, ptr %8, align 4, !tbaa !9
  %40 = add nsw i32 %38, %39
  %41 = call noundef float @_ZN10HDR_Encode5ep_dfEii(i32 noundef %35, i32 noundef %40)
  %42 = fcmp oge float %33, %41
  br i1 %42, label %43, label %45

43:                                               ; preds = %28
  %44 = load i32, ptr %11, align 4, !tbaa !9
  store i32 %44, ptr %9, align 4, !tbaa !9
  br label %47

45:                                               ; preds = %28
  %46 = load i32, ptr %11, align 4, !tbaa !9
  store i32 %46, ptr %10, align 4, !tbaa !9
  br label %47

47:                                               ; preds = %45, %43
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  br label %23, !llvm.loop !11

48:                                               ; preds = %23
  %49 = load i32, ptr %9, align 4, !tbaa !9
  %50 = load i32, ptr %7, align 4, !tbaa !9
  %51 = shl i32 %49, %50
  %52 = load i32, ptr %8, align 4, !tbaa !9
  %53 = add nsw i32 %51, %52
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  ret i32 %53
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 = !{!"float", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; 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 float @_ZN10HDR_Encode5ep_dfEii(i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_ZN10HDR_Encode13ep_find_floorEfiii(float noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = sub nsw i32 %1, %2
  %6 = shl nuw i32 1, %5
  %.not = icmp eq i32 %2, 0
  %7 = select i1 %.not, i32 0, i32 %3
  %8 = icmp sgt i32 %6, 1
  br i1 %8, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %4
  %9 = add nsw i32 %1, -5
  br label %10

10:                                               ; preds = %.lr.ph, %10
  %.020 = phi i32 [ %6, %.lr.ph ], [ %..0, %10 ]
  %.01719 = phi i32 [ 0, %.lr.ph ], [ %.017., %10 ]
  %11 = add nsw i32 %.020, %.01719
  %12 = sdiv i32 %11, 2
  %13 = shl i32 %12, %2
  %14 = add nsw i32 %13, %7
  %15 = tail call noundef float @_ZN10HDR_Encode5ep_dfEii(i32 noundef %9, i32 noundef %14)
  %16 = fcmp ugt float %15, %0
  %.017. = select i1 %16, i32 %.01719, i32 %12
  %..0 = select i1 %16, i32 %12, i32 %.020
  %17 = sub nsw i32 %..0, %.017.
  %18 = icmp sgt i32 %17, 1
  br i1 %18, label %10, label %._crit_edge, !llvm.loop !5

._crit_edge:                                      ; preds = %10, %4
  %.017.lcssa = phi i32 [ 0, %4 ], [ %.017., %10 ]
  %19 = shl i32 %.017.lcssa, %2
  %20 = add nsw i32 %19, %7
  ret i32 %20
}

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

!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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass