; LLVM IR from reyadussalahin/problem-solving :: data_structure/segment_tree/codeforces/F558E (Solution)

; 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.Node = type { [28 x i32] }

@t = external dso_local global [400400 x %struct.Node], align 16
@sw = external dso_local global [400400 x i32], align 16

; Function Attrs: nounwind uwtable
declare dso_local void @merge(ptr dead_on_unwind noalias writable sret(%struct.Node) align 4, ptr noundef byval(%struct.Node) align 8, ptr noundef byval(%struct.Node) align 8) #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: nounwind uwtable
declare dso_local void @split(ptr noundef byval(%struct.Node) align 8, ptr noundef, ptr noundef, i32 noundef) #0

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

21:                                               ; preds = %7
  %22 = load i32, ptr %10, align 4, !tbaa !5
  %23 = load i32, ptr %12, align 4, !tbaa !5
  %24 = icmp eq i32 %22, %23
  br i1 %24, label %25, label %33

25:                                               ; preds = %21
  %26 = load i32, ptr %8, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [400400 x %struct.Node], ptr @t, i64 0, i64 %27
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %28, ptr align 8 %5, i64 112, i1 false), !tbaa.struct !9
  %29 = load i32, ptr %13, align 4, !tbaa !5
  %30 = load i32, ptr %8, align 4, !tbaa !5
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [400400 x i32], ptr @sw, i64 0, i64 %31
  store i32 %29, ptr %32, align 4, !tbaa !5
  br label %111

33:                                               ; preds = %21, %7
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #3
  %34 = load i32, ptr %9, align 4, !tbaa !5
  %35 = load i32, ptr %10, align 4, !tbaa !5
  %36 = add nsw i32 %34, %35
  %37 = sdiv i32 %36, 2
  store i32 %37, ptr %14, align 4, !tbaa !5
  %38 = load i32, ptr %12, align 4, !tbaa !5
  %39 = load i32, ptr %14, align 4, !tbaa !5
  %40 = icmp sle i32 %38, %39
  br i1 %40, label %41, label %50

41:                                               ; preds = %33
  %42 = load i32, ptr %8, align 4, !tbaa !5
  %43 = mul nsw i32 2, %42
  %44 = add nsw i32 %43, 1
  %45 = load i32, ptr %9, align 4, !tbaa !5
  %46 = load i32, ptr %14, align 4, !tbaa !5
  %47 = load i32, ptr %11, align 4, !tbaa !5
  %48 = load i32, ptr %12, align 4, !tbaa !5
  %49 = load i32, ptr %13, align 4, !tbaa !5
  call void @distribute(i32 noundef %44, i32 noundef %45, i32 noundef %46, i32 noundef %47, i32 noundef %48, ptr noundef byval(%struct.Node) align 8 %5, i32 noundef %49)
  br label %97

50:                                               ; preds = %33
  %51 = load i32, ptr %11, align 4, !tbaa !5
  %52 = load i32, ptr %14, align 4, !tbaa !5
  %53 = icmp sgt i32 %51, %52
  br i1 %53, label %54, label %64

54:                                               ; preds = %50
  %55 = load i32, ptr %8, align 4, !tbaa !5
  %56 = mul nsw i32 2, %55
  %57 = add nsw i32 %56, 2
  %58 = load i32, ptr %14, align 4, !tbaa !5
  %59 = add nsw i32 %58, 1
  %60 = load i32, ptr %10, align 4, !tbaa !5
  %61 = load i32, ptr %11, align 4, !tbaa !5
  %62 = load i32, ptr %12, align 4, !tbaa !5
  %63 = load i32, ptr %13, align 4, !tbaa !5
  call void @distribute(i32 noundef %57, i32 noundef %59, i32 noundef %60, i32 noundef %61, i32 noundef %62, ptr noundef byval(%struct.Node) align 8 %5, i32 noundef %63)
  br label %96

64:                                               ; preds = %50
  call void @llvm.lifetime.start.p0(i64 112, ptr %15) #3
  call void @llvm.lifetime.start.p0(i64 112, ptr %16) #3
  %65 = load i32, ptr %13, align 4, !tbaa !5
  %66 = and i32 %65, 1
  %67 = icmp ne i32 %66, 0
  br i1 %67, label %68, label %73

68:                                               ; preds = %64
  %69 = load i32, ptr %14, align 4, !tbaa !5
  %70 = load i32, ptr %11, align 4, !tbaa !5
  %71 = sub nsw i32 %69, %70
  %72 = add nsw i32 %71, 1
  call void @split(ptr noundef byval(%struct.Node) align 8 %5, ptr noundef %15, ptr noundef %16, i32 noundef %72)
  br label %77

73:                                               ; preds = %64
  %74 = load i32, ptr %12, align 4, !tbaa !5
  %75 = load i32, ptr %14, align 4, !tbaa !5
  %76 = sub nsw i32 %74, %75
  call void @split(ptr noundef byval(%struct.Node) align 8 %5, ptr noundef %16, ptr noundef %15, i32 noundef %76)
  br label %77

77:                                               ; preds = %73, %68
  %78 = load i32, ptr %8, align 4, !tbaa !5
  %79 = mul nsw i32 2, %78
  %80 = add nsw i32 %79, 1
  %81 = load i32, ptr %9, align 4, !tbaa !5
  %82 = load i32, ptr %14, align 4, !tbaa !5
  %83 = load i32, ptr %11, align 4, !tbaa !5
  %84 = load i32, ptr %14, align 4, !tbaa !5
  %85 = load i32, ptr %13, align 4, !tbaa !5
  call void @distribute(i32 noundef %80, i32 noundef %81, i32 noundef %82, i32 noundef %83, i32 noundef %84, ptr noundef byval(%struct.Node) align 8 %15, i32 noundef %85)
  %86 = load i32, ptr %8, align 4, !tbaa !5
  %87 = mul nsw i32 2, %86
  %88 = add nsw i32 %87, 2
  %89 = load i32, ptr %14, align 4, !tbaa !5
  %90 = add nsw i32 %89, 1
  %91 = load i32, ptr %10, align 4, !tbaa !5
  %92 = load i32, ptr %14, align 4, !tbaa !5
  %93 = add nsw i32 %92, 1
  %94 = load i32, ptr %12, align 4, !tbaa !5
  %95 = load i32, ptr %13, align 4, !tbaa !5
  call void @distribute(i32 noundef %88, i32 noundef %90, i32 noundef %91, i32 noundef %93, i32 noundef %94, ptr noundef byval(%struct.Node) align 8 %16, i32 noundef %95)
  call void @llvm.lifetime.end.p0(i64 112, ptr %16) #3
  call void @llvm.lifetime.end.p0(i64 112, ptr %15) #3
  br label %96

96:                                               ; preds = %77, %54
  br label %97

97:                                               ; preds = %96, %41
  %98 = load i32, ptr %8, align 4, !tbaa !5
  %99 = sext i32 %98 to i64
  %100 = getelementptr inbounds [400400 x %struct.Node], ptr @t, i64 0, i64 %99
  call void @llvm.lifetime.start.p0(i64 112, ptr %17) #3
  %101 = load i32, ptr %8, align 4, !tbaa !5
  %102 = mul nsw i32 2, %101
  %103 = add nsw i32 %102, 1
  %104 = sext i32 %103 to i64
  %105 = getelementptr inbounds [400400 x %struct.Node], ptr @t, i64 0, i64 %104
  %106 = load i32, ptr %8, align 4, !tbaa !5
  %107 = mul nsw i32 2, %106
  %108 = add nsw i32 %107, 2
  %109 = sext i32 %108 to i64
  %110 = getelementptr inbounds [400400 x %struct.Node], ptr @t, i64 0, i64 %109
  call void @merge(ptr dead_on_unwind writable sret(%struct.Node) align 4 %17, ptr noundef byval(%struct.Node) align 8 %105, ptr noundef byval(%struct.Node) align 8 %110)
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %100, ptr align 4 %17, i64 112, i1 false), !tbaa.struct !9
  call void @llvm.lifetime.end.p0(i64 112, ptr %17) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #3
  br label %111

111:                                              ; preds = %97, %25
  ret void
}

attributes #0 = { 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 = { 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/C++ TBAA"}
!9 = !{i64 0, i64 112, !10}
!10 = !{!7, !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.Node = type { [28 x i32] }

@t = external dso_local global [400400 x %struct.Node], align 16
@sw = external dso_local local_unnamed_addr global [400400 x i32], align 16

; Function Attrs: nounwind uwtable
declare dso_local void @merge(ptr dead_on_unwind noalias writable sret(%struct.Node) align 4, ptr noundef byval(%struct.Node) align 8, ptr noundef byval(%struct.Node) align 8) local_unnamed_addr #0

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

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

; Function Attrs: nounwind uwtable
declare dso_local void @split(ptr noundef byval(%struct.Node) align 8, ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #0

; 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: nounwind uwtable
define dso_local void @distribute(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4, ptr noundef byval(%struct.Node) align 8 %5, i32 noundef %6) local_unnamed_addr #0 {
  %8 = alloca %struct.Node, align 8
  %9 = alloca %struct.Node, align 8
  %10 = alloca %struct.Node, align 4
  %11 = icmp eq i32 %1, %3
  %12 = icmp eq i32 %2, %4
  %or.cond = and i1 %11, %12
  br i1 %or.cond, label %13, label %17

13:                                               ; preds = %7
  %14 = sext i32 %0 to i64
  %15 = getelementptr inbounds [400400 x %struct.Node], ptr @t, i64 0, i64 %14
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(112) %15, ptr noundef nonnull align 8 dereferenceable(112) %5, i64 112, i1 false), !tbaa.struct !5
  %16 = getelementptr inbounds [400400 x i32], ptr @sw, i64 0, i64 %14
  store i32 %6, ptr %16, align 4, !tbaa !9
  br label %47

17:                                               ; preds = %7
  %18 = add nsw i32 %2, %1
  %19 = sdiv i32 %18, 2
  %.not = icmp slt i32 %19, %4
  br i1 %.not, label %23, label %20

20:                                               ; preds = %17
  %21 = shl nsw i32 %0, 1
  %22 = or disjoint i32 %21, 1
  tail call void @distribute(i32 noundef %22, i32 noundef %1, i32 noundef %19, i32 noundef %3, i32 noundef %4, ptr noundef nonnull byval(%struct.Node) align 8 %5, i32 noundef %6)
  %.pre51 = add nsw i32 %21, 2
  br label %40

23:                                               ; preds = %17
  %24 = icmp slt i32 %19, %3
  br i1 %24, label %25, label %29

25:                                               ; preds = %23
  %26 = shl nsw i32 %0, 1
  %27 = add nsw i32 %26, 2
  %28 = add nsw i32 %19, 1
  tail call void @distribute(i32 noundef %27, i32 noundef %28, i32 noundef %2, i32 noundef %3, i32 noundef %4, ptr noundef nonnull byval(%struct.Node) align 8 %5, i32 noundef %6)
  %.pre = or disjoint i32 %26, 1
  br label %40

29:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 112, ptr nonnull %8) #3
  call void @llvm.lifetime.start.p0(i64 112, ptr nonnull %9) #3
  %30 = and i32 %6, 1
  %.not49 = icmp eq i32 %30, 0
  br i1 %.not49, label %33, label %31

31:                                               ; preds = %29
  %reass.sub = sub i32 %19, %3
  %32 = add i32 %reass.sub, 1
  call void @split(ptr noundef nonnull byval(%struct.Node) align 8 %5, ptr noundef nonnull %8, ptr noundef nonnull %9, i32 noundef %32)
  br label %35

33:                                               ; preds = %29
  %34 = sub nsw i32 %4, %19
  call void @split(ptr noundef nonnull byval(%struct.Node) align 8 %5, ptr noundef nonnull %9, ptr noundef nonnull %8, i32 noundef %34)
  br label %35

35:                                               ; preds = %33, %31
  %36 = shl nsw i32 %0, 1
  %37 = or disjoint i32 %36, 1
  call void @distribute(i32 noundef %37, i32 noundef %1, i32 noundef %19, i32 noundef %3, i32 noundef %19, ptr noundef nonnull byval(%struct.Node) align 8 %8, i32 noundef %6)
  %38 = add nsw i32 %36, 2
  %39 = add nsw i32 %19, 1
  call void @distribute(i32 noundef %38, i32 noundef %39, i32 noundef %2, i32 noundef %39, i32 noundef %4, ptr noundef nonnull byval(%struct.Node) align 8 %9, i32 noundef %6)
  call void @llvm.lifetime.end.p0(i64 112, ptr nonnull %9) #3
  call void @llvm.lifetime.end.p0(i64 112, ptr nonnull %8) #3
  br label %40

40:                                               ; preds = %25, %35, %20
  %.pre-phi52 = phi i32 [ %27, %25 ], [ %38, %35 ], [ %.pre51, %20 ]
  %.pre-phi50 = phi i32 [ %.pre, %25 ], [ %37, %35 ], [ %22, %20 ]
  %41 = sext i32 %0 to i64
  %42 = getelementptr inbounds [400400 x %struct.Node], ptr @t, i64 0, i64 %41
  call void @llvm.lifetime.start.p0(i64 112, ptr nonnull %10) #3
  %43 = sext i32 %.pre-phi50 to i64
  %44 = getelementptr inbounds [400400 x %struct.Node], ptr @t, i64 0, i64 %43
  %45 = sext i32 %.pre-phi52 to i64
  %46 = getelementptr inbounds [400400 x %struct.Node], ptr @t, i64 0, i64 %45
  call void @merge(ptr dead_on_unwind nonnull writable sret(%struct.Node) align 4 %10, ptr noundef nonnull byval(%struct.Node) align 8 %44, ptr noundef nonnull byval(%struct.Node) align 8 %46)
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(112) %42, ptr noundef nonnull align 4 dereferenceable(112) %10, i64 112, i1 false), !tbaa.struct !5
  call void @llvm.lifetime.end.p0(i64 112, ptr nonnull %10) #3
  br label %47

47:                                               ; preds = %40, %13
  ret void
}

attributes #0 = { 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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{i64 0, i64 112, !6}
!6 = !{!7, !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}


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