#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 *);
scalar_t__ strncmp(char *, char *, int);
char *findstr(char *haystack, char *needle, int size) {
  int n = 0;
  int len = strlen(needle);
  while ((n + len) < size) {
    if (strncmp(haystack + n, needle, len) == 0)
      return haystack + n;
    n++;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<findstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%eax
add    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1198 <findstr+0x78>
mov    -0x10(%rbp),%rdi
movslq -0x20(%rbp),%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    118a <findstr+0x6a>
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    11a0 <findstr+0x80>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1146 <findstr+0x26>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<findstr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbx
mov    %rsi,%rdi
call   1040 <strlen@plt>
cmp    %r14d,%eax
jge    116c <findstr+0x4c>
mov    %eax,%ebp
sub    %eax,%r14d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     116e <findstr+0x4e>
add    $0x1,%rbx
add    $0xffffffffffffffff,%r14
jne    1150 <findstr+0x30>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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