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

; 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 [1003 x [1003 x i8]], align 16
@dp = external dso_local global [1003 x i32], align 16
@.str = external hidden unnamed_addr constant [3 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)
  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 = %18, %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 %21

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

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

21:                                               ; preds = %11
  %22 = load i32, ptr @n, align 4, !tbaa !5
  %23 = sub nsw i32 %22, 1
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [1003 x [1003 x i8]], ptr @A, i64 0, i64 %24
  %26 = load i32, ptr @n, align 4, !tbaa !5
  %27 = sub nsw i32 %26, 1
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [1003 x i8], ptr %25, i64 0, i64 %28
  %30 = load i8, ptr %29, align 1, !tbaa !11
  %31 = sext i8 %30 to i32
  %32 = icmp eq i32 %31, 46
  %33 = zext i1 %32 to i32
  %34 = load i32, ptr @n, align 4, !tbaa !5
  %35 = sub nsw i32 %34, 1
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [1003 x i32], ptr @dp, i64 0, i64 %36
  store i32 %33, ptr %37, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  %38 = load i32, ptr @n, align 4, !tbaa !5
  %39 = sub nsw i32 %38, 1
  store i32 %39, ptr %3, align 4, !tbaa !5
  br label %40

40:                                               ; preds = %97, %21
  %41 = load i32, ptr %3, align 4, !tbaa !5
  %42 = xor i32 %41, -1
  %43 = icmp ne i32 %42, 0
  br i1 %43, label %45, label %44

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

45:                                               ; preds = %40
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %46 = load i32, ptr @n, align 4, !tbaa !5
  %47 = sub nsw i32 %46, 1
  store i32 %47, ptr %5, align 4, !tbaa !5
  br label %48

48:                                               ; preds = %93, %45
  %49 = load i32, ptr %5, align 4, !tbaa !5
  %50 = xor i32 %49, -1
  %51 = icmp ne i32 %50, 0
  br i1 %51, label %53, label %52

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

53:                                               ; preds = %48
  %54 = load i32, ptr %3, align 4, !tbaa !5
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds [1003 x [1003 x i8]], ptr @A, i64 0, i64 %55
  %57 = load i32, ptr %5, align 4, !tbaa !5
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds [1003 x i8], ptr %56, i64 0, i64 %58
  %60 = load i8, ptr %59, align 1, !tbaa !11
  %61 = sext i8 %60 to i32
  %62 = icmp ne i32 %61, 46
  br i1 %62, label %63, label %67

63:                                               ; preds = %53
  %64 = load i32, ptr %5, align 4, !tbaa !5
  %65 = sext i32 %64 to i64
  %66 = getelementptr inbounds [1003 x i32], ptr @dp, i64 0, i64 %65
  store i32 0, ptr %66, align 4, !tbaa !5
  br label %93

67:                                               ; preds = %53
  %68 = load i32, ptr %5, align 4, !tbaa !5
  %69 = sext i32 %68 to i64
  %70 = getelementptr inbounds [1003 x i32], ptr @dp, i64 0, i64 %69
  %71 = load i32, ptr %70, align 4, !tbaa !5
  %72 = load i32, ptr %5, align 4, !tbaa !5
  %73 = add nsw i32 %72, 1
  %74 = sext i32 %73 to i64
  %75 = getelementptr inbounds [1003 x i32], ptr @dp, i64 0, i64 %74
  %76 = load i32, ptr %75, align 4, !tbaa !5
  %77 = add nsw i32 %71, %76
  %78 = load i32, ptr %5, align 4, !tbaa !5
  %79 = sext i32 %78 to i64
  %80 = getelementptr inbounds [1003 x i32], ptr @dp, i64 0, i64 %79
  store i32 %77, ptr %80, align 4, !tbaa !5
  %81 = load i32, ptr %5, align 4, !tbaa !5
  %82 = sext i32 %81 to i64
  %83 = getelementptr inbounds [1003 x i32], ptr @dp, i64 0, i64 %82
  %84 = load i32, ptr %83, align 4, !tbaa !5
  %85 = icmp sge i32 %84, 1000000007
  br i1 %85, label %86, label %92

86:                                               ; preds = %67
  %87 = load i32, ptr %5, align 4, !tbaa !5
  %88 = sext i32 %87 to i64
  %89 = getelementptr inbounds [1003 x i32], ptr @dp, i64 0, i64 %88
  %90 = load i32, ptr %89, align 4, !tbaa !5
  %91 = sub nsw i32 %90, 1000000007
  store i32 %91, ptr %89, align 4, !tbaa !5
  br label %92

92:                                               ; preds = %86, %67
  br label %93

93:                                               ; preds = %92, %63
  %94 = load i32, ptr %5, align 4, !tbaa !5
  %95 = add nsw i32 %94, -1
  store i32 %95, ptr %5, align 4, !tbaa !5
  br label %48, !llvm.loop !12

96:                                               ; preds = %52
  br label %97

97:                                               ; preds = %96
  %98 = load i32, ptr %3, align 4, !tbaa !5
  %99 = add nsw i32 %98, -1
  store i32 %99, ptr %3, align 4, !tbaa !5
  br label %40, !llvm.loop !13

100:                                              ; preds = %44
  %101 = load i32, ptr @dp, align 16, !tbaa !5
  %102 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i32 noundef %101)
  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 = !{!7, !7, i64 0}
!12 = distinct !{!12, !10}
!13 = distinct !{!13, !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
@A = external dso_local global [1003 x [1003 x i8]], align 16
@dp = external dso_local local_unnamed_addr global [1003 x i32], align 16
@.str = external hidden unnamed_addr constant [3 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)
  %2 = load i32, ptr @n, align 4, !tbaa !5
  %3 = icmp sgt i32 %2, 0
  br i1 %3, label %.lr.ph, label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph, %0
  %.lcssa = phi i32 [ %2, %0 ], [ %14, %.lr.ph ]
  %4 = add nsw i32 %.lcssa, -1
  %5 = sext i32 %4 to i64
  %6 = getelementptr inbounds [1003 x [1003 x i8]], ptr @A, i64 0, i64 %5, i64 %5
  %7 = load i8, ptr %6, align 1, !tbaa !9
  %8 = icmp eq i8 %7, 46
  %9 = zext i1 %8 to i32
  %10 = getelementptr inbounds [1003 x i32], ptr @dp, i64 0, i64 %5
  store i32 %9, ptr %10, align 4, !tbaa !5
  %.not28 = icmp eq i32 %.lcssa, 0
  br i1 %.not28, label %._crit_edge30, label %.preheader.lr.ph

.preheader.lr.ph:                                 ; preds = %._crit_edge
  %11 = sext i32 %.lcssa to i64
  br label %.preheader

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

..loopexit_crit_edge:                             ; preds = %31
  %.not = icmp eq i64 %indvars.iv.next37, 0
  br i1 %.not, label %._crit_edge30, label %.preheader, !llvm.loop !12

.preheader:                                       ; preds = %.preheader.lr.ph, %..loopexit_crit_edge
  %indvars.iv36 = phi i64 [ %11, %.preheader.lr.ph ], [ %indvars.iv.next37, %..loopexit_crit_edge ]
  %indvars.iv.next37 = add nsw i64 %indvars.iv36, -1
  br label %19

._crit_edge30:                                    ; preds = %..loopexit_crit_edge, %._crit_edge
  %17 = load i32, ptr @dp, align 16, !tbaa !5
  %18 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %17)
  ret i32 0

19:                                               ; preds = %.preheader, %31
  %indvars.iv33 = phi i64 [ %11, %.preheader ], [ %indvars.iv.next34, %31 ]
  %indvars.iv.next34 = add nsw i64 %indvars.iv33, -1
  %20 = getelementptr inbounds [1003 x [1003 x i8]], ptr @A, i64 0, i64 %indvars.iv.next37, i64 %indvars.iv.next34
  %21 = load i8, ptr %20, align 1, !tbaa !9
  %.not20 = icmp eq i8 %21, 46
  %22 = getelementptr inbounds [1003 x i32], ptr @dp, i64 0, i64 %indvars.iv.next34
  br i1 %.not20, label %23, label %.sink.split

23:                                               ; preds = %19
  %24 = load i32, ptr %22, align 4, !tbaa !5
  %25 = getelementptr inbounds [1003 x i32], ptr @dp, i64 0, i64 %indvars.iv33
  %26 = load i32, ptr %25, align 4, !tbaa !5
  %27 = add nsw i32 %26, %24
  store i32 %27, ptr %22, align 4, !tbaa !5
  %28 = icmp sgt i32 %27, 1000000006
  br i1 %28, label %29, label %31

29:                                               ; preds = %23
  %30 = add nsw i32 %27, -1000000007
  br label %.sink.split

.sink.split:                                      ; preds = %19, %29
  %.sink = phi i32 [ %30, %29 ], [ 0, %19 ]
  store i32 %.sink, ptr %22, align 4, !tbaa !5
  br label %31

31:                                               ; preds = %.sink.split, %23
  %.not19 = icmp eq i64 %indvars.iv.next34, 0
  br i1 %.not19, label %..loopexit_crit_edge, label %19, !llvm.loop !13
}

; 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 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = distinct !{!12, !11}
!13 = distinct !{!13, !11}


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