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

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

; Function Attrs: nounwind uwtable
define dso_local void @merry_requestHdlr_empty() #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)
  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 !10
  br label %4

4:                                                ; preds = %14, %0
  %5 = load i64, ptr %2, align 8, !tbaa !10
  %6 = load ptr, ptr @req_hdlr, align 8, !tbaa !12
  %7 = getelementptr inbounds %struct.MerryRequestQueue, ptr %6, i32 0, i32 1
  %8 = load i64, ptr %7, align 8, !tbaa !13
  %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 %17

11:                                               ; preds = %4
  %12 = load ptr, ptr @req_hdlr, align 8, !tbaa !12
  %13 = call zeroext i8 @merry_pop_request(ptr noundef %12, ptr noundef %1)
  br label %14

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

17:                                               ; preds = %10
  %18 = 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 %18)
  call void @llvm.lifetime.end.p0(i64 40, ptr %1) #3
  ret void
}

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 = !{!11, !11, i64 0}
!11 = !{!"long", !8, i64 0}
!12 = !{!6, !7, i64 0}
!13 = !{!14, !11, i64 8}
!14 = !{!"MerryRequestQueue", !11, i64 0, !11, i64 8, !7, i64 16, !7, i64 24}
!15 = distinct !{!15, !16}
!16 = !{!"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_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

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

; Function Attrs: nounwind uwtable
define dso_local void @merry_requestHdlr_empty() 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
  call void @llvm.lifetime.start.p0(i64 40, ptr nonnull %1) #3
  %3 = load ptr, ptr @req_hdlr, align 8, !tbaa !10
  %4 = getelementptr inbounds i8, ptr %3, i64 8
  %5 = load i64, ptr %4, align 8, !tbaa !11
  %.not = icmp eq i64 %5, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph

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

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

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, !7, i64 0}
!11 = !{!12, !13, i64 8}
!12 = !{!"MerryRequestQueue", !13, i64 0, !13, i64 8, !7, i64 16, !7, i64 24}
!13 = !{!"long", !8, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


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