; LLVM IR from IOsonata/IOsonata :: src/bluetooth (bt_gap)

; 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.__Bt_Gap_Connection = type { i16, i8, i8, [6 x i8] }

@_ZL17s_BtGapConnection = external hidden global <{ %struct.__Bt_Gap_Connection, [9 x %struct.__Bt_Gap_Connection] }>, align 4

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

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local zeroext i16 @BtGapGetConnection() #1 {
  %1 = alloca i16, align 2
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #2
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %4

4:                                                ; preds = %23, %0
  %5 = load i32, ptr %2, align 4, !tbaa !5
  %6 = icmp slt i32 %5, 10
  br i1 %6, label %8, label %7

7:                                                ; preds = %4
  store i32 2, ptr %3, align 4
  br label %26

8:                                                ; preds = %4
  %9 = load i32, ptr %2, align 4, !tbaa !5
  %10 = sext i32 %9 to i64
  %11 = getelementptr inbounds [10 x %struct.__Bt_Gap_Connection], ptr @_ZL17s_BtGapConnection, i64 0, i64 %10
  %12 = getelementptr inbounds %struct.__Bt_Gap_Connection, ptr %11, i32 0, i32 0
  %13 = load i16, ptr %12, align 2, !tbaa !9
  %14 = zext i16 %13 to i32
  %15 = icmp ne i32 %14, 65535
  br i1 %15, label %16, label %22

16:                                               ; preds = %8
  %17 = load i32, ptr %2, align 4, !tbaa !5
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [10 x %struct.__Bt_Gap_Connection], ptr @_ZL17s_BtGapConnection, i64 0, i64 %18
  %20 = getelementptr inbounds %struct.__Bt_Gap_Connection, ptr %19, i32 0, i32 0
  %21 = load i16, ptr %20, align 2, !tbaa !9
  store i16 %21, ptr %1, align 2
  store i32 1, ptr %3, align 4
  br label %26

22:                                               ; preds = %8
  br label %23

23:                                               ; preds = %22
  %24 = load i32, ptr %2, align 4, !tbaa !5
  %25 = add nsw i32 %24, 1
  store i32 %25, ptr %2, align 4, !tbaa !5
  br label %4, !llvm.loop !12

26:                                               ; preds = %16, %7
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #2
  %27 = load i32, ptr %3, align 4
  switch i32 %27, label %31 [
    i32 2, label %28
    i32 1, label %29
  ]

28:                                               ; preds = %26
  store i16 -1, ptr %1, align 2
  br label %29

29:                                               ; preds = %28, %26
  %30 = load i16, ptr %1, align 2
  ret i16 %30

31:                                               ; preds = %26
  unreachable
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !11, i64 0}
!10 = !{!"_ZTS19__Bt_Gap_Connection", !11, i64 0, !7, i64 2, !7, i64 3, !7, i64 4}
!11 = !{!"short", !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"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"

%struct.__Bt_Gap_Connection = type { i16, i8, i8, [6 x i8] }

@_ZL17s_BtGapConnection = external hidden local_unnamed_addr global <{ %struct.__Bt_Gap_Connection, [9 x %struct.__Bt_Gap_Connection] }>, align 4

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local zeroext i16 @BtGapGetConnection() local_unnamed_addr #0 {
  %1 = load i16, ptr @_ZL17s_BtGapConnection, align 4, !tbaa !5
  %.not = icmp eq i16 %1, -1
  br i1 %.not, label %2, label %19

2:                                                ; preds = %0
  %3 = load i16, ptr getelementptr inbounds (i8, ptr @_ZL17s_BtGapConnection, i64 10), align 2, !tbaa !5
  %.not.1 = icmp eq i16 %3, -1
  br i1 %.not.1, label %4, label %19

4:                                                ; preds = %2
  %5 = load i16, ptr getelementptr inbounds (i8, ptr @_ZL17s_BtGapConnection, i64 20), align 4, !tbaa !5
  %.not.2 = icmp eq i16 %5, -1
  br i1 %.not.2, label %6, label %19

6:                                                ; preds = %4
  %7 = load i16, ptr getelementptr inbounds (i8, ptr @_ZL17s_BtGapConnection, i64 30), align 2, !tbaa !5
  %.not.3 = icmp eq i16 %7, -1
  br i1 %.not.3, label %8, label %19

8:                                                ; preds = %6
  %9 = load i16, ptr getelementptr inbounds (i8, ptr @_ZL17s_BtGapConnection, i64 40), align 4, !tbaa !5
  %.not.4 = icmp eq i16 %9, -1
  br i1 %.not.4, label %10, label %19

10:                                               ; preds = %8
  %11 = load i16, ptr getelementptr inbounds (i8, ptr @_ZL17s_BtGapConnection, i64 50), align 2, !tbaa !5
  %.not.5 = icmp eq i16 %11, -1
  br i1 %.not.5, label %12, label %19

12:                                               ; preds = %10
  %13 = load i16, ptr getelementptr inbounds (i8, ptr @_ZL17s_BtGapConnection, i64 60), align 4, !tbaa !5
  %.not.6 = icmp eq i16 %13, -1
  br i1 %.not.6, label %14, label %19

14:                                               ; preds = %12
  %15 = load i16, ptr getelementptr inbounds (i8, ptr @_ZL17s_BtGapConnection, i64 70), align 2, !tbaa !5
  %.not.7 = icmp eq i16 %15, -1
  br i1 %.not.7, label %16, label %19

16:                                               ; preds = %14
  %17 = load i16, ptr getelementptr inbounds (i8, ptr @_ZL17s_BtGapConnection, i64 80), align 4, !tbaa !5
  %.not.8 = icmp eq i16 %17, -1
  %18 = load i16, ptr getelementptr inbounds (i8, ptr @_ZL17s_BtGapConnection, i64 90), align 2
  %spec.select11 = select i1 %.not.8, i16 %18, i16 %17
  br label %19

19:                                               ; preds = %16, %14, %12, %10, %8, %6, %4, %2, %0
  %spec.select = phi i16 [ %1, %0 ], [ %3, %2 ], [ %5, %4 ], [ %7, %6 ], [ %9, %8 ], [ %11, %10 ], [ %13, %12 ], [ %15, %14 ], [ %spec.select11, %16 ]
  ret i16 %spec.select
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) 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 = !{!6, !7, i64 0}
!6 = !{!"_ZTS19__Bt_Gap_Connection", !7, i64 0, !8, i64 2, !8, i64 3, !8, i64 4}
!7 = !{!"short", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}


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