#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*/ assert(int /*<<< orphan*/);
scalar_t__ strlen(char const *);
int /*<<< orphan*/ strncmp(char const *, char const *, int);
int str_indexof(const char *haystack, const char *needle) {
  assert(haystack != NULL);
  assert(needle != NULL);
  int hlen = (int)strlen(haystack);
  int nlen = (int)strlen(needle);
  if (nlen > hlen)
    return -1;
  for (int i = 0; i <= hlen - nlen; i++) {
    if (!strncmp(&haystack[i], needle, nlen))
      return i;
  }
  return -1;
}


x86 assembly (clang, -O0):
<str_indexof>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
cmpq   $0x0,-0x18(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jle    1194 <str_indexof+0x64>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f1 <str_indexof+0xc1>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x1c(%rbp),%ecx
sub    -0x20(%rbp),%ecx
cmp    %ecx,%eax
jg     11ea <str_indexof+0xba>
mov    -0x10(%rbp),%rdi
movslq -0x24(%rbp),%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    11d7 <str_indexof+0xa7>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f1 <str_indexof+0xc1>
jmp    11dc <str_indexof+0xac>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    119b <str_indexof+0x6b>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_indexof>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
xor    %edi,%edi
test   %r12,%r12
setne  %dil
call   1050 <assert@plt>
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1050 <assert@plt>
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbp
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rax,%r13
mov    $0xffffffff,%r15d
cmp    %r13d,%ebp
jl     11b1 <str_indexof+0x81>
sub    %r13d,%ebp
add    $0x1,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
lea    (%r12,%rbx,1),%rdi
mov    %r14,%rsi
mov    %r13d,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
je     11ae <str_indexof+0x7e>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1190 <str_indexof+0x60>
jmp    11b1 <str_indexof+0x81>
mov    %ebx,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @str_indexof(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
%10 = icmp ne i8* %9, null
%11 = zext i1 %10 to i32
%12 = call i32 @assert(i32 %11)
%13 = load i8*, i8** %5
%14 = icmp ne i8* %13, null
%15 = zext i1 %14 to i32
%16 = call i32 @assert(i32 %15)
%17 = load i8*, i8** %4
%18 = call i64 @strlen(i8* %17)
%19 = trunc i64 %18 to i32
store i32 %19, i32* %6
%20 = load i8*, i8** %5
%21 = call i64 @strlen(i8* %20)
%22 = trunc i64 %21 to i32
store i32 %22, i32* %7
%23 = load i32, i32* %7
%24 = load i32, i32* %6
%25 = icmp sgt i32 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %2
store i32 -1, i32* %3
br label %50
27: ; preds = %2
store i32 0, i32* %8
br label %28
28: ; preds = %46, %27
%29 = load i32, i32* %8
%30 = load i32, i32* %6
%31 = load i32, i32* %7
%32 = sub nsw i32 %30, %31
%33 = icmp sle i32 %29, %32
br i1 %33, label %34, label %49
34: ; preds = %28
%35 = load i8*, i8** %4
%36 = load i32, i32* %8
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
%39 = load i8*, i8** %5
%40 = load i32, i32* %7
%41 = call i32 @strncmp(i8* %38, i8* %39, i32 %40)
%42 = icmp ne i32 %41, 0
br i1 %42, label %45, label %43
43: ; preds = %34
%44 = load i32, i32* %8
store i32 %44, i32* %3
br label %50
45: ; preds = %34
br label %46
46: ; preds = %45
%47 = load i32, i32* %8
%48 = add nsw i32 %47, 1
store i32 %48, i32* %8
br label %28
49: ; preds = %28
store i32 -1, i32* %3
br label %50
50: ; preds = %49, %43, %26
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
define i32 @str_indexof(i8* %0, i8* %1) {
%3 = icmp ne i8* %0, null
%4 = zext i1 %3 to i32
%5 = tail call i32 @assert(i32 %4)
%6 = icmp ne i8* %1, null
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%10 = trunc i64 %9 to i32
%11 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%12 = trunc i64 %11 to i32
%13 = icmp slt i32 %10, %12
br i1 %13, label %28, label %14
14: ; preds = %2
%15 = add i64 %9, 1
%16 = sub i64 %15, %11
%17 = and i64 %16, 4294967295
br label %18
18: ; preds = %14, %23
%19 = phi i64 [ 0, %14 ], [ %24, %23 ]
%20 = getelementptr inbounds i8, i8* %0, i64 %19
%21 = tail call i32 @strncmp(i8* %20, i8* %1, i32 %12)
%22 = icmp eq i32 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %18
%24 = add nuw nsw i64 %19, 1
%25 = icmp eq i64 %24, %17
br i1 %25, label %28, label %18
26: ; preds = %18
%27 = trunc i64 %19 to i32
br label %28
28: ; preds = %23, %26, %2
%29 = phi i32 [ -1, %2 ], [ %27, %26 ], [ -1, %23 ]
ret i32 %29
}