#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int /*<<< orphan*/ haystack;

/* Variables and functions */
void *memmem(char *, size_t, char *, int);
void *memrmem(char *, size_t, char *, int);
int /*<<< orphan*/ printf(char *, int, ...);
int main(int argc, char **argv) {
  char haystack[] = "Woo Hello World, Woo this is Haystack";
  size_t haystack_len;
  void *p;
  haystack_len = sizeof(haystack);
  p = memmem(haystack, haystack_len, "World", 5);
  printf("%2lu %s\n", p - (void *)haystack, (char *)p);
  p = memmem(haystack, haystack_len, "Woo", 3);
  printf("%2lu %s\n", p - (void *)haystack, (char *)p);
  p = memrmem(haystack, haystack_len, "Woo", 3);
  printf("%2lu %s\n", p - (void *)haystack, (char *)p);
  p = memrmem(haystack, haystack_len, "thesht", 6);
  printf("p is NULL %d\n", p == NULL);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x40(%rbp),%rdi
lea    0xe9f(%rip),%rsi
mov    $0x26,%edx
call   1060 <memcpy@plt>
movq   $0x26,-0x48(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rsi
lea    0xea4(%rip),%rdx
mov    $0x5,%ecx
call   1050 <memmem@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rax
lea    -0x40(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%esi
mov    -0x50(%rbp),%rdx
lea    0xe84(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rsi
lea    0xe77(%rip),%rdx
mov    $0x3,%ecx
call   1050 <memmem@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rax
lea    -0x40(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%esi
mov    -0x50(%rbp),%rdx
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rsi
lea    0xe3b(%rip),%rdx
mov    $0x3,%ecx
call   1030 <memrmem@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rax
lea    -0x40(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%esi
mov    -0x50(%rbp),%rdx
lea    0xe0c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rsi
lea    0xe03(%rip),%rdx
mov    $0x6,%ecx
call   1030 <memrmem@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%esi
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x30,%rsp
movaps 0xed0(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xeb4(%rip),%xmm0
movaps %xmm0,(%rsp)
movabs $0x6b636174737961,%rax
mov    %rax,0x1e(%rsp)
lea    0xec0(%rip),%rdx
mov    %rsp,%rbx
mov    $0x26,%esi
mov    %rbx,%rdi
mov    $0x5,%ecx
call   1050 <memmem@plt>
mov    %eax,%esi
sub    %ebx,%esi
lea    0xea6(%rip),%r14
mov    %r14,%rdi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe9b(%rip),%r15
mov    $0x26,%esi
mov    %rbx,%rdi
mov    %r15,%rdx
mov    $0x3,%ecx
call   1050 <memmem@plt>
mov    %eax,%esi
sub    %ebx,%esi
mov    %r14,%rdi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x26,%esi
mov    %rbx,%rdi
mov    %r15,%rdx
mov    $0x3,%ecx
call   1030 <memrmem@plt>
mov    %eax,%esi
sub    %ebx,%esi
mov    %r14,%rdi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe4c(%rip),%rdx
mov    $0x26,%esi
mov    %rbx,%rdi
mov    $0x6,%ecx
call   1030 <memrmem@plt>
xor    %esi,%esi
test   %rax,%rax
sete   %sil
lea    0xe31(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@__const.main.haystack = [38 x i8] c"Woo Hello World, Woo this is Haystack\00"
@.str = [6 x i8] c"World\00"
@.str.1 = [9 x i8] c"%2lu %s\0A\00"
@.str.2 = [4 x i8] c"Woo\00"
@.str.3 = [7 x i8] c"thesht\00"
@.str.4 = [14 x i8] c"p is NULL %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [38 x i8]
%7 = alloca i64
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = bitcast [38 x i8]* %6 to i8*
store i64 38, i64* %7
%10 = getelementptr inbounds [38 x i8], [38 x i8]* %6, i64 0, i64 0
%11 = load i64, i64* %7
%12 = call i8* @memmem(i8* %10, i64 %11, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 5)
store i8* %12, i8** %8
%13 = load i8*, i8** %8
%14 = getelementptr inbounds [38 x i8], [38 x i8]* %6, i64 0, i64 0
%15 = ptrtoint i8* %13 to i64
%16 = ptrtoint i8* %14 to i64
%17 = sub i64 %15, %16
%18 = trunc i64 %17 to i32
%19 = load i8*, i8** %8
%20 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %18, i8* %19)
%21 = getelementptr inbounds [38 x i8], [38 x i8]* %6, i64 0, i64 0
%22 = load i64, i64* %7
%23 = call i8* @memmem(i8* %21, i64 %22, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
store i8* %23, i8** %8
%24 = load i8*, i8** %8
%25 = getelementptr inbounds [38 x i8], [38 x i8]* %6, i64 0, i64 0
%26 = ptrtoint i8* %24 to i64
%27 = ptrtoint i8* %25 to i64
%28 = sub i64 %26, %27
%29 = trunc i64 %28 to i32
%30 = load i8*, i8** %8
%31 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %29, i8* %30)
%32 = getelementptr inbounds [38 x i8], [38 x i8]* %6, i64 0, i64 0
%33 = load i64, i64* %7
%34 = call i8* @memrmem(i8* %32, i64 %33, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
store i8* %34, i8** %8
%35 = load i8*, i8** %8
%36 = getelementptr inbounds [38 x i8], [38 x i8]* %6, i64 0, i64 0
%37 = ptrtoint i8* %35 to i64
%38 = ptrtoint i8* %36 to i64
%39 = sub i64 %37, %38
%40 = trunc i64 %39 to i32
%41 = load i8*, i8** %8
%42 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %40, i8* %41)
%43 = getelementptr inbounds [38 x i8], [38 x i8]* %6, i64 0, i64 0
%44 = load i64, i64* %7
%45 = call i8* @memrmem(i8* %43, i64 %44, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 6)
store i8* %45, i8** %8
%46 = load i8*, i8** %8
%47 = icmp eq i8* %46, null
%48 = zext i1 %47 to i32
%49 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %48)
ret i32 0
}

LLVM IR (-O2):
@__const.main.haystack = [38 x i8] c"Woo Hello World, Woo this is Haystack\00"
@.str = [6 x i8] c"World\00"
@.str.1 = [9 x i8] c"%2lu %s\0A\00"
@.str.2 = [4 x i8] c"Woo\00"
@.str.3 = [7 x i8] c"thesht\00"
@.str.4 = [14 x i8] c"p is NULL %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [38 x i8]
%4 = getelementptr inbounds [38 x i8], [38 x i8]* %3, i64 0, i64 0
%5 = call i8* @memmem(i8* %4, i64 38, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 5)
%6 = ptrtoint i8* %5 to i64
%7 = ptrtoint [38 x i8]* %3 to i64
%8 = sub i64 %6, %7
%9 = trunc i64 %8 to i32
%10 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %9, i8* %5)
%11 = call i8* @memmem(i8* %4, i64 38, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
%12 = ptrtoint i8* %11 to i64
%13 = sub i64 %12, %7
%14 = trunc i64 %13 to i32
%15 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %14, i8* %11)
%16 = call i8* @memrmem(i8* %4, i64 38, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
%17 = ptrtoint i8* %16 to i64
%18 = sub i64 %17, %7
%19 = trunc i64 %18 to i32
%20 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %19, i8* %16)
%21 = call i8* @memrmem(i8* %4, i64 38, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 6)
%22 = icmp eq i8* %21, null
%23 = zext i1 %22 to i32
%24 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %23)
ret i32 0
}