; LLVM IR from Izaron/ACM-ICPC :: Russian Council of School Olympiads (R.S.O.Sh.)/Zaoch/2017/napoleon.ll (Z9calc_nexti)

; 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.0" = type { %"struct.std::_Vector_base.1" }
%"struct.std::_Vector_base.1" = type { %"struct.std::_Vector_base<std::pair<int, int>, std::allocator<std::pair<int, int>>>::_Vector_impl" }
%"struct.std::_Vector_base<std::pair<int, int>, std::allocator<std::pair<int, int>>>::_Vector_impl" = type { %"struct.std::_Vector_base<std::pair<int, int>, std::allocator<std::pair<int, int>>>::_Vector_impl_data" }
%"struct.std::_Vector_base<std::pair<int, int>, std::allocator<std::pair<int, int>>>::_Vector_impl_data" = type { ptr, ptr, ptr }
%"struct.std::pair" = type { i32, i32 }

@n = external dso_local global i32, align 4
@vec = external dso_local global %"class.std::vector.0", align 8

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

; 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 nounwind uwtable
define dso_local noundef i32 @_Z9calc_nexti(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  %9 = load i32, ptr @n, align 4, !tbaa !5
  %10 = sub nsw i32 %9, 1
  %11 = sext i32 %10 to i64
  %12 = call noundef nonnull align 4 dereferenceable(8) ptr @_ZNSt6vectorISt4pairIiiESaIS1_EEixEm(ptr noundef nonnull align 8 dereferenceable(24) @vec, i64 noundef %11) #2
  %13 = getelementptr inbounds %"struct.std::pair", ptr %12, i32 0, i32 0
  %14 = load i32, ptr %13, align 4, !tbaa !9
  %15 = load i32, ptr %3, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = call noundef nonnull align 4 dereferenceable(8) ptr @_ZNSt6vectorISt4pairIiiESaIS1_EEixEm(ptr noundef nonnull align 8 dereferenceable(24) @vec, i64 noundef %16) #2
  %18 = getelementptr inbounds %"struct.std::pair", ptr %17, i32 0, i32 1
  %19 = load i32, ptr %18, align 4, !tbaa !11
  %20 = icmp sle i32 %14, %19
  br i1 %20, label %21, label %23

21:                                               ; preds = %1
  %22 = load i32, ptr @n, align 4, !tbaa !5
  store i32 %22, ptr %2, align 4
  br label %80

23:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 0, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  %24 = load i32, ptr @n, align 4, !tbaa !5
  %25 = sub nsw i32 %24, 1
  store i32 %25, ptr %5, align 4, !tbaa !5
  br label %26

26:                                               ; preds = %77, %23
  %27 = load i32, ptr %4, align 4, !tbaa !5
  %28 = load i32, ptr %5, align 4, !tbaa !5
  %29 = icmp ne i32 %27, %28
  br i1 %29, label %30, label %78

30:                                               ; preds = %26
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %31 = load i32, ptr %4, align 4, !tbaa !5
  store i32 %31, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %32 = load i32, ptr %5, align 4, !tbaa !5
  store i32 %32, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %33 = load i32, ptr %4, align 4, !tbaa !5
  %34 = load i32, ptr %5, align 4, !tbaa !5
  %35 = add nsw i32 %33, %34
  %36 = sdiv i32 %35, 2
  store i32 %36, ptr %8, align 4, !tbaa !5
  %37 = load i32, ptr %8, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = call noundef nonnull align 4 dereferenceable(8) ptr @_ZNSt6vectorISt4pairIiiESaIS1_EEixEm(ptr noundef nonnull align 8 dereferenceable(24) @vec, i64 noundef %38) #2
  %40 = getelementptr inbounds %"struct.std::pair", ptr %39, i32 0, i32 0
  %41 = load i32, ptr %40, align 4, !tbaa !9
  %42 = load i32, ptr %3, align 4, !tbaa !5
  %43 = sext i32 %42 to i64
  %44 = call noundef nonnull align 4 dereferenceable(8) ptr @_ZNSt6vectorISt4pairIiiESaIS1_EEixEm(ptr noundef nonnull align 8 dereferenceable(24) @vec, i64 noundef %43) #2
  %45 = getelementptr inbounds %"struct.std::pair", ptr %44, i32 0, i32 1
  %46 = load i32, ptr %45, align 4, !tbaa !11
  %47 = icmp sgt i32 %41, %46
  br i1 %47, label %48, label %50

48:                                               ; preds = %30
  %49 = load i32, ptr %8, align 4, !tbaa !5
  store i32 %49, ptr %5, align 4, !tbaa !5
  br label %52

50:                                               ; preds = %30
  %51 = load i32, ptr %8, align 4, !tbaa !5
  store i32 %51, ptr %4, align 4, !tbaa !5
  br label %52

52:                                               ; preds = %50, %48
  %53 = load i32, ptr %6, align 4, !tbaa !5
  %54 = load i32, ptr %4, align 4, !tbaa !5
  %55 = icmp eq i32 %53, %54
  br i1 %55, label %56, label %77

56:                                               ; preds = %52
  %57 = load i32, ptr %7, align 4, !tbaa !5
  %58 = load i32, ptr %5, align 4, !tbaa !5
  %59 = icmp eq i32 %57, %58
  br i1 %59, label %60, label %77

60:                                               ; preds = %56
  %61 = load i32, ptr %4, align 4, !tbaa !5
  %62 = sext i32 %61 to i64
  %63 = call noundef nonnull align 4 dereferenceable(8) ptr @_ZNSt6vectorISt4pairIiiESaIS1_EEixEm(ptr noundef nonnull align 8 dereferenceable(24) @vec, i64 noundef %62) #2
  %64 = getelementptr inbounds %"struct.std::pair", ptr %63, i32 0, i32 0
  %65 = load i32, ptr %64, align 4, !tbaa !9
  %66 = load i32, ptr %3, align 4, !tbaa !5
  %67 = sext i32 %66 to i64
  %68 = call noundef nonnull align 4 dereferenceable(8) ptr @_ZNSt6vectorISt4pairIiiESaIS1_EEixEm(ptr noundef nonnull align 8 dereferenceable(24) @vec, i64 noundef %67) #2
  %69 = getelementptr inbounds %"struct.std::pair", ptr %68, i32 0, i32 1
  %70 = load i32, ptr %69, align 4, !tbaa !11
  %71 = icmp sgt i32 %65, %70
  br i1 %71, label %72, label %74

72:                                               ; preds = %60
  %73 = load i32, ptr %4, align 4, !tbaa !5
  store i32 %73, ptr %5, align 4, !tbaa !5
  br label %76

74:                                               ; preds = %60
  %75 = load i32, ptr %5, align 4, !tbaa !5
  store i32 %75, ptr %4, align 4, !tbaa !5
  br label %76

76:                                               ; preds = %74, %72
  br label %77

77:                                               ; preds = %76, %56, %52
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  br label %26, !llvm.loop !12

78:                                               ; preds = %26
  %79 = load i32, ptr %4, align 4, !tbaa !5
  store i32 %79, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  br label %80

80:                                               ; preds = %78, %21
  %81 = load i32, ptr %2, align 4
  ret i32 %81
}

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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, !6, i64 0}
!10 = !{!"_ZTSSt4pairIiiE", !6, i64 0, !6, i64 4}
!11 = !{!10, !6, i64 4}
!12 = distinct !{!12, !13}
!13 = !{!"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.0" = type { %"struct.std::_Vector_base.1" }
%"struct.std::_Vector_base.1" = type { %"struct.std::_Vector_base<std::pair<int, int>, std::allocator<std::pair<int, int>>>::_Vector_impl" }
%"struct.std::_Vector_base<std::pair<int, int>, std::allocator<std::pair<int, int>>>::_Vector_impl" = type { %"struct.std::_Vector_base<std::pair<int, int>, std::allocator<std::pair<int, int>>>::_Vector_impl_data" }
%"struct.std::_Vector_base<std::pair<int, int>, std::allocator<std::pair<int, int>>>::_Vector_impl_data" = type { ptr, ptr, ptr }

@n = external dso_local local_unnamed_addr global i32, align 4
@vec = external dso_local global %"class.std::vector.0", align 8

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z9calc_nexti(i32 noundef %0) local_unnamed_addr #0 {
  %2 = load i32, ptr @n, align 4, !tbaa !5
  %3 = add nsw i32 %2, -1
  %4 = sext i32 %3 to i64
  %5 = tail call noundef nonnull align 4 dereferenceable(8) ptr @_ZNSt6vectorISt4pairIiiESaIS1_EEixEm(ptr noundef nonnull align 8 dereferenceable(24) @vec, i64 noundef %4) #1
  %6 = load i32, ptr %5, align 4, !tbaa !9
  %7 = sext i32 %0 to i64
  %8 = tail call noundef nonnull align 4 dereferenceable(8) ptr @_ZNSt6vectorISt4pairIiiESaIS1_EEixEm(ptr noundef nonnull align 8 dereferenceable(24) @vec, i64 noundef %7) #1
  %9 = getelementptr inbounds i8, ptr %8, i64 4
  %10 = load i32, ptr %9, align 4, !tbaa !11
  %.not = icmp sgt i32 %6, %10
  %11 = load i32, ptr @n, align 4, !tbaa !5
  br i1 %.not, label %12, label %.loopexit

12:                                               ; preds = %1
  %13 = add nsw i32 %11, -1
  %.not2526 = icmp eq i32 %13, 0
  br i1 %.not2526, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %12, %32
  %.02128 = phi i32 [ %.021., %32 ], [ 0, %12 ]
  %.02227 = phi i32 [ %..022, %32 ], [ %13, %12 ]
  %14 = add nsw i32 %.02128, %.02227
  %15 = sdiv i32 %14, 2
  %16 = sext i32 %15 to i64
  %17 = tail call noundef nonnull align 4 dereferenceable(8) ptr @_ZNSt6vectorISt4pairIiiESaIS1_EEixEm(ptr noundef nonnull align 8 dereferenceable(24) @vec, i64 noundef %16) #1
  %18 = load i32, ptr %17, align 4, !tbaa !9
  %19 = tail call noundef nonnull align 4 dereferenceable(8) ptr @_ZNSt6vectorISt4pairIiiESaIS1_EEixEm(ptr noundef nonnull align 8 dereferenceable(24) @vec, i64 noundef %7) #1
  %20 = getelementptr inbounds i8, ptr %19, i64 4
  %21 = load i32, ptr %20, align 4, !tbaa !11
  %22 = icmp sgt i32 %18, %21
  %..022 = select i1 %22, i32 %15, i32 %.02227
  %.021. = select i1 %22, i32 %.02128, i32 %15
  %23 = icmp eq i32 %.02128, %.021.
  %24 = icmp eq i32 %.02227, %..022
  %or.cond = and i1 %23, %24
  br i1 %or.cond, label %.thread, label %32

.thread:                                          ; preds = %.lr.ph
  %25 = sext i32 %.02128 to i64
  %26 = tail call noundef nonnull align 4 dereferenceable(8) ptr @_ZNSt6vectorISt4pairIiiESaIS1_EEixEm(ptr noundef nonnull align 8 dereferenceable(24) @vec, i64 noundef %25) #1
  %27 = load i32, ptr %26, align 4, !tbaa !9
  %28 = tail call noundef nonnull align 4 dereferenceable(8) ptr @_ZNSt6vectorISt4pairIiiESaIS1_EEixEm(ptr noundef nonnull align 8 dereferenceable(24) @vec, i64 noundef %7) #1
  %29 = getelementptr inbounds i8, ptr %28, i64 4
  %30 = load i32, ptr %29, align 4, !tbaa !11
  %31 = icmp sgt i32 %27, %30
  %.021....022 = select i1 %31, i32 %.02128, i32 %.02227
  br label %.loopexit

32:                                               ; preds = %.lr.ph
  %.not25 = icmp eq i32 %.021., %..022
  br i1 %.not25, label %.loopexit, label %.lr.ph, !llvm.loop !12

.loopexit:                                        ; preds = %32, %1, %.thread, %12
  %.0 = phi i32 [ 0, %12 ], [ %.021....022, %.thread ], [ %11, %1 ], [ %..022, %32 ]
  ret i32 %.0
}

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, !6, i64 0}
!10 = !{!"_ZTSSt4pairIiiE", !6, i64 0, !6, i64 4}
!11 = !{!10, !6, i64 4}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


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