; LLVM IR from AdrianWR/push_swap :: src/push_swap (sort_complex)

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

%struct.s_stack = type { i32, i32, ptr }

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

declare i32 @min(ptr noundef) #1

declare void @smart_rotate_a(ptr noundef, i32 noundef) #1

; 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 @sort_chunk(ptr noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local void @move_chunk(ptr noundef, ptr noundef, i32 noundef, i32 noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local i64 @get_chunks(ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local void @sort_complex(ptr noundef %0, ptr noundef %1) #2 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = call i64 @get_chunks(ptr noundef %9)
  store i64 %10, ptr %7, align 8, !tbaa !9
  %11 = load ptr, ptr %3, align 8, !tbaa !5
  %12 = call i32 @max(ptr noundef %11)
  store i32 %12, ptr %6, align 4, !tbaa !11
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.s_stack, ptr %13, i32 0, i32 0
  %15 = load i32, ptr %14, align 8, !tbaa !13
  %16 = add nsw i32 %15, 1
  %17 = sext i32 %16 to i64
  %18 = load i64, ptr %7, align 8, !tbaa !9
  %19 = udiv i64 %17, %18
  store i64 %19, ptr %8, align 8, !tbaa !9
  br label %20

20:                                               ; preds = %36, %2
  %21 = load i64, ptr %7, align 8, !tbaa !9
  %22 = icmp uge i64 %21, 1
  br i1 %22, label %23, label %47

23:                                               ; preds = %20
  %24 = load i64, ptr %7, align 8, !tbaa !9
  %25 = icmp eq i64 %24, 1
  br i1 %25, label %26, label %29

26:                                               ; preds = %23
  %27 = load ptr, ptr %3, align 8, !tbaa !5
  %28 = call i32 @min(ptr noundef %27)
  store i32 %28, ptr %5, align 4, !tbaa !11
  br label %36

29:                                               ; preds = %23
  %30 = load i32, ptr %6, align 4, !tbaa !11
  %31 = sext i32 %30 to i64
  %32 = load i64, ptr %8, align 8, !tbaa !9
  %33 = sub i64 %31, %32
  %34 = add i64 %33, 1
  %35 = trunc i64 %34 to i32
  store i32 %35, ptr %5, align 4, !tbaa !11
  br label %36

36:                                               ; preds = %29, %26
  %37 = load ptr, ptr %3, align 8, !tbaa !5
  %38 = load ptr, ptr %4, align 8, !tbaa !5
  %39 = load i32, ptr %5, align 4, !tbaa !11
  %40 = load i32, ptr %6, align 4, !tbaa !11
  call void @move_chunk(ptr noundef %37, ptr noundef %38, i32 noundef %39, i32 noundef %40)
  %41 = load ptr, ptr %3, align 8, !tbaa !5
  %42 = load ptr, ptr %4, align 8, !tbaa !5
  call void @sort_chunk(ptr noundef %41, ptr noundef %42)
  %43 = load i32, ptr %5, align 4, !tbaa !11
  %44 = sub nsw i32 %43, 1
  store i32 %44, ptr %6, align 4, !tbaa !11
  %45 = load i64, ptr %7, align 8, !tbaa !9
  %46 = add i64 %45, -1
  store i64 %46, ptr %7, align 8, !tbaa !9
  br label %20, !llvm.loop !15

47:                                               ; preds = %20
  %48 = load ptr, ptr %3, align 8, !tbaa !5
  %49 = load ptr, ptr %3, align 8, !tbaa !5
  %50 = call i32 @min(ptr noundef %49)
  call void @smart_rotate_a(ptr noundef %48, i32 noundef %50)
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, 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
  ret void
}

declare i32 @max(ptr noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "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 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !12, i64 0}
!14 = !{!"s_stack", !12, i64 0, !12, i64 4, !6, i64 8}
!15 = distinct !{!15, !16}
!16 = !{!"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"

declare i32 @min(ptr noundef) local_unnamed_addr #0

declare void @smart_rotate_a(ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @sort_chunk(ptr noundef, ptr noundef) local_unnamed_addr #1

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

; Function Attrs: nounwind uwtable
declare dso_local i64 @get_chunks(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @sort_complex(ptr noundef %0, ptr noundef %1) local_unnamed_addr #1 {
  %3 = tail call i64 @get_chunks(ptr noundef %0)
  %4 = tail call i32 @max(ptr noundef %0) #2
  %5 = load i32, ptr %0, align 8, !tbaa !5
  %6 = add nsw i32 %5, 1
  %7 = sext i32 %6 to i64
  %8 = udiv i64 %7, %3
  %9 = trunc i64 %8 to i32
  br label %10

10:                                               ; preds = %15, %2
  %.019 = phi i32 [ %4, %2 ], [ %16, %15 ]
  %.018 = phi i64 [ %3, %2 ], [ %17, %15 ]
  switch i64 %.018, label %13 [
    i64 0, label %18
    i64 1, label %11
  ]

11:                                               ; preds = %10
  %12 = tail call i32 @min(ptr noundef nonnull %0) #2
  br label %15

13:                                               ; preds = %10
  %reass.sub = sub i32 %.019, %9
  %14 = add i32 %reass.sub, 1
  br label %15

15:                                               ; preds = %13, %11
  %.0 = phi i32 [ %12, %11 ], [ %14, %13 ]
  tail call void @move_chunk(ptr noundef nonnull %0, ptr noundef %1, i32 noundef %.0, i32 noundef %.019)
  tail call void @sort_chunk(ptr noundef nonnull %0, ptr noundef %1)
  %16 = add nsw i32 %.0, -1
  %17 = add i64 %.018, -1
  br label %10, !llvm.loop !11

18:                                               ; preds = %10
  %19 = tail call i32 @min(ptr noundef nonnull %0) #2
  tail call void @smart_rotate_a(ptr noundef nonnull %0, i32 noundef %19) #2
  ret void
}

declare i32 @max(ptr noundef) local_unnamed_addr #0

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


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,LICMPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass