; LLVM IR from VAR-solutions/Algorithms :: Tree/segment tree/cpp/Inversion Count Segment tree.ll (Z5queryxxxx)

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

@tree = external dso_local global [800001 x i64], align 16
@sum = external dso_local global i64, align 8

; Function Attrs: mustprogress uwtable
define dso_local void @_Z5queryxxxx(i64 noundef %0, i64 noundef %1, i64 noundef %2, i64 noundef %3) #0 {
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  store i64 %0, ptr %5, align 8, !tbaa !5
  store i64 %1, ptr %6, align 8, !tbaa !5
  store i64 %2, ptr %7, align 8, !tbaa !5
  store i64 %3, ptr %8, align 8, !tbaa !5
  %10 = load i64, ptr %6, align 8, !tbaa !5
  %11 = load i64, ptr %7, align 8, !tbaa !5
  %12 = icmp eq i64 %10, %11
  br i1 %12, label %13, label %18

13:                                               ; preds = %4
  %14 = load i64, ptr %5, align 8, !tbaa !5
  %15 = getelementptr inbounds [800001 x i64], ptr @tree, i64 0, i64 %14
  %16 = load i64, ptr %15, align 8, !tbaa !5
  %17 = add nsw i64 %16, 1
  store i64 %17, ptr %15, align 8, !tbaa !5
  br label %59

18:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  %19 = load i64, ptr %6, align 8, !tbaa !5
  %20 = load i64, ptr %7, align 8, !tbaa !5
  %21 = add nsw i64 %19, %20
  %22 = sdiv i64 %21, 2
  store i64 %22, ptr %9, align 8, !tbaa !5
  %23 = load i64, ptr %8, align 8, !tbaa !5
  %24 = load i64, ptr %9, align 8, !tbaa !5
  %25 = icmp sle i64 %23, %24
  br i1 %25, label %26, label %32

26:                                               ; preds = %18
  %27 = load i64, ptr %5, align 8, !tbaa !5
  %28 = mul nsw i64 2, %27
  %29 = load i64, ptr %6, align 8, !tbaa !5
  %30 = load i64, ptr %9, align 8, !tbaa !5
  %31 = load i64, ptr %8, align 8, !tbaa !5
  call void @_Z5queryxxxx(i64 noundef %28, i64 noundef %29, i64 noundef %30, i64 noundef %31)
  br label %46

32:                                               ; preds = %18
  %33 = load i64, ptr %5, align 8, !tbaa !5
  %34 = mul nsw i64 2, %33
  %35 = getelementptr inbounds [800001 x i64], ptr @tree, i64 0, i64 %34
  %36 = load i64, ptr %35, align 8, !tbaa !5
  %37 = load i64, ptr @sum, align 8, !tbaa !5
  %38 = add nsw i64 %37, %36
  store i64 %38, ptr @sum, align 8, !tbaa !5
  %39 = load i64, ptr %5, align 8, !tbaa !5
  %40 = mul nsw i64 2, %39
  %41 = add nsw i64 %40, 1
  %42 = load i64, ptr %9, align 8, !tbaa !5
  %43 = add nsw i64 %42, 1
  %44 = load i64, ptr %7, align 8, !tbaa !5
  %45 = load i64, ptr %8, align 8, !tbaa !5
  call void @_Z5queryxxxx(i64 noundef %41, i64 noundef %43, i64 noundef %44, i64 noundef %45)
  br label %46

46:                                               ; preds = %32, %26
  %47 = load i64, ptr %5, align 8, !tbaa !5
  %48 = mul nsw i64 2, %47
  %49 = getelementptr inbounds [800001 x i64], ptr @tree, i64 0, i64 %48
  %50 = load i64, ptr %49, align 8, !tbaa !5
  %51 = load i64, ptr %5, align 8, !tbaa !5
  %52 = mul nsw i64 2, %51
  %53 = add nsw i64 %52, 1
  %54 = getelementptr inbounds [800001 x i64], ptr @tree, i64 0, i64 %53
  %55 = load i64, ptr %54, align 8, !tbaa !5
  %56 = add nsw i64 %50, %55
  %57 = load i64, ptr %5, align 8, !tbaa !5
  %58 = getelementptr inbounds [800001 x i64], ptr @tree, i64 0, i64 %57
  store i64 %56, ptr %58, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  br label %59

59:                                               ; preds = %46, %13
  ret void
}

; 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

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

@tree = external dso_local local_unnamed_addr global [800001 x i64], align 16
@sum = external dso_local local_unnamed_addr global i64, align 8

; Function Attrs: mustprogress nofree nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z5queryxxxx(i64 noundef %0, i64 noundef %1, i64 noundef %2, i64 noundef %3) local_unnamed_addr #0 {
  %5 = icmp eq i64 %1, %2
  br i1 %5, label %6, label %10

6:                                                ; preds = %4
  %7 = getelementptr inbounds [800001 x i64], ptr @tree, i64 0, i64 %0
  %8 = load i64, ptr %7, align 8, !tbaa !5
  %9 = add nsw i64 %8, 1
  store i64 %9, ptr %7, align 8, !tbaa !5
  br label %29

10:                                               ; preds = %4
  %11 = add nsw i64 %2, %1
  %12 = sdiv i64 %11, 2
  %.not = icmp slt i64 %12, %3
  %13 = shl nsw i64 %0, 1
  br i1 %.not, label %15, label %14

14:                                               ; preds = %10
  tail call void @_Z5queryxxxx(i64 noundef %13, i64 noundef %1, i64 noundef %12, i64 noundef %3)
  %.pre = or disjoint i64 %13, 1
  br label %22

15:                                               ; preds = %10
  %16 = getelementptr inbounds [800001 x i64], ptr @tree, i64 0, i64 %13
  %17 = load i64, ptr %16, align 16, !tbaa !5
  %18 = load i64, ptr @sum, align 8, !tbaa !5
  %19 = add nsw i64 %18, %17
  store i64 %19, ptr @sum, align 8, !tbaa !5
  %20 = or disjoint i64 %13, 1
  %21 = add nsw i64 %12, 1
  tail call void @_Z5queryxxxx(i64 noundef %20, i64 noundef %21, i64 noundef %2, i64 noundef %3)
  br label %22

22:                                               ; preds = %15, %14
  %.pre-phi23 = phi i64 [ %20, %15 ], [ %.pre, %14 ]
  %23 = getelementptr inbounds [800001 x i64], ptr @tree, i64 0, i64 %13
  %24 = load i64, ptr %23, align 16, !tbaa !5
  %25 = getelementptr inbounds [800001 x i64], ptr @tree, i64 0, i64 %.pre-phi23
  %26 = load i64, ptr %25, align 8, !tbaa !5
  %27 = add nsw i64 %26, %24
  %28 = getelementptr inbounds [800001 x i64], ptr @tree, i64 0, i64 %0
  store i64 %27, ptr %28, align 8, !tbaa !5
  br label %29

29:                                               ; preds = %22, %6
  ret void
}

attributes #0 = { mustprogress nofree nosync nounwind memory(readwrite, 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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass