; LLVM IR from codeIIEST/Algorithms :: Competitive Coding/Tree/Binary Indexed Tree (SPOJ_ctrick)

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

@hash_array_mod = external dso_local global [200002 x i64], align 16

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_Z3sumPxx(ptr noundef, i64 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 i64 @_Z22modified_binary_searchPxxxx(ptr noundef %0, i64 noundef %1, i64 noundef %2, i64 noundef %3) #2 {
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca i64, align 8
  %12 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i64 %1, ptr %7, align 8, !tbaa !9
  store i64 %2, ptr %8, align 8, !tbaa !9
  store i64 %3, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %13 = load i64, ptr %8, align 8, !tbaa !9
  %14 = load i64, ptr %9, align 8, !tbaa !9
  %15 = add nsw i64 %13, %14
  %16 = sdiv i64 %15, 2
  store i64 %16, ptr %10, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  %17 = load ptr, ptr %6, align 8, !tbaa !5
  %18 = load i64, ptr %10, align 8, !tbaa !9
  %19 = call noundef i64 @_Z3sumPxx(ptr noundef %17, i64 noundef %18)
  store i64 %19, ptr %11, align 8, !tbaa !9
  %20 = load i64, ptr %11, align 8, !tbaa !9
  %21 = load i64, ptr %7, align 8, !tbaa !9
  %22 = icmp eq i64 %20, %21
  br i1 %22, label %23, label %31

23:                                               ; preds = %4
  %24 = load i64, ptr %10, align 8, !tbaa !9
  %25 = add nsw i64 %24, 1
  %26 = getelementptr inbounds [200002 x i64], ptr @hash_array_mod, i64 0, i64 %25
  %27 = load i64, ptr %26, align 8, !tbaa !9
  %28 = icmp eq i64 %27, 0
  br i1 %28, label %29, label %31

29:                                               ; preds = %23
  %30 = load i64, ptr %10, align 8, !tbaa !9
  store i64 %30, ptr %5, align 8
  store i32 1, ptr %12, align 4
  br label %59

31:                                               ; preds = %23, %4
  %32 = load i64, ptr %11, align 8, !tbaa !9
  %33 = load i64, ptr %7, align 8, !tbaa !9
  %34 = icmp sgt i64 %32, %33
  br i1 %34, label %45, label %35

35:                                               ; preds = %31
  %36 = load i64, ptr %11, align 8, !tbaa !9
  %37 = load i64, ptr %7, align 8, !tbaa !9
  %38 = icmp eq i64 %36, %37
  br i1 %38, label %39, label %52

39:                                               ; preds = %35
  %40 = load i64, ptr %10, align 8, !tbaa !9
  %41 = add nsw i64 %40, 1
  %42 = getelementptr inbounds [200002 x i64], ptr @hash_array_mod, i64 0, i64 %41
  %43 = load i64, ptr %42, align 8, !tbaa !9
  %44 = icmp ne i64 %43, 0
  br i1 %44, label %45, label %52

45:                                               ; preds = %39, %31
  %46 = load ptr, ptr %6, align 8, !tbaa !5
  %47 = load i64, ptr %7, align 8, !tbaa !9
  %48 = load i64, ptr %8, align 8, !tbaa !9
  %49 = load i64, ptr %10, align 8, !tbaa !9
  %50 = sub nsw i64 %49, 1
  %51 = call noundef i64 @_Z22modified_binary_searchPxxxx(ptr noundef %46, i64 noundef %47, i64 noundef %48, i64 noundef %50)
  store i64 %51, ptr %5, align 8
  store i32 1, ptr %12, align 4
  br label %59

52:                                               ; preds = %39, %35
  %53 = load ptr, ptr %6, align 8, !tbaa !5
  %54 = load i64, ptr %7, align 8, !tbaa !9
  %55 = load i64, ptr %10, align 8, !tbaa !9
  %56 = add nsw i64 %55, 1
  %57 = load i64, ptr %9, align 8, !tbaa !9
  %58 = call noundef i64 @_Z22modified_binary_searchPxxxx(ptr noundef %53, i64 noundef %54, i64 noundef %56, i64 noundef %57)
  store i64 %58, ptr %5, align 8
  store i32 1, ptr %12, align 4
  br label %59

59:                                               ; preds = %52, %45, %29
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  %60 = load i64, ptr %5, align 8
  ret i64 %60
}

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 = { 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 = !{!"any pointer", !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"

@hash_array_mod = external dso_local local_unnamed_addr global [200002 x i64], align 16

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_Z3sumPxx(ptr noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef range(i64 -4611686018427387904, 4611686018427387904) i64 @_Z22modified_binary_searchPxxxx(ptr noundef %0, i64 noundef %1, i64 noundef %2, i64 noundef %3) local_unnamed_addr #0 {
  br label %tailrecurse.outer

tailrecurse.outer:                                ; preds = %.thread22, %4
  %.tr24.ph = phi i64 [ %.pre, %.thread22 ], [ %2, %4 ]
  %.tr25.ph = phi i64 [ %.tr25, %.thread22 ], [ %3, %4 ]
  br label %tailrecurse

tailrecurse:                                      ; preds = %tailrecurse.outer, %15
  %.tr25 = phi i64 [ %16, %15 ], [ %.tr25.ph, %tailrecurse.outer ]
  %5 = add nsw i64 %.tr25, %.tr24.ph
  %6 = sdiv i64 %5, 2
  %7 = tail call noundef i64 @_Z3sumPxx(ptr noundef %0, i64 noundef %6)
  %8 = icmp eq i64 %7, %1
  br i1 %8, label %9, label %.thread

9:                                                ; preds = %tailrecurse
  %10 = add nsw i64 %6, 1
  %11 = getelementptr inbounds [200002 x i64], ptr @hash_array_mod, i64 0, i64 %10
  %12 = load i64, ptr %11, align 8, !tbaa !5
  %13 = icmp eq i64 %12, 0
  br i1 %13, label %17, label %15

.thread:                                          ; preds = %tailrecurse
  %14 = icmp sgt i64 %7, %1
  br i1 %14, label %15, label %.thread22

15:                                               ; preds = %9, %.thread
  %16 = add nsw i64 %6, -1
  br label %tailrecurse

.thread22:                                        ; preds = %.thread
  %.pre = add nsw i64 %6, 1
  br label %tailrecurse.outer

17:                                               ; preds = %9
  ret i64 %6
}

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

!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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}


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