; LLVM IR from ippeb/ACM-ICPC :: data_structure/segment_tree/segment_tree_add_intervals.ll (Z13save_intervaliiiPiS_)

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

; Function Attrs: mustprogress uwtable
define dso_local void @_Z13save_intervaliiiPiS_(i32 noundef %0, i32 noundef %1, i32 noundef %2, ptr noundef %3, ptr noundef %4) #0 {
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  store i32 %0, ptr %6, align 4, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !5
  store i32 %2, ptr %8, align 4, !tbaa !5
  store ptr %3, ptr %9, align 8, !tbaa !9
  store ptr %4, ptr %10, align 8, !tbaa !9
  %12 = load ptr, ptr %9, align 8, !tbaa !9
  %13 = load i32, ptr %6, align 4, !tbaa !5
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds i32, ptr %12, i64 %14
  %16 = load i32, ptr %15, align 4, !tbaa !5
  %17 = icmp sge i32 %16, 1
  br i1 %17, label %18, label %34

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

20:                                               ; preds = %30, %18
  %21 = load i32, ptr %11, align 4, !tbaa !5
  %22 = load i32, ptr %8, align 4, !tbaa !5
  %23 = icmp sle i32 %21, %22
  br i1 %23, label %25, label %24

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

25:                                               ; preds = %20
  %26 = load ptr, ptr %10, align 8, !tbaa !9
  %27 = load i32, ptr %11, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds i32, ptr %26, i64 %28
  store i32 1, ptr %29, align 4, !tbaa !5
  br label %30

30:                                               ; preds = %25
  %31 = load i32, ptr %11, align 4, !tbaa !5
  %32 = add nsw i32 %31, 1
  store i32 %32, ptr %11, align 4, !tbaa !5
  br label %20, !llvm.loop !11

33:                                               ; preds = %24
  br label %60

34:                                               ; preds = %5
  %35 = load i32, ptr %7, align 4, !tbaa !5
  %36 = load i32, ptr %8, align 4, !tbaa !5
  %37 = icmp eq i32 %35, %36
  br i1 %37, label %38, label %39

38:                                               ; preds = %34
  br label %60

39:                                               ; preds = %34
  %40 = load i32, ptr %6, align 4, !tbaa !5
  %41 = mul nsw i32 %40, 2
  %42 = load i32, ptr %7, align 4, !tbaa !5
  %43 = load i32, ptr %7, align 4, !tbaa !5
  %44 = load i32, ptr %8, align 4, !tbaa !5
  %45 = add nsw i32 %43, %44
  %46 = sdiv i32 %45, 2
  %47 = load ptr, ptr %9, align 8, !tbaa !9
  %48 = load ptr, ptr %10, align 8, !tbaa !9
  call void @_Z13save_intervaliiiPiS_(i32 noundef %41, i32 noundef %42, i32 noundef %46, ptr noundef %47, ptr noundef %48)
  %49 = load i32, ptr %6, align 4, !tbaa !5
  %50 = mul nsw i32 %49, 2
  %51 = add nsw i32 %50, 1
  %52 = load i32, ptr %7, align 4, !tbaa !5
  %53 = load i32, ptr %8, align 4, !tbaa !5
  %54 = add nsw i32 %52, %53
  %55 = sdiv i32 %54, 2
  %56 = add nsw i32 %55, 1
  %57 = load i32, ptr %8, align 4, !tbaa !5
  %58 = load ptr, ptr %9, align 8, !tbaa !9
  %59 = load ptr, ptr %10, align 8, !tbaa !9
  call void @_Z13save_intervaliiiPiS_(i32 noundef %51, i32 noundef %56, i32 noundef %57, ptr noundef %58, ptr noundef %59)
  br label %60

60:                                               ; preds = %39, %38, %33
  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 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 = !{!"any pointer", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


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

; Function Attrs: mustprogress nofree nosync nounwind memory(argmem: readwrite) uwtable
define dso_local void @_Z13save_intervaliiiPiS_(i32 noundef %0, i32 noundef %1, i32 noundef %2, ptr nocapture noundef readonly %3, ptr nocapture noundef writeonly %4) local_unnamed_addr #0 {
  %6 = sext i32 %0 to i64
  %7 = getelementptr inbounds i32, ptr %3, i64 %6
  %8 = load i32, ptr %7, align 4, !tbaa !5
  %9 = icmp sgt i32 %8, 0
  br i1 %9, label %.preheader, label %.lr.ph

.preheader:                                       ; preds = %tailrecurse, %5
  %.tr23.lcssa = phi i32 [ %1, %5 ], [ %23, %tailrecurse ]
  %.not31 = icmp sgt i32 %.tr23.lcssa, %2
  br i1 %.not31, label %.loopexit, label %.lr.ph33.preheader

.lr.ph33.preheader:                               ; preds = %.preheader
  %10 = sext i32 %.tr23.lcssa to i64
  %11 = add i32 %2, 1
  %12 = sub i32 %2, %.tr23.lcssa
  %13 = zext i32 %12 to i64
  %14 = add nuw nsw i64 %13, 1
  %min.iters.check = icmp ult i32 %12, 7
  br i1 %min.iters.check, label %.lr.ph33.preheader38, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph33.preheader
  %n.vec = and i64 %14, 8589934584
  %ind.end = add nsw i64 %n.vec, %10
  %invariant.gep = getelementptr i32, ptr %4, i64 %10
  br label %vector.body

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

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %14, %n.vec
  br i1 %cmp.n, label %.loopexit, label %.lr.ph33.preheader38

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

.lr.ph33:                                         ; preds = %.lr.ph33.preheader38, %.lr.ph33
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph33 ], [ %indvars.iv.ph, %.lr.ph33.preheader38 ]
  %17 = getelementptr inbounds i32, ptr %4, i64 %indvars.iv
  store i32 1, ptr %17, align 4, !tbaa !5
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %lftr.wideiv = trunc i64 %indvars.iv.next to i32
  %exitcond.not = icmp eq i32 %11, %lftr.wideiv
  br i1 %exitcond.not, label %.loopexit, label %.lr.ph33, !llvm.loop !13

.lr.ph:                                           ; preds = %5, %tailrecurse
  %.tr2330 = phi i32 [ %23, %tailrecurse ], [ %1, %5 ]
  %.tr29 = phi i32 [ %22, %tailrecurse ], [ %0, %5 ]
  %18 = icmp eq i32 %.tr2330, %2
  br i1 %18, label %.loopexit, label %tailrecurse

tailrecurse:                                      ; preds = %.lr.ph
  %19 = shl nsw i32 %.tr29, 1
  %20 = add nsw i32 %.tr2330, %2
  %21 = sdiv i32 %20, 2
  tail call void @_Z13save_intervaliiiPiS_(i32 noundef %19, i32 noundef %.tr2330, i32 noundef %21, ptr noundef nonnull %3, ptr noundef %4)
  %22 = or disjoint i32 %19, 1
  %23 = add nsw i32 %21, 1
  %24 = sext i32 %22 to i64
  %25 = getelementptr inbounds i32, ptr %3, i64 %24
  %26 = load i32, ptr %25, align 4, !tbaa !5
  %27 = icmp sgt i32 %26, 0
  br i1 %27, label %.preheader, label %.lr.ph

.loopexit:                                        ; preds = %.lr.ph, %.lr.ph33, %middle.block, %.preheader
  ret void
}

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


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