; LLVM IR from ippeb/ACM-ICPC :: data_structure/binary_indexed_tree/2d_binary_indexed_tree.ll (Z6updateiii)

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

@tree = external dso_local global [1025 x [1025 x i32]], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z6updateiii(i32 noundef %0, i32 noundef %1, i32 noundef %2) #0 {
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !5
  store i32 %2, ptr %6, align 4, !tbaa !5
  %8 = load i32, ptr %4, align 4, !tbaa !5
  %9 = add nsw i32 %8, 1
  store i32 %9, ptr %4, align 4, !tbaa !5
  %10 = load i32, ptr %5, align 4, !tbaa !5
  %11 = add nsw i32 %10, 1
  store i32 %11, ptr %5, align 4, !tbaa !5
  br label %12

12:                                               ; preds = %36, %3
  %13 = load i32, ptr %4, align 4, !tbaa !5
  %14 = icmp sle i32 %13, 1025
  br i1 %14, label %15, label %43

15:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %16 = load i32, ptr %5, align 4, !tbaa !5
  store i32 %16, ptr %7, align 4, !tbaa !5
  br label %17

17:                                               ; preds = %20, %15
  %18 = load i32, ptr %7, align 4, !tbaa !5
  %19 = icmp sle i32 %18, 1025
  br i1 %19, label %20, label %36

20:                                               ; preds = %17
  %21 = load i32, ptr %6, align 4, !tbaa !5
  %22 = load i32, ptr %4, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [1025 x [1025 x i32]], ptr @tree, i64 0, i64 %23
  %25 = load i32, ptr %7, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [1025 x i32], ptr %24, i64 0, i64 %26
  %28 = load i32, ptr %27, align 4, !tbaa !5
  %29 = add nsw i32 %28, %21
  store i32 %29, ptr %27, align 4, !tbaa !5
  %30 = load i32, ptr %7, align 4, !tbaa !5
  %31 = load i32, ptr %7, align 4, !tbaa !5
  %32 = sub nsw i32 0, %31
  %33 = and i32 %30, %32
  %34 = load i32, ptr %7, align 4, !tbaa !5
  %35 = add nsw i32 %34, %33
  store i32 %35, ptr %7, align 4, !tbaa !5
  br label %17, !llvm.loop !9

36:                                               ; preds = %17
  %37 = load i32, ptr %4, align 4, !tbaa !5
  %38 = load i32, ptr %4, align 4, !tbaa !5
  %39 = sub nsw i32 0, %38
  %40 = and i32 %37, %39
  %41 = load i32, ptr %4, align 4, !tbaa !5
  %42 = add nsw i32 %41, %40
  store i32 %42, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  br label %12, !llvm.loop !11

43:                                               ; preds = %12
  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}


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

@tree = external dso_local local_unnamed_addr global [1025 x [1025 x i32]], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z6updateiii(i32 noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = add nsw i32 %1, 1
  %5 = icmp slt i32 %0, 1025
  %6 = icmp slt i32 %1, 1025
  %or.cond = and i1 %5, %6
  br i1 %or.cond, label %.preheader.us.preheader, label %._crit_edge17

.preheader.us.preheader:                          ; preds = %3
  %7 = add nsw i32 %0, 1
  br label %.preheader.us

.preheader.us:                                    ; preds = %.preheader.us.preheader, %._crit_edge.us
  %.01416.us = phi i32 [ %20, %._crit_edge.us ], [ %7, %.preheader.us.preheader ]
  %8 = sext i32 %.01416.us to i64
  br label %9

9:                                                ; preds = %.preheader.us, %9
  %.015.us = phi i32 [ %4, %.preheader.us ], [ %16, %9 ]
  %10 = sext i32 %.015.us to i64
  %11 = getelementptr inbounds [1025 x [1025 x i32]], ptr @tree, i64 0, i64 %8, i64 %10
  %12 = load i32, ptr %11, align 4, !tbaa !5
  %13 = add nsw i32 %12, %2
  store i32 %13, ptr %11, align 4, !tbaa !5
  %14 = sub nsw i32 0, %.015.us
  %15 = and i32 %.015.us, %14
  %16 = add nsw i32 %15, %.015.us
  %17 = icmp slt i32 %16, 1026
  br i1 %17, label %9, label %._crit_edge.us, !llvm.loop !9

._crit_edge.us:                                   ; preds = %9
  %18 = sub nsw i32 0, %.01416.us
  %19 = and i32 %.01416.us, %18
  %20 = add nsw i32 %19, %.01416.us
  %21 = icmp slt i32 %20, 1026
  br i1 %21, label %.preheader.us, label %._crit_edge17, !llvm.loop !11

._crit_edge17:                                    ; preds = %._crit_edge.us, %3
  ret void
}

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


; Effective O3 passes: GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass