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

; 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
@.str.20 = external hidden unnamed_addr constant [29 x i8], align 1
@__PRETTY_FUNCTION__._Z19cbGetChanInstrumentjj = external hidden unnamed_addr constant [49 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
define dso_local noundef i32 @_Z19cbGetChanInstrumentjj(i32 noundef %0, i32 noundef %1) #2 {
  %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
  %8 = alloca i32, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 1, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %9 = load i32, ptr %5, align 4, !tbaa !5
  %10 = zext i32 %9 to i64
  %11 = getelementptr inbounds [4 x i32], ptr @cb_library_index, i64 0, i64 %10
  %12 = load i32, ptr %11, align 4, !tbaa !5
  store i32 %12, ptr %7, align 4, !tbaa !5
  %13 = load i32, ptr %4, align 4, !tbaa !5
  %14 = sub i32 %13, 1
  %15 = icmp uge i32 %14, 596
  br i1 %15, label %16, label %17

16:                                               ; preds = %2
  store i32 0, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %56

17:                                               ; preds = %2
  %18 = load i32, ptr %7, align 4, !tbaa !5
  %19 = zext i32 %18 to i64
  %20 = getelementptr inbounds [4 x ptr], ptr @cb_cfg_buffer_ptr, i64 0, i64 %19
  %21 = load ptr, ptr %20, align 8, !tbaa !9
  %22 = getelementptr inbounds %struct.cbCFGBUFF, ptr %21, i32 0, i32 11
  %23 = load i32, ptr %4, align 4, !tbaa !5
  %24 = sub i32 %23, 1
  %25 = zext i32 %24 to i64
  %26 = getelementptr inbounds [596 x %struct.cbPKT_CHANINFO], ptr %22, i64 0, i64 %25
  %27 = getelementptr inbounds %struct.cbPKT_CHANINFO, ptr %26, i32 0, i32 0
  %28 = getelementptr inbounds %struct.cbPKT_HEADER, ptr %27, i32 0, i32 4
  %29 = load i8, ptr %28, align 1, !tbaa !11
  %30 = zext i8 %29 to i32
  %31 = icmp slt i32 %30, 3
  br i1 %31, label %32, label %47

32:                                               ; preds = %17
  %33 = load i32, ptr %7, align 4, !tbaa !5
  %34 = zext i32 %33 to i64
  %35 = getelementptr inbounds [4 x ptr], ptr @cb_cfg_buffer_ptr, i64 0, i64 %34
  %36 = load ptr, ptr %35, align 8, !tbaa !9
  %37 = getelementptr inbounds %struct.cbCFGBUFF, ptr %36, i32 0, i32 11
  %38 = load i32, ptr %4, align 4, !tbaa !5
  %39 = sub i32 %38, 1
  %40 = zext i32 %39 to i64
  %41 = getelementptr inbounds [596 x %struct.cbPKT_CHANINFO], ptr %37, i64 0, i64 %40
  %42 = getelementptr inbounds %struct.cbPKT_CHANINFO, ptr %41, i32 0, i32 0
  %43 = getelementptr inbounds %struct.cbPKT_HEADER, ptr %42, i32 0, i32 4
  %44 = load i8, ptr %43, align 1, !tbaa !11
  %45 = zext i8 %44 to i32
  %46 = add nsw i32 %45, 1
  store i32 %46, ptr %6, align 4, !tbaa !5
  br label %47

47:                                               ; preds = %32, %17
  %48 = load i32, ptr %6, align 4, !tbaa !5
  %49 = sub i32 %48, 1
  %50 = icmp ult i32 %49, 3
  br i1 %50, label %51, label %52

51:                                               ; preds = %47
  br label %54

52:                                               ; preds = %47
  call void @__assert_fail(ptr noundef @.str.20, ptr noundef @.str.2, i32 noundef 1237, ptr noundef @__PRETTY_FUNCTION__._Z19cbGetChanInstrumentjj) #4
  unreachable

53:                                               ; No predecessors!
  br label %54

54:                                               ; preds = %53, %51
  %55 = load i32, ptr %6, align 4, !tbaa !5
  store i32 %55, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %56

56:                                               ; preds = %54, %16
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  %57 = load i32, ptr %3, align 4
  ret i32 %57
}

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 = { nounwind }
attributes #4 = { 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, !7, i64 14}
!12 = !{!"_ZTS14cbPKT_CHANINFO", !13, i64 0, !6, i64 16, !6, i64 20, !6, i64 24, !6, i64 28, !6, i64 32, !6, i64 36, !6, i64 40, !6, i64 44, !6, i64 48, !6, i64 52, !16, i64 56, !17, i64 80, !16, i64 120, !17, i64 144, !7, i64 184, !6, i64 200, !7, i64 204, !16, i64 220, !16, i64 244, !6, i64 268, !6, i64 272, !6, i64 276, !6, i64 280, !7, i64 284, !7, i64 292, !7, i64 293, !7, i64 295, !15, i64 296, !15, i64 298, !6, i64 300, !6, i64 304, !6, i64 308, !6, i64 312, !6, i64 316, !6, i64 320, !6, i64 324, !6, i64 328, !6, i64 332, !6, i64 336, !6, i64 340, !6, i64 344, !6, i64 348, !15, i64 352, !15, i64 354, !6, i64 356, !7, i64 360, !7, i64 520}
!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 = !{!"_ZTS9cbSCALING", !15, i64 0, !15, i64 2, !6, i64 4, !6, i64 8, !6, i64 12, !7, i64 16}
!17 = !{!"_ZTS10cbFILTDESC", !7, i64 0, !6, i64 16, !6, i64 20, !6, i64 24, !6, i64 28, !6, i64 32, !6, i64 36}


; 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_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.cbPKT_HEADER = type { i64, i16, i16, i16, i8, i8 }
%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 }

@.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
@.str.20 = external hidden unnamed_addr constant [29 x i8], align 1
@__PRETTY_FUNCTION__._Z19cbGetChanInstrumentjj = external hidden unnamed_addr constant [49 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
define dso_local noundef range(i32 -2147483647, 4) i32 @_Z19cbGetChanInstrumentjj(i32 noundef %0, i32 noundef %1) local_unnamed_addr #1 {
  %3 = add i32 %0, -1
  %4 = icmp ugt i32 %3, 595
  br i1 %4, label %19, label %5

5:                                                ; preds = %2
  %6 = zext i32 %1 to i64
  %7 = getelementptr inbounds [4 x i32], ptr @cb_library_index, i64 0, i64 %6
  %8 = load i32, ptr %7, align 4, !tbaa !5
  %9 = zext i32 %8 to i64
  %10 = getelementptr inbounds [4 x ptr], ptr @cb_cfg_buffer_ptr, i64 0, i64 %9
  %11 = load ptr, ptr %10, align 8, !tbaa !9
  %12 = getelementptr inbounds i8, ptr %11, i64 49880
  %13 = zext nneg i32 %3 to i64
  %14 = getelementptr inbounds [596 x %struct.cbPKT_CHANINFO], ptr %12, i64 0, i64 %13, i32 0, i32 4
  %15 = load i8, ptr %14, align 1, !tbaa !11
  %16 = icmp ult i8 %15, 3
  %narrow = add nuw nsw i8 %15, 1
  %narrow12 = select i1 %16, i8 %narrow, i8 1
  %.09 = zext nneg i8 %narrow12 to i32
  %17 = icmp ult i8 %narrow12, 4
  br i1 %17, label %19, label %18

18:                                               ; preds = %5
  tail call void @__assert_fail(ptr noundef nonnull @.str.20, ptr noundef nonnull @.str.2, i32 noundef 1237, ptr noundef nonnull @__PRETTY_FUNCTION__._Z19cbGetChanInstrumentjj) #2
  unreachable

19:                                               ; preds = %5, %2
  %.0 = phi i32 [ 0, %2 ], [ %.09, %5 ]
  ret i32 %.0
}

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, !7, i64 14}
!12 = !{!"_ZTS14cbPKT_CHANINFO", !13, i64 0, !6, i64 16, !6, i64 20, !6, i64 24, !6, i64 28, !6, i64 32, !6, i64 36, !6, i64 40, !6, i64 44, !6, i64 48, !6, i64 52, !16, i64 56, !17, i64 80, !16, i64 120, !17, i64 144, !7, i64 184, !6, i64 200, !7, i64 204, !16, i64 220, !16, i64 244, !6, i64 268, !6, i64 272, !6, i64 276, !6, i64 280, !7, i64 284, !7, i64 292, !7, i64 293, !7, i64 295, !15, i64 296, !15, i64 298, !6, i64 300, !6, i64 304, !6, i64 308, !6, i64 312, !6, i64 316, !6, i64 320, !6, i64 324, !6, i64 328, !6, i64 332, !6, i64 336, !6, i64 340, !6, i64 344, !6, i64 348, !15, i64 352, !15, i64 354, !6, i64 356, !7, i64 360, !7, i64 520}
!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 = !{!"_ZTS9cbSCALING", !15, i64 0, !15, i64 2, !6, i64 4, !6, i64 8, !6, i64 12, !7, i64 16}
!17 = !{!"_ZTS10cbFILTDESC", !7, i64 0, !6, i64 16, !6, i64 20, !6, i64 24, !6, i64 28, !6, i64 32, !6, i64 36}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,SROAPass,SimplifyCFGPass,TailCallElimPass