#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 */
size_t strlen(char const *);
scalar_t__ strncmp(char *, char const *, size_t);
char *strstr(const char *hay, const char *needle) {
  char *pos;
  size_t hlen, nlen;
  if (hay == NULL || needle == NULL)
    return NULL;
  hlen = strlen(hay);
  nlen = strlen(needle);
  if (nlen < 1)
    return (char *)hay;
  for (pos = (char *)hay; pos < hay + hlen; pos++) {
    if (strncmp(pos, needle, nlen) == 0) {
      return pos;
    }
  }
  return NULL;
}


x86 assembly (clang, -O0):
<strstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1146 <strstr+0x26>
cmpq   $0x0,-0x18(%rbp)
jne    1153 <strstr+0x33>
movq   $0x0,-0x8(%rbp)
jmp    11e8 <strstr+0xc8>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x1,-0x30(%rbp)
jae    1185 <strstr+0x65>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11e8 <strstr+0xc8>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
add    -0x28(%rbp),%rcx
cmp    %rcx,%rax
jae    11e0 <strstr+0xc0>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    11ca <strstr+0xaa>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11e8 <strstr+0xc8>
jmp    11cf <strstr+0xaf>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    118d <strstr+0x6d>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strstr>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
xor    %r15d,%r15d
test   %rdi,%rdi
je     1196 <strstr+0x76>
mov    %rsi,%r14
test   %rsi,%rsi
je     1196 <strstr+0x76>
mov    %rdi,%rbx
mov    %r14,%rdi
call   1040 <strlen@plt>
test   %rax,%rax
je     118e <strstr+0x6e>
mov    %rax,%r12
mov    %rbx,%rdi
call   1040 <strlen@plt>
test   %rax,%rax
jle    1193 <strstr+0x73>
mov    %rax,%r13
add    %rbx,%r13
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
call   1030 <strncmp@plt>
test   %rax,%rax
je     118e <strstr+0x6e>
add    $0x1,%rbx
cmp    %r13,%rbx
jb     1170 <strstr+0x50>
jmp    1196 <strstr+0x76>
mov    %rbx,%r15
jmp    1196 <strstr+0x76>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @strstr(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
%10 = icmp eq i8* %9, null
br i1 %10, label %14, label %11
11: ; preds = %2
%12 = load i8*, i8** %5
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %15
14: ; preds = %11, %2
store i8* null, i8** %3
br label %45
15: ; preds = %11
%16 = load i8*, i8** %4
%17 = call i64 @strlen(i8* %16)
store i64 %17, i64* %7
%18 = load i8*, i8** %5
%19 = call i64 @strlen(i8* %18)
store i64 %19, i64* %8
%20 = load i64, i64* %8
%21 = icmp ult i64 %20, 1
br i1 %21, label %22, label %24
22: ; preds = %15
%23 = load i8*, i8** %4
store i8* %23, i8** %3
br label %45
24: ; preds = %15
%25 = load i8*, i8** %4
store i8* %25, i8** %6
br label %26
26: ; preds = %41, %24
%27 = load i8*, i8** %6
%28 = load i8*, i8** %4
%29 = load i64, i64* %7
%30 = getelementptr inbounds i8, i8* %28, i64 %29
%31 = icmp ult i8* %27, %30
br i1 %31, label %32, label %44
32: ; preds = %26
%33 = load i8*, i8** %6
%34 = load i8*, i8** %5
%35 = load i64, i64* %8
%36 = call i64 @strncmp(i8* %33, i8* %34, i64 %35)
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %32
%39 = load i8*, i8** %6
store i8* %39, i8** %3
br label %45
40: ; preds = %32
br label %41
41: ; preds = %40
%42 = load i8*, i8** %6
%43 = getelementptr inbounds i8, i8* %42, i32 1
store i8* %43, i8** %6
br label %26
44: ; preds = %26
store i8* null, i8** %3
br label %45
45: ; preds = %44, %38, %22, %14
%46 = load i8*, i8** %3
ret i8* %46
}

LLVM IR (-O2):
define i8* @strstr(i8* %0, i8* %1) {
%3 = icmp eq i8* %0, null
%4 = icmp eq i8* %1, null
%5 = or i1 %3, %4
br i1 %5, label %20, label %6
6: ; preds = %2
%7 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%8 = icmp eq i64 %7, 0
br i1 %8, label %20, label %9
9: ; preds = %6
%10 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%11 = getelementptr inbounds i8, i8* %0, i64 %10
%12 = icmp sgt i64 %10, 0
br i1 %12, label %13, label %20
13: ; preds = %9, %17
%14 = phi i8* [ %18, %17 ], [ %0, %9 ]
%15 = tail call i64 @strncmp(i8* %14, i8* %1, i64 %7)
%16 = icmp eq i64 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %13
%18 = getelementptr inbounds i8, i8* %14, i64 1
%19 = icmp ult i8* %18, %11
br i1 %19, label %13, label %20
20: ; preds = %13, %17, %9, %6, %2
%21 = phi i8* [ null, %2 ], [ %0, %6 ], [ null, %9 ], [ %14, %13 ], [ null, %17 ]
ret i8* %21
}