; LLVM IR from VastoLorde95/Competitive-Programming :: CodeChef/WF/Medium Hard/FNCS/dreamoon.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"

@seg_sum = external dso_local global [201 x i64], align 16
@L = external dso_local global [100500 x i32], align 16
@R = external dso_local global [100500 x i32], align 16

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_Z4get2i(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 nounwind uwtable
define dso_local noundef i64 @_Z3geti(i32 noundef %0) #0 {
  %2 = alloca i64, align 8
  %3 = alloca i32, align 4
  %4 = alloca i64, align 8
  %5 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  %6 = load i32, ptr %3, align 4, !tbaa !5
  %7 = icmp slt i32 %6, 0
  br i1 %7, label %8, label %9

8:                                                ; preds = %1
  store i64 0, ptr %2, align 8
  br label %53

9:                                                ; preds = %1
  %10 = load i32, ptr %3, align 4, !tbaa !5
  %11 = add nsw i32 %10, 1
  store i32 %11, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  store i64 0, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  %12 = load i32, ptr %3, align 4, !tbaa !5
  %13 = sdiv i32 %12, 500
  %14 = sub nsw i32 %13, 1
  store i32 %14, ptr %5, align 4, !tbaa !5
  br label %15

15:                                               ; preds = %26, %9
  %16 = load i32, ptr %5, align 4, !tbaa !5
  %17 = icmp sge i32 %16, 0
  br i1 %17, label %19, label %18

18:                                               ; preds = %15
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  br label %29

19:                                               ; preds = %15
  %20 = load i32, ptr %5, align 4, !tbaa !5
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [201 x i64], ptr @seg_sum, i64 0, i64 %21
  %23 = load i64, ptr %22, align 8, !tbaa !9
  %24 = load i64, ptr %4, align 8, !tbaa !9
  %25 = add nsw i64 %24, %23
  store i64 %25, ptr %4, align 8, !tbaa !9
  br label %26

26:                                               ; preds = %19
  %27 = load i32, ptr %5, align 4, !tbaa !5
  %28 = add nsw i32 %27, -1
  store i32 %28, ptr %5, align 4, !tbaa !5
  br label %15, !llvm.loop !11

29:                                               ; preds = %18
  br label %30

30:                                               ; preds = %34, %29
  %31 = load i32, ptr %3, align 4, !tbaa !5
  %32 = srem i32 %31, 500
  %33 = icmp ne i32 %32, 0
  br i1 %33, label %34, label %51

34:                                               ; preds = %30
  %35 = load i32, ptr %3, align 4, !tbaa !5
  %36 = add nsw i32 %35, -1
  store i32 %36, ptr %3, align 4, !tbaa !5
  %37 = load i32, ptr %3, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [100500 x i32], ptr @R, i64 0, i64 %38
  %40 = load i32, ptr %39, align 4, !tbaa !5
  %41 = call noundef i64 @_Z4get2i(i32 noundef %40)
  %42 = load i32, ptr %3, align 4, !tbaa !5
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds [100500 x i32], ptr @L, i64 0, i64 %43
  %45 = load i32, ptr %44, align 4, !tbaa !5
  %46 = sub nsw i32 %45, 1
  %47 = call noundef i64 @_Z4get2i(i32 noundef %46)
  %48 = sub nsw i64 %41, %47
  %49 = load i64, ptr %4, align 8, !tbaa !9
  %50 = add nsw i64 %49, %48
  store i64 %50, ptr %4, align 8, !tbaa !9
  br label %30, !llvm.loop !13

51:                                               ; preds = %30
  %52 = load i64, ptr %4, align 8, !tbaa !9
  store i64 %52, ptr %2, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  br label %53

53:                                               ; preds = %51, %8
  %54 = load i64, ptr %2, align 8
  ret i64 %54
}

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 = !{!10, !10, i64 0}
!10 = !{!"long long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


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

@seg_sum = external dso_local local_unnamed_addr global [201 x i64], align 16
@L = external dso_local local_unnamed_addr global [100500 x i32], align 16
@R = external dso_local local_unnamed_addr global [100500 x i32], align 16

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z3geti(i32 noundef %0) local_unnamed_addr #0 {
  %2 = icmp slt i32 %0, 0
  br i1 %2, label %.loopexit, label %3

3:                                                ; preds = %1
  %4 = add nuw i32 %0, 1
  %.not25 = icmp ult i32 %0, 499
  br i1 %.not25, label %.preheader, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %3
  %5 = udiv i32 %4, 500
  %6 = zext nneg i32 %5 to i64
  %min.iters.check = icmp ult i32 %0, 1999
  br i1 %min.iters.check, label %.lr.ph.preheader35, label %vector.ph

.lr.ph.preheader35:                               ; preds = %middle.block, %.lr.ph.preheader
  %indvars.iv.ph = phi i64 [ %6, %.lr.ph.preheader ], [ %ind.end, %middle.block ]
  %.01418.ph = phi i64 [ 0, %.lr.ph.preheader ], [ %15, %middle.block ]
  br label %.lr.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %6, 16777212
  %ind.end = and i64 %6, 3
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <2 x i64> [ zeroinitializer, %vector.ph ], [ %12, %vector.body ]
  %vec.phi32 = phi <2 x i64> [ zeroinitializer, %vector.ph ], [ %13, %vector.body ]
  %7 = xor i64 %index, -1
  %8 = add i64 %7, %6
  %9 = getelementptr inbounds [201 x i64], ptr @seg_sum, i64 0, i64 %8
  %10 = getelementptr inbounds i8, ptr %9, i64 -8
  %11 = getelementptr inbounds i8, ptr %9, i64 -24
  %wide.load = load <2 x i64>, ptr %10, align 8, !tbaa !5
  %reverse = shufflevector <2 x i64> %wide.load, <2 x i64> poison, <2 x i32> <i32 1, i32 0>
  %wide.load33 = load <2 x i64>, ptr %11, align 8, !tbaa !5
  %reverse34 = shufflevector <2 x i64> %wide.load33, <2 x i64> poison, <2 x i32> <i32 1, i32 0>
  %12 = add <2 x i64> %reverse, %vec.phi
  %13 = add <2 x i64> %reverse34, %vec.phi32
  %index.next = add nuw i64 %index, 4
  %14 = icmp eq i64 %index.next, %n.vec
  br i1 %14, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <2 x i64> %13, %12
  %15 = tail call i64 @llvm.vector.reduce.add.v2i64(<2 x i64> %bin.rdx)
  %cmp.n = icmp eq i64 %n.vec, %6
  br i1 %cmp.n, label %.preheader, label %.lr.ph.preheader35

.preheader:                                       ; preds = %.lr.ph, %middle.block, %3
  %.014.lcssa = phi i64 [ 0, %3 ], [ %15, %middle.block ], [ %20, %.lr.ph ]
  %16 = urem i32 %4, 500
  %.not20 = icmp eq i32 %16, 0
  br i1 %.not20, label %.loopexit, label %.lr.ph23.preheader

.lr.ph23.preheader:                               ; preds = %.preheader
  %17 = sext i32 %4 to i64
  br label %.lr.ph23

.lr.ph:                                           ; preds = %.lr.ph.preheader35, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ %indvars.iv.ph, %.lr.ph.preheader35 ]
  %.01418 = phi i64 [ %20, %.lr.ph ], [ %.01418.ph, %.lr.ph.preheader35 ]
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %18 = getelementptr inbounds [201 x i64], ptr @seg_sum, i64 0, i64 %indvars.iv.next
  %19 = load i64, ptr %18, align 8, !tbaa !5
  %20 = add nsw i64 %19, %.01418
  %21 = icmp ugt i64 %indvars.iv, 1
  br i1 %21, label %.lr.ph, label %.preheader, !llvm.loop !13

.lr.ph23:                                         ; preds = %.lr.ph23.preheader, %.lr.ph23
  %indvars.iv27 = phi i64 [ %17, %.lr.ph23.preheader ], [ %indvars.iv.next28, %.lr.ph23 ]
  %.122 = phi i64 [ %.014.lcssa, %.lr.ph23.preheader ], [ %30, %.lr.ph23 ]
  %indvars.iv.next28 = add nsw i64 %indvars.iv27, -1
  %22 = getelementptr inbounds [100500 x i32], ptr @R, i64 0, i64 %indvars.iv.next28
  %23 = load i32, ptr %22, align 4, !tbaa !14
  %24 = tail call noundef i64 @_Z4get2i(i32 noundef %23)
  %25 = getelementptr inbounds [100500 x i32], ptr @L, i64 0, i64 %indvars.iv.next28
  %26 = load i32, ptr %25, align 4, !tbaa !14
  %27 = add nsw i32 %26, -1
  %28 = tail call noundef i64 @_Z4get2i(i32 noundef %27)
  %29 = add i64 %24, %.122
  %30 = sub i64 %29, %28
  %31 = trunc nsw i64 %indvars.iv.next28 to i32
  %32 = srem i32 %31, 500
  %.not = icmp eq i32 %32, 0
  br i1 %.not, label %.loopexit, label %.lr.ph23, !llvm.loop !16

.loopexit:                                        ; preds = %.lr.ph23, %.preheader, %1
  %.016 = phi i64 [ 0, %1 ], [ %.014.lcssa, %.preheader ], [ %30, %.lr.ph23 ]
  ret i64 %.016
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.vector.reduce.add.v2i64(<2 x i64>) #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 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 = !{!"long long", !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 = !{!15, !15, i64 0}
!15 = !{!"int", !7, i64 0}
!16 = distinct !{!16, !10}


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