#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*/ memcmp(char const *, char const *, size_t);
size_t strlen(char const *);
char *strnstr(const char *string1, const char *string2, size_t len) {
  size_t length2;
  length2 = strlen(string2);
  if (!length2) {
    return (char *)string1;
  }
  while (len >= length2) {
    len--;
    if (!memcmp(string1, string2, length2))
      return (char *)string1;
    string1++;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<strnstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1159 <strnstr+0x39>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11b8 <strnstr+0x98>
jmp    115e <strnstr+0x3e>
mov    -0x20(%rbp),%rax
cmp    -0x28(%rbp),%rax
jb     11b0 <strnstr+0x90>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <memcmp@plt>
cmp    $0x0,%eax
jne    119f <strnstr+0x7f>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11b8 <strnstr+0x98>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    115e <strnstr+0x3e>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strnstr>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsi,%rdi
call   1030 <strlen@plt>
test   %rax,%rax
je     1172 <strnstr+0x52>
mov    %rax,%r12
cmp    %rbx,%rax
ja     116f <strnstr+0x4f>
cs nopw 0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1040 <bcmp@plt>
test   %eax,%eax
je     1172 <strnstr+0x52>
add    $0xffffffffffffffff,%rbx
add    $0x1,%r14
cmp    %r12,%rbx
jae    1150 <strnstr+0x30>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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