; LLVM IR from Muhammad-Magdi/problem-solving :: LIS.ll (Z12iterativeLISv)

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

@n = external dso_local global i32, align 4
@A = external dso_local global [1009 x i32], align 16
@dp = external dso_local global [1009 x [1009 x i32]], align 16

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) #1

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z12iterativeLISv() #2 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #3
  store i32 0, ptr %1, align 4, !tbaa !5
  br label %6

6:                                                ; preds = %18, %0
  %7 = load i32, ptr %1, align 4, !tbaa !5
  %8 = load i32, ptr @n, align 4, !tbaa !5
  %9 = icmp sle i32 %7, %8
  br i1 %9, label %11, label %10

10:                                               ; preds = %6
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #3
  br label %21

11:                                               ; preds = %6
  %12 = load i32, ptr @n, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [1009 x [1009 x i32]], ptr @dp, i64 0, i64 %13
  %15 = load i32, ptr %1, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [1009 x i32], ptr %14, i64 0, i64 %16
  store i32 0, ptr %17, align 4, !tbaa !5
  br label %18

18:                                               ; preds = %11
  %19 = load i32, ptr %1, align 4, !tbaa !5
  %20 = add nsw i32 %19, 1
  store i32 %20, ptr %1, align 4, !tbaa !5
  br label %6, !llvm.loop !9

21:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  %22 = load i32, ptr @n, align 4, !tbaa !5
  %23 = sub nsw i32 %22, 1
  store i32 %23, ptr %2, align 4, !tbaa !5
  br label %24

24:                                               ; preds = %82, %21
  %25 = load i32, ptr %2, align 4, !tbaa !5
  %26 = xor i32 %25, -1
  %27 = icmp ne i32 %26, 0
  br i1 %27, label %29, label %28

28:                                               ; preds = %24
  store i32 5, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  br label %85

29:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  %30 = load i32, ptr @n, align 4, !tbaa !5
  store i32 %30, ptr %4, align 4, !tbaa !5
  br label %31

31:                                               ; preds = %78, %29
  %32 = load i32, ptr %4, align 4, !tbaa !5
  %33 = xor i32 %32, -1
  %34 = icmp ne i32 %33, 0
  br i1 %34, label %36, label %35

35:                                               ; preds = %31
  store i32 8, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  br label %81

36:                                               ; preds = %31
  %37 = load i32, ptr %2, align 4, !tbaa !5
  %38 = add nsw i32 %37, 1
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [1009 x [1009 x i32]], ptr @dp, i64 0, i64 %39
  %41 = load i32, ptr %4, align 4, !tbaa !5
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds [1009 x i32], ptr %40, i64 0, i64 %42
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %44 = load i32, ptr %4, align 4, !tbaa !5
  %45 = load i32, ptr @n, align 4, !tbaa !5
  %46 = icmp eq i32 %44, %45
  br i1 %46, label %57, label %47

47:                                               ; preds = %36
  %48 = load i32, ptr %2, align 4, !tbaa !5
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds [1009 x i32], ptr @A, i64 0, i64 %49
  %51 = load i32, ptr %50, align 4, !tbaa !5
  %52 = load i32, ptr %4, align 4, !tbaa !5
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds [1009 x i32], ptr @A, i64 0, i64 %53
  %55 = load i32, ptr %54, align 4, !tbaa !5
  %56 = icmp sgt i32 %51, %55
  br i1 %56, label %57, label %67

57:                                               ; preds = %47, %36
  %58 = load i32, ptr %2, align 4, !tbaa !5
  %59 = add nsw i32 %58, 1
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds [1009 x [1009 x i32]], ptr @dp, i64 0, i64 %60
  %62 = load i32, ptr %2, align 4, !tbaa !5
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds [1009 x i32], ptr %61, i64 0, i64 %63
  %65 = load i32, ptr %64, align 4, !tbaa !5
  %66 = add nsw i32 %65, 1
  br label %68

67:                                               ; preds = %47
  br label %68

68:                                               ; preds = %67, %57
  %69 = phi i32 [ %66, %57 ], [ 0, %67 ]
  store i32 %69, ptr %5, align 4, !tbaa !5
  %70 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %43, ptr noundef nonnull align 4 dereferenceable(4) %5)
  %71 = load i32, ptr %70, align 4, !tbaa !5
  %72 = load i32, ptr %2, align 4, !tbaa !5
  %73 = sext i32 %72 to i64
  %74 = getelementptr inbounds [1009 x [1009 x i32]], ptr @dp, i64 0, i64 %73
  %75 = load i32, ptr %4, align 4, !tbaa !5
  %76 = sext i32 %75 to i64
  %77 = getelementptr inbounds [1009 x i32], ptr %74, i64 0, i64 %76
  store i32 %71, ptr %77, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  br label %78

78:                                               ; preds = %68
  %79 = load i32, ptr %4, align 4, !tbaa !5
  %80 = add nsw i32 %79, -1
  store i32 %80, ptr %4, align 4, !tbaa !5
  br label %31, !llvm.loop !11

81:                                               ; preds = %35
  br label %82

82:                                               ; preds = %81
  %83 = load i32, ptr %2, align 4, !tbaa !5
  %84 = add nsw i32 %83, -1
  store i32 %84, ptr %2, align 4, !tbaa !5
  br label %24, !llvm.loop !12

85:                                               ; preds = %28
  %86 = load i32, ptr @n, align 4, !tbaa !5
  %87 = sext i32 %86 to i64
  %88 = getelementptr inbounds [1009 x i32], ptr @dp, i64 0, i64 %87
  %89 = load i32, ptr %88, align 4, !tbaa !5
  ret i32 %89
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { inlinehint mustprogress 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 = { mustprogress 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !10}


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

@n = external dso_local local_unnamed_addr global i32, align 4
@A = external dso_local local_unnamed_addr global [1009 x i32], align 16
@dp = external dso_local global [1009 x [1009 x i32]], align 16

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #1

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z12iterativeLISv() local_unnamed_addr #2 {
  %1 = alloca i32, align 4
  %2 = load i32, ptr @n, align 4, !tbaa !5
  %.not21 = icmp slt i32 %2, 0
  br i1 %.not21, label %.lr.ph30, label %.preheader

.preheader:                                       ; preds = %0
  %3 = zext nneg i32 %2 to i64
  %4 = mul nuw nsw i64 %3, 4036
  %scevgep = getelementptr i8, ptr @dp, i64 %4
  %5 = add nuw i32 %2, 1
  %6 = zext i32 %5 to i64
  %7 = shl nuw nsw i64 %6, 2
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 dereferenceable(1) %scevgep, i8 0, i64 %7, i1 false), !tbaa !5
  %.not1927 = icmp eq i32 %2, 0
  br i1 %.not1927, label %._crit_edge, label %.lr.ph30.split.preheader

.lr.ph30:                                         ; preds = %0
  %8 = icmp eq i32 %2, -1
  br i1 %8, label %._crit_edge, label %.lr.ph30.split.preheader

.lr.ph30.split.preheader:                         ; preds = %.preheader, %.lr.ph30
  %.01626 = add i32 %2, -1
  %9 = sext i32 %.01626 to i64
  br label %.lr.ph30.split

.loopexit:                                        ; preds = %34, %.lr.ph30.split
  %indvars.iv.next35 = add nsw i64 %indvars.iv34, -1
  %10 = icmp eq i64 %indvars.iv34, 0
  %11 = trunc nsw i64 %indvars.iv34 to i32
  br i1 %10, label %._crit_edge.loopexit31, label %.lr.ph30.split, !llvm.loop !9

._crit_edge.loopexit31:                           ; preds = %.loopexit
  %.pre = load i32, ptr @n, align 4, !tbaa !5
  %12 = sext i32 %.pre to i64
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph30, %._crit_edge.loopexit31, %.preheader
  %13 = phi i64 [ %12, %._crit_edge.loopexit31 ], [ 0, %.preheader ], [ -1, %.lr.ph30 ]
  %14 = getelementptr inbounds [1009 x i32], ptr @dp, i64 0, i64 %13
  %15 = load i32, ptr %14, align 4, !tbaa !5
  ret i32 %15

.lr.ph30.split:                                   ; preds = %.lr.ph30.split.preheader, %.loopexit
  %indvars.iv34 = phi i64 [ %9, %.lr.ph30.split.preheader ], [ %indvars.iv.next35, %.loopexit ]
  %.016.in28 = phi i32 [ %2, %.lr.ph30.split.preheader ], [ %11, %.loopexit ]
  %16 = load i32, ptr @n, align 4, !tbaa !5
  %.not2023 = icmp eq i32 %16, -1
  br i1 %.not2023, label %.loopexit, label %.lr.ph25

.lr.ph25:                                         ; preds = %.lr.ph30.split
  %17 = sext i32 %.016.in28 to i64
  %18 = getelementptr inbounds [1009 x i32], ptr @A, i64 0, i64 %indvars.iv34
  %19 = getelementptr inbounds [1009 x [1009 x i32]], ptr @dp, i64 0, i64 %17, i64 %indvars.iv34
  %20 = sext i32 %16 to i64
  br label %21

21:                                               ; preds = %.lr.ph25, %34
  %indvars.iv = phi i64 [ %20, %.lr.ph25 ], [ %indvars.iv.next, %34 ]
  %22 = getelementptr inbounds [1009 x [1009 x i32]], ptr @dp, i64 0, i64 %17, i64 %indvars.iv
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %1) #4
  %23 = load i32, ptr @n, align 4, !tbaa !5
  %24 = trunc nsw i64 %indvars.iv to i32
  %25 = icmp eq i32 %23, %24
  br i1 %25, label %31, label %26

26:                                               ; preds = %21
  %27 = load i32, ptr %18, align 4, !tbaa !5
  %28 = getelementptr inbounds [1009 x i32], ptr @A, i64 0, i64 %indvars.iv
  %29 = load i32, ptr %28, align 4, !tbaa !5
  %30 = icmp sgt i32 %27, %29
  br i1 %30, label %31, label %34

31:                                               ; preds = %26, %21
  %32 = load i32, ptr %19, align 4, !tbaa !5
  %33 = add nsw i32 %32, 1
  br label %34

34:                                               ; preds = %26, %31
  %35 = phi i32 [ %33, %31 ], [ 0, %26 ]
  store i32 %35, ptr %1, align 4, !tbaa !5
  %36 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %22, ptr noundef nonnull align 4 dereferenceable(4) %1)
  %37 = load i32, ptr %36, align 4, !tbaa !5
  %38 = getelementptr inbounds [1009 x [1009 x i32]], ptr @dp, i64 0, i64 %indvars.iv34, i64 %indvars.iv
  store i32 %37, ptr %38, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %1) #4
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %39 = icmp eq i64 %indvars.iv, 0
  br i1 %39, label %.loopexit, label %21, !llvm.loop !12
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #3

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { inlinehint mustprogress 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 = { mustprogress 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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10, !11}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!"llvm.loop.unswitch.partial.disable"}
!12 = distinct !{!12, !10}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LICMPass,LoopIdiomRecognizePass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimpleLoopUnswitchPass,SimplifyCFGPass,TailCallElimPass