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

@mat = external dso_local global [0 x ptr], align 8
@sd = external dso_local global %struct._sd, align 4
@buffer = external dso_local global [512 x i8], align 16

; 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
define dso_local zeroext i8 @write_message_next(i32 noundef %0, ptr noundef %1) #2 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i8, align 1
  %7 = alloca i8, align 1
  %8 = alloca %struct._sat_message, align 1
  %9 = alloca ptr, align 8
  store i32 %0, ptr %3, align 4, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.start.p0(i64 1, ptr %6) #4
  call void @llvm.lifetime.start.p0(i64 1, ptr %7) #4
  call void @llvm.lifetime.start.p0(i64 513, ptr %8) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %10 = load ptr, ptr %4, align 8, !tbaa !9
  store ptr %10, ptr %9, align 8, !tbaa !9
  %11 = load ptr, ptr %9, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %8, ptr align 1 %11, i64 513, i1 false), !tbaa.struct !11
  %12 = load i32, ptr %3, align 4, !tbaa !5
  %13 = zext i32 %12 to i64
  %14 = getelementptr inbounds [0 x ptr], ptr @mat, i64 0, i64 %13
  %15 = load ptr, ptr %14, align 8, !tbaa !9
  %16 = getelementptr inbounds %struct._sat_MAT, ptr %15, i32 0, i32 5
  %17 = load i8, ptr %16, align 4, !tbaa !13
  store i8 %17, ptr %6, align 1, !tbaa !12
  br label %18

18:                                               ; preds = %35, %2
  %19 = load i32, ptr %3, align 4, !tbaa !5
  %20 = zext i32 %19 to i64
  %21 = getelementptr inbounds [0 x ptr], ptr @mat, i64 0, i64 %20
  %22 = load ptr, ptr %21, align 8, !tbaa !9
  %23 = getelementptr inbounds %struct._sat_MAT, ptr %22, i32 0, i32 2
  %24 = load i8, ptr %6, align 1, !tbaa !12
  %25 = zext i8 %24 to i32
  %26 = ashr i32 %25, 5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [8 x i32], ptr %23, i64 0, i64 %27
  %29 = load i32, ptr %28, align 4, !tbaa !5
  %30 = load i8, ptr %6, align 1, !tbaa !12
  %31 = zext i8 %30 to i32
  %32 = shl i32 1, %31
  %33 = and i32 %29, %32
  %34 = icmp ne i32 %33, 0
  br i1 %34, label %35, label %38

35:                                               ; preds = %18
  %36 = load i8, ptr %6, align 1, !tbaa !12
  %37 = add i8 %36, 1
  store i8 %37, ptr %6, align 1, !tbaa !12
  br label %18, !llvm.loop !15

38:                                               ; preds = %18
  %39 = load i8, ptr %6, align 1, !tbaa !12
  store i8 %39, ptr %7, align 1, !tbaa !12
  %40 = load i32, ptr %3, align 4, !tbaa !5
  %41 = zext i32 %40 to i64
  %42 = getelementptr inbounds [0 x ptr], ptr @mat, i64 0, i64 %41
  %43 = load ptr, ptr %42, align 8, !tbaa !9
  %44 = getelementptr inbounds %struct._sat_MAT, ptr %43, i32 0, i32 4
  %45 = load i8, ptr %6, align 1, !tbaa !12
  %46 = zext i8 %45 to i64
  %47 = getelementptr inbounds [2 x i32], ptr %44, i64 0, i64 %46
  %48 = load i32, ptr %47, align 4, !tbaa !5
  store i32 %48, ptr %5, align 4, !tbaa !5
  store i8 0, ptr %6, align 1, !tbaa !12
  br label %49

49:                                               ; preds = %57, %38
  %50 = getelementptr inbounds %struct._sat_message, ptr %8, i32 0, i32 0
  %51 = load i8, ptr %6, align 1, !tbaa !12
  %52 = zext i8 %51 to i64
  %53 = getelementptr inbounds [512 x i8], ptr %50, i64 0, i64 %52
  %54 = load i8, ptr %53, align 1, !tbaa !12
  %55 = zext i8 %54 to i32
  %56 = icmp ne i32 %55, 126
  br i1 %56, label %57, label %68

57:                                               ; preds = %49
  %58 = getelementptr inbounds %struct._sat_message, ptr %8, i32 0, i32 0
  %59 = load i8, ptr %6, align 1, !tbaa !12
  %60 = zext i8 %59 to i64
  %61 = getelementptr inbounds [512 x i8], ptr %58, i64 0, i64 %60
  %62 = load i8, ptr %61, align 1, !tbaa !12
  %63 = load i8, ptr %6, align 1, !tbaa !12
  %64 = zext i8 %63 to i64
  %65 = getelementptr inbounds [512 x i8], ptr @buffer, i64 0, i64 %64
  store i8 %62, ptr %65, align 1, !tbaa !12
  %66 = load i8, ptr %6, align 1, !tbaa !12
  %67 = add i8 %66, 1
  store i8 %67, ptr %6, align 1, !tbaa !12
  br label %49, !llvm.loop !17

68:                                               ; preds = %49
  %69 = load i32, ptr %5, align 4, !tbaa !5
  %70 = call i32 @sd_write_block(ptr noundef @sd, i32 noundef %69, ptr noundef @buffer)
  %71 = trunc i32 %70 to i8
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 513, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 1, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 1, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  ret i8 %71
}

declare i32 @sd_write_block(ptr noundef, i32 noundef, ptr noundef) #3

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 = { "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 = !{i64 0, i64 512, !12, i64 512, i64 1, !12}
!12 = !{!7, !7, i64 0}
!13 = !{!14, !7, i64 80}
!14 = !{!"_sat_MAT", !6, i64 0, !7, i64 4, !7, i64 8, !7, i64 40, !7, i64 72, !7, i64 80}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = distinct !{!17, !16}


; 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_message = type { [512 x i8], i8 }

@mat = external dso_local local_unnamed_addr global [0 x ptr], align 8
@sd = external dso_local global %struct._sd, align 4
@buffer = external dso_local global [512 x i8], align 16

; 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

; 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 i8 @write_message_next(i32 noundef %0, ptr nocapture noundef readonly %1) local_unnamed_addr #2 {
  %3 = alloca %struct._sat_message, align 1
  call void @llvm.lifetime.start.p0(i64 513, ptr nonnull %3) #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), !tbaa.struct !5
  %4 = zext i32 %0 to i64
  %5 = getelementptr inbounds [0 x ptr], ptr @mat, i64 0, i64 %4
  %6 = load ptr, ptr %5, align 8, !tbaa !9
  %7 = getelementptr inbounds i8, ptr %6, i64 80
  %8 = load i8, ptr %7, align 4, !tbaa !11
  %9 = getelementptr inbounds i8, ptr %6, i64 8
  br label %10

10:                                               ; preds = %10, %2
  %.0 = phi i8 [ %8, %2 ], [ %18, %10 ]
  %11 = zext i8 %.0 to i32
  %12 = lshr i32 %11, 5
  %13 = zext nneg i32 %12 to i64
  %14 = getelementptr inbounds [8 x i32], ptr %9, i64 0, i64 %13
  %15 = load i32, ptr %14, align 4, !tbaa !14
  %16 = shl nuw i32 1, %11
  %17 = and i32 %16, %15
  %.not = icmp eq i32 %17, 0
  %18 = add i8 %.0, 1
  br i1 %.not, label %19, label %10, !llvm.loop !15

19:                                               ; preds = %10
  %20 = getelementptr inbounds i8, ptr %6, i64 72
  %21 = zext i8 %.0 to i64
  %22 = getelementptr inbounds [2 x i32], ptr %20, i64 0, i64 %21
  %23 = load i32, ptr %22, align 4, !tbaa !14
  %24 = load i8, ptr %3, align 1, !tbaa !6
  %.not1415 = icmp eq i8 %24, 126
  br i1 %.not1415, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %19, %.lr.ph
  %25 = phi i8 [ %31, %.lr.ph ], [ %24, %19 ]
  %26 = phi i64 [ %29, %.lr.ph ], [ 0, %19 ]
  %.116 = phi i8 [ %28, %.lr.ph ], [ 0, %19 ]
  %27 = getelementptr inbounds [512 x i8], ptr @buffer, i64 0, i64 %26
  store i8 %25, ptr %27, align 1, !tbaa !6
  %28 = add i8 %.116, 1
  %29 = zext i8 %28 to i64
  %30 = getelementptr inbounds [512 x i8], ptr %3, i64 0, i64 %29
  %31 = load i8, ptr %30, align 1, !tbaa !6
  %.not14 = icmp eq i8 %31, 126
  br i1 %.not14, label %._crit_edge, label %.lr.ph, !llvm.loop !17

._crit_edge:                                      ; preds = %.lr.ph, %19
  %32 = tail call i32 @sd_write_block(ptr noundef nonnull @sd, i32 noundef %23, ptr noundef nonnull @buffer) #4
  %33 = trunc i32 %32 to i8
  call void @llvm.lifetime.end.p0(i64 513, ptr nonnull %3) #4
  ret i8 %33
}

declare i32 @sd_write_block(ptr noundef, i32 noundef, ptr noundef) 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 = { 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 = { "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 = !{i64 0, i64 512, !6, i64 512, i64 1, !6}
!6 = !{!7, !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 = !{!12, !7, i64 80}
!12 = !{!"_sat_MAT", !13, i64 0, !7, i64 4, !7, i64 8, !7, i64 40, !7, i64 72, !7, i64 80}
!13 = !{!"int", !7, i64 0}
!14 = !{!13, !13, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = distinct !{!17, !16}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass