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

; 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.FLASH_ProcessTypeDef = type { i32, i32, i32, i32, i32, i32, i32 }
%struct.FLASH_TypeDef = type { i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, [2 x i32], i32, i32, i32, i32, i32, [40 x i32], i32, i32, i32, i32, i32, [4 x i32], i32, i32, i32, i32, i32, i32, [4 x i32], i32, i32, i32, i32, i32 }

@pFlash = external dso_local global %struct.FLASH_ProcessTypeDef, align 4

; 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 @FLASH_OB_WaitForLastOperation(i32 noundef %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  %6 = call i32 @HAL_GetTick()
  store i32 %6, ptr %4, align 4, !tbaa !5
  br label %7

7:                                                ; preds = %26, %1
  %8 = load volatile i32, ptr getelementptr inbounds (%struct.FLASH_TypeDef, ptr inttoptr (i64 1375739904 to ptr), i32 0, i32 7), align 4, !tbaa !9
  %9 = zext i32 %8 to i64
  %10 = and i64 %9, 1
  %11 = icmp ne i64 %10, 0
  br i1 %11, label %12, label %27

12:                                               ; preds = %7
  %13 = load i32, ptr %3, align 4, !tbaa !5
  %14 = icmp ne i32 %13, -1
  br i1 %14, label %15, label %26

15:                                               ; preds = %12
  %16 = call i32 @HAL_GetTick()
  %17 = load i32, ptr %4, align 4, !tbaa !5
  %18 = sub i32 %16, %17
  %19 = load i32, ptr %3, align 4, !tbaa !5
  %20 = icmp ugt i32 %18, %19
  br i1 %20, label %24, label %21

21:                                               ; preds = %15
  %22 = load i32, ptr %3, align 4, !tbaa !5
  %23 = icmp eq i32 %22, 0
  br i1 %23, label %24, label %25

24:                                               ; preds = %21, %15
  store i32 3, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %42

25:                                               ; preds = %21
  br label %26

26:                                               ; preds = %25, %12
  br label %7, !llvm.loop !11

27:                                               ; preds = %7
  %28 = load volatile i32, ptr getelementptr inbounds (%struct.FLASH_TypeDef, ptr inttoptr (i64 1375739904 to ptr), i32 0, i32 7), align 4, !tbaa !9
  %29 = zext i32 %28 to i64
  %30 = and i64 %29, 1073741824
  %31 = icmp ne i64 %30, 0
  br i1 %31, label %32, label %41

32:                                               ; preds = %27
  %33 = load volatile i32, ptr getelementptr inbounds (%struct.FLASH_ProcessTypeDef, ptr @pFlash, i32 0, i32 6), align 4, !tbaa !13
  %34 = zext i32 %33 to i64
  %35 = or i64 %34, 1073741824
  %36 = trunc i64 %35 to i32
  store volatile i32 %36, ptr getelementptr inbounds (%struct.FLASH_ProcessTypeDef, ptr @pFlash, i32 0, i32 6), align 4, !tbaa !13
  %37 = load volatile i32, ptr getelementptr inbounds (%struct.FLASH_TypeDef, ptr inttoptr (i64 1375739904 to ptr), i32 0, i32 9), align 4, !tbaa !15
  %38 = zext i32 %37 to i64
  %39 = or i64 %38, 1073741824
  %40 = trunc i64 %39 to i32
  store volatile i32 %40, ptr getelementptr inbounds (%struct.FLASH_TypeDef, ptr inttoptr (i64 1375739904 to ptr), i32 0, i32 9), align 4, !tbaa !15
  store i32 1, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %42

41:                                               ; preds = %27
  store i32 0, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %42

42:                                               ; preds = %41, %32, %24
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  %43 = load i32, ptr %2, align 4
  ret i32 %43
}

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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 28}
!10 = !{!"", !6, i64 0, !6, i64 4, !6, i64 8, !6, i64 12, !6, i64 16, !6, i64 20, !6, i64 24, !6, i64 28, !6, i64 32, !6, i64 36, !6, i64 40, !6, i64 44, !6, i64 48, !6, i64 52, !6, i64 56, !6, i64 60, !6, i64 64, !6, i64 68, !7, i64 72, !6, i64 80, !6, i64 84, !6, i64 88, !6, i64 92, !6, i64 96, !7, i64 100, !6, i64 260, !6, i64 264, !6, i64 268, !6, i64 272, !6, i64 276, !7, i64 280, !6, i64 296, !6, i64 300, !6, i64 304, !6, i64 308, !6, i64 312, !6, i64 316, !7, i64 320, !6, i64 336, !6, i64 340, !6, i64 344, !6, i64 348, !6, i64 352}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!14, !6, i64 24}
!14 = !{!"", !6, i64 0, !6, i64 4, !6, i64 8, !6, i64 12, !6, i64 16, !6, i64 20, !6, i64 24}
!15 = !{!10, !6, i64 36}


; 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"

%struct.FLASH_ProcessTypeDef = type { i32, i32, i32, i32, i32, i32, i32 }

@pFlash = external dso_local global %struct.FLASH_ProcessTypeDef, align 4

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 4) i32 @FLASH_OB_WaitForLastOperation(i32 noundef %0) local_unnamed_addr #0 {
  %2 = tail call i32 @HAL_GetTick() #2
  %3 = load volatile i32, ptr inttoptr (i64 1375739932 to ptr), align 4, !tbaa !5
  %4 = and i32 %3, 1
  %.not9 = icmp eq i32 %4, 0
  br i1 %.not9, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %1
  switch i32 %0, 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
  %5 = load volatile i32, ptr inttoptr (i64 1375739932 to ptr), align 4, !tbaa !5
  %6 = and i32 %5, 1
  %.not.us = icmp eq i32 %6, 0
  br i1 %.not.us, label %._crit_edge, label %.lr.ph.split.us, !llvm.loop !10

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

.lr.ph.split.split:                               ; preds = %.lr.ph, %11
  %8 = tail call i32 @HAL_GetTick() #2
  %9 = sub i32 %8, %2
  %10 = icmp ugt i32 %9, %0
  br i1 %10, label %.loopexit, label %11

11:                                               ; preds = %.lr.ph.split.split
  %12 = load volatile i32, ptr inttoptr (i64 1375739932 to ptr), align 4, !tbaa !5
  %13 = and i32 %12, 1
  %.not = icmp eq i32 %13, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph.split.split, !llvm.loop !10

._crit_edge:                                      ; preds = %.lr.ph.split.us, %11, %1
  %14 = load volatile i32, ptr inttoptr (i64 1375739932 to ptr), align 4, !tbaa !5
  %15 = and i32 %14, 1073741824
  %.not7 = icmp eq i32 %15, 0
  br i1 %.not7, label %.loopexit, label %16

16:                                               ; preds = %._crit_edge
  %17 = load volatile i32, ptr getelementptr inbounds (i8, ptr @pFlash, i64 24), align 4, !tbaa !12
  %18 = or i32 %17, 1073741824
  store volatile i32 %18, ptr getelementptr inbounds (i8, ptr @pFlash, i64 24), align 4, !tbaa !12
  %19 = load volatile i32, ptr inttoptr (i64 1375739940 to ptr), align 4, !tbaa !14
  %20 = or i32 %19, 1073741824
  store volatile i32 %20, ptr inttoptr (i64 1375739940 to ptr), align 4, !tbaa !14
  br label %.loopexit

.loopexit:                                        ; preds = %.lr.ph.split.split, %.lr.ph.split.split.us, %._crit_edge, %16
  %.0 = phi i32 [ 1, %16 ], [ 0, %._crit_edge ], [ 3, %.lr.ph.split.split.us ], [ 3, %.lr.ph.split.split ]
  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 28}
!6 = !{!"", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12, !7, i64 16, !7, i64 20, !7, i64 24, !7, i64 28, !7, i64 32, !7, i64 36, !7, i64 40, !7, i64 44, !7, i64 48, !7, i64 52, !7, i64 56, !7, i64 60, !7, i64 64, !7, i64 68, !8, i64 72, !7, i64 80, !7, i64 84, !7, i64 88, !7, i64 92, !7, i64 96, !8, i64 100, !7, i64 260, !7, i64 264, !7, i64 268, !7, i64 272, !7, i64 276, !8, i64 280, !7, i64 296, !7, i64 300, !7, i64 304, !7, i64 308, !7, i64 312, !7, i64 316, !8, i64 320, !7, i64 336, !7, i64 340, !7, i64 344, !7, i64 348, !7, i64 352}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = !{!13, !7, i64 24}
!13 = !{!"", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12, !7, i64 16, !7, i64 20, !7, i64 24}
!14 = !{!6, !7, i64 36}


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