; 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

; 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

declare void @sd_delay(i16 noundef zeroext) #2

; 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
define dso_local zeroext i8 @write_file(i32 noundef %0, ptr noundef %1) #3 {
  %3 = alloca i8, align 1
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca %struct._sat_file, align 1
  %7 = alloca %struct._sat_message, align 1
  %8 = alloca ptr, align 8
  %9 = alloca i8, align 1
  %10 = alloca i32, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 1027, ptr %6) #4
  call void @llvm.lifetime.start.p0(i64 513, ptr %7) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.start.p0(i64 1, ptr %9) #4
  %11 = load ptr, ptr @root, align 8, !tbaa !9
  %12 = getelementptr inbounds %struct._sat_FAT, ptr %11, i32 0, i32 2
  %13 = load i32, ptr %4, align 4, !tbaa !5
  %14 = lshr i32 %13, 5
  %15 = zext i32 %14 to i64
  %16 = getelementptr inbounds [4 x i32], ptr %12, i64 0, i64 %15
  %17 = load i32, ptr %16, align 4, !tbaa !5
  %18 = load i32, ptr %4, align 4, !tbaa !5
  %19 = shl i32 1, %18
  %20 = and i32 %17, %19
  %21 = icmp ne i32 %20, 0
  br i1 %21, label %22, label %46

22:                                               ; preds = %2
  store i8 0, ptr %9, align 1, !tbaa !11
  br label %23

23:                                               ; preds = %39, %22
  %24 = load i8, ptr %9, align 1, !tbaa !11
  %25 = zext i8 %24 to i32
  %26 = icmp slt i32 %25, 2
  br i1 %26, label %27, label %40

27:                                               ; preds = %23
  %28 = load ptr, ptr %5, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %6, ptr align 1 %28, i64 1027, i1 false), !tbaa.struct !12
  %29 = getelementptr inbounds %struct._sat_file, ptr %6, i32 0, i32 0
  %30 = load i8, ptr %9, align 1, !tbaa !11
  %31 = zext i8 %30 to i64
  %32 = getelementptr inbounds [2 x %struct._sat_message], ptr %29, i64 0, i64 %31
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %7, ptr align 1 %32, i64 513, i1 false), !tbaa.struct !13
  store ptr %7, ptr %8, align 8, !tbaa !9
  %33 = load i32, ptr %4, align 4, !tbaa !5
  %34 = load ptr, ptr %8, align 8, !tbaa !9
  %35 = load i8, ptr %9, align 1, !tbaa !11
  %36 = call zeroext i8 @write_message(i32 noundef %33, ptr noundef %34, i8 noundef zeroext %35)
  %37 = icmp ne i8 %36, 0
  br i1 %37, label %39, label %38

38:                                               ; preds = %27
  br label %40

39:                                               ; preds = %27
  call void @sd_delay(i16 noundef zeroext 15)
  br label %23, !llvm.loop !14

40:                                               ; preds = %38, %23
  %41 = load i8, ptr %9, align 1, !tbaa !11
  %42 = zext i8 %41 to i32
  %43 = icmp eq i32 %42, 2
  br i1 %43, label %44, label %45

44:                                               ; preds = %40
  store i8 0, ptr %3, align 1
  store i32 1, ptr %10, align 4
  br label %47

45:                                               ; preds = %40
  store i8 17, ptr %3, align 1
  store i32 1, ptr %10, align 4
  br label %47

46:                                               ; preds = %2
  store i8 1, ptr %3, align 1
  store i32 1, ptr %10, align 4
  br label %47

47:                                               ; preds = %46, %45, %44
  call void @llvm.lifetime.end.p0(i64 1, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 513, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 1027, ptr %6) #4
  %48 = load i8, ptr %3, align 1
  ret i8 %48
}

; Function Attrs: nounwind uwtable
declare dso_local zeroext i8 @write_message(i32 noundef, ptr noundef, i8 noundef zeroext) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 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 #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 1026, !11, i64 1026, i64 1, !11}
!13 = !{i64 0, i64 512, !11, i64 512, i64 1, !11}
!14 = distinct !{!14, !15}
!15 = !{!"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._sat_message = type { [512 x i8], i8 }

@root = external dso_local local_unnamed_addr global ptr, align 8

; 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 nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

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

; 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
define dso_local zeroext range(i8 1, 18) i8 @write_file(i32 noundef %0, ptr nocapture noundef readonly %1) local_unnamed_addr #3 {
  %3 = alloca %struct._sat_message, align 1
  call void @llvm.lifetime.start.p0(i64 513, ptr nonnull %3) #4
  %4 = load ptr, ptr @root, align 8, !tbaa !5
  %5 = lshr i32 %0, 5
  %6 = zext nneg i32 %5 to i64
  %7 = getelementptr inbounds [4 x i32], ptr %4, i64 0, i64 %6
  %8 = load i32, ptr %7, align 4, !tbaa !9
  %9 = shl nuw i32 1, %0
  %10 = and i32 %8, %9
  %.not = icmp eq i32 %10, 0
  br i1 %.not, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %2
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(513) %3, ptr noundef nonnull align 1 dereferenceable(513) %1, i64 513, i1 false)
  %11 = call zeroext i8 @write_message(i32 noundef %0, ptr noundef nonnull %3, i8 noundef zeroext 0)
  %.not910 = icmp eq i8 %11, 0
  br i1 %.not910, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  call void @sd_delay(i16 noundef zeroext 15) #4
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(513) %3, ptr noundef nonnull align 1 dereferenceable(513) %1, i64 513, i1 false)
  %12 = call zeroext i8 @write_message(i32 noundef %0, ptr noundef nonnull %3, i8 noundef zeroext 0)
  %.not9 = icmp eq i8 %12, 0
  br i1 %.not9, label %.loopexit, label %.lr.ph, !llvm.loop !11

.loopexit:                                        ; preds = %.lr.ph, %.preheader, %2
  %.0 = phi i8 [ 1, %2 ], [ 17, %.preheader ], [ 17, %.lr.ph ]
  call void @llvm.lifetime.end.p0(i64 513, ptr nonnull %3) #4
  ret i8 %.0
}

; Function Attrs: nounwind uwtable
declare dso_local zeroext i8 @write_message(i32 noundef, ptr noundef, i8 noundef zeroext) local_unnamed_addr #3

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 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 #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 = !{!"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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,DSEPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,MemCpyOptPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass