#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 */
char *strchr(int /*<<< orphan*/, char const);
int strlen(char const *);
scalar_t__ strncmp(char *, char const *, size_t);
int last_index_of(const char *src, const char *need) {
  int i;
  const char *p = src + strlen(src);
  size_t len = strlen(need);
  char *buf;
  for (i = 0; i < strlen(src); i++) {
    buf = strchr(p--, *need);
    if (!buf) {
      continue;
    }
    if (strncmp(buf, need, len) == 0) {
      return strlen(src) - strlen((char *)buf);
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<last_index_of>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
cltq
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x44(%rbp),%eax
cmp    %ecx,%eax
jge    1216 <last_index_of+0xe6>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x28(%rbp)
mov    %eax,%edi
mov    -0x18(%rbp),%rax
movsbl (%rax),%esi
call   1050 <strchr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11c4 <last_index_of+0x94>
jmp    1208 <last_index_of+0xd8>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1203 <last_index_of+0xd3>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x48(%rbp)
mov    -0x38(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    121d <last_index_of+0xed>
jmp    1208 <last_index_of+0xd8>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1177 <last_index_of+0x47>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<last_index_of>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r13
mov    %rdi,%r14
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %r13,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    $0xffffffff,%r15d
test   %eax,%eax
jle    11c9 <last_index_of+0x99>
movslq %ebx,%r12
add    %r14d,%ebp
xor    %r15d,%r15d
jmp    1184 <last_index_of+0x54>
nop
add    $0x1,%r15d
mov    %r14,%rdi
call   1040 <strlen@plt>
add    $0xffffffff,%ebp
cmp    %eax,%r15d
jge    11c3 <last_index_of+0x93>
movsbl 0x0(%r13),%esi
mov    %ebp,%edi
call   1050 <strchr@plt>
test   %rax,%rax
je     1170 <last_index_of+0x40>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r13,%rsi
mov    %r12,%rdx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    1170 <last_index_of+0x40>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%r15d
mov    %rbx,%rdi
call   1040 <strlen@plt>
sub    %eax,%r15d
jmp    11c9 <last_index_of+0x99>
mov    $0xffffffff,%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 @last_index_of(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = load i8*, i8** %4
%11 = load i8*, i8** %4
%12 = call i32 @strlen(i8* %11)
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %10, i64 %13
store i8* %14, i8** %7
%15 = load i8*, i8** %5
%16 = call i32 @strlen(i8* %15)
%17 = sext i32 %16 to i64
store i64 %17, i64* %8
store i32 0, i32* %6
br label %18
18: ; preds = %46, %2
%19 = load i32, i32* %6
%20 = load i8*, i8** %4
%21 = call i32 @strlen(i8* %20)
%22 = icmp slt i32 %19, %21
br i1 %22, label %23, label %49
23: ; preds = %18
%24 = load i8*, i8** %7
%25 = getelementptr inbounds i8, i8* %24, i32 -1
store i8* %25, i8** %7
%26 = ptrtoint i8* %24 to i32
%27 = load i8*, i8** %5
%28 = load i8, i8* %27
%29 = call i8* @strchr(i32 %26, i8 signext %28)
store i8* %29, i8** %9
%30 = load i8*, i8** %9
%31 = icmp ne i8* %30, null
br i1 %31, label %33, label %32
32: ; preds = %23
br label %46
33: ; preds = %23
%34 = load i8*, i8** %9
%35 = load i8*, i8** %5
%36 = load i64, i64* %8
%37 = call i64 @strncmp(i8* %34, i8* %35, i64 %36)
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %45
39: ; preds = %33
%40 = load i8*, i8** %4
%41 = call i32 @strlen(i8* %40)
%42 = load i8*, i8** %9
%43 = call i32 @strlen(i8* %42)
%44 = sub nsw i32 %41, %43
store i32 %44, i32* %3
br label %50
45: ; preds = %33
br label %46
46: ; preds = %45, %32
%47 = load i32, i32* %6
%48 = add nsw i32 %47, 1
store i32 %48, i32* %6
br label %18
49: ; preds = %18
store i32 -1, i32* %3
br label %50
50: ; preds = %49, %39
%51 = load i32, i32* %3
ret i32 %51
}

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