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

; 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_Characteristic = type { i16, i16, i32, ptr, ptr, ptr, ptr, ptr, ptr, i16, i8, i8, i8, i16, i16, i16, i16, i16, ptr }

@_ZL17s_pBtGattSrvcList = external hidden global ptr, align 8

; 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 uwtable
define dso_local void @BtGattSendCompleted(i16 noundef zeroext %0, i16 noundef zeroext %1) #1 {
  %3 = alloca i16, align 2
  %4 = alloca i16, align 2
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  store i16 %0, ptr %3, align 2, !tbaa !5
  store i16 %1, ptr %4, align 2, !tbaa !5
  %7 = load ptr, ptr @_ZL17s_pBtGattSrvcList, align 8, !tbaa !9
  %8 = icmp ne ptr %7, null
  br i1 %8, label %9, label %57

9:                                                ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %10 = load ptr, ptr @_ZL17s_pBtGattSrvcList, align 8, !tbaa !9
  store ptr %10, ptr %5, align 8, !tbaa !9
  br label %11

11:                                               ; preds = %52, %9
  %12 = load ptr, ptr %5, align 8, !tbaa !9
  %13 = icmp ne ptr %12, null
  br i1 %13, label %14, label %56

14:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  store i32 0, ptr %6, align 4, !tbaa !11
  br label %15

15:                                               ; preds = %49, %14
  %16 = load i32, ptr %6, align 4, !tbaa !11
  %17 = load ptr, ptr %5, align 8, !tbaa !9
  %18 = getelementptr inbounds %struct.__Bt_Service, ptr %17, i32 0, i32 0
  %19 = load i32, ptr %18, align 4, !tbaa !13
  %20 = icmp slt i32 %16, %19
  br i1 %20, label %22, label %21

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

22:                                               ; preds = %15
  %23 = load ptr, ptr %5, align 8, !tbaa !9
  %24 = getelementptr inbounds %struct.__Bt_Service, ptr %23, i32 0, i32 1
  %25 = load ptr, ptr %24, align 4, !tbaa !17
  %26 = load i32, ptr %6, align 4, !tbaa !11
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds %struct.__Bt_Characteristic, ptr %25, i64 %27
  %29 = getelementptr inbounds %struct.__Bt_Characteristic, ptr %28, i32 0, i32 7
  %30 = load ptr, ptr %29, align 4, !tbaa !18
  %31 = icmp ne ptr %30, null
  br i1 %31, label %32, label %48

32:                                               ; preds = %22
  %33 = load ptr, ptr %5, align 8, !tbaa !9
  %34 = getelementptr inbounds %struct.__Bt_Service, ptr %33, i32 0, i32 1
  %35 = load ptr, ptr %34, align 4, !tbaa !17
  %36 = load i32, ptr %6, align 4, !tbaa !11
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds %struct.__Bt_Characteristic, ptr %35, i64 %37
  %39 = getelementptr inbounds %struct.__Bt_Characteristic, ptr %38, i32 0, i32 7
  %40 = load ptr, ptr %39, align 4, !tbaa !18
  %41 = load ptr, ptr %5, align 8, !tbaa !9
  %42 = getelementptr inbounds %struct.__Bt_Service, ptr %41, i32 0, i32 1
  %43 = load ptr, ptr %42, align 4, !tbaa !17
  %44 = load i32, ptr %6, align 4, !tbaa !11
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds %struct.__Bt_Characteristic, ptr %43, i64 %45
  %47 = load i32, ptr %6, align 4, !tbaa !11
  call void %40(ptr noundef %46, i32 noundef %47)
  br label %48

48:                                               ; preds = %32, %22
  br label %49

49:                                               ; preds = %48
  %50 = load i32, ptr %6, align 4, !tbaa !11
  %51 = add nsw i32 %50, 1
  store i32 %51, ptr %6, align 4, !tbaa !11
  br label %15, !llvm.loop !21

52:                                               ; preds = %21
  %53 = load ptr, ptr %5, align 8, !tbaa !9
  %54 = getelementptr inbounds %struct.__Bt_Service, ptr %53, i32 0, i32 10
  %55 = load ptr, ptr %54, align 4, !tbaa !23
  store ptr %55, ptr %5, align 8, !tbaa !9
  br label %11, !llvm.loop !24

56:                                               ; preds = %11
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  br label %57

57:                                               ; preds = %56, %2
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 = !{!"short", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !12, i64 0}
!14 = !{!"_ZTS12__Bt_Service", !12, i64 0, !10, i64 4, !6, i64 12, !15, i64 14, !10, i64 20, !12, i64 28, !10, i64 32, !10, i64 40, !10, i64 48, !10, i64 56}
!15 = !{!"_ZTS9__Bt_Uuid", !7, i64 0, !16, i64 0, !7, i64 1}
!16 = !{!"_ZTS14__Bt_Uuid_Type", !7, i64 0}
!17 = !{!14, !10, i64 4}
!18 = !{!19, !10, i64 40}
!19 = !{!"_ZTS19__Bt_Characteristic", !6, i64 0, !6, i64 2, !12, i64 4, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !10, i64 40, !10, i64 48, !6, i64 56, !20, i64 58, !20, i64 59, !7, i64 60, !6, i64 62, !6, i64 64, !6, i64 66, !6, i64 68, !6, i64 70, !10, i64 72}
!20 = !{!"bool", !7, i64 0}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}
!23 = !{!14, !10, i64 56}
!24 = distinct !{!24, !22}


; 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_Characteristic = type { i16, i16, i32, ptr, ptr, ptr, ptr, ptr, ptr, i16, i8, i8, i8, i16, i16, i16, i16, i16, ptr }

@_ZL17s_pBtGattSrvcList = external hidden local_unnamed_addr global ptr, align 8

; Function Attrs: mustprogress uwtable
define dso_local void @BtGattSendCompleted(i16 noundef zeroext %0, i16 noundef zeroext %1) local_unnamed_addr #0 {
  %3 = load ptr, ptr @_ZL17s_pBtGattSrvcList, align 8, !tbaa !5
  %.not = icmp eq ptr %3, null
  br i1 %.not, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %2, %._crit_edge
  %.01118 = phi ptr [ %8, %._crit_edge ], [ %3, %2 ]
  %4 = load i32, ptr %.01118, align 4, !tbaa !9
  %5 = icmp sgt i32 %4, 0
  br i1 %5, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %.preheader
  %6 = getelementptr inbounds i8, ptr %.01118, i64 4
  br label %9

._crit_edge:                                      ; preds = %17, %.preheader
  %7 = getelementptr inbounds i8, ptr %.01118, i64 56
  %8 = load ptr, ptr %7, align 4, !tbaa !15
  %.not14 = icmp eq ptr %8, null
  br i1 %.not14, label %.loopexit, label %.preheader, !llvm.loop !16

9:                                                ; preds = %.lr.ph, %17
  %10 = phi i32 [ %4, %.lr.ph ], [ %18, %17 ]
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %17 ]
  %11 = load ptr, ptr %6, align 4, !tbaa !18
  %12 = getelementptr inbounds %struct.__Bt_Characteristic, ptr %11, i64 %indvars.iv
  %13 = getelementptr inbounds i8, ptr %12, i64 40
  %14 = load ptr, ptr %13, align 4, !tbaa !19
  %.not15 = icmp eq ptr %14, null
  br i1 %.not15, label %17, label %15

15:                                               ; preds = %9
  %16 = trunc nuw nsw i64 %indvars.iv to i32
  tail call void %14(ptr noundef nonnull %12, i32 noundef %16)
  %.pre = load i32, ptr %.01118, align 4, !tbaa !9
  br label %17

17:                                               ; preds = %9, %15
  %18 = phi i32 [ %10, %9 ], [ %.pre, %15 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %19 = sext i32 %18 to i64
  %20 = icmp slt i64 %indvars.iv.next, %19
  br i1 %20, label %9, label %._crit_edge, !llvm.loop !22

.loopexit:                                        ; preds = %._crit_edge, %2
  ret void
}

attributes #0 = { 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" }

!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, !11, i64 0}
!10 = !{!"_ZTS12__Bt_Service", !11, i64 0, !6, i64 4, !12, i64 12, !13, i64 14, !6, i64 20, !11, i64 28, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56}
!11 = !{!"int", !7, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = !{!"_ZTS9__Bt_Uuid", !7, i64 0, !14, i64 0, !7, i64 1}
!14 = !{!"_ZTS14__Bt_Uuid_Type", !7, i64 0}
!15 = !{!10, !6, i64 56}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = !{!10, !6, i64 4}
!19 = !{!20, !6, i64 40}
!20 = !{!"_ZTS19__Bt_Characteristic", !12, i64 0, !12, i64 2, !11, i64 4, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !12, i64 56, !21, i64 58, !21, i64 59, !7, i64 60, !12, i64 62, !12, i64 64, !12, i64 66, !12, i64 68, !12, i64 70, !6, i64 72}
!21 = !{!"bool", !7, i64 0}
!22 = distinct !{!22, !17}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass