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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @usbd_register_control_callback(ptr noundef %0, i8 noundef zeroext %1, i8 noundef zeroext %2, ptr noundef %3) #0 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i8, align 1
  %8 = alloca i8, align 1
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i8 %1, ptr %7, align 1, !tbaa !9
  store i8 %2, ptr %8, align 1, !tbaa !9
  store ptr %3, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  store i32 0, ptr %10, align 4, !tbaa !10
  br label %12

12:                                               ; preds = %47, %4
  %13 = load i32, ptr %10, align 4, !tbaa !10
  %14 = icmp slt i32 %13, 4
  br i1 %14, label %15, label %50

15:                                               ; preds = %12
  %16 = load ptr, ptr %6, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct._usbd_device, ptr %16, i32 0, i32 14
  %18 = load i32, ptr %10, align 4, !tbaa !10
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [4 x %struct.user_control_callback], ptr %17, i64 0, i64 %19
  %21 = getelementptr inbounds %struct.user_control_callback, ptr %20, i32 0, i32 0
  %22 = load ptr, ptr %21, align 8, !tbaa !12
  %23 = icmp ne ptr %22, null
  br i1 %23, label %24, label %25

24:                                               ; preds = %15
  br label %47

25:                                               ; preds = %15
  %26 = load i8, ptr %7, align 1, !tbaa !9
  %27 = load ptr, ptr %6, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct._usbd_device, ptr %27, i32 0, i32 14
  %29 = load i32, ptr %10, align 4, !tbaa !10
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [4 x %struct.user_control_callback], ptr %28, i64 0, i64 %30
  %32 = getelementptr inbounds %struct.user_control_callback, ptr %31, i32 0, i32 1
  store i8 %26, ptr %32, align 8, !tbaa !14
  %33 = load i8, ptr %8, align 1, !tbaa !9
  %34 = load ptr, ptr %6, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct._usbd_device, ptr %34, i32 0, i32 14
  %36 = load i32, ptr %10, align 4, !tbaa !10
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [4 x %struct.user_control_callback], ptr %35, i64 0, i64 %37
  %39 = getelementptr inbounds %struct.user_control_callback, ptr %38, i32 0, i32 2
  store i8 %33, ptr %39, align 1, !tbaa !15
  %40 = load ptr, ptr %9, align 8, !tbaa !5
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct._usbd_device, ptr %41, i32 0, i32 14
  %43 = load i32, ptr %10, align 4, !tbaa !10
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds [4 x %struct.user_control_callback], ptr %42, i64 0, i64 %44
  %46 = getelementptr inbounds %struct.user_control_callback, ptr %45, i32 0, i32 0
  store ptr %40, ptr %46, align 8, !tbaa !12
  store i32 0, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %51

47:                                               ; preds = %24
  %48 = load i32, ptr %10, align 4, !tbaa !10
  %49 = add nsw i32 %48, 1
  store i32 %49, ptr %10, align 4, !tbaa !10
  br label %12, !llvm.loop !16

50:                                               ; preds = %12
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %51

51:                                               ; preds = %50, %25
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  %52 = load i32, ptr %5, align 4
  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

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 = { 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 = !{!"int", !7, i64 0}
!12 = !{!13, !6, i64 0}
!13 = !{!"user_control_callback", !6, i64 0, !7, i64 8, !7, i64 9}
!14 = !{!13, !7, i64 8}
!15 = !{!13, !7, i64 9}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


; 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 nosync nounwind willreturn memory(write, argmem: readwrite, inaccessiblemem: none) uwtable
define dso_local range(i32 -1, 1) i32 @usbd_register_control_callback(ptr nocapture noundef %0, i8 noundef zeroext %1, i8 noundef zeroext %2, ptr noundef %3) local_unnamed_addr #0 {
  %5 = getelementptr inbounds i8, ptr %0, i64 128
  %6 = load ptr, ptr %5, align 8, !tbaa !5
  %.not = icmp eq ptr %6, null
  br i1 %.not, label %16, label %7

7:                                                ; preds = %4
  %8 = getelementptr inbounds i8, ptr %0, i64 144
  %9 = load ptr, ptr %8, align 8, !tbaa !5
  %.not.1 = icmp eq ptr %9, null
  br i1 %.not.1, label %16, label %10

10:                                               ; preds = %7
  %11 = getelementptr inbounds i8, ptr %0, i64 160
  %12 = load ptr, ptr %11, align 8, !tbaa !5
  %.not.2 = icmp eq ptr %12, null
  br i1 %.not.2, label %16, label %13

13:                                               ; preds = %10
  %14 = getelementptr inbounds i8, ptr %0, i64 176
  %15 = load ptr, ptr %14, align 8, !tbaa !5
  %.not.3 = icmp eq ptr %15, null
  br i1 %.not.3, label %16, label %.loopexit

16:                                               ; preds = %13, %10, %7, %4
  %.lcssa = phi ptr [ %5, %4 ], [ %8, %7 ], [ %11, %10 ], [ %14, %13 ]
  %17 = getelementptr inbounds i8, ptr %.lcssa, i64 8
  store i8 %1, ptr %17, align 8, !tbaa !10
  %18 = getelementptr inbounds i8, ptr %.lcssa, i64 9
  store i8 %2, ptr %18, align 1, !tbaa !11
  store ptr %3, ptr %.lcssa, align 8, !tbaa !5
  br label %.loopexit

.loopexit:                                        ; preds = %13, %16
  %.013 = phi i32 [ 0, %16 ], [ -1, %13 ]
  ret i32 %.013
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(write, argmem: readwrite, inaccessiblemem: 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)"}
!5 = !{!6, !7, i64 0}
!6 = !{!"user_control_callback", !7, i64 0, !8, i64 8, !8, i64 9}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!6, !8, i64 8}
!11 = !{!6, !8, i64 9}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass