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

; 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.0" = type { i32, %"struct.std::pair" }
%"struct.std::pair" = type { i32, i32 }

@segbit = external dso_local global [20000000 x %"struct.std::pair.0"], align 16

; 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
define dso_local noundef i32 @_Z5queryiiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) #1 {
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %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
  store i32 %0, ptr %6, align 4, !tbaa !5
  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
  %12 = load i32, ptr %8, align 4, !tbaa !5
  %13 = load i32, ptr %9, align 4, !tbaa !5
  %14 = icmp sle i32 %12, %13
  br i1 %14, label %15, label %21

15:                                               ; preds = %4
  %16 = load i32, ptr %6, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [20000000 x %"struct.std::pair.0"], ptr @segbit, i64 0, i64 %17
  %19 = getelementptr inbounds %"struct.std::pair.0", ptr %18, i32 0, i32 0
  %20 = load i32, ptr %19, align 4, !tbaa !9
  store i32 %20, ptr %5, align 4
  br label %75

21:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  %22 = load i32, ptr %7, align 4, !tbaa !5
  %23 = load i32, ptr %8, align 4, !tbaa !5
  %24 = add nsw i32 %22, %23
  %25 = sdiv i32 %24, 2
  store i32 %25, ptr %10, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  store i32 0, ptr %11, align 4, !tbaa !5
  %26 = load i32, ptr %6, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [20000000 x %"struct.std::pair.0"], ptr @segbit, i64 0, i64 %27
  %29 = getelementptr inbounds %"struct.std::pair.0", ptr %28, i32 0, i32 1
  %30 = getelementptr inbounds %"struct.std::pair", ptr %29, i32 0, i32 0
  %31 = load i32, ptr %30, align 4, !tbaa !12
  %32 = icmp ne i32 %31, 0
  br i1 %32, label %33, label %46

33:                                               ; preds = %21
  %34 = load i32, ptr %6, align 4, !tbaa !5
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [20000000 x %"struct.std::pair.0"], ptr @segbit, i64 0, i64 %35
  %37 = getelementptr inbounds %"struct.std::pair.0", ptr %36, i32 0, i32 1
  %38 = getelementptr inbounds %"struct.std::pair", ptr %37, i32 0, i32 0
  %39 = load i32, ptr %38, align 4, !tbaa !12
  %40 = load i32, ptr %7, align 4, !tbaa !5
  %41 = load i32, ptr %10, align 4, !tbaa !5
  %42 = load i32, ptr %9, align 4, !tbaa !5
  %43 = call noundef i32 @_Z5queryiiii(i32 noundef %39, i32 noundef %40, i32 noundef %41, i32 noundef %42)
  %44 = load i32, ptr %11, align 4, !tbaa !5
  %45 = add nsw i32 %44, %43
  store i32 %45, ptr %11, align 4, !tbaa !5
  br label %46

46:                                               ; preds = %33, %21
  %47 = load i32, ptr %6, align 4, !tbaa !5
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [20000000 x %"struct.std::pair.0"], ptr @segbit, i64 0, i64 %48
  %50 = getelementptr inbounds %"struct.std::pair.0", ptr %49, i32 0, i32 1
  %51 = getelementptr inbounds %"struct.std::pair", ptr %50, i32 0, i32 1
  %52 = load i32, ptr %51, align 4, !tbaa !13
  %53 = icmp ne i32 %52, 0
  br i1 %53, label %54, label %73

54:                                               ; preds = %46
  %55 = load i32, ptr %10, align 4, !tbaa !5
  %56 = add nsw i32 %55, 1
  %57 = load i32, ptr %9, align 4, !tbaa !5
  %58 = icmp sle i32 %56, %57
  br i1 %58, label %59, label %73

59:                                               ; preds = %54
  %60 = load i32, ptr %6, align 4, !tbaa !5
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds [20000000 x %"struct.std::pair.0"], ptr @segbit, i64 0, i64 %61
  %63 = getelementptr inbounds %"struct.std::pair.0", ptr %62, i32 0, i32 1
  %64 = getelementptr inbounds %"struct.std::pair", ptr %63, i32 0, i32 1
  %65 = load i32, ptr %64, align 4, !tbaa !13
  %66 = load i32, ptr %10, align 4, !tbaa !5
  %67 = add nsw i32 %66, 1
  %68 = load i32, ptr %8, align 4, !tbaa !5
  %69 = load i32, ptr %9, align 4, !tbaa !5
  %70 = call noundef i32 @_Z5queryiiii(i32 noundef %65, i32 noundef %67, i32 noundef %68, i32 noundef %69)
  %71 = load i32, ptr %11, align 4, !tbaa !5
  %72 = add nsw i32 %71, %70
  store i32 %72, ptr %11, align 4, !tbaa !5
  br label %73

73:                                               ; preds = %59, %54, %46
  %74 = load i32, ptr %11, align 4, !tbaa !5
  store i32 %74, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  br label %75

75:                                               ; preds = %73, %15
  %76 = load i32, ptr %5, align 4
  ret i32 %76
}

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 = { 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 = !{!"_ZTSSt4pairIiS_IiiEE", !6, i64 0, !11, i64 4}
!11 = !{!"_ZTSSt4pairIiiE", !6, i64 0, !6, i64 4}
!12 = !{!10, !6, i64 4}
!13 = !{!10, !6, i64 8}


; 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.0" = type { i32, %"struct.std::pair" }
%"struct.std::pair" = type { i32, i32 }

@segbit = external dso_local local_unnamed_addr global [20000000 x %"struct.std::pair.0"], align 16

; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_Z5queryiiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %.not = icmp sgt i32 %2, %3
  br i1 %.not, label %.lr.ph.split.us, label %17

.lr.ph.split.us:                                  ; preds = %4, %tailrecurse.us
  %.tr3038.us = phi i32 [ %15, %tailrecurse.us ], [ %1, %4 ]
  %.tr37.us = phi i32 [ %14, %tailrecurse.us ], [ %0, %4 ]
  %accumulator.tr36.us = phi i32 [ %16, %tailrecurse.us ], [ 0, %4 ]
  %5 = add nsw i32 %.tr3038.us, %2
  %6 = sdiv i32 %5, 2
  %7 = sext i32 %.tr37.us to i64
  %8 = getelementptr inbounds [20000000 x %"struct.std::pair.0"], ptr @segbit, i64 0, i64 %7, i32 1
  %9 = load i32, ptr %8, align 4, !tbaa !5
  %.not27.us = icmp eq i32 %9, 0
  br i1 %.not27.us, label %12, label %10

10:                                               ; preds = %.lr.ph.split.us
  %11 = tail call noundef i32 @_Z5queryiiii(i32 noundef %9, i32 noundef %.tr3038.us, i32 noundef %6, i32 noundef %3)
  br label %12

12:                                               ; preds = %10, %.lr.ph.split.us
  %.0.us = phi i32 [ %11, %10 ], [ 0, %.lr.ph.split.us ]
  %13 = getelementptr inbounds i8, ptr %8, i64 4
  %14 = load i32, ptr %13, align 4, !tbaa !11
  %.not28.us = icmp ne i32 %14, 0
  %.not29.not.us = icmp slt i32 %6, %3
  %or.cond.us = select i1 %.not28.us, i1 %.not29.not.us, i1 false
  br i1 %or.cond.us, label %tailrecurse.us, label %.loopexit

tailrecurse.us:                                   ; preds = %12
  %15 = add nsw i32 %6, 1
  %16 = add nsw i32 %.0.us, %accumulator.tr36.us
  br label %.lr.ph.split.us

17:                                               ; preds = %4
  %18 = sext i32 %0 to i64
  %19 = getelementptr inbounds [20000000 x %"struct.std::pair.0"], ptr @segbit, i64 0, i64 %18
  %20 = load i32, ptr %19, align 4, !tbaa !12
  br label %.loopexit

.loopexit:                                        ; preds = %12, %17
  %accumulator.tr35 = phi i32 [ 0, %17 ], [ %accumulator.tr36.us, %12 ]
  %.021 = phi i32 [ %20, %17 ], [ %.0.us, %12 ]
  %accumulator.ret.tr = add nsw i32 %.021, %accumulator.tr35
  ret i32 %accumulator.ret.tr
}

attributes #0 = { mustprogress nofree nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) 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, !7, i64 4}
!6 = !{!"_ZTSSt4pairIiS_IiiEE", !7, i64 0, !10, i64 4}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"_ZTSSt4pairIiiE", !7, i64 0, !7, i64 4}
!11 = !{!6, !7, i64 8}
!12 = !{!6, !7, i64 0}


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