#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 */

/* Variables and functions */
int /*<<< orphan*/ printf(char *, ...);
const char *my_strstr(const char *haystack, const char *needle) {
  printf("Input %s needle: %s\n", haystack, needle);
  if (haystack == NULL || needle == NULL) {
    return (NULL);
  }
  while (*haystack != '\0') {
    printf("starting at %c\n", *haystack);
    if (*haystack == *needle) {
      const char *h = haystack;
      const char *n = needle;
      printf("match starting at %c\n", *haystack);
      while (*n != '\0' && *n == *h) {
        n++;
        h++;
      }
      if (*n == '\0') {
        return ((const char *)haystack);
      }
    }
    haystack++;
  }
  return (NULL);
}


x86 assembly (clang, -O0):
<my_strstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xed1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpq   $0x0,-0x10(%rbp)
je     114c <my_strstr+0x3c>
cmpq   $0x0,-0x18(%rbp)
jne    1159 <my_strstr+0x49>
movq   $0x0,-0x8(%rbp)
jmp    1251 <my_strstr+0x141>
jmp    115e <my_strstr+0x4e>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1249 <my_strstr+0x139>
mov    -0x10(%rbp),%rax
movsbl (%rax),%esi
lea    0xe9f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
mov    -0x18(%rbp),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    1238 <my_strstr+0x128>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%esi
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x28(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x29(%rbp)
je     11e9 <my_strstr+0xd9>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
mov    -0x20(%rbp),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
sete   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    11f9 <my_strstr+0xe9>
jmp    1216 <my_strstr+0x106>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11be <my_strstr+0xae>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1233 <my_strstr+0x123>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1251 <my_strstr+0x141>
jmp    1238 <my_strstr+0x128>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    115e <my_strstr+0x4e>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_strstr>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0xeda(%rip),%rdi
xor    %r14d,%r14d
mov    %rbx,%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
test   %rbx,%rbx
je     11bb <my_strstr+0xab>
test   %r15,%r15
je     11bb <my_strstr+0xab>
mov    (%rbx),%al
test   %al,%al
je     11b8 <my_strstr+0xa8>
lea    0xeca(%rip),%r12
xor    %r14d,%r14d
lea    0xeba(%rip),%r13
jmp    116b <my_strstr+0x5b>
nopl   (%rax)
mov    0x1(%rbx),%al
add    $0x1,%rbx
test   %al,%al
je     11bb <my_strstr+0xab>
movsbl %al,%esi
mov    %r12,%rdi
xor    %eax,%eax
call   1030 <printf@plt>
movsbl (%rbx),%esi
cmp    (%r15),%sil
jne    1160 <my_strstr+0x50>
mov    %r13,%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    (%r15),%al
test   %al,%al
je     11b3 <my_strstr+0xa3>
xor    %ecx,%ecx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmp    (%rbx,%rcx,1),%al
jne    1160 <my_strstr+0x50>
movzbl 0x1(%r15,%rcx,1),%eax
add    $0x1,%rcx
test   %al,%al
jne    11a0 <my_strstr+0x90>
mov    %rbx,%r14
jmp    11bb <my_strstr+0xab>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [21 x i8] c"Input %s needle: %s\0A\00"
@.str.1 = [16 x i8] c"starting at %c\0A\00"
@.str.2 = [22 x i8] c"match starting at %c\0A\00"
define i8* @my_strstr(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %4
%9 = load i8*, i8** %5
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %8, i8* %9)
%11 = load i8*, i8** %4
%12 = icmp eq i8* %11, null
br i1 %12, label %16, label %13
13: ; preds = %2
%14 = load i8*, i8** %5
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %17
16: ; preds = %13, %2
store i8* null, i8** %3
br label %74
17: ; preds = %13
br label %18
18: ; preds = %70, %17
%19 = load i8*, i8** %4
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %73
23: ; preds = %18
%24 = load i8*, i8** %4
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %26)
%28 = load i8*, i8** %4
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = load i8*, i8** %5
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = icmp eq i32 %30, %33
br i1 %34, label %35, label %70
35: ; preds = %23
%36 = load i8*, i8** %4
store i8* %36, i8** %6
%37 = load i8*, i8** %5
store i8* %37, i8** %7
%38 = load i8*, i8** %4
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %40)
br label %42
42: ; preds = %57, %35
%43 = load i8*, i8** %7
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %55
47: ; preds = %42
%48 = load i8*, i8** %7
%49 = load i8, i8* %48
%50 = sext i8 %49 to i32
%51 = load i8*, i8** %6
%52 = load i8, i8* %51
%53 = sext i8 %52 to i32
%54 = icmp eq i32 %50, %53
br label %55
55: ; preds = %47, %42
%56 = phi i1 [ false, %42 ], [ %54, %47 ]
br i1 %56, label %57, label %62
57: ; preds = %55
%58 = load i8*, i8** %7
%59 = getelementptr inbounds i8, i8* %58, i32 1
store i8* %59, i8** %7
%60 = load i8*, i8** %6
%61 = getelementptr inbounds i8, i8* %60, i32 1
store i8* %61, i8** %6
br label %42
62: ; preds = %55
%63 = load i8*, i8** %7
%64 = load i8, i8* %63
%65 = sext i8 %64 to i32
%66 = icmp eq i32 %65, 0
br i1 %66, label %67, label %69
67: ; preds = %62
%68 = load i8*, i8** %4
store i8* %68, i8** %3
br label %74
69: ; preds = %62
br label %70
70: ; preds = %69, %23
%71 = load i8*, i8** %4
%72 = getelementptr inbounds i8, i8* %71, i32 1
store i8* %72, i8** %4
br label %18
73: ; preds = %18
store i8* null, i8** %3
br label %74
74: ; preds = %73, %67, %16
%75 = load i8*, i8** %3
ret i8* %75
}

LLVM IR (-O2):
@.str = [21 x i8] c"Input %s needle: %s\0A\00"
@.str.1 = [16 x i8] c"starting at %c\0A\00"
@.str.2 = [22 x i8] c"match starting at %c\0A\00"
define i8* @my_strstr(i8* %0, i8* %1) {
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%4 = icmp eq i8* %0, null
%5 = icmp eq i8* %1, null
%6 = or i1 %4, %5
br i1 %6, label %38, label %7
7: ; preds = %2
%8 = load i8, i8* %0
%9 = icmp eq i8 %8, 0
br i1 %9, label %38, label %10
10: ; preds = %7, %34
%11 = phi i8 [ %36, %34 ], [ %8, %7 ]
%12 = phi i8* [ %35, %34 ], [ %0, %7 ]
%13 = sext i8 %11 to i32
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = load i8, i8* %12
%16 = load i8, i8* %1
%17 = icmp eq i8 %15, %16
br i1 %17, label %18, label %34
18: ; preds = %10
%19 = sext i8 %15 to i32
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %19)
%21 = load i8, i8* %1
%22 = icmp eq i8 %21, 0
br i1 %22, label %38, label %23
23: ; preds = %18, %29
%24 = phi i8 [ %32, %29 ], [ %21, %18 ]
%25 = phi i8* [ %30, %29 ], [ %1, %18 ]
%26 = phi i8* [ %31, %29 ], [ %12, %18 ]
%27 = load i8, i8* %26
%28 = icmp eq i8 %24, %27
br i1 %28, label %29, label %34
29: ; preds = %23
%30 = getelementptr inbounds i8, i8* %25, i64 1
%31 = getelementptr inbounds i8, i8* %26, i64 1
%32 = load i8, i8* %30
%33 = icmp eq i8 %32, 0
br i1 %33, label %38, label %23
34: ; preds = %23, %10
%35 = getelementptr inbounds i8, i8* %12, i64 1
%36 = load i8, i8* %35
%37 = icmp eq i8 %36, 0
br i1 %37, label %38, label %10
38: ; preds = %34, %18, %29, %7, %2
%39 = phi i8* [ null, %2 ], [ null, %7 ], [ %12, %29 ], [ %12, %18 ], [ null, %34 ]
ret i8* %39
}