; LLVM IR from rantala/string-sorting :: external (msd)

; 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.memory = type { [100 x ptr], i32, i32, i32, ptr, ptr, ptr }
%struct.stackrec = type { ptr, ptr, i32, i32 }
%struct.listrec = type { ptr, ptr, i32 }

@stackmem = external hidden global [1 x %struct.memory], align 16

; Function Attrs: nounwind uwtable
define dso_local ptr @MSD1(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  store ptr null, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %9 = load i32, ptr %5, align 4, !tbaa !9
  %10 = icmp slt i32 %9, 2
  br i1 %10, label %11, label %13

11:                                               ; preds = %2
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %12, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %46

13:                                               ; preds = %2
  %14 = load i32, ptr %5, align 4, !tbaa !9
  %15 = sdiv i32 %14, 50
  call void @initmem(ptr noundef @stackmem, i32 noundef 24, i32 noundef %15)
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = load i32, ptr %5, align 4, !tbaa !9
  call void @push(ptr noundef %16, ptr noundef null, i32 noundef %17, i32 noundef 0)
  br label %18

18:                                               ; preds = %37, %28, %13
  %19 = call i32 @stackempty()
  %20 = icmp ne i32 %19, 0
  %21 = xor i1 %20, true
  br i1 %21, label %22, label %44

22:                                               ; preds = %18
  %23 = call ptr @pop()
  store ptr %23, ptr %7, align 8, !tbaa !5
  %24 = load ptr, ptr %7, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.stackrec, ptr %24, i32 0, i32 2
  %26 = load i32, ptr %25, align 8, !tbaa !11
  %27 = icmp ne i32 %26, 0
  br i1 %27, label %37, label %28

28:                                               ; preds = %22
  %29 = load ptr, ptr %6, align 8, !tbaa !5
  %30 = load ptr, ptr %7, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.stackrec, ptr %30, i32 0, i32 1
  %32 = load ptr, ptr %31, align 8, !tbaa !13
  %33 = getelementptr inbounds %struct.listrec, ptr %32, i32 0, i32 1
  store ptr %29, ptr %33, align 8, !tbaa !14
  %34 = load ptr, ptr %7, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.stackrec, ptr %34, i32 0, i32 0
  %36 = load ptr, ptr %35, align 8, !tbaa !16
  store ptr %36, ptr %6, align 8, !tbaa !5
  br label %18, !llvm.loop !17

37:                                               ; preds = %22
  %38 = load ptr, ptr %7, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.stackrec, ptr %38, i32 0, i32 0
  %40 = load ptr, ptr %39, align 8, !tbaa !16
  %41 = load ptr, ptr %7, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.stackrec, ptr %41, i32 0, i32 3
  %43 = load i32, ptr %42, align 4, !tbaa !19
  call void @bucketing(ptr noundef %40, i32 noundef %43)
  br label %18, !llvm.loop !17

44:                                               ; preds = %18
  call void @freemem(ptr noundef @stackmem)
  %45 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %45, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %46

46:                                               ; preds = %44, %11
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  %47 = load ptr, ptr %3, align 8
  ret ptr %47
}

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

declare void @initmem(ptr noundef, i32 noundef, i32 noundef) #2

; Function Attrs: nounwind uwtable
declare hidden void @push(ptr noundef, ptr noundef, i32 noundef, i32 noundef) #0

; Function Attrs: nounwind uwtable
declare hidden void @bucketing(ptr noundef, i32 noundef) #0

declare void @freemem(ptr noundef) #2

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

; Function Attrs: nounwind uwtable
declare hidden i32 @stackempty() #0

; Function Attrs: nounwind uwtable
declare hidden ptr @pop() #0

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 = { "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 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 16}
!12 = !{!"stackrec", !6, i64 0, !6, i64 8, !10, i64 16, !10, i64 20}
!13 = !{!12, !6, i64 8}
!14 = !{!15, !6, i64 8}
!15 = !{!"listrec", !6, i64 0, !6, i64 8, !10, i64 16}
!16 = !{!12, !6, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = !{!12, !10, i64 20}


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

%struct.memory = type { [100 x ptr], i32, i32, i32, ptr, ptr, ptr }

@stackmem = external hidden global [1 x %struct.memory], align 16

; Function Attrs: nounwind uwtable
define dso_local ptr @MSD1(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp slt i32 %1, 2
  br i1 %3, label %21, label %4

4:                                                ; preds = %2
  %5 = udiv i32 %1, 50
  tail call void @initmem(ptr noundef nonnull @stackmem, i32 noundef 24, i32 noundef %5) #2
  tail call void @push(ptr noundef %0, ptr noundef null, i32 noundef %1, i32 noundef 0)
  %6 = tail call i32 @stackempty()
  %.not1517 = icmp eq i32 %6, 0
  br i1 %.not1517, label %.lr.ph, label %.outer._crit_edge

.lr.ph:                                           ; preds = %4, %.outer
  %.012.ph18 = phi ptr [ %14, %.outer ], [ null, %4 ]
  br label %7

7:                                                ; preds = %.lr.ph, %16
  %8 = tail call ptr @pop()
  %9 = getelementptr inbounds i8, ptr %8, i64 16
  %10 = load i32, ptr %9, align 8, !tbaa !5
  %.not14 = icmp eq i32 %10, 0
  br i1 %.not14, label %.outer, label %16

.outer:                                           ; preds = %7
  %11 = getelementptr inbounds i8, ptr %8, i64 8
  %12 = load ptr, ptr %11, align 8, !tbaa !11
  %13 = getelementptr inbounds i8, ptr %12, i64 8
  store ptr %.012.ph18, ptr %13, align 8, !tbaa !12
  %14 = load ptr, ptr %8, align 8, !tbaa !14
  %15 = tail call i32 @stackempty()
  %.not15 = icmp eq i32 %15, 0
  br i1 %.not15, label %.lr.ph, label %.outer._crit_edge, !llvm.loop !15

16:                                               ; preds = %7
  %17 = load ptr, ptr %8, align 8, !tbaa !14
  %18 = getelementptr inbounds i8, ptr %8, i64 20
  %19 = load i32, ptr %18, align 4, !tbaa !17
  tail call void @bucketing(ptr noundef %17, i32 noundef %19)
  %20 = tail call i32 @stackempty()
  %.not = icmp eq i32 %20, 0
  br i1 %.not, label %7, label %.outer._crit_edge, !llvm.loop !15

.outer._crit_edge:                                ; preds = %.outer, %16, %4
  %.012.ph.lcssa = phi ptr [ null, %4 ], [ %.012.ph18, %16 ], [ %14, %.outer ]
  tail call void @freemem(ptr noundef nonnull @stackmem) #2
  br label %21

21:                                               ; preds = %2, %.outer._crit_edge
  %.0 = phi ptr [ %.012.ph.lcssa, %.outer._crit_edge ], [ %0, %2 ]
  ret ptr %.0
}

declare void @initmem(ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden void @push(ptr noundef, ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden void @bucketing(ptr noundef, i32 noundef) local_unnamed_addr #0

declare void @freemem(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden i32 @stackempty() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden ptr @pop() local_unnamed_addr #0

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, !10, i64 16}
!6 = !{!"stackrec", !7, i64 0, !7, i64 8, !10, i64 16, !10, i64 20}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!6, !7, i64 8}
!12 = !{!13, !7, i64 8}
!13 = !{!"listrec", !7, i64 0, !7, i64 8, !10, i64 16}
!14 = !{!6, !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!6, !10, i64 20}


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