From a509cabf15ac7d231c902aa850499e144dc57eff Mon Sep 17 00:00:00 2001
From: "Kirill A. Korinsky" <kirill@korins.ky>
Date: Wed, 17 Aug 2022 16:07:55 +0200
Subject: [PATCH] Fix build issues pre-Lion due to missing a memmem definition

---
 compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp | 34 ++++++++++++++++++++++
 1 file changed, 34 insertions(+)

diff --git a/compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp b/compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp
index fc57b724db10..f538696facff 100644
--- a/compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp
+++ b/compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp
@@ -27,6 +27,40 @@
 #include <thread>
 #include <unistd.h>
 
+#ifdef __APPLE__
+#include <Availability.h>
+#if __MAC_OS_X_VERSION_MIN_REQUIRED < 1070
+void *
+memmem(const void *l, size_t l_len, const void *s, size_t s_len)
+{
+  register char *cur, *last;
+  const char *cl = (const char *)l;
+  const char *cs = (const char *)s;
+
+  /* we need something to compare */
+  if (l_len == 0 || s_len == 0)
+    return NULL;
+
+  /* "s" must be smaller or equal to "l" */
+  if (l_len < s_len)
+    return NULL;
+
+  /* special case where s_len == 1 */
+  if (s_len == 1)
+    return memchr(l, (int)*cs, l_len);
+
+  /* the last position where its possible to find "s" in "l" */
+  last = (char *)cl + l_len - s_len;
+
+  for (cur = (char *)cl; cur <= last; cur++)
+    if (cur[0] == cs[0] && memcmp(cur, cs, s_len) == 0)
+      return cur;
+
+  return NULL;
+}
+#endif
+#endif
+
 namespace fuzzer {
 
 static void AlarmHandler(int, siginfo_t *, void *) {
-- 
2.37.2

