; LLVM IR from FreeRTOS/backoffAlgorithm :: source (backoff_algorithm)

; 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.BackoffAlgorithmContext = type { i16, i32, i16, i32 }

@.str = external hidden unnamed_addr constant [22 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [86 x i8], align 1
@__PRETTY_FUNCTION__.BackoffAlgorithm_GetNextBackoff = external hidden unnamed_addr constant [108 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [21 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local i32 @BackoffAlgorithm_GetNextBackoff(ptr noundef %0, i32 noundef %1, ptr noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  store ptr %2, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 0, ptr %7, align 4, !tbaa !9
  %8 = load ptr, ptr %4, align 8, !tbaa !5
  %9 = icmp ne ptr %8, null
  br i1 %9, label %10, label %11

10:                                               ; preds = %3
  br label %12

11:                                               ; preds = %3
  call void @__assert_fail(ptr noundef @.str, ptr noundef @.str.1, i32 noundef 46, ptr noundef @__PRETTY_FUNCTION__.BackoffAlgorithm_GetNextBackoff) #4
  unreachable

12:                                               ; preds = %10
  %13 = load ptr, ptr %6, align 8, !tbaa !5
  %14 = icmp ne ptr %13, null
  br i1 %14, label %15, label %16

15:                                               ; preds = %12
  br label %17

16:                                               ; preds = %12
  call void @__assert_fail(ptr noundef @.str.2, ptr noundef @.str.1, i32 noundef 47, ptr noundef @__PRETTY_FUNCTION__.BackoffAlgorithm_GetNextBackoff) #4
  unreachable

17:                                               ; preds = %15
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.BackoffAlgorithmContext, ptr %18, i32 0, i32 3
  %20 = load i32, ptr %19, align 4, !tbaa !11
  %21 = icmp eq i32 %20, -1
  br i1 %21, label %30, label %22

22:                                               ; preds = %17
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.BackoffAlgorithmContext, ptr %23, i32 0, i32 1
  %25 = load i32, ptr %24, align 4, !tbaa !14
  %26 = load ptr, ptr %4, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.BackoffAlgorithmContext, ptr %26, i32 0, i32 3
  %28 = load i32, ptr %27, align 4, !tbaa !11
  %29 = icmp ult i32 %25, %28
  br i1 %29, label %30, label %72

30:                                               ; preds = %22, %17
  %31 = load i32, ptr %5, align 4, !tbaa !9
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.BackoffAlgorithmContext, ptr %32, i32 0, i32 2
  %34 = load i16, ptr %33, align 4, !tbaa !15
  %35 = zext i16 %34 to i32
  %36 = add i32 %35, 1
  %37 = urem i32 %31, %36
  %38 = trunc i32 %37 to i16
  %39 = load ptr, ptr %6, align 8, !tbaa !5
  store i16 %38, ptr %39, align 2, !tbaa !16
  %40 = load ptr, ptr %4, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.BackoffAlgorithmContext, ptr %40, i32 0, i32 1
  %42 = load i32, ptr %41, align 4, !tbaa !14
  %43 = add i32 %42, 1
  store i32 %43, ptr %41, align 4, !tbaa !14
  %44 = load ptr, ptr %4, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.BackoffAlgorithmContext, ptr %44, i32 0, i32 2
  %46 = load i16, ptr %45, align 4, !tbaa !15
  %47 = zext i16 %46 to i32
  %48 = load ptr, ptr %4, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.BackoffAlgorithmContext, ptr %48, i32 0, i32 0
  %50 = load i16, ptr %49, align 4, !tbaa !17
  %51 = zext i16 %50 to i32
  %52 = udiv i32 %51, 2
  %53 = icmp ult i32 %47, %52
  br i1 %53, label %54, label %65

54:                                               ; preds = %30
  %55 = load ptr, ptr %4, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.BackoffAlgorithmContext, ptr %55, i32 0, i32 2
  %57 = load i16, ptr %56, align 4, !tbaa !15
  %58 = zext i16 %57 to i32
  %59 = load ptr, ptr %4, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.BackoffAlgorithmContext, ptr %59, i32 0, i32 2
  %61 = load i16, ptr %60, align 4, !tbaa !15
  %62 = zext i16 %61 to i32
  %63 = add nsw i32 %62, %58
  %64 = trunc i32 %63 to i16
  store i16 %64, ptr %60, align 4, !tbaa !15
  br label %71

65:                                               ; preds = %30
  %66 = load ptr, ptr %4, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.BackoffAlgorithmContext, ptr %66, i32 0, i32 0
  %68 = load i16, ptr %67, align 4, !tbaa !17
  %69 = load ptr, ptr %4, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.BackoffAlgorithmContext, ptr %69, i32 0, i32 2
  store i16 %68, ptr %70, align 4, !tbaa !15
  br label %71

71:                                               ; preds = %65, %54
  br label %73

72:                                               ; preds = %22
  store i32 1, ptr %7, align 4, !tbaa !9
  br label %73

73:                                               ; preds = %72, %71
  %74 = load i32, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  ret i32 %74
}

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

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) #2

; 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 = { noreturn 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 #3 = { nounwind }
attributes #4 = { noreturn 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 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 12}
!12 = !{!"BackoffAlgorithmContext", !13, i64 0, !10, i64 4, !13, i64 8, !10, i64 12}
!13 = !{!"short", !7, i64 0}
!14 = !{!12, !10, i64 4}
!15 = !{!12, !13, i64 8}
!16 = !{!13, !13, i64 0}
!17 = !{!12, !13, 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"

@.str = external hidden unnamed_addr constant [22 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [86 x i8], align 1
@__PRETTY_FUNCTION__.BackoffAlgorithm_GetNextBackoff = external hidden unnamed_addr constant [108 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [21 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @BackoffAlgorithm_GetNextBackoff(ptr noundef %0, i32 noundef %1, ptr noundef writeonly %2) local_unnamed_addr #0 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %4, label %5

4:                                                ; preds = %3
  tail call void @__assert_fail(ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 46, ptr noundef nonnull @__PRETTY_FUNCTION__.BackoffAlgorithm_GetNextBackoff) #2
  unreachable

5:                                                ; preds = %3
  %.not19 = icmp eq ptr %2, null
  br i1 %.not19, label %6, label %7

6:                                                ; preds = %5
  tail call void @__assert_fail(ptr noundef nonnull @.str.2, ptr noundef nonnull @.str.1, i32 noundef 47, ptr noundef nonnull @__PRETTY_FUNCTION__.BackoffAlgorithm_GetNextBackoff) #2
  unreachable

7:                                                ; preds = %5
  %8 = getelementptr inbounds i8, ptr %0, i64 12
  %9 = load i32, ptr %8, align 4, !tbaa !5
  %10 = icmp eq i32 %9, -1
  %.phi.trans.insert = getelementptr inbounds i8, ptr %0, i64 4
  %.pre = load i32, ptr %.phi.trans.insert, align 4, !tbaa !11
  %11 = icmp ult i32 %.pre, %9
  %or.cond = select i1 %10, i1 true, i1 %11
  br i1 %or.cond, label %._crit_edge, label %25

._crit_edge:                                      ; preds = %7
  %12 = getelementptr inbounds i8, ptr %0, i64 8
  %13 = load i16, ptr %12, align 4, !tbaa !12
  %14 = zext i16 %13 to i32
  %15 = add nuw nsw i32 %14, 1
  %16 = urem i32 %1, %15
  %17 = trunc nuw i32 %16 to i16
  store i16 %17, ptr %2, align 2, !tbaa !13
  %18 = getelementptr inbounds i8, ptr %0, i64 4
  %19 = add i32 %.pre, 1
  store i32 %19, ptr %18, align 4, !tbaa !11
  %20 = load i16, ptr %12, align 4, !tbaa !12
  %21 = load i16, ptr %0, align 4, !tbaa !14
  %22 = lshr i16 %21, 1
  %23 = icmp ult i16 %20, %22
  %24 = shl nuw i16 %20, 1
  %.sink = select i1 %23, i16 %24, i16 %21
  store i16 %.sink, ptr %12, align 4, !tbaa !12
  br label %25

25:                                               ; preds = %._crit_edge, %7
  %.0 = phi i32 [ 1, %7 ], [ 0, %._crit_edge ]
  ret i32 %.0
}

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #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 = { noreturn 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 #2 = { noreturn 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, !10, i64 12}
!6 = !{!"BackoffAlgorithmContext", !7, i64 0, !10, i64 4, !7, i64 8, !10, i64 12}
!7 = !{!"short", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!6, !10, i64 4}
!12 = !{!6, !7, i64 8}
!13 = !{!7, !7, i64 0}
!14 = !{!6, !7, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass