; LLVM IR from ros2/rcutils :: src/testing/fault_injection.ll (rcutils_fault_injection_maybe_fail)

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

@g_rcutils_fault_injection_count = external hidden global i64, align 8

; Function Attrs: nounwind uwtable
declare dso_local i64 @rcutils_fault_injection_get_count() #0

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

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

; Function Attrs: nounwind uwtable
define dso_local i64 @_rcutils_fault_injection_maybe_fail() #0 {
  %1 = alloca i64, align 8
  %2 = alloca i8, align 1
  %3 = alloca i64, align 8
  %4 = alloca i32, align 4
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i8, align 1
  call void @llvm.lifetime.start.p0(i64 1, ptr %2) #2
  store i8 0, ptr %2, align 1, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  %8 = call i64 @rcutils_fault_injection_get_count()
  store i64 %8, ptr %3, align 8, !tbaa !9
  br label %9

9:                                                ; preds = %29, %0
  %10 = load i64, ptr %3, align 8, !tbaa !9
  %11 = icmp sle i64 %10, -1
  br i1 %11, label %12, label %14

12:                                               ; preds = %9
  %13 = load i64, ptr %3, align 8, !tbaa !9
  store i64 %13, ptr %1, align 8
  store i32 1, ptr %4, align 4
  br label %35

14:                                               ; preds = %9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %15 = load i64, ptr %3, align 8, !tbaa !9
  %16 = sub nsw i64 %15, 1
  store i64 %16, ptr %5, align 8, !tbaa !9
  %17 = load i64, ptr %5, align 8, !tbaa !9
  store i64 %17, ptr %6, align 8, !tbaa !9
  %18 = load i64, ptr %3, align 8
  %19 = load i64, ptr %6, align 8
  %20 = cmpxchg ptr @g_rcutils_fault_injection_count, i64 %18, i64 %19 seq_cst seq_cst, align 8
  %21 = extractvalue { i64, i1 } %20, 0
  %22 = extractvalue { i64, i1 } %20, 1
  br i1 %22, label %24, label %23

23:                                               ; preds = %14
  store i64 %21, ptr %3, align 8
  br label %24

24:                                               ; preds = %23, %14
  %25 = zext i1 %22 to i8
  store i8 %25, ptr %7, align 1, !tbaa !5
  %26 = load i8, ptr %7, align 1, !tbaa !5, !range !11, !noundef !12
  %27 = trunc i8 %26 to i1
  %28 = zext i1 %27 to i8
  store i8 %28, ptr %2, align 1, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  br label %29

29:                                               ; preds = %24
  %30 = load i8, ptr %2, align 1, !tbaa !5, !range !11, !noundef !12
  %31 = trunc i8 %30 to i1
  %32 = xor i1 %31, true
  br i1 %32, label %9, label %33, !llvm.loop !13

33:                                               ; preds = %29
  %34 = load i64, ptr %3, align 8, !tbaa !9
  store i64 %34, ptr %1, align 8
  store i32 1, ptr %4, align 4
  br label %35

35:                                               ; preds = %33, %12
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %2) #2
  %36 = load i64, ptr %1, align 8
  ret i64 %36
}

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 = { 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 = !{!"_Bool", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !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"

@g_rcutils_fault_injection_count = external hidden global i64, align 8

; Function Attrs: nounwind uwtable
declare dso_local i64 @rcutils_fault_injection_get_count() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i64 @_rcutils_fault_injection_maybe_fail() local_unnamed_addr #0 {
  %1 = tail call i64 @rcutils_fault_injection_get_count()
  br label %2

2:                                                ; preds = %4, %0
  %.010 = phi i64 [ %1, %0 ], [ %8, %4 ]
  %3 = icmp slt i64 %.010, 0
  br i1 %3, label %9, label %4

4:                                                ; preds = %2
  %5 = add nsw i64 %.010, -1
  %6 = cmpxchg ptr @g_rcutils_fault_injection_count, i64 %.010, i64 %5 seq_cst seq_cst, align 8
  %7 = extractvalue { i64, i1 } %6, 1
  %8 = extractvalue { i64, i1 } %6, 0
  br i1 %7, label %9, label %2, !llvm.loop !5

9:                                                ; preds = %4, %2
  ret i64 %.010
}

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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass