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

; 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_ChannelPollForCkab(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 %51

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 = %44, %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, 16711680
  %24 = load i32, ptr %7, align 4, !tbaa !9
  %25 = add i32 16, %24
  %26 = zext i32 %25 to i64
  %27 = lshr i64 %23, %26
  %28 = and i64 %27, 1
  %29 = icmp eq i64 %28, 0
  br i1 %29, label %30, label %45

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

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

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

42:                                               ; preds = %39, %33
  store i32 3, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %51

43:                                               ; preds = %39
  br label %44

44:                                               ; preds = %43, %30
  br label %20, !llvm.loop !19

45:                                               ; preds = %20
  %46 = load i32, ptr %7, align 4, !tbaa !9
  %47 = add i32 16, %46
  %48 = zext i32 %47 to i64
  %49 = shl i64 1, %48
  %50 = trunc i64 %49 to i32
  store volatile i32 %50, 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 %51

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

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_ChannelPollForCkab(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, 16
  %10 = zext nneg i32 %9 to i64
  %11 = shl nuw i64 1, %10
  %12 = load volatile i32, ptr inttoptr (i64 1073836296 to ptr), align 8, !tbaa !15
  %13 = and i32 %12, 16711680
  %14 = zext nneg i32 %13 to i64
  %15 = and i64 %11, %14
  %16 = icmp eq i64 %15, 0
  br i1 %16, 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
  %17 = load volatile i32, ptr inttoptr (i64 1073836296 to ptr), align 8, !tbaa !15
  %18 = and i32 %17, 16711680
  %19 = zext nneg i32 %18 to i64
  %20 = and i64 %11, %19
  %21 = icmp eq i64 %20, 0
  br i1 %21, label %.lr.ph.split.us, label %._crit_edge, !llvm.loop !17

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

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

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

._crit_edge:                                      ; preds = %.lr.ph.split.us, %26, %5
  %32 = trunc i64 %11 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