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

; 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.cbPROCINFO = type { i32, [64 x i8], i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }

@.str.2 = external hidden unnamed_addr constant [75 x i8], align 1
@.str.20 = external hidden unnamed_addr constant [29 x i8], align 1
@__PRETTY_FUNCTION__._Z26cbGetExpandedChannelNumberjj = external hidden unnamed_addr constant [56 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

declare noundef i32 @_Z13cbGetProcInfojP10cbPROCINFOj(i32 noundef, ptr noundef, i32 noundef) #2

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z26cbGetExpandedChannelNumberjj(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 %struct.cbPROCINFO, align 1
  %8 = alloca i32, align 4
  %9 = 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) #5
  store i32 0, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 108, ptr %7) #5
  %10 = load i32, ptr %5, align 4, !tbaa !5
  %11 = sub i32 %10, 1
  %12 = icmp uge i32 %11, 596
  br i1 %12, label %13, label %14

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

14:                                               ; preds = %2
  %15 = load i32, ptr %4, align 4, !tbaa !5
  %16 = sub i32 %15, 1
  %17 = icmp ult i32 %16, 3
  br i1 %17, label %18, label %19

18:                                               ; preds = %14
  br label %21

19:                                               ; preds = %14
  call void @__assert_fail(ptr noundef @.str.20, ptr noundef @.str.2, i32 noundef 1208, ptr noundef @__PRETTY_FUNCTION__._Z26cbGetExpandedChannelNumberjj) #6
  unreachable

20:                                               ; No predecessors!
  br label %21

21:                                               ; preds = %20, %18
  %22 = load i32, ptr %4, align 4, !tbaa !5
  %23 = icmp ugt i32 %22, 3
  br i1 %23, label %24, label %26

24:                                               ; preds = %21
  %25 = load i32, ptr %5, align 4, !tbaa !5
  store i32 %25, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %53

26:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #5
  store i32 1, ptr %9, align 4, !tbaa !5
  br label %27

27:                                               ; preds = %46, %26
  %28 = load i32, ptr %9, align 4, !tbaa !5
  %29 = icmp ule i32 %28, 3
  br i1 %29, label %31, label %30

30:                                               ; preds = %27
  store i32 2, ptr %8, align 4
  br label %49

31:                                               ; preds = %27
  %32 = load i32, ptr %4, align 4, !tbaa !5
  %33 = load i32, ptr %9, align 4, !tbaa !5
  %34 = icmp eq i32 %32, %33
  br i1 %34, label %35, label %39

35:                                               ; preds = %31
  %36 = load i32, ptr %5, align 4, !tbaa !5
  %37 = load i32, ptr %6, align 4, !tbaa !5
  %38 = add i32 %36, %37
  store i32 %38, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %49

39:                                               ; preds = %31
  call void @llvm.memset.p0.i64(ptr align 1 %7, i8 0, i64 108, i1 false)
  %40 = load i32, ptr %9, align 4, !tbaa !5
  %41 = call noundef i32 @_Z13cbGetProcInfojP10cbPROCINFOj(i32 noundef %40, ptr noundef %7, i32 noundef 0)
  %42 = getelementptr inbounds %struct.cbPROCINFO, ptr %7, i32 0, i32 3
  %43 = load i32, ptr %42, align 1, !tbaa !9
  %44 = load i32, ptr %6, align 4, !tbaa !5
  %45 = add i32 %44, %43
  store i32 %45, ptr %6, align 4, !tbaa !5
  br label %46

46:                                               ; preds = %39
  %47 = load i32, ptr %9, align 4, !tbaa !5
  %48 = add i32 %47, 1
  store i32 %48, ptr %9, align 4, !tbaa !5
  br label %27, !llvm.loop !11

49:                                               ; preds = %35, %30
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #5
  %50 = load i32, ptr %8, align 4
  switch i32 %50, label %53 [
    i32 2, label %51
  ]

51:                                               ; preds = %49
  %52 = load i32, ptr %5, align 4, !tbaa !5
  store i32 %52, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %53

53:                                               ; preds = %51, %49, %24, %13
  call void @llvm.lifetime.end.p0(i64 108, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #5
  %54 = load i32, ptr %3, align 4
  ret i32 %54
}

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

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 = { "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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #5 = { nounwind }
attributes #6 = { 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, !6, i64 72}
!10 = !{!"_ZTS10cbPROCINFO", !6, i64 0, !7, i64 4, !6, i64 68, !6, i64 72, !6, i64 76, !6, i64 80, !6, i64 84, !6, i64 88, !6, i64 92, !6, i64 96, !6, i64 100, !6, i64 104}
!11 = distinct !{!11, !12}
!12 = !{!"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.cbPROCINFO = type { i32, [64 x i8], i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }

@.str.2 = external hidden unnamed_addr constant [75 x i8], align 1
@.str.20 = external hidden unnamed_addr constant [29 x i8], align 1
@__PRETTY_FUNCTION__._Z26cbGetExpandedChannelNumberjj = external hidden unnamed_addr constant [56 x i8], align 1

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: mustprogress 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) local_unnamed_addr #1

declare noundef i32 @_Z13cbGetProcInfojP10cbPROCINFOj(i32 noundef, ptr noundef, i32 noundef) local_unnamed_addr #2

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z26cbGetExpandedChannelNumberjj(i32 noundef %0, i32 noundef %1) local_unnamed_addr #3 {
  %3 = alloca %struct.cbPROCINFO, align 1
  call void @llvm.lifetime.start.p0(i64 108, ptr nonnull %3) #5
  %4 = add i32 %1, -597
  %5 = icmp ult i32 %4, -596
  br i1 %5, label %.loopexit, label %6

6:                                                ; preds = %2
  %7 = add i32 %0, -1
  %8 = icmp ult i32 %7, 3
  br i1 %8, label %.preheader, label %11

.preheader:                                       ; preds = %6
  %9 = getelementptr inbounds i8, ptr %3, i64 72
  %10 = icmp eq i32 %0, 1
  br i1 %10, label %12, label %14

11:                                               ; preds = %6
  tail call void @__assert_fail(ptr noundef nonnull @.str.20, ptr noundef nonnull @.str.2, i32 noundef 1208, ptr noundef nonnull @__PRETTY_FUNCTION__._Z26cbGetExpandedChannelNumberjj) #6
  unreachable

12:                                               ; preds = %18, %14, %.preheader
  %.01620.lcssa = phi i32 [ 0, %.preheader ], [ %16, %14 ], [ %21, %18 ]
  %13 = add i32 %.01620.lcssa, %1
  br label %.loopexit

14:                                               ; preds = %.preheader
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 1 dereferenceable(108) %3, i8 0, i64 108, i1 false)
  %15 = call noundef i32 @_Z13cbGetProcInfojP10cbPROCINFOj(i32 noundef 1, ptr noundef nonnull %3, i32 noundef 0)
  %16 = load i32, ptr %9, align 1, !tbaa !5
  %17 = icmp eq i32 %0, 2
  br i1 %17, label %12, label %18

18:                                               ; preds = %14
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 1 dereferenceable(108) %3, i8 0, i64 108, i1 false)
  %19 = call noundef i32 @_Z13cbGetProcInfojP10cbPROCINFOj(i32 noundef 2, ptr noundef nonnull %3, i32 noundef 0)
  %20 = load i32, ptr %9, align 1, !tbaa !5
  %21 = add i32 %20, %16
  br label %12

.loopexit:                                        ; preds = %12, %2
  %.1 = phi i32 [ 0, %2 ], [ %13, %12 ]
  call void @llvm.lifetime.end.p0(i64 108, ptr nonnull %3) #5
  ret i32 %.1
}

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

attributes #0 = { mustprogress 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 = { "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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #5 = { nounwind }
attributes #6 = { 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, !7, i64 72}
!6 = !{!"_ZTS10cbPROCINFO", !7, i64 0, !8, i64 4, !7, i64 68, !7, i64 72, !7, i64 76, !7, i64 80, !7, i64 84, !7, i64 88, !7, i64 92, !7, i64 96, !7, i64 100, !7, i64 104}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}


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