; LLVM IR from lzyrapx/Competitive-Programming :: 百度之星/2016 Astar Contest - Round 2A/1003-Snacks.ll (Z3qryiii)

; 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.segtree = type { i64, i64 }

@ql = external dso_local global i32, align 4
@qr = external dso_local global i32, align 4
@ans = external dso_local global i64, align 8
@t = external dso_local global [800040 x %struct.segtree], 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 nounwind uwtable
declare dso_local void @_Z2pdi(i32 noundef) #1

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) #2

; Function Attrs: mustprogress uwtable
define dso_local void @_Z3qryiii(i32 noundef %0, i32 noundef %1, i32 noundef %2) #3 {
  %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
  store i32 %2, ptr %6, align 4, !tbaa !5
  %8 = load i32, ptr %4, align 4, !tbaa !5
  call void @_Z2pdi(i32 noundef %8)
  %9 = load i32, ptr @ql, align 4, !tbaa !5
  %10 = load i32, ptr %6, align 4, !tbaa !5
  %11 = icmp sgt i32 %9, %10
  br i1 %11, label %16, label %12

12:                                               ; preds = %3
  %13 = load i32, ptr @qr, align 4, !tbaa !5
  %14 = load i32, ptr %5, align 4, !tbaa !5
  %15 = icmp slt i32 %13, %14
  br i1 %15, label %16, label %17

16:                                               ; preds = %12, %3
  br label %47

17:                                               ; preds = %12
  %18 = load i32, ptr @ql, align 4, !tbaa !5
  %19 = load i32, ptr %5, align 4, !tbaa !5
  %20 = icmp sle i32 %18, %19
  br i1 %20, label %21, label %32

21:                                               ; preds = %17
  %22 = load i32, ptr %6, align 4, !tbaa !5
  %23 = load i32, ptr @qr, align 4, !tbaa !5
  %24 = icmp sle i32 %22, %23
  br i1 %24, label %25, label %32

25:                                               ; preds = %21
  %26 = load i32, ptr %4, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [800040 x %struct.segtree], ptr @t, i64 0, i64 %27
  %29 = getelementptr inbounds %struct.segtree, ptr %28, i32 0, i32 0
  %30 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) @ans, ptr noundef nonnull align 8 dereferenceable(8) %29)
  %31 = load i64, ptr %30, align 8, !tbaa !9
  store i64 %31, ptr @ans, align 8, !tbaa !9
  br label %47

32:                                               ; preds = %21, %17
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  %33 = load i32, ptr %5, align 4, !tbaa !5
  %34 = load i32, ptr %6, align 4, !tbaa !5
  %35 = add nsw i32 %33, %34
  %36 = ashr i32 %35, 1
  store i32 %36, ptr %7, align 4, !tbaa !5
  %37 = load i32, ptr %4, align 4, !tbaa !5
  %38 = shl i32 %37, 1
  %39 = load i32, ptr %5, align 4, !tbaa !5
  %40 = load i32, ptr %7, align 4, !tbaa !5
  call void @_Z3qryiii(i32 noundef %38, i32 noundef %39, i32 noundef %40)
  %41 = load i32, ptr %4, align 4, !tbaa !5
  %42 = shl i32 %41, 1
  %43 = or i32 %42, 1
  %44 = load i32, ptr %7, align 4, !tbaa !5
  %45 = add nsw i32 %44, 1
  %46 = load i32, ptr %6, align 4, !tbaa !5
  call void @_Z3qryiii(i32 noundef %43, i32 noundef %45, i32 noundef %46)
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  br label %47

47:                                               ; preds = %32, %25, %16
  ret void
}

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


; 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.segtree = type { i64, i64 }

@ql = external dso_local local_unnamed_addr global i32, align 4
@qr = external dso_local local_unnamed_addr global i32, align 4
@ans = external dso_local global i64, align 8
@t = external dso_local global [800040 x %struct.segtree], align 16

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) local_unnamed_addr #1

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z3qryiii(i32 noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  tail call void @_Z2pdi(i32 noundef %0)
  %4 = load i32, ptr @ql, align 4, !tbaa !5
  %5 = icmp sgt i32 %4, %2
  br i1 %5, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %3, %tailrecurse
  %6 = phi i32 [ %20, %tailrecurse ], [ %4, %3 ]
  %.tr1822 = phi i32 [ %19, %tailrecurse ], [ %1, %3 ]
  %.tr21 = phi i32 [ %18, %tailrecurse ], [ %0, %3 ]
  %7 = load i32, ptr @qr, align 4, !tbaa !5
  %8 = icmp slt i32 %7, %.tr1822
  br i1 %8, label %.loopexit, label %9

9:                                                ; preds = %.lr.ph
  %.not = icmp sgt i32 %6, %.tr1822
  %.not17 = icmp slt i32 %7, %2
  %or.cond = or i1 %.not, %.not17
  br i1 %or.cond, label %tailrecurse, label %10

10:                                               ; preds = %9
  %11 = sext i32 %.tr21 to i64
  %12 = getelementptr inbounds [800040 x %struct.segtree], ptr @t, i64 0, i64 %11
  %13 = tail call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) @ans, ptr noundef nonnull align 8 dereferenceable(8) %12)
  %14 = load i64, ptr %13, align 8, !tbaa !9
  store i64 %14, ptr @ans, align 8, !tbaa !9
  br label %.loopexit

tailrecurse:                                      ; preds = %9
  %15 = add nsw i32 %.tr1822, %2
  %16 = ashr i32 %15, 1
  %17 = shl i32 %.tr21, 1
  tail call void @_Z3qryiii(i32 noundef %17, i32 noundef %.tr1822, i32 noundef %16)
  %18 = or disjoint i32 %17, 1
  %19 = add nsw i32 %16, 1
  tail call void @_Z2pdi(i32 noundef %18)
  %20 = load i32, ptr @ql, align 4, !tbaa !5
  %21 = icmp sgt i32 %20, %2
  br i1 %21, label %.loopexit, label %.lr.ph

.loopexit:                                        ; preds = %.lr.ph, %tailrecurse, %3, %10
  ret void
}

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

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


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