; LLVM IR from mkazhdan/DMG :: Util/Socket.ll (ZN18SharedMemoryBuffer11FirstStream5writeEPKhi)

; 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.Signaller = type <{ ptr, i8, [7 x i8] }>
%"class.SharedMemoryBuffer::FirstStream" = type { %class.DataStream, ptr }
%class.DataStream = type { ptr }
%class.SharedMemoryBuffer = type { ptr, ptr, i32, i32, %struct.Signal, %struct.Signal }
%struct.Signal = type { %"class.boost::mutex", i8, %"class.boost::condition_variable" }
%"class.boost::mutex" = type { %union.pthread_mutex_t }
%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 }
%"class.boost::condition_variable" = type { %union.pthread_mutex_t, %union.pthread_cond_t }
%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 }

@stdout = external global ptr, align 8
@.str.14 = external hidden unnamed_addr constant [9 x i8], align 1

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

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

declare i32 @__gxx_personality_v0(...)

declare i32 @printf(ptr noundef, ...) #1

declare i32 @fflush(ptr noundef) #1

; Function Attrs: nounwind
declare void @free(ptr noundef) #2

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN9SignallerC2ER6Signalb(ptr noundef nonnull align 8 dereferenceable(9), ptr noundef nonnull align 8 dereferenceable(136), i1 noundef zeroext) unnamed_addr #3 align 2

; 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) #4

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN9SignallerD2Ev(ptr noundef nonnull align 8 dereferenceable(9)) unnamed_addr #5 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef zeroext i1 @_ZN18SharedMemoryBuffer11FirstStream5writeEPKhi(ptr noundef nonnull align 8 dereferenceable(16) %0, ptr noundef %1, i32 noundef %2) unnamed_addr #3 align 2 personality ptr @__gxx_personality_v0 {
  %4 = alloca i1, align 1
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca %struct.Signaller, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !9
  %12 = load ptr, ptr %5, align 8
  call void @llvm.lifetime.start.p0(i64 16, ptr %8) #6
  %13 = getelementptr inbounds %"class.SharedMemoryBuffer::FirstStream", ptr %12, i32 0, i32 1
  %14 = load ptr, ptr %13, align 8, !tbaa !11
  %15 = getelementptr inbounds %class.SharedMemoryBuffer, ptr %14, i32 0, i32 5
  call void @_ZN9SignallerC2ER6Signalb(ptr noundef nonnull align 8 dereferenceable(9) %8, ptr noundef nonnull align 8 dereferenceable(136) %15, i1 noundef zeroext true)
  %16 = load i32, ptr %7, align 4, !tbaa !9
  %17 = getelementptr inbounds %"class.SharedMemoryBuffer::FirstStream", ptr %12, i32 0, i32 1
  %18 = load ptr, ptr %17, align 8, !tbaa !11
  %19 = getelementptr inbounds %class.SharedMemoryBuffer, ptr %18, i32 0, i32 3
  %20 = load i32, ptr %19, align 4, !tbaa !14
  %21 = icmp sgt i32 %16, %20
  br i1 %21, label %22, label %67

22:                                               ; preds = %3
  %23 = getelementptr inbounds %"class.SharedMemoryBuffer::FirstStream", ptr %12, i32 0, i32 1
  %24 = load ptr, ptr %23, align 8, !tbaa !11
  %25 = getelementptr inbounds %class.SharedMemoryBuffer, ptr %24, i32 0, i32 1
  %26 = load ptr, ptr %25, align 8, !tbaa !20
  %27 = icmp ne ptr %26, null
  br i1 %27, label %28, label %36

28:                                               ; preds = %22
  %29 = getelementptr inbounds %"class.SharedMemoryBuffer::FirstStream", ptr %12, i32 0, i32 1
  %30 = load ptr, ptr %29, align 8, !tbaa !11
  %31 = getelementptr inbounds %class.SharedMemoryBuffer, ptr %30, i32 0, i32 1
  %32 = load ptr, ptr %31, align 8, !tbaa !20
  call void @free(ptr noundef %32) #6
  %33 = getelementptr inbounds %"class.SharedMemoryBuffer::FirstStream", ptr %12, i32 0, i32 1
  %34 = load ptr, ptr %33, align 8, !tbaa !11
  %35 = getelementptr inbounds %class.SharedMemoryBuffer, ptr %34, i32 0, i32 1
  store ptr null, ptr %35, align 8, !tbaa !20
  br label %36

36:                                               ; preds = %28, %22
  %37 = getelementptr inbounds %"class.SharedMemoryBuffer::FirstStream", ptr %12, i32 0, i32 1
  %38 = load ptr, ptr %37, align 8, !tbaa !11
  %39 = getelementptr inbounds %class.SharedMemoryBuffer, ptr %38, i32 0, i32 3
  store i32 0, ptr %39, align 4, !tbaa !14
  %40 = load i32, ptr %7, align 4, !tbaa !9
  %41 = sext i32 %40 to i64
  %42 = invoke noundef ptr @_Z12AllocPointerIhEPT_m(i64 noundef %41)
          to label %43 unwind label %58

43:                                               ; preds = %36
  %44 = getelementptr inbounds %"class.SharedMemoryBuffer::FirstStream", ptr %12, i32 0, i32 1
  %45 = load ptr, ptr %44, align 8, !tbaa !11
  %46 = getelementptr inbounds %class.SharedMemoryBuffer, ptr %45, i32 0, i32 1
  store ptr %42, ptr %46, align 8, !tbaa !20
  %47 = getelementptr inbounds %"class.SharedMemoryBuffer::FirstStream", ptr %12, i32 0, i32 1
  %48 = load ptr, ptr %47, align 8, !tbaa !11
  %49 = getelementptr inbounds %class.SharedMemoryBuffer, ptr %48, i32 0, i32 1
  %50 = load ptr, ptr %49, align 8, !tbaa !20
  %51 = icmp ne ptr %50, null
  br i1 %51, label %62, label %52

52:                                               ; preds = %43
  %53 = invoke i32 (ptr, ...) @printf(ptr noundef @.str.14)
          to label %54 unwind label %58

54:                                               ; preds = %52
  %55 = load ptr, ptr @stdout, align 8, !tbaa !5
  %56 = invoke i32 @fflush(ptr noundef %55)
          to label %57 unwind label %58

57:                                               ; preds = %54
  store i1 false, ptr %4, align 1
  store i32 1, ptr %11, align 4
  br label %75

58:                                               ; preds = %54, %52, %36
  %59 = landingpad { ptr, i32 }
          cleanup
  %60 = extractvalue { ptr, i32 } %59, 0
  store ptr %60, ptr %9, align 8
  %61 = extractvalue { ptr, i32 } %59, 1
  store i32 %61, ptr %10, align 4
  call void @_ZN9SignallerD2Ev(ptr noundef nonnull align 8 dereferenceable(9) %8) #6
  call void @llvm.lifetime.end.p0(i64 16, ptr %8) #6
  br label %77

62:                                               ; preds = %43
  %63 = load i32, ptr %7, align 4, !tbaa !9
  %64 = getelementptr inbounds %"class.SharedMemoryBuffer::FirstStream", ptr %12, i32 0, i32 1
  %65 = load ptr, ptr %64, align 8, !tbaa !11
  %66 = getelementptr inbounds %class.SharedMemoryBuffer, ptr %65, i32 0, i32 3
  store i32 %63, ptr %66, align 4, !tbaa !14
  br label %67

67:                                               ; preds = %62, %3
  %68 = getelementptr inbounds %"class.SharedMemoryBuffer::FirstStream", ptr %12, i32 0, i32 1
  %69 = load ptr, ptr %68, align 8, !tbaa !11
  %70 = getelementptr inbounds %class.SharedMemoryBuffer, ptr %69, i32 0, i32 1
  %71 = load ptr, ptr %70, align 8, !tbaa !20
  %72 = load ptr, ptr %6, align 8, !tbaa !5
  %73 = load i32, ptr %7, align 4, !tbaa !9
  %74 = sext i32 %73 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %71, ptr align 1 %72, i64 %74, i1 false)
  store i1 true, ptr %4, align 1
  store i32 1, ptr %11, align 4
  br label %75

75:                                               ; preds = %67, %57
  call void @_ZN9SignallerD2Ev(ptr noundef nonnull align 8 dereferenceable(9) %8) #6
  call void @llvm.lifetime.end.p0(i64 16, ptr %8) #6
  %76 = load i1, ptr %4, align 1
  ret i1 %76

77:                                               ; preds = %58
  %78 = load ptr, ptr %9, align 8
  %79 = load i32, ptr %10, align 4
  %80 = insertvalue { ptr, i32 } poison, ptr %78, 0
  %81 = insertvalue { ptr, i32 } %80, i32 %79, 1
  resume { ptr, i32 } %81
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef ptr @_Z12AllocPointerIhEPT_m(i64 noundef) #5

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 "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 = { mustprogress 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 #4 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { mustprogress 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 #6 = { 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 8}
!12 = !{!"_ZTSN18SharedMemoryBuffer11FirstStreamE", !13, i64 0, !6, i64 8}
!13 = !{!"_ZTS10DataStream"}
!14 = !{!15, !10, i64 20}
!15 = !{!"_ZTS18SharedMemoryBuffer", !6, i64 0, !6, i64 8, !10, i64 16, !10, i64 20, !16, i64 24, !16, i64 160}
!16 = !{!"_ZTS6Signal", !17, i64 0, !18, i64 40, !19, i64 48}
!17 = !{!"_ZTSN5boost5mutexE", !7, i64 0}
!18 = !{!"bool", !7, i64 0}
!19 = !{!"_ZTSN5boost18condition_variableE", !7, i64 0, !7, i64 40}
!20 = !{!15, !6, 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"

%struct.Signaller = type <{ ptr, i8, [7 x i8] }>

@stdout = external local_unnamed_addr global ptr, align 8
@.str.14 = external hidden unnamed_addr constant [9 x i8], align 1

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

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

declare i32 @__gxx_personality_v0(...)

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

; Function Attrs: nofree nounwind
declare noundef i32 @fflush(ptr nocapture noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #2

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN9SignallerC2ER6Signalb(ptr noundef nonnull align 8 dereferenceable(9), ptr noundef nonnull align 8 dereferenceable(136), i1 noundef zeroext) unnamed_addr #3 align 2

; 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) #4

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN9SignallerD2Ev(ptr noundef nonnull align 8 dereferenceable(9)) unnamed_addr #5 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef zeroext i1 @_ZN18SharedMemoryBuffer11FirstStream5writeEPKhi(ptr nocapture noundef nonnull readonly align 8 dereferenceable(16) %0, ptr nocapture noundef readonly %1, i32 noundef %2) unnamed_addr #3 align 2 personality ptr @__gxx_personality_v0 {
  %4 = alloca %struct.Signaller, align 8
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %4) #6
  %5 = getelementptr inbounds i8, ptr %0, i64 8
  %6 = load ptr, ptr %5, align 8, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %6, i64 160
  call void @_ZN9SignallerC2ER6Signalb(ptr noundef nonnull align 8 dereferenceable(9) %4, ptr noundef nonnull align 8 dereferenceable(136) %7, i1 noundef zeroext true)
  %8 = load ptr, ptr %5, align 8, !tbaa !5
  %9 = getelementptr inbounds i8, ptr %8, i64 20
  %10 = load i32, ptr %9, align 4, !tbaa !11
  %11 = icmp slt i32 %10, %2
  %12 = getelementptr inbounds i8, ptr %8, i64 8
  %13 = load ptr, ptr %12, align 8, !tbaa !18
  br i1 %11, label %14, label %._crit_edge

._crit_edge:                                      ; preds = %3
  %.pre9 = sext i32 %2 to i64
  br label %31

14:                                               ; preds = %3
  %.not = icmp eq ptr %13, null
  br i1 %.not, label %18, label %15

15:                                               ; preds = %14
  call void @free(ptr noundef nonnull %13) #6
  %16 = load ptr, ptr %5, align 8, !tbaa !5
  %17 = getelementptr inbounds i8, ptr %16, i64 8
  store ptr null, ptr %17, align 8, !tbaa !18
  br label %18

18:                                               ; preds = %15, %14
  %19 = phi ptr [ %16, %15 ], [ %8, %14 ]
  %20 = getelementptr inbounds i8, ptr %19, i64 20
  store i32 0, ptr %20, align 4, !tbaa !11
  %21 = sext i32 %2 to i64
  %22 = call noundef ptr @_Z12AllocPointerIhEPT_m(i64 noundef %21)
  %23 = load ptr, ptr %5, align 8, !tbaa !5
  %24 = getelementptr inbounds i8, ptr %23, i64 8
  store ptr %22, ptr %24, align 8, !tbaa !18
  %.not8 = icmp eq ptr %22, null
  br i1 %.not8, label %25, label %29

25:                                               ; preds = %18
  %26 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.14)
  %27 = load ptr, ptr @stdout, align 8, !tbaa !19
  %28 = call i32 @fflush(ptr noundef %27)
  br label %33

29:                                               ; preds = %18
  %30 = getelementptr inbounds i8, ptr %23, i64 20
  store i32 %2, ptr %30, align 4, !tbaa !11
  br label %31

31:                                               ; preds = %._crit_edge, %29
  %.pre-phi = phi i64 [ %.pre9, %._crit_edge ], [ %21, %29 ]
  %32 = phi ptr [ %13, %._crit_edge ], [ %22, %29 ]
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %32, ptr align 1 %1, i64 %.pre-phi, i1 false)
  br label %33

33:                                               ; preds = %31, %25
  %.0 = phi i1 [ true, %31 ], [ false, %25 ]
  call void @_ZN9SignallerD2Ev(ptr noundef nonnull align 8 dereferenceable(9) %4) #6
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %4) #6
  ret i1 %.0
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef ptr @_Z12AllocPointerIhEPT_m(i64 noundef) local_unnamed_addr #5

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nofree 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 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { mustprogress 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 #4 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { mustprogress 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 #6 = { 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, !8, i64 8}
!6 = !{!"_ZTSN18SharedMemoryBuffer11FirstStreamE", !7, i64 0, !8, i64 8}
!7 = !{!"_ZTS10DataStream"}
!8 = !{!"any pointer", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C++ TBAA"}
!11 = !{!12, !13, i64 20}
!12 = !{!"_ZTS18SharedMemoryBuffer", !8, i64 0, !8, i64 8, !13, i64 16, !13, i64 20, !14, i64 24, !14, i64 160}
!13 = !{!"int", !9, i64 0}
!14 = !{!"_ZTS6Signal", !15, i64 0, !16, i64 40, !17, i64 48}
!15 = !{!"_ZTSN5boost5mutexE", !9, i64 0}
!16 = !{!"bool", !9, i64 0}
!17 = !{!"_ZTSN5boost18condition_variableE", !9, i64 0, !9, i64 40}
!18 = !{!12, !8, i64 8}
!19 = !{!8, !8, i64 0}


; Effective O3 passes: BDCEPass,CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass