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

%struct.Person = type { i32, [32 x i8] }

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

; Function Attrs: nounwind
declare i32 @rand() #2

; Function Attrs: nounwind uwtable
define dso_local ptr @person_array_sample(ptr noundef %0, ptr noundef %1, ptr noundef %2, i64 noundef %3) #3 {
  %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) #4
  store i64 0, ptr %10, align 8, !tbaa !9
  br label %14

14:                                               ; preds = %34, %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 %37

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 %struct.Person, ptr %24, i64 %25
  store ptr %26, ptr %5, align 8
  store i32 1, ptr %11, align 4
  br label %37

27:                                               ; preds = %19
  %28 = load ptr, ptr %8, align 8, !tbaa !5
  %29 = load i64, ptr %10, align 8, !tbaa !9
  %30 = getelementptr inbounds %struct.Person, ptr %28, i64 %29
  %31 = load ptr, ptr %6, align 8, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %30, ptr align 4 %31, i64 36, i1 false), !tbaa.struct !11
  %32 = load ptr, ptr %6, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.Person, ptr %32, i32 1
  store ptr %33, ptr %6, align 8, !tbaa !5
  br label %34

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

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

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

42:                                               ; preds = %59, %39
  %43 = load ptr, ptr %6, align 8, !tbaa !5
  %44 = load ptr, ptr %7, align 8, !tbaa !5
  %45 = icmp ne ptr %43, %44
  br i1 %45, label %46, label %64

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

54:                                               ; preds = %46
  %55 = load ptr, ptr %8, align 8, !tbaa !5
  %56 = load i64, ptr %13, align 8, !tbaa !9
  %57 = getelementptr inbounds %struct.Person, ptr %55, i64 %56
  %58 = load ptr, ptr %6, align 8, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %57, ptr align 4 %58, i64 36, i1 false), !tbaa.struct !11
  br label %59

59:                                               ; preds = %54, %46
  %60 = load ptr, ptr %6, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.Person, ptr %60, i32 1
  store ptr %61, ptr %6, align 8, !tbaa !5
  %62 = load i64, ptr %12, align 8, !tbaa !9
  %63 = add i64 %62, 1
  store i64 %63, ptr %12, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #4
  br label %42, !llvm.loop !17

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

68:                                               ; preds = %64, %37
  %69 = load ptr, ptr %5, align 8
  ret ptr %69

70:                                               ; preds = %37
  unreachable
}

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


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

%struct.Person = type { i32, [32 x i8] }

; 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) #0

; Function Attrs: nounwind
declare i32 @rand() local_unnamed_addr #1

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

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

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

6:                                                ; preds = %.lr.ph
  %7 = getelementptr inbounds %struct.Person, ptr %2, i64 %.02739
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(36) %7, ptr noundef nonnull align 4 dereferenceable(36) %.02838, i64 36, i1 false), !tbaa.struct !5
  %8 = getelementptr inbounds i8, ptr %.02838, i64 36
  %9 = add nuw i64 %.02739, 1
  %exitcond.not = icmp eq i64 %9, %3
  br i1 %exitcond.not, label %.preheader, label %.lr.ph, !llvm.loop !11

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

14:                                               ; preds = %.lr.ph44
  %15 = getelementptr inbounds %struct.Person, ptr %2, i64 %12
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(36) %15, ptr noundef nonnull align 4 dereferenceable(36) %.12942, i64 36, i1 false), !tbaa.struct !5
  br label %16

16:                                               ; preds = %14, %.lr.ph44
  %17 = getelementptr inbounds i8, ptr %.12942, i64 36
  %.not = icmp eq ptr %17, %1
  br i1 %.not, label %.thread, label %.lr.ph44, !llvm.loop !13

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

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


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