; LLVM IR from kaynewbie/algorithm :: algorithm/CodingInterviews (question-21)

; 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: nounwind uwtable
define dso_local void @reorderArray(ptr noundef %0, i32 noundef %1, ptr noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  store ptr %2, ptr %6, align 8, !tbaa !5
  %10 = load i32, ptr %5, align 4, !tbaa !9
  %11 = icmp sle i32 %10, 0
  br i1 %11, label %12, label %13

12:                                               ; preds = %3
  br label %70

13:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  store i32 0, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %14 = load i32, ptr %5, align 4, !tbaa !9
  %15 = sub nsw i32 %14, 1
  store i32 %15, ptr %8, align 4, !tbaa !9
  br label %16

16:                                               ; preds = %66, %13
  %17 = load i32, ptr %7, align 4, !tbaa !9
  %18 = load i32, ptr %8, align 4, !tbaa !9
  %19 = icmp slt i32 %17, %18
  br i1 %19, label %20, label %67

20:                                               ; preds = %16
  br label %21

21:                                               ; preds = %31, %20
  %22 = load ptr, ptr %6, align 8, !tbaa !5
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  %24 = load i32, ptr %7, align 4, !tbaa !9
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds i32, ptr %23, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !9
  %28 = call zeroext i1 %22(i32 noundef %27)
  %29 = zext i1 %28 to i32
  %30 = icmp eq i32 %29, 0
  br i1 %30, label %31, label %34

31:                                               ; preds = %21
  %32 = load i32, ptr %7, align 4, !tbaa !9
  %33 = add nsw i32 %32, 1
  store i32 %33, ptr %7, align 4, !tbaa !9
  br label %21, !llvm.loop !11

34:                                               ; preds = %21
  br label %35

35:                                               ; preds = %45, %34
  %36 = load ptr, ptr %6, align 8, !tbaa !5
  %37 = load ptr, ptr %4, align 8, !tbaa !5
  %38 = load i32, ptr %8, align 4, !tbaa !9
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds i32, ptr %37, i64 %39
  %41 = load i32, ptr %40, align 4, !tbaa !9
  %42 = call zeroext i1 %36(i32 noundef %41)
  %43 = zext i1 %42 to i32
  %44 = icmp eq i32 %43, 1
  br i1 %44, label %45, label %48

45:                                               ; preds = %35
  %46 = load i32, ptr %8, align 4, !tbaa !9
  %47 = add nsw i32 %46, -1
  store i32 %47, ptr %8, align 4, !tbaa !9
  br label %35, !llvm.loop !13

48:                                               ; preds = %35
  %49 = load i32, ptr %7, align 4, !tbaa !9
  %50 = load i32, ptr %8, align 4, !tbaa !9
  %51 = icmp slt i32 %49, %50
  br i1 %51, label %52, label %65

52:                                               ; preds = %48
  %53 = load ptr, ptr %4, align 8, !tbaa !5
  %54 = load i32, ptr %7, align 4, !tbaa !9
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds i32, ptr %53, i64 %55
  %57 = load ptr, ptr %4, align 8, !tbaa !5
  %58 = load i32, ptr %8, align 4, !tbaa !9
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds i32, ptr %57, i64 %59
  call void @swap(ptr noundef %56, ptr noundef %60)
  %61 = load i32, ptr %7, align 4, !tbaa !9
  %62 = add nsw i32 %61, 1
  store i32 %62, ptr %7, align 4, !tbaa !9
  %63 = load i32, ptr %8, align 4, !tbaa !9
  %64 = add nsw i32 %63, -1
  store i32 %64, ptr %8, align 4, !tbaa !9
  br label %66

65:                                               ; preds = %48
  store i32 1, ptr %9, align 4
  br label %68

66:                                               ; preds = %52
  br label %16, !llvm.loop !14

67:                                               ; preds = %16
  store i32 0, ptr %9, align 4
  br label %68

68:                                               ; preds = %67, %65
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  %69 = load i32, ptr %9, align 4
  switch i32 %69, label %71 [
    i32 0, label %70
    i32 1, label %70
  ]

70:                                               ; preds = %68, %68, %12
  ret void

71:                                               ; preds = %68
  unreachable
}

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

; Function Attrs: nounwind uwtable
declare hidden void @swap(ptr noundef, ptr noundef) #0

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

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" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"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"}
!13 = distinct !{!13, !12}
!14 = distinct !{!14, !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
define dso_local void @reorderArray(ptr noundef %0, i32 noundef %1, ptr nocapture noundef readonly %2) local_unnamed_addr #0 {
  %4 = icmp sgt i32 %1, 1
  br i1 %4, label %.preheader22.preheader, label %.loopexit

.preheader22.preheader:                           ; preds = %3
  %.026 = add nsw i32 %1, -1
  br label %.preheader22

.preheader22:                                     ; preds = %.preheader22.preheader, %19
  %.028 = phi i32 [ %.0, %19 ], [ %.026, %.preheader22.preheader ]
  %.01927 = phi i32 [ %22, %19 ], [ 0, %.preheader22.preheader ]
  %5 = sext i32 %.01927 to i64
  br label %6

6:                                                ; preds = %6, %.preheader22
  %indvars.iv = phi i64 [ %indvars.iv.next, %6 ], [ %5, %.preheader22 ]
  %7 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv
  %8 = load i32, ptr %7, align 4, !tbaa !5
  %9 = tail call zeroext i1 %2(i32 noundef %8) #1
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  br i1 %9, label %.preheader, label %6, !llvm.loop !9

.preheader:                                       ; preds = %6
  %10 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv
  %11 = trunc nsw i64 %indvars.iv to i32
  %12 = sext i32 %.028 to i64
  br label %13

13:                                               ; preds = %13, %.preheader
  %indvars.iv32 = phi i64 [ %indvars.iv.next33, %13 ], [ %12, %.preheader ]
  %14 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv32
  %15 = load i32, ptr %14, align 4, !tbaa !5
  %16 = tail call zeroext i1 %2(i32 noundef %15) #1
  %indvars.iv.next33 = add nsw i64 %indvars.iv32, -1
  br i1 %16, label %13, label %17, !llvm.loop !11

17:                                               ; preds = %13
  %18 = icmp slt i64 %indvars.iv, %indvars.iv32
  br i1 %18, label %19, label %.loopexit

19:                                               ; preds = %17
  %20 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv32
  %21 = trunc nsw i64 %indvars.iv32 to i32
  tail call void @swap(ptr noundef nonnull %10, ptr noundef nonnull %20)
  %22 = add nsw i32 %11, 1
  %.0 = add nsw i32 %21, -1
  %23 = icmp slt i32 %22, %.0
  br i1 %23, label %.preheader22, label %.loopexit, !llvm.loop !12

.loopexit:                                        ; preds = %19, %17, %3
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden void @swap(ptr noundef, ptr noundef) local_unnamed_addr #0

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


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