; LLVM IR from ahampriyanshu/algo-ds-101 :: Algorithms/DP/LIS (LIS)

; 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 @lis(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca [10000 x i32], align 16
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = 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 40000, ptr %5) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %9 = getelementptr inbounds [10000 x i32], ptr %5, i64 0, i64 0
  store i32 1, ptr %9, align 16, !tbaa !9
  store i32 1, ptr %6, align 4, !tbaa !9
  br label %10

10:                                               ; preds = %59, %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 %14, label %62

14:                                               ; preds = %10
  %15 = load i32, ptr %6, align 4, !tbaa !9
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [10000 x i32], ptr %5, i64 0, i64 %16
  store i32 1, ptr %17, align 4, !tbaa !9
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %18

18:                                               ; preds = %55, %14
  %19 = load i32, ptr %7, align 4, !tbaa !9
  %20 = load i32, ptr %6, align 4, !tbaa !9
  %21 = icmp slt i32 %19, %20
  br i1 %21, label %22, label %58

22:                                               ; preds = %18
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  %24 = load i32, ptr %6, align 4, !tbaa !9
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds i32, ptr %23, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !9
  %28 = load ptr, ptr %3, align 8, !tbaa !5
  %29 = load i32, ptr %7, align 4, !tbaa !9
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds i32, ptr %28, i64 %30
  %32 = load i32, ptr %31, align 4, !tbaa !9
  %33 = icmp sgt i32 %27, %32
  br i1 %33, label %34, label %54

34:                                               ; preds = %22
  %35 = load i32, ptr %6, align 4, !tbaa !9
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [10000 x i32], ptr %5, i64 0, i64 %36
  %38 = load i32, ptr %37, align 4, !tbaa !9
  %39 = load i32, ptr %7, align 4, !tbaa !9
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds [10000 x i32], ptr %5, i64 0, i64 %40
  %42 = load i32, ptr %41, align 4, !tbaa !9
  %43 = add nsw i32 %42, 1
  %44 = icmp slt i32 %38, %43
  br i1 %44, label %45, label %54

45:                                               ; preds = %34
  %46 = load i32, ptr %7, align 4, !tbaa !9
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds [10000 x i32], ptr %5, i64 0, i64 %47
  %49 = load i32, ptr %48, align 4, !tbaa !9
  %50 = add nsw i32 %49, 1
  %51 = load i32, ptr %6, align 4, !tbaa !9
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds [10000 x i32], ptr %5, i64 0, i64 %52
  store i32 %50, ptr %53, align 4, !tbaa !9
  br label %54

54:                                               ; preds = %45, %34, %22
  br label %55

55:                                               ; preds = %54
  %56 = load i32, ptr %7, align 4, !tbaa !9
  %57 = add nsw i32 %56, 1
  store i32 %57, ptr %7, align 4, !tbaa !9
  br label %18, !llvm.loop !11

58:                                               ; preds = %18
  br label %59

59:                                               ; preds = %58
  %60 = load i32, ptr %6, align 4, !tbaa !9
  %61 = add nsw i32 %60, 1
  store i32 %61, ptr %6, align 4, !tbaa !9
  br label %10, !llvm.loop !13

62:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  store i32 -99999, ptr %8, align 4, !tbaa !9
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %63

63:                                               ; preds = %80, %62
  %64 = load i32, ptr %6, align 4, !tbaa !9
  %65 = load i32, ptr %4, align 4, !tbaa !9
  %66 = icmp slt i32 %64, %65
  br i1 %66, label %67, label %83

67:                                               ; preds = %63
  %68 = load i32, ptr %8, align 4, !tbaa !9
  %69 = load i32, ptr %6, align 4, !tbaa !9
  %70 = sext i32 %69 to i64
  %71 = getelementptr inbounds [10000 x i32], ptr %5, i64 0, i64 %70
  %72 = load i32, ptr %71, align 4, !tbaa !9
  %73 = icmp slt i32 %68, %72
  br i1 %73, label %74, label %79

74:                                               ; preds = %67
  %75 = load i32, ptr %6, align 4, !tbaa !9
  %76 = sext i32 %75 to i64
  %77 = getelementptr inbounds [10000 x i32], ptr %5, i64 0, i64 %76
  %78 = load i32, ptr %77, align 4, !tbaa !9
  store i32 %78, ptr %8, align 4, !tbaa !9
  br label %79

79:                                               ; preds = %74, %67
  br label %80

80:                                               ; preds = %79
  %81 = load i32, ptr %6, align 4, !tbaa !9
  %82 = add nsw i32 %81, 1
  store i32 %82, ptr %6, align 4, !tbaa !9
  br label %63, !llvm.loop !14

83:                                               ; preds = %63
  %84 = load i32, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 40000, ptr %5) #2
  ret i32 %84
}

; 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}
!14 = distinct !{!14, !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 -99999, -2147483648) i32 @lis(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = alloca [10000 x i32], align 16
  call void @llvm.lifetime.start.p0(i64 40000, ptr nonnull %3) #3
  store i32 1, ptr %3, align 16, !tbaa !5
  %4 = icmp sgt i32 %1, 1
  br i1 %4, label %.lr.ph.preheader, label %.preheader

.lr.ph.preheader:                                 ; preds = %2
  %wide.trip.count36 = zext nneg i32 %1 to i64
  br label %.lr.ph

.preheader:                                       ; preds = %.epilog-lcssa, %2
  %5 = icmp sgt i32 %1, 0
  br i1 %5, label %.lr.ph31.preheader, label %._crit_edge

.lr.ph31.preheader:                               ; preds = %.preheader
  %wide.trip.count = zext nneg i32 %1 to i64
  %min.iters.check = icmp ult i32 %1, 8
  br i1 %min.iters.check, label %.lr.ph31.preheader44, label %vector.ph

.lr.ph31.preheader44:                             ; preds = %middle.block, %.lr.ph31.preheader
  %indvars.iv38.ph = phi i64 [ 0, %.lr.ph31.preheader ], [ %n.vec, %middle.block ]
  %.030.ph = phi i32 [ -99999, %.lr.ph31.preheader ], [ %11, %middle.block ]
  br label %.lr.ph31

vector.ph:                                        ; preds = %.lr.ph31.preheader
  %n.vec = and i64 %wide.trip.count, 2147483640
  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> [ <i32 -99999, i32 -99999, i32 -99999, i32 -99999>, %vector.ph ], [ %8, %vector.body ]
  %vec.phi42 = phi <4 x i32> [ <i32 -99999, i32 -99999, i32 -99999, i32 -99999>, %vector.ph ], [ %9, %vector.body ]
  %6 = getelementptr inbounds [10000 x i32], ptr %3, i64 0, i64 %index
  %7 = getelementptr inbounds i8, ptr %6, i64 16
  %wide.load = load <4 x i32>, ptr %6, align 16, !tbaa !5
  %wide.load43 = load <4 x i32>, ptr %7, align 16, !tbaa !5
  %8 = tail call <4 x i32> @llvm.smax.v4i32(<4 x i32> %vec.phi, <4 x i32> %wide.load)
  %9 = tail call <4 x i32> @llvm.smax.v4i32(<4 x i32> %vec.phi42, <4 x i32> %wide.load43)
  %index.next = add nuw i64 %index, 8
  %10 = icmp eq i64 %index.next, %n.vec
  br i1 %10, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %rdx.minmax = tail call <4 x i32> @llvm.smax.v4i32(<4 x i32> %8, <4 x i32> %9)
  %11 = tail call i32 @llvm.vector.reduce.smax.v4i32(<4 x i32> %rdx.minmax)
  %cmp.n = icmp eq i64 %n.vec, %wide.trip.count
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph31.preheader44

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.epilog-lcssa
  %indvar = phi i64 [ 0, %.lr.ph.preheader ], [ %indvar.next, %.epilog-lcssa ]
  %indvars.iv33 = phi i64 [ 1, %.lr.ph.preheader ], [ %indvars.iv.next34, %.epilog-lcssa ]
  %12 = getelementptr inbounds [10000 x i32], ptr %3, i64 0, i64 %indvars.iv33
  store i32 1, ptr %12, align 4, !tbaa !5
  %13 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv33
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %xtraiter = and i64 %indvars.iv33, 1
  %15 = icmp eq i64 %indvar, 0
  br i1 %15, label %.unr-lcssa, label %.lr.ph.new

.lr.ph.new:                                       ; preds = %.lr.ph
  %unroll_iter = and i64 %indvars.iv33, 9223372036854775806
  br label %16

16:                                               ; preds = %36, %.lr.ph.new
  %17 = phi i32 [ 1, %.lr.ph.new ], [ %37, %36 ]
  %indvars.iv = phi i64 [ 0, %.lr.ph.new ], [ %indvars.iv.next.1, %36 ]
  %niter = phi i64 [ 0, %.lr.ph.new ], [ %niter.next.1, %36 ]
  %18 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv
  %19 = load i32, ptr %18, align 4, !tbaa !5
  %20 = icmp sgt i32 %14, %19
  br i1 %20, label %21, label %26

21:                                               ; preds = %16
  %22 = getelementptr inbounds [10000 x i32], ptr %3, i64 0, i64 %indvars.iv
  %23 = load i32, ptr %22, align 8, !tbaa !5
  %.not = icmp sgt i32 %17, %23
  br i1 %.not, label %26, label %24

24:                                               ; preds = %21
  %25 = add nsw i32 %23, 1
  store i32 %25, ptr %12, align 4, !tbaa !5
  br label %26

26:                                               ; preds = %16, %21, %24
  %27 = phi i32 [ %17, %16 ], [ %17, %21 ], [ %25, %24 ]
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %28 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.next
  %29 = load i32, ptr %28, align 4, !tbaa !5
  %30 = icmp sgt i32 %14, %29
  br i1 %30, label %31, label %36

31:                                               ; preds = %26
  %32 = getelementptr inbounds [10000 x i32], ptr %3, i64 0, i64 %indvars.iv.next
  %33 = load i32, ptr %32, align 4, !tbaa !5
  %.not.1 = icmp sgt i32 %27, %33
  br i1 %.not.1, label %36, label %34

34:                                               ; preds = %31
  %35 = add nsw i32 %33, 1
  store i32 %35, ptr %12, align 4, !tbaa !5
  br label %36

36:                                               ; preds = %34, %31, %26
  %37 = phi i32 [ %27, %26 ], [ %27, %31 ], [ %35, %34 ]
  %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 %.unr-lcssa, label %16, !llvm.loop !13

.unr-lcssa:                                       ; preds = %36, %.lr.ph
  %.unr = phi i32 [ 1, %.lr.ph ], [ %37, %36 ]
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next.1, %36 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.epilog-lcssa, label %.epil.preheader

.epil.preheader:                                  ; preds = %.unr-lcssa
  %38 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.unr
  %39 = load i32, ptr %38, align 4, !tbaa !5
  %40 = icmp sgt i32 %14, %39
  br i1 %40, label %41, label %.epilog-lcssa

41:                                               ; preds = %.epil.preheader
  %42 = getelementptr inbounds [10000 x i32], ptr %3, i64 0, i64 %indvars.iv.unr
  %43 = load i32, ptr %42, align 4, !tbaa !5
  %.not.epil = icmp sgt i32 %.unr, %43
  br i1 %.not.epil, label %.epilog-lcssa, label %44

44:                                               ; preds = %41
  %45 = add nsw i32 %43, 1
  store i32 %45, ptr %12, align 4, !tbaa !5
  br label %.epilog-lcssa

.epilog-lcssa:                                    ; preds = %.epil.preheader, %41, %44, %.unr-lcssa
  %indvars.iv.next34 = add nuw nsw i64 %indvars.iv33, 1
  %exitcond37.not = icmp eq i64 %indvars.iv.next34, %wide.trip.count36
  %indvar.next = add i64 %indvar, 1
  br i1 %exitcond37.not, label %.preheader, label %.lr.ph, !llvm.loop !14

.lr.ph31:                                         ; preds = %.lr.ph31.preheader44, %.lr.ph31
  %indvars.iv38 = phi i64 [ %indvars.iv.next39, %.lr.ph31 ], [ %indvars.iv38.ph, %.lr.ph31.preheader44 ]
  %.030 = phi i32 [ %spec.select, %.lr.ph31 ], [ %.030.ph, %.lr.ph31.preheader44 ]
  %46 = getelementptr inbounds [10000 x i32], ptr %3, i64 0, i64 %indvars.iv38
  %47 = load i32, ptr %46, align 4, !tbaa !5
  %spec.select = tail call i32 @llvm.smax.i32(i32 %.030, i32 %47)
  %indvars.iv.next39 = add nuw nsw i64 %indvars.iv38, 1
  %exitcond41.not = icmp eq i64 %indvars.iv.next39, %wide.trip.count
  br i1 %exitcond41.not, label %._crit_edge, label %.lr.ph31, !llvm.loop !15

._crit_edge:                                      ; preds = %.lr.ph31, %middle.block, %.preheader
  %.0.lcssa = phi i32 [ -99999, %.preheader ], [ %11, %middle.block ], [ %spec.select, %.lr.ph31 ]
  call void @llvm.lifetime.end.p0(i64 40000, ptr nonnull %3) #3
  ret i32 %.0.lcssa
}

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

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

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

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

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

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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10, !11, !12}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!"llvm.loop.isvectorized", i32 1}
!12 = !{!"llvm.loop.unroll.runtime.disable"}
!13 = distinct !{!13, !10}
!14 = distinct !{!14, !10}
!15 = distinct !{!15, !10, !12, !11}


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