; 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 i64 @BtGapGetConnectedHandles(ptr noundef %0, i64 noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i64 %1, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  store i64 0, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  store i32 0, ptr %6, align 4, !tbaa !11
  br label %7

7:                                                ; preds = %30, %2
  %8 = load i32, ptr %6, align 4, !tbaa !11
  %9 = icmp slt i32 %8, 10
  br i1 %9, label %11, label %10

10:                                               ; preds = %7
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  br label %33

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

19:                                               ; preds = %11
  %20 = load i32, ptr %6, align 4, !tbaa !11
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [10 x %struct.__Bt_Gap_Connection], ptr @_ZL17s_BtGapConnection, i64 0, i64 %21
  %23 = getelementptr inbounds %struct.__Bt_Gap_Connection, ptr %22, i32 0, i32 0
  %24 = load i16, ptr %23, align 2, !tbaa !13
  %25 = load ptr, ptr %3, align 8, !tbaa !5
  %26 = load i64, ptr %5, align 8, !tbaa !9
  %27 = add i64 %26, 1
  store i64 %27, ptr %5, align 8, !tbaa !9
  %28 = getelementptr inbounds i16, ptr %25, i64 %26
  store i16 %24, ptr %28, align 2, !tbaa !16
  br label %29

29:                                               ; preds = %19, %11
  br label %30

30:                                               ; preds = %29
  %31 = load i32, ptr %6, align 4, !tbaa !11
  %32 = add nsw i32 %31, 1
  store i32 %32, ptr %6, align 4, !tbaa !11
  br label %7, !llvm.loop !17

33:                                               ; preds = %10
  %34 = load i64, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  ret i64 %34
}

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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !15, i64 0}
!14 = !{!"_ZTS19__Bt_Gap_Connection", !15, i64 0, !7, i64 2, !7, i64 3, !7, i64 4}
!15 = !{!"short", !7, i64 0}
!16 = !{!15, !15, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"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: write, inaccessiblemem: none) uwtable
define dso_local i64 @BtGapGetConnectedHandles(ptr nocapture noundef writeonly %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = load i16, ptr @_ZL17s_BtGapConnection, align 4, !tbaa !5
  %.not = icmp eq i16 %3, -1
  br i1 %.not, label %5, label %4

4:                                                ; preds = %2
  store i16 %3, ptr %0, align 2, !tbaa !10
  br label %5

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

7:                                                ; preds = %5
  %8 = add nuw nsw i64 %.1, 1
  %9 = getelementptr inbounds i16, ptr %0, i64 %.1
  store i16 %6, ptr %9, align 2, !tbaa !10
  br label %10

10:                                               ; preds = %7, %5
  %.1.1 = phi i64 [ %8, %7 ], [ %.1, %5 ]
  %11 = load i16, ptr getelementptr inbounds (i8, ptr @_ZL17s_BtGapConnection, i64 20), align 4, !tbaa !5
  %.not.2 = icmp eq i16 %11, -1
  br i1 %.not.2, label %15, label %12

12:                                               ; preds = %10
  %13 = add nuw nsw i64 %.1.1, 1
  %14 = getelementptr inbounds i16, ptr %0, i64 %.1.1
  store i16 %11, ptr %14, align 2, !tbaa !10
  br label %15

15:                                               ; preds = %12, %10
  %.1.2 = phi i64 [ %13, %12 ], [ %.1.1, %10 ]
  %16 = load i16, ptr getelementptr inbounds (i8, ptr @_ZL17s_BtGapConnection, i64 30), align 2, !tbaa !5
  %.not.3 = icmp eq i16 %16, -1
  br i1 %.not.3, label %20, label %17

17:                                               ; preds = %15
  %18 = add nuw nsw i64 %.1.2, 1
  %19 = getelementptr inbounds i16, ptr %0, i64 %.1.2
  store i16 %16, ptr %19, align 2, !tbaa !10
  br label %20

20:                                               ; preds = %17, %15
  %.1.3 = phi i64 [ %18, %17 ], [ %.1.2, %15 ]
  %21 = load i16, ptr getelementptr inbounds (i8, ptr @_ZL17s_BtGapConnection, i64 40), align 4, !tbaa !5
  %.not.4 = icmp eq i16 %21, -1
  br i1 %.not.4, label %25, label %22

22:                                               ; preds = %20
  %23 = add nuw nsw i64 %.1.3, 1
  %24 = getelementptr inbounds i16, ptr %0, i64 %.1.3
  store i16 %21, ptr %24, align 2, !tbaa !10
  br label %25

25:                                               ; preds = %22, %20
  %.1.4 = phi i64 [ %23, %22 ], [ %.1.3, %20 ]
  %26 = load i16, ptr getelementptr inbounds (i8, ptr @_ZL17s_BtGapConnection, i64 50), align 2, !tbaa !5
  %.not.5 = icmp eq i16 %26, -1
  br i1 %.not.5, label %30, label %27

27:                                               ; preds = %25
  %28 = add nuw nsw i64 %.1.4, 1
  %29 = getelementptr inbounds i16, ptr %0, i64 %.1.4
  store i16 %26, ptr %29, align 2, !tbaa !10
  br label %30

30:                                               ; preds = %27, %25
  %.1.5 = phi i64 [ %28, %27 ], [ %.1.4, %25 ]
  %31 = load i16, ptr getelementptr inbounds (i8, ptr @_ZL17s_BtGapConnection, i64 60), align 4, !tbaa !5
  %.not.6 = icmp eq i16 %31, -1
  br i1 %.not.6, label %35, label %32

32:                                               ; preds = %30
  %33 = add nuw nsw i64 %.1.5, 1
  %34 = getelementptr inbounds i16, ptr %0, i64 %.1.5
  store i16 %31, ptr %34, align 2, !tbaa !10
  br label %35

35:                                               ; preds = %32, %30
  %.1.6 = phi i64 [ %33, %32 ], [ %.1.5, %30 ]
  %36 = load i16, ptr getelementptr inbounds (i8, ptr @_ZL17s_BtGapConnection, i64 70), align 2, !tbaa !5
  %.not.7 = icmp eq i16 %36, -1
  br i1 %.not.7, label %40, label %37

37:                                               ; preds = %35
  %38 = add nuw nsw i64 %.1.6, 1
  %39 = getelementptr inbounds i16, ptr %0, i64 %.1.6
  store i16 %36, ptr %39, align 2, !tbaa !10
  br label %40

40:                                               ; preds = %37, %35
  %.1.7 = phi i64 [ %38, %37 ], [ %.1.6, %35 ]
  %41 = load i16, ptr getelementptr inbounds (i8, ptr @_ZL17s_BtGapConnection, i64 80), align 4, !tbaa !5
  %.not.8 = icmp eq i16 %41, -1
  br i1 %.not.8, label %45, label %42

42:                                               ; preds = %40
  %43 = add nuw nsw i64 %.1.7, 1
  %44 = getelementptr inbounds i16, ptr %0, i64 %.1.7
  store i16 %41, ptr %44, align 2, !tbaa !10
  br label %45

45:                                               ; preds = %42, %40
  %.1.8 = phi i64 [ %43, %42 ], [ %.1.7, %40 ]
  %46 = load i16, ptr getelementptr inbounds (i8, ptr @_ZL17s_BtGapConnection, i64 90), align 2, !tbaa !5
  %.not.9 = icmp eq i16 %46, -1
  br i1 %.not.9, label %50, label %47

47:                                               ; preds = %45
  %48 = add nuw nsw i64 %.1.8, 1
  %49 = getelementptr inbounds i16, ptr %0, i64 %.1.8
  store i16 %46, ptr %49, align 2, !tbaa !10
  br label %50

50:                                               ; preds = %47, %45
  %.1.9 = phi i64 [ %48, %47 ], [ %.1.8, %45 ]
  ret i64 %.1.9
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: write, 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"}
!10 = !{!7, !7, i64 0}


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