; LLVM IR from daanx/mimalloc-bench :: bench/xmalloc-test (xmalloc-test)

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

%union.pthread_cond_t = type { %struct.__pthread_cond_s }
%struct.__pthread_cond_s = type { %union.__atomic_wide_counter, %union.__atomic_wide_counter, [2 x i32], [2 x i32], i32, i32, [2 x i32] }
%union.__atomic_wide_counter = type { i64 }
%union.pthread_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }
%struct.batch = type { ptr, [4096 x ptr] }

@done_flag = external dso_local global i32, align 4
@batches = external dso_local global ptr, align 8
@batch_count = external dso_local global i32, align 4
@empty_cv = external dso_local global %union.pthread_cond_t, align 8
@full_cv = external dso_local global %union.pthread_cond_t, align 8
@lock = external dso_local global %union.pthread_mutex_t, 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: nounwind
declare i32 @pthread_mutex_lock(ptr noundef) #1

declare i32 @pthread_cond_wait(ptr noundef, ptr noundef) #2

; Function Attrs: nounwind
declare i32 @pthread_cond_signal(ptr noundef) #1

; Function Attrs: nounwind
declare i32 @pthread_mutex_unlock(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local ptr @dequeue_batch() #3 {
  %1 = alloca i32, align 4
  %2 = alloca ptr, align 8
  %3 = call i32 @pthread_mutex_lock(ptr noundef @lock) #4
  br label %4

4:                                                ; preds = %14, %0
  %5 = load ptr, ptr @batches, align 8, !tbaa !5
  %6 = icmp eq ptr %5, null
  br i1 %6, label %7, label %12

7:                                                ; preds = %4
  %8 = load atomic volatile i32, ptr @done_flag acquire, align 4
  store i32 %8, ptr %1, align 4
  %9 = load i32, ptr %1, align 4, !tbaa !9
  %10 = icmp ne i32 %9, 0
  %11 = xor i1 %10, true
  br label %12

12:                                               ; preds = %7, %4
  %13 = phi i1 [ false, %4 ], [ %11, %7 ]
  br i1 %13, label %14, label %16

14:                                               ; preds = %12
  %15 = call i32 @pthread_cond_wait(ptr noundef @empty_cv, ptr noundef @lock)
  br label %4, !llvm.loop !11

16:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #4
  %17 = load ptr, ptr @batches, align 8, !tbaa !5
  store ptr %17, ptr %2, align 8, !tbaa !5
  %18 = load ptr, ptr %2, align 8, !tbaa !5
  %19 = icmp ne ptr %18, null
  br i1 %19, label %20, label %27

20:                                               ; preds = %16
  %21 = load ptr, ptr %2, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.batch, ptr %21, i32 0, i32 0
  %23 = load ptr, ptr %22, align 8, !tbaa !13
  store ptr %23, ptr @batches, align 8, !tbaa !5
  %24 = load volatile i32, ptr @batch_count, align 4, !tbaa !9
  %25 = add nsw i32 %24, -1
  store volatile i32 %25, ptr @batch_count, align 4, !tbaa !9
  %26 = call i32 @pthread_cond_signal(ptr noundef @full_cv) #4
  br label %27

27:                                               ; preds = %20, %16
  %28 = call i32 @pthread_mutex_unlock(ptr noundef @lock) #4
  %29 = load ptr, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #4
  ret ptr %29
}

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

!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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!14, !6, i64 0}
!14 = !{!"batch", !6, i64 0, !7, 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"

%union.pthread_cond_t = type { %struct.__pthread_cond_s }
%struct.__pthread_cond_s = type { %union.__atomic_wide_counter, %union.__atomic_wide_counter, [2 x i32], [2 x i32], i32, i32, [2 x i32] }
%union.__atomic_wide_counter = type { i64 }
%union.pthread_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }

@done_flag = external dso_local global i32, align 4
@batches = external dso_local local_unnamed_addr global ptr, align 8
@batch_count = external dso_local global i32, align 4
@empty_cv = external dso_local global %union.pthread_cond_t, align 8
@full_cv = external dso_local global %union.pthread_cond_t, align 8
@lock = external dso_local global %union.pthread_mutex_t, align 8

; Function Attrs: nounwind
declare i32 @pthread_mutex_lock(ptr noundef) local_unnamed_addr #0

declare i32 @pthread_cond_wait(ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind
declare i32 @pthread_cond_signal(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind
declare i32 @pthread_mutex_unlock(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local noundef ptr @dequeue_batch() local_unnamed_addr #2 {
  %1 = tail call i32 @pthread_mutex_lock(ptr noundef nonnull @lock) #3
  %2 = load ptr, ptr @batches, align 8, !tbaa !5
  %3 = icmp eq ptr %2, null
  br i1 %3, label %.lr.ph, label %.critedge.thread

.lr.ph:                                           ; preds = %0, %5
  %4 = load atomic volatile i32, ptr @done_flag acquire, align 4
  %.not = icmp eq i32 %4, 0
  br i1 %.not, label %5, label %.critedge

5:                                                ; preds = %.lr.ph
  %6 = tail call i32 @pthread_cond_wait(ptr noundef nonnull @empty_cv, ptr noundef nonnull @lock) #3
  %7 = load ptr, ptr @batches, align 8, !tbaa !5
  %8 = icmp eq ptr %7, null
  br i1 %8, label %.lr.ph, label %.critedge.thread, !llvm.loop !9

.critedge:                                        ; preds = %.lr.ph
  %.pr = load ptr, ptr @batches, align 8, !tbaa !5
  %.not3 = icmp eq ptr %.pr, null
  br i1 %.not3, label %14, label %.critedge.thread

.critedge.thread:                                 ; preds = %5, %0, %.critedge
  %9 = phi ptr [ %.pr, %.critedge ], [ %2, %0 ], [ %7, %5 ]
  %10 = load ptr, ptr %9, align 8, !tbaa !11
  store ptr %10, ptr @batches, align 8, !tbaa !5
  %11 = load volatile i32, ptr @batch_count, align 4, !tbaa !13
  %12 = add nsw i32 %11, -1
  store volatile i32 %12, ptr @batch_count, align 4, !tbaa !13
  %13 = tail call i32 @pthread_cond_signal(ptr noundef nonnull @full_cv) #3
  br label %14

14:                                               ; preds = %.critedge.thread, %.critedge
  %15 = phi ptr [ %9, %.critedge.thread ], [ null, %.critedge ]
  %16 = tail call i32 @pthread_mutex_unlock(ptr noundef nonnull @lock) #3
  ret ptr %15
}

attributes #0 = { 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 = { "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 }

!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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!12, !6, i64 0}
!12 = !{!"batch", !6, i64 0, !7, i64 8}
!13 = !{!14, !14, i64 0}
!14 = !{!"int", !7, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass