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

@.str.1 = external hidden unnamed_addr constant [70 x i8], align 1
@internal_malloc_kind = external dso_local global i32, align 64
@libxsmm_ninit = external global i32, align 4
@.str.78 = external hidden unnamed_addr constant [56 x i8], align 1
@__PRETTY_FUNCTION__.libxsmm_realloc = external hidden unnamed_addr constant [38 x i8], align 1
@.str.79 = external hidden unnamed_addr constant [38 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
declare dso_local void @internal_scratch_malloc(ptr noundef, i64 noundef, i64 noundef, i32 noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local i32 @libxsmm_xmalloc(ptr noundef, i64 noundef, i64 noundef, i32 noundef, ptr noundef, i64 noundef) #2

declare void @libxsmm_init() #3

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @libxsmm_trace_caller_id(i32 noundef) #4

; Function Attrs: nounwind uwtable
define dso_local ptr @libxsmm_realloc(i64 noundef %0, ptr noundef %1) #2 {
  %3 = alloca i64, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  store i64 %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #6
  %9 = load ptr, ptr %4, align 8, !tbaa !9
  %10 = ptrtoint ptr %9 to i64
  %11 = icmp ne i64 0, %10
  %12 = zext i1 %11 to i32
  %13 = load ptr, ptr %4, align 8, !tbaa !9
  %14 = ptrtoint ptr %13 to i64
  %15 = call i64 @llvm.cttz.i64(i64 %14, i1 true)
  %16 = trunc i64 %15 to i32
  %17 = mul nsw i32 %12, %16
  store i32 %17, ptr %5, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #6
  %18 = load i32, ptr %5, align 4, !tbaa !11
  %19 = shl i32 1, %18
  store i32 %19, ptr %6, align 4, !tbaa !11
  %20 = load ptr, ptr %4, align 8, !tbaa !9
  %21 = ptrtoint ptr %20 to i64
  %22 = load i32, ptr %5, align 4, !tbaa !11
  %23 = zext i32 %22 to i64
  %24 = shl i64 -1, %23
  %25 = xor i64 %24, -1
  %26 = and i64 %21, %25
  %27 = icmp eq i64 0, %26
  br i1 %27, label %28, label %29

28:                                               ; preds = %2
  br label %30

29:                                               ; preds = %2
  call void @__assert_fail(ptr noundef @.str.78, ptr noundef @.str.1, i32 noundef 2388, ptr noundef @__PRETTY_FUNCTION__.libxsmm_realloc) #7
  unreachable

30:                                               ; preds = %28
  %31 = load i32, ptr @libxsmm_ninit, align 4, !tbaa !11
  %32 = icmp ugt i32 2, %31
  br i1 %32, label %33, label %34

33:                                               ; preds = %30
  call void @libxsmm_init()
  br label %34

34:                                               ; preds = %33, %30
  %35 = load i32, ptr @internal_malloc_kind, align 64, !tbaa !11
  %36 = icmp sgt i32 2, %35
  br i1 %36, label %37, label %50

37:                                               ; preds = %34
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #6
  %38 = load i64, ptr %3, align 8, !tbaa !5
  %39 = load i32, ptr %6, align 4, !tbaa !11
  %40 = sext i32 %39 to i64
  %41 = call i32 @libxsmm_xmalloc(ptr noundef %4, i64 noundef %38, i64 noundef %40, i32 noundef 4, ptr noundef null, i64 noundef 0)
  store i32 %41, ptr %7, align 4, !tbaa !11
  %42 = load i32, ptr %7, align 4, !tbaa !11
  %43 = icmp eq i32 0, %42
  br i1 %43, label %47, label %44

44:                                               ; preds = %37
  %45 = load ptr, ptr %4, align 8, !tbaa !9
  %46 = icmp eq ptr null, %45
  br i1 %46, label %47, label %48

47:                                               ; preds = %44, %37
  br label %49

48:                                               ; preds = %44
  call void @__assert_fail(ptr noundef @.str.79, ptr noundef @.str.1, i32 noundef 2395, ptr noundef @__PRETTY_FUNCTION__.libxsmm_realloc) #7
  unreachable

49:                                               ; preds = %47
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #6
  br label %56

50:                                               ; preds = %34
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #6
  %51 = call ptr @libxsmm_trace_caller_id(i32 noundef 0)
  store ptr %51, ptr %8, align 8, !tbaa !9
  %52 = load i64, ptr %3, align 8, !tbaa !5
  %53 = load i32, ptr %6, align 4, !tbaa !11
  %54 = sext i32 %53 to i64
  %55 = load ptr, ptr %8, align 8, !tbaa !9
  call void @internal_scratch_malloc(ptr noundef %4, i64 noundef %52, i64 noundef %54, i32 noundef 4, ptr noundef %55)
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #6
  br label %56

56:                                               ; preds = %50, %49
  %57 = load ptr, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #6
  ret ptr %57
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.cttz.i64(i64, i1 immarg) #5

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 = { "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 = { inlinehint 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 #5 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #6 = { nounwind }
attributes #7 = { 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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}


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

@.str.1 = external hidden unnamed_addr constant [70 x i8], align 1
@internal_malloc_kind = external dso_local local_unnamed_addr global i32, align 64
@libxsmm_ninit = external local_unnamed_addr global i32, align 4
@.str.78 = external hidden unnamed_addr constant [56 x i8], align 1
@__PRETTY_FUNCTION__.libxsmm_realloc = external hidden unnamed_addr constant [38 x i8], align 1
@.str.79 = external hidden unnamed_addr constant [38 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
declare dso_local void @internal_scratch_malloc(ptr noundef, i64 noundef, i64 noundef, i32 noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @libxsmm_xmalloc(ptr noundef, i64 noundef, i64 noundef, i32 noundef, ptr noundef, i64 noundef) local_unnamed_addr #1

declare void @libxsmm_init() local_unnamed_addr #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @libxsmm_trace_caller_id(i32 noundef) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
define dso_local ptr @libxsmm_realloc(i64 noundef %0, ptr noundef %1) local_unnamed_addr #1 {
  %3 = alloca ptr, align 8
  store ptr %1, ptr %3, align 8, !tbaa !5
  %4 = ptrtoint ptr %1 to i64
  %.not = icmp eq ptr %1, null
  %5 = tail call range(i64 0, 65) i64 @llvm.cttz.i64(i64 %4, i1 true)
  %6 = trunc nuw nsw i64 %5 to i32
  %7 = select i1 %.not, i32 0, i32 %6
  %8 = shl nuw i32 1, %7
  %9 = zext nneg i32 %7 to i64
  %10 = shl nsw i64 -1, %9
  %11 = xor i64 %10, -1
  %12 = and i64 %11, %4
  %13 = icmp eq i64 %12, 0
  br i1 %13, label %15, label %14

14:                                               ; preds = %2
  tail call void @__assert_fail(ptr noundef nonnull @.str.78, ptr noundef nonnull @.str.1, i32 noundef 2388, ptr noundef nonnull @__PRETTY_FUNCTION__.libxsmm_realloc) #5
  unreachable

15:                                               ; preds = %2
  %16 = load i32, ptr @libxsmm_ninit, align 4, !tbaa !9
  %17 = icmp ult i32 %16, 2
  br i1 %17, label %18, label %19

18:                                               ; preds = %15
  tail call void @libxsmm_init() #6
  br label %19

19:                                               ; preds = %18, %15
  %20 = load i32, ptr @internal_malloc_kind, align 64, !tbaa !9
  %21 = icmp slt i32 %20, 2
  br i1 %21, label %22, label %29

22:                                               ; preds = %19
  %23 = sext i32 %8 to i64
  %24 = call i32 @libxsmm_xmalloc(ptr noundef nonnull %3, i64 noundef %0, i64 noundef %23, i32 noundef 4, ptr noundef null, i64 noundef 0)
  %25 = icmp eq i32 %24, 0
  %26 = load ptr, ptr %3, align 8
  %27 = icmp eq ptr %26, null
  %or.cond = select i1 %25, i1 true, i1 %27
  br i1 %or.cond, label %32, label %28

28:                                               ; preds = %22
  call void @__assert_fail(ptr noundef nonnull @.str.79, ptr noundef nonnull @.str.1, i32 noundef 2395, ptr noundef nonnull @__PRETTY_FUNCTION__.libxsmm_realloc) #5
  unreachable

29:                                               ; preds = %19
  %30 = tail call ptr @libxsmm_trace_caller_id(i32 noundef 0)
  %31 = sext i32 %8 to i64
  call void @internal_scratch_malloc(ptr noundef nonnull %3, i64 noundef %0, i64 noundef %31, i32 noundef 4, ptr noundef %30)
  %.pre = load ptr, ptr %3, align 8, !tbaa !5
  br label %32

32:                                               ; preds = %22, %29
  %33 = phi ptr [ %26, %22 ], [ %.pre, %29 ]
  ret ptr %33
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.cttz.i64(i64, i1 immarg) #4

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 = { "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 = { inlinehint 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 nosync nounwind speculatable willreturn memory(none) }
attributes #5 = { noreturn nounwind }
attributes #6 = { 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}


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