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

; 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] }
%struct.FS_DESC = type { i32, i32, i32, i32, i32, i32, i16, i8 }

@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

declare signext i16 @_supply_voltage_check() #1

; Function Attrs: nounwind uwtable
declare hidden signext i16 @sflash_sadr_invalid(i32 noundef) #2

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

; Function Attrs: nounwind uwtable
define dso_local signext i16 @fs_close(ptr noundef %0) #2 {
  %2 = alloca i16, align 2
  %3 = alloca ptr, align 8
  %4 = alloca i16, align 2
  %5 = alloca i32, align 4
  %6 = alloca [2 x i32], align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 2, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %8 = load i8, ptr getelementptr inbounds (%struct.SFLASH_INFO, ptr @sflash_info, i32 0, i32 11), align 4, !tbaa !9
  %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 %64

13:                                               ; preds = %1
  %14 = load ptr, ptr %3, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.FS_DESC, ptr %14, i32 0, i32 0
  %16 = load i32, ptr %15, align 4, !tbaa !13
  %17 = icmp ne i32 %16, 0
  br i1 %17, label %19, label %18

18:                                               ; preds = %13
  store i16 -117, ptr %2, align 2
  store i32 1, ptr %7, align 4
  br label %64

19:                                               ; preds = %13
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.FS_DESC, ptr %20, i32 0, i32 0
  %22 = load i32, ptr %21, align 4, !tbaa !13
  store i32 %22, ptr %5, align 4, !tbaa !15
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.FS_DESC, ptr %23, i32 0, i32 0
  store i32 0, ptr %24, align 4, !tbaa !13
  %25 = load ptr, ptr %3, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.FS_DESC, ptr %25, i32 0, i32 7
  %27 = load i8, ptr %26, align 2, !tbaa !16
  %28 = zext i8 %27 to i32
  %29 = and i32 %28, 4
  %30 = icmp ne i32 %29, 0
  br i1 %30, label %31, label %63

31:                                               ; preds = %19
  %32 = load i32, ptr %5, align 4, !tbaa !15
  %33 = call signext i16 @sflash_sadr_invalid(i32 noundef %32)
  %34 = icmp ne i16 %33, 0
  br i1 %34, label %35, label %36

35:                                               ; preds = %31
  store i16 -120, ptr %2, align 2
  store i32 1, ptr %7, align 4
  br label %64

36:                                               ; preds = %31
  %37 = call signext i16 @_supply_voltage_check()
  %38 = icmp ne i16 %37, 0
  br i1 %38, label %39, label %44

39:                                               ; preds = %36
  %40 = load i8, ptr getelementptr inbounds (%struct.SFLASH_INFO, ptr @sflash_info, i32 0, i32 11), align 4, !tbaa !9
  %41 = zext i8 %40 to i32
  %42 = or i32 %41, 2
  %43 = trunc i32 %42 to i8
  store i8 %43, ptr getelementptr inbounds (%struct.SFLASH_INFO, ptr @sflash_info, i32 0, i32 11), align 4, !tbaa !9
  store i16 -147, ptr %2, align 2
  store i32 1, ptr %7, align 4
  br label %64

44:                                               ; preds = %36
  %45 = load ptr, ptr %3, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.FS_DESC, ptr %45, i32 0, i32 2
  %47 = load i32, ptr %46, align 4, !tbaa !17
  %48 = getelementptr inbounds [2 x i32], ptr %6, i64 0, i64 0
  store i32 %47, ptr %48, align 4, !tbaa !15
  %49 = load ptr, ptr %3, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.FS_DESC, ptr %49, i32 0, i32 4
  %51 = load i32, ptr %50, align 4, !tbaa !18
  %52 = getelementptr inbounds [2 x i32], ptr %6, i64 0, i64 1
  store i32 %51, ptr %52, align 4, !tbaa !15
  %53 = load i32, ptr %5, align 4, !tbaa !15
  %54 = add i32 %53, 12
  %55 = add i32 %54, 0
  %56 = getelementptr inbounds [2 x i32], ptr %6, i64 0, i64 0
  %57 = call signext i16 @sflash_SectorWrite(i32 noundef %55, ptr noundef %56, i32 noundef 8)
  store i16 %57, ptr %4, align 2, !tbaa !19
  %58 = load i16, ptr %4, align 2, !tbaa !19
  %59 = icmp ne i16 %58, 0
  br i1 %59, label %60, label %62

60:                                               ; preds = %44
  %61 = load i16, ptr %4, align 2, !tbaa !19
  store i16 %61, ptr %2, align 2
  store i32 1, ptr %7, align 4
  br label %64

62:                                               ; preds = %44
  br label %63

63:                                               ; preds = %62, %19
  store i16 0, ptr %2, align 2
  store i32 1, ptr %7, align 4
  br label %64

64:                                               ; preds = %63, %60, %39, %35, %18, %12
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %4) #3
  %65 = load i16, ptr %2, align 2
  ret i16 %65
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "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 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !7, i64 160}
!10 = !{!"", !11, i64 0, !11, i64 4, !11, i64 8, !11, i64 12, !11, i64 16, !12, i64 20, !12, i64 22, !12, i64 24, !12, i64 26, !12, i64 28, !7, i64 32, !7, i64 160}
!11 = !{!"int", !7, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = !{!14, !11, i64 0}
!14 = !{!"", !11, i64 0, !11, i64 4, !11, i64 8, !11, i64 12, !11, i64 16, !11, i64 20, !12, i64 24, !7, i64 26}
!15 = !{!11, !11, i64 0}
!16 = !{!14, !7, i64 26}
!17 = !{!14, !11, i64 8}
!18 = !{!14, !11, i64 16}
!19 = !{!12, !12, 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

declare signext i16 @_supply_voltage_check() local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden signext i16 @sflash_sadr_invalid(i32 noundef) local_unnamed_addr #2

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

; Function Attrs: nounwind uwtable
define dso_local signext i16 @fs_close(ptr nocapture noundef %0) local_unnamed_addr #2 {
  %2 = alloca [2 x i32], align 4
  call void @llvm.lifetime.start.p0(i64 8, 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 %27

5:                                                ; preds = %1
  %6 = load i32, ptr %0, align 4, !tbaa !11
  %.not12 = icmp eq i32 %6, 0
  br i1 %.not12, label %27, label %7

7:                                                ; preds = %5
  store i32 0, ptr %0, align 4, !tbaa !11
  %8 = getelementptr inbounds i8, ptr %0, i64 26
  %9 = load i8, ptr %8, align 2, !tbaa !13
  %10 = and i8 %9, 4
  %.not13 = icmp eq i8 %10, 0
  br i1 %.not13, label %26, label %11

11:                                               ; preds = %7
  %12 = tail call signext i16 @sflash_sadr_invalid(i32 noundef %6)
  %.not14 = icmp eq i16 %12, 0
  br i1 %.not14, label %13, label %27

13:                                               ; preds = %11
  %14 = tail call signext i16 @_supply_voltage_check() #3
  %.not15 = icmp eq i16 %14, 0
  br i1 %.not15, label %18, label %15

15:                                               ; preds = %13
  %16 = load i8, ptr getelementptr inbounds (i8, ptr @sflash_info, i64 160), align 4, !tbaa !5
  %17 = or i8 %16, 2
  store i8 %17, ptr getelementptr inbounds (i8, ptr @sflash_info, i64 160), align 4, !tbaa !5
  br label %27

18:                                               ; preds = %13
  %19 = getelementptr inbounds i8, ptr %0, i64 8
  %20 = load i32, ptr %19, align 4, !tbaa !14
  store i32 %20, ptr %2, align 4, !tbaa !15
  %21 = getelementptr inbounds i8, ptr %0, i64 16
  %22 = load i32, ptr %21, align 4, !tbaa !16
  %23 = getelementptr inbounds i8, ptr %2, i64 4
  store i32 %22, ptr %23, align 4, !tbaa !15
  %24 = add i32 %6, 12
  %25 = call signext i16 @sflash_SectorWrite(i32 noundef %24, ptr noundef nonnull %2, i32 noundef 8) #3
  %.not16 = icmp eq i16 %25, 0
  br i1 %.not16, label %26, label %27

26:                                               ; preds = %18, %7
  br label %27

27:                                               ; preds = %18, %11, %5, %1, %26, %15
  %.0 = phi i16 [ -147, %15 ], [ 0, %26 ], [ -148, %1 ], [ -117, %5 ], [ -120, %11 ], [ %25, %18 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2) #3
  ret i16 %.0
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "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 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, !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 = !{!12, !7, i64 0}
!12 = !{!"", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12, !7, i64 16, !7, i64 20, !10, i64 24, !8, i64 26}
!13 = !{!12, !8, i64 26}
!14 = !{!12, !7, i64 8}
!15 = !{!7, !7, i64 0}
!16 = !{!12, !7, i64 16}


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