; 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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

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

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

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

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

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

9:                                                ; preds = %6
  store i32 2, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %83

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

11:                                               ; preds = %10
  br label %12

12:                                               ; preds = %11
  br label %13

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

19:                                               ; preds = %13
  %20 = call i32 @FLASH_WaitForLastOperation(i32 noundef 50000, i32 noundef 1)
  %21 = icmp ne i32 %20, 0
  br i1 %21, label %22, label %23

22:                                               ; preds = %19
  store i32 1, ptr %4, align 4, !tbaa !9
  br label %23

23:                                               ; preds = %22, %19
  br label %24

24:                                               ; preds = %23, %13
  %25 = load i32, ptr %4, align 4, !tbaa !9
  %26 = icmp ne i32 %25, 0
  br i1 %26, label %27, label %31

27:                                               ; preds = %24
  br label %28

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

29:                                               ; preds = %28
  br label %30

30:                                               ; preds = %29
  br label %81

31:                                               ; preds = %24
  %32 = load ptr, ptr %3, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.FLASH_EraseInitTypeDef, ptr %32, i32 0, i32 1
  %34 = load i32, ptr %33, align 4, !tbaa !14
  %35 = and i32 %34, 1
  %36 = icmp eq i32 %35, 1
  br i1 %36, label %37, label %42

37:                                               ; preds = %31
  %38 = load volatile i32, ptr getelementptr inbounds (%struct.FLASH_TypeDef, ptr inttoptr (i64 1375739904 to ptr), i32 0, i32 3), align 4, !tbaa !16
  %39 = zext i32 %38 to i64
  %40 = or i64 %39, 7274496
  %41 = trunc i64 %40 to i32
  store volatile i32 %41, ptr getelementptr inbounds (%struct.FLASH_TypeDef, ptr inttoptr (i64 1375739904 to ptr), i32 0, i32 3), align 4, !tbaa !16
  br label %42

42:                                               ; preds = %37, %31
  %43 = load ptr, ptr %3, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.FLASH_EraseInitTypeDef, ptr %43, i32 0, i32 0
  %45 = load i32, ptr %44, align 4, !tbaa !18
  %46 = icmp eq i32 %45, 1
  br i1 %46, label %47, label %61

47:                                               ; preds = %42
  %48 = load ptr, ptr %3, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.FLASH_EraseInitTypeDef, ptr %48, i32 0, i32 1
  %50 = load i32, ptr %49, align 4, !tbaa !14
  %51 = icmp eq i32 %50, 1
  br i1 %51, label %52, label %53

52:                                               ; preds = %47
  store volatile i32 2, ptr @pFlash, align 4, !tbaa !19
  br label %54

53:                                               ; preds = %47
  store volatile i32 7, ptr @pFlash, align 4, !tbaa !19
  br label %54

54:                                               ; preds = %53, %52
  %55 = load ptr, ptr %3, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.FLASH_EraseInitTypeDef, ptr %55, i32 0, i32 4
  %57 = load i32, ptr %56, align 4, !tbaa !20
  %58 = load ptr, ptr %3, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.FLASH_EraseInitTypeDef, ptr %58, i32 0, i32 1
  %60 = load i32, ptr %59, align 4, !tbaa !14
  call void @FLASH_MassErase(i32 noundef %57, i32 noundef %60)
  br label %80

61:                                               ; preds = %42
  store volatile i32 1, ptr @pFlash, align 4, !tbaa !19
  %62 = load ptr, ptr %3, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.FLASH_EraseInitTypeDef, ptr %62, i32 0, i32 3
  %64 = load i32, ptr %63, align 4, !tbaa !21
  store volatile i32 %64, ptr getelementptr inbounds (%struct.FLASH_ProcessTypeDef, ptr @pFlash, i32 0, i32 1), align 4, !tbaa !22
  %65 = load ptr, ptr %3, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.FLASH_EraseInitTypeDef, ptr %65, i32 0, i32 2
  %67 = load i32, ptr %66, align 4, !tbaa !23
  store volatile i32 %67, ptr getelementptr inbounds (%struct.FLASH_ProcessTypeDef, ptr @pFlash, i32 0, i32 3), align 4, !tbaa !24
  %68 = load ptr, ptr %3, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.FLASH_EraseInitTypeDef, ptr %68, i32 0, i32 4
  %70 = load i32, ptr %69, align 4, !tbaa !20
  store volatile i32 %70, ptr getelementptr inbounds (%struct.FLASH_ProcessTypeDef, ptr @pFlash, i32 0, i32 2), align 4, !tbaa !25
  %71 = load ptr, ptr %3, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.FLASH_EraseInitTypeDef, ptr %71, i32 0, i32 2
  %73 = load i32, ptr %72, align 4, !tbaa !23
  %74 = load ptr, ptr %3, 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 %3, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct.FLASH_EraseInitTypeDef, ptr %77, i32 0, i32 4
  %79 = load i32, ptr %78, align 4, !tbaa !20
  call void @FLASH_Erase_Sector(i32 noundef %73, i32 noundef %76, i32 noundef %79)
  br label %80

80:                                               ; preds = %61, %54
  br label %81

81:                                               ; preds = %80, %30
  %82 = load i32, ptr %4, align 4, !tbaa !9
  store i32 %82, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %83

83:                                               ; preds = %81, %9
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  %84 = load i32, ptr %2, align 4
  ret i32 %84
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 #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 = !{!17, !10, i64 12}
!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, !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}
!18 = !{!15, !10, i64 0}
!19 = !{!12, !10, i64 0}
!20 = !{!15, !10, i64 16}
!21 = !{!15, !10, i64 12}
!22 = !{!12, !10, i64 4}
!23 = !{!15, !10, i64 8}
!24 = !{!12, !10, i64 12}
!25 = !{!12, !10, i64 8}


; 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

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

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

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

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

4:                                                ; preds = %1
  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
  %5 = getelementptr inbounds i8, ptr %0, i64 4
  %6 = load i32, ptr %5, align 4, !tbaa !11
  %7 = and i32 %6, 1
  %.not = icmp eq i32 %7, 0
  br i1 %.not, label %.thread.thread, label %8

8:                                                ; preds = %4
  %9 = tail call i32 @FLASH_WaitForLastOperation(i32 noundef 50000, i32 noundef 1) #2
  %.not15 = icmp eq i32 %9, 0
  br i1 %.not15, label %.thread, label %10

10:                                               ; preds = %8
  store i32 0, ptr getelementptr inbounds (i8, ptr @pFlash, i64 20), align 4, !tbaa !5
  br label %29

.thread:                                          ; preds = %8
  %.pr = load i32, ptr %5, align 4, !tbaa !11
  %.pre = and i32 %.pr, 1
  %11 = icmp eq i32 %.pre, 0
  br i1 %11, label %.thread.thread, label %12

12:                                               ; preds = %.thread
  %13 = load volatile i32, ptr inttoptr (i64 1375739916 to ptr), align 4, !tbaa !13
  %14 = or i32 %13, 7274496
  store volatile i32 %14, ptr inttoptr (i64 1375739916 to ptr), align 4, !tbaa !13
  br label %.thread.thread

.thread.thread:                                   ; preds = %4, %12, %.thread
  %15 = phi i32 [ %.pr, %12 ], [ %.pr, %.thread ], [ %6, %4 ]
  %16 = load i32, ptr %0, align 4, !tbaa !15
  %17 = icmp eq i32 %16, 1
  br i1 %17, label %18, label %22

18:                                               ; preds = %.thread.thread
  %19 = icmp eq i32 %15, 1
  %. = select i1 %19, i32 2, i32 7
  store volatile i32 %., ptr @pFlash, align 4, !tbaa !16
  %20 = getelementptr inbounds i8, ptr %0, i64 16
  %21 = load i32, ptr %20, align 4, !tbaa !17
  tail call void @FLASH_MassErase(i32 noundef %21, i32 noundef %15)
  br label %29

22:                                               ; preds = %.thread.thread
  store volatile i32 1, ptr @pFlash, align 4, !tbaa !16
  %23 = getelementptr inbounds i8, ptr %0, i64 12
  %24 = load i32, ptr %23, align 4, !tbaa !18
  store volatile i32 %24, ptr getelementptr inbounds (i8, ptr @pFlash, i64 4), align 4, !tbaa !19
  %25 = getelementptr inbounds i8, ptr %0, i64 8
  %26 = load i32, ptr %25, align 4, !tbaa !20
  store volatile i32 %26, ptr getelementptr inbounds (i8, ptr @pFlash, i64 12), align 4, !tbaa !21
  %27 = getelementptr inbounds i8, ptr %0, i64 16
  %28 = load i32, ptr %27, align 4, !tbaa !17
  store volatile i32 %28, ptr getelementptr inbounds (i8, ptr @pFlash, i64 8), align 4, !tbaa !22
  tail call void @FLASH_Erase_Sector(i32 noundef %26, i32 noundef %15, i32 noundef %28)
  br label %29

29:                                               ; preds = %10, %22, %18, %1
  %.014 = phi i32 [ 2, %1 ], [ 0, %18 ], [ 0, %22 ], [ 1, %10 ]
  ret i32 %.014
}

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 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 = !{!14, !7, i64 12}
!14 = !{!"", !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}
!15 = !{!12, !7, i64 0}
!16 = !{!6, !7, i64 0}
!17 = !{!12, !7, i64 16}
!18 = !{!12, !7, i64 12}
!19 = !{!6, !7, i64 4}
!20 = !{!12, !7, i64 8}
!21 = !{!6, !7, i64 12}
!22 = !{!6, !7, i64 8}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass