#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*/);
int 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 = strlen(haystack);
  int nlen = strlen(needle);
  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)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x1c(%rbp),%ecx
sub    -0x20(%rbp),%ecx
cmp    %ecx,%eax
jg     11d2 <str_indexof+0xa2>
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    11bf <str_indexof+0x8f>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d9 <str_indexof+0xa9>
jmp    11c4 <str_indexof+0x94>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1183 <str_indexof+0x53>
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    %eax,%ebp
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    $0xffffffff,%r15d
sub    %eax,%ebp
jl     11b1 <str_indexof+0x81>
mov    %eax,%r13d
add    $0x1,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   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 i32 @strlen(i8* %17)
store i32 %18, i32* %6
%19 = load i8*, i8** %5
%20 = call i32 @strlen(i8* %19)
store i32 %20, i32* %7
store i32 0, i32* %8
br label %21
21: ; preds = %39, %2
%22 = load i32, i32* %8
%23 = load i32, i32* %6
%24 = load i32, i32* %7
%25 = sub nsw i32 %23, %24
%26 = icmp sle i32 %22, %25
br i1 %26, label %27, label %42
27: ; preds = %21
%28 = load i8*, i8** %4
%29 = load i32, i32* %8
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
%32 = load i8*, i8** %5
%33 = load i32, i32* %7
%34 = call i32 @strncmp(i8* %31, i8* %32, i32 %33)
%35 = icmp ne i32 %34, 0
br i1 %35, label %38, label %36
36: ; preds = %27
%37 = load i32, i32* %8
store i32 %37, i32* %3
br label %43
38: ; preds = %27
br label %39
39: ; preds = %38
%40 = load i32, i32* %8
%41 = add nsw i32 %40, 1
store i32 %41, i32* %8
br label %21
42: ; preds = %21
store i32 -1, i32* %3
br label %43
43: ; preds = %42, %36
%44 = load i32, i32* %3
ret i32 %44
}

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 i32 @strlen(i8* %0)
%10 = tail call i32 @strlen(i8* %1)
%11 = icmp slt i32 %9, %10
br i1 %11, label %26, label %12
12: ; preds = %2
%13 = add i32 %9, 1
%14 = sub i32 %13, %10
%15 = zext i32 %14 to i64
br label %16
16: ; preds = %12, %21
%17 = phi i64 [ 0, %12 ], [ %22, %21 ]
%18 = getelementptr inbounds i8, i8* %0, i64 %17
%19 = tail call i32 @strncmp(i8* %18, i8* %1, i32 %10)
%20 = icmp eq i32 %19, 0
br i1 %20, label %24, label %21
21: ; preds = %16
%22 = add nuw nsw i64 %17, 1
%23 = icmp eq i64 %22, %15
br i1 %23, label %26, label %16
24: ; preds = %16
%25 = trunc i64 %17 to i32
br label %26
26: ; preds = %21, %24, %2
%27 = phi i32 [ -1, %2 ], [ %25, %24 ], [ -1, %21 ]
ret i32 %27
}