; LLVM IR from ctlst-tech/catpilot :: bsp/ics/cubeio (cubeio)

; 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.cubeio_t = type { [32 x i8], ptr, %struct.cubeio_interface_t, i32, %struct.cubeio_sync_t, %struct.cubeio_packet_t, %struct.cubeio_packet_t, i32, %struct.cubeio_page_config_t, %struct.cubeio_pwm_out_t, %struct.cubeio_pwm_in_t, %struct.cubeio_rate_t, %struct.cubeio_page_reg_status_t, %struct.cubeio_page_rc_input_t, [16 x %struct.cubeio_range_cfg_t], [16 x %struct.cubeio_range_cfg_t], [16 x double], double, [16 x double], i32 }
%struct.cubeio_interface_t = type { ptr }
%struct.cubeio_sync_t = type { ptr, ptr, ptr, i32, i32, i32, i32, i32 }
%struct.cubeio_packet_t = type { i8, i8, i8, i8, [22 x i16] }
%struct.cubeio_page_config_t = type { i16, i16 }
%struct.cubeio_pwm_out_t = type { i8, [16 x i16], [16 x i16], i8, i16 }
%struct.cubeio_pwm_in_t = type { [16 x i16] }
%struct.cubeio_rate_t = type { i16, i16, i16, i16, i8, i8 }
%struct.cubeio_page_reg_status_t = type { i16, i32, i16, i16, i32, i32, i8, i8, i8, i8, i8, i8, i8 }
%struct.cubeio_page_rc_input_t = type { i8, i8, i8, [16 x i16], i16 }
%struct.cubeio_range_cfg_t = type { i16, i16, i16 }

@__const.cubeio_set_freq.ch_masks = external hidden unnamed_addr constant [3 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: 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: nounwind uwtable
declare hidden void @cubeio_push_event(ptr noundef, i32 noundef) #2

; Function Attrs: nounwind uwtable
define dso_local void @cubeio_set_freq(ptr noundef %0, i16 noundef zeroext %1, i16 noundef zeroext %2) #2 {
  %4 = alloca ptr, align 8
  %5 = alloca i16, align 2
  %6 = alloca i16, align 2
  %7 = alloca [3 x i8], align 1
  %8 = alloca i64, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i16 %1, ptr %5, align 2, !tbaa !9
  store i16 %2, ptr %6, align 2, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 3, ptr %7) #3
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %7, ptr align 1 @__const.cubeio_set_freq.ch_masks, i64 3, i1 false)
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  store i64 0, ptr %8, align 8, !tbaa !11
  br label %9

9:                                                ; preds = %32, %3
  %10 = load i64, ptr %8, align 8, !tbaa !11
  %11 = icmp ult i64 %10, 3
  br i1 %11, label %13, label %12

12:                                               ; preds = %9
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  br label %35

13:                                               ; preds = %9
  %14 = load i16, ptr %5, align 2, !tbaa !9
  %15 = zext i16 %14 to i32
  %16 = load i64, ptr %8, align 8, !tbaa !11
  %17 = getelementptr inbounds [3 x i8], ptr %7, i64 0, i64 %16
  %18 = load i8, ptr %17, align 1, !tbaa !13
  %19 = zext i8 %18 to i32
  %20 = and i32 %15, %19
  %21 = icmp ne i32 %20, 0
  br i1 %21, label %22, label %31

22:                                               ; preds = %13
  %23 = load i64, ptr %8, align 8, !tbaa !11
  %24 = getelementptr inbounds [3 x i8], ptr %7, i64 0, i64 %23
  %25 = load i8, ptr %24, align 1, !tbaa !13
  %26 = zext i8 %25 to i32
  %27 = load i16, ptr %5, align 2, !tbaa !9
  %28 = zext i16 %27 to i32
  %29 = or i32 %28, %26
  %30 = trunc i32 %29 to i16
  store i16 %30, ptr %5, align 2, !tbaa !9
  br label %31

31:                                               ; preds = %22, %13
  br label %32

32:                                               ; preds = %31
  %33 = load i64, ptr %8, align 8, !tbaa !11
  %34 = add i64 %33, 1
  store i64 %34, ptr %8, align 8, !tbaa !11
  br label %9, !llvm.loop !14

35:                                               ; preds = %12
  %36 = load i16, ptr %6, align 2, !tbaa !9
  %37 = load ptr, ptr %4, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.cubeio_t, ptr %37, i32 0, i32 11
  %39 = getelementptr inbounds %struct.cubeio_rate_t, ptr %38, i32 0, i32 0
  store i16 %36, ptr %39, align 2, !tbaa !16
  %40 = load i16, ptr %5, align 2, !tbaa !9
  %41 = load ptr, ptr %4, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.cubeio_t, ptr %41, i32 0, i32 11
  %43 = getelementptr inbounds %struct.cubeio_rate_t, ptr %42, i32 0, i32 1
  store i16 %40, ptr %43, align 2, !tbaa !29
  %44 = load ptr, ptr %4, align 8, !tbaa !5
  call void @cubeio_push_event(ptr noundef %44, i32 noundef 8)
  call void @llvm.lifetime.end.p0(i64 3, ptr %7) #3
  ret void
}

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

!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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"short", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!17, !10, i64 310}
!17 = !{!"", !7, i64 0, !6, i64 32, !18, i64 40, !19, i64 48, !20, i64 56, !21, i64 104, !21, i64 152, !19, i64 200, !22, i64 204, !23, i64 208, !24, i64 278, !25, i64 310, !26, i64 320, !27, i64 348, !7, i64 386, !7, i64 482, !7, i64 584, !28, i64 712, !7, i64 720, !19, i64 848}
!18 = !{!"", !6, i64 0}
!19 = !{!"int", !7, i64 0}
!20 = !{!"", !6, i64 0, !6, i64 8, !6, i64 16, !19, i64 24, !19, i64 28, !19, i64 32, !19, i64 36, !19, i64 40}
!21 = !{!"", !7, i64 0, !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 3, !7, i64 4}
!22 = !{!"", !10, i64 0, !10, i64 2}
!23 = !{!"", !7, i64 0, !7, i64 2, !7, i64 34, !7, i64 66, !10, i64 68}
!24 = !{!"", !7, i64 0}
!25 = !{!"", !10, i64 0, !10, i64 2, !10, i64 4, !10, i64 6, !7, i64 8, !7, i64 9}
!26 = !{!"", !10, i64 0, !19, i64 4, !10, i64 8, !10, i64 10, !19, i64 12, !19, i64 16, !7, i64 20, !7, i64 21, !7, i64 22, !7, i64 23, !7, i64 24, !7, i64 25, !7, i64 26}
!27 = !{!"", !7, i64 0, !7, i64 1, !7, i64 1, !7, i64 2, !7, i64 4, !10, i64 36}
!28 = !{!"double", !7, i64 0}
!29 = !{!17, !10, i64 312}


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

@__const.cubeio_set_freq.ch_masks = external hidden unnamed_addr constant [3 x i8], align 1

; Function Attrs: nounwind uwtable
declare hidden void @cubeio_push_event(ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @cubeio_set_freq(ptr noundef %0, i16 noundef zeroext %1, i16 noundef zeroext %2) local_unnamed_addr #0 {
  %4 = load i8, ptr @__const.cubeio_set_freq.ch_masks, align 1, !tbaa !5
  %5 = zext i8 %4 to i16
  %6 = and i16 %5, %1
  %.not = icmp eq i16 %6, 0
  %7 = select i1 %.not, i16 0, i16 %5
  %.1 = or i16 %7, %1
  %8 = load i8, ptr getelementptr inbounds (i8, ptr @__const.cubeio_set_freq.ch_masks, i64 1), align 1, !tbaa !5
  %9 = zext i8 %8 to i16
  %10 = and i16 %.1, %9
  %.not.1 = icmp eq i16 %10, 0
  %11 = select i1 %.not.1, i16 0, i16 %9
  %.1.1 = or i16 %11, %.1
  %12 = load i8, ptr getelementptr inbounds (i8, ptr @__const.cubeio_set_freq.ch_masks, i64 2), align 1, !tbaa !5
  %13 = zext i8 %12 to i16
  %14 = and i16 %.1.1, %13
  %.not.2 = icmp eq i16 %14, 0
  %15 = select i1 %.not.2, i16 0, i16 %13
  %.1.2 = or i16 %15, %.1.1
  %16 = getelementptr inbounds i8, ptr %0, i64 310
  store i16 %2, ptr %16, align 2, !tbaa !8
  %17 = getelementptr inbounds i8, ptr %0, i64 312
  store i16 %.1.2, ptr %17, align 2, !tbaa !23
  tail call void @cubeio_push_event(ptr noundef %0, i32 noundef 8)
  ret void
}

attributes #0 = { 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" }

!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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !16, i64 310}
!9 = !{!"", !6, i64 0, !10, i64 32, !11, i64 40, !12, i64 48, !13, i64 56, !14, i64 104, !14, i64 152, !12, i64 200, !15, i64 204, !17, i64 208, !18, i64 278, !19, i64 310, !20, i64 320, !21, i64 348, !6, i64 386, !6, i64 482, !6, i64 584, !22, i64 712, !6, i64 720, !12, i64 848}
!10 = !{!"any pointer", !6, i64 0}
!11 = !{!"", !10, i64 0}
!12 = !{!"int", !6, i64 0}
!13 = !{!"", !10, i64 0, !10, i64 8, !10, i64 16, !12, i64 24, !12, i64 28, !12, i64 32, !12, i64 36, !12, i64 40}
!14 = !{!"", !6, i64 0, !6, i64 0, !6, i64 1, !6, i64 2, !6, i64 3, !6, i64 4}
!15 = !{!"", !16, i64 0, !16, i64 2}
!16 = !{!"short", !6, i64 0}
!17 = !{!"", !6, i64 0, !6, i64 2, !6, i64 34, !6, i64 66, !16, i64 68}
!18 = !{!"", !6, i64 0}
!19 = !{!"", !16, i64 0, !16, i64 2, !16, i64 4, !16, i64 6, !6, i64 8, !6, i64 9}
!20 = !{!"", !16, i64 0, !12, i64 4, !16, i64 8, !16, i64 10, !12, i64 12, !12, i64 16, !6, i64 20, !6, i64 21, !6, i64 22, !6, i64 23, !6, i64 24, !6, i64 25, !6, i64 26}
!21 = !{!"", !6, i64 0, !6, i64 1, !6, i64 1, !6, i64 2, !6, i64 4, !16, i64 36}
!22 = !{!"double", !6, i64 0}
!23 = !{!9, !16, i64 312}


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