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

; 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.RTC_HandleTypeDef = type { ptr, %struct.RTC_InitTypeDef, i32, i32 }
%struct.RTC_InitTypeDef = type { i32, i32, i32, i32, i32, i32, i32 }
%struct.RTC_TypeDef = type { i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, 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 i32 @HAL_RTCEx_PollForTimeStampEvent(ptr noundef %0, i32 noundef %1) #1 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %8 = call i32 @HAL_GetTick()
  store i32 %8, ptr %6, align 4, !tbaa !9
  br label %9

9:                                                ; preds = %65, %2
  %10 = load ptr, ptr %4, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.RTC_HandleTypeDef, ptr %10, i32 0, i32 0
  %12 = load ptr, ptr %11, align 8, !tbaa !11
  %13 = getelementptr inbounds %struct.RTC_TypeDef, ptr %12, i32 0, i32 3
  %14 = load volatile i32, ptr %13, align 4, !tbaa !14
  %15 = zext i32 %14 to i64
  %16 = and i64 %15, 2048
  %17 = icmp ne i64 %16, 0
  %18 = zext i1 %17 to i64
  %19 = select i1 %17, i32 1, i32 0
  %20 = icmp eq i32 %19, 0
  br i1 %20, label %21, label %66

21:                                               ; preds = %9
  %22 = load ptr, ptr %4, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.RTC_HandleTypeDef, ptr %22, i32 0, i32 0
  %24 = load ptr, ptr %23, align 8, !tbaa !11
  %25 = getelementptr inbounds %struct.RTC_TypeDef, ptr %24, i32 0, i32 3
  %26 = load volatile i32, ptr %25, align 4, !tbaa !14
  %27 = zext i32 %26 to i64
  %28 = and i64 %27, 4096
  %29 = icmp ne i64 %28, 0
  %30 = zext i1 %29 to i64
  %31 = select i1 %29, i32 1, i32 0
  %32 = icmp ne i32 %31, 0
  br i1 %32, label %33, label %49

33:                                               ; preds = %21
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.RTC_HandleTypeDef, ptr %34, i32 0, i32 0
  %36 = load ptr, ptr %35, align 8, !tbaa !11
  %37 = getelementptr inbounds %struct.RTC_TypeDef, ptr %36, i32 0, i32 3
  %38 = load volatile i32, ptr %37, align 4, !tbaa !14
  %39 = zext i32 %38 to i64
  %40 = and i64 %39, 128
  %41 = or i64 -4225, %40
  %42 = trunc i64 %41 to i32
  %43 = load ptr, ptr %4, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.RTC_HandleTypeDef, ptr %43, i32 0, i32 0
  %45 = load ptr, ptr %44, align 8, !tbaa !11
  %46 = getelementptr inbounds %struct.RTC_TypeDef, ptr %45, i32 0, i32 3
  store volatile i32 %42, ptr %46, align 4, !tbaa !14
  %47 = load ptr, ptr %4, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.RTC_HandleTypeDef, ptr %47, i32 0, i32 3
  store volatile i32 4, ptr %48, align 8, !tbaa !16
  store i32 1, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %69

49:                                               ; preds = %21
  %50 = load i32, ptr %5, align 4, !tbaa !9
  %51 = icmp ne i32 %50, -1
  br i1 %51, label %52, label %65

52:                                               ; preds = %49
  %53 = call i32 @HAL_GetTick()
  %54 = load i32, ptr %6, align 4, !tbaa !9
  %55 = sub i32 %53, %54
  %56 = load i32, ptr %5, align 4, !tbaa !9
  %57 = icmp ugt i32 %55, %56
  br i1 %57, label %61, label %58

58:                                               ; preds = %52
  %59 = load i32, ptr %5, align 4, !tbaa !9
  %60 = icmp eq i32 %59, 0
  br i1 %60, label %61, label %64

61:                                               ; preds = %58, %52
  %62 = load ptr, ptr %4, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.RTC_HandleTypeDef, ptr %62, i32 0, i32 3
  store volatile i32 3, ptr %63, align 8, !tbaa !16
  store i32 3, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %69

64:                                               ; preds = %58
  br label %65

65:                                               ; preds = %64, %49
  br label %9, !llvm.loop !17

66:                                               ; preds = %9
  %67 = load ptr, ptr %4, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.RTC_HandleTypeDef, ptr %67, i32 0, i32 3
  store volatile i32 1, ptr %68, align 8, !tbaa !16
  store i32 0, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %69

69:                                               ; preds = %66, %61, %33
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  %70 = load i32, ptr %3, align 4
  ret i32 %70
}

declare i32 @HAL_GetTick() #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 = { "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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"", !6, i64 0, !13, i64 8, !10, i64 36, !10, i64 40}
!13 = !{!"", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12, !10, i64 16, !10, i64 20, !10, i64 24}
!14 = !{!15, !10, i64 12}
!15 = !{!"", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12, !10, i64 16, !10, i64 20, !10, i64 24, !10, i64 28, !10, i64 32, !10, i64 36, !10, i64 40, !10, i64 44, !10, i64 48, !10, i64 52, !10, i64 56, !10, i64 60, !10, i64 64, !10, i64 68, !10, i64 72, !10, i64 76, !10, i64 80, !10, i64 84, !10, i64 88, !10, i64 92, !10, i64 96, !10, i64 100, !10, i64 104, !10, i64 108, !10, i64 112, !10, i64 116, !10, i64 120, !10, i64 124, !10, i64 128, !10, i64 132, !10, i64 136, !10, i64 140, !10, i64 144, !10, i64 148, !10, i64 152, !10, i64 156, !10, i64 160, !10, i64 164, !10, i64 168, !10, i64 172, !10, i64 176, !10, i64 180, !10, i64 184, !10, i64 188, !10, i64 192, !10, i64 196, !10, i64 200, !10, i64 204}
!16 = !{!12, !10, i64 40}
!17 = distinct !{!17, !18}
!18 = !{!"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 range(i32 0, 4) i32 @HAL_RTCEx_PollForTimeStampEvent(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = tail call i32 @HAL_GetTick() #2
  %4 = load ptr, ptr %0, align 8, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %4, i64 12
  %6 = load volatile i32, ptr %5, align 4, !tbaa !12
  %7 = and i32 %6, 2048
  %8 = icmp eq i32 %7, 0
  br i1 %8, label %.lr.ph, label %.split20

.lr.ph:                                           ; preds = %2
  switch i32 %1, label %.lr.ph.split.split [
    i32 -1, label %.lr.ph.split.us
    i32 0, label %.lr.ph.split.split.us
  ]

.lr.ph.split.us:                                  ; preds = %.lr.ph, %11
  %9 = load volatile i32, ptr %5, align 4, !tbaa !12
  %10 = and i32 %9, 4096
  %.not.us = icmp eq i32 %10, 0
  br i1 %.not.us, label %11, label %.split.us

11:                                               ; preds = %.lr.ph.split.us
  %12 = load volatile i32, ptr %5, align 4, !tbaa !12
  %13 = and i32 %12, 2048
  %14 = icmp eq i32 %13, 0
  br i1 %14, label %.lr.ph.split.us, label %.split20, !llvm.loop !14

.lr.ph.split.split.us:                            ; preds = %.lr.ph
  %15 = load volatile i32, ptr %5, align 4, !tbaa !12
  %16 = and i32 %15, 4096
  %.not.us16 = icmp eq i32 %16, 0
  br i1 %.not.us16, label %.split20.us, label %.split.us

.split20.us:                                      ; preds = %.lr.ph.split.split.us
  %17 = tail call i32 @HAL_GetTick() #2
  br label %.split20

.lr.ph.split.split:                               ; preds = %.lr.ph, %28
  %18 = phi ptr [ %30, %28 ], [ %5, %.lr.ph ]
  %19 = load volatile i32, ptr %18, align 4, !tbaa !12
  %20 = and i32 %19, 4096
  %.not = icmp eq i32 %20, 0
  br i1 %.not, label %24, label %.split.us

.split.us:                                        ; preds = %.lr.ph.split.us, %.lr.ph.split.split, %.lr.ph.split.split.us
  %.us-phi = phi ptr [ %5, %.lr.ph.split.split.us ], [ %18, %.lr.ph.split.split ], [ %5, %.lr.ph.split.us ]
  %21 = load volatile i32, ptr %.us-phi, align 4, !tbaa !12
  %22 = and i32 %21, 128
  %23 = or disjoint i32 %22, -4225
  store volatile i32 %23, ptr %.us-phi, align 4, !tbaa !12
  br label %.split20

24:                                               ; preds = %.lr.ph.split.split
  %25 = tail call i32 @HAL_GetTick() #2
  %26 = sub i32 %25, %3
  %27 = icmp ugt i32 %26, %1
  br i1 %27, label %.split20, label %28

28:                                               ; preds = %24
  %29 = load ptr, ptr %0, align 8, !tbaa !5
  %30 = getelementptr inbounds i8, ptr %29, i64 12
  %31 = load volatile i32, ptr %30, align 4, !tbaa !12
  %32 = and i32 %31, 2048
  %33 = icmp eq i32 %32, 0
  br i1 %33, label %.lr.ph.split.split, label %.split20, !llvm.loop !14

.split20:                                         ; preds = %11, %28, %24, %2, %.split20.us, %.split.us
  %.sink = phi i32 [ 4, %.split.us ], [ 3, %.split20.us ], [ 1, %2 ], [ 3, %24 ], [ 1, %28 ], [ 1, %11 ]
  %.0 = phi i32 [ 1, %.split.us ], [ 3, %.split20.us ], [ 0, %2 ], [ 3, %24 ], [ 0, %28 ], [ 0, %11 ]
  %34 = getelementptr inbounds i8, ptr %0, i64 40
  store volatile i32 %.sink, ptr %34, align 8, !tbaa !16
  ret i32 %.0
}

declare i32 @HAL_GetTick() 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, !7, i64 0}
!6 = !{!"", !7, i64 0, !10, i64 8, !11, i64 36, !11, i64 40}
!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 = !{!"int", !8, i64 0}
!12 = !{!13, !11, i64 12}
!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, !11, i64 48, !11, i64 52, !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, !11, i64 100, !11, i64 104, !11, i64 108, !11, i64 112, !11, i64 116, !11, i64 120, !11, i64 124, !11, i64 128, !11, i64 132, !11, i64 136, !11, i64 140, !11, i64 144, !11, i64 148, !11, i64 152, !11, i64 156, !11, i64 160, !11, i64 164, !11, i64 168, !11, i64 172, !11, i64 176, !11, i64 180, !11, i64 184, !11, i64 188, !11, i64 192, !11, i64 196, !11, i64 200, !11, i64 204}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!6, !11, i64 40}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass