; LLVM IR from simonaertssen/MIT-6.172-Performance-Engineering-of-Software-Systems :: Week 2 Bentleys rules and bit hacks/recitation (qsort)

; 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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @partition(ptr noundef, i32 noundef, i32 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @quickSortIterative(ptr noundef %0, i32 noundef %1, i32 noundef %2) #1 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  store i32 %2, ptr %6, align 4, !tbaa !9
  %11 = load i32, ptr %6, align 4, !tbaa !9
  %12 = load i32, ptr %5, align 4, !tbaa !9
  %13 = sub nsw i32 %11, %12
  %14 = add nsw i32 %13, 1
  %15 = zext i32 %14 to i64
  %16 = call ptr @llvm.stacksave.p0()
  store ptr %16, ptr %7, align 8
  %17 = alloca i32, i64 %15, align 16
  store i64 %15, ptr %8, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  store i32 -1, ptr %9, align 4, !tbaa !9
  %18 = load i32, ptr %5, align 4, !tbaa !9
  %19 = load i32, ptr %9, align 4, !tbaa !9
  %20 = add nsw i32 %19, 1
  store i32 %20, ptr %9, align 4, !tbaa !9
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds i32, ptr %17, i64 %21
  store i32 %18, ptr %22, align 4, !tbaa !9
  %23 = load i32, ptr %6, align 4, !tbaa !9
  %24 = load i32, ptr %9, align 4, !tbaa !9
  %25 = add nsw i32 %24, 1
  store i32 %25, ptr %9, align 4, !tbaa !9
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds i32, ptr %17, i64 %26
  store i32 %23, ptr %27, align 4, !tbaa !9
  br label %28

28:                                               ; preds = %79, %3
  %29 = load i32, ptr %9, align 4, !tbaa !9
  %30 = icmp sge i32 %29, 0
  br i1 %30, label %31, label %80

31:                                               ; preds = %28
  %32 = load i32, ptr %9, align 4, !tbaa !9
  %33 = add nsw i32 %32, -1
  store i32 %33, ptr %9, align 4, !tbaa !9
  %34 = sext i32 %32 to i64
  %35 = getelementptr inbounds i32, ptr %17, i64 %34
  %36 = load i32, ptr %35, align 4, !tbaa !9
  store i32 %36, ptr %6, align 4, !tbaa !9
  %37 = load i32, ptr %9, align 4, !tbaa !9
  %38 = add nsw i32 %37, -1
  store i32 %38, ptr %9, align 4, !tbaa !9
  %39 = sext i32 %37 to i64
  %40 = getelementptr inbounds i32, ptr %17, i64 %39
  %41 = load i32, ptr %40, align 4, !tbaa !9
  store i32 %41, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  %42 = load ptr, ptr %4, align 8, !tbaa !5
  %43 = load i32, ptr %5, align 4, !tbaa !9
  %44 = load i32, ptr %6, align 4, !tbaa !9
  %45 = call i32 @partition(ptr noundef %42, i32 noundef %43, i32 noundef %44)
  store i32 %45, ptr %10, align 4, !tbaa !9
  %46 = load i32, ptr %10, align 4, !tbaa !9
  %47 = sub nsw i32 %46, 1
  %48 = load i32, ptr %5, align 4, !tbaa !9
  %49 = icmp sgt i32 %47, %48
  br i1 %49, label %50, label %62

50:                                               ; preds = %31
  %51 = load i32, ptr %5, align 4, !tbaa !9
  %52 = load i32, ptr %9, align 4, !tbaa !9
  %53 = add nsw i32 %52, 1
  store i32 %53, ptr %9, align 4, !tbaa !9
  %54 = sext i32 %53 to i64
  %55 = getelementptr inbounds i32, ptr %17, i64 %54
  store i32 %51, ptr %55, align 4, !tbaa !9
  %56 = load i32, ptr %10, align 4, !tbaa !9
  %57 = sub nsw i32 %56, 1
  %58 = load i32, ptr %9, align 4, !tbaa !9
  %59 = add nsw i32 %58, 1
  store i32 %59, ptr %9, align 4, !tbaa !9
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds i32, ptr %17, i64 %60
  store i32 %57, ptr %61, align 4, !tbaa !9
  br label %62

62:                                               ; preds = %50, %31
  %63 = load i32, ptr %10, align 4, !tbaa !9
  %64 = add nsw i32 %63, 1
  %65 = load i32, ptr %6, align 4, !tbaa !9
  %66 = icmp slt i32 %64, %65
  br i1 %66, label %67, label %79

67:                                               ; preds = %62
  %68 = load i32, ptr %10, align 4, !tbaa !9
  %69 = add nsw i32 %68, 1
  %70 = load i32, ptr %9, align 4, !tbaa !9
  %71 = add nsw i32 %70, 1
  store i32 %71, ptr %9, align 4, !tbaa !9
  %72 = sext i32 %71 to i64
  %73 = getelementptr inbounds i32, ptr %17, i64 %72
  store i32 %69, ptr %73, align 4, !tbaa !9
  %74 = load i32, ptr %6, align 4, !tbaa !9
  %75 = load i32, ptr %9, align 4, !tbaa !9
  %76 = add nsw i32 %75, 1
  store i32 %76, ptr %9, align 4, !tbaa !9
  %77 = sext i32 %76 to i64
  %78 = getelementptr inbounds i32, ptr %17, i64 %77
  store i32 %74, ptr %78, align 4, !tbaa !9
  br label %79

79:                                               ; preds = %67, %62
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  br label %28, !llvm.loop !11

80:                                               ; preds = %28
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  %81 = load ptr, ptr %7, align 8
  call void @llvm.stackrestore.p0(ptr %81)
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #2

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 = { nocallback nofree nosync nounwind willreturn }
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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"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"

; Function Attrs: nounwind uwtable
declare dso_local i32 @partition(ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @quickSortIterative(ptr noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %reass.sub = sub i32 %2, %1
  %4 = add i32 %reass.sub, 1
  %5 = zext i32 %4 to i64
  %6 = alloca i32, i64 %5, align 16
  store i32 %1, ptr %6, align 16, !tbaa !5
  %7 = getelementptr i8, ptr %6, i64 4
  store i32 %2, ptr %7, align 4, !tbaa !5
  br label %8

8:                                                ; preds = %3, %27
  %.028 = phi i32 [ 1, %3 ], [ %.2, %27 ]
  %9 = zext nneg i32 %.028 to i64
  %10 = getelementptr inbounds i32, ptr %6, i64 %9
  %11 = load i32, ptr %10, align 4, !tbaa !5
  %12 = add nsw i32 %.028, -2
  %13 = getelementptr i8, ptr %10, i64 -4
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %15 = tail call i32 @partition(ptr noundef %0, i32 noundef %14, i32 noundef %11)
  %16 = add nsw i32 %15, -1
  %17 = icmp sgt i32 %16, %14
  br i1 %17, label %18, label %19

18:                                               ; preds = %8
  store i32 %16, ptr %10, align 4, !tbaa !5
  br label %19

19:                                               ; preds = %18, %8
  %.1 = phi i32 [ %.028, %18 ], [ %12, %8 ]
  %20 = add nsw i32 %15, 1
  %21 = icmp slt i32 %20, %11
  br i1 %21, label %22, label %27

22:                                               ; preds = %19
  %23 = sext i32 %.1 to i64
  %gep = getelementptr i32, ptr %7, i64 %23
  store i32 %20, ptr %gep, align 4, !tbaa !5
  %24 = add nsw i32 %.1, 2
  %25 = zext nneg i32 %24 to i64
  %26 = getelementptr inbounds i32, ptr %6, i64 %25
  store i32 %11, ptr %26, align 4, !tbaa !5
  br label %27

27:                                               ; preds = %22, %19
  %.2 = phi i32 [ %24, %22 ], [ %.1, %19 ]
  %28 = icmp sgt i32 %.2, -1
  br i1 %28, label %8, label %29, !llvm.loop !9

29:                                               ; preds = %27
  ret void
}

attributes #0 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,LICMPass,LoopRotatePass,ReassociatePass,SROAPass,TailCallElimPass