; LLVM IR from bqi343/cp-notebook :: Contests/USACO Solutions/2015-16/Feb 2016/Plat/16 Feb P1.ll (Z5queryiiiiib)

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

@t = external dso_local global [2 x [262144 x i32]], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z5queryiiiiib(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4, i1 noundef zeroext %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 i8, align 1
  store i32 %0, ptr %8, align 4, !tbaa !5
  store i32 %1, ptr %9, align 4, !tbaa !5
  store i32 %2, ptr %10, align 4, !tbaa !5
  store i32 %3, ptr %11, align 4, !tbaa !5
  store i32 %4, ptr %12, align 4, !tbaa !5
  %14 = zext i1 %5 to i8
  store i8 %14, ptr %13, align 1, !tbaa !9
  %15 = load i32, ptr %9, align 4, !tbaa !5
  %16 = load i32, ptr %12, align 4, !tbaa !5
  %17 = icmp sgt i32 %15, %16
  br i1 %17, label %22, label %18

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

22:                                               ; preds = %18, %6
  store i32 0, ptr %7, align 4
  br label %68

23:                                               ; preds = %18
  %24 = load i32, ptr %11, align 4, !tbaa !5
  %25 = load i32, ptr %9, align 4, !tbaa !5
  %26 = icmp sle i32 %24, %25
  br i1 %26, label %27, label %40

27:                                               ; preds = %23
  %28 = load i32, ptr %10, align 4, !tbaa !5
  %29 = load i32, ptr %12, align 4, !tbaa !5
  %30 = icmp sle i32 %28, %29
  br i1 %30, label %31, label %40

31:                                               ; preds = %27
  %32 = load i8, ptr %13, align 1, !tbaa !9, !range !11, !noundef !12
  %33 = trunc i8 %32 to i1
  %34 = zext i1 %33 to i64
  %35 = getelementptr inbounds [2 x [262144 x i32]], ptr @t, i64 0, i64 %34
  %36 = load i32, ptr %8, align 4, !tbaa !5
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [262144 x i32], ptr %35, i64 0, i64 %37
  %39 = load i32, ptr %38, align 4, !tbaa !5
  store i32 %39, ptr %7, align 4
  br label %68

40:                                               ; preds = %27, %23
  %41 = load i32, ptr %8, align 4, !tbaa !5
  %42 = mul nsw i32 2, %41
  %43 = load i32, ptr %9, align 4, !tbaa !5
  %44 = load i32, ptr %9, align 4, !tbaa !5
  %45 = load i32, ptr %10, align 4, !tbaa !5
  %46 = add nsw i32 %44, %45
  %47 = sdiv i32 %46, 2
  %48 = load i32, ptr %11, align 4, !tbaa !5
  %49 = load i32, ptr %12, align 4, !tbaa !5
  %50 = load i8, ptr %13, align 1, !tbaa !9, !range !11, !noundef !12
  %51 = trunc i8 %50 to i1
  %52 = call noundef i32 @_Z5queryiiiiib(i32 noundef %42, i32 noundef %43, i32 noundef %47, i32 noundef %48, i32 noundef %49, i1 noundef zeroext %51)
  %53 = load i32, ptr %8, align 4, !tbaa !5
  %54 = mul nsw i32 2, %53
  %55 = add nsw i32 %54, 1
  %56 = load i32, ptr %9, align 4, !tbaa !5
  %57 = load i32, ptr %10, align 4, !tbaa !5
  %58 = add nsw i32 %56, %57
  %59 = sdiv i32 %58, 2
  %60 = add nsw i32 %59, 1
  %61 = load i32, ptr %10, align 4, !tbaa !5
  %62 = load i32, ptr %11, align 4, !tbaa !5
  %63 = load i32, ptr %12, align 4, !tbaa !5
  %64 = load i8, ptr %13, align 1, !tbaa !9, !range !11, !noundef !12
  %65 = trunc i8 %64 to i1
  %66 = call noundef i32 @_Z5queryiiiiib(i32 noundef %55, i32 noundef %60, i32 noundef %61, i32 noundef %62, i32 noundef %63, i1 noundef zeroext %65)
  %67 = add nsw i32 %52, %66
  store i32 %67, ptr %7, align 4
  br label %68

68:                                               ; preds = %40, %31, %22
  %69 = load i32, ptr %7, align 4
  ret i32 %69
}

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

!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 = !{!"bool", !7, i64 0}
!11 = !{i8 0, i8 2}
!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"

@t = external dso_local local_unnamed_addr global [2 x [262144 x i32]], align 16

; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_Z5queryiiiiib(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4, i1 noundef zeroext %5) local_unnamed_addr #0 {
  %7 = icmp slt i32 %2, %3
  %8 = icmp sgt i32 %1, %4
  %or.cond39 = or i1 %7, %8
  br i1 %or.cond39, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %6
  %.not29 = icmp sgt i32 %2, %4
  br i1 %.not29, label %tailrecurse.us, label %.lr.ph.split.split

tailrecurse.us:                                   ; preds = %.lr.ph, %tailrecurse.us
  %.tr3142.us = phi i32 [ %14, %tailrecurse.us ], [ %1, %.lr.ph ]
  %.tr41.us = phi i32 [ %13, %tailrecurse.us ], [ %0, %.lr.ph ]
  %accumulator.tr40.us = phi i32 [ %15, %tailrecurse.us ], [ 0, %.lr.ph ]
  %9 = shl nsw i32 %.tr41.us, 1
  %10 = add nsw i32 %.tr3142.us, %2
  %11 = sdiv i32 %10, 2
  %12 = tail call noundef i32 @_Z5queryiiiiib(i32 noundef %9, i32 noundef %.tr3142.us, i32 noundef %11, i32 noundef %3, i32 noundef %4, i1 noundef zeroext %5)
  %13 = or disjoint i32 %9, 1
  %14 = add nsw i32 %11, 1
  %15 = add nsw i32 %12, %accumulator.tr40.us
  %.not54 = icmp slt i32 %11, %4
  br i1 %.not54, label %tailrecurse.us, label %.loopexit

.lr.ph.split.split:                               ; preds = %.lr.ph, %tailrecurse
  %.tr3142 = phi i32 [ %25, %tailrecurse ], [ %1, %.lr.ph ]
  %.tr41 = phi i32 [ %24, %tailrecurse ], [ %0, %.lr.ph ]
  %accumulator.tr40 = phi i32 [ %26, %tailrecurse ], [ 0, %.lr.ph ]
  %.not = icmp slt i32 %.tr3142, %3
  br i1 %.not, label %tailrecurse, label %.split.us

.split.us:                                        ; preds = %.lr.ph.split.split
  %16 = zext i1 %5 to i64
  %17 = sext i32 %.tr41 to i64
  %18 = getelementptr inbounds [2 x [262144 x i32]], ptr @t, i64 0, i64 %16, i64 %17
  %19 = load i32, ptr %18, align 4, !tbaa !5
  br label %.loopexit

tailrecurse:                                      ; preds = %.lr.ph.split.split
  %20 = shl nsw i32 %.tr41, 1
  %21 = add nsw i32 %.tr3142, %2
  %22 = sdiv i32 %21, 2
  %23 = tail call noundef i32 @_Z5queryiiiiib(i32 noundef %20, i32 noundef %.tr3142, i32 noundef %22, i32 noundef %3, i32 noundef %4, i1 noundef zeroext %5)
  %24 = or disjoint i32 %20, 1
  %25 = add nsw i32 %22, 1
  %26 = add nsw i32 %23, %accumulator.tr40
  %.not53 = icmp slt i32 %22, %4
  br i1 %.not53, label %.lr.ph.split.split, label %.loopexit

.loopexit:                                        ; preds = %tailrecurse, %tailrecurse.us, %6, %.split.us
  %accumulator.tr38 = phi i32 [ %accumulator.tr40, %.split.us ], [ 0, %6 ], [ %15, %tailrecurse.us ], [ %26, %tailrecurse ]
  %.0 = phi i32 [ %19, %.split.us ], [ 0, %6 ], [ 0, %tailrecurse.us ], [ 0, %tailrecurse ]
  %accumulator.ret.tr = add nsw i32 %.0, %accumulator.tr38
  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, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}


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