; LLVM IR from Aryansh-S/USACO :: USACO/2018/Dec/Gold/teamwork.ll (Z5solvei)

; 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.SEG = type { i32, i32, %"class.std::vector" }
%"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 }

@n = external dso_local global i32, align 4
@k = external dso_local global i32, align 4
@dp = external dso_local global [200005 x i32], align 16
@cow = external dso_local global %struct.SEG, align 8

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z5solvei(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  %6 = load i32, ptr %3, align 4, !tbaa !5
  %7 = sext i32 %6 to i64
  %8 = getelementptr inbounds [200005 x i32], ptr @dp, i64 0, i64 %7
  %9 = load i32, ptr %8, align 4, !tbaa !5
  %10 = icmp ne i32 %9, 0
  br i1 %10, label %11, label %16

11:                                               ; preds = %1
  %12 = load i32, ptr %3, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [200005 x i32], ptr @dp, i64 0, i64 %13
  %15 = load i32, ptr %14, align 4, !tbaa !5
  store i32 %15, ptr %2, align 4
  br label %53

16:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  store i32 1, ptr %4, align 4, !tbaa !5
  br label %17

17:                                               ; preds = %45, %16
  %18 = load i32, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  %19 = load i32, ptr @n, align 4, !tbaa !5
  %20 = load i32, ptr %3, align 4, !tbaa !5
  %21 = sub nsw i32 %19, %20
  store i32 %21, ptr %5, align 4, !tbaa !5
  %22 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %5, ptr noundef nonnull align 4 dereferenceable(4) @k)
  %23 = load i32, ptr %22, align 4, !tbaa !5
  %24 = add nsw i32 %23, 1
  %25 = icmp slt i32 %18, %24
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  br i1 %25, label %27, label %26

26:                                               ; preds = %17
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  br label %48

27:                                               ; preds = %17
  %28 = load i32, ptr %3, align 4, !tbaa !5
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [200005 x i32], ptr @dp, i64 0, i64 %29
  %31 = load i32, ptr %3, align 4, !tbaa !5
  %32 = load i32, ptr %3, align 4, !tbaa !5
  %33 = load i32, ptr %4, align 4, !tbaa !5
  %34 = add nsw i32 %32, %33
  %35 = sub nsw i32 %34, 1
  %36 = call noundef i32 @_ZN3SEG3qryEii(ptr noundef nonnull align 8 dereferenceable(32) @cow, i32 noundef %31, i32 noundef %35)
  %37 = load i32, ptr %4, align 4, !tbaa !5
  %38 = mul nsw i32 %36, %37
  %39 = load i32, ptr %3, align 4, !tbaa !5
  %40 = load i32, ptr %4, align 4, !tbaa !5
  %41 = add nsw i32 %39, %40
  %42 = call noundef i32 @_Z5solvei(i32 noundef %41)
  %43 = add nsw i32 %38, %42
  %44 = call noundef zeroext i1 @_Z5ckmaxIiEbRT_S0_(ptr noundef nonnull align 4 dereferenceable(4) %30, i32 noundef %43)
  br label %45

45:                                               ; preds = %27
  %46 = load i32, ptr %4, align 4, !tbaa !5
  %47 = add nsw i32 %46, 1
  store i32 %47, ptr %4, align 4, !tbaa !5
  br label %17, !llvm.loop !9

48:                                               ; preds = %26
  %49 = load i32, ptr %3, align 4, !tbaa !5
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds [200005 x i32], ptr @dp, i64 0, i64 %50
  %52 = load i32, ptr %51, align 4, !tbaa !5
  store i32 %52, ptr %2, align 4
  br label %53

53:                                               ; preds = %48, %11
  %54 = load i32, ptr %2, align 4
  ret i32 %54
}

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) #2

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_Z5ckmaxIiEbRT_S0_(ptr noundef nonnull align 4 dereferenceable(4), i32 noundef) #3

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_ZN3SEG3qryEii(ptr noundef nonnull align 8 dereferenceable(32), i32 noundef, i32 noundef) #0 align 2

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 = { inlinehint 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 = { 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 = distinct !{!9, !10}
!10 = !{!"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.SEG = type { i32, i32, %"class.std::vector" }
%"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 }

@n = external dso_local local_unnamed_addr global i32, align 4
@k = external dso_local global i32, align 4
@dp = external dso_local global [200005 x i32], align 16
@cow = external dso_local global %struct.SEG, align 8

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z5solvei(i32 noundef %0) local_unnamed_addr #0 {
  %2 = alloca i32, align 4
  %3 = sext i32 %0 to i64
  %4 = getelementptr inbounds [200005 x i32], ptr @dp, i64 0, i64 %3
  %5 = load i32, ptr %4, align 4, !tbaa !5
  %.not = icmp eq i32 %5, 0
  br i1 %.not, label %.preheader, label %23

.preheader:                                       ; preds = %1
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %2) #4
  %6 = load i32, ptr @n, align 4, !tbaa !5
  %7 = sub nsw i32 %6, %0
  store i32 %7, ptr %2, align 4, !tbaa !5
  %8 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %2, ptr noundef nonnull align 4 dereferenceable(4) @k)
  %9 = load i32, ptr %8, align 4, !tbaa !5
  %.not1617 = icmp slt i32 %9, 1
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %2) #4
  br i1 %.not1617, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %.preheader
  %10 = load i32, ptr %4, align 4, !tbaa !5
  br label %23

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %.018 = phi i32 [ %18, %.lr.ph ], [ 1, %.preheader ]
  %11 = add nsw i32 %.018, %0
  %12 = add nsw i32 %11, -1
  %13 = call noundef i32 @_ZN3SEG3qryEii(ptr noundef nonnull align 8 dereferenceable(32) @cow, i32 noundef %0, i32 noundef %12)
  %14 = mul nsw i32 %13, %.018
  %15 = call noundef i32 @_Z5solvei(i32 noundef %11)
  %16 = add nsw i32 %14, %15
  %17 = call noundef zeroext i1 @_Z5ckmaxIiEbRT_S0_(ptr noundef nonnull align 4 dereferenceable(4) %4, i32 noundef %16)
  %18 = add nuw nsw i32 %.018, 1
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %2) #4
  %19 = load i32, ptr @n, align 4, !tbaa !5
  %20 = sub nsw i32 %19, %0
  store i32 %20, ptr %2, align 4, !tbaa !5
  %21 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %2, ptr noundef nonnull align 4 dereferenceable(4) @k)
  %22 = load i32, ptr %21, align 4, !tbaa !5
  %.not16.not = icmp slt i32 %.018, %22
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %2) #4
  br i1 %.not16.not, label %.lr.ph, label %._crit_edge, !llvm.loop !9

23:                                               ; preds = %1, %._crit_edge
  %.014 = phi i32 [ %10, %._crit_edge ], [ %5, %1 ]
  ret i32 %.014
}

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #2

; 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 nounwind uwtable
declare dso_local noundef zeroext i1 @_Z5ckmaxIiEbRT_S0_(ptr noundef nonnull align 4 dereferenceable(4), i32 noundef) local_unnamed_addr #3

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_ZN3SEG3qryEii(ptr noundef nonnull align 8 dereferenceable(32), i32 noundef, i32 noundef) local_unnamed_addr #0 align 2

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 nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { inlinehint 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 = { 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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass