#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*/ ssize_t;
typedef int /*<<< orphan*/ haystack;

/* Variables and functions */
int /*<<< orphan*/ printf(char *, int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ strpos(char *, char *);
int /*<<< orphan*/ strrpos(char *, char *);
char *strrstr(char *, char *);
char *strstr(char *, char *);
void __test_strstr(void) {
  char haystack[] = "Woo Hello World, Woo this is Haystack";
  size_t haystack_len;
  ssize_t x;
  char *p;
  haystack_len = sizeof(haystack);
  p = strstr(haystack, "World");
  x = strpos(haystack, "World");
  printf("%2lu %2ld %s\n", p - haystack, x, p);
  p = strstr(haystack, "Woo");
  x = strpos(haystack, "Woo");
  printf("%2lu %2ld %s\n", p - haystack, x, p);
  p = strrstr(haystack, "Woo");
  x = strrpos(haystack, "Woo");
  printf("%2lu %2ld %s\n", p - haystack, x, p);
  p = strstr(haystack, "thesht");
  x = strpos(haystack, "thesht");
  printf("p is NULL %d: %2ld\n", p == NULL, x);
  p = strrstr(haystack, "thesht");
  x = strrpos(haystack, "thesht");
  printf("p is NULL %d - %2ld\n", p == NULL, x);
}


x86 assembly (clang, -O0):
<__test_strstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
lea    -0x30(%rbp),%rdi
lea    0xe8d(%rip),%rsi
mov    $0x26,%edx
call   1050 <memcpy@plt>
movq   $0x26,-0x38(%rbp)
lea    -0x30(%rbp),%rdi
lea    0xe96(%rip),%rsi
call   1080 <strstr@plt>
mov    %rax,-0x48(%rbp)
lea    -0x30(%rbp),%rdi
lea    0xe82(%rip),%rsi
call   1060 <strpos@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x48(%rbp),%rax
lea    -0x30(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%esi
mov    -0x3c(%rbp),%edx
mov    -0x48(%rbp),%rcx
lea    0xe65(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x30(%rbp),%rdi
lea    0xe61(%rip),%rsi
call   1080 <strstr@plt>
mov    %rax,-0x48(%rbp)
lea    -0x30(%rbp),%rdi
lea    0xe4d(%rip),%rsi
call   1060 <strpos@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x48(%rbp),%rax
lea    -0x30(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%esi
mov    -0x3c(%rbp),%edx
mov    -0x48(%rbp),%rcx
lea    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x30(%rbp),%rdi
lea    0xe18(%rip),%rsi
call   1030 <strrstr@plt>
mov    %rax,-0x48(%rbp)
lea    -0x30(%rbp),%rdi
lea    0xe04(%rip),%rsi
call   1070 <strrpos@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x48(%rbp),%rax
lea    -0x30(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%esi
mov    -0x3c(%rbp),%edx
mov    -0x48(%rbp),%rcx
lea    0xdd3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x30(%rbp),%rdi
lea    0xdd3(%rip),%rsi
call   1080 <strstr@plt>
mov    %rax,-0x48(%rbp)
lea    -0x30(%rbp),%rdi
lea    0xdbf(%rip),%rsi
call   1060 <strpos@plt>
mov    %eax,-0x3c(%rbp)
cmpq   $0x0,-0x48(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%esi
mov    -0x3c(%rbp),%edx
lea    0xda7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x30(%rbp),%rdi
lea    0xd8e(%rip),%rsi
call   1030 <strrstr@plt>
mov    %rax,-0x48(%rbp)
lea    -0x30(%rbp),%rdi
lea    0xd7a(%rip),%rsi
call   1070 <strrpos@plt>
mov    %eax,-0x3c(%rbp)
cmpq   $0x0,-0x48(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%esi
mov    -0x3c(%rbp),%edx
lea    0xd76(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__test_strstr>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
movaps 0xeae(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xe92(%rip),%xmm0
movaps %xmm0,(%rsp)
movabs $0x6b636174737961,%rax
mov    %rax,0x1e(%rsp)
lea    0xe9e(%rip),%r14
mov    %rsp,%r12
mov    %r12,%rdi
mov    %r14,%rsi
call   1070 <strstr@plt>
mov    %rax,%rbx
mov    %r12,%rdi
mov    %r14,%rsi
call   1050 <strpos@plt>
mov    %ebx,%esi
sub    %r12d,%esi
lea    0xe7c(%rip),%r14
mov    %r14,%rdi
mov    %eax,%edx
mov    %rbx,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe74(%rip),%r15
mov    %r12,%rdi
mov    %r15,%rsi
call   1070 <strstr@plt>
mov    %rax,%rbx
mov    %r12,%rdi
mov    %r15,%rsi
call   1050 <strpos@plt>
mov    %ebx,%esi
sub    %r12d,%esi
mov    %r14,%rdi
mov    %eax,%edx
mov    %rbx,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r12,%rdi
mov    %r15,%rsi
call   1030 <strrstr@plt>
mov    %rax,%rbx
mov    %r12,%rdi
mov    %r15,%rsi
call   1060 <strrpos@plt>
mov    %ebx,%esi
sub    %r12d,%esi
mov    %r14,%rdi
mov    %eax,%edx
mov    %rbx,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe17(%rip),%r14
mov    %r12,%rdi
mov    %r14,%rsi
call   1070 <strstr@plt>
mov    %rax,%rbx
mov    %r12,%rdi
mov    %r14,%rsi
call   1050 <strpos@plt>
xor    %esi,%esi
test   %rbx,%rbx
sete   %sil
lea    0xdf5(%rip),%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r12,%rdi
mov    %r14,%rsi
call   1030 <strrstr@plt>
mov    %rax,%rbx
mov    %r12,%rdi
mov    %r14,%rsi
call   1060 <strrpos@plt>
xor    %esi,%esi
test   %rbx,%rbx
sete   %sil
lea    0xdd7(%rip),%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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