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

; 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
@dp = external dso_local global [1000005 x i32], align 16
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = 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
  store i32 0, ptr %1, align 4
  %5 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @n)
  store i32 1, ptr @dp, align 16, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  store i32 1, ptr %2, align 4, !tbaa !5
  br label %6

6:                                                ; preds = %43, %0
  %7 = load i32, ptr %2, 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
  store i32 2, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  br label %46

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

12:                                               ; preds = %39, %11
  %13 = load i32, ptr %4, align 4, !tbaa !5
  %14 = icmp sle i32 %13, 6
  br i1 %14, label %15, label %20

15:                                               ; preds = %12
  %16 = load i32, ptr %2, align 4, !tbaa !5
  %17 = load i32, ptr %4, align 4, !tbaa !5
  %18 = sub nsw i32 %16, %17
  %19 = icmp sge i32 %18, 0
  br label %20

20:                                               ; preds = %15, %12
  %21 = phi i1 [ false, %12 ], [ %19, %15 ]
  br i1 %21, label %23, label %22

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

23:                                               ; preds = %20
  %24 = load i32, ptr %2, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [1000005 x i32], ptr @dp, i64 0, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %28 = load i32, ptr %2, align 4, !tbaa !5
  %29 = load i32, ptr %4, align 4, !tbaa !5
  %30 = sub nsw i32 %28, %29
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [1000005 x i32], ptr @dp, i64 0, i64 %31
  %33 = load i32, ptr %32, align 4, !tbaa !5
  %34 = add nsw i32 %27, %33
  %35 = srem i32 %34, 1000000007
  %36 = load i32, ptr %2, align 4, !tbaa !5
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [1000005 x i32], ptr @dp, i64 0, i64 %37
  store i32 %35, ptr %38, align 4, !tbaa !5
  br label %39

39:                                               ; preds = %23
  %40 = load i32, ptr %4, align 4, !tbaa !5
  %41 = add nsw i32 %40, 1
  store i32 %41, ptr %4, align 4, !tbaa !5
  br label %12, !llvm.loop !9

42:                                               ; preds = %22
  br label %43

43:                                               ; preds = %42
  %44 = load i32, ptr %2, align 4, !tbaa !5
  %45 = add nsw i32 %44, 1
  store i32 %45, ptr %2, align 4, !tbaa !5
  br label %6, !llvm.loop !11

46:                                               ; preds = %10
  %47 = load i32, ptr @n, align 4, !tbaa !5
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [1000005 x i32], ptr @dp, i64 0, i64 %48
  %50 = load i32, ptr %49, align 4, !tbaa !5
  %51 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %50)
  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}


; 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
@dp = external dso_local local_unnamed_addr global [1000005 x i32], align 16
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = 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)
  store i32 1, ptr @dp, align 16, !tbaa !5
  %2 = load i32, ptr @n, align 4, !tbaa !5
  %.not14 = icmp slt i32 %2, 1
  br i1 %.not14, label %._crit_edge16, label %.lr.ph

.lr.ph:                                           ; preds = %0, %._crit_edge
  %indvars.iv20 = phi i64 [ %indvars.iv.next21, %._crit_edge ], [ 1, %0 ]
  %indvars.iv18 = phi i32 [ %indvars.iv.next19, %._crit_edge ], [ 0, %0 ]
  %umin = tail call i32 @llvm.umin.i32(i32 %indvars.iv18, i32 5)
  %3 = add nuw nsw i32 %umin, 2
  %4 = getelementptr inbounds [1000005 x i32], ptr @dp, i64 0, i64 %indvars.iv20
  %5 = add nuw i64 %indvars.iv20, 4294967295
  %.promoted = load i32, ptr %4, align 4, !tbaa !5
  %6 = and i64 %5, 4294967295
  %7 = getelementptr inbounds [1000005 x i32], ptr @dp, i64 0, i64 %6
  %8 = load i32, ptr %7, align 4, !tbaa !5
  %9 = add nsw i32 %8, %.promoted
  %10 = srem i32 %9, 1000000007
  store i32 %10, ptr %4, align 4, !tbaa !5
  %exitcond.not = icmp eq i32 %indvars.iv18, 0
  br i1 %exitcond.not, label %._crit_edge, label %15, !llvm.loop !9

._crit_edge16:                                    ; preds = %._crit_edge, %0
  %11 = sext i32 %2 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.1, i32 noundef %13)
  ret i32 0

._crit_edge:                                      ; preds = %43, %36, %29, %22, %15, %.lr.ph
  %indvars.iv.next21 = add nuw nsw i64 %indvars.iv20, 1
  %indvars.iv.next19 = add nuw nsw i32 %indvars.iv18, 1
  %exitcond23.not = icmp eq i32 %indvars.iv.next19, %2
  br i1 %exitcond23.not, label %._crit_edge16, label %.lr.ph, !llvm.loop !11

15:                                               ; preds = %.lr.ph
  %16 = add nuw i64 %indvars.iv20, 4294967294
  %17 = and i64 %16, 4294967295
  %18 = getelementptr inbounds [1000005 x i32], ptr @dp, i64 0, i64 %17
  %19 = load i32, ptr %18, align 4, !tbaa !5
  %20 = add nsw i32 %19, %10
  %21 = srem i32 %20, 1000000007
  store i32 %21, ptr %4, align 4, !tbaa !5
  %exitcond.not.1 = icmp eq i32 %3, 3
  br i1 %exitcond.not.1, label %._crit_edge, label %22, !llvm.loop !9

22:                                               ; preds = %15
  %23 = add nuw i64 %indvars.iv20, 4294967293
  %24 = and i64 %23, 4294967295
  %25 = getelementptr inbounds [1000005 x i32], ptr @dp, i64 0, i64 %24
  %26 = load i32, ptr %25, align 4, !tbaa !5
  %27 = add nsw i32 %26, %21
  %28 = srem i32 %27, 1000000007
  store i32 %28, ptr %4, align 4, !tbaa !5
  %exitcond.not.2 = icmp eq i32 %3, 4
  br i1 %exitcond.not.2, label %._crit_edge, label %29, !llvm.loop !9

29:                                               ; preds = %22
  %30 = add nuw i64 %indvars.iv20, 4294967292
  %31 = and i64 %30, 4294967295
  %32 = getelementptr inbounds [1000005 x i32], ptr @dp, i64 0, i64 %31
  %33 = load i32, ptr %32, align 4, !tbaa !5
  %34 = add nsw i32 %33, %28
  %35 = srem i32 %34, 1000000007
  store i32 %35, ptr %4, align 4, !tbaa !5
  %exitcond.not.3 = icmp eq i32 %3, 5
  br i1 %exitcond.not.3, label %._crit_edge, label %36, !llvm.loop !9

36:                                               ; preds = %29
  %37 = add nuw i64 %indvars.iv20, 4294967291
  %38 = and i64 %37, 4294967295
  %39 = getelementptr inbounds [1000005 x i32], ptr @dp, i64 0, i64 %38
  %40 = load i32, ptr %39, align 4, !tbaa !5
  %41 = add nsw i32 %40, %35
  %42 = srem i32 %41, 1000000007
  store i32 %42, ptr %4, align 4, !tbaa !5
  %exitcond.not.4 = icmp eq i32 %3, 6
  br i1 %exitcond.not.4, label %._crit_edge, label %43, !llvm.loop !9

43:                                               ; preds = %36
  %44 = add nuw i64 %indvars.iv20, 4294967290
  %45 = and i64 %44, 4294967295
  %46 = getelementptr inbounds [1000005 x i32], ptr @dp, i64 0, i64 %45
  %47 = load i32, ptr %46, align 4, !tbaa !5
  %48 = add nsw i32 %47, %42
  %49 = srem i32 %48, 1000000007
  store i32 %49, ptr %4, align 4, !tbaa !5
  br label %._crit_edge
}

; 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

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

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" }
attributes #2 = { 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 = !{!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}


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