; LLVM IR from embeddednirvana/A-Rudimentary-RTOS-for-Satellite-Control :: fs/patches (sat_fs)

; 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._sd = type { i32, i32, i8 }
%struct._sat_FAT = type { [0 x i32], [0 x i32], [4 x i32] }
%struct._sat_MAT = type { i32, [2 x i8], [8 x i32], [8 x i32], [2 x i32], i8 }

@root = external dso_local global ptr, align 8
@mat = external dso_local global [0 x ptr], align 8
@sd = external dso_local global %struct._sd, align 4
@root_fat = external dso_local global %struct._sat_FAT, align 4
@root_mat = external dso_local global [0 x %struct._sat_MAT], 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 nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

; 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 zeroext i8 @setup_FAT() #2

; Function Attrs: nounwind uwtable
declare dso_local zeroext i8 @setup_MAT() #2

; Function Attrs: nounwind uwtable
declare dso_local zeroext i8 @fs_mount_chip(i64, i8) #2

; Function Attrs: nounwind uwtable
define dso_local zeroext i8 @setup_File_System() #2 {
  %1 = alloca i32, align 4
  %2 = alloca i8, align 1
  %3 = alloca { i64, i8 }, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #3
  call void @llvm.lifetime.start.p0(i64 1, ptr %2) #3
  store i8 0, ptr %2, align 1, !tbaa !5
  store ptr @root_fat, ptr @root, align 8, !tbaa !8
  %4 = call zeroext i8 @setup_FAT()
  store i32 0, ptr %1, align 4, !tbaa !10
  br label %5

5:                                                ; preds = %15, %0
  %6 = load i32, ptr %1, align 4, !tbaa !10
  %7 = icmp ult i32 %6, 0
  br i1 %7, label %8, label %18

8:                                                ; preds = %5
  %9 = load i32, ptr %1, align 4, !tbaa !10
  %10 = zext i32 %9 to i64
  %11 = getelementptr inbounds %struct._sat_MAT, ptr @root_mat, i64 %10
  %12 = load i32, ptr %1, align 4, !tbaa !10
  %13 = zext i32 %12 to i64
  %14 = getelementptr inbounds [0 x ptr], ptr @mat, i64 0, i64 %13
  store ptr %11, ptr %14, align 8, !tbaa !8
  br label %15

15:                                               ; preds = %8
  %16 = load i32, ptr %1, align 4, !tbaa !10
  %17 = add i32 %16, 1
  store i32 %17, ptr %1, align 4, !tbaa !10
  br label %5, !llvm.loop !12

18:                                               ; preds = %5
  %19 = call zeroext i8 @setup_MAT()
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %3, ptr align 4 @sd, i64 12, i1 false)
  %20 = getelementptr inbounds { i64, i8 }, ptr %3, i32 0, i32 0
  %21 = load i64, ptr %20, align 4
  %22 = getelementptr inbounds { i64, i8 }, ptr %3, i32 0, i32 1
  %23 = load i8, ptr %22, align 4
  %24 = call zeroext i8 @fs_mount_chip(i64 %21, i8 %23)
  %25 = icmp ne i8 %24, 0
  br i1 %25, label %31, label %26

26:                                               ; preds = %18
  %27 = call zeroext i8 @check_filesystem()
  %28 = icmp ne i8 %27, 0
  br i1 %28, label %29, label %30

29:                                               ; preds = %26
  store i8 -1, ptr %2, align 1, !tbaa !5
  br label %30

30:                                               ; preds = %29, %26
  br label %31

31:                                               ; preds = %30, %18
  %32 = load i8, ptr %2, align 1, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 1, ptr %2) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #3
  ret i8 %32
}

; Function Attrs: nounwind uwtable
declare dso_local zeroext i8 @check_filesystem() #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"any pointer", !6, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"int", !6, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"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._sd = type { i32, i32, i8 }
%struct._sat_FAT = type { [0 x i32], [0 x i32], [4 x i32] }

@root = external dso_local local_unnamed_addr global ptr, align 8
@sd = external dso_local local_unnamed_addr global %struct._sd, align 4
@root_fat = external dso_local global %struct._sat_FAT, align 4

; Function Attrs: nounwind uwtable
declare dso_local zeroext i8 @setup_FAT() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local zeroext i8 @setup_MAT() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local zeroext i8 @fs_mount_chip(i64, i8) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local zeroext range(i8 -1, 1) i8 @setup_File_System() local_unnamed_addr #0 {
  store ptr @root_fat, ptr @root, align 8, !tbaa !5
  %1 = tail call zeroext i8 @setup_FAT()
  %2 = tail call zeroext i8 @setup_MAT()
  %.sroa.0.0.copyload = load i64, ptr @sd, align 4
  %.sroa.2.0.copyload = load i8, ptr getelementptr inbounds (i8, ptr @sd, i64 8), align 4
  %3 = tail call zeroext i8 @fs_mount_chip(i64 %.sroa.0.0.copyload, i8 %.sroa.2.0.copyload)
  %.not = icmp eq i8 %3, 0
  br i1 %.not, label %4, label %6

4:                                                ; preds = %0
  %5 = tail call zeroext i8 @check_filesystem()
  %.not6 = icmp ne i8 %5, 0
  %spec.select = sext i1 %.not6 to i8
  br label %6

6:                                                ; preds = %4, %0
  %.05 = phi i8 [ 0, %0 ], [ %spec.select, %4 ]
  ret i8 %.05
}

; Function Attrs: nounwind uwtable
declare dso_local zeroext i8 @check_filesystem() 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}


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