; LLVM IR from cvra/robot-software :: lib/msgbus (messagebus)

; 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.topic_s = type { ptr, i64, ptr, ptr, [65 x i8], i8, ptr, ptr, ptr, %struct.messagebus_topic_stats_t }
%struct.messagebus_topic_stats_t = type { i32 }
%struct.messagebus_watcher_s = type { ptr, ptr }
%struct.messagebus_watchgroup_s = type { ptr, ptr, ptr }

declare void @messagebus_lock_acquire(ptr noundef) #0

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

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

declare void @messagebus_condvar_broadcast(ptr noundef) #0

declare void @messagebus_lock_release(ptr noundef) #0

; Function Attrs: nounwind uwtable
define dso_local zeroext i1 @messagebus_topic_publish(ptr noundef %0, ptr noundef %1, i64 noundef %2) #2 {
  %4 = alloca i1, align 1
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i64 %2, ptr %7, align 8, !tbaa !9
  %9 = load ptr, ptr %5, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.topic_s, ptr %9, i32 0, i32 1
  %11 = load i64, ptr %10, align 8, !tbaa !11
  %12 = load i64, ptr %7, align 8, !tbaa !9
  %13 = icmp ult i64 %11, %12
  br i1 %13, label %14, label %15

14:                                               ; preds = %3
  store i1 false, ptr %4, align 1
  br label %69

15:                                               ; preds = %3
  %16 = load ptr, ptr %5, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.topic_s, ptr %16, i32 0, i32 2
  %18 = load ptr, ptr %17, align 8, !tbaa !16
  call void @messagebus_lock_acquire(ptr noundef %18)
  %19 = load ptr, ptr %5, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.topic_s, ptr %19, i32 0, i32 0
  %21 = load ptr, ptr %20, align 8, !tbaa !17
  %22 = load ptr, ptr %6, align 8, !tbaa !5
  %23 = load i64, ptr %7, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %21, ptr align 1 %22, i64 %23, i1 false)
  %24 = load ptr, ptr %5, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.topic_s, ptr %24, i32 0, i32 5
  store i8 1, ptr %25, align 1, !tbaa !18
  %26 = load ptr, ptr %5, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.topic_s, ptr %26, i32 0, i32 9
  %28 = getelementptr inbounds %struct.messagebus_topic_stats_t, ptr %27, i32 0, i32 0
  %29 = load i32, ptr %28, align 8, !tbaa !19
  %30 = add nsw i32 %29, 1
  store i32 %30, ptr %28, align 8, !tbaa !19
  %31 = load ptr, ptr %5, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.topic_s, ptr %31, i32 0, i32 3
  %33 = load ptr, ptr %32, align 8, !tbaa !20
  call void @messagebus_condvar_broadcast(ptr noundef %33)
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %34 = load ptr, ptr %5, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.topic_s, ptr %34, i32 0, i32 6
  %36 = load ptr, ptr %35, align 8, !tbaa !21
  store ptr %36, ptr %8, align 8, !tbaa !5
  br label %37

37:                                               ; preds = %61, %15
  %38 = load ptr, ptr %8, align 8, !tbaa !5
  %39 = icmp ne ptr %38, null
  br i1 %39, label %40, label %65

40:                                               ; preds = %37
  %41 = load ptr, ptr %8, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.messagebus_watcher_s, ptr %41, i32 0, i32 0
  %43 = load ptr, ptr %42, align 8, !tbaa !22
  %44 = getelementptr inbounds %struct.messagebus_watchgroup_s, ptr %43, i32 0, i32 0
  %45 = load ptr, ptr %44, align 8, !tbaa !24
  call void @messagebus_lock_acquire(ptr noundef %45)
  %46 = load ptr, ptr %5, align 8, !tbaa !5
  %47 = load ptr, ptr %8, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.messagebus_watcher_s, ptr %47, i32 0, i32 0
  %49 = load ptr, ptr %48, align 8, !tbaa !22
  %50 = getelementptr inbounds %struct.messagebus_watchgroup_s, ptr %49, i32 0, i32 2
  store ptr %46, ptr %50, align 8, !tbaa !26
  %51 = load ptr, ptr %8, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.messagebus_watcher_s, ptr %51, i32 0, i32 0
  %53 = load ptr, ptr %52, align 8, !tbaa !22
  %54 = getelementptr inbounds %struct.messagebus_watchgroup_s, ptr %53, i32 0, i32 1
  %55 = load ptr, ptr %54, align 8, !tbaa !27
  call void @messagebus_condvar_broadcast(ptr noundef %55)
  %56 = load ptr, ptr %8, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.messagebus_watcher_s, ptr %56, i32 0, i32 0
  %58 = load ptr, ptr %57, align 8, !tbaa !22
  %59 = getelementptr inbounds %struct.messagebus_watchgroup_s, ptr %58, i32 0, i32 0
  %60 = load ptr, ptr %59, align 8, !tbaa !24
  call void @messagebus_lock_release(ptr noundef %60)
  br label %61

61:                                               ; preds = %40
  %62 = load ptr, ptr %8, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.messagebus_watcher_s, ptr %62, i32 0, i32 1
  %64 = load ptr, ptr %63, align 8, !tbaa !28
  store ptr %64, ptr %8, align 8, !tbaa !5
  br label %37, !llvm.loop !29

65:                                               ; preds = %37
  %66 = load ptr, ptr %5, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.topic_s, ptr %66, i32 0, i32 2
  %68 = load ptr, ptr %67, align 8, !tbaa !16
  call void @messagebus_lock_release(ptr noundef %68)
  store i1 true, ptr %4, align 1
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  br label %69

69:                                               ; preds = %65, %14
  %70 = load i1, ptr %4, align 1
  ret i1 %70
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

attributes #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 = { 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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = !{!"long", !7, i64 0}
!11 = !{!12, !10, i64 8}
!12 = !{!"topic_s", !6, i64 0, !10, i64 8, !6, i64 16, !6, i64 24, !7, i64 32, !13, i64 97, !6, i64 104, !6, i64 112, !6, i64 120, !14, i64 128}
!13 = !{!"_Bool", !7, i64 0}
!14 = !{!"", !15, i64 0}
!15 = !{!"int", !7, i64 0}
!16 = !{!12, !6, i64 16}
!17 = !{!12, !6, i64 0}
!18 = !{!12, !13, i64 97}
!19 = !{!12, !15, i64 128}
!20 = !{!12, !6, i64 24}
!21 = !{!12, !6, i64 104}
!22 = !{!23, !6, i64 0}
!23 = !{!"messagebus_watcher_s", !6, i64 0, !6, i64 8}
!24 = !{!25, !6, i64 0}
!25 = !{!"messagebus_watchgroup_s", !6, i64 0, !6, i64 8, !6, i64 16}
!26 = !{!25, !6, i64 16}
!27 = !{!25, !6, i64 8}
!28 = !{!23, !6, i64 8}
!29 = distinct !{!29, !30}
!30 = !{!"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"

declare void @messagebus_lock_acquire(ptr noundef) local_unnamed_addr #0

declare void @messagebus_condvar_broadcast(ptr noundef) local_unnamed_addr #0

declare void @messagebus_lock_release(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local noundef zeroext i1 @messagebus_topic_publish(ptr noundef %0, ptr nocapture noundef readonly %1, i64 noundef %2) local_unnamed_addr #1 {
  %4 = getelementptr inbounds i8, ptr %0, i64 8
  %5 = load i64, ptr %4, align 8, !tbaa !5
  %6 = icmp uge i64 %5, %2
  br i1 %6, label %7, label %28

7:                                                ; preds = %3
  %8 = getelementptr inbounds i8, ptr %0, i64 16
  %9 = load ptr, ptr %8, align 8, !tbaa !14
  tail call void @messagebus_lock_acquire(ptr noundef %9) #3
  %10 = load ptr, ptr %0, align 8, !tbaa !15
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %10, ptr align 1 %1, i64 %2, i1 false)
  %11 = getelementptr inbounds i8, ptr %0, i64 97
  store i8 1, ptr %11, align 1, !tbaa !16
  %12 = getelementptr inbounds i8, ptr %0, i64 128
  %13 = load i32, ptr %12, align 8, !tbaa !17
  %14 = add nsw i32 %13, 1
  store i32 %14, ptr %12, align 8, !tbaa !17
  %15 = getelementptr inbounds i8, ptr %0, i64 24
  %16 = load ptr, ptr %15, align 8, !tbaa !18
  tail call void @messagebus_condvar_broadcast(ptr noundef %16) #3
  %17 = getelementptr inbounds i8, ptr %0, i64 104
  %.020 = load ptr, ptr %17, align 8, !tbaa !19
  %.not21 = icmp eq ptr %.020, null
  br i1 %.not21, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %7, %.lr.ph
  %.022 = phi ptr [ %.0, %.lr.ph ], [ %.020, %7 ]
  %18 = load ptr, ptr %.022, align 8, !tbaa !20
  %19 = load ptr, ptr %18, align 8, !tbaa !22
  tail call void @messagebus_lock_acquire(ptr noundef %19) #3
  %20 = load ptr, ptr %.022, align 8, !tbaa !20
  %21 = getelementptr inbounds i8, ptr %20, i64 16
  store ptr %0, ptr %21, align 8, !tbaa !24
  %22 = getelementptr inbounds i8, ptr %20, i64 8
  %23 = load ptr, ptr %22, align 8, !tbaa !25
  tail call void @messagebus_condvar_broadcast(ptr noundef %23) #3
  %24 = load ptr, ptr %.022, align 8, !tbaa !20
  %25 = load ptr, ptr %24, align 8, !tbaa !22
  tail call void @messagebus_lock_release(ptr noundef %25) #3
  %26 = getelementptr inbounds i8, ptr %.022, i64 8
  %.0 = load ptr, ptr %26, align 8, !tbaa !19
  %.not = icmp eq ptr %.0, null
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !26

._crit_edge:                                      ; preds = %.lr.ph, %7
  %27 = load ptr, ptr %8, align 8, !tbaa !14
  tail call void @messagebus_lock_release(ptr noundef %27) #3
  br label %28

28:                                               ; preds = %3, %._crit_edge
  ret i1 %6
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

attributes #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 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 #2 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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, !10, i64 8}
!6 = !{!"topic_s", !7, i64 0, !10, i64 8, !7, i64 16, !7, i64 24, !8, i64 32, !11, i64 97, !7, i64 104, !7, i64 112, !7, i64 120, !12, i64 128}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"_Bool", !8, i64 0}
!12 = !{!"", !13, i64 0}
!13 = !{!"int", !8, i64 0}
!14 = !{!6, !7, i64 16}
!15 = !{!6, !7, i64 0}
!16 = !{!6, !11, i64 97}
!17 = !{!6, !13, i64 128}
!18 = !{!6, !7, i64 24}
!19 = !{!7, !7, i64 0}
!20 = !{!21, !7, i64 0}
!21 = !{!"messagebus_watcher_s", !7, i64 0, !7, i64 8}
!22 = !{!23, !7, i64 0}
!23 = !{!"messagebus_watchgroup_s", !7, i64 0, !7, i64 8, !7, i64 16}
!24 = !{!23, !7, i64 16}
!25 = !{!23, !7, i64 8}
!26 = distinct !{!26, !27}
!27 = !{!"llvm.loop.mustprogress"}


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