; LLVM IR from VastoLorde95/Competitive-Programming :: Timus/1554/A.ll (Z3geti)

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

@p = external dso_local global [10010 x i32], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z3geti(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  store i32 2, ptr %3, align 4, !tbaa !5
  br label %7

7:                                                ; preds = %16, %1
  %8 = load i32, ptr %3, align 4, !tbaa !5
  %9 = load i32, ptr %2, align 4, !tbaa !5
  %10 = icmp sle i32 %8, %9
  br i1 %10, label %12, label %11

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

12:                                               ; preds = %7
  %13 = load i32, ptr %3, align 4, !tbaa !5
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [10010 x i32], ptr @p, i64 0, i64 %14
  store i32 1, ptr %15, align 4, !tbaa !5
  br label %16

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

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

20:                                               ; preds = %50, %19
  %21 = load i32, ptr %4, align 4, !tbaa !5
  %22 = load i32, ptr %2, align 4, !tbaa !5
  %23 = icmp sle i32 %21, %22
  br i1 %23, label %25, label %24

24:                                               ; preds = %20
  store i32 5, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  br label %53

25:                                               ; preds = %20
  %26 = load i32, ptr %4, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [10010 x i32], ptr @p, i64 0, i64 %27
  %29 = load i32, ptr %28, align 4, !tbaa !5
  %30 = icmp ne i32 %29, 0
  br i1 %30, label %32, label %31

31:                                               ; preds = %25
  br label %50

32:                                               ; preds = %25
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %33 = load i32, ptr %4, align 4, !tbaa !5
  %34 = load i32, ptr %4, align 4, !tbaa !5
  %35 = mul nsw i32 %33, %34
  store i32 %35, ptr %6, align 4, !tbaa !5
  br label %36

36:                                               ; preds = %45, %32
  %37 = load i32, ptr %6, align 4, !tbaa !5
  %38 = load i32, ptr %2, align 4, !tbaa !5
  %39 = icmp sle i32 %37, %38
  br i1 %39, label %41, label %40

40:                                               ; preds = %36
  store i32 8, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  br label %49

41:                                               ; preds = %36
  %42 = load i32, ptr %6, align 4, !tbaa !5
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds [10010 x i32], ptr @p, i64 0, i64 %43
  store i32 0, ptr %44, align 4, !tbaa !5
  br label %45

45:                                               ; preds = %41
  %46 = load i32, ptr %4, align 4, !tbaa !5
  %47 = load i32, ptr %6, align 4, !tbaa !5
  %48 = add nsw i32 %47, %46
  store i32 %48, ptr %6, align 4, !tbaa !5
  br label %36, !llvm.loop !11

49:                                               ; preds = %40
  br label %50

50:                                               ; preds = %49, %31
  %51 = load i32, ptr %4, align 4, !tbaa !5
  %52 = add nsw i32 %51, 1
  store i32 %52, ptr %4, align 4, !tbaa !5
  br label %20, !llvm.loop !12

53:                                               ; preds = %24
  ret void
}

; 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

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

@p = external dso_local local_unnamed_addr global [10010 x i32], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z3geti(i32 noundef %0) local_unnamed_addr #0 {
  %.not21 = icmp slt i32 %0, 2
  br i1 %.not21, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %1
  %2 = add nuw i32 %0, 1
  %wide.trip.count = zext i32 %2 to i64
  %3 = add nsw i64 %wide.trip.count, -2
  %min.iters.check = icmp ult i64 %3, 8
  br i1 %min.iters.check, label %.lr.ph.preheader47, label %vector.ph

.lr.ph.preheader47:                               ; preds = %middle.block, %.lr.ph.preheader
  %indvars.iv.ph = phi i64 [ 2, %.lr.ph.preheader ], [ %ind.end, %middle.block ]
  br label %.lr.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %3, -8
  %ind.end = or disjoint i64 %n.vec, 2
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %offset.idx = or disjoint i64 %index, 2
  %4 = getelementptr inbounds [10010 x i32], ptr @p, i64 0, i64 %offset.idx
  %5 = getelementptr inbounds i8, ptr %4, i64 16
  store <4 x i32> <i32 1, i32 1, i32 1, i32 1>, ptr %4, align 8, !tbaa !5
  store <4 x i32> <i32 1, i32 1, i32 1, i32 1>, ptr %5, align 8, !tbaa !5
  %index.next = add nuw i64 %index, 8
  %6 = icmp eq i64 %index.next, %n.vec
  br i1 %6, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %3, %n.vec
  br i1 %cmp.n, label %.preheader, label %.lr.ph.preheader47

.preheader:                                       ; preds = %.lr.ph, %middle.block
  br i1 %.not21, label %._crit_edge, label %.lr.ph29.preheader

.lr.ph29.preheader:                               ; preds = %.preheader
  %7 = add nuw i32 %0, 1
  %wide.trip.count45 = zext i32 %7 to i64
  br label %.lr.ph29

.lr.ph:                                           ; preds = %.lr.ph.preheader47, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ %indvars.iv.ph, %.lr.ph.preheader47 ]
  %8 = getelementptr inbounds [10010 x i32], ptr @p, i64 0, i64 %indvars.iv
  store i32 1, ptr %8, align 4, !tbaa !5
  %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 %.preheader, label %.lr.ph, !llvm.loop !13

._crit_edge:                                      ; preds = %.loopexit, %1, %.preheader
  ret void

.lr.ph29:                                         ; preds = %.lr.ph29.preheader, %.loopexit
  %indvars.iv40 = phi i64 [ 5, %.lr.ph29.preheader ], [ %indvars.iv.next41, %.loopexit ]
  %indvars.iv35 = phi i64 [ 2, %.lr.ph29.preheader ], [ %indvars.iv.next36, %.loopexit ]
  %indvars.iv33 = phi i32 [ 4, %.lr.ph29.preheader ], [ %indvars.iv.next34, %.loopexit ]
  %9 = getelementptr inbounds [10010 x i32], ptr @p, i64 0, i64 %indvars.iv35
  %10 = load i32, ptr %9, align 4, !tbaa !5
  %.not19 = icmp eq i32 %10, 0
  %11 = mul nuw nsw i64 %indvars.iv35, %indvars.iv35
  %12 = trunc nuw i64 %11 to i32
  %.not2023 = icmp sgt i32 %12, %0
  %or.cond = select i1 %.not19, i1 true, i1 %.not2023
  br i1 %or.cond, label %.loopexit, label %.lr.ph25.preheader

.lr.ph25.preheader:                               ; preds = %.lr.ph29
  %13 = zext i32 %indvars.iv33 to i64
  br label %.lr.ph25

.lr.ph25:                                         ; preds = %.lr.ph25.preheader, %.lr.ph25
  %indvars.iv37 = phi i64 [ %13, %.lr.ph25.preheader ], [ %indvars.iv.next38, %.lr.ph25 ]
  %14 = getelementptr inbounds [10010 x i32], ptr @p, i64 0, i64 %indvars.iv37
  store i32 0, ptr %14, align 4, !tbaa !5
  %indvars.iv.next38 = add nuw nsw i64 %indvars.iv37, %indvars.iv35
  %15 = trunc nuw i64 %indvars.iv.next38 to i32
  %.not20 = icmp sgt i32 %15, %0
  br i1 %.not20, label %.loopexit, label %.lr.ph25, !llvm.loop !14

.loopexit:                                        ; preds = %.lr.ph25, %.lr.ph29
  %indvars.iv.next36 = add nuw nsw i64 %indvars.iv35, 1
  %indvars.iv.next41 = add nuw nsw i64 %indvars.iv40, 2
  %16 = trunc nuw i64 %indvars.iv40 to i32
  %indvars.iv.next34 = add i32 %indvars.iv33, %16
  %exitcond46.not = icmp eq i64 %indvars.iv.next36, %wide.trip.count45
  br i1 %exitcond46.not, label %._crit_edge, label %.lr.ph29, !llvm.loop !15
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) 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, !11, !12}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!"llvm.loop.isvectorized", i32 1}
!12 = !{!"llvm.loop.unroll.runtime.disable"}
!13 = distinct !{!13, !10, !12, !11}
!14 = distinct !{!14, !10}
!15 = distinct !{!15, !10}


; Effective O3 passes: GVNPass,GlobalOptPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass