; LLVM IR from VastoLorde95/Competitive-Programming :: Codeforces/179 Division 1/Problem A/A.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"

%struct.node = type { i64 }

@tree = external dso_local global [524288 x %struct.node], 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 uwtable
declare dso_local void @_Z5shiftxxx(i64 noundef, i64 noundef, i64 noundef) #1

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z5queryxxxx(i64 noundef %0, i64 noundef %1, i64 noundef %2, i64 noundef %3) #1 {
  %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
  %10 = alloca i64, align 8
  %11 = alloca i64, align 8
  %12 = alloca i64, align 8
  %13 = alloca i32, align 4
  store i64 %0, ptr %6, align 8, !tbaa !5
  store i64 %1, ptr %7, align 8, !tbaa !5
  store i64 %2, ptr %8, align 8, !tbaa !5
  store i64 %3, ptr %9, align 8, !tbaa !5
  %14 = load i64, ptr %8, align 8, !tbaa !5
  %15 = add nsw i64 %14, 1
  %16 = load i64, ptr %9, align 8, !tbaa !5
  %17 = icmp eq i64 %15, %16
  br i1 %17, label %18, label %23

18:                                               ; preds = %4
  %19 = load i64, ptr %7, align 8, !tbaa !5
  %20 = getelementptr inbounds [524288 x %struct.node], ptr @tree, i64 0, i64 %19
  %21 = getelementptr inbounds %struct.node, ptr %20, i32 0, i32 0
  %22 = load i64, ptr %21, align 8, !tbaa !9
  store i64 %22, ptr %5, align 8
  br label %51

23:                                               ; preds = %4
  %24 = load i64, ptr %7, align 8, !tbaa !5
  %25 = load i64, ptr %8, align 8, !tbaa !5
  %26 = load i64, ptr %9, align 8, !tbaa !5
  call void @_Z5shiftxxx(i64 noundef %24, i64 noundef %25, i64 noundef %26)
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  %27 = load i64, ptr %7, align 8, !tbaa !5
  %28 = shl i64 %27, 1
  store i64 %28, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #2
  %29 = load i64, ptr %10, align 8, !tbaa !5
  %30 = add nsw i64 %29, 1
  store i64 %30, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #2
  %31 = load i64, ptr %8, align 8, !tbaa !5
  %32 = load i64, ptr %9, align 8, !tbaa !5
  %33 = add nsw i64 %31, %32
  %34 = ashr i64 %33, 1
  store i64 %34, ptr %12, align 8, !tbaa !5
  %35 = load i64, ptr %6, align 8, !tbaa !5
  %36 = load i64, ptr %12, align 8, !tbaa !5
  %37 = icmp slt i64 %35, %36
  br i1 %37, label %38, label %44

38:                                               ; preds = %23
  %39 = load i64, ptr %6, align 8, !tbaa !5
  %40 = load i64, ptr %10, align 8, !tbaa !5
  %41 = load i64, ptr %8, align 8, !tbaa !5
  %42 = load i64, ptr %12, align 8, !tbaa !5
  %43 = call noundef i64 @_Z5queryxxxx(i64 noundef %39, i64 noundef %40, i64 noundef %41, i64 noundef %42)
  store i64 %43, ptr %5, align 8
  store i32 1, ptr %13, align 4
  br label %50

44:                                               ; preds = %23
  %45 = load i64, ptr %6, align 8, !tbaa !5
  %46 = load i64, ptr %11, align 8, !tbaa !5
  %47 = load i64, ptr %12, align 8, !tbaa !5
  %48 = load i64, ptr %9, align 8, !tbaa !5
  %49 = call noundef i64 @_Z5queryxxxx(i64 noundef %45, i64 noundef %46, i64 noundef %47, i64 noundef %48)
  store i64 %49, ptr %5, align 8
  store i32 1, ptr %13, align 4
  br label %50

50:                                               ; preds = %44, %38
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  br label %51

51:                                               ; preds = %50, %18
  %52 = load i64, ptr %5, align 8
  ret i64 %52
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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"}
!9 = !{!10, !6, i64 0}
!10 = !{!"_ZTS4node", !6, 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.node = type { i64 }

@tree = external dso_local local_unnamed_addr global [524288 x %struct.node], align 16

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z5shiftxxx(i64 noundef, i64 noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z5queryxxxx(i64 noundef %0, i64 noundef %1, i64 noundef %2, i64 noundef %3) local_unnamed_addr #0 {
  br label %tailrecurse.outer

tailrecurse.outer:                                ; preds = %15, %4
  %.tr26.ph = phi i64 [ %16, %15 ], [ %1, %4 ]
  %.tr27.ph = phi i64 [ %13, %15 ], [ %2, %4 ]
  %.tr28.ph = phi i64 [ %.tr28, %15 ], [ %3, %4 ]
  %5 = add nsw i64 %.tr27.ph, 1
  br label %tailrecurse

tailrecurse:                                      ; preds = %tailrecurse.outer, %10
  %.tr26 = phi i64 [ %11, %10 ], [ %.tr26.ph, %tailrecurse.outer ]
  %.tr28 = phi i64 [ %13, %10 ], [ %.tr28.ph, %tailrecurse.outer ]
  %6 = icmp eq i64 %5, %.tr28
  br i1 %6, label %7, label %10

7:                                                ; preds = %tailrecurse
  %8 = getelementptr inbounds [524288 x %struct.node], ptr @tree, i64 0, i64 %.tr26
  %9 = load i64, ptr %8, align 8, !tbaa !5
  ret i64 %9

10:                                               ; preds = %tailrecurse
  tail call void @_Z5shiftxxx(i64 noundef %.tr26, i64 noundef %.tr27.ph, i64 noundef %.tr28)
  %11 = shl i64 %.tr26, 1
  %12 = add nsw i64 %.tr28, %.tr27.ph
  %13 = ashr i64 %12, 1
  %14 = icmp sgt i64 %13, %0
  br i1 %14, label %tailrecurse, label %15

15:                                               ; preds = %10
  %16 = or disjoint i64 %11, 1
  br label %tailrecurse.outer
}

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


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