; LLVM IR from CerebusOSS/CereLink :: Central/Instrument.ll (ZN10Instrument15LoopbackRecvLowEPvS0_j)

; 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.cbPKT_GENERIC = type { %struct.cbPKT_HEADER, [252 x i32] }
%struct.cbPKT_HEADER = type { i64, i16, i16, i16, i8, i8 }
%struct.cbXMTBUFF = type { i32, i32, i32, i32, i32, [0 x i32] }

@cb_library_index = external global [4 x i32], align 16
@cb_xmt_local_buffer_ptr = external global [4 x ptr], 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 @_ZN10Instrument15LoopbackRecvLowEPvS0_j(ptr noundef nonnull align 8 dereferenceable(12516) %0, ptr noundef %1, ptr noundef %2, i32 noundef %3) #1 align 2 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !5
  store i32 %3, ptr %8, align 4, !tbaa !9
  %13 = load ptr, ptr %5, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  %14 = load i32, ptr %8, align 4, !tbaa !9
  %15 = zext i32 %14 to i64
  %16 = getelementptr inbounds [4 x i32], ptr @cb_library_index, i64 0, i64 %15
  %17 = load i32, ptr %16, align 4, !tbaa !9
  store i32 %17, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %18 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %18, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #4
  %19 = load ptr, ptr %10, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.cbPKT_GENERIC, ptr %19, i32 0, i32 0
  %21 = getelementptr inbounds %struct.cbPKT_HEADER, ptr %20, i32 0, i32 3
  %22 = load i16, ptr %21, align 1, !tbaa !11
  %23 = zext i16 %22 to i64
  %24 = add i64 %23, 4
  %25 = trunc i64 %24 to i32
  store i32 %25, ptr %11, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #4
  %26 = load i32, ptr %11, align 4, !tbaa !9
  %27 = shl i32 %26, 2
  store i32 %27, ptr %12, align 4, !tbaa !9
  %28 = load ptr, ptr %6, align 8, !tbaa !5
  %29 = load ptr, ptr %10, align 8, !tbaa !5
  %30 = load i32, ptr %12, align 4, !tbaa !9
  %31 = sext i32 %30 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %28, ptr align 1 %29, i64 %31, i1 false)
  %32 = load ptr, ptr %10, align 8, !tbaa !5
  %33 = load i32, ptr %12, align 4, !tbaa !9
  %34 = sext i32 %33 to i64
  call void @llvm.memset.p0.i64(ptr align 1 %32, i8 0, i64 %34, i1 false)
  %35 = load i32, ptr %9, align 4, !tbaa !9
  %36 = zext i32 %35 to i64
  %37 = getelementptr inbounds [4 x ptr], ptr @cb_xmt_local_buffer_ptr, i64 0, i64 %36
  %38 = load ptr, ptr %37, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.cbXMTBUFF, ptr %38, i32 0, i32 0
  %40 = load i32, ptr %39, align 1, !tbaa !16
  %41 = add i32 %40, 1
  store i32 %41, ptr %39, align 1, !tbaa !16
  %42 = load i32, ptr %11, align 4, !tbaa !9
  %43 = load i32, ptr %9, align 4, !tbaa !9
  %44 = zext i32 %43 to i64
  %45 = getelementptr inbounds [4 x ptr], ptr @cb_xmt_local_buffer_ptr, i64 0, i64 %44
  %46 = load ptr, ptr %45, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.cbXMTBUFF, ptr %46, i32 0, i32 2
  %48 = load i32, ptr %47, align 1, !tbaa !18
  %49 = add i32 %48, %42
  store i32 %49, ptr %47, align 1, !tbaa !18
  %50 = load i32, ptr %9, align 4, !tbaa !9
  %51 = zext i32 %50 to i64
  %52 = getelementptr inbounds [4 x ptr], ptr @cb_xmt_local_buffer_ptr, i64 0, i64 %51
  %53 = load ptr, ptr %52, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.cbXMTBUFF, ptr %53, i32 0, i32 2
  %55 = load i32, ptr %54, align 1, !tbaa !18
  %56 = load i32, ptr %9, align 4, !tbaa !9
  %57 = zext i32 %56 to i64
  %58 = getelementptr inbounds [4 x ptr], ptr @cb_xmt_local_buffer_ptr, i64 0, i64 %57
  %59 = load ptr, ptr %58, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.cbXMTBUFF, ptr %59, i32 0, i32 4
  %61 = load i32, ptr %60, align 1, !tbaa !19
  %62 = sub i32 %61, 14520
  %63 = icmp ugt i32 %55, %62
  br i1 %63, label %64, label %70

64:                                               ; preds = %4
  %65 = load i32, ptr %9, align 4, !tbaa !9
  %66 = zext i32 %65 to i64
  %67 = getelementptr inbounds [4 x ptr], ptr @cb_xmt_local_buffer_ptr, i64 0, i64 %66
  %68 = load ptr, ptr %67, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.cbXMTBUFF, ptr %68, i32 0, i32 2
  store i32 0, ptr %69, align 1, !tbaa !18
  br label %70

70:                                               ; preds = %64, %4
  %71 = load i32, ptr %12, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  ret i32 %71
}

; 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

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

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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #4 = { 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 = !{!"any pointer", !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, !15, i64 12}
!12 = !{!"_ZTS13cbPKT_GENERIC", !13, i64 0, !7, i64 16}
!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 = !{!17, !10, i64 0}
!17 = !{!"_ZTS9cbXMTBUFF", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12, !10, i64 16, !7, i64 20}
!18 = !{!17, !10, i64 8}
!19 = !{!17, !10, i64 16}


; 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"

@cb_library_index = external local_unnamed_addr global [4 x i32], align 16
@cb_xmt_local_buffer_ptr = external local_unnamed_addr global [4 x ptr], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(readwrite, inaccessiblemem: none) uwtable
define dso_local noundef range(i32 16, 262157) i32 @_ZN10Instrument15LoopbackRecvLowEPvS0_j(ptr nocapture noundef nonnull readnone align 8 dereferenceable(12516) %0, ptr nocapture noundef writeonly %1, ptr nocapture noundef %2, i32 noundef %3) local_unnamed_addr #0 align 2 {
  %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 = getelementptr inbounds i8, ptr %2, i64 12
  %9 = load i16, ptr %8, align 1, !tbaa !9
  %10 = zext i16 %9 to i32
  %11 = add nuw nsw i32 %10, 4
  %12 = shl nuw nsw i32 %11, 2
  %13 = zext nneg i32 %12 to i64
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(1) %1, ptr noundef nonnull align 1 dereferenceable(1) %2, i64 %13, i1 false)
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 1 dereferenceable(1) %2, i8 0, i64 %13, i1 false)
  %14 = zext i32 %7 to i64
  %15 = getelementptr inbounds [4 x ptr], ptr @cb_xmt_local_buffer_ptr, i64 0, i64 %14
  %16 = load ptr, ptr %15, align 8, !tbaa !14
  %17 = load i32, ptr %16, align 1, !tbaa !16
  %18 = add i32 %17, 1
  store i32 %18, ptr %16, align 1, !tbaa !16
  %19 = getelementptr inbounds i8, ptr %16, i64 8
  %20 = load i32, ptr %19, align 1, !tbaa !18
  %21 = add i32 %20, %11
  %22 = getelementptr inbounds i8, ptr %16, i64 16
  %23 = load i32, ptr %22, align 1, !tbaa !19
  %24 = add i32 %23, -14520
  %25 = icmp ugt i32 %21, %24
  %spec.store.select = select i1 %25, i32 0, i32 %21
  store i32 %spec.store.select, ptr %19, align 1
  ret i32 %12
}

; 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

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

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) }
attributes #2 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }

!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, !13, i64 12}
!10 = !{!"_ZTS13cbPKT_GENERIC", !11, i64 0, !7, i64 16}
!11 = !{!"_ZTS12cbPKT_HEADER", !12, i64 0, !13, i64 8, !13, i64 10, !13, i64 12, !7, i64 14, !7, i64 15}
!12 = !{!"long", !7, i64 0}
!13 = !{!"short", !7, i64 0}
!14 = !{!15, !15, i64 0}
!15 = !{!"any pointer", !7, i64 0}
!16 = !{!17, !6, i64 0}
!17 = !{!"_ZTS9cbXMTBUFF", !6, i64 0, !6, i64 4, !6, i64 8, !6, i64 12, !6, i64 16, !7, i64 20}
!18 = !{!17, !6, i64 8}
!19 = !{!17, !6, i64 16}


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