; LLVM IR from tangjz/acm-icpc :: codeforces/gym/100959G.ll (Z3dfsi4Node)

; 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, [5 x i32], [5 x i32] }

@n = external dso_local global i32, align 4
@tot = external dso_local global i32, align 4
@seq = external dso_local global [100001 x %struct.Node], align 16
@pat = external dso_local global %struct.Node, align 4

; 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) #0

; 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

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z4calcii(i32 noundef, i32 noundef) #2

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z3dfsi4Node(i32 noundef %0, ptr noundef byval(%struct.Node) align 8 %1) #2 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca %struct.Node, align 4
  %8 = alloca %struct.Node, align 8
  store i32 %0, ptr %3, align 4, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 @pat, ptr align 8 %1, i64 44, i1 false), !tbaa.struct !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  %9 = load i32, ptr @n, align 4, !tbaa !5
  %10 = add nsw i32 %9, 2
  %11 = call noundef i32 @_Z4calcii(i32 noundef 0, i32 noundef %10)
  store i32 %11, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  %12 = load i32, ptr %3, align 4, !tbaa !5
  %13 = add nsw i32 %12, 1
  store i32 %13, ptr %5, align 4, !tbaa !5
  br label %14

14:                                               ; preds = %40, %2
  %15 = load i32, ptr %5, align 4, !tbaa !5
  %16 = load i32, ptr @tot, align 4, !tbaa !5
  %17 = icmp slt i32 %15, %16
  br i1 %17, label %19, label %18

18:                                               ; preds = %14
  store i32 2, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  br label %43

19:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 44, ptr %7) #4
  %20 = load i32, ptr %5, align 4, !tbaa !5
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [100001 x %struct.Node], ptr @seq, i64 0, i64 %21
  call void @_ZNK4Node9intersectERKS_(ptr dead_on_unwind writable sret(%struct.Node) align 4 %7, ptr noundef nonnull align 4 dereferenceable(44) %1, ptr noundef nonnull align 4 dereferenceable(44) %22)
  %23 = getelementptr inbounds %struct.Node, ptr %7, i32 0, i32 0
  %24 = load i32, ptr %23, align 4, !tbaa !11
  %25 = icmp eq i32 %24, -1
  br i1 %25, label %26, label %27

26:                                               ; preds = %19
  store i32 4, ptr %6, align 4
  br label %37

27:                                               ; preds = %19
  %28 = load i32, ptr %5, align 4, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %8, ptr align 4 %7, i64 44, i1 false), !tbaa.struct !9
  %29 = call noundef i32 @_Z3dfsi4Node(i32 noundef %28, ptr noundef byval(%struct.Node) align 8 %8)
  %30 = load i32, ptr %4, align 4, !tbaa !5
  %31 = sub nsw i32 %30, %29
  store i32 %31, ptr %4, align 4, !tbaa !5
  %32 = icmp slt i32 %31, 0
  br i1 %32, label %33, label %36

33:                                               ; preds = %27
  %34 = load i32, ptr %4, align 4, !tbaa !5
  %35 = add nsw i32 %34, 1000000007
  store i32 %35, ptr %4, align 4, !tbaa !5
  br label %36

36:                                               ; preds = %33, %27
  store i32 0, ptr %6, align 4
  br label %37

37:                                               ; preds = %36, %26
  call void @llvm.lifetime.end.p0(i64 44, ptr %7) #4
  %38 = load i32, ptr %6, align 4
  switch i32 %38, label %45 [
    i32 0, label %39
    i32 4, label %40
  ]

39:                                               ; preds = %37
  br label %40

40:                                               ; preds = %39, %37
  %41 = load i32, ptr %5, align 4, !tbaa !5
  %42 = add nsw i32 %41, 1
  store i32 %42, ptr %5, align 4, !tbaa !5
  br label %14, !llvm.loop !13

43:                                               ; preds = %18
  %44 = load i32, ptr %4, align 4, !tbaa !5
  store i32 1, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  ret i32 %44

45:                                               ; preds = %37
  unreachable
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZNK4Node9intersectERKS_(ptr dead_on_unwind noalias writable sret(%struct.Node) align 4, ptr noundef nonnull align 4 dereferenceable(44), ptr noundef nonnull align 4 dereferenceable(44)) #3 align 2

attributes #0 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 #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 = !{i64 0, i64 4, !5, i64 4, i64 20, !10, i64 24, i64 20, !10}
!10 = !{!7, !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"_ZTS4Node", !6, i64 0, !7, i64 4, !7, i64 24}
!13 = distinct !{!13, !14}
!14 = !{!"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"

%struct.Node = type { i32, [5 x i32], [5 x i32] }

@n = external dso_local local_unnamed_addr global i32, align 4
@tot = external dso_local local_unnamed_addr global i32, align 4
@seq = external dso_local global [100001 x %struct.Node], align 16
@pat = external dso_local local_unnamed_addr global %struct.Node, align 4

; 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) #0

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

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

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z4calcii(i32 noundef, i32 noundef) local_unnamed_addr #2

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z3dfsi4Node(i32 noundef %0, ptr noundef byval(%struct.Node) align 8 %1) local_unnamed_addr #2 {
  %3 = alloca %struct.Node, align 8
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(44) @pat, ptr noundef nonnull align 8 dereferenceable(44) %1, i64 44, i1 false), !tbaa.struct !5
  %4 = load i32, ptr @n, align 4, !tbaa !6
  %5 = add nsw i32 %4, 2
  %6 = tail call noundef i32 @_Z4calcii(i32 noundef 0, i32 noundef %5)
  %.011 = add nsw i32 %0, 1
  %7 = load i32, ptr @tot, align 4, !tbaa !6
  %8 = icmp slt i32 %.011, %7
  br i1 %8, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %2
  %9 = sext i32 %.011 to i64
  br label %.lr.ph

._crit_edge:                                      ; preds = %19, %2
  %.08.lcssa = phi i32 [ %6, %2 ], [ %.2, %19 ]
  ret i32 %.08.lcssa

.lr.ph:                                           ; preds = %.lr.ph.preheader, %19
  %indvars.iv = phi i64 [ %9, %.lr.ph.preheader ], [ %indvars.iv.next, %19 ]
  %.0812 = phi i32 [ %6, %.lr.ph.preheader ], [ %.2, %19 ]
  call void @llvm.lifetime.start.p0(i64 44, ptr nonnull %3) #4
  %10 = getelementptr inbounds [100001 x %struct.Node], ptr @seq, i64 0, i64 %indvars.iv
  call void @_ZNK4Node9intersectERKS_(ptr dead_on_unwind nonnull writable sret(%struct.Node) align 4 %3, ptr noundef nonnull align 4 dereferenceable(44) %1, ptr noundef nonnull align 4 dereferenceable(44) %10)
  %11 = load i32, ptr %3, align 8, !tbaa !11
  %12 = icmp eq i32 %11, -1
  br i1 %12, label %19, label %13

13:                                               ; preds = %.lr.ph
  %14 = trunc nsw i64 %indvars.iv to i32
  %15 = call noundef i32 @_Z3dfsi4Node(i32 noundef %14, ptr noundef nonnull byval(%struct.Node) align 8 %3)
  %16 = sub nsw i32 %.0812, %15
  %17 = icmp slt i32 %16, 0
  %18 = add nsw i32 %16, 1000000007
  %spec.select = select i1 %17, i32 %18, i32 %16
  br label %19

19:                                               ; preds = %.lr.ph, %13
  %.2 = phi i32 [ %spec.select, %13 ], [ %.0812, %.lr.ph ]
  call void @llvm.lifetime.end.p0(i64 44, ptr nonnull %3) #4
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %20 = load i32, ptr @tot, align 4, !tbaa !6
  %21 = sext i32 %20 to i64
  %22 = icmp slt i64 %indvars.iv.next, %21
  br i1 %22, label %.lr.ph, label %._crit_edge, !llvm.loop !13
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZNK4Node9intersectERKS_(ptr dead_on_unwind noalias writable sret(%struct.Node) align 4, ptr noundef nonnull align 4 dereferenceable(44), ptr noundef nonnull align 4 dereferenceable(44)) local_unnamed_addr #3 align 2

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


; Effective O3 passes: DSEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,MemCpyOptPass,SROAPass,SimplifyCFGPass,TailCallElimPass