; LLVM IR from lichen-community-systems/signaletic :: hosts/daisy/vendor/libDaisy/src/util (usbh_diskio)

; 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._USBH_HandleTypeDef = type { i32, i32, i32, %struct.USBH_CtrlTypeDef, %struct.USBH_DeviceTypeDef, [1 x ptr], ptr, i32, [16 x i32], i32, i32, i8, ptr, ptr }
%struct.USBH_CtrlTypeDef = type { i8, i8, i8, ptr, i16, i16, %union._USB_Setup, i32, i8 }
%union._USB_Setup = type { [2 x i32] }
%struct.USBH_DeviceTypeDef = type { [256 x i8], [512 x i8], i8, i8, i8, i8, i8, i8, i8, i8, i8, %struct._DeviceDescriptor, %struct._ConfigurationDescriptor }
%struct._DeviceDescriptor = type { i8, i8, i16, i8, i8, i8, i8, i16, i16, i16, i8, i8, i8, i8 }
%struct._ConfigurationDescriptor = type { i8, i8, i16, i8, i8, i8, i8, i8, [2 x %struct._InterfaceDescriptor] }
%struct._InterfaceDescriptor = type { i8, i8, i8, i8, i8, i8, i8, i8, i8, [2 x %struct._EndpointDescriptor] }
%struct._EndpointDescriptor = type { i8, i8, i8, i8, i16, i8 }
%struct.MSC_LUNTypeDef = type { i32, i32, i32, %struct.SCSI_CapacityTypeDef, %struct.SCSI_SenseTypeDef, %struct.SCSI_StdInquiryDataTypeDef, i8 }
%struct.SCSI_CapacityTypeDef = type { i32, i16 }
%struct.SCSI_SenseTypeDef = type { i8, i8, i8 }
%struct.SCSI_StdInquiryDataTypeDef = type { i8, i8, i8, [9 x i8], [17 x i8], [5 x i8] }

@hUsbHostHS = external global %struct._USBH_HandleTypeDef, align 8

; Function Attrs: nounwind uwtable
define dso_local i32 @USBH_ioctl(i8 noundef zeroext %0, i8 noundef zeroext %1, ptr noundef %2) #0 {
  %4 = alloca i8, align 1
  %5 = alloca i8, align 1
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca %struct.MSC_LUNTypeDef, align 4
  store i8 %0, ptr %4, align 1, !tbaa !5
  store i8 %1, ptr %5, align 1, !tbaa !5
  store ptr %2, ptr %6, align 8, !tbaa !8
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 1, ptr %7, align 4, !tbaa !10
  call void @llvm.lifetime.start.p0(i64 60, ptr %8) #3
  %9 = load i8, ptr %5, align 1, !tbaa !5
  %10 = zext i8 %9 to i32
  switch i32 %10, label %50 [
    i32 0, label %11
    i32 1, label %12
    i32 2, label %24
    i32 3, label %36
  ]

11:                                               ; preds = %3
  store i32 0, ptr %7, align 4, !tbaa !10
  br label %51

12:                                               ; preds = %3
  %13 = load i8, ptr %4, align 1, !tbaa !5
  %14 = call i32 @USBH_MSC_GetLUNInfo(ptr noundef @hUsbHostHS, i8 noundef zeroext %13, ptr noundef %8)
  %15 = icmp eq i32 %14, 0
  br i1 %15, label %16, label %22

16:                                               ; preds = %12
  %17 = getelementptr inbounds %struct.MSC_LUNTypeDef, ptr %8, i32 0, i32 3
  %18 = getelementptr inbounds %struct.SCSI_CapacityTypeDef, ptr %17, i32 0, i32 0
  %19 = load i32, ptr %18, align 4, !tbaa !12
  %20 = zext i32 %19 to i64
  %21 = load ptr, ptr %6, align 8, !tbaa !8
  store i64 %20, ptr %21, align 8, !tbaa !18
  store i32 0, ptr %7, align 4, !tbaa !10
  br label %23

22:                                               ; preds = %12
  store i32 1, ptr %7, align 4, !tbaa !10
  br label %23

23:                                               ; preds = %22, %16
  br label %51

24:                                               ; preds = %3
  %25 = load i8, ptr %4, align 1, !tbaa !5
  %26 = call i32 @USBH_MSC_GetLUNInfo(ptr noundef @hUsbHostHS, i8 noundef zeroext %25, ptr noundef %8)
  %27 = icmp eq i32 %26, 0
  br i1 %27, label %28, label %34

28:                                               ; preds = %24
  %29 = getelementptr inbounds %struct.MSC_LUNTypeDef, ptr %8, i32 0, i32 3
  %30 = getelementptr inbounds %struct.SCSI_CapacityTypeDef, ptr %29, i32 0, i32 1
  %31 = load i16, ptr %30, align 4, !tbaa !20
  %32 = zext i16 %31 to i64
  %33 = load ptr, ptr %6, align 8, !tbaa !8
  store i64 %32, ptr %33, align 8, !tbaa !18
  store i32 0, ptr %7, align 4, !tbaa !10
  br label %35

34:                                               ; preds = %24
  store i32 1, ptr %7, align 4, !tbaa !10
  br label %35

35:                                               ; preds = %34, %28
  br label %51

36:                                               ; preds = %3
  %37 = load i8, ptr %4, align 1, !tbaa !5
  %38 = call i32 @USBH_MSC_GetLUNInfo(ptr noundef @hUsbHostHS, i8 noundef zeroext %37, ptr noundef %8)
  %39 = icmp eq i32 %38, 0
  br i1 %39, label %40, label %48

40:                                               ; preds = %36
  %41 = getelementptr inbounds %struct.MSC_LUNTypeDef, ptr %8, i32 0, i32 3
  %42 = getelementptr inbounds %struct.SCSI_CapacityTypeDef, ptr %41, i32 0, i32 1
  %43 = load i16, ptr %42, align 4, !tbaa !20
  %44 = zext i16 %43 to i32
  %45 = sdiv i32 %44, 512
  %46 = sext i32 %45 to i64
  %47 = load ptr, ptr %6, align 8, !tbaa !8
  store i64 %46, ptr %47, align 8, !tbaa !18
  store i32 0, ptr %7, align 4, !tbaa !10
  br label %49

48:                                               ; preds = %36
  store i32 1, ptr %7, align 4, !tbaa !10
  br label %49

49:                                               ; preds = %48, %40
  br label %51

50:                                               ; preds = %3
  store i32 4, ptr %7, align 4, !tbaa !10
  br label %51

51:                                               ; preds = %50, %49, %35, %23, %11
  %52 = load i32, ptr %7, align 4, !tbaa !10
  call void @llvm.lifetime.end.p0(i64 60, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  ret i32 %52
}

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

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

declare i32 @USBH_MSC_GetLUNInfo(ptr noundef, i8 noundef zeroext, ptr noundef) #2

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" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"any pointer", !6, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"int", !6, i64 0}
!12 = !{!13, !11, i64 12}
!13 = !{!"", !11, i64 0, !11, i64 4, !11, i64 8, !14, i64 12, !16, i64 20, !17, i64 23, !6, i64 57}
!14 = !{!"", !11, i64 0, !15, i64 4}
!15 = !{!"short", !6, i64 0}
!16 = !{!"", !6, i64 0, !6, i64 1, !6, i64 2}
!17 = !{!"", !6, i64 0, !6, i64 1, !6, i64 2, !6, i64 3, !6, i64 12, !6, i64 29}
!18 = !{!19, !19, i64 0}
!19 = !{!"long", !6, i64 0}
!20 = !{!13, !15, 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"

%struct._USBH_HandleTypeDef = type { i32, i32, i32, %struct.USBH_CtrlTypeDef, %struct.USBH_DeviceTypeDef, [1 x ptr], ptr, i32, [16 x i32], i32, i32, i8, ptr, ptr }
%struct.USBH_CtrlTypeDef = type { i8, i8, i8, ptr, i16, i16, %union._USB_Setup, i32, i8 }
%union._USB_Setup = type { [2 x i32] }
%struct.USBH_DeviceTypeDef = type { [256 x i8], [512 x i8], i8, i8, i8, i8, i8, i8, i8, i8, i8, %struct._DeviceDescriptor, %struct._ConfigurationDescriptor }
%struct._DeviceDescriptor = type { i8, i8, i16, i8, i8, i8, i8, i16, i16, i16, i8, i8, i8, i8 }
%struct._ConfigurationDescriptor = type { i8, i8, i16, i8, i8, i8, i8, i8, [2 x %struct._InterfaceDescriptor] }
%struct._InterfaceDescriptor = type { i8, i8, i8, i8, i8, i8, i8, i8, i8, [2 x %struct._EndpointDescriptor] }
%struct._EndpointDescriptor = type { i8, i8, i8, i8, i16, i8 }
%struct.MSC_LUNTypeDef = type { i32, i32, i32, %struct.SCSI_CapacityTypeDef, %struct.SCSI_SenseTypeDef, %struct.SCSI_StdInquiryDataTypeDef, i8 }
%struct.SCSI_CapacityTypeDef = type { i32, i16 }
%struct.SCSI_SenseTypeDef = type { i8, i8, i8 }
%struct.SCSI_StdInquiryDataTypeDef = type { i8, i8, i8, [9 x i8], [17 x i8], [5 x i8] }

@hUsbHostHS = external global %struct._USBH_HandleTypeDef, align 8

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 5) i32 @USBH_ioctl(i8 noundef zeroext %0, i8 noundef zeroext %1, ptr nocapture noundef writeonly %2) local_unnamed_addr #0 {
  %4 = alloca %struct.MSC_LUNTypeDef, align 4
  call void @llvm.lifetime.start.p0(i64 60, ptr nonnull %4) #3
  switch i8 %1, label %27 [
    i8 0, label %28
    i8 1, label %5
    i8 2, label %12
    i8 3, label %19
  ]

5:                                                ; preds = %3
  %6 = call i32 @USBH_MSC_GetLUNInfo(ptr noundef nonnull @hUsbHostHS, i8 noundef zeroext %0, ptr noundef nonnull %4) #3
  %7 = icmp eq i32 %6, 0
  br i1 %7, label %8, label %28

8:                                                ; preds = %5
  %9 = getelementptr inbounds i8, ptr %4, i64 12
  %10 = load i32, ptr %9, align 4, !tbaa !5
  %11 = zext i32 %10 to i64
  store i64 %11, ptr %2, align 8, !tbaa !14
  br label %28

12:                                               ; preds = %3
  %13 = call i32 @USBH_MSC_GetLUNInfo(ptr noundef nonnull @hUsbHostHS, i8 noundef zeroext %0, ptr noundef nonnull %4) #3
  %14 = icmp eq i32 %13, 0
  br i1 %14, label %15, label %28

15:                                               ; preds = %12
  %16 = getelementptr inbounds i8, ptr %4, i64 16
  %17 = load i16, ptr %16, align 4, !tbaa !16
  %18 = zext i16 %17 to i64
  store i64 %18, ptr %2, align 8, !tbaa !14
  br label %28

19:                                               ; preds = %3
  %20 = call i32 @USBH_MSC_GetLUNInfo(ptr noundef nonnull @hUsbHostHS, i8 noundef zeroext %0, ptr noundef nonnull %4) #3
  %21 = icmp eq i32 %20, 0
  br i1 %21, label %22, label %28

22:                                               ; preds = %19
  %23 = getelementptr inbounds i8, ptr %4, i64 16
  %24 = load i16, ptr %23, align 4, !tbaa !16
  %25 = lshr i16 %24, 9
  %26 = zext nneg i16 %25 to i64
  store i64 %26, ptr %2, align 8, !tbaa !14
  br label %28

27:                                               ; preds = %3
  br label %28

28:                                               ; preds = %19, %12, %5, %3, %22, %15, %8, %27
  %.0 = phi i32 [ 4, %27 ], [ 0, %22 ], [ 0, %15 ], [ 0, %8 ], [ 0, %3 ], [ 1, %5 ], [ 1, %12 ], [ 1, %19 ]
  call void @llvm.lifetime.end.p0(i64 60, ptr nonnull %4) #3
  ret i32 %.0
}

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

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

declare i32 @USBH_MSC_GetLUNInfo(ptr noundef, i8 noundef zeroext, ptr noundef) local_unnamed_addr #2

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" }
attributes #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 12}
!6 = !{!"", !7, i64 0, !7, i64 4, !7, i64 8, !10, i64 12, !12, i64 20, !13, i64 23, !8, i64 57}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"", !7, i64 0, !11, i64 4}
!11 = !{!"short", !8, i64 0}
!12 = !{!"", !8, i64 0, !8, i64 1, !8, i64 2}
!13 = !{!"", !8, i64 0, !8, i64 1, !8, i64 2, !8, i64 3, !8, i64 12, !8, i64 29}
!14 = !{!15, !15, i64 0}
!15 = !{!"long", !8, i64 0}
!16 = !{!6, !11, i64 16}


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