; 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_Service = type <{ i32, ptr, i16, %struct.__Bt_Uuid, i8, ptr, i32, ptr, ptr, ptr, ptr }>
%struct.__Bt_Uuid = type <{ i8, %union.anon }>
%union.anon = type { i32 }
%struct.__Bt_Gap_Connection = type { i16, i8, i8, [6 x i8] }

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

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #0

; Function Attrs: mustprogress nounwind uwtable
declare dso_local zeroext i1 @isBtGapConnected() #1

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

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

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

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

7:                                                ; preds = %4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #5
  br label %26

8:                                                ; preds = %4
  %9 = load i32, ptr %3, align 4, !tbaa !9
  %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 !11
  %14 = zext i16 %13 to i32
  %15 = load i16, ptr %2, align 2, !tbaa !5
  %16 = zext i16 %15 to i32
  %17 = icmp eq i32 %14, %16
  br i1 %17, label %18, label %22

18:                                               ; preds = %8
  %19 = load i32, ptr %3, align 4, !tbaa !9
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [10 x %struct.__Bt_Gap_Connection], ptr @_ZL17s_BtGapConnection, i64 0, i64 %20
  call void @llvm.memset.p0.i64(ptr align 2 %21, i8 -1, i64 10, i1 false)
  br label %22

22:                                               ; preds = %18, %8
  br label %23

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

26:                                               ; preds = %7
  %27 = call zeroext i1 @isBtGapConnected()
  %28 = zext i1 %27 to i32
  %29 = icmp eq i32 %28, 0
  br i1 %29, label %30, label %31

30:                                               ; preds = %26
  call void @BtGattSrvcDisconnected(ptr noundef @_ZL11s_BtGapSrvc)
  call void @BtGattSrvcDisconnected(ptr noundef @_ZL12s_BtGattSrvc)
  br label %31

31:                                               ; preds = %30, %26
  ret void
}

declare void @BtGattSrvcDisconnected(ptr noundef) #4

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

%struct.__Bt_Service = type <{ i32, ptr, i16, %struct.__Bt_Uuid, i8, ptr, i32, ptr, ptr, ptr, ptr }>
%struct.__Bt_Uuid = type <{ i8, %union.anon }>
%union.anon = type { i32 }
%struct.__Bt_Gap_Connection = type { i16, i8, i8, [6 x i8] }

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

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #0

; Function Attrs: mustprogress nounwind uwtable
declare dso_local zeroext i1 @isBtGapConnected() local_unnamed_addr #1

; Function Attrs: mustprogress uwtable
define dso_local void @BtGapDeleteConnection(i16 noundef zeroext %0) local_unnamed_addr #2 {
  %2 = load i16, ptr @_ZL17s_BtGapConnection, align 4, !tbaa !5
  %3 = icmp eq i16 %2, %0
  br i1 %3, label %4, label %5

4:                                                ; preds = %1
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 dereferenceable(10) @_ZL17s_BtGapConnection, i8 -1, i64 10, i1 false)
  br label %5

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

8:                                                ; preds = %5
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 2 dereferenceable(10) getelementptr inbounds (i8, ptr @_ZL17s_BtGapConnection, i64 10), i8 -1, i64 10, i1 false)
  br label %9

9:                                                ; preds = %8, %5
  %10 = load i16, ptr getelementptr inbounds (i8, ptr @_ZL17s_BtGapConnection, i64 20), align 4, !tbaa !5
  %11 = icmp eq i16 %10, %0
  br i1 %11, label %12, label %13

12:                                               ; preds = %9
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 dereferenceable(10) getelementptr inbounds (i8, ptr @_ZL17s_BtGapConnection, i64 20), i8 -1, i64 10, i1 false)
  br label %13

13:                                               ; preds = %12, %9
  %14 = load i16, ptr getelementptr inbounds (i8, ptr @_ZL17s_BtGapConnection, i64 30), align 2, !tbaa !5
  %15 = icmp eq i16 %14, %0
  br i1 %15, label %16, label %17

16:                                               ; preds = %13
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 2 dereferenceable(10) getelementptr inbounds (i8, ptr @_ZL17s_BtGapConnection, i64 30), i8 -1, i64 10, i1 false)
  br label %17

17:                                               ; preds = %16, %13
  %18 = load i16, ptr getelementptr inbounds (i8, ptr @_ZL17s_BtGapConnection, i64 40), align 4, !tbaa !5
  %19 = icmp eq i16 %18, %0
  br i1 %19, label %20, label %21

20:                                               ; preds = %17
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 dereferenceable(10) getelementptr inbounds (i8, ptr @_ZL17s_BtGapConnection, i64 40), i8 -1, i64 10, i1 false)
  br label %21

21:                                               ; preds = %20, %17
  %22 = load i16, ptr getelementptr inbounds (i8, ptr @_ZL17s_BtGapConnection, i64 50), align 2, !tbaa !5
  %23 = icmp eq i16 %22, %0
  br i1 %23, label %24, label %25

24:                                               ; preds = %21
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 2 dereferenceable(10) getelementptr inbounds (i8, ptr @_ZL17s_BtGapConnection, i64 50), i8 -1, i64 10, i1 false)
  br label %25

25:                                               ; preds = %24, %21
  %26 = load i16, ptr getelementptr inbounds (i8, ptr @_ZL17s_BtGapConnection, i64 60), align 4, !tbaa !5
  %27 = icmp eq i16 %26, %0
  br i1 %27, label %28, label %29

28:                                               ; preds = %25
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 dereferenceable(10) getelementptr inbounds (i8, ptr @_ZL17s_BtGapConnection, i64 60), i8 -1, i64 10, i1 false)
  br label %29

29:                                               ; preds = %28, %25
  %30 = load i16, ptr getelementptr inbounds (i8, ptr @_ZL17s_BtGapConnection, i64 70), align 2, !tbaa !5
  %31 = icmp eq i16 %30, %0
  br i1 %31, label %32, label %33

32:                                               ; preds = %29
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 2 dereferenceable(10) getelementptr inbounds (i8, ptr @_ZL17s_BtGapConnection, i64 70), i8 -1, i64 10, i1 false)
  br label %33

33:                                               ; preds = %32, %29
  %34 = load i16, ptr getelementptr inbounds (i8, ptr @_ZL17s_BtGapConnection, i64 80), align 4, !tbaa !5
  %35 = icmp eq i16 %34, %0
  br i1 %35, label %36, label %37

36:                                               ; preds = %33
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 dereferenceable(10) getelementptr inbounds (i8, ptr @_ZL17s_BtGapConnection, i64 80), i8 -1, i64 10, i1 false)
  br label %37

37:                                               ; preds = %36, %33
  %38 = load i16, ptr getelementptr inbounds (i8, ptr @_ZL17s_BtGapConnection, i64 90), align 2, !tbaa !5
  %39 = icmp eq i16 %38, %0
  br i1 %39, label %40, label %41

40:                                               ; preds = %37
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 2 dereferenceable(10) getelementptr inbounds (i8, ptr @_ZL17s_BtGapConnection, i64 90), i8 -1, i64 10, i1 false)
  br label %41

41:                                               ; preds = %40, %37
  %42 = tail call zeroext i1 @isBtGapConnected()
  br i1 %42, label %44, label %43

43:                                               ; preds = %41
  tail call void @BtGattSrvcDisconnected(ptr noundef nonnull @_ZL11s_BtGapSrvc)
  tail call void @BtGattSrvcDisconnected(ptr noundef nonnull @_ZL12s_BtGattSrvc)
  br label %44

44:                                               ; preds = %43, %41
  ret void
}

declare void @BtGattSrvcDisconnected(ptr noundef) local_unnamed_addr #3

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
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 = { mustprogress 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 #3 = { "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: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,SROAPass,SimplifyCFGPass,TailCallElimPass