; 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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

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

; Function Attrs: nounwind uwtable
define dso_local zeroext i8 @read_message_next(i32 noundef %0) #2 {
  %2 = alloca i8, align 1
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i8, align 1
  %6 = alloca i8, align 1
  %7 = alloca i8, align 1
  %8 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 1, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 1, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 1, ptr %7) #3
  %9 = load i32, ptr %3, align 4, !tbaa !5
  %10 = zext i32 %9 to i64
  %11 = getelementptr inbounds [0 x ptr], ptr @mat, i64 0, i64 %10
  %12 = load ptr, ptr %11, align 8, !tbaa !9
  %13 = getelementptr inbounds %struct._sat_MAT, ptr %12, i32 0, i32 5
  %14 = load i8, ptr %13, align 4, !tbaa !11
  store i8 %14, ptr %6, align 1, !tbaa !13
  %15 = load i32, ptr %3, 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 3
  %20 = load i8, ptr %6, align 1, !tbaa !13
  %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 %6, align 1, !tbaa !13
  %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 %77

34:                                               ; preds = %1
  %35 = load i32, ptr %3, 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 %6, align 1, !tbaa !13
  %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 %4, align 4, !tbaa !5
  %44 = load i32, ptr %3, align 4, !tbaa !5
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [0 x ptr], ptr @mat, i64 0, i64 %45
  %47 = load ptr, ptr %46, align 8, !tbaa !9
  %48 = getelementptr inbounds %struct._sat_MAT, ptr %47, i32 0, i32 5
  %49 = load i8, ptr %48, align 4, !tbaa !11
  %50 = add i8 %49, 1
  store i8 %50, ptr %48, align 4, !tbaa !11
  %51 = load i32, ptr %4, align 4, !tbaa !5
  %52 = call i32 @sd_read_block(ptr noundef @sd, i32 noundef %51, ptr noundef @buffer)
  %53 = trunc i32 %52 to i8
  store i8 %53, ptr %7, align 1, !tbaa !13
  %54 = load i8, ptr %7, align 1, !tbaa !13
  %55 = zext i8 %54 to i32
  %56 = icmp eq i32 %55, 512
  br i1 %56, label %57, label %76

57:                                               ; preds = %34
  store i8 0, ptr %5, align 1, !tbaa !13
  br label %58

58:                                               ; preds = %72, %57
  %59 = load i8, ptr %5, align 1, !tbaa !13
  %60 = zext i8 %59 to i32
  %61 = load i8, ptr %7, align 1, !tbaa !13
  %62 = zext i8 %61 to i32
  %63 = icmp slt i32 %60, %62
  br i1 %63, label %64, label %75

64:                                               ; preds = %58
  %65 = load i8, ptr %5, align 1, !tbaa !13
  %66 = zext i8 %65 to i64
  %67 = getelementptr inbounds [512 x i8], ptr @buffer, i64 0, i64 %66
  %68 = load i8, ptr %67, align 1, !tbaa !13
  %69 = load i8, ptr %5, align 1, !tbaa !13
  %70 = zext i8 %69 to i64
  %71 = getelementptr inbounds [512 x i8], ptr @temp, i64 0, i64 %70
  store i8 %68, ptr %71, align 1, !tbaa !13
  br label %72

72:                                               ; preds = %64
  %73 = load i8, ptr %5, align 1, !tbaa !13
  %74 = add i8 %73, 1
  store i8 %74, ptr %5, align 1, !tbaa !13
  br label %58, !llvm.loop !14

75:                                               ; preds = %58
  store i8 17, ptr getelementptr inbounds (%struct._sat_message, ptr @temp, i32 0, i32 1), align 1, !tbaa !16
  store ptr @temp, ptr @ptr, align 8, !tbaa !9
  store i8 0, ptr %2, align 1
  store i32 1, ptr %8, align 4
  br label %78

76:                                               ; preds = %34
  store i8 15, ptr %2, align 1
  store i32 1, ptr %8, align 4
  br label %78

77:                                               ; preds = %1
  store i8 -1, ptr %2, align 1
  store i32 1, ptr %8, align 4
  br label %78

78:                                               ; preds = %77, %76, %75
  call void @llvm.lifetime.end.p0(i64 1, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  %79 = load i8, ptr %2, align 1
  ret i8 %79
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 = !{!12, !7, i64 80}
!12 = !{!"_sat_MAT", !6, i64 0, !7, i64 4, !7, i64 8, !7, i64 40, !7, i64 72, !7, i64 80}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!17, !7, i64 512}
!17 = !{!"_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

declare i32 @sd_read_block(ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #0

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

13:                                               ; preds = %1
  %14 = getelementptr inbounds i8, ptr %4, i64 72
  %15 = load i32, ptr %14, align 4, !tbaa !12
  %16 = sext i32 %0 to i64
  %17 = getelementptr inbounds [0 x ptr], ptr @mat, i64 0, i64 %16
  %18 = load ptr, ptr %17, align 8, !tbaa !5
  %19 = getelementptr inbounds i8, ptr %18, i64 80
  %20 = load i8, ptr %19, align 4, !tbaa !9
  %21 = add i8 %20, 1
  store i8 %21, ptr %19, align 4, !tbaa !9
  %22 = tail call i32 @sd_read_block(ptr noundef nonnull @sd, i32 noundef %15, ptr noundef nonnull @buffer) #2
  br label %23

23:                                               ; preds = %1, %13
  %.0 = phi i8 [ 15, %13 ], [ -1, %1 ]
  ret i8 %.0
}

attributes #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 = { 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 = { 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, !7, i64 80}
!10 = !{!"_sat_MAT", !11, i64 0, !7, i64 4, !7, i64 8, !7, i64 40, !7, i64 72, !7, i64 80}
!11 = !{!"int", !7, i64 0}
!12 = !{!11, !11, i64 0}


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