; LLVM IR from mitdbg/deneva :: system/lock_free_queue.ll (ZN13LockfreeQueue7dequeueERm)

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

%class.LockfreeQueue = type { ptr, ptr }
%struct.QueueEntry = type { i64, ptr }

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

declare i32 @usleep(i32 noundef) #1

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

; Function Attrs: mustprogress uwtable
define dso_local noundef zeroext i1 @_ZN13LockfreeQueue7dequeueERm(ptr noundef nonnull align 8 dereferenceable(16) %0, ptr noundef nonnull align 8 dereferenceable(8) %1) #2 align 2 {
  %3 = alloca i1, align 1
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i8, align 1
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  %10 = load ptr, ptr %4, align 8
  call void @llvm.lifetime.start.p0(i64 1, ptr %6) #4
  store i8 0, ptr %6, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  br label %11

11:                                               ; preds = %66, %2
  %12 = load i8, ptr %6, align 1, !tbaa !9, !range !11, !noundef !12
  %13 = trunc i8 %12 to i1
  %14 = xor i1 %13, true
  br i1 %14, label %15, label %67

15:                                               ; preds = %11
  %16 = getelementptr inbounds %class.LockfreeQueue, ptr %10, i32 0, i32 1
  %17 = load volatile ptr, ptr %16, align 8, !tbaa !13
  store ptr %17, ptr %7, align 8, !tbaa !5
  %18 = getelementptr inbounds %class.LockfreeQueue, ptr %10, i32 0, i32 0
  %19 = load volatile ptr, ptr %18, align 8, !tbaa !15
  store ptr %19, ptr %8, align 8, !tbaa !5
  %20 = load ptr, ptr %7, align 8, !tbaa !5
  %21 = icmp eq ptr %20, null
  br i1 %21, label %22, label %24

22:                                               ; preds = %15
  %23 = call i32 @usleep(i32 noundef 1)
  store i1 false, ptr %3, align 1
  store i32 1, ptr %9, align 4
  br label %73

24:                                               ; preds = %15
  %25 = load ptr, ptr %7, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.QueueEntry, ptr %25, i32 0, i32 1
  %27 = load ptr, ptr %26, align 8, !tbaa !16
  %28 = icmp ne ptr %27, null
  br i1 %28, label %29, label %40

29:                                               ; preds = %24
  %30 = getelementptr inbounds %class.LockfreeQueue, ptr %10, i32 0, i32 1
  %31 = load ptr, ptr %7, align 8, !tbaa !5
  %32 = ptrtoint ptr %31 to i64
  %33 = load ptr, ptr %7, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.QueueEntry, ptr %33, i32 0, i32 1
  %35 = load ptr, ptr %34, align 8, !tbaa !16
  %36 = ptrtoint ptr %35 to i64
  %37 = cmpxchg ptr %30, i64 %32, i64 %36 seq_cst seq_cst, align 8
  %38 = extractvalue { i64, i1 } %37, 1
  %39 = zext i1 %38 to i8
  store i8 %39, ptr %6, align 1, !tbaa !9
  br label %61

40:                                               ; preds = %24
  %41 = load ptr, ptr %7, align 8, !tbaa !5
  %42 = load ptr, ptr %8, align 8, !tbaa !5
  %43 = icmp eq ptr %41, %42
  br i1 %43, label %44, label %60

44:                                               ; preds = %40
  %45 = getelementptr inbounds %class.LockfreeQueue, ptr %10, i32 0, i32 0
  %46 = load ptr, ptr %7, align 8, !tbaa !5
  %47 = ptrtoint ptr %46 to i64
  %48 = cmpxchg ptr %45, i64 %47, i64 0 seq_cst seq_cst, align 8
  %49 = extractvalue { i64, i1 } %48, 1
  %50 = zext i1 %49 to i8
  store i8 %50, ptr %6, align 1, !tbaa !9
  %51 = load i8, ptr %6, align 1, !tbaa !9, !range !11, !noundef !12
  %52 = trunc i8 %51 to i1
  br i1 %52, label %53, label %59

53:                                               ; preds = %44
  %54 = getelementptr inbounds %class.LockfreeQueue, ptr %10, i32 0, i32 1
  %55 = load ptr, ptr %7, align 8, !tbaa !5
  %56 = ptrtoint ptr %55 to i64
  %57 = cmpxchg ptr %54, i64 %56, i64 0 seq_cst seq_cst, align 8
  %58 = extractvalue { i64, i1 } %57, 1
  br label %59

59:                                               ; preds = %53, %44
  br label %60

60:                                               ; preds = %59, %40
  br label %61

61:                                               ; preds = %60, %29
  %62 = load i8, ptr %6, align 1, !tbaa !9, !range !11, !noundef !12
  %63 = trunc i8 %62 to i1
  br i1 %63, label %66, label %64

64:                                               ; preds = %61
  %65 = call i32 @usleep(i32 noundef 1)
  br label %66

66:                                               ; preds = %64, %61
  br label %11, !llvm.loop !19

67:                                               ; preds = %11
  %68 = load ptr, ptr %7, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.QueueEntry, ptr %68, i32 0, i32 0
  %70 = load volatile i64, ptr %69, align 8, !tbaa !21
  %71 = load ptr, ptr %5, align 8, !tbaa !5
  store i64 %70, ptr %71, align 8, !tbaa !22
  %72 = load ptr, ptr %7, align 8, !tbaa !5
  call void @free(ptr noundef %72) #4
  store i1 true, ptr %3, align 1
  store i32 1, ptr %9, align 4
  br label %73

73:                                               ; preds = %67, %22
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 1, ptr %6) #4
  %74 = load i1, ptr %3, align 1
  ret i1 %74
}

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

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 = { 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 #3 = { 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 #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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"bool", !7, i64 0}
!11 = !{i8 0, i8 2}
!12 = !{}
!13 = !{!14, !6, i64 8}
!14 = !{!"_ZTS13LockfreeQueue", !6, i64 0, !6, i64 8}
!15 = !{!14, !6, i64 0}
!16 = !{!17, !6, i64 8}
!17 = !{!"_ZTS10QueueEntry", !18, i64 0, !6, i64 8}
!18 = !{!"long", !7, i64 0}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}
!21 = !{!17, !18, i64 0}
!22 = !{!18, !18, i64 0}


; 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 i32 @usleep(i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local noundef zeroext i1 @_ZN13LockfreeQueue7dequeueERm(ptr noundef nonnull align 8 dereferenceable(16) %0, ptr nocapture noundef nonnull writeonly align 8 dereferenceable(8) %1) local_unnamed_addr #1 align 2 {
  %3 = getelementptr inbounds i8, ptr %0, i64 8
  br label %4

4:                                                ; preds = %2, %29
  %.01626 = phi i8 [ 0, %2 ], [ %.120, %29 ]
  %5 = load volatile ptr, ptr %3, align 8, !tbaa !5
  %6 = load volatile ptr, ptr %0, align 8, !tbaa !10
  %7 = icmp ne ptr %5, null
  br i1 %7, label %10, label %8

8:                                                ; preds = %4
  %9 = tail call i32 @usleep(i32 noundef 1)
  br label %32

10:                                               ; preds = %4
  %11 = getelementptr inbounds i8, ptr %5, i64 8
  %12 = load ptr, ptr %11, align 8, !tbaa !11
  %.not = icmp eq ptr %12, null
  br i1 %.not, label %19, label %13

13:                                               ; preds = %10
  %14 = ptrtoint ptr %5 to i64
  %15 = ptrtoint ptr %12 to i64
  %16 = cmpxchg ptr %3, i64 %14, i64 %15 seq_cst seq_cst, align 8
  %17 = extractvalue { i64, i1 } %16, 1
  %18 = zext i1 %17 to i8
  br label %26

19:                                               ; preds = %10
  %20 = icmp eq ptr %5, %6
  br i1 %20, label %21, label %26

21:                                               ; preds = %19
  %22 = ptrtoint ptr %5 to i64
  %23 = cmpxchg ptr %0, i64 %22, i64 0 seq_cst seq_cst, align 8
  %24 = extractvalue { i64, i1 } %23, 1
  br i1 %24, label %.thread31, label %.thread21

.thread31:                                        ; preds = %21
  %25 = cmpxchg ptr %3, i64 %22, i64 0 seq_cst seq_cst, align 8
  br label %.loopexit

26:                                               ; preds = %19, %13
  %.1 = phi i8 [ %18, %13 ], [ %.01626, %19 ]
  %27 = trunc nuw i8 %.1 to i1
  br i1 %27, label %29, label %.thread21

.thread21:                                        ; preds = %21, %26
  %.123 = phi i8 [ %.1, %26 ], [ 0, %21 ]
  %28 = tail call i32 @usleep(i32 noundef 1)
  br label %29

29:                                               ; preds = %.thread21, %26
  %.120 = phi i8 [ %.123, %.thread21 ], [ %.1, %26 ]
  %30 = trunc nuw i8 %.120 to i1
  br i1 %30, label %.loopexit, label %4, !llvm.loop !14

.loopexit:                                        ; preds = %29, %.thread31
  %31 = load volatile i64, ptr %5, align 8, !tbaa !16
  store i64 %31, ptr %1, align 8, !tbaa !17
  tail call void @free(ptr noundef nonnull %5) #3
  br label %32

32:                                               ; preds = %.loopexit, %8
  ret i1 %7
}

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #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 = { 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 #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 = { 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, !7, i64 8}
!6 = !{!"_ZTS13LockfreeQueue", !7, i64 0, !7, i64 8}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!6, !7, i64 0}
!11 = !{!12, !7, i64 8}
!12 = !{!"_ZTS10QueueEntry", !13, i64 0, !7, i64 8}
!13 = !{!"long", !8, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!12, !13, i64 0}
!17 = !{!13, !13, i64 0}


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