; LLVM IR from reazon-research/OpenArm :: control/canfd_control/Drivers/STM32H7xx_HAL_Driver/Src (stm32h7xx_hal_flash)

; 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, [3 x 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: nounwind uwtable
define dso_local i32 @FLASH_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
  %9 = 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
  store i32 4, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 0, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %10 = call i32 @HAL_GetTick()
  store i32 %10, ptr %8, align 4, !tbaa !5
  br label %11

11:                                               ; preds = %31, %2
  %12 = load volatile i32, ptr getelementptr inbounds (%struct.FLASH_TypeDef, ptr inttoptr (i64 1375739904 to ptr), i32 0, i32 4), align 4, !tbaa !9
  %13 = load i32, ptr %6, align 4, !tbaa !5
  %14 = and i32 %12, %13
  %15 = load i32, ptr %6, align 4, !tbaa !5
  %16 = icmp eq i32 %14, %15
  br i1 %16, label %17, label %32

17:                                               ; preds = %11
  %18 = load i32, ptr %4, align 4, !tbaa !5
  %19 = icmp ne i32 %18, -1
  br i1 %19, label %20, label %31

20:                                               ; preds = %17
  %21 = call i32 @HAL_GetTick()
  %22 = load i32, ptr %8, align 4, !tbaa !5
  %23 = sub i32 %21, %22
  %24 = load i32, ptr %4, align 4, !tbaa !5
  %25 = icmp ugt i32 %23, %24
  br i1 %25, label %29, label %26

26:                                               ; preds = %20
  %27 = load i32, ptr %4, align 4, !tbaa !5
  %28 = icmp eq i32 %27, 0
  br i1 %28, label %29, label %30

29:                                               ; preds = %26, %20
  store i32 3, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %60

30:                                               ; preds = %26
  br label %31

31:                                               ; preds = %30, %17
  br label %11, !llvm.loop !11

32:                                               ; preds = %11
  %33 = load i32, ptr %5, align 4, !tbaa !5
  %34 = icmp eq i32 %33, 1
  br i1 %34, label %35, label %40

35:                                               ; preds = %32
  %36 = load volatile i32, ptr getelementptr inbounds (%struct.FLASH_TypeDef, ptr inttoptr (i64 1375739904 to ptr), i32 0, i32 4), align 4, !tbaa !9
  %37 = zext i32 %36 to i64
  %38 = and i64 %37, 401473536
  %39 = trunc i64 %38 to i32
  store i32 %39, ptr %7, align 4, !tbaa !5
  br label %40

40:                                               ; preds = %35, %32
  %41 = load i32, ptr %7, align 4, !tbaa !5
  %42 = and i32 %41, 2147483647
  %43 = icmp ne i32 %42, 0
  br i1 %43, label %44, label %49

44:                                               ; preds = %40
  %45 = load i32, ptr %7, align 4, !tbaa !5
  %46 = load volatile i32, ptr getelementptr inbounds (%struct.FLASH_ProcessTypeDef, ptr @pFlash, i32 0, i32 6), align 4, !tbaa !13
  %47 = or i32 %46, %45
  store volatile i32 %47, ptr getelementptr inbounds (%struct.FLASH_ProcessTypeDef, ptr @pFlash, i32 0, i32 6), align 4, !tbaa !13
  %48 = load i32, ptr %7, align 4, !tbaa !5
  store volatile i32 %48, ptr getelementptr inbounds (%struct.FLASH_TypeDef, ptr inttoptr (i64 1375739904 to ptr), i32 0, i32 5), align 4, !tbaa !15
  store i32 1, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %60

49:                                               ; preds = %40
  %50 = load i32, ptr %5, align 4, !tbaa !5
  %51 = icmp eq i32 %50, 1
  br i1 %51, label %52, label %59

52:                                               ; preds = %49
  %53 = load volatile i32, ptr getelementptr inbounds (%struct.FLASH_TypeDef, ptr inttoptr (i64 1375739904 to ptr), i32 0, i32 4), align 4, !tbaa !9
  %54 = zext i32 %53 to i64
  %55 = and i64 %54, 65536
  %56 = icmp eq i64 %55, 65536
  br i1 %56, label %57, label %58

57:                                               ; preds = %52
  store volatile i32 65536, ptr getelementptr inbounds (%struct.FLASH_TypeDef, ptr inttoptr (i64 1375739904 to ptr), i32 0, i32 5), align 4, !tbaa !15
  br label %58

58:                                               ; preds = %57, %52
  br label %59

59:                                               ; preds = %58, %49
  store i32 0, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %60

60:                                               ; preds = %59, %44, %29
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  %61 = load i32, ptr %3, align 4
  ret i32 %61
}

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

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 112, !6, i64 116}
!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 20}


; 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_WaitForLastOperation(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = tail call i32 @HAL_GetTick() #2
  %4 = load volatile i32, ptr inttoptr (i64 1375739920 to ptr), align 16, !tbaa !5
  %5 = and i32 %4, 4
  %.not21 = icmp eq i32 %5, 0
  br i1 %.not21, label %._crit_edge, label %.lr.ph

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

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

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

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

._crit_edge:                                      ; preds = %.lr.ph.split.us, %12, %2
  %15 = icmp eq i32 %1, 1
  br i1 %15, label %16, label %.loopexit

16:                                               ; preds = %._crit_edge
  %17 = load volatile i32, ptr inttoptr (i64 1375739920 to ptr), align 16, !tbaa !5
  %18 = and i32 %17, 401473536
  %.not15 = icmp eq i32 %18, 0
  br i1 %.not15, label %22, label %19

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

22:                                               ; preds = %16
  %23 = load volatile i32, ptr inttoptr (i64 1375739920 to ptr), align 16, !tbaa !5
  %24 = and i32 %23, 65536
  %.not16 = icmp eq i32 %24, 0
  br i1 %.not16, label %.loopexit, label %25

25:                                               ; preds = %22
  store volatile i32 65536, ptr inttoptr (i64 1375739924 to ptr), align 4, !tbaa !14
  br label %.loopexit

.loopexit:                                        ; preds = %.lr.ph.split.split, %.lr.ph.split.split.us, %._crit_edge, %25, %22, %19
  %.0 = phi i32 [ 1, %19 ], [ 0, %22 ], [ 0, %25 ], [ 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 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 112, !7, i64 116}
!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 20}


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