; LLVM IR from eyangch/competitive-programming :: CSES/Sorting_and_Searching (collecting_numbers)

; 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
@x = external dso_local global [200000 x i32], align 16

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z5printi(i32 noundef) #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

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z4getNv() #0

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() #2 {
  %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 noundef i32 @_Z4getNv()
  store i32 %6, ptr @N, align 4, !tbaa !5
  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 = %19, %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 %22

12:                                               ; preds = %7
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  %13 = call noundef i32 @_Z4getNv()
  store i32 %13, ptr %3, align 4, !tbaa !5
  %14 = load i32, ptr %2, align 4, !tbaa !5
  %15 = load i32, ptr %3, align 4, !tbaa !5
  %16 = sub nsw i32 %15, 1
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [200000 x i32], ptr @x, i64 0, i64 %17
  store i32 %14, ptr %18, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  br label %19

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

22:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  store i32 1, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  store i32 1, ptr %5, align 4, !tbaa !5
  br label %23

23:                                               ; preds = %43, %22
  %24 = load i32, ptr %5, 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 %5) #3
  br label %46

28:                                               ; preds = %23
  %29 = load i32, ptr %5, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [200000 x i32], ptr @x, i64 0, i64 %30
  %32 = load i32, ptr %31, align 4, !tbaa !5
  %33 = load i32, ptr %5, align 4, !tbaa !5
  %34 = sub nsw i32 %33, 1
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [200000 x i32], ptr @x, i64 0, i64 %35
  %37 = load i32, ptr %36, align 4, !tbaa !5
  %38 = icmp slt i32 %32, %37
  br i1 %38, label %39, label %42

39:                                               ; preds = %28
  %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 %42

42:                                               ; preds = %39, %28
  br label %43

43:                                               ; preds = %42
  %44 = load i32, ptr %5, align 4, !tbaa !5
  %45 = add nsw i32 %44, 1
  store i32 %45, ptr %5, align 4, !tbaa !5
  br label %23, !llvm.loop !11

46:                                               ; preds = %27
  %47 = load i32, ptr %4, align 4, !tbaa !5
  call void @_Z5printi(i32 noundef %47)
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  %48 = load i32, ptr %1, align 4
  ret i32 %48
}

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #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}


; 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 local_unnamed_addr global i32, align 4
@x = external dso_local local_unnamed_addr global [200000 x i32], align 16

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

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z4getNv() local_unnamed_addr #0

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() local_unnamed_addr #1 {
  %1 = tail call noundef i32 @_Z4getNv()
  store i32 %1, ptr @N, align 4, !tbaa !5
  %2 = icmp sgt i32 %1, 0
  br i1 %2, label %.lr.ph, label %._crit_edge

.preheader:                                       ; preds = %.lr.ph
  %3 = icmp sgt i32 %22, 1
  br i1 %3, label %.lr.ph16.preheader, label %._crit_edge

.lr.ph16.preheader:                               ; preds = %.preheader
  %wide.trip.count = zext nneg i32 %22 to i64
  %.pre = load i32, ptr @x, align 16, !tbaa !5
  %4 = add nsw i64 %wide.trip.count, -1
  %min.iters.check = icmp ult i32 %22, 9
  br i1 %min.iters.check, label %.lr.ph16.preheader23, label %vector.ph

.lr.ph16.preheader23:                             ; preds = %middle.block, %.lr.ph16.preheader
  %.ph = phi i32 [ %.pre, %.lr.ph16.preheader ], [ %vector.recur.extract, %middle.block ]
  %indvars.iv.ph = phi i64 [ 1, %.lr.ph16.preheader ], [ %ind.end, %middle.block ]
  %.01114.ph = phi i32 [ 1, %.lr.ph16.preheader ], [ %16, %middle.block ]
  br label %.lr.ph16

vector.ph:                                        ; preds = %.lr.ph16.preheader
  %n.vec = and i64 %4, -8
  %ind.end = or disjoint i64 %n.vec, 1
  %vector.recur.init = insertelement <4 x i32> poison, i32 %.pre, i64 3
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vector.recur = phi <4 x i32> [ %vector.recur.init, %vector.ph ], [ %wide.load22, %vector.body ]
  %vec.phi = phi <4 x i32> [ <i32 1, i32 0, i32 0, i32 0>, %vector.ph ], [ %13, %vector.body ]
  %vec.phi21 = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %14, %vector.body ]
  %offset.idx = or disjoint i64 %index, 1
  %5 = getelementptr inbounds [200000 x i32], ptr @x, i64 0, i64 %offset.idx
  %6 = getelementptr inbounds i8, ptr %5, i64 16
  %wide.load = load <4 x i32>, ptr %5, align 4, !tbaa !5
  %wide.load22 = load <4 x i32>, ptr %6, align 4, !tbaa !5
  %7 = shufflevector <4 x i32> %vector.recur, <4 x i32> %wide.load, <4 x i32> <i32 3, i32 4, i32 5, i32 6>
  %8 = shufflevector <4 x i32> %wide.load, <4 x i32> %wide.load22, <4 x i32> <i32 3, i32 4, i32 5, i32 6>
  %9 = icmp slt <4 x i32> %wide.load, %7
  %10 = icmp slt <4 x i32> %wide.load22, %8
  %11 = zext <4 x i1> %9 to <4 x i32>
  %12 = zext <4 x i1> %10 to <4 x i32>
  %13 = add <4 x i32> %vec.phi, %11
  %14 = add <4 x i32> %vec.phi21, %12
  %index.next = add nuw i64 %index, 8
  %15 = icmp eq i64 %index.next, %n.vec
  br i1 %15, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <4 x i32> %14, %13
  %16 = tail call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx)
  %vector.recur.extract = extractelement <4 x i32> %wide.load22, i64 3
  %cmp.n = icmp eq i64 %4, %n.vec
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph16.preheader23

.lr.ph:                                           ; preds = %0, %.lr.ph
  %.01213 = phi i32 [ %21, %.lr.ph ], [ 0, %0 ]
  %17 = tail call noundef i32 @_Z4getNv()
  %18 = add nsw i32 %17, -1
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [200000 x i32], ptr @x, i64 0, i64 %19
  store i32 %.01213, ptr %20, align 4, !tbaa !5
  %21 = add nuw nsw i32 %.01213, 1
  %22 = load i32, ptr @N, align 4, !tbaa !5
  %23 = icmp slt i32 %21, %22
  br i1 %23, label %.lr.ph, label %.preheader, !llvm.loop !13

._crit_edge:                                      ; preds = %.lr.ph16, %middle.block, %0, %.preheader
  %.011.lcssa = phi i32 [ 1, %.preheader ], [ 1, %0 ], [ %16, %middle.block ], [ %spec.select, %.lr.ph16 ]
  tail call void @_Z5printi(i32 noundef %.011.lcssa)
  ret i32 0

.lr.ph16:                                         ; preds = %.lr.ph16.preheader23, %.lr.ph16
  %24 = phi i32 [ %26, %.lr.ph16 ], [ %.ph, %.lr.ph16.preheader23 ]
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph16 ], [ %indvars.iv.ph, %.lr.ph16.preheader23 ]
  %.01114 = phi i32 [ %spec.select, %.lr.ph16 ], [ %.01114.ph, %.lr.ph16.preheader23 ]
  %25 = getelementptr inbounds [200000 x i32], ptr @x, i64 0, i64 %indvars.iv
  %26 = load i32, ptr %25, align 4, !tbaa !5
  %27 = icmp slt i32 %26, %24
  %28 = zext i1 %27 to i32
  %spec.select = add nuw nsw i32 %.01114, %28
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph16, !llvm.loop !14
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.vector.reduce.add.v4i32(<4 x i32>) #2

attributes #0 = { 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 #1 = { 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 #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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, !11, !12}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!"llvm.loop.isvectorized", i32 1}
!12 = !{!"llvm.loop.unroll.runtime.disable"}
!13 = distinct !{!13, !10}
!14 = distinct !{!14, !10, !12, !11}


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