; LLVM IR from tangjz/acm-icpc :: lydsy/3673.ll (Z5buildRiii)

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

@tot = external dso_local global i32, align 4
@pL = external dso_local global [8000001 x i32], align 16
@pR = external dso_local global [8000001 x i32], align 16
@dsu = external dso_local global [8000001 x i32], align 16

; Function Attrs: mustprogress uwtable
define dso_local void @_Z5buildRiii(ptr noundef nonnull align 4 dereferenceable(4) %0, i32 noundef %1, i32 noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  store i32 %2, ptr %6, align 4, !tbaa !9
  %8 = load i32, ptr @tot, align 4, !tbaa !9
  %9 = add nsw i32 %8, 1
  store i32 %9, ptr @tot, align 4, !tbaa !9
  %10 = load ptr, ptr %4, align 8, !tbaa !5
  store i32 %8, ptr %10, align 4, !tbaa !9
  %11 = load i32, ptr %5, align 4, !tbaa !9
  %12 = load i32, ptr %6, align 4, !tbaa !9
  %13 = icmp eq i32 %11, %12
  br i1 %13, label %14, label %19

14:                                               ; preds = %3
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  %16 = load i32, ptr %15, align 4, !tbaa !9
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [8000001 x i32], ptr @dsu, i64 0, i64 %17
  store i32 -1, ptr %18, align 4, !tbaa !9
  br label %37

19:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %20 = load i32, ptr %5, align 4, !tbaa !9
  %21 = load i32, ptr %6, align 4, !tbaa !9
  %22 = add nsw i32 %20, %21
  %23 = ashr i32 %22, 1
  store i32 %23, ptr %7, align 4, !tbaa !9
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = load i32, ptr %24, align 4, !tbaa !9
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [8000001 x i32], ptr @pL, i64 0, i64 %26
  %28 = load i32, ptr %5, align 4, !tbaa !9
  %29 = load i32, ptr %7, align 4, !tbaa !9
  call void @_Z5buildRiii(ptr noundef nonnull align 4 dereferenceable(4) %27, i32 noundef %28, i32 noundef %29)
  %30 = load ptr, ptr %4, align 8, !tbaa !5
  %31 = load i32, ptr %30, align 4, !tbaa !9
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [8000001 x i32], ptr @pR, i64 0, i64 %32
  %34 = load i32, ptr %7, align 4, !tbaa !9
  %35 = add nsw i32 %34, 1
  %36 = load i32, ptr %6, align 4, !tbaa !9
  call void @_Z5buildRiii(ptr noundef nonnull align 4 dereferenceable(4) %33, i32 noundef %35, i32 noundef %36)
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  br label %37

37:                                               ; preds = %19, %14
  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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}


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

@tot = external dso_local local_unnamed_addr global i32, align 4
@pL = external dso_local global [8000001 x i32], align 16
@pR = external dso_local local_unnamed_addr global [8000001 x i32], align 16
@dsu = external dso_local local_unnamed_addr global [8000001 x i32], align 16

; Function Attrs: mustprogress nofree nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @_Z5buildRiii(ptr nocapture noundef nonnull align 4 dereferenceable(4) %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = load i32, ptr @tot, align 4, !tbaa !5
  %5 = add nsw i32 %4, 1
  store i32 %5, ptr @tot, align 4, !tbaa !5
  store i32 %4, ptr %0, align 4, !tbaa !5
  %6 = icmp eq i32 %1, %2
  br i1 %6, label %tailrecurse._crit_edge, label %tailrecurse

tailrecurse._crit_edge:                           ; preds = %tailrecurse, %3
  %.lcssa = phi i32 [ %4, %3 ], [ %18, %tailrecurse ]
  %7 = sext i32 %.lcssa to i64
  %8 = getelementptr inbounds [8000001 x i32], ptr @dsu, i64 0, i64 %7
  store i32 -1, ptr %8, align 4, !tbaa !5
  ret void

tailrecurse:                                      ; preds = %3, %tailrecurse
  %9 = phi i32 [ %18, %tailrecurse ], [ %4, %3 ]
  %.tr1215 = phi i32 [ %17, %tailrecurse ], [ %1, %3 ]
  %.tr14 = phi ptr [ %16, %tailrecurse ], [ %0, %3 ]
  %10 = add nsw i32 %.tr1215, %2
  %11 = ashr i32 %10, 1
  %12 = sext i32 %9 to i64
  %13 = getelementptr inbounds [8000001 x i32], ptr @pL, i64 0, i64 %12
  tail call void @_Z5buildRiii(ptr noundef nonnull align 4 dereferenceable(4) %13, i32 noundef %.tr1215, i32 noundef %11)
  %14 = load i32, ptr %.tr14, align 4, !tbaa !5
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds [8000001 x i32], ptr @pR, i64 0, i64 %15
  %17 = add nsw i32 %11, 1
  %18 = load i32, ptr @tot, align 4, !tbaa !5
  %19 = add nsw i32 %18, 1
  store i32 %19, ptr @tot, align 4, !tbaa !5
  store i32 %18, ptr %16, align 4, !tbaa !5
  %20 = icmp eq i32 %17, %2
  br i1 %20, label %tailrecurse._crit_edge, label %tailrecurse
}

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


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