; LLVM IR from stan-dev/math :: lib/sundials_6.1.1/benchmarks/nvector (test_nvector_performance)

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

@rand_realtype_zero_one.rand_max = external hidden global i32, align 4

; 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 i64 @time(ptr noundef) #1

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

9:                                                ; preds = %27, %2
  %10 = load i32, ptr %5, align 4, !tbaa !11
  %11 = sext i32 %10 to i64
  %12 = load i64, ptr %4, align 8, !tbaa !9
  %13 = icmp slt i64 %11, %12
  br i1 %13, label %14, label %30

14:                                               ; preds = %9
  %15 = load i32, ptr %6, align 4, !tbaa !11
  %16 = mul nsw i32 1103515245, %15
  %17 = add nsw i32 %16, 12345
  %18 = load i32, ptr @rand_realtype_zero_one.rand_max, align 4, !tbaa !11
  %19 = and i32 %17, %18
  store i32 %19, ptr %6, align 4, !tbaa !11
  %20 = load i32, ptr %6, align 4, !tbaa !11
  %21 = srem i32 %20, 2
  %22 = sitofp i32 %21 to double
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  %24 = load i32, ptr %5, align 4, !tbaa !11
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds double, ptr %23, i64 %25
  store double %22, ptr %26, align 8, !tbaa !13
  br label %27

27:                                               ; preds = %14
  %28 = load i32, ptr %5, align 4, !tbaa !11
  %29 = add nsw i32 %28, 1
  store i32 %29, ptr %5, align 4, !tbaa !11
  br label %9, !llvm.loop !15

30:                                               ; preds = %9
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  ret void
}

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 = !{!14, !14, i64 0}
!14 = !{!"double", !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"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"

@rand_realtype_zero_one.rand_max = external hidden local_unnamed_addr global i32, align 4

; Function Attrs: nounwind
declare i64 @time(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @rand_realtype_zero_one(ptr nocapture noundef writeonly %0, i64 noundef %1) local_unnamed_addr #1 {
  %3 = tail call i64 @time(ptr noundef null) #2
  %4 = icmp sgt i64 %1, 0
  br i1 %4, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %2
  %5 = trunc i64 %3 to i32
  %6 = load i32, ptr @rand_realtype_zero_one.rand_max, align 4, !tbaa !5
  %xtraiter = and i64 %1, 1
  %7 = icmp eq i64 %1, 1
  br i1 %7, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.new

.lr.ph.new:                                       ; preds = %.lr.ph
  %unroll_iter = and i64 %1, 9223372036854775806
  br label %8

8:                                                ; preds = %8, %.lr.ph.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.new ], [ %indvars.iv.next.1, %8 ]
  %.09 = phi i32 [ %5, %.lr.ph.new ], [ %17, %8 ]
  %niter = phi i64 [ 0, %.lr.ph.new ], [ %niter.next.1, %8 ]
  %9 = mul nsw i32 %.09, 1103515245
  %10 = add nsw i32 %9, 12345
  %11 = and i32 %6, %10
  %12 = srem i32 %11, 2
  %13 = sitofp i32 %12 to double
  %14 = getelementptr inbounds double, ptr %0, i64 %indvars.iv
  store double %13, ptr %14, align 8, !tbaa !9
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %15 = mul nsw i32 %11, 1103515245
  %16 = add nsw i32 %15, 12345
  %17 = and i32 %6, %16
  %18 = srem i32 %17, 2
  %19 = sitofp i32 %18 to double
  %20 = getelementptr inbounds double, ptr %0, i64 %indvars.iv.next
  store double %19, ptr %20, align 8, !tbaa !9
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.loopexit.unr-lcssa, label %8, !llvm.loop !11

._crit_edge.loopexit.unr-lcssa:                   ; preds = %8, %.lr.ph
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next.1, %8 ]
  %.09.unr = phi i32 [ %5, %.lr.ph ], [ %17, %8 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %._crit_edge.loopexit.epilog-lcssa

._crit_edge.loopexit.epilog-lcssa:                ; preds = %._crit_edge.loopexit.unr-lcssa
  %21 = mul nsw i32 %.09.unr, 1103515245
  %22 = add nsw i32 %21, 12345
  %23 = and i32 %6, %22
  %24 = srem i32 %23, 2
  %25 = sitofp i32 %24 to double
  %26 = getelementptr inbounds double, ptr %0, i64 %indvars.iv.unr
  store double %25, ptr %26, align 8, !tbaa !9
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit.epilog-lcssa, %._crit_edge.loopexit.unr-lcssa, %2
  ret void
}

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 = !{!10, !10, i64 0}
!10 = !{!"double", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


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