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

; 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_EraseInitTypeDef = type { 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 global %struct.FLASH_ProcessTypeDef, align 4

; Function Attrs: nounwind uwtable
define dso_local i32 @HAL_FLASHEx_Erase(ptr noundef %0, ptr noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %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 ptr %1, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 0, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  br label %9

9:                                                ; preds = %2
  %10 = load i32, ptr getelementptr inbounds (%struct.FLASH_ProcessTypeDef, ptr @pFlash, i32 0, i32 5), align 4, !tbaa !11
  %11 = icmp eq i32 %10, 1
  br i1 %11, label %12, label %13

12:                                               ; preds = %9
  store i32 2, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %108

13:                                               ; preds = %9
  store i32 1, ptr getelementptr inbounds (%struct.FLASH_ProcessTypeDef, ptr @pFlash, i32 0, i32 5), align 4, !tbaa !11
  br label %14

14:                                               ; preds = %13
  br label %15

15:                                               ; preds = %14
  br label %16

16:                                               ; preds = %15
  store volatile i32 0, ptr getelementptr inbounds (%struct.FLASH_ProcessTypeDef, ptr @pFlash, i32 0, i32 6), align 4, !tbaa !13
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.FLASH_EraseInitTypeDef, ptr %17, i32 0, i32 1
  %19 = load i32, ptr %18, align 4, !tbaa !14
  %20 = and i32 %19, 1
  %21 = icmp eq i32 %20, 1
  br i1 %21, label %22, label %27

22:                                               ; preds = %16
  %23 = call i32 @FLASH_WaitForLastOperation(i32 noundef 50000, i32 noundef 1)
  %24 = icmp ne i32 %23, 0
  br i1 %24, label %25, label %26

25:                                               ; preds = %22
  store i32 1, ptr %6, align 4, !tbaa !9
  br label %26

26:                                               ; preds = %25, %22
  br label %27

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

30:                                               ; preds = %27
  %31 = load ptr, ptr %4, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.FLASH_EraseInitTypeDef, ptr %31, i32 0, i32 0
  %33 = load i32, ptr %32, align 4, !tbaa !16
  %34 = icmp eq i32 %33, 1
  br i1 %34, label %35, label %57

35:                                               ; preds = %30
  %36 = load ptr, ptr %4, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.FLASH_EraseInitTypeDef, ptr %36, i32 0, i32 4
  %38 = load i32, ptr %37, align 4, !tbaa !17
  %39 = load ptr, ptr %4, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.FLASH_EraseInitTypeDef, ptr %39, i32 0, i32 1
  %41 = load i32, ptr %40, align 4, !tbaa !14
  call void @FLASH_MassErase(i32 noundef %38, i32 noundef %41)
  %42 = load ptr, ptr %4, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.FLASH_EraseInitTypeDef, ptr %42, i32 0, i32 1
  %44 = load i32, ptr %43, align 4, !tbaa !14
  %45 = and i32 %44, 1
  %46 = icmp eq i32 %45, 1
  br i1 %46, label %47, label %56

47:                                               ; preds = %35
  %48 = call i32 @FLASH_WaitForLastOperation(i32 noundef 50000, i32 noundef 1)
  %49 = icmp ne i32 %48, 0
  br i1 %49, label %50, label %51

50:                                               ; preds = %47
  store i32 1, ptr %6, align 4, !tbaa !9
  br label %51

51:                                               ; preds = %50, %47
  %52 = load volatile i32, ptr getelementptr inbounds (%struct.FLASH_TypeDef, ptr inttoptr (i64 1375739904 to ptr), i32 0, i32 3), align 4, !tbaa !18
  %53 = zext i32 %52 to i64
  %54 = and i64 %53, -9
  %55 = trunc i64 %54 to i32
  store volatile i32 %55, ptr getelementptr inbounds (%struct.FLASH_TypeDef, ptr inttoptr (i64 1375739904 to ptr), i32 0, i32 3), align 4, !tbaa !18
  br label %56

56:                                               ; preds = %51, %35
  br label %102

57:                                               ; preds = %30
  %58 = load ptr, ptr %5, align 8, !tbaa !5
  store i32 -1, ptr %58, align 4, !tbaa !9
  %59 = load ptr, ptr %4, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.FLASH_EraseInitTypeDef, ptr %59, i32 0, i32 2
  %61 = load i32, ptr %60, align 4, !tbaa !20
  store i32 %61, ptr %7, align 4, !tbaa !9
  br label %62

62:                                               ; preds = %98, %57
  %63 = load i32, ptr %7, align 4, !tbaa !9
  %64 = load ptr, ptr %4, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.FLASH_EraseInitTypeDef, ptr %64, i32 0, i32 3
  %66 = load i32, ptr %65, align 4, !tbaa !21
  %67 = load ptr, ptr %4, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.FLASH_EraseInitTypeDef, ptr %67, i32 0, i32 2
  %69 = load i32, ptr %68, align 4, !tbaa !20
  %70 = add i32 %66, %69
  %71 = icmp ult i32 %63, %70
  br i1 %71, label %72, label %101

72:                                               ; preds = %62
  %73 = load i32, ptr %7, align 4, !tbaa !9
  %74 = load ptr, ptr %4, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.FLASH_EraseInitTypeDef, ptr %74, i32 0, i32 1
  %76 = load i32, ptr %75, align 4, !tbaa !14
  %77 = load ptr, ptr %4, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct.FLASH_EraseInitTypeDef, ptr %77, i32 0, i32 4
  %79 = load i32, ptr %78, align 4, !tbaa !17
  call void @FLASH_Erase_Sector(i32 noundef %73, i32 noundef %76, i32 noundef %79)
  %80 = load ptr, ptr %4, align 8, !tbaa !5
  %81 = getelementptr inbounds %struct.FLASH_EraseInitTypeDef, ptr %80, i32 0, i32 1
  %82 = load i32, ptr %81, align 4, !tbaa !14
  %83 = and i32 %82, 1
  %84 = icmp eq i32 %83, 1
  br i1 %84, label %85, label %91

85:                                               ; preds = %72
  %86 = call i32 @FLASH_WaitForLastOperation(i32 noundef 50000, i32 noundef 1)
  store i32 %86, ptr %6, align 4, !tbaa !9
  %87 = load volatile i32, ptr getelementptr inbounds (%struct.FLASH_TypeDef, ptr inttoptr (i64 1375739904 to ptr), i32 0, i32 3), align 4, !tbaa !18
  %88 = zext i32 %87 to i64
  %89 = and i64 %88, -1797
  %90 = trunc i64 %89 to i32
  store volatile i32 %90, ptr getelementptr inbounds (%struct.FLASH_TypeDef, ptr inttoptr (i64 1375739904 to ptr), i32 0, i32 3), align 4, !tbaa !18
  br label %91

91:                                               ; preds = %85, %72
  %92 = load i32, ptr %6, align 4, !tbaa !9
  %93 = icmp ne i32 %92, 0
  br i1 %93, label %94, label %97

94:                                               ; preds = %91
  %95 = load i32, ptr %7, align 4, !tbaa !9
  %96 = load ptr, ptr %5, align 8, !tbaa !5
  store i32 %95, ptr %96, align 4, !tbaa !9
  br label %101

97:                                               ; preds = %91
  br label %98

98:                                               ; preds = %97
  %99 = load i32, ptr %7, align 4, !tbaa !9
  %100 = add i32 %99, 1
  store i32 %100, ptr %7, align 4, !tbaa !9
  br label %62, !llvm.loop !22

101:                                              ; preds = %94, %62
  br label %102

102:                                              ; preds = %101, %56
  br label %103

103:                                              ; preds = %102, %27
  br label %104

104:                                              ; preds = %103
  store i32 0, ptr getelementptr inbounds (%struct.FLASH_ProcessTypeDef, ptr @pFlash, i32 0, i32 5), align 4, !tbaa !11
  br label %105

105:                                              ; preds = %104
  br label %106

106:                                              ; preds = %105
  %107 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %107, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %108

108:                                              ; preds = %106, %12
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  %109 = load i32, ptr %3, align 4
  ret i32 %109
}

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

declare i32 @FLASH_WaitForLastOperation(i32 noundef, i32 noundef) #2

; Function Attrs: nounwind uwtable
declare hidden void @FLASH_MassErase(i32 noundef, i32 noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local void @FLASH_Erase_Sector(i32 noundef, i32 noundef, i32 noundef) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { 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 20}
!12 = !{!"", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12, !10, i64 16, !10, i64 20, !10, i64 24}
!13 = !{!12, !10, i64 24}
!14 = !{!15, !10, i64 4}
!15 = !{!"", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12, !10, i64 16}
!16 = !{!15, !10, i64 0}
!17 = !{!15, !10, i64 16}
!18 = !{!19, !10, i64 12}
!19 = !{!"", !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, !10, i64 60, !10, i64 64, !10, i64 68, !7, i64 72, !10, i64 80, !10, i64 84, !10, i64 88, !10, i64 92, !10, i64 96, !7, i64 100, !10, i64 112, !10, i64 116}
!20 = !{!15, !10, i64 8}
!21 = !{!15, !10, i64 12}
!22 = distinct !{!22, !23}
!23 = !{!"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"

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @HAL_FLASHEx_Erase(ptr nocapture noundef readonly %0, ptr nocapture noundef writeonly %1) local_unnamed_addr #0 {
  %3 = load i32, ptr getelementptr inbounds (i8, ptr @pFlash, i64 20), align 4, !tbaa !5
  %4 = icmp eq i32 %3, 1
  br i1 %4, label %46, label %5

5:                                                ; preds = %2
  store i32 1, ptr getelementptr inbounds (i8, ptr @pFlash, i64 20), align 4, !tbaa !5
  store volatile i32 0, ptr getelementptr inbounds (i8, ptr @pFlash, i64 24), align 4, !tbaa !10
  %6 = getelementptr inbounds i8, ptr %0, i64 4
  %7 = load i32, ptr %6, align 4, !tbaa !11
  %8 = and i32 %7, 1
  %.not = icmp eq i32 %8, 0
  br i1 %.not, label %.critedge, label %9

9:                                                ; preds = %5
  %10 = tail call i32 @FLASH_WaitForLastOperation(i32 noundef 50000, i32 noundef 1) #2
  %.not22 = icmp eq i32 %10, 0
  br i1 %.not22, label %.critedge, label %.loopexit

.critedge:                                        ; preds = %5, %9
  %11 = load i32, ptr %0, align 4, !tbaa !13
  %12 = icmp eq i32 %11, 1
  br i1 %12, label %13, label %23

13:                                               ; preds = %.critedge
  %14 = getelementptr inbounds i8, ptr %0, i64 16
  %15 = load i32, ptr %14, align 4, !tbaa !14
  %16 = load i32, ptr %6, align 4, !tbaa !11
  tail call void @FLASH_MassErase(i32 noundef %15, i32 noundef %16)
  %17 = load i32, ptr %6, align 4, !tbaa !11
  %18 = and i32 %17, 1
  %.not25 = icmp eq i32 %18, 0
  br i1 %.not25, label %.loopexit, label %19

19:                                               ; preds = %13
  %20 = tail call i32 @FLASH_WaitForLastOperation(i32 noundef 50000, i32 noundef 1) #2
  %.not26 = icmp ne i32 %20, 0
  %spec.select27 = zext i1 %.not26 to i32
  %21 = load volatile i32, ptr inttoptr (i64 1375739916 to ptr), align 4, !tbaa !15
  %22 = and i32 %21, -9
  store volatile i32 %22, ptr inttoptr (i64 1375739916 to ptr), align 4, !tbaa !15
  br label %.loopexit

23:                                               ; preds = %.critedge
  store i32 -1, ptr %1, align 4, !tbaa !17
  %24 = getelementptr inbounds i8, ptr %0, i64 8
  %25 = load i32, ptr %24, align 4, !tbaa !18
  %26 = getelementptr inbounds i8, ptr %0, i64 12
  %27 = load i32, ptr %26, align 4, !tbaa !19
  %28 = add i32 %25, %27
  %29 = icmp ult i32 %25, %28
  br i1 %29, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %23
  %30 = getelementptr inbounds i8, ptr %0, i64 16
  br label %31

31:                                               ; preds = %.lr.ph, %.thread
  %.031 = phi i32 [ %25, %.lr.ph ], [ %41, %.thread ]
  %32 = load i32, ptr %6, align 4, !tbaa !11
  %33 = load i32, ptr %30, align 4, !tbaa !14
  tail call void @FLASH_Erase_Sector(i32 noundef %.031, i32 noundef %32, i32 noundef %33)
  %34 = load i32, ptr %6, align 4, !tbaa !11
  %35 = and i32 %34, 1
  %.not23 = icmp eq i32 %35, 0
  br i1 %.not23, label %.thread, label %36

36:                                               ; preds = %31
  %37 = tail call i32 @FLASH_WaitForLastOperation(i32 noundef 50000, i32 noundef 1) #2
  %38 = load volatile i32, ptr inttoptr (i64 1375739916 to ptr), align 4, !tbaa !15
  %39 = and i32 %38, -1797
  store volatile i32 %39, ptr inttoptr (i64 1375739916 to ptr), align 4, !tbaa !15
  %.not24 = icmp eq i32 %37, 0
  br i1 %.not24, label %.thread, label %40

40:                                               ; preds = %36
  store i32 %.031, ptr %1, align 4, !tbaa !17
  br label %.loopexit

.thread:                                          ; preds = %31, %36
  %41 = add nuw i32 %.031, 1
  %42 = load i32, ptr %26, align 4, !tbaa !19
  %43 = load i32, ptr %24, align 4, !tbaa !18
  %44 = add i32 %43, %42
  %45 = icmp ult i32 %41, %44
  br i1 %45, label %31, label %.loopexit, !llvm.loop !20

.loopexit:                                        ; preds = %.thread, %23, %9, %40, %13, %19
  %.4 = phi i32 [ %spec.select27, %19 ], [ 0, %13 ], [ %37, %40 ], [ 1, %9 ], [ 0, %23 ], [ 0, %.thread ]
  store i32 0, ptr getelementptr inbounds (i8, ptr @pFlash, i64 20), align 4, !tbaa !5
  br label %46

46:                                               ; preds = %2, %.loopexit
  %.021 = phi i32 [ %.4, %.loopexit ], [ 2, %2 ]
  ret i32 %.021
}

declare i32 @FLASH_WaitForLastOperation(i32 noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden void @FLASH_MassErase(i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @FLASH_Erase_Sector(i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #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, !7, i64 20}
!6 = !{!"", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12, !7, i64 16, !7, i64 20, !7, i64 24}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!6, !7, i64 24}
!11 = !{!12, !7, i64 4}
!12 = !{!"", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12, !7, i64 16}
!13 = !{!12, !7, i64 0}
!14 = !{!12, !7, i64 16}
!15 = !{!16, !7, i64 12}
!16 = !{!"", !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}
!17 = !{!7, !7, i64 0}
!18 = !{!12, !7, i64 8}
!19 = !{!12, !7, i64 12}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass