; LLVM IR from forestLoop/Learning-ITMO-Academy-Pilot-Course :: binary-search/4-A.ll (Z16locateMaxSegmentRKSt6vectorIiSaIiEEi)

; 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.std::pair" = type { i32, i32 }
%class.anon = type { ptr, i32, i32 }
%"struct.std::pair.0" = type { i8, %"struct.std::pair" }

; Function Attrs: mustprogress uwtable
define dso_local i64 @_Z16locateMaxSegmentRKSt6vectorIiSaIiEEi(ptr noundef nonnull align 8 dereferenceable(24) %0, i32 noundef %1) #0 {
  %3 = alloca %"struct.std::pair", align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca %class.anon, align 8
  %8 = alloca double, align 8
  %9 = alloca double, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca double, align 8
  %13 = alloca %"struct.std::pair.0", align 4
  %14 = alloca { i64, i32 }, align 8
  %15 = alloca %"struct.std::pair.0", align 4
  %16 = alloca { i64, i32 }, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #5
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  %18 = call noundef i64 @_ZNKSt6vectorIiSaIiEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(24) %17) #5
  %19 = trunc i64 %18 to i32
  store i32 %19, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 16, ptr %7) #5
  %20 = getelementptr inbounds %class.anon, ptr %7, i32 0, i32 0
  %21 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %21, ptr %20, align 8, !tbaa !5
  %22 = getelementptr inbounds %class.anon, ptr %7, i32 0, i32 1
  %23 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %23, ptr %22, align 8, !tbaa !11
  %24 = getelementptr inbounds %class.anon, ptr %7, i32 0, i32 2
  %25 = load i32, ptr %5, align 4, !tbaa !9
  store i32 %25, ptr %24, align 4, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  store double 0.000000e+00, ptr %8, align 8, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #5
  store double 1.010000e+02, ptr %9, align 8, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #5
  store i32 25, ptr %10, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #5
  store i32 0, ptr %11, align 4, !tbaa !9
  br label %26

26:                                               ; preds = %47, %2
  %27 = load i32, ptr %11, align 4, !tbaa !9
  %28 = icmp slt i32 %27, 25
  br i1 %28, label %30, label %29

29:                                               ; preds = %26
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #5
  br label %50

30:                                               ; preds = %26
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #5
  %31 = load double, ptr %8, align 8, !tbaa !14
  %32 = load double, ptr %9, align 8, !tbaa !14
  %33 = load double, ptr %8, align 8, !tbaa !14
  %34 = fsub double %32, %33
  %35 = fdiv double %34, 2.000000e+00
  %36 = fadd double %31, %35
  store double %36, ptr %12, align 8, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 12, ptr %13) #5
  %37 = load double, ptr %12, align 8, !tbaa !14
  %38 = call { i64, i32 } @"_ZZ16locateMaxSegmentRKSt6vectorIiSaIiEEiENK3$_0clEdb"(ptr noundef nonnull align 8 dereferenceable(16) %7, double noundef %37, i1 noundef zeroext false)
  store { i64, i32 } %38, ptr %14, align 8
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %13, ptr align 8 %14, i64 12, i1 false)
  %39 = getelementptr inbounds %"struct.std::pair.0", ptr %13, i32 0, i32 0
  %40 = load i8, ptr %39, align 4, !tbaa !16, !range !20, !noundef !21
  %41 = trunc i8 %40 to i1
  call void @llvm.lifetime.end.p0(i64 12, ptr %13) #5
  br i1 %41, label %42, label %44

42:                                               ; preds = %30
  %43 = load double, ptr %12, align 8, !tbaa !14
  store double %43, ptr %8, align 8, !tbaa !14
  br label %46

44:                                               ; preds = %30
  %45 = load double, ptr %12, align 8, !tbaa !14
  store double %45, ptr %9, align 8, !tbaa !14
  br label %46

46:                                               ; preds = %44, %42
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #5
  br label %47

47:                                               ; preds = %46
  %48 = load i32, ptr %11, align 4, !tbaa !9
  %49 = add nsw i32 %48, 1
  store i32 %49, ptr %11, align 4, !tbaa !9
  br label %26, !llvm.loop !22

50:                                               ; preds = %29
  call void @llvm.lifetime.start.p0(i64 12, ptr %15) #5
  %51 = load double, ptr %8, align 8, !tbaa !14
  %52 = call { i64, i32 } @"_ZZ16locateMaxSegmentRKSt6vectorIiSaIiEEiENK3$_0clEdb"(ptr noundef nonnull align 8 dereferenceable(16) %7, double noundef %51, i1 noundef zeroext true)
  store { i64, i32 } %52, ptr %16, align 8
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %15, ptr align 8 %16, i64 12, i1 false)
  %53 = getelementptr inbounds %"struct.std::pair.0", ptr %15, i32 0, i32 1
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %3, ptr align 4 %53, i64 8, i1 false)
  call void @llvm.lifetime.end.p0(i64 12, ptr %15) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 16, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #5
  %54 = load i64, ptr %3, align 4
  ret i64 %54
}

; 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: inlinehint mustprogress uwtable
declare hidden { i64, i32 } @"_ZZ16locateMaxSegmentRKSt6vectorIiSaIiEEiENK3$_0clEdb"(ptr noundef nonnull align 8 dereferenceable(16), double noundef, i1 noundef zeroext) #3 align 2

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #4

; 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 = { 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 #4 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 8}
!12 = !{!"_ZTSZ16locateMaxSegmentRKSt6vectorIiSaIiEEiE3$_0", !6, i64 0, !10, i64 8, !10, i64 12}
!13 = !{!12, !10, i64 12}
!14 = !{!15, !15, i64 0}
!15 = !{!"double", !7, i64 0}
!16 = !{!17, !18, i64 0}
!17 = !{!"_ZTSSt4pairIbS_IiiEE", !18, i64 0, !19, i64 4}
!18 = !{!"bool", !7, i64 0}
!19 = !{!"_ZTSSt4pairIiiE", !10, i64 0, !10, i64 4}
!20 = !{i8 0, i8 2}
!21 = !{}
!22 = distinct !{!22, !23}
!23 = !{!"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.anon = type { ptr, i32, i32 }

; Function Attrs: mustprogress uwtable
define dso_local i64 @_Z16locateMaxSegmentRKSt6vectorIiSaIiEEi(ptr noundef nonnull align 8 dereferenceable(24) %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = alloca %class.anon, align 8
  %4 = tail call noundef i64 @_ZNKSt6vectorIiSaIiEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(24) %0) #5
  %5 = trunc i64 %4 to i32
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %3) #5
  store ptr %0, ptr %3, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %3, i64 8
  store i32 %5, ptr %6, align 8, !tbaa !9
  %7 = getelementptr inbounds i8, ptr %3, i64 12
  store i32 %1, ptr %7, align 4, !tbaa !12
  br label %10

8:                                                ; preds = %10
  %9 = call { i64, i32 } @"_ZZ16locateMaxSegmentRKSt6vectorIiSaIiEEiENK3$_0clEdb"(ptr noundef nonnull align 8 dereferenceable(16) %3, double noundef %..0, i1 noundef zeroext true)
  %.fca.0.extract = extractvalue { i64, i32 } %9, 0
  %.fca.1.extract = extractvalue { i64, i32 } %9, 1
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %3) #5
  %.sroa.220.0.insert.ext = zext i32 %.fca.1.extract to i64
  %.sroa.019.0.insert.insert = call i64 @llvm.fshl.i64(i64 %.sroa.220.0.insert.ext, i64 %.fca.0.extract, i64 32)
  ret i64 %.sroa.019.0.insert.insert

10:                                               ; preds = %2, %10
  %.026 = phi double [ 0.000000e+00, %2 ], [ %..0, %10 ]
  %.02125 = phi double [ 1.010000e+02, %2 ], [ %.021., %10 ]
  %.02324 = phi i32 [ 0, %2 ], [ %16, %10 ]
  %11 = fsub double %.02125, %.026
  %12 = fmul double %11, 5.000000e-01
  %13 = fadd double %.026, %12
  %14 = call { i64, i32 } @"_ZZ16locateMaxSegmentRKSt6vectorIiSaIiEEiENK3$_0clEdb"(ptr noundef nonnull align 8 dereferenceable(16) %3, double noundef %13, i1 noundef zeroext false)
  %.fca.0.extract2 = extractvalue { i64, i32 } %14, 0
  %15 = trunc i64 %.fca.0.extract2 to i1
  %.021. = select i1 %15, double %.02125, double %13
  %..0 = select i1 %15, double %13, double %.026
  %16 = add nuw nsw i32 %.02324, 1
  %exitcond.not = icmp eq i32 %16, 25
  br i1 %exitcond.not, label %8, label %10, !llvm.loop !13
}

; Function Attrs: mustprogress 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)) local_unnamed_addr #2 align 2

; Function Attrs: inlinehint mustprogress uwtable
declare hidden { i64, i32 } @"_ZZ16locateMaxSegmentRKSt6vectorIiSaIiEEiENK3$_0clEdb"(ptr noundef nonnull align 8 dereferenceable(16), double noundef, i1 noundef zeroext) local_unnamed_addr #3 align 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: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.fshl.i64(i64, i64, i64) #4

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 = { 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 = { 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 #4 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #5 = { 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !11, i64 8}
!10 = !{!"_ZTSZ16locateMaxSegmentRKSt6vectorIiSaIiEEiE3$_0", !6, i64 0, !11, i64 8, !11, i64 12}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !11, i64 12}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LoopRotatePass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass