; LLVM IR from tangjz/acm-icpc :: codeforces/complete (315B)

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

%class.XDTree = type { i32, i32, [524288 x i64] }

@xds = external dso_local global %class.XDTree, align 8
@q = external dso_local global i32, align 4
@num = external dso_local global [100001 x i64], align 16
@.str = external hidden unnamed_addr constant [5 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [6 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [8 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i64, align 8
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  %5 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @xds, ptr noundef @q)
  store i32 1, ptr getelementptr inbounds (%class.XDTree, ptr @xds, i32 0, i32 1), align 4, !tbaa !5
  br label %6

6:                                                ; preds = %12, %0
  %7 = load i32, ptr getelementptr inbounds (%class.XDTree, ptr @xds, i32 0, i32 1), align 4, !tbaa !5
  %8 = load i32, ptr @xds, align 8, !tbaa !10
  %9 = add nsw i32 %8, 1
  %10 = icmp sle i32 %7, %9
  br i1 %10, label %11, label %15

11:                                               ; preds = %6
  br label %12

12:                                               ; preds = %11
  %13 = load i32, ptr getelementptr inbounds (%class.XDTree, ptr @xds, i32 0, i32 1), align 4, !tbaa !5
  %14 = shl i32 %13, 1
  store i32 %14, ptr getelementptr inbounds (%class.XDTree, ptr @xds, i32 0, i32 1), align 4, !tbaa !5
  br label %6, !llvm.loop !11

15:                                               ; preds = %6
  store i32 1, ptr %2, align 4, !tbaa !13
  br label %16

16:                                               ; preds = %40, %15
  %17 = load i32, ptr %2, align 4, !tbaa !13
  %18 = load i32, ptr @xds, align 8, !tbaa !10
  %19 = icmp sle i32 %17, %18
  br i1 %19, label %20, label %43

20:                                               ; preds = %16
  %21 = load i32, ptr %2, align 4, !tbaa !13
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [100001 x i64], ptr @num, i64 0, i64 %22
  %24 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, ptr noundef %23)
  %25 = load i32, ptr %2, align 4, !tbaa !13
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [100001 x i64], ptr @num, i64 0, i64 %26
  %28 = load i64, ptr %27, align 8, !tbaa !14
  %29 = load i32, ptr %2, align 4, !tbaa !13
  %30 = sub nsw i32 %29, 1
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [100001 x i64], ptr @num, i64 0, i64 %31
  %33 = load i64, ptr %32, align 8, !tbaa !14
  %34 = sub nsw i64 %28, %33
  %35 = load i32, ptr getelementptr inbounds (%class.XDTree, ptr @xds, i32 0, i32 1), align 4, !tbaa !5
  %36 = load i32, ptr %2, align 4, !tbaa !13
  %37 = add nsw i32 %35, %36
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [524288 x i64], ptr getelementptr inbounds (%class.XDTree, ptr @xds, i32 0, i32 2), i64 0, i64 %38
  store i64 %34, ptr %39, align 8, !tbaa !14
  br label %40

40:                                               ; preds = %20
  %41 = load i32, ptr %2, align 4, !tbaa !13
  %42 = add nsw i32 %41, 1
  store i32 %42, ptr %2, align 4, !tbaa !13
  br label %16, !llvm.loop !16

43:                                               ; preds = %16
  call void @_ZN6XDTree4initEv(ptr noundef nonnull align 8 dereferenceable(4194312) @xds)
  store i32 0, ptr %2, align 4, !tbaa !13
  br label %44

44:                                               ; preds = %84, %43
  %45 = load i32, ptr %2, align 4, !tbaa !13
  %46 = load i32, ptr @q, align 4, !tbaa !13
  %47 = icmp slt i32 %45, %46
  br i1 %47, label %48, label %87

48:                                               ; preds = %44
  %49 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, ptr noundef %4)
  %50 = load i64, ptr %4, align 8, !tbaa !14
  %51 = icmp eq i64 %50, 1
  br i1 %51, label %52, label %70

52:                                               ; preds = %48
  %53 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.2, ptr noundef %3, ptr noundef %4)
  %54 = load i32, ptr %3, align 4, !tbaa !13
  %55 = call noundef i32 @_ZN6XDTree5queryEii(ptr noundef nonnull align 8 dereferenceable(4194312) @xds, i32 noundef 1, i32 noundef %54)
  %56 = sext i32 %55 to i64
  %57 = load i64, ptr %4, align 8, !tbaa !14
  %58 = sub nsw i64 %57, %56
  store i64 %58, ptr %4, align 8, !tbaa !14
  %59 = load i32, ptr %3, align 4, !tbaa !13
  %60 = load i64, ptr %4, align 8, !tbaa !14
  call void @_ZN6XDTree6changeEix(ptr noundef nonnull align 8 dereferenceable(4194312) @xds, i32 noundef %59, i64 noundef %60)
  %61 = load i32, ptr %3, align 4, !tbaa !13
  %62 = load i32, ptr @xds, align 8, !tbaa !10
  %63 = icmp slt i32 %61, %62
  br i1 %63, label %64, label %69

64:                                               ; preds = %52
  %65 = load i32, ptr %3, align 4, !tbaa !13
  %66 = add nsw i32 %65, 1
  %67 = load i64, ptr %4, align 8, !tbaa !14
  %68 = sub nsw i64 0, %67
  call void @_ZN6XDTree6changeEix(ptr noundef nonnull align 8 dereferenceable(4194312) @xds, i32 noundef %66, i64 noundef %68)
  br label %69

69:                                               ; preds = %64, %52
  br label %83

70:                                               ; preds = %48
  %71 = load i64, ptr %4, align 8, !tbaa !14
  %72 = icmp eq i64 %71, 2
  br i1 %72, label %73, label %76

73:                                               ; preds = %70
  %74 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, ptr noundef %4)
  %75 = load i64, ptr %4, align 8, !tbaa !14
  call void @_ZN6XDTree6changeEix(ptr noundef nonnull align 8 dereferenceable(4194312) @xds, i32 noundef 1, i64 noundef %75)
  br label %82

76:                                               ; preds = %70
  %77 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, ptr noundef %4)
  %78 = load i64, ptr %4, align 8, !tbaa !14
  %79 = trunc i64 %78 to i32
  %80 = call noundef i32 @_ZN6XDTree5queryEii(ptr noundef nonnull align 8 dereferenceable(4194312) @xds, i32 noundef 1, i32 noundef %79)
  %81 = call i32 (ptr, ...) @printf(ptr noundef @.str.3, i32 noundef %80)
  br label %82

82:                                               ; preds = %76, %73
  br label %83

83:                                               ; preds = %82, %69
  br label %84

84:                                               ; preds = %83
  %85 = load i32, ptr %2, align 4, !tbaa !13
  %86 = add nsw i32 %85, 1
  store i32 %86, ptr %2, align 4, !tbaa !13
  br label %44, !llvm.loop !17

87:                                               ; preds = %44
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #4
  ret i32 0
}

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

declare i32 @__isoc99_scanf(ptr noundef, ...) #2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN6XDTree4initEv(ptr noundef nonnull align 8 dereferenceable(4194312)) #3 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZN6XDTree5queryEii(ptr noundef nonnull align 8 dereferenceable(4194312), i32 noundef, i32 noundef) #3 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN6XDTree6changeEix(ptr noundef nonnull align 8 dereferenceable(4194312), i32 noundef, i64 noundef) #3 align 2

declare i32 @printf(ptr noundef, ...) #2

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

attributes #0 = { mustprogress norecurse 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 = { "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 #3 = { 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 #4 = { 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, !7, i64 4}
!6 = !{!"_ZTS6XDTree", !7, i64 0, !7, i64 4, !8, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!6, !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!7, !7, i64 0}
!14 = !{!15, !15, i64 0}
!15 = !{!"long long", !8, i64 0}
!16 = distinct !{!16, !12}
!17 = distinct !{!17, !12}


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

%class.XDTree = type { i32, i32, [524288 x i64] }

@xds = external dso_local global %class.XDTree, align 8
@q = external dso_local global i32, align 4
@num = external dso_local global [100001 x i64], align 16
@.str = external hidden unnamed_addr constant [5 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [6 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [8 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress norecurse nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = alloca i32, align 4
  %2 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %1) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2) #4
  %3 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @xds, ptr noundef nonnull @q)
  %4 = load i32, ptr @xds, align 8, !tbaa !5
  %5 = add nsw i32 %4, 1
  br label %6

6:                                                ; preds = %6, %0
  %storemerge = phi i32 [ 1, %0 ], [ %7, %6 ]
  %.not = icmp sgt i32 %storemerge, %5
  %7 = shl i32 %storemerge, 1
  br i1 %.not, label %.preheader, label %6, !llvm.loop !10

.preheader:                                       ; preds = %6
  store i32 %storemerge, ptr getelementptr inbounds (i8, ptr @xds, i64 4), align 4, !tbaa !12
  %.not1112 = icmp slt i32 %4, 1
  br i1 %.not1112, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 1, %.preheader ]
  %8 = getelementptr inbounds [100001 x i64], ptr @num, i64 0, i64 %indvars.iv
  %9 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull %8)
  %10 = load i64, ptr %8, align 8, !tbaa !13
  %11 = add nsw i64 %indvars.iv, -1
  %12 = getelementptr inbounds [100001 x i64], ptr @num, i64 0, i64 %11
  %13 = load i64, ptr %12, align 8, !tbaa !13
  %14 = sub nsw i64 %10, %13
  %15 = load i32, ptr getelementptr inbounds (i8, ptr @xds, i64 4), align 4, !tbaa !12
  %16 = sext i32 %15 to i64
  %17 = add nsw i64 %indvars.iv, %16
  %18 = getelementptr inbounds [524288 x i64], ptr getelementptr inbounds (i8, ptr @xds, i64 8), i64 0, i64 %17
  store i64 %14, ptr %18, align 8, !tbaa !13
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %19 = load i32, ptr @xds, align 8, !tbaa !5
  %20 = sext i32 %19 to i64
  %.not11.not = icmp slt i64 %indvars.iv, %20
  br i1 %.not11.not, label %.lr.ph, label %._crit_edge, !llvm.loop !15

._crit_edge:                                      ; preds = %.lr.ph, %.preheader
  tail call void @_ZN6XDTree4initEv(ptr noundef nonnull align 8 dereferenceable(4194312) @xds)
  %21 = load i32, ptr @q, align 4, !tbaa !16
  %22 = icmp sgt i32 %21, 0
  br i1 %22, label %.lr.ph16, label %._crit_edge17

.lr.ph16:                                         ; preds = %._crit_edge, %49
  %.114 = phi i32 [ %50, %49 ], [ 0, %._crit_edge ]
  %23 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull %2)
  %24 = load i64, ptr %2, align 8, !tbaa !13
  switch i64 %24, label %43 [
    i64 1, label %25
    i64 2, label %40
  ]

25:                                               ; preds = %.lr.ph16
  %26 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.2, ptr noundef nonnull %1, ptr noundef nonnull %2)
  %27 = load i32, ptr %1, align 4, !tbaa !16
  %28 = call noundef i32 @_ZN6XDTree5queryEii(ptr noundef nonnull align 8 dereferenceable(4194312) @xds, i32 noundef 1, i32 noundef %27)
  %29 = sext i32 %28 to i64
  %30 = load i64, ptr %2, align 8, !tbaa !13
  %31 = sub nsw i64 %30, %29
  store i64 %31, ptr %2, align 8, !tbaa !13
  %32 = load i32, ptr %1, align 4, !tbaa !16
  call void @_ZN6XDTree6changeEix(ptr noundef nonnull align 8 dereferenceable(4194312) @xds, i32 noundef %32, i64 noundef %31)
  %33 = load i32, ptr %1, align 4, !tbaa !16
  %34 = load i32, ptr @xds, align 8, !tbaa !5
  %35 = icmp slt i32 %33, %34
  br i1 %35, label %36, label %49

36:                                               ; preds = %25
  %37 = add nsw i32 %33, 1
  %38 = load i64, ptr %2, align 8, !tbaa !13
  %39 = sub nsw i64 0, %38
  call void @_ZN6XDTree6changeEix(ptr noundef nonnull align 8 dereferenceable(4194312) @xds, i32 noundef %37, i64 noundef %39)
  br label %49

40:                                               ; preds = %.lr.ph16
  %41 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull %2)
  %42 = load i64, ptr %2, align 8, !tbaa !13
  call void @_ZN6XDTree6changeEix(ptr noundef nonnull align 8 dereferenceable(4194312) @xds, i32 noundef 1, i64 noundef %42)
  br label %49

43:                                               ; preds = %.lr.ph16
  %44 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull %2)
  %45 = load i64, ptr %2, align 8, !tbaa !13
  %46 = trunc i64 %45 to i32
  %47 = call noundef i32 @_ZN6XDTree5queryEii(ptr noundef nonnull align 8 dereferenceable(4194312) @xds, i32 noundef 1, i32 noundef %46)
  %48 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3, i32 noundef %47)
  br label %49

49:                                               ; preds = %36, %25, %43, %40
  %50 = add nuw nsw i32 %.114, 1
  %51 = load i32, ptr @q, align 4, !tbaa !16
  %52 = icmp slt i32 %50, %51
  br i1 %52, label %.lr.ph16, label %._crit_edge17, !llvm.loop !17

._crit_edge17:                                    ; preds = %49, %._crit_edge
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %1) #4
  ret i32 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: nofree nounwind
declare noundef i32 @__isoc99_scanf(ptr nocapture noundef readonly, ...) local_unnamed_addr #2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN6XDTree4initEv(ptr noundef nonnull align 8 dereferenceable(4194312)) local_unnamed_addr #3 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZN6XDTree5queryEii(ptr noundef nonnull align 8 dereferenceable(4194312), i32 noundef, i32 noundef) local_unnamed_addr #3 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN6XDTree6changeEix(ptr noundef nonnull align 8 dereferenceable(4194312), i32 noundef, i64 noundef) local_unnamed_addr #3 align 2

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #2

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

attributes #0 = { mustprogress norecurse 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 = { nofree nounwind "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 #3 = { 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 #4 = { 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, !7, i64 0}
!6 = !{!"_ZTS6XDTree", !7, i64 0, !7, i64 4, !8, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = !{!6, !7, i64 4}
!13 = !{!14, !14, i64 0}
!14 = !{!"long long", !8, i64 0}
!15 = distinct !{!15, !11}
!16 = !{!7, !7, i64 0}
!17 = distinct !{!17, !11}


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