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

; 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.DFSDM_Channel_HandleTypeDef = type { ptr, %struct.DFSDM_Channel_InitTypeDef, i32 }
%struct.DFSDM_Channel_InitTypeDef = type { %struct.DFSDM_Channel_OutputClockTypeDef, %struct.DFSDM_Channel_InputTypeDef, %struct.DFSDM_Channel_SerialInterfaceTypeDef, %struct.DFSDM_Channel_AwdTypeDef, i32, i32 }
%struct.DFSDM_Channel_OutputClockTypeDef = type { i32, i32, i32 }
%struct.DFSDM_Channel_InputTypeDef = type { i32, i32, i32 }
%struct.DFSDM_Channel_SerialInterfaceTypeDef = type { i32, i32 }
%struct.DFSDM_Channel_AwdTypeDef = type { i32, i32 }
%struct.DFSDM_Filter_TypeDef = type { i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }

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

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

declare i32 @HAL_GetTick() #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @HAL_DFSDM_ChannelPollForScd(ptr noundef %0, i32 noundef %1) #0 {
  %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
  %8 = 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
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.DFSDM_Channel_HandleTypeDef, ptr %9, i32 0, i32 2
  %11 = load i32, ptr %10, align 8, !tbaa !11
  %12 = icmp ne i32 %11, 1
  br i1 %12, label %13, label %14

13:                                               ; preds = %2
  store i32 1, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %50

14:                                               ; preds = %2
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.DFSDM_Channel_HandleTypeDef, ptr %15, i32 0, i32 0
  %17 = load ptr, ptr %16, align 8, !tbaa !16
  %18 = call i32 @DFSDM_GetChannelFromInstance(ptr noundef %17)
  store i32 %18, ptr %7, align 4, !tbaa !9
  %19 = call i32 @HAL_GetTick()
  store i32 %19, ptr %6, align 4, !tbaa !9
  br label %20

20:                                               ; preds = %43, %14
  %21 = load volatile i32, ptr getelementptr inbounds (%struct.DFSDM_Filter_TypeDef, ptr inttoptr (i64 1073836288 to ptr), i32 0, i32 2), align 4, !tbaa !17
  %22 = zext i32 %21 to i64
  %23 = and i64 %22, 4278190080
  %24 = load i32, ptr %7, align 4, !tbaa !9
  %25 = add i32 24, %24
  %26 = zext i32 %25 to i64
  %27 = lshr i64 %23, %26
  %28 = icmp eq i64 %27, 0
  br i1 %28, label %29, label %44

29:                                               ; preds = %20
  %30 = load i32, ptr %5, align 4, !tbaa !9
  %31 = icmp ne i32 %30, -1
  br i1 %31, label %32, label %43

32:                                               ; preds = %29
  %33 = call i32 @HAL_GetTick()
  %34 = load i32, ptr %6, align 4, !tbaa !9
  %35 = sub i32 %33, %34
  %36 = load i32, ptr %5, align 4, !tbaa !9
  %37 = icmp ugt i32 %35, %36
  br i1 %37, label %41, label %38

38:                                               ; preds = %32
  %39 = load i32, ptr %5, align 4, !tbaa !9
  %40 = icmp eq i32 %39, 0
  br i1 %40, label %41, label %42

41:                                               ; preds = %38, %32
  store i32 3, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %50

42:                                               ; preds = %38
  br label %43

43:                                               ; preds = %42, %29
  br label %20, !llvm.loop !19

44:                                               ; preds = %20
  %45 = load i32, ptr %7, align 4, !tbaa !9
  %46 = add i32 24, %45
  %47 = zext i32 %46 to i64
  %48 = shl i64 1, %47
  %49 = trunc i64 %48 to i32
  store volatile i32 %49, ptr getelementptr inbounds (%struct.DFSDM_Filter_TypeDef, ptr inttoptr (i64 1073836288 to ptr), i32 0, i32 3), align 4, !tbaa !21
  store i32 0, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %50

50:                                               ; preds = %44, %41, %13
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  %51 = load i32, ptr %3, align 4
  ret i32 %51
}

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 = { "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, !10, i64 56}
!12 = !{!"", !6, i64 0, !13, i64 8, !10, i64 56}
!13 = !{!"", !14, i64 0, !14, i64 12, !15, i64 24, !15, i64 32, !10, i64 40, !10, i64 44}
!14 = !{!"", !10, i64 0, !10, i64 4, !10, i64 8}
!15 = !{!"", !10, i64 0, !10, i64 4}
!16 = !{!12, !6, i64 0}
!17 = !{!18, !10, i64 8}
!18 = !{!"", !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}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}
!21 = !{!18, !10, i64 12}


; 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
declare hidden i32 @DFSDM_GetChannelFromInstance(ptr noundef) local_unnamed_addr #0

declare i32 @HAL_GetTick() local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 4) i32 @HAL_DFSDM_ChannelPollForScd(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 56
  %4 = load i32, ptr %3, align 8, !tbaa !5
  %.not = icmp eq i32 %4, 1
  br i1 %.not, label %5, label %.loopexit

5:                                                ; preds = %2
  %6 = load ptr, ptr %0, align 8, !tbaa !14
  %7 = tail call i32 @DFSDM_GetChannelFromInstance(ptr noundef %6)
  %8 = tail call i32 @HAL_GetTick() #2
  %9 = add i32 %7, 24
  %10 = zext i32 %9 to i64
  %11 = load volatile i32, ptr inttoptr (i64 1073836296 to ptr), align 8, !tbaa !15
  %12 = and i32 %11, -16777216
  %13 = zext i32 %12 to i64
  %14 = lshr i64 %13, %10
  %15 = icmp eq i64 %14, 0
  br i1 %15, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %5
  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, %.lr.ph.split.us
  %16 = load volatile i32, ptr inttoptr (i64 1073836296 to ptr), align 8, !tbaa !15
  %17 = and i32 %16, -16777216
  %18 = zext i32 %17 to i64
  %19 = lshr i64 %18, %10
  %20 = icmp eq i64 %19, 0
  br i1 %20, label %.lr.ph.split.us, label %._crit_edge, !llvm.loop !17

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

.lr.ph.split.split:                               ; preds = %.lr.ph, %25
  %22 = tail call i32 @HAL_GetTick() #2
  %23 = sub i32 %22, %8
  %24 = icmp ugt i32 %23, %1
  br i1 %24, label %.loopexit, label %25

25:                                               ; preds = %.lr.ph.split.split
  %26 = load volatile i32, ptr inttoptr (i64 1073836296 to ptr), align 8, !tbaa !15
  %27 = and i32 %26, -16777216
  %28 = zext i32 %27 to i64
  %29 = lshr i64 %28, %10
  %30 = icmp eq i64 %29, 0
  br i1 %30, label %.lr.ph.split.split, label %._crit_edge, !llvm.loop !17

._crit_edge:                                      ; preds = %.lr.ph.split.us, %25, %5
  %31 = shl nuw i64 1, %10
  %32 = trunc i64 %31 to i32
  store volatile i32 %32, ptr inttoptr (i64 1073836300 to ptr), align 4, !tbaa !19
  br label %.loopexit

.loopexit:                                        ; preds = %.lr.ph.split.split, %.lr.ph.split.split.us, %2, %._crit_edge
  %.0 = phi i32 [ 0, %._crit_edge ], [ 1, %2 ], [ 3, %.lr.ph.split.split.us ], [ 3, %.lr.ph.split.split ]
  ret i32 %.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, !12, i64 56}
!6 = !{!"", !7, i64 0, !10, i64 8, !12, i64 56}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"", !11, i64 0, !11, i64 12, !13, i64 24, !13, i64 32, !12, i64 40, !12, i64 44}
!11 = !{!"", !12, i64 0, !12, i64 4, !12, i64 8}
!12 = !{!"int", !8, i64 0}
!13 = !{!"", !12, i64 0, !12, i64 4}
!14 = !{!6, !7, i64 0}
!15 = !{!16, !12, i64 8}
!16 = !{!"", !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, i64 52, !12, i64 56}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = !{!16, !12, i64 12}


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