; 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
@flag = 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: 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
declare dso_local void @lazy(i32 noundef, i32 noundef, i32 noundef) #0

; Function Attrs: nounwind uwtable
define dso_local void @combine(ptr dead_on_unwind noalias writable sret(%struct.Node) align 4 %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4, i32 noundef %5) #0 {
  %7 = alloca i32, align 4
  %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 %struct.Node, align 8
  %15 = alloca %struct.Node, align 8
  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
  store i32 %5, ptr %11, align 4, !tbaa !5
  %16 = load i32, ptr %8, align 4, !tbaa !5
  %17 = load i32, ptr %10, align 4, !tbaa !5
  %18 = icmp eq i32 %16, %17
  br i1 %18, label %19, label %30

19:                                               ; preds = %6
  %20 = load i32, ptr %9, align 4, !tbaa !5
  %21 = load i32, ptr %11, align 4, !tbaa !5
  %22 = icmp eq i32 %20, %21
  br i1 %22, label %23, label %30

23:                                               ; preds = %19
  %24 = load i32, ptr %7, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [400400 x i32], ptr @flag, i64 0, i64 %25
  store i32 1, ptr %26, align 4, !tbaa !5
  %27 = load i32, ptr %7, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [400400 x %struct.Node], ptr @t, i64 0, i64 %28
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %0, ptr align 16 %29, i64 112, i1 false), !tbaa.struct !9
  br label %87

30:                                               ; preds = %19, %6
  %31 = load i32, ptr %7, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [400400 x i32], ptr @sw, i64 0, i64 %32
  %34 = load i32, ptr %33, align 4, !tbaa !5
  %35 = icmp ne i32 %34, 0
  br i1 %35, label %36, label %40

36:                                               ; preds = %30
  %37 = load i32, ptr %7, align 4, !tbaa !5
  %38 = load i32, ptr %8, align 4, !tbaa !5
  %39 = load i32, ptr %9, align 4, !tbaa !5
  call void @lazy(i32 noundef %37, i32 noundef %38, i32 noundef %39)
  br label %40

40:                                               ; preds = %36, %30
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  %41 = load i32, ptr %8, align 4, !tbaa !5
  %42 = load i32, ptr %9, align 4, !tbaa !5
  %43 = add nsw i32 %41, %42
  %44 = sdiv i32 %43, 2
  store i32 %44, ptr %12, align 4, !tbaa !5
  %45 = load i32, ptr %11, align 4, !tbaa !5
  %46 = load i32, ptr %12, align 4, !tbaa !5
  %47 = icmp sle i32 %45, %46
  br i1 %47, label %48, label %56

48:                                               ; preds = %40
  %49 = load i32, ptr %7, align 4, !tbaa !5
  %50 = mul nsw i32 2, %49
  %51 = add nsw i32 %50, 1
  %52 = load i32, ptr %8, align 4, !tbaa !5
  %53 = load i32, ptr %12, align 4, !tbaa !5
  %54 = load i32, ptr %10, align 4, !tbaa !5
  %55 = load i32, ptr %11, align 4, !tbaa !5
  call void @combine(ptr dead_on_unwind writable sret(%struct.Node) align 4 %0, i32 noundef %51, i32 noundef %52, i32 noundef %53, i32 noundef %54, i32 noundef %55)
  store i32 1, ptr %13, align 4
  br label %86

56:                                               ; preds = %40
  %57 = load i32, ptr %10, align 4, !tbaa !5
  %58 = load i32, ptr %12, align 4, !tbaa !5
  %59 = icmp sgt i32 %57, %58
  br i1 %59, label %60, label %69

60:                                               ; preds = %56
  %61 = load i32, ptr %7, align 4, !tbaa !5
  %62 = mul nsw i32 2, %61
  %63 = add nsw i32 %62, 2
  %64 = load i32, ptr %12, align 4, !tbaa !5
  %65 = add nsw i32 %64, 1
  %66 = load i32, ptr %9, align 4, !tbaa !5
  %67 = load i32, ptr %10, align 4, !tbaa !5
  %68 = load i32, ptr %11, align 4, !tbaa !5
  call void @combine(ptr dead_on_unwind writable sret(%struct.Node) align 4 %0, i32 noundef %63, i32 noundef %65, i32 noundef %66, i32 noundef %67, i32 noundef %68)
  store i32 1, ptr %13, align 4
  br label %86

69:                                               ; preds = %56
  %70 = load i32, ptr %7, align 4, !tbaa !5
  %71 = mul nsw i32 2, %70
  %72 = add nsw i32 %71, 1
  %73 = load i32, ptr %8, align 4, !tbaa !5
  %74 = load i32, ptr %12, align 4, !tbaa !5
  %75 = load i32, ptr %10, align 4, !tbaa !5
  %76 = load i32, ptr %12, align 4, !tbaa !5
  call void @combine(ptr dead_on_unwind writable sret(%struct.Node) align 4 %14, i32 noundef %72, i32 noundef %73, i32 noundef %74, i32 noundef %75, i32 noundef %76)
  %77 = load i32, ptr %7, align 4, !tbaa !5
  %78 = mul nsw i32 2, %77
  %79 = add nsw i32 %78, 2
  %80 = load i32, ptr %12, align 4, !tbaa !5
  %81 = add nsw i32 %80, 1
  %82 = load i32, ptr %9, align 4, !tbaa !5
  %83 = load i32, ptr %12, align 4, !tbaa !5
  %84 = add nsw i32 %83, 1
  %85 = load i32, ptr %11, align 4, !tbaa !5
  call void @combine(ptr dead_on_unwind writable sret(%struct.Node) align 4 %15, i32 noundef %79, i32 noundef %81, i32 noundef %82, i32 noundef %84, i32 noundef %85)
  call void @merge(ptr dead_on_unwind writable sret(%struct.Node) align 4 %0, ptr noundef byval(%struct.Node) align 8 %14, ptr noundef byval(%struct.Node) align 8 %15)
  store i32 1, ptr %13, align 4
  br label %86

86:                                               ; preds = %69, %60, %48
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  br label %87

87:                                               ; preds = %86, %23
  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 local_unnamed_addr global [400400 x %struct.Node], align 16
@sw = external dso_local local_unnamed_addr global [400400 x i32], align 16
@flag = 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 nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

; Function Attrs: nounwind uwtable
declare dso_local void @lazy(i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @combine(ptr dead_on_unwind noalias writable sret(%struct.Node) align 4 %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4, i32 noundef %5) local_unnamed_addr #0 {
  %7 = alloca %struct.Node, align 8
  %8 = alloca %struct.Node, align 8
  %9 = icmp eq i32 %2, %4
  %10 = icmp eq i32 %3, %5
  %or.cond4955 = and i1 %9, %10
  br i1 %or.cond4955, label %tailrecurse.outer._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %6, %tailrecurse.outer
  %11 = phi i1 [ %38, %tailrecurse.outer ], [ %9, %6 ]
  %.tr43.ph58 = phi i32 [ %.us-phi54, %tailrecurse.outer ], [ %3, %6 ]
  %.tr42.ph57 = phi i32 [ %37, %tailrecurse.outer ], [ %2, %6 ]
  %.tr41.ph56 = phi i32 [ %36, %tailrecurse.outer ], [ %1, %6 ]
  br i1 %11, label %.lr.ph.split, label %.lr.ph.split.us

.lr.ph.split.us:                                  ; preds = %.lr.ph, %tailrecurse.us
  %.tr4351.us = phi i32 [ %18, %tailrecurse.us ], [ %.tr43.ph58, %.lr.ph ]
  %.tr4150.us = phi i32 [ %20, %tailrecurse.us ], [ %.tr41.ph56, %.lr.ph ]
  %12 = sext i32 %.tr4150.us to i64
  %13 = getelementptr inbounds [400400 x i32], ptr @sw, i64 0, i64 %12
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %.not.us = icmp eq i32 %14, 0
  br i1 %.not.us, label %16, label %15

15:                                               ; preds = %.lr.ph.split.us
  tail call void @lazy(i32 noundef %.tr4150.us, i32 noundef %.tr42.ph57, i32 noundef %.tr4351.us)
  br label %16

16:                                               ; preds = %15, %.lr.ph.split.us
  %17 = add i32 %.tr4351.us, %.tr42.ph57
  %18 = sdiv i32 %17, 2
  %.not40.us = icmp slt i32 %18, %5
  br i1 %.not40.us, label %.split.us, label %tailrecurse.us

tailrecurse.us:                                   ; preds = %16
  %19 = shl nsw i32 %.tr4150.us, 1
  %20 = or disjoint i32 %19, 1
  br label %.lr.ph.split.us

tailrecurse.outer._crit_edge:                     ; preds = %tailrecurse.outer, %tailrecurse, %6
  %.tr41.lcssa = phi i32 [ %1, %6 ], [ %32, %tailrecurse ], [ %36, %tailrecurse.outer ]
  %21 = sext i32 %.tr41.lcssa to i64
  %22 = getelementptr inbounds [400400 x i32], ptr @flag, i64 0, i64 %21
  store i32 1, ptr %22, align 4, !tbaa !5
  %23 = getelementptr inbounds [400400 x %struct.Node], ptr @t, i64 0, i64 %21
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(112) %0, ptr noundef nonnull align 16 dereferenceable(112) %23, i64 112, i1 false), !tbaa.struct !9
  br label %44

.lr.ph.split:                                     ; preds = %.lr.ph, %tailrecurse
  %.tr4351 = phi i32 [ %30, %tailrecurse ], [ %.tr43.ph58, %.lr.ph ]
  %.tr4150 = phi i32 [ %32, %tailrecurse ], [ %.tr41.ph56, %.lr.ph ]
  %24 = sext i32 %.tr4150 to i64
  %25 = getelementptr inbounds [400400 x i32], ptr @sw, i64 0, i64 %24
  %26 = load i32, ptr %25, align 4, !tbaa !5
  %.not = icmp eq i32 %26, 0
  br i1 %.not, label %28, label %27

27:                                               ; preds = %.lr.ph.split
  tail call void @lazy(i32 noundef %.tr4150, i32 noundef %.tr42.ph57, i32 noundef %.tr4351)
  br label %28

28:                                               ; preds = %27, %.lr.ph.split
  %29 = add i32 %.tr4351, %.tr42.ph57
  %30 = sdiv i32 %29, 2
  %.not40 = icmp slt i32 %30, %5
  br i1 %.not40, label %.split.us, label %tailrecurse

tailrecurse:                                      ; preds = %28
  %31 = shl nsw i32 %.tr4150, 1
  %32 = or disjoint i32 %31, 1
  %33 = icmp eq i32 %30, %5
  br i1 %33, label %tailrecurse.outer._crit_edge, label %.lr.ph.split

.split.us:                                        ; preds = %16, %28
  %.us-phi = phi i32 [ %30, %28 ], [ %18, %16 ]
  %.us-phi53 = phi i32 [ %.tr4150, %28 ], [ %.tr4150.us, %16 ]
  %.us-phi54 = phi i32 [ %.tr4351, %28 ], [ %.tr4351.us, %16 ]
  %34 = icmp slt i32 %.us-phi, %4
  %35 = shl nsw i32 %.us-phi53, 1
  br i1 %34, label %tailrecurse.outer, label %40

tailrecurse.outer:                                ; preds = %.split.us
  %36 = add nsw i32 %35, 2
  %37 = add nsw i32 %.us-phi, 1
  %38 = icmp eq i32 %37, %4
  %39 = icmp eq i32 %.us-phi54, %5
  %or.cond49 = and i1 %38, %39
  br i1 %or.cond49, label %tailrecurse.outer._crit_edge, label %.lr.ph

40:                                               ; preds = %.split.us
  %41 = or disjoint i32 %35, 1
  call void @combine(ptr dead_on_unwind nonnull writable sret(%struct.Node) align 4 %7, i32 noundef %41, i32 noundef %.tr42.ph57, i32 noundef %.us-phi, i32 noundef %4, i32 noundef %.us-phi)
  %42 = add nsw i32 %35, 2
  %43 = add nsw i32 %.us-phi, 1
  call void @combine(ptr dead_on_unwind nonnull writable sret(%struct.Node) align 4 %8, i32 noundef %42, i32 noundef %43, i32 noundef %.us-phi54, i32 noundef %43, i32 noundef %5)
  call void @merge(ptr dead_on_unwind writable sret(%struct.Node) align 4 %0, ptr noundef nonnull byval(%struct.Node) align 8 %7, ptr noundef nonnull byval(%struct.Node) align 8 %8)
  br label %44

44:                                               ; preds = %40, %tailrecurse.outer._crit_edge
  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 nounwind willreturn memory(argmem: readwrite) }

!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}


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