; LLVM IR from mtsamis/box2d-optimized :: src/common/b2_stack_allocator.ll (ZN16b2StackAllocator8AllocateEi)

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

%class.b2StackAllocator = type <{ [102400 x i8], i32, i32, i32, [4 x i8], [32 x %struct.b2StackEntry], i32, [4 x i8] }>
%struct.b2StackEntry = type { ptr, i32, i8 }

@.str.1 = external hidden unnamed_addr constant [91 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [34 x i8], align 1
@__PRETTY_FUNCTION__._ZN16b2StackAllocator8AllocateEi = external hidden unnamed_addr constant [40 x i8], align 1

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) #0

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_ZN16b2StackAllocator8AllocateEi(ptr noundef nonnull align 8 dereferenceable(102932) %0, i32 noundef %1) #1 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  %7 = load ptr, ptr %3, align 8
  %8 = getelementptr inbounds %class.b2StackAllocator, ptr %7, i32 0, i32 6
  %9 = load i32, ptr %8, align 8, !tbaa !11
  %10 = icmp slt i32 %9, 32
  br i1 %10, label %11, label %12

11:                                               ; preds = %2
  br label %14

12:                                               ; preds = %2
  call void @__assert_fail(ptr noundef @.str.3, ptr noundef @.str.1, i32 noundef 42, ptr noundef @__PRETTY_FUNCTION__._ZN16b2StackAllocator8AllocateEi) #5
  unreachable

13:                                               ; No predecessors!
  br label %14

14:                                               ; preds = %13, %11
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #6
  %15 = load i32, ptr %4, align 4, !tbaa !9
  %16 = add nsw i32 %15, 7
  %17 = and i32 %16, -8
  store i32 %17, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #6
  %18 = getelementptr inbounds %class.b2StackAllocator, ptr %7, i32 0, i32 5
  %19 = getelementptr inbounds [32 x %struct.b2StackEntry], ptr %18, i64 0, i64 0
  %20 = getelementptr inbounds %class.b2StackAllocator, ptr %7, i32 0, i32 6
  %21 = load i32, ptr %20, align 8, !tbaa !11
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds %struct.b2StackEntry, ptr %19, i64 %22
  store ptr %23, ptr %6, align 8, !tbaa !5
  %24 = load i32, ptr %5, align 4, !tbaa !9
  %25 = load ptr, ptr %6, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.b2StackEntry, ptr %25, i32 0, i32 1
  store i32 %24, ptr %26, align 8, !tbaa !13
  %27 = getelementptr inbounds %class.b2StackAllocator, ptr %7, i32 0, i32 1
  %28 = load i32, ptr %27, align 8, !tbaa !16
  %29 = load i32, ptr %5, align 4, !tbaa !9
  %30 = add nsw i32 %28, %29
  %31 = icmp sgt i32 %30, 102400
  br i1 %31, label %32, label %39

32:                                               ; preds = %14
  %33 = load i32, ptr %5, align 4, !tbaa !9
  %34 = call noundef ptr @_Z7b2Alloci(i32 noundef %33)
  %35 = load ptr, ptr %6, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.b2StackEntry, ptr %35, i32 0, i32 0
  store ptr %34, ptr %36, align 8, !tbaa !17
  %37 = load ptr, ptr %6, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.b2StackEntry, ptr %37, i32 0, i32 2
  store i8 1, ptr %38, align 4, !tbaa !18
  br label %54

39:                                               ; preds = %14
  %40 = getelementptr inbounds %class.b2StackAllocator, ptr %7, i32 0, i32 0
  %41 = getelementptr inbounds [102400 x i8], ptr %40, i64 0, i64 0
  %42 = getelementptr inbounds %class.b2StackAllocator, ptr %7, i32 0, i32 1
  %43 = load i32, ptr %42, align 8, !tbaa !16
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds i8, ptr %41, i64 %44
  %46 = load ptr, ptr %6, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.b2StackEntry, ptr %46, i32 0, i32 0
  store ptr %45, ptr %47, align 8, !tbaa !17
  %48 = load ptr, ptr %6, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.b2StackEntry, ptr %48, i32 0, i32 2
  store i8 0, ptr %49, align 4, !tbaa !18
  %50 = load i32, ptr %5, align 4, !tbaa !9
  %51 = getelementptr inbounds %class.b2StackAllocator, ptr %7, i32 0, i32 1
  %52 = load i32, ptr %51, align 8, !tbaa !16
  %53 = add nsw i32 %52, %50
  store i32 %53, ptr %51, align 8, !tbaa !16
  br label %54

54:                                               ; preds = %39, %32
  %55 = load i32, ptr %5, align 4, !tbaa !9
  %56 = getelementptr inbounds %class.b2StackAllocator, ptr %7, i32 0, i32 2
  %57 = load i32, ptr %56, align 4, !tbaa !19
  %58 = add nsw i32 %57, %55
  store i32 %58, ptr %56, align 4, !tbaa !19
  %59 = getelementptr inbounds %class.b2StackAllocator, ptr %7, i32 0, i32 3
  %60 = load i32, ptr %59, align 8, !tbaa !20
  %61 = getelementptr inbounds %class.b2StackAllocator, ptr %7, i32 0, i32 2
  %62 = load i32, ptr %61, align 4, !tbaa !19
  %63 = call noundef i32 @_Z5b2MaxIiET_S0_S0_(i32 noundef %60, i32 noundef %62)
  %64 = getelementptr inbounds %class.b2StackAllocator, ptr %7, i32 0, i32 3
  store i32 %63, ptr %64, align 8, !tbaa !20
  %65 = getelementptr inbounds %class.b2StackAllocator, ptr %7, i32 0, i32 6
  %66 = load i32, ptr %65, align 8, !tbaa !11
  %67 = add nsw i32 %66, 1
  store i32 %67, ptr %65, align 8, !tbaa !11
  %68 = load ptr, ptr %6, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.b2StackEntry, ptr %68, i32 0, i32 0
  %70 = load ptr, ptr %69, align 8, !tbaa !17
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #6
  ret ptr %70
}

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

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef ptr @_Z7b2Alloci(i32 noundef) #3

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z5b2MaxIiET_S0_S0_(i32 noundef, i32 noundef) #4

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

attributes #0 = { noreturn 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 #1 = { mustprogress 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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { inlinehint mustprogress 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 #4 = { inlinehint mustprogress 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 #5 = { noreturn nounwind }
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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 102928}
!12 = !{!"_ZTS16b2StackAllocator", !7, i64 0, !10, i64 102400, !10, i64 102404, !10, i64 102408, !7, i64 102416, !10, i64 102928}
!13 = !{!14, !10, i64 8}
!14 = !{!"_ZTS12b2StackEntry", !6, i64 0, !10, i64 8, !15, i64 12}
!15 = !{!"bool", !7, i64 0}
!16 = !{!12, !10, i64 102400}
!17 = !{!14, !6, i64 0}
!18 = !{!14, !15, i64 12}
!19 = !{!12, !10, i64 102404}
!20 = !{!12, !10, i64 102408}


; 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.b2StackEntry = type { ptr, i32, i8 }

@.str.1 = external hidden unnamed_addr constant [91 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [34 x i8], align 1
@__PRETTY_FUNCTION__._ZN16b2StackAllocator8AllocateEi = external hidden unnamed_addr constant [40 x i8], align 1

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_ZN16b2StackAllocator8AllocateEi(ptr noundef nonnull align 8 dereferenceable(102932) %0, i32 noundef %1) local_unnamed_addr #1 align 2 {
  %3 = getelementptr inbounds i8, ptr %0, i64 102928
  %4 = load i32, ptr %3, align 8, !tbaa !5
  %5 = icmp slt i32 %4, 32
  br i1 %5, label %7, label %6

6:                                                ; preds = %2
  tail call void @__assert_fail(ptr noundef nonnull @.str.3, ptr noundef nonnull @.str.1, i32 noundef 42, ptr noundef nonnull @__PRETTY_FUNCTION__._ZN16b2StackAllocator8AllocateEi) #4
  unreachable

7:                                                ; preds = %2
  %8 = add nsw i32 %1, 7
  %9 = and i32 %8, -8
  %10 = getelementptr inbounds i8, ptr %0, i64 102416
  %11 = sext i32 %4 to i64
  %12 = getelementptr inbounds %struct.b2StackEntry, ptr %10, i64 %11
  %13 = getelementptr inbounds i8, ptr %12, i64 8
  store i32 %9, ptr %13, align 8, !tbaa !10
  %14 = getelementptr inbounds i8, ptr %0, i64 102400
  %15 = load i32, ptr %14, align 8, !tbaa !14
  %16 = add nsw i32 %15, %9
  %17 = icmp sgt i32 %16, 102400
  br i1 %17, label %18, label %20

18:                                               ; preds = %7
  %19 = tail call noundef ptr @_Z7b2Alloci(i32 noundef %9)
  br label %23

20:                                               ; preds = %7
  %21 = sext i32 %15 to i64
  %22 = getelementptr inbounds i8, ptr %0, i64 %21
  store i32 %16, ptr %14, align 8, !tbaa !14
  br label %23

23:                                               ; preds = %20, %18
  %.sink12 = phi ptr [ %19, %18 ], [ %22, %20 ]
  %.sink = phi i8 [ 1, %18 ], [ 0, %20 ]
  store ptr %.sink12, ptr %12, align 8
  %24 = getelementptr inbounds i8, ptr %12, i64 12
  store i8 %.sink, ptr %24, align 4
  %25 = getelementptr inbounds i8, ptr %0, i64 102404
  %26 = load i32, ptr %25, align 4, !tbaa !15
  %27 = add nsw i32 %26, %9
  store i32 %27, ptr %25, align 4, !tbaa !15
  %28 = getelementptr inbounds i8, ptr %0, i64 102408
  %29 = load i32, ptr %28, align 8, !tbaa !16
  %30 = tail call noundef i32 @_Z5b2MaxIiET_S0_S0_(i32 noundef %29, i32 noundef %27)
  store i32 %30, ptr %28, align 8, !tbaa !16
  %31 = load i32, ptr %3, align 8, !tbaa !5
  %32 = add nsw i32 %31, 1
  store i32 %32, ptr %3, align 8, !tbaa !5
  %33 = load ptr, ptr %12, align 8, !tbaa !17
  ret ptr %33
}

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef ptr @_Z7b2Alloci(i32 noundef) local_unnamed_addr #2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z5b2MaxIiET_S0_S0_(i32 noundef, i32 noundef) local_unnamed_addr #3

attributes #0 = { noreturn 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 #1 = { mustprogress 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 = { inlinehint mustprogress 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 = { inlinehint mustprogress 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 #4 = { noreturn 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, !9, i64 102928}
!6 = !{!"_ZTS16b2StackAllocator", !7, i64 0, !9, i64 102400, !9, i64 102404, !9, i64 102408, !7, i64 102416, !9, i64 102928}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!"int", !7, i64 0}
!10 = !{!11, !9, i64 8}
!11 = !{!"_ZTS12b2StackEntry", !12, i64 0, !9, i64 8, !13, i64 12}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!"bool", !7, i64 0}
!14 = !{!6, !9, i64 102400}
!15 = !{!6, !9, i64 102404}
!16 = !{!6, !9, i64 102408}
!17 = !{!11, !12, i64 0}


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