; LLVM IR from cvra/robot-software :: lib/parameter_flash_storage (flash_f3)

; 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"

@write_last_byte = external hidden global i8, align 1
@last_byte = external hidden global i8, align 1

; Function Attrs: nounwind uwtable
define dso_local void @flash_write(ptr noundef %0, ptr noundef %1, i64 noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i16, align 2
  %10 = alloca i64, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i64 %2, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %11, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %12, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 2, ptr %9) #2
  call void @flash_wait_for_last_operation()
  %13 = load ptr, ptr %8, align 8, !tbaa !5
  %14 = ptrtoint ptr %13 to i32
  %15 = and i32 %14, 1
  %16 = icmp ne i32 %15, 0
  br i1 %16, label %17, label %52

17:                                               ; preds = %3
  %18 = load ptr, ptr %8, align 8, !tbaa !5
  %19 = ptrtoint ptr %18 to i32
  %20 = and i32 %19, -2
  %21 = zext i32 %20 to i64
  %22 = inttoptr i64 %21 to ptr
  store ptr %22, ptr %8, align 8, !tbaa !5
  %23 = load i8, ptr @write_last_byte, align 1, !tbaa !11, !range !13, !noundef !14
  %24 = trunc i8 %23 to i1
  br i1 %24, label %25, label %28

25:                                               ; preds = %17
  %26 = load i8, ptr @last_byte, align 1, !tbaa !15
  %27 = zext i8 %26 to i16
  store i16 %27, ptr %9, align 2, !tbaa !16
  store i8 0, ptr @write_last_byte, align 1, !tbaa !11
  br label %34

28:                                               ; preds = %17
  %29 = load ptr, ptr %8, align 8, !tbaa !5
  %30 = load i16, ptr %29, align 2, !tbaa !16
  %31 = zext i16 %30 to i32
  %32 = and i32 %31, 255
  %33 = trunc i32 %32 to i16
  store i16 %33, ptr %9, align 2, !tbaa !16
  br label %34

34:                                               ; preds = %28, %25
  %35 = load ptr, ptr %7, align 8, !tbaa !5
  %36 = load i8, ptr %35, align 1, !tbaa !15
  %37 = zext i8 %36 to i16
  %38 = zext i16 %37 to i32
  %39 = shl i32 %38, 8
  %40 = load i16, ptr %9, align 2, !tbaa !16
  %41 = zext i16 %40 to i32
  %42 = or i32 %41, %39
  %43 = trunc i32 %42 to i16
  store i16 %43, ptr %9, align 2, !tbaa !16
  %44 = load ptr, ptr %8, align 8, !tbaa !5
  %45 = load i16, ptr %9, align 2, !tbaa !16
  call void @flash_write_half_word(ptr noundef %44, i16 noundef zeroext %45)
  %46 = load ptr, ptr %8, align 8, !tbaa !5
  %47 = getelementptr inbounds i16, ptr %46, i32 1
  store ptr %47, ptr %8, align 8, !tbaa !5
  %48 = load ptr, ptr %7, align 8, !tbaa !5
  %49 = getelementptr inbounds i8, ptr %48, i32 1
  store ptr %49, ptr %7, align 8, !tbaa !5
  %50 = load i64, ptr %6, align 8, !tbaa !9
  %51 = add i64 %50, -1
  store i64 %51, ptr %6, align 8, !tbaa !9
  br label %52

52:                                               ; preds = %34, %3
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  %53 = load i64, ptr %6, align 8, !tbaa !9
  store i64 %53, ptr %10, align 8, !tbaa !9
  br label %54

54:                                               ; preds = %75, %52
  %55 = load i64, ptr %10, align 8, !tbaa !9
  %56 = icmp ugt i64 %55, 1
  br i1 %56, label %57, label %78

57:                                               ; preds = %54
  %58 = load ptr, ptr %7, align 8, !tbaa !5
  %59 = getelementptr inbounds i8, ptr %58, i32 1
  store ptr %59, ptr %7, align 8, !tbaa !5
  %60 = load i8, ptr %58, align 1, !tbaa !15
  %61 = zext i8 %60 to i16
  store i16 %61, ptr %9, align 2, !tbaa !16
  %62 = load ptr, ptr %7, align 8, !tbaa !5
  %63 = getelementptr inbounds i8, ptr %62, i32 1
  store ptr %63, ptr %7, align 8, !tbaa !5
  %64 = load i8, ptr %62, align 1, !tbaa !15
  %65 = zext i8 %64 to i16
  %66 = zext i16 %65 to i32
  %67 = shl i32 %66, 8
  %68 = load i16, ptr %9, align 2, !tbaa !16
  %69 = zext i16 %68 to i32
  %70 = or i32 %69, %67
  %71 = trunc i32 %70 to i16
  store i16 %71, ptr %9, align 2, !tbaa !16
  %72 = load ptr, ptr %8, align 8, !tbaa !5
  %73 = getelementptr inbounds i16, ptr %72, i32 1
  store ptr %73, ptr %8, align 8, !tbaa !5
  %74 = load i16, ptr %9, align 2, !tbaa !16
  call void @flash_write_half_word(ptr noundef %72, i16 noundef zeroext %74)
  br label %75

75:                                               ; preds = %57
  %76 = load i64, ptr %10, align 8, !tbaa !9
  %77 = sub i64 %76, 2
  store i64 %77, ptr %10, align 8, !tbaa !9
  br label %54, !llvm.loop !18

78:                                               ; preds = %54
  %79 = load i64, ptr %10, align 8, !tbaa !9
  %80 = icmp eq i64 %79, 1
  br i1 %80, label %81, label %84

81:                                               ; preds = %78
  %82 = load ptr, ptr %7, align 8, !tbaa !5
  %83 = load i8, ptr %82, align 1, !tbaa !15
  store i8 %83, ptr @last_byte, align 1, !tbaa !15
  store i8 1, ptr @write_last_byte, align 1, !tbaa !11
  br label %84

84:                                               ; preds = %81, %78
  %85 = load volatile i32, ptr inttoptr (i64 1073881104 to ptr), align 4, !tbaa !20
  %86 = and i32 %85, -2
  store volatile i32 %86, ptr inttoptr (i64 1073881104 to ptr), align 4, !tbaa !20
  %87 = load volatile i32, ptr inttoptr (i64 1073881100 to ptr), align 4, !tbaa !20
  %88 = or i32 %87, 32
  store volatile i32 %88, ptr inttoptr (i64 1073881100 to ptr), align 4, !tbaa !20
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  ret void
}

; 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 hidden void @flash_wait_for_last_operation() #0

; Function Attrs: nounwind uwtable
declare hidden void @flash_write_half_word(ptr noundef, i16 noundef zeroext) #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 = { 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 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"_Bool", !7, i64 0}
!13 = !{i8 0, i8 2}
!14 = !{}
!15 = !{!7, !7, i64 0}
!16 = !{!17, !17, i64 0}
!17 = !{!"short", !7, i64 0}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}
!20 = !{!21, !21, i64 0}
!21 = !{!"int", !7, i64 0}


; 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"

@write_last_byte = external hidden local_unnamed_addr global i8, align 1
@last_byte = external hidden local_unnamed_addr global i8, align 1

; Function Attrs: nounwind uwtable
define dso_local void @flash_write(ptr noundef %0, ptr nocapture noundef readonly %1, i64 noundef %2) local_unnamed_addr #0 {
  tail call void @flash_wait_for_last_operation()
  %4 = ptrtoint ptr %0 to i64
  %5 = and i64 %4, 1
  %.not = icmp eq i64 %5, 0
  br i1 %.not, label %25, label %6

6:                                                ; preds = %3
  %7 = and i64 %4, 4294967294
  %8 = inttoptr i64 %7 to ptr
  %9 = load i8, ptr @write_last_byte, align 1, !tbaa !5, !range !9, !noundef !10
  %10 = trunc nuw i8 %9 to i1
  br i1 %10, label %11, label %14

11:                                               ; preds = %6
  %12 = load i8, ptr @last_byte, align 1, !tbaa !11
  %13 = zext i8 %12 to i16
  store i8 0, ptr @write_last_byte, align 1, !tbaa !5
  br label %17

14:                                               ; preds = %6
  %15 = load i16, ptr %8, align 2, !tbaa !12
  %16 = and i16 %15, 255
  br label %17

17:                                               ; preds = %14, %11
  %.021 = phi i16 [ %13, %11 ], [ %16, %14 ]
  %18 = load i8, ptr %1, align 1, !tbaa !11
  %19 = zext i8 %18 to i16
  %20 = shl nuw i16 %19, 8
  %21 = or disjoint i16 %20, %.021
  tail call void @flash_write_half_word(ptr noundef %8, i16 noundef zeroext %21)
  %22 = getelementptr inbounds i8, ptr %8, i64 2
  %23 = getelementptr inbounds i8, ptr %1, i64 1
  %24 = add i64 %2, -1
  br label %25

25:                                               ; preds = %17, %3
  %.025 = phi i64 [ %24, %17 ], [ %2, %3 ]
  %.023 = phi ptr [ %23, %17 ], [ %1, %3 ]
  %.022 = phi ptr [ %22, %17 ], [ %0, %3 ]
  %26 = icmp ugt i64 %.025, 1
  br i1 %26, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %25, %.lr.ph
  %.028 = phi i64 [ %30, %.lr.ph ], [ %.025, %25 ]
  %.127 = phi ptr [ %29, %.lr.ph ], [ %.022, %25 ]
  %.12426 = phi ptr [ %28, %.lr.ph ], [ %.023, %25 ]
  %27 = load i16, ptr %.12426, align 1
  %28 = getelementptr inbounds i8, ptr %.12426, i64 2
  %29 = getelementptr inbounds i8, ptr %.127, i64 2
  tail call void @flash_write_half_word(ptr noundef %.127, i16 noundef zeroext %27)
  %30 = add i64 %.028, -2
  %31 = icmp ugt i64 %30, 1
  br i1 %31, label %.lr.ph, label %._crit_edge, !llvm.loop !14

._crit_edge:                                      ; preds = %.lr.ph, %25
  %.124.lcssa = phi ptr [ %.023, %25 ], [ %28, %.lr.ph ]
  %.0.lcssa = phi i64 [ %.025, %25 ], [ %30, %.lr.ph ]
  %.not30 = icmp eq i64 %.0.lcssa, 0
  br i1 %.not30, label %34, label %32

32:                                               ; preds = %._crit_edge
  %33 = load i8, ptr %.124.lcssa, align 1, !tbaa !11
  store i8 %33, ptr @last_byte, align 1, !tbaa !11
  store i8 1, ptr @write_last_byte, align 1, !tbaa !5
  br label %34

34:                                               ; preds = %32, %._crit_edge
  %35 = load volatile i32, ptr inttoptr (i64 1073881104 to ptr), align 16, !tbaa !16
  %36 = and i32 %35, -2
  store volatile i32 %36, ptr inttoptr (i64 1073881104 to ptr), align 16, !tbaa !16
  %37 = load volatile i32, ptr inttoptr (i64 1073881100 to ptr), align 4, !tbaa !16
  %38 = or i32 %37, 32
  store volatile i32 %38, ptr inttoptr (i64 1073881100 to ptr), align 4, !tbaa !16
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden void @flash_wait_for_last_operation() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden void @flash_write_half_word(ptr noundef, i16 noundef zeroext) 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" }

!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 = !{!"_Bool", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{i8 0, i8 2}
!10 = !{}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"short", !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!17, !17, i64 0}
!17 = !{!"int", !7, i64 0}


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