#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 *sstrnstr(char *haystack, const char *needle, size_t length) {
  size_t needle_length = strlen(needle);
  size_t i;
  for (i = 0; i < length; i++) {
    if (i + needle_length > length) {
      return NULL;
    }
    if (strncmp(&haystack[i], needle, needle_length) == 0) {
      return &haystack[i];
    }
  }
  return NULL;
}


x86 assembly (clang, -O0):
<sstrnstr>:
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   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    11bc <sstrnstr+0x9c>
mov    -0x30(%rbp),%rax
add    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jbe    1176 <sstrnstr+0x56>
movq   $0x0,-0x8(%rbp)
jmp    11c4 <sstrnstr+0xa4>
mov    -0x10(%rbp),%rdi
add    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    11a6 <sstrnstr+0x86>
mov    -0x10(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11c4 <sstrnstr+0xa4>
jmp    11ab <sstrnstr+0x8b>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1149 <sstrnstr+0x29>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sstrnstr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r13
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsi,%rdi
call   1040 <strlen@plt>
test   %r13,%r13
je     118f <sstrnstr+0x6f>
mov    %rax,%rbx
xor    %eax,%eax
mov    %rax,(%rsp)
cmp    %r13,%rbx
ja     119b <sstrnstr+0x7b>
mov    $0x1,%r12d
xor    %eax,%eax
mov    %rax,(%rsp)
nopl   0x0(%rax,%rax,1)
lea    (%r15,%r12,1),%rbp
add    $0xffffffffffffffff,%rbp
mov    %rbp,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <strncmp@plt>
test   %rax,%rax
je     1197 <sstrnstr+0x77>
cmp    %r13,%r12
jae    119b <sstrnstr+0x7b>
lea    (%rbx,%r12,1),%rax
add    $0x1,%r12
cmp    %r13,%rax
jbe    1160 <sstrnstr+0x40>
jmp    119b <sstrnstr+0x7b>
xor    %eax,%eax
mov    %rax,(%rsp)
jmp    119b <sstrnstr+0x7b>
mov    %rbp,(%rsp)
mov    (%rsp),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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