; LLVM IR from stan-dev/math :: lib/tbb_2020.3/src/test/test_malloc_pools.ll (Z17CrossThreadGetMemlRm)

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

@_ZL9poolSpace = external hidden global ptr, align 8

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

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef ptr @_Z17CrossThreadGetMemlRm(i64 noundef %0, ptr noundef nonnull align 8 dereferenceable(8) %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  store i64 %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !9
  %7 = load ptr, ptr @_ZL9poolSpace, align 8, !tbaa !9
  %8 = load i64, ptr %4, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.PoolSpace, ptr %7, i64 %8
  %10 = getelementptr inbounds %struct.PoolSpace, ptr %9, i32 0, i32 0
  %11 = load i64, ptr %10, align 8, !tbaa !11
  %12 = load ptr, ptr %5, align 8, !tbaa !9
  %13 = load i64, ptr %12, align 8, !tbaa !5
  %14 = add i64 %11, %13
  %15 = load ptr, ptr @_ZL9poolSpace, align 8, !tbaa !9
  %16 = load i64, ptr %4, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.PoolSpace, ptr %15, i64 %16
  %18 = getelementptr inbounds %struct.PoolSpace, ptr %17, i32 0, i32 2
  %19 = load i64, ptr %18, align 8, !tbaa !14
  %20 = icmp ugt i64 %14, %19
  br i1 %20, label %21, label %22

21:                                               ; preds = %2
  store ptr null, ptr %3, align 8
  br label %49

22:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %23 = load ptr, ptr @_ZL9poolSpace, align 8, !tbaa !9
  %24 = load i64, ptr %4, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.PoolSpace, ptr %23, i64 %24
  %26 = getelementptr inbounds %struct.PoolSpace, ptr %25, i32 0, i32 3
  %27 = load ptr, ptr %26, align 8, !tbaa !15
  %28 = load ptr, ptr @_ZL9poolSpace, align 8, !tbaa !9
  %29 = load i64, ptr %4, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.PoolSpace, ptr %28, i64 %29
  %31 = getelementptr inbounds %struct.PoolSpace, ptr %30, i32 0, i32 0
  %32 = load i64, ptr %31, align 8, !tbaa !11
  %33 = getelementptr inbounds i8, ptr %27, i64 %32
  store ptr %33, ptr %6, align 8, !tbaa !9
  %34 = load ptr, ptr %5, align 8, !tbaa !9
  %35 = load i64, ptr %34, align 8, !tbaa !5
  %36 = load ptr, ptr @_ZL9poolSpace, align 8, !tbaa !9
  %37 = load i64, ptr %4, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.PoolSpace, ptr %36, i64 %37
  %39 = getelementptr inbounds %struct.PoolSpace, ptr %38, i32 0, i32 0
  %40 = load i64, ptr %39, align 8, !tbaa !11
  %41 = add i64 %40, %35
  store i64 %41, ptr %39, align 8, !tbaa !11
  %42 = load ptr, ptr @_ZL9poolSpace, align 8, !tbaa !9
  %43 = load i64, ptr %4, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.PoolSpace, ptr %42, i64 %43
  %45 = getelementptr inbounds %struct.PoolSpace, ptr %44, i32 0, i32 1
  %46 = load i32, ptr %45, align 8, !tbaa !16
  %47 = add nsw i32 %46, 1
  store i32 %47, ptr %45, align 8, !tbaa !16
  %48 = load ptr, ptr %6, align 8, !tbaa !9
  store ptr %48, ptr %3, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  br label %49

49:                                               ; preds = %22, %21
  %50 = load ptr, ptr %3, align 8
  ret ptr %50
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 }

!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 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"_ZTS9PoolSpace", !6, i64 0, !13, i64 8, !6, i64 16, !10, i64 24}
!13 = !{!"int", !7, i64 0}
!14 = !{!12, !6, i64 16}
!15 = !{!12, !10, i64 24}
!16 = !{!12, !13, i64 8}


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

@_ZL9poolSpace = external hidden local_unnamed_addr global ptr, align 8

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(readwrite, inaccessiblemem: none) uwtable
define dso_local noundef ptr @_Z17CrossThreadGetMemlRm(i64 noundef %0, ptr nocapture noundef nonnull readonly align 8 dereferenceable(8) %1) local_unnamed_addr #0 {
  %3 = load ptr, ptr @_ZL9poolSpace, align 8, !tbaa !5
  %4 = getelementptr inbounds %struct.PoolSpace, ptr %3, i64 %0
  %5 = load i64, ptr %4, align 8, !tbaa !9
  %6 = load i64, ptr %1, align 8, !tbaa !13
  %7 = add i64 %6, %5
  %8 = getelementptr inbounds i8, ptr %4, i64 16
  %9 = load i64, ptr %8, align 8, !tbaa !14
  %10 = icmp ugt i64 %7, %9
  br i1 %10, label %18, label %11

11:                                               ; preds = %2
  %12 = getelementptr inbounds i8, ptr %4, i64 24
  %13 = load ptr, ptr %12, align 8, !tbaa !15
  %14 = getelementptr inbounds i8, ptr %13, i64 %5
  store i64 %7, ptr %4, align 8, !tbaa !9
  %15 = getelementptr inbounds %struct.PoolSpace, ptr %3, i64 %0, i32 1
  %16 = load i32, ptr %15, align 8, !tbaa !16
  %17 = add nsw i32 %16, 1
  store i32 %17, ptr %15, align 8, !tbaa !16
  br label %18

18:                                               ; preds = %2, %11
  %.0 = phi ptr [ %14, %11 ], [ null, %2 ]
  ret ptr %.0
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(readwrite, inaccessiblemem: none) 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" }

!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, !11, i64 0}
!10 = !{!"_ZTS9PoolSpace", !11, i64 0, !12, i64 8, !11, i64 16, !6, i64 24}
!11 = !{!"long", !7, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!11, !11, i64 0}
!14 = !{!10, !11, i64 16}
!15 = !{!10, !6, i64 24}
!16 = !{!10, !12, i64 8}


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