; 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: nounwind uwtable
define dso_local void @enqueue_batch(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %4 = call i32 @pthread_mutex_lock(ptr noundef @lock) #3
  br label %5

5:                                                ; preds = %15, %1
  %6 = load volatile i32, ptr @batch_count, align 4, !tbaa !9
  %7 = icmp sge i32 %6, 100
  br i1 %7, label %8, label %13

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

13:                                               ; preds = %8, %5
  %14 = phi i1 [ false, %5 ], [ %12, %8 ]
  br i1 %14, label %15, label %17

15:                                               ; preds = %13
  %16 = call i32 @pthread_cond_wait(ptr noundef @full_cv, ptr noundef @lock)
  br label %5, !llvm.loop !11

17:                                               ; preds = %13
  %18 = load ptr, ptr @batches, align 8, !tbaa !5
  %19 = load ptr, ptr %2, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.batch, ptr %19, i32 0, i32 0
  store ptr %18, ptr %20, align 8, !tbaa !13
  %21 = load ptr, ptr %2, align 8, !tbaa !5
  store ptr %21, ptr @batches, align 8, !tbaa !5
  %22 = load volatile i32, ptr @batch_count, align 4, !tbaa !9
  %23 = add nsw i32 %22, 1
  store volatile i32 %23, ptr @batch_count, align 4, !tbaa !9
  %24 = call i32 @pthread_cond_signal(ptr noundef @empty_cv) #3
  %25 = call i32 @pthread_mutex_unlock(ptr noundef @lock) #3
  ret void
}

; 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

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

!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 uwtable
define dso_local void @enqueue_batch(ptr noundef %0) local_unnamed_addr #0 {
  %2 = tail call i32 @pthread_mutex_lock(ptr noundef nonnull @lock) #3
  %3 = load volatile i32, ptr @batch_count, align 4, !tbaa !5
  %4 = icmp sgt i32 %3, 99
  br i1 %4, label %.lr.ph, label %.critedge

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

6:                                                ; preds = %.lr.ph
  %7 = tail call i32 @pthread_cond_wait(ptr noundef nonnull @full_cv, ptr noundef nonnull @lock) #3
  %8 = load volatile i32, ptr @batch_count, align 4, !tbaa !5
  %9 = icmp sgt i32 %8, 99
  br i1 %9, label %.lr.ph, label %.critedge, !llvm.loop !9

.critedge:                                        ; preds = %.lr.ph, %6, %1
  %10 = load ptr, ptr @batches, align 8, !tbaa !11
  store ptr %10, ptr %0, align 8, !tbaa !13
  store ptr %0, ptr @batches, align 8, !tbaa !11
  %11 = load volatile i32, ptr @batch_count, align 4, !tbaa !5
  %12 = add nsw i32 %11, 1
  store volatile i32 %12, ptr @batch_count, align 4, !tbaa !5
  %13 = tail call i32 @pthread_cond_signal(ptr noundef nonnull @empty_cv) #3
  %14 = tail call i32 @pthread_mutex_unlock(ptr noundef nonnull @lock) #3
  ret void
}

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

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

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

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

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

!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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!14, !12, i64 0}
!14 = !{!"batch", !12, i64 0, !7, i64 8}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass