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

@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
@buffer = external dso_local global [512 x i8], align 16
@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: nounwind uwtable
define dso_local zeroext i8 @read_message(i32 noundef %0, i8 noundef zeroext %1) #1 {
  %3 = alloca i8, align 1
  %4 = alloca i32, align 4
  %5 = alloca i8, align 1
  %6 = alloca i32, align 4
  %7 = alloca i8, align 1
  %8 = alloca i8, align 1
  %9 = alloca i32, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store i8 %1, ptr %5, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 1, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 1, ptr %8) #3
  %10 = load i32, ptr %4, align 4, !tbaa !5
  %11 = zext i32 %10 to i64
  %12 = getelementptr inbounds [0 x ptr], ptr @mat, i64 0, i64 %11
  %13 = load ptr, ptr %12, align 8, !tbaa !10
  %14 = getelementptr inbounds %struct._sat_MAT, ptr %13, i32 0, i32 3
  %15 = load i8, ptr %5, align 1, !tbaa !9
  %16 = zext i8 %15 to i32
  %17 = ashr i32 %16, 5
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [8 x i32], ptr %14, i64 0, i64 %18
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = load i8, ptr %5, align 1, !tbaa !9
  %22 = zext i8 %21 to i32
  %23 = shl i32 1, %22
  %24 = and i32 %20, %23
  %25 = icmp ne i32 %24, 0
  br i1 %25, label %26, label %69

26:                                               ; preds = %2
  %27 = load i32, ptr %4, align 4, !tbaa !5
  %28 = zext i32 %27 to i64
  %29 = getelementptr inbounds [0 x ptr], ptr @mat, i64 0, i64 %28
  %30 = load ptr, ptr %29, align 8, !tbaa !10
  %31 = getelementptr inbounds %struct._sat_MAT, ptr %30, i32 0, i32 4
  %32 = load i8, ptr %5, align 1, !tbaa !9
  %33 = zext i8 %32 to i64
  %34 = getelementptr inbounds [2 x i32], ptr %31, i64 0, i64 %33
  %35 = load i32, ptr %34, align 4, !tbaa !5
  store i32 %35, ptr %6, align 4, !tbaa !5
  %36 = load i32, ptr %6, align 4, !tbaa !5
  %37 = call i32 @sd_read_block(ptr noundef @sd, i32 noundef %36, ptr noundef @buffer)
  %38 = trunc i32 %37 to i8
  store i8 %38, ptr %8, align 1, !tbaa !9
  %39 = load i8, ptr %8, align 1, !tbaa !9
  %40 = zext i8 %39 to i32
  %41 = icmp eq i32 %40, 512
  br i1 %41, label %42, label %68

42:                                               ; preds = %26
  store i8 0, ptr %7, align 1, !tbaa !9
  br label %43

43:                                               ; preds = %57, %42
  %44 = load i8, ptr %7, align 1, !tbaa !9
  %45 = zext i8 %44 to i32
  %46 = load i8, ptr %8, align 1, !tbaa !9
  %47 = zext i8 %46 to i32
  %48 = icmp slt i32 %45, %47
  br i1 %48, label %49, label %60

49:                                               ; preds = %43
  %50 = load i8, ptr %7, align 1, !tbaa !9
  %51 = zext i8 %50 to i64
  %52 = getelementptr inbounds [512 x i8], ptr @buffer, i64 0, i64 %51
  %53 = load i8, ptr %52, align 1, !tbaa !9
  %54 = load i8, ptr %7, align 1, !tbaa !9
  %55 = zext i8 %54 to i64
  %56 = getelementptr inbounds [512 x i8], ptr @temp, i64 0, i64 %55
  store i8 %53, ptr %56, align 1, !tbaa !9
  br label %57

57:                                               ; preds = %49
  %58 = load i8, ptr %7, align 1, !tbaa !9
  %59 = add i8 %58, 1
  store i8 %59, ptr %7, align 1, !tbaa !9
  br label %43, !llvm.loop !12

60:                                               ; preds = %43
  store i8 17, ptr getelementptr inbounds (%struct._sat_message, ptr @temp, i32 0, i32 1), align 1, !tbaa !14
  %61 = load i32, ptr %4, align 4, !tbaa !5
  %62 = load i8, ptr %5, align 1, !tbaa !9
  %63 = call zeroext i8 @delete_message(i32 noundef %61, i8 noundef zeroext %62)
  %64 = icmp ne i8 %63, 0
  br i1 %64, label %65, label %66

65:                                               ; preds = %60
  br label %67

66:                                               ; preds = %60
  store i8 -16, ptr %3, align 1
  store i32 1, ptr %9, align 4
  br label %70

67:                                               ; preds = %65
  store ptr @temp, ptr @ptr, align 8, !tbaa !10
  store i8 0, ptr %3, align 1
  store i32 1, ptr %9, align 4
  br label %70

68:                                               ; preds = %26
  store i8 15, ptr %3, align 1
  store i32 1, ptr %9, align 4
  br label %70

69:                                               ; preds = %2
  store i8 -1, ptr %3, align 1
  store i32 1, ptr %9, align 4
  br label %70

70:                                               ; preds = %69, %68, %67, %66
  call void @llvm.lifetime.end.p0(i64 1, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  %71 = load i8, ptr %3, align 1
  ret i8 %71
}

; 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
declare dso_local zeroext i8 @delete_message(i32 noundef, i8 noundef zeroext) #1

declare i32 @sd_read_block(ptr noundef, i32 noundef, ptr noundef) #2

attributes #0 = { nocallback nofree nosync 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"any pointer", !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!15, !7, i64 512}
!15 = !{!"_sat_message", !7, i64 0, !7, i64 512}


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

@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: nounwind uwtable
define dso_local zeroext range(i8 -1, 16) i8 @read_message(i32 noundef %0, i8 noundef zeroext %1) local_unnamed_addr #0 {
  %3 = zext i32 %0 to i64
  %4 = getelementptr inbounds [0 x ptr], ptr @mat, i64 0, i64 %3
  %5 = load ptr, ptr %4, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %5, i64 40
  %7 = zext i8 %1 to i32
  %8 = lshr i32 %7, 5
  %9 = zext nneg i32 %8 to i64
  %10 = getelementptr inbounds [8 x i32], ptr %6, i64 0, i64 %9
  %11 = load i32, ptr %10, align 4, !tbaa !9
  %12 = shl nuw i32 1, %7
  %13 = and i32 %11, %12
  %.not = icmp eq i32 %13, 0
  br i1 %.not, label %20, label %14

14:                                               ; preds = %2
  %15 = getelementptr inbounds i8, ptr %5, i64 72
  %16 = zext i8 %1 to i64
  %17 = getelementptr inbounds [2 x i32], ptr %15, i64 0, i64 %16
  %18 = load i32, ptr %17, align 4, !tbaa !9
  %19 = tail call i32 @sd_read_block(ptr noundef nonnull @sd, i32 noundef %18, ptr noundef nonnull @buffer) #2
  br label %20

20:                                               ; preds = %2, %14
  %.0 = phi i8 [ 15, %14 ], [ -1, %2 ]
  ret i8 %.0
}

declare i32 @sd_read_block(ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #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 = { "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 = { 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}


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