; LLVM IR from Shah-Shishir/Codeforces-Solutions :: 1041A. Heist (main)

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

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

5:                                                ; preds = %54, %0
  %6 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %2)
  %7 = icmp ne i32 %6, -1
  br i1 %7, label %8, label %57

8:                                                ; preds = %5
  store i64 0, ptr %3, align 8, !tbaa !5
  br label %9

9:                                                ; preds = %17, %8
  %10 = load i64, ptr %3, align 8, !tbaa !5
  %11 = load i64, ptr %2, align 8, !tbaa !5
  %12 = icmp slt i64 %10, %11
  br i1 %12, label %13, label %20

13:                                               ; preds = %9
  %14 = load i64, ptr %3, align 8, !tbaa !5
  %15 = getelementptr inbounds [1005 x i64], ptr @arr, i64 0, i64 %14
  %16 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %15)
  br label %17

17:                                               ; preds = %13
  %18 = load i64, ptr %3, align 8, !tbaa !5
  %19 = add nsw i64 %18, 1
  store i64 %19, ptr %3, align 8, !tbaa !5
  br label %9, !llvm.loop !9

20:                                               ; preds = %9
  %21 = load i64, ptr %2, align 8, !tbaa !5
  %22 = getelementptr inbounds i64, ptr @arr, i64 %21
  call void @_ZSt4sortIPxEvT_S1_(ptr noundef @arr, ptr noundef %22)
  store i64 0, ptr %4, align 8, !tbaa !5
  store i64 0, ptr %3, align 8, !tbaa !5
  br label %23

23:                                               ; preds = %51, %20
  %24 = load i64, ptr %3, align 8, !tbaa !5
  %25 = load i64, ptr %2, align 8, !tbaa !5
  %26 = sub nsw i64 %25, 1
  %27 = icmp slt i64 %24, %26
  br i1 %27, label %28, label %54

28:                                               ; preds = %23
  %29 = load i64, ptr %3, align 8, !tbaa !5
  %30 = add nsw i64 %29, 1
  %31 = getelementptr inbounds [1005 x i64], ptr @arr, i64 0, i64 %30
  %32 = load i64, ptr %31, align 8, !tbaa !5
  %33 = load i64, ptr %3, align 8, !tbaa !5
  %34 = getelementptr inbounds [1005 x i64], ptr @arr, i64 0, i64 %33
  %35 = load i64, ptr %34, align 8, !tbaa !5
  %36 = sub nsw i64 %32, %35
  %37 = icmp sgt i64 %36, 1
  br i1 %37, label %38, label %50

38:                                               ; preds = %28
  %39 = load i64, ptr %3, align 8, !tbaa !5
  %40 = add nsw i64 %39, 1
  %41 = getelementptr inbounds [1005 x i64], ptr @arr, i64 0, i64 %40
  %42 = load i64, ptr %41, align 8, !tbaa !5
  %43 = load i64, ptr %3, align 8, !tbaa !5
  %44 = getelementptr inbounds [1005 x i64], ptr @arr, i64 0, i64 %43
  %45 = load i64, ptr %44, align 8, !tbaa !5
  %46 = sub nsw i64 %42, %45
  %47 = sub nsw i64 %46, 1
  %48 = load i64, ptr %4, align 8, !tbaa !5
  %49 = add nsw i64 %48, %47
  store i64 %49, ptr %4, align 8, !tbaa !5
  br label %50

50:                                               ; preds = %38, %28
  br label %51

51:                                               ; preds = %50
  %52 = load i64, ptr %3, align 8, !tbaa !5
  %53 = add nsw i64 %52, 1
  store i64 %53, ptr %3, align 8, !tbaa !5
  br label %23, !llvm.loop !11

54:                                               ; preds = %23
  %55 = load i64, ptr %4, align 8, !tbaa !5
  %56 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i64 noundef %55)
  br label %5, !llvm.loop !12

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

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

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

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

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

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { 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 = { 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 = { 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 = 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"

@arr = external dso_local global [1005 x i64], align 16
@.str = external hidden unnamed_addr constant [5 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [6 x i8], align 1

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %1) #5
  %2 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %1)
  %.not22 = icmp eq i32 %2, -1
  br i1 %.not22, label %._crit_edge23, label %.preheader

.preheader:                                       ; preds = %0, %._crit_edge20
  %3 = load i64, ptr %1, align 8, !tbaa !5
  %4 = icmp sgt i64 %3, 0
  br i1 %4, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %.01115 = phi i64 [ %7, %.lr.ph ], [ 0, %.preheader ]
  %5 = getelementptr inbounds [1005 x i64], ptr @arr, i64 0, i64 %.01115
  %6 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %5)
  %7 = add nuw nsw i64 %.01115, 1
  %8 = load i64, ptr %1, align 8, !tbaa !5
  %9 = icmp slt i64 %7, %8
  br i1 %9, label %.lr.ph, label %._crit_edge, !llvm.loop !9

._crit_edge:                                      ; preds = %.lr.ph, %.preheader
  %.lcssa = phi i64 [ %3, %.preheader ], [ %8, %.lr.ph ]
  %10 = getelementptr inbounds i64, ptr @arr, i64 %.lcssa
  call void @_ZSt4sortIPxEvT_S1_(ptr noundef nonnull @arr, ptr noundef nonnull %10)
  %11 = load i64, ptr %1, align 8, !tbaa !5
  %12 = icmp sgt i64 %11, 1
  br i1 %12, label %.lr.ph19.preheader, label %._crit_edge20

.lr.ph19.preheader:                               ; preds = %._crit_edge
  %.pre = load i64, ptr @arr, align 16, !tbaa !5
  %13 = add nsw i64 %11, -2
  %14 = add nsw i64 %11, -1
  %min.iters.check = icmp ult i64 %11, 5
  br i1 %min.iters.check, label %.lr.ph19.preheader29, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph19.preheader
  %n.vec = and i64 %14, -4
  %vector.recur.init = insertelement <2 x i64> poison, i64 %.pre, i64 1
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vector.recur = phi <2 x i64> [ %vector.recur.init, %vector.ph ], [ %wide.load28, %vector.body ]
  %vec.phi = phi <2 x i64> [ zeroinitializer, %vector.ph ], [ %28, %vector.body ]
  %vec.phi27 = phi <2 x i64> [ zeroinitializer, %vector.ph ], [ %29, %vector.body ]
  %15 = or disjoint i64 %index, 1
  %16 = getelementptr inbounds [1005 x i64], ptr @arr, i64 0, i64 %15
  %17 = getelementptr inbounds i8, ptr %16, i64 16
  %wide.load = load <2 x i64>, ptr %16, align 8, !tbaa !5
  %wide.load28 = load <2 x i64>, ptr %17, align 8, !tbaa !5
  %18 = shufflevector <2 x i64> %vector.recur, <2 x i64> %wide.load, <2 x i32> <i32 1, i32 2>
  %19 = shufflevector <2 x i64> %wide.load, <2 x i64> %wide.load28, <2 x i32> <i32 1, i32 2>
  %20 = sub nsw <2 x i64> %wide.load, %18
  %21 = sub nsw <2 x i64> %wide.load28, %19
  %22 = icmp sgt <2 x i64> %20, <i64 1, i64 1>
  %23 = icmp sgt <2 x i64> %21, <i64 1, i64 1>
  %24 = add nsw <2 x i64> %20, <i64 -1, i64 -1>
  %25 = add nsw <2 x i64> %21, <i64 -1, i64 -1>
  %26 = select <2 x i1> %22, <2 x i64> %24, <2 x i64> zeroinitializer
  %27 = select <2 x i1> %23, <2 x i64> %25, <2 x i64> zeroinitializer
  %28 = add <2 x i64> %26, %vec.phi
  %29 = add <2 x i64> %27, %vec.phi27
  %index.next = add nuw i64 %index, 4
  %30 = icmp eq i64 %index.next, %n.vec
  br i1 %30, label %middle.block, label %vector.body, !llvm.loop !11

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <2 x i64> %29, %28
  %31 = call i64 @llvm.vector.reduce.add.v2i64(<2 x i64> %bin.rdx)
  %vector.recur.extract = extractelement <2 x i64> %wide.load28, i64 1
  %cmp.n = icmp eq i64 %14, %n.vec
  br i1 %cmp.n, label %._crit_edge20, label %.lr.ph19.preheader29

.lr.ph19.preheader29:                             ; preds = %middle.block, %.lr.ph19.preheader
  %.ph = phi i64 [ %.pre, %.lr.ph19.preheader ], [ %vector.recur.extract, %middle.block ]
  %.017.ph = phi i64 [ 0, %.lr.ph19.preheader ], [ %31, %middle.block ]
  %.11216.ph = phi i64 [ 0, %.lr.ph19.preheader ], [ %n.vec, %middle.block ]
  br label %.lr.ph19

.lr.ph19:                                         ; preds = %.lr.ph19.preheader29, %.lr.ph19
  %32 = phi i64 [ %35, %.lr.ph19 ], [ %.ph, %.lr.ph19.preheader29 ]
  %.017 = phi i64 [ %.1, %.lr.ph19 ], [ %.017.ph, %.lr.ph19.preheader29 ]
  %.11216 = phi i64 [ %33, %.lr.ph19 ], [ %.11216.ph, %.lr.ph19.preheader29 ]
  %33 = add nuw nsw i64 %.11216, 1
  %34 = getelementptr inbounds [1005 x i64], ptr @arr, i64 0, i64 %33
  %35 = load i64, ptr %34, align 8, !tbaa !5
  %36 = sub nsw i64 %35, %32
  %37 = icmp sgt i64 %36, 1
  %38 = add nsw i64 %36, -1
  %39 = select i1 %37, i64 %38, i64 0
  %.1 = add nsw i64 %39, %.017
  %exitcond.not = icmp eq i64 %.11216, %13
  br i1 %exitcond.not, label %._crit_edge20, label %.lr.ph19, !llvm.loop !14

._crit_edge20:                                    ; preds = %.lr.ph19, %middle.block, %._crit_edge
  %.0.lcssa = phi i64 [ 0, %._crit_edge ], [ %31, %middle.block ], [ %.1, %.lr.ph19 ]
  %40 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i64 noundef %.0.lcssa)
  %41 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %1)
  %.not = icmp eq i32 %41, -1
  br i1 %.not, label %._crit_edge23, label %.preheader, !llvm.loop !15

._crit_edge23:                                    ; preds = %._crit_edge20, %0
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %1) #5
  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: nofree nounwind
declare noundef i32 @__isoc99_scanf(ptr nocapture noundef readonly, ...) local_unnamed_addr #2

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

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

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.vector.reduce.add.v2i64(<2 x i64>) #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 = { 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 = { 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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = !{!"long long", !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, !13}
!12 = !{!"llvm.loop.isvectorized", i32 1}
!13 = !{!"llvm.loop.unroll.runtime.disable"}
!14 = distinct !{!14, !10, !13, !12}
!15 = distinct !{!15, !10}


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