; LLVM IR from xiaoxianrouzhiyou/BBearEditor-2.0 :: Code/BBearEditor/Engine/Allocator/tlsf (tlsf)

; 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.2 = external hidden unnamed_addr constant [113 x i8], align 1
@.str.29 = external hidden unnamed_addr constant [56 x i8], align 1
@__PRETTY_FUNCTION__.tlsf_realloc = external hidden unnamed_addr constant [43 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: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare hidden i32 @block_is_free(ptr noundef) #2

; Function Attrs: nounwind uwtable
declare hidden ptr @block_next(ptr noundef) #2

; Function Attrs: nounwind uwtable
declare hidden i64 @block_size(ptr noundef) #2

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

; Function Attrs: nounwind uwtable
declare hidden ptr @block_from_ptr(ptr noundef) #2

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

; Function Attrs: nounwind uwtable
declare hidden i64 @adjust_request_size(i64 noundef, i64 noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local void @tlsf_free(ptr noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
declare hidden ptr @block_merge_next(ptr noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local ptr @tlsf_realloc(ptr noundef %0, ptr noundef %1, i64 noundef %2) #2 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i64, align 8
  %12 = alloca i64, align 8
  %13 = alloca i64, align 8
  %14 = alloca i64, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i64 %2, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %15, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  store ptr null, ptr %8, align 8, !tbaa !5
  %16 = load ptr, ptr %5, align 8, !tbaa !5
  %17 = icmp ne ptr %16, null
  br i1 %17, label %18, label %24

18:                                               ; preds = %3
  %19 = load i64, ptr %6, align 8, !tbaa !9
  %20 = icmp eq i64 %19, 0
  br i1 %20, label %21, label %24

21:                                               ; preds = %18
  %22 = load ptr, ptr %4, align 8, !tbaa !5
  %23 = load ptr, ptr %5, align 8, !tbaa !5
  call void @tlsf_free(ptr noundef %22, ptr noundef %23)
  br label %101

24:                                               ; preds = %18, %3
  %25 = load ptr, ptr %5, align 8, !tbaa !5
  %26 = icmp ne ptr %25, null
  br i1 %26, label %31, label %27

27:                                               ; preds = %24
  %28 = load ptr, ptr %4, align 8, !tbaa !5
  %29 = load i64, ptr %6, align 8, !tbaa !9
  %30 = call ptr @tlsf_malloc(ptr noundef %28, i64 noundef %29)
  store ptr %30, ptr %8, align 8, !tbaa !5
  br label %100

31:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %32 = load ptr, ptr %5, align 8, !tbaa !5
  %33 = call ptr @block_from_ptr(ptr noundef %32)
  store ptr %33, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %34 = load ptr, ptr %9, align 8, !tbaa !5
  %35 = call ptr @block_next(ptr noundef %34)
  store ptr %35, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  %36 = load ptr, ptr %9, align 8, !tbaa !5
  %37 = call i64 @block_size(ptr noundef %36)
  store i64 %37, ptr %11, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #4
  %38 = load i64, ptr %11, align 8, !tbaa !9
  %39 = load ptr, ptr %10, align 8, !tbaa !5
  %40 = call i64 @block_size(ptr noundef %39)
  %41 = add i64 %38, %40
  %42 = add i64 %41, 8
  store i64 %42, ptr %12, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #4
  %43 = load i64, ptr %6, align 8, !tbaa !9
  %44 = call i64 @adjust_request_size(i64 noundef %43, i64 noundef 8)
  store i64 %44, ptr %13, align 8, !tbaa !9
  %45 = load ptr, ptr %9, align 8, !tbaa !5
  %46 = call i32 @block_is_free(ptr noundef %45)
  %47 = icmp ne i32 %46, 0
  br i1 %47, label %50, label %48

48:                                               ; preds = %31
  br i1 true, label %49, label %50

49:                                               ; preds = %48
  br label %51

50:                                               ; preds = %48, %31
  call void @__assert_fail(ptr noundef @.str.29, ptr noundef @.str.2, i32 noundef 1017, ptr noundef @__PRETTY_FUNCTION__.tlsf_realloc) #5
  unreachable

51:                                               ; preds = %49
  %52 = load i64, ptr %13, align 8, !tbaa !9
  %53 = load i64, ptr %11, align 8, !tbaa !9
  %54 = icmp ugt i64 %52, %53
  br i1 %54, label %55, label %85

55:                                               ; preds = %51
  %56 = load ptr, ptr %10, align 8, !tbaa !5
  %57 = call i32 @block_is_free(ptr noundef %56)
  %58 = icmp ne i32 %57, 0
  br i1 %58, label %59, label %63

59:                                               ; preds = %55
  %60 = load i64, ptr %13, align 8, !tbaa !9
  %61 = load i64, ptr %12, align 8, !tbaa !9
  %62 = icmp ugt i64 %60, %61
  br i1 %62, label %63, label %85

63:                                               ; preds = %59, %55
  %64 = load ptr, ptr %4, align 8, !tbaa !5
  %65 = load i64, ptr %6, align 8, !tbaa !9
  %66 = call ptr @tlsf_malloc(ptr noundef %64, i64 noundef %65)
  store ptr %66, ptr %8, align 8, !tbaa !5
  %67 = load ptr, ptr %8, align 8, !tbaa !5
  %68 = icmp ne ptr %67, null
  br i1 %68, label %69, label %84

69:                                               ; preds = %63
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #4
  %70 = load i64, ptr %11, align 8, !tbaa !9
  %71 = load i64, ptr %6, align 8, !tbaa !9
  %72 = icmp ult i64 %70, %71
  br i1 %72, label %73, label %75

73:                                               ; preds = %69
  %74 = load i64, ptr %11, align 8, !tbaa !9
  br label %77

75:                                               ; preds = %69
  %76 = load i64, ptr %6, align 8, !tbaa !9
  br label %77

77:                                               ; preds = %75, %73
  %78 = phi i64 [ %74, %73 ], [ %76, %75 ]
  store i64 %78, ptr %14, align 8, !tbaa !9
  %79 = load ptr, ptr %8, align 8, !tbaa !5
  %80 = load ptr, ptr %5, align 8, !tbaa !5
  %81 = load i64, ptr %14, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %79, ptr align 1 %80, i64 %81, i1 false)
  %82 = load ptr, ptr %4, align 8, !tbaa !5
  %83 = load ptr, ptr %5, align 8, !tbaa !5
  call void @tlsf_free(ptr noundef %82, ptr noundef %83)
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #4
  br label %84

84:                                               ; preds = %77, %63
  br label %99

85:                                               ; preds = %59, %51
  %86 = load i64, ptr %13, align 8, !tbaa !9
  %87 = load i64, ptr %11, align 8, !tbaa !9
  %88 = icmp ugt i64 %86, %87
  br i1 %88, label %89, label %94

89:                                               ; preds = %85
  %90 = load ptr, ptr %7, align 8, !tbaa !5
  %91 = load ptr, ptr %9, align 8, !tbaa !5
  %92 = call ptr @block_merge_next(ptr noundef %90, ptr noundef %91)
  %93 = load ptr, ptr %9, align 8, !tbaa !5
  call void @block_mark_as_used(ptr noundef %93)
  br label %94

94:                                               ; preds = %89, %85
  %95 = load ptr, ptr %7, align 8, !tbaa !5
  %96 = load ptr, ptr %9, align 8, !tbaa !5
  %97 = load i64, ptr %13, align 8, !tbaa !9
  call void @block_trim_used(ptr noundef %95, ptr noundef %96, i64 noundef %97)
  %98 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %98, ptr %8, align 8, !tbaa !5
  br label %99

99:                                               ; preds = %94, %84
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  br label %100

100:                                              ; preds = %99, %27
  br label %101

101:                                              ; preds = %100, %21
  %102 = load ptr, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  ret ptr %102
}

; 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) #3

; Function Attrs: nounwind uwtable
declare hidden void @block_mark_as_used(ptr noundef) #2

; Function Attrs: nounwind uwtable
declare hidden void @block_trim_used(ptr noundef, ptr noundef, i64 noundef) #2

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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { nounwind }
attributes #5 = { 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 = !{!"long", !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.2 = external hidden unnamed_addr constant [113 x i8], align 1
@.str.29 = external hidden unnamed_addr constant [56 x i8], align 1
@__PRETTY_FUNCTION__.tlsf_realloc = external hidden unnamed_addr constant [43 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 hidden i32 @block_is_free(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden ptr @block_next(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden i64 @block_size(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden ptr @block_from_ptr(ptr noundef) local_unnamed_addr #1

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

; Function Attrs: nounwind uwtable
declare hidden i64 @adjust_request_size(i64 noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @tlsf_free(ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden ptr @block_merge_next(ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local ptr @tlsf_realloc(ptr noundef %0, ptr noundef %1, i64 noundef %2) local_unnamed_addr #1 {
  %4 = icmp ne ptr %1, null
  %5 = icmp eq i64 %2, 0
  %or.cond = and i1 %4, %5
  br i1 %or.cond, label %6, label %7

6:                                                ; preds = %3
  tail call void @tlsf_free(ptr noundef %0, ptr noundef nonnull %1)
  br label %31

7:                                                ; preds = %3
  br i1 %4, label %10, label %8

8:                                                ; preds = %7
  %9 = tail call ptr @tlsf_malloc(ptr noundef %0, i64 noundef %2)
  br label %31

10:                                               ; preds = %7
  %11 = tail call ptr @block_from_ptr(ptr noundef nonnull %1)
  %12 = tail call ptr @block_next(ptr noundef %11)
  %13 = tail call i64 @block_size(ptr noundef %11)
  %14 = tail call i64 @block_size(ptr noundef %12)
  %15 = add i64 %13, 8
  %16 = add i64 %15, %14
  %17 = tail call i64 @adjust_request_size(i64 noundef %2, i64 noundef 8)
  %18 = tail call i32 @block_is_free(ptr noundef %11)
  %.not = icmp eq i32 %18, 0
  br i1 %.not, label %20, label %19

19:                                               ; preds = %10
  tail call void @__assert_fail(ptr noundef nonnull @.str.29, ptr noundef nonnull @.str.2, i32 noundef 1017, ptr noundef nonnull @__PRETTY_FUNCTION__.tlsf_realloc) #4
  unreachable

20:                                               ; preds = %10
  %21 = icmp ugt i64 %17, %13
  br i1 %21, label %22, label %.critedge

22:                                               ; preds = %20
  %23 = tail call i32 @block_is_free(ptr noundef %12)
  %.not48 = icmp eq i32 %23, 0
  %24 = icmp ugt i64 %17, %16
  %or.cond50 = select i1 %.not48, i1 true, i1 %24
  br i1 %or.cond50, label %25, label %29

25:                                               ; preds = %22
  %26 = tail call ptr @tlsf_malloc(ptr noundef %0, i64 noundef %2)
  %.not49 = icmp eq ptr %26, null
  br i1 %.not49, label %31, label %27

27:                                               ; preds = %25
  %28 = tail call i64 @llvm.umin.i64(i64 %13, i64 %2)
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %26, ptr nonnull align 1 %1, i64 %28, i1 false)
  tail call void @tlsf_free(ptr noundef %0, ptr noundef nonnull %1)
  br label %31

29:                                               ; preds = %22
  %30 = tail call ptr @block_merge_next(ptr noundef %0, ptr noundef %11)
  tail call void @block_mark_as_used(ptr noundef %11)
  br label %.critedge

.critedge:                                        ; preds = %20, %29
  tail call void @block_trim_used(ptr noundef %0, ptr noundef %11, i64 noundef %17)
  br label %31

31:                                               ; preds = %.critedge, %27, %25, %8, %6
  %.1 = phi ptr [ null, %6 ], [ %9, %8 ], [ %26, %27 ], [ null, %25 ], [ %1, %.critedge ]
  ret ptr %.1
}

; 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) #2

; Function Attrs: nounwind uwtable
declare hidden void @block_mark_as_used(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden void @block_trim_used(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umin.i64(i64, i64) #3

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


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