; LLVM IR from bnidia/push_swap :: sort (sort_final)

; 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_minmaxmed = type { i32, i32, i32 }
%struct.s_listc = type { i32, i32, i32, i32, i32, ptr, ptr }

; Function Attrs: nounwind uwtable
define dso_local void @sort_final(ptr noundef %0, i64 %1, i32 %2) #0 {
  %4 = alloca %struct.s_minmaxmed, align 4
  %5 = alloca { i64, i32 }, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = getelementptr inbounds { i64, i32 }, ptr %5, i32 0, i32 0
  store i64 %1, ptr %10, align 4
  %11 = getelementptr inbounds { i64, i32 }, ptr %5, i32 0, i32 1
  store i32 %2, ptr %11, align 4
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %4, ptr align 4 %5, i64 12, i1 false)
  store ptr %0, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  %12 = load ptr, ptr %6, align 8, !tbaa !5
  %13 = load ptr, ptr %12, align 8, !tbaa !5
  store ptr %13, ptr %7, align 8, !tbaa !5
  store i32 0, ptr %9, align 4, !tbaa !9
  %14 = load ptr, ptr %7, align 8, !tbaa !5
  %15 = call i32 @ft_lstc_size(ptr noundef %14)
  store i32 %15, ptr %8, align 4, !tbaa !9
  br label %16

16:                                               ; preds = %23, %3
  %17 = load ptr, ptr %7, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.s_listc, ptr %17, i32 0, i32 0
  %19 = load i32, ptr %18, align 8, !tbaa !11
  %20 = getelementptr inbounds %struct.s_minmaxmed, ptr %4, i32 0, i32 0
  %21 = load i32, ptr %20, align 4, !tbaa !13
  %22 = icmp ne i32 %19, %21
  br i1 %22, label %23, label %29

23:                                               ; preds = %16
  %24 = load ptr, ptr %7, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.s_listc, ptr %24, i32 0, i32 6
  %26 = load ptr, ptr %25, align 8, !tbaa !15
  store ptr %26, ptr %7, align 8, !tbaa !5
  %27 = load i32, ptr %9, align 4, !tbaa !9
  %28 = add nsw i32 %27, 1
  store i32 %28, ptr %9, align 4, !tbaa !9
  br label %16, !llvm.loop !16

29:                                               ; preds = %16
  %30 = load i32, ptr %9, align 4, !tbaa !9
  %31 = load i32, ptr %8, align 4, !tbaa !9
  %32 = sdiv i32 %31, 2
  %33 = icmp slt i32 %30, %32
  br i1 %33, label %34, label %46

34:                                               ; preds = %29
  br label %35

35:                                               ; preds = %43, %34
  %36 = load ptr, ptr %6, align 8, !tbaa !5
  %37 = load ptr, ptr %36, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.s_listc, ptr %37, i32 0, i32 0
  %39 = load i32, ptr %38, align 8, !tbaa !11
  %40 = getelementptr inbounds %struct.s_minmaxmed, ptr %4, i32 0, i32 0
  %41 = load i32, ptr %40, align 4, !tbaa !13
  %42 = icmp ne i32 %39, %41
  br i1 %42, label %43, label %45

43:                                               ; preds = %35
  %44 = load ptr, ptr %6, align 8, !tbaa !5
  call void @rra(ptr noundef %44, i1 noundef zeroext true)
  br label %35, !llvm.loop !18

45:                                               ; preds = %35
  br label %58

46:                                               ; preds = %29
  br label %47

47:                                               ; preds = %55, %46
  %48 = load ptr, ptr %6, align 8, !tbaa !5
  %49 = load ptr, ptr %48, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.s_listc, ptr %49, i32 0, i32 0
  %51 = load i32, ptr %50, align 8, !tbaa !11
  %52 = getelementptr inbounds %struct.s_minmaxmed, ptr %4, i32 0, i32 0
  %53 = load i32, ptr %52, align 4, !tbaa !13
  %54 = icmp ne i32 %51, %53
  br i1 %54, label %55, label %57

55:                                               ; preds = %47
  %56 = load ptr, ptr %6, align 8, !tbaa !5
  call void @ra(ptr noundef %56, i1 noundef zeroext true)
  br label %47, !llvm.loop !19

57:                                               ; preds = %47
  br label %58

58:                                               ; preds = %57, %45
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  ret void
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

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

declare i32 @ft_lstc_size(ptr noundef) #3

declare void @rra(ptr noundef, i1 noundef zeroext) #3

declare void @ra(ptr noundef, i1 noundef zeroext) #3

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

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 nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { "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 #4 = { 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 = !{!12, !10, i64 0}
!12 = !{!"s_listc", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12, !10, i64 16, !6, i64 24, !6, i64 32}
!13 = !{!14, !10, i64 0}
!14 = !{!"s_minmaxmed", !10, i64 0, !10, i64 4, !10, i64 8}
!15 = !{!12, !6, i64 32}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = distinct !{!18, !17}
!19 = distinct !{!19, !17}


; 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 @sort_final(ptr noundef %0, i64 %1, i32 %2) local_unnamed_addr #0 {
  %.sroa.010.0.extract.trunc = trunc i64 %1 to i32
  %4 = load ptr, ptr %0, align 8, !tbaa !5
  %5 = tail call i32 @ft_lstc_size(ptr noundef %4) #2
  %6 = load i32, ptr %4, align 8, !tbaa !9
  %.not18 = icmp eq i32 %6, %.sroa.010.0.extract.trunc
  br i1 %.not18, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %3, %.lr.ph
  %.020 = phi i32 [ %9, %.lr.ph ], [ 0, %3 ]
  %.01319 = phi ptr [ %8, %.lr.ph ], [ %4, %3 ]
  %7 = getelementptr inbounds i8, ptr %.01319, i64 32
  %8 = load ptr, ptr %7, align 8, !tbaa !12
  %9 = add nuw nsw i32 %.020, 1
  %10 = load i32, ptr %8, align 8, !tbaa !9
  %.not = icmp eq i32 %10, %.sroa.010.0.extract.trunc
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !13

._crit_edge:                                      ; preds = %.lr.ph, %3
  %.0.lcssa = phi i32 [ 0, %3 ], [ %9, %.lr.ph ]
  %11 = sdiv i32 %5, 2
  %12 = icmp slt i32 %.0.lcssa, %11
  %13 = load ptr, ptr %0, align 8, !tbaa !5
  %14 = load i32, ptr %13, align 8, !tbaa !9
  %.not1523 = icmp eq i32 %14, %.sroa.010.0.extract.trunc
  br i1 %12, label %.preheader, label %.preheader16

.preheader16:                                     ; preds = %._crit_edge
  br i1 %.not1523, label %.loopexit, label %.lr.ph22

.preheader:                                       ; preds = %._crit_edge
  br i1 %.not1523, label %.loopexit, label %.lr.ph24

.lr.ph24:                                         ; preds = %.preheader, %.lr.ph24
  tail call void @rra(ptr noundef nonnull %0, i1 noundef zeroext true) #2
  %15 = load ptr, ptr %0, align 8, !tbaa !5
  %16 = load i32, ptr %15, align 8, !tbaa !9
  %.not15 = icmp eq i32 %16, %.sroa.010.0.extract.trunc
  br i1 %.not15, label %.loopexit, label %.lr.ph24, !llvm.loop !15

.lr.ph22:                                         ; preds = %.preheader16, %.lr.ph22
  tail call void @ra(ptr noundef nonnull %0, i1 noundef zeroext true) #2
  %17 = load ptr, ptr %0, align 8, !tbaa !5
  %18 = load i32, ptr %17, align 8, !tbaa !9
  %.not14 = icmp eq i32 %18, %.sroa.010.0.extract.trunc
  br i1 %.not14, label %.loopexit, label %.lr.ph22, !llvm.loop !16

.loopexit:                                        ; preds = %.lr.ph22, %.lr.ph24, %.preheader16, %.preheader
  ret void
}

declare i32 @ft_lstc_size(ptr noundef) local_unnamed_addr #1

declare void @rra(ptr noundef, i1 noundef zeroext) local_unnamed_addr #1

declare void @ra(ptr noundef, i1 noundef zeroext) local_unnamed_addr #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 = { "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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 0}
!10 = !{!"s_listc", !11, i64 0, !11, i64 4, !11, i64 8, !11, i64 12, !11, i64 16, !6, i64 24, !6, i64 32}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !6, i64 32}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}
!16 = distinct !{!16, !14}


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