#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*/ memcpy(char *, char const *, size_t);
char *strstr(char *, char const *);
char *strnstr(const char *s1, const char *s2, size_t n) {
  char tmp[n], *p;
  memcpy(tmp, s1, n);
  tmp[n] = '\0';
  if ((p = strstr(tmp, s2)) == NULL)
    return NULL;
  return (char *)&s1[p - tmp];
}


x86 assembly (clang, -O0):
<strnstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x48(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x48(%rbp),%rdi
mov    -0x20(%rbp),%rax
movb   $0x0,(%rdi,%rax,1)
mov    -0x18(%rbp),%rsi
call   1040 <strstr@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    119f <strnstr+0x7f>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    11bc <strnstr+0x9c>
mov    -0x48(%rbp),%rdx
mov    -0x10(%rbp),%rax
mov    -0x38(%rbp),%rcx
sub    %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strnstr>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsp,%rbx
lea    0xf(%rdx),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
mov    %r15,%rsi
call   1030 <memcpy@plt>
movb   $0x0,(%r12,%rbx,1)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <strstr@plt>
mov    %rax,%rcx
sub    %rbx,%rcx
add    %r15,%rcx
test   %rax,%rax
cmovne %rcx,%rax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
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 i8*
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%12 = load i64, i64* %7
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %8
%14 = alloca i8, i64 %12
store i64 %12, i64* %9
%15 = load i8*, i8** %5
%16 = load i64, i64* %7
%17 = call i32 @memcpy(i8* %14, i8* %15, i64 %16)
%18 = load i64, i64* %7
%19 = getelementptr inbounds i8, i8* %14, i64 %18
store i8 0, i8* %19
%20 = load i8*, i8** %6
%21 = call i8* @strstr(i8* %14, i8* %20)
store i8* %21, i8** %10
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %24
23: ; preds = %3
store i8* null, i8** %4
store i32 1, i32* %11
br label %31
24: ; preds = %3
%25 = load i8*, i8** %5
%26 = load i8*, i8** %10
%27 = ptrtoint i8* %26 to i64
%28 = ptrtoint i8* %14 to i64
%29 = sub i64 %27, %28
%30 = getelementptr inbounds i8, i8* %25, i64 %29
store i8* %30, i8** %4
store i32 1, i32* %11
br label %31
31: ; preds = %24, %23
%32 = load i8*, i8** %8
%33 = load i8*, i8** %4
ret i8* %33
}

LLVM IR (-O2):
define i8* @strnstr(i8* %0, i8* nocapture %1, i64 %2) {
%4 = alloca i8, i64 %2
%5 = call i32 @memcpy(i8* %4, i8* %0, i64 %2)
%6 = getelementptr inbounds i8, i8* %4, i64 %2
store i8 0, i8* %6
%7 = call i8* @strstr(i8* %4, i8* dereferenceable(1) %1)
%8 = icmp eq i8* %7, null
%9 = ptrtoint i8* %7 to i64
%10 = ptrtoint i8* %4 to i64
%11 = sub i64 %9, %10
%12 = getelementptr inbounds i8, i8* %0, i64 %11
%13 = select i1 %8, i8* null, i8* %12
ret i8* %13
}