; LLVM IR from knizhnik/cppgc :: copygc/gc.ll (ZN2GC15MemoryAllocator9_allocateEm)

; 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.GC::MemoryAllocator" = type { i64, i64, ptr, ptr, i64, ptr, ptr, i64, i64, ptr, ptr }
%"struct.GC::MemorySegment" = type { ptr, ptr }
%"union.GC::ObjectHeader" = type { i64 }

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_ZN2GC15MemoryAllocator9_allocateEm(ptr noundef nonnull align 8 dereferenceable(88) %0, i64 noundef %1) #0 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i64 %1, ptr %4, align 8, !tbaa !9
  %8 = load ptr, ptr %3, align 8
  %9 = getelementptr inbounds %"class.GC::MemoryAllocator", ptr %8, i32 0, i32 4
  %10 = load i64, ptr %9, align 8, !tbaa !11
  %11 = getelementptr inbounds %"class.GC::MemoryAllocator", ptr %8, i32 0, i32 8
  %12 = load i64, ptr %11, align 8, !tbaa !13
  %13 = icmp ugt i64 %10, %12
  br i1 %13, label %14, label %15

14:                                               ; preds = %2
  call void @_ZN2GC15MemoryAllocator2gcEv()
  br label %15

15:                                               ; preds = %14, %2
  %16 = load i64, ptr %4, align 8, !tbaa !9
  %17 = add i64 %16, 8
  %18 = add i64 %17, 7
  %19 = and i64 %18, -8
  store i64 %19, ptr %4, align 8, !tbaa !9
  %20 = getelementptr inbounds %"class.GC::MemoryAllocator", ptr %8, i32 0, i32 1
  %21 = load i64, ptr %20, align 8, !tbaa !14
  %22 = load i64, ptr %4, align 8, !tbaa !9
  %23 = add i64 %21, %22
  %24 = getelementptr inbounds %"class.GC::MemoryAllocator", ptr %8, i32 0, i32 0
  %25 = load i64, ptr %24, align 8, !tbaa !15
  %26 = icmp ugt i64 %23, %25
  br i1 %26, label %27, label %80

27:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  %28 = getelementptr inbounds %"class.GC::MemoryAllocator", ptr %8, i32 0, i32 2
  %29 = load ptr, ptr %28, align 8, !tbaa !16
  store ptr %29, ptr %5, align 8, !tbaa !5
  %30 = load ptr, ptr %5, align 8, !tbaa !5
  %31 = icmp eq ptr %30, null
  br i1 %31, label %37, label %32

32:                                               ; preds = %27
  %33 = load i64, ptr %4, align 8, !tbaa !9
  %34 = getelementptr inbounds %"class.GC::MemoryAllocator", ptr %8, i32 0, i32 0
  %35 = load i64, ptr %34, align 8, !tbaa !15
  %36 = icmp ugt i64 %33, %35
  br i1 %36, label %37, label %65

37:                                               ; preds = %32, %27
  %38 = load i64, ptr %4, align 8, !tbaa !9
  %39 = getelementptr inbounds %"class.GC::MemoryAllocator", ptr %8, i32 0, i32 0
  %40 = load i64, ptr %39, align 8, !tbaa !15
  %41 = icmp ugt i64 %38, %40
  br i1 %41, label %42, label %53

42:                                               ; preds = %37
  %43 = load i64, ptr %4, align 8, !tbaa !9
  %44 = add i64 16, %43
  %45 = call noalias ptr @malloc(i64 noundef %44) #5
  store ptr %45, ptr %5, align 8, !tbaa !5
  %46 = getelementptr inbounds %"class.GC::MemoryAllocator", ptr %8, i32 0, i32 3
  %47 = load ptr, ptr %46, align 8, !tbaa !17
  %48 = ptrtoint ptr %47 to i64
  %49 = add i64 %48, 2
  %50 = inttoptr i64 %49 to ptr
  %51 = load ptr, ptr %5, align 8, !tbaa !5
  %52 = getelementptr inbounds %"struct.GC::MemorySegment", ptr %51, i32 0, i32 0
  store ptr %50, ptr %52, align 8, !tbaa !18
  br label %62

53:                                               ; preds = %37
  %54 = getelementptr inbounds %"class.GC::MemoryAllocator", ptr %8, i32 0, i32 0
  %55 = load i64, ptr %54, align 8, !tbaa !15
  %56 = add i64 16, %55
  %57 = call noalias ptr @malloc(i64 noundef %56) #5
  store ptr %57, ptr %5, align 8, !tbaa !5
  %58 = getelementptr inbounds %"class.GC::MemoryAllocator", ptr %8, i32 0, i32 3
  %59 = load ptr, ptr %58, align 8, !tbaa !17
  %60 = load ptr, ptr %5, align 8, !tbaa !5
  %61 = getelementptr inbounds %"struct.GC::MemorySegment", ptr %60, i32 0, i32 0
  store ptr %59, ptr %61, align 8, !tbaa !18
  br label %62

62:                                               ; preds = %53, %42
  %63 = load ptr, ptr %5, align 8, !tbaa !5
  %64 = getelementptr inbounds %"struct.GC::MemorySegment", ptr %63, i32 0, i32 1
  store ptr %8, ptr %64, align 8, !tbaa !20
  br label %76

65:                                               ; preds = %32
  %66 = getelementptr inbounds %"class.GC::MemoryAllocator", ptr %8, i32 0, i32 2
  %67 = load ptr, ptr %66, align 8, !tbaa !16
  store ptr %67, ptr %5, align 8, !tbaa !5
  %68 = load ptr, ptr %5, align 8, !tbaa !5
  %69 = getelementptr inbounds %"struct.GC::MemorySegment", ptr %68, i32 0, i32 0
  %70 = load ptr, ptr %69, align 8, !tbaa !18
  %71 = getelementptr inbounds %"class.GC::MemoryAllocator", ptr %8, i32 0, i32 2
  store ptr %70, ptr %71, align 8, !tbaa !16
  %72 = getelementptr inbounds %"class.GC::MemoryAllocator", ptr %8, i32 0, i32 3
  %73 = load ptr, ptr %72, align 8, !tbaa !17
  %74 = load ptr, ptr %5, align 8, !tbaa !5
  %75 = getelementptr inbounds %"struct.GC::MemorySegment", ptr %74, i32 0, i32 0
  store ptr %73, ptr %75, align 8, !tbaa !18
  br label %76

76:                                               ; preds = %65, %62
  %77 = load ptr, ptr %5, align 8, !tbaa !5
  %78 = getelementptr inbounds %"class.GC::MemoryAllocator", ptr %8, i32 0, i32 3
  store ptr %77, ptr %78, align 8, !tbaa !17
  %79 = getelementptr inbounds %"class.GC::MemoryAllocator", ptr %8, i32 0, i32 1
  store i64 0, ptr %79, align 8, !tbaa !14
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  br label %80

80:                                               ; preds = %76, %15
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %81 = getelementptr inbounds %"class.GC::MemoryAllocator", ptr %8, i32 0, i32 3
  %82 = load ptr, ptr %81, align 8, !tbaa !17
  %83 = getelementptr inbounds %"struct.GC::MemorySegment", ptr %82, i64 1
  %84 = getelementptr inbounds %"class.GC::MemoryAllocator", ptr %8, i32 0, i32 1
  %85 = load i64, ptr %84, align 8, !tbaa !14
  %86 = getelementptr inbounds i8, ptr %83, i64 %85
  store ptr %86, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %87 = load ptr, ptr %6, align 8, !tbaa !5
  %88 = getelementptr inbounds %"union.GC::ObjectHeader", ptr %87, i64 1
  store ptr %88, ptr %7, align 8, !tbaa !5
  %89 = load i64, ptr %4, align 8, !tbaa !9
  %90 = getelementptr inbounds %"class.GC::MemoryAllocator", ptr %8, i32 0, i32 1
  %91 = load i64, ptr %90, align 8, !tbaa !14
  %92 = add i64 %91, %89
  store i64 %92, ptr %90, align 8, !tbaa !14
  %93 = load i64, ptr %4, align 8, !tbaa !9
  %94 = getelementptr inbounds %"class.GC::MemoryAllocator", ptr %8, i32 0, i32 4
  %95 = load i64, ptr %94, align 8, !tbaa !11
  %96 = add i64 %95, %93
  store i64 %96, ptr %94, align 8, !tbaa !11
  %97 = getelementptr inbounds %"class.GC::MemoryAllocator", ptr %8, i32 0, i32 3
  %98 = load ptr, ptr %97, align 8, !tbaa !17
  %99 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %98, ptr %99, align 8, !tbaa !21
  %100 = getelementptr inbounds %"class.GC::MemoryAllocator", ptr %8, i32 0, i32 9
  %101 = load ptr, ptr %100, align 8, !tbaa !22
  %102 = icmp ne ptr %101, null
  br i1 %102, label %103, label %110

103:                                              ; preds = %80
  %104 = load ptr, ptr %7, align 8, !tbaa !5
  %105 = ptrtoint ptr %104 to i64
  %106 = or i64 %105, 1
  %107 = getelementptr inbounds %"class.GC::MemoryAllocator", ptr %8, i32 0, i32 9
  %108 = load ptr, ptr %107, align 8, !tbaa !22
  %109 = call noundef ptr @_ZNK2GC6Object9getHeaderEv(ptr noundef nonnull align 8 dereferenceable(8) %108)
  store i64 %106, ptr %109, align 8, !tbaa !21
  br label %110

110:                                              ; preds = %103, %80
  %111 = load ptr, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  ret ptr %111
}

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN2GC15MemoryAllocator2gcEv() #0 align 2

; 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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef ptr @_ZNK2GC6Object9getHeaderEv(ptr noundef nonnull align 8 dereferenceable(8)) #3 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 = { 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 = { 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 = { 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !10, i64 32}
!12 = !{!"_ZTSN2GC15MemoryAllocatorE", !10, i64 0, !10, i64 8, !6, i64 16, !6, i64 24, !10, i64 32, !6, i64 40, !6, i64 48, !10, i64 56, !10, i64 64, !6, i64 72, !6, i64 80}
!13 = !{!12, !10, i64 64}
!14 = !{!12, !10, i64 8}
!15 = !{!12, !10, i64 0}
!16 = !{!12, !6, i64 16}
!17 = !{!12, !6, i64 24}
!18 = !{!19, !6, i64 0}
!19 = !{!"_ZTSN2GC13MemorySegmentE", !6, i64 0, !6, i64 8}
!20 = !{!19, !6, i64 8}
!21 = !{!7, !7, i64 0}
!22 = !{!12, !6, i64 72}


; 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: mustprogress uwtable
define dso_local noundef nonnull ptr @_ZN2GC15MemoryAllocator9_allocateEm(ptr noundef nonnull align 8 dereferenceable(88) %0, i64 noundef %1) local_unnamed_addr #0 align 2 {
  %3 = getelementptr inbounds i8, ptr %0, i64 32
  %4 = load i64, ptr %3, align 8, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %0, i64 64
  %6 = load i64, ptr %5, align 8, !tbaa !11
  %7 = icmp ugt i64 %4, %6
  br i1 %7, label %8, label %9

8:                                                ; preds = %2
  tail call void @_ZN2GC15MemoryAllocator2gcEv()
  br label %9

9:                                                ; preds = %8, %2
  %10 = add i64 %1, 15
  %11 = and i64 %10, -8
  %12 = getelementptr inbounds i8, ptr %0, i64 8
  %13 = load i64, ptr %12, align 8, !tbaa !12
  %14 = add i64 %13, %11
  %15 = load i64, ptr %0, align 8, !tbaa !13
  %16 = icmp ugt i64 %14, %15
  br i1 %16, label %17, label %._crit_edge

._crit_edge:                                      ; preds = %9
  %.phi.trans.insert = getelementptr inbounds i8, ptr %0, i64 24
  %.pre = load ptr, ptr %.phi.trans.insert, align 8, !tbaa !14
  br label %43

17:                                               ; preds = %9
  %18 = getelementptr inbounds i8, ptr %0, i64 16
  %19 = load ptr, ptr %18, align 8, !tbaa !15
  %20 = icmp eq ptr %19, null
  %21 = icmp ugt i64 %11, %15
  %or.cond = or i1 %21, %20
  br i1 %or.cond, label %22, label %37

22:                                               ; preds = %17
  %23 = getelementptr inbounds i8, ptr %0, i64 24
  br i1 %21, label %24, label %31

24:                                               ; preds = %22
  %25 = add i64 %11, 16
  %26 = tail call noalias ptr @malloc(i64 noundef %25) #3
  %27 = load ptr, ptr %23, align 8, !tbaa !14
  %28 = ptrtoint ptr %27 to i64
  %29 = add i64 %28, 2
  %30 = inttoptr i64 %29 to ptr
  store ptr %30, ptr %26, align 8, !tbaa !16
  br label %35

31:                                               ; preds = %22
  %32 = add i64 %15, 16
  %33 = tail call noalias ptr @malloc(i64 noundef %32) #3
  %34 = load ptr, ptr %23, align 8, !tbaa !14
  store ptr %34, ptr %33, align 8, !tbaa !16
  br label %35

35:                                               ; preds = %31, %24
  %.0 = phi ptr [ %26, %24 ], [ %33, %31 ]
  %36 = getelementptr inbounds i8, ptr %.0, i64 8
  store ptr %0, ptr %36, align 8, !tbaa !18
  br label %41

37:                                               ; preds = %17
  %38 = load ptr, ptr %19, align 8, !tbaa !16
  store ptr %38, ptr %18, align 8, !tbaa !15
  %39 = getelementptr inbounds i8, ptr %0, i64 24
  %40 = load ptr, ptr %39, align 8, !tbaa !14
  store ptr %40, ptr %19, align 8, !tbaa !16
  br label %41

41:                                               ; preds = %37, %35
  %.1 = phi ptr [ %.0, %35 ], [ %19, %37 ]
  %42 = getelementptr inbounds i8, ptr %0, i64 24
  store ptr %.1, ptr %42, align 8, !tbaa !14
  br label %43

43:                                               ; preds = %._crit_edge, %41
  %44 = phi i64 [ %13, %._crit_edge ], [ 0, %41 ]
  %45 = phi ptr [ %.pre, %._crit_edge ], [ %.1, %41 ]
  %46 = getelementptr inbounds i8, ptr %45, i64 16
  %47 = getelementptr inbounds i8, ptr %46, i64 %44
  %48 = getelementptr inbounds i8, ptr %47, i64 8
  %49 = add i64 %44, %11
  store i64 %49, ptr %12, align 8, !tbaa !12
  %50 = load i64, ptr %3, align 8, !tbaa !5
  %51 = add i64 %50, %11
  store i64 %51, ptr %3, align 8, !tbaa !5
  store ptr %45, ptr %47, align 8, !tbaa !19
  %52 = getelementptr inbounds i8, ptr %0, i64 72
  %53 = load ptr, ptr %52, align 8, !tbaa !20
  %.not = icmp eq ptr %53, null
  br i1 %.not, label %58, label %54

54:                                               ; preds = %43
  %55 = ptrtoint ptr %48 to i64
  %56 = or i64 %55, 1
  %57 = tail call noundef ptr @_ZNK2GC6Object9getHeaderEv(ptr noundef nonnull align 8 dereferenceable(8) %53)
  store i64 %56, ptr %57, align 8, !tbaa !19
  br label %58

58:                                               ; preds = %54, %43
  ret ptr %48
}

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN2GC15MemoryAllocator2gcEv() local_unnamed_addr #0 align 2

; 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 uwtable
declare dso_local noundef ptr @_ZNK2GC6Object9getHeaderEv(ptr noundef nonnull align 8 dereferenceable(8)) local_unnamed_addr #2 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 = { 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 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 = { 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, !7, i64 32}
!6 = !{!"_ZTSN2GC15MemoryAllocatorE", !7, i64 0, !7, i64 8, !10, i64 16, !10, i64 24, !7, i64 32, !10, i64 40, !10, i64 48, !7, i64 56, !7, i64 64, !10, i64 72, !10, i64 80}
!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 64}
!12 = !{!6, !7, i64 8}
!13 = !{!6, !7, i64 0}
!14 = !{!6, !10, i64 24}
!15 = !{!6, !10, i64 16}
!16 = !{!17, !10, i64 0}
!17 = !{!"_ZTSN2GC13MemorySegmentE", !10, i64 0, !10, i64 8}
!18 = !{!17, !10, i64 8}
!19 = !{!8, !8, i64 0}
!20 = !{!6, !10, i64 72}


; Effective O3 passes: BDCEPass,DSEPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass