; LLVM IR from embeddednirvana/A-Rudimentary-RTOS-for-Satellite-Control :: fs (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._sat_file = type { [2 x %struct._sat_message], i8 }
%struct._sat_message = type { [512 x i8], i8 }
%struct._sat_FAT = type { [0 x i32], [0 x i32], [4 x i32] }

@root = external dso_local global ptr, align 8
@ptr = external dso_local global ptr, align 8
@tempf = external dso_local global %struct._sat_file, align 1
@ptrf = external dso_local global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local zeroext i8 @read_file(i32 noundef %0) #0 {
  %2 = alloca i8, align 1
  %3 = alloca i32, align 4
  %4 = alloca %struct._sat_message, align 1
  %5 = alloca i8, align 1
  %6 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 513, ptr %4) #4
  call void @llvm.lifetime.start.p0(i64 1, ptr %5) #4
  %7 = load ptr, ptr @root, align 8, !tbaa !9
  %8 = getelementptr inbounds %struct._sat_FAT, ptr %7, i32 0, i32 2
  %9 = load i32, ptr %3, align 4, !tbaa !5
  %10 = lshr i32 %9, 5
  %11 = zext i32 %10 to i64
  %12 = getelementptr inbounds [4 x i32], ptr %8, i64 0, i64 %11
  %13 = load i32, ptr %12, align 4, !tbaa !5
  %14 = load i32, ptr %3, align 4, !tbaa !5
  %15 = shl i32 1, %14
  %16 = and i32 %13, %15
  %17 = icmp ne i32 %16, 0
  br i1 %17, label %18, label %38

18:                                               ; preds = %1
  store i8 0, ptr %5, align 1, !tbaa !11
  br label %19

19:                                               ; preds = %34, %18
  %20 = load i8, ptr %5, align 1, !tbaa !11
  %21 = zext i8 %20 to i32
  %22 = icmp slt i32 %21, 2
  br i1 %22, label %23, label %37

23:                                               ; preds = %19
  %24 = load i32, ptr %3, align 4, !tbaa !5
  %25 = load i8, ptr %5, align 1, !tbaa !11
  %26 = call zeroext i8 @read_message(i32 noundef %24, i8 noundef zeroext %25)
  %27 = icmp ne i8 %26, 0
  br i1 %27, label %28, label %29

28:                                               ; preds = %23
  br label %29

29:                                               ; preds = %28, %23
  %30 = load ptr, ptr @ptr, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %4, ptr align 1 %30, i64 513, i1 false), !tbaa.struct !12
  %31 = load i8, ptr %5, align 1, !tbaa !11
  %32 = zext i8 %31 to i64
  %33 = getelementptr inbounds [2 x %struct._sat_message], ptr @tempf, i64 0, i64 %32
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %33, ptr align 1 %4, i64 513, i1 false), !tbaa.struct !12
  call void @sd_delay(i16 noundef zeroext 15)
  br label %34

34:                                               ; preds = %29
  %35 = load i8, ptr %5, align 1, !tbaa !11
  %36 = add i8 %35, 1
  store i8 %36, ptr %5, align 1, !tbaa !11
  br label %19, !llvm.loop !13

37:                                               ; preds = %19
  store i8 17, ptr getelementptr inbounds (%struct._sat_file, ptr @tempf, i32 0, i32 1), align 1, !tbaa !15
  store ptr @tempf, ptr @ptrf, align 8, !tbaa !9
  store i8 0, ptr %2, align 1
  store i32 1, ptr %6, align 4
  br label %39

38:                                               ; preds = %1
  store i8 17, ptr %2, align 1
  store i32 1, ptr %6, align 4
  br label %39

39:                                               ; preds = %38, %37
  call void @llvm.lifetime.end.p0(i64 1, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 513, ptr %4) #4
  %40 = load i8, ptr %2, align 1
  ret i8 %40
}

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

; Function Attrs: nounwind uwtable
declare dso_local zeroext i8 @read_message(i32 noundef, i8 noundef zeroext) #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) #2

declare void @sd_delay(i16 noundef zeroext) #3

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { "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 #4 = { 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 = !{!7, !7, i64 0}
!12 = !{i64 0, i64 512, !11, i64 512, i64 1, !11}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!16, !7, i64 1026}
!16 = !{!"_sat_file", !7, i64 0, !7, i64 1026}


; 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._sat_file = type { [2 x %struct._sat_message], i8 }
%struct._sat_message = type { [512 x i8], i8 }

@root = external dso_local local_unnamed_addr global ptr, align 8
@ptr = external dso_local local_unnamed_addr global ptr, align 8
@tempf = external dso_local global %struct._sat_file, align 1
@ptrf = external dso_local local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local zeroext range(i8 0, 18) i8 @read_file(i32 noundef %0) local_unnamed_addr #0 {
  %2 = load ptr, ptr @root, align 8, !tbaa !5
  %3 = lshr i32 %0, 5
  %4 = zext nneg i32 %3 to i64
  %5 = getelementptr inbounds [4 x i32], ptr %2, i64 0, i64 %4
  %6 = load i32, ptr %5, align 4, !tbaa !9
  %7 = shl nuw i32 1, %0
  %8 = and i32 %6, %7
  %.not = icmp eq i32 %8, 0
  br i1 %.not, label %13, label %.preheader.preheader

.preheader.preheader:                             ; preds = %1
  %9 = tail call zeroext i8 @read_message(i32 noundef %0, i8 noundef zeroext 0)
  %10 = load ptr, ptr @ptr, align 8, !tbaa !5
  tail call void @llvm.memmove.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(513) @tempf, ptr noundef nonnull align 1 dereferenceable(513) %10, i64 513, i1 false)
  tail call void @sd_delay(i16 noundef zeroext 15) #3
  %11 = tail call zeroext i8 @read_message(i32 noundef %0, i8 noundef zeroext 1)
  %12 = load ptr, ptr @ptr, align 8, !tbaa !5
  tail call void @llvm.memmove.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(513) getelementptr inbounds (i8, ptr @tempf, i64 513), ptr noundef nonnull align 1 dereferenceable(513) %12, i64 513, i1 false)
  tail call void @sd_delay(i16 noundef zeroext 15) #3
  store i8 17, ptr getelementptr inbounds (i8, ptr @tempf, i64 1026), align 1, !tbaa !11
  store ptr @tempf, ptr @ptrf, align 8, !tbaa !5
  br label %13

13:                                               ; preds = %1, %.preheader.preheader
  %.08 = phi i8 [ 0, %.preheader.preheader ], [ 17, %1 ]
  ret i8 %.08
}

; Function Attrs: nounwind uwtable
declare dso_local zeroext i8 @read_message(i32 noundef, i8 noundef zeroext) local_unnamed_addr #0

declare void @sd_delay(i16 noundef zeroext) local_unnamed_addr #1

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memmove.p0.p0.i64(ptr nocapture writeonly, ptr nocapture readonly, i64, i1 immarg) #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 = { "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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !7, i64 1026}
!12 = !{!"_sat_file", !7, i64 0, !7, i64 1026}


; Effective O3 passes: CorrelatedValuePropagationPass,DSEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,LoopSimplifyPass,MemCpyOptPass,SROAPass,SimplifyCFGPass,TailCallElimPass