; LLVM IR from ahr9n/psolving-paradigms :: 3.binary-search/Kattis outofsorts - Out of Sorts.ll (Z2okx)

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

@n = external dso_local global i64, align 8
@x = external dso_local global [1000005 x i64], align 16
@l = external dso_local global i64, align 8
@r = external dso_local global i64, align 8
@mid = external dso_local global i64, align 8

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef zeroext i1 @_Z2okx(i64 noundef %0) #0 {
  %2 = alloca i1, align 1
  %3 = alloca i64, align 8
  store i64 %0, ptr %3, align 8, !tbaa !5
  store i64 1, ptr @l, align 8, !tbaa !5
  %4 = load i64, ptr @n, align 8, !tbaa !5
  store i64 %4, ptr @r, align 8, !tbaa !5
  br label %5

5:                                                ; preds = %32, %1
  %6 = load i64, ptr @l, align 8, !tbaa !5
  %7 = load i64, ptr @r, align 8, !tbaa !5
  %8 = icmp sle i64 %6, %7
  br i1 %8, label %9, label %33

9:                                                ; preds = %5
  %10 = load i64, ptr @l, align 8, !tbaa !5
  %11 = load i64, ptr @r, align 8, !tbaa !5
  %12 = add nsw i64 %10, %11
  %13 = sdiv i64 %12, 2
  store i64 %13, ptr @mid, align 8, !tbaa !5
  %14 = load i64, ptr @mid, align 8, !tbaa !5
  %15 = getelementptr inbounds [1000005 x i64], ptr @x, i64 0, i64 %14
  %16 = load i64, ptr %15, align 8, !tbaa !5
  %17 = load i64, ptr %3, align 8, !tbaa !5
  %18 = icmp eq i64 %16, %17
  br i1 %18, label %19, label %20

19:                                               ; preds = %9
  store i1 true, ptr %2, align 1
  br label %34

20:                                               ; preds = %9
  %21 = load i64, ptr @mid, align 8, !tbaa !5
  %22 = getelementptr inbounds [1000005 x i64], ptr @x, i64 0, i64 %21
  %23 = load i64, ptr %22, align 8, !tbaa !5
  %24 = load i64, ptr %3, align 8, !tbaa !5
  %25 = icmp sgt i64 %23, %24
  br i1 %25, label %26, label %29

26:                                               ; preds = %20
  %27 = load i64, ptr @mid, align 8, !tbaa !5
  %28 = sub nsw i64 %27, 1
  store i64 %28, ptr @r, align 8, !tbaa !5
  br label %32

29:                                               ; preds = %20
  %30 = load i64, ptr @mid, align 8, !tbaa !5
  %31 = add nsw i64 %30, 1
  store i64 %31, ptr @l, align 8, !tbaa !5
  br label %32

32:                                               ; preds = %29, %26
  br label %5, !llvm.loop !9

33:                                               ; preds = %5
  store i1 false, ptr %2, align 1
  br label %34

34:                                               ; preds = %33, %19
  %35 = load i1, ptr %2, align 1
  ret i1 %35
}

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

!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 = !{!"long long", !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"

@n = external dso_local local_unnamed_addr global i64, align 8
@x = external dso_local local_unnamed_addr global [1000005 x i64], align 16
@l = external dso_local local_unnamed_addr global i64, align 8
@r = external dso_local local_unnamed_addr global i64, align 8
@mid = external dso_local local_unnamed_addr global i64, align 8

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef zeroext i1 @_Z2okx(i64 noundef %0) local_unnamed_addr #0 {
  store i64 1, ptr @l, align 8, !tbaa !5
  %2 = load i64, ptr @n, align 8, !tbaa !5
  store i64 %2, ptr @r, align 8, !tbaa !5
  %.not6 = icmp sgt i64 %2, 0
  br i1 %.not6, label %.lr.ph, label %19

.lr.ph:                                           ; preds = %1, %16
  %3 = phi i64 [ %18, %16 ], [ 1, %1 ]
  %4 = phi i64 [ %17, %16 ], [ %2, %1 ]
  %5 = add nsw i64 %4, %3
  %6 = sdiv i64 %5, 2
  %7 = getelementptr inbounds [1000005 x i64], ptr @x, i64 0, i64 %6
  %8 = load i64, ptr %7, align 8, !tbaa !5
  %9 = icmp eq i64 %8, %0
  br i1 %9, label %.sink.split, label %10

10:                                               ; preds = %.lr.ph
  %11 = icmp sgt i64 %8, %0
  br i1 %11, label %12, label %14

12:                                               ; preds = %10
  %13 = add nsw i64 %6, -1
  store i64 %13, ptr @r, align 8, !tbaa !5
  br label %16

14:                                               ; preds = %10
  %15 = add nsw i64 %6, 1
  store i64 %15, ptr @l, align 8, !tbaa !5
  br label %16

16:                                               ; preds = %14, %12
  %17 = phi i64 [ %4, %14 ], [ %13, %12 ]
  %18 = phi i64 [ %15, %14 ], [ %3, %12 ]
  %.not.not = icmp sgt i64 %18, %17
  br i1 %.not.not, label %.sink.split, label %.lr.ph, !llvm.loop !9

.sink.split:                                      ; preds = %.lr.ph, %16
  store i64 %6, ptr @mid, align 8, !tbaa !5
  br label %19

19:                                               ; preds = %.sink.split, %1
  %.not.lcssa = phi i1 [ false, %1 ], [ %9, %.sink.split ]
  ret i1 %.not.lcssa
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) 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" }

!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 = !{!"long long", !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,InstSimplifyPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass