; LLVM IR from MostafaTwfiq/C-DataStructures-And-Algorithms :: Unit Test/Tests/AlgorithmsTests/ArraysAlgTest (ArraysAlgTest)

; 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.ErrorTestStruct = type { i32 }

@.str = external hidden unnamed_addr constant [140 x i8], align 1
@ERROR_TEST = external dso_local global ptr, align 8
@__const.testArrBinarySearch.arr = external hidden unnamed_addr constant [10 x i32], align 16

; 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 nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

declare void @CuAssertIntEquals_LineMsg(ptr noundef, ptr noundef, i32 noundef, ptr noundef, i32 noundef, i32 noundef) #2

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

declare i32 @compareIntPointers(ptr noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local void @testArrBinarySearch(ptr noundef %0) #3 {
  %2 = alloca ptr, align 8
  %3 = alloca [10 x i32], align 16
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 40, ptr %3) #4
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %3, ptr align 16 @__const.testArrBinarySearch.arr, i64 40, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  %6 = call i32 @arrBinarySearch(ptr noundef null, ptr noundef null, i32 noundef 0, i32 noundef 0, ptr noundef null)
  %7 = load ptr, ptr %2, align 8, !tbaa !5
  %8 = load ptr, ptr @ERROR_TEST, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.ErrorTestStruct, ptr %8, i32 0, i32 0
  %10 = load i32, ptr %9, align 4, !tbaa !9
  call void @CuAssertIntEquals_LineMsg(ptr noundef %7, ptr noundef @.str, i32 noundef 1075, ptr noundef null, i32 noundef -83386157, i32 noundef %10)
  %11 = getelementptr inbounds [10 x i32], ptr %3, i64 0, i64 0
  %12 = call i32 @arrBinarySearch(ptr noundef %11, ptr noundef null, i32 noundef 0, i32 noundef -1, ptr noundef null)
  %13 = load ptr, ptr %2, align 8, !tbaa !5
  %14 = load ptr, ptr @ERROR_TEST, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.ErrorTestStruct, ptr %14, i32 0, i32 0
  %16 = load i32, ptr %15, align 4, !tbaa !9
  call void @CuAssertIntEquals_LineMsg(ptr noundef %13, ptr noundef @.str, i32 noundef 1078, ptr noundef null, i32 noundef -83384987, i32 noundef %16)
  %17 = getelementptr inbounds [10 x i32], ptr %3, i64 0, i64 0
  %18 = call i32 @arrBinarySearch(ptr noundef %17, ptr noundef %4, i32 noundef 0, i32 noundef 0, ptr noundef null)
  %19 = load ptr, ptr %2, align 8, !tbaa !5
  %20 = load ptr, ptr @ERROR_TEST, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.ErrorTestStruct, ptr %20, i32 0, i32 0
  %22 = load i32, ptr %21, align 4, !tbaa !9
  call void @CuAssertIntEquals_LineMsg(ptr noundef %19, ptr noundef @.str, i32 noundef 1081, ptr noundef null, i32 noundef -83384987, i32 noundef %22)
  %23 = getelementptr inbounds [10 x i32], ptr %3, i64 0, i64 0
  %24 = call i32 @arrBinarySearch(ptr noundef %23, ptr noundef %4, i32 noundef -1, i32 noundef 0, ptr noundef @compareIntPointers)
  %25 = load ptr, ptr %2, align 8, !tbaa !5
  %26 = load ptr, ptr @ERROR_TEST, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.ErrorTestStruct, ptr %26, i32 0, i32 0
  %28 = load i32, ptr %27, align 4, !tbaa !9
  call void @CuAssertIntEquals_LineMsg(ptr noundef %25, ptr noundef @.str, i32 noundef 1084, ptr noundef null, i32 noundef -83384987, i32 noundef %28)
  %29 = getelementptr inbounds [10 x i32], ptr %3, i64 0, i64 0
  %30 = call i32 @arrBinarySearch(ptr noundef %29, ptr noundef %4, i32 noundef 0, i32 noundef 0, ptr noundef @compareIntPointers)
  %31 = load ptr, ptr %2, align 8, !tbaa !5
  %32 = load ptr, ptr @ERROR_TEST, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.ErrorTestStruct, ptr %32, i32 0, i32 0
  %34 = load i32, ptr %33, align 4, !tbaa !9
  call void @CuAssertIntEquals_LineMsg(ptr noundef %31, ptr noundef @.str, i32 noundef 1087, ptr noundef null, i32 noundef -83384987, i32 noundef %34)
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  store i32 1, ptr %5, align 4, !tbaa !12
  br label %35

35:                                               ; preds = %45, %1
  %36 = load i32, ptr %5, align 4, !tbaa !12
  %37 = icmp sle i32 %36, 10
  br i1 %37, label %39, label %38

38:                                               ; preds = %35
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  br label %48

39:                                               ; preds = %35
  %40 = load ptr, ptr %2, align 8, !tbaa !5
  %41 = load i32, ptr %5, align 4, !tbaa !12
  %42 = sub nsw i32 %41, 1
  %43 = getelementptr inbounds [10 x i32], ptr %3, i64 0, i64 0
  %44 = call i32 @arrBinarySearch(ptr noundef %43, ptr noundef %5, i32 noundef 10, i32 noundef 4, ptr noundef @compareIntPointers)
  call void @CuAssertIntEquals_LineMsg(ptr noundef %40, ptr noundef @.str, i32 noundef 1090, ptr noundef null, i32 noundef %42, i32 noundef %44)
  br label %45

45:                                               ; preds = %39
  %46 = load i32, ptr %5, align 4, !tbaa !12
  %47 = add nsw i32 %46, 1
  store i32 %47, ptr %5, align 4, !tbaa !12
  br label %35, !llvm.loop !13

48:                                               ; preds = %38
  store i32 11, ptr %4, align 4, !tbaa !12
  %49 = load ptr, ptr %2, align 8, !tbaa !5
  %50 = getelementptr inbounds [10 x i32], ptr %3, i64 0, i64 0
  %51 = call i32 @arrBinarySearch(ptr noundef %50, ptr noundef %4, i32 noundef 10, i32 noundef 4, ptr noundef @compareIntPointers)
  call void @CuAssertIntEquals_LineMsg(ptr noundef %49, ptr noundef @.str, i32 noundef 1093, ptr noundef null, i32 noundef -1, i32 noundef %51)
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 40, ptr %3) #4
  ret void
}

declare i32 @arrBinarySearch(ptr noundef, ptr noundef, i32 noundef, i32 noundef, ptr noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = { 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 #4 = { 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, !11, i64 0}
!10 = !{!"ErrorTestStruct", !11, i64 0}
!11 = !{!"int", !7, i64 0}
!12 = !{!11, !11, 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"

@.str = external hidden unnamed_addr constant [140 x i8], align 1
@ERROR_TEST = external dso_local local_unnamed_addr global ptr, align 8
@__const.testArrBinarySearch.arr = external hidden unnamed_addr constant [10 x i32], align 16

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

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

declare void @CuAssertIntEquals_LineMsg(ptr noundef, ptr noundef, i32 noundef, ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #2

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

declare i32 @compareIntPointers(ptr noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local void @testArrBinarySearch(ptr noundef %0) local_unnamed_addr #3 {
  %2 = alloca [10 x i32], align 16
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 40, ptr nonnull %2) #4
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(40) %2, ptr noundef nonnull align 16 dereferenceable(40) @__const.testArrBinarySearch.arr, i64 40, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %3) #4
  %5 = tail call i32 @arrBinarySearch(ptr noundef null, ptr noundef null, i32 noundef 0, i32 noundef 0, ptr noundef null) #4
  %6 = load ptr, ptr @ERROR_TEST, align 8, !tbaa !5
  %7 = load i32, ptr %6, align 4, !tbaa !9
  tail call void @CuAssertIntEquals_LineMsg(ptr noundef %0, ptr noundef nonnull @.str, i32 noundef 1075, ptr noundef null, i32 noundef -83386157, i32 noundef %7) #4
  %8 = call i32 @arrBinarySearch(ptr noundef nonnull %2, ptr noundef null, i32 noundef 0, i32 noundef -1, ptr noundef null) #4
  %9 = load ptr, ptr @ERROR_TEST, align 8, !tbaa !5
  %10 = load i32, ptr %9, align 4, !tbaa !9
  call void @CuAssertIntEquals_LineMsg(ptr noundef %0, ptr noundef nonnull @.str, i32 noundef 1078, ptr noundef null, i32 noundef -83384987, i32 noundef %10) #4
  %11 = call i32 @arrBinarySearch(ptr noundef nonnull %2, ptr noundef nonnull %3, i32 noundef 0, i32 noundef 0, ptr noundef null) #4
  %12 = load ptr, ptr @ERROR_TEST, align 8, !tbaa !5
  %13 = load i32, ptr %12, align 4, !tbaa !9
  call void @CuAssertIntEquals_LineMsg(ptr noundef %0, ptr noundef nonnull @.str, i32 noundef 1081, ptr noundef null, i32 noundef -83384987, i32 noundef %13) #4
  %14 = call i32 @arrBinarySearch(ptr noundef nonnull %2, ptr noundef nonnull %3, i32 noundef -1, i32 noundef 0, ptr noundef nonnull @compareIntPointers) #4
  %15 = load ptr, ptr @ERROR_TEST, align 8, !tbaa !5
  %16 = load i32, ptr %15, align 4, !tbaa !9
  call void @CuAssertIntEquals_LineMsg(ptr noundef %0, ptr noundef nonnull @.str, i32 noundef 1084, ptr noundef null, i32 noundef -83384987, i32 noundef %16) #4
  %17 = call i32 @arrBinarySearch(ptr noundef nonnull %2, ptr noundef nonnull %3, i32 noundef 0, i32 noundef 0, ptr noundef nonnull @compareIntPointers) #4
  %18 = load ptr, ptr @ERROR_TEST, align 8, !tbaa !5
  %19 = load i32, ptr %18, align 4, !tbaa !9
  call void @CuAssertIntEquals_LineMsg(ptr noundef %0, ptr noundef nonnull @.str, i32 noundef 1087, ptr noundef null, i32 noundef -83384987, i32 noundef %19) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %4) #4
  store i32 1, ptr %4, align 4, !tbaa !12
  br label %22

20:                                               ; preds = %22
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %4) #4
  store i32 11, ptr %3, align 4, !tbaa !12
  %21 = call i32 @arrBinarySearch(ptr noundef nonnull %2, ptr noundef nonnull %3, i32 noundef 10, i32 noundef 4, ptr noundef nonnull @compareIntPointers) #4
  call void @CuAssertIntEquals_LineMsg(ptr noundef %0, ptr noundef nonnull @.str, i32 noundef 1093, ptr noundef null, i32 noundef -1, i32 noundef %21) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %3) #4
  call void @llvm.lifetime.end.p0(i64 40, ptr nonnull %2) #4
  ret void

22:                                               ; preds = %1, %22
  %storemerge7 = phi i32 [ 0, %1 ], [ %24, %22 ]
  %23 = call i32 @arrBinarySearch(ptr noundef nonnull %2, ptr noundef nonnull %4, i32 noundef 10, i32 noundef 4, ptr noundef nonnull @compareIntPointers) #4
  call void @CuAssertIntEquals_LineMsg(ptr noundef %0, ptr noundef nonnull @.str, i32 noundef 1090, ptr noundef null, i32 noundef %storemerge7, i32 noundef %23) #4
  %24 = load i32, ptr %4, align 4, !tbaa !12
  %25 = add nsw i32 %24, 1
  store i32 %25, ptr %4, align 4, !tbaa !12
  %26 = icmp slt i32 %24, 10
  br i1 %26, label %22, label %20, !llvm.loop !13
}

declare i32 @arrBinarySearch(ptr noundef, ptr noundef, i32 noundef, i32 noundef, ptr noundef) local_unnamed_addr #2

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = { 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 #4 = { 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, !11, i64 0}
!10 = !{!"ErrorTestStruct", !11, i64 0}
!11 = !{!"int", !7, i64 0}
!12 = !{!11, !11, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


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