; LLVM IR from VastoLorde95/Competitive-Programming :: Codeforces/277 Division 2/Problem D/D.ll (Z3dfsiiii)

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

%"class.std::vector" = type { %"struct.std::_Vector_base" }
%"struct.std::_Vector_base" = type { %"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl" }
%"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl" = type { %"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl_data" }
%"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl_data" = type { ptr, ptr, ptr }

@d = external dso_local global i32, align 4
@a = external dso_local global [2001 x i32], align 16
@v = external dso_local global i32, align 4
@g = external dso_local global [2001 x %"class.std::vector"], align 16
@MOD = external dso_local global i64, align 8

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z3dfsiiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) #0 {
  %5 = alloca i64, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i64, align 8
  %11 = alloca i32, align 4
  store i32 %0, ptr %6, align 4, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !5
  store i32 %2, ptr %8, align 4, !tbaa !5
  store i32 %3, ptr %9, align 4, !tbaa !5
  %12 = load i32, ptr %6, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [2001 x i32], ptr @a, i64 0, i64 %13
  %15 = load i32, ptr %14, align 4, !tbaa !5
  %16 = load i32, ptr %8, align 4, !tbaa !5
  %17 = load i32, ptr @d, align 4, !tbaa !5
  %18 = add nsw i32 %16, %17
  %19 = icmp sgt i32 %15, %18
  br i1 %19, label %27, label %20

20:                                               ; preds = %4
  %21 = load i32, ptr %6, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [2001 x i32], ptr @a, i64 0, i64 %22
  %24 = load i32, ptr %23, align 4, !tbaa !5
  %25 = load i32, ptr %8, align 4, !tbaa !5
  %26 = icmp slt i32 %24, %25
  br i1 %26, label %27, label %28

27:                                               ; preds = %20, %4
  store i64 0, ptr %5, align 8
  br label %79

28:                                               ; preds = %20
  %29 = load i32, ptr %6, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [2001 x i32], ptr @a, i64 0, i64 %30
  %32 = load i32, ptr %31, align 4, !tbaa !5
  %33 = load i32, ptr %8, align 4, !tbaa !5
  %34 = icmp eq i32 %32, %33
  br i1 %34, label %35, label %40

35:                                               ; preds = %28
  %36 = load i32, ptr %6, align 4, !tbaa !5
  %37 = load i32, ptr %9, align 4, !tbaa !5
  %38 = icmp slt i32 %36, %37
  br i1 %38, label %39, label %40

39:                                               ; preds = %35
  store i64 0, ptr %5, align 8
  br label %79

40:                                               ; preds = %35, %28
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  store i64 1, ptr %10, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  store i32 0, ptr %11, align 4, !tbaa !5
  br label %41

41:                                               ; preds = %74, %40
  %42 = load i32, ptr %11, align 4, !tbaa !5
  %43 = sext i32 %42 to i64
  %44 = load i32, ptr %6, align 4, !tbaa !5
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [2001 x %"class.std::vector"], ptr @g, i64 0, i64 %45
  %47 = call noundef i64 @_ZNKSt6vectorIiSaIiEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(24) %46) #3
  %48 = icmp ult i64 %43, %47
  br i1 %48, label %50, label %49

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

50:                                               ; preds = %41
  %51 = load i32, ptr %6, align 4, !tbaa !5
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds [2001 x %"class.std::vector"], ptr @g, i64 0, i64 %52
  %54 = load i32, ptr %11, align 4, !tbaa !5
  %55 = sext i32 %54 to i64
  %56 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) %53, i64 noundef %55) #3
  %57 = load i32, ptr %56, align 4, !tbaa !5
  store i32 %57, ptr @v, align 4, !tbaa !5
  %58 = load i32, ptr @v, align 4, !tbaa !5
  %59 = load i32, ptr %7, align 4, !tbaa !5
  %60 = icmp ne i32 %58, %59
  br i1 %60, label %61, label %73

61:                                               ; preds = %50
  %62 = load i32, ptr @v, align 4, !tbaa !5
  %63 = load i32, ptr %6, align 4, !tbaa !5
  %64 = load i32, ptr %8, align 4, !tbaa !5
  %65 = load i32, ptr %9, align 4, !tbaa !5
  %66 = call noundef i64 @_Z3dfsiiii(i32 noundef %62, i32 noundef %63, i32 noundef %64, i32 noundef %65)
  %67 = add nsw i64 1, %66
  %68 = load i64, ptr %10, align 8, !tbaa !9
  %69 = mul nsw i64 %68, %67
  store i64 %69, ptr %10, align 8, !tbaa !9
  %70 = load i64, ptr @MOD, align 8, !tbaa !9
  %71 = load i64, ptr %10, align 8, !tbaa !9
  %72 = srem i64 %71, %70
  store i64 %72, ptr %10, align 8, !tbaa !9
  br label %73

73:                                               ; preds = %61, %50
  br label %74

74:                                               ; preds = %73
  %75 = load i32, ptr %11, align 4, !tbaa !5
  %76 = add nsw i32 %75, 1
  store i32 %76, ptr %11, align 4, !tbaa !5
  br label %41, !llvm.loop !11

77:                                               ; preds = %49
  %78 = load i64, ptr %10, align 8, !tbaa !9
  store i64 %78, ptr %5, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  br label %79

79:                                               ; preds = %77, %39, %27
  %80 = load i64, ptr %5, align 8
  ret i64 %80
}

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZNKSt6vectorIiSaIiEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(24)) #2 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24), i64 noundef) #2 align 2

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

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #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}
!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"

%"class.std::vector" = type { %"struct.std::_Vector_base" }
%"struct.std::_Vector_base" = type { %"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl" }
%"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl" = type { %"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl_data" }
%"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl_data" = type { ptr, ptr, ptr }

@d = external dso_local local_unnamed_addr global i32, align 4
@a = external dso_local local_unnamed_addr global [2001 x i32], align 16
@v = external dso_local local_unnamed_addr global i32, align 4
@g = external dso_local global [2001 x %"class.std::vector"], align 16
@MOD = external dso_local local_unnamed_addr global i64, align 8

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z3dfsiiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = sext i32 %0 to i64
  %6 = getelementptr inbounds [2001 x i32], ptr @a, i64 0, i64 %5
  %7 = load i32, ptr %6, align 4, !tbaa !5
  %8 = load i32, ptr @d, align 4, !tbaa !5
  %9 = add nsw i32 %8, %2
  %10 = icmp sgt i32 %7, %9
  %11 = icmp slt i32 %7, %2
  %or.cond = or i1 %11, %10
  br i1 %or.cond, label %.loopexit, label %12

12:                                               ; preds = %4
  %13 = icmp eq i32 %7, %2
  %14 = icmp slt i32 %0, %3
  %or.cond27 = and i1 %14, %13
  br i1 %or.cond27, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %12
  %15 = getelementptr inbounds [2001 x %"class.std::vector"], ptr @g, i64 0, i64 %5
  %16 = tail call noundef i64 @_ZNKSt6vectorIiSaIiEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(24) %15) #1
  %.not30 = icmp eq i64 %16, 0
  br i1 %.not30, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %25
  %indvars.iv = phi i64 [ %indvars.iv.next, %25 ], [ 0, %.preheader ]
  %.02128 = phi i64 [ %.1, %25 ], [ 1, %.preheader ]
  %17 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) %15, i64 noundef %indvars.iv) #1
  %18 = load i32, ptr %17, align 4, !tbaa !5
  store i32 %18, ptr @v, align 4, !tbaa !5
  %.not = icmp eq i32 %18, %1
  br i1 %.not, label %25, label %19

19:                                               ; preds = %.lr.ph
  %20 = tail call noundef i64 @_Z3dfsiiii(i32 noundef %18, i32 noundef %0, i32 noundef %2, i32 noundef %3)
  %21 = add nsw i64 %20, 1
  %22 = mul nsw i64 %21, %.02128
  %23 = load i64, ptr @MOD, align 8, !tbaa !9
  %24 = srem i64 %22, %23
  br label %25

25:                                               ; preds = %.lr.ph, %19
  %.1 = phi i64 [ %24, %19 ], [ %.02128, %.lr.ph ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %26 = tail call noundef i64 @_ZNKSt6vectorIiSaIiEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(24) %15) #1
  %27 = icmp ugt i64 %26, %indvars.iv.next
  br i1 %27, label %.lr.ph, label %.loopexit, !llvm.loop !11

.loopexit:                                        ; preds = %25, %.preheader, %12, %4
  %.022 = phi i64 [ 0, %4 ], [ 0, %12 ], [ 1, %.preheader ], [ %.1, %25 ]
  ret i64 %.022
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZNKSt6vectorIiSaIiEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(24)) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24), i64 noundef) local_unnamed_addr #0 align 2

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 = { 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}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


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