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

@ptr = external dso_local global ptr, align 8
@mat = external dso_local global [0 x ptr], align 8
@sd = external dso_local global %struct._sd, align 4
@temp = external dso_local global %struct._sat_message, align 1

; 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 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) #4
  %10 = load ptr, ptr %6, align 8, !tbaa !9
  store ptr %10, ptr @ptr, align 8, !tbaa !9
  %11 = load ptr, ptr @ptr, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 @temp, ptr align 1 %11, i64 513, i1 false), !tbaa.struct !12
  %12 = load i32, ptr %5, 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 2
  %17 = load i8, ptr %7, align 1, !tbaa !11
  %18 = zext i8 %17 to i32
  %19 = ashr i32 %18, 5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [8 x i32], ptr %16, i64 0, i64 %20
  %22 = load i32, ptr %21, align 4, !tbaa !5
  %23 = icmp ne i32 %22, 0
  %24 = xor i1 %23, true
  %25 = zext i1 %24 to i32
  %26 = load i8, ptr %7, align 1, !tbaa !11
  %27 = zext i8 %26 to i32
  %28 = shl i32 1, %27
  %29 = and i32 %25, %28
  %30 = icmp ne i32 %29, 0
  br i1 %30, label %31, label %44

31:                                               ; preds = %3
  %32 = load i32, ptr %5, align 4, !tbaa !5
  %33 = zext i32 %32 to i64
  %34 = getelementptr inbounds [0 x ptr], ptr @mat, i64 0, i64 %33
  %35 = load ptr, ptr %34, align 8, !tbaa !9
  %36 = getelementptr inbounds %struct._sat_MAT, ptr %35, i32 0, i32 4
  %37 = load i8, ptr %7, align 1, !tbaa !11
  %38 = zext i8 %37 to i64
  %39 = getelementptr inbounds [2 x i32], ptr %36, i64 0, i64 %38
  %40 = load i32, ptr %39, align 4, !tbaa !5
  store i32 %40, ptr %8, align 4, !tbaa !5
  %41 = load i32, ptr %8, align 4, !tbaa !5
  %42 = call i32 @sd_write_block(ptr noundef @sd, i32 noundef %41, ptr noundef @temp)
  %43 = trunc i32 %42 to i8
  store i8 %43, ptr %4, align 1
  store i32 1, ptr %9, align 4
  br label %45

44:                                               ; preds = %3
  store i8 -1, ptr %4, align 1
  store i32 1, ptr %9, align 4
  br label %45

45:                                               ; preds = %44, %31
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  %46 = load i8, ptr %4, align 1
  ret i8 %46
}

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 = !{!7, !7, i64 0}
!12 = !{i64 0, i64 512, !11, i64 512, i64 1, !11}


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

@ptr = external dso_local local_unnamed_addr global ptr, align 8
@mat = external dso_local local_unnamed_addr global [0 x ptr], align 8
@sd = external dso_local global %struct._sd, align 4
@temp = external dso_local global %struct._sat_message, align 1

; 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) #0

; Function Attrs: nounwind uwtable
define dso_local zeroext i8 @write_message(i32 noundef %0, ptr noundef %1, i8 noundef zeroext %2) local_unnamed_addr #1 {
  store ptr %1, ptr @ptr, align 8, !tbaa !5
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(513) @temp, ptr noundef nonnull align 1 dereferenceable(513) %1, i64 513, i1 false), !tbaa.struct !9
  %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 !5
  %7 = getelementptr inbounds i8, ptr %6, i64 8
  %8 = lshr i8 %2, 5
  %9 = zext nneg i8 %8 to i64
  %10 = getelementptr inbounds [8 x i32], ptr %7, i64 0, i64 %9
  %11 = load i32, ptr %10, align 4, !tbaa !11
  %.not = icmp eq i32 %11, 0
  %12 = icmp eq i8 %2, 0
  %narrow = and i1 %12, %.not
  br i1 %narrow, label %13, label %18

13:                                               ; preds = %3
  %14 = getelementptr inbounds i8, ptr %6, i64 72
  %15 = load i32, ptr %14, align 4, !tbaa !11
  %16 = tail call i32 @sd_write_block(ptr noundef nonnull @sd, i32 noundef %15, ptr noundef nonnull @temp) #3
  %17 = trunc i32 %16 to i8
  br label %18

18:                                               ; preds = %3, %13
  %.0 = phi i8 [ %17, %13 ], [ -1, %3 ]
  ret i8 %.0
}

declare i32 @sd_write_block(ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #2

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 }

!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 = !{i64 0, i64 512, !10, i64 512, i64 1, !10}
!10 = !{!7, !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass