; LLVM IR from VastoLorde95/Competitive-Programming :: Hackerrank/Company Contests/CodeStorm/Problem D (D)

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

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

4:                                                ; preds = %70, %0
  %5 = load i32, ptr %2, align 4, !tbaa !9
  %6 = icmp sle i32 %5, 1000
  br i1 %6, label %8, label %7

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

8:                                                ; preds = %4
  %9 = load i32, ptr %2, align 4, !tbaa !9
  %10 = srem i32 %9, 2
  %11 = icmp eq i32 %10, 0
  br i1 %11, label %12, label %59

12:                                               ; preds = %8
  %13 = load i32, ptr %2, align 4, !tbaa !9
  %14 = sub nsw i32 %13, 1
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds [2000 x i64], ptr @dp, i64 0, i64 %15
  %17 = load i64, ptr %16, align 8, !tbaa !5
  %18 = load i32, ptr %2, align 4, !tbaa !9
  %19 = sdiv i32 %18, 2
  %20 = sext i32 %19 to i64
  %21 = icmp sge i64 %17, %20
  br i1 %21, label %22, label %32

22:                                               ; preds = %12
  %23 = load i32, ptr %2, align 4, !tbaa !9
  %24 = sub nsw i32 %23, 1
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [2000 x i64], ptr @dp, i64 0, i64 %25
  %27 = load i64, ptr %26, align 8, !tbaa !5
  %28 = add nsw i64 %27, 2
  %29 = load i32, ptr %2, align 4, !tbaa !9
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [2000 x i64], ptr @dp, i64 0, i64 %30
  store i64 %28, ptr %31, align 8, !tbaa !5
  br label %42

32:                                               ; preds = %12
  %33 = load i32, ptr %2, align 4, !tbaa !9
  %34 = sub nsw i32 %33, 1
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [2000 x i64], ptr @dp, i64 0, i64 %35
  %37 = load i64, ptr %36, align 8, !tbaa !5
  %38 = add nsw i64 %37, 1
  %39 = load i32, ptr %2, align 4, !tbaa !9
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds [2000 x i64], ptr @dp, i64 0, i64 %40
  store i64 %38, ptr %41, align 8, !tbaa !5
  br label %42

42:                                               ; preds = %32, %22
  %43 = load i32, ptr %2, align 4, !tbaa !9
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds [2000 x i64], ptr @dp, i64 0, i64 %44
  %46 = load i64, ptr %45, align 8, !tbaa !5
  %47 = load i32, ptr %2, align 4, !tbaa !9
  %48 = sext i32 %47 to i64
  %49 = icmp sgt i64 %46, %48
  br i1 %49, label %50, label %58

50:                                               ; preds = %42
  %51 = load i32, ptr %2, align 4, !tbaa !9
  %52 = sext i32 %51 to i64
  %53 = load i32, ptr %2, align 4, !tbaa !9
  %54 = sext i32 %53 to i64
  %55 = getelementptr inbounds [2000 x i64], ptr @dp, i64 0, i64 %54
  %56 = load i64, ptr %55, align 8, !tbaa !5
  %57 = sub nsw i64 %56, %52
  store i64 %57, ptr %55, align 8, !tbaa !5
  br label %58

58:                                               ; preds = %50, %42
  br label %69

59:                                               ; preds = %8
  %60 = load i32, ptr %2, align 4, !tbaa !9
  %61 = sub nsw i32 %60, 1
  %62 = sext i32 %61 to i64
  %63 = getelementptr inbounds [2000 x i64], ptr @dp, i64 0, i64 %62
  %64 = load i64, ptr %63, align 8, !tbaa !5
  %65 = add nsw i64 %64, 1
  %66 = load i32, ptr %2, align 4, !tbaa !9
  %67 = sext i32 %66 to i64
  %68 = getelementptr inbounds [2000 x i64], ptr @dp, i64 0, i64 %67
  store i64 %65, ptr %68, align 8, !tbaa !5
  br label %69

69:                                               ; preds = %59, %58
  br label %70

70:                                               ; preds = %69
  %71 = load i32, ptr %2, align 4, !tbaa !9
  %72 = add nsw i32 %71, 1
  store i32 %72, ptr %2, align 4, !tbaa !9
  br label %4, !llvm.loop !11

73:                                               ; preds = %7
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  %74 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @t)
  br label %75

75:                                               ; preds = %79, %73
  %76 = load i32, ptr @t, align 4, !tbaa !9
  %77 = add nsw i32 %76, -1
  store i32 %77, ptr @t, align 4, !tbaa !9
  %78 = icmp ne i32 %76, 0
  br i1 %78, label %79, label %85

79:                                               ; preds = %75
  %80 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, ptr noundef %3)
  %81 = load i64, ptr %3, align 8, !tbaa !5
  %82 = getelementptr inbounds [2000 x i64], ptr @dp, i64 0, i64 %81
  %83 = load i64, ptr %82, align 8, !tbaa !5
  %84 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i64 noundef %83)
  br label %75, !llvm.loop !13

85:                                               ; preds = %75
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  ret i32 0
}

; 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

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

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

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

@t = external dso_local global i32, align 4
@dp = external dso_local local_unnamed_addr global [2000 x i64], align 16
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [6 x i8], align 1

; Function Attrs: mustprogress nofree norecurse nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = alloca i64, align 8
  store i64 1, ptr getelementptr inbounds (i8, ptr @dp, i64 8), align 8, !tbaa !5
  br label %6

2:                                                ; preds = %23
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %1) #3
  %3 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @t)
  %4 = load i32, ptr @t, align 4, !tbaa !9
  %5 = add nsw i32 %4, -1
  store i32 %5, ptr @t, align 4, !tbaa !9
  %.not20 = icmp eq i32 %4, 0
  br i1 %.not20, label %._crit_edge, label %.lr.ph

6:                                                ; preds = %0, %23
  %indvars.iv = phi i64 [ 2, %0 ], [ %indvars.iv.next, %23 ]
  %7 = and i64 %indvars.iv, 1
  %8 = icmp eq i64 %7, 0
  %9 = add nsw i64 %indvars.iv, -1
  %10 = getelementptr inbounds [2000 x i64], ptr @dp, i64 0, i64 %9
  %11 = load i64, ptr %10, align 8, !tbaa !5
  br i1 %8, label %12, label %20

12:                                               ; preds = %6
  %13 = lshr exact i64 %indvars.iv, 1
  %14 = and i64 %13, 2147483647
  %.not18 = icmp slt i64 %11, %14
  %. = select i1 %.not18, i64 1, i64 2
  %15 = add nsw i64 %11, %.
  %16 = getelementptr inbounds [2000 x i64], ptr @dp, i64 0, i64 %indvars.iv
  store i64 %15, ptr %16, align 8
  %17 = icmp sgt i64 %15, %indvars.iv
  br i1 %17, label %18, label %23

18:                                               ; preds = %12
  %19 = sub nsw i64 %15, %indvars.iv
  store i64 %19, ptr %16, align 8, !tbaa !5
  br label %23

20:                                               ; preds = %6
  %21 = add nsw i64 %11, 1
  %22 = getelementptr inbounds [2000 x i64], ptr @dp, i64 0, i64 %indvars.iv
  store i64 %21, ptr %22, align 8, !tbaa !5
  br label %23

23:                                               ; preds = %20, %18, %12
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, 1001
  br i1 %exitcond.not, label %2, label %6, !llvm.loop !11

.lr.ph:                                           ; preds = %2, %.lr.ph
  %24 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull %1)
  %25 = load i64, ptr %1, align 8, !tbaa !5
  %26 = getelementptr inbounds [2000 x i64], ptr @dp, i64 0, i64 %25
  %27 = load i64, ptr %26, align 8, !tbaa !5
  %28 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i64 noundef %27)
  %29 = load i32, ptr @t, align 4, !tbaa !9
  %30 = add nsw i32 %29, -1
  store i32 %30, ptr @t, align 4, !tbaa !9
  %.not = icmp eq i32 %29, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !13

._crit_edge:                                      ; preds = %.lr.ph, %2
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %1) #3
  ret i32 0
}

; 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: nofree nounwind
declare noundef i32 @__isoc99_scanf(ptr nocapture noundef readonly, ...) local_unnamed_addr #2

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,MergedLoadStoreMotionPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass