; 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_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
@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(i32 noundef %0, ptr noundef %1, i8 noundef zeroext %2) #2 {
  %4 = alloca i8, align 1
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i8, align 1
  %8 = alloca i32, align 4
  %9 = alloca i8, align 1
  %10 = alloca %struct._sat_message, align 1
  %11 = alloca ptr, align 8
  %12 = alloca i32, align 4
  store i32 %0, ptr %5, align 4, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !9
  store i8 %2, ptr %7, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 1, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 513, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  %13 = load ptr, ptr %6, align 8, !tbaa !9
  store ptr %13, ptr %11, align 8, !tbaa !9
  %14 = load ptr, ptr %11, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %10, ptr align 1 %14, i64 513, i1 false), !tbaa.struct !12
  %15 = load i32, ptr %5, align 4, !tbaa !5
  %16 = zext i32 %15 to i64
  %17 = getelementptr inbounds [0 x ptr], ptr @mat, i64 0, i64 %16
  %18 = load ptr, ptr %17, align 8, !tbaa !9
  %19 = getelementptr inbounds %struct._sat_MAT, ptr %18, i32 0, i32 2
  %20 = load i8, ptr %7, align 1, !tbaa !11
  %21 = zext i8 %20 to i32
  %22 = ashr i32 %21, 5
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [8 x i32], ptr %19, i64 0, i64 %23
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %26 = icmp ne i32 %25, 0
  %27 = xor i1 %26, true
  %28 = zext i1 %27 to i32
  %29 = load i8, ptr %7, align 1, !tbaa !11
  %30 = zext i8 %29 to i32
  %31 = shl i32 1, %30
  %32 = and i32 %28, %31
  %33 = icmp ne i32 %32, 0
  br i1 %33, label %34, label %70

34:                                               ; preds = %3
  %35 = load i32, ptr %5, align 4, !tbaa !5
  %36 = zext i32 %35 to i64
  %37 = getelementptr inbounds [0 x ptr], ptr @mat, i64 0, i64 %36
  %38 = load ptr, ptr %37, align 8, !tbaa !9
  %39 = getelementptr inbounds %struct._sat_MAT, ptr %38, i32 0, i32 4
  %40 = load i8, ptr %7, align 1, !tbaa !11
  %41 = zext i8 %40 to i64
  %42 = getelementptr inbounds [2 x i32], ptr %39, i64 0, i64 %41
  %43 = load i32, ptr %42, align 4, !tbaa !5
  store i32 %43, ptr %8, align 4, !tbaa !5
  store i8 0, ptr %9, align 1, !tbaa !11
  br label %44

44:                                               ; preds = %58, %34
  %45 = load i8, ptr %9, align 1, !tbaa !11
  %46 = zext i8 %45 to i32
  %47 = icmp slt i32 %46, 512
  br i1 %47, label %48, label %56

48:                                               ; preds = %44
  %49 = getelementptr inbounds %struct._sat_message, ptr %10, i32 0, i32 0
  %50 = load i8, ptr %9, align 1, !tbaa !11
  %51 = zext i8 %50 to i64
  %52 = getelementptr inbounds [512 x i8], ptr %49, i64 0, i64 %51
  %53 = load i8, ptr %52, align 1, !tbaa !11
  %54 = zext i8 %53 to i32
  %55 = icmp ne i32 %54, 126
  br label %56

56:                                               ; preds = %48, %44
  %57 = phi i1 [ false, %44 ], [ %55, %48 ]
  br i1 %57, label %58, label %69

58:                                               ; preds = %56
  %59 = getelementptr inbounds %struct._sat_message, ptr %10, i32 0, i32 0
  %60 = load i8, ptr %9, align 1, !tbaa !11
  %61 = zext i8 %60 to i64
  %62 = getelementptr inbounds [512 x i8], ptr %59, i64 0, i64 %61
  %63 = load i8, ptr %62, align 1, !tbaa !11
  %64 = load i8, ptr %9, align 1, !tbaa !11
  %65 = zext i8 %64 to i64
  %66 = getelementptr inbounds [512 x i8], ptr @buffer, i64 0, i64 %65
  store i8 %63, ptr %66, align 1, !tbaa !11
  %67 = load i8, ptr %9, align 1, !tbaa !11
  %68 = add i8 %67, 1
  store i8 %68, ptr %9, align 1, !tbaa !11
  br label %44, !llvm.loop !13

69:                                               ; preds = %56
  br label %71

70:                                               ; preds = %3
  store i8 -1, ptr %4, align 1
  store i32 1, ptr %12, align 4
  br label %72

71:                                               ; preds = %69
  store i32 0, ptr %12, align 4
  br label %72

72:                                               ; preds = %71, %70
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 513, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  %73 = load i32, ptr %12, align 4
  switch i32 %73, label %76 [
    i32 0, label %74
    i32 1, label %74
  ]

74:                                               ; preds = %72, %72
  %75 = load i8, ptr %4, align 1
  ret i8 %75

76:                                               ; preds = %72
  unreachable
}

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 = !{!"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"}


; 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 }

@mat = external dso_local local_unnamed_addr global [0 x ptr], align 8
@buffer = external dso_local local_unnamed_addr 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: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local noundef zeroext i8 @write_message(i32 noundef %0, ptr nocapture noundef readonly %1, i8 noundef zeroext %2) local_unnamed_addr #2 {
  %4 = alloca %struct._sat_message, align 1
  call void @llvm.lifetime.start.p0(i64 513, ptr nonnull %4) #3
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(513) %4, ptr noundef nonnull align 1 dereferenceable(513) %1, i64 513, i1 false), !tbaa.struct !5
  %5 = zext i32 %0 to i64
  %6 = getelementptr inbounds [0 x ptr], ptr @mat, i64 0, i64 %5
  %7 = load ptr, ptr %6, align 8, !tbaa !9
  %8 = getelementptr inbounds i8, ptr %7, i64 8
  %9 = lshr i8 %2, 5
  %10 = zext nneg i8 %9 to i64
  %11 = getelementptr inbounds [8 x i32], ptr %8, i64 0, i64 %10
  %12 = load i32, ptr %11, align 4, !tbaa !11
  %.not = icmp ne i32 %12, 0
  %13 = icmp ne i8 %2, 0
  %narrow.not16 = or i1 %13, %.not
  %14 = load i8, ptr %4, align 1
  %.not1213 = icmp eq i8 %14, 126
  %or.cond = select i1 %narrow.not16, i1 true, i1 %.not1213
  br i1 %or.cond, label %.critedge, label %.lr.ph

.lr.ph:                                           ; preds = %3, %.lr.ph
  %15 = phi i8 [ %21, %.lr.ph ], [ %14, %3 ]
  %16 = phi i64 [ %19, %.lr.ph ], [ 0, %3 ]
  %.014 = phi i8 [ %18, %.lr.ph ], [ 0, %3 ]
  %17 = getelementptr inbounds [512 x i8], ptr @buffer, i64 0, i64 %16
  store i8 %15, ptr %17, align 1, !tbaa !6
  %18 = add i8 %.014, 1
  %19 = zext i8 %18 to i64
  %20 = getelementptr inbounds [512 x i8], ptr %4, i64 0, i64 %19
  %21 = load i8, ptr %20, align 1, !tbaa !6
  %.not12 = icmp eq i8 %21, 126
  br i1 %.not12, label %.critedge, label %.lr.ph, !llvm.loop !13

.critedge:                                        ; preds = %.lr.ph, %3
  call void @llvm.lifetime.end.p0(i64 513, ptr nonnull %4) #3
  ret i8 -1
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) 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 = !{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, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


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