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


x86 assembly (clang, -O0):
<str_last_indexof>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    -0x18(%rbp),%ecx
cmp    %ecx,%eax
jg     11d4 <str_last_indexof+0xa4>
mov    -0x8(%rbp),%rdi
movslq -0x20(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    11c1 <str_last_indexof+0x91>
mov    -0x20(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    11c6 <str_last_indexof+0x96>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    118a <str_last_indexof+0x5a>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_last_indexof>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
xor    %edi,%edi
test   %r15,%r15
setne  %dil
call   1050 <assert@plt>
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1050 <assert@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %eax,%r13d
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    $0xffffffff,%ebp
sub    %eax,%r13d
jl     11ad <str_last_indexof+0x7d>
mov    %eax,%r12d
add    $0x1,%r13d
mov    $0xffffffff,%ebp
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
lea    (%r15,%rbx,1),%rdi
mov    %r14,%rsi
mov    %r12d,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
cmove  %ebx,%ebp
add    $0x1,%rbx
cmp    %rbx,%r13
jne    1190 <str_last_indexof+0x60>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @str_last_indexof(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%9 = load i8*, i8** %3
%10 = icmp ne i8* %9, null
%11 = zext i1 %10 to i32
%12 = call i32 @assert(i32 %11)
%13 = load i8*, i8** %4
%14 = icmp ne i8* %13, null
%15 = zext i1 %14 to i32
%16 = call i32 @assert(i32 %15)
%17 = load i8*, i8** %3
%18 = call i32 @strlen(i8* %17)
store i32 %18, i32* %5
%19 = load i8*, i8** %4
%20 = call i32 @strlen(i8* %19)
store i32 %20, i32* %6
store i32 -1, i32* %7
store i32 0, i32* %8
br label %21
21: ; preds = %39, %2
%22 = load i32, i32* %8
%23 = load i32, i32* %5
%24 = load i32, i32* %6
%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** %3
%29 = load i32, i32* %8
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
%32 = load i8*, i8** %4
%33 = load i32, i32* %6
%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* %7
br label %38
38: ; preds = %36, %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
%43 = load i32, i32* %7
ret i32 %43
}

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