; 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_CRC_WaitForLastOperation(i32 noundef %0, i32 noundef %1) #1 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %9 = call i32 @HAL_GetTick()
  store i32 %9, ptr %7, align 4, !tbaa !5
  %10 = load i32, ptr %5, align 4, !tbaa !5
  %11 = icmp eq i32 %10, 1
  br i1 %11, label %12, label %13

12:                                               ; preds = %2
  store i32 8, ptr %6, align 4, !tbaa !5
  br label %14

13:                                               ; preds = %2
  store i32 -2147483640, ptr %6, align 4, !tbaa !5
  br label %14

14:                                               ; preds = %13, %12
  br label %15

15:                                               ; preds = %55, %14
  %16 = load i32, ptr %6, align 4, !tbaa !5
  %17 = zext i32 %16 to i64
  %18 = and i64 %17, 535756815
  %19 = load i32, ptr %6, align 4, !tbaa !5
  %20 = zext i32 %19 to i64
  %21 = icmp eq i64 %18, %20
  br i1 %21, label %22, label %29

22:                                               ; preds = %15
  %23 = load volatile i32, ptr getelementptr inbounds (%struct.FLASH_TypeDef, ptr inttoptr (i64 1375739904 to ptr), i32 0, i32 4), align 4, !tbaa !9
  %24 = load i32, ptr %6, align 4, !tbaa !5
  %25 = and i32 %23, %24
  %26 = load i32, ptr %6, align 4, !tbaa !5
  %27 = icmp eq i32 %25, %26
  %28 = zext i1 %27 to i32
  br label %38

29:                                               ; preds = %15
  %30 = load volatile i32, ptr getelementptr inbounds (%struct.FLASH_TypeDef, ptr inttoptr (i64 1375739904 to ptr), i32 0, i32 28), align 4, !tbaa !11
  %31 = load i32, ptr %6, align 4, !tbaa !5
  %32 = and i32 %31, 2147483647
  %33 = and i32 %30, %32
  %34 = load i32, ptr %6, align 4, !tbaa !5
  %35 = and i32 %34, 2147483647
  %36 = icmp eq i32 %33, %35
  %37 = zext i1 %36 to i32
  br label %38

38:                                               ; preds = %29, %22
  %39 = phi i32 [ %28, %22 ], [ %37, %29 ]
  %40 = icmp ne i32 %39, 0
  br i1 %40, label %41, label %56

41:                                               ; preds = %38
  %42 = load i32, ptr %4, align 4, !tbaa !5
  %43 = icmp ne i32 %42, -1
  br i1 %43, label %44, label %55

44:                                               ; preds = %41
  %45 = call i32 @HAL_GetTick()
  %46 = load i32, ptr %7, align 4, !tbaa !5
  %47 = sub i32 %45, %46
  %48 = load i32, ptr %4, align 4, !tbaa !5
  %49 = icmp ugt i32 %47, %48
  br i1 %49, label %53, label %50

50:                                               ; preds = %44
  %51 = load i32, ptr %4, align 4, !tbaa !5
  %52 = icmp eq i32 %51, 0
  br i1 %52, label %53, label %54

53:                                               ; preds = %50, %44
  store i32 3, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %82

54:                                               ; preds = %50
  br label %55

55:                                               ; preds = %54, %41
  br label %15, !llvm.loop !12

56:                                               ; preds = %38
  %57 = load i32, ptr %5, align 4, !tbaa !5
  %58 = icmp eq i32 %57, 1
  br i1 %58, label %59, label %70

59:                                               ; preds = %56
  %60 = load volatile i32, ptr getelementptr inbounds (%struct.FLASH_TypeDef, ptr inttoptr (i64 1375739904 to ptr), i32 0, i32 4), align 4, !tbaa !9
  %61 = zext i32 %60 to i64
  %62 = and i64 %61, 268435456
  %63 = icmp eq i64 %62, 268435456
  br i1 %63, label %64, label %69

64:                                               ; preds = %59
  %65 = load volatile i32, ptr getelementptr inbounds (%struct.FLASH_ProcessTypeDef, ptr @pFlash, i32 0, i32 6), align 4, !tbaa !14
  %66 = zext i32 %65 to i64
  %67 = or i64 %66, 268435456
  %68 = trunc i64 %67 to i32
  store volatile i32 %68, ptr getelementptr inbounds (%struct.FLASH_ProcessTypeDef, ptr @pFlash, i32 0, i32 6), align 4, !tbaa !14
  store volatile i32 268435456, ptr getelementptr inbounds (%struct.FLASH_TypeDef, ptr inttoptr (i64 1375739904 to ptr), i32 0, i32 5), align 4, !tbaa !16
  store i32 1, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %82

69:                                               ; preds = %59
  br label %81

70:                                               ; preds = %56
  %71 = load volatile i32, ptr getelementptr inbounds (%struct.FLASH_TypeDef, ptr inttoptr (i64 1375739904 to ptr), i32 0, i32 28), align 4, !tbaa !11
  %72 = zext i32 %71 to i64
  %73 = and i64 %72, 268435456
  %74 = icmp eq i64 %73, 268435456
  br i1 %74, label %75, label %80

75:                                               ; preds = %70
  %76 = load volatile i32, ptr getelementptr inbounds (%struct.FLASH_ProcessTypeDef, ptr @pFlash, i32 0, i32 6), align 4, !tbaa !14
  %77 = zext i32 %76 to i64
  %78 = or i64 %77, 2415919104
  %79 = trunc i64 %78 to i32
  store volatile i32 %79, ptr getelementptr inbounds (%struct.FLASH_ProcessTypeDef, ptr @pFlash, i32 0, i32 6), align 4, !tbaa !14
  store volatile i32 268435456, ptr getelementptr inbounds (%struct.FLASH_TypeDef, ptr inttoptr (i64 1375739904 to ptr), i32 0, i32 29), align 4, !tbaa !17
  store i32 1, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %82

80:                                               ; preds = %70
  br label %81

81:                                               ; preds = %80, %69
  store i32 0, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %82

82:                                               ; preds = %81, %75, %64, %53
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  %83 = load i32, ptr %3, align 4
  ret i32 %83
}

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 16}
!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 = !{!10, !6, i64 272}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!15, !6, i64 24}
!15 = !{!"", !6, i64 0, !6, i64 4, !6, i64 8, !6, i64 12, !6, i64 16, !6, i64 20, !6, i64 24}
!16 = !{!10, !6, i64 20}
!17 = !{!10, !6, i64 276}


; 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_CRC_WaitForLastOperation(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = tail call i32 @HAL_GetTick() #2
  %4 = icmp eq i32 %1, 1
  %.not18 = icmp eq i32 %0, -1
  %5 = icmp eq i32 %0, 0
  br i1 %4, label %.split.us, label %.split

.split.us:                                        ; preds = %2
  %6 = load volatile i32, ptr inttoptr (i64 1375739920 to ptr), align 16, !tbaa !5
  %7 = and i32 %6, 8
  %.not.us25 = icmp eq i32 %7, 0
  br i1 %.not.us25, label %.split22.us, label %.lr.ph26

.lr.ph26:                                         ; preds = %.split.us
  br i1 %.not18, label %.lr.ph26.split.us, label %.lr.ph26.split

.lr.ph26.split.us:                                ; preds = %.lr.ph26, %.lr.ph26.split.us
  %8 = load volatile i32, ptr inttoptr (i64 1375739920 to ptr), align 16, !tbaa !5
  %9 = and i32 %8, 8
  %.not.us.us = icmp eq i32 %9, 0
  br i1 %.not.us.us, label %.split22.us, label %.lr.ph26.split.us, !llvm.loop !10

.lr.ph26.split:                                   ; preds = %.lr.ph26
  br i1 %5, label %.lr.ph26.split.split.us, label %.lr.ph26.split.split

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

.lr.ph26.split.split:                             ; preds = %.lr.ph26.split, %14
  %11 = tail call i32 @HAL_GetTick() #2
  %12 = sub i32 %11, %3
  %13 = icmp ugt i32 %12, %0
  br i1 %13, label %.loopexit, label %14

14:                                               ; preds = %.lr.ph26.split.split
  %15 = load volatile i32, ptr inttoptr (i64 1375739920 to ptr), align 16, !tbaa !5
  %16 = and i32 %15, 8
  %.not.us = icmp eq i32 %16, 0
  br i1 %.not.us, label %.split22.us, label %.lr.ph26.split.split, !llvm.loop !10

.split:                                           ; preds = %2
  %17 = load volatile i32, ptr inttoptr (i64 1375740176 to ptr), align 16, !tbaa !12
  %18 = and i32 %17, 8
  %.not1923 = icmp eq i32 %18, 0
  br i1 %.not1923, label %.thread20, label %.lr.ph

.lr.ph:                                           ; preds = %.split
  br i1 %.not18, label %.thread.us, label %.lr.ph.split

.thread.us:                                       ; preds = %.lr.ph, %.thread.us
  %19 = load volatile i32, ptr inttoptr (i64 1375740176 to ptr), align 16, !tbaa !12
  %20 = and i32 %19, 8
  %.not19.us = icmp eq i32 %20, 0
  br i1 %.not19.us, label %.thread20, label %.thread.us, !llvm.loop !10

.lr.ph.split:                                     ; preds = %.lr.ph
  br i1 %5, label %.lr.ph.split.split.us, label %.lr.ph.split.split

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

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

.thread:                                          ; preds = %.lr.ph.split.split
  %25 = load volatile i32, ptr inttoptr (i64 1375740176 to ptr), align 16, !tbaa !12
  %26 = and i32 %25, 8
  %.not19 = icmp eq i32 %26, 0
  br i1 %.not19, label %.thread20, label %.lr.ph.split.split, !llvm.loop !10

.split22.us:                                      ; preds = %14, %.lr.ph26.split.us, %.split.us
  %27 = load volatile i32, ptr inttoptr (i64 1375739920 to ptr), align 16, !tbaa !5
  %28 = and i32 %27, 268435456
  %.not17 = icmp eq i32 %28, 0
  br i1 %.not17, label %.loopexit, label %29

29:                                               ; preds = %.split22.us
  %30 = load volatile i32, ptr getelementptr inbounds (i8, ptr @pFlash, i64 24), align 4, !tbaa !13
  %31 = or i32 %30, 268435456
  store volatile i32 %31, ptr getelementptr inbounds (i8, ptr @pFlash, i64 24), align 4, !tbaa !13
  store volatile i32 268435456, ptr inttoptr (i64 1375739924 to ptr), align 4, !tbaa !15
  br label %.loopexit

.thread20:                                        ; preds = %.thread, %.thread.us, %.split
  %32 = load volatile i32, ptr inttoptr (i64 1375740176 to ptr), align 16, !tbaa !12
  %33 = and i32 %32, 268435456
  %.not16 = icmp eq i32 %33, 0
  br i1 %.not16, label %.loopexit, label %34

34:                                               ; preds = %.thread20
  %35 = load volatile i32, ptr getelementptr inbounds (i8, ptr @pFlash, i64 24), align 4, !tbaa !13
  %36 = or i32 %35, -1879048192
  store volatile i32 %36, ptr getelementptr inbounds (i8, ptr @pFlash, i64 24), align 4, !tbaa !13
  store volatile i32 268435456, ptr inttoptr (i64 1375740180 to ptr), align 4, !tbaa !16
  br label %.loopexit

.loopexit:                                        ; preds = %.lr.ph.split.split, %.lr.ph26.split.split, %.lr.ph26.split.split.us, %.lr.ph.split.split.us, %.split22.us, %.thread20, %34, %29
  %.0 = phi i32 [ 1, %29 ], [ 1, %34 ], [ 0, %.thread20 ], [ 0, %.split22.us ], [ 3, %.lr.ph.split.split.us ], [ 3, %.lr.ph26.split.split.us ], [ 3, %.lr.ph26.split.split ], [ 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 16}
!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 = !{!6, !7, i64 272}
!13 = !{!14, !7, i64 24}
!14 = !{!"", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12, !7, i64 16, !7, i64 20, !7, i64 24}
!15 = !{!6, !7, i64 20}
!16 = !{!6, !7, i64 276}


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