; LLVM IR from ninn-kou/Lux :: LeetCode/0121. Best Time to Buy and Sell Stock (121-141020)

; 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: nounwind uwtable
define dso_local i32 @maxProfit(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  store i32 0, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %10

10:                                               ; preds = %45, %2
  %11 = load i32, ptr %6, align 4, !tbaa !9
  %12 = load i32, ptr %4, align 4, !tbaa !9
  %13 = icmp slt i32 %11, %12
  br i1 %13, label %15, label %14

14:                                               ; preds = %10
  store i32 2, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  br label %48

15:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %16 = load i32, ptr %6, align 4, !tbaa !9
  %17 = add nsw i32 %16, 1
  store i32 %17, ptr %8, align 4, !tbaa !9
  br label %18

18:                                               ; preds = %41, %15
  %19 = load i32, ptr %8, align 4, !tbaa !9
  %20 = load i32, ptr %4, align 4, !tbaa !9
  %21 = icmp slt i32 %19, %20
  br i1 %21, label %23, label %22

22:                                               ; preds = %18
  store i32 5, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  br label %44

23:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  %24 = load ptr, ptr %3, align 8, !tbaa !5
  %25 = load i32, ptr %8, align 4, !tbaa !9
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds i32, ptr %24, i64 %26
  %28 = load i32, ptr %27, align 4, !tbaa !9
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = load i32, ptr %6, align 4, !tbaa !9
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds i32, ptr %29, i64 %31
  %33 = load i32, ptr %32, align 4, !tbaa !9
  %34 = sub nsw i32 %28, %33
  store i32 %34, ptr %9, align 4, !tbaa !9
  %35 = load i32, ptr %9, align 4, !tbaa !9
  %36 = load i32, ptr %5, align 4, !tbaa !9
  %37 = icmp sgt i32 %35, %36
  br i1 %37, label %38, label %40

38:                                               ; preds = %23
  %39 = load i32, ptr %9, align 4, !tbaa !9
  store i32 %39, ptr %5, align 4, !tbaa !9
  br label %40

40:                                               ; preds = %38, %23
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  br label %41

41:                                               ; preds = %40
  %42 = load i32, ptr %8, align 4, !tbaa !9
  %43 = add nsw i32 %42, 1
  store i32 %43, ptr %8, align 4, !tbaa !9
  br label %18, !llvm.loop !11

44:                                               ; preds = %22
  br label %45

45:                                               ; preds = %44
  %46 = load i32, ptr %6, align 4, !tbaa !9
  %47 = add nsw i32 %46, 1
  store i32 %47, ptr %6, align 4, !tbaa !9
  br label %10, !llvm.loop !13

48:                                               ; preds = %14
  %49 = load i32, ptr %5, align 4, !tbaa !9
  store i32 1, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  ret i32 %49
}

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

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

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


; 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: nofree norecurse nosync nounwind memory(argmem: read) uwtable
define dso_local range(i32 0, -2147483648) i32 @maxProfit(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp sgt i32 %1, 0
  br i1 %3, label %.lr.ph22.preheader, label %._crit_edge

.lr.ph22.preheader:                               ; preds = %2
  %4 = zext nneg i32 %1 to i64
  %wide.trip.count30 = zext nneg i32 %1 to i64
  br label %.lr.ph22

.loopexit:                                        ; preds = %scalar.ph, %middle.block, %.lr.ph22
  %.1.lcssa = phi i32 [ %.021, %.lr.ph22 ], [ %18, %middle.block ], [ %spec.select, %scalar.ph ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond31.not = icmp eq i64 %indvars.iv.next28, %wide.trip.count30
  br i1 %exitcond31.not, label %._crit_edge, label %.lr.ph22, !llvm.loop !5

._crit_edge:                                      ; preds = %.loopexit, %2
  %.0.lcssa = phi i32 [ 0, %2 ], [ %.1.lcssa, %.loopexit ]
  ret i32 %.0.lcssa

.lr.ph22:                                         ; preds = %.lr.ph22.preheader, %.loopexit
  %indvars.iv27 = phi i64 [ 0, %.lr.ph22.preheader ], [ %indvars.iv.next28, %.loopexit ]
  %indvars.iv = phi i64 [ 1, %.lr.ph22.preheader ], [ %indvars.iv.next, %.loopexit ]
  %.021 = phi i32 [ 0, %.lr.ph22.preheader ], [ %.1.lcssa, %.loopexit ]
  %5 = xor i64 %indvars.iv27, -1
  %6 = add nsw i64 %5, %wide.trip.count30
  %indvars.iv.next28 = add nuw nsw i64 %indvars.iv27, 1
  %7 = icmp ult i64 %indvars.iv.next28, %4
  br i1 %7, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %.lr.ph22
  %8 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv27
  %9 = load i32, ptr %8, align 4, !tbaa !7
  %min.iters.check = icmp ult i64 %6, 8
  br i1 %min.iters.check, label %scalar.ph.preheader, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph
  %n.vec = and i64 %6, -8
  %ind.end = add i64 %indvars.iv, %n.vec
  %minmax.ident.splatinsert = insertelement <4 x i32> poison, i32 %.021, i64 0
  %minmax.ident.splat = shufflevector <4 x i32> %minmax.ident.splatinsert, <4 x i32> poison, <4 x i32> zeroinitializer
  %broadcast.splatinsert = insertelement <4 x i32> poison, i32 %9, i64 0
  %broadcast.splat = shufflevector <4 x i32> %broadcast.splatinsert, <4 x i32> poison, <4 x i32> zeroinitializer
  %10 = getelementptr i32, ptr %0, i64 %indvars.iv
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <4 x i32> [ %minmax.ident.splat, %vector.ph ], [ %15, %vector.body ]
  %vec.phi32 = phi <4 x i32> [ %minmax.ident.splat, %vector.ph ], [ %16, %vector.body ]
  %11 = getelementptr i32, ptr %10, i64 %index
  %12 = getelementptr inbounds i8, ptr %11, i64 16
  %wide.load = load <4 x i32>, ptr %11, align 4, !tbaa !7
  %wide.load33 = load <4 x i32>, ptr %12, align 4, !tbaa !7
  %13 = sub nsw <4 x i32> %wide.load, %broadcast.splat
  %14 = sub nsw <4 x i32> %wide.load33, %broadcast.splat
  %15 = tail call <4 x i32> @llvm.smax.v4i32(<4 x i32> %13, <4 x i32> %vec.phi)
  %16 = tail call <4 x i32> @llvm.smax.v4i32(<4 x i32> %14, <4 x i32> %vec.phi32)
  %index.next = add nuw i64 %index, 8
  %17 = icmp eq i64 %index.next, %n.vec
  br i1 %17, label %middle.block, label %vector.body, !llvm.loop !11

middle.block:                                     ; preds = %vector.body
  %rdx.minmax = tail call <4 x i32> @llvm.smax.v4i32(<4 x i32> %15, <4 x i32> %16)
  %18 = tail call i32 @llvm.vector.reduce.smax.v4i32(<4 x i32> %rdx.minmax)
  %cmp.n = icmp eq i64 %6, %n.vec
  br i1 %cmp.n, label %.loopexit, label %scalar.ph.preheader

scalar.ph.preheader:                              ; preds = %middle.block, %.lr.ph
  %indvars.iv24.ph = phi i64 [ %indvars.iv, %.lr.ph ], [ %ind.end, %middle.block ]
  %.119.ph = phi i32 [ %.021, %.lr.ph ], [ %18, %middle.block ]
  br label %scalar.ph

scalar.ph:                                        ; preds = %scalar.ph.preheader, %scalar.ph
  %indvars.iv24 = phi i64 [ %indvars.iv.next25, %scalar.ph ], [ %indvars.iv24.ph, %scalar.ph.preheader ]
  %.119 = phi i32 [ %spec.select, %scalar.ph ], [ %.119.ph, %scalar.ph.preheader ]
  %19 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv24
  %20 = load i32, ptr %19, align 4, !tbaa !7
  %21 = sub nsw i32 %20, %9
  %spec.select = tail call i32 @llvm.smax.i32(i32 %21, i32 %.119)
  %indvars.iv.next25 = add nuw nsw i64 %indvars.iv24, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next25, %wide.trip.count30
  br i1 %exitcond.not, label %.loopexit, label %scalar.ph, !llvm.loop !14
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare <4 x i32> @llvm.smax.v4i32(<4 x i32>, <4 x i32>) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.vector.reduce.smax.v4i32(<4 x i32>) #1

attributes #0 = { nofree norecurse nosync nounwind memory(argmem: read) 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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = !{!8, !8, i64 0}
!8 = !{!"int", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = distinct !{!11, !6, !12, !13}
!12 = !{!"llvm.loop.isvectorized", i32 1}
!13 = !{!"llvm.loop.unroll.runtime.disable"}
!14 = distinct !{!14, !6, !13, !12}


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