; LLVM IR from ayush-tulsyan/ACM-ICPC-Handbook :: code/lazyProp.ll (Z5queryiiiii)

; 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 [404000 x i64], align 16
@lazy = external dso_local global [404000 x i64], 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 void @_Z9Propogateiii(i32 noundef, i32 noundef, i32 noundef) #1

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z5queryiiiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4) #2 {
  %6 = alloca i64, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  %16 = alloca i64, align 8
  %17 = alloca i64, align 8
  store i32 %0, ptr %7, align 4, !tbaa !5
  store i32 %1, ptr %8, align 4, !tbaa !5
  store i32 %2, ptr %9, align 4, !tbaa !5
  store i32 %3, ptr %10, align 4, !tbaa !5
  store i32 %4, ptr %11, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  %18 = load i32, ptr %10, align 4, !tbaa !5
  %19 = load i32, ptr %11, align 4, !tbaa !5
  %20 = add nsw i32 %18, %19
  %21 = ashr i32 %20, 1
  store i32 %21, ptr %12, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #3
  %22 = load i32, ptr %7, align 4, !tbaa !5
  %23 = shl i32 %22, 1
  store i32 %23, ptr %13, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #3
  %24 = load i32, ptr %13, align 4, !tbaa !5
  %25 = or i32 %24, 1
  store i32 %25, ptr %14, align 4, !tbaa !5
  %26 = load i32, ptr %7, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [404000 x i64], ptr @lazy, i64 0, i64 %27
  %29 = load i64, ptr %28, align 8, !tbaa !9
  %30 = icmp ne i64 %29, 0
  br i1 %30, label %31, label %35

31:                                               ; preds = %5
  %32 = load i32, ptr %7, align 4, !tbaa !5
  %33 = load i32, ptr %10, align 4, !tbaa !5
  %34 = load i32, ptr %11, align 4, !tbaa !5
  call void @_Z9Propogateiii(i32 noundef %32, i32 noundef %33, i32 noundef %34)
  br label %35

35:                                               ; preds = %31, %5
  %36 = load i32, ptr %10, align 4, !tbaa !5
  %37 = load i32, ptr %11, align 4, !tbaa !5
  %38 = icmp sgt i32 %36, %37
  br i1 %38, label %47, label %39

39:                                               ; preds = %35
  %40 = load i32, ptr %10, align 4, !tbaa !5
  %41 = load i32, ptr %9, align 4, !tbaa !5
  %42 = icmp sgt i32 %40, %41
  br i1 %42, label %47, label %43

43:                                               ; preds = %39
  %44 = load i32, ptr %11, align 4, !tbaa !5
  %45 = load i32, ptr %8, align 4, !tbaa !5
  %46 = icmp slt i32 %44, %45
  br i1 %46, label %47, label %48

47:                                               ; preds = %43, %39, %35
  store i64 0, ptr %6, align 8
  store i32 1, ptr %15, align 4
  br label %79

48:                                               ; preds = %43
  call void @llvm.lifetime.start.p0(i64 8, ptr %16) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %17) #3
  %49 = load i32, ptr %8, align 4, !tbaa !5
  %50 = load i32, ptr %10, align 4, !tbaa !5
  %51 = icmp sle i32 %49, %50
  br i1 %51, label %52, label %61

52:                                               ; preds = %48
  %53 = load i32, ptr %11, align 4, !tbaa !5
  %54 = load i32, ptr %9, align 4, !tbaa !5
  %55 = icmp sle i32 %53, %54
  br i1 %55, label %56, label %61

56:                                               ; preds = %52
  %57 = load i32, ptr %7, align 4, !tbaa !5
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds [404000 x i64], ptr @Tree, i64 0, i64 %58
  %60 = load i64, ptr %59, align 8, !tbaa !9
  store i64 %60, ptr %6, align 8
  store i32 1, ptr %15, align 4
  br label %78

61:                                               ; preds = %52, %48
  %62 = load i32, ptr %13, align 4, !tbaa !5
  %63 = load i32, ptr %8, align 4, !tbaa !5
  %64 = load i32, ptr %9, align 4, !tbaa !5
  %65 = load i32, ptr %10, align 4, !tbaa !5
  %66 = load i32, ptr %12, align 4, !tbaa !5
  %67 = call noundef i64 @_Z5queryiiiii(i32 noundef %62, i32 noundef %63, i32 noundef %64, i32 noundef %65, i32 noundef %66)
  store i64 %67, ptr %16, align 8, !tbaa !9
  %68 = load i32, ptr %14, align 4, !tbaa !5
  %69 = load i32, ptr %8, align 4, !tbaa !5
  %70 = load i32, ptr %9, align 4, !tbaa !5
  %71 = load i32, ptr %12, align 4, !tbaa !5
  %72 = add nsw i32 %71, 1
  %73 = load i32, ptr %11, align 4, !tbaa !5
  %74 = call noundef i64 @_Z5queryiiiii(i32 noundef %68, i32 noundef %69, i32 noundef %70, i32 noundef %72, i32 noundef %73)
  store i64 %74, ptr %17, align 8, !tbaa !9
  %75 = load i64, ptr %16, align 8, !tbaa !9
  %76 = load i64, ptr %17, align 8, !tbaa !9
  %77 = add nsw i64 %75, %76
  store i64 %77, ptr %6, align 8
  store i32 1, ptr %15, align 4
  br label %78

78:                                               ; preds = %61, %56
  call void @llvm.lifetime.end.p0(i64 8, ptr %17) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #3
  br label %79

79:                                               ; preds = %78, %47
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  %80 = load i64, ptr %6, align 8
  ret i64 %80
}

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

@Tree = external dso_local local_unnamed_addr global [404000 x i64], align 16
@lazy = external dso_local local_unnamed_addr global [404000 x i64], align 16

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z5queryiiiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4) local_unnamed_addr #0 {
  %invariant.smin = tail call i32 @llvm.smin.i32(i32 %4, i32 %2)
  %6 = icmp slt i32 %4, %1
  br i1 %6, label %tailrecurse.us, label %.split

tailrecurse.us:                                   ; preds = %5
  %7 = sext i32 %0 to i64
  %8 = getelementptr inbounds [404000 x i64], ptr @lazy, i64 0, i64 %7
  %9 = load i64, ptr %8, align 8, !tbaa !5
  %.not.us = icmp eq i64 %9, 0
  br i1 %.not.us, label %.loopexit, label %10

10:                                               ; preds = %tailrecurse.us
  tail call void @_Z9Propogateiii(i32 noundef %0, i32 noundef %3, i32 noundef %4)
  br label %.loopexit

.split:                                           ; preds = %5
  %.not37 = icmp sgt i32 %4, %2
  br i1 %.not37, label %tailrecurse.us47, label %tailrecurse

tailrecurse.us47:                                 ; preds = %.split, %20
  %accumulator.tr.us48 = phi i64 [ %23, %20 ], [ 0, %.split ]
  %.tr.us49 = phi i32 [ %14, %20 ], [ %0, %.split ]
  %.tr42.us50 = phi i32 [ %22, %20 ], [ %3, %.split ]
  %11 = add nsw i32 %.tr42.us50, %4
  %12 = ashr i32 %11, 1
  %13 = shl i32 %.tr.us49, 1
  %14 = or disjoint i32 %13, 1
  %15 = sext i32 %.tr.us49 to i64
  %16 = getelementptr inbounds [404000 x i64], ptr @lazy, i64 0, i64 %15
  %17 = load i64, ptr %16, align 8, !tbaa !5
  %.not.us51 = icmp eq i64 %17, 0
  br i1 %.not.us51, label %19, label %18

18:                                               ; preds = %tailrecurse.us47
  tail call void @_Z9Propogateiii(i32 noundef %.tr.us49, i32 noundef %.tr42.us50, i32 noundef %4)
  br label %19

19:                                               ; preds = %18, %tailrecurse.us47
  %or.cond.us = icmp sgt i32 %.tr42.us50, %invariant.smin
  br i1 %or.cond.us, label %.loopexit, label %20

20:                                               ; preds = %19
  %21 = tail call noundef i64 @_Z5queryiiiii(i32 noundef %13, i32 noundef %1, i32 noundef %2, i32 noundef %.tr42.us50, i32 noundef %12)
  %22 = add nsw i32 %12, 1
  %23 = add nsw i64 %21, %accumulator.tr.us48
  br label %tailrecurse.us47

tailrecurse:                                      ; preds = %.split, %37
  %accumulator.tr = phi i64 [ %40, %37 ], [ 0, %.split ]
  %.tr = phi i32 [ %27, %37 ], [ %0, %.split ]
  %.tr42 = phi i32 [ %39, %37 ], [ %3, %.split ]
  %24 = add nsw i32 %.tr42, %4
  %25 = ashr i32 %24, 1
  %26 = shl i32 %.tr, 1
  %27 = or disjoint i32 %26, 1
  %28 = sext i32 %.tr to i64
  %29 = getelementptr inbounds [404000 x i64], ptr @lazy, i64 0, i64 %28
  %30 = load i64, ptr %29, align 8, !tbaa !5
  %.not = icmp eq i64 %30, 0
  br i1 %.not, label %32, label %31

31:                                               ; preds = %tailrecurse
  tail call void @_Z9Propogateiii(i32 noundef %.tr, i32 noundef %.tr42, i32 noundef %4)
  br label %32

32:                                               ; preds = %31, %tailrecurse
  %or.cond = icmp sgt i32 %.tr42, %invariant.smin
  br i1 %or.cond, label %.loopexit, label %33

33:                                               ; preds = %32
  %.not36 = icmp slt i32 %.tr42, %1
  br i1 %.not36, label %37, label %34

34:                                               ; preds = %33
  %35 = getelementptr inbounds [404000 x i64], ptr @Tree, i64 0, i64 %28
  %36 = load i64, ptr %35, align 8, !tbaa !5
  br label %.loopexit

37:                                               ; preds = %33
  %38 = tail call noundef i64 @_Z5queryiiiii(i32 noundef %26, i32 noundef %1, i32 noundef %2, i32 noundef %.tr42, i32 noundef %25)
  %39 = add nsw i32 %25, 1
  %40 = add nsw i64 %38, %accumulator.tr
  br label %tailrecurse

.loopexit:                                        ; preds = %32, %19, %10, %tailrecurse.us, %34
  %accumulator.tr46 = phi i64 [ %accumulator.tr, %34 ], [ 0, %tailrecurse.us ], [ 0, %10 ], [ %accumulator.tr.us48, %19 ], [ %accumulator.tr, %32 ]
  %.1 = phi i64 [ %36, %34 ], [ 0, %tailrecurse.us ], [ 0, %10 ], [ 0, %19 ], [ 0, %32 ]
  %accumulator.ret.tr = add nsw i64 %.1, %accumulator.tr46
  ret i64 %accumulator.ret.tr
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smin.i32(i32, i32) #1

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 speculatable willreturn memory(none) }

!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,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopInstSimplifyPass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass