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

; 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

declare void @usbd_ep_nak_set(ptr noundef, i8 noundef zeroext, i8 noundef zeroext) #1

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

; Function Attrs: nounwind uwtable
define dso_local void @_usbd_control_in(ptr noundef %0, i8 noundef zeroext %1) #2 {
  %3 = alloca ptr, align 8
  %4 = alloca i8, align 1
  %5 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i8 %1, ptr %4, align 1, !tbaa !9
  %6 = load i8, ptr %4, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %7 = load ptr, ptr %3, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct._usbd_device, ptr %7, i32 0, i32 13
  %9 = getelementptr inbounds %struct.usb_control_state, ptr %8, i32 0, i32 1
  store ptr %9, ptr %5, align 8, !tbaa !5
  %10 = load ptr, ptr %3, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct._usbd_device, ptr %10, i32 0, i32 13
  %12 = getelementptr inbounds %struct.usb_control_state, ptr %11, i32 0, i32 0
  %13 = load i32, ptr %12, align 8, !tbaa !10
  switch i32 %13, label %63 [
    i32 2, label %14
    i32 3, label %16
    i32 4, label %21
  ]

14:                                               ; preds = %2
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  call void @usb_control_send_chunk(ptr noundef %15)
  br label %65

16:                                               ; preds = %2
  %17 = load ptr, ptr %3, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct._usbd_device, ptr %17, i32 0, i32 13
  %19 = getelementptr inbounds %struct.usb_control_state, ptr %18, i32 0, i32 0
  store i32 7, ptr %19, align 8, !tbaa !10
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  call void @usbd_ep_nak_set(ptr noundef %20, i8 noundef zeroext 0, i8 noundef zeroext 0)
  br label %65

21:                                               ; preds = %2
  %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 4
  %25 = load ptr, ptr %24, align 8, !tbaa !17
  %26 = icmp ne ptr %25, null
  br i1 %26, label %27, label %36

27:                                               ; preds = %21
  %28 = load ptr, ptr %3, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct._usbd_device, ptr %28, i32 0, i32 13
  %30 = getelementptr inbounds %struct.usb_control_state, ptr %29, i32 0, i32 4
  %31 = load ptr, ptr %30, align 8, !tbaa !17
  %32 = load ptr, ptr %3, align 8, !tbaa !5
  %33 = load ptr, ptr %3, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct._usbd_device, ptr %33, i32 0, i32 13
  %35 = getelementptr inbounds %struct.usb_control_state, ptr %34, i32 0, i32 1
  call void %31(ptr noundef %32, ptr noundef %35)
  br label %36

36:                                               ; preds = %27, %21
  %37 = load ptr, ptr %5, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.usb_setup_data, ptr %37, i32 0, i32 0
  %39 = load i8, ptr %38, align 1, !tbaa !18
  %40 = zext i8 %39 to i32
  %41 = icmp eq i32 %40, 0
  br i1 %41, label %42, label %59

42:                                               ; preds = %36
  %43 = load ptr, ptr %5, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.usb_setup_data, ptr %43, i32 0, i32 1
  %45 = load i8, ptr %44, align 1, !tbaa !19
  %46 = zext i8 %45 to i32
  %47 = icmp eq i32 %46, 5
  br i1 %47, label %48, label %59

48:                                               ; preds = %42
  %49 = load ptr, ptr %3, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct._usbd_device, ptr %49, i32 0, i32 18
  %51 = load ptr, ptr %50, align 8, !tbaa !20
  %52 = getelementptr inbounds %struct._usbd_driver, ptr %51, i32 0, i32 1
  %53 = load ptr, ptr %52, align 8, !tbaa !21
  %54 = load ptr, ptr %3, align 8, !tbaa !5
  %55 = load ptr, ptr %5, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.usb_setup_data, ptr %55, i32 0, i32 2
  %57 = load i16, ptr %56, align 1, !tbaa !23
  %58 = trunc i16 %57 to i8
  call void %53(ptr noundef %54, i8 noundef zeroext %58)
  br label %59

59:                                               ; preds = %48, %42, %36
  %60 = load ptr, ptr %3, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct._usbd_device, ptr %60, i32 0, i32 13
  %62 = getelementptr inbounds %struct.usb_control_state, ptr %61, i32 0, i32 0
  store i32 0, ptr %62, align 8, !tbaa !10
  br label %65

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

65:                                               ; preds = %63, %59, %16, %14
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden void @usb_control_send_chunk(ptr noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 = !{!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, !6, i64 112}
!18 = !{!15, !7, i64 0}
!19 = !{!15, !7, i64 1}
!20 = !{!11, !6, i64 424}
!21 = !{!22, !6, i64 8}
!22 = !{!"_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, !12, i64 88, !16, i64 92, !13, i64 94}
!23 = !{!15, !13, i64 2}


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

declare void @usbd_ep_nak_set(ptr noundef, i8 noundef zeroext, i8 noundef zeroext) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local void @_usbd_control_in(ptr noundef %0, i8 noundef zeroext %1) local_unnamed_addr #1 {
  %3 = getelementptr inbounds i8, ptr %0, i64 80
  %4 = getelementptr inbounds i8, ptr %0, i64 84
  %5 = load i32, ptr %3, align 8, !tbaa !5
  switch i32 %5, label %28 [
    i32 2, label %6
    i32 3, label %7
    i32 4, label %8
  ]

6:                                                ; preds = %2
  tail call void @usb_control_send_chunk(ptr noundef nonnull %0)
  br label %29

7:                                                ; preds = %2
  store i32 7, ptr %3, align 8, !tbaa !5
  tail call void @usbd_ep_nak_set(ptr noundef nonnull %0, i8 noundef zeroext 0, i8 noundef zeroext 0) #2
  br label %29

8:                                                ; preds = %2
  %9 = getelementptr inbounds i8, ptr %0, i64 112
  %10 = load ptr, ptr %9, align 8, !tbaa !15
  %.not = icmp eq ptr %10, null
  br i1 %.not, label %12, label %11

11:                                               ; preds = %8
  tail call void %10(ptr noundef nonnull %0, ptr noundef nonnull %4) #2
  br label %12

12:                                               ; preds = %11, %8
  %13 = load i8, ptr %4, align 1, !tbaa !16
  %14 = icmp eq i8 %13, 0
  br i1 %14, label %15, label %27

15:                                               ; preds = %12
  %16 = getelementptr inbounds i8, ptr %0, i64 85
  %17 = load i8, ptr %16, align 1, !tbaa !17
  %18 = icmp eq i8 %17, 5
  br i1 %18, label %19, label %27

19:                                               ; preds = %15
  %20 = getelementptr inbounds i8, ptr %0, i64 424
  %21 = load ptr, ptr %20, align 8, !tbaa !18
  %22 = getelementptr inbounds i8, ptr %21, i64 8
  %23 = load ptr, ptr %22, align 8, !tbaa !19
  %24 = getelementptr inbounds i8, ptr %0, i64 86
  %25 = load i16, ptr %24, align 1, !tbaa !21
  %26 = trunc i16 %25 to i8
  tail call void %23(ptr noundef nonnull %0, i8 noundef zeroext %26) #2
  br label %27

27:                                               ; preds = %19, %15, %12
  store i32 0, ptr %3, align 8, !tbaa !5
  br label %29

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

29:                                               ; preds = %28, %27, %7, %6
  ret void
}

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

attributes #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 = { 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 = { 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, !7, i64 112}
!16 = !{!13, !8, i64 0}
!17 = !{!13, !8, i64 1}
!18 = !{!6, !7, i64 424}
!19 = !{!20, !7, i64 8}
!20 = !{!"_usbd_driver", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !7, i64 32, !7, i64 40, !7, i64 48, !7, i64 56, !7, i64 64, !7, i64 72, !7, i64 80, !10, i64 88, !14, i64 92, !11, i64 94}
!21 = !{!13, !11, i64 2}


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