; LLVM IR from zeroth-robotics/zeroth-bot :: kos-zbot/firmware/feetech (feetech)

; 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.ServoCommand.0 = type { i8, i8, i8 }

@shared_mem = external hidden global ptr, align 8

; 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: nounwind uwtable
declare hidden i32 @perform_mailbox_operation(i32 noundef, i64 noundef) #2

; 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 i32 @servo_read(i8 noundef zeroext %0, i8 noundef zeroext %1, ptr noundef %2, i8 noundef zeroext %3) #2 {
  %5 = alloca i32, align 4
  %6 = alloca i8, align 1
  %7 = alloca i8, align 1
  %8 = alloca ptr, align 8
  %9 = alloca i8, align 1
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  store i8 %0, ptr %6, align 1, !tbaa !5
  store i8 %1, ptr %7, align 1, !tbaa !5
  store ptr %2, ptr %8, align 8, !tbaa !8
  store i8 %3, ptr %9, align 1, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %13 = load ptr, ptr @shared_mem, align 8, !tbaa !8
  store ptr %13, ptr %10, align 8, !tbaa !8
  %14 = load i8, ptr %6, align 1, !tbaa !5
  %15 = load ptr, ptr %10, align 8, !tbaa !8
  %16 = getelementptr inbounds %struct.ServoCommand.0, ptr %15, i32 0, i32 0
  store i8 %14, ptr %16, align 1, !tbaa !10
  %17 = load i8, ptr %7, align 1, !tbaa !5
  %18 = load ptr, ptr %10, align 8, !tbaa !8
  %19 = getelementptr inbounds %struct.ServoCommand.0, ptr %18, i32 0, i32 1
  store i8 %17, ptr %19, align 1, !tbaa !12
  %20 = load i8, ptr %9, align 1, !tbaa !5
  %21 = load ptr, ptr %10, align 8, !tbaa !8
  %22 = getelementptr inbounds %struct.ServoCommand.0, ptr %21, i32 0, i32 2
  store i8 %20, ptr %22, align 1, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  %23 = call i32 @perform_mailbox_operation(i32 noundef 33, i64 noundef 3)
  store i32 %23, ptr %11, align 4, !tbaa !14
  %24 = load i32, ptr %11, align 4, !tbaa !14
  %25 = icmp slt i32 %24, 0
  br i1 %25, label %26, label %28

26:                                               ; preds = %4
  %27 = load i32, ptr %11, align 4, !tbaa !14
  store i32 %27, ptr %5, align 4
  store i32 1, ptr %12, align 4
  br label %38

28:                                               ; preds = %4
  %29 = load ptr, ptr %8, align 8, !tbaa !8
  %30 = load ptr, ptr @shared_mem, align 8, !tbaa !8
  %31 = getelementptr inbounds i8, ptr %30, i64 5
  %32 = load i8, ptr %9, align 1, !tbaa !5
  %33 = zext i8 %32 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %29, ptr align 1 %31, i64 %33, i1 false)
  %34 = load ptr, ptr @shared_mem, align 8, !tbaa !8
  %35 = getelementptr inbounds i8, ptr %34, i64 4
  %36 = load i8, ptr %35, align 1, !tbaa !5
  %37 = zext i8 %36 to i32
  store i32 %37, ptr %5, align 4
  store i32 1, ptr %12, align 4
  br label %38

38:                                               ; preds = %28, %26
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  %39 = load i32, ptr %5, align 4
  ret i32 %39
}

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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"any pointer", !6, i64 0}
!10 = !{!11, !6, i64 0}
!11 = !{!"", !6, i64 0, !6, i64 1, !6, i64 2}
!12 = !{!11, !6, i64 1}
!13 = !{!11, !6, i64 2}
!14 = !{!15, !15, i64 0}
!15 = !{!"int", !6, i64 0}


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

@shared_mem = external hidden local_unnamed_addr global ptr, align 8

; 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
declare hidden i32 @perform_mailbox_operation(i32 noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local range(i32 -2147483648, 256) i32 @servo_read(i8 noundef zeroext %0, i8 noundef zeroext %1, ptr nocapture noundef writeonly %2, i8 noundef zeroext %3) local_unnamed_addr #1 {
  %5 = load ptr, ptr @shared_mem, align 8, !tbaa !5
  store i8 %0, ptr %5, align 1, !tbaa !9
  %6 = getelementptr inbounds i8, ptr %5, i64 1
  store i8 %1, ptr %6, align 1, !tbaa !11
  %7 = getelementptr inbounds i8, ptr %5, i64 2
  store i8 %3, ptr %7, align 1, !tbaa !12
  %8 = tail call i32 @perform_mailbox_operation(i32 noundef 33, i64 noundef 3)
  %9 = icmp slt i32 %8, 0
  br i1 %9, label %18, label %10

10:                                               ; preds = %4
  %11 = load ptr, ptr @shared_mem, align 8, !tbaa !5
  %12 = getelementptr inbounds i8, ptr %11, i64 5
  %13 = zext i8 %3 to i64
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %2, ptr nonnull align 1 %12, i64 %13, i1 false)
  %14 = load ptr, ptr @shared_mem, align 8, !tbaa !5
  %15 = getelementptr inbounds i8, ptr %14, i64 4
  %16 = load i8, ptr %15, align 1, !tbaa !13
  %17 = zext i8 %16 to i32
  br label %18

18:                                               ; preds = %4, %10
  %.0 = phi i32 [ %17, %10 ], [ %8, %4 ]
  ret i32 %.0
}

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

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


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