; LLVM IR from sca-secure-library-sca25519/sca25519 :: libopencm3/lib/usb (usb_dwc_common)

; 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._usbd_device = type { ptr, ptr, ptr, i32, ptr, i16, i8, i8, i16, ptr, ptr, ptr, ptr, %struct.usb_control_state, [4 x %struct.user_control_callback], [8 x [3 x ptr]], [4 x ptr], ptr, ptr, i16, i16, [4 x i8], [4 x i32], i16 }
%struct.usb_control_state = type { i32, %struct.usb_setup_data, ptr, i16, ptr, i8 }
%struct.usb_setup_data = type { i8, i8, i16, i16, i16 }
%struct.user_control_callback = type { ptr, i8, i8 }
%struct._usbd_driver = type { ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, i32, i8, i16 }

; 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: nounwind uwtable
define dso_local zeroext i16 @dwc_ep_read_packet(ptr noundef %0, i8 noundef zeroext %1, ptr noundef %2, i16 noundef zeroext %3) #1 {
  %5 = alloca ptr, align 8
  %6 = alloca i8, align 1
  %7 = alloca ptr, align 8
  %8 = alloca i16, align 2
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i8 %1, ptr %6, align 1, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !5
  store i16 %3, ptr %8, align 2, !tbaa !10
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %12 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %12, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  %13 = load i8, ptr %6, align 1, !tbaa !9
  %14 = load i16, ptr %8, align 2, !tbaa !10
  %15 = zext i16 %14 to i32
  %16 = load ptr, ptr %5, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct._usbd_device, ptr %16, i32 0, i32 23
  %18 = load i16, ptr %17, align 8, !tbaa !12
  %19 = zext i16 %18 to i32
  %20 = icmp slt i32 %15, %19
  br i1 %20, label %21, label %24

21:                                               ; preds = %4
  %22 = load i16, ptr %8, align 2, !tbaa !10
  %23 = zext i16 %22 to i32
  br label %29

24:                                               ; preds = %4
  %25 = load ptr, ptr %5, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct._usbd_device, ptr %25, i32 0, i32 23
  %27 = load i16, ptr %26, align 8, !tbaa !12
  %28 = zext i16 %27 to i32
  br label %29

29:                                               ; preds = %24, %21
  %30 = phi i32 [ %23, %21 ], [ %28, %24 ]
  %31 = trunc i32 %30 to i16
  store i16 %31, ptr %8, align 2, !tbaa !10
  %32 = load i32, ptr %9, align 4, !tbaa !18
  %33 = icmp ne i32 %32, 0
  br i1 %33, label %34, label %63

34:                                               ; preds = %29
  %35 = load ptr, ptr %5, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct._usbd_device, ptr %35, i32 0, i32 18
  %37 = load ptr, ptr %36, align 8, !tbaa !19
  %38 = getelementptr inbounds %struct._usbd_driver, ptr %37, i32 0, i32 11
  %39 = load i32, ptr %38, align 8, !tbaa !20
  %40 = add i32 4096, %39
  %41 = zext i32 %40 to i64
  %42 = inttoptr i64 %41 to ptr
  %43 = load volatile i32, ptr %42, align 4, !tbaa !18
  store i32 %43, ptr %11, align 4, !tbaa !18
  %44 = load ptr, ptr %5, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct._usbd_device, ptr %44, i32 0, i32 23
  %46 = load i16, ptr %45, align 8, !tbaa !12
  %47 = zext i16 %46 to i32
  %48 = icmp slt i32 %47, 4
  br i1 %48, label %49, label %52

49:                                               ; preds = %34
  %50 = load ptr, ptr %5, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct._usbd_device, ptr %50, i32 0, i32 23
  store i16 0, ptr %51, align 8, !tbaa !12
  br label %59

52:                                               ; preds = %34
  %53 = load ptr, ptr %5, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct._usbd_device, ptr %53, i32 0, i32 23
  %55 = load i16, ptr %54, align 8, !tbaa !12
  %56 = zext i16 %55 to i32
  %57 = sub nsw i32 %56, 4
  %58 = trunc i32 %57 to i16
  store i16 %58, ptr %54, align 8, !tbaa !12
  br label %59

59:                                               ; preds = %52, %49
  %60 = load ptr, ptr %10, align 8, !tbaa !5
  %61 = load i32, ptr %9, align 4, !tbaa !18
  %62 = sext i32 %61 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %60, ptr align 4 %11, i64 %62, i1 false)
  br label %63

63:                                               ; preds = %59, %29
  %64 = load i16, ptr %8, align 2, !tbaa !10
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  ret i16 %64
}

; 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 = { 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"short", !7, i64 0}
!12 = !{!13, !11, i64 456}
!13 = !{!"_usbd_device", !6, i64 0, !6, i64 8, !6, i64 16, !14, i64 24, !6, i64 32, !11, i64 40, !7, i64 42, !7, i64 43, !11, i64 44, !6, i64 48, !6, i64 56, !6, i64 64, !6, i64 72, !15, i64 80, !7, i64 128, !7, i64 192, !7, i64 384, !6, i64 416, !6, i64 424, !11, i64 432, !11, i64 434, !7, i64 436, !7, i64 440, !11, i64 456}
!14 = !{!"int", !7, i64 0}
!15 = !{!"usb_control_state", !14, i64 0, !16, i64 4, !6, i64 16, !11, i64 24, !6, i64 32, !17, i64 40}
!16 = !{!"usb_setup_data", !7, i64 0, !7, i64 1, !11, i64 2, !11, i64 4, !11, i64 6}
!17 = !{!"_Bool", !7, i64 0}
!18 = !{!14, !14, i64 0}
!19 = !{!13, !6, i64 424}
!20 = !{!21, !14, i64 88}
!21 = !{!"_usbd_driver", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !14, i64 88, !17, i64 92, !11, i64 94}


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

; Function Attrs: mustprogress nofree norecurse noreturn nosync nounwind willreturn memory(none) uwtable
define dso_local noundef zeroext i16 @dwc_ep_read_packet(ptr nocapture noundef readnone %0, i8 noundef zeroext %1, ptr nocapture noundef readnone %2, i16 noundef zeroext %3) local_unnamed_addr #0 {
  unreachable
}

attributes #0 = { mustprogress nofree norecurse noreturn nosync nounwind willreturn memory(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" }

!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)"}


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