; 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.MerryRequestQueue = type { i64, i64, ptr, ptr }

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

; Function Attrs: nounwind uwtable
define dso_local zeroext i8 @merry_requestHdlr_push_request(i64 noundef %0, i64 noundef %1, ptr noundef %2) #0 {
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i8, align 1
  store i64 %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !5
  store ptr %2, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 1, ptr %7) #4
  store i8 0, ptr %7, align 1, !tbaa !11
  %8 = load ptr, ptr getelementptr inbounds (%struct.MerryRequestHdlr, ptr @req_hdlr, i32 0, i32 1), align 8, !tbaa !12
  call void @merry_mutex_lock(ptr noundef %8)
  %9 = load i8, ptr getelementptr inbounds (%struct.MerryRequestHdlr, ptr @req_hdlr, i32 0, i32 3), align 8, !tbaa !14
  %10 = zext i8 %9 to i32
  %11 = icmp eq i32 %10, 0
  br i1 %11, label %12, label %13

12:                                               ; preds = %3
  br label %51

13:                                               ; preds = %3
  %14 = load ptr, ptr @req_hdlr, align 8, !tbaa !15
  %15 = load ptr, ptr %6, align 8, !tbaa !9
  %16 = load i64, ptr %4, align 8, !tbaa !5
  %17 = load i64, ptr %5, align 8, !tbaa !5
  %18 = call zeroext i8 @merry_push_request(ptr noundef %14, ptr noundef %15, i64 noundef %16, i64 noundef %17)
  %19 = zext i8 %18 to i32
  %20 = icmp eq i32 %19, 0
  br i1 %20, label %21, label %30

21:                                               ; preds = %13
  %22 = load ptr, ptr @req_hdlr, align 8, !tbaa !15
  call void @merry_panic_push(ptr noundef %22, i64 noundef 1)
  %23 = load ptr, ptr @req_hdlr, align 8, !tbaa !15
  %24 = getelementptr inbounds %struct.MerryRequestQueue, ptr %23, i32 0, i32 1
  %25 = load i64, ptr %24, align 8, !tbaa !16
  %26 = icmp eq i64 %25, 1
  br i1 %26, label %27, label %29

27:                                               ; preds = %21
  %28 = load ptr, ptr getelementptr inbounds (%struct.MerryRequestHdlr, ptr @req_hdlr, i32 0, i32 2), align 8, !tbaa !18
  call void @merry_cond_signal(ptr noundef %28)
  br label %29

29:                                               ; preds = %27, %21
  store i8 1, ptr %7, align 1, !tbaa !11
  br label %50

30:                                               ; preds = %13
  %31 = load ptr, ptr @req_hdlr, align 8, !tbaa !15
  %32 = getelementptr inbounds %struct.MerryRequestQueue, ptr %31, i32 0, i32 1
  %33 = load i64, ptr %32, align 8, !tbaa !16
  %34 = icmp eq i64 %33, 1
  br i1 %34, label %35, label %37

35:                                               ; preds = %30
  %36 = load ptr, ptr getelementptr inbounds (%struct.MerryRequestHdlr, ptr @req_hdlr, i32 0, i32 2), align 8, !tbaa !18
  call void @merry_cond_signal(ptr noundef %36)
  br label %37

37:                                               ; preds = %35, %30
  %38 = load ptr, ptr %6, align 8, !tbaa !9
  %39 = icmp ne ptr %38, null
  %40 = xor i1 %39, true
  %41 = xor i1 %40, true
  %42 = zext i1 %41 to i32
  %43 = sext i32 %42 to i64
  %44 = call i64 @llvm.expect.i64(i64 %43, i64 1)
  %45 = icmp ne i64 %44, 0
  br i1 %45, label %46, label %49

46:                                               ; preds = %37
  %47 = load ptr, ptr %6, align 8, !tbaa !9
  %48 = load ptr, ptr getelementptr inbounds (%struct.MerryRequestHdlr, ptr @req_hdlr, i32 0, i32 1), align 8, !tbaa !12
  call void @merry_cond_wait(ptr noundef %47, ptr noundef %48)
  br label %49

49:                                               ; preds = %46, %37
  br label %50

50:                                               ; preds = %49, %29
  br label %51

51:                                               ; preds = %50, %12
  %52 = load ptr, ptr getelementptr inbounds (%struct.MerryRequestHdlr, ptr @req_hdlr, i32 0, i32 1), align 8, !tbaa !12
  call void @merry_mutex_unlock(ptr noundef %52)
  %53 = load i8, ptr %7, align 1, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 1, ptr %7) #4
  ret i8 %53
}

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

declare void @merry_mutex_lock(ptr noundef) #2

declare zeroext i8 @merry_push_request(ptr noundef, ptr noundef, i64 noundef, i64 noundef) #2

declare void @merry_panic_push(ptr noundef, i64 noundef) #2

declare void @merry_cond_signal(ptr noundef) #2

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

declare void @merry_cond_wait(ptr noundef, ptr noundef) #2

declare void @merry_mutex_unlock(ptr noundef) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { nocallback nofree nosync nounwind willreturn memory(none) }
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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !10, i64 8}
!13 = !{!"MerryRequestHdlr", !10, i64 0, !10, i64 8, !10, i64 16, !7, i64 24}
!14 = !{!13, !7, i64 24}
!15 = !{!13, !10, i64 0}
!16 = !{!17, !6, i64 8}
!17 = !{!"MerryRequestQueue", !6, i64 0, !6, i64 8, !10, i64 16, !10, i64 24}
!18 = !{!13, !10, i64 16}


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

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

; Function Attrs: nounwind uwtable
define dso_local zeroext range(i8 0, 2) i8 @merry_requestHdlr_push_request(i64 noundef %0, i64 noundef %1, ptr noundef %2) local_unnamed_addr #0 {
  %4 = load ptr, ptr getelementptr inbounds (i8, ptr @req_hdlr, i64 8), align 8, !tbaa !5
  tail call void @merry_mutex_lock(ptr noundef %4) #2
  %5 = load i8, ptr getelementptr inbounds (i8, ptr @req_hdlr, i64 24), align 8, !tbaa !10
  %6 = icmp eq i8 %5, 0
  br i1 %6, label %28, label %7

7:                                                ; preds = %3
  %8 = load ptr, ptr @req_hdlr, align 8, !tbaa !11
  %9 = tail call zeroext i8 @merry_push_request(ptr noundef %8, ptr noundef %2, i64 noundef %0, i64 noundef %1) #2
  %10 = icmp eq i8 %9, 0
  %11 = load ptr, ptr @req_hdlr, align 8, !tbaa !11
  br i1 %10, label %12, label %19

12:                                               ; preds = %7
  tail call void @merry_panic_push(ptr noundef %11, i64 noundef 1) #2
  %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 eq i64 %15, 1
  br i1 %16, label %17, label %28

17:                                               ; preds = %12
  %18 = load ptr, ptr getelementptr inbounds (i8, ptr @req_hdlr, i64 16), align 8, !tbaa !15
  tail call void @merry_cond_signal(ptr noundef %18) #2
  br label %28

19:                                               ; preds = %7
  %20 = getelementptr inbounds i8, ptr %11, i64 8
  %21 = load i64, ptr %20, align 8, !tbaa !12
  %22 = icmp eq i64 %21, 1
  br i1 %22, label %23, label %25

23:                                               ; preds = %19
  %24 = load ptr, ptr getelementptr inbounds (i8, ptr @req_hdlr, i64 16), align 8, !tbaa !15
  tail call void @merry_cond_signal(ptr noundef %24) #2
  br label %25

25:                                               ; preds = %23, %19
  %.not = icmp eq ptr %2, null
  br i1 %.not, label %28, label %26, !prof !16

26:                                               ; preds = %25
  %27 = load ptr, ptr getelementptr inbounds (i8, ptr @req_hdlr, i64 8), align 8, !tbaa !5
  tail call void @merry_cond_wait(ptr noundef nonnull %2, ptr noundef %27) #2
  br label %28

28:                                               ; preds = %12, %17, %26, %25, %3
  %.0 = phi i8 [ 0, %3 ], [ 0, %26 ], [ 0, %25 ], [ 1, %17 ], [ 1, %12 ]
  %29 = load ptr, ptr getelementptr inbounds (i8, ptr @req_hdlr, i64 8), align 8, !tbaa !5
  tail call void @merry_mutex_unlock(ptr noundef %29) #2
  ret i8 %.0
}

declare void @merry_mutex_lock(ptr noundef) local_unnamed_addr #1

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

declare void @merry_panic_push(ptr noundef, i64 noundef) local_unnamed_addr #1

declare void @merry_cond_signal(ptr noundef) local_unnamed_addr #1

declare void @merry_cond_wait(ptr noundef, ptr noundef) local_unnamed_addr #1

declare void @merry_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 = { "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 }

!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 = !{!6, !7, i64 16}
!16 = !{!"branch_weights", !"expected", i32 1, i32 2000}


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