; LLVM IR from Aeternum-Veritas/Merry :: merry (merry_request_hdlr)

; 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.MerryRequestHdlr = type { ptr, ptr, ptr, i8 }
%struct.MerryOSRequest = type { i64, ptr, i64, i8, i8, i64 }
%struct.MerryRequestQueue = type { i64, i64, ptr, ptr }

@req_hdlr = external hidden global %struct.MerryRequestHdlr, align 8

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

declare void @merry_mutex_lock(ptr noundef) #1

declare void @merry_cond_signal(ptr noundef) #1

declare void @merry_mutex_unlock(ptr noundef) #1

; 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
define dso_local void @merry_requestHdlr_kill_requests() #2 {
  %1 = alloca %struct.MerryOSRequest, align 8
  %2 = alloca i64, align 8
  %3 = load ptr, ptr getelementptr inbounds (%struct.MerryRequestHdlr, ptr @req_hdlr, i32 0, i32 1), align 8, !tbaa !5
  call void @merry_mutex_lock(ptr noundef %3)
  store i8 0, ptr getelementptr inbounds (%struct.MerryRequestHdlr, ptr @req_hdlr, i32 0, i32 3), align 8, !tbaa !10
  call void @llvm.lifetime.start.p0(i64 40, ptr %1) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #3
  store i64 0, ptr %2, align 8, !tbaa !11
  br label %4

4:                                                ; preds = %16, %0
  %5 = load i64, ptr %2, align 8, !tbaa !11
  %6 = load ptr, ptr @req_hdlr, align 8, !tbaa !13
  %7 = getelementptr inbounds %struct.MerryRequestQueue, ptr %6, i32 0, i32 1
  %8 = load i64, ptr %7, align 8, !tbaa !14
  %9 = icmp ult i64 %5, %8
  br i1 %9, label %11, label %10

10:                                               ; preds = %4
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #3
  br label %19

11:                                               ; preds = %4
  %12 = load ptr, ptr @req_hdlr, align 8, !tbaa !13
  %13 = call zeroext i8 @merry_pop_request(ptr noundef %12, ptr noundef %1)
  %14 = getelementptr inbounds %struct.MerryOSRequest, ptr %1, i32 0, i32 1
  %15 = load ptr, ptr %14, align 8, !tbaa !16
  call void @merry_cond_signal(ptr noundef %15)
  br label %16

16:                                               ; preds = %11
  %17 = load i64, ptr %2, align 8, !tbaa !11
  %18 = add i64 %17, 1
  store i64 %18, ptr %2, align 8, !tbaa !11
  br label %4, !llvm.loop !18

19:                                               ; preds = %10
  %20 = load ptr, ptr getelementptr inbounds (%struct.MerryRequestHdlr, ptr @req_hdlr, i32 0, i32 1), align 8, !tbaa !5
  call void @merry_mutex_unlock(ptr noundef %20)
  call void @llvm.lifetime.end.p0(i64 40, ptr %1) #3
  ret void
}

declare zeroext i8 @merry_pop_request(ptr noundef, ptr noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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, !7, i64 8}
!6 = !{!"MerryRequestHdlr", !7, i64 0, !7, i64 8, !7, i64 16, !8, i64 24}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!6, !8, i64 24}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !8, i64 0}
!13 = !{!6, !7, i64 0}
!14 = !{!15, !12, i64 8}
!15 = !{!"MerryRequestQueue", !12, i64 0, !12, i64 8, !7, i64 16, !7, i64 24}
!16 = !{!17, !7, i64 8}
!17 = !{!"MerryOSRequest", !12, i64 0, !7, i64 8, !12, i64 16, !8, i64 24, !8, i64 25, !12, i64 32}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}


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

%struct.MerryRequestHdlr = type { ptr, ptr, ptr, i8 }
%struct.MerryOSRequest = type { i64, ptr, i64, i8, i8, i64 }

@req_hdlr = external hidden local_unnamed_addr global %struct.MerryRequestHdlr, align 8

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

declare void @merry_mutex_lock(ptr noundef) local_unnamed_addr #1

declare void @merry_cond_signal(ptr noundef) local_unnamed_addr #1

declare void @merry_mutex_unlock(ptr noundef) local_unnamed_addr #1

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

; Function Attrs: nounwind uwtable
define dso_local void @merry_requestHdlr_kill_requests() local_unnamed_addr #2 {
  %1 = alloca %struct.MerryOSRequest, align 8
  %2 = load ptr, ptr getelementptr inbounds (i8, ptr @req_hdlr, i64 8), align 8, !tbaa !5
  tail call void @merry_mutex_lock(ptr noundef %2) #3
  store i8 0, ptr getelementptr inbounds (i8, ptr @req_hdlr, i64 24), align 8, !tbaa !10
  call void @llvm.lifetime.start.p0(i64 40, ptr nonnull %1) #3
  %3 = load ptr, ptr @req_hdlr, align 8, !tbaa !11
  %4 = getelementptr inbounds i8, ptr %3, i64 8
  %5 = load i64, ptr %4, align 8, !tbaa !12
  %.not = icmp eq i64 %5, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %0
  %6 = getelementptr inbounds i8, ptr %1, i64 8
  br label %8

._crit_edge:                                      ; preds = %8, %0
  %7 = load ptr, ptr getelementptr inbounds (i8, ptr @req_hdlr, i64 8), align 8, !tbaa !5
  call void @merry_mutex_unlock(ptr noundef %7) #3
  call void @llvm.lifetime.end.p0(i64 40, ptr nonnull %1) #3
  ret void

8:                                                ; preds = %.lr.ph, %8
  %9 = phi ptr [ %3, %.lr.ph ], [ %13, %8 ]
  %.02 = phi i64 [ 0, %.lr.ph ], [ %12, %8 ]
  %10 = call zeroext i8 @merry_pop_request(ptr noundef nonnull %9, ptr noundef nonnull %1) #3
  %11 = load ptr, ptr %6, align 8, !tbaa !15
  call void @merry_cond_signal(ptr noundef %11) #3
  %12 = add nuw i64 %.02, 1
  %13 = load ptr, ptr @req_hdlr, align 8, !tbaa !11
  %14 = getelementptr inbounds i8, ptr %13, i64 8
  %15 = load i64, ptr %14, align 8, !tbaa !12
  %16 = icmp ult i64 %12, %15
  br i1 %16, label %8, label %._crit_edge, !llvm.loop !17
}

declare zeroext i8 @merry_pop_request(ptr noundef, ptr noundef) local_unnamed_addr #1

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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, !7, i64 8}
!6 = !{!"MerryRequestHdlr", !7, i64 0, !7, i64 8, !7, i64 16, !8, i64 24}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!6, !8, i64 24}
!11 = !{!6, !7, i64 0}
!12 = !{!13, !14, i64 8}
!13 = !{!"MerryRequestQueue", !14, i64 0, !14, i64 8, !7, i64 16, !7, i64 24}
!14 = !{!"long", !8, i64 0}
!15 = !{!16, !7, i64 8}
!16 = !{!"MerryOSRequest", !14, i64 0, !7, i64 8, !14, i64 16, !8, i64 24, !8, i64 25, !14, i64 32}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


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