; LLVM IR from PetarV-/Algorithms :: Sorting Algorithms/Quicksort.ll (Z5qsortii)

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

@niz = external dso_local global [1000001 x i32], align 16

; Function Attrs: mustprogress uwtable
define dso_local void @_Z5qsortii(i32 noundef %0, i32 noundef %1) #0 {
  %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
  store i32 %1, ptr %4, align 4, !tbaa !5
  %8 = load i32, ptr %3, align 4, !tbaa !5
  %9 = load i32, ptr %4, align 4, !tbaa !5
  %10 = icmp slt i32 %8, %9
  br i1 %10, label %11, label %68

11:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %12 = load i32, ptr %3, align 4, !tbaa !5
  store i32 %12, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %13 = load i32, ptr %4, align 4, !tbaa !5
  store i32 %13, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %14 = load i32, ptr %5, align 4, !tbaa !5
  %15 = load i32, ptr %6, align 4, !tbaa !5
  %16 = add nsw i32 %14, %15
  %17 = sdiv i32 %16, 2
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [1000001 x i32], ptr @niz, i64 0, i64 %18
  %20 = load i32, ptr %19, align 4, !tbaa !5
  store i32 %20, ptr %7, align 4, !tbaa !5
  br label %21

21:                                               ; preds = %62, %11
  %22 = load i32, ptr %5, align 4, !tbaa !5
  %23 = load i32, ptr %6, align 4, !tbaa !5
  %24 = icmp sle i32 %22, %23
  br i1 %24, label %25, label %63

25:                                               ; preds = %21
  br label %26

26:                                               ; preds = %33, %25
  %27 = load i32, ptr %5, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [1000001 x i32], ptr @niz, i64 0, i64 %28
  %30 = load i32, ptr %29, align 4, !tbaa !5
  %31 = load i32, ptr %7, align 4, !tbaa !5
  %32 = icmp slt i32 %30, %31
  br i1 %32, label %33, label %36

33:                                               ; preds = %26
  %34 = load i32, ptr %5, align 4, !tbaa !5
  %35 = add nsw i32 %34, 1
  store i32 %35, ptr %5, align 4, !tbaa !5
  br label %26, !llvm.loop !9

36:                                               ; preds = %26
  br label %37

37:                                               ; preds = %44, %36
  %38 = load i32, ptr %6, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [1000001 x i32], ptr @niz, i64 0, i64 %39
  %41 = load i32, ptr %40, align 4, !tbaa !5
  %42 = load i32, ptr %7, align 4, !tbaa !5
  %43 = icmp sgt i32 %41, %42
  br i1 %43, label %44, label %47

44:                                               ; preds = %37
  %45 = load i32, ptr %6, align 4, !tbaa !5
  %46 = add nsw i32 %45, -1
  store i32 %46, ptr %6, align 4, !tbaa !5
  br label %37, !llvm.loop !11

47:                                               ; preds = %37
  %48 = load i32, ptr %5, align 4, !tbaa !5
  %49 = load i32, ptr %6, align 4, !tbaa !5
  %50 = icmp sle i32 %48, %49
  br i1 %50, label %51, label %62

51:                                               ; preds = %47
  %52 = load i32, ptr %5, align 4, !tbaa !5
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds [1000001 x i32], ptr @niz, i64 0, i64 %53
  %55 = load i32, ptr %6, align 4, !tbaa !5
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds [1000001 x i32], ptr @niz, i64 0, i64 %56
  call void @_ZSt4swapIiENSt9enable_ifIXsr6__and_ISt6__not_ISt15__is_tuple_likeIT_EESt21is_move_constructibleIS3_ESt18is_move_assignableIS3_EEE5valueEvE4typeERS3_SC_(ptr noundef nonnull align 4 dereferenceable(4) %54, ptr noundef nonnull align 4 dereferenceable(4) %57) #3
  %58 = load i32, ptr %5, align 4, !tbaa !5
  %59 = add nsw i32 %58, 1
  store i32 %59, ptr %5, align 4, !tbaa !5
  %60 = load i32, ptr %6, align 4, !tbaa !5
  %61 = add nsw i32 %60, -1
  store i32 %61, ptr %6, align 4, !tbaa !5
  br label %62

62:                                               ; preds = %51, %47
  br label %21, !llvm.loop !12

63:                                               ; preds = %21
  %64 = load i32, ptr %3, align 4, !tbaa !5
  %65 = load i32, ptr %6, align 4, !tbaa !5
  call void @_Z5qsortii(i32 noundef %64, i32 noundef %65)
  %66 = load i32, ptr %5, align 4, !tbaa !5
  %67 = load i32, ptr %4, align 4, !tbaa !5
  call void @_Z5qsortii(i32 noundef %66, i32 noundef %67)
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  br label %68

68:                                               ; preds = %63, %2
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local void @_ZSt4swapIiENSt9enable_ifIXsr6__and_ISt6__not_ISt15__is_tuple_likeIT_EESt21is_move_constructibleIS3_ESt18is_move_assignableIS3_EEE5valueEvE4typeERS3_SC_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) #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 = { inlinehint 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !10}


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

@niz = external dso_local global [1000001 x i32], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z5qsortii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp slt i32 %0, %1
  br i1 %3, label %.lr.ph, label %tailrecurse._crit_edge

.lr.ph:                                           ; preds = %2, %tailrecurse
  %.tr36 = phi i32 [ %.2, %tailrecurse ], [ %0, %2 ]
  %4 = add nsw i32 %.tr36, %1
  %5 = sdiv i32 %4, 2
  %6 = sext i32 %5 to i64
  %7 = getelementptr inbounds [1000001 x i32], ptr @niz, i64 0, i64 %6
  %8 = load i32, ptr %7, align 4, !tbaa !5
  br label %.preheader31

.preheader31:                                     ; preds = %.lr.ph, %27
  %.035 = phi i32 [ %.tr36, %.lr.ph ], [ %.2, %27 ]
  %.02434 = phi i32 [ %1, %.lr.ph ], [ %.226, %27 ]
  %9 = sext i32 %.035 to i64
  br label %10

10:                                               ; preds = %10, %.preheader31
  %indvars.iv = phi i64 [ %indvars.iv.next, %10 ], [ %9, %.preheader31 ]
  %11 = getelementptr inbounds [1000001 x i32], ptr @niz, i64 0, i64 %indvars.iv
  %12 = load i32, ptr %11, align 4, !tbaa !5
  %13 = icmp slt i32 %12, %8
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  br i1 %13, label %10, label %.preheader, !llvm.loop !9

.preheader:                                       ; preds = %10
  %14 = getelementptr inbounds [1000001 x i32], ptr @niz, i64 0, i64 %indvars.iv
  %15 = trunc nsw i64 %indvars.iv to i32
  %16 = sext i32 %.02434 to i64
  br label %17

17:                                               ; preds = %17, %.preheader
  %indvars.iv39 = phi i64 [ %indvars.iv.next40, %17 ], [ %16, %.preheader ]
  %18 = getelementptr inbounds [1000001 x i32], ptr @niz, i64 0, i64 %indvars.iv39
  %19 = load i32, ptr %18, align 4, !tbaa !5
  %20 = icmp sgt i32 %19, %8
  %indvars.iv.next40 = add nsw i64 %indvars.iv39, -1
  br i1 %20, label %17, label %21, !llvm.loop !11

21:                                               ; preds = %17
  %22 = trunc nsw i64 %indvars.iv39 to i32
  %.not29 = icmp sgt i64 %indvars.iv, %indvars.iv39
  br i1 %.not29, label %27, label %23

23:                                               ; preds = %21
  %24 = getelementptr inbounds [1000001 x i32], ptr @niz, i64 0, i64 %indvars.iv39
  tail call void @_ZSt4swapIiENSt9enable_ifIXsr6__and_ISt6__not_ISt15__is_tuple_likeIT_EESt21is_move_constructibleIS3_ESt18is_move_assignableIS3_EEE5valueEvE4typeERS3_SC_(ptr noundef nonnull align 4 dereferenceable(4) %14, ptr noundef nonnull align 4 dereferenceable(4) %24) #2
  %25 = add nsw i32 %15, 1
  %26 = add nsw i32 %22, -1
  br label %27

27:                                               ; preds = %23, %21
  %.226 = phi i32 [ %26, %23 ], [ %22, %21 ]
  %.2 = phi i32 [ %25, %23 ], [ %15, %21 ]
  %.not = icmp sgt i32 %.2, %.226
  br i1 %.not, label %tailrecurse, label %.preheader31, !llvm.loop !12

tailrecurse:                                      ; preds = %27
  tail call void @_Z5qsortii(i32 noundef %.tr36, i32 noundef %.226)
  %28 = icmp slt i32 %.2, %1
  br i1 %28, label %.lr.ph, label %tailrecurse._crit_edge

tailrecurse._crit_edge:                           ; preds = %tailrecurse, %2
  ret void
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local void @_ZSt4swapIiENSt9enable_ifIXsr6__and_ISt6__not_ISt15__is_tuple_likeIT_EESt21is_move_constructibleIS3_ESt18is_move_assignableIS3_EEE5valueEvE4typeERS3_SC_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #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 = { inlinehint 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 #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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !10}


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