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

@add_temp = external dso_local global i32, align 4
@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: 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 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
  %9 = load i32, ptr %4, 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 !10
  %13 = getelementptr inbounds %struct._sat_MAT, ptr %12, i32 0, i32 3
  %14 = load i8, ptr %5, align 1, !tbaa !9
  %15 = zext i8 %14 to i32
  %16 = ashr i32 %15, 5
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [8 x i32], ptr %13, i64 0, i64 %17
  %19 = load i32, ptr %18, align 4, !tbaa !5
  %20 = icmp ne i32 %19, 0
  %21 = xor i1 %20, true
  %22 = zext i1 %21 to i32
  %23 = load i8, ptr %5, align 1, !tbaa !9
  %24 = zext i8 %23 to i32
  %25 = shl i32 1, %24
  %26 = and i32 %22, %25
  %27 = icmp ne i32 %26, 0
  br i1 %27, label %28, label %50

28:                                               ; preds = %2
  %29 = load i32, ptr %4, align 4, !tbaa !5
  %30 = zext i32 %29 to i64
  %31 = getelementptr inbounds [0 x ptr], ptr @mat, i64 0, i64 %30
  %32 = load ptr, ptr %31, align 8, !tbaa !10
  %33 = getelementptr inbounds %struct._sat_MAT, ptr %32, i32 0, i32 4
  %34 = load i8, ptr %5, align 1, !tbaa !9
  %35 = zext i8 %34 to i64
  %36 = getelementptr inbounds [2 x i32], ptr %33, i64 0, i64 %35
  %37 = load i32, ptr %36, align 4, !tbaa !5
  store i32 %37, ptr %6, align 4, !tbaa !5
  %38 = load i32, ptr %6, align 4, !tbaa !5
  store i32 %38, ptr @add_temp, align 4, !tbaa !5
  %39 = load i32, ptr %6, align 4, !tbaa !5
  %40 = call i32 @sd_read_block(ptr noundef @sd, i32 noundef %39, ptr noundef @temp)
  %41 = trunc i32 %40 to i8
  store i8 %41, ptr %7, align 1, !tbaa !9
  store i8 17, ptr getelementptr inbounds (%struct._sat_message, ptr @temp, i32 0, i32 1), align 1, !tbaa !12
  %42 = load i32, ptr %4, align 4, !tbaa !5
  %43 = load i8, ptr %5, align 1, !tbaa !9
  %44 = call zeroext i8 @delete_message(i32 noundef %42, i8 noundef zeroext %43)
  %45 = icmp ne i8 %44, 0
  br i1 %45, label %46, label %47

46:                                               ; preds = %28
  br label %48

47:                                               ; preds = %28
  store i8 -16, ptr %3, align 1
  store i32 1, ptr %8, align 4
  br label %51

48:                                               ; preds = %46
  store ptr @temp, ptr @ptr, align 8, !tbaa !10
  %49 = load i8, ptr %7, align 1, !tbaa !9
  store i8 %49, ptr %3, align 1
  store i32 1, ptr %8, align 4
  br label %51

50:                                               ; preds = %2
  store i8 15, ptr %3, align 1
  store i32 1, ptr %8, align 4
  br label %51

51:                                               ; preds = %50, %48, %47
  call void @llvm.lifetime.end.p0(i64 1, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  %52 = load i8, ptr %3, align 1
  ret i8 %52
}

; 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 = !{!13, !7, i64 512}
!13 = !{!"_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 }
%struct._sat_message = type { [512 x i8], i8 }

@add_temp = external dso_local local_unnamed_addr global i32, align 4
@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: nounwind uwtable
define dso_local zeroext 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 = lshr i8 %1, 5
  %8 = zext nneg i8 %7 to i64
  %9 = getelementptr inbounds [8 x i32], ptr %6, i64 0, i64 %8
  %10 = load i32, ptr %9, align 4, !tbaa !9
  %.not = icmp eq i32 %10, 0
  %11 = icmp eq i8 %1, 0
  %narrow = and i1 %11, %.not
  br i1 %narrow, label %12, label %19

12:                                               ; preds = %2
  %13 = getelementptr inbounds i8, ptr %5, i64 72
  %14 = load i32, ptr %13, align 4, !tbaa !9
  store i32 %14, ptr @add_temp, align 4, !tbaa !9
  %15 = tail call i32 @sd_read_block(ptr noundef nonnull @sd, i32 noundef %14, ptr noundef nonnull @temp) #2
  store i8 17, ptr getelementptr inbounds (i8, ptr @temp, i64 512), align 1, !tbaa !11
  %16 = tail call zeroext i8 @delete_message(i32 noundef %0, i8 noundef zeroext 0)
  %.not11 = icmp eq i8 %16, 0
  br i1 %.not11, label %19, label %17

17:                                               ; preds = %12
  %18 = trunc i32 %15 to i8
  store ptr @temp, ptr @ptr, align 8, !tbaa !5
  br label %19

19:                                               ; preds = %2, %12, %17
  %.0 = phi i8 [ %18, %17 ], [ -16, %12 ], [ 15, %2 ]
  ret i8 %.0
}

; Function Attrs: nounwind uwtable
declare dso_local zeroext i8 @delete_message(i32 noundef, i8 noundef zeroext) local_unnamed_addr #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}
!11 = !{!12, !7, i64 512}
!12 = !{!"_sat_message", !7, i64 0, !7, i64 512}


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