; LLVM IR from VastoLorde95/Competitive-Programming :: Codeforces/214 Division 2/Problem B (B)

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

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

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

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

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

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

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

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

25:                                               ; preds = %20
  %26 = load i32, ptr %3, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [100000 x i32], ptr @a, i64 0, i64 %27
  %29 = load i32, ptr %28, align 4, !tbaa !5
  store i32 %29, ptr @s, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  %30 = load i32, ptr %3, align 4, !tbaa !5
  %31 = load i32, ptr @k, align 4, !tbaa !5
  %32 = add nsw i32 %30, %31
  %33 = load i32, ptr @n, align 4, !tbaa !5
  %34 = srem i32 %32, %33
  store i32 %34, ptr %4, align 4, !tbaa !5
  br label %35

35:                                               ; preds = %46, %25
  %36 = load i32, ptr %4, align 4, !tbaa !5
  %37 = load i32, ptr %3, align 4, !tbaa !5
  %38 = icmp ne i32 %36, %37
  br i1 %38, label %39, label %53

39:                                               ; preds = %35
  %40 = load i32, ptr %4, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [100000 x i32], ptr @a, i64 0, i64 %41
  %43 = load i32, ptr %42, align 4, !tbaa !5
  %44 = load i32, ptr @s, align 4, !tbaa !5
  %45 = add nsw i32 %44, %43
  store i32 %45, ptr @s, align 4, !tbaa !5
  br label %46

46:                                               ; preds = %39
  %47 = load i32, ptr @n, align 4, !tbaa !5
  %48 = load i32, ptr @k, align 4, !tbaa !5
  %49 = load i32, ptr %4, align 4, !tbaa !5
  %50 = add nsw i32 %49, %48
  store i32 %50, ptr %4, align 4, !tbaa !5
  %51 = load i32, ptr %4, align 4, !tbaa !5
  %52 = srem i32 %51, %47
  store i32 %52, ptr %4, align 4, !tbaa !5
  br label %35, !llvm.loop !11

53:                                               ; preds = %35
  %54 = load i32, ptr @s, align 4, !tbaa !5
  %55 = load i32, ptr @mn, align 4, !tbaa !5
  %56 = icmp slt i32 %54, %55
  br i1 %56, label %57, label %60

57:                                               ; preds = %53
  %58 = load i32, ptr @s, align 4, !tbaa !5
  store i32 %58, ptr @mn, align 4, !tbaa !5
  %59 = load i32, ptr %3, align 4, !tbaa !5
  store i32 %59, ptr @res, align 4, !tbaa !5
  br label %60

60:                                               ; preds = %57, %53
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  br label %61

61:                                               ; preds = %60
  %62 = load i32, ptr %3, align 4, !tbaa !5
  %63 = add nsw i32 %62, 1
  store i32 %63, ptr %3, align 4, !tbaa !5
  br label %20, !llvm.loop !12

64:                                               ; preds = %24
  %65 = load i32, ptr @res, align 4, !tbaa !5
  %66 = add nsw i32 %65, 1
  %67 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i32 noundef %66)
  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
@k = external dso_local global i32, align 4
@a = external dso_local global [100000 x i32], align 16
@mn = external dso_local local_unnamed_addr global i32, align 4
@res = external dso_local local_unnamed_addr global i32, align 4
@s = external dso_local local_unnamed_addr global i32, align 4
@.str = external hidden unnamed_addr constant [5 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 @k)
  %2 = load i32, ptr @n, align 4, !tbaa !5
  %3 = icmp sgt i32 %2, 0
  br i1 %3, label %.lr.ph, label %.preheader

.preheader:                                       ; preds = %.lr.ph, %0
  %.lcssa = phi i32 [ %2, %0 ], [ %9, %.lr.ph ]
  %4 = load i32, ptr @k, align 4, !tbaa !5
  %5 = icmp sgt i32 %4, 0
  br i1 %5, label %.lr.ph28.preheader, label %12

.lr.ph28.preheader:                               ; preds = %.preheader
  %mn.promoted = load i32, ptr @mn, align 4, !tbaa !5
  %6 = zext nneg i32 %4 to i64
  br label %.lr.ph28

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

._crit_edge29:                                    ; preds = %29
  store i32 %storemerge.lcssa, ptr @s, align 4, !tbaa !5
  br label %12

12:                                               ; preds = %._crit_edge29, %.preheader
  %13 = load i32, ptr @res, align 4, !tbaa !5
  %14 = add nsw i32 %13, 1
  %15 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %14)
  ret i32 0

.lr.ph28:                                         ; preds = %.lr.ph28.preheader, %29
  %indvars.iv33 = phi i64 [ 0, %.lr.ph28.preheader ], [ %indvars.iv.next34, %29 ]
  %storemerge.lcssa2526 = phi i32 [ %mn.promoted, %.lr.ph28.preheader ], [ %storemerge.lcssa24, %29 ]
  %16 = getelementptr inbounds [100000 x i32], ptr @a, i64 0, i64 %indvars.iv33
  %17 = load i32, ptr %16, align 4, !tbaa !5
  %18 = trunc i64 %indvars.iv33 to i32
  %19 = add i32 %4, %18
  %.018 = srem i32 %19, %.lcssa
  %20 = zext i32 %.018 to i64
  %.not19 = icmp eq i64 %indvars.iv33, %20
  br i1 %.not19, label %._crit_edge, label %.lr.ph22

.lr.ph22:                                         ; preds = %.lr.ph28, %.lr.ph22
  %.021 = phi i32 [ %.0, %.lr.ph22 ], [ %.018, %.lr.ph28 ]
  %storemerge20 = phi i32 [ %24, %.lr.ph22 ], [ %17, %.lr.ph28 ]
  %21 = sext i32 %.021 to i64
  %22 = getelementptr inbounds [100000 x i32], ptr @a, i64 0, i64 %21
  %23 = load i32, ptr %22, align 4, !tbaa !5
  %24 = add nsw i32 %storemerge20, %23
  %.pn = add nsw i32 %.021, %4
  %.0 = srem i32 %.pn, %.lcssa
  %25 = zext i32 %.0 to i64
  %.not = icmp eq i64 %indvars.iv33, %25
  br i1 %.not, label %._crit_edge, label %.lr.ph22, !llvm.loop !11

._crit_edge:                                      ; preds = %.lr.ph22, %.lr.ph28
  %storemerge.lcssa = phi i32 [ %17, %.lr.ph28 ], [ %24, %.lr.ph22 ]
  %26 = icmp slt i32 %storemerge.lcssa, %storemerge.lcssa2526
  br i1 %26, label %27, label %29

27:                                               ; preds = %._crit_edge
  store i32 %storemerge.lcssa, ptr @mn, align 4, !tbaa !5
  %28 = trunc nuw nsw i64 %indvars.iv33 to i32
  store i32 %28, ptr @res, align 4, !tbaa !5
  br label %29

29:                                               ; preds = %27, %._crit_edge
  %storemerge.lcssa24 = phi i32 [ %storemerge.lcssa, %27 ], [ %storemerge.lcssa2526, %._crit_edge ]
  %indvars.iv.next34 = add nuw nsw i64 %indvars.iv33, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next34, %6
  br i1 %exitcond.not, label %._crit_edge29, label %.lr.ph28, !llvm.loop !12
}

; 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 = distinct !{!11, !10}
!12 = distinct !{!12, !10}


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