; LLVM IR from joembedded/JesFs :: jesfs_hl (fs_format)

; 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 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 i32 @fs_get_secs() #1

; Function Attrs: nounwind uwtable
declare dso_local signext i16 @fs_start(i8 noundef zeroext) #1

declare signext i16 @_supply_voltage_check() #2

declare void @sflash_read(i32 noundef, ptr noundef, i16 noundef zeroext) #2

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

12:                                               ; preds = %1
  store i16 -148, ptr %2, align 2
  store i32 1, ptr %7, align 4
  br label %83

13:                                               ; preds = %1
  %14 = call signext i16 @_supply_voltage_check()
  %15 = icmp ne i16 %14, 0
  br i1 %15, label %16, label %21

16:                                               ; preds = %13
  %17 = load i8, ptr getelementptr inbounds (%struct.SFLASH_INFO, ptr @sflash_info, i32 0, i32 11), align 4, !tbaa !8
  %18 = zext i8 %17 to i32
  %19 = or i32 %18, 2
  %20 = trunc i32 %19 to i8
  store i8 %20, ptr getelementptr inbounds (%struct.SFLASH_INFO, ptr @sflash_info, i32 0, i32 11), align 4, !tbaa !8
  store i16 -147, ptr %2, align 2
  store i32 1, ptr %7, align 4
  br label %83

21:                                               ; preds = %13
  %22 = load i8, ptr %3, align 1, !tbaa !5
  %23 = zext i8 %22 to i32
  %24 = icmp eq i32 %23, 2
  br i1 %24, label %25, label %54

25:                                               ; preds = %21
  store i32 0, ptr %6, align 4, !tbaa !12
  br label %26

26:                                               ; preds = %50, %25
  %27 = load i32, ptr %6, align 4, !tbaa !12
  %28 = load i32, ptr getelementptr inbounds (%struct.SFLASH_INFO, ptr @sflash_info, i32 0, i32 1), align 4, !tbaa !13
  %29 = icmp ult i32 %27, %28
  br i1 %29, label %30, label %53

30:                                               ; preds = %26
  %31 = load i32, ptr %6, align 4, !tbaa !12
  call void @sflash_read(i32 noundef %31, ptr noundef %4, i16 noundef zeroext 8)
  %32 = getelementptr inbounds [3 x i32], ptr %4, i64 0, i64 0
  %33 = load i32, ptr %32, align 4, !tbaa !12
  %34 = icmp eq i32 %33, -1
  br i1 %34, label %35, label %42

35:                                               ; preds = %30
  %36 = load i32, ptr %6, align 4, !tbaa !12
  %37 = call zeroext i16 @sflash_find_mlen(i32 noundef %36, i16 noundef zeroext 4096)
  store i16 %37, ptr %5, align 2, !tbaa !14
  %38 = load i16, ptr %5, align 2, !tbaa !14
  %39 = icmp ne i16 %38, 0
  br i1 %39, label %41, label %40

40:                                               ; preds = %35
  br label %50

41:                                               ; preds = %35
  br label %42

42:                                               ; preds = %41, %30
  %43 = load i32, ptr %6, align 4, !tbaa !12
  %44 = call signext i16 @sflash_SectorErase(i32 noundef %43)
  store i16 %44, ptr %5, align 2, !tbaa !14
  %45 = load i16, ptr %5, align 2, !tbaa !14
  %46 = icmp ne i16 %45, 0
  br i1 %46, label %47, label %49

47:                                               ; preds = %42
  %48 = load i16, ptr %5, align 2, !tbaa !14
  store i16 %48, ptr %2, align 2
  store i32 1, ptr %7, align 4
  br label %83

49:                                               ; preds = %42
  br label %50

50:                                               ; preds = %49, %40
  %51 = load i32, ptr %6, align 4, !tbaa !12
  %52 = add i32 %51, 4096
  store i32 %52, ptr %6, align 4, !tbaa !12
  br label %26, !llvm.loop !15

53:                                               ; preds = %26
  br label %69

54:                                               ; preds = %21
  %55 = load i8, ptr %3, align 1, !tbaa !5
  %56 = zext i8 %55 to i32
  %57 = icmp eq i32 %56, 1
  br i1 %57, label %58, label %67

58:                                               ; preds = %54
  %59 = call signext i16 @sflash_WaitWriteEnabled()
  %60 = icmp ne i16 %59, 0
  br i1 %60, label %61, label %62

61:                                               ; preds = %58
  store i16 -102, ptr %2, align 2
  store i32 1, ptr %7, align 4
  br label %83

62:                                               ; preds = %58
  call void @sflash_BulkErase()
  %63 = call signext i16 @sflash_WaitBusy(i32 noundef 240000)
  %64 = icmp ne i16 %63, 0
  br i1 %64, label %65, label %66

65:                                               ; preds = %62
  store i16 -101, ptr %2, align 2
  store i32 1, ptr %7, align 4
  br label %83

66:                                               ; preds = %62
  br label %68

67:                                               ; preds = %54
  store i16 -139, ptr %2, align 2
  store i32 1, ptr %7, align 4
  br label %83

68:                                               ; preds = %66
  br label %69

69:                                               ; preds = %68, %53
  %70 = getelementptr inbounds [3 x i32], ptr %4, i64 0, i64 0
  store i32 1181967690, ptr %70, align 4, !tbaa !12
  %71 = load i32, ptr @sflash_info, align 4, !tbaa !17
  %72 = getelementptr inbounds [3 x i32], ptr %4, i64 0, i64 1
  store i32 %71, ptr %72, align 4, !tbaa !12
  %73 = call i32 @fs_get_secs()
  %74 = getelementptr inbounds [3 x i32], ptr %4, i64 0, i64 2
  store i32 %73, ptr %74, align 4, !tbaa !12
  %75 = getelementptr inbounds [3 x i32], ptr %4, i64 0, i64 0
  %76 = call signext i16 @sflash_SectorWrite(i32 noundef 0, ptr noundef %75, i32 noundef 12)
  store i16 %76, ptr %5, align 2, !tbaa !14
  %77 = load i16, ptr %5, align 2, !tbaa !14
  %78 = icmp ne i16 %77, 0
  br i1 %78, label %79, label %81

79:                                               ; preds = %69
  %80 = load i16, ptr %5, align 2, !tbaa !14
  store i16 %80, ptr %2, align 2
  store i32 1, ptr %7, align 4
  br label %83

81:                                               ; preds = %69
  %82 = call signext i16 @fs_start(i8 noundef zeroext 0)
  store i16 %82, ptr %2, align 2
  store i32 1, ptr %7, align 4
  br label %83

83:                                               ; preds = %81, %79, %67, %65, %61, %47, %16, %12
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 12, ptr %4) #3
  %84 = load i16, ptr %2, align 2
  ret i16 %84
}

; Function Attrs: nounwind uwtable
declare hidden zeroext i16 @sflash_find_mlen(i32 noundef, i16 noundef zeroext) #1

declare signext i16 @sflash_SectorErase(i32 noundef) #2

declare signext i16 @sflash_WaitWriteEnabled() #2

declare void @sflash_BulkErase() #2

declare signext i16 @sflash_WaitBusy(i32 noundef) #2

declare signext i16 @sflash_SectorWrite(i32 noundef, ptr noundef, i32 noundef) #2

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

%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 local_unnamed_addr global %struct.SFLASH_INFO, align 4

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

; Function Attrs: mustprogress 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 i32 @fs_get_secs() local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local signext i16 @fs_start(i8 noundef zeroext) local_unnamed_addr #1

declare signext i16 @_supply_voltage_check() local_unnamed_addr #2

declare void @sflash_read(i32 noundef, ptr noundef, i16 noundef zeroext) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local signext i16 @fs_format(i8 noundef zeroext %0) local_unnamed_addr #1 {
  %2 = alloca [3 x i32], align 4
  call void @llvm.lifetime.start.p0(i64 12, ptr nonnull %2) #3
  %3 = load i8, ptr getelementptr inbounds (i8, ptr @sflash_info, i64 160), align 4, !tbaa !5
  %4 = and i8 %3, 3
  %.not = icmp eq i8 %4, 0
  br i1 %.not, label %5, label %.loopexit23

5:                                                ; preds = %1
  %6 = tail call signext i16 @_supply_voltage_check() #3
  %.not17 = icmp eq i16 %6, 0
  br i1 %.not17, label %10, label %7

7:                                                ; preds = %5
  %8 = load i8, ptr getelementptr inbounds (i8, ptr @sflash_info, i64 160), align 4, !tbaa !5
  %9 = or i8 %8, 2
  store i8 %9, ptr getelementptr inbounds (i8, ptr @sflash_info, i64 160), align 4, !tbaa !5
  br label %.loopexit23

10:                                               ; preds = %5
  switch i8 %0, label %.loopexit23 [
    i8 2, label %.preheader
    i8 1, label %22
  ]

.preheader:                                       ; preds = %10
  %11 = load i32, ptr getelementptr inbounds (i8, ptr @sflash_info, i64 4), align 4, !tbaa !11
  %.not25 = icmp eq i32 %11, 0
  br i1 %.not25, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %18
  %.024 = phi i32 [ %19, %18 ], [ 0, %.preheader ]
  call void @sflash_read(i32 noundef %.024, ptr noundef nonnull %2, i16 noundef zeroext 8) #3
  %12 = load i32, ptr %2, align 4, !tbaa !12
  %13 = icmp eq i32 %12, -1
  br i1 %13, label %14, label %16

14:                                               ; preds = %.lr.ph
  %15 = call zeroext i16 @sflash_find_mlen(i32 noundef %.024, i16 noundef zeroext 4096)
  %.not21 = icmp eq i16 %15, 0
  br i1 %.not21, label %18, label %16

16:                                               ; preds = %14, %.lr.ph
  %17 = call signext i16 @sflash_SectorErase(i32 noundef %.024) #3
  %.not22 = icmp eq i16 %17, 0
  br i1 %.not22, label %18, label %.loopexit23

18:                                               ; preds = %16, %14
  %19 = add i32 %.024, 4096
  %20 = load i32, ptr getelementptr inbounds (i8, ptr @sflash_info, i64 4), align 4, !tbaa !11
  %21 = icmp ult i32 %19, %20
  br i1 %21, label %.lr.ph, label %.loopexit, !llvm.loop !13

22:                                               ; preds = %10
  %23 = tail call signext i16 @sflash_WaitWriteEnabled() #3
  %.not18 = icmp eq i16 %23, 0
  br i1 %.not18, label %24, label %.loopexit23

24:                                               ; preds = %22
  tail call void @sflash_BulkErase() #3
  %25 = tail call signext i16 @sflash_WaitBusy(i32 noundef 240000) #3
  %.not19 = icmp eq i16 %25, 0
  br i1 %.not19, label %.loopexit, label %.loopexit23

.loopexit:                                        ; preds = %18, %.preheader, %24
  store i32 1181967690, ptr %2, align 4, !tbaa !12
  %26 = load i32, ptr @sflash_info, align 4, !tbaa !15
  %27 = getelementptr inbounds i8, ptr %2, i64 4
  store i32 %26, ptr %27, align 4, !tbaa !12
  %28 = call i32 @fs_get_secs()
  %29 = getelementptr inbounds i8, ptr %2, i64 8
  store i32 %28, ptr %29, align 4, !tbaa !12
  %30 = call signext i16 @sflash_SectorWrite(i32 noundef 0, ptr noundef nonnull %2, i32 noundef 12) #3
  %.not20 = icmp eq i16 %30, 0
  br i1 %.not20, label %31, label %.loopexit23

31:                                               ; preds = %.loopexit
  %32 = call signext i16 @fs_start(i8 noundef zeroext 0)
  br label %.loopexit23

.loopexit23:                                      ; preds = %16, %.loopexit, %10, %24, %22, %1, %31, %7
  %.013 = phi i16 [ -147, %7 ], [ %32, %31 ], [ -148, %1 ], [ -102, %22 ], [ -101, %24 ], [ -139, %10 ], [ %30, %.loopexit ], [ %17, %16 ]
  call void @llvm.lifetime.end.p0(i64 12, ptr nonnull %2) #3
  ret i16 %.013
}

; Function Attrs: nounwind uwtable
declare hidden zeroext i16 @sflash_find_mlen(i32 noundef, i16 noundef zeroext) local_unnamed_addr #1

declare signext i16 @sflash_SectorErase(i32 noundef) local_unnamed_addr #2

declare signext i16 @sflash_WaitWriteEnabled() local_unnamed_addr #2

declare void @sflash_BulkErase() local_unnamed_addr #2

declare signext i16 @sflash_WaitBusy(i32 noundef) local_unnamed_addr #2

declare signext i16 @sflash_SectorWrite(i32 noundef, ptr noundef, i32 noundef) local_unnamed_addr #2

attributes #0 = { mustprogress 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 = { "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, !8, i64 160}
!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 = !{!6, !7, i64 4}
!12 = !{!7, !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!6, !7, i64 0}


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