; 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: nounwind uwtable
define dso_local i32 @HAL_FLASH_Program(i32 noundef %0, i32 noundef %1, i32 noundef %2) #0 {
  %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 ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca i8, align 1
  %13 = alloca i32, align 4
  store i32 %0, ptr %5, align 4, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %14 = load i32, ptr %6, align 4, !tbaa !5
  %15 = zext i32 %14 to i64
  %16 = inttoptr i64 %15 to ptr
  store ptr %16, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %17 = load i32, ptr %7, align 4, !tbaa !5
  %18 = zext i32 %17 to i64
  %19 = inttoptr i64 %18 to ptr
  store ptr %19, ptr %10, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.start.p0(i64 1, ptr %12) #3
  store i8 8, ptr %12, align 1, !tbaa !11
  br label %20

20:                                               ; preds = %3
  %21 = load i32, ptr getelementptr inbounds (%struct.FLASH_ProcessTypeDef, ptr @pFlash, i32 0, i32 5), align 4, !tbaa !12
  %22 = icmp eq i32 %21, 1
  br i1 %22, label %23, label %24

23:                                               ; preds = %20
  store i32 2, ptr %4, align 4
  store i32 1, ptr %13, align 4
  br label %73

24:                                               ; preds = %20
  store i32 1, ptr getelementptr inbounds (%struct.FLASH_ProcessTypeDef, ptr @pFlash, i32 0, i32 5), align 4, !tbaa !12
  br label %25

25:                                               ; preds = %24
  br label %26

26:                                               ; preds = %25
  br label %27

27:                                               ; preds = %26
  %28 = load i32, ptr %6, align 4, !tbaa !5
  %29 = zext i32 %28 to i64
  %30 = icmp uge i64 %29, 134217728
  br i1 %30, label %31, label %36

31:                                               ; preds = %27
  %32 = load i32, ptr %6, align 4, !tbaa !5
  %33 = zext i32 %32 to i64
  %34 = icmp ule i64 %33, 135266303
  br i1 %34, label %35, label %36

35:                                               ; preds = %31
  store i32 1, ptr %11, align 4, !tbaa !5
  br label %37

36:                                               ; preds = %31, %27
  store i32 1, ptr %4, align 4
  store i32 1, ptr %13, align 4
  br label %73

37:                                               ; preds = %35
  store volatile i32 0, ptr getelementptr inbounds (%struct.FLASH_ProcessTypeDef, ptr @pFlash, i32 0, i32 6), align 4, !tbaa !14
  %38 = load i32, ptr %11, align 4, !tbaa !5
  %39 = call i32 @FLASH_WaitForLastOperation(i32 noundef 50000, i32 noundef %38)
  store i32 %39, ptr %8, align 4, !tbaa !5
  %40 = load i32, ptr %8, align 4, !tbaa !5
  %41 = icmp eq i32 %40, 0
  br i1 %41, label %42, label %68

42:                                               ; preds = %37
  %43 = load volatile i32, ptr getelementptr inbounds (%struct.FLASH_TypeDef, ptr inttoptr (i64 1375739904 to ptr), i32 0, i32 3), align 4, !tbaa !15
  %44 = zext i32 %43 to i64
  %45 = or i64 %44, 2
  %46 = trunc i64 %45 to i32
  store volatile i32 %46, ptr getelementptr inbounds (%struct.FLASH_TypeDef, ptr inttoptr (i64 1375739904 to ptr), i32 0, i32 3), align 4, !tbaa !15
  call void @__ISB()
  call void @__DSB()
  br label %47

47:                                               ; preds = %57, %42
  %48 = load ptr, ptr %10, align 8, !tbaa !9
  %49 = load volatile i32, ptr %48, align 4, !tbaa !5
  %50 = load ptr, ptr %9, align 8, !tbaa !9
  store volatile i32 %49, ptr %50, align 4, !tbaa !5
  %51 = load ptr, ptr %9, align 8, !tbaa !9
  %52 = getelementptr inbounds i32, ptr %51, i32 1
  store ptr %52, ptr %9, align 8, !tbaa !9
  %53 = load ptr, ptr %10, align 8, !tbaa !9
  %54 = getelementptr inbounds i32, ptr %53, i32 1
  store ptr %54, ptr %10, align 8, !tbaa !9
  %55 = load i8, ptr %12, align 1, !tbaa !11
  %56 = add i8 %55, -1
  store i8 %56, ptr %12, align 1, !tbaa !11
  br label %57

57:                                               ; preds = %47
  %58 = load i8, ptr %12, align 1, !tbaa !11
  %59 = zext i8 %58 to i32
  %60 = icmp ne i32 %59, 0
  br i1 %60, label %47, label %61, !llvm.loop !17

61:                                               ; preds = %57
  call void @__ISB()
  call void @__DSB()
  %62 = load i32, ptr %11, align 4, !tbaa !5
  %63 = call i32 @FLASH_WaitForLastOperation(i32 noundef 50000, i32 noundef %62)
  store i32 %63, ptr %8, align 4, !tbaa !5
  %64 = load volatile i32, ptr getelementptr inbounds (%struct.FLASH_TypeDef, ptr inttoptr (i64 1375739904 to ptr), i32 0, i32 3), align 4, !tbaa !15
  %65 = zext i32 %64 to i64
  %66 = and i64 %65, -3
  %67 = trunc i64 %66 to i32
  store volatile i32 %67, ptr getelementptr inbounds (%struct.FLASH_TypeDef, ptr inttoptr (i64 1375739904 to ptr), i32 0, i32 3), align 4, !tbaa !15
  br label %68

68:                                               ; preds = %61, %37
  br label %69

69:                                               ; preds = %68
  store i32 0, ptr getelementptr inbounds (%struct.FLASH_ProcessTypeDef, ptr @pFlash, i32 0, i32 5), align 4, !tbaa !12
  br label %70

70:                                               ; preds = %69
  br label %71

71:                                               ; preds = %70
  %72 = load i32, ptr %8, align 4, !tbaa !5
  store i32 %72, ptr %4, align 4
  store i32 1, ptr %13, align 4
  br label %73

73:                                               ; preds = %71, %36, %23
  call void @llvm.lifetime.end.p0(i64 1, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  %74 = load i32, ptr %4, align 4
  ret i32 %74
}

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

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

; Function Attrs: alwaysinline nounwind uwtable
declare hidden void @__ISB() #2

; Function Attrs: alwaysinline nounwind uwtable
declare hidden void @__DSB() #2

; 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 = { alwaysinline 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !6, i64 20}
!13 = !{!"", !6, i64 0, !6, i64 4, !6, i64 8, !6, i64 12, !6, i64 16, !6, i64 20, !6, i64 24}
!14 = !{!13, !6, i64 24}
!15 = !{!16, !6, i64 12}
!16 = !{!"", !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}
!17 = distinct !{!17, !18}
!18 = !{!"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 dso_local global %struct.FLASH_ProcessTypeDef, align 4

; Function Attrs: nounwind uwtable
define dso_local i32 @HAL_FLASH_Program(i32 noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = zext i32 %1 to i64
  %5 = inttoptr i64 %4 to ptr
  %6 = zext i32 %2 to i64
  %7 = inttoptr i64 %6 to ptr
  %8 = load i32, ptr getelementptr inbounds (i8, ptr @pFlash, i64 20), align 4, !tbaa !5
  %9 = icmp eq i32 %8, 1
  br i1 %9, label %44, label %10

10:                                               ; preds = %3
  store i32 1, ptr getelementptr inbounds (i8, ptr @pFlash, i64 20), align 4, !tbaa !5
  %11 = and i32 %1, -1048576
  %or.cond = icmp eq i32 %11, 134217728
  br i1 %or.cond, label %12, label %44

12:                                               ; preds = %10
  store volatile i32 0, ptr getelementptr inbounds (i8, ptr @pFlash, i64 24), align 4, !tbaa !10
  %13 = tail call i32 @FLASH_WaitForLastOperation(i32 noundef 50000, i32 noundef 1)
  %14 = icmp eq i32 %13, 0
  br i1 %14, label %15, label %43

15:                                               ; preds = %12
  %16 = load volatile i32, ptr inttoptr (i64 1375739916 to ptr), align 4, !tbaa !11
  %17 = or i32 %16, 2
  store volatile i32 %17, ptr inttoptr (i64 1375739916 to ptr), align 4, !tbaa !11
  tail call void @__ISB()
  tail call void @__DSB()
  %18 = load volatile i32, ptr %7, align 4, !tbaa !13
  store volatile i32 %18, ptr %5, align 4, !tbaa !13
  %19 = getelementptr inbounds i8, ptr %5, i64 4
  %20 = getelementptr inbounds i8, ptr %7, i64 4
  %21 = load volatile i32, ptr %20, align 4, !tbaa !13
  store volatile i32 %21, ptr %19, align 4, !tbaa !13
  %22 = getelementptr inbounds i8, ptr %5, i64 8
  %23 = getelementptr inbounds i8, ptr %7, i64 8
  %24 = load volatile i32, ptr %23, align 4, !tbaa !13
  store volatile i32 %24, ptr %22, align 4, !tbaa !13
  %25 = getelementptr inbounds i8, ptr %5, i64 12
  %26 = getelementptr inbounds i8, ptr %7, i64 12
  %27 = load volatile i32, ptr %26, align 4, !tbaa !13
  store volatile i32 %27, ptr %25, align 4, !tbaa !13
  %28 = getelementptr inbounds i8, ptr %5, i64 16
  %29 = getelementptr inbounds i8, ptr %7, i64 16
  %30 = load volatile i32, ptr %29, align 4, !tbaa !13
  store volatile i32 %30, ptr %28, align 4, !tbaa !13
  %31 = getelementptr inbounds i8, ptr %5, i64 20
  %32 = getelementptr inbounds i8, ptr %7, i64 20
  %33 = load volatile i32, ptr %32, align 4, !tbaa !13
  store volatile i32 %33, ptr %31, align 4, !tbaa !13
  %34 = getelementptr inbounds i8, ptr %5, i64 24
  %35 = getelementptr inbounds i8, ptr %7, i64 24
  %36 = load volatile i32, ptr %35, align 4, !tbaa !13
  store volatile i32 %36, ptr %34, align 4, !tbaa !13
  %37 = getelementptr inbounds i8, ptr %5, i64 28
  %38 = getelementptr inbounds i8, ptr %7, i64 28
  %39 = load volatile i32, ptr %38, align 4, !tbaa !13
  store volatile i32 %39, ptr %37, align 4, !tbaa !13
  tail call void @__ISB()
  tail call void @__DSB()
  %40 = tail call i32 @FLASH_WaitForLastOperation(i32 noundef 50000, i32 noundef 1)
  %41 = load volatile i32, ptr inttoptr (i64 1375739916 to ptr), align 4, !tbaa !11
  %42 = and i32 %41, -3
  store volatile i32 %42, ptr inttoptr (i64 1375739916 to ptr), align 4, !tbaa !11
  br label %43

43:                                               ; preds = %12, %15
  %.019 = phi i32 [ %40, %15 ], [ %13, %12 ]
  store i32 0, ptr getelementptr inbounds (i8, ptr @pFlash, i64 20), align 4, !tbaa !5
  br label %44

44:                                               ; preds = %10, %3, %43
  %.016 = phi i32 [ %.019, %43 ], [ 2, %3 ], [ 1, %10 ]
  ret i32 %.016
}

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

; Function Attrs: alwaysinline nounwind uwtable
declare hidden void @__ISB() local_unnamed_addr #1

; Function Attrs: alwaysinline nounwind uwtable
declare hidden void @__DSB() 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 = { alwaysinline 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" }

!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 12}
!12 = !{!"", !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}
!13 = !{!7, !7, i64 0}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,SROAPass,SimplifyCFGPass,TailCallElimPass