#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 const *, size_t);
size_t strlen(char const *);
char *memstr(char *src, int size, const char *key) {
  size_t len = strlen(key);
  while (size) {
    if (!memcmp(src, key, len))
      return src;
    src++;
    size--;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<memstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     118b <memstr+0x6b>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <memcmp@plt>
cmp    $0x0,%eax
jne    1171 <memstr+0x51>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1193 <memstr+0x73>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1140 <memstr+0x20>
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    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %rdx,%rdi
call   1030 <strlen@plt>
test   %ebp,%ebp
je     115b <memstr+0x3b>
mov    %rax,%r15
xchg   %ax,%ax
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <bcmp@plt>
test   %eax,%eax
je     115d <memstr+0x3d>
add    $0x1,%rbx
add    $0xffffffff,%ebp
jne    1140 <memstr+0x20>
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, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%9 = load i8*, i8** %7
%10 = call i64 @strlen(i8* %9)
store i64 %10, i64* %8
br label %11
11: ; preds = %22, %3
%12 = load i32, i32* %6
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %27
14: ; preds = %11
%15 = load i8*, i8** %5
%16 = load i8*, i8** %7
%17 = load i64, i64* %8
%18 = call i32 @memcmp(i8* %15, i8* %16, i64 %17)
%19 = icmp ne i32 %18, 0
br i1 %19, label %22, label %20
20: ; preds = %14
%21 = load i8*, i8** %5
store i8* %21, i8** %4
br label %28
22: ; preds = %14
%23 = load i8*, i8** %5
%24 = getelementptr inbounds i8, i8* %23, i32 1
store i8* %24, i8** %5
%25 = load i32, i32* %6
%26 = add nsw i32 %25, -1
store i32 %26, i32* %6
br label %11
27: ; preds = %11
store i8* null, i8** %4
br label %28
28: ; preds = %27, %20
%29 = load i8*, i8** %4
ret i8* %29
}

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