; LLVM IR from AnneLivia/Competitive-Programming :: Online Judges/Neps Academy/OgrosOBI2008.ll (Z6binaryi)

; 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<Data, std::allocator<Data>>::_Vector_impl" }
%"struct.std::_Vector_base<Data, std::allocator<Data>>::_Vector_impl" = type { %"struct.std::_Vector_base<Data, std::allocator<Data>>::_Vector_impl_data" }
%"struct.std::_Vector_base<Data, std::allocator<Data>>::_Vector_impl_data" = type { ptr, ptr, ptr }
%struct.Data = type { i64, i64, i64 }

@v = external dso_local global %"class.std::vector", align 8

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z6binaryi(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
  store i32 %0, ptr %3, align 4, !tbaa !5
  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
  %8 = call noundef i64 @_ZNKSt6vectorI4DataSaIS0_EE4sizeEv(ptr noundef nonnull align 8 dereferenceable(24) @v) #2
  %9 = trunc i64 %8 to i32
  %10 = sub nsw i32 %9, 1
  store i32 %10, ptr %5, align 4, !tbaa !5
  br label %11

11:                                               ; preds = %63, %1
  %12 = load i32, ptr %4, align 4, !tbaa !5
  %13 = load i32, ptr %5, align 4, !tbaa !5
  %14 = icmp sle i32 %12, %13
  br i1 %14, label %15, label %64

15:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %16 = load i32, ptr %4, align 4, !tbaa !5
  %17 = load i32, ptr %5, align 4, !tbaa !5
  %18 = add nsw i32 %16, %17
  %19 = sdiv i32 %18, 2
  store i32 %19, ptr %6, align 4, !tbaa !5
  %20 = load i32, ptr %6, align 4, !tbaa !5
  %21 = sext i32 %20 to i64
  %22 = call noundef nonnull align 8 dereferenceable(24) ptr @_ZNSt6vectorI4DataSaIS0_EEixEm(ptr noundef nonnull align 8 dereferenceable(24) @v, i64 noundef %21) #2
  %23 = getelementptr inbounds %struct.Data, ptr %22, i32 0, i32 0
  %24 = load i64, ptr %23, align 8, !tbaa !9
  %25 = load i32, ptr %3, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = icmp sle i64 %24, %26
  br i1 %27, label %28, label %44

28:                                               ; preds = %15
  %29 = load i32, ptr %3, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = load i32, ptr %6, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = call noundef nonnull align 8 dereferenceable(24) ptr @_ZNSt6vectorI4DataSaIS0_EEixEm(ptr noundef nonnull align 8 dereferenceable(24) @v, i64 noundef %32) #2
  %34 = getelementptr inbounds %struct.Data, ptr %33, i32 0, i32 1
  %35 = load i64, ptr %34, align 8, !tbaa !12
  %36 = icmp sle i64 %30, %35
  br i1 %36, label %37, label %44

37:                                               ; preds = %28
  %38 = load i32, ptr %6, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = call noundef nonnull align 8 dereferenceable(24) ptr @_ZNSt6vectorI4DataSaIS0_EEixEm(ptr noundef nonnull align 8 dereferenceable(24) @v, i64 noundef %39) #2
  %41 = getelementptr inbounds %struct.Data, ptr %40, i32 0, i32 2
  %42 = load i64, ptr %41, align 8, !tbaa !13
  %43 = trunc i64 %42 to i32
  store i32 %43, ptr %2, align 4
  store i32 1, ptr %7, align 4
  br label %61

44:                                               ; preds = %28, %15
  %45 = load i32, ptr %3, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = load i32, ptr %6, align 4, !tbaa !5
  %48 = sext i32 %47 to i64
  %49 = call noundef nonnull align 8 dereferenceable(24) ptr @_ZNSt6vectorI4DataSaIS0_EEixEm(ptr noundef nonnull align 8 dereferenceable(24) @v, i64 noundef %48) #2
  %50 = getelementptr inbounds %struct.Data, ptr %49, i32 0, i32 0
  %51 = load i64, ptr %50, align 8, !tbaa !9
  %52 = icmp slt i64 %46, %51
  br i1 %52, label %53, label %56

53:                                               ; preds = %44
  %54 = load i32, ptr %6, align 4, !tbaa !5
  %55 = sub nsw i32 %54, 1
  store i32 %55, ptr %5, align 4, !tbaa !5
  br label %59

56:                                               ; preds = %44
  %57 = load i32, ptr %6, align 4, !tbaa !5
  %58 = add nsw i32 %57, 1
  store i32 %58, ptr %4, align 4, !tbaa !5
  br label %59

59:                                               ; preds = %56, %53
  br label %60

60:                                               ; preds = %59
  store i32 0, ptr %7, align 4
  br label %61

61:                                               ; preds = %60, %37
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  %62 = load i32, ptr %7, align 4
  switch i32 %62, label %65 [
    i32 0, label %63
  ]

63:                                               ; preds = %61
  br label %11, !llvm.loop !14

64:                                               ; preds = %11
  store i32 0, ptr %2, align 4
  store i32 1, ptr %7, align 4
  br label %65

65:                                               ; preds = %64, %61
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  %66 = load i32, ptr %2, align 4
  ret i32 %66
}

; 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 @_ZNKSt6vectorI4DataSaIS0_EE4sizeEv(ptr noundef nonnull align 8 dereferenceable(24)) #0 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(24) ptr @_ZNSt6vectorI4DataSaIS0_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.end.p0(i64 immarg, ptr nocapture) #1

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, !11, i64 0}
!10 = !{!"_ZTS4Data", !11, i64 0, !11, i64 8, !11, i64 16}
!11 = !{!"long", !7, i64 0}
!12 = !{!10, !11, i64 8}
!13 = !{!10, !11, i64 16}
!14 = distinct !{!14, !15}
!15 = !{!"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<Data, std::allocator<Data>>::_Vector_impl" }
%"struct.std::_Vector_base<Data, std::allocator<Data>>::_Vector_impl" = type { %"struct.std::_Vector_base<Data, std::allocator<Data>>::_Vector_impl_data" }
%"struct.std::_Vector_base<Data, std::allocator<Data>>::_Vector_impl_data" = type { ptr, ptr, ptr }

@v = external dso_local global %"class.std::vector", align 8

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z6binaryi(i32 noundef %0) local_unnamed_addr #0 {
  %2 = tail call noundef i64 @_ZNKSt6vectorI4DataSaIS0_EE4sizeEv(ptr noundef nonnull align 8 dereferenceable(24) @v) #1
  %3 = trunc i64 %2 to i32
  %.not29 = icmp slt i32 %3, 1
  br i1 %.not29, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %1
  %4 = add nsw i32 %3, -1
  %5 = sext i32 %0 to i64
  br label %6

6:                                                ; preds = %.lr.ph, %20
  %.01631 = phi i32 [ %4, %.lr.ph ], [ %.117, %20 ]
  %.01930 = phi i32 [ 0, %.lr.ph ], [ %.120, %20 ]
  %7 = add nuw nsw i32 %.01631, %.01930
  %8 = lshr i32 %7, 1
  %9 = zext nneg i32 %8 to i64
  %10 = tail call noundef nonnull align 8 dereferenceable(24) ptr @_ZNSt6vectorI4DataSaIS0_EEixEm(ptr noundef nonnull align 8 dereferenceable(24) @v, i64 noundef %9) #1
  %11 = load i64, ptr %10, align 8, !tbaa !5
  %.not23 = icmp sgt i64 %11, %5
  br i1 %.not23, label %20, label %12

12:                                               ; preds = %6
  %13 = tail call noundef nonnull align 8 dereferenceable(24) ptr @_ZNSt6vectorI4DataSaIS0_EEixEm(ptr noundef nonnull align 8 dereferenceable(24) @v, i64 noundef %9) #1
  %14 = getelementptr inbounds i8, ptr %13, i64 8
  %15 = load i64, ptr %14, align 8, !tbaa !10
  %.not24 = icmp slt i64 %15, %5
  br i1 %.not24, label %20, label %.thread

.thread:                                          ; preds = %12
  %16 = tail call noundef nonnull align 8 dereferenceable(24) ptr @_ZNSt6vectorI4DataSaIS0_EEixEm(ptr noundef nonnull align 8 dereferenceable(24) @v, i64 noundef %9) #1
  %17 = getelementptr inbounds i8, ptr %16, i64 16
  %18 = load i64, ptr %17, align 8, !tbaa !11
  %19 = trunc i64 %18 to i32
  br label %.loopexit

20:                                               ; preds = %6, %12
  %21 = tail call noundef nonnull align 8 dereferenceable(24) ptr @_ZNSt6vectorI4DataSaIS0_EEixEm(ptr noundef nonnull align 8 dereferenceable(24) @v, i64 noundef %9) #1
  %22 = load i64, ptr %21, align 8, !tbaa !5
  %23 = icmp sgt i64 %22, %5
  %24 = add nsw i32 %8, -1
  %25 = add nuw nsw i32 %8, 1
  %.120 = select i1 %23, i32 %.01930, i32 %25
  %.117 = select i1 %23, i32 %24, i32 %.01631
  %.not = icmp sgt i32 %.120, %.117
  br i1 %.not, label %.loopexit, label %6, !llvm.loop !12

.loopexit:                                        ; preds = %20, %1, %.thread
  %.2 = phi i32 [ %19, %.thread ], [ 0, %1 ], [ 0, %20 ]
  ret i32 %.2
}

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(24) ptr @_ZNSt6vectorI4DataSaIS0_EEixEm(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, !7, i64 0}
!6 = !{!"_ZTS4Data", !7, i64 0, !7, i64 8, !7, i64 16}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!6, !7, i64 8}
!11 = !{!6, !7, i64 16}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


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