#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);
  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    %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
store i32 0, i32* %8
br label %23
23: ; preds = %41, %2
%24 = load i32, i32* %8
%25 = load i32, i32* %6
%26 = load i32, i32* %7
%27 = sub nsw i32 %25, %26
%28 = icmp sle i32 %24, %27
br i1 %28, label %29, label %44
29: ; preds = %23
%30 = load i8*, i8** %4
%31 = load i32, i32* %8
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
%34 = load i8*, i8** %5
%35 = load i32, i32* %7
%36 = call i32 @strncmp(i8* %33, i8* %34, i32 %35)
%37 = icmp ne i32 %36, 0
br i1 %37, label %40, label %38
38: ; preds = %29
%39 = load i32, i32* %8
store i32 %39, i32* %3
br label %45
40: ; preds = %29
br label %41
41: ; preds = %40
%42 = load i32, i32* %8
%43 = add nsw i32 %42, 1
store i32 %43, i32* %8
br label %23
44: ; preds = %23
store i32 -1, i32* %3
br label %45
45: ; preds = %44, %38
%46 = load i32, i32* %3
ret i32 %46
}

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
}