; LLVM IR from nbdd0121/GenerationalGC :: MemorySpace.ll (ZN6norlit2gc11MemorySpace8AllocateEmb)

; 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.norlit::gc::MemorySpace" = type { i64, i64, i64, ptr, [1 x i64] }

@.str = external hidden unnamed_addr constant [16 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [72 x i8], align 1
@__PRETTY_FUNCTION__._ZN6norlit2gc11MemorySpace8AllocateEmb = external hidden unnamed_addr constant [54 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_ZN6norlit2gc11MemorySpace8AllocateEmb(ptr noundef nonnull align 8 dereferenceable(40) %0, i64 noundef %1, i1 noundef zeroext %2) #0 align 2 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i8, align 1
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i64 %1, ptr %6, align 8, !tbaa !9
  %10 = zext i1 %2 to i8
  store i8 %10, ptr %7, align 1, !tbaa !11
  %11 = load ptr, ptr %5, align 8
  %12 = load i64, ptr %6, align 8, !tbaa !9
  %13 = and i64 %12, 7
  %14 = icmp eq i64 %13, 0
  br i1 %14, label %15, label %16

15:                                               ; preds = %3
  br label %18

16:                                               ; preds = %3
  call void @__assert_fail(ptr noundef @.str, ptr noundef @.str.1, i32 noundef 16, ptr noundef @__PRETTY_FUNCTION__._ZN6norlit2gc11MemorySpace8AllocateEmb) #3
  unreachable

17:                                               ; No predecessors!
  br label %18

18:                                               ; preds = %17, %15
  %19 = getelementptr inbounds %"struct.norlit::gc::MemorySpace", ptr %11, i32 0, i32 0
  %20 = load i64, ptr %19, align 8, !tbaa !13
  %21 = load i64, ptr %6, align 8, !tbaa !9
  %22 = add i64 %20, %21
  %23 = getelementptr inbounds %"struct.norlit::gc::MemorySpace", ptr %11, i32 0, i32 1
  %24 = load i64, ptr %23, align 8, !tbaa !15
  %25 = icmp ugt i64 %22, %24
  br i1 %25, label %26, label %57

26:                                               ; preds = %18
  %27 = getelementptr inbounds %"struct.norlit::gc::MemorySpace", ptr %11, i32 0, i32 3
  %28 = load ptr, ptr %27, align 8, !tbaa !16
  %29 = icmp ne ptr %28, null
  br i1 %29, label %50, label %30

30:                                               ; preds = %26
  %31 = load i8, ptr %7, align 1, !tbaa !11, !range !17, !noundef !18
  %32 = trunc i8 %31 to i1
  br i1 %32, label %34, label %33

33:                                               ; preds = %30
  store ptr null, ptr %4, align 8
  br label %66

34:                                               ; preds = %30
  %35 = getelementptr inbounds %"struct.norlit::gc::MemorySpace", ptr %11, i32 0, i32 1
  %36 = load i64, ptr %35, align 8, !tbaa !15
  %37 = call noundef ptr @_ZN6norlit2gc11MemorySpace3NewEm(i64 noundef %36)
  %38 = getelementptr inbounds %"struct.norlit::gc::MemorySpace", ptr %11, i32 0, i32 3
  store ptr %37, ptr %38, align 8, !tbaa !16
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %39 = getelementptr inbounds %"struct.norlit::gc::MemorySpace", ptr %11, i32 0, i32 3
  %40 = load ptr, ptr %39, align 8, !tbaa !16
  %41 = load i64, ptr %6, align 8, !tbaa !9
  %42 = call noundef ptr @_ZN6norlit2gc11MemorySpace8AllocateEmb(ptr noundef nonnull align 8 dereferenceable(40) %40, i64 noundef %41, i1 noundef zeroext false)
  store ptr %42, ptr %8, align 8, !tbaa !5
  %43 = load ptr, ptr %8, align 8, !tbaa !5
  %44 = icmp ne ptr %43, null
  br i1 %44, label %45, label %46

45:                                               ; preds = %34
  br label %48

46:                                               ; preds = %34
  call void @__assert_fail(ptr noundef @.str.2, ptr noundef @.str.1, i32 noundef 24, ptr noundef @__PRETTY_FUNCTION__._ZN6norlit2gc11MemorySpace8AllocateEmb) #3
  unreachable

47:                                               ; No predecessors!
  br label %48

48:                                               ; preds = %47, %45
  %49 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %49, ptr %4, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  br label %66

50:                                               ; preds = %26
  %51 = getelementptr inbounds %"struct.norlit::gc::MemorySpace", ptr %11, i32 0, i32 3
  %52 = load ptr, ptr %51, align 8, !tbaa !16
  %53 = load i64, ptr %6, align 8, !tbaa !9
  %54 = load i8, ptr %7, align 1, !tbaa !11, !range !17, !noundef !18
  %55 = trunc i8 %54 to i1
  %56 = call noundef ptr @_ZN6norlit2gc11MemorySpace8AllocateEmb(ptr noundef nonnull align 8 dereferenceable(40) %52, i64 noundef %53, i1 noundef zeroext %55)
  store ptr %56, ptr %4, align 8
  br label %66

57:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %58 = getelementptr inbounds %"struct.norlit::gc::MemorySpace", ptr %11, i32 0, i32 0
  %59 = load i64, ptr %58, align 8, !tbaa !13
  %60 = getelementptr inbounds i8, ptr %11, i64 %59
  store ptr %60, ptr %9, align 8, !tbaa !5
  %61 = load i64, ptr %6, align 8, !tbaa !9
  %62 = getelementptr inbounds %"struct.norlit::gc::MemorySpace", ptr %11, i32 0, i32 0
  %63 = load i64, ptr %62, align 8, !tbaa !13
  %64 = add i64 %63, %61
  store i64 %64, ptr %62, align 8, !tbaa !13
  %65 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %65, ptr %4, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  br label %66

66:                                               ; preds = %57, %50, %48, %33
  %67 = load ptr, ptr %4, align 8
  ret ptr %67
}

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

; Function Attrs: mustprogress uwtable
declare dso_local noundef ptr @_ZN6norlit2gc11MemorySpace3NewEm(i64 noundef) #0 align 2

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

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

attributes #0 = { 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 #1 = { 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 #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { noreturn nounwind }
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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"bool", !7, i64 0}
!13 = !{!14, !10, i64 0}
!14 = !{!"_ZTSN6norlit2gc11MemorySpaceE", !10, i64 0, !10, i64 8, !10, i64 16, !6, i64 24, !7, i64 32}
!15 = !{!14, !10, i64 8}
!16 = !{!14, !6, i64 24}
!17 = !{i8 0, i8 2}
!18 = !{}


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

@.str = external hidden unnamed_addr constant [16 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [72 x i8], align 1
@__PRETTY_FUNCTION__._ZN6norlit2gc11MemorySpace8AllocateEmb = external hidden unnamed_addr constant [54 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_ZN6norlit2gc11MemorySpace8AllocateEmb(ptr noundef nonnull align 8 dereferenceable(40) %0, i64 noundef %1, i1 noundef zeroext %2) local_unnamed_addr #0 align 2 {
  %4 = and i64 %1, 7
  %5 = icmp eq i64 %4, 0
  br i1 %5, label %tailrecurse, label %11

tailrecurse:                                      ; preds = %3, %12
  %.tr = phi ptr [ %14, %12 ], [ %0, %3 ]
  %6 = load i64, ptr %.tr, align 8, !tbaa !5
  %7 = add i64 %6, %1
  %8 = getelementptr inbounds i8, ptr %.tr, i64 8
  %9 = load i64, ptr %8, align 8, !tbaa !11
  %10 = icmp ugt i64 %7, %9
  br i1 %10, label %12, label %21

11:                                               ; preds = %3
  tail call void @__assert_fail(ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 16, ptr noundef nonnull @__PRETTY_FUNCTION__._ZN6norlit2gc11MemorySpace8AllocateEmb) #2
  unreachable

12:                                               ; preds = %tailrecurse
  %13 = getelementptr inbounds i8, ptr %.tr, i64 24
  %14 = load ptr, ptr %13, align 8, !tbaa !12
  %.not = icmp eq ptr %14, null
  br i1 %.not, label %15, label %tailrecurse

15:                                               ; preds = %12
  br i1 %2, label %16, label %23

16:                                               ; preds = %15
  %17 = getelementptr inbounds i8, ptr %.tr, i64 24
  %18 = tail call noundef ptr @_ZN6norlit2gc11MemorySpace3NewEm(i64 noundef %9)
  store ptr %18, ptr %17, align 8, !tbaa !12
  %19 = tail call noundef ptr @_ZN6norlit2gc11MemorySpace8AllocateEmb(ptr noundef nonnull align 8 dereferenceable(40) %18, i64 noundef %1, i1 noundef zeroext false)
  %.not15 = icmp eq ptr %19, null
  br i1 %.not15, label %20, label %23

20:                                               ; preds = %16
  tail call void @__assert_fail(ptr noundef nonnull @.str.2, ptr noundef nonnull @.str.1, i32 noundef 24, ptr noundef nonnull @__PRETTY_FUNCTION__._ZN6norlit2gc11MemorySpace8AllocateEmb) #2
  unreachable

21:                                               ; preds = %tailrecurse
  %22 = getelementptr inbounds i8, ptr %.tr, i64 %6
  store i64 %7, ptr %.tr, align 8, !tbaa !5
  br label %23

23:                                               ; preds = %16, %15, %21
  %.0 = phi ptr [ %22, %21 ], [ null, %15 ], [ %19, %16 ]
  ret ptr %.0
}

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

; Function Attrs: mustprogress uwtable
declare dso_local noundef ptr @_ZN6norlit2gc11MemorySpace3NewEm(i64 noundef) local_unnamed_addr #0 align 2

attributes #0 = { 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 #1 = { 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 #2 = { 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, !7, i64 0}
!6 = !{!"_ZTSN6norlit2gc11MemorySpaceE", !7, i64 0, !7, i64 8, !7, i64 16, !10, i64 24, !8, i64 32}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !7, i64 8}
!12 = !{!6, !10, i64 24}


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