; LLVM IR from DionysiosB/CodeForces :: 1100-1199/1145A-ThanosSort.ll (Z15getThanosLengthRKSt6vectorIlSaIlEEll)

; 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"

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z15getThanosLengthRKSt6vectorIlSaIlEEll(ptr noundef nonnull align 8 dereferenceable(24) %0, i64 noundef %1, i64 noundef %2) #0 {
  %4 = alloca i64, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i8, align 1
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  %11 = alloca i64, align 8
  %12 = alloca i64, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i64 %1, ptr %6, align 8, !tbaa !9
  store i64 %2, ptr %7, align 8, !tbaa !9
  %13 = load i64, ptr %7, align 8, !tbaa !9
  %14 = load i64, ptr %6, align 8, !tbaa !9
  %15 = add nsw i64 %14, 1
  %16 = icmp sle i64 %13, %15
  br i1 %16, label %17, label %21

17:                                               ; preds = %3
  %18 = load i64, ptr %7, align 8, !tbaa !9
  %19 = load i64, ptr %6, align 8, !tbaa !9
  %20 = sub nsw i64 %18, %19
  store i64 %20, ptr %4, align 8
  br label %78

21:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 1, ptr %8) #3
  store i8 1, ptr %8, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %22 = load i64, ptr %6, align 8, !tbaa !9
  %23 = add nsw i64 %22, 1
  store i64 %23, ptr %9, align 8, !tbaa !9
  br label %24

24:                                               ; preds = %42, %21
  %25 = load i64, ptr %9, align 8, !tbaa !9
  %26 = load i64, ptr %7, align 8, !tbaa !9
  %27 = icmp slt i64 %25, %26
  br i1 %27, label %29, label %28

28:                                               ; preds = %24
  store i32 2, ptr %10, align 4
  br label %45

29:                                               ; preds = %24
  %30 = load ptr, ptr %5, align 8, !tbaa !5
  %31 = load i64, ptr %9, align 8, !tbaa !9
  %32 = sub nsw i64 %31, 1
  %33 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNKSt6vectorIlSaIlEEixEm(ptr noundef nonnull align 8 dereferenceable(24) %30, i64 noundef %32) #3
  %34 = load i64, ptr %33, align 8, !tbaa !9
  %35 = load ptr, ptr %5, align 8, !tbaa !5
  %36 = load i64, ptr %9, align 8, !tbaa !9
  %37 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNKSt6vectorIlSaIlEEixEm(ptr noundef nonnull align 8 dereferenceable(24) %35, i64 noundef %36) #3
  %38 = load i64, ptr %37, align 8, !tbaa !9
  %39 = icmp sgt i64 %34, %38
  br i1 %39, label %40, label %41

40:                                               ; preds = %29
  store i8 0, ptr %8, align 1, !tbaa !11
  store i32 2, ptr %10, align 4
  br label %45

41:                                               ; preds = %29
  br label %42

42:                                               ; preds = %41
  %43 = load i64, ptr %9, align 8, !tbaa !9
  %44 = add nsw i64 %43, 1
  store i64 %44, ptr %9, align 8, !tbaa !9
  br label %24, !llvm.loop !13

45:                                               ; preds = %40, %28
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  br label %46

46:                                               ; preds = %45
  %47 = load i8, ptr %8, align 1, !tbaa !11, !range !15, !noundef !16
  %48 = trunc i8 %47 to i1
  br i1 %48, label %49, label %53

49:                                               ; preds = %46
  %50 = load i64, ptr %7, align 8, !tbaa !9
  %51 = load i64, ptr %6, align 8, !tbaa !9
  %52 = sub nsw i64 %50, %51
  store i64 %52, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %77

53:                                               ; preds = %46
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  %54 = load ptr, ptr %5, align 8, !tbaa !5
  %55 = load i64, ptr %6, align 8, !tbaa !9
  %56 = load i64, ptr %6, align 8, !tbaa !9
  %57 = load i64, ptr %7, align 8, !tbaa !9
  %58 = add nsw i64 %56, %57
  %59 = sdiv i64 %58, 2
  %60 = call noundef i64 @_Z15getThanosLengthRKSt6vectorIlSaIlEEll(ptr noundef nonnull align 8 dereferenceable(24) %54, i64 noundef %55, i64 noundef %59)
  store i64 %60, ptr %11, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  %61 = load ptr, ptr %5, align 8, !tbaa !5
  %62 = load i64, ptr %6, align 8, !tbaa !9
  %63 = load i64, ptr %7, align 8, !tbaa !9
  %64 = add nsw i64 %62, %63
  %65 = sdiv i64 %64, 2
  %66 = load i64, ptr %7, align 8, !tbaa !9
  %67 = call noundef i64 @_Z15getThanosLengthRKSt6vectorIlSaIlEEll(ptr noundef nonnull align 8 dereferenceable(24) %61, i64 noundef %65, i64 noundef %66)
  store i64 %67, ptr %12, align 8, !tbaa !9
  %68 = load i64, ptr %11, align 8, !tbaa !9
  %69 = load i64, ptr %12, align 8, !tbaa !9
  %70 = icmp sgt i64 %68, %69
  br i1 %70, label %71, label %73

71:                                               ; preds = %53
  %72 = load i64, ptr %11, align 8, !tbaa !9
  br label %75

73:                                               ; preds = %53
  %74 = load i64, ptr %12, align 8, !tbaa !9
  br label %75

75:                                               ; preds = %73, %71
  %76 = phi i64 [ %72, %71 ], [ %74, %73 ]
  store i64 %76, ptr %4, align 8
  store i32 1, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  br label %77

77:                                               ; preds = %75, %49
  call void @llvm.lifetime.end.p0(i64 1, ptr %8) #3
  br label %78

78:                                               ; preds = %77, %17
  %79 = load i64, ptr %4, align 8
  ret i64 %79
}

; 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 nonnull align 8 dereferenceable(8) ptr @_ZNKSt6vectorIlSaIlEEixEm(ptr noundef nonnull align 8 dereferenceable(24), i64 noundef) #2 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 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 = { 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 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"bool", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{i8 0, i8 2}
!16 = !{}


; 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"

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z15getThanosLengthRKSt6vectorIlSaIlEEll(ptr noundef nonnull align 8 dereferenceable(24) %0, i64 noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = add nsw i64 %1, 1
  %.not39 = icmp slt i64 %4, %2
  br i1 %.not39, label %.preheader, label %tailrecurse._crit_edge

.preheader:                                       ; preds = %3, %tailrecurse
  %5 = phi i64 [ %19, %tailrecurse ], [ %4, %3 ]
  %.tr3341 = phi i64 [ %16, %tailrecurse ], [ %1, %3 ]
  %accumulator.tr40 = phi i64 [ %18, %tailrecurse ], [ -9223372036854775808, %3 ]
  br label %8

6:                                                ; preds = %8
  %7 = add nsw i64 %.02838, 1
  %.not32 = icmp slt i64 %7, %2
  br i1 %.not32, label %8, label %tailrecurse._crit_edge, !llvm.loop !5

8:                                                ; preds = %.preheader, %6
  %.02838 = phi i64 [ %5, %.preheader ], [ %7, %6 ]
  %9 = add nsw i64 %.02838, -1
  %10 = tail call noundef nonnull align 8 dereferenceable(8) ptr @_ZNKSt6vectorIlSaIlEEixEm(ptr noundef nonnull align 8 dereferenceable(24) %0, i64 noundef %9) #2
  %11 = load i64, ptr %10, align 8, !tbaa !7
  %12 = tail call noundef nonnull align 8 dereferenceable(8) ptr @_ZNKSt6vectorIlSaIlEEixEm(ptr noundef nonnull align 8 dereferenceable(24) %0, i64 noundef %.02838) #2
  %13 = load i64, ptr %12, align 8, !tbaa !7
  %14 = icmp sgt i64 %11, %13
  br i1 %14, label %tailrecurse, label %6

tailrecurse:                                      ; preds = %8
  %15 = add nsw i64 %.tr3341, %2
  %16 = sdiv i64 %15, 2
  %17 = tail call noundef i64 @_Z15getThanosLengthRKSt6vectorIlSaIlEEll(ptr noundef nonnull align 8 dereferenceable(24) %0, i64 noundef %.tr3341, i64 noundef %16)
  %18 = tail call i64 @llvm.smax.i64(i64 %17, i64 %accumulator.tr40)
  %19 = add nsw i64 %16, 1
  %.not = icmp slt i64 %19, %2
  br i1 %.not, label %.preheader, label %tailrecurse._crit_edge

tailrecurse._crit_edge:                           ; preds = %tailrecurse, %6, %3
  %accumulator.tr37 = phi i64 [ -9223372036854775808, %3 ], [ %accumulator.tr40, %6 ], [ %18, %tailrecurse ]
  %.tr33.lcssa.pn = phi i64 [ %1, %3 ], [ %.tr3341, %6 ], [ %16, %tailrecurse ]
  %.1 = sub nsw i64 %2, %.tr33.lcssa.pn
  %accumulator.ret.tr = tail call i64 @llvm.smax.i64(i64 %.1, i64 %accumulator.tr37)
  ret i64 %accumulator.ret.tr
}

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.smax.i64(i64, i64) #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 speculatable willreturn memory(none) }
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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = !{!8, !8, i64 0}
!8 = !{!"long", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C++ TBAA"}


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