; LLVM IR from h-ssiqueira/Sort_Algorithms :: src/noncomparisonDistribution (pigeonholeSort)

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

16:                                               ; preds = %40, %2
  %17 = load ptr, ptr %7, align 8, !tbaa !5
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = load i32, ptr %4, align 4, !tbaa !9
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds i64, ptr %18, i64 %20
  %22 = icmp ult ptr %17, %21
  br i1 %22, label %23, label %43

23:                                               ; preds = %16
  %24 = load ptr, ptr %7, align 8, !tbaa !5
  %25 = load i64, ptr %24, align 8, !tbaa !11
  %26 = load i64, ptr %5, align 8, !tbaa !11
  %27 = icmp sgt i64 %25, %26
  br i1 %27, label %28, label %31

28:                                               ; preds = %23
  %29 = load ptr, ptr %7, align 8, !tbaa !5
  %30 = load i64, ptr %29, align 8, !tbaa !11
  store i64 %30, ptr %5, align 8, !tbaa !11
  br label %31

31:                                               ; preds = %28, %23
  %32 = load ptr, ptr %7, align 8, !tbaa !5
  %33 = load i64, ptr %32, align 8, !tbaa !11
  %34 = load i64, ptr %6, align 8, !tbaa !11
  %35 = icmp slt i64 %33, %34
  br i1 %35, label %36, label %39

36:                                               ; preds = %31
  %37 = load ptr, ptr %7, align 8, !tbaa !5
  %38 = load i64, ptr %37, align 8, !tbaa !11
  store i64 %38, ptr %6, align 8, !tbaa !11
  br label %39

39:                                               ; preds = %36, %31
  br label %40

40:                                               ; preds = %39
  %41 = load ptr, ptr %7, align 8, !tbaa !5
  %42 = getelementptr inbounds i64, ptr %41, i32 1
  store ptr %42, ptr %7, align 8, !tbaa !5
  br label %16, !llvm.loop !13

43:                                               ; preds = %16
  %44 = load i64, ptr %5, align 8, !tbaa !11
  %45 = load i64, ptr %6, align 8, !tbaa !11
  %46 = sub nsw i64 %44, %45
  %47 = add nsw i64 %46, 1
  %48 = trunc i64 %47 to i32
  store i32 %48, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %49 = load i32, ptr %9, align 4, !tbaa !9
  %50 = sext i32 %49 to i64
  %51 = call noalias ptr @calloc(i64 noundef %50, i64 noundef 8) #5
  store ptr %51, ptr %10, align 8, !tbaa !5
  %52 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %52, ptr %7, align 8, !tbaa !5
  br label %53

53:                                               ; preds = %70, %43
  %54 = load ptr, ptr %7, align 8, !tbaa !5
  %55 = load ptr, ptr %3, align 8, !tbaa !5
  %56 = load i32, ptr %4, align 4, !tbaa !9
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds i64, ptr %55, i64 %57
  %59 = icmp ult ptr %54, %58
  br i1 %59, label %60, label %73

60:                                               ; preds = %53
  %61 = load ptr, ptr %10, align 8, !tbaa !5
  %62 = load ptr, ptr %7, align 8, !tbaa !5
  %63 = load i64, ptr %62, align 8, !tbaa !11
  %64 = getelementptr inbounds i64, ptr %61, i64 %63
  %65 = load i64, ptr %6, align 8, !tbaa !11
  %66 = sub i64 0, %65
  %67 = getelementptr inbounds i64, ptr %64, i64 %66
  %68 = load i64, ptr %67, align 8, !tbaa !11
  %69 = add nsw i64 %68, 1
  store i64 %69, ptr %67, align 8, !tbaa !11
  br label %70

70:                                               ; preds = %60
  %71 = load ptr, ptr %7, align 8, !tbaa !5
  %72 = getelementptr inbounds i64, ptr %71, i32 1
  store ptr %72, ptr %7, align 8, !tbaa !5
  br label %53, !llvm.loop !15

73:                                               ; preds = %53
  %74 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %74, ptr %7, align 8, !tbaa !5
  %75 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %75, ptr %8, align 8, !tbaa !5
  br label %76

76:                                               ; preds = %102, %73
  %77 = load ptr, ptr %7, align 8, !tbaa !5
  %78 = load ptr, ptr %10, align 8, !tbaa !5
  %79 = load i32, ptr %9, align 4, !tbaa !9
  %80 = sext i32 %79 to i64
  %81 = getelementptr inbounds i64, ptr %78, i64 %80
  %82 = icmp ult ptr %77, %81
  br i1 %82, label %83, label %105

83:                                               ; preds = %76
  br label %84

84:                                               ; preds = %89, %83
  %85 = load ptr, ptr %7, align 8, !tbaa !5
  %86 = load i64, ptr %85, align 8, !tbaa !11
  %87 = add nsw i64 %86, -1
  store i64 %87, ptr %85, align 8, !tbaa !11
  %88 = icmp sgt i64 %86, 0
  br i1 %88, label %89, label %101

89:                                               ; preds = %84
  %90 = load ptr, ptr %7, align 8, !tbaa !5
  %91 = load ptr, ptr %10, align 8, !tbaa !5
  %92 = ptrtoint ptr %90 to i64
  %93 = ptrtoint ptr %91 to i64
  %94 = sub i64 %92, %93
  %95 = sdiv exact i64 %94, 8
  %96 = load i64, ptr %6, align 8, !tbaa !11
  %97 = add nsw i64 %95, %96
  %98 = load ptr, ptr %8, align 8, !tbaa !5
  store i64 %97, ptr %98, align 8, !tbaa !11
  %99 = load ptr, ptr %8, align 8, !tbaa !5
  %100 = getelementptr inbounds i64, ptr %99, i32 1
  store ptr %100, ptr %8, align 8, !tbaa !5
  br label %84, !llvm.loop !16

101:                                              ; preds = %84
  br label %102

102:                                              ; preds = %101
  %103 = load ptr, ptr %7, align 8, !tbaa !5
  %104 = getelementptr inbounds i64, ptr %103, i32 1
  store ptr %104, ptr %7, align 8, !tbaa !5
  br label %76, !llvm.loop !17

105:                                              ; preds = %76
  %106 = load ptr, ptr %10, align 8, !tbaa !5
  call void @free(ptr noundef %106) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 4, 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,1)
declare noalias ptr @calloc(i64 noundef, 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,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 #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,1) }

!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 @pigeonholeSort(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = load i64, ptr %0, align 8, !tbaa !5
  %4 = sext i32 %1 to i64
  %5 = getelementptr inbounds i64, ptr %0, i64 %4
  %6 = icmp sgt i32 %1, 0
  br i1 %6, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %2, %.lr.ph
  %.048 = phi i64 [ %spec.select, %.lr.ph ], [ %3, %2 ]
  %.03647 = phi i64 [ %.137, %.lr.ph ], [ %3, %2 ]
  %.04046 = phi ptr [ %8, %.lr.ph ], [ %0, %2 ]
  %7 = load i64, ptr %.04046, align 8, !tbaa !5
  %spec.select = tail call i64 @llvm.smax.i64(i64 %7, i64 %.048)
  %.137 = tail call i64 @llvm.smin.i64(i64 %7, i64 %.03647)
  %8 = getelementptr inbounds i8, ptr %.04046, i64 8
  %9 = icmp ult ptr %8, %5
  br i1 %9, label %.lr.ph, label %._crit_edge, !llvm.loop !9

._crit_edge:                                      ; preds = %.lr.ph, %2
  %.036.lcssa = phi i64 [ %3, %2 ], [ %.137, %.lr.ph ]
  %.0.lcssa = phi i64 [ %3, %2 ], [ %spec.select, %.lr.ph ]
  %10 = sub nsw i64 %.0.lcssa, %.036.lcssa
  %11 = shl i64 %10, 32
  %sext = add i64 %11, 4294967296
  %12 = ashr exact i64 %sext, 32
  %13 = tail call noalias ptr @calloc(i64 noundef %12, i64 noundef 8) #4
  br i1 %6, label %.lr.ph53, label %.preheader45

.lr.ph53:                                         ; preds = %._crit_edge
  %14 = sub i64 0, %.036.lcssa
  %invariant.gep = getelementptr i64, ptr %13, i64 %14
  br label %18

.preheader45:                                     ; preds = %18, %._crit_edge
  %15 = getelementptr inbounds i64, ptr %13, i64 %12
  %16 = icmp sgt i64 %sext, 0
  br i1 %16, label %.preheader.lr.ph, label %._crit_edge60

.preheader.lr.ph:                                 ; preds = %.preheader45
  %17 = ptrtoint ptr %13 to i64
  br label %.preheader

18:                                               ; preds = %.lr.ph53, %18
  %.14151 = phi ptr [ %0, %.lr.ph53 ], [ %22, %18 ]
  %19 = load i64, ptr %.14151, align 8, !tbaa !5
  %gep = getelementptr i64, ptr %invariant.gep, i64 %19
  %20 = load i64, ptr %gep, align 8, !tbaa !5
  %21 = add nsw i64 %20, 1
  store i64 %21, ptr %gep, align 8, !tbaa !5
  %22 = getelementptr inbounds i8, ptr %.14151, i64 8
  %23 = icmp ult ptr %22, %5
  br i1 %23, label %18, label %.preheader45, !llvm.loop !11

.preheader:                                       ; preds = %.preheader.lr.ph, %._crit_edge56
  %.03859 = phi ptr [ %0, %.preheader.lr.ph ], [ %.139.lcssa, %._crit_edge56 ]
  %.258 = phi ptr [ %13, %.preheader.lr.ph ], [ %36, %._crit_edge56 ]
  %24 = load i64, ptr %.258, align 8, !tbaa !5
  %25 = add nsw i64 %24, -1
  store i64 %25, ptr %.258, align 8, !tbaa !5
  %26 = icmp sgt i64 %24, 0
  br i1 %26, label %.lr.ph55, label %._crit_edge56

.lr.ph55:                                         ; preds = %.preheader
  %27 = ptrtoint ptr %.258 to i64
  %28 = sub i64 %27, %17
  %29 = ashr exact i64 %28, 3
  %30 = add nsw i64 %29, %.036.lcssa
  br label %31

31:                                               ; preds = %.lr.ph55, %31
  %.13954 = phi ptr [ %.03859, %.lr.ph55 ], [ %32, %31 ]
  store i64 %30, ptr %.13954, align 8, !tbaa !5
  %32 = getelementptr inbounds i8, ptr %.13954, i64 8
  %33 = load i64, ptr %.258, align 8, !tbaa !5
  %34 = add nsw i64 %33, -1
  store i64 %34, ptr %.258, align 8, !tbaa !5
  %35 = icmp sgt i64 %33, 0
  br i1 %35, label %31, label %._crit_edge56, !llvm.loop !12

._crit_edge56:                                    ; preds = %31, %.preheader
  %.139.lcssa = phi ptr [ %.03859, %.preheader ], [ %32, %31 ]
  %36 = getelementptr inbounds i8, ptr %.258, i64 8
  %37 = icmp ult ptr %36, %15
  br i1 %37, label %.preheader, label %._crit_edge60, !llvm.loop !13

._crit_edge60:                                    ; preds = %._crit_edge56, %.preheader45
  tail call void @free(ptr noundef %13) #5
  ret void
}

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,zeroed") allocsize(0,1) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @calloc(i64 noundef, 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

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,zeroed") allocsize(0,1) 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 = { nounwind allocsize(0,1) }
attributes #5 = { 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}
!13 = distinct !{!13, !10}


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