#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 *, char *, int);
int strlen(char *);
char *memstr(char *haystack, char *needle, int hlen) {
  int i, l = strlen(needle) + 1;
  for (i = 0; i <= hlen - l; i++) {
    if (!memcmp(haystack, needle, l))
      return haystack;
    haystack++;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<memstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    -0x1c(%rbp),%ecx
sub    -0x24(%rbp),%ecx
cmp    %ecx,%eax
jg     119a <memstr+0x7a>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1040 <memcmp@plt>
cmp    $0x0,%eax
jne    1180 <memstr+0x60>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11a2 <memstr+0x82>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1149 <memstr+0x29>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<memstr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsi,%rdi
call   1030 <strlen@plt>
cmp    %ebp,%eax
jge    116b <memstr+0x4b>
mov    %eax,%r15d
sub    %eax,%ebp
add    $0x1,%r15d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15d,%edx
call   1040 <memcmp@plt>
test   %eax,%eax
je     116d <memstr+0x4d>
add    $0x1,%rbx
add    $0xffffffff,%ebp
jne    1150 <memstr+0x30>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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