; LLVM IR from aap/librw :: src/engine.ll (ZN2rw14malloc_managedEmj)

; 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.rw::LinkList" = type { %"struct.rw::LLLink" }
%"struct.rw::LLLink" = type { ptr, ptr }
%"struct.rw::MemoryBlock" = type { i64, i32, ptr, ptr, %"struct.rw::LLLink" }

@_ZN2rw13allocLocationE = external dso_local global ptr, align 8
@_ZN2rw11allocationsE = external dso_local global %"struct.rw::LinkList", align 8
@_ZN2rw20totalMemoryAllocatedE = external dso_local global i64, align 8

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

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_ZN2rw14malloc_managedEmj(i64 noundef %0, i32 noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  store i64 %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) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %10 = load i64, ptr %4, align 8, !tbaa !5
  %11 = icmp eq i64 %10, 0
  br i1 %11, label %12, label %13

12:                                               ; preds = %2
  store ptr null, ptr %3, align 8
  store i32 1, ptr %9, align 4
  br label %50

13:                                               ; preds = %2
  %14 = load i64, ptr %4, align 8, !tbaa !5
  %15 = add i64 %14, 48
  %16 = add i64 %15, 15
  %17 = call noalias ptr @malloc(i64 noundef %16) #5
  store ptr %17, ptr %6, align 8, !tbaa !11
  %18 = load ptr, ptr %6, align 8, !tbaa !11
  %19 = icmp eq ptr %18, null
  br i1 %19, label %20, label %21

20:                                               ; preds = %13
  store ptr null, ptr %3, align 8
  store i32 1, ptr %9, align 4
  br label %50

21:                                               ; preds = %13
  %22 = load i64, ptr %4, align 8, !tbaa !5
  %23 = load i64, ptr @_ZN2rw20totalMemoryAllocatedE, align 8, !tbaa !5
  %24 = add i64 %23, %22
  store i64 %24, ptr @_ZN2rw20totalMemoryAllocatedE, align 8, !tbaa !5
  %25 = load ptr, ptr %6, align 8, !tbaa !11
  store ptr %25, ptr %7, align 8, !tbaa !11
  %26 = load ptr, ptr %7, align 8, !tbaa !11
  %27 = getelementptr inbounds i8, ptr %26, i64 48
  store ptr %27, ptr %7, align 8, !tbaa !11
  %28 = load ptr, ptr %7, align 8, !tbaa !11
  %29 = ptrtoint ptr %28 to i64
  %30 = add i64 %29, 15
  %31 = and i64 %30, -16
  %32 = inttoptr i64 %31 to ptr
  store ptr %32, ptr %7, align 8, !tbaa !11
  %33 = load ptr, ptr %7, align 8, !tbaa !11
  %34 = getelementptr inbounds i8, ptr %33, i64 -48
  store ptr %34, ptr %8, align 8, !tbaa !11
  %35 = load i64, ptr %4, align 8, !tbaa !5
  %36 = load ptr, ptr %8, align 8, !tbaa !11
  %37 = getelementptr inbounds %"struct.rw::MemoryBlock", ptr %36, i32 0, i32 0
  store i64 %35, ptr %37, align 8, !tbaa !13
  %38 = load i32, ptr %5, align 4, !tbaa !9
  %39 = load ptr, ptr %8, align 8, !tbaa !11
  %40 = getelementptr inbounds %"struct.rw::MemoryBlock", ptr %39, i32 0, i32 1
  store i32 %38, ptr %40, align 8, !tbaa !16
  %41 = load ptr, ptr %6, align 8, !tbaa !11
  %42 = load ptr, ptr %8, align 8, !tbaa !11
  %43 = getelementptr inbounds %"struct.rw::MemoryBlock", ptr %42, i32 0, i32 2
  store ptr %41, ptr %43, align 8, !tbaa !17
  %44 = load ptr, ptr @_ZN2rw13allocLocationE, align 8, !tbaa !11
  %45 = load ptr, ptr %8, align 8, !tbaa !11
  %46 = getelementptr inbounds %"struct.rw::MemoryBlock", ptr %45, i32 0, i32 3
  store ptr %44, ptr %46, align 8, !tbaa !18
  %47 = load ptr, ptr %8, align 8, !tbaa !11
  %48 = getelementptr inbounds %"struct.rw::MemoryBlock", ptr %47, i32 0, i32 4
  call void @_ZN2rw8LinkList3addEPNS_6LLLinkE(ptr noundef nonnull align 8 dereferenceable(16) @_ZN2rw11allocationsE, ptr noundef %48)
  %49 = load ptr, ptr %7, align 8, !tbaa !11
  store ptr %49, ptr %3, align 8
  store i32 1, ptr %9, align 4
  br label %50

50:                                               ; preds = %21, %20, %12
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  %51 = load ptr, ptr %3, align 8
  ret ptr %51
}

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN2rw8LinkList3addEPNS_6LLLinkE(ptr noundef nonnull align 8 dereferenceable(16), ptr noundef) #3 align 2

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

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 = { 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 = { 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 = !{!"long", !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, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!14, !6, i64 0}
!14 = !{!"_ZTSN2rw11MemoryBlockE", !6, i64 0, !10, i64 8, !12, i64 16, !12, i64 24, !15, i64 32}
!15 = !{!"_ZTSN2rw6LLLinkE", !12, i64 0, !12, i64 8}
!16 = !{!14, !10, i64 8}
!17 = !{!14, !12, i64 16}
!18 = !{!14, !12, i64 24}


; 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.rw::LinkList" = type { %"struct.rw::LLLink" }
%"struct.rw::LLLink" = type { ptr, ptr }

@_ZN2rw13allocLocationE = external dso_local local_unnamed_addr global ptr, align 8
@_ZN2rw11allocationsE = external dso_local global %"struct.rw::LinkList", align 8
@_ZN2rw20totalMemoryAllocatedE = external dso_local local_unnamed_addr global i64, align 8

; 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 nounwind uwtable
define dso_local noundef ptr @_ZN2rw14malloc_managedEmj(i64 noundef %0, i32 noundef %1) local_unnamed_addr #1 {
  %3 = icmp eq i64 %0, 0
  br i1 %3, label %22, label %4

4:                                                ; preds = %2
  %5 = add i64 %0, 63
  %6 = tail call noalias ptr @malloc(i64 noundef %5) #2
  %7 = icmp eq ptr %6, null
  br i1 %7, label %22, label %8

8:                                                ; preds = %4
  %9 = load i64, ptr @_ZN2rw20totalMemoryAllocatedE, align 8, !tbaa !5
  %10 = add i64 %9, %0
  store i64 %10, ptr @_ZN2rw20totalMemoryAllocatedE, align 8, !tbaa !5
  %11 = getelementptr inbounds i8, ptr %6, i64 48
  %12 = ptrtoint ptr %11 to i64
  %13 = add i64 %12, 15
  %14 = and i64 %13, -16
  %15 = inttoptr i64 %14 to ptr
  %16 = getelementptr inbounds i8, ptr %15, i64 -48
  store i64 %0, ptr %16, align 16, !tbaa !9
  %17 = getelementptr inbounds i8, ptr %15, i64 -40
  store i32 %1, ptr %17, align 8, !tbaa !14
  %18 = getelementptr inbounds i8, ptr %15, i64 -32
  store ptr %6, ptr %18, align 16, !tbaa !15
  %19 = load ptr, ptr @_ZN2rw13allocLocationE, align 8, !tbaa !16
  %20 = getelementptr inbounds i8, ptr %15, i64 -24
  store ptr %19, ptr %20, align 8, !tbaa !17
  %21 = getelementptr inbounds i8, ptr %15, i64 -16
  tail call void @_ZN2rw8LinkList3addEPNS_6LLLinkE(ptr noundef nonnull align 8 dereferenceable(16) @_ZN2rw11allocationsE, ptr noundef nonnull %21)
  br label %22

22:                                               ; preds = %4, %2, %8
  %.0 = phi ptr [ %15, %8 ], [ null, %2 ], [ null, %4 ]
  ret ptr %.0
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN2rw8LinkList3addEPNS_6LLLinkE(ptr noundef nonnull align 8 dereferenceable(16), ptr noundef) local_unnamed_addr #1 align 2

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 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 #2 = { 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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !6, i64 0}
!10 = !{!"_ZTSN2rw11MemoryBlockE", !6, i64 0, !11, i64 8, !12, i64 16, !12, i64 24, !13, i64 32}
!11 = !{!"int", !7, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!"_ZTSN2rw6LLLinkE", !12, i64 0, !12, i64 8}
!14 = !{!10, !11, i64 8}
!15 = !{!10, !12, i64 16}
!16 = !{!12, !12, i64 0}
!17 = !{!10, !12, i64 24}


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