; LLVM IR from Stephane-D/SGDK :: src/ext/flash-save (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"

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

; 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
declare hidden zeroext i16 @bus_read16(i64 noundef) #1 section ".ramprog"

; Function Attrs: noinline nounwind uwtable
declare hidden void @word_program(i64 noundef, i16 noundef zeroext) #2 section ".ramprog"

; Function Attrs: nounwind uwtable
define dso_local signext i16 @flash_copy(i64 noundef %0, i64 noundef %1, i16 noundef zeroext %2) #1 {
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i16, align 2
  %7 = alloca i16, align 2
  %8 = alloca i16, align 2
  store i64 %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !5
  store i16 %2, ptr %6, align 2, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 2, ptr %7) #3
  %9 = load i16, ptr %6, align 2, !tbaa !9
  %10 = zext i16 %9 to i32
  %11 = ashr i32 %10, 1
  %12 = trunc i32 %11 to i16
  store i16 %12, ptr %7, align 2, !tbaa !9
  br label %13

13:                                               ; preds = %17, %3
  %14 = load i16, ptr %7, align 2, !tbaa !9
  %15 = add i16 %14, -1
  store i16 %15, ptr %7, align 2, !tbaa !9
  %16 = icmp ne i16 %14, 0
  br i1 %16, label %17, label %26

17:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 2, ptr %8) #3
  %18 = load i64, ptr %5, align 8, !tbaa !5
  %19 = call zeroext i16 @bus_read16(i64 noundef %18)
  store i16 %19, ptr %8, align 2, !tbaa !9
  %20 = load i64, ptr %4, align 8, !tbaa !5
  %21 = load i16, ptr %8, align 2, !tbaa !9
  call void @word_program(i64 noundef %20, i16 noundef zeroext %21)
  %22 = load i64, ptr %5, align 8, !tbaa !5
  %23 = add i64 %22, 2
  store i64 %23, ptr %5, align 8, !tbaa !5
  %24 = load i64, ptr %4, align 8, !tbaa !5
  %25 = add i64 %24, 2
  store i64 %25, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 2, ptr %8) #3
  br label %13, !llvm.loop !11

26:                                               ; preds = %13
  %27 = load i16, ptr %6, align 2, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 2, ptr %7) #3
  ret i16 %27
}

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 = { noinline 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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"short", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"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"

; Function Attrs: nounwind uwtable
declare hidden zeroext i16 @bus_read16(i64 noundef) local_unnamed_addr #0 section ".ramprog"

; Function Attrs: noinline nounwind uwtable
declare hidden void @word_program(i64 noundef, i16 noundef zeroext) local_unnamed_addr #1 section ".ramprog"

; Function Attrs: nounwind uwtable
define dso_local noundef signext i16 @flash_copy(i64 noundef %0, i64 noundef %1, i16 noundef returned zeroext %2) local_unnamed_addr #0 {
  %.not10 = icmp ult i16 %2, 2
  br i1 %.not10, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %3
  %4 = lshr i16 %2, 1
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %.013 = phi i64 [ %8, %.lr.ph ], [ %0, %.lr.ph.preheader ]
  %.0812 = phi i16 [ %5, %.lr.ph ], [ %4, %.lr.ph.preheader ]
  %.0911 = phi i64 [ %7, %.lr.ph ], [ %1, %.lr.ph.preheader ]
  %5 = add nsw i16 %.0812, -1
  %6 = tail call zeroext i16 @bus_read16(i64 noundef %.0911)
  tail call void @word_program(i64 noundef %.013, i16 noundef zeroext %6)
  %7 = add i64 %.0911, 2
  %8 = add i64 %.013, 2
  %.not = icmp eq i16 %5, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !5

._crit_edge:                                      ; preds = %.lr.ph, %3
  ret i16 %2
}

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 = { noinline 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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass