; 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._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) #3
  call void @llvm.lifetime.start.p0(i64 1, ptr %5) #3
  %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 %39

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

19:                                               ; preds = %35, %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 %38

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 = zext i8 %26 to i32
  %28 = icmp eq i32 %27, 0
  br i1 %28, label %29, label %30

29:                                               ; preds = %23
  br label %30

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

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

38:                                               ; 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 %40

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

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

; 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

; 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 = { 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)
  %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)
  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

; 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) #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 nounwind willreturn memory(argmem: readwrite) }

!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