; LLVM IR from joembedded/JesFs :: jesfs_ml (sflash_SectorWrite)

; 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.SFLASH_INFO = type { i32, i32, i32, i32, i32, i16, i16, i16, i16, i16, %union.SF_BUFFER, i8 }
%union.SF_BUFFER = type { [32 x i32] }

@sflash_info = external dso_local global %struct.SFLASH_INFO, align 4

; 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 dso_local void @sflash_PageWrite(i32 noundef, ptr noundef, i16 noundef zeroext) #1

; Function Attrs: nounwind uwtable
declare dso_local signext i16 @sflash_WaitBusy(i32 noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local signext i16 @sflash_WaitWriteEnabled() #1

; Function Attrs: nounwind uwtable
define dso_local signext i16 @sflash_SectorWrite(i32 noundef %0, ptr noundef %1, i32 noundef %2) #1 {
  %4 = alloca i16, align 2
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store i32 %0, ptr %5, align 4, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !9
  store i32 %2, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %10 = load i32, ptr %5, align 4, !tbaa !5
  %11 = load i32, ptr getelementptr inbounds (%struct.SFLASH_INFO, ptr @sflash_info, i32 0, i32 1), align 4, !tbaa !11
  %12 = icmp uge i32 %10, %11
  br i1 %12, label %13, label %14

13:                                               ; preds = %3
  store i16 -105, ptr %4, align 2
  store i32 1, ptr %9, align 4
  br label %60

14:                                               ; preds = %3
  %15 = load i32, ptr %5, align 4, !tbaa !5
  %16 = and i32 %15, 4095
  %17 = sub i32 4096, %16
  store i32 %17, ptr %8, align 4, !tbaa !5
  %18 = load i32, ptr %7, align 4, !tbaa !5
  %19 = load i32, ptr %8, align 4, !tbaa !5
  %20 = icmp ugt i32 %18, %19
  br i1 %20, label %21, label %22

21:                                               ; preds = %14
  store i16 -106, ptr %4, align 2
  store i32 1, ptr %9, align 4
  br label %60

22:                                               ; preds = %14
  br label %23

23:                                               ; preds = %48, %22
  %24 = load i32, ptr %7, align 4, !tbaa !5
  %25 = icmp ne i32 %24, 0
  br i1 %25, label %26, label %59

26:                                               ; preds = %23
  %27 = load i32, ptr %5, align 4, !tbaa !5
  %28 = trunc i32 %27 to i8
  %29 = zext i8 %28 to i32
  %30 = sub nsw i32 256, %29
  store i32 %30, ptr %8, align 4, !tbaa !5
  %31 = load i32, ptr %7, align 4, !tbaa !5
  %32 = load i32, ptr %8, align 4, !tbaa !5
  %33 = icmp ult i32 %31, %32
  br i1 %33, label %34, label %36

34:                                               ; preds = %26
  %35 = load i32, ptr %7, align 4, !tbaa !5
  store i32 %35, ptr %8, align 4, !tbaa !5
  br label %36

36:                                               ; preds = %34, %26
  %37 = call signext i16 @sflash_WaitWriteEnabled()
  %38 = icmp ne i16 %37, 0
  br i1 %38, label %39, label %40

39:                                               ; preds = %36
  store i16 -102, ptr %4, align 2
  store i32 1, ptr %9, align 4
  br label %60

40:                                               ; preds = %36
  %41 = load i32, ptr %5, align 4, !tbaa !5
  %42 = load ptr, ptr %6, align 8, !tbaa !9
  %43 = load i32, ptr %8, align 4, !tbaa !5
  %44 = trunc i32 %43 to i16
  call void @sflash_PageWrite(i32 noundef %41, ptr noundef %42, i16 noundef zeroext %44)
  %45 = call signext i16 @sflash_WaitBusy(i32 noundef 100)
  %46 = icmp ne i16 %45, 0
  br i1 %46, label %47, label %48

47:                                               ; preds = %40
  store i16 -101, ptr %4, align 2
  store i32 1, ptr %9, align 4
  br label %60

48:                                               ; preds = %40
  %49 = load i32, ptr %8, align 4, !tbaa !5
  %50 = load ptr, ptr %6, align 8, !tbaa !9
  %51 = zext i32 %49 to i64
  %52 = getelementptr inbounds i8, ptr %50, i64 %51
  store ptr %52, ptr %6, align 8, !tbaa !9
  %53 = load i32, ptr %8, align 4, !tbaa !5
  %54 = load i32, ptr %5, align 4, !tbaa !5
  %55 = add i32 %54, %53
  store i32 %55, ptr %5, align 4, !tbaa !5
  %56 = load i32, ptr %8, align 4, !tbaa !5
  %57 = load i32, ptr %7, align 4, !tbaa !5
  %58 = sub i32 %57, %56
  store i32 %58, ptr %7, align 4, !tbaa !5
  br label %23, !llvm.loop !14

59:                                               ; preds = %23
  store i16 0, ptr %4, align 2
  store i32 1, ptr %9, align 4
  br label %60

60:                                               ; preds = %59, %47, %39, %21, %13
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  %61 = load i16, ptr %4, align 2
  ret i16 %61
}

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 = { 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 = !{!12, !6, i64 4}
!12 = !{!"", !6, i64 0, !6, i64 4, !6, i64 8, !6, i64 12, !6, i64 16, !13, i64 20, !13, i64 22, !13, i64 24, !13, i64 26, !13, i64 28, !7, i64 32, !7, i64 160}
!13 = !{!"short", !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"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.SFLASH_INFO = type { i32, i32, i32, i32, i32, i16, i16, i16, i16, i16, %union.SF_BUFFER, i8 }
%union.SF_BUFFER = type { [32 x i32] }

@sflash_info = external dso_local local_unnamed_addr global %struct.SFLASH_INFO, align 4

; Function Attrs: nounwind uwtable
declare dso_local void @sflash_PageWrite(i32 noundef, ptr noundef, i16 noundef zeroext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local signext i16 @sflash_WaitBusy(i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local signext i16 @sflash_WaitWriteEnabled() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local signext range(i16 -106, 1) i16 @sflash_SectorWrite(i32 noundef %0, ptr noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = load i32, ptr getelementptr inbounds (i8, ptr @sflash_info, i64 4), align 4, !tbaa !5
  %.not = icmp ugt i32 %4, %0
  br i1 %.not, label %5, label %.loopexit

5:                                                ; preds = %3
  %6 = and i32 %0, 4095
  %7 = sub nuw nsw i32 4096, %6
  %8 = icmp ult i32 %7, %2
  br i1 %8, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %5
  %.not2528 = icmp eq i32 %2, 0
  br i1 %.not2528, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %15
  %.01831 = phi i32 [ %19, %15 ], [ %2, %.preheader ]
  %.01930 = phi ptr [ %17, %15 ], [ %1, %.preheader ]
  %.02029 = phi i32 [ %18, %15 ], [ %0, %.preheader ]
  %9 = and i32 %.02029, 255
  %10 = sub nuw nsw i32 256, %9
  %spec.select = tail call i32 @llvm.umin.i32(i32 %.01831, i32 %10)
  %11 = tail call signext i16 @sflash_WaitWriteEnabled()
  %.not26 = icmp eq i16 %11, 0
  br i1 %.not26, label %12, label %.loopexit

12:                                               ; preds = %.lr.ph
  %13 = trunc nuw nsw i32 %spec.select to i16
  tail call void @sflash_PageWrite(i32 noundef %.02029, ptr noundef %.01930, i16 noundef zeroext %13)
  %14 = tail call signext i16 @sflash_WaitBusy(i32 noundef 100)
  %.not27 = icmp eq i16 %14, 0
  br i1 %.not27, label %15, label %.loopexit

15:                                               ; preds = %12
  %16 = zext nneg i32 %spec.select to i64
  %17 = getelementptr inbounds i8, ptr %.01930, i64 %16
  %18 = add i32 %spec.select, %.02029
  %19 = sub i32 %.01831, %spec.select
  %.not25 = icmp eq i32 %19, 0
  br i1 %.not25, label %.loopexit, label %.lr.ph, !llvm.loop !11

.loopexit:                                        ; preds = %.lr.ph, %12, %15, %.preheader, %5, %3
  %.021 = phi i16 [ -105, %3 ], [ -106, %5 ], [ 0, %.preheader ], [ -102, %.lr.ph ], [ -101, %12 ], [ 0, %15 ]
  ret i16 %.021
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.umin.i32(i32, i32) #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 speculatable willreturn memory(none) }

!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 4}
!6 = !{!"", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12, !7, i64 16, !10, i64 20, !10, i64 22, !10, i64 24, !10, i64 26, !10, i64 28, !8, i64 32, !8, i64 160}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"short", !8, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


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