; LLVM IR from lichen-community-systems/signaletic :: hosts/daisy/vendor/libDaisy/Drivers/STM32H7xx_HAL_Driver/Src (VBUSDetect)

; 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.PCD_HandleTypeDef = type { ptr, %struct.USB_OTG_CfgTypeDef, i8, [16 x %struct.USB_OTG_EPTypeDef], [16 x %struct.USB_OTG_EPTypeDef], i32, i32, i32, [12 x i32], i32, i32, i32, i32, ptr }
%struct.USB_OTG_CfgTypeDef = type { i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }
%struct.USB_OTG_EPTypeDef = type { i8, i8, i8, i8, i8, i8, i16, i32, ptr, i32, i32, i32 }
%struct.USB_OTG_GlobalTypeDef = type { i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, [2 x i32], i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, [39 x i32], i32, [15 x i32] }

; 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 void @HAL_PCDEx_BCD_VBUSDetect(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  %6 = load ptr, ptr %2, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct.PCD_HandleTypeDef, ptr %6, i32 0, i32 0
  %8 = load ptr, ptr %7, align 8, !tbaa !9
  store ptr %8, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  %9 = call i32 @HAL_GetTick()
  store i32 %9, ptr %4, align 4, !tbaa !13
  %10 = load ptr, ptr %3, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.USB_OTG_GlobalTypeDef, ptr %10, i32 0, i32 13
  %12 = load volatile i32, ptr %11, align 4, !tbaa !14
  %13 = zext i32 %12 to i64
  %14 = or i64 %13, 262144
  %15 = trunc i64 %14 to i32
  store volatile i32 %15, ptr %11, align 4, !tbaa !14
  br label %16

16:                                               ; preds = %30, %1
  %17 = load ptr, ptr %3, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.USB_OTG_GlobalTypeDef, ptr %17, i32 0, i32 13
  %19 = load volatile i32, ptr %18, align 4, !tbaa !14
  %20 = zext i32 %19 to i64
  %21 = and i64 %20, 1
  %22 = icmp eq i64 %21, 0
  br i1 %22, label %23, label %31

23:                                               ; preds = %16
  %24 = call i32 @HAL_GetTick()
  %25 = load i32, ptr %4, align 4, !tbaa !13
  %26 = sub i32 %24, %25
  %27 = icmp ugt i32 %26, 1000
  br i1 %27, label %28, label %30

28:                                               ; preds = %23
  %29 = load ptr, ptr %2, align 8, !tbaa !5
  call void @HAL_PCDEx_BCD_Callback(ptr noundef %29, i32 noundef 255)
  store i32 1, ptr %5, align 4
  br label %89

30:                                               ; preds = %23
  br label %16, !llvm.loop !16

31:                                               ; preds = %16
  call void @HAL_Delay(i32 noundef 200)
  %32 = load ptr, ptr %3, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.USB_OTG_GlobalTypeDef, ptr %32, i32 0, i32 13
  %34 = load volatile i32, ptr %33, align 4, !tbaa !14
  %35 = zext i32 %34 to i64
  %36 = and i64 %35, 1
  %37 = icmp eq i64 %36, 1
  br i1 %37, label %38, label %40

38:                                               ; preds = %31
  %39 = load ptr, ptr %2, align 8, !tbaa !5
  call void @HAL_PCDEx_BCD_Callback(ptr noundef %39, i32 noundef 254)
  br label %40

40:                                               ; preds = %38, %31
  %41 = load ptr, ptr %3, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.USB_OTG_GlobalTypeDef, ptr %41, i32 0, i32 13
  %43 = load volatile i32, ptr %42, align 4, !tbaa !14
  %44 = zext i32 %43 to i64
  %45 = and i64 %44, -262145
  %46 = trunc i64 %45 to i32
  store volatile i32 %46, ptr %42, align 4, !tbaa !14
  call void @HAL_Delay(i32 noundef 50)
  %47 = load ptr, ptr %3, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.USB_OTG_GlobalTypeDef, ptr %47, i32 0, i32 13
  %49 = load volatile i32, ptr %48, align 4, !tbaa !14
  %50 = zext i32 %49 to i64
  %51 = or i64 %50, 524288
  %52 = trunc i64 %51 to i32
  store volatile i32 %52, ptr %48, align 4, !tbaa !14
  call void @HAL_Delay(i32 noundef 50)
  %53 = load ptr, ptr %3, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.USB_OTG_GlobalTypeDef, ptr %53, i32 0, i32 13
  %55 = load volatile i32, ptr %54, align 4, !tbaa !14
  %56 = zext i32 %55 to i64
  %57 = and i64 %56, 2
  %58 = icmp eq i64 %57, 0
  br i1 %58, label %59, label %61

59:                                               ; preds = %40
  %60 = load ptr, ptr %2, align 8, !tbaa !5
  call void @HAL_PCDEx_BCD_Callback(ptr noundef %60, i32 noundef 253)
  br label %85

61:                                               ; preds = %40
  %62 = load ptr, ptr %3, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.USB_OTG_GlobalTypeDef, ptr %62, i32 0, i32 13
  %64 = load volatile i32, ptr %63, align 4, !tbaa !14
  %65 = zext i32 %64 to i64
  %66 = and i64 %65, -524289
  %67 = trunc i64 %66 to i32
  store volatile i32 %67, ptr %63, align 4, !tbaa !14
  call void @HAL_Delay(i32 noundef 50)
  %68 = load ptr, ptr %3, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.USB_OTG_GlobalTypeDef, ptr %68, i32 0, i32 13
  %70 = load volatile i32, ptr %69, align 4, !tbaa !14
  %71 = zext i32 %70 to i64
  %72 = or i64 %71, 1048576
  %73 = trunc i64 %72 to i32
  store volatile i32 %73, ptr %69, align 4, !tbaa !14
  call void @HAL_Delay(i32 noundef 50)
  %74 = load ptr, ptr %3, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.USB_OTG_GlobalTypeDef, ptr %74, i32 0, i32 13
  %76 = load volatile i32, ptr %75, align 4, !tbaa !14
  %77 = zext i32 %76 to i64
  %78 = and i64 %77, 4
  %79 = icmp eq i64 %78, 4
  br i1 %79, label %80, label %82

80:                                               ; preds = %61
  %81 = load ptr, ptr %2, align 8, !tbaa !5
  call void @HAL_PCDEx_BCD_Callback(ptr noundef %81, i32 noundef 251)
  br label %84

82:                                               ; preds = %61
  %83 = load ptr, ptr %2, align 8, !tbaa !5
  call void @HAL_PCDEx_BCD_Callback(ptr noundef %83, i32 noundef 252)
  br label %84

84:                                               ; preds = %82, %80
  br label %85

85:                                               ; preds = %84, %59
  %86 = load ptr, ptr %2, align 8, !tbaa !5
  %87 = call i32 @HAL_PCDEx_DeActivateBCD(ptr noundef %86)
  %88 = load ptr, ptr %2, align 8, !tbaa !5
  call void @HAL_PCDEx_BCD_Callback(ptr noundef %88, i32 noundef 0)
  store i32 0, ptr %5, align 4
  br label %89

89:                                               ; preds = %85, %28
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  %90 = load i32, ptr %5, align 4
  switch i32 %90, label %92 [
    i32 0, label %91
    i32 1, label %91
  ]

91:                                               ; preds = %89, %89
  ret void

92:                                               ; preds = %89
  unreachable
}

declare i32 @HAL_GetTick() #2

; Function Attrs: nounwind uwtable
declare dso_local void @HAL_PCDEx_BCD_Callback(ptr noundef, i32 noundef) #1

declare void @HAL_Delay(i32 noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local i32 @HAL_PCDEx_DeActivateBCD(ptr noundef) #1

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 = { "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 = !{!10, !6, i64 0}
!10 = !{!"", !6, i64 0, !11, i64 8, !7, i64 60, !7, i64 64, !7, i64 704, !12, i64 1344, !12, i64 1348, !12, i64 1352, !7, i64 1356, !12, i64 1404, !12, i64 1408, !12, i64 1412, !12, i64 1416, !6, i64 1424}
!11 = !{!"", !12, i64 0, !12, i64 4, !12, i64 8, !12, i64 12, !12, i64 16, !12, i64 20, !12, i64 24, !12, i64 28, !12, i64 32, !12, i64 36, !12, i64 40, !12, i64 44, !12, i64 48}
!12 = !{!"int", !7, i64 0}
!13 = !{!12, !12, i64 0}
!14 = !{!15, !12, i64 56}
!15 = !{!"", !12, i64 0, !12, i64 4, !12, i64 8, !12, i64 12, !12, i64 16, !12, i64 20, !12, i64 24, !12, i64 28, !12, i64 32, !12, i64 36, !12, i64 40, !12, i64 44, !7, i64 48, !12, i64 56, !12, i64 60, !12, i64 64, !12, i64 68, !12, i64 72, !12, i64 76, !12, i64 80, !12, i64 84, !12, i64 88, !12, i64 92, !12, i64 96, !7, i64 100, !12, i64 256, !7, i64 260}
!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: nounwind uwtable
define dso_local void @HAL_PCDEx_BCD_VBUSDetect(ptr noundef %0) local_unnamed_addr #0 {
  %2 = load ptr, ptr %0, align 8, !tbaa !5
  %3 = tail call i32 @HAL_GetTick() #2
  %4 = getelementptr inbounds i8, ptr %2, i64 56
  %5 = load volatile i32, ptr %4, align 4, !tbaa !12
  %6 = or i32 %5, 262144
  store volatile i32 %6, ptr %4, align 4, !tbaa !12
  br label %7

7:                                                ; preds = %11, %1
  %8 = load volatile i32, ptr %4, align 4, !tbaa !12
  %9 = and i32 %8, 1
  %10 = icmp eq i32 %9, 0
  br i1 %10, label %11, label %15

11:                                               ; preds = %7
  %12 = tail call i32 @HAL_GetTick() #2
  %13 = sub i32 %12, %3
  %14 = icmp ugt i32 %13, 1000
  br i1 %14, label %.loopexit, label %7, !llvm.loop !14

15:                                               ; preds = %7
  tail call void @HAL_Delay(i32 noundef 200) #2
  %16 = load volatile i32, ptr %4, align 4, !tbaa !12
  %17 = and i32 %16, 1
  %.not = icmp eq i32 %17, 0
  br i1 %.not, label %19, label %18

18:                                               ; preds = %15
  tail call void @HAL_PCDEx_BCD_Callback(ptr noundef nonnull %0, i32 noundef 254)
  br label %19

19:                                               ; preds = %18, %15
  %20 = load volatile i32, ptr %4, align 4, !tbaa !12
  %21 = and i32 %20, -262145
  store volatile i32 %21, ptr %4, align 4, !tbaa !12
  tail call void @HAL_Delay(i32 noundef 50) #2
  %22 = load volatile i32, ptr %4, align 4, !tbaa !12
  %23 = or i32 %22, 524288
  store volatile i32 %23, ptr %4, align 4, !tbaa !12
  tail call void @HAL_Delay(i32 noundef 50) #2
  %24 = load volatile i32, ptr %4, align 4, !tbaa !12
  %25 = and i32 %24, 2
  %26 = icmp eq i32 %25, 0
  br i1 %26, label %34, label %27

27:                                               ; preds = %19
  %28 = load volatile i32, ptr %4, align 4, !tbaa !12
  %29 = and i32 %28, -524289
  store volatile i32 %29, ptr %4, align 4, !tbaa !12
  tail call void @HAL_Delay(i32 noundef 50) #2
  %30 = load volatile i32, ptr %4, align 4, !tbaa !12
  %31 = or i32 %30, 1048576
  store volatile i32 %31, ptr %4, align 4, !tbaa !12
  tail call void @HAL_Delay(i32 noundef 50) #2
  %32 = load volatile i32, ptr %4, align 4, !tbaa !12
  %33 = and i32 %32, 4
  %.not17 = icmp eq i32 %33, 0
  %. = select i1 %.not17, i32 252, i32 251
  br label %34

34:                                               ; preds = %27, %19
  %.sink = phi i32 [ 253, %19 ], [ %., %27 ]
  tail call void @HAL_PCDEx_BCD_Callback(ptr noundef nonnull %0, i32 noundef %.sink)
  %35 = tail call i32 @HAL_PCDEx_DeActivateBCD(ptr noundef nonnull %0)
  br label %.loopexit

.loopexit:                                        ; preds = %11, %34
  %.sink18 = phi i32 [ 0, %34 ], [ 255, %11 ]
  tail call void @HAL_PCDEx_BCD_Callback(ptr noundef nonnull %0, i32 noundef %.sink18)
  ret void
}

declare i32 @HAL_GetTick() local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @HAL_PCDEx_BCD_Callback(ptr noundef, i32 noundef) local_unnamed_addr #0

declare void @HAL_Delay(i32 noundef) local_unnamed_addr #1

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

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, !7, i64 0}
!6 = !{!"", !7, i64 0, !10, i64 8, !8, i64 60, !8, i64 64, !8, i64 704, !11, i64 1344, !11, i64 1348, !11, i64 1352, !8, i64 1356, !11, i64 1404, !11, i64 1408, !11, i64 1412, !11, i64 1416, !7, i64 1424}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"", !11, i64 0, !11, i64 4, !11, i64 8, !11, i64 12, !11, i64 16, !11, i64 20, !11, i64 24, !11, i64 28, !11, i64 32, !11, i64 36, !11, i64 40, !11, i64 44, !11, i64 48}
!11 = !{!"int", !8, i64 0}
!12 = !{!13, !11, i64 56}
!13 = !{!"", !11, i64 0, !11, i64 4, !11, i64 8, !11, i64 12, !11, i64 16, !11, i64 20, !11, i64 24, !11, i64 28, !11, i64 32, !11, i64 36, !11, i64 40, !11, i64 44, !8, i64 48, !11, i64 56, !11, i64 60, !11, i64 64, !11, i64 68, !11, i64 72, !11, i64 76, !11, i64 80, !11, i64 84, !11, i64 88, !11, i64 92, !11, i64 96, !8, i64 100, !11, i64 256, !8, i64 260}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass