; LLVM IR from ncduy0303/Competitive-Programming :: Data Structures/Segment Tree/Advanced example 1.ll (Z6updateiiiii)

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

%struct.tdata = type { i64, i64, i64, i64 }

@arr = external dso_local global [200001 x i32], align 16
@st = external dso_local global [800004 x %struct.tdata], align 16

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN5tdataC2Ei(ptr noundef nonnull align 8 dereferenceable(32), i32 noundef) unnamed_addr #1 align 2

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN5tdataC2ES_S_(ptr noundef nonnull align 8 dereferenceable(32), ptr noundef byval(%struct.tdata) align 8, ptr noundef byval(%struct.tdata) align 8) unnamed_addr #1 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_Z6updateiiiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4) #1 {
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca %struct.tdata, align 8
  %12 = alloca i32, align 4
  %13 = alloca %struct.tdata, align 8
  %14 = alloca %struct.tdata, align 8
  %15 = alloca %struct.tdata, align 8
  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 i32 %3, ptr %9, align 4, !tbaa !5
  store i32 %4, ptr %10, align 4, !tbaa !5
  %16 = load i32, ptr %7, align 4, !tbaa !5
  %17 = load i32, ptr %8, align 4, !tbaa !5
  %18 = icmp eq i32 %16, %17
  br i1 %18, label %19, label %28

19:                                               ; preds = %5
  %20 = load i32, ptr %10, align 4, !tbaa !5
  %21 = load i32, ptr %9, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [200001 x i32], ptr @arr, i64 0, i64 %22
  store i32 %20, ptr %23, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 32, ptr %11) #3
  %24 = load i32, ptr %10, align 4, !tbaa !5
  call void @_ZN5tdataC2Ei(ptr noundef nonnull align 8 dereferenceable(32) %11, i32 noundef %24)
  %25 = load i32, ptr %6, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [800004 x %struct.tdata], ptr @st, i64 0, i64 %26
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %27, ptr align 8 %11, i64 32, i1 false), !tbaa.struct !9
  call void @llvm.lifetime.end.p0(i64 32, ptr %11) #3
  br label %65

28:                                               ; preds = %5
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  %29 = load i32, ptr %7, align 4, !tbaa !5
  %30 = load i32, ptr %8, align 4, !tbaa !5
  %31 = add nsw i32 %29, %30
  %32 = sdiv i32 %31, 2
  store i32 %32, ptr %12, align 4, !tbaa !5
  %33 = load i32, ptr %9, align 4, !tbaa !5
  %34 = load i32, ptr %12, align 4, !tbaa !5
  %35 = icmp sle i32 %33, %34
  br i1 %35, label %36, label %43

36:                                               ; preds = %28
  %37 = load i32, ptr %6, align 4, !tbaa !5
  %38 = mul nsw i32 2, %37
  %39 = load i32, ptr %7, align 4, !tbaa !5
  %40 = load i32, ptr %12, align 4, !tbaa !5
  %41 = load i32, ptr %9, align 4, !tbaa !5
  %42 = load i32, ptr %10, align 4, !tbaa !5
  call void @_Z6updateiiiii(i32 noundef %38, i32 noundef %39, i32 noundef %40, i32 noundef %41, i32 noundef %42)
  br label %52

43:                                               ; preds = %28
  %44 = load i32, ptr %6, align 4, !tbaa !5
  %45 = mul nsw i32 2, %44
  %46 = add nsw i32 %45, 1
  %47 = load i32, ptr %12, align 4, !tbaa !5
  %48 = add nsw i32 %47, 1
  %49 = load i32, ptr %8, align 4, !tbaa !5
  %50 = load i32, ptr %9, align 4, !tbaa !5
  %51 = load i32, ptr %10, align 4, !tbaa !5
  call void @_Z6updateiiiii(i32 noundef %46, i32 noundef %48, i32 noundef %49, i32 noundef %50, i32 noundef %51)
  br label %52

52:                                               ; preds = %43, %36
  call void @llvm.lifetime.start.p0(i64 32, ptr %13) #3
  %53 = load i32, ptr %6, align 4, !tbaa !5
  %54 = mul nsw i32 2, %53
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds [800004 x %struct.tdata], ptr @st, i64 0, i64 %55
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %14, ptr align 16 %56, i64 32, i1 false), !tbaa.struct !9
  %57 = load i32, ptr %6, align 4, !tbaa !5
  %58 = mul nsw i32 2, %57
  %59 = add nsw i32 %58, 1
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds [800004 x %struct.tdata], ptr @st, i64 0, i64 %60
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %15, ptr align 16 %61, i64 32, i1 false), !tbaa.struct !9
  call void @_ZN5tdataC2ES_S_(ptr noundef nonnull align 8 dereferenceable(32) %13, ptr noundef byval(%struct.tdata) align 8 %14, ptr noundef byval(%struct.tdata) align 8 %15)
  %62 = load i32, ptr %6, align 4, !tbaa !5
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds [800004 x %struct.tdata], ptr @st, i64 0, i64 %63
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %64, ptr align 8 %13, i64 32, i1 false), !tbaa.struct !9
  call void @llvm.lifetime.end.p0(i64 32, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  br label %65

65:                                               ; preds = %52, %19
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 = !{i64 0, i64 8, !10, i64 8, i64 8, !10, i64 16, i64 8, !10, i64 24, i64 8, !10}
!10 = !{!11, !11, i64 0}
!11 = !{!"long long", !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"

%struct.tdata = type { i64, i64, i64, i64 }

@arr = external dso_local local_unnamed_addr global [200001 x i32], align 16
@st = external dso_local local_unnamed_addr global [800004 x %struct.tdata], align 16

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN5tdataC2Ei(ptr noundef nonnull align 8 dereferenceable(32), i32 noundef) unnamed_addr #1 align 2

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN5tdataC2ES_S_(ptr noundef nonnull align 8 dereferenceable(32), ptr noundef byval(%struct.tdata) align 8, ptr noundef byval(%struct.tdata) align 8) unnamed_addr #1 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_Z6updateiiiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4) local_unnamed_addr #1 {
  %6 = alloca %struct.tdata, align 8
  %7 = alloca %struct.tdata, align 8
  %8 = icmp eq i32 %1, %2
  br i1 %8, label %9, label %14

9:                                                ; preds = %5
  %10 = sext i32 %3 to i64
  %11 = getelementptr inbounds [200001 x i32], ptr @arr, i64 0, i64 %10
  store i32 %4, ptr %11, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %6) #3
  call void @_ZN5tdataC2Ei(ptr noundef nonnull align 8 dereferenceable(32) %6, i32 noundef %4)
  %12 = sext i32 %0 to i64
  %13 = getelementptr inbounds [800004 x %struct.tdata], ptr @st, i64 0, i64 %12
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(32) %13, ptr noundef nonnull align 8 dereferenceable(32) %6, i64 32, i1 false), !tbaa.struct !9
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %6) #3
  br label %29

14:                                               ; preds = %5
  %15 = add nsw i32 %2, %1
  %16 = sdiv i32 %15, 2
  %.not = icmp slt i32 %16, %3
  %17 = shl nsw i32 %0, 1
  br i1 %.not, label %19, label %18

18:                                               ; preds = %14
  tail call void @_Z6updateiiiii(i32 noundef %17, i32 noundef %1, i32 noundef %16, i32 noundef %3, i32 noundef %4)
  %.pre = or disjoint i32 %17, 1
  br label %22

19:                                               ; preds = %14
  %20 = or disjoint i32 %17, 1
  %21 = add nsw i32 %16, 1
  tail call void @_Z6updateiiiii(i32 noundef %20, i32 noundef %21, i32 noundef %2, i32 noundef %3, i32 noundef %4)
  br label %22

22:                                               ; preds = %19, %18
  %.pre-phi27 = phi i32 [ %20, %19 ], [ %.pre, %18 ]
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %7) #3
  %23 = sext i32 %17 to i64
  %24 = getelementptr inbounds [800004 x %struct.tdata], ptr @st, i64 0, i64 %23
  %25 = sext i32 %.pre-phi27 to i64
  %26 = getelementptr inbounds [800004 x %struct.tdata], ptr @st, i64 0, i64 %25
  call void @_ZN5tdataC2ES_S_(ptr noundef nonnull align 8 dereferenceable(32) %7, ptr noundef nonnull byval(%struct.tdata) align 8 %24, ptr noundef nonnull byval(%struct.tdata) align 8 %26)
  %27 = sext i32 %0 to i64
  %28 = getelementptr inbounds [800004 x %struct.tdata], ptr @st, i64 0, i64 %27
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(32) %28, ptr noundef nonnull align 8 dereferenceable(32) %7, i64 32, i1 false), !tbaa.struct !9
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %7) #3
  br label %29

29:                                               ; preds = %22, %9
  ret void
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 = !{i64 0, i64 8, !10, i64 8, i64 8, !10, i64 16, i64 8, !10, i64 24, i64 8, !10}
!10 = !{!11, !11, i64 0}
!11 = !{!"long long", !7, i64 0}


; Effective O3 passes: DSEPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,MemCpyOptPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass