; LLVM IR from ohler55/agoo :: ext/agoo (queue)

; 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._agooQueue = type { ptr, ptr, %struct._agooAtom, %struct._agooAtom, i8, i8, %struct._agooAtom, %struct._agooAtom, %struct._agooAtom, i32, i32 }
%struct._agooAtom = type { ptr, %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 }

@.str.2 = external hidden unnamed_addr constant [2 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local void @agoo_queue_push(ptr noundef %0, ptr noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  %6 = load ptr, ptr %3, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct._agooQueue, ptr %6, i32 0, i32 4
  %8 = load i8, ptr %7, align 8, !tbaa !9, !range !14, !noundef !15
  %9 = trunc i8 %8 to i1
  br i1 %9, label %10, label %18

10:                                               ; preds = %2
  br label %11

11:                                               ; preds = %15, %10
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct._agooQueue, ptr %12, i32 0, i32 6
  %14 = call zeroext i1 @atomic_flag_test_and_set(ptr noundef %13)
  br i1 %14, label %15, label %17

15:                                               ; preds = %11
  %16 = call double @dsleep(double noundef 1.000000e-04)
  br label %11, !llvm.loop !16

17:                                               ; preds = %11
  br label %18

18:                                               ; preds = %17, %2
  br label %19

19:                                               ; preds = %27, %18
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct._agooQueue, ptr %20, i32 0, i32 2
  %22 = call ptr @atomic_load(ptr noundef %21)
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct._agooQueue, ptr %23, i32 0, i32 3
  %25 = call ptr @atomic_load(ptr noundef %24)
  %26 = icmp eq ptr %22, %25
  br i1 %26, label %27, label %29

27:                                               ; preds = %19
  %28 = call double @dsleep(double noundef 1.000000e-04)
  br label %19, !llvm.loop !18

29:                                               ; preds = %19
  %30 = load ptr, ptr %4, align 8, !tbaa !5
  %31 = load ptr, ptr %3, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct._agooQueue, ptr %31, i32 0, i32 3
  %33 = call ptr @atomic_load(ptr noundef %32)
  store ptr %30, ptr %33, align 8, !tbaa !5
  %34 = load ptr, ptr %3, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct._agooQueue, ptr %34, i32 0, i32 3
  %36 = call ptr @atomic_load(ptr noundef %35)
  %37 = getelementptr inbounds ptr, ptr %36, i64 1
  store ptr %37, ptr %5, align 8, !tbaa !5
  %38 = load ptr, ptr %3, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct._agooQueue, ptr %38, i32 0, i32 1
  %40 = load ptr, ptr %39, align 8, !tbaa !19
  %41 = load ptr, ptr %5, align 8, !tbaa !5
  %42 = icmp ule ptr %40, %41
  br i1 %42, label %43, label %47

43:                                               ; preds = %29
  %44 = load ptr, ptr %3, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct._agooQueue, ptr %44, i32 0, i32 0
  %46 = load ptr, ptr %45, align 8, !tbaa !20
  store ptr %46, ptr %5, align 8, !tbaa !5
  br label %47

47:                                               ; preds = %43, %29
  %48 = load ptr, ptr %3, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct._agooQueue, ptr %48, i32 0, i32 3
  %50 = load ptr, ptr %5, align 8, !tbaa !5
  call void @_atomic_store(ptr noundef %49, ptr noundef %50)
  %51 = load ptr, ptr %3, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct._agooQueue, ptr %51, i32 0, i32 4
  %53 = load i8, ptr %52, align 8, !tbaa !9, !range !14, !noundef !15
  %54 = trunc i8 %53 to i1
  br i1 %54, label %55, label %58

55:                                               ; preds = %47
  %56 = load ptr, ptr %3, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct._agooQueue, ptr %56, i32 0, i32 6
  call void @atomic_flag_clear(ptr noundef %57)
  br label %58

58:                                               ; preds = %55, %47
  %59 = load ptr, ptr %3, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct._agooQueue, ptr %59, i32 0, i32 10
  %61 = load i32, ptr %60, align 4, !tbaa !21
  %62 = icmp ne i32 0, %61
  br i1 %62, label %63, label %79

63:                                               ; preds = %58
  %64 = load ptr, ptr %3, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct._agooQueue, ptr %64, i32 0, i32 8
  %66 = call ptr @atomic_load(ptr noundef %65)
  %67 = ptrtoint ptr %66 to i64
  %68 = icmp eq i64 1, %67
  br i1 %68, label %69, label %79

69:                                               ; preds = %63
  %70 = load ptr, ptr %3, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct._agooQueue, ptr %70, i32 0, i32 10
  %72 = load i32, ptr %71, align 4, !tbaa !21
  %73 = call i64 @write(i32 noundef %72, ptr noundef @.str.2, i64 noundef 1)
  %74 = icmp ne i64 %73, 0
  br i1 %74, label %75, label %76

75:                                               ; preds = %69
  br label %76

76:                                               ; preds = %75, %69
  %77 = load ptr, ptr %3, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct._agooQueue, ptr %77, i32 0, i32 8
  call void @_atomic_store(ptr noundef %78, ptr noundef inttoptr (i64 2 to ptr))
  br label %79

79:                                               ; preds = %76, %63, %58
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  ret void
}

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden zeroext i1 @atomic_flag_test_and_set(ptr noundef) #2

declare double @dsleep(double noundef) #3

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @atomic_load(ptr noundef) #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @_atomic_store(ptr noundef, ptr noundef) #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @atomic_flag_clear(ptr noundef) #2

declare i64 @write(i32 noundef, ptr noundef, i64 noundef) #3

; 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 = { inlinehint 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 = { "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 = { 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, !12, i64 112}
!10 = !{!"_agooQueue", !6, i64 0, !6, i64 8, !11, i64 16, !11, i64 64, !12, i64 112, !12, i64 113, !11, i64 120, !11, i64 168, !11, i64 216, !13, i64 264, !13, i64 268}
!11 = !{!"_agooAtom", !6, i64 0, !7, i64 8}
!12 = !{!"_Bool", !7, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = !{i8 0, i8 2}
!15 = !{}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = distinct !{!18, !17}
!19 = !{!10, !6, i64 8}
!20 = !{!10, !6, i64 0}
!21 = !{!10, !13, i64 268}


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

@.str.2 = external hidden unnamed_addr constant [2 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local void @agoo_queue_push(ptr noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 112
  %4 = load i8, ptr %3, align 8, !tbaa !5, !range !13, !noundef !14
  %5 = trunc nuw i8 %4 to i1
  br i1 %5, label %.preheader, label %.loopexit

.preheader:                                       ; preds = %2
  %6 = getelementptr inbounds i8, ptr %0, i64 120
  %7 = tail call zeroext i1 @atomic_flag_test_and_set(ptr noundef nonnull %6)
  br i1 %7, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %8 = tail call double @dsleep(double noundef 1.000000e-04) #4
  %9 = tail call zeroext i1 @atomic_flag_test_and_set(ptr noundef nonnull %6)
  br i1 %9, label %.lr.ph, label %.loopexit, !llvm.loop !15

.loopexit:                                        ; preds = %.lr.ph, %.preheader, %2
  %10 = getelementptr inbounds i8, ptr %0, i64 16
  %11 = tail call ptr @atomic_load(ptr noundef nonnull %10)
  %12 = getelementptr inbounds i8, ptr %0, i64 64
  %13 = tail call ptr @atomic_load(ptr noundef nonnull %12)
  %14 = icmp eq ptr %11, %13
  br i1 %14, label %.lr.ph20, label %._crit_edge

.lr.ph20:                                         ; preds = %.loopexit, %.lr.ph20
  %15 = tail call double @dsleep(double noundef 1.000000e-04) #4
  %16 = tail call ptr @atomic_load(ptr noundef nonnull %10)
  %17 = tail call ptr @atomic_load(ptr noundef nonnull %12)
  %18 = icmp eq ptr %16, %17
  br i1 %18, label %.lr.ph20, label %._crit_edge, !llvm.loop !17

._crit_edge:                                      ; preds = %.lr.ph20, %.loopexit
  %19 = tail call ptr @atomic_load(ptr noundef nonnull %12)
  store ptr %1, ptr %19, align 8, !tbaa !18
  %20 = tail call ptr @atomic_load(ptr noundef nonnull %12)
  %21 = getelementptr inbounds i8, ptr %20, i64 8
  %22 = getelementptr inbounds i8, ptr %0, i64 8
  %23 = load ptr, ptr %22, align 8, !tbaa !19
  %.not = icmp ugt ptr %23, %21
  br i1 %.not, label %26, label %24

24:                                               ; preds = %._crit_edge
  %25 = load ptr, ptr %0, align 8, !tbaa !20
  br label %26

26:                                               ; preds = %24, %._crit_edge
  %.0 = phi ptr [ %25, %24 ], [ %21, %._crit_edge ]
  tail call void @_atomic_store(ptr noundef nonnull %12, ptr noundef %.0)
  %27 = load i8, ptr %3, align 8, !tbaa !5, !range !13, !noundef !14
  %28 = trunc nuw i8 %27 to i1
  br i1 %28, label %29, label %31

29:                                               ; preds = %26
  %30 = getelementptr inbounds i8, ptr %0, i64 120
  tail call void @atomic_flag_clear(ptr noundef nonnull %30)
  br label %31

31:                                               ; preds = %29, %26
  %32 = getelementptr inbounds i8, ptr %0, i64 268
  %33 = load i32, ptr %32, align 4, !tbaa !21
  %.not19 = icmp eq i32 %33, 0
  br i1 %.not19, label %41, label %34

34:                                               ; preds = %31
  %35 = getelementptr inbounds i8, ptr %0, i64 216
  %36 = tail call ptr @atomic_load(ptr noundef nonnull %35)
  %37 = icmp eq ptr %36, inttoptr (i64 1 to ptr)
  br i1 %37, label %38, label %41

38:                                               ; preds = %34
  %39 = load i32, ptr %32, align 4, !tbaa !21
  %40 = tail call i64 @write(i32 noundef %39, ptr noundef nonnull @.str.2, i64 noundef 1) #4
  tail call void @_atomic_store(ptr noundef nonnull %35, ptr noundef nonnull inttoptr (i64 2 to ptr))
  br label %41

41:                                               ; preds = %38, %34, %31
  ret void
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden zeroext i1 @atomic_flag_test_and_set(ptr noundef) local_unnamed_addr #1

declare double @dsleep(double noundef) local_unnamed_addr #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @atomic_load(ptr noundef) local_unnamed_addr #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @_atomic_store(ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @atomic_flag_clear(ptr noundef) local_unnamed_addr #1

; Function Attrs: nofree
declare noundef i64 @write(i32 noundef, ptr nocapture noundef readonly, i64 noundef) local_unnamed_addr #3

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 = { inlinehint 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 = { "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 = { nofree "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 = { 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, !11, i64 112}
!6 = !{!"_agooQueue", !7, i64 0, !7, i64 8, !10, i64 16, !10, i64 64, !11, i64 112, !11, i64 113, !10, i64 120, !10, i64 168, !10, i64 216, !12, i64 264, !12, i64 268}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"_agooAtom", !7, i64 0, !8, i64 8}
!11 = !{!"_Bool", !8, i64 0}
!12 = !{!"int", !8, i64 0}
!13 = !{i8 0, i8 2}
!14 = !{}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = distinct !{!17, !16}
!18 = !{!7, !7, i64 0}
!19 = !{!6, !7, i64 8}
!20 = !{!6, !7, i64 0}
!21 = !{!6, !12, i64 268}


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