#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*/ starts_with(char const *, int, char const *);
int find(const char *what, const char *buf, int maxlen) {
  int i = 0;
  while (!starts_with(&buf[i], maxlen - i, what)) {
    i++;
    if (i == maxlen)
      return -1;
  }
  return i;
}


x86 assembly (clang, -O0):
<find>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
movslq -0x20(%rbp),%rax
add    %rax,%rdi
mov    -0x1c(%rbp),%esi
sub    -0x20(%rbp),%esi
mov    -0x10(%rbp),%rdx
call   1030 <starts_with@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1159 <find+0x49>
jmp    117f <find+0x6f>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jne    117a <find+0x6a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1185 <find+0x75>
jmp    112a <find+0x1a>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    %edx,%ebx
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    (%r14,%r12,1),%rdi
mov    %ebx,%esi
mov    %r15,%rdx
call   1030 <starts_with@plt>
test   %eax,%eax
jne    1152 <find+0x42>
add    $0x1,%r12
add    $0xffffffffffffffff,%rbx
jne    1130 <find+0x20>
mov    $0xffffffff,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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