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

; 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.__UART_HandleTypeDef = type { ptr, %struct.UART_InitTypeDef, %struct.UART_AdvFeatureInitTypeDef, ptr, i16, i16, ptr, i16, i16, i16, i32, i16, i16, ptr, ptr, ptr, ptr, i32, i32, i32, i32 }
%struct.UART_InitTypeDef = type { i32, i32, i32, i32, i32, i32, i32, i32, i32 }
%struct.UART_AdvFeatureInitTypeDef = type { i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }
%struct.USART_TypeDef = type { i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }

declare i32 @HAL_GetTick() #0

; Function Attrs: nounwind uwtable
define dso_local i32 @UART_WaitOnFlagUntilTimeout(ptr noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4) #1 {
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store ptr %0, ptr %7, align 8, !tbaa !5
  store i32 %1, ptr %8, align 4, !tbaa !9
  store i32 %2, ptr %9, align 4, !tbaa !9
  store i32 %3, ptr %10, align 4, !tbaa !9
  store i32 %4, ptr %11, align 4, !tbaa !9
  br label %12

12:                                               ; preds = %64, %5
  %13 = load ptr, ptr %7, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.__UART_HandleTypeDef, ptr %13, i32 0, i32 0
  %15 = load ptr, ptr %14, align 8, !tbaa !11
  %16 = getelementptr inbounds %struct.USART_TypeDef, ptr %15, i32 0, i32 7
  %17 = load volatile i32, ptr %16, align 4, !tbaa !16
  %18 = load i32, ptr %8, align 4, !tbaa !9
  %19 = and i32 %17, %18
  %20 = load i32, ptr %8, align 4, !tbaa !9
  %21 = icmp eq i32 %19, %20
  %22 = zext i1 %21 to i64
  %23 = select i1 %21, i32 1, i32 0
  %24 = load i32, ptr %9, align 4, !tbaa !9
  %25 = icmp eq i32 %23, %24
  br i1 %25, label %26, label %65

26:                                               ; preds = %12
  %27 = load i32, ptr %11, align 4, !tbaa !9
  %28 = icmp ne i32 %27, -1
  br i1 %28, label %29, label %64

29:                                               ; preds = %26
  %30 = call i32 @HAL_GetTick()
  %31 = load i32, ptr %10, align 4, !tbaa !9
  %32 = sub i32 %30, %31
  %33 = load i32, ptr %11, align 4, !tbaa !9
  %34 = icmp ugt i32 %32, %33
  br i1 %34, label %38, label %35

35:                                               ; preds = %29
  %36 = load i32, ptr %11, align 4, !tbaa !9
  %37 = icmp eq i32 %36, 0
  br i1 %37, label %38, label %63

38:                                               ; preds = %35, %29
  %39 = load ptr, ptr %7, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.__UART_HandleTypeDef, ptr %39, i32 0, i32 0
  %41 = load ptr, ptr %40, align 8, !tbaa !11
  %42 = getelementptr inbounds %struct.USART_TypeDef, ptr %41, i32 0, i32 0
  %43 = load volatile i32, ptr %42, align 4, !tbaa !18
  %44 = zext i32 %43 to i64
  %45 = and i64 %44, -417
  %46 = trunc i64 %45 to i32
  store volatile i32 %46, ptr %42, align 4, !tbaa !18
  %47 = load ptr, ptr %7, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.__UART_HandleTypeDef, ptr %47, i32 0, i32 0
  %49 = load ptr, ptr %48, align 8, !tbaa !11
  %50 = getelementptr inbounds %struct.USART_TypeDef, ptr %49, i32 0, i32 2
  %51 = load volatile i32, ptr %50, align 4, !tbaa !19
  %52 = zext i32 %51 to i64
  %53 = and i64 %52, -2
  %54 = trunc i64 %53 to i32
  store volatile i32 %54, ptr %50, align 4, !tbaa !19
  %55 = load ptr, ptr %7, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.__UART_HandleTypeDef, ptr %55, i32 0, i32 18
  store volatile i32 32, ptr %56, align 4, !tbaa !20
  %57 = load ptr, ptr %7, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.__UART_HandleTypeDef, ptr %57, i32 0, i32 19
  store volatile i32 32, ptr %58, align 8, !tbaa !21
  br label %59

59:                                               ; preds = %38
  %60 = load ptr, ptr %7, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.__UART_HandleTypeDef, ptr %60, i32 0, i32 17
  store i32 0, ptr %61, align 8, !tbaa !22
  br label %62

62:                                               ; preds = %59
  store i32 3, ptr %6, align 4
  br label %66

63:                                               ; preds = %35
  br label %64

64:                                               ; preds = %63, %26
  br label %12, !llvm.loop !23

65:                                               ; preds = %12
  store i32 0, ptr %6, align 4
  br label %66

66:                                               ; preds = %65, %62
  %67 = load i32, ptr %6, align 4
  ret i32 %67
}

attributes #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 = { 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" }

!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 = !{!"__UART_HandleTypeDef", !6, i64 0, !13, i64 8, !14, i64 44, !6, i64 88, !15, i64 96, !15, i64 98, !6, i64 104, !15, i64 112, !15, i64 114, !15, i64 116, !10, i64 120, !15, i64 124, !15, i64 126, !6, i64 128, !6, i64 136, !6, i64 144, !6, i64 152, !10, i64 160, !10, i64 164, !10, i64 168, !10, i64 172}
!13 = !{!"", !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}
!14 = !{!"", !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}
!15 = !{!"short", !7, i64 0}
!16 = !{!17, !10, i64 28}
!17 = !{!"", !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}
!18 = !{!17, !10, i64 0}
!19 = !{!17, !10, i64 8}
!20 = !{!12, !10, i64 164}
!21 = !{!12, !10, i64 168}
!22 = !{!12, !10, i64 160}
!23 = distinct !{!23, !24}
!24 = !{!"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"

declare i32 @HAL_GetTick() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 4) i32 @UART_WaitOnFlagUntilTimeout(ptr noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4) local_unnamed_addr #1 {
  %6 = load ptr, ptr %0, align 8, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %6, i64 28
  %8 = load volatile i32, ptr %7, align 4, !tbaa !14
  %9 = and i32 %8, %1
  %10 = icmp eq i32 %9, %1
  %11 = zext i1 %10 to i32
  %12 = icmp eq i32 %11, %2
  br i1 %12, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %5
  switch i32 %4, 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
  %13 = load volatile i32, ptr %7, align 4, !tbaa !14
  %14 = and i32 %13, %1
  %15 = icmp eq i32 %14, %1
  %16 = zext i1 %15 to i32
  %17 = icmp eq i32 %16, %2
  br i1 %17, label %.lr.ph.split.us, label %.loopexit, !llvm.loop !16

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

.lr.ph.split.split:                               ; preds = %.lr.ph, %31
  %19 = tail call i32 @HAL_GetTick() #2
  %20 = sub i32 %19, %3
  %21 = icmp ugt i32 %20, %4
  br i1 %21, label %.split, label %31

.split:                                           ; preds = %.lr.ph.split.split, %.lr.ph.split.split.us
  %22 = load ptr, ptr %0, align 8, !tbaa !5
  %23 = load volatile i32, ptr %22, align 4, !tbaa !18
  %24 = and i32 %23, -417
  store volatile i32 %24, ptr %22, align 4, !tbaa !18
  %25 = getelementptr inbounds i8, ptr %22, i64 8
  %26 = load volatile i32, ptr %25, align 4, !tbaa !19
  %27 = and i32 %26, -2
  store volatile i32 %27, ptr %25, align 4, !tbaa !19
  %28 = getelementptr inbounds i8, ptr %0, i64 164
  store volatile i32 32, ptr %28, align 4, !tbaa !20
  %29 = getelementptr inbounds i8, ptr %0, i64 168
  store volatile i32 32, ptr %29, align 8, !tbaa !21
  %30 = getelementptr inbounds i8, ptr %0, i64 160
  store i32 0, ptr %30, align 8, !tbaa !22
  br label %.loopexit

31:                                               ; preds = %.lr.ph.split.split
  %32 = load ptr, ptr %0, align 8, !tbaa !5
  %33 = getelementptr inbounds i8, ptr %32, i64 28
  %34 = load volatile i32, ptr %33, align 4, !tbaa !14
  %35 = and i32 %34, %1
  %36 = icmp eq i32 %35, %1
  %37 = zext i1 %36 to i32
  %38 = icmp eq i32 %37, %2
  br i1 %38, label %.lr.ph.split.split, label %.loopexit, !llvm.loop !16

.loopexit:                                        ; preds = %.lr.ph.split.us, %31, %5, %.split
  %.0 = phi i32 [ 3, %.split ], [ 0, %5 ], [ 0, %31 ], [ 0, %.lr.ph.split.us ]
  ret i32 %.0
}

attributes #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 = { 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 = { 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 = !{!"__UART_HandleTypeDef", !7, i64 0, !10, i64 8, !12, i64 44, !7, i64 88, !13, i64 96, !13, i64 98, !7, i64 104, !13, i64 112, !13, i64 114, !13, i64 116, !11, i64 120, !13, i64 124, !13, i64 126, !7, i64 128, !7, i64 136, !7, i64 144, !7, i64 152, !11, i64 160, !11, i64 164, !11, i64 168, !11, i64 172}
!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 = !{!"int", !8, i64 0}
!12 = !{!"", !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}
!13 = !{!"short", !8, i64 0}
!14 = !{!15, !11, i64 28}
!15 = !{!"", !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}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = !{!15, !11, i64 0}
!19 = !{!15, !11, i64 8}
!20 = !{!6, !11, i64 164}
!21 = !{!6, !11, i64 168}
!22 = !{!6, !11, i64 160}


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