; LLVM IR from aap/pdp6 :: emu (threading)

; 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.Channel = type { i32, i32, ptr, i32, i32, i32, %struct.Lock, %struct.Rendez, %struct.Rendez }
%struct.Lock = type { i32, %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 }
%struct.Rendez = type { i32, ptr, %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 }

; Function Attrs: nounwind uwtable
declare dso_local void @lock(ptr noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local void @unlock(ptr noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local void @rsleep(ptr noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local void @rwakeup(ptr noundef) #0

; Function Attrs: nounwind uwtable
define dso_local i32 @chansend(ptr noundef %0, ptr noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  %6 = load ptr, ptr %4, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct.Channel, ptr %6, i32 0, i32 6
  call void @lock(ptr noundef %7)
  br label %8

8:                                                ; preds = %20, %2
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.Channel, ptr %9, i32 0, i32 5
  %11 = load i32, ptr %10, align 8, !tbaa !9
  %12 = icmp ne i32 %11, 0
  br i1 %12, label %17, label %13

13:                                               ; preds = %8
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  %15 = call i32 @cansend(ptr noundef %14)
  %16 = icmp ne i32 %15, 0
  br label %17

17:                                               ; preds = %13, %8
  %18 = phi i1 [ true, %8 ], [ %16, %13 ]
  %19 = xor i1 %18, true
  br i1 %19, label %20, label %23

20:                                               ; preds = %17
  %21 = load ptr, ptr %4, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.Channel, ptr %21, i32 0, i32 7
  call void @rsleep(ptr noundef %22)
  br label %8, !llvm.loop !14

23:                                               ; preds = %17
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.Channel, ptr %24, i32 0, i32 5
  %26 = load i32, ptr %25, align 8, !tbaa !9
  %27 = icmp ne i32 %26, 0
  br i1 %27, label %28, label %31

28:                                               ; preds = %23
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.Channel, ptr %29, i32 0, i32 6
  call void @unlock(ptr noundef %30)
  store i32 -1, ptr %3, align 4
  br label %38

31:                                               ; preds = %23
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = load ptr, ptr %5, align 8, !tbaa !5
  call void @chansend_(ptr noundef %32, ptr noundef %33)
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.Channel, ptr %34, i32 0, i32 8
  call void @rwakeup(ptr noundef %35)
  %36 = load ptr, ptr %4, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.Channel, ptr %36, i32 0, i32 6
  call void @unlock(ptr noundef %37)
  store i32 1, ptr %3, align 4
  br label %38

38:                                               ; preds = %31, %28
  %39 = load i32, ptr %3, align 4
  ret i32 %39
}

; Function Attrs: nounwind uwtable
declare hidden i32 @cansend(ptr noundef) #0

; Function Attrs: nounwind uwtable
declare hidden void @chansend_(ptr noundef, ptr noundef) #0

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

!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, !11, i64 24}
!10 = !{!"Channel", !11, i64 0, !11, i64 4, !6, i64 8, !11, i64 16, !11, i64 20, !11, i64 24, !12, i64 32, !13, i64 80, !13, i64 144}
!11 = !{!"int", !7, i64 0}
!12 = !{!"Lock", !11, i64 0, !7, i64 8}
!13 = !{!"Rendez", !11, i64 0, !6, i64 8, !7, i64 16}
!14 = distinct !{!14, !15}
!15 = !{!"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"

; Function Attrs: nounwind uwtable
declare dso_local void @lock(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @unlock(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @rsleep(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @rwakeup(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 2) i32 @chansend(ptr noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 32
  tail call void @lock(ptr noundef nonnull %3)
  %4 = getelementptr inbounds i8, ptr %0, i64 24
  %5 = load i32, ptr %4, align 8, !tbaa !5
  %.not14 = icmp eq i32 %5, 0
  br i1 %.not14, label %.lr.ph, label %.critedge.thread

.lr.ph:                                           ; preds = %2
  %6 = getelementptr inbounds i8, ptr %0, i64 80
  br label %7

7:                                                ; preds = %.lr.ph, %9
  %8 = tail call i32 @cansend(ptr noundef nonnull %0)
  %.not12 = icmp eq i32 %8, 0
  br i1 %.not12, label %9, label %.critedge

9:                                                ; preds = %7
  tail call void @rsleep(ptr noundef nonnull %6)
  %10 = load i32, ptr %4, align 8, !tbaa !5
  %.not = icmp eq i32 %10, 0
  br i1 %.not, label %7, label %.critedge.thread, !llvm.loop !13

.critedge:                                        ; preds = %7
  %.pr = load i32, ptr %4, align 8, !tbaa !5
  %.not10 = icmp eq i32 %.pr, 0
  br i1 %.not10, label %11, label %.critedge.thread

11:                                               ; preds = %.critedge
  tail call void @chansend_(ptr noundef nonnull %0, ptr noundef %1)
  %12 = getelementptr inbounds i8, ptr %0, i64 144
  tail call void @rwakeup(ptr noundef nonnull %12)
  br label %.critedge.thread

.critedge.thread:                                 ; preds = %9, %.critedge, %2, %11
  %.0 = phi i32 [ 1, %11 ], [ -1, %2 ], [ -1, %.critedge ], [ -1, %9 ]
  tail call void @unlock(ptr noundef nonnull %3)
  ret i32 %.0
}

; Function Attrs: nounwind uwtable
declare hidden i32 @cansend(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden void @chansend_(ptr noundef, ptr noundef) local_unnamed_addr #0

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

!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 24}
!6 = !{!"Channel", !7, i64 0, !7, i64 4, !10, i64 8, !7, i64 16, !7, i64 20, !7, i64 24, !11, i64 32, !12, i64 80, !12, i64 144}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"Lock", !7, i64 0, !8, i64 8}
!12 = !{!"Rendez", !7, i64 0, !10, i64 8, !8, i64 16}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


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