#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 const *, char const *, size_t const);
extern char *strstr(const char *str, const char *substr) {
  const size_t length = strlen(substr);
  const char *match = NULL;
  const char *p;
  for (p = str; *p != '\0' && match == NULL; ++p)
    if (strncmp(p, substr, length) == 0)
      match = p;
  return (char *)match;
}


x86 assembly (clang, -O0):
<strstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x29(%rbp)
je     116d <strstr+0x4d>
cmpq   $0x0,-0x20(%rbp)
sete   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    117d <strstr+0x5d>
jmp    11b6 <strstr+0x96>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    11a0 <strstr+0x80>
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    11a5 <strstr+0x85>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    114d <strstr+0x2d>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strstr>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsi,%rdi
call   1040 <strlen@plt>
cmpb   $0x0,(%rbx)
je     116f <strstr+0x4f>
mov    %rax,%r15
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1030 <strncmp@plt>
lea    0x1(%rbx),%rdx
test   %rax,%rax
je     1160 <strstr+0x40>
cmpb   $0x0,0x1(%rbx)
mov    %rdx,%rbx
jne    1140 <strstr+0x20>
add    $0xffffffffffffffff,%rdx
xor    %ecx,%ecx
test   %rax,%rax
cmove  %rdx,%rcx
jmp    1171 <strstr+0x51>
xor    %ecx,%ecx
mov    %rcx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @strstr(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%8 = load i8*, i8** %4
%9 = call i64 @strlen(i8* %8)
store i64 %9, i64* %5
store i8* null, i8** %6
%10 = load i8*, i8** %3
store i8* %10, i8** %7
br label %11
11: ; preds = %30, %2
%12 = load i8*, i8** %7
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = load i8*, i8** %6
%18 = icmp eq i8* %17, null
br label %19
19: ; preds = %16, %11
%20 = phi i1 [ false, %11 ], [ %18, %16 ]
br i1 %20, label %21, label %33
21: ; preds = %19
%22 = load i8*, i8** %7
%23 = load i8*, i8** %4
%24 = load i64, i64* %5
%25 = call i64 @strncmp(i8* %22, i8* %23, i64 %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %21
%28 = load i8*, i8** %7
store i8* %28, i8** %6
br label %29
29: ; preds = %27, %21
br label %30
30: ; preds = %29
%31 = load i8*, i8** %7
%32 = getelementptr inbounds i8, i8* %31, i32 1
store i8* %32, i8** %7
br label %11
33: ; preds = %19
%34 = load i8*, i8** %6
ret i8* %34
}

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