; LLVM IR from clibraries/array-algorithms :: test (impl)

; 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
declare i32 @rand() #1

; Function Attrs: nounwind uwtable
define dso_local ptr @intv_sample(ptr noundef %0, ptr noundef %1, ptr noundef %2, i64 noundef %3) #2 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca i32, align 4
  %12 = alloca i64, align 8
  %13 = alloca i64, align 8
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store ptr %2, ptr %8, align 8, !tbaa !5
  store i64 %3, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  store i64 0, ptr %10, align 8, !tbaa !9
  br label %14

14:                                               ; preds = %35, %4
  %15 = load i64, ptr %10, align 8, !tbaa !9
  %16 = load i64, ptr %9, align 8, !tbaa !9
  %17 = icmp ult i64 %15, %16
  br i1 %17, label %19, label %18

18:                                               ; preds = %14
  store i32 2, ptr %11, align 4
  br label %38

19:                                               ; preds = %14
  %20 = load ptr, ptr %6, align 8, !tbaa !5
  %21 = load ptr, ptr %7, align 8, !tbaa !5
  %22 = icmp eq ptr %20, %21
  br i1 %22, label %23, label %27

23:                                               ; preds = %19
  %24 = load ptr, ptr %8, align 8, !tbaa !5
  %25 = load i64, ptr %10, align 8, !tbaa !9
  %26 = getelementptr inbounds i32, ptr %24, i64 %25
  store ptr %26, ptr %5, align 8
  store i32 1, ptr %11, align 4
  br label %38

27:                                               ; preds = %19
  %28 = load ptr, ptr %6, align 8, !tbaa !5
  %29 = load i32, ptr %28, align 4, !tbaa !11
  %30 = load ptr, ptr %8, align 8, !tbaa !5
  %31 = load i64, ptr %10, align 8, !tbaa !9
  %32 = getelementptr inbounds i32, ptr %30, i64 %31
  store i32 %29, ptr %32, align 4, !tbaa !11
  %33 = load ptr, ptr %6, align 8, !tbaa !5
  %34 = getelementptr inbounds i32, ptr %33, i32 1
  store ptr %34, ptr %6, align 8, !tbaa !5
  br label %35

35:                                               ; preds = %27
  %36 = load i64, ptr %10, align 8, !tbaa !9
  %37 = add i64 %36, 1
  store i64 %37, ptr %10, align 8, !tbaa !9
  br label %14, !llvm.loop !13

38:                                               ; preds = %23, %18
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  %39 = load i32, ptr %11, align 4
  switch i32 %39, label %72 [
    i32 2, label %40
    i32 1, label %70
  ]

40:                                               ; preds = %38
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  %41 = load i64, ptr %9, align 8, !tbaa !9
  %42 = add i64 %41, 1
  store i64 %42, ptr %12, align 8, !tbaa !9
  br label %43

43:                                               ; preds = %61, %40
  %44 = load ptr, ptr %6, align 8, !tbaa !5
  %45 = load ptr, ptr %7, align 8, !tbaa !5
  %46 = icmp ne ptr %44, %45
  br i1 %46, label %47, label %66

47:                                               ; preds = %43
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #3
  %48 = call i32 @rand() #3
  %49 = sext i32 %48 to i64
  %50 = load i64, ptr %12, align 8, !tbaa !9
  %51 = urem i64 %49, %50
  store i64 %51, ptr %13, align 8, !tbaa !9
  %52 = load i64, ptr %13, align 8, !tbaa !9
  %53 = load i64, ptr %9, align 8, !tbaa !9
  %54 = icmp ult i64 %52, %53
  br i1 %54, label %55, label %61

55:                                               ; preds = %47
  %56 = load ptr, ptr %6, align 8, !tbaa !5
  %57 = load i32, ptr %56, align 4, !tbaa !11
  %58 = load ptr, ptr %8, align 8, !tbaa !5
  %59 = load i64, ptr %13, align 8, !tbaa !9
  %60 = getelementptr inbounds i32, ptr %58, i64 %59
  store i32 %57, ptr %60, align 4, !tbaa !11
  br label %61

61:                                               ; preds = %55, %47
  %62 = load ptr, ptr %6, align 8, !tbaa !5
  %63 = getelementptr inbounds i32, ptr %62, i32 1
  store ptr %63, ptr %6, align 8, !tbaa !5
  %64 = load i64, ptr %12, align 8, !tbaa !9
  %65 = add i64 %64, 1
  store i64 %65, ptr %12, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #3
  br label %43, !llvm.loop !15

66:                                               ; preds = %43
  %67 = load ptr, ptr %8, align 8, !tbaa !5
  %68 = load i64, ptr %9, align 8, !tbaa !9
  %69 = getelementptr inbounds i32, ptr %67, i64 %68
  store ptr %69, ptr %5, align 8
  store i32 1, ptr %11, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  br label %70

70:                                               ; preds = %66, %38
  %71 = load ptr, ptr %5, align 8
  ret ptr %71

72:                                               ; preds = %38
  unreachable
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { 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 #3 = { 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"}
!15 = distinct !{!15, !14}


; 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
declare i32 @rand() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local ptr @intv_sample(ptr noundef readonly %0, ptr noundef readnone %1, ptr noundef writeonly %2, i64 noundef %3) local_unnamed_addr #1 {
  %.not45 = icmp eq i64 %3, 0
  br i1 %.not45, label %.preheader, label %.lr.ph

.preheader:                                       ; preds = %6, %4
  %.028.lcssa = phi ptr [ %0, %4 ], [ %9, %6 ]
  %.not41 = icmp eq ptr %.028.lcssa, %1
  br i1 %.not41, label %.thread, label %.lr.ph44

.lr.ph:                                           ; preds = %4, %6
  %.02739 = phi i64 [ %10, %6 ], [ 0, %4 ]
  %.02838 = phi ptr [ %9, %6 ], [ %0, %4 ]
  %5 = icmp eq ptr %.02838, %1
  br i1 %5, label %.thread, label %6

6:                                                ; preds = %.lr.ph
  %7 = load i32, ptr %.02838, align 4, !tbaa !5
  %8 = getelementptr inbounds i32, ptr %2, i64 %.02739
  store i32 %7, ptr %8, align 4, !tbaa !5
  %9 = getelementptr inbounds i8, ptr %.02838, i64 4
  %10 = add nuw i64 %.02739, 1
  %exitcond.not = icmp eq i64 %10, %3
  br i1 %exitcond.not, label %.preheader, label %.lr.ph, !llvm.loop !9

.lr.ph44:                                         ; preds = %.preheader, %18
  %.02543.in = phi i64 [ %.02543, %18 ], [ %3, %.preheader ]
  %.12942 = phi ptr [ %19, %18 ], [ %.028.lcssa, %.preheader ]
  %.02543 = add i64 %.02543.in, 1
  %11 = tail call i32 @rand() #2
  %12 = sext i32 %11 to i64
  %13 = urem i64 %12, %.02543
  %14 = icmp ult i64 %13, %3
  br i1 %14, label %15, label %18

15:                                               ; preds = %.lr.ph44
  %16 = load i32, ptr %.12942, align 4, !tbaa !5
  %17 = getelementptr inbounds i32, ptr %2, i64 %13
  store i32 %16, ptr %17, align 4, !tbaa !5
  br label %18

18:                                               ; preds = %15, %.lr.ph44
  %19 = getelementptr inbounds i8, ptr %.12942, i64 4
  %.not = icmp eq ptr %19, %1
  br i1 %.not, label %.thread, label %.lr.ph44, !llvm.loop !11

.thread:                                          ; preds = %.lr.ph, %18, %.preheader
  %.pn = phi i64 [ %3, %.preheader ], [ %3, %18 ], [ %.02739, %.lr.ph ]
  %.1 = getelementptr inbounds i32, ptr %2, i64 %.pn
  ret ptr %.1
}

attributes #0 = { 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 #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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


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