; LLVM IR from bqi343/cp-notebook :: Contests/USACO Solutions/2016-17/Feb 2017/Plat (17 Feb P3 (Segtree))

; 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.std::pair" = type { i32, i32 }
%"class.std::basic_ostream" = type { ptr, %"class.std::basic_ios" }
%"class.std::basic_ios" = type { %"class.std::ios_base", ptr, i8, i8, ptr, ptr, ptr, ptr }
%"class.std::ios_base" = type { ptr, i64, i64, i32, i32, i32, ptr, %"struct.std::ios_base::_Words", [8 x %"struct.std::ios_base::_Words"], i32, ptr, %"class.std::locale" }
%"struct.std::ios_base::_Words" = type { ptr, i64 }
%"class.std::locale" = type { ptr }

@N = external dso_local global i32, align 4
@K = external dso_local global i32, align 4
@x = external dso_local global [100001 x %"struct.std::pair"], align 16
@ans = external dso_local global i64, align 8
@_ZSt4cout = external global %"class.std::basic_ostream", align 8

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

; 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 @_Z4updbii(i32 noundef, i32 noundef) #1

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z6querybiii(i32 noundef, i32 noundef, i32 noundef) #1

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z4initv() #1

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() #2 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  call void @_Z4initv()
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  %5 = load i32, ptr @N, align 4, !tbaa !5
  store i32 %5, ptr %2, align 4, !tbaa !5
  br label %6

6:                                                ; preds = %55, %0
  %7 = load i32, ptr %2, align 4, !tbaa !5
  %8 = icmp sgt i32 %7, 0
  br i1 %8, label %10, label %9

9:                                                ; preds = %6
  store i32 2, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  br label %58

10:                                               ; preds = %6
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  %11 = load i32, ptr %2, align 4, !tbaa !5
  %12 = load i32, ptr @K, align 4, !tbaa !5
  %13 = add nsw i32 %11, %12
  %14 = add nsw i32 %13, 1
  store i32 %14, ptr %4, align 4, !tbaa !5
  %15 = load i32, ptr %4, align 4, !tbaa !5
  %16 = load i32, ptr @N, align 4, !tbaa !5
  %17 = icmp sgt i32 %15, %16
  br i1 %17, label %18, label %19

18:                                               ; preds = %10
  store i32 4, ptr %3, align 4
  br label %52

19:                                               ; preds = %10
  %20 = load i32, ptr %4, align 4, !tbaa !5
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [100001 x %"struct.std::pair"], ptr @x, i64 0, i64 %21
  %23 = getelementptr inbounds %"struct.std::pair", ptr %22, i32 0, i32 0
  %24 = load i32, ptr %23, align 8, !tbaa !9
  %25 = load i32, ptr %4, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [100001 x %"struct.std::pair"], ptr @x, i64 0, i64 %26
  %28 = getelementptr inbounds %"struct.std::pair", ptr %27, i32 0, i32 1
  %29 = load i32, ptr %28, align 4, !tbaa !11
  call void @_Z4updbii(i32 noundef %24, i32 noundef %29)
  %30 = load i32, ptr @N, align 4, !tbaa !5
  %31 = load i32, ptr %2, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [100001 x %"struct.std::pair"], ptr @x, i64 0, i64 %32
  %34 = getelementptr inbounds %"struct.std::pair", ptr %33, i32 0, i32 1
  %35 = load i32, ptr %34, align 4, !tbaa !11
  call void @_Z6querybiii(i32 noundef %30, i32 noundef %35, i32 noundef 1)
  %36 = load i32, ptr %2, align 4, !tbaa !5
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [100001 x %"struct.std::pair"], ptr @x, i64 0, i64 %37
  %39 = getelementptr inbounds %"struct.std::pair", ptr %38, i32 0, i32 0
  %40 = load i32, ptr %39, align 8, !tbaa !9
  %41 = load i32, ptr @N, align 4, !tbaa !5
  call void @_Z6querybiii(i32 noundef %40, i32 noundef %41, i32 noundef 1)
  %42 = load i32, ptr %2, align 4, !tbaa !5
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds [100001 x %"struct.std::pair"], ptr @x, i64 0, i64 %43
  %45 = getelementptr inbounds %"struct.std::pair", ptr %44, i32 0, i32 0
  %46 = load i32, ptr %45, align 8, !tbaa !9
  %47 = load i32, ptr %2, align 4, !tbaa !5
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [100001 x %"struct.std::pair"], ptr @x, i64 0, i64 %48
  %50 = getelementptr inbounds %"struct.std::pair", ptr %49, i32 0, i32 1
  %51 = load i32, ptr %50, align 4, !tbaa !11
  call void @_Z6querybiii(i32 noundef %46, i32 noundef %51, i32 noundef -2)
  store i32 0, ptr %3, align 4
  br label %52

52:                                               ; preds = %19, %18
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  %53 = load i32, ptr %3, align 4
  switch i32 %53, label %62 [
    i32 0, label %54
    i32 4, label %55
  ]

54:                                               ; preds = %52
  br label %55

55:                                               ; preds = %54, %52
  %56 = load i32, ptr %2, align 4, !tbaa !5
  %57 = add nsw i32 %56, -1
  store i32 %57, ptr %2, align 4, !tbaa !5
  br label %6, !llvm.loop !12

58:                                               ; preds = %9
  %59 = load i64, ptr @ans, align 8, !tbaa !14
  %60 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEx(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, i64 noundef %59)
  %61 = load i32, ptr %1, align 4
  ret i32 %61

62:                                               ; preds = %52
  unreachable
}

; Function Attrs: mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEx(ptr noundef nonnull align 8 dereferenceable(8), i64 noundef) #1 align 2

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 = { 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 #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 = !{!10, !6, i64 0}
!10 = !{!"_ZTSSt4pairIiiE", !6, i64 0, !6, i64 4}
!11 = !{!10, !6, i64 4}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!15, !15, i64 0}
!15 = !{!"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.std::pair" = type { i32, i32 }
%"class.std::basic_ostream" = type { ptr, %"class.std::basic_ios" }
%"class.std::basic_ios" = type { %"class.std::ios_base", ptr, i8, i8, ptr, ptr, ptr, ptr }
%"class.std::ios_base" = type { ptr, i64, i64, i32, i32, i32, ptr, %"struct.std::ios_base::_Words", [8 x %"struct.std::ios_base::_Words"], i32, ptr, %"class.std::locale" }
%"struct.std::ios_base::_Words" = type { ptr, i64 }
%"class.std::locale" = type { ptr }

@N = external dso_local local_unnamed_addr global i32, align 4
@K = external dso_local local_unnamed_addr global i32, align 4
@x = external dso_local local_unnamed_addr global [100001 x %"struct.std::pair"], align 16
@ans = external dso_local local_unnamed_addr global i64, align 8
@_ZSt4cout = external global %"class.std::basic_ostream", align 8

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

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

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z4initv() local_unnamed_addr #0

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() local_unnamed_addr #1 {
  tail call void @_Z4initv()
  %1 = load i32, ptr @N, align 4, !tbaa !5
  %2 = icmp sgt i32 %1, 0
  br i1 %2, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %0
  %3 = zext nneg i32 %1 to i64
  %.pre14 = load i32, ptr @K, align 4, !tbaa !5
  br label %.lr.ph

._crit_edge:                                      ; preds = %25, %0
  %4 = load i64, ptr @ans, align 8, !tbaa !9
  %5 = tail call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEx(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, i64 noundef %4)
  ret i32 0

.lr.ph:                                           ; preds = %.lr.ph.preheader, %25
  %6 = phi i32 [ %1, %.lr.ph.preheader ], [ %26, %25 ]
  %7 = phi i32 [ %.pre14, %.lr.ph.preheader ], [ %27, %25 ]
  %indvars.iv = phi i64 [ %3, %.lr.ph.preheader ], [ %indvars.iv.next, %25 ]
  %8 = trunc nuw nsw i64 %indvars.iv to i32
  %9 = add nsw i32 %7, %8
  %.not = icmp slt i32 %9, %6
  br i1 %.not, label %10, label %25

10:                                               ; preds = %.lr.ph
  %11 = add nsw i32 %9, 1
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [100001 x %"struct.std::pair"], ptr @x, i64 0, i64 %12
  %14 = load i32, ptr %13, align 8, !tbaa !11
  %15 = getelementptr inbounds i8, ptr %13, i64 4
  %16 = load i32, ptr %15, align 4, !tbaa !13
  tail call void @_Z4updbii(i32 noundef %14, i32 noundef %16)
  %17 = load i32, ptr @N, align 4, !tbaa !5
  %18 = getelementptr inbounds [100001 x %"struct.std::pair"], ptr @x, i64 0, i64 %indvars.iv
  %19 = getelementptr inbounds i8, ptr %18, i64 4
  %20 = load i32, ptr %19, align 4, !tbaa !13
  tail call void @_Z6querybiii(i32 noundef %17, i32 noundef %20, i32 noundef 1)
  %21 = load i32, ptr %18, align 8, !tbaa !11
  %22 = load i32, ptr @N, align 4, !tbaa !5
  tail call void @_Z6querybiii(i32 noundef %21, i32 noundef %22, i32 noundef 1)
  %23 = load i32, ptr %18, align 8, !tbaa !11
  %24 = load i32, ptr %19, align 4, !tbaa !13
  tail call void @_Z6querybiii(i32 noundef %23, i32 noundef %24, i32 noundef -2)
  %.pre = load i32, ptr @K, align 4, !tbaa !5
  %.pre15 = load i32, ptr @N, align 4, !tbaa !5
  br label %25

25:                                               ; preds = %.lr.ph, %10
  %26 = phi i32 [ %6, %.lr.ph ], [ %.pre15, %10 ]
  %27 = phi i32 [ %7, %.lr.ph ], [ %.pre, %10 ]
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %28 = icmp sgt i64 %indvars.iv, 1
  br i1 %28, label %.lr.ph, label %._crit_edge, !llvm.loop !14
}

; Function Attrs: mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEx(ptr noundef nonnull align 8 dereferenceable(8), i64 noundef) local_unnamed_addr #0 align 2

attributes #0 = { 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 #1 = { 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" }

!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 = !{!10, !10, i64 0}
!10 = !{!"long long", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"_ZTSSt4pairIiiE", !6, i64 0, !6, i64 4}
!13 = !{!12, !6, i64 4}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass