; 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
@dflt = external dso_local global %struct.Node, align 4
@a = external dso_local global [100100 x i8], 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: nounwind uwtable
define dso_local void @build(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
  %8 = alloca %struct.Node, 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
  %9 = load i32, ptr %5, align 4, !tbaa !5
  %10 = load i32, ptr %6, align 4, !tbaa !5
  %11 = icmp eq i32 %9, %10
  br i1 %11, label %12, label %36

12:                                               ; preds = %3
  %13 = load i32, ptr %4, align 4, !tbaa !5
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [400400 x %struct.Node], ptr @t, i64 0, i64 %14
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %15, ptr align 4 @dflt, i64 112, i1 false), !tbaa.struct !9
  %16 = load i32, ptr %4, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [400400 x %struct.Node], ptr @t, i64 0, i64 %17
  %19 = getelementptr inbounds %struct.Node, ptr %18, i32 0, i32 0
  %20 = load i32, ptr %5, align 4, !tbaa !5
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [100100 x i8], ptr @a, i64 0, i64 %21
  %23 = load i8, ptr %22, align 1, !tbaa !10
  %24 = sext i8 %23 to i32
  %25 = sub nsw i32 %24, 97
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [28 x i32], ptr %19, i64 0, i64 %26
  %28 = load i32, ptr %27, align 4, !tbaa !5
  %29 = add nsw i32 %28, 1
  store i32 %29, ptr %27, align 4, !tbaa !5
  %30 = load i32, ptr %4, align 4, !tbaa !5
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [400400 x i32], ptr @sw, i64 0, i64 %31
  store i32 0, ptr %32, align 4, !tbaa !5
  %33 = load i32, ptr %4, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [400400 x i32], ptr @flag, i64 0, i64 %34
  store i32 0, ptr %35, align 4, !tbaa !5
  br label %71

36:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %37 = load i32, ptr %5, align 4, !tbaa !5
  %38 = load i32, ptr %6, align 4, !tbaa !5
  %39 = add nsw i32 %37, %38
  %40 = sdiv i32 %39, 2
  store i32 %40, ptr %7, align 4, !tbaa !5
  %41 = load i32, ptr %4, align 4, !tbaa !5
  %42 = mul nsw i32 2, %41
  %43 = add nsw i32 %42, 1
  %44 = load i32, ptr %5, align 4, !tbaa !5
  %45 = load i32, ptr %7, align 4, !tbaa !5
  call void @build(i32 noundef %43, i32 noundef %44, i32 noundef %45)
  %46 = load i32, ptr %4, align 4, !tbaa !5
  %47 = mul nsw i32 2, %46
  %48 = add nsw i32 %47, 2
  %49 = load i32, ptr %7, align 4, !tbaa !5
  %50 = add nsw i32 %49, 1
  %51 = load i32, ptr %6, align 4, !tbaa !5
  call void @build(i32 noundef %48, i32 noundef %50, i32 noundef %51)
  %52 = load i32, ptr %4, align 4, !tbaa !5
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds [400400 x %struct.Node], ptr @t, i64 0, i64 %53
  call void @llvm.lifetime.start.p0(i64 112, ptr %8) #3
  %55 = load i32, ptr %4, align 4, !tbaa !5
  %56 = mul nsw i32 2, %55
  %57 = add nsw i32 %56, 1
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds [400400 x %struct.Node], ptr @t, i64 0, i64 %58
  %60 = load i32, ptr %4, align 4, !tbaa !5
  %61 = mul nsw i32 2, %60
  %62 = add nsw i32 %61, 2
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds [400400 x %struct.Node], ptr @t, i64 0, i64 %63
  call void @merge(ptr dead_on_unwind writable sret(%struct.Node) align 4 %8, ptr noundef byval(%struct.Node) align 8 %59, ptr noundef byval(%struct.Node) align 8 %64)
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %54, ptr align 4 %8, i64 112, i1 false), !tbaa.struct !9
  call void @llvm.lifetime.end.p0(i64 112, ptr %8) #3
  %65 = load i32, ptr %4, align 4, !tbaa !5
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds [400400 x i32], ptr @sw, i64 0, i64 %66
  store i32 0, ptr %67, align 4, !tbaa !5
  %68 = load i32, ptr %4, align 4, !tbaa !5
  %69 = sext i32 %68 to i64
  %70 = getelementptr inbounds [400400 x i32], ptr @flag, i64 0, i64 %69
  store i32 0, ptr %70, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  br label %71

71:                                               ; preds = %36, %12
  ret void
}

; 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

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
@dflt = external dso_local local_unnamed_addr global %struct.Node, align 4
@a = external dso_local local_unnamed_addr global [100100 x i8], 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 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
define dso_local void @build(i32 noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = alloca %struct.Node, align 4
  %5 = icmp eq i32 %1, %2
  br i1 %5, label %6, label %19

6:                                                ; preds = %3
  %7 = sext i32 %0 to i64
  %8 = getelementptr inbounds [400400 x %struct.Node], ptr @t, i64 0, i64 %7
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(112) %8, ptr noundef nonnull align 4 dereferenceable(112) @dflt, i64 112, i1 false), !tbaa.struct !5
  %9 = sext i32 %1 to i64
  %10 = getelementptr inbounds [100100 x i8], ptr @a, i64 0, i64 %9
  %11 = load i8, ptr %10, align 1, !tbaa !6
  %12 = sext i8 %11 to i64
  %13 = add nsw i64 %12, -97
  %14 = getelementptr inbounds [28 x i32], ptr %8, i64 0, i64 %13
  %15 = load i32, ptr %14, align 4, !tbaa !9
  %16 = add nsw i32 %15, 1
  store i32 %16, ptr %14, align 4, !tbaa !9
  br label %common.ret

common.ret:                                       ; preds = %6, %19
  %.sink22 = phi i64 [ %7, %6 ], [ %26, %19 ]
  %17 = getelementptr inbounds [400400 x i32], ptr @sw, i64 0, i64 %.sink22
  store i32 0, ptr %17, align 4, !tbaa !9
  %18 = getelementptr inbounds [400400 x i32], ptr @flag, i64 0, i64 %.sink22
  store i32 0, ptr %18, align 4, !tbaa !9
  ret void

19:                                               ; preds = %3
  %20 = add nsw i32 %2, %1
  %21 = sdiv i32 %20, 2
  %22 = shl nsw i32 %0, 1
  %23 = or disjoint i32 %22, 1
  tail call void @build(i32 noundef %23, i32 noundef %1, i32 noundef %21)
  %24 = add nsw i32 %22, 2
  %25 = add nsw i32 %21, 1
  tail call void @build(i32 noundef %24, i32 noundef %25, i32 noundef %2)
  %26 = sext i32 %0 to i64
  %27 = getelementptr inbounds [400400 x %struct.Node], ptr @t, i64 0, i64 %26
  call void @llvm.lifetime.start.p0(i64 112, ptr nonnull %4) #3
  %28 = sext i32 %23 to i64
  %29 = getelementptr inbounds [400400 x %struct.Node], ptr @t, i64 0, i64 %28
  %30 = sext i32 %24 to i64
  %31 = getelementptr inbounds [400400 x %struct.Node], ptr @t, i64 0, i64 %30
  call void @merge(ptr dead_on_unwind nonnull writable sret(%struct.Node) align 4 %4, ptr noundef nonnull byval(%struct.Node) align 8 %29, ptr noundef nonnull byval(%struct.Node) align 8 %31)
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(112) %27, ptr noundef nonnull align 4 dereferenceable(112) %4, i64 112, i1 false), !tbaa.struct !5
  call void @llvm.lifetime.end.p0(i64 112, ptr nonnull %4) #3
  br label %common.ret
}

; 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

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: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass