; LLVM IR from Muhammad-Magdi/problem-solving :: CSES/Dynamic Programming (cses1636)

; 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
@x = external dso_local global i32, align 4
@A = external dso_local global [105 x i32], align 16
@dp = external dso_local global [1000005 x i32], align 16
@.str = external hidden unnamed_addr constant [6 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() #0 {
  %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
  store i32 0, ptr %1, align 4
  %6 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @n, ptr noundef @x)
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %7

7:                                                ; preds = %17, %0
  %8 = load i32, ptr %2, align 4, !tbaa !5
  %9 = load i32, ptr @n, align 4, !tbaa !5
  %10 = icmp slt i32 %8, %9
  br i1 %10, label %12, label %11

11:                                               ; preds = %7
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  br label %20

12:                                               ; preds = %7
  %13 = load i32, ptr %2, align 4, !tbaa !5
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds i32, ptr @A, i64 %14
  %16 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, ptr noundef %15)
  br label %17

17:                                               ; preds = %12
  %18 = load i32, ptr %2, align 4, !tbaa !5
  %19 = add nsw i32 %18, 1
  store i32 %19, ptr %2, align 4, !tbaa !5
  br label %7, !llvm.loop !9

20:                                               ; preds = %11
  store i32 1, ptr @dp, align 16, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %21

21:                                               ; preds = %67, %20
  %22 = load i32, ptr %3, align 4, !tbaa !5
  %23 = load i32, ptr @n, align 4, !tbaa !5
  %24 = icmp slt i32 %22, %23
  br i1 %24, label %26, label %25

25:                                               ; preds = %21
  store i32 5, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  br label %70

26:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %27 = load i32, ptr %3, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [105 x i32], ptr @A, i64 0, i64 %28
  %30 = load i32, ptr %29, align 4, !tbaa !5
  store i32 %30, ptr %5, align 4, !tbaa !5
  br label %31

31:                                               ; preds = %63, %26
  %32 = load i32, ptr %5, align 4, !tbaa !5
  %33 = load i32, ptr @x, align 4, !tbaa !5
  %34 = icmp sle i32 %32, %33
  br i1 %34, label %36, label %35

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

36:                                               ; preds = %31
  %37 = load i32, ptr %5, align 4, !tbaa !5
  %38 = load i32, ptr %3, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [105 x i32], ptr @A, i64 0, i64 %39
  %41 = load i32, ptr %40, align 4, !tbaa !5
  %42 = sub nsw i32 %37, %41
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds [1000005 x i32], ptr @dp, i64 0, i64 %43
  %45 = load i32, ptr %44, align 4, !tbaa !5
  %46 = load i32, ptr %5, align 4, !tbaa !5
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds [1000005 x i32], ptr @dp, i64 0, i64 %47
  %49 = load i32, ptr %48, align 4, !tbaa !5
  %50 = add nsw i32 %49, %45
  store i32 %50, ptr %48, align 4, !tbaa !5
  %51 = load i32, ptr %5, align 4, !tbaa !5
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds [1000005 x i32], ptr @dp, i64 0, i64 %52
  %54 = load i32, ptr %53, align 4, !tbaa !5
  %55 = icmp sge i32 %54, 1000000007
  br i1 %55, label %56, label %62

56:                                               ; preds = %36
  %57 = load i32, ptr %5, align 4, !tbaa !5
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds [1000005 x i32], ptr @dp, i64 0, i64 %58
  %60 = load i32, ptr %59, align 4, !tbaa !5
  %61 = sub nsw i32 %60, 1000000007
  store i32 %61, ptr %59, align 4, !tbaa !5
  br label %62

62:                                               ; preds = %56, %36
  br label %63

63:                                               ; preds = %62
  %64 = load i32, ptr %5, align 4, !tbaa !5
  %65 = add nsw i32 %64, 1
  store i32 %65, ptr %5, align 4, !tbaa !5
  br label %31, !llvm.loop !11

66:                                               ; preds = %35
  br label %67

67:                                               ; preds = %66
  %68 = load i32, ptr %3, align 4, !tbaa !5
  %69 = add nsw i32 %68, 1
  store i32 %69, ptr %3, align 4, !tbaa !5
  br label %21, !llvm.loop !12

70:                                               ; preds = %25
  %71 = load i32, ptr @x, align 4, !tbaa !5
  %72 = sext i32 %71 to i64
  %73 = getelementptr inbounds [1000005 x i32], ptr @dp, i64 0, i64 %72
  %74 = load i32, ptr %73, align 4, !tbaa !5
  %75 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i32 noundef %74)
  ret i32 0
}

declare i32 @__isoc99_scanf(ptr noundef, ...) #1

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

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

declare i32 @printf(ptr noundef, ...) #1

attributes #0 = { mustprogress norecurse 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 = { "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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 global i32, align 4
@x = external dso_local global i32, align 4
@A = external dso_local global [105 x i32], align 16
@dp = external dso_local local_unnamed_addr global [1000005 x i32], align 16
@.str = external hidden unnamed_addr constant [6 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress nofree norecurse nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @n, ptr noundef nonnull @x)
  %2 = load i32, ptr @n, align 4, !tbaa !5
  %3 = icmp sgt i32 %2, 0
  br i1 %3, label %.lr.ph, label %._crit_edge.thread

._crit_edge.thread:                               ; preds = %0
  store i32 1, ptr @dp, align 16, !tbaa !5
  %.pre36 = load i32, ptr @x, align 4, !tbaa !5
  br label %._crit_edge25

._crit_edge:                                      ; preds = %.lr.ph
  store i32 1, ptr @dp, align 16, !tbaa !5
  %4 = icmp sgt i32 %8, 0
  %.pre = load i32, ptr @x, align 4, !tbaa !5
  br i1 %4, label %.lr.ph24, label %._crit_edge25

.lr.ph24:                                         ; preds = %._crit_edge
  %5 = add i32 %.pre, 1
  %wide.trip.count = zext nneg i32 %8 to i64
  br label %15

.lr.ph:                                           ; preds = %0, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %0 ]
  %6 = getelementptr inbounds i32, ptr @A, i64 %indvars.iv
  %7 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull %6)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %8 = load i32, ptr @n, align 4, !tbaa !5
  %9 = sext i32 %8 to i64
  %10 = icmp slt i64 %indvars.iv.next, %9
  br i1 %10, label %.lr.ph, label %._crit_edge, !llvm.loop !9

._crit_edge25:                                    ; preds = %._crit_edge21, %._crit_edge.thread, %._crit_edge
  %.pre37 = phi i32 [ %.pre36, %._crit_edge.thread ], [ %.pre, %._crit_edge ], [ %.pre, %._crit_edge21 ]
  %11 = sext i32 %.pre37 to i64
  %12 = getelementptr inbounds [1000005 x i32], ptr @dp, i64 0, i64 %11
  %13 = load i32, ptr %12, align 4, !tbaa !5
  %14 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %13)
  ret i32 0

15:                                               ; preds = %.lr.ph24, %._crit_edge21
  %indvars.iv31 = phi i64 [ 0, %.lr.ph24 ], [ %indvars.iv.next32, %._crit_edge21 ]
  %16 = getelementptr inbounds [105 x i32], ptr @A, i64 0, i64 %indvars.iv31
  %17 = load i32, ptr %16, align 4, !tbaa !5
  %.not17 = icmp sgt i32 %17, %.pre
  br i1 %.not17, label %._crit_edge21, label %.lr.ph20.preheader

.lr.ph20.preheader:                               ; preds = %15
  %18 = sext i32 %17 to i64
  %19 = sub i32 %.pre, %17
  %20 = zext i32 %19 to i64
  %21 = add nuw nsw i64 %20, 1
  %min.iters.check = icmp ult i32 %19, 7
  br i1 %min.iters.check, label %.lr.ph20.preheader43, label %vector.memcheck

.lr.ph20.preheader43:                             ; preds = %middle.block, %vector.memcheck, %.lr.ph20.preheader
  %indvars.iv28.ph = phi i64 [ %18, %vector.memcheck ], [ %18, %.lr.ph20.preheader ], [ %ind.end, %middle.block ]
  %22 = trunc i64 %indvars.iv28.ph to i32
  %23 = add i32 %.pre, %22
  %24 = and i32 %23, 1
  %lcmp.mod.not.not = icmp eq i32 %24, 0
  br i1 %lcmp.mod.not.not, label %.lr.ph20.prol, label %.lr.ph20.prol.loopexit

.lr.ph20.prol:                                    ; preds = %.lr.ph20.preheader43
  %25 = sub nsw i64 %indvars.iv28.ph, %18
  %26 = getelementptr inbounds [1000005 x i32], ptr @dp, i64 0, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %28 = getelementptr inbounds [1000005 x i32], ptr @dp, i64 0, i64 %indvars.iv28.ph
  %29 = load i32, ptr %28, align 4, !tbaa !5
  %30 = add nsw i32 %29, %27
  %31 = icmp sgt i32 %30, 1000000006
  %32 = add nsw i32 %30, -1000000007
  %spec.select.prol = select i1 %31, i32 %32, i32 %30
  store i32 %spec.select.prol, ptr %28, align 4, !tbaa !5
  %indvars.iv.next29.prol = add nsw i64 %indvars.iv28.ph, 1
  br label %.lr.ph20.prol.loopexit

.lr.ph20.prol.loopexit:                           ; preds = %.lr.ph20.prol, %.lr.ph20.preheader43
  %indvars.iv28.unr = phi i64 [ %indvars.iv28.ph, %.lr.ph20.preheader43 ], [ %indvars.iv.next29.prol, %.lr.ph20.prol ]
  %33 = icmp eq i32 %.pre, %22
  br i1 %33, label %._crit_edge21, label %.lr.ph20

vector.memcheck:                                  ; preds = %.lr.ph20.preheader
  %34 = shl nsw i64 %18, 2
  %scevgep = getelementptr i8, ptr @dp, i64 %34
  %35 = sub i32 %.pre, %17
  %36 = zext i32 %35 to i64
  %37 = shl nuw nsw i64 %36, 2
  %38 = getelementptr i8, ptr getelementptr inbounds (i8, ptr @dp, i64 4), i64 %34
  %scevgep38 = getelementptr i8, ptr %38, i64 %37
  %scevgep39 = getelementptr i8, ptr getelementptr inbounds (i8, ptr @dp, i64 4), i64 %37
  %bound0 = icmp ult ptr %scevgep, %scevgep39
  %bound1 = icmp ugt ptr %scevgep38, @dp
  %found.conflict = and i1 %bound0, %bound1
  br i1 %found.conflict, label %.lr.ph20.preheader43, label %vector.ph

vector.ph:                                        ; preds = %vector.memcheck
  %n.vec = and i64 %21, 8589934584
  %ind.end = add nsw i64 %n.vec, %18
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %offset.idx = add i64 %index, %18
  %39 = getelementptr inbounds [1000005 x i32], ptr @dp, i64 0, i64 %index
  %40 = getelementptr inbounds i8, ptr %39, i64 16
  %wide.load = load <4 x i32>, ptr %39, align 16, !tbaa !5, !alias.scope !11
  %wide.load40 = load <4 x i32>, ptr %40, align 16, !tbaa !5, !alias.scope !11
  %41 = getelementptr inbounds [1000005 x i32], ptr @dp, i64 0, i64 %offset.idx
  %42 = getelementptr inbounds i8, ptr %41, i64 16
  %wide.load41 = load <4 x i32>, ptr %41, align 4, !tbaa !5, !alias.scope !14, !noalias !11
  %wide.load42 = load <4 x i32>, ptr %42, align 4, !tbaa !5, !alias.scope !14, !noalias !11
  %43 = add nsw <4 x i32> %wide.load41, %wide.load
  %44 = add nsw <4 x i32> %wide.load42, %wide.load40
  %45 = icmp sgt <4 x i32> %43, <i32 1000000006, i32 1000000006, i32 1000000006, i32 1000000006>
  %46 = icmp sgt <4 x i32> %44, <i32 1000000006, i32 1000000006, i32 1000000006, i32 1000000006>
  %47 = add nsw <4 x i32> %43, <i32 -1000000007, i32 -1000000007, i32 -1000000007, i32 -1000000007>
  %48 = add nsw <4 x i32> %44, <i32 -1000000007, i32 -1000000007, i32 -1000000007, i32 -1000000007>
  %49 = select <4 x i1> %45, <4 x i32> %47, <4 x i32> %43
  %50 = select <4 x i1> %46, <4 x i32> %48, <4 x i32> %44
  store <4 x i32> %49, ptr %41, align 4, !tbaa !5, !alias.scope !14, !noalias !11
  store <4 x i32> %50, ptr %42, align 4, !tbaa !5, !alias.scope !14, !noalias !11
  %index.next = add nuw i64 %index, 8
  %51 = icmp eq i64 %index.next, %n.vec
  br i1 %51, label %middle.block, label %vector.body, !llvm.loop !16

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %21, %n.vec
  br i1 %cmp.n, label %._crit_edge21, label %.lr.ph20.preheader43

._crit_edge21:                                    ; preds = %.lr.ph20.prol.loopexit, %.lr.ph20, %middle.block, %15
  %indvars.iv.next32 = add nuw nsw i64 %indvars.iv31, 1
  %exitcond34.not = icmp eq i64 %indvars.iv.next32, %wide.trip.count
  br i1 %exitcond34.not, label %._crit_edge25, label %15, !llvm.loop !19

.lr.ph20:                                         ; preds = %.lr.ph20.prol.loopexit, %.lr.ph20
  %indvars.iv28 = phi i64 [ %indvars.iv.next29.1, %.lr.ph20 ], [ %indvars.iv28.unr, %.lr.ph20.prol.loopexit ]
  %52 = sub nsw i64 %indvars.iv28, %18
  %53 = getelementptr inbounds [1000005 x i32], ptr @dp, i64 0, i64 %52
  %54 = load i32, ptr %53, align 4, !tbaa !5
  %55 = getelementptr inbounds [1000005 x i32], ptr @dp, i64 0, i64 %indvars.iv28
  %56 = load i32, ptr %55, align 4, !tbaa !5
  %57 = add nsw i32 %56, %54
  %58 = icmp sgt i32 %57, 1000000006
  %59 = add nsw i32 %57, -1000000007
  %spec.select = select i1 %58, i32 %59, i32 %57
  store i32 %spec.select, ptr %55, align 4, !tbaa !5
  %indvars.iv.next29 = add nsw i64 %indvars.iv28, 1
  %60 = sub nsw i64 %indvars.iv.next29, %18
  %61 = getelementptr inbounds [1000005 x i32], ptr @dp, i64 0, i64 %60
  %62 = load i32, ptr %61, align 4, !tbaa !5
  %63 = getelementptr inbounds [1000005 x i32], ptr @dp, i64 0, i64 %indvars.iv.next29
  %64 = load i32, ptr %63, align 4, !tbaa !5
  %65 = add nsw i32 %64, %62
  %66 = icmp sgt i32 %65, 1000000006
  %67 = add nsw i32 %65, -1000000007
  %spec.select.1 = select i1 %66, i32 %67, i32 %65
  store i32 %spec.select.1, ptr %63, align 4, !tbaa !5
  %indvars.iv.next29.1 = add nsw i64 %indvars.iv28, 2
  %lftr.wideiv.1 = trunc i64 %indvars.iv.next29.1 to i32
  %exitcond.not.1 = icmp eq i32 %5, %lftr.wideiv.1
  br i1 %exitcond.not.1, label %._crit_edge21, label %.lr.ph20, !llvm.loop !20
}

; Function Attrs: nofree nounwind
declare noundef i32 @__isoc99_scanf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

attributes #0 = { mustprogress nofree norecurse 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 = { nofree 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" }

!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 = !{!12}
!12 = distinct !{!12, !13}
!13 = distinct !{!13, !"LVerDomain"}
!14 = !{!15}
!15 = distinct !{!15, !13}
!16 = distinct !{!16, !10, !17, !18}
!17 = !{!"llvm.loop.isvectorized", i32 1}
!18 = !{!"llvm.loop.unroll.runtime.disable"}
!19 = distinct !{!19, !10}
!20 = distinct !{!20, !10, !17}


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