; LLVM IR from tangjz/acm-icpc :: hdu/5683.ll (Z5queryii)

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

@seq = external dso_local global [40002 x i32], align 16
@seg = external dso_local global [80004 x i32], align 16
@qL = external dso_local global i32, align 4
@qR = external dso_local global i32, align 4

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z7seg_idxii(i32 noundef, i32 noundef) #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: mustprogress uwtable
define dso_local noundef i32 @_Z5queryii(i32 noundef %0, i32 noundef %1) #2 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !5
  %8 = load i32, ptr @qL, align 4, !tbaa !5
  %9 = load i32, ptr %4, align 4, !tbaa !5
  %10 = sext i32 %9 to i64
  %11 = getelementptr inbounds [40002 x i32], ptr @seq, i64 0, i64 %10
  %12 = load i32, ptr %11, align 4, !tbaa !5
  %13 = icmp sle i32 %8, %12
  br i1 %13, label %14, label %28

14:                                               ; preds = %2
  %15 = load i32, ptr %5, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [40002 x i32], ptr @seq, i64 0, i64 %16
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = load i32, ptr @qR, align 4, !tbaa !5
  %20 = icmp sle i32 %18, %19
  br i1 %20, label %21, label %28

21:                                               ; preds = %14
  %22 = load i32, ptr %4, align 4, !tbaa !5
  %23 = load i32, ptr %5, align 4, !tbaa !5
  %24 = call noundef i32 @_Z7seg_idxii(i32 noundef %22, i32 noundef %23)
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [80004 x i32], ptr @seg, i64 0, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !5
  store i32 %27, ptr %3, align 4
  br label %62

28:                                               ; preds = %14, %2
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %29 = load i32, ptr %4, align 4, !tbaa !5
  %30 = load i32, ptr %5, align 4, !tbaa !5
  %31 = add nsw i32 %29, %30
  %32 = ashr i32 %31, 1
  store i32 %32, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 0, ptr %7, align 4, !tbaa !5
  %33 = load i32, ptr @qL, align 4, !tbaa !5
  %34 = load i32, ptr %6, align 4, !tbaa !5
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [40002 x i32], ptr @seq, i64 0, i64 %35
  %37 = load i32, ptr %36, align 4, !tbaa !5
  %38 = icmp sle i32 %33, %37
  br i1 %38, label %39, label %45

39:                                               ; preds = %28
  %40 = load i32, ptr %4, align 4, !tbaa !5
  %41 = load i32, ptr %6, align 4, !tbaa !5
  %42 = call noundef i32 @_Z5queryii(i32 noundef %40, i32 noundef %41)
  %43 = load i32, ptr %7, align 4, !tbaa !5
  %44 = add nsw i32 %43, %42
  store i32 %44, ptr %7, align 4, !tbaa !5
  br label %45

45:                                               ; preds = %39, %28
  %46 = load i32, ptr @qR, align 4, !tbaa !5
  %47 = load i32, ptr %6, align 4, !tbaa !5
  %48 = add nsw i32 %47, 1
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds [40002 x i32], ptr @seq, i64 0, i64 %49
  %51 = load i32, ptr %50, align 4, !tbaa !5
  %52 = icmp sge i32 %46, %51
  br i1 %52, label %53, label %60

53:                                               ; preds = %45
  %54 = load i32, ptr %6, align 4, !tbaa !5
  %55 = add nsw i32 %54, 1
  %56 = load i32, ptr %5, align 4, !tbaa !5
  %57 = call noundef i32 @_Z5queryii(i32 noundef %55, i32 noundef %56)
  %58 = load i32, ptr %7, align 4, !tbaa !5
  %59 = add nsw i32 %58, %57
  store i32 %59, ptr %7, align 4, !tbaa !5
  br label %60

60:                                               ; preds = %53, %45
  %61 = load i32, ptr %7, align 4, !tbaa !5
  store i32 %61, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  br label %62

62:                                               ; preds = %60, %21
  %63 = load i32, ptr %3, align 4
  ret i32 %63
}

attributes #0 = { inlinehint 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #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"}


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

@seq = external dso_local local_unnamed_addr global [40002 x i32], align 16
@seg = external dso_local local_unnamed_addr global [80004 x i32], align 16
@qL = external dso_local local_unnamed_addr global i32, align 4
@qR = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z7seg_idxii(i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z5queryii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #1 {
  %3 = sext i32 %1 to i64
  %4 = getelementptr inbounds [40002 x i32], ptr @seq, i64 0, i64 %3
  %.pre27 = load i32, ptr @qL, align 4, !tbaa !5
  %.phi.trans.insert = sext i32 %0 to i64
  %.phi.trans.insert28 = getelementptr inbounds [40002 x i32], ptr @seq, i64 0, i64 %.phi.trans.insert
  %.pre29 = load i32, ptr %.phi.trans.insert28, align 4, !tbaa !5
  br label %tailrecurse

tailrecurse:                                      ; preds = %30, %2
  %5 = phi i32 [ %.pre29, %2 ], [ %29, %30 ]
  %6 = phi i32 [ %.pre27, %2 ], [ %24, %30 ]
  %accumulator.tr = phi i32 [ 0, %2 ], [ %31, %30 ]
  %.tr = phi i32 [ %0, %2 ], [ %26, %30 ]
  %.not = icmp sgt i32 %6, %5
  br i1 %.not, label %15, label %7

7:                                                ; preds = %tailrecurse
  %8 = load i32, ptr %4, align 4, !tbaa !5
  %9 = load i32, ptr @qR, align 4, !tbaa !5
  %.not17 = icmp sgt i32 %8, %9
  br i1 %.not17, label %15, label %10

10:                                               ; preds = %7
  %11 = tail call noundef i32 @_Z7seg_idxii(i32 noundef %.tr, i32 noundef %1)
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [80004 x i32], ptr @seg, i64 0, i64 %12
  %14 = load i32, ptr %13, align 4, !tbaa !5
  br label %.loopexit

15:                                               ; preds = %7, %tailrecurse
  %16 = add nsw i32 %.tr, %1
  %17 = ashr i32 %16, 1
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [40002 x i32], ptr @seq, i64 0, i64 %18
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %.not18 = icmp sgt i32 %6, %20
  br i1 %.not18, label %23, label %21

21:                                               ; preds = %15
  %22 = tail call noundef i32 @_Z5queryii(i32 noundef %.tr, i32 noundef %17)
  %.pre = load i32, ptr @qL, align 4, !tbaa !5
  br label %23

23:                                               ; preds = %21, %15
  %24 = phi i32 [ %.pre, %21 ], [ %6, %15 ]
  %.0 = phi i32 [ %22, %21 ], [ 0, %15 ]
  %25 = load i32, ptr @qR, align 4, !tbaa !5
  %26 = add nsw i32 %17, 1
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [40002 x i32], ptr @seq, i64 0, i64 %27
  %29 = load i32, ptr %28, align 4, !tbaa !5
  %.not19 = icmp slt i32 %25, %29
  br i1 %.not19, label %.loopexit, label %30

30:                                               ; preds = %23
  %31 = add nsw i32 %.0, %accumulator.tr
  br label %tailrecurse

.loopexit:                                        ; preds = %23, %10
  %.016 = phi i32 [ %14, %10 ], [ %.0, %23 ]
  %accumulator.ret.tr = add nsw i32 %.016, %accumulator.tr
  ret i32 %accumulator.ret.tr
}

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

!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: BDCEPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass