; LLVM IR from libxsmm/libxsmm :: src (libxsmm_malloc)

; 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.libxsmm_scratch_info = type { i64, i64, i64, i64, i64, i32 }
%union.internal_malloc_pool_type = type { %struct.anon, [8 x i8] }
%struct.anon = type { i64, i64, i64, ptr, ptr, ptr, i32 }

@.str.1 = external hidden unnamed_addr constant [70 x i8], align 1
@libxsmm_scratch_pools = external global i32, align 4
@internal_malloc_pool_buffer = external dso_local global [65599 x i8], align 64
@internal_malloc_local_max = external dso_local global i64, align 64
@internal_malloc_private_max = external dso_local global i64, align 64
@internal_malloc_scratch_nmallocs = external dso_local global i64, align 64
@internal_malloc_public_max = external dso_local global i64, align 64
@.str.80 = external hidden unnamed_addr constant [30 x i8], align 1
@__PRETTY_FUNCTION__.libxsmm_get_scratch_info = external hidden unnamed_addr constant [53 x i8], align 1

; 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: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @libxsmm_get_scratch_info(ptr noundef %0) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i8, align 1
  %5 = alloca ptr, align 8
  %6 = alloca i8, align 1
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  store i32 0, ptr %3, align 4, !tbaa !9
  %10 = load ptr, ptr %2, align 8, !tbaa !5
  %11 = icmp ne ptr null, %10
  br i1 %11, label %12, label %89

12:                                               ; preds = %1
  br label %13

13:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 1, ptr %4) #3
  store i8 48, ptr %4, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %14 = load ptr, ptr %2, align 8, !tbaa !5
  store ptr %14, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %6) #3
  store i8 0, ptr %6, align 1, !tbaa !11
  br label %15

15:                                               ; preds = %26, %13
  %16 = load i8, ptr %6, align 1, !tbaa !11
  %17 = sext i8 %16 to i32
  %18 = load i8, ptr %4, align 1, !tbaa !11
  %19 = sext i8 %18 to i32
  %20 = icmp slt i32 %17, %19
  br i1 %20, label %21, label %29

21:                                               ; preds = %15
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  %23 = load i8, ptr %6, align 1, !tbaa !11
  %24 = sext i8 %23 to i64
  %25 = getelementptr inbounds i8, ptr %22, i64 %24
  store i8 0, ptr %25, align 1, !tbaa !11
  br label %26

26:                                               ; preds = %21
  %27 = load i8, ptr %6, align 1, !tbaa !11
  %28 = add i8 %27, 1
  store i8 %28, ptr %6, align 1, !tbaa !11
  br label %15, !llvm.loop !12

29:                                               ; preds = %15
  call void @llvm.lifetime.end.p0(i64 1, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %4) #3
  br label %30

30:                                               ; preds = %29
  br label %31

31:                                               ; preds = %30
  %32 = load i64, ptr @internal_malloc_scratch_nmallocs, align 64, !tbaa !15
  %33 = load ptr, ptr %2, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.libxsmm_scratch_info, ptr %33, i32 0, i32 4
  store i64 %32, ptr %34, align 8, !tbaa !17
  %35 = load i64, ptr @internal_malloc_private_max, align 64, !tbaa !15
  %36 = load ptr, ptr %2, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.libxsmm_scratch_info, ptr %36, i32 0, i32 2
  store i64 %35, ptr %37, align 8, !tbaa !19
  %38 = load i64, ptr @internal_malloc_local_max, align 64, !tbaa !15
  %39 = load ptr, ptr %2, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.libxsmm_scratch_info, ptr %39, i32 0, i32 1
  store i64 %38, ptr %40, align 8, !tbaa !20
  %41 = load i64, ptr @internal_malloc_public_max, align 64, !tbaa !15
  %42 = load ptr, ptr %2, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.libxsmm_scratch_info, ptr %42, i32 0, i32 0
  store i64 %41, ptr %43, align 8, !tbaa !21
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %44 = and i64 add (i64 ptrtoint (ptr @internal_malloc_pool_buffer to i64), i64 63), -64
  %45 = inttoptr i64 %44 to ptr
  store ptr %45, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %46 = load ptr, ptr %7, align 8, !tbaa !5
  %47 = load i32, ptr @libxsmm_scratch_pools, align 4, !tbaa !9
  %48 = zext i32 %47 to i64
  %49 = getelementptr inbounds %union.internal_malloc_pool_type, ptr %46, i64 %48
  store ptr %49, ptr %8, align 8, !tbaa !5
  %50 = load i32, ptr @libxsmm_scratch_pools, align 4, !tbaa !9
  %51 = icmp ule i32 %50, 1024
  br i1 %51, label %52, label %53

52:                                               ; preds = %31
  br label %54

53:                                               ; preds = %31
  call void @__assert_fail(ptr noundef @.str.80, ptr noundef @.str.1, i32 noundef 2581, ptr noundef @__PRETTY_FUNCTION__.libxsmm_get_scratch_info) #4
  unreachable

54:                                               ; preds = %52
  br label %55

55:                                               ; preds = %85, %54
  %56 = load ptr, ptr %7, align 8, !tbaa !5
  %57 = load ptr, ptr %8, align 8, !tbaa !5
  %58 = icmp ne ptr %56, %57
  br i1 %58, label %59, label %88

59:                                               ; preds = %55
  %60 = load ptr, ptr %7, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.anon, ptr %60, i32 0, i32 5
  %62 = load ptr, ptr %61, align 8, !tbaa !11
  %63 = icmp ne ptr inttoptr (i64 -1 to ptr), %62
  br i1 %63, label %64, label %84

64:                                               ; preds = %59
  %65 = load ptr, ptr %7, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.anon, ptr %65, i32 0, i32 0
  %67 = load i64, ptr %66, align 8, !tbaa !11
  %68 = icmp ne i64 0, %67
  br i1 %68, label %69, label %82

69:                                               ; preds = %64
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %70 = load ptr, ptr %7, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.anon, ptr %70, i32 0, i32 1
  %72 = load i64, ptr %71, align 8, !tbaa !11
  store i64 %72, ptr %9, align 8, !tbaa !15
  %73 = load i64, ptr %9, align 8, !tbaa !15
  %74 = load ptr, ptr %2, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.libxsmm_scratch_info, ptr %74, i32 0, i32 3
  %76 = load i64, ptr %75, align 8, !tbaa !22
  %77 = add i64 %76, %73
  store i64 %77, ptr %75, align 8, !tbaa !22
  %78 = load ptr, ptr %2, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.libxsmm_scratch_info, ptr %78, i32 0, i32 5
  %80 = load i32, ptr %79, align 8, !tbaa !23
  %81 = add i32 %80, 1
  store i32 %81, ptr %79, align 8, !tbaa !23
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  br label %83

82:                                               ; preds = %64
  br label %88

83:                                               ; preds = %69
  br label %84

84:                                               ; preds = %83, %59
  br label %85

85:                                               ; preds = %84
  %86 = load ptr, ptr %7, align 8, !tbaa !5
  %87 = getelementptr inbounds %union.internal_malloc_pool_type, ptr %86, i32 1
  store ptr %87, ptr %7, align 8, !tbaa !5
  br label %55, !llvm.loop !24

88:                                               ; preds = %82, %55
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  br label %90

89:                                               ; preds = %1
  store i32 1, ptr %3, align 4, !tbaa !9
  br label %90

90:                                               ; preds = %89, %88
  %91 = load i32, ptr %3, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  ret i32 %91
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 }
attributes #4 = { 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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13, !14}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!"llvm.loop.unroll.count", i32 4}
!15 = !{!16, !16, i64 0}
!16 = !{!"long", !7, i64 0}
!17 = !{!18, !16, i64 32}
!18 = !{!"libxsmm_scratch_info", !16, i64 0, !16, i64 8, !16, i64 16, !16, i64 24, !16, i64 32, !10, i64 40}
!19 = !{!18, !16, i64 16}
!20 = !{!18, !16, i64 8}
!21 = !{!18, !16, i64 0}
!22 = !{!18, !16, i64 24}
!23 = !{!18, !10, i64 40}
!24 = distinct !{!24, !13}


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

%union.internal_malloc_pool_type = type { %struct.anon, [8 x i8] }
%struct.anon = type { i64, i64, i64, ptr, ptr, ptr, i32 }

@.str.1 = external hidden unnamed_addr constant [70 x i8], align 1
@libxsmm_scratch_pools = external local_unnamed_addr global i32, align 4
@internal_malloc_pool_buffer = external dso_local global [65599 x i8], align 64
@internal_malloc_local_max = external dso_local local_unnamed_addr global i64, align 64
@internal_malloc_private_max = external dso_local local_unnamed_addr global i64, align 64
@internal_malloc_scratch_nmallocs = external dso_local local_unnamed_addr global i64, align 64
@internal_malloc_public_max = external dso_local local_unnamed_addr global i64, align 64
@.str.80 = external hidden unnamed_addr constant [30 x i8], align 1
@__PRETTY_FUNCTION__.libxsmm_get_scratch_info = external hidden unnamed_addr constant [53 x i8], align 1

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @libxsmm_get_scratch_info(ptr noundef writeonly %0) local_unnamed_addr #1 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %.loopexit, label %.preheader27.preheader

.preheader27.preheader:                           ; preds = %1
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 1 dereferenceable(48) %0, i8 0, i64 48, i1 false), !tbaa !5
  %2 = load i64, ptr @internal_malloc_scratch_nmallocs, align 64, !tbaa !8
  %3 = getelementptr inbounds i8, ptr %0, i64 32
  store i64 %2, ptr %3, align 8, !tbaa !10
  %4 = load i64, ptr @internal_malloc_private_max, align 64, !tbaa !8
  %5 = getelementptr inbounds i8, ptr %0, i64 16
  store i64 %4, ptr %5, align 8, !tbaa !13
  %6 = load i64, ptr @internal_malloc_local_max, align 64, !tbaa !8
  %7 = getelementptr inbounds i8, ptr %0, i64 8
  store i64 %6, ptr %7, align 8, !tbaa !14
  %8 = load i64, ptr @internal_malloc_public_max, align 64, !tbaa !8
  store i64 %8, ptr %0, align 8, !tbaa !15
  %9 = and i64 add (i64 ptrtoint (ptr @internal_malloc_pool_buffer to i64), i64 63), -64
  %10 = inttoptr i64 %9 to ptr
  %11 = load i32, ptr @libxsmm_scratch_pools, align 4, !tbaa !16
  %12 = zext i32 %11 to i64
  %13 = getelementptr inbounds %union.internal_malloc_pool_type, ptr %10, i64 %12
  %14 = icmp ult i32 %11, 1025
  br i1 %14, label %.preheader, label %17

.preheader:                                       ; preds = %.preheader27.preheader
  %.not2429 = icmp eq i32 %11, 0
  br i1 %.not2429, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader
  %15 = getelementptr inbounds i8, ptr %0, i64 24
  %16 = getelementptr inbounds i8, ptr %0, i64 40
  br label %18

17:                                               ; preds = %.preheader27.preheader
  tail call void @__assert_fail(ptr noundef nonnull @.str.80, ptr noundef nonnull @.str.1, i32 noundef 2581, ptr noundef nonnull @__PRETTY_FUNCTION__.libxsmm_get_scratch_info) #3
  unreachable

18:                                               ; preds = %.lr.ph, %30
  %19 = phi i32 [ 0, %.lr.ph ], [ %31, %30 ]
  %20 = phi i64 [ 0, %.lr.ph ], [ %32, %30 ]
  %.02230 = phi ptr [ %10, %.lr.ph ], [ %33, %30 ]
  %21 = getelementptr inbounds i8, ptr %.02230, i64 40
  %22 = load ptr, ptr %21, align 8, !tbaa !5
  %.not25 = icmp eq ptr %22, inttoptr (i64 -1 to ptr)
  br i1 %.not25, label %30, label %23

23:                                               ; preds = %18
  %24 = load i64, ptr %.02230, align 8, !tbaa !5
  %.not26 = icmp eq i64 %24, 0
  br i1 %.not26, label %.loopexit, label %25

25:                                               ; preds = %23
  %26 = getelementptr inbounds i8, ptr %.02230, i64 8
  %27 = load i64, ptr %26, align 8, !tbaa !5
  %28 = add i64 %20, %27
  store i64 %28, ptr %15, align 8, !tbaa !17
  %29 = add i32 %19, 1
  store i32 %29, ptr %16, align 8, !tbaa !18
  br label %30

30:                                               ; preds = %18, %25
  %31 = phi i32 [ %19, %18 ], [ %29, %25 ]
  %32 = phi i64 [ %20, %18 ], [ %28, %25 ]
  %33 = getelementptr inbounds i8, ptr %.02230, i64 64
  %.not24 = icmp eq ptr %33, %13
  br i1 %.not24, label %.loopexit, label %18, !llvm.loop !19

.loopexit:                                        ; preds = %23, %30, %.preheader, %1
  %.0 = phi i32 [ 1, %1 ], [ 0, %.preheader ], [ 0, %30 ], [ 0, %23 ]
  ret i32 %.0
}

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

attributes #0 = { 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 #1 = { 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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { 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, !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"long", !6, i64 0}
!10 = !{!11, !9, i64 32}
!11 = !{!"libxsmm_scratch_info", !9, i64 0, !9, i64 8, !9, i64 16, !9, i64 24, !9, i64 32, !12, i64 40}
!12 = !{!"int", !6, i64 0}
!13 = !{!11, !9, i64 16}
!14 = !{!11, !9, i64 8}
!15 = !{!11, !9, i64 0}
!16 = !{!12, !12, i64 0}
!17 = !{!11, !9, i64 24}
!18 = !{!11, !12, i64 40}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopIdiomRecognizePass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass