; 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 @chanrecv(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 @canrecv(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 8
  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 = call i32 @canrecv(ptr noundef %24)
  %26 = icmp ne i32 %25, 0
  br i1 %26, label %27, label %34

27:                                               ; preds = %23
  %28 = load ptr, ptr %4, align 8, !tbaa !5
  %29 = load ptr, ptr %5, align 8, !tbaa !5
  call void @chanrecv_(ptr noundef %28, ptr noundef %29)
  %30 = load ptr, ptr %4, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.Channel, ptr %30, i32 0, i32 7
  call void @rwakeup(ptr noundef %31)
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.Channel, ptr %32, i32 0, i32 6
  call void @unlock(ptr noundef %33)
  store i32 1, ptr %3, align 4
  br label %37

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

37:                                               ; preds = %34, %27
  %38 = load i32, ptr %3, align 4
  ret i32 %38
}

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

; Function Attrs: nounwind uwtable
declare hidden void @chanrecv_(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 @chanrecv(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
  %.not12 = icmp eq i32 %5, 0
  br i1 %.not12, label %.lr.ph, label %.critedge

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

7:                                                ; preds = %.lr.ph, %9
  %8 = tail call i32 @canrecv(ptr noundef nonnull %0)
  %.not11 = icmp eq i32 %8, 0
  br i1 %.not11, 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, !llvm.loop !13

.critedge:                                        ; preds = %7, %9, %2
  %11 = tail call i32 @canrecv(ptr noundef nonnull %0)
  %.not10 = icmp eq i32 %11, 0
  br i1 %.not10, label %14, label %12

12:                                               ; preds = %.critedge
  tail call void @chanrecv_(ptr noundef nonnull %0, ptr noundef %1)
  %13 = getelementptr inbounds i8, ptr %0, i64 80
  tail call void @rwakeup(ptr noundef nonnull %13)
  br label %14

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

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

; Function Attrs: nounwind uwtable
declare hidden void @chanrecv_(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,GlobalOptPass,IPSCCPPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass