; LLVM IR from Sopiro/Muli :: src/util/linear_allocator.ll (ZN4muli15LinearAllocator8AllocateEi)

; 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.muli::LinearAllocator" = type { %"class.muli::Allocator", ptr, i32, i32, ptr, i32, i32, i32, i32 }
%"class.muli::Allocator" = type { ptr }
%"struct.muli::LinearAllocator::MemoryEntry" = type { ptr, i32, i8 }

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #0

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local void @_ZN4muli4FreeEPv(ptr noundef) #2

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_ZN4muli15LinearAllocator8AllocateEi(ptr noundef nonnull align 8 dereferenceable(48) %0, i32 noundef %1) unnamed_addr #3 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %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.muli::LinearAllocator", ptr %7, i32 0, i32 2
  %9 = load i32, ptr %8, align 8, !tbaa !11
  %10 = getelementptr inbounds %"class.muli::LinearAllocator", ptr %7, i32 0, i32 3
  %11 = load i32, ptr %10, align 4, !tbaa !14
  %12 = icmp eq i32 %9, %11
  br i1 %12, label %13, label %49

13:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #6
  %14 = getelementptr inbounds %"class.muli::LinearAllocator", ptr %7, i32 0, i32 1
  %15 = load ptr, ptr %14, align 8, !tbaa !15
  store ptr %15, ptr %5, align 8, !tbaa !5
  %16 = getelementptr inbounds %"class.muli::LinearAllocator", ptr %7, i32 0, i32 3
  %17 = load i32, ptr %16, align 4, !tbaa !14
  %18 = sdiv i32 %17, 2
  %19 = getelementptr inbounds %"class.muli::LinearAllocator", ptr %7, i32 0, i32 3
  %20 = load i32, ptr %19, align 4, !tbaa !14
  %21 = add nsw i32 %20, %18
  store i32 %21, ptr %19, align 4, !tbaa !14
  %22 = getelementptr inbounds %"class.muli::LinearAllocator", ptr %7, i32 0, i32 3
  %23 = load i32, ptr %22, align 4, !tbaa !14
  %24 = sext i32 %23 to i64
  %25 = mul i64 %24, 16
  %26 = call noalias ptr @malloc(i64 noundef %25) #7
  %27 = getelementptr inbounds %"class.muli::LinearAllocator", ptr %7, i32 0, i32 1
  store ptr %26, ptr %27, align 8, !tbaa !15
  %28 = getelementptr inbounds %"class.muli::LinearAllocator", ptr %7, i32 0, i32 1
  %29 = load ptr, ptr %28, align 8, !tbaa !15
  %30 = load ptr, ptr %5, align 8, !tbaa !5
  %31 = getelementptr inbounds %"class.muli::LinearAllocator", ptr %7, i32 0, i32 2
  %32 = load i32, ptr %31, align 8, !tbaa !11
  %33 = sext i32 %32 to i64
  %34 = mul i64 %33, 16
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %29, ptr align 8 %30, i64 %34, i1 false)
  %35 = getelementptr inbounds %"class.muli::LinearAllocator", ptr %7, i32 0, i32 1
  %36 = load ptr, ptr %35, align 8, !tbaa !15
  %37 = getelementptr inbounds %"class.muli::LinearAllocator", ptr %7, i32 0, i32 2
  %38 = load i32, ptr %37, align 8, !tbaa !11
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds %"struct.muli::LinearAllocator::MemoryEntry", ptr %36, i64 %39
  %41 = getelementptr inbounds %"class.muli::LinearAllocator", ptr %7, i32 0, i32 3
  %42 = load i32, ptr %41, align 4, !tbaa !14
  %43 = getelementptr inbounds %"class.muli::LinearAllocator", ptr %7, i32 0, i32 2
  %44 = load i32, ptr %43, align 8, !tbaa !11
  %45 = sub nsw i32 %42, %44
  %46 = sext i32 %45 to i64
  %47 = mul i64 %46, 16
  call void @llvm.memset.p0.i64(ptr align 8 %40, i8 0, i64 %47, i1 false)
  %48 = load ptr, ptr %5, align 8, !tbaa !5
  call void @_ZN4muli4FreeEPv(ptr noundef %48)
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #6
  br label %49

49:                                               ; preds = %13, %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #6
  %50 = getelementptr inbounds %"class.muli::LinearAllocator", ptr %7, i32 0, i32 1
  %51 = load ptr, ptr %50, align 8, !tbaa !15
  %52 = getelementptr inbounds %"class.muli::LinearAllocator", ptr %7, i32 0, i32 2
  %53 = load i32, ptr %52, align 8, !tbaa !11
  %54 = sext i32 %53 to i64
  %55 = getelementptr inbounds %"struct.muli::LinearAllocator::MemoryEntry", ptr %51, i64 %54
  store ptr %55, ptr %6, align 8, !tbaa !5
  %56 = load i32, ptr %4, align 4, !tbaa !9
  %57 = load ptr, ptr %6, align 8, !tbaa !5
  %58 = getelementptr inbounds %"struct.muli::LinearAllocator::MemoryEntry", ptr %57, i32 0, i32 1
  store i32 %56, ptr %58, align 8, !tbaa !16
  %59 = getelementptr inbounds %"class.muli::LinearAllocator", ptr %7, i32 0, i32 6
  %60 = load i32, ptr %59, align 4, !tbaa !19
  %61 = load i32, ptr %4, align 4, !tbaa !9
  %62 = add nsw i32 %60, %61
  %63 = getelementptr inbounds %"class.muli::LinearAllocator", ptr %7, i32 0, i32 5
  %64 = load i32, ptr %63, align 8, !tbaa !20
  %65 = icmp sgt i32 %62, %64
  br i1 %65, label %66, label %74

66:                                               ; preds = %49
  %67 = load i32, ptr %4, align 4, !tbaa !9
  %68 = sext i32 %67 to i64
  %69 = call noalias ptr @malloc(i64 noundef %68) #7
  %70 = load ptr, ptr %6, align 8, !tbaa !5
  %71 = getelementptr inbounds %"struct.muli::LinearAllocator::MemoryEntry", ptr %70, i32 0, i32 0
  store ptr %69, ptr %71, align 8, !tbaa !21
  %72 = load ptr, ptr %6, align 8, !tbaa !5
  %73 = getelementptr inbounds %"struct.muli::LinearAllocator::MemoryEntry", ptr %72, i32 0, i32 2
  store i8 1, ptr %73, align 4, !tbaa !22
  br label %89

74:                                               ; preds = %49
  %75 = getelementptr inbounds %"class.muli::LinearAllocator", ptr %7, i32 0, i32 4
  %76 = load ptr, ptr %75, align 8, !tbaa !23
  %77 = getelementptr inbounds %"class.muli::LinearAllocator", ptr %7, i32 0, i32 6
  %78 = load i32, ptr %77, align 4, !tbaa !19
  %79 = sext i32 %78 to i64
  %80 = getelementptr inbounds i8, ptr %76, i64 %79
  %81 = load ptr, ptr %6, align 8, !tbaa !5
  %82 = getelementptr inbounds %"struct.muli::LinearAllocator::MemoryEntry", ptr %81, i32 0, i32 0
  store ptr %80, ptr %82, align 8, !tbaa !21
  %83 = load ptr, ptr %6, align 8, !tbaa !5
  %84 = getelementptr inbounds %"struct.muli::LinearAllocator::MemoryEntry", ptr %83, i32 0, i32 2
  store i8 0, ptr %84, align 4, !tbaa !22
  %85 = load i32, ptr %4, align 4, !tbaa !9
  %86 = getelementptr inbounds %"class.muli::LinearAllocator", ptr %7, i32 0, i32 6
  %87 = load i32, ptr %86, align 4, !tbaa !19
  %88 = add nsw i32 %87, %85
  store i32 %88, ptr %86, align 4, !tbaa !19
  br label %89

89:                                               ; preds = %74, %66
  %90 = load i32, ptr %4, align 4, !tbaa !9
  %91 = getelementptr inbounds %"class.muli::LinearAllocator", ptr %7, i32 0, i32 7
  %92 = load i32, ptr %91, align 8, !tbaa !24
  %93 = add nsw i32 %92, %90
  store i32 %93, ptr %91, align 8, !tbaa !24
  %94 = getelementptr inbounds %"class.muli::LinearAllocator", ptr %7, i32 0, i32 7
  %95 = load i32, ptr %94, align 8, !tbaa !24
  %96 = getelementptr inbounds %"class.muli::LinearAllocator", ptr %7, i32 0, i32 8
  %97 = load i32, ptr %96, align 4, !tbaa !25
  %98 = icmp sgt i32 %95, %97
  br i1 %98, label %99, label %103

99:                                               ; preds = %89
  %100 = getelementptr inbounds %"class.muli::LinearAllocator", ptr %7, i32 0, i32 7
  %101 = load i32, ptr %100, align 8, !tbaa !24
  %102 = getelementptr inbounds %"class.muli::LinearAllocator", ptr %7, i32 0, i32 8
  store i32 %101, ptr %102, align 4, !tbaa !25
  br label %103

103:                                              ; preds = %99, %89
  %104 = getelementptr inbounds %"class.muli::LinearAllocator", ptr %7, i32 0, i32 2
  %105 = load i32, ptr %104, align 8, !tbaa !11
  %106 = add nsw i32 %105, 1
  store i32 %106, ptr %104, align 8, !tbaa !11
  %107 = load ptr, ptr %6, align 8, !tbaa !5
  %108 = getelementptr inbounds %"struct.muli::LinearAllocator::MemoryEntry", ptr %107, i32 0, i32 0
  %109 = load ptr, ptr %108, align 8, !tbaa !21
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #6
  ret ptr %109
}

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

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #5

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

attributes #0 = { 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 #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { 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 #3 = { 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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #6 = { nounwind }
attributes #7 = { 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 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 16}
!12 = !{!"_ZTSN4muli15LinearAllocatorE", !13, i64 0, !6, i64 8, !10, i64 16, !10, i64 20, !6, i64 24, !10, i64 32, !10, i64 36, !10, i64 40, !10, i64 44}
!13 = !{!"_ZTSN4muli9AllocatorE"}
!14 = !{!12, !10, i64 20}
!15 = !{!12, !6, i64 8}
!16 = !{!17, !10, i64 8}
!17 = !{!"_ZTSN4muli15LinearAllocator11MemoryEntryE", !6, i64 0, !10, i64 8, !18, i64 12}
!18 = !{!"bool", !7, i64 0}
!19 = !{!12, !10, i64 36}
!20 = !{!12, !10, i64 32}
!21 = !{!17, !6, i64 0}
!22 = !{!17, !18, i64 12}
!23 = !{!12, !6, i64 24}
!24 = !{!12, !10, i64 40}
!25 = !{!12, !10, i64 44}


; 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.muli::LinearAllocator::MemoryEntry" = type { ptr, i32, i8 }

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local void @_ZN4muli4FreeEPv(ptr noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef ptr @_ZN4muli15LinearAllocator8AllocateEi(ptr nocapture noundef nonnull align 8 dereferenceable(48) %0, i32 noundef %1) unnamed_addr #3 align 2 {
  %3 = getelementptr inbounds i8, ptr %0, i64 16
  %4 = load i32, ptr %3, align 8, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %0, i64 20
  %6 = load i32, ptr %5, align 4, !tbaa !12
  %7 = icmp eq i32 %4, %6
  br i1 %7, label %8, label %21

8:                                                ; preds = %2
  %9 = getelementptr inbounds i8, ptr %0, i64 8
  %10 = load ptr, ptr %9, align 8, !tbaa !13
  %11 = sdiv i32 %4, 2
  %12 = add nsw i32 %11, %4
  store i32 %12, ptr %5, align 4, !tbaa !12
  %13 = sext i32 %12 to i64
  %14 = shl nsw i64 %13, 4
  %15 = tail call noalias ptr @malloc(i64 noundef %14) #5
  store ptr %15, ptr %9, align 8, !tbaa !13
  %16 = sext i32 %4 to i64
  %17 = shl nsw i64 %16, 4
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 8 %15, ptr align 8 %10, i64 %17, i1 false)
  %18 = getelementptr inbounds %"struct.muli::LinearAllocator::MemoryEntry", ptr %15, i64 %16
  %19 = sext i32 %11 to i64
  %20 = shl nsw i64 %19, 4
  tail call void @llvm.memset.p0.i64(ptr align 8 %18, i8 0, i64 %20, i1 false)
  tail call void @_ZN4muli4FreeEPv(ptr noundef %10)
  %.pre = load i32, ptr %3, align 8, !tbaa !5
  br label %21

21:                                               ; preds = %8, %2
  %22 = phi i32 [ %.pre, %8 ], [ %4, %2 ]
  %23 = getelementptr inbounds i8, ptr %0, i64 8
  %24 = load ptr, ptr %23, align 8, !tbaa !13
  %25 = sext i32 %22 to i64
  %26 = getelementptr inbounds %"struct.muli::LinearAllocator::MemoryEntry", ptr %24, i64 %25
  %27 = getelementptr inbounds i8, ptr %26, i64 8
  store i32 %1, ptr %27, align 8, !tbaa !14
  %28 = getelementptr inbounds i8, ptr %0, i64 36
  %29 = load i32, ptr %28, align 4, !tbaa !17
  %30 = add nsw i32 %29, %1
  %31 = getelementptr inbounds i8, ptr %0, i64 32
  %32 = load i32, ptr %31, align 8, !tbaa !18
  %33 = icmp sgt i32 %30, %32
  br i1 %33, label %34, label %37

34:                                               ; preds = %21
  %35 = sext i32 %1 to i64
  %36 = tail call noalias ptr @malloc(i64 noundef %35) #5
  br label %42

37:                                               ; preds = %21
  %38 = getelementptr inbounds i8, ptr %0, i64 24
  %39 = load ptr, ptr %38, align 8, !tbaa !19
  %40 = sext i32 %29 to i64
  %41 = getelementptr inbounds i8, ptr %39, i64 %40
  store i32 %30, ptr %28, align 4, !tbaa !17
  br label %42

42:                                               ; preds = %37, %34
  %43 = phi ptr [ %36, %34 ], [ %41, %37 ]
  %.sink = phi i8 [ 1, %34 ], [ 0, %37 ]
  store ptr %43, ptr %26, align 8
  %44 = getelementptr inbounds i8, ptr %26, i64 12
  store i8 %.sink, ptr %44, align 4
  %45 = getelementptr inbounds i8, ptr %0, i64 40
  %46 = load i32, ptr %45, align 8, !tbaa !20
  %47 = add nsw i32 %46, %1
  store i32 %47, ptr %45, align 8, !tbaa !20
  %48 = getelementptr inbounds i8, ptr %0, i64 44
  %49 = load i32, ptr %48, align 4, !tbaa !21
  %50 = icmp sgt i32 %47, %49
  br i1 %50, label %51, label %52

51:                                               ; preds = %42
  store i32 %47, ptr %48, align 4, !tbaa !21
  br label %52

52:                                               ; preds = %51, %42
  %53 = add nsw i32 %22, 1
  store i32 %53, ptr %3, align 8, !tbaa !5
  ret ptr %43
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #4

attributes #0 = { 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 #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { 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 #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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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, !11, i64 16}
!6 = !{!"_ZTSN4muli15LinearAllocatorE", !7, i64 0, !8, i64 8, !11, i64 16, !11, i64 20, !8, i64 24, !11, i64 32, !11, i64 36, !11, i64 40, !11, i64 44}
!7 = !{!"_ZTSN4muli9AllocatorE"}
!8 = !{!"any pointer", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C++ TBAA"}
!11 = !{!"int", !9, i64 0}
!12 = !{!6, !11, i64 20}
!13 = !{!6, !8, i64 8}
!14 = !{!15, !11, i64 8}
!15 = !{!"_ZTSN4muli15LinearAllocator11MemoryEntryE", !8, i64 0, !11, i64 8, !16, i64 12}
!16 = !{!"bool", !9, i64 0}
!17 = !{!6, !11, i64 36}
!18 = !{!6, !11, i64 32}
!19 = !{!6, !8, i64 24}
!20 = !{!6, !11, i64 40}
!21 = !{!6, !11, i64 44}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,MergedLoadStoreMotionPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,TailCallElimPass