; LLVM IR from VastoLorde95/Competitive-Programming :: Hackerrank/Week of Code/Week 20/Problem F/FFF.ll (Z5queryiii)

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

@l = external dso_local global [100100 x i32], align 16
@r = external dso_local global [100100 x i32], align 16

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z3getii(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 @_Z5queryiii(i32 noundef %0, i32 noundef %1, i32 noundef %2) #0 {
  %4 = alloca i32, align 4
  %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
  store i32 %0, ptr %5, align 4, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !5
  %11 = load i32, ptr %6, align 4, !tbaa !5
  %12 = load i32, ptr %5, align 4, !tbaa !5
  %13 = sub nsw i32 %11, %12
  %14 = add nsw i32 %13, 1
  %15 = load i32, ptr %7, align 4, !tbaa !5
  %16 = icmp slt i32 %14, %15
  br i1 %16, label %17, label %18

17:                                               ; preds = %3
  store i32 0, ptr %4, align 4
  br label %56

18:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %19 = load i32, ptr %5, align 4, !tbaa !5
  %20 = load i32, ptr %6, align 4, !tbaa !5
  %21 = call noundef i32 @_Z3getii(i32 noundef %19, i32 noundef %20)
  store i32 %21, ptr %8, align 4, !tbaa !5
  %22 = load i32, ptr %8, align 4, !tbaa !5
  %23 = icmp eq i32 %22, 1000000000
  br i1 %23, label %24, label %25

24:                                               ; preds = %18
  store i32 0, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %55

25:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  %26 = load i32, ptr %8, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [100100 x i32], ptr @r, i64 0, i64 %27
  %29 = load i32, ptr %28, align 4, !tbaa !5
  %30 = load i32, ptr %8, align 4, !tbaa !5
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [100100 x i32], ptr @l, i64 0, i64 %31
  %33 = load i32, ptr %32, align 4, !tbaa !5
  %34 = sub nsw i32 %29, %33
  %35 = add nsw i32 %34, 1
  store i32 %35, ptr %10, align 4, !tbaa !5
  %36 = load i32, ptr %10, align 4, !tbaa !5
  %37 = load i32, ptr %5, align 4, !tbaa !5
  %38 = load i32, ptr %8, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [100100 x i32], ptr @l, i64 0, i64 %39
  %41 = load i32, ptr %40, align 4, !tbaa !5
  %42 = sub nsw i32 %41, 1
  %43 = load i32, ptr %7, align 4, !tbaa !5
  %44 = call noundef i32 @_Z5queryiii(i32 noundef %37, i32 noundef %42, i32 noundef %43)
  %45 = add nsw i32 %36, %44
  %46 = load i32, ptr %8, align 4, !tbaa !5
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds [100100 x i32], ptr @r, i64 0, i64 %47
  %49 = load i32, ptr %48, align 4, !tbaa !5
  %50 = add nsw i32 %49, 1
  %51 = load i32, ptr %6, align 4, !tbaa !5
  %52 = load i32, ptr %7, align 4, !tbaa !5
  %53 = call noundef i32 @_Z5queryiii(i32 noundef %50, i32 noundef %51, i32 noundef %52)
  %54 = add nsw i32 %45, %53
  store i32 %54, ptr %4, align 4
  store i32 1, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  br label %55

55:                                               ; preds = %25, %24
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  br label %56

56:                                               ; preds = %55, %17
  %57 = load i32, ptr %4, align 4
  ret i32 %57
}

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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"}


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

@l = external dso_local local_unnamed_addr global [100100 x i32], align 16
@r = external dso_local local_unnamed_addr global [100100 x i32], align 16

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z5queryiii(i32 noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = add i32 %1, 1
  %5 = sub i32 %4, %0
  %6 = icmp slt i32 %5, %2
  br i1 %6, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %3, %tailrecurse
  %.tr22 = phi i32 [ %17, %tailrecurse ], [ %0, %3 ]
  %accumulator.tr21 = phi i32 [ %21, %tailrecurse ], [ 0, %3 ]
  %7 = tail call noundef i32 @_Z3getii(i32 noundef %.tr22, i32 noundef %1)
  %8 = icmp eq i32 %7, 1000000000
  br i1 %8, label %._crit_edge, label %tailrecurse

tailrecurse:                                      ; preds = %.lr.ph
  %9 = sext i32 %7 to i64
  %10 = getelementptr inbounds [100100 x i32], ptr @r, i64 0, i64 %9
  %11 = load i32, ptr %10, align 4, !tbaa !5
  %12 = getelementptr inbounds [100100 x i32], ptr @l, i64 0, i64 %9
  %13 = load i32, ptr %12, align 4, !tbaa !5
  %14 = add nsw i32 %13, -1
  %15 = tail call noundef i32 @_Z5queryiii(i32 noundef %.tr22, i32 noundef %14, i32 noundef %2)
  %16 = load i32, ptr %10, align 4, !tbaa !5
  %17 = add nsw i32 %16, 1
  %18 = add i32 %accumulator.tr21, 1
  %19 = add i32 %18, %11
  %20 = sub i32 %19, %13
  %21 = add i32 %20, %15
  %22 = sub i32 %1, %16
  %23 = icmp slt i32 %22, %2
  br i1 %23, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %tailrecurse, %.lr.ph, %3
  %accumulator.tr.lcssa = phi i32 [ 0, %3 ], [ %accumulator.tr21, %.lr.ph ], [ %21, %tailrecurse ]
  ret i32 %accumulator.tr.lcssa
}

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


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