; LLVM IR from VastoLorde95/Competitive-Programming :: CodeChef/WF/Medium Hard/XRQRS/A.ll (Z6query3iii)

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

@roots1 = external dso_local global [500100 x ptr], 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 noundef ptr @_Z2goP4nodei(ptr noundef, i32 noundef) #1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZN4node4lcntEv(ptr noundef nonnull align 8 dereferenceable(20)) #1 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z6query3iii(i32 noundef %0, i32 noundef %1, i32 noundef %2) #2 {
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = 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
  %12 = load i32, ptr %4, align 4, !tbaa !5
  %13 = add nsw i32 %12, -1
  store i32 %13, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 0, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %14 = load i32, ptr %4, align 4, !tbaa !5
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds [500100 x ptr], ptr @roots1, i64 0, i64 %15
  %17 = load ptr, ptr %16, align 8, !tbaa !9
  store ptr %17, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %18 = load i32, ptr %5, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [500100 x ptr], ptr @roots1, i64 0, i64 %19
  %21 = load ptr, ptr %20, align 8, !tbaa !9
  store ptr %21, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  store i32 0, ptr %10, align 4, !tbaa !5
  br label %22

22:                                               ; preds = %65, %3
  %23 = load i32, ptr %10, align 4, !tbaa !5
  %24 = icmp slt i32 %23, 30
  br i1 %24, label %26, label %25

25:                                               ; preds = %22
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  br label %68

26:                                               ; preds = %22
  %27 = load i32, ptr %7, align 4, !tbaa !5
  %28 = shl i32 %27, 1
  store i32 %28, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  %29 = load ptr, ptr %9, align 8, !tbaa !9
  %30 = icmp eq ptr %29, null
  br i1 %30, label %31, label %32

31:                                               ; preds = %26
  br label %35

32:                                               ; preds = %26
  %33 = load ptr, ptr %9, align 8, !tbaa !9
  %34 = call noundef i32 @_ZN4node4lcntEv(ptr noundef nonnull align 8 dereferenceable(20) %33)
  br label %35

35:                                               ; preds = %32, %31
  %36 = phi i32 [ 0, %31 ], [ %34, %32 ]
  %37 = load ptr, ptr %8, align 8, !tbaa !9
  %38 = icmp eq ptr %37, null
  br i1 %38, label %39, label %40

39:                                               ; preds = %35
  br label %43

40:                                               ; preds = %35
  %41 = load ptr, ptr %8, align 8, !tbaa !9
  %42 = call noundef i32 @_ZN4node4lcntEv(ptr noundef nonnull align 8 dereferenceable(20) %41)
  br label %43

43:                                               ; preds = %40, %39
  %44 = phi i32 [ 0, %39 ], [ %42, %40 ]
  %45 = sub nsw i32 %36, %44
  store i32 %45, ptr %11, align 4, !tbaa !5
  %46 = load i32, ptr %11, align 4, !tbaa !5
  %47 = load i32, ptr %6, align 4, !tbaa !5
  %48 = icmp sge i32 %46, %47
  br i1 %48, label %49, label %54

49:                                               ; preds = %43
  %50 = load ptr, ptr %8, align 8, !tbaa !9
  %51 = call noundef ptr @_Z2goP4nodei(ptr noundef %50, i32 noundef 0)
  store ptr %51, ptr %8, align 8, !tbaa !9
  %52 = load ptr, ptr %9, align 8, !tbaa !9
  %53 = call noundef ptr @_Z2goP4nodei(ptr noundef %52, i32 noundef 0)
  store ptr %53, ptr %9, align 8, !tbaa !9
  br label %64

54:                                               ; preds = %43
  %55 = load i32, ptr %11, align 4, !tbaa !5
  %56 = load i32, ptr %6, align 4, !tbaa !5
  %57 = sub nsw i32 %56, %55
  store i32 %57, ptr %6, align 4, !tbaa !5
  %58 = load ptr, ptr %8, align 8, !tbaa !9
  %59 = call noundef ptr @_Z2goP4nodei(ptr noundef %58, i32 noundef 1)
  store ptr %59, ptr %8, align 8, !tbaa !9
  %60 = load ptr, ptr %9, align 8, !tbaa !9
  %61 = call noundef ptr @_Z2goP4nodei(ptr noundef %60, i32 noundef 1)
  store ptr %61, ptr %9, align 8, !tbaa !9
  %62 = load i32, ptr %7, align 4, !tbaa !5
  %63 = or i32 %62, 1
  store i32 %63, ptr %7, align 4, !tbaa !5
  br label %64

64:                                               ; preds = %54, %49
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  br label %65

65:                                               ; preds = %64
  %66 = load i32, ptr %10, align 4, !tbaa !5
  %67 = add nsw i32 %66, 1
  store i32 %67, ptr %10, align 4, !tbaa !5
  br label %22, !llvm.loop !11

68:                                               ; preds = %25
  %69 = load i32, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  ret i32 %69
}

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 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


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

@roots1 = external dso_local local_unnamed_addr global [500100 x ptr], align 16

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZN4node4lcntEv(ptr noundef nonnull align 8 dereferenceable(20)) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z6query3iii(i32 noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = add nsw i32 %0, -1
  %5 = sext i32 %4 to i64
  %6 = getelementptr inbounds [500100 x ptr], ptr @roots1, i64 0, i64 %5
  %7 = load ptr, ptr %6, align 8, !tbaa !5
  %8 = sext i32 %1 to i64
  %9 = getelementptr inbounds [500100 x ptr], ptr @roots1, i64 0, i64 %8
  %10 = load ptr, ptr %9, align 8, !tbaa !5
  br label %12

11:                                               ; preds = %33
  ret i32 %.125

12:                                               ; preds = %3, %33
  %.031 = phi i32 [ %2, %3 ], [ %.1, %33 ]
  %.01930 = phi i32 [ 0, %3 ], [ %34, %33 ]
  %.02029 = phi ptr [ %10, %3 ], [ %.121, %33 ]
  %.02228 = phi ptr [ %7, %3 ], [ %.123, %33 ]
  %.02427 = phi i32 [ 0, %3 ], [ %.125, %33 ]
  %13 = shl i32 %.02427, 1
  %14 = icmp eq ptr %.02029, null
  br i1 %14, label %17, label %15

15:                                               ; preds = %12
  %16 = tail call noundef i32 @_ZN4node4lcntEv(ptr noundef nonnull align 8 dereferenceable(20) %.02029)
  br label %17

17:                                               ; preds = %12, %15
  %18 = phi i32 [ %16, %15 ], [ 0, %12 ]
  %19 = icmp eq ptr %.02228, null
  br i1 %19, label %22, label %20

20:                                               ; preds = %17
  %21 = tail call noundef i32 @_ZN4node4lcntEv(ptr noundef nonnull align 8 dereferenceable(20) %.02228)
  br label %22

22:                                               ; preds = %17, %20
  %23 = phi i32 [ %21, %20 ], [ 0, %17 ]
  %24 = sub nsw i32 %18, %23
  %.not = icmp slt i32 %24, %.031
  br i1 %.not, label %28, label %25

25:                                               ; preds = %22
  %26 = tail call noundef ptr @_Z2goP4nodei(ptr noundef %.02228, i32 noundef 0)
  %27 = tail call noundef ptr @_Z2goP4nodei(ptr noundef %.02029, i32 noundef 0)
  br label %33

28:                                               ; preds = %22
  %29 = sub nsw i32 %.031, %24
  %30 = tail call noundef ptr @_Z2goP4nodei(ptr noundef %.02228, i32 noundef 1)
  %31 = tail call noundef ptr @_Z2goP4nodei(ptr noundef %.02029, i32 noundef 1)
  %32 = or disjoint i32 %13, 1
  br label %33

33:                                               ; preds = %28, %25
  %.125 = phi i32 [ %13, %25 ], [ %32, %28 ]
  %.123 = phi ptr [ %26, %25 ], [ %30, %28 ]
  %.121 = phi ptr [ %27, %25 ], [ %31, %28 ]
  %.1 = phi i32 [ %.031, %25 ], [ %29, %28 ]
  %34 = add nuw nsw i32 %.01930, 1
  %exitcond.not = icmp eq i32 %34, 30
  br i1 %exitcond.not, label %11, label %12, !llvm.loop !9
}

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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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