; LLVM IR from Tunghohin/Competitive_Coding :: Uva/uva11992.ll (Z5queryiiiP4Node)

; 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 { i32, i32, i64, i32, i64, i32, i32 }

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z7push_upR4NodeS0_S0_(ptr noundef nonnull align 8 dereferenceable(40), ptr noundef nonnull align 8 dereferenceable(40), ptr noundef nonnull align 8 dereferenceable(40)) #0

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_Z9push_downiP4Node(i32 noundef, ptr noundef) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #2

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

; Function Attrs: mustprogress uwtable
define dso_local void @_Z5queryiiiP4Node(ptr dead_on_unwind noalias writable sret(%struct.Node) align 8 %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, ptr noundef %4) #0 {
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca %struct.Node, align 8
  %13 = alloca %struct.Node, align 8
  store i32 %1, ptr %6, align 4, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !5
  store i32 %3, ptr %8, align 4, !tbaa !5
  store ptr %4, ptr %9, align 8, !tbaa !9
  %14 = load ptr, ptr %9, align 8, !tbaa !9
  %15 = load i32, ptr %6, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds %struct.Node, ptr %14, i64 %16
  %18 = getelementptr inbounds %struct.Node, ptr %17, i32 0, i32 0
  %19 = load i32, ptr %18, align 8, !tbaa !11
  %20 = load i32, ptr %7, align 4, !tbaa !5
  %21 = icmp sge i32 %19, %20
  br i1 %21, label %22, label %36

22:                                               ; preds = %5
  %23 = load ptr, ptr %9, align 8, !tbaa !9
  %24 = load i32, ptr %6, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds %struct.Node, ptr %23, i64 %25
  %27 = getelementptr inbounds %struct.Node, ptr %26, i32 0, i32 1
  %28 = load i32, ptr %27, align 4, !tbaa !14
  %29 = load i32, ptr %8, align 4, !tbaa !5
  %30 = icmp sle i32 %28, %29
  br i1 %30, label %31, label %36

31:                                               ; preds = %22
  %32 = load ptr, ptr %9, align 8, !tbaa !9
  %33 = load i32, ptr %6, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds %struct.Node, ptr %32, i64 %34
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %0, ptr align 8 %35, i64 40, i1 false), !tbaa.struct !15
  br label %86

36:                                               ; preds = %22, %5
  %37 = load i32, ptr %6, align 4, !tbaa !5
  %38 = load ptr, ptr %9, align 8, !tbaa !9
  call void @_Z9push_downiP4Node(i32 noundef %37, ptr noundef %38)
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  %39 = load ptr, ptr %9, align 8, !tbaa !9
  %40 = load i32, ptr %6, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds %struct.Node, ptr %39, i64 %41
  %43 = getelementptr inbounds %struct.Node, ptr %42, i32 0, i32 0
  %44 = load i32, ptr %43, align 8, !tbaa !11
  %45 = load ptr, ptr %9, align 8, !tbaa !9
  %46 = load i32, ptr %6, align 4, !tbaa !5
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds %struct.Node, ptr %45, i64 %47
  %49 = getelementptr inbounds %struct.Node, ptr %48, i32 0, i32 1
  %50 = load i32, ptr %49, align 4, !tbaa !14
  %51 = add nsw i32 %44, %50
  %52 = ashr i32 %51, 1
  store i32 %52, ptr %10, align 4, !tbaa !5
  %53 = load i32, ptr %8, align 4, !tbaa !5
  %54 = load i32, ptr %10, align 4, !tbaa !5
  %55 = icmp sle i32 %53, %54
  br i1 %55, label %56, label %62

56:                                               ; preds = %36
  %57 = load i32, ptr %6, align 4, !tbaa !5
  %58 = shl i32 %57, 1
  %59 = load i32, ptr %7, align 4, !tbaa !5
  %60 = load i32, ptr %8, align 4, !tbaa !5
  %61 = load ptr, ptr %9, align 8, !tbaa !9
  call void @_Z5queryiiiP4Node(ptr dead_on_unwind writable sret(%struct.Node) align 8 %0, i32 noundef %58, i32 noundef %59, i32 noundef %60, ptr noundef %61)
  store i32 1, ptr %11, align 4
  br label %85

62:                                               ; preds = %36
  %63 = load i32, ptr %7, align 4, !tbaa !5
  %64 = load i32, ptr %10, align 4, !tbaa !5
  %65 = icmp sgt i32 %63, %64
  br i1 %65, label %66, label %73

66:                                               ; preds = %62
  %67 = load i32, ptr %6, align 4, !tbaa !5
  %68 = shl i32 %67, 1
  %69 = or i32 %68, 1
  %70 = load i32, ptr %7, align 4, !tbaa !5
  %71 = load i32, ptr %8, align 4, !tbaa !5
  %72 = load ptr, ptr %9, align 8, !tbaa !9
  call void @_Z5queryiiiP4Node(ptr dead_on_unwind writable sret(%struct.Node) align 8 %0, i32 noundef %69, i32 noundef %70, i32 noundef %71, ptr noundef %72)
  store i32 1, ptr %11, align 4
  br label %85

73:                                               ; preds = %62
  call void @llvm.lifetime.start.p0(i64 40, ptr %12) #4
  %74 = load i32, ptr %6, align 4, !tbaa !5
  %75 = shl i32 %74, 1
  %76 = load i32, ptr %7, align 4, !tbaa !5
  %77 = load i32, ptr %8, align 4, !tbaa !5
  %78 = load ptr, ptr %9, align 8, !tbaa !9
  call void @_Z5queryiiiP4Node(ptr dead_on_unwind writable sret(%struct.Node) align 8 %12, i32 noundef %75, i32 noundef %76, i32 noundef %77, ptr noundef %78)
  call void @llvm.lifetime.start.p0(i64 40, ptr %13) #4
  %79 = load i32, ptr %6, align 4, !tbaa !5
  %80 = shl i32 %79, 1
  %81 = or i32 %80, 1
  %82 = load i32, ptr %7, align 4, !tbaa !5
  %83 = load i32, ptr %8, align 4, !tbaa !5
  %84 = load ptr, ptr %9, align 8, !tbaa !9
  call void @_Z5queryiiiP4Node(ptr dead_on_unwind writable sret(%struct.Node) align 8 %13, i32 noundef %81, i32 noundef %82, i32 noundef %83, ptr noundef %84)
  call void @_Z7push_upR4NodeS0_S0_(ptr noundef nonnull align 8 dereferenceable(40) %0, ptr noundef nonnull align 8 dereferenceable(40) %12, ptr noundef nonnull align 8 dereferenceable(40) %13)
  store i32 1, ptr %11, align 4
  call void @llvm.lifetime.end.p0(i64 40, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 40, ptr %12) #4
  br label %85

85:                                               ; preds = %73, %66, %56
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  br label %86

86:                                               ; preds = %85, %31
  ret void
}

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 = { 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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { 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 = !{!12, !6, i64 0}
!12 = !{!"_ZTS4Node", !6, i64 0, !6, i64 4, !13, i64 8, !6, i64 16, !13, i64 24, !6, i64 32, !6, i64 36}
!13 = !{!"long long", !7, i64 0}
!14 = !{!12, !6, i64 4}
!15 = !{i64 0, i64 4, !5, i64 4, i64 4, !5, i64 8, i64 8, !16, i64 16, i64 4, !5, i64 24, i64 8, !16, i64 32, i64 4, !5, i64 36, i64 4, !5}
!16 = !{!13, !13, 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 { i32, i32, i64, i32, i64, i32, i32 }

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z7push_upR4NodeS0_S0_(ptr noundef nonnull align 8 dereferenceable(40), ptr noundef nonnull align 8 dereferenceable(40), ptr noundef nonnull align 8 dereferenceable(40)) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_Z9push_downiP4Node(i32 noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #2

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #2

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

; Function Attrs: mustprogress uwtable
define dso_local void @_Z5queryiiiP4Node(ptr dead_on_unwind noalias writable sret(%struct.Node) align 8 %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, ptr noundef %4) local_unnamed_addr #0 {
  %6 = alloca %struct.Node, align 8
  %7 = alloca %struct.Node, align 8
  br label %tailrecurse

tailrecurse:                                      ; preds = %tailrecurse.backedge, %5
  %.tr38 = phi i32 [ %1, %5 ], [ %.tr38.be, %tailrecurse.backedge ]
  %8 = sext i32 %.tr38 to i64
  %9 = getelementptr inbounds %struct.Node, ptr %4, i64 %8
  %10 = load i32, ptr %9, align 8, !tbaa !5
  %.not = icmp slt i32 %10, %2
  br i1 %.not, label %15, label %11

11:                                               ; preds = %tailrecurse
  %12 = getelementptr inbounds i8, ptr %9, i64 4
  %13 = load i32, ptr %12, align 4, !tbaa !11
  %.not36 = icmp sgt i32 %13, %3
  br i1 %.not36, label %15, label %14

14:                                               ; preds = %11
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(40) %0, ptr noundef nonnull align 8 dereferenceable(40) %9, i64 40, i1 false), !tbaa.struct !12
  br label %31

15:                                               ; preds = %11, %tailrecurse
  tail call void @_Z9push_downiP4Node(i32 noundef %.tr38, ptr noundef nonnull %4)
  %16 = load i32, ptr %9, align 8, !tbaa !5
  %17 = getelementptr inbounds i8, ptr %9, i64 4
  %18 = load i32, ptr %17, align 4, !tbaa !11
  %19 = add nsw i32 %18, %16
  %20 = ashr i32 %19, 1
  %.not37 = icmp slt i32 %20, %3
  br i1 %.not37, label %23, label %21

21:                                               ; preds = %15
  %22 = shl i32 %.tr38, 1
  br label %tailrecurse.backedge

tailrecurse.backedge:                             ; preds = %21, %25
  %.tr38.be = phi i32 [ %22, %21 ], [ %27, %25 ]
  br label %tailrecurse

23:                                               ; preds = %15
  %24 = icmp slt i32 %20, %2
  br i1 %24, label %25, label %28

25:                                               ; preds = %23
  %26 = shl i32 %.tr38, 1
  %27 = or disjoint i32 %26, 1
  br label %tailrecurse.backedge

28:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 40, ptr nonnull %6) #4
  %29 = shl i32 %.tr38, 1
  call void @_Z5queryiiiP4Node(ptr dead_on_unwind nonnull writable sret(%struct.Node) align 8 %6, i32 noundef %29, i32 noundef %2, i32 noundef %3, ptr noundef nonnull %4)
  call void @llvm.lifetime.start.p0(i64 40, ptr nonnull %7) #4
  %30 = or disjoint i32 %29, 1
  call void @_Z5queryiiiP4Node(ptr dead_on_unwind nonnull writable sret(%struct.Node) align 8 %7, i32 noundef %30, i32 noundef %2, i32 noundef %3, ptr noundef nonnull %4)
  call void @_Z7push_upR4NodeS0_S0_(ptr noundef nonnull align 8 dereferenceable(40) %0, ptr noundef nonnull align 8 dereferenceable(40) %6, ptr noundef nonnull align 8 dereferenceable(40) %7)
  call void @llvm.lifetime.end.p0(i64 40, ptr nonnull %7) #4
  call void @llvm.lifetime.end.p0(i64 40, ptr nonnull %6) #4
  br label %31

31:                                               ; preds = %28, %14
  ret void
}

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 = { 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 nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { 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, !7, i64 0}
!6 = !{!"_ZTS4Node", !7, i64 0, !7, i64 4, !10, i64 8, !7, i64 16, !10, i64 24, !7, i64 32, !7, i64 36}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"long long", !8, i64 0}
!11 = !{!6, !7, i64 4}
!12 = !{i64 0, i64 4, !13, i64 4, i64 4, !13, i64 8, i64 8, !14, i64 16, i64 4, !13, i64 24, i64 8, !14, i64 32, i64 4, !13, i64 36, i64 4, !13}
!13 = !{!7, !7, i64 0}
!14 = !{!10, !10, i64 0}


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