#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_last_indexof(const char *haystack, const char *needle) {
  assert(haystack != NULL);
  assert(needle != NULL);
  int hlen = (int)strlen(haystack);
  int nlen = (int)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    %rax,%r12
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rax,%r13
mov    $0xffffffff,%ebp
cmp    %r13d,%r12d
jl     11ad <str_last_indexof+0x7d>
sub    %r13d,%r12d
add    $0x1,%r12d
mov    $0xffffffff,%ebp
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
lea    (%r15,%rbx,1),%rdi
mov    %r14,%rsi
mov    %r13d,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
cmove  %ebx,%ebp
add    $0x1,%rbx
cmp    %rbx,%r12
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 i64 @strlen(i8* %17)
%19 = trunc i64 %18 to i32
store i32 %19, i32* %5
%20 = load i8*, i8** %4
%21 = call i64 @strlen(i8* %20)
%22 = trunc i64 %21 to i32
store i32 %22, i32* %6
store i32 -1, i32* %7
store i32 0, i32* %8
br label %23
23: ; preds = %41, %2
%24 = load i32, i32* %8
%25 = load i32, i32* %5
%26 = load i32, i32* %6
%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** %3
%31 = load i32, i32* %8
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
%34 = load i8*, i8** %4
%35 = load i32, i32* %6
%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* %7
br label %40
40: ; preds = %38, %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
%45 = load i32, i32* %7
ret i32 %45
}

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