; LLVM IR from xergioalex/analysisOfSortAlgorithms :: algorithms/c (benchmark)

; 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 void @swap(i32 noundef, i32 noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @partition(i32 noundef %0, i32 noundef %1, i32 noundef %2, ptr noundef %3) #1 {
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  store i32 %0, ptr %5, align 4, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !5
  store ptr %3, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  %11 = load i32, ptr %5, align 4, !tbaa !5
  %12 = sub nsw i32 %11, 1
  store i32 %12, ptr %9, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  %13 = load i32, ptr %6, align 4, !tbaa !5
  store i32 %13, ptr %10, align 4, !tbaa !5
  br label %14

14:                                               ; preds = %51, %4
  br label %15

15:                                               ; preds = %14
  br label %16

16:                                               ; preds = %25, %15
  %17 = load ptr, ptr %8, align 8, !tbaa !9
  %18 = load i32, ptr %9, align 4, !tbaa !5
  %19 = add nsw i32 %18, 1
  store i32 %19, ptr %9, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds i32, ptr %17, i64 %20
  %22 = load i32, ptr %21, align 4, !tbaa !5
  %23 = load i32, ptr %7, align 4, !tbaa !5
  %24 = icmp slt i32 %22, %23
  br i1 %24, label %25, label %26

25:                                               ; preds = %16
  br label %16, !llvm.loop !11

26:                                               ; preds = %16
  br label %27

27:                                               ; preds = %41, %26
  %28 = load i32, ptr %10, align 4, !tbaa !5
  %29 = icmp sgt i32 %28, 0
  br i1 %29, label %30, label %39

30:                                               ; preds = %27
  %31 = load ptr, ptr %8, align 8, !tbaa !9
  %32 = load i32, ptr %10, align 4, !tbaa !5
  %33 = add nsw i32 %32, -1
  store i32 %33, ptr %10, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds i32, ptr %31, i64 %34
  %36 = load i32, ptr %35, align 4, !tbaa !5
  %37 = load i32, ptr %7, align 4, !tbaa !5
  %38 = icmp sgt i32 %36, %37
  br label %39

39:                                               ; preds = %30, %27
  %40 = phi i1 [ false, %27 ], [ %38, %30 ]
  br i1 %40, label %41, label %42

41:                                               ; preds = %39
  br label %27, !llvm.loop !13

42:                                               ; preds = %39
  %43 = load i32, ptr %9, align 4, !tbaa !5
  %44 = load i32, ptr %10, align 4, !tbaa !5
  %45 = icmp sge i32 %43, %44
  br i1 %45, label %46, label %47

46:                                               ; preds = %42
  br label %52

47:                                               ; preds = %42
  %48 = load i32, ptr %9, align 4, !tbaa !5
  %49 = load i32, ptr %10, align 4, !tbaa !5
  %50 = load ptr, ptr %8, align 8, !tbaa !9
  call void @swap(i32 noundef %48, i32 noundef %49, ptr noundef %50)
  br label %51

51:                                               ; preds = %47
  br label %14

52:                                               ; preds = %46
  %53 = load i32, ptr %9, align 4, !tbaa !5
  %54 = load i32, ptr %6, align 4, !tbaa !5
  %55 = load ptr, ptr %8, align 8, !tbaa !9
  call void @swap(i32 noundef %53, i32 noundef %54, ptr noundef %55)
  %56 = load i32, ptr %9, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  ret i32 %56
}

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 = { 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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


; 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 void @swap(i32 noundef, i32 noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local range(i32 -2147483647, -2147483648) i32 @partition(i32 noundef %0, i32 noundef %1, i32 noundef %2, ptr noundef %3) local_unnamed_addr #0 {
  %5 = add nsw i32 %0, -1
  br label %6

6:                                                ; preds = %21, %4
  %.019 = phi i32 [ %5, %4 ], [ %13, %21 ]
  %.0 = phi i32 [ %1, %4 ], [ %.2, %21 ]
  %7 = sext i32 %.019 to i64
  br label %8

8:                                                ; preds = %8, %6
  %indvars.iv = phi i64 [ %indvars.iv.next, %8 ], [ %7, %6 ]
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %9 = getelementptr inbounds i32, ptr %3, i64 %indvars.iv.next
  %10 = load i32, ptr %9, align 4, !tbaa !5
  %11 = icmp slt i32 %10, %2
  br i1 %11, label %8, label %12, !llvm.loop !9

12:                                               ; preds = %8
  %13 = trunc nsw i64 %indvars.iv.next to i32
  %.old1 = icmp sgt i32 %.0, 0
  br i1 %.old1, label %.preheader.preheader, label %.critedge

.preheader.preheader:                             ; preds = %12
  %14 = zext nneg i32 %.0 to i64
  br label %.preheader

.preheader:                                       ; preds = %.preheader.preheader, %.preheader
  %indvars.iv27 = phi i64 [ %14, %.preheader.preheader ], [ %15, %.preheader ]
  %15 = add nsw i64 %indvars.iv27, -1
  %16 = getelementptr inbounds i32, ptr %3, i64 %15
  %17 = load i32, ptr %16, align 4, !tbaa !5
  %18 = icmp sgt i32 %17, %2
  %19 = icmp ugt i64 %indvars.iv27, 1
  %or.cond = and i1 %19, %18
  br i1 %or.cond, label %.preheader, label %.critedge.loopexit, !llvm.loop !11

.critedge.loopexit:                               ; preds = %.preheader
  %20 = trunc i64 %15 to i32
  br label %.critedge

.critedge:                                        ; preds = %.critedge.loopexit, %12
  %.2 = phi i32 [ %.0, %12 ], [ %20, %.critedge.loopexit ]
  %.not = icmp sgt i32 %.2, %13
  br i1 %.not, label %21, label %22

21:                                               ; preds = %.critedge
  tail call void @swap(i32 noundef %13, i32 noundef %.2, ptr noundef nonnull %3)
  br label %6

22:                                               ; preds = %.critedge
  tail call void @swap(i32 noundef %13, i32 noundef %1, ptr noundef nonnull %3)
  ret i32 %13
}

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"}
!11 = distinct !{!11, !10}


; Effective O3 passes: CorrelatedValuePropagationPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass