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

; 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] }

@cb_library_index = external global [4 x i32], align 16
@cb_cfg_buffer_ptr = external global [4 x ptr], align 16
@cb_library_initialized = external global [4 x i32], align 16

; 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 noundef i32 @_Z22cbGetNTrodeUnitMappingjtP19cbMANUALUNITMAPPINGj(i32 noundef %0, i16 noundef zeroext %1, ptr noundef %2, i32 noundef %3) #1 {
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i16, align 2
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store i32 %0, ptr %6, align 4, !tbaa !5
  store i16 %1, ptr %7, align 2, !tbaa !9
  store ptr %2, ptr %8, align 8, !tbaa !11
  store i32 %3, ptr %9, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  %12 = load i32, ptr %9, align 4, !tbaa !5
  %13 = zext i32 %12 to i64
  %14 = getelementptr inbounds [4 x i32], ptr @cb_library_index, i64 0, i64 %13
  %15 = load i32, ptr %14, align 4, !tbaa !5
  store i32 %15, ptr %10, align 4, !tbaa !5
  %16 = load i32, ptr %10, align 4, !tbaa !5
  %17 = zext i32 %16 to i64
  %18 = getelementptr inbounds [4 x i32], ptr @cb_library_initialized, i64 0, i64 %17
  %19 = load i32, ptr %18, align 4, !tbaa !5
  %20 = icmp ne i32 %19, 0
  br i1 %20, label %22, label %21

21:                                               ; preds = %4
  store i32 1, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %68

22:                                               ; preds = %4
  %23 = load i32, ptr %6, align 4, !tbaa !5
  %24 = sub i32 %23, 1
  %25 = icmp uge i32 %24, 280
  br i1 %25, label %26, label %27

26:                                               ; preds = %22
  store i32 13, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %68

27:                                               ; preds = %22
  %28 = load i16, ptr %7, align 2, !tbaa !9
  %29 = zext i16 %28 to i32
  %30 = icmp sge i32 %29, 6
  br i1 %30, label %31, label %32

31:                                               ; preds = %27
  store i32 7, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %68

32:                                               ; preds = %27
  %33 = load i32, ptr %10, 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 !11
  %37 = getelementptr inbounds %struct.cbCFGBUFF, ptr %36, i32 0, i32 13
  %38 = load i32, ptr %6, align 4, !tbaa !5
  %39 = sub i32 %38, 1
  %40 = zext i32 %39 to i64
  %41 = getelementptr inbounds [280 x %struct.cbPKT_NTRODEINFO], ptr %37, i64 0, i64 %40
  %42 = getelementptr inbounds %struct.cbPKT_NTRODEINFO, ptr %41, i32 0, i32 0
  %43 = getelementptr inbounds %struct.cbPKT_HEADER, ptr %42, i32 0, i32 1
  %44 = load i16, ptr %43, align 1, !tbaa !13
  %45 = zext i16 %44 to i32
  %46 = icmp eq i32 %45, 0
  br i1 %46, label %47, label %48

47:                                               ; preds = %32
  store i32 13, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %68

48:                                               ; preds = %32
  %49 = load ptr, ptr %8, align 8, !tbaa !11
  %50 = icmp ne ptr %49, null
  br i1 %50, label %51, label %67

51:                                               ; preds = %48
  %52 = load ptr, ptr %8, align 8, !tbaa !11
  %53 = load i32, ptr %10, align 4, !tbaa !5
  %54 = zext i32 %53 to i64
  %55 = getelementptr inbounds [4 x ptr], ptr @cb_cfg_buffer_ptr, i64 0, i64 %54
  %56 = load ptr, ptr %55, align 8, !tbaa !11
  %57 = getelementptr inbounds %struct.cbCFGBUFF, ptr %56, i32 0, i32 13
  %58 = load i32, ptr %6, align 4, !tbaa !5
  %59 = sub i32 %58, 1
  %60 = zext i32 %59 to i64
  %61 = getelementptr inbounds [280 x %struct.cbPKT_NTRODEINFO], ptr %57, i64 0, i64 %60
  %62 = getelementptr inbounds %struct.cbPKT_NTRODEINFO, ptr %61, i32 0, i32 3
  %63 = load i16, ptr %7, align 2, !tbaa !9
  %64 = zext i16 %63 to i64
  %65 = getelementptr inbounds [6 x [5 x %struct.cbMANUALUNITMAPPING]], ptr %62, i64 0, i64 %64
  %66 = getelementptr inbounds [5 x %struct.cbMANUALUNITMAPPING], ptr %65, i64 0, i64 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %52, ptr align 1 %66, i64 160, i1 false)
  br label %67

67:                                               ; preds = %51, %48
  store i32 0, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %68

68:                                               ; preds = %67, %47, %31, %26, %21
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  %69 = load i32, ptr %5, align 4
  ret i32 %69
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 = !{!"short", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!14, !10, i64 8}
!14 = !{!"_ZTS16cbPKT_NTRODEINFO", !15, i64 0, !6, i64 16, !7, i64 20, !7, i64 36, !10, i64 996, !10, i64 998, !7, i64 1000}
!15 = !{!"_ZTS12cbPKT_HEADER", !16, i64 0, !10, i64 8, !10, i64 10, !10, i64 12, !7, i64 14, !7, i64 15}
!16 = !{!"long", !7, i64 0}


; 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_NTRODEINFO = type { %struct.cbPKT_HEADER, i32, [16 x i8], [6 x [5 x %struct.cbMANUALUNITMAPPING]], i16, i16, [4 x i16] }
%struct.cbPKT_HEADER = type { i64, i16, i16, i16, i8, i8 }
%struct.cbMANUALUNITMAPPING = type { i16, [3 x i16], [3 x [3 x i16]], i16, i32 }

@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_library_initialized = external local_unnamed_addr global [4 x i32], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(readwrite, inaccessiblemem: none) uwtable
define dso_local noundef range(i32 0, 14) i32 @_Z22cbGetNTrodeUnitMappingjtP19cbMANUALUNITMAPPINGj(i32 noundef %0, i16 noundef zeroext %1, ptr noundef writeonly %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = zext i32 %3 to i64
  %6 = getelementptr inbounds [4 x i32], ptr @cb_library_index, i64 0, i64 %5
  %7 = load i32, ptr %6, align 4, !tbaa !5
  %8 = zext i32 %7 to i64
  %9 = getelementptr inbounds [4 x i32], ptr @cb_library_initialized, i64 0, i64 %8
  %10 = load i32, ptr %9, align 4, !tbaa !5
  %.not = icmp eq i32 %10, 0
  br i1 %.not, label %30, label %11

11:                                               ; preds = %4
  %12 = add i32 %0, -1
  %13 = icmp ugt i32 %12, 279
  br i1 %13, label %30, label %14

14:                                               ; preds = %11
  %15 = icmp ugt i16 %1, 5
  br i1 %15, label %30, label %16

16:                                               ; preds = %14
  %17 = getelementptr inbounds [4 x ptr], ptr @cb_cfg_buffer_ptr, i64 0, i64 %8
  %18 = load ptr, ptr %17, align 8, !tbaa !9
  %19 = getelementptr inbounds i8, ptr %18, i64 1845184
  %20 = zext nneg i32 %12 to i64
  %21 = getelementptr inbounds [280 x %struct.cbPKT_NTRODEINFO], ptr %19, i64 0, i64 %20
  %22 = getelementptr inbounds i8, ptr %21, i64 8
  %23 = load i16, ptr %22, align 1, !tbaa !11
  %24 = icmp eq i16 %23, 0
  br i1 %24, label %30, label %25

25:                                               ; preds = %16
  %.not13 = icmp eq ptr %2, null
  br i1 %.not13, label %30, label %26

26:                                               ; preds = %25
  %27 = getelementptr inbounds i8, ptr %21, i64 36
  %28 = zext nneg i16 %1 to i64
  %29 = getelementptr inbounds [6 x [5 x %struct.cbMANUALUNITMAPPING]], ptr %27, i64 0, i64 %28
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(160) %2, ptr noundef nonnull align 1 dereferenceable(160) %29, i64 160, i1 false)
  br label %30

30:                                               ; preds = %25, %26, %16, %14, %11, %4
  %.0 = phi i32 [ 1, %4 ], [ 13, %11 ], [ 7, %14 ], [ 13, %16 ], [ 0, %26 ], [ 0, %25 ]
  ret i32 %.0
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(readwrite, 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" }
attributes #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }

!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, !15, i64 8}
!12 = !{!"_ZTS16cbPKT_NTRODEINFO", !13, i64 0, !6, i64 16, !7, i64 20, !7, i64 36, !15, i64 996, !15, i64 998, !7, i64 1000}
!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}


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