; LLVM IR from h-ssiqueira/Sort_Algorithms :: src/esotericFunMiscellaneous (spaghettiSort)

; 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 @spaghettiSort(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i64, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  %11 = load i32, ptr %4, align 4, !tbaa !9
  %12 = sext i32 %11 to i64
  %13 = mul i64 %12, 8
  %14 = call noalias ptr @malloc(i64 noundef %13) #5
  store ptr %14, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  %16 = load i64, ptr %15, align 8, !tbaa !11
  store i64 %16, ptr %6, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %17 = load ptr, ptr %3, align 8, !tbaa !5
  %18 = load i64, ptr %17, align 8, !tbaa !11
  store i64 %18, ptr %7, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %19 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %19, ptr %8, align 8, !tbaa !5
  br label %20

20:                                               ; preds = %44, %2
  %21 = load ptr, ptr %8, align 8, !tbaa !5
  %22 = load ptr, ptr %3, align 8, !tbaa !5
  %23 = load i32, ptr %4, align 4, !tbaa !9
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds i64, ptr %22, i64 %24
  %26 = icmp ult ptr %21, %25
  br i1 %26, label %27, label %47

27:                                               ; preds = %20
  %28 = load ptr, ptr %8, align 8, !tbaa !5
  %29 = load i64, ptr %28, align 8, !tbaa !11
  %30 = load i64, ptr %7, align 8, !tbaa !11
  %31 = icmp sgt i64 %29, %30
  br i1 %31, label %32, label %35

32:                                               ; preds = %27
  %33 = load ptr, ptr %8, align 8, !tbaa !5
  %34 = load i64, ptr %33, align 8, !tbaa !11
  store i64 %34, ptr %7, align 8, !tbaa !11
  br label %35

35:                                               ; preds = %32, %27
  %36 = load ptr, ptr %8, align 8, !tbaa !5
  %37 = load i64, ptr %36, align 8, !tbaa !11
  %38 = load i64, ptr %6, align 8, !tbaa !11
  %39 = icmp slt i64 %37, %38
  br i1 %39, label %40, label %43

40:                                               ; preds = %35
  %41 = load ptr, ptr %8, align 8, !tbaa !5
  %42 = load i64, ptr %41, align 8, !tbaa !11
  store i64 %42, ptr %6, align 8, !tbaa !11
  br label %43

43:                                               ; preds = %40, %35
  br label %44

44:                                               ; preds = %43
  %45 = load ptr, ptr %8, align 8, !tbaa !5
  %46 = getelementptr inbounds i64, ptr %45, i32 1
  store ptr %46, ptr %8, align 8, !tbaa !5
  br label %20, !llvm.loop !13

47:                                               ; preds = %20
  %48 = load i64, ptr %6, align 8, !tbaa !11
  store i64 %48, ptr %10, align 8, !tbaa !11
  %49 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %49, ptr %9, align 8, !tbaa !5
  br label %50

50:                                               ; preds = %79, %47
  %51 = load i64, ptr %10, align 8, !tbaa !11
  %52 = load i64, ptr %7, align 8, !tbaa !11
  %53 = icmp sle i64 %51, %52
  br i1 %53, label %54, label %82

54:                                               ; preds = %50
  %55 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %55, ptr %8, align 8, !tbaa !5
  br label %56

56:                                               ; preds = %75, %54
  %57 = load ptr, ptr %8, align 8, !tbaa !5
  %58 = load ptr, ptr %3, align 8, !tbaa !5
  %59 = load i32, ptr %4, align 4, !tbaa !9
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds i64, ptr %58, i64 %60
  %62 = icmp ult ptr %57, %61
  br i1 %62, label %63, label %78

63:                                               ; preds = %56
  %64 = load ptr, ptr %8, align 8, !tbaa !5
  %65 = load i64, ptr %64, align 8, !tbaa !11
  %66 = load i64, ptr %10, align 8, !tbaa !11
  %67 = icmp eq i64 %65, %66
  br i1 %67, label %68, label %74

68:                                               ; preds = %63
  %69 = load ptr, ptr %8, align 8, !tbaa !5
  %70 = load i64, ptr %69, align 8, !tbaa !11
  %71 = load ptr, ptr %9, align 8, !tbaa !5
  store i64 %70, ptr %71, align 8, !tbaa !11
  %72 = load ptr, ptr %9, align 8, !tbaa !5
  %73 = getelementptr inbounds i64, ptr %72, i32 1
  store ptr %73, ptr %9, align 8, !tbaa !5
  br label %74

74:                                               ; preds = %68, %63
  br label %75

75:                                               ; preds = %74
  %76 = load ptr, ptr %8, align 8, !tbaa !5
  %77 = getelementptr inbounds i64, ptr %76, i32 1
  store ptr %77, ptr %8, align 8, !tbaa !5
  br label %56, !llvm.loop !15

78:                                               ; preds = %56
  br label %79

79:                                               ; preds = %78
  %80 = load i64, ptr %10, align 8, !tbaa !11
  %81 = add nsw i64 %80, 1
  store i64 %81, ptr %10, align 8, !tbaa !11
  br label %50, !llvm.loop !16

82:                                               ; preds = %50
  %83 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %83, ptr %8, align 8, !tbaa !5
  %84 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %84, ptr %9, align 8, !tbaa !5
  br label %85

85:                                               ; preds = %96, %82
  %86 = load ptr, ptr %8, align 8, !tbaa !5
  %87 = load ptr, ptr %3, align 8, !tbaa !5
  %88 = load i32, ptr %4, align 4, !tbaa !9
  %89 = sext i32 %88 to i64
  %90 = getelementptr inbounds i64, ptr %87, i64 %89
  %91 = icmp ult ptr %86, %90
  br i1 %91, label %92, label %101

92:                                               ; preds = %85
  %93 = load ptr, ptr %9, align 8, !tbaa !5
  %94 = load i64, ptr %93, align 8, !tbaa !11
  %95 = load ptr, ptr %8, align 8, !tbaa !5
  store i64 %94, ptr %95, align 8, !tbaa !11
  br label %96

96:                                               ; preds = %92
  %97 = load ptr, ptr %8, align 8, !tbaa !5
  %98 = getelementptr inbounds i64, ptr %97, i32 1
  store ptr %98, ptr %8, align 8, !tbaa !5
  %99 = load ptr, ptr %9, align 8, !tbaa !5
  %100 = getelementptr inbounds i64, ptr %99, i32 1
  store ptr %100, ptr %9, align 8, !tbaa !5
  br label %85, !llvm.loop !17

101:                                              ; preds = %85
  %102 = load ptr, ptr %5, align 8, !tbaa !5
  call void @free(ptr noundef %102) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  ret void
}

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

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #2

; Function Attrs: nounwind
declare void @free(ptr noundef) #3

; 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 allocsize(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 "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 }
attributes #5 = { nounwind allocsize(0) }

!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, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}
!16 = distinct !{!16, !14}
!17 = distinct !{!17, !14}


; 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 @spaghettiSort(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = ptrtoint ptr %0 to i64
  %4 = sext i32 %1 to i64
  %5 = shl nsw i64 %4, 3
  %6 = tail call noalias ptr @malloc(i64 noundef %5) #5
  %7 = getelementptr inbounds i64, ptr %0, i64 %4
  %8 = icmp sgt i32 %1, 0
  br i1 %8, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %2
  %9 = load i64, ptr %0, align 8, !tbaa !5
  br label %.lr.ph

.preheader50.lr.ph:                               ; preds = %.lr.ph
  br i1 %8, label %.preheader50.us.preheader, label %._crit_edge

.preheader50.us.preheader:                        ; preds = %.preheader50.lr.ph
  %10 = add i64 %spec.select, %.145
  %11 = and i64 %10, 1
  %lcmp.mod.not.not = icmp eq i64 %11, 0
  br i1 %lcmp.mod.not.not, label %.preheader50.us.prol, label %.preheader50.us.prol.loopexit

.preheader50.us.prol:                             ; preds = %.preheader50.us.preheader, %16
  %.158.us.prol = phi ptr [ %.2.us.prol, %16 ], [ %6, %.preheader50.us.preheader ]
  %.14057.us.prol = phi ptr [ %17, %16 ], [ %0, %.preheader50.us.preheader ]
  %12 = load i64, ptr %.14057.us.prol, align 8, !tbaa !5
  %13 = icmp eq i64 %12, %.145
  br i1 %13, label %14, label %16

14:                                               ; preds = %.preheader50.us.prol
  store i64 %.145, ptr %.158.us.prol, align 8, !tbaa !5
  %15 = getelementptr inbounds i8, ptr %.158.us.prol, i64 8
  br label %16

16:                                               ; preds = %14, %.preheader50.us.prol
  %.2.us.prol = phi ptr [ %15, %14 ], [ %.158.us.prol, %.preheader50.us.prol ]
  %17 = getelementptr inbounds i8, ptr %.14057.us.prol, i64 8
  %18 = icmp ult ptr %17, %7
  br i1 %18, label %.preheader50.us.prol, label %._crit_edge.us.prol, !llvm.loop !9

._crit_edge.us.prol:                              ; preds = %16
  %19 = add i64 %.145, 1
  br label %.preheader50.us.prol.loopexit

.preheader50.us.prol.loopexit:                    ; preds = %._crit_edge.us.prol, %.preheader50.us.preheader
  %.063.us.unr = phi i64 [ %.145, %.preheader50.us.preheader ], [ %19, %._crit_edge.us.prol ]
  %.03862.us.unr = phi ptr [ %6, %.preheader50.us.preheader ], [ %.2.us.prol, %._crit_edge.us.prol ]
  %20 = icmp eq i64 %spec.select, %.145
  br i1 %20, label %.preheader, label %.preheader50.us

.preheader50.us:                                  ; preds = %.preheader50.us.prol.loopexit, %._crit_edge.us.1
  %.063.us = phi i64 [ %38, %._crit_edge.us.1 ], [ %.063.us.unr, %.preheader50.us.prol.loopexit ]
  %.03862.us = phi ptr [ %.2.us.1, %._crit_edge.us.1 ], [ %.03862.us.unr, %.preheader50.us.prol.loopexit ]
  br label %21

21:                                               ; preds = %.preheader50.us, %26
  %.158.us = phi ptr [ %.03862.us, %.preheader50.us ], [ %.2.us, %26 ]
  %.14057.us = phi ptr [ %0, %.preheader50.us ], [ %27, %26 ]
  %22 = load i64, ptr %.14057.us, align 8, !tbaa !5
  %23 = icmp eq i64 %22, %.063.us
  br i1 %23, label %24, label %26

24:                                               ; preds = %21
  store i64 %.063.us, ptr %.158.us, align 8, !tbaa !5
  %25 = getelementptr inbounds i8, ptr %.158.us, i64 8
  br label %26

26:                                               ; preds = %24, %21
  %.2.us = phi ptr [ %25, %24 ], [ %.158.us, %21 ]
  %27 = getelementptr inbounds i8, ptr %.14057.us, i64 8
  %28 = icmp ult ptr %27, %7
  br i1 %28, label %21, label %._crit_edge.us, !llvm.loop !9

._crit_edge.us:                                   ; preds = %26
  %29 = add i64 %.063.us, 1
  br label %30

30:                                               ; preds = %35, %._crit_edge.us
  %.158.us.1 = phi ptr [ %.2.us, %._crit_edge.us ], [ %.2.us.1, %35 ]
  %.14057.us.1 = phi ptr [ %0, %._crit_edge.us ], [ %36, %35 ]
  %31 = load i64, ptr %.14057.us.1, align 8, !tbaa !5
  %32 = icmp eq i64 %31, %29
  br i1 %32, label %33, label %35

33:                                               ; preds = %30
  store i64 %29, ptr %.158.us.1, align 8, !tbaa !5
  %34 = getelementptr inbounds i8, ptr %.158.us.1, i64 8
  br label %35

35:                                               ; preds = %33, %30
  %.2.us.1 = phi ptr [ %34, %33 ], [ %.158.us.1, %30 ]
  %36 = getelementptr inbounds i8, ptr %.14057.us.1, i64 8
  %37 = icmp ult ptr %36, %7
  br i1 %37, label %30, label %._crit_edge.us.1, !llvm.loop !9

._crit_edge.us.1:                                 ; preds = %35
  %38 = add i64 %.063.us, 2
  %exitcond.not.1 = icmp eq i64 %29, %spec.select
  br i1 %exitcond.not.1, label %.preheader, label %.preheader50.us, !llvm.loop !11

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %.03954 = phi ptr [ %40, %.lr.ph ], [ %0, %.lr.ph.preheader ]
  %.04253 = phi i64 [ %spec.select, %.lr.ph ], [ %9, %.lr.ph.preheader ]
  %.04452 = phi i64 [ %.145, %.lr.ph ], [ %9, %.lr.ph.preheader ]
  %39 = load i64, ptr %.03954, align 8, !tbaa !5
  %spec.select = tail call i64 @llvm.smax.i64(i64 %39, i64 %.04253)
  %.145 = tail call i64 @llvm.smin.i64(i64 %39, i64 %.04452)
  %40 = getelementptr inbounds i8, ptr %.03954, i64 8
  %41 = icmp ult ptr %40, %7
  br i1 %41, label %.lr.ph, label %.preheader50.lr.ph, !llvm.loop !12

.preheader:                                       ; preds = %._crit_edge.us.1, %.preheader50.us.prol.loopexit
  br i1 %8, label %.lr.ph66.preheader, label %._crit_edge

.lr.ph66.preheader:                               ; preds = %.preheader
  %42 = add i64 %5, %3
  %43 = add i64 %3, 8
  %umax = tail call i64 @llvm.umax.i64(i64 %42, i64 %43)
  %44 = xor i64 %3, -1
  %45 = add i64 %umax, %44
  %46 = and i64 %45, -8
  %47 = add i64 %46, 8
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 8 %0, ptr align 8 %6, i64 %47, i1 false), !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %2, %.preheader50.lr.ph, %.lr.ph66.preheader, %.preheader
  tail call void @free(ptr noundef %6) #6
  ret void
}

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.smax.i64(i64, i64) #3

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.smin.i64(i64, i64) #3

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umax.i64(i64, i64) #3

; 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) #4

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 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { nounwind allocsize(0) }
attributes #6 = { 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 = !{!"long", !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: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopIdiomRecognizePass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass