; LLVM IR from VastoLorde95/Competitive-Programming :: Codeforces/358 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
@a = external dso_local global [100100 x i32], align 16
@.str = external hidden unnamed_addr constant [3 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)
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #5
  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) #5
  br label %19

11:                                               ; preds = %6
  %12 = load i32, ptr %2, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [100100 x i32], ptr @a, i64 0, i64 %13
  %15 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, 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
  %20 = load i32, ptr @n, align 4, !tbaa !5
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds i32, ptr @a, i64 %21
  call void @_ZSt4sortIPiEvT_S1_(ptr noundef @a, ptr noundef %22)
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #5
  store i32 1, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #5
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %23

23:                                               ; preds = %39, %19
  %24 = load i32, ptr %4, align 4, !tbaa !5
  %25 = load i32, ptr @n, align 4, !tbaa !5
  %26 = icmp slt i32 %24, %25
  br i1 %26, label %28, label %27

27:                                               ; preds = %23
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #5
  br label %42

28:                                               ; preds = %23
  %29 = load i32, ptr %4, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [100100 x i32], ptr @a, i64 0, i64 %30
  %32 = load i32, ptr %31, align 4, !tbaa !5
  %33 = load i32, ptr %3, align 4, !tbaa !5
  %34 = icmp sge i32 %32, %33
  br i1 %34, label %35, label %38

35:                                               ; preds = %28
  %36 = load i32, ptr %3, align 4, !tbaa !5
  %37 = add nsw i32 %36, 1
  store i32 %37, ptr %3, align 4, !tbaa !5
  br label %38

38:                                               ; preds = %35, %28
  br label %39

39:                                               ; preds = %38
  %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 %23, !llvm.loop !11

42:                                               ; preds = %27
  %43 = load i32, ptr %3, align 4, !tbaa !5
  call void @_Z2trIiJEEvT_DpRKT0_(i32 noundef %43)
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #5
  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

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_ZSt4sortIPiEvT_S1_(ptr noundef, ptr noundef) #3

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z2trIiJEEvT_DpRKT0_(i32 noundef) #4

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 = { inlinehint mustprogress 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 #4 = { mustprogress 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 #5 = { 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
@a = external dso_local global [100100 x i32], align 16
@.str = external hidden unnamed_addr constant [3 x i8], align 1

; Function Attrs: mustprogress norecurse 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

.._crit_edge_crit_edge:                           ; preds = %0
  %.pre = sext i32 %2 to i64
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph, %.._crit_edge_crit_edge
  %.pre-phi = phi i64 [ %.pre, %.._crit_edge_crit_edge ], [ %11, %.lr.ph ]
  %4 = getelementptr inbounds i32, ptr @a, i64 %.pre-phi
  tail call void @_ZSt4sortIPiEvT_S1_(ptr noundef nonnull @a, ptr noundef nonnull %4)
  %5 = load i32, ptr @n, align 4, !tbaa !5
  %6 = icmp sgt i32 %5, 0
  br i1 %6, label %.lr.ph16.preheader, label %._crit_edge17

.lr.ph16.preheader:                               ; preds = %._crit_edge
  %wide.trip.count = zext nneg i32 %5 to i64
  %xtraiter = and i64 %wide.trip.count, 3
  %7 = icmp ult i32 %5, 4
  br i1 %7, label %._crit_edge17.loopexit.unr-lcssa, label %.lr.ph16.preheader.new

.lr.ph16.preheader.new:                           ; preds = %.lr.ph16.preheader
  %unroll_iter = and i64 %wide.trip.count, 2147483644
  br label %.lr.ph16

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

._crit_edge17.loopexit.unr-lcssa:                 ; preds = %.lr.ph16, %.lr.ph16.preheader
  %spec.select.lcssa.ph = phi i32 [ poison, %.lr.ph16.preheader ], [ %spec.select.3, %.lr.ph16 ]
  %indvars.iv21.unr = phi i64 [ 0, %.lr.ph16.preheader ], [ %indvars.iv.next22.3, %.lr.ph16 ]
  %.0913.unr = phi i32 [ 1, %.lr.ph16.preheader ], [ %spec.select.3, %.lr.ph16 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge17, label %.lr.ph16.epil

.lr.ph16.epil:                                    ; preds = %._crit_edge17.loopexit.unr-lcssa, %.lr.ph16.epil
  %indvars.iv21.epil = phi i64 [ %indvars.iv.next22.epil, %.lr.ph16.epil ], [ %indvars.iv21.unr, %._crit_edge17.loopexit.unr-lcssa ]
  %.0913.epil = phi i32 [ %spec.select.epil, %.lr.ph16.epil ], [ %.0913.unr, %._crit_edge17.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.lr.ph16.epil ], [ 0, %._crit_edge17.loopexit.unr-lcssa ]
  %13 = getelementptr inbounds [100100 x i32], ptr @a, i64 0, i64 %indvars.iv21.epil
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %.not.epil = icmp sge i32 %14, %.0913.epil
  %15 = zext i1 %.not.epil to i32
  %spec.select.epil = add nuw nsw i32 %.0913.epil, %15
  %indvars.iv.next22.epil = add nuw nsw i64 %indvars.iv21.epil, 1
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %._crit_edge17, label %.lr.ph16.epil, !llvm.loop !11

._crit_edge17:                                    ; preds = %._crit_edge17.loopexit.unr-lcssa, %.lr.ph16.epil, %._crit_edge
  %.09.lcssa = phi i32 [ 1, %._crit_edge ], [ %spec.select.lcssa.ph, %._crit_edge17.loopexit.unr-lcssa ], [ %spec.select.epil, %.lr.ph16.epil ]
  tail call void @_Z2trIiJEEvT_DpRKT0_(i32 noundef %.09.lcssa)
  ret i32 0

.lr.ph16:                                         ; preds = %.lr.ph16, %.lr.ph16.preheader.new
  %indvars.iv21 = phi i64 [ 0, %.lr.ph16.preheader.new ], [ %indvars.iv.next22.3, %.lr.ph16 ]
  %.0913 = phi i32 [ 1, %.lr.ph16.preheader.new ], [ %spec.select.3, %.lr.ph16 ]
  %niter = phi i64 [ 0, %.lr.ph16.preheader.new ], [ %niter.next.3, %.lr.ph16 ]
  %16 = getelementptr inbounds [100100 x i32], ptr @a, i64 0, i64 %indvars.iv21
  %17 = load i32, ptr %16, align 16, !tbaa !5
  %.not = icmp sge i32 %17, %.0913
  %18 = zext i1 %.not to i32
  %spec.select = add nuw nsw i32 %.0913, %18
  %indvars.iv.next22 = or disjoint i64 %indvars.iv21, 1
  %19 = getelementptr inbounds [100100 x i32], ptr @a, i64 0, i64 %indvars.iv.next22
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %.not.1 = icmp sge i32 %20, %spec.select
  %21 = zext i1 %.not.1 to i32
  %spec.select.1 = add nuw nsw i32 %spec.select, %21
  %indvars.iv.next22.1 = or disjoint i64 %indvars.iv21, 2
  %22 = getelementptr inbounds [100100 x i32], ptr @a, i64 0, i64 %indvars.iv.next22.1
  %23 = load i32, ptr %22, align 8, !tbaa !5
  %.not.2 = icmp sge i32 %23, %spec.select.1
  %24 = zext i1 %.not.2 to i32
  %spec.select.2 = add nuw nsw i32 %spec.select.1, %24
  %indvars.iv.next22.2 = or disjoint i64 %indvars.iv21, 3
  %25 = getelementptr inbounds [100100 x i32], ptr @a, i64 0, i64 %indvars.iv.next22.2
  %26 = load i32, ptr %25, align 4, !tbaa !5
  %.not.3 = icmp sge i32 %26, %spec.select.2
  %27 = zext i1 %.not.3 to i32
  %spec.select.3 = add nuw nsw i32 %spec.select.2, %27
  %indvars.iv.next22.3 = add nuw nsw i64 %indvars.iv21, 4
  %niter.next.3 = add i64 %niter, 4
  %niter.ncmp.3 = icmp eq i64 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3, label %._crit_edge17.loopexit.unr-lcssa, label %.lr.ph16, !llvm.loop !13
}

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

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_ZSt4sortIPiEvT_S1_(ptr noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z2trIiJEEvT_DpRKT0_(i32 noundef) local_unnamed_addr #3

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 = { 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 = { inlinehint mustprogress 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 #3 = { mustprogress 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" }

!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, !12}
!12 = !{!"llvm.loop.unroll.disable"}
!13 = distinct !{!13, !10}


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