#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 strlen(char const *);
scalar_t__ strncmp(char const *, char const *, int);
char *find_str(const char *haystack, int hsize, const char *needle) {
  int i = 0;
  int nsize = strlen(needle);
  hsize -= nsize - 1;
  for (i = 0; i < hsize; i++) {
    if (strncmp(haystack + i, needle, nsize) == 0) {
      return (char *)(haystack + i);
    }
  }
  return NULL;
}


x86 assembly (clang, -O0):
<find_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%ecx
sub    $0x1,%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11af <find_str+0x8f>
mov    -0x10(%rbp),%rdi
movslq -0x24(%rbp),%rax
add    %rax,%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    119c <find_str+0x7c>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    11b7 <find_str+0x97>
jmp    11a1 <find_str+0x81>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    115b <find_str+0x3b>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_str>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %esi,%r15d
mov    %rdi,%rbx
mov    %rdx,%rdi
call   1040 <strlen@plt>
sub    %eax,%r15d
add    $0x1,%r15d
test   %r15d,%r15d
jle    116d <find_str+0x4d>
mov    %eax,%r12d
mov    %r15d,%ebp
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r12d,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     116f <find_str+0x4f>
add    $0x1,%rbx
add    $0xffffffffffffffff,%rbp
jne    1150 <find_str+0x30>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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