; LLVM IR from bqi343/cp-notebook :: Contests/USACO Solutions/2015-16/Feb 2016/Plat/cbarn_lc_deque.ll (Z3updv)

; 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.LCdeque = type <{ %"class.std::deque", i32, [4 x i8] }>
%"class.std::deque" = type { %"class.std::_Deque_base" }
%"class.std::_Deque_base" = type { %"struct.std::_Deque_base<Line, std::allocator<Line>>::_Deque_impl" }
%"struct.std::_Deque_base<Line, std::allocator<Line>>::_Deque_impl" = type { %"struct.std::_Deque_base<Line, std::allocator<Line>>::_Deque_impl_data" }
%"struct.std::_Deque_base<Line, std::allocator<Line>>::_Deque_impl_data" = type { ptr, i64, %"struct.std::_Deque_iterator", %"struct.std::_Deque_iterator" }
%"struct.std::_Deque_iterator" = type { ptr, ptr, ptr, ptr }

@n = external dso_local global i32, align 4
@dp = external dso_local global [1001 x i64], align 16
@pre = external dso_local global [1001 x i64], align 16
@PRE = external dso_local global [1001 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

declare i32 @__gxx_personality_v0(...)

; Function Attrs: mustprogress uwtable
define dso_local void @_Z3updv() #1 personality ptr @__gxx_personality_v0 {
  %1 = alloca %struct.LCdeque, align 8
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 88, ptr %1) #4
  call void @_ZN7LCdequeC2Ev(ptr noundef nonnull align 8 dereferenceable(84) %1)
  %5 = getelementptr inbounds %struct.LCdeque, ptr %1, i32 0, i32 1
  store i32 1, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #4
  store i32 0, ptr %2, align 4, !tbaa !17
  br label %6

6:                                                ; preds = %59, %0
  %7 = load i32, ptr %2, align 4, !tbaa !17
  %8 = load i32, ptr @n, align 4, !tbaa !17
  %9 = add nsw i32 %8, 1
  %10 = icmp slt i32 %7, %9
  br i1 %10, label %12, label %11

11:                                               ; preds = %6
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #4
  br label %62

12:                                               ; preds = %6
  %13 = load i32, ptr %2, align 4, !tbaa !17
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [1001 x i64], ptr @dp, i64 0, i64 %14
  %16 = load i64, ptr %15, align 8, !tbaa !18
  %17 = icmp ne i64 %16, 1000000007
  br i1 %17, label %18, label %38

18:                                               ; preds = %12
  %19 = load i32, ptr %2, align 4, !tbaa !17
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [1001 x i64], ptr @pre, i64 0, i64 %20
  %22 = load i64, ptr %21, align 8, !tbaa !18
  %23 = sub nsw i64 0, %22
  %24 = load i32, ptr %2, align 4, !tbaa !17
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [1001 x i64], ptr @dp, i64 0, i64 %25
  %27 = load i64, ptr %26, align 8, !tbaa !18
  %28 = load i32, ptr %2, align 4, !tbaa !17
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [1001 x i64], ptr @PRE, i64 0, i64 %29
  %31 = load i64, ptr %30, align 8, !tbaa !18
  %32 = add nsw i64 %27, %31
  invoke void @_ZN7LCdeque3addExx(ptr noundef nonnull align 8 dereferenceable(84) %1, i64 noundef %23, i64 noundef %32)
          to label %33 unwind label %34

33:                                               ; preds = %18
  br label %38

34:                                               ; preds = %38, %18
  %35 = landingpad { ptr, i32 }
          cleanup
  %36 = extractvalue { ptr, i32 } %35, 0
  store ptr %36, ptr %3, align 8
  %37 = extractvalue { ptr, i32 } %35, 1
  store i32 %37, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #4
  call void @_ZNSt5dequeI4LineSaIS0_EED2Ev(ptr noundef nonnull align 8 dereferenceable(84) %1) #4
  call void @llvm.lifetime.end.p0(i64 88, ptr %1) #4
  br label %63

38:                                               ; preds = %33, %12
  %39 = load i32, ptr %2, align 4, !tbaa !17
  %40 = sext i32 %39 to i64
  %41 = invoke noundef i64 @_ZN7LCdeque5queryEx(ptr noundef nonnull align 8 dereferenceable(84) %1, i64 noundef %40)
          to label %42 unwind label %34

42:                                               ; preds = %38
  %43 = load i32, ptr %2, align 4, !tbaa !17
  %44 = sext i32 %43 to i64
  %45 = load i32, ptr %2, align 4, !tbaa !17
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [1001 x i64], ptr @pre, i64 0, i64 %46
  %48 = load i64, ptr %47, align 8, !tbaa !18
  %49 = mul nsw i64 %44, %48
  %50 = add nsw i64 %41, %49
  %51 = load i32, ptr %2, align 4, !tbaa !17
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds [1001 x i64], ptr @PRE, i64 0, i64 %52
  %54 = load i64, ptr %53, align 8, !tbaa !18
  %55 = sub nsw i64 %50, %54
  %56 = load i32, ptr %2, align 4, !tbaa !17
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds [1001 x i64], ptr @dp, i64 0, i64 %57
  store i64 %55, ptr %58, align 8, !tbaa !18
  br label %59

59:                                               ; preds = %42
  %60 = load i32, ptr %2, align 4, !tbaa !17
  %61 = add nsw i32 %60, 1
  store i32 %61, ptr %2, align 4, !tbaa !17
  br label %6, !llvm.loop !20

62:                                               ; preds = %11
  call void @_ZNSt5dequeI4LineSaIS0_EED2Ev(ptr noundef nonnull align 8 dereferenceable(84) %1) #4
  call void @llvm.lifetime.end.p0(i64 88, ptr %1) #4
  ret void

63:                                               ; preds = %34
  %64 = load ptr, ptr %3, align 8
  %65 = load i32, ptr %4, align 4
  %66 = insertvalue { ptr, i32 } poison, ptr %64, 0
  %67 = insertvalue { ptr, i32 } %66, i32 %65, 1
  resume { ptr, i32 } %67
}

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_ZN7LCdequeC2Ev(ptr noundef nonnull align 8 dereferenceable(84)) unnamed_addr #2 align 2

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN7LCdeque3addExx(ptr noundef nonnull align 8 dereferenceable(84), i64 noundef, i64 noundef) #1 align 2

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_ZN7LCdeque5queryEx(ptr noundef nonnull align 8 dereferenceable(84), i64 noundef) #1 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZNSt5dequeI4LineSaIS0_EED2Ev(ptr noundef nonnull align 8 dereferenceable(80)) unnamed_addr #3 align 2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { inlinehint 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, !16, i64 80}
!6 = !{!"_ZTS7LCdeque", !7, i64 0, !16, i64 80}
!7 = !{!"_ZTSSt5dequeI4LineSaIS0_EE", !8, i64 0}
!8 = !{!"_ZTSSt11_Deque_baseI4LineSaIS0_EE", !9, i64 0}
!9 = !{!"_ZTSNSt11_Deque_baseI4LineSaIS0_EE11_Deque_implE", !10, i64 0}
!10 = !{!"_ZTSNSt11_Deque_baseI4LineSaIS0_EE16_Deque_impl_dataE", !11, i64 0, !14, i64 8, !15, i64 16, !15, i64 48}
!11 = !{!"any pointer", !12, i64 0}
!12 = !{!"omnipotent char", !13, i64 0}
!13 = !{!"Simple C++ TBAA"}
!14 = !{!"long", !12, i64 0}
!15 = !{!"_ZTSSt15_Deque_iteratorI4LineRS0_PS0_E", !11, i64 0, !11, i64 8, !11, i64 16, !11, i64 24}
!16 = !{!"int", !12, i64 0}
!17 = !{!16, !16, i64 0}
!18 = !{!19, !19, i64 0}
!19 = !{!"long long", !12, i64 0}
!20 = distinct !{!20, !21}
!21 = !{!"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.LCdeque = type <{ %"class.std::deque", i32, [4 x i8] }>
%"class.std::deque" = type { %"class.std::_Deque_base" }
%"class.std::_Deque_base" = type { %"struct.std::_Deque_base<Line, std::allocator<Line>>::_Deque_impl" }
%"struct.std::_Deque_base<Line, std::allocator<Line>>::_Deque_impl" = type { %"struct.std::_Deque_base<Line, std::allocator<Line>>::_Deque_impl_data" }
%"struct.std::_Deque_base<Line, std::allocator<Line>>::_Deque_impl_data" = type { ptr, i64, %"struct.std::_Deque_iterator", %"struct.std::_Deque_iterator" }
%"struct.std::_Deque_iterator" = type { ptr, ptr, ptr, ptr }

@n = external dso_local local_unnamed_addr global i32, align 4
@dp = external dso_local local_unnamed_addr global [1001 x i64], align 16
@pre = external dso_local local_unnamed_addr global [1001 x i64], align 16
@PRE = external dso_local local_unnamed_addr global [1001 x i64], align 16

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

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

declare i32 @__gxx_personality_v0(...)

; Function Attrs: mustprogress uwtable
define dso_local void @_Z3updv() local_unnamed_addr #1 personality ptr @__gxx_personality_v0 {
  %1 = alloca %struct.LCdeque, align 8
  call void @llvm.lifetime.start.p0(i64 88, ptr nonnull %1) #4
  call void @_ZN7LCdequeC2Ev(ptr noundef nonnull align 8 dereferenceable(84) %1)
  %2 = getelementptr inbounds i8, ptr %1, i64 80
  store i32 1, ptr %2, align 8, !tbaa !5
  %3 = load i32, ptr @n, align 4, !tbaa !17
  %.not14 = icmp slt i32 %3, 0
  br i1 %.not14, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %17, %0
  call void @_ZNSt5dequeI4LineSaIS0_EED2Ev(ptr noundef nonnull align 8 dereferenceable(84) %1) #4
  call void @llvm.lifetime.end.p0(i64 88, ptr nonnull %1) #4
  ret void

.lr.ph:                                           ; preds = %0, %17
  %indvars.iv = phi i64 [ %indvars.iv.next, %17 ], [ 0, %0 ]
  %4 = getelementptr inbounds [1001 x i64], ptr @dp, i64 0, i64 %indvars.iv
  %5 = load i64, ptr %4, align 8, !tbaa !18
  %.not13 = icmp eq i64 %5, 1000000007
  br i1 %.not13, label %15, label %6

6:                                                ; preds = %.lr.ph
  %7 = getelementptr inbounds [1001 x i64], ptr @pre, i64 0, i64 %indvars.iv
  %8 = load i64, ptr %7, align 8, !tbaa !18
  %9 = sub nsw i64 0, %8
  %10 = getelementptr inbounds [1001 x i64], ptr @PRE, i64 0, i64 %indvars.iv
  %11 = load i64, ptr %10, align 8, !tbaa !18
  %12 = add nsw i64 %11, %5
  invoke void @_ZN7LCdeque3addExx(ptr noundef nonnull align 8 dereferenceable(84) %1, i64 noundef %9, i64 noundef %12)
          to label %15 unwind label %13

13:                                               ; preds = %15, %6
  %14 = landingpad { ptr, i32 }
          cleanup
  call void @_ZNSt5dequeI4LineSaIS0_EED2Ev(ptr noundef nonnull align 8 dereferenceable(84) %1) #4
  call void @llvm.lifetime.end.p0(i64 88, ptr nonnull %1) #4
  resume { ptr, i32 } %14

15:                                               ; preds = %6, %.lr.ph
  %16 = invoke noundef i64 @_ZN7LCdeque5queryEx(ptr noundef nonnull align 8 dereferenceable(84) %1, i64 noundef %indvars.iv)
          to label %17 unwind label %13

17:                                               ; preds = %15
  %18 = getelementptr inbounds [1001 x i64], ptr @pre, i64 0, i64 %indvars.iv
  %19 = load i64, ptr %18, align 8, !tbaa !18
  %20 = mul nsw i64 %19, %indvars.iv
  %21 = add nsw i64 %20, %16
  %22 = getelementptr inbounds [1001 x i64], ptr @PRE, i64 0, i64 %indvars.iv
  %23 = load i64, ptr %22, align 8, !tbaa !18
  %24 = sub i64 %21, %23
  store i64 %24, ptr %4, align 8, !tbaa !18
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %25 = load i32, ptr @n, align 4, !tbaa !17
  %26 = sext i32 %25 to i64
  %.not.not = icmp slt i64 %indvars.iv, %26
  br i1 %.not.not, label %.lr.ph, label %._crit_edge, !llvm.loop !20
}

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_ZN7LCdequeC2Ev(ptr noundef nonnull align 8 dereferenceable(84)) unnamed_addr #2 align 2

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN7LCdeque3addExx(ptr noundef nonnull align 8 dereferenceable(84), i64 noundef, i64 noundef) local_unnamed_addr #1 align 2

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_ZN7LCdeque5queryEx(ptr noundef nonnull align 8 dereferenceable(84), i64 noundef) local_unnamed_addr #1 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZNSt5dequeI4LineSaIS0_EED2Ev(ptr noundef nonnull align 8 dereferenceable(80)) unnamed_addr #3 align 2

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { inlinehint 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, !16, i64 80}
!6 = !{!"_ZTS7LCdeque", !7, i64 0, !16, i64 80}
!7 = !{!"_ZTSSt5dequeI4LineSaIS0_EE", !8, i64 0}
!8 = !{!"_ZTSSt11_Deque_baseI4LineSaIS0_EE", !9, i64 0}
!9 = !{!"_ZTSNSt11_Deque_baseI4LineSaIS0_EE11_Deque_implE", !10, i64 0}
!10 = !{!"_ZTSNSt11_Deque_baseI4LineSaIS0_EE16_Deque_impl_dataE", !11, i64 0, !14, i64 8, !15, i64 16, !15, i64 48}
!11 = !{!"any pointer", !12, i64 0}
!12 = !{!"omnipotent char", !13, i64 0}
!13 = !{!"Simple C++ TBAA"}
!14 = !{!"long", !12, i64 0}
!15 = !{!"_ZTSSt15_Deque_iteratorI4LineRS0_PS0_E", !11, i64 0, !11, i64 8, !11, i64 16, !11, i64 24}
!16 = !{!"int", !12, i64 0}
!17 = !{!16, !16, i64 0}
!18 = !{!19, !19, i64 0}
!19 = !{!"long long", !12, i64 0}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}


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