; LLVM IR from microsoft/LightAIMD :: src (spinlock)

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

; Function Attrs: nounwind uwtable
define dso_local void @spinlock_lock(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca i8, align 1
  %4 = alloca i8, align 1
  %5 = alloca i8, align 1
  store ptr %0, ptr %2, align 8, !tbaa !5
  br label %6

6:                                                ; preds = %20, %1
  %7 = load ptr, ptr %2, align 8, !tbaa !5
  store i8 1, ptr %3, align 1, !tbaa !9
  %8 = load i8, ptr %3, align 1
  %9 = atomicrmw volatile xchg ptr %7, i8 %8 acquire, align 1
  store i8 %9, ptr %4, align 1
  %10 = load i8, ptr %4, align 1, !tbaa !9, !range !11, !noundef !12
  %11 = trunc i8 %10 to i1
  br i1 %11, label %13, label %12

12:                                               ; preds = %6
  ret void

13:                                               ; preds = %6
  br label %14

14:                                               ; preds = %19, %13
  %15 = load ptr, ptr %2, align 8, !tbaa !5
  %16 = load atomic volatile i8, ptr %15 monotonic, align 1
  store i8 %16, ptr %5, align 1
  %17 = load i8, ptr %5, align 1, !tbaa !9, !range !11, !noundef !12
  %18 = trunc i8 %17 to i1
  br i1 %18, label %19, label %20

19:                                               ; preds = %14
  call void @llvm.x86.sse2.pause()
  br label %14, !llvm.loop !13

20:                                               ; preds = %14
  br label %6
}

; Function Attrs: nounwind
declare void @llvm.x86.sse2.pause() #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 = { 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, !10, i64 0}
!10 = !{!"_Bool", !7, i64 0}
!11 = !{i8 0, i8 2}
!12 = !{}
!13 = distinct !{!13, !14}
!14 = !{!"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
define dso_local void @spinlock_lock(ptr noundef %0) local_unnamed_addr #0 {
  %2 = atomicrmw volatile xchg ptr %0, i8 1 acquire, align 1
  %3 = trunc i8 %2 to i1
  br i1 %3, label %.preheader, label %._crit_edge

.loopexit:                                        ; preds = %.lr.ph, %.preheader
  %4 = atomicrmw volatile xchg ptr %0, i8 1 acquire, align 1
  %5 = trunc i8 %4 to i1
  br i1 %5, label %.preheader, label %._crit_edge

.preheader:                                       ; preds = %1, %.loopexit
  %6 = load atomic volatile i8, ptr %0 monotonic, align 1
  %7 = trunc i8 %6 to i1
  br i1 %7, label %.lr.ph, label %.loopexit

._crit_edge:                                      ; preds = %.loopexit, %1
  ret void

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  tail call void @llvm.x86.sse2.pause()
  %8 = load atomic volatile i8, ptr %0 monotonic, align 1
  %9 = trunc i8 %8 to i1
  br i1 %9, label %.lr.ph, label %.loopexit, !llvm.loop !5
}

; Function Attrs: nounwind
declare void @llvm.x86.sse2.pause() #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 = { 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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: GlobalOptPass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass