; LLVM IR from sca-secure-library-sca25519/sca25519 :: libopencm3/lib/usb/usb_control.ll (usbd_control_out)

; 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.usb_device_descriptor = type { i8, i8, i16, i8, i8, i8, i8, i16, i16, i16, i8, i8, i8, i8 }

; Function Attrs: nounwind uwtable
define dso_local void @_usbd_control_out(ptr noundef %0, i8 noundef zeroext %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca i8, align 1
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i8 %1, ptr %4, align 1, !tbaa !9
  %5 = load i8, ptr %4, align 1, !tbaa !9
  %6 = load ptr, ptr %3, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct._usbd_device, ptr %6, i32 0, i32 13
  %8 = getelementptr inbounds %struct.usb_control_state, ptr %7, i32 0, i32 0
  %9 = load i32, ptr %8, align 8, !tbaa !10
  switch i32 %9, label %85 [
    i32 5, label %10
    i32 6, label %40
    i32 7, label %61
  ]

10:                                               ; preds = %2
  %11 = load ptr, ptr %3, align 8, !tbaa !5
  %12 = call i32 @usb_control_recv_chunk(ptr noundef %11)
  %13 = icmp slt i32 %12, 0
  br i1 %13, label %14, label %15

14:                                               ; preds = %10
  br label %87

15:                                               ; preds = %10
  %16 = load ptr, ptr %3, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct._usbd_device, ptr %16, i32 0, i32 13
  %18 = getelementptr inbounds %struct.usb_control_state, ptr %17, i32 0, i32 1
  %19 = getelementptr inbounds %struct.usb_setup_data, ptr %18, i32 0, i32 4
  %20 = load i16, ptr %19, align 2, !tbaa !17
  %21 = zext i16 %20 to i32
  %22 = load ptr, ptr %3, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct._usbd_device, ptr %22, i32 0, i32 13
  %24 = getelementptr inbounds %struct.usb_control_state, ptr %23, i32 0, i32 3
  %25 = load i16, ptr %24, align 8, !tbaa !18
  %26 = zext i16 %25 to i32
  %27 = sub nsw i32 %21, %26
  %28 = load ptr, ptr %3, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct._usbd_device, ptr %28, i32 0, i32 0
  %30 = load ptr, ptr %29, align 8, !tbaa !19
  %31 = getelementptr inbounds %struct.usb_device_descriptor, ptr %30, i32 0, i32 6
  %32 = load i8, ptr %31, align 1, !tbaa !20
  %33 = zext i8 %32 to i32
  %34 = icmp sle i32 %27, %33
  br i1 %34, label %35, label %39

35:                                               ; preds = %15
  %36 = load ptr, ptr %3, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct._usbd_device, ptr %36, i32 0, i32 13
  %38 = getelementptr inbounds %struct.usb_control_state, ptr %37, i32 0, i32 0
  store i32 6, ptr %38, align 8, !tbaa !10
  br label %39

39:                                               ; preds = %35, %15
  br label %87

40:                                               ; preds = %2
  %41 = load ptr, ptr %3, align 8, !tbaa !5
  %42 = call i32 @usb_control_recv_chunk(ptr noundef %41)
  %43 = icmp slt i32 %42, 0
  br i1 %43, label %44, label %45

44:                                               ; preds = %40
  br label %87

45:                                               ; preds = %40
  %46 = load ptr, ptr %3, align 8, !tbaa !5
  %47 = load ptr, ptr %3, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct._usbd_device, ptr %47, i32 0, i32 13
  %49 = getelementptr inbounds %struct.usb_control_state, ptr %48, i32 0, i32 1
  %50 = call i32 @usb_control_request_dispatch(ptr noundef %46, ptr noundef %49)
  %51 = icmp ne i32 %50, 0
  br i1 %51, label %52, label %58

52:                                               ; preds = %45
  %53 = load ptr, ptr %3, align 8, !tbaa !5
  %54 = call zeroext i16 @usbd_ep_write_packet(ptr noundef %53, i8 noundef zeroext 0, ptr noundef null, i16 noundef zeroext 0)
  %55 = load ptr, ptr %3, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct._usbd_device, ptr %55, i32 0, i32 13
  %57 = getelementptr inbounds %struct.usb_control_state, ptr %56, i32 0, i32 0
  store i32 4, ptr %57, align 8, !tbaa !10
  br label %60

58:                                               ; preds = %45
  %59 = load ptr, ptr %3, align 8, !tbaa !5
  call void @stall_transaction(ptr noundef %59)
  br label %60

60:                                               ; preds = %58, %52
  br label %87

61:                                               ; preds = %2
  %62 = load ptr, ptr %3, align 8, !tbaa !5
  %63 = call zeroext i16 @usbd_ep_read_packet(ptr noundef %62, i8 noundef zeroext 0, ptr noundef null, i16 noundef zeroext 0)
  %64 = load ptr, ptr %3, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct._usbd_device, ptr %64, i32 0, i32 13
  %66 = getelementptr inbounds %struct.usb_control_state, ptr %65, i32 0, i32 0
  store i32 0, ptr %66, align 8, !tbaa !10
  %67 = load ptr, ptr %3, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct._usbd_device, ptr %67, i32 0, i32 13
  %69 = getelementptr inbounds %struct.usb_control_state, ptr %68, i32 0, i32 4
  %70 = load ptr, ptr %69, align 8, !tbaa !22
  %71 = icmp ne ptr %70, null
  br i1 %71, label %72, label %81

72:                                               ; preds = %61
  %73 = load ptr, ptr %3, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct._usbd_device, ptr %73, i32 0, i32 13
  %75 = getelementptr inbounds %struct.usb_control_state, ptr %74, i32 0, i32 4
  %76 = load ptr, ptr %75, align 8, !tbaa !22
  %77 = load ptr, ptr %3, align 8, !tbaa !5
  %78 = load ptr, ptr %3, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct._usbd_device, ptr %78, i32 0, i32 13
  %80 = getelementptr inbounds %struct.usb_control_state, ptr %79, i32 0, i32 1
  call void %76(ptr noundef %77, ptr noundef %80)
  br label %81

81:                                               ; preds = %72, %61
  %82 = load ptr, ptr %3, align 8, !tbaa !5
  %83 = getelementptr inbounds %struct._usbd_device, ptr %82, i32 0, i32 13
  %84 = getelementptr inbounds %struct.usb_control_state, ptr %83, i32 0, i32 4
  store ptr null, ptr %84, align 8, !tbaa !22
  br label %87

85:                                               ; preds = %2
  %86 = load ptr, ptr %3, align 8, !tbaa !5
  call void @stall_transaction(ptr noundef %86)
  br label %87

87:                                               ; preds = %85, %81, %60, %44, %39, %14
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden i32 @usb_control_recv_chunk(ptr noundef) #0

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

declare zeroext i16 @usbd_ep_write_packet(ptr noundef, i8 noundef zeroext, ptr noundef, i16 noundef zeroext) #1

; Function Attrs: nounwind uwtable
declare hidden void @stall_transaction(ptr noundef) #0

declare zeroext i16 @usbd_ep_read_packet(ptr noundef, i8 noundef zeroext, ptr noundef, i16 noundef zeroext) #1

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


; 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: nounwind uwtable
define dso_local void @_usbd_control_out(ptr noundef %0, i8 noundef zeroext %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 80
  %4 = load i32, ptr %3, align 8, !tbaa !5
  switch i32 %4, label %37 [
    i32 5, label %5
    i32 6, label %21
    i32 7, label %30
  ]

5:                                                ; preds = %2
  %6 = tail call i32 @usb_control_recv_chunk(ptr noundef nonnull %0)
  %7 = icmp slt i32 %6, 0
  br i1 %7, label %38, label %8

8:                                                ; preds = %5
  %9 = getelementptr inbounds i8, ptr %0, i64 90
  %10 = load i16, ptr %9, align 2, !tbaa !15
  %11 = zext i16 %10 to i32
  %12 = getelementptr inbounds i8, ptr %0, i64 104
  %13 = load i16, ptr %12, align 8, !tbaa !16
  %14 = zext i16 %13 to i32
  %15 = sub nsw i32 %11, %14
  %16 = load ptr, ptr %0, align 8, !tbaa !17
  %17 = getelementptr inbounds i8, ptr %16, i64 7
  %18 = load i8, ptr %17, align 1, !tbaa !18
  %19 = zext i8 %18 to i32
  %.not21 = icmp sgt i32 %15, %19
  br i1 %.not21, label %38, label %20

20:                                               ; preds = %8
  store i32 6, ptr %3, align 8, !tbaa !5
  br label %38

21:                                               ; preds = %2
  %22 = tail call i32 @usb_control_recv_chunk(ptr noundef nonnull %0)
  %23 = icmp slt i32 %22, 0
  br i1 %23, label %38, label %24

24:                                               ; preds = %21
  %25 = getelementptr inbounds i8, ptr %0, i64 84
  %26 = tail call i32 @usb_control_request_dispatch(ptr noundef nonnull %0, ptr noundef nonnull %25)
  %.not20 = icmp eq i32 %26, 0
  br i1 %.not20, label %29, label %27

27:                                               ; preds = %24
  %28 = tail call zeroext i16 @usbd_ep_write_packet(ptr noundef nonnull %0, i8 noundef zeroext 0, ptr noundef null, i16 noundef zeroext 0) #2
  store i32 4, ptr %3, align 8, !tbaa !5
  br label %38

29:                                               ; preds = %24
  tail call void @stall_transaction(ptr noundef nonnull %0)
  br label %38

30:                                               ; preds = %2
  %31 = tail call zeroext i16 @usbd_ep_read_packet(ptr noundef nonnull %0, i8 noundef zeroext 0, ptr noundef null, i16 noundef zeroext 0) #2
  store i32 0, ptr %3, align 8, !tbaa !5
  %32 = getelementptr inbounds i8, ptr %0, i64 112
  %33 = load ptr, ptr %32, align 8, !tbaa !20
  %.not = icmp eq ptr %33, null
  br i1 %.not, label %36, label %34

34:                                               ; preds = %30
  %35 = getelementptr inbounds i8, ptr %0, i64 84
  tail call void %33(ptr noundef nonnull %0, ptr noundef nonnull %35) #2
  br label %36

36:                                               ; preds = %34, %30
  store ptr null, ptr %32, align 8, !tbaa !20
  br label %38

37:                                               ; preds = %2
  tail call void @stall_transaction(ptr noundef nonnull %0)
  br label %38

38:                                               ; preds = %27, %29, %21, %8, %20, %5, %37, %36
  ret void
}

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

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

declare zeroext i16 @usbd_ep_write_packet(ptr noundef, i8 noundef zeroext, ptr noundef, i16 noundef zeroext) local_unnamed_addr #1

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

declare zeroext i16 @usbd_ep_read_packet(ptr noundef, i8 noundef zeroext, ptr noundef, i16 noundef zeroext) local_unnamed_addr #1

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


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,InstCombinePass,SROAPass,SimplifyCFGPass,TailCallElimPass