; LLVM IR from ahampriyanshu/algo-ds-101 :: Algorithms/Sorting/Circle_Sort (Circle_Sort)

; 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 i32 @circle_sort_inner(ptr noundef %0, ptr noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = load ptr, ptr %5, align 8, !tbaa !5
  %13 = icmp eq ptr %11, %12
  br i1 %13, label %14, label %15

14:                                               ; preds = %2
  store i32 0, ptr %3, align 4
  store i32 1, ptr %10, align 4
  br label %64

15:                                               ; preds = %2
  store i32 0, ptr %9, align 4, !tbaa !9
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %16, ptr %6, align 8, !tbaa !5
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %17, ptr %7, align 8, !tbaa !5
  br label %18

18:                                               ; preds = %49, %15
  %19 = load ptr, ptr %6, align 8, !tbaa !5
  %20 = load ptr, ptr %7, align 8, !tbaa !5
  %21 = icmp ult ptr %19, %20
  br i1 %21, label %32, label %22

22:                                               ; preds = %18
  %23 = load ptr, ptr %6, align 8, !tbaa !5
  %24 = load ptr, ptr %7, align 8, !tbaa !5
  %25 = icmp eq ptr %23, %24
  br i1 %25, label %26, label %30

26:                                               ; preds = %22
  %27 = load ptr, ptr %7, align 8, !tbaa !5
  %28 = getelementptr inbounds i32, ptr %27, i32 1
  store ptr %28, ptr %7, align 8, !tbaa !5
  %29 = icmp ne ptr %28, null
  br label %30

30:                                               ; preds = %26, %22
  %31 = phi i1 [ false, %22 ], [ %29, %26 ]
  br label %32

32:                                               ; preds = %30, %18
  %33 = phi i1 [ true, %18 ], [ %31, %30 ]
  br i1 %33, label %34, label %54

34:                                               ; preds = %32
  %35 = load ptr, ptr %6, align 8, !tbaa !5
  %36 = load i32, ptr %35, align 4, !tbaa !9
  %37 = load ptr, ptr %7, align 8, !tbaa !5
  %38 = load i32, ptr %37, align 4, !tbaa !9
  %39 = icmp sgt i32 %36, %38
  br i1 %39, label %40, label %48

40:                                               ; preds = %34
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  %42 = load i32, ptr %41, align 4, !tbaa !9
  store i32 %42, ptr %8, align 4, !tbaa !9
  %43 = load ptr, ptr %7, align 8, !tbaa !5
  %44 = load i32, ptr %43, align 4, !tbaa !9
  %45 = load ptr, ptr %6, align 8, !tbaa !5
  store i32 %44, ptr %45, align 4, !tbaa !9
  %46 = load i32, ptr %8, align 4, !tbaa !9
  %47 = load ptr, ptr %7, align 8, !tbaa !5
  store i32 %46, ptr %47, align 4, !tbaa !9
  store i32 1, ptr %9, align 4, !tbaa !9
  br label %48

48:                                               ; preds = %40, %34
  br label %49

49:                                               ; preds = %48
  %50 = load ptr, ptr %6, align 8, !tbaa !5
  %51 = getelementptr inbounds i32, ptr %50, i32 1
  store ptr %51, ptr %6, align 8, !tbaa !5
  %52 = load ptr, ptr %7, align 8, !tbaa !5
  %53 = getelementptr inbounds i32, ptr %52, i32 -1
  store ptr %53, ptr %7, align 8, !tbaa !5
  br label %18, !llvm.loop !11

54:                                               ; preds = %32
  %55 = load i32, ptr %9, align 4, !tbaa !9
  %56 = load ptr, ptr %4, align 8, !tbaa !5
  %57 = load ptr, ptr %7, align 8, !tbaa !5
  %58 = call i32 @circle_sort_inner(ptr noundef %56, ptr noundef %57)
  %59 = or i32 %55, %58
  %60 = load ptr, ptr %6, align 8, !tbaa !5
  %61 = load ptr, ptr %5, align 8, !tbaa !5
  %62 = call i32 @circle_sort_inner(ptr noundef %60, ptr noundef %61)
  %63 = or i32 %59, %62
  store i32 %63, ptr %3, align 4
  store i32 1, ptr %10, align 4
  br label %64

64:                                               ; preds = %54, %14
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  %65 = load i32, ptr %3, align 4
  ret i32 %65
}

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

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


; 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: nofree nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local range(i32 0, 2) i32 @circle_sort_inner(ptr noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  %3 = icmp eq ptr %0, %1
  br i1 %3, label %common.ret33, label %.preheader

.preheader:                                       ; preds = %2, %13
  %.028 = phi ptr [ %14, %13 ], [ %0, %2 ]
  %.026 = phi ptr [ %15, %13 ], [ %1, %2 ]
  %.0 = phi i32 [ %.1, %13 ], [ 0, %2 ]
  %4 = icmp ult ptr %.028, %.026
  br i1 %4, label %.critedge, label %5

5:                                                ; preds = %.preheader
  %6 = icmp eq ptr %.028, %.026
  br i1 %6, label %7, label %.critedge2

7:                                                ; preds = %5
  %8 = getelementptr inbounds i8, ptr %.026, i64 4
  br label %.critedge

.critedge:                                        ; preds = %7, %.preheader
  %.127 = phi ptr [ %.026, %.preheader ], [ %8, %7 ]
  %9 = load i32, ptr %.028, align 4, !tbaa !5
  %10 = load i32, ptr %.127, align 4, !tbaa !5
  %11 = icmp sgt i32 %9, %10
  br i1 %11, label %12, label %13

12:                                               ; preds = %.critedge
  store i32 %10, ptr %.028, align 4, !tbaa !5
  store i32 %9, ptr %.127, align 4, !tbaa !5
  br label %13

13:                                               ; preds = %.critedge, %12
  %.1 = phi i32 [ 1, %12 ], [ %.0, %.critedge ]
  %14 = getelementptr inbounds i8, ptr %.028, i64 4
  %15 = getelementptr inbounds i8, ptr %.127, i64 -4
  br label %.preheader, !llvm.loop !9

common.ret33:                                     ; preds = %2, %.critedge2
  %common.ret33.op = phi i32 [ %19, %.critedge2 ], [ 0, %2 ]
  ret i32 %common.ret33.op

.critedge2:                                       ; preds = %5
  %16 = tail call i32 @circle_sort_inner(ptr noundef %0, ptr noundef %.026)
  %17 = tail call i32 @circle_sort_inner(ptr noundef %.028, ptr noundef %1)
  %18 = or i32 %16, %17
  %19 = or i32 %18, %.0
  br label %common.ret33
}

attributes #0 = { nofree nosync nounwind memory(readwrite, 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 = !{!"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: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass