; LLVM IR from CerebusOSS/CereLink :: cbhwlib/cbHwlibHi.ll (Z14cbGetChanUnitsjj)

; 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.cbCFGBUFF = type <{ i32, i32, %struct.cbOPTIONTABLE, %struct.cbCOLORTABLE, %struct.cbPKT_SYSINFO, [3 x %struct.cbPKT_PROCINFO], [3 x [22 x %struct.cbPKT_BANKINFO]], [3 x [8 x %struct.cbPKT_GROUPINFO]], [3 x [32 x %struct.cbPKT_FILTINFO]], [3 x %struct.cbPKT_ADAPTFILTINFO], [3 x %struct.cbPKT_REFELECFILTINFO], [596 x %struct.cbPKT_CHANINFO], %struct.cbSPIKE_SORTING, [280 x %struct.cbPKT_NTRODEINFO], [18 x [5 x %struct.cbPKT_AOUT_WAVEFORM]], [3 x %struct.cbPKT_LNC], %struct.cbPKT_NPLAY, [1 x %struct.cbVIDEOSOURCE], [20 x %struct.cbTRACKOBJ], %struct.cbPKT_FILECFG, ptr }>
%struct.cbOPTIONTABLE = type { float, [31 x i32] }
%struct.cbCOLORTABLE = type { [48 x i32], i32, i32, i32, i32, i32, i32, i32, i32, i32, [16 x i32], i32, [3 x i32], [5 x i32], [14 x i32] }
%struct.cbPKT_SYSINFO = type { %struct.cbPKT_HEADER, i32, i32, i32, i32, i32, i32 }
%struct.cbPKT_HEADER = type { i64, i16, i16, i16, i8, i8 }
%struct.cbPKT_PROCINFO = type { %struct.cbPKT_HEADER, i32, i32, [64 x i8], i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }
%struct.cbPKT_BANKINFO = type <{ %struct.cbPKT_HEADER, i32, i32, i32, [64 x i8], [16 x i8], i32, i32 }>
%struct.cbPKT_GROUPINFO = type { %struct.cbPKT_HEADER, i32, i32, [16 x i8], i32, i32, [560 x i16] }
%struct.cbPKT_FILTINFO = type { %struct.cbPKT_HEADER, i32, i32, [16 x i8], i32, i32, i32, i32, i32, i32, double, double, double, double, double, double, double, double, double }
%struct.cbPKT_ADAPTFILTINFO = type <{ %struct.cbPKT_HEADER, i32, i32, float, i32, i32 }>
%struct.cbPKT_REFELECFILTINFO = type <{ %struct.cbPKT_HEADER, i32, i32, i32 }>
%struct.cbPKT_CHANINFO = type { %struct.cbPKT_HEADER, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, %struct.cbSCALING, %struct.cbFILTDESC, %struct.cbSCALING, %struct.cbFILTDESC, [16 x i8], i32, [4 x i32], %struct.cbSCALING, %struct.cbSCALING, i32, i32, i32, i32, %union.anon, i8, [2 x i8], i8, i16, i16, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i16, i16, i32, [5 x %struct.cbMANUALUNITMAPPING], [5 x [4 x %struct.cbHOOP]] }
%struct.cbFILTDESC = type { [16 x i8], i32, i32, i32, i32, i32, i32 }
%struct.cbSCALING = type { i16, i16, i32, i32, i32, [8 x i8] }
%union.anon = type { %struct.anon }
%struct.anon = type { i16, i16, i32 }
%struct.cbMANUALUNITMAPPING = type { i16, [3 x i16], [3 x [3 x i16]], i16, i32 }
%struct.cbHOOP = type { i16, i16, i16, i16 }
%struct.cbSPIKE_SORTING = type { [596 x %struct.cbPKT_FS_BASIS], [596 x [7 x %struct.cbPKT_SS_MODELSET]], %struct.cbPKT_SS_DETECT, %struct.cbPKT_SS_ARTIF_REJECT, [596 x %struct.cbPKT_SS_NOISE_BOUNDARY], %struct.cbPKT_SS_STATISTICS, %struct.cbPKT_SS_STATUS }
%struct.cbPKT_FS_BASIS = type <{ %struct.cbPKT_HEADER, i32, i32, i32, [128 x [3 x float]] }>
%struct.cbPKT_SS_MODELSET = type <{ %struct.cbPKT_HEADER, i32, i32, i32, i32, i32, [2 x float], [2 x [2 x float]], float, [2 x [2 x float]], float, float, float, float, float }>
%struct.cbPKT_SS_DETECT = type { %struct.cbPKT_HEADER, float, float }
%struct.cbPKT_SS_ARTIF_REJECT = type { %struct.cbPKT_HEADER, i32, i32 }
%struct.cbPKT_SS_NOISE_BOUNDARY = type <{ %struct.cbPKT_HEADER, i32, [3 x float], [3 x [3 x float]] }>
%struct.cbPKT_SS_STATISTICS = type { %struct.cbPKT_HEADER, i32, i32, i32, float, float, float, float, float, float, float, i32, i32 }
%struct.cbPKT_SS_STATUS = type { %struct.cbPKT_HEADER, %struct.cbAdaptControl, %struct.cbAdaptControl }
%struct.cbAdaptControl = type { i32, float, float }
%struct.cbPKT_NTRODEINFO = type { %struct.cbPKT_HEADER, i32, [16 x i8], [6 x [5 x %struct.cbMANUALUNITMAPPING]], i16, i16, [4 x i16] }
%struct.cbPKT_AOUT_WAVEFORM = type { %struct.cbPKT_HEADER, i16, i16, i32, i8, i8, i16, i16, i8, i8, %struct.cbWaveformData }
%struct.cbWaveformData = type { i16, %union.anon.1 }
%union.anon.1 = type { %struct.anon.3 }
%struct.anon.3 = type { i16, i16, i16, [246 x i16], [246 x i16] }
%struct.cbPKT_LNC = type <{ %struct.cbPKT_HEADER, i32, i32, i32 }>
%struct.cbPKT_NPLAY = type { %struct.cbPKT_HEADER, %union.anon.4, i64, i64, i64, i16, i16, float, [968 x i8] }
%union.anon.4 = type { i64 }
%struct.cbVIDEOSOURCE = type { [16 x i8], float }
%struct.cbTRACKOBJ = type { [16 x i8], i16, i16 }
%struct.cbPKT_FILECFG = type { %struct.cbPKT_HEADER, i32, i32, i32, i32, [256 x i8], %union.anon.5, [256 x i8] }
%union.anon.5 = type { [256 x i8] }

@.str.2 = external hidden unnamed_addr constant [75 x i8], align 1
@cb_library_index = external global [4 x i32], align 16
@cb_cfg_buffer_ptr = external global [4 x ptr], align 16
@cb_pc_status_buffer_ptr = external global [4 x ptr], align 16
@.str.17 = external hidden unnamed_addr constant [19 x i8], align 1
@__PRETTY_FUNCTION__._Z14cbGetChanUnitsjj = external hidden unnamed_addr constant [44 x i8], align 1
@.str.18 = external hidden unnamed_addr constant [20 x i8], align 1

; 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: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) #1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZN10cbPcStatus18cbGetNumTotalChansEv(ptr noundef nonnull align 1 dereferenceable(6160)) #2 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z14cbGetChanUnitsjj(i32 noundef %0, i32 noundef %1) #3 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  %8 = load i32, ptr %4, align 4, !tbaa !5
  %9 = zext i32 %8 to i64
  %10 = getelementptr inbounds [4 x i32], ptr @cb_library_index, i64 0, i64 %9
  %11 = load i32, ptr %10, align 4, !tbaa !5
  store i32 %11, ptr %5, align 4, !tbaa !5
  %12 = load i32, ptr %3, align 4, !tbaa !5
  %13 = icmp uge i32 %12, 1
  br i1 %13, label %14, label %15

14:                                               ; preds = %2
  br label %17

15:                                               ; preds = %2
  call void @__assert_fail(ptr noundef @.str.17, ptr noundef @.str.2, i32 noundef 806, ptr noundef @__PRETTY_FUNCTION__._Z14cbGetChanUnitsjj) #5
  unreachable

16:                                               ; No predecessors!
  br label %17

17:                                               ; preds = %16, %14
  %18 = load i32, ptr %3, align 4, !tbaa !5
  %19 = icmp ule i32 %18, 596
  br i1 %19, label %20, label %21

20:                                               ; preds = %17
  br label %23

21:                                               ; preds = %17
  call void @__assert_fail(ptr noundef @.str.18, ptr noundef @.str.2, i32 noundef 807, ptr noundef @__PRETTY_FUNCTION__._Z14cbGetChanUnitsjj) #5
  unreachable

22:                                               ; No predecessors!
  br label %23

23:                                               ; preds = %22, %20
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  store i32 0, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  store i32 1, ptr %7, align 4, !tbaa !5
  br label %24

24:                                               ; preds = %51, %23
  %25 = load i32, ptr %7, align 4, !tbaa !5
  %26 = load ptr, ptr @cb_pc_status_buffer_ptr, align 16, !tbaa !9
  %27 = call noundef i32 @_ZN10cbPcStatus18cbGetNumTotalChansEv(ptr noundef nonnull align 1 dereferenceable(6160) %26)
  %28 = icmp ult i32 %25, %27
  br i1 %28, label %30, label %29

29:                                               ; preds = %24
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  br label %54

30:                                               ; preds = %24
  %31 = load i32, ptr %5, align 4, !tbaa !5
  %32 = zext i32 %31 to i64
  %33 = getelementptr inbounds [4 x ptr], ptr @cb_cfg_buffer_ptr, i64 0, i64 %32
  %34 = load ptr, ptr %33, align 8, !tbaa !9
  %35 = getelementptr inbounds %struct.cbCFGBUFF, ptr %34, i32 0, i32 12
  %36 = getelementptr inbounds %struct.cbSPIKE_SORTING, ptr %35, i32 0, i32 1
  %37 = load i32, ptr %3, align 4, !tbaa !5
  %38 = sub i32 %37, 1
  %39 = zext i32 %38 to i64
  %40 = getelementptr inbounds [596 x [7 x %struct.cbPKT_SS_MODELSET]], ptr %36, i64 0, i64 %39
  %41 = load i32, ptr %7, align 4, !tbaa !5
  %42 = zext i32 %41 to i64
  %43 = getelementptr inbounds [7 x %struct.cbPKT_SS_MODELSET], ptr %40, i64 0, i64 %42
  %44 = getelementptr inbounds %struct.cbPKT_SS_MODELSET, ptr %43, i32 0, i32 3
  %45 = load i32, ptr %44, align 1, !tbaa !11
  %46 = icmp ne i32 %45, 0
  br i1 %46, label %47, label %50

47:                                               ; preds = %30
  %48 = load i32, ptr %6, align 4, !tbaa !5
  %49 = add i32 %48, 1
  store i32 %49, ptr %6, align 4, !tbaa !5
  br label %50

50:                                               ; preds = %47, %30
  br label %51

51:                                               ; preds = %50
  %52 = load i32, ptr %7, align 4, !tbaa !5
  %53 = add i32 %52, 1
  store i32 %53, ptr %7, align 4, !tbaa !5
  br label %24, !llvm.loop !17

54:                                               ; preds = %29
  %55 = load i32, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  ret i32 %55
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 #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 = { nounwind }
attributes #5 = { 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 = !{!"int", !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, !6, i64 24}
!12 = !{!"_ZTS17cbPKT_SS_MODELSET", !13, i64 0, !6, i64 16, !6, i64 20, !6, i64 24, !6, i64 28, !6, i64 32, !7, i64 36, !7, i64 44, !16, i64 60, !7, i64 64, !16, i64 80, !16, i64 84, !16, i64 88, !16, i64 92, !16, i64 96}
!13 = !{!"_ZTS12cbPKT_HEADER", !14, i64 0, !15, i64 8, !15, i64 10, !15, i64 12, !7, i64 14, !7, i64 15}
!14 = !{!"long", !7, i64 0}
!15 = !{!"short", !7, i64 0}
!16 = !{!"float", !7, 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.cbPKT_SS_MODELSET = type <{ %struct.cbPKT_HEADER, i32, i32, i32, i32, i32, [2 x float], [2 x [2 x float]], float, [2 x [2 x float]], float, float, float, float, float }>
%struct.cbPKT_HEADER = type { i64, i16, i16, i16, i8, i8 }

@.str.2 = external hidden unnamed_addr constant [75 x i8], align 1
@cb_library_index = external local_unnamed_addr global [4 x i32], align 16
@cb_cfg_buffer_ptr = external local_unnamed_addr global [4 x ptr], align 16
@cb_pc_status_buffer_ptr = external local_unnamed_addr global [4 x ptr], align 16
@.str.17 = external hidden unnamed_addr constant [19 x i8], align 1
@__PRETTY_FUNCTION__._Z14cbGetChanUnitsjj = external hidden unnamed_addr constant [44 x i8], align 1
@.str.18 = external hidden unnamed_addr constant [20 x i8], align 1

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZN10cbPcStatus18cbGetNumTotalChansEv(ptr noundef nonnull align 1 dereferenceable(6160)) local_unnamed_addr #1 align 2

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z14cbGetChanUnitsjj(i32 noundef %0, i32 noundef %1) local_unnamed_addr #1 {
  %3 = zext i32 %1 to i64
  %4 = getelementptr inbounds [4 x i32], ptr @cb_library_index, i64 0, i64 %3
  %5 = load i32, ptr %4, align 4, !tbaa !5
  %.not = icmp eq i32 %0, 0
  br i1 %.not, label %6, label %7

6:                                                ; preds = %2
  tail call void @__assert_fail(ptr noundef nonnull @.str.17, ptr noundef nonnull @.str.2, i32 noundef 806, ptr noundef nonnull @__PRETTY_FUNCTION__._Z14cbGetChanUnitsjj) #2
  unreachable

7:                                                ; preds = %2
  %8 = icmp ult i32 %0, 597
  br i1 %8, label %.preheader, label %16

.preheader:                                       ; preds = %7
  %9 = load ptr, ptr @cb_pc_status_buffer_ptr, align 16, !tbaa !9
  %10 = tail call noundef i32 @_ZN10cbPcStatus18cbGetNumTotalChansEv(ptr noundef nonnull align 1 dereferenceable(6160) %9)
  %11 = icmp ugt i32 %10, 1
  br i1 %11, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %.preheader
  %12 = zext i32 %5 to i64
  %13 = getelementptr inbounds [4 x ptr], ptr @cb_cfg_buffer_ptr, i64 0, i64 %12
  %14 = add nsw i32 %0, -1
  %15 = zext nneg i32 %14 to i64
  br label %17

16:                                               ; preds = %7
  tail call void @__assert_fail(ptr noundef nonnull @.str.18, ptr noundef nonnull @.str.2, i32 noundef 807, ptr noundef nonnull @__PRETTY_FUNCTION__._Z14cbGetChanUnitsjj) #2
  unreachable

._crit_edge:                                      ; preds = %17, %.preheader
  %.010.lcssa = phi i32 [ 0, %.preheader ], [ %spec.select, %17 ]
  ret i32 %.010.lcssa

17:                                               ; preds = %.lr.ph, %17
  %indvars.iv = phi i64 [ 1, %.lr.ph ], [ %indvars.iv.next, %17 ]
  %.01013 = phi i32 [ 0, %.lr.ph ], [ %spec.select, %17 ]
  %18 = load ptr, ptr %13, align 8, !tbaa !9
  %19 = getelementptr inbounds i8, ptr %18, i64 1387304
  %20 = getelementptr inbounds [596 x [7 x %struct.cbPKT_SS_MODELSET]], ptr %19, i64 0, i64 %15, i64 %indvars.iv, i32 3
  %21 = load i32, ptr %20, align 1, !tbaa !11
  %.not12 = icmp ne i32 %21, 0
  %22 = zext i1 %.not12 to i32
  %spec.select = add i32 %.01013, %22
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %23 = load ptr, ptr @cb_pc_status_buffer_ptr, align 16, !tbaa !9
  %24 = tail call noundef i32 @_ZN10cbPcStatus18cbGetNumTotalChansEv(ptr noundef nonnull align 1 dereferenceable(6160) %23)
  %25 = zext i32 %24 to i64
  %26 = icmp ult i64 %indvars.iv.next, %25
  br i1 %26, label %17, label %._crit_edge, !llvm.loop !17
}

attributes #0 = { 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 #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 = { 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 = !{!"int", !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, !6, i64 24}
!12 = !{!"_ZTS17cbPKT_SS_MODELSET", !13, i64 0, !6, i64 16, !6, i64 20, !6, i64 24, !6, i64 28, !6, i64 32, !7, i64 36, !7, i64 44, !16, i64 60, !7, i64 64, !16, i64 80, !16, i64 84, !16, i64 88, !16, i64 92, !16, i64 96}
!13 = !{!"_ZTS12cbPKT_HEADER", !14, i64 0, !15, i64 8, !15, i64 10, !15, i64 12, !7, i64 14, !7, i64 15}
!14 = !{!"long", !7, i64 0}
!15 = !{!"short", !7, i64 0}
!16 = !{!"float", !7, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass