; LLVM IR from ahr9n/psolving-paradigms :: 3.binary-search/CodeForces 803D - Magazine Ad.ll (Z5solvev)

; 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::basic_ostream" = type { ptr, %"class.std::basic_ios" }
%"class.std::basic_ios" = type { %"class.std::ios_base", ptr, i8, i8, ptr, ptr, ptr, ptr }
%"class.std::ios_base" = type { ptr, i64, i64, i32, i32, i32, ptr, %"struct.std::ios_base::_Words", [8 x %"struct.std::ios_base::_Words"], i32, ptr, %"class.std::locale" }
%"struct.std::ios_base::_Words" = type { ptr, i64 }
%"class.std::locale" = type { ptr }

@l = external dso_local global i32, align 4
@r = external dso_local global i32, align 4
@mid = external dso_local global i32, align 4
@ans = external dso_local global i32, align 4
@_ZSt4cout = external global %"class.std::basic_ostream", align 8

; Function Attrs: mustprogress uwtable
declare dso_local noundef zeroext i1 @_Z3cani(i32 noundef) #0

; Function Attrs: mustprogress uwtable
define dso_local void @_Z5solvev() #0 {
  store i32 1, ptr @l, align 4, !tbaa !5
  store i32 1000010, ptr @r, align 4, !tbaa !5
  br label %1

1:                                                ; preds = %19, %0
  %2 = load i32, ptr @l, align 4, !tbaa !5
  %3 = load i32, ptr @r, align 4, !tbaa !5
  %4 = icmp sle i32 %2, %3
  br i1 %4, label %5, label %20

5:                                                ; preds = %1
  %6 = load i32, ptr @l, align 4, !tbaa !5
  %7 = load i32, ptr @r, align 4, !tbaa !5
  %8 = add nsw i32 %6, %7
  %9 = sdiv i32 %8, 2
  store i32 %9, ptr @mid, align 4, !tbaa !5
  %10 = load i32, ptr @mid, align 4, !tbaa !5
  %11 = call noundef zeroext i1 @_Z3cani(i32 noundef %10)
  br i1 %11, label %12, label %16

12:                                               ; preds = %5
  %13 = load i32, ptr @mid, align 4, !tbaa !5
  store i32 %13, ptr @ans, align 4, !tbaa !5
  %14 = load i32, ptr @mid, align 4, !tbaa !5
  %15 = sub nsw i32 %14, 1
  store i32 %15, ptr @r, align 4, !tbaa !5
  br label %19

16:                                               ; preds = %5
  %17 = load i32, ptr @mid, align 4, !tbaa !5
  %18 = add nsw i32 %17, 1
  store i32 %18, ptr @l, align 4, !tbaa !5
  br label %19

19:                                               ; preds = %16, %12
  br label %1, !llvm.loop !9

20:                                               ; preds = %1
  %21 = load i32, ptr @ans, align 4, !tbaa !5
  %22 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEi(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, i32 noundef %21)
  ret void
}

declare noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEi(ptr noundef nonnull align 8 dereferenceable(8), i32 noundef) #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 = { "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" }

!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 = distinct !{!9, !10}
!10 = !{!"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::basic_ostream" = type { ptr, %"class.std::basic_ios" }
%"class.std::basic_ios" = type { %"class.std::ios_base", ptr, i8, i8, ptr, ptr, ptr, ptr }
%"class.std::ios_base" = type { ptr, i64, i64, i32, i32, i32, ptr, %"struct.std::ios_base::_Words", [8 x %"struct.std::ios_base::_Words"], i32, ptr, %"class.std::locale" }
%"struct.std::ios_base::_Words" = type { ptr, i64 }
%"class.std::locale" = type { ptr }

@l = external dso_local local_unnamed_addr global i32, align 4
@r = external dso_local local_unnamed_addr global i32, align 4
@mid = external dso_local local_unnamed_addr global i32, align 4
@ans = external dso_local local_unnamed_addr global i32, align 4
@_ZSt4cout = external global %"class.std::basic_ostream", align 8

; Function Attrs: mustprogress uwtable
declare dso_local noundef zeroext i1 @_Z3cani(i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local void @_Z5solvev() local_unnamed_addr #0 {
.lr.ph.preheader:
  store i32 1, ptr @l, align 4, !tbaa !5
  store i32 1000010, ptr @r, align 4, !tbaa !5
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %10
  %0 = phi i32 [ %11, %10 ], [ 1000010, %.lr.ph.preheader ]
  %1 = phi i32 [ %12, %10 ], [ 1, %.lr.ph.preheader ]
  %2 = add nsw i32 %0, %1
  %3 = sdiv i32 %2, 2
  store i32 %3, ptr @mid, align 4, !tbaa !5
  %4 = tail call noundef zeroext i1 @_Z3cani(i32 noundef %3)
  %5 = load i32, ptr @mid, align 4, !tbaa !5
  br i1 %4, label %6, label %8

6:                                                ; preds = %.lr.ph
  store i32 %5, ptr @ans, align 4, !tbaa !5
  %7 = add nsw i32 %5, -1
  store i32 %7, ptr @r, align 4, !tbaa !5
  %.pre = load i32, ptr @l, align 4, !tbaa !5
  br label %10

8:                                                ; preds = %.lr.ph
  %9 = add nsw i32 %5, 1
  store i32 %9, ptr @l, align 4, !tbaa !5
  %.pre3 = load i32, ptr @r, align 4, !tbaa !5
  br label %10

10:                                               ; preds = %8, %6
  %11 = phi i32 [ %.pre3, %8 ], [ %7, %6 ]
  %12 = phi i32 [ %9, %8 ], [ %.pre, %6 ]
  %.not = icmp sgt i32 %12, %11
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !9

._crit_edge:                                      ; preds = %10
  %13 = load i32, ptr @ans, align 4, !tbaa !5
  %14 = tail call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEi(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, i32 noundef %13)
  ret void
}

declare noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEi(ptr noundef nonnull align 8 dereferenceable(8), i32 noundef) local_unnamed_addr #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 = { "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" }

!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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,ReassociatePass,SCCPPass,SimplifyCFGPass,TailCallElimPass