; LLVM IR from kmilo17pet/qlibs :: qtypegeneric (qTypeGeneric_BSearch)

; 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: nounwind uwtable
define dso_local ptr @qTypeGeneric_BSearch(ptr noundef %0, ptr noundef %1, i64 noundef %2, i64 noundef %3, ptr noundef %4, ptr noundef %5) #1 {
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca ptr, align 8
  %12 = alloca ptr, align 8
  %13 = alloca ptr, align 8
  %14 = alloca i64, align 8
  %15 = alloca ptr, align 8
  %16 = alloca ptr, align 8
  %17 = alloca i32, align 4
  %18 = alloca i32, align 4
  store ptr %0, ptr %7, align 8, !tbaa !5
  store ptr %1, ptr %8, align 8, !tbaa !5
  store i64 %2, ptr %9, align 8, !tbaa !9
  store i64 %3, ptr %10, align 8, !tbaa !9
  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
  %19 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %19, ptr %13, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #2
  %20 = load i64, ptr %9, align 8, !tbaa !9
  store i64 %20, ptr %14, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %16) #2
  store ptr null, ptr %16, align 8, !tbaa !5
  br label %21

21:                                               ; preds = %56, %6
  %22 = load i64, ptr %14, align 8, !tbaa !9
  %23 = icmp ne i64 0, %22
  br i1 %23, label %24, label %57

24:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 4, ptr %17) #2
  %25 = load ptr, ptr %13, align 8, !tbaa !5
  %26 = load i64, ptr %14, align 8, !tbaa !9
  %27 = lshr i64 %26, 1
  %28 = load i64, ptr %10, align 8, !tbaa !9
  %29 = mul i64 %27, %28
  %30 = getelementptr inbounds i8, ptr %25, i64 %29
  store ptr %30, ptr %15, align 8, !tbaa !5
  %31 = load ptr, ptr %11, align 8, !tbaa !5
  %32 = load ptr, ptr %7, align 8, !tbaa !5
  %33 = load ptr, ptr %15, align 8, !tbaa !5
  %34 = load ptr, ptr %12, align 8, !tbaa !5
  %35 = call i32 %31(ptr noundef %32, ptr noundef %33, ptr noundef %34)
  store i32 %35, ptr %17, align 4, !tbaa !11
  %36 = load i32, ptr %17, align 4, !tbaa !11
  %37 = icmp eq i32 0, %36
  br i1 %37, label %38, label %40

38:                                               ; preds = %24
  %39 = load ptr, ptr %15, align 8, !tbaa !5
  store ptr %39, ptr %16, align 8, !tbaa !5
  store i32 3, ptr %18, align 4
  br label %54

40:                                               ; preds = %24
  %41 = load i32, ptr %17, align 4, !tbaa !11
  %42 = icmp sgt i32 %41, 0
  br i1 %42, label %43, label %49

43:                                               ; preds = %40
  %44 = load ptr, ptr %15, align 8, !tbaa !5
  %45 = load i64, ptr %10, align 8, !tbaa !9
  %46 = getelementptr inbounds i8, ptr %44, i64 %45
  store ptr %46, ptr %13, align 8, !tbaa !5
  %47 = load i64, ptr %14, align 8, !tbaa !9
  %48 = add i64 %47, -1
  store i64 %48, ptr %14, align 8, !tbaa !9
  br label %50

49:                                               ; preds = %40
  br label %50

50:                                               ; preds = %49, %43
  br label %51

51:                                               ; preds = %50
  %52 = load i64, ptr %14, align 8, !tbaa !9
  %53 = lshr i64 %52, 1
  store i64 %53, ptr %14, align 8, !tbaa !9
  store i32 0, ptr %18, align 4
  br label %54

54:                                               ; preds = %51, %38
  call void @llvm.lifetime.end.p0(i64 4, ptr %17) #2
  %55 = load i32, ptr %18, align 4
  switch i32 %55, label %59 [
    i32 0, label %56
    i32 3, label %57
  ]

56:                                               ; preds = %54
  br label %21, !llvm.loop !13

57:                                               ; preds = %54, %21
  %58 = load ptr, ptr %16, align 8, !tbaa !5
  store i32 1, ptr %18, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #2
  ret ptr %58

59:                                               ; preds = %54
  unreachable
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 = !{!"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, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"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: nounwind uwtable
define dso_local noundef ptr @qTypeGeneric_BSearch(ptr noundef %0, ptr noundef %1, i64 noundef %2, i64 noundef %3, ptr nocapture noundef readonly %4, ptr noundef %5) local_unnamed_addr #0 {
  %.not31 = icmp eq i64 %2, 0
  br i1 %.not31, label %.thread, label %.lr.ph

.lr.ph:                                           ; preds = %6, %12
  %.01833 = phi ptr [ %.1, %12 ], [ %1, %6 ]
  %.02232 = phi i64 [ %16, %12 ], [ %2, %6 ]
  %7 = lshr i64 %.02232, 1
  %8 = mul i64 %7, %3
  %9 = getelementptr inbounds i8, ptr %.01833, i64 %8
  %10 = tail call i32 %4(ptr noundef %0, ptr noundef %9, ptr noundef %5) #1
  %11 = icmp eq i32 %10, 0
  br i1 %11, label %.thread, label %12

12:                                               ; preds = %.lr.ph
  %13 = icmp sgt i32 %10, 0
  %14 = getelementptr inbounds i8, ptr %9, i64 %3
  %15 = sext i1 %13 to i64
  %.123 = add i64 %.02232, %15
  %.1 = select i1 %13, ptr %14, ptr %.01833
  %16 = lshr i64 %.123, 1
  %.not = icmp ult i64 %.123, 2
  br i1 %.not, label %.thread, label %.lr.ph

.thread:                                          ; preds = %12, %.lr.ph, %6
  %.221 = phi ptr [ null, %6 ], [ %9, %.lr.ph ], [ null, %12 ]
  ret ptr %.221
}

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 = { 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)"}


; Effective O3 passes: CorrelatedValuePropagationPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass